From 4cb2ac7b27e7b170a9102af638b98b86e589bc9d Mon Sep 17 00:00:00 2001 From: Moxie Marlinspike Date: Fri, 1 Dec 2017 11:02:30 -0800 Subject: [PATCH] Use FileProvider for external camera capture Uri on L+ Fixes #7237 --- AndroidManifest.xml | 9 ++++++++ res/xml/file_provider_paths.xml | 4 ++++ .../securesms/CreateProfileActivity.java | 3 ++- .../providers/PersistentBlobProvider.java | 7 +++++-- .../securesms/util/FileProviderUtil.java | 21 +++++++++++++++++++ 5 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 res/xml/file_provider_paths.xml create mode 100644 src/org/thoughtcrime/securesms/util/FileProviderUtil.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index bcb31ef920..e7aac6fda3 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -524,6 +524,15 @@ android:exported="false" android:authorities="org.thoughtcrime.provider.securesms.mms" /> + + + + + + diff --git a/res/xml/file_provider_paths.xml b/res/xml/file_provider_paths.xml new file mode 100644 index 0000000000..30c4032eb2 --- /dev/null +++ b/res/xml/file_provider_paths.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/src/org/thoughtcrime/securesms/CreateProfileActivity.java b/src/org/thoughtcrime/securesms/CreateProfileActivity.java index b06254af0e..16169753b5 100644 --- a/src/org/thoughtcrime/securesms/CreateProfileActivity.java +++ b/src/org/thoughtcrime/securesms/CreateProfileActivity.java @@ -50,6 +50,7 @@ import org.thoughtcrime.securesms.util.BitmapDecodingException; import org.thoughtcrime.securesms.util.BitmapUtil; import org.thoughtcrime.securesms.util.DynamicLanguage; import org.thoughtcrime.securesms.util.DynamicTheme; +import org.thoughtcrime.securesms.util.FileProviderUtil; import org.thoughtcrime.securesms.util.IntentUtils; import org.thoughtcrime.securesms.util.TextSecurePreferences; import org.thoughtcrime.securesms.util.Util; @@ -376,7 +377,7 @@ public class CreateProfileActivity extends BaseActionBarActivity implements Inje Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (captureFile != null && cameraIntent.resolveActivity(getPackageManager()) != null) { - cameraIntent.putExtra(EXTRA_OUTPUT, Uri.fromFile(captureFile)); + cameraIntent.putExtra(EXTRA_OUTPUT, FileProviderUtil.getUriFor(this, captureFile)); extraIntents.add(cameraIntent); } } diff --git a/src/org/thoughtcrime/securesms/providers/PersistentBlobProvider.java b/src/org/thoughtcrime/securesms/providers/PersistentBlobProvider.java index aad182b2fd..b2011e36f7 100644 --- a/src/org/thoughtcrime/securesms/providers/PersistentBlobProvider.java +++ b/src/org/thoughtcrime/securesms/providers/PersistentBlobProvider.java @@ -5,8 +5,10 @@ import android.content.ContentUris; import android.content.Context; import android.content.UriMatcher; import android.net.Uri; +import android.os.Build; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.support.v4.content.FileProvider; import android.util.Log; import android.webkit.MimeTypeMap; @@ -14,6 +16,7 @@ import org.thoughtcrime.securesms.crypto.DecryptingPartInputStream; import org.thoughtcrime.securesms.crypto.EncryptingPartOutputStream; import org.thoughtcrime.securesms.crypto.MasterCipher; import org.thoughtcrime.securesms.crypto.MasterSecret; +import org.thoughtcrime.securesms.util.FileProviderUtil; import org.thoughtcrime.securesms.util.Util; import org.whispersystems.libsignal.InvalidMessageException; @@ -128,8 +131,8 @@ public class PersistentBlobProvider { } public Uri createForExternal(@NonNull String mimeType) throws IOException { - return Uri.fromFile(new File(getExternalDir(context), - String.valueOf(System.currentTimeMillis()) + "." + getExtensionFromMimeType(mimeType))); + File target = new File(getExternalDir(context), String.valueOf(System.currentTimeMillis()) + "." + getExtensionFromMimeType(mimeType)); + return FileProviderUtil.getUriFor(context, target); } public boolean delete(@NonNull Uri uri) { diff --git a/src/org/thoughtcrime/securesms/util/FileProviderUtil.java b/src/org/thoughtcrime/securesms/util/FileProviderUtil.java new file mode 100644 index 0000000000..26146e9dc4 --- /dev/null +++ b/src/org/thoughtcrime/securesms/util/FileProviderUtil.java @@ -0,0 +1,21 @@ +package org.thoughtcrime.securesms.util; + + +import android.content.Context; +import android.net.Uri; +import android.os.Build; +import android.support.annotation.NonNull; +import android.support.v4.content.FileProvider; + +import java.io.File; + +public class FileProviderUtil { + + private static final String AUTHORITY = "org.thoughtcrime.securesms.fileprovider"; + + public static Uri getUriFor(@NonNull Context context, @NonNull File file) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) return FileProvider.getUriForFile(context, AUTHORITY, file); + else return Uri.fromFile(file); + } + +}