improving definition send and view intents

This commit is contained in:
Dominik 2012-09-12 18:35:58 +02:00
parent 511cc4af9f
commit 054ad2bb97
3 changed files with 281 additions and 191 deletions

View File

@ -24,6 +24,7 @@
<!-- <!--
General remarks General remarks
=============== ===============
- Last APG 1 version was 10900 (1.0.9 beta 00)
- APG 2 starting with versionCode 20000! - APG 2 starting with versionCode 20000!
@ -88,6 +89,7 @@
<intent-filter> <intent-filter>
<action android:name="android.intent.action.SEARCH" /> <action android:name="android.intent.action.SEARCH" />
</intent-filter> </intent-filter>
<!-- APG's own Actions -->
<intent-filter android:label="@string/intent_import_key" > <intent-filter android:label="@string/intent_import_key" >
<action android:name="org.thialfihar.android.apg.intent.IMPORT" /> <action android:name="org.thialfihar.android.apg.intent.IMPORT" />
@ -95,6 +97,7 @@
<data android:mimeType="*/*" /> <data android:mimeType="*/*" />
</intent-filter> </intent-filter>
<!-- Linking "Import key" to file types -->
<intent-filter android:label="@string/intent_import_key" > <intent-filter android:label="@string/intent_import_key" >
<action android:name="android.intent.action.VIEW" /> <action android:name="android.intent.action.VIEW" />
@ -212,6 +215,8 @@
android:label="@string/title_encrypt" android:label="@string/title_encrypt"
android:uiOptions="splitActionBarWhenNarrow" android:uiOptions="splitActionBarWhenNarrow"
android:windowSoftInputMode="stateHidden" > android:windowSoftInputMode="stateHidden" >
<!-- APG's own Actions -->
<intent-filter> <intent-filter>
<action android:name="org.thialfihar.android.apg.intent.ENCRYPT" /> <action android:name="org.thialfihar.android.apg.intent.ENCRYPT" />
<action android:name="org.thialfihar.android.apg.intent.ENCRYPT_FILE" /> <action android:name="org.thialfihar.android.apg.intent.ENCRYPT_FILE" />
@ -220,6 +225,14 @@
<category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="*/*" />
</intent-filter>
<!-- Android's Send Action -->
<intent-filter android:label="@string/intent_send_encrypt" >
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="*/*" /> <data android:mimeType="*/*" />
</intent-filter> </intent-filter>
</activity> </activity>
@ -229,6 +242,8 @@
android:label="@string/title_decrypt" android:label="@string/title_decrypt"
android:uiOptions="splitActionBarWhenNarrow" android:uiOptions="splitActionBarWhenNarrow"
android:windowSoftInputMode="stateHidden" > android:windowSoftInputMode="stateHidden" >
<!-- APG's own Actions -->
<intent-filter> <intent-filter>
<action android:name="org.thialfihar.android.apg.intent.DECRYPT" /> <action android:name="org.thialfihar.android.apg.intent.DECRYPT" />
<action android:name="org.thialfihar.android.apg.intent.DECRYPT_FILE" /> <action android:name="org.thialfihar.android.apg.intent.DECRYPT_FILE" />
@ -238,6 +253,15 @@
<data android:mimeType="*/*" /> <data android:mimeType="*/*" />
</intent-filter> </intent-filter>
<!-- Android's Send Action -->
<intent-filter android:label="@string/intent_send_decrypt" >
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="*/*" />
</intent-filter>
<!-- Linking "Decrypt" to file types -->
<intent-filter android:label="@string/intent_decrypt_file" > <intent-filter android:label="@string/intent_decrypt_file" >
<action android:name="android.intent.action.VIEW" /> <action android:name="android.intent.action.VIEW" />

View File

