open attachments

This commit is contained in:
B. Petersen
2018-10-03 18:28:34 +02:00
parent fc6dded69a
commit 5011f44ddf
9 changed files with 164 additions and 15 deletions
+8 -1
View File
@@ -466,7 +466,7 @@
android:authorities="{$applicationId}.provider.securesms.mms" />
<provider android:name="android.support.v4.content.FileProvider"
android:authorities="{$applicationId}.securesms.fileprovider"
android:authorities="{$applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
@@ -474,6 +474,13 @@
</provider>
<provider
android:name=".connect.AttachmentsContentProvider"
android:authorities="${applicationId}.attachments"
android:grantUriPermissions="true"
android:exported="false">
</provider>
<receiver android:name=".service.BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
+2 -1
View File
@@ -117,7 +117,6 @@
<string name="ConversationItem_click_to_approve_unencrypted_sms_dialog_title">Fallback to unencrypted SMS?</string>
<string name="ConversationItem_click_to_approve_unencrypted_mms_dialog_title">Fallback to unencrypted MMS?</string>
<string name="ConversationItem_click_to_approve_unencrypted_dialog_message">This message will <b>not</b> be encrypted because the recipient is no longer a Delta Chat user.\n\nSend unsecured message?</string>
<string name="ConversationItem_unable_to_open_media">Can\'t find an app able to open this media.</string>
<string name="ConversationItem_copied_text">Copied %s</string>
<string name="ConversationItem_from_s">from %s</string>
<string name="ConversationItem_to_s">to %s</string>
@@ -293,6 +292,8 @@
<!-- ShareActivity -->
<string name="ShareActivity_share_with">Share with</string>
<string name="ShareActivity_unable_to_open_media">Can\'t find an app able to open this media.</string>
<string name="ShareActivity_file_not_found">Could not find %1$s.</string>
<!-- ExperienceUpgradeActivity -->
<string name="ExperienceUpgradeActivity_welcome_to_signal_dgaf">Welcome to Delta Chat.</string>
@@ -743,18 +743,7 @@ public class ConversationItem extends LinearLayout
context.startActivity(intent);
} else if (slide.getUri() != null) {
Log.w(TAG, "Clicked: " + slide.getUri() + " , " + slide.getContentType());
Uri publicUri = PartAuthority.getAttachmentPublicUri(slide.getUri());
Log.w(TAG, "Public URI: " + publicUri);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(PartAuthority.getAttachmentPublicUri(slide.getUri()), slide.getContentType());
try {
context.startActivity(intent);
} catch (ActivityNotFoundException anfe) {
Log.w(TAG, "No activity existed to view the media.");
Toast.makeText(context, R.string.ConversationItem_unable_to_open_media, Toast.LENGTH_LONG).show();
}
dcContext.openForViewOrShare(slide.getDcMsgId(), Intent.ACTION_VIEW);
}
}
}
@@ -16,11 +16,15 @@
*/
package org.thoughtcrime.securesms.connect;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.PowerManager;
import android.support.annotation.NonNull;
import android.support.v4.content.FileProvider;
import android.util.Log;
import android.widget.Toast;
@@ -30,8 +34,10 @@ import com.b44t.messenger.DcContact;
import com.b44t.messenger.DcContext;
import com.b44t.messenger.DcEventCenter;
import com.b44t.messenger.DcLot;
import com.b44t.messenger.DcMsg;
import org.thoughtcrime.securesms.ApplicationContext;
import org.thoughtcrime.securesms.BuildConfig;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.database.Address;
import org.thoughtcrime.securesms.database.DatabaseFactory;
@@ -52,6 +58,7 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
public class ApplicationDcContext extends DcContext {
@@ -96,6 +103,52 @@ public class ApplicationDcContext extends DcContext {
return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
}
public static HashMap<String, Integer> sharedFiles = new HashMap<>();
public void openForViewOrShare(int msg_id, String cmd)
{
DcMsg msg = getMsg(msg_id);
String path = msg.getFile();
String mimeType = msg.getFilemime();
try {
File file = new File(path);
if( !file.exists() ) {
Toast.makeText(context, context.getString(R.string.ShareActivity_file_not_found, path), Toast.LENGTH_LONG).show();
return;
}
Uri uri;
if (path.startsWith(getBlobdir())) {
uri = Uri.parse("content://" + BuildConfig.APPLICATION_ID + ".attachments/" + file.getName());
sharedFiles.put("/"+file.getName(), 1); // as different Android version handle uris in putExtra differently, we also check them on our own
} else {
if (Build.VERSION.SDK_INT >= 24) {
uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileprovider", file);
}
else {
uri = Uri.fromFile(file);
}
}
if( cmd.equals(Intent.ACTION_VIEW) ) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, mimeType);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(intent);
}
else {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType(mimeType);
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(Intent.createChooser(intent, context.getString(R.string.ShareActivity_share_with)));
}
}
catch (Exception e) {
Toast.makeText(context, R.string.ShareActivity_unable_to_open_media, Toast.LENGTH_LONG).show();
}
}
/***********************************************************************************************
* create objects compatible to the database model of Signal
@@ -0,0 +1,92 @@
/*******************************************************************************
*
* Delta Chat Android
* (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 org.thoughtcrime.securesms.connect;
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.
When starting an Intent for viewing, 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 alternative 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 {
ApplicationDcContext dcContext = DcHelper.getContext(getContext());
String path = uri.getPath();
if (!dcContext.sharedFiles.containsKey(path)) {
throw new FileNotFoundException("File was not shared before.");
}
File privateFile = new File(dcContext.getBlobdir(), path);
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;
}
}
@@ -39,6 +39,7 @@ public class AudioSlide extends Slide {
public AudioSlide(Context context, DcMsg dcMsg) {
this(context, Uri.fromFile(new File(dcMsg.getFile())), dcMsg.getBytes(), dcMsg.getType()==DcMsg.DC_MSG_VOICE);
dcMsgId = dcMsg.getId();
}
public AudioSlide(Context context, Uri uri, long dataSize, boolean voiceNote) {
@@ -21,6 +21,7 @@ public class DocumentSlide extends Slide {
public DocumentSlide(Context context, DcMsg dcMsg) {
this(context, Uri.fromFile(new File(dcMsg.getFile())), dcMsg.getFilemime(), dcMsg.getBytes(), dcMsg.getFilename());
dcMsgId = dcMsg.getId();
}
public DocumentSlide(@NonNull Context context, @NonNull Uri uri,
@@ -35,6 +35,7 @@ import java.security.SecureRandom;
public abstract class Slide {
protected int dcMsgId;
protected final Attachment attachment;
protected final Context context;
@@ -44,6 +45,10 @@ public abstract class Slide {
}
public int getDcMsgId() {
return dcMsgId;
}
public String getContentType() {
return attachment.getContentType();
}
@@ -13,7 +13,7 @@ import java.io.File;
public class FileProviderUtil {
private static final String AUTHORITY = BuildConfig.APPLICATION_ID+".securesms.fileprovider";
private static final String AUTHORITY = BuildConfig.APPLICATION_ID+".fileprovider";
public static Uri getUriFor(@NonNull Context context, @NonNull File file) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) return FileProvider.getUriForFile(context, AUTHORITY, file);