unify getFileUri across share and open intents

This commit is contained in:
Daniel Gultsch 2017-01-09 17:00:08 +01:00
parent 39c8867ed7
commit b116926bb1
3 changed files with 32 additions and 30 deletions

View File

@ -10,7 +10,6 @@ import android.graphics.BitmapFactory;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.graphics.Matrix; import android.graphics.Matrix;
import android.graphics.Paint; import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF; import android.graphics.RectF;
import android.media.MediaMetadataRetriever; import android.media.MediaMetadataRetriever;
import android.net.Uri; import android.net.Uri;
@ -491,16 +490,24 @@ public class FileBackend {
file = new File(getTakePhotoPath() + "IMG_" + this.IMAGE_DATE_FORMAT.format(new Date()) + ".jpg"); file = new File(getTakePhotoPath() + "IMG_" + this.IMAGE_DATE_FORMAT.format(new Date()) + ".jpg");
} }
file.getParentFile().mkdirs(); file.getParentFile().mkdirs();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N || Config.ONLY_INTERNAL_STORAGE) {
return getUriForFile(mXmppConnectionService,file); return getUriForFile(mXmppConnectionService,file);
}
public static Uri getUriForFile(Context context, File file) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N || Config.ONLY_INTERNAL_STORAGE) {
try {
String packageId = context.getPackageName();
return FileProvider.getUriForFile(context, packageId + FILE_PROVIDER, file);
} catch(IllegalArgumentException e) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
throw new SecurityException();
} else { } else {
return Uri.fromFile(file); return Uri.fromFile(file);
} }
} }
} else {
public static Uri getUriForFile(Context context, File file) { return Uri.fromFile(file);
String packageId = context.getPackageName(); }
return FileProvider.getUriForFile(context, packageId + FILE_PROVIDER, file);
} }
public static Uri getIndexableTakePhotoUri(Uri original) { public static Uri getIndexableTakePhotoUri(Uri original) {
@ -749,10 +756,6 @@ public class FileBackend {
return inSampleSize; return inSampleSize;
} }
public Uri getJingleFileUri(Message message) {
return getUriForFile(mXmppConnectionService,getFile(message));
}
public void updateFileParams(Message message) { public void updateFileParams(Message message) {
updateFileParams(message,null); updateFileParams(message,null);
} }

View File

@ -9,6 +9,8 @@ import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.IntentSender.SendIntentException; import android.content.IntentSender.SendIntentException;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.support.v13.view.inputmethod.InputConnectionCompat; import android.support.v13.view.inputmethod.InputConnectionCompat;
@ -58,6 +60,7 @@ import eu.siacs.conversations.entities.Presence;
import eu.siacs.conversations.entities.Transferable; import eu.siacs.conversations.entities.Transferable;
import eu.siacs.conversations.entities.TransferablePlaceholder; import eu.siacs.conversations.entities.TransferablePlaceholder;
import eu.siacs.conversations.http.HttpDownloadConnection; import eu.siacs.conversations.http.HttpDownloadConnection;
import eu.siacs.conversations.persistance.FileBackend;
import eu.siacs.conversations.services.MessageArchiveService; import eu.siacs.conversations.services.MessageArchiveService;
import eu.siacs.conversations.services.XmppConnectionService; import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.ui.XmppActivity.OnPresenceSelected; import eu.siacs.conversations.ui.XmppActivity.OnPresenceSelected;
@ -718,8 +721,13 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa
shareIntent.putExtra(Intent.EXTRA_TEXT, message.getBody()); shareIntent.putExtra(Intent.EXTRA_TEXT, message.getBody());
shareIntent.setType("text/plain"); shareIntent.setType("text/plain");
} else { } else {
shareIntent.putExtra(Intent.EXTRA_STREAM, final DownloadableFile file = activity.xmppConnectionService.getFileBackend().getFile(message);
activity.xmppConnectionService.getFileBackend().getJingleFileUri(message)); try {
shareIntent.putExtra(Intent.EXTRA_STREAM, FileBackend.getUriForFile(activity, file));
} catch (SecurityException e) {
Toast.makeText(activity, activity.getString(R.string.no_permission_to_access_x, file.getAbsolutePath()), Toast.LENGTH_SHORT).show();
return;
}
shareIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); shareIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
String mime = message.getMimeType(); String mime = message.getMimeType();
if (mime == null) { if (mime == null) {

View File

@ -865,27 +865,18 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie
mime = "*/*"; mime = "*/*";
} }
Uri uri; Uri uri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N || Config.ONLY_INTERNAL_STORAGE) {
try { try {
uri = FileBackend.getUriForFile(activity, file); uri = FileBackend.getUriForFile(activity, file);
} catch (IllegalArgumentException e) { } catch (SecurityException e) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Toast.makeText(activity, activity.getString(R.string.no_permission_to_access_x, file.getAbsolutePath()), Toast.LENGTH_SHORT).show(); Toast.makeText(activity, activity.getString(R.string.no_permission_to_access_x, file.getAbsolutePath()), Toast.LENGTH_SHORT).show();
return; return;
} else {
uri = Uri.fromFile(file);
}
} }
openIntent.setDataAndType(uri, mime); openIntent.setDataAndType(uri, mime);
openIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); openIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
} else {
uri = Uri.fromFile(file);
}
openIntent.setDataAndType(uri, mime);
PackageManager manager = activity.getPackageManager(); PackageManager manager = activity.getPackageManager();
List<ResolveInfo> info = manager.queryIntentActivities(openIntent, 0); List<ResolveInfo> info = manager.queryIntentActivities(openIntent, 0);
if (info.size() == 0) { if (info.size() == 0) {
openIntent.setDataAndType(Uri.fromFile(file),"*/*"); openIntent.setDataAndType(uri,"*/*");
} }
try { try {
getContext().startActivity(openIntent); getContext().startActivity(openIntent);