blob: e0dcd4bfedcd51f56cc55baf0245cf0f20c274c5 (
plain)
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
|
package de.thedevstack.conversationsplus.ui.listeners;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.view.View;
import android.widget.Toast;
import java.util.List;
import de.thedevstack.conversationsplus.ConversationsPlusApplication;
import de.thedevstack.conversationsplus.R;
import de.thedevstack.conversationsplus.entities.DownloadableFile;
import de.thedevstack.conversationsplus.entities.Message;
import de.thedevstack.conversationsplus.persistance.FileBackend;
import de.thedevstack.conversationsplus.providers.ConversationsPlusFileProvider;
/**
*/
public class OpenFileOnClickListener implements View.OnClickListener {
private final Activity activity;
private final Message message;
public OpenFileOnClickListener(Activity activity, Message message) {
this.activity = activity;
this.message = message;
}
@Override
public void onClick(View view) {
this.openFile();
}
public void openFile() {
DownloadableFile file = FileBackend.getFile(message);
if (!file.exists()) {
Toast.makeText(this.activity, R.string.file_deleted, Toast.LENGTH_SHORT).show();
return;
}
boolean bInPrivateStorage = false;
if (file.getAbsolutePath().startsWith(FileBackend.getPrivateFileDirectoryPath())) {
bInPrivateStorage = true;
}
Intent openIntent = new Intent(Intent.ACTION_VIEW);
String mime = file.getMimeType();
if (mime == null) {
mime = "*/*";
}
Uri uri;
if (bInPrivateStorage) {
uri = ConversationsPlusFileProvider.createUriForPrivateFile(file);
} else {
uri = Uri.fromFile(file);
}
openIntent.setDataAndType(uri, mime);
PackageManager manager = this.activity.getPackageManager();
List<ResolveInfo> infos = manager.queryIntentActivities(openIntent, 0);
if (bInPrivateStorage) {
for (ResolveInfo info : infos) {
ConversationsPlusApplication.getAppContext().grantUriPermission(info.activityInfo.packageName, uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
}
if (infos.size() == 0) {
openIntent.setDataAndType(uri, "*/*");
}
if (bInPrivateStorage) {
openIntent.putExtra(Intent.EXTRA_STREAM, uri);
openIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
try {
this.activity.startActivity(openIntent);
return;
} catch (ActivityNotFoundException e) {
//ignored
}
Toast.makeText(this.activity, R.string.no_application_found_to_open_file, Toast.LENGTH_SHORT).show();
}
}
|