Bug 1337692 - Ask for permission on input=file/accept. r=sebastian, r=grisha, a=lizzard
authorcnevinchen@gmail.com
Mon, 27 Feb 2017 21:02:00 -0600
changeset 376496 65c9d527fc8c73f764ad481ea309dd7d6d3450bc
parent 376495 a50fdc5eebdf5c6abcf23bf7000cf56f2a8d567d
child 376497 56b6481b93fdcbfe965a5173e24ec399aa71d04c
push id6996
push userjlorenzo@mozilla.com
push dateMon, 06 Mar 2017 20:48:21 +0000
treeherdermozilla-beta@d89512dab048 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssebastian, grisha, lizzard
bugs1337692
milestone53.0a2
Bug 1337692 - Ask for permission on input=file/accept. r=sebastian, r=grisha, a=lizzard
mobile/android/base/java/org/mozilla/gecko/FilePicker.java
--- a/mobile/android/base/java/org/mozilla/gecko/FilePicker.java
+++ b/mobile/android/base/java/org/mozilla/gecko/FilePicker.java
@@ -1,19 +1,21 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.gecko;
 
 import org.mozilla.gecko.GeckoAppShell;
+import org.mozilla.gecko.permissions.Permissions;
 import org.mozilla.gecko.util.BundleEventListener;
 import org.mozilla.gecko.util.EventCallback;
 import org.mozilla.gecko.util.GeckoBundle;
 
+import android.Manifest;
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
 import android.content.pm.PackageManager;
 import android.content.pm.ResolveInfo;
 import android.net.Uri;
 import android.os.Environment;
 import android.os.Parcelable;
@@ -56,25 +58,52 @@ public class FilePicker implements Bundl
             final String title = message.getString("title");
 
             if ("mimeType".equals(mode)) {
                 mimeType = message.getString("mimeType");
             } else if ("extension".equals(mode)) {
                 mimeType = GeckoAppShell.getMimeTypeFromExtensions(message.getString("extensions"));
             }
 
-            showFilePickerAsync(title, mimeType, new ResultHandler() {
-                @Override
-                public void gotFile(final String filename) {
-                    callback.sendSuccess(filename);
-                }
-            }, tabId);
+            final String[] requiredPermission = getPermissionsForMimeType(mimeType);
+            final String finalMimeType = mimeType;
+            // Use activity context cause we want to prompt for runtime permission. (bug 1337692)
+            Permissions.from(GeckoAppShell.getGeckoInterface().getActivity())
+                    .withPermissions(requiredPermission)
+                    .andFallback(new Runnable() {
+                        @Override
+                        public void run() {
+                            callback.sendError(null);
+                        }
+                    })
+                    .run(new Runnable() {
+                        @Override
+                        public void run() {
+                            showFilePickerAsync(title, finalMimeType, new ResultHandler() {
+                                @Override
+                                public void gotFile(final String filename) {
+                                    callback.sendSuccess(filename);
+                                }
+                            }, tabId);
+                        }
+                    });
         }
     }
 
+    private static String[] getPermissionsForMimeType(final String mimeType) {
+        if (mimeType.startsWith("audio/")) {
+            return new String[] { Manifest.permission.RECORD_AUDIO };
+        } else if (mimeType.startsWith("image/")) {
+            return new String[] { Manifest.permission.CAMERA };
+        } else if (mimeType.startsWith("video/")) {
+            return new String[] { Manifest.permission.CAMERA };
+        }
+        return new String[] { Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO };
+    }
+
     private void addActivities(Intent intent, HashMap<String, Intent> intents, HashMap<String, Intent> filters) {
         PackageManager pm = context.getPackageManager();
         List<ResolveInfo> lri = pm.queryIntentActivities(intent, 0);
         for (ResolveInfo ri : lri) {
             ComponentName cn = new ComponentName(ri.activityInfo.applicationInfo.packageName, ri.activityInfo.name);
             if (filters != null && !filters.containsKey(cn.toString())) {
                 Intent rintent = new Intent(intent);
                 rintent.setComponent(cn);