Skip to content

Commit

Permalink
Merge pull request #28 from jrschifa/master
Browse files Browse the repository at this point in the history
added runtime permission handling
  • Loading branch information
sarriaroman committed May 5, 2016
2 parents cfd3530 + b75e528 commit 92bd4ed
Showing 1 changed file with 51 additions and 8 deletions.
59 changes: 51 additions & 8 deletions src/android/PhotoViewer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;

/**
* Class to Open PhotoViewer with the Required Parameters from Cordova
Expand All @@ -15,22 +18,62 @@
*/
public class PhotoViewer extends CordovaPlugin {

public static final int PERMISSION_DENIED_ERROR = 20;

public static final String WRITE = Manifest.permission.WRITE_EXTERNAL_STORAGE;
public static final String READ = Manifest.permission.READ_EXTERNAL_STORAGE;

public static final int REQ_CODE = 0;

protected JSONArray args;
protected CallbackContext callbackContext;

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
if (action.equals("show")) {
this.args = args;
this.callbackContext = callbackContext;

Intent i = new Intent(this.cordova.getActivity(), com.sarriaroman.PhotoViewer.PhotoActivity.class);
if (cordova.hasPermission(READ) && cordova.hasPermission(WRITE)) {
this.launchActivity();
} else {
this.getPermission();
}
return true;
}
return false;
}

i.putExtra("url", args.getString(0));
i.putExtra("title", args.getString(1));
i.putExtra("options", args.optJSONObject(2).toString());
protected void getPermission() {
cordova.requestPermissions(this, REQ_CODE, new String[]{WRITE, READ});
}

this.cordova.getActivity().startActivity(i);
protected void launchActivity() throws JSONException {
Intent i = new Intent(this.cordova.getActivity(), com.sarriaroman.PhotoViewer.PhotoActivity.class);

callbackContext.success("");
i.putExtra("url", this.args.getString(0));
i.putExtra("title", this.args.getString(1));
i.putExtra("options", this.args.optJSONObject(2).toString());

return true;
this.cordova.getActivity().startActivity(i);
this.callbackContext.success("");
}

@Override
public void onRequestPermissionResult(int requestCode, String[] permissions,
int[] grantResults) throws JSONException {
for(int r:grantResults) {
if(r == PackageManager.PERMISSION_DENIED) {
this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, PERMISSION_DENIED_ERROR));
return;
}
}
return false;

switch(requestCode) {
case REQ_CODE:
launchActivity();
break;
}

}
}

0 comments on commit 92bd4ed

Please sign in to comment.