@ -58,11 +58,9 @@ import android.widget.Toast;
import android.widget.ViewFlipper; import android.widget.ViewFlipper;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -83,8 +81,6 @@ public class DecryptActivity extends SherlockFragmentActivity {
private long mSignatureKeyId = 0; private long mSignatureKeyId = 0;
private Intent mIntent;
private boolean mReturnResult = false; private boolean mReturnResult = false;
private String mReplyTo = null; private String mReplyTo = null;
private String mSubject = null; private String mSubject = null;
@ -253,10 +249,12 @@ public class DecryptActivity extends SherlockFragmentActivity {
boolean decryptImmediately = false; boolean decryptImmediately = false;
mIntent = getIntent(); // Get intent, action and MIME type
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
// handled separately from other actions as it uses mIntent.setAction() if (Intent.ACTION_VIEW.equals(action)) {
if (Intent.ACTION_VIEW.equals(mIntent.getAction())) {
// TODO: old implementation of ACTION_VIEW. Is this used in K9? // TODO: old implementation of ACTION_VIEW. Is this used in K9?
@ -279,100 +277,29 @@ public class DecryptActivity extends SherlockFragmentActivity {
// } // }
// same as ACTION_DECRYPT_FILE but decrypt it immediately // same as ACTION_DECRYPT_FILE but decrypt it immediately
mIntent.setAction(ACTION_DECRYPT_FILE); handleActionDecryptFile(intent);
decryptImmediately = true;
} else if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("text/plain".equals(type)) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
if (sharedText != null) {
intent.putExtra(EXTRA_TEXT, sharedText);
handleActionDecrypt(intent);
decryptImmediately = true; decryptImmediately = true;
} }
if (ACTION_DECRYPT.equals(mIntent.getAction())) {
Log.d(Constants.TAG, "Apg Intent DECRYPT startet");
Bundle extras = mIntent.getExtras();
if (extras == null) {
Log.d(Constants.TAG, "extra bundle was null");
extras = new Bundle();
} else { } else {
Log.d(Constants.TAG, "got extras"); Uri uri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
} if (uri != null) {
// TODO: Implement for binary
mData = extras.getByteArray(EXTRA_DATA);
String textData = null;
if (mData == null) {
Log.d(Constants.TAG, "EXTRA_DATA was null");
textData = extras.getString(EXTRA_TEXT);
} else {
Log.d(Constants.TAG, "Got data from EXTRA_DATA");
}
if (textData != null) {
Log.d(Constants.TAG, "textData null, matching text ...");
Matcher matcher = PGPMain.PGP_MESSAGE.matcher(textData);
if (matcher.matches()) {
Log.d(Constants.TAG, "PGP_MESSAGE matched");
textData = matcher.group(1);
// replace non breakable spaces
textData = textData.replaceAll("\\xa0", " ");
mMessage.setText(textData);
} else {
matcher = PGPMain.PGP_SIGNED_MESSAGE.matcher(textData);
if (matcher.matches()) {
Log.d(Constants.TAG, "PGP_SIGNED_MESSAGE matched");
textData = matcher.group(1);
// replace non breakable spaces
textData = textData.replaceAll("\\xa0", " ");
mMessage.setText(textData);
mDecryptString = getString(R.string.btn_verify);
// build new action bar
invalidateOptionsMenu();
} else {
Log.d(Constants.TAG, "Nothing matched!");
} }
} }
} } else if (ACTION_DECRYPT.equals(action)) {
mReplyTo = extras.getString(EXTRA_REPLY_TO); handleActionDecrypt(intent);
mSubject = extras.getString(EXTRA_SUBJECT); } else if (ACTION_DECRYPT_FILE.equals(action)) {
} else if (ACTION_DECRYPT_FILE.equals(mIntent.getAction())) { handleActionDecryptFile(intent);
mInputFilename = mIntent.getData().getPath(); } else if (ACTION_DECRYPT_AND_RETURN.equals(action)) {
mFilename.setText(mInputFilename); handleActionDecryptAndReturn(intent);
guessOutputFilename();
mSource.setInAnimation(null);
mSource.setOutAnimation(null);
while (mSource.getCurrentView().getId() != R.id.sourceFile) {
mSource.showNext();
}
} else if (ACTION_DECRYPT_AND_RETURN.equals(mIntent.getAction())) {
mContentUri = mIntent.getData();
Bundle extras = mIntent.getExtras();
if (extras == null) {
extras = new Bundle();
}
mReturnBinary = extras.getBoolean(EXTRA_BINARY, false);
if (mContentUri == null) {
mData = extras.getByteArray(EXTRA_DATA);
String data = extras.getString(EXTRA_TEXT);
if (data != null) {
Matcher matcher = PGPMain.PGP_MESSAGE.matcher(data);
if (matcher.matches()) {
data = matcher.group(1);
// replace non breakable spaces
data = data.replaceAll("\\xa0", " ");
mMessage.setText(data);
} else {
matcher = PGPMain.PGP_SIGNED_MESSAGE.matcher(data);
if (matcher.matches()) {
data = matcher.group(1);
// replace non breakable spaces
data = data.replaceAll("\\xa0", " ");
mMessage.setText(data);
mDecryptString = getString(R.string.btn_verify);
// build new action bar
invalidateOptionsMenu();
}
}
}
}
mReturnResult = true;
} }
if (mSource.getCurrentView().getId() == R.id.sourceMessage if (mSource.getCurrentView().getId() == R.id.sourceMessage
@ -437,6 +364,118 @@ public class DecryptActivity extends SherlockFragmentActivity {
} }
} }
/**
* Handles activity intent with ACTION_DECRYPT
*
* @param intent
*/
private void handleActionDecrypt(Intent intent) {
Log.d(Constants.TAG, "Apg Intent DECRYPT startet");
Bundle extras = intent.getExtras();
if (extras == null) {
Log.d(Constants.TAG, "extra bundle was null");
extras = new Bundle();
} else {
Log.d(Constants.TAG, "got extras");
}
mData = extras.getByteArray(EXTRA_DATA);
String textData = null;
if (mData == null) {
Log.d(Constants.TAG, "EXTRA_DATA was null");
textData = extras.getString(EXTRA_TEXT);
} else {
Log.d(Constants.TAG, "Got data from EXTRA_DATA");
}
if (textData != null) {
Log.d(Constants.TAG, "textData null, matching text ...");
Matcher matcher = PGPMain.PGP_MESSAGE.matcher(textData);
if (matcher.matches()) {
Log.d(Constants.TAG, "PGP_MESSAGE matched");
textData = matcher.group(1);
// replace non breakable spaces
textData = textData.replaceAll("\\xa0", " ");
mMessage.setText(textData);
} else {
matcher = PGPMain.PGP_SIGNED_MESSAGE.matcher(textData);
if (matcher.matches()) {
Log.d(Constants.TAG, "PGP_SIGNED_MESSAGE matched");
textData = matcher.group(1);
// replace non breakable spaces
textData = textData.replaceAll("\\xa0", " ");
mMessage.setText(textData);
mDecryptString = getString(R.string.btn_verify);
// build new action bar
invalidateOptionsMenu();
} else {
Log.d(Constants.TAG, "Nothing matched!");
}
}
}
mReplyTo = extras.getString(EXTRA_REPLY_TO);
mSubject = extras.getString(EXTRA_SUBJECT);
}
/**
* Handles activity intent with ACTION_DECRYPT_FILE
*
* @param intent
*/
private void handleActionDecryptFile(Intent intent) {
mInputFilename = intent.getData().getPath();
mFilename.setText(mInputFilename);
guessOutputFilename();
mSource.setInAnimation(null);
mSource.setOutAnimation(null);
while (mSource.getCurrentView().getId() != R.id.sourceFile) {
mSource.showNext();
}
}
/**
* Handles activity intent with ACTION_DECRYPT_AND_RETURN
*
* @param intent
*/
private void handleActionDecryptAndReturn(Intent intent) {
mContentUri = intent.getData();
Bundle extras = intent.getExtras();
if (extras == null) {
extras = new Bundle();
}
mReturnBinary = extras.getBoolean(EXTRA_BINARY, false);
if (mContentUri == null) {
mData = extras.getByteArray(EXTRA_DATA);
String data = extras.getString(EXTRA_TEXT);
if (data != null) {
Matcher matcher = PGPMain.PGP_MESSAGE.matcher(data);
if (matcher.matches()) {
data = matcher.group(1);
// replace non breakable spaces
data = data.replaceAll("\\xa0", " ");
mMessage.setText(data);
} else {
matcher = PGPMain.PGP_SIGNED_MESSAGE.matcher(data);
if (matcher.matches()) {
data = matcher.group(1);
// replace non breakable spaces
data = data.replaceAll("\\xa0", " ");
mMessage.setText(data);
mDecryptString = getString(R.string.btn_verify);
// build new action bar
invalidateOptionsMenu();
}
}
}
}
mReturnResult = true;
}
private void guessOutputFilename() { private void guessOutputFilename() {
mInputFilename = mFilename.getText().toString(); mInputFilename = mFilename.getText().toString();
File file = new File(mInputFilename); File file = new File(mInputFilename);

View File

@ -86,7 +86,6 @@ public class EncryptActivity extends SherlockFragmentActivity {
public static final String EXTRA_SIGNATURE_KEY_ID = "signatureKeyId"; public static final String EXTRA_SIGNATURE_KEY_ID = "signatureKeyId";
public static final String EXTRA_ENCRYPTION_KEY_IDS = "encryptionKeyIds"; public static final String EXTRA_ENCRYPTION_KEY_IDS = "encryptionKeyIds";
private Intent mIntent = null;
private String mSubject = null; private String mSubject = null;
private String mSendTo = null; private String mSendTo = null;
@ -343,23 +342,78 @@ public class EncryptActivity extends SherlockFragmentActivity {
} }
}); });
mIntent = getIntent(); // Get intent, action and MIME type
if (ACTION_ENCRYPT.equals(mIntent.getAction()) Intent intent = getIntent();
|| ACTION_ENCRYPT_FILE.equals(mIntent.getAction()) String action = intent.getAction();
|| ACTION_ENCRYPT_AND_RETURN.equals(mIntent.getAction()) String type = intent.getType();
|| ACTION_GENERATE_SIGNATURE.equals(mIntent.getAction())) {
mContentUri = mIntent.getData(); if (Intent.ACTION_SEND.equals(action) && type != null) {
Bundle extras = mIntent.getExtras(); if ("text/plain".equals(type)) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
if (sharedText != null) {
intent.setAction(ACTION_ENCRYPT);
intent.putExtra(EXTRA_TEXT, sharedText);
intent.putExtra(EXTRA_ASCII_ARMOUR, true);
handleActionEncryptSign(intent);
}
} else {
Uri uri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (uri != null) {
// TODO: Implement for binary
}
}
} else if (ACTION_ENCRYPT.equals(action) || ACTION_ENCRYPT_FILE.equals(action)
|| ACTION_ENCRYPT_AND_RETURN.equals(action)
|| ACTION_GENERATE_SIGNATURE.equals(action)) {
handleActionEncryptSign(intent);
}
updateView();
updateSource();
updateMode();
if (mReturnResult) {
mSourcePrevious.setClickable(false);
mSourcePrevious.setEnabled(false);
mSourcePrevious.setVisibility(View.INVISIBLE);
mSourceNext.setClickable(false);
mSourceNext.setEnabled(false);
mSourceNext.setVisibility(View.INVISIBLE);
mSourceLabel.setClickable(false);
mSourceLabel.setEnabled(false);
}
updateActionBarButtons();
if (mReturnResult
&& (mMessage.getText().length() > 0 || mData != null || mContentUri != null)
&& ((mEncryptionKeyIds != null && mEncryptionKeyIds.length > 0) || getSecretKeyId() != 0)) {
encryptClicked();
}
}
/**
* Handles all actions with this intent
*
* @param intent
*/
private void handleActionEncryptSign(Intent intent) {
String action = intent.getAction();
mContentUri = intent.getData();
Bundle extras = intent.getExtras();
if (extras == null) { if (extras == null) {
extras = new Bundle(); extras = new Bundle();
} }
if (ACTION_ENCRYPT_AND_RETURN.equals(mIntent.getAction()) if (ACTION_ENCRYPT_AND_RETURN.equals(action) || ACTION_GENERATE_SIGNATURE.equals(action)) {
|| ACTION_GENERATE_SIGNATURE.equals(mIntent.getAction())) {
mReturnResult = true; mReturnResult = true;
} }
if (ACTION_GENERATE_SIGNATURE.equals(mIntent.getAction())) { if (ACTION_GENERATE_SIGNATURE.equals(action)) {
mGenerateSignature = true; mGenerateSignature = true;
mOverrideAsciiArmour = true; mOverrideAsciiArmour = true;
mAsciiArmourDemand = false; mAsciiArmourDemand = false;
@ -420,9 +474,8 @@ public class EncryptActivity extends SherlockFragmentActivity {
} }
} }
if (ACTION_ENCRYPT.equals(mIntent.getAction()) if (ACTION_ENCRYPT.equals(action) || ACTION_ENCRYPT_AND_RETURN.equals(action)
|| ACTION_ENCRYPT_AND_RETURN.equals(mIntent.getAction()) || ACTION_GENERATE_SIGNATURE.equals(action)) {
|| ACTION_GENERATE_SIGNATURE.equals(mIntent.getAction())) {
if (textData != null) { if (textData != null) {
mMessage.setText(textData); mMessage.setText(textData);
} }
@ -431,8 +484,8 @@ public class EncryptActivity extends SherlockFragmentActivity {
while (mSource.getCurrentView().getId() != R.id.sourceMessage) { while (mSource.getCurrentView().getId() != R.id.sourceMessage) {
mSource.showNext(); mSource.showNext();
} }
} else if (ACTION_ENCRYPT_FILE.equals(mIntent.getAction())) { } else if (ACTION_ENCRYPT_FILE.equals(action)) {
mInputFilename = mIntent.getData().getPath(); mInputFilename = intent.getData().getPath();
mFilename.setText(mInputFilename); mFilename.setText(mInputFilename);
guessOutputFilename(); guessOutputFilename();
mSource.setInAnimation(null); mSource.setInAnimation(null);
@ -443,32 +496,6 @@ public class EncryptActivity extends SherlockFragmentActivity {
} }
} }
updateView();
updateSource();
updateMode();
if (mReturnResult) {
mSourcePrevious.setClickable(false);
mSourcePrevious.setEnabled(false);
mSourcePrevious.setVisibility(View.INVISIBLE);
mSourceNext.setClickable(false);
mSourceNext.setEnabled(false);
mSourceNext.setVisibility(View.INVISIBLE);
mSourceLabel.setClickable(false);
mSourceLabel.setEnabled(false);
}
updateActionBarButtons();
if (mReturnResult
&& (mMessage.getText().length() > 0 || mData != null || mContentUri != null)
&& ((mEncryptionKeyIds != null && mEncryptionKeyIds.length > 0) || getSecretKeyId() != 0)) {
encryptClicked();
}
}
private void guessOutputFilename() { private void guessOutputFilename() {
mInputFilename = mFilename.getText().toString(); mInputFilename = mFilename.getText().toString();
File file = new File(mInputFilename); File file = new File(mInputFilename);