diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/TemporaryStorageProvider.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/TemporaryStorageProvider.java index ca79f2027..fc3d43eaf 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/TemporaryStorageProvider.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/TemporaryStorageProvider.java @@ -18,6 +18,12 @@ package org.sufficientlysecure.keychain.provider; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.UUID; + import android.content.ClipDescription; import android.content.ContentProvider; import android.content.ContentValues; @@ -29,6 +35,7 @@ import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.net.Uri; import android.os.Bundle; +import android.os.CancellationSignal; import android.os.ParcelFileDescriptor; import android.provider.OpenableColumns; @@ -36,11 +43,6 @@ import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.util.DatabaseUtil; import org.sufficientlysecure.keychain.util.Log; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.util.UUID; - public class TemporaryStorageProvider extends ContentProvider { private static final String DB_NAME = "tempstorage.db"; @@ -73,6 +75,12 @@ public class TemporaryStorageProvider extends ContentProvider { return context.getContentResolver().insert(BASE_URI, contentValues); } + public static int setMimeType(Context context, Uri uri, String mimetype) { + ContentValues values = new ContentValues(); + values.put(COLUMN_TYPE, mimetype); + return context.getContentResolver().update(uri, values, null, null); + } + public static int cleanUp(Context context) { return context.getContentResolver().delete(BASE_URI, COLUMN_TIME + "< ?", new String[]{Long.toString(System.currentTimeMillis() - Constants.TEMPFILE_TTL)}); @@ -218,12 +226,6 @@ public class TemporaryStorageProvider extends ContentProvider { return 0; } - public int setMimeType(Uri uri, String mimetype) { - ContentValues values = new ContentValues(); - values.put(COLUMN_TYPE, mimetype); - return update(uri, values, null, null); - } - @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { if (values.size() != 1 || !values.containsKey(COLUMN_TYPE)) { @@ -240,4 +242,5 @@ public class TemporaryStorageProvider extends ContentProvider { public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException { return openFileHelper(uri, mode); } + } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptListFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptListFragment.java index 1199ebc27..036fbe8c5 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptListFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptListFragment.java @@ -25,10 +25,14 @@ import java.util.HashMap; import java.util.List; import android.app.Activity; +import android.content.ClipDescription; import android.content.Context; import android.content.Intent; import android.content.pm.LabeledIntent; import android.content.pm.ResolveInfo; +import android.graphics.Bitmap; +import android.graphics.Point; +import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.AsyncTask; @@ -270,12 +274,54 @@ public class DecryptListFragment private void processResult(final Uri uri, final DecryptVerifyResult result) { - Drawable icon = null; - OnClickListener onFileClick = null, onKeyClick = null; + new AsyncTask() { + @Override + protected Drawable doInBackground(Void... params) { - if (result.getDecryptMetadata() != null && result.getDecryptMetadata().getMimeType() != null) { - icon = loadIcon(result.getDecryptMetadata().getMimeType()); - } + Context context = getActivity(); + if (result.getDecryptMetadata() == null || context == null) { + return null; + } + + String type = result.getDecryptMetadata().getMimeType(); + Uri outputUri = mOutputUris.get(uri); + if (type == null || outputUri == null) { + return null; + } + + TemporaryStorageProvider.setMimeType(context, outputUri, type); + + if (ClipDescription.compareMimeTypes(type, "image/*")) { + int px = FormattingUtils.dpToPx(context, 48); + Bitmap bitmap = FileHelper.getThumbnail(context, outputUri, new Point(px, px)); + return new BitmapDrawable(context.getResources(), bitmap); + } + + final Intent intent = new Intent(Intent.ACTION_VIEW); + intent.setType(type); + + final List matches = + context.getPackageManager().queryIntentActivities(intent, 0); + //noinspection LoopStatementThatDoesntLoop + for (ResolveInfo match : matches) { + return match.loadIcon(getActivity().getPackageManager()); + } + + return null; + + } + + @Override + protected void onPostExecute(Drawable icon) { + processResult(uri, result, icon); + } + }.execute(); + + } + + private void processResult(final Uri uri, DecryptVerifyResult result, Drawable icon) { + + OnClickListener onFileClick = null, onKeyClick = null; OpenPgpSignatureResult sigResult = result.getSignatureResult(); if (sigResult != null) { @@ -300,7 +346,7 @@ public class DecryptListFragment onFileClick = new OnClickListener() { @Override public void onClick(View view) { - displayUri(uri); + displayWithViewIntent(uri); } }; } @@ -309,7 +355,7 @@ public class DecryptListFragment } - public void displayUri(final Uri uri) { + public void displayWithViewIntent(final Uri uri) { Activity activity = getActivity(); if (activity == null || mCurrentInputUri != null) { return; @@ -340,18 +386,7 @@ public class DecryptListFragment } Intent intent = new Intent(Intent.ACTION_VIEW); - intent.putExtra(DisplayTextActivity.EXTRA_METADATA, result); intent.setDataAndType(outputUri, "text/plain"); - - String plaintext; - try { - plaintext = FileHelper.readTextFromUri(activity, outputUri, result.getCharset()); - } catch (IOException e) { - Notify.create(activity, R.string.error_preparing_data, Style.ERROR).show(); - return null; - } - intent.putExtra(Intent.EXTRA_TEXT, plaintext); - return intent; } @@ -364,7 +399,9 @@ public class DecryptListFragment } LabeledIntent internalIntent = new LabeledIntent( - new Intent(intent).setClass(activity, DisplayTextActivity.class), + new Intent(intent) + .setClass(activity, DisplayTextActivity.class) + .putExtra(DisplayTextActivity.EXTRA_METADATA, result), BuildConfig.APPLICATION_ID, R.string.view_internal, R.drawable.ic_launcher); Intent chooserIntent = Intent.createChooser(intent, getString(R.string.intent_show)); @@ -376,20 +413,13 @@ public class DecryptListFragment }.execute(); - } else { - Intent intent = new Intent(activity, DisplayTextActivity.class); - intent.setAction(Intent.ACTION_VIEW); - - // put output uri as stream, and grant permission to other apps to use it + Intent intent = new Intent(Intent.ACTION_VIEW); + intent.setDataAndType(outputUri, metadata.getMimeType()); intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); - intent.putExtra(Intent.EXTRA_STREAM, outputUri); - intent.setType(metadata.getMimeType()); - - // put metadata, although this is not likely to be used - intent.putExtra(DisplayTextActivity.EXTRA_METADATA, result); Intent chooserIntent = Intent.createChooser(intent, getString(R.string.intent_show)); + chooserIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); activity.startActivity(chooserIntent); } @@ -763,18 +793,4 @@ public class DecryptListFragment } } - private Drawable loadIcon(String mimeType) { - final Intent intent = new Intent(Intent.ACTION_VIEW); - intent.setType(mimeType); - - final List matches = getActivity() - .getPackageManager().queryIntentActivities(intent, 0); - //noinspection LoopStatementThatDoesntLoop - for (ResolveInfo match : matches) { - return match.loadIcon(getActivity().getPackageManager()); - } - return null; - - } - } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DisplayTextActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DisplayTextActivity.java index 5f04eb43b..80ab30890 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DisplayTextActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DisplayTextActivity.java @@ -19,6 +19,8 @@ package org.sufficientlysecure.keychain.ui; +import java.io.IOException; + import android.app.Activity; import android.content.Intent; import android.os.Bundle; @@ -31,6 +33,9 @@ import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.intents.OpenKeychainIntents; import org.sufficientlysecure.keychain.operations.results.DecryptVerifyResult; import org.sufficientlysecure.keychain.ui.base.BaseActivity; +import org.sufficientlysecure.keychain.ui.util.Notify; +import org.sufficientlysecure.keychain.ui.util.Notify.Style; +import org.sufficientlysecure.keychain.util.FileHelper; import org.sufficientlysecure.keychain.util.Log; public class DisplayTextActivity extends BaseActivity { @@ -66,15 +71,19 @@ public class DisplayTextActivity extends BaseActivity { return; } - Log.d(Constants.TAG, "ACTION_DECRYPT_TEXT"); - DecryptVerifyResult result = intent.getParcelableExtra(EXTRA_METADATA); - String plaintext = intent.getStringExtra(Intent.EXTRA_TEXT); - if (plaintext != null && result != null) { + String plaintext; + try { + plaintext = FileHelper.readTextFromUri(this, intent.getData(), result.getCharset()); + } catch (IOException e) { + Toast.makeText(this, R.string.error_preparing_data, Toast.LENGTH_LONG).show(); + return; + } + + if (plaintext != null) { loadFragment(plaintext, result); } else { - Log.e(Constants.TAG, "Invalid data error!"); Toast.makeText(this, R.string.error_invalid_data, Toast.LENGTH_LONG).show(); finish(); }