cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bows...@apache.org
Subject [2/3] cordova-plugin-media git commit: Adding the media permissions code
Date Wed, 28 Oct 2015 19:01:52 GMT
Adding the media permissions code


Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-media/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugin-media/commit/c4547c94
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-media/tree/c4547c94
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-media/diff/c4547c94

Branch: refs/heads/master
Commit: c4547c94c5850c619b0936551ec1180f4465a95b
Parents: ec1e9ba
Author: Joe Bowser <bowserj@apache.org>
Authored: Thu Sep 24 15:46:24 2015 -0700
Committer: Joe Bowser <bowserj@apache.org>
Committed: Wed Oct 28 12:01:42 2015 -0700

----------------------------------------------------------------------
 src/android/AudioHandler.java | 88 +++++++++++++++++++++++++++++++++++++-
 1 file changed, 86 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-media/blob/c4547c94/src/android/AudioHandler.java
----------------------------------------------------------------------
diff --git a/src/android/AudioHandler.java b/src/android/AudioHandler.java
index f06b75a..49031c6 100644
--- a/src/android/AudioHandler.java
+++ b/src/android/AudioHandler.java
@@ -22,11 +22,15 @@ import org.apache.cordova.CallbackContext;
 import org.apache.cordova.CordovaPlugin;
 import org.apache.cordova.CordovaResourceApi;
 
+import android.Manifest;
 import android.content.Context;
+import android.content.pm.PackageManager;
 import android.media.AudioManager;
 import android.net.Uri;
+import android.os.Build;
 import android.util.Log;
 
+import java.security.Permission;
 import java.util.ArrayList;
 
 import org.apache.cordova.PluginResult;
@@ -55,6 +59,12 @@ public class AudioHandler extends CordovaPlugin {
     private int origVolumeStream = -1;
     private CallbackContext messageChannel;
 
+
+    public static final int PERMISSION_DENIED_ERROR = 20;
+
+    private String recordId;
+    private String fileUriStr;
+
     /**
      * Constructor.
      */
@@ -63,6 +73,41 @@ public class AudioHandler extends CordovaPlugin {
         this.pausedForPhone = new ArrayList<AudioPlayer>();
     }
 
+    protected int checkWritePermission()
+    {
+        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
+        {
+            return cordova.getActivity().checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
+        }
+        else
+        {
+            return PackageManager.PERMISSION_GRANTED;
+        }
+    }
+
+    protected void getWritePermission(int requestCode)
+    {
+        cordova.requestPermission(this, requestCode, Manifest.permission.WRITE_EXTERNAL_STORAGE);
+    }
+
+    protected int checkMicrophone()
+    {
+        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
+        {
+            return cordova.getActivity().checkSelfPermission(Manifest.permission.RECORD_AUDIO);
+        }
+        else
+        {
+            return PackageManager.PERMISSION_GRANTED;
+        }
+    }
+
+    protected void getMicPermission(int requestCode)
+    {
+        cordova.requestPermission(this, requestCode, Manifest.permission.RECORD_AUDIO);
+    }
+
+
     /**
      * Executes the request and returns PluginResult.
      * @param action 		The action to execute.
@@ -73,18 +118,19 @@ public class AudioHandler extends CordovaPlugin {
     public boolean execute(String action, JSONArray args, CallbackContext callbackContext)
throws JSONException {
         CordovaResourceApi resourceApi = webView.getResourceApi();
         PluginResult.Status status = PluginResult.Status.OK;
+        messageChannel = callbackContext;
         String result = "";
 
         if (action.equals("startRecordingAudio")) {
+            recordId = args.getString(0);
             String target = args.getString(1);
-            String fileUriStr;
             try {
                 Uri targetUri = resourceApi.remapUri(Uri.parse(target));
                 fileUriStr = targetUri.toString();
             } catch (IllegalArgumentException e) {
                 fileUriStr = target;
             }
-            this.startRecordingAudio(args.getString(0), FileHelper.stripFileProtocol(fileUriStr));
+            promptForRecord();
         }
         else if (action.equals("stopRecordingAudio")) {
             this.stopRecordingAudio(args.getString(0));
@@ -407,4 +453,42 @@ public class AudioHandler extends CordovaPlugin {
             messageChannel.sendPluginResult(pluginResult);
         }
     }
+
+    public void onRequestPermissionResult(int requestCode, String[] permissions,
+                                          int[] grantResults) throws JSONException
+    {
+        for(int r:grantResults)
+        {
+            if(r == PackageManager.PERMISSION_DENIED)
+            {
+                this.messageChannel.sendPluginResult(new PluginResult(PluginResult.Status.ERROR,
PERMISSION_DENIED_ERROR));
+                return;
+            }
+        }
+        promptForRecord();
+    }
+
+    /*
+     * This little utility method catch-all work great for multi-permission stuff.
+     *
+     */
+
+    private void promptForRecord()
+    {
+        if(checkWritePermission() == PackageManager.PERMISSION_GRANTED  &&
+                checkMicrophone() == PackageManager.PERMISSION_GRANTED) {
+            this.startRecordingAudio(recordId, FileHelper.stripFileProtocol(fileUriStr));
+        }
+        else if(checkMicrophone() == PackageManager.PERMISSION_GRANTED)
+        {
+            getWritePermission(0);
+        }
+        else
+        {
+            getMicPermission(0);
+        }
+
+    }
+
+
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org


Mime
View raw message