working encryption/decryption of text and files after GeneralActivity hands it over

This commit is contained in:
Thialfihar 2010-06-06 00:35:00 +00:00
parent 8c404d83d7
commit 26a500956f
7 changed files with 82 additions and 37 deletions

View File

@ -126,7 +126,7 @@
<action android:name="org.thialfihar.android.apg.intent.ENCRYPT_FILE" />
<action android:name="org.thialfihar.android.apg.intent.ENCRYPT_AND_RETURN" />
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/*"/>
<data android:mimeType="*/*"/>
</intent-filter>
</activity>
@ -141,7 +141,7 @@
<action android:name="org.thialfihar.android.apg.intent.DECRYPT_FILE" />
<action android:name="org.thialfihar.android.apg.intent.DECRYPT_AND_RETURN" />
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/*"/>
<data android:mimeType="*/*"/>
</intent-filter>
</activity>

View File

@ -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 {

View File

@ -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());

View File

@ -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;
}

View File

@ -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<Choice> 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<Choice> choices = new Vector<Choice>();
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;
}

View File

@ -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;
}
}

View File

@ -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);