Bug 1450449 - Part 3: Starting from Nougat, share images via content:// URIs. r?jchen draft
authorJan Henning <jh+bugzilla@buttercookie.de>
Sat, 12 May 2018 23:17:38 +0200
changeset 803596 cffc3091e60c1c103755925afa362e542f8c84a5
parent 803595 f6464ef0686ed9328c58dcc4a0852b2f6dc0ea3e
child 803597 ce464e1f8dfe459789161f77a13cd2840283ca5a
push id112161
push usermozilla@buttercookie.de
push dateMon, 04 Jun 2018 18:00:37 +0000
reviewersjchen
bugs1450449
milestone62.0a1
Bug 1450449 - Part 3: Starting from Nougat, share images via content:// URIs. r?jchen For sharing images we download the image to a temporary file in our internal storage area. This is a perfect use case for granting temporary access to the file only via a content:// URI instead of directly exposing the real file system path. Since support for content:// URIs by arbitrary other apps might be patchy on older Android versions, though, we only start doing this from Nougat onwards. MozReview-Commit-ID: E2I1t8dZzKj
mobile/android/base/java/org/mozilla/gecko/widget/GeckoActionProvider.java
--- a/mobile/android/base/java/org/mozilla/gecko/widget/GeckoActionProvider.java
+++ b/mobile/android/base/java/org/mozilla/gecko/widget/GeckoActionProvider.java
@@ -3,19 +3,21 @@
  * 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.widget;
 
 import android.app.Activity;
 import android.net.Uri;
 import android.support.design.widget.Snackbar;
+import android.support.v4.content.FileProvider;
 import android.util.Base64;
 import android.view.Menu;
 
+import org.mozilla.gecko.AppConstants;
 import org.mozilla.gecko.GeckoApp;
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.SnackbarBuilder;
 import org.mozilla.gecko.Telemetry;
 import org.mozilla.gecko.TelemetryContract;
 import org.mozilla.gecko.overlays.ui.ShareDialog;
 import org.mozilla.gecko.menu.MenuItemSwitcherLayout;
 import org.mozilla.gecko.util.IOUtils;
@@ -323,17 +325,17 @@ public class GeckoActionProvider {
 
                 final File imageFile = File.createTempFile("image", "." + extension, dir);
                 os = new FileOutputStream(imageFile);
 
                 byte[] buf = Base64.decode(src.substring(dataStart + 1), Base64.DEFAULT);
                 os.write(buf);
 
                 // Only alter the intent when we're sure everything has worked
-                intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(imageFile));
+                addFileExtra(intent, imageFile);
             } else {
                 InputStream is = null;
                 try {
                     final byte[] buf = new byte[2048];
                     final URL url = new URL(src);
                     final String filename = URLUtil.guessFileName(src, null, type);
                     is = url.openStream();
 
@@ -341,20 +343,31 @@ public class GeckoActionProvider {
                     os = new FileOutputStream(imageFile);
 
                     int length;
                     while ((length = is.read(buf)) != -1) {
                         os.write(buf, 0, length);
                     }
 
                     // Only alter the intent when we're sure everything has worked
-                    intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(imageFile));
+                    addFileExtra(intent, imageFile);
                 } finally {
                     IOUtils.safeStreamClose(is);
                 }
             }
         } catch (IOException ex) {
             // If something went wrong, we'll just leave the intent un-changed
         } finally {
             IOUtils.safeStreamClose(os);
         }
     }
+
+    private void addFileExtra(final Intent intent, final File file) {
+        if (AppConstants.Versions.preN) {
+            intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
+        } else {
+            Uri contentUri = FileProvider.getUriForFile(mContext,
+                    AppConstants.MOZ_FILE_PROVIDER_AUTHORITY, file);
+            intent.putExtra(Intent.EXTRA_STREAM, contentUri);
+            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
+        }
+    }
 }