From 22246afa4b05f4cd2ae0f011b9c2d829321a77ec Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Sat, 20 Jun 2015 05:29:22 +0200 Subject: [PATCH] use uris for clipboard data if available in DecryptActivity --- .../keychain/ui/DecryptActivity.java | 28 +++++++++++++++---- .../keychain/ui/EncryptFilesFragment.java | 4 +-- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptActivity.java index 07e227d55..d14c8555c 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptActivity.java @@ -22,6 +22,9 @@ import java.io.OutputStream; import java.util.ArrayList; import android.app.Activity; +import android.content.ClipData; +import android.content.ClipboardManager; +import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Bundle; @@ -32,9 +35,7 @@ import android.widget.Toast; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; -import org.sufficientlysecure.keychain.compatibility.ClipboardReflection; import org.sufficientlysecure.keychain.intents.OpenKeychainIntents; -import org.sufficientlysecure.keychain.pgp.PgpHelper; import org.sufficientlysecure.keychain.provider.TemporaryStorageProvider; import org.sufficientlysecure.keychain.ui.base.BaseActivity; @@ -87,7 +88,6 @@ public class DecryptActivity extends BaseActivity { try { - // TODO handle ACTION_DECRYPT_FROM_CLIPBOARD switch (action) { case Intent.ACTION_SEND: { // When sending to Keychain Decrypt via share menu @@ -124,9 +124,25 @@ public class DecryptActivity extends BaseActivity { case ACTION_DECRYPT_FROM_CLIPBOARD: { action = ACTION_DECRYPT_DATA; - CharSequence clipboardText = ClipboardReflection.getClipboardText(this); - String text = PgpHelper.getPgpContent(clipboardText); - Uri uri = readToTempFile(text); + ClipboardManager clipMan = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); + ClipData clip = clipMan.getPrimaryClip(); + + // check if data is available as uri + Uri uri = null; + for (int i = 0; i < clip.getItemCount(); i++) { + ClipData.Item item = clip.getItemAt(i); + Uri itemUri = item.getUri(); + if (itemUri != null) { + uri = itemUri; + break; + } + } + + // otherwise, coerce to text (almost always possible) and work from there + if (uri == null) { + String text = clip.getItemAt(0).coerceToText(this).toString(); + uri = readToTempFile(text); + } uris.add(uri); break; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptFilesFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptFilesFragment.java index 57c82ef9c..c14f957f7 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptFilesFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptFilesFragment.java @@ -51,7 +51,6 @@ import android.widget.TextView; import org.spongycastle.bcpg.CompressionAlgorithmTags; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; -import org.sufficientlysecure.keychain.compatibility.ClipboardReflection; import org.sufficientlysecure.keychain.operations.results.SignEncryptResult; import org.sufficientlysecure.keychain.pgp.KeyRing; import org.sufficientlysecure.keychain.pgp.PgpConstants; @@ -422,9 +421,8 @@ public class EncryptFilesFragment } ClipboardManager clipMan = (ClipboardManager) activity.getSystemService(Context.CLIPBOARD_SERVICE); - // ClipData clip = ClipData.newUri(getActivity().getContentResolver(), - // getString(R.string.label_clip_title), mOutputUris.get(0)); ClipData clip = new ClipData(getString(R.string.label_clip_title), + // make available as application/pgp-encrypted new String[] { "text/plain" }, new ClipData.Item(mOutputUris.get(0)) );