From b116926bb16e0293374f08a845f95ccea6a0cd2a Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Mon, 9 Jan 2017 17:00:08 +0100 Subject: [PATCH] unify getFileUri across share and open intents --- .../persistance/FileBackend.java | 27 ++++++++++--------- .../ui/ConversationFragment.java | 12 +++++++-- .../ui/adapter/MessageAdapter.java | 23 +++++----------- 3 files changed, 32 insertions(+), 30 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/persistance/FileBackend.java b/src/main/java/eu/siacs/conversations/persistance/FileBackend.java index 1cc70d5a8..3239ce3d6 100644 --- a/src/main/java/eu/siacs/conversations/persistance/FileBackend.java +++ b/src/main/java/eu/siacs/conversations/persistance/FileBackend.java @@ -10,7 +10,6 @@ import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.Paint; -import android.graphics.Rect; import android.graphics.RectF; import android.media.MediaMetadataRetriever; import android.net.Uri; @@ -491,16 +490,24 @@ public class FileBackend { file = new File(getTakePhotoPath() + "IMG_" + this.IMAGE_DATE_FORMAT.format(new Date()) + ".jpg"); } file.getParentFile().mkdirs(); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N || Config.ONLY_INTERNAL_STORAGE) { - return getUriForFile(mXmppConnectionService,file); - } else { - return Uri.fromFile(file); - } + return getUriForFile(mXmppConnectionService,file); } public static Uri getUriForFile(Context context, File file) { - String packageId = context.getPackageName(); - return FileProvider.getUriForFile(context, packageId + FILE_PROVIDER, file); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N || Config.ONLY_INTERNAL_STORAGE) { + try { + String packageId = context.getPackageName(); + return FileProvider.getUriForFile(context, packageId + FILE_PROVIDER, file); + } catch(IllegalArgumentException e) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + throw new SecurityException(); + } else { + return Uri.fromFile(file); + } + } + } else { + return Uri.fromFile(file); + } } public static Uri getIndexableTakePhotoUri(Uri original) { @@ -749,10 +756,6 @@ public class FileBackend { return inSampleSize; } - public Uri getJingleFileUri(Message message) { - return getUriForFile(mXmppConnectionService,getFile(message)); - } - public void updateFileParams(Message message) { updateFileParams(message,null); } diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java index 27b12ae26..709244dda 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java @@ -9,6 +9,8 @@ import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.IntentSender.SendIntentException; +import android.net.Uri; +import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.support.v13.view.inputmethod.InputConnectionCompat; @@ -58,6 +60,7 @@ import eu.siacs.conversations.entities.Presence; import eu.siacs.conversations.entities.Transferable; import eu.siacs.conversations.entities.TransferablePlaceholder; import eu.siacs.conversations.http.HttpDownloadConnection; +import eu.siacs.conversations.persistance.FileBackend; import eu.siacs.conversations.services.MessageArchiveService; import eu.siacs.conversations.services.XmppConnectionService; import eu.siacs.conversations.ui.XmppActivity.OnPresenceSelected; @@ -718,8 +721,13 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa shareIntent.putExtra(Intent.EXTRA_TEXT, message.getBody()); shareIntent.setType("text/plain"); } else { - shareIntent.putExtra(Intent.EXTRA_STREAM, - activity.xmppConnectionService.getFileBackend().getJingleFileUri(message)); + final DownloadableFile file = activity.xmppConnectionService.getFileBackend().getFile(message); + try { + shareIntent.putExtra(Intent.EXTRA_STREAM, FileBackend.getUriForFile(activity, file)); + } catch (SecurityException e) { + Toast.makeText(activity, activity.getString(R.string.no_permission_to_access_x, file.getAbsolutePath()), Toast.LENGTH_SHORT).show(); + return; + } shareIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); String mime = message.getMimeType(); if (mime == null) { diff --git a/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java b/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java index f9c9d8c15..3668cc54c 100644 --- a/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java +++ b/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java @@ -865,27 +865,18 @@ public class MessageAdapter extends ArrayAdapter implements CopyTextVie mime = "*/*"; } Uri uri; - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N || Config.ONLY_INTERNAL_STORAGE) { - try { - uri = FileBackend.getUriForFile(activity, file); - } catch (IllegalArgumentException e) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { - Toast.makeText(activity, activity.getString(R.string.no_permission_to_access_x, file.getAbsolutePath()), Toast.LENGTH_SHORT).show(); - return; - } else { - uri = Uri.fromFile(file); - } - } - openIntent.setDataAndType(uri, mime); - openIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); - } else { - uri = Uri.fromFile(file); + try { + uri = FileBackend.getUriForFile(activity, file); + } catch (SecurityException e) { + Toast.makeText(activity, activity.getString(R.string.no_permission_to_access_x, file.getAbsolutePath()), Toast.LENGTH_SHORT).show(); + return; } openIntent.setDataAndType(uri, mime); + openIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); PackageManager manager = activity.getPackageManager(); List info = manager.queryIntentActivities(openIntent, 0); if (info.size() == 0) { - openIntent.setDataAndType(Uri.fromFile(file),"*/*"); + openIntent.setDataAndType(uri,"*/*"); } try { getContext().startActivity(openIntent);