diff options
author | Daniel Gultsch <daniel@gultsch.de> | 2017-01-09 17:00:08 +0100 |
---|---|---|
committer | Daniel Gultsch <daniel@gultsch.de> | 2017-01-09 17:00:08 +0100 |
commit | b116926bb16e0293374f08a845f95ccea6a0cd2a (patch) | |
tree | b20cdd9020fead2b22c9b0d9a6ec1f2990d7a1e4 /src/main/java | |
parent | 39c8867ed7a13480f29e35ac1c5fdd5db62fcff8 (diff) |
unify getFileUri across share and open intents
Diffstat (limited to 'src/main/java')
3 files changed, 31 insertions, 29 deletions
diff --git a/src/main/java/eu/siacs/conversations/persistance/FileBackend.java b/src/main/java/eu/siacs/conversations/persistance/FileBackend.java index 1cc70d5a..3239ce3d 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,18 +490,26 @@ public class FileBackend { file = new File(getTakePhotoPath() + "IMG_" + this.IMAGE_DATE_FORMAT.format(new Date()) + ".jpg"); } file.getParentFile().mkdirs(); + return getUriForFile(mXmppConnectionService,file); + } + + public static Uri getUriForFile(Context context, File file) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N || Config.ONLY_INTERNAL_STORAGE) { - return getUriForFile(mXmppConnectionService,file); + 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 getUriForFile(Context context, File file) { - String packageId = context.getPackageName(); - return FileProvider.getUriForFile(context, packageId + FILE_PROVIDER, file); - } - public static Uri getIndexableTakePhotoUri(Uri original) { if (Config.ONLY_INTERNAL_STORAGE || "file".equals(original.getScheme())) { return 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 27b12ae2..709244dd 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 f9c9d8c1..3668cc54 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<Message> 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<ResolveInfo> info = manager.queryIntentActivities(openIntent, 0); if (info.size() == 0) { - openIntent.setDataAndType(Uri.fromFile(file),"*/*"); + openIntent.setDataAndType(uri,"*/*"); } try { getContext().startActivity(openIntent); |