aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/ui/listeners/OpenFileOnClickListener.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/de/thedevstack/conversationsplus/ui/listeners/OpenFileOnClickListener.java')
-rw-r--r--src/main/java/de/thedevstack/conversationsplus/ui/listeners/OpenFileOnClickListener.java81
1 files changed, 81 insertions, 0 deletions
diff --git a/src/main/java/de/thedevstack/conversationsplus/ui/listeners/OpenFileOnClickListener.java b/src/main/java/de/thedevstack/conversationsplus/ui/listeners/OpenFileOnClickListener.java
new file mode 100644
index 00000000..e0dcd4bf
--- /dev/null
+++ b/src/main/java/de/thedevstack/conversationsplus/ui/listeners/OpenFileOnClickListener.java
@@ -0,0 +1,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();
+ }
+}