diff --git a/AndroidManifest.xml b/AndroidManifest.xml index d05b0e020..d79421e44 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -126,7 +126,7 @@ - + @@ -141,7 +141,7 @@ - + diff --git a/src/org/thialfihar/android/apg/Apg.java b/src/org/thialfihar/android/apg/Apg.java index 744bb736c..5b1f66414 100644 --- a/src/org/thialfihar/android/apg/Apg.java +++ b/src/org/thialfihar/android/apg/Apg.java @@ -110,6 +110,7 @@ public class Apg { public static final String SELECT_SECRET_KEY = "org.thialfihar.android.apg.intent.SELECT_SECRET_KEY"; } + public static final String EXTRA_TEXT = "text"; public static final String EXTRA_DATA = "data"; public static final String EXTRA_STATUS = "status"; public static final String EXTRA_ERROR = "error"; @@ -1702,6 +1703,23 @@ public class Apg { return returnData; } + public static int getStreamContent(Context context, InputStream inStream) + throws IOException { + InputStream in = PGPUtil.getDecoderStream(inStream); + PGPObjectFactory pgpF = new PGPObjectFactory(in); + Object object = pgpF.nextObject(); + while (object != null) { + if (object instanceof PGPPublicKeyRing || + object instanceof PGPSecretKeyRing) { + return Id.content.keys; + } else if (object instanceof PGPEncryptedDataList) { + return Id.content.encrypted_data; + } + } + + return Id.content.unknown; + } + // taken from ClearSignedFileProcessor in BC private static int readInputLine(ByteArrayOutputStream bOut, InputStream fIn) throws IOException { diff --git a/src/org/thialfihar/android/apg/DecryptActivity.java b/src/org/thialfihar/android/apg/DecryptActivity.java index de8dcb3ff..42c4d0b9c 100644 --- a/src/org/thialfihar/android/apg/DecryptActivity.java +++ b/src/org/thialfihar/android/apg/DecryptActivity.java @@ -178,25 +178,12 @@ public class DecryptActivity extends BaseActivity { } catch (IOException e) { // ignore, then } - } else if (Intent.ACTION_SEND.equals(mIntent.getAction())) { - Bundle extras = mIntent.getExtras(); - if (extras == null) { - extras = new Bundle(); - } - String data = extras.getString(Intent.EXTRA_TEXT); - if (data != null) { - mMessage.setText(data); - } - mSubject = extras.getString(Intent.EXTRA_SUBJECT); - if (mSubject != null && mSubject.startsWith("Fwd: ")) { - mSubject = mSubject.substring(5); - } } else if (Apg.Intent.DECRYPT.equals(mIntent.getAction())) { Bundle extras = mIntent.getExtras(); if (extras == null) { extras = new Bundle(); } - String data = extras.getString(Apg.EXTRA_DATA); + String data = extras.getString(Apg.EXTRA_TEXT); if (data != null) { Matcher matcher = Apg.PGP_MESSAGE.matcher(data); if (matcher.matches()) { @@ -218,6 +205,11 @@ public class DecryptActivity extends BaseActivity { mReplyTo = extras.getString(Apg.EXTRA_REPLY_TO); mSubject = extras.getString(Apg.EXTRA_SUBJECT); } else if (Apg.Intent.DECRYPT_FILE.equals(mIntent.getAction())) { + if ("file".equals(mIntent.getScheme())) { + mInputFilename = mIntent.getDataString().replace("file://", ""); + mFilename.setText(mInputFilename); + guessOutputFilename(); + } mSource.setInAnimation(null); mSource.setOutAnimation(null); while (mSource.getCurrentView().getId() != R.id.sourceFile) { @@ -228,7 +220,7 @@ public class DecryptActivity extends BaseActivity { if (extras == null) { extras = new Bundle(); } - String data = extras.getString(Apg.EXTRA_DATA); + String data = extras.getString(Apg.EXTRA_TEXT); if (data != null) { Matcher matcher = Apg.PGP_MESSAGE.matcher(data); if (matcher.matches()) { @@ -450,7 +442,7 @@ public class DecryptActivity extends BaseActivity { String data = mMessage.getText().toString(); data = data.replaceAll("(?m)^", "> "); data = "\n\n" + data; - intent.putExtra(Apg.EXTRA_DATA, data); + intent.putExtra(Apg.EXTRA_TEXT, data); intent.putExtra(Apg.EXTRA_SUBJECT, "Re: " + mSubject); intent.putExtra(Apg.EXTRA_SEND_TO, mReplyTo); intent.putExtra(Apg.EXTRA_SIGNATURE_KEY_ID, getSecretKeyId()); diff --git a/src/org/thialfihar/android/apg/EncryptActivity.java b/src/org/thialfihar/android/apg/EncryptActivity.java index 926d79599..da74e9a89 100644 --- a/src/org/thialfihar/android/apg/EncryptActivity.java +++ b/src/org/thialfihar/android/apg/EncryptActivity.java @@ -279,7 +279,7 @@ public class EncryptActivity extends BaseActivity { mReturnResult = true; } - String data = extras.getString(Apg.EXTRA_DATA); + String data = extras.getString(Apg.EXTRA_TEXT); mSendTo = extras.getString(Apg.EXTRA_SEND_TO); mSubject = extras.getString(Apg.EXTRA_SUBJECT); long signatureKeyId = extras.getLong(Apg.EXTRA_SIGNATURE_KEY_ID); @@ -335,6 +335,11 @@ public class EncryptActivity extends BaseActivity { mSource.showNext(); } } else if (Apg.Intent.ENCRYPT_FILE.equals(mIntent.getAction())) { + if ("file".equals(mIntent.getScheme())) { + mInputFilename = mIntent.getDataString().replace("file://", ""); + mFilename.setText(mInputFilename); + guessOutputFilename(); + } mSource.setInAnimation(null); mSource.setOutAnimation(null); while (mSource.getCurrentView().getId() != R.id.sourceFile) { @@ -403,6 +408,7 @@ public class EncryptActivity extends BaseActivity { mSourceLabel.setText(R.string.label_file); mEncryptButton.setText(R.string.btn_encrypt); mEncryptToClipboardButton.setEnabled(false); + mEncryptToClipboardButton.setVisibility(View.INVISIBLE); break; } @@ -410,6 +416,7 @@ public class EncryptActivity extends BaseActivity { mSourceLabel.setText(R.string.label_message); mEncryptButton.setText(R.string.btn_send); mEncryptToClipboardButton.setEnabled(true); + mEncryptToClipboardButton.setVisibility(View.VISIBLE); break; } diff --git a/src/org/thialfihar/android/apg/GeneralActivity.java b/src/org/thialfihar/android/apg/GeneralActivity.java index af919bb48..a66a7c1b4 100644 --- a/src/org/thialfihar/android/apg/GeneralActivity.java +++ b/src/org/thialfihar/android/apg/GeneralActivity.java @@ -2,6 +2,7 @@ package org.thialfihar.android.apg; import java.io.ByteArrayInputStream; import java.io.FileNotFoundException; +import java.io.IOException; import java.io.InputStream; import java.util.Vector; @@ -24,6 +25,8 @@ public class GeneralActivity extends BaseActivity { private ArrayAdapter mAdapter; private ListView mList; private Button mCancelButton; + private String mDataString; + private Uri mDataUri; @Override protected void onCreate(Bundle savedInstanceState) { @@ -33,48 +36,57 @@ public class GeneralActivity extends BaseActivity { mIntent = getIntent(); - boolean isEncrypted = false; - boolean containsKeys = false; - InputStream inStream = null; { - byte[] data = mIntent.getByteArrayExtra(Intent.EXTRA_TEXT); + String data = mIntent.getStringExtra(Intent.EXTRA_TEXT); if (data != null) { - inStream = new ByteArrayInputStream(data); + mDataString = data; + inStream = new ByteArrayInputStream(data.getBytes()); } } if (inStream == null) { Uri data = mIntent.getData(); if (data != null) { + mDataUri = data; try { inStream = getContentResolver().openInputStream(data); } catch (FileNotFoundException e) { // didn't work - Toast.makeText(this, "failed to open stream", Toast.LENGTH_SHORT); + Toast.makeText(this, "failed to open stream", Toast.LENGTH_SHORT).show(); } } } if (inStream == null) { - Toast.makeText(this, "no data found", Toast.LENGTH_SHORT); + Toast.makeText(this, "no data found", Toast.LENGTH_SHORT).show(); finish(); return; } + int contentType = Id.content.unknown; + try { + contentType = Apg.getStreamContent(this, inStream); + inStream.close(); + } catch (IOException e) { + // just means that there's no PGP data in there + } + mList = (ListView) findViewById(R.id.options); Vector choices = new Vector(); - if (containsKeys) { - choices.add(new Choice(Id.choice.action.import_public, getString(R.string.action_importPublic))); - choices.add(new Choice(Id.choice.action.import_secret, getString(R.string.action_importSecret))); + if (contentType == Id.content.keys) { + choices.add(new Choice(Id.choice.action.import_public, + getString(R.string.action_importPublic))); + choices.add(new Choice(Id.choice.action.import_secret, + getString(R.string.action_importSecret))); } - if (isEncrypted) { + if (contentType == Id.content.encrypted_data) { choices.add(new Choice(Id.choice.action.decrypt, getString(R.string.action_decrypt))); } - if (!containsKeys && !isEncrypted) { + if (contentType == Id.content.unknown) { choices.add(new Choice(Id.choice.action.encrypt, getString(R.string.action_encrypt))); } @@ -107,17 +119,27 @@ public class GeneralActivity extends BaseActivity { switch (id) { case Id.choice.action.encrypt: { intent.setClass(this, EncryptActivity.class); - if (mIntent.hasExtra(Intent.EXTRA_TEXT)) { - intent.putExtra(Intent.EXTRA_TEXT, mIntent.getByteArrayExtra(Intent.EXTRA_TEXT)); - } else if (mIntent.getData() != null) { - intent.setData(mIntent.getData()); - intent.setType(mIntent.getType()); + if (mDataString != null) { + intent.setAction(Apg.Intent.ENCRYPT); + intent.putExtra(Apg.EXTRA_TEXT, mDataString); + } else if (mDataUri != null) { + intent.setAction(Apg.Intent.ENCRYPT_FILE); + intent.setDataAndType(mDataUri, mIntent.getType()); } break; } case Id.choice.action.decrypt: { + intent.setClass(this, DecryptActivity.class); + if (mDataString != null) { + intent.setAction(Apg.Intent.DECRYPT); + intent.putExtra(Apg.EXTRA_TEXT, mDataString); + } else if (mDataUri != null) { + intent.setAction(Apg.Intent.DECRYPT_FILE); + intent.setDataAndType(mDataUri, mIntent.getType()); + } + break; } diff --git a/src/org/thialfihar/android/apg/Id.java b/src/org/thialfihar/android/apg/Id.java index 72942d2ba..69966e2ff 100644 --- a/src/org/thialfihar/android/apg/Id.java +++ b/src/org/thialfihar/android/apg/Id.java @@ -139,4 +139,10 @@ public final class Id { public static final int none = 0; public static final int symmetric = -1; } + + public static final class content { + public static final int unknown = 0; + public static final int encrypted_data = 1; + public static final int keys = 2; + } } diff --git a/src/org/thialfihar/android/apg/MailListActivity.java b/src/org/thialfihar/android/apg/MailListActivity.java index d166feede..fb4d39f6a 100644 --- a/src/org/thialfihar/android/apg/MailListActivity.java +++ b/src/org/thialfihar/android/apg/MailListActivity.java @@ -153,7 +153,7 @@ public class MailListActivity extends ListActivity { Intent intent = new Intent(MailListActivity.this, DecryptActivity.class); intent.setAction(Apg.Intent.DECRYPT); Message message = (Message) ((MailboxAdapter) getListAdapter()).getItem(position); - intent.putExtra(Apg.EXTRA_DATA, message.data); + intent.putExtra(Apg.EXTRA_TEXT, message.data); intent.putExtra(Apg.EXTRA_SUBJECT, message.subject); intent.putExtra(Apg.EXTRA_REPLY_TO, message.replyTo); startActivity(intent);