diff options
author | Christian Schneppe <christian@pix-art.de> | 2017-01-09 22:08:47 +0100 |
---|---|---|
committer | Christian Schneppe <christian@pix-art.de> | 2017-01-09 22:08:47 +0100 |
commit | 06c108c58dd3e4e658534633bf7af7b6f73e3a77 (patch) | |
tree | b997636e55cfff049a4babba80e960bf323d871e | |
parent | b4be51d2a1b637f0f5da8a343277aea3dd36ee06 (diff) |
unify getFileUri across share and open intents
5 files changed, 43 insertions, 37 deletions
diff --git a/src/main/java/de/pixart/messenger/persistance/FileBackend.java b/src/main/java/de/pixart/messenger/persistance/FileBackend.java index c85c2c34a..147226b79 100644 --- a/src/main/java/de/pixart/messenger/persistance/FileBackend.java +++ b/src/main/java/de/pixart/messenger/persistance/FileBackend.java @@ -62,7 +62,7 @@ import de.pixart.messenger.xmpp.pep.Avatar; public class FileBackend { private static final SimpleDateFormat fileDateFormat = new SimpleDateFormat("yyyyMMdd_HHmmssSSS", Locale.US); - public static final String CONVERSATIONS_FILE_PROVIDER = "de.pixart.messenger.files"; + private static final String FILE_PROVIDER = ".files"; private XmppConnectionService mXmppConnectionService; @@ -499,8 +499,21 @@ public class FileBackend { public Uri getTakePhotoUri() { File file = new File(getTakePhotoPath() + "IMG_" + fileDateFormat.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) { - return FileProvider.getUriForFile(mXmppConnectionService, CONVERSATIONS_FILE_PROVIDER, 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); } @@ -752,11 +765,6 @@ public class FileBackend { return inSampleSize; } - public Uri getJingleFileUri(Message message) { - File file = getFile(message); - return Uri.parse("file://" + file.getAbsolutePath()); - } - public void updateFileParams(Message message) { updateFileParams(message, null); } diff --git a/src/main/java/de/pixart/messenger/services/BarcodeProvider.java b/src/main/java/de/pixart/messenger/services/BarcodeProvider.java index ddd92af63..cd28cb74d 100644 --- a/src/main/java/de/pixart/messenger/services/BarcodeProvider.java +++ b/src/main/java/de/pixart/messenger/services/BarcodeProvider.java @@ -34,7 +34,7 @@ import de.pixart.messenger.xmpp.jid.Jid; public class BarcodeProvider extends ContentProvider implements ServiceConnection { - private static final String AUTHORITY = "de.pixart.messenger.barcodes"; + private static final String AUTHORITY = ".barcodes"; private final Object lock = new Object(); @@ -163,8 +163,9 @@ public class BarcodeProvider extends ContentProvider implements ServiceConnectio } } - public static Uri getUriForAccount(Account account) { - return Uri.parse("content://" + AUTHORITY + "/" + account.getJid().toBareJid() + ".png"); + public static Uri getUriForAccount(Context context, Account account) { + final String packageId = context.getPackageName(); + return Uri.parse("content://" + packageId + AUTHORITY + "/" + account.getJid().toBareJid() + ".png"); } public static Bitmap createAztecBitmap(String input, int size) { diff --git a/src/main/java/de/pixart/messenger/ui/ConversationFragment.java b/src/main/java/de/pixart/messenger/ui/ConversationFragment.java index ec0242131..b40092b16 100644 --- a/src/main/java/de/pixart/messenger/ui/ConversationFragment.java +++ b/src/main/java/de/pixart/messenger/ui/ConversationFragment.java @@ -743,9 +743,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/de/pixart/messenger/ui/EditAccountActivity.java b/src/main/java/de/pixart/messenger/ui/EditAccountActivity.java index eea15a577..2ed75cd21 100644 --- a/src/main/java/de/pixart/messenger/ui/EditAccountActivity.java +++ b/src/main/java/de/pixart/messenger/ui/EditAccountActivity.java @@ -779,7 +779,7 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat private void shareBarcode() { Intent intent = new Intent(Intent.ACTION_SEND); - intent.putExtra(Intent.EXTRA_STREAM, BarcodeProvider.getUriForAccount(mAccount)); + intent.putExtra(Intent.EXTRA_STREAM, BarcodeProvider.getUriForAccount(this, mAccount)); intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.setType("image/png"); startActivity(Intent.createChooser(intent, getText(R.string.share_with))); diff --git a/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java b/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java index 0b98614b7..09d2a8c40 100644 --- a/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java +++ b/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java @@ -12,8 +12,6 @@ import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.AsyncTask; -import android.os.Build; -import android.support.v4.content.FileProvider; import android.text.Spannable; import android.text.SpannableString; import android.text.SpannableStringBuilder; @@ -942,15 +940,15 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie } @Override - public boolean onCreateActionMode(ActionMode mode, Menu menu) { - if (onQuoteListener != null) { - int quoteResId = activity.getThemeResource(R.attr.icon_quote, R.drawable.ic_action_reply); - // 3rd item is placed after "copy" item - menu.add(0, android.R.id.button1, 3, R.string.quote).setIcon(quoteResId) - .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT); - } - return false; - } + public boolean onCreateActionMode(ActionMode mode, Menu menu) { + if (onQuoteListener != null) { + int quoteResId = activity.getThemeResource(R.attr.icon_quote, R.drawable.ic_action_reply); + // 3rd item is placed after "copy" item + menu.add(0, android.R.id.button1, 3, R.string.quote).setIcon(quoteResId) + .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT); + } + return false; + } @Override public boolean onPrepareActionMode(ActionMode mode, Menu menu) { @@ -1009,23 +1007,18 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie mime = "*/*"; } Uri uri; - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { - try { - uri = FileProvider.getUriForFile(activity, FileBackend.CONVERSATIONS_FILE_PROVIDER, file); - } catch (IllegalArgumentException 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); - } 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); |