1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
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.entities.DownloadableFile;
import de.pixart.messenger.persistance.FileBackend;
import de.pixart.messenger.ui.MediaViewerActivity;
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, DownloadableFile file) {
if (!file.exists()) {
Toast.makeText(context, R.string.file_deleted, Toast.LENGTH_SHORT).show();
return;
}
String mime = file.getMimeType();
if (mime == null) {
mime = "*/*";
}
view(context, file, mime);
}
public static void view(Context context, File file, String mime) {
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;
}
// use internal viewer for images and videos
if (mime.startsWith("image/")) {
Intent intent = new Intent(context, MediaViewerActivity.class);
intent.putExtra("image", Uri.fromFile(file));
try {
context.startActivity(intent);
return;
} catch (ActivityNotFoundException e) {
//ignored
}
} else if (mime.startsWith("video/")) {
Intent intent = new Intent(context, MediaViewerActivity.class);
intent.putExtra("video", Uri.fromFile(file));
try {
context.startActivity(intent);
return;
} catch (ActivityNotFoundException e) {
//ignored
}
} else {
Intent openIntent = new Intent(Intent.ACTION_VIEW);
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();
}
}
}
}
|