Bug 1450449 - Part 3: Starting from Nougat, share images via content:// URIs. r=jchen
authorJan Henning <jh+bugzilla@buttercookie.de>
Sat, 12 May 2018 23:17:38 +0200
changeset 476057 5d0a03ae227ac7888fb200a1a88353975a374f95
parent 476056 dd8790d74ceca129f42b8e59cc6d31d6048cd5ea
child 476058 eb2c61b1014ceb1ac30211e2f9651ff36bb55770
push id9374
push userjlund@mozilla.com
push dateMon, 18 Jun 2018 21:43:20 +0000
treeherdermozilla-beta@160e085dfb0b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjchen
bugs1450449
milestone62.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
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);
+        }
+    }
 }