mirror of
https://github.com/nextcloud/talk-android
synced 2025-06-20 12:09:45 +01:00
add support for all files that can be handled by an external app
Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
This commit is contained in:
parent
6a387e4cb8
commit
b97e670253
@ -26,7 +26,6 @@ import android.annotation.SuppressLint;
|
|||||||
import android.content.ComponentName;
|
import android.content.ComponentName;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.pm.ResolveInfo;
|
|
||||||
import android.graphics.drawable.Drawable;
|
import android.graphics.drawable.Drawable;
|
||||||
import android.graphics.drawable.LayerDrawable;
|
import android.graphics.drawable.LayerDrawable;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
@ -57,14 +56,12 @@ import com.nextcloud.talk.utils.bundle.BundleKeys;
|
|||||||
import com.stfalcon.chatkit.messages.MessageHolders;
|
import com.stfalcon.chatkit.messages.MessageHolders;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.Callable;
|
import java.util.concurrent.Callable;
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
import androidx.core.app.ShareCompat;
|
|
||||||
import androidx.core.content.ContextCompat;
|
import androidx.core.content.ContextCompat;
|
||||||
import androidx.core.content.FileProvider;
|
import androidx.core.content.FileProvider;
|
||||||
import androidx.emoji.widget.EmojiTextView;
|
import androidx.emoji.widget.EmojiTextView;
|
||||||
@ -149,7 +146,7 @@ public class MagicPreviewMessageViewHolder extends MessageHolders.IncomingImageM
|
|||||||
|
|
||||||
image.setOnClickListener(v -> {
|
image.setOnClickListener(v -> {
|
||||||
String mimetype = message.getSelectedIndividualHashMap().get("mimetype");
|
String mimetype = message.getSelectedIndividualHashMap().get("mimetype");
|
||||||
if (isSupportedMimetype(mimetype)) {
|
if (isSupportedForInternalViewer(mimetype) || canBeHandledByExternalApp(mimetype, fileName)) {
|
||||||
openOrDownloadFile(message);
|
openOrDownloadFile(message);
|
||||||
} else {
|
} else {
|
||||||
openFileInFilesApp(message, accountString);
|
openFileInFilesApp(message, accountString);
|
||||||
@ -208,29 +205,11 @@ public class MagicPreviewMessageViewHolder extends MessageHolders.IncomingImageM
|
|||||||
if (file.exists()) {
|
if (file.exists()) {
|
||||||
openFile(filename, mimetype);
|
openFile(filename, mimetype);
|
||||||
} else {
|
} else {
|
||||||
String size = message.getSelectedIndividualHashMap().get("size");
|
downloadFileToCache(message);
|
||||||
|
|
||||||
if (size == null) {
|
|
||||||
size = "-1";
|
|
||||||
}
|
|
||||||
Integer fileSize = Integer.valueOf(size);
|
|
||||||
|
|
||||||
String fileId = message.getSelectedIndividualHashMap().get("id");
|
|
||||||
String path = message.getSelectedIndividualHashMap().get("path");
|
|
||||||
downloadFileToCache(
|
|
||||||
message.activeUser.getBaseUrl(),
|
|
||||||
message.activeUser.getUserId(),
|
|
||||||
message.activeUser.getAttachmentFolder(),
|
|
||||||
filename,
|
|
||||||
path,
|
|
||||||
mimetype,
|
|
||||||
fileSize,
|
|
||||||
fileId
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isSupportedMimetype(String mimetype){
|
public boolean isSupportedForInternalViewer(String mimetype){
|
||||||
switch (mimetype) {
|
switch (mimetype) {
|
||||||
case "image/png":
|
case "image/png":
|
||||||
case "image/jpeg":
|
case "image/jpeg":
|
||||||
@ -243,7 +222,6 @@ public class MagicPreviewMessageViewHolder extends MessageHolders.IncomingImageM
|
|||||||
case "video/ogg":
|
case "video/ogg":
|
||||||
case "text/markdown":
|
case "text/markdown":
|
||||||
case "text/plain":
|
case "text/plain":
|
||||||
case "application/pdf":
|
|
||||||
return true;
|
return true;
|
||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
@ -269,27 +247,24 @@ public class MagicPreviewMessageViewHolder extends MessageHolders.IncomingImageM
|
|||||||
case "text/plain":
|
case "text/plain":
|
||||||
openTextView(filename, mimetype);
|
openTextView(filename, mimetype);
|
||||||
break;
|
break;
|
||||||
case "application/pdf":
|
|
||||||
openPdf(filename);
|
|
||||||
break;
|
|
||||||
default:
|
default:
|
||||||
Log.w(TAG, "no method defined for mimetype: " + mimetype);
|
openFileByExternalApp(filename, mimetype);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void openPdf(String fileName) {
|
private void openFileByExternalApp(String fileName, String mimetype) {
|
||||||
String path = context.getCacheDir().getAbsolutePath() + "/" + fileName;
|
String path = context.getCacheDir().getAbsolutePath() + "/" + fileName;
|
||||||
File file = new File(path);
|
File file = new File(path);
|
||||||
Intent intent = null;
|
Intent intent;
|
||||||
if (Build.VERSION.SDK_INT < 24) {
|
if (Build.VERSION.SDK_INT < 24) {
|
||||||
intent = new Intent(Intent.ACTION_VIEW);
|
intent = new Intent(Intent.ACTION_VIEW);
|
||||||
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
|
intent.setDataAndType(Uri.fromFile(file), mimetype);
|
||||||
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
|
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
|
||||||
} else {
|
} else {
|
||||||
intent = new Intent();
|
intent = new Intent();
|
||||||
intent.setAction(Intent.ACTION_VIEW);
|
intent.setAction(Intent.ACTION_VIEW);
|
||||||
Uri pdfURI = FileProvider.getUriForFile(context, context.getPackageName(), file);
|
Uri pdfURI = FileProvider.getUriForFile(context, context.getPackageName(), file);
|
||||||
intent.setDataAndType(pdfURI, "application/pdf");
|
intent.setDataAndType(pdfURI, mimetype);
|
||||||
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
|
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
|
||||||
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
||||||
}
|
}
|
||||||
@ -299,13 +274,22 @@ public class MagicPreviewMessageViewHolder extends MessageHolders.IncomingImageM
|
|||||||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||||
context.startActivity(intent);
|
context.startActivity(intent);
|
||||||
} else {
|
} else {
|
||||||
Toast.makeText(context, "No Application found to open the pdf", Toast.LENGTH_SHORT).show();
|
Log.e(TAG, "No Application found to open the file. This should have been handled beforehand!");
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Log.e(TAG, "Error while opening pdf file", e);
|
Log.e(TAG, "Error while opening file", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean canBeHandledByExternalApp(String mimetype, String fileName){
|
||||||
|
String path = context.getCacheDir().getAbsolutePath() + "/" + fileName;
|
||||||
|
File file = new File(path);
|
||||||
|
Intent intent = new Intent(Intent.ACTION_VIEW);
|
||||||
|
intent.setDataAndType(Uri.fromFile(file), mimetype);
|
||||||
|
|
||||||
|
return intent.resolveActivity(context.getPackageManager()) != null;
|
||||||
|
}
|
||||||
|
|
||||||
private void openImageView(String filename, String mimetype) {
|
private void openImageView(String filename, String mimetype) {
|
||||||
Intent fullScreenImageIntent = new Intent(context, FullScreenImageActivity.class);
|
Intent fullScreenImageIntent = new Intent(context, FullScreenImageActivity.class);
|
||||||
fullScreenImageIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
fullScreenImageIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||||
@ -332,7 +316,7 @@ public class MagicPreviewMessageViewHolder extends MessageHolders.IncomingImageM
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void onMessageViewLongClick(ChatMessage message, String accountString) {
|
private void onMessageViewLongClick(ChatMessage message, String accountString) {
|
||||||
if (isSupportedMimetype(message.getSelectedIndividualHashMap().get("mimetype"))) {
|
if (isSupportedForInternalViewer(message.getSelectedIndividualHashMap().get("mimetype"))) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -348,14 +332,24 @@ public class MagicPreviewMessageViewHolder extends MessageHolders.IncomingImageM
|
|||||||
}
|
}
|
||||||
|
|
||||||
@SuppressLint("LongLogTag")
|
@SuppressLint("LongLogTag")
|
||||||
private void downloadFileToCache(String baseUrl,
|
private void downloadFileToCache(ChatMessage message) {
|
||||||
String userId,
|
|
||||||
String attachmentFolder,
|
String baseUrl = message.activeUser.getBaseUrl();
|
||||||
String fileName,
|
String userId = message.activeUser.getUserId();
|
||||||
String path,
|
String attachmentFolder = message.activeUser.getAttachmentFolder();
|
||||||
String mimetype,
|
|
||||||
Integer size,
|
String fileName = message.getSelectedIndividualHashMap().get("name");
|
||||||
String fileId) {
|
String mimetype = message.getSelectedIndividualHashMap().get("mimetype");
|
||||||
|
|
||||||
|
String size = message.getSelectedIndividualHashMap().get("size");
|
||||||
|
|
||||||
|
if (size == null) {
|
||||||
|
size = "-1";
|
||||||
|
}
|
||||||
|
Integer fileSize = Integer.valueOf(size);
|
||||||
|
|
||||||
|
String fileId = message.getSelectedIndividualHashMap().get("id");
|
||||||
|
String path = message.getSelectedIndividualHashMap().get("path");
|
||||||
|
|
||||||
// check if download worker is already running
|
// check if download worker is already running
|
||||||
ListenableFuture<List<WorkInfo>> workers = WorkManager.getInstance(context).getWorkInfosByTag(fileId);
|
ListenableFuture<List<WorkInfo>> workers = WorkManager.getInstance(context).getWorkInfosByTag(fileId);
|
||||||
@ -381,7 +375,7 @@ public class MagicPreviewMessageViewHolder extends MessageHolders.IncomingImageM
|
|||||||
.putString(DownloadFileToCacheWorker.KEY_ATTACHMENT_FOLDER, attachmentFolder)
|
.putString(DownloadFileToCacheWorker.KEY_ATTACHMENT_FOLDER, attachmentFolder)
|
||||||
.putString(DownloadFileToCacheWorker.KEY_FILE_NAME, fileName)
|
.putString(DownloadFileToCacheWorker.KEY_FILE_NAME, fileName)
|
||||||
.putString(DownloadFileToCacheWorker.KEY_FILE_PATH, path)
|
.putString(DownloadFileToCacheWorker.KEY_FILE_PATH, path)
|
||||||
.putInt(DownloadFileToCacheWorker.KEY_FILE_SIZE, size)
|
.putInt(DownloadFileToCacheWorker.KEY_FILE_SIZE, fileSize)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
downloadWorker = new OneTimeWorkRequest.Builder(DownloadFileToCacheWorker.class)
|
downloadWorker = new OneTimeWorkRequest.Builder(DownloadFileToCacheWorker.class)
|
||||||
@ -411,7 +405,8 @@ public class MagicPreviewMessageViewHolder extends MessageHolders.IncomingImageM
|
|||||||
if (image.isShown()) {
|
if (image.isShown()) {
|
||||||
openFile(fileName, mimetype);
|
openFile(fileName, mimetype);
|
||||||
} else {
|
} else {
|
||||||
Log.d(TAG, "image " + fileName + " was downloaded but it's not opened (view is not shown)");
|
Log.d(TAG, "file " + fileName + " was downloaded but it's not opened because view is not shown on" +
|
||||||
|
" screen");
|
||||||
}
|
}
|
||||||
messageText.setText(fileName);
|
messageText.setText(fileName);
|
||||||
progressBar.setVisibility(View.GONE);
|
progressBar.setVisibility(View.GONE);
|
||||||
|
Loading…
Reference in New Issue
Block a user