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 public static void view(Context context, File file, String mime) {
41 Intent openIntent = new Intent(Intent.ACTION_VIEW);
42 Uri uri;
43 try {
44 uri = FileBackend.getUriForFile(context, file);
45 } catch (SecurityException e) {
46 Log.d(Config.LOGTAG, "No permission to access " + file.getAbsolutePath(), e);
47 Toast.makeText(context, context.getString(R.string.no_permission_to_access_x, file.getAbsolutePath()), Toast.LENGTH_SHORT).show();
48 return;
49 }
50 openIntent.setDataAndType(uri, mime);
51 openIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
52 PackageManager manager = context.getPackageManager();
53 List<ResolveInfo> info = manager.queryIntentActivities(openIntent, 0);
54 if (info.size() == 0) {
55 openIntent.setDataAndType(uri, "*/*");
56 }
57 try {
58 context.startActivity(openIntent);
59 } catch (ActivityNotFoundException e) {
60 Toast.makeText(context, R.string.no_application_found_to_open_file, Toast.LENGTH_SHORT).show();
61 }
62 }
63
64}