aboutsummaryrefslogtreecommitdiffstats
path: root/src/org/thialfihar/android/apg/DecryptActivity.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/org/thialfihar/android/apg/DecryptActivity.java')
-rw-r--r--src/org/thialfihar/android/apg/DecryptActivity.java169
1 files changed, 77 insertions, 92 deletions
diff --git a/src/org/thialfihar/android/apg/DecryptActivity.java b/src/org/thialfihar/android/apg/DecryptActivity.java
index 571edbdf5..d8b7ccd9c 100644
--- a/src/org/thialfihar/android/apg/DecryptActivity.java
+++ b/src/org/thialfihar/android/apg/DecryptActivity.java
@@ -16,12 +16,9 @@
package org.thialfihar.android.apg;
-import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
-import java.io.FileInputStream;
import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@@ -88,6 +85,11 @@ public class DecryptActivity extends BaseActivity {
private String mOutputFilename = null;
private Uri mContentUri = null;
+ private byte[] mData = null;
+ private boolean mReturnBinary = false;
+
+ private DataSource mDataSource = null;
+ private DataDestination mDataDestination = null;
@Override
public void onCreate(Bundle savedInstanceState) {
@@ -186,21 +188,26 @@ public class DecryptActivity extends BaseActivity {
if (extras == null) {
extras = new Bundle();
}
- String data = extras.getString(Apg.EXTRA_TEXT);
- if (data != null) {
- Matcher matcher = Apg.PGP_MESSAGE.matcher(data);
+
+ mData = extras.getByteArray(Apg.EXTRA_DATA);
+ String textData = null;
+ if (mData == null) {
+ textData = extras.getString(Apg.EXTRA_TEXT);
+ }
+ if (textData != null) {
+ Matcher matcher = Apg.PGP_MESSAGE.matcher(textData);
if (matcher.matches()) {
- data = matcher.group(1);
+ textData = matcher.group(1);
// replace non breakable spaces
- data = data.replaceAll("\\xa0", " ");
- mMessage.setText(data);
+ textData = textData.replaceAll("\\xa0", " ");
+ mMessage.setText(textData);
} else {
- matcher = Apg.PGP_SIGNED_MESSAGE.matcher(data);
+ matcher = Apg.PGP_SIGNED_MESSAGE.matcher(textData);
if (matcher.matches()) {
- data = matcher.group(1);
+ textData = matcher.group(1);
// replace non breakable spaces
- data = data.replaceAll("\\xa0", " ");
- mMessage.setText(data);
+ textData = textData.replaceAll("\\xa0", " ");
+ mMessage.setText(textData);
mDecryptButton.setText(R.string.btn_verify);
}
}
@@ -208,15 +215,12 @@ 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())) {
+ mInputFilename = mIntent.getDataString();
if ("file".equals(mIntent.getScheme())) {
- mInputFilename = mIntent.getDataString().replace("file://", "");
- mFilename.setText(mInputFilename);
- guessOutputFilename();
- } else if ("content".equals(mIntent.getScheme())) {
- mInputFilename = mIntent.getDataString();
- mFilename.setText(mInputFilename);
- guessOutputFilename();
+ mInputFilename = mInputFilename.substring(6);
}
+ mFilename.setText(mInputFilename);
+ guessOutputFilename();
mSource.setInAnimation(null);
mSource.setOutAnimation(null);
while (mSource.getCurrentView().getId() != R.id.sourceFile) {
@@ -224,11 +228,15 @@ public class DecryptActivity extends BaseActivity {
}
} else if (Apg.Intent.DECRYPT_AND_RETURN.equals(mIntent.getAction())) {
mContentUri = mIntent.getData();
+ Bundle extras = mIntent.getExtras();
+ if (extras == null) {
+ extras = new Bundle();
+ }
+
+ mReturnBinary = extras.getBoolean(Apg.EXTRA_BINARY, false);
+
if (mContentUri == null) {
- Bundle extras = mIntent.getExtras();
- if (extras == null) {
- extras = new Bundle();
- }
+ mData = extras.getByteArray(Apg.EXTRA_DATA);
String data = extras.getString(Apg.EXTRA_TEXT);
if (data != null) {
Matcher matcher = Apg.PGP_MESSAGE.matcher(data);
@@ -397,19 +405,9 @@ public class DecryptActivity extends BaseActivity {
// else treat it as an decrypted message/file
mSignedOnly = false;
String error = null;
+ fillDataSource();
try {
- InputStream in;
- if (mContentUri != null) {
- in = getContentResolver().openInputStream(mContentUri);
- } else if (mDecryptTarget == Id.target.file) {
- if (mInputFilename.startsWith("file")) {
- in = new FileInputStream(mInputFilename);
- } else {
- in = getContentResolver().openInputStream(Uri.parse(mInputFilename));
- }
- } else {
- in = new ByteArrayInputStream(mMessage.getText().toString().getBytes());
- }
+ InputData in = mDataSource.getInputData(this, false);
try {
setSecretKeyId(Apg.getDecryptionKeyId(this, in));
if (getSecretKeyId() == Id.key.none) {
@@ -418,19 +416,7 @@ public class DecryptActivity extends BaseActivity {
mAssumeSymmetricEncryption = false;
} catch (Apg.NoAsymmetricEncryptionException e) {
setSecretKeyId(Id.key.symmetric);
- // look at the file/message again to check whether there's
- // symmetric encryption data in there
- if (mContentUri != null) {
- in = getContentResolver().openInputStream(mContentUri);
- } else if (mDecryptTarget == Id.target.file) {
- if (mInputFilename.startsWith("file")) {
- in = new FileInputStream(mInputFilename);
- } else {
- in = getContentResolver().openInputStream(Uri.parse(mInputFilename));
- }
- } else {
- in = new ByteArrayInputStream(mMessage.getText().toString().getBytes());
- }
+ in = mDataSource.getInputData(this, false);
if (!Apg.hasSymmetricEncryption(this, in)) {
throw new Apg.GeneralException(getString(R.string.error_noKnownEncryptionFound));
}
@@ -500,60 +486,32 @@ public class DecryptActivity extends BaseActivity {
Bundle data = new Bundle();
Message msg = new Message();
-
+ fillDataSource();
+ fillDataDestination();
try {
- PositionAwareInputStream in = null;
- OutputStream out = null;
- String randomString = null;
- long size = 0;
-
- if (mContentUri != null) {
- in = new PositionAwareInputStream(getContentResolver().openInputStream(mContentUri));
- size = Apg.getLengthOfStream(getContentResolver().openInputStream(mContentUri));
- try {
- while (true) {
- randomString = Apg.generateRandomString(32);
- if (randomString == null) {
- throw new Apg.GeneralException("couldn't generate random file name");
- }
- this.openFileInput(randomString).close();
- }
- } catch (FileNotFoundException e) {
- // found a name that isn't used yet
- }
- out = openFileOutput(randomString, MODE_PRIVATE);
- } else if (mDecryptTarget == Id.target.message) {
- String messageData = mMessage.getText().toString();
- in = new PositionAwareInputStream(new ByteArrayInputStream(messageData.getBytes()));
- out = new ByteArrayOutputStream();
- size = messageData.getBytes().length;
- } else {
- if (mInputFilename.startsWith("content")) {
- size = Apg.getLengthOfStream(getContentResolver().openInputStream(Uri.parse(mInputFilename)));
- in = new PositionAwareInputStream(
- getContentResolver().openInputStream(Uri.parse(mInputFilename)));
- } else {
- in = new PositionAwareInputStream(new FileInputStream(mInputFilename));
- File file = new File(mInputFilename);
- size = file.length();
- }
- out = new FileOutputStream(mOutputFilename);
- }
+ InputData in = mDataSource.getInputData(this, true);
+ OutputStream out = mDataDestination.getOutputStream(this);
if (mSignedOnly) {
data = Apg.verifyText(this, in, out, this);
} else {
- data = Apg.decrypt(this, in, out, size, Apg.getCachedPassPhrase(getSecretKeyId()),
+ data = Apg.decrypt(this, in, out, Apg.getCachedPassPhrase(getSecretKeyId()),
this, mAssumeSymmetricEncryption);
}
out.close();
- if (randomString != null) {
- data.putString(Apg.EXTRA_RESULT_URI, "content://" + DataProvider.AUTHORITY + "/data/" + randomString);
+ if (mDataDestination.getStreamFilename() != null) {
+ data.putString(Apg.EXTRA_RESULT_URI, "content://" + DataProvider.AUTHORITY +
+ "/data/" + mDataDestination.getStreamFilename());
} else if (mDecryptTarget == Id.target.message) {
- data.putString(Apg.EXTRA_DECRYPTED_MESSAGE,
- new String(((ByteArrayOutputStream) out).toByteArray()));
+ if (mReturnBinary) {
+ data.putByteArray(Apg.EXTRA_DECRYPTED_DATA,
+ ((ByteArrayOutputStream) out).toByteArray());
+ } else {
+ data.putString(Apg.EXTRA_DECRYPTED_MESSAGE,
+ new String(((ByteArrayOutputStream) out).toByteArray()));
+ }
}
} catch (PGPException e) {
error = "" + e;
@@ -727,4 +685,31 @@ public class DecryptActivity extends BaseActivity {
return super.onCreateDialog(id);
}
+
+ protected void fillDataSource() {
+ mDataSource = new DataSource();
+ if (mContentUri != null) {
+ mDataSource.setUri(mContentUri);
+ } else if (mDecryptTarget == Id.target.file) {
+ mDataSource.setUri(mInputFilename);
+ } else {
+ if (mData != null) {
+ mDataSource.setData(mData);
+ } else {
+ mDataSource.setText(mMessage.getText().toString());
+ }
+ }
+ }
+
+ protected void fillDataDestination() {
+ mDataDestination = new DataDestination();
+ if (mContentUri != null) {
+ mDataDestination.setMode(Id.mode.stream);
+ } else if (mDecryptTarget == Id.target.file) {
+ mDataDestination.setFilename(mOutputFilename);
+ mDataDestination.setMode(Id.mode.file);
+ } else {
+ mDataDestination.setMode(Id.mode.byte_array);
+ }
+ }
}