1
0
Fork 0
mirror of https://github.com/M66B/FairEmail.git synced 2024-12-25 17:27:00 +00:00

Fixed checking attachment viewer

This commit is contained in:
M66B 2018-08-05 05:38:48 +00:00
parent eea2c15587
commit eba14cbf84

View file

@ -99,15 +99,26 @@ public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.Vi
});
} else {
// Build file name
File dir = new File(context.getCacheDir(), "attachments");
dir.mkdir();
final File dir = new File(context.getCacheDir(), "attachments");
final File file = new File(dir, TextUtils.isEmpty(attachment.name) ? "noname" : attachment.name);
// https://developer.android.com/reference/android/support/v4/content/FileProvider
Uri uri = FileProvider.getUriForFile(context, "eu.faircode.email", file);
// Build intent
final Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(uri);
//intent.setType(attachment.type);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Log.i(Helper.TAG, "Sharing " + file + " type=" + attachment.type);
// Set permissions
List<ResolveInfo> targets = context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : targets)
context.grantUriPermission(resolveInfo.activityInfo.packageName, uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
// Check if viewer available
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.fromFile(file));
PackageManager pm = context.getPackageManager();
if (pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY) == null) {
if (targets.size() == 0) {
Toast.makeText(context, R.string.title_no_viewer, Toast.LENGTH_LONG).show();
return;
}
@ -119,6 +130,7 @@ public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.Vi
try {
// Create file
if (!file.exists()) {
dir.mkdir();
file.createNewFile();
FileOutputStream fos = null;
@ -131,21 +143,6 @@ public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.Vi
}
}
// https://developer.android.com/reference/android/support/v4/content/FileProvider
Uri uri = FileProvider.getUriForFile(context, "eu.faircode.email", file);
// Build intent
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(uri);
//intent.setType(attachment.type);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Log.i(Helper.TAG, "Sharing " + file + " type=" + attachment.type);
// Set permissions
List<ResolveInfo> targets = context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : targets)
context.grantUriPermission(resolveInfo.activityInfo.packageName, uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
// Start viewer
context.startActivity(intent);
} catch (Throwable ex) {