diff --git a/MessengerProj/src/main/AndroidManifest.xml b/MessengerProj/src/main/AndroidManifest.xml index a8662c3a3..408216ad0 100644 --- a/MessengerProj/src/main/AndroidManifest.xml +++ b/MessengerProj/src/main/AndroidManifest.xml @@ -221,6 +221,8 @@ + + diff --git a/MessengerProj/src/main/java/com/b44t/messenger/AndroidUtilities.java b/MessengerProj/src/main/java/com/b44t/messenger/AndroidUtilities.java index 08816fa24..cffef509b 100644 --- a/MessengerProj/src/main/java/com/b44t/messenger/AndroidUtilities.java +++ b/MessengerProj/src/main/java/com/b44t/messenger/AndroidUtilities.java @@ -988,50 +988,25 @@ public class AndroidUtilities { } */ - public static void openForView(MessageObject message, Activity activity) throws Exception { - // TODO: this seems not to work for files in our private directory! - File f = null; - String fileName = message.getFileName(); - if (message.messageOwner.attachPath != null && message.messageOwner.attachPath.length() != 0) { - f = new File(message.messageOwner.attachPath); + public static void openForView(MessageObject message, Activity activity) throws Exception + { + String fileName = message.messageOwner.media.document.file_name; + Uri toOpen = Uri.parse("content://" + BuildConfig.APPLICATION_ID + ".attachments/" + fileName); + + String mimeType = null; + MimeTypeMap mimeMap = MimeTypeMap.getSingleton(); + int idx = fileName.lastIndexOf('.'); + if (idx != -1) { + String ext = fileName.substring(idx + 1); + mimeType = mimeMap.getMimeTypeFromExtension(ext.toLowerCase()); } - if (f == null || !f.exists()) { - f = FileLoader.getPathToMessage(message.messageOwner); - } - if (f != null && f.exists()) { - String realMimeType = null; - Intent intent = new Intent(Intent.ACTION_VIEW); - MimeTypeMap myMime = MimeTypeMap.getSingleton(); - int idx = fileName.lastIndexOf('.'); - if (idx != -1) { - String ext = fileName.substring(idx + 1); - realMimeType = myMime.getMimeTypeFromExtension(ext.toLowerCase()); - if (realMimeType == null) { - if (message.type == MessageObject.MO_TYPE9_FILE || message.type == MessageObject.MO_TYPE0_TEXT) { - realMimeType = message.getDocument().mime_type; - } - if (realMimeType == null || realMimeType.length() == 0) { - realMimeType = null; - } - } - if (realMimeType != null) { - intent.setDataAndType(Uri.fromFile(f), realMimeType); - } else { - intent.setDataAndType(Uri.fromFile(f), "text/plain"); - } - } else { - intent.setDataAndType(Uri.fromFile(f), "text/plain"); - } - if (realMimeType != null) { - try { - activity.startActivityForResult(intent, 500); - } catch (Exception e) { - intent.setDataAndType(Uri.fromFile(f), "text/plain"); - activity.startActivityForResult(intent, 500); - } - } else { - activity.startActivityForResult(intent, 500); - } + if (mimeType == null) { + mimeType = message.messageOwner.media.document.mime_type; } + + Intent intent = new Intent(Intent.ACTION_VIEW); + intent.setDataAndType(toOpen, mimeType); + intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); + activity.startActivity(intent); } } diff --git a/MessengerProj/src/main/java/com/b44t/messenger/AttachmentsContentProvider.java b/MessengerProj/src/main/java/com/b44t/messenger/AttachmentsContentProvider.java new file mode 100644 index 000000000..524488c8d --- /dev/null +++ b/MessengerProj/src/main/java/com/b44t/messenger/AttachmentsContentProvider.java @@ -0,0 +1,85 @@ +/******************************************************************************* + * + * Messenger Android Frontend + * (C) 2017 Björn Petersen + * Contact: r10s@b44t.com, http://b44t.com + * + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see http://www.gnu.org/licenses/ . + * + ******************************************************************************/ + + +package com.b44t.messenger; + +import android.content.ContentProvider; +import android.content.ContentValues; +import android.database.Cursor; +import android.net.Uri; +import android.os.ParcelFileDescriptor; + +import java.io.File; +import java.io.FileNotFoundException; + + +public class AttachmentsContentProvider extends ContentProvider { + + /* We save all attachments in our private files-directory + that cannot be read by other apps. + + So starting, when starting an Intent for viewing (eg. openForView()), we cannot use the paths. + Instead, we give a content://-url that results in calls to this class. + + (An alternative would be to copy files to view to a public directory, however, this would + lead to duplicate data. + Another alterntive would be to write all attachments to a public directory, however, this + may lead to security problems as files are system-wide-readable and would also cause problems + if the user or another app deletes these files) */ + + @Override + public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException { + File privateFile = new File(MrMailbox.getBlobdir(), uri.getPath()); + return ParcelFileDescriptor.open(privateFile, ParcelFileDescriptor.MODE_READ_ONLY); + } + + @Override + public int delete(Uri arg0, String arg1, String[] arg2) { + return 0; + } + + @Override + public String getType(Uri arg0) { + return null; + } + + @Override + public Uri insert(Uri arg0, ContentValues arg1) { + return null; + } + + @Override + public boolean onCreate() { + return false; + } + + @Override + public Cursor query(Uri arg0, String[] arg1, String arg2, String[] arg3, + String arg4) { + return null; + } + + @Override + public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3) { + return 0; + } +}