diff options
author | Christian Schneppe <christian@pix-art.de> | 2018-09-26 20:27:33 +0200 |
---|---|---|
committer | Christian Schneppe <christian@pix-art.de> | 2018-09-26 20:27:33 +0200 |
commit | b2a122ec71c8c2af58ea6d99806d07d76efcb0f4 (patch) | |
tree | 5cb02e46240aaa316bbfa5d6d73d4ab1f7c7053a /src/main/java/de/pixart/messenger/ui/util/ViewUtil.java | |
parent | 3c71297965573ffbd3eb423296790b416dc9ce05 (diff) |
show conversation media in contact/conference details
Diffstat (limited to '')
-rw-r--r-- | src/main/java/de/pixart/messenger/ui/util/ViewUtil.java | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/main/java/de/pixart/messenger/ui/util/ViewUtil.java b/src/main/java/de/pixart/messenger/ui/util/ViewUtil.java new file mode 100644 index 000000000..2442df8dc --- /dev/null +++ b/src/main/java/de/pixart/messenger/ui/util/ViewUtil.java @@ -0,0 +1,50 @@ +package de.pixart.messenger.ui.util; + +import android.content.ActivityNotFoundException; +import android.content.Context; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; +import android.net.Uri; +import android.util.Log; +import android.widget.Toast; + +import java.io.File; +import java.util.List; + +import de.pixart.messenger.Config; +import de.pixart.messenger.R; +import de.pixart.messenger.persistance.FileBackend; + +public class ViewUtil { + + public static void view(Context context, Attachment attachment) { + File file = new File(attachment.getUri().getPath()); + final String mime = attachment.getMime() == null ? "*/*" : attachment.getMime(); + view(context, file, mime); + } + + public static void view(Context context, File file, String mime) { + Intent openIntent = new Intent(Intent.ACTION_VIEW); + Uri uri; + try { + uri = FileBackend.getUriForFile(context, file); + } catch (SecurityException e) { + Log.d(Config.LOGTAG, "No permission to access " + file.getAbsolutePath(), e); + Toast.makeText(context, context.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 = context.getPackageManager(); + List<ResolveInfo> info = manager.queryIntentActivities(openIntent, 0); + if (info.size() == 0) { + openIntent.setDataAndType(uri, "*/*"); + } + try { + context.startActivity(openIntent); + } catch (ActivityNotFoundException e) { + Toast.makeText(context, R.string.no_application_found_to_open_file, Toast.LENGTH_SHORT).show(); + } + } +}
\ No newline at end of file |