1package eu.siacs.conversations.ui.util;
2
3import android.content.ActivityNotFoundException;
4import android.content.Context;
5import android.content.Intent;
6import android.content.pm.PackageManager;
7import android.content.pm.ResolveInfo;
8import android.net.Uri;
9import android.util.Log;
10import android.widget.Toast;
11
12import java.io.File;
13import java.util.List;
14
15import eu.siacs.conversations.Config;
16import eu.siacs.conversations.R;
17import eu.siacs.conversations.entities.DownloadableFile;
18import eu.siacs.conversations.persistance.FileBackend;
19
20public class ViewUtil {
21
22 public static void view(Context context, Attachment attachment) {
23 File file = new File(attachment.getUri().getPath());
24 final String mime = attachment.getMime() == null ? "*/*" : attachment.getMime();
25 view(context, file, mime);
26 }
27
28 public static void view (Context context, DownloadableFile file) {
29 if (!file.exists()) {
30 Toast.makeText(context, R.string.file_deleted, Toast.LENGTH_SHORT).show();
31 return;
32 }
33 String mime = file.getMimeType();
34 if (mime == null) {
35 mime = "*/*";
36 }
37 view(context, file, mime);
38 }
39
40 private static void view(Context context, File file, String mime) {
41 Log.d(Config.LOGTAG,"viewing "+file.getAbsolutePath()+" "+mime);
42 final Intent openIntent = new Intent(Intent.ACTION_VIEW);
43 final Uri uri;
44 try {
45 uri = FileBackend.getUriForFile(context, file);
46 } catch (SecurityException e) {
47 Log.d(Config.LOGTAG, "No permission to access " + file.getAbsolutePath(), e);
48 Toast.makeText(context, context.getString(R.string.no_permission_to_access_x, file.getAbsolutePath()), Toast.LENGTH_SHORT).show();
49 return;
50 }
51 openIntent.setDataAndType(uri, mime);
52 openIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
53 try {
54 context.startActivity(openIntent);
55 } catch (final ActivityNotFoundException e) {
56 Toast.makeText(context, R.string.no_application_found_to_open_file, Toast.LENGTH_SHORT).show();
57 }
58 }
59
60}