Commit 62ce20f7 authored by Jan Henning's avatar Jan Henning Committed by Georg Koppen
Browse files

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

--HG--
extra : rebase_source : 84449c39aed622a995e7e009b8e33d21ff02db23
parent 8ed0f4a3
Loading
Loading
Loading
Loading
+15 −2
Original line number Diff line number Diff line
@@ -8,9 +8,11 @@ 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;
@@ -328,7 +330,7 @@ public class GeckoActionProvider {
                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 {
@@ -346,7 +348,7 @@ public class GeckoActionProvider {
                    }

                    // 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);
                }
@@ -357,4 +359,15 @@ public class GeckoActionProvider {
            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);
        }
    }
}