eventbus: initial attempt, replace messenger hack with eventbus communication

This commit is contained in:
Vincent Breitmoser
2015-06-06 23:17:42 +02:00
parent 7cfc0d80d0
commit 074b6633b0
23 changed files with 529 additions and 688 deletions

View File

@@ -64,7 +64,8 @@ import org.sufficientlysecure.keychain.util.Preferences;
import java.util.ArrayList;
public class CertifyKeyFragment extends CachingCryptoOperationFragment<CertifyActionsParcel>
public class CertifyKeyFragment
extends CachingCryptoOperationFragment<CertifyActionsParcel, CertifyResult>
implements LoaderManager.LoaderCallbacks<Cursor> {
public static final String ARG_CHECK_STATES = "check_states";
@@ -89,7 +90,6 @@ public class CertifyKeyFragment extends CachingCryptoOperationFragment<CertifyAc
private static final int INDEX_IS_REVOKED = 4;
private MultiUserIdsAdapter mUserIdsAdapter;
private Messenger mPassthroughMessenger;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
@@ -102,10 +102,6 @@ public class CertifyKeyFragment extends CachingCryptoOperationFragment<CertifyAc
return;
}
mPassthroughMessenger = getActivity().getIntent().getParcelableExtra(
KeychainService.EXTRA_MESSENGER);
mPassthroughMessenger = null; // TODO doesn't work with CryptoOperationFragment, disabled for now
ArrayList<Boolean> checkedStates;
if (savedInstanceState != null) {
checkedStates = (ArrayList<Boolean>) savedInstanceState.getSerializable(ARG_CHECK_STATES);
@@ -306,97 +302,39 @@ public class CertifyKeyFragment extends CachingCryptoOperationFragment<CertifyAc
}
@Override
protected void cryptoOperation(CryptoInputParcel cryptoInput, CertifyActionsParcel actionsParcel) {
Bundle data = new Bundle();
{
protected CertifyActionsParcel createOperationInput() {
if (actionsParcel == null) {
// Bail out if there is not at least one user id selected
ArrayList<CertifyAction> certifyActions = mUserIdsAdapter.getSelectedCertifyActions();
if (certifyActions.isEmpty()) {
Notify.create(getActivity(), "No identities selected!",
Notify.Style.ERROR).show();
return;
}
long selectedKeyId = mCertifyKeySpinner.getSelectedKeyId();
// fill values for this action
actionsParcel = new CertifyActionsParcel(selectedKeyId);
actionsParcel.mCertifyActions.addAll(certifyActions);
// cached for next cryptoOperation loop
cacheActionsParcel(actionsParcel);
}
data.putParcelable(KeychainService.EXTRA_CRYPTO_INPUT, cryptoInput);
data.putParcelable(KeychainService.CERTIFY_PARCEL, actionsParcel);
if (mUploadKeyCheckbox.isChecked()) {
String keyserver = Preferences.getPreferences(getActivity()).getPreferredKeyserver();
data.putString(KeychainService.UPLOAD_KEY_SERVER, keyserver);
}
// Bail out if there is not at least one user id selected
ArrayList<CertifyAction> certifyActions = mUserIdsAdapter.getSelectedCertifyActions();
if (certifyActions.isEmpty()) {
Notify.create(getActivity(), "No identities selected!",
Notify.Style.ERROR).show();
return null;
}
// Send all information needed to service to sign key in other thread
Intent intent = new Intent(getActivity(), KeychainService.class);
intent.setAction(KeychainService.ACTION_CERTIFY_KEYRING);
intent.putExtra(KeychainService.EXTRA_DATA, data);
long selectedKeyId = mCertifyKeySpinner.getSelectedKeyId();
if (mPassthroughMessenger != null) {
intent.putExtra(KeychainService.EXTRA_MESSENGER, mPassthroughMessenger);
} else {
// fill values for this action
CertifyActionsParcel actionsParcel = new CertifyActionsParcel(selectedKeyId);
actionsParcel.mCertifyActions.addAll(certifyActions);
// Message is received after signing is done in KeychainService
ServiceProgressHandler saveHandler = new ServiceProgressHandler(
getActivity(),
getString(R.string.progress_certifying),
ProgressDialog.STYLE_SPINNER,
true
) {
@Override
public void handleMessage(Message message) {
// handle messages by KeychainIntentCryptoServiceHandler first
super.handleMessage(message);
// cached for next cryptoOperation loop
cacheActionsParcel(actionsParcel);
// handle pending messages
if (handlePendingMessage(message)) {
return;
}
if (message.arg1 == MessageStatus.OKAY.ordinal()) {
Bundle data = message.getData();
CertifyResult result = data.getParcelable(CertifyResult.EXTRA_RESULT);
Intent intent = new Intent();
intent.putExtra(CertifyResult.EXTRA_RESULT, result);
getActivity().setResult(Activity.RESULT_OK, intent);
getActivity().finish();
}
}
};
// Create a new Messenger for the communication back
Messenger messenger = new Messenger(saveHandler);
intent.putExtra(KeychainService.EXTRA_MESSENGER, messenger);
// show progress dialog
saveHandler.showProgressDialog(getActivity());
}
// start service with intent
getActivity().startService(intent);
if (mPassthroughMessenger != null) {
getActivity().setResult(Activity.RESULT_OK);
getActivity().finish();
}
return actionsParcel;
}
@Override
protected void onCryptoOperationSuccess(CertifyResult result) {
Intent intent = new Intent();
intent.putExtra(CertifyResult.EXTRA_RESULT, result);
getActivity().setResult(Activity.RESULT_OK, intent);
getActivity().finish();
}
@Override
protected void onCryptoOperationCancelled() {
super.onCryptoOperationCancelled();
}
}

View File

@@ -63,8 +63,6 @@ public class DecryptFilesFragment extends DecryptFragment {
private Uri mInputUri = null;
private Uri mOutputUri = null;
private String mCurrentCryptoOperation;
/**
* Creates new instance of this fragment
*/
@@ -151,7 +149,7 @@ public class DecryptFilesFragment extends DecryptFragment {
return;
}
startDecryptFilenames();
cryptoOperation();
}
private String removeEncryptedAppend(String name) {
@@ -179,112 +177,6 @@ public class DecryptFilesFragment extends DecryptFragment {
}
}
private void startDecrypt() {
mCurrentCryptoOperation = KeychainService.ACTION_DECRYPT_VERIFY;
cryptoOperation(new CryptoInputParcel());
}
private void startDecryptFilenames() {
mCurrentCryptoOperation = KeychainService.ACTION_DECRYPT_METADATA;
cryptoOperation(new CryptoInputParcel());
}
@Override
@SuppressLint("HandlerLeak")
protected void cryptoOperation(CryptoInputParcel cryptoInput) {
// Send all information needed to service to decrypt in other thread
Intent intent = new Intent(getActivity(), KeychainService.class);
// fill values for this action
Bundle data = new Bundle();
// use current operation, either decrypt metadata or decrypt payload
intent.setAction(mCurrentCryptoOperation);
// data
Log.d(Constants.TAG, "mInputUri=" + mInputUri + ", mOutputUri=" + mOutputUri);
PgpDecryptVerifyInputParcel input = new PgpDecryptVerifyInputParcel(mInputUri, mOutputUri)
.setAllowSymmetricDecryption(true);
data.putParcelable(KeychainService.DECRYPT_VERIFY_PARCEL, input);
data.putParcelable(KeychainService.EXTRA_CRYPTO_INPUT, cryptoInput);
intent.putExtra(KeychainService.EXTRA_DATA, data);
// Message is received after decrypting is done in KeychainService
ServiceProgressHandler saveHandler = new ServiceProgressHandler(
getActivity(),
getString(R.string.progress_decrypting),
ProgressDialog.STYLE_HORIZONTAL
) {
@Override
public void handleMessage(Message message) {
// handle messages by standard KeychainIntentServiceHandler first
super.handleMessage(message);
// handle pending messages
if (handlePendingMessage(message)) {
return;
}
if (message.arg1 == MessageStatus.OKAY.ordinal()) {
// get returned data bundle
Bundle returnData = message.getData();
DecryptVerifyResult pgpResult =
returnData.getParcelable(DecryptVerifyResult.EXTRA_RESULT);
if (pgpResult.success()) {
switch (mCurrentCryptoOperation) {
case KeychainService.ACTION_DECRYPT_METADATA: {
askForOutputFilename(pgpResult.getDecryptMetadata().getFilename());
break;
}
case KeychainService.ACTION_DECRYPT_VERIFY: {
// display signature result in activity
loadVerifyResult(pgpResult);
if (mDeleteAfter.isChecked()) {
// Create and show dialog to delete original file
DeleteFileDialogFragment deleteFileDialog = DeleteFileDialogFragment.newInstance(mInputUri);
deleteFileDialog.show(getActivity().getSupportFragmentManager(), "deleteDialog");
setInputUri(null);
}
/*
// A future open after decryption feature
if () {
Intent viewFile = new Intent(Intent.ACTION_VIEW);
viewFile.setInputData(mOutputUri);
startActivity(viewFile);
}
*/
break;
}
default: {
Log.e(Constants.TAG, "Bug: not supported operation!");
break;
}
}
}
pgpResult.createNotify(getActivity()).show(DecryptFilesFragment.this);
}
}
};
// Create a new Messenger for the communication back
Messenger messenger = new Messenger(saveHandler);
intent.putExtra(KeychainService.EXTRA_MESSENGER, messenger);
// show progress dialog
saveHandler.showProgressDialog(getActivity());
// start service with intent
getActivity().startService(intent);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
@@ -299,7 +191,7 @@ public class DecryptFilesFragment extends DecryptFragment {
// This happens after output file was selected, so start our operation
if (resultCode == Activity.RESULT_OK && data != null) {
mOutputUri = data.getData();
startDecrypt();
cryptoOperation();
}
return;
}
@@ -314,4 +206,20 @@ public class DecryptFilesFragment extends DecryptFragment {
protected void onVerifyLoaded(boolean hideErrorOverlay) {
}
@Override
protected PgpDecryptVerifyInputParcel createOperationInput() {
return new PgpDecryptVerifyInputParcel(mInputUri, mOutputUri).setAllowSymmetricDecryption(true);
}
@Override
protected void onCryptoOperationSuccess(DecryptVerifyResult result) {
// display signature result in activity
loadVerifyResult(result);
// TODO delete after decrypt not implemented!
}
}

View File

@@ -43,12 +43,14 @@ import org.sufficientlysecure.keychain.operations.results.DecryptVerifyResult;
import org.sufficientlysecure.keychain.operations.results.ImportKeyResult;
import org.sufficientlysecure.keychain.operations.results.OperationResult;
import org.sufficientlysecure.keychain.pgp.KeyRing;
import org.sufficientlysecure.keychain.pgp.PgpDecryptVerifyInputParcel;
import org.sufficientlysecure.keychain.pgp.exception.PgpKeyNotFoundException;
import org.sufficientlysecure.keychain.provider.KeychainContract;
import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings;
import org.sufficientlysecure.keychain.provider.ProviderHelper;
import org.sufficientlysecure.keychain.service.KeychainService;
import org.sufficientlysecure.keychain.service.ServiceProgressHandler;
import org.sufficientlysecure.keychain.ui.base.CachingCryptoOperationFragment;
import org.sufficientlysecure.keychain.ui.base.CryptoOperationFragment;
import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils;
import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils.State;
@@ -56,8 +58,9 @@ import org.sufficientlysecure.keychain.ui.util.Notify;
import org.sufficientlysecure.keychain.ui.util.Notify.Style;
import org.sufficientlysecure.keychain.util.Preferences;
public abstract class DecryptFragment extends CryptoOperationFragment implements
LoaderManager.LoaderCallbacks<Cursor> {
public abstract class DecryptFragment
extends CachingCryptoOperationFragment<PgpDecryptVerifyInputParcel, DecryptVerifyResult>
implements LoaderManager.LoaderCallbacks<Cursor> {
public static final int LOADER_ID_UNIFIED = 0;
public static final String ARG_DECRYPT_VERIFY_RESULT = "decrypt_verify_result";

View File

@@ -17,11 +17,8 @@
package org.sufficientlysecure.keychain.ui;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.os.Messenger;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
@@ -35,9 +32,6 @@ import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.compatibility.ClipboardReflection;
import org.sufficientlysecure.keychain.operations.results.DecryptVerifyResult;
import org.sufficientlysecure.keychain.pgp.PgpDecryptVerifyInputParcel;
import org.sufficientlysecure.keychain.service.KeychainService;
import org.sufficientlysecure.keychain.service.ServiceProgressHandler;
import org.sufficientlysecure.keychain.service.input.CryptoInputParcel;
import org.sufficientlysecure.keychain.ui.util.Notify;
import org.sufficientlysecure.keychain.util.ShareHelper;
@@ -115,7 +109,7 @@ public class DecryptTextFragment extends DecryptFragment {
mShowMenuOptions = args.getBoolean(ARG_SHOW_MENU, false);
if (savedInstanceState == null) {
cryptoOperation(new CryptoInputParcel());
cryptoOperation();
}
}
@@ -158,77 +152,8 @@ public class DecryptTextFragment extends DecryptFragment {
}
@Override
protected void cryptoOperation(CryptoInputParcel cryptoInput) {
// Send all information needed to service to decrypt in other thread
Intent intent = new Intent(getActivity(), KeychainService.class);
// fill values for this action
Bundle data = new Bundle();
intent.setAction(KeychainService.ACTION_DECRYPT_VERIFY);
PgpDecryptVerifyInputParcel input = new PgpDecryptVerifyInputParcel(mCiphertext.getBytes());
data.putParcelable(KeychainService.DECRYPT_VERIFY_PARCEL, input);
data.putParcelable(KeychainService.EXTRA_CRYPTO_INPUT, cryptoInput);
intent.putExtra(KeychainService.EXTRA_DATA, data);
// Message is received after encrypting is done in KeychainService
ServiceProgressHandler saveHandler = new ServiceProgressHandler(
getActivity(),
getString(R.string.progress_decrypting),
ProgressDialog.STYLE_HORIZONTAL
) {
public void handleMessage(Message message) {
// handle messages by standard KeychainIntentServiceHandler first
super.handleMessage(message);
// handle pending messages
if (handlePendingMessage(message)) {
return;
}
if (message.arg1 == MessageStatus.OKAY.ordinal()) {
// get returned data bundle
Bundle returnData = message.getData();
DecryptVerifyResult pgpResult =
returnData.getParcelable(DecryptVerifyResult.EXTRA_RESULT);
if (pgpResult.success()) {
byte[] decryptedMessage = pgpResult.getOutputBytes();
String displayMessage;
if (pgpResult.getCharset() != null) {
try {
displayMessage = new String(decryptedMessage, pgpResult.getCharset());
} catch (UnsupportedEncodingException e) {
// if we can't decode properly, just fall back to utf-8
displayMessage = new String(decryptedMessage);
}
} else {
displayMessage = new String(decryptedMessage);
}
mText.setText(displayMessage);
// display signature result in activity
loadVerifyResult(pgpResult);
} else {
// TODO: show also invalid layout with different text?
}
pgpResult.createNotify(getActivity()).show(DecryptTextFragment.this);
}
}
};
// Create a new Messenger for the communication back
Messenger messenger = new Messenger(saveHandler);
intent.putExtra(KeychainService.EXTRA_MESSENGER, messenger);
// show progress dialog
saveHandler.showProgressDialog(getActivity());
// start service with intent
getActivity().startService(intent);
protected PgpDecryptVerifyInputParcel createOperationInput() {
return new PgpDecryptVerifyInputParcel(mCiphertext.getBytes());
}
@Override
@@ -236,4 +161,27 @@ public class DecryptTextFragment extends DecryptFragment {
mShowMenuOptions = hideErrorOverlay;
getActivity().supportInvalidateOptionsMenu();
}
@Override
protected void onCryptoOperationSuccess(DecryptVerifyResult result) {
byte[] decryptedMessage = result.getOutputBytes();
String displayMessage;
if (result.getCharset() != null) {
try {
displayMessage = new String(decryptedMessage, result.getCharset());
} catch (UnsupportedEncodingException e) {
// if we can't decode properly, just fall back to utf-8
displayMessage = new String(decryptedMessage);
}
} else {
displayMessage = new String(decryptedMessage);
}
mText.setText(displayMessage);
// display signature result in activity
loadVerifyResult(result);
}
}

View File

@@ -26,6 +26,7 @@ import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Messenger;
import android.os.Parcelable;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
@@ -39,6 +40,9 @@ import android.widget.ListView;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.compatibility.DialogFragmentWorkaround;
import org.sufficientlysecure.keychain.operations.results.DecryptVerifyResult;
import org.sufficientlysecure.keychain.operations.results.EditKeyResult;
import org.sufficientlysecure.keychain.operations.results.InputPendingResult;
import org.sufficientlysecure.keychain.operations.results.OperationResult;
import org.sufficientlysecure.keychain.operations.results.OperationResult.LogType;
import org.sufficientlysecure.keychain.operations.results.SingletonResult;
@@ -66,9 +70,8 @@ import org.sufficientlysecure.keychain.ui.util.Notify;
import org.sufficientlysecure.keychain.util.Log;
import org.sufficientlysecure.keychain.util.Passphrase;
public class EditKeyFragment extends CryptoOperationFragment implements
LoaderManager.LoaderCallbacks<Cursor> {
public class EditKeyFragment extends CryptoOperationFragment<SaveKeyringParcel, OperationResult>
implements LoaderManager.LoaderCallbacks<Cursor> {
public static final String ARG_DATA_URI = "uri";
public static final String ARG_SAVE_KEYRING_PARCEL = "save_keyring_parcel";
@@ -572,7 +575,7 @@ public class EditKeyFragment extends CryptoOperationFragment implements
addSubkeyDialogFragment.show(getActivity().getSupportFragmentManager(), "addSubkeyDialog");
}
private void returnKeyringParcel() {
protected void returnKeyringParcel() {
if (mSaveKeyringParcel.mAddUserIds.size() == 0) {
Notify.create(getActivity(), R.string.edit_key_error_add_identity, Notify.Style.ERROR).show();
return;
@@ -591,76 +594,6 @@ public class EditKeyFragment extends CryptoOperationFragment implements
getActivity().finish();
}
@Override
protected void cryptoOperation(CryptoInputParcel cryptoInput) {
Log.d(Constants.TAG, "cryptoInput:\n" + cryptoInput);
Log.d(Constants.TAG, "mSaveKeyringParcel:\n" + mSaveKeyringParcel);
ServiceProgressHandler saveHandler = new ServiceProgressHandler(
getActivity(),
getString(R.string.progress_saving),
ProgressDialog.STYLE_HORIZONTAL,
true
) {
public void handleMessage(Message message) {
// handle messages by standard KeychainIntentServiceHandler first
super.handleMessage(message);
if (handlePendingMessage(message)) {
return;
}
if (message.arg1 == MessageStatus.OKAY.ordinal()) {
// get returned data bundle
Bundle returnData = message.getData();
if (returnData == null) {
return;
}
final OperationResult result =
returnData.getParcelable(OperationResult.EXTRA_RESULT);
if (result == null) {
return;
}
// if bad -> display here!
if (!result.success()) {
result.createNotify(getActivity()).show();
return;
}
// if good -> finish, return result to showkey and display there!
Intent intent = new Intent();
intent.putExtra(OperationResult.EXTRA_RESULT, result);
getActivity().setResult(EditKeyActivity.RESULT_OK, intent);
getActivity().finish();
}
}
};
// Send all information needed to service to import key in other thread
Intent intent = new Intent(getActivity(), KeychainService.class);
intent.setAction(KeychainService.ACTION_EDIT_KEYRING);
// fill values for this action
Bundle data = new Bundle();
data.putParcelable(KeychainService.EXTRA_CRYPTO_INPUT, cryptoInput);
data.putParcelable(KeychainService.EDIT_KEYRING_PARCEL, mSaveKeyringParcel);
intent.putExtra(KeychainService.EXTRA_DATA, data);
// Create a new Messenger for the communication back
Messenger messenger = new Messenger(saveHandler);
intent.putExtra(KeychainService.EXTRA_MESSENGER, messenger);
// show progress dialog
saveHandler.showProgressDialog(getActivity());
// start service with intent
getActivity().startService(intent);
}
/**
* Closes this activity, returning a result parcel with a single error log entry.
*/
@@ -675,4 +608,20 @@ public class EditKeyFragment extends CryptoOperationFragment implements
getActivity().finish();
}
@Override
protected SaveKeyringParcel createOperationInput() {
return mSaveKeyringParcel;
}
@Override
protected void onCryptoOperationSuccess(OperationResult result) {
// if good -> finish, return result to showkey and display there!
Intent intent = new Intent();
intent.putExtra(OperationResult.EXTRA_RESULT, result);
getActivity().setResult(EditKeyActivity.RESULT_OK, intent);
getActivity().finish();
}
}

View File

@@ -49,6 +49,7 @@ import org.sufficientlysecure.keychain.pgp.KeyRing;
import org.sufficientlysecure.keychain.pgp.PgpConstants;
import org.sufficientlysecure.keychain.pgp.SignEncryptParcel;
import org.sufficientlysecure.keychain.provider.TemporaryStorageProvider;
import org.sufficientlysecure.keychain.service.KeychainNewService;
import org.sufficientlysecure.keychain.service.KeychainService;
import org.sufficientlysecure.keychain.service.ServiceProgressHandler;
import org.sufficientlysecure.keychain.service.input.CryptoInputParcel;
@@ -72,7 +73,8 @@ import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class EncryptFilesFragment extends CachingCryptoOperationFragment<SignEncryptParcel> {
public class EncryptFilesFragment
extends CachingCryptoOperationFragment<SignEncryptParcel, SignEncryptResult> {
public static final String ARG_DELETE_AFTER_ENCRYPT = "delete_after_encrypt";
public static final String ARG_ENCRYPT_FILENAMES = "encrypt_filenames";
@@ -272,11 +274,13 @@ public class EncryptFilesFragment extends CachingCryptoOperationFragment<SignEnc
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.encrypt_save: {
cryptoOperation(false);
mShareAfterEncrypt = false;
cryptoOperation();
break;
}
case R.id.encrypt_share: {
cryptoOperation(true);
mShareAfterEncrypt = true;
cryptoOperation();
break;
}
case R.id.check_use_armor: {
@@ -374,7 +378,9 @@ public class EncryptFilesFragment extends CachingCryptoOperationFragment<SignEnc
}
public void onEncryptSuccess(final SignEncryptResult result) {
@Override
protected void onCryptoOperationSuccess(final SignEncryptResult result) {
if (mDeleteAfterEncrypt) {
DeleteFileDialogFragment deleteFileDialog =
DeleteFileDialogFragment.newInstance(mFilesAdapter.getAsArrayList());
@@ -402,6 +408,7 @@ public class EncryptFilesFragment extends CachingCryptoOperationFragment<SignEnc
result.createNotify(getActivity()).show();
}
}
}
// prepares mOutputUris, either directly and returns false, or indirectly
@@ -441,7 +448,46 @@ public class EncryptFilesFragment extends CachingCryptoOperationFragment<SignEnc
}
}
protected SignEncryptParcel createIncompleteEncryptBundle() {
protected SignEncryptParcel createOperationInput() {
SignEncryptParcel actionsParcel = getCachedActionsParcel();
// we have three cases here: nothing cached, cached except output, fully cached
if (actionsParcel == null) {
// clear output uris for now, they will be created by prepareOutputStreams later
mOutputUris = null;
actionsParcel = createIncompleteCryptoInput();
// this is null if invalid, just return in that case
if (actionsParcel == null) {
return null;
}
cacheActionsParcel(actionsParcel);
}
// if it's incomplete, prepare output streams
if (actionsParcel.isIncomplete()) {
// if this is still null, prepare output streams again
if (mOutputUris == null) {
// this may interrupt the flow, and call us again from onActivityResult
if (prepareOutputStreams(mShareAfterEncrypt)) {
return null;
}
}
actionsParcel.addOutputUris(mOutputUris);
cacheActionsParcel(actionsParcel);
}
return actionsParcel;
}
protected SignEncryptParcel createIncompleteCryptoInput() {
// fill values for this action
SignEncryptParcel data = new SignEncryptParcel();
@@ -546,92 +592,6 @@ public class EncryptFilesFragment extends CachingCryptoOperationFragment<SignEnc
return sendIntent;
}
public void cryptoOperation(boolean share) {
mShareAfterEncrypt = share;
cryptoOperation();
}
@Override
protected void cryptoOperation(CryptoInputParcel cryptoInput, SignEncryptParcel actionsParcel) {
// we have three cases here: nothing cached, cached except output, fully cached
if (actionsParcel == null) {
// clear output uris for now, they will be created by prepareOutputStreams later
mOutputUris = null;
actionsParcel = createIncompleteEncryptBundle();
// this is null if invalid, just return in that case
if (actionsParcel == null) {
// Notify was created by createEncryptBundle.
return;
}
cacheActionsParcel(actionsParcel);
}
// if it's incomplete, prepare output streams
if (actionsParcel.isIncomplete()) {
// if this is still null, prepare output streams again
if (mOutputUris == null) {
// this may interrupt the flow, and call us again from onActivityResult
if (prepareOutputStreams(mShareAfterEncrypt)) {
return;
}
}
actionsParcel.addOutputUris(mOutputUris);
cacheActionsParcel(actionsParcel);
}
// Send all information needed to service to edit key in other thread
Intent intent = new Intent(getActivity(), KeychainService.class);
intent.setAction(KeychainService.ACTION_SIGN_ENCRYPT);
Bundle data = new Bundle();
data.putParcelable(KeychainService.SIGN_ENCRYPT_PARCEL, actionsParcel);
data.putParcelable(KeychainService.EXTRA_CRYPTO_INPUT, cryptoInput);
intent.putExtra(KeychainService.EXTRA_DATA, data);
// Message is received after encrypting is done in KeychainService
ServiceProgressHandler serviceHandler = new ServiceProgressHandler(
getActivity(),
getString(R.string.progress_encrypting),
ProgressDialog.STYLE_HORIZONTAL,
true
) {
@Override
public void handleMessage(Message message) {
// handle messages by standard KeychainIntentServiceHandler first
super.handleMessage(message);
// handle pending messages
if (handlePendingMessage(message)) {
return;
}
if (message.arg1 == MessageStatus.OKAY.ordinal()) {
SignEncryptResult result =
message.getData().getParcelable(SignEncryptResult.EXTRA_RESULT);
if (result.success()) {
onEncryptSuccess(result);
} else {
result.createNotify(getActivity()).show();
}
}
}
};
// Create a new Messenger for the communication back
Messenger messenger = new Messenger(serviceHandler);
intent.putExtra(KeychainService.EXTRA_MESSENGER, messenger);
// show progress dialog
serviceHandler.showProgressDialog(getActivity());
// start service with intent
getActivity().startService(intent);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
@@ -646,7 +606,8 @@ public class EncryptFilesFragment extends CachingCryptoOperationFragment<SignEnc
if (resultCode == Activity.RESULT_OK && data != null) {
mOutputUris = new ArrayList<>(1);
mOutputUris.add(data.getData());
cryptoOperation(false);
mShareAfterEncrypt = false;
cryptoOperation();
}
return;
}

View File

@@ -18,11 +18,8 @@
package org.sufficientlysecure.keychain.ui;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.os.Messenger;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
@@ -33,6 +30,7 @@ import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import de.greenrobot.event.EventBus;
import org.spongycastle.bcpg.CompressionAlgorithmTags;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R;
@@ -41,9 +39,6 @@ import org.sufficientlysecure.keychain.operations.results.SignEncryptResult;
import org.sufficientlysecure.keychain.pgp.KeyRing;
import org.sufficientlysecure.keychain.pgp.PgpConstants;
import org.sufficientlysecure.keychain.pgp.SignEncryptParcel;
import org.sufficientlysecure.keychain.service.KeychainService;
import org.sufficientlysecure.keychain.service.ServiceProgressHandler;
import org.sufficientlysecure.keychain.service.input.CryptoInputParcel;
import org.sufficientlysecure.keychain.ui.base.CachingCryptoOperationFragment;
import org.sufficientlysecure.keychain.ui.util.Notify;
import org.sufficientlysecure.keychain.ui.util.Notify.ActionListener;
@@ -55,7 +50,8 @@ import org.sufficientlysecure.keychain.util.ShareHelper;
import java.util.HashSet;
import java.util.Set;
public class EncryptTextFragment extends CachingCryptoOperationFragment<SignEncryptParcel> {
public class EncryptTextFragment
extends CachingCryptoOperationFragment<SignEncryptParcel, SignEncryptResult> {
public static final String ARG_TEXT = "text";
public static final String ARG_USE_COMPRESSION = "use_compression";
@@ -145,6 +141,7 @@ public class EncryptTextFragment extends CachingCryptoOperationFragment<SignEncr
}
setHasOptionsMenu(true);
}
@Override
@@ -168,11 +165,13 @@ public class EncryptTextFragment extends CachingCryptoOperationFragment<SignEncr
// break;
// }
case R.id.encrypt_copy: {
cryptoOperation(false);
mShareAfterEncrypt = false;
cryptoOperation();
break;
}
case R.id.encrypt_share: {
cryptoOperation(true);
mShareAfterEncrypt = true;
cryptoOperation();
break;
}
default: {
@@ -204,21 +203,7 @@ public class EncryptTextFragment extends CachingCryptoOperationFragment<SignEncr
}
protected void onEncryptSuccess(SignEncryptResult result) {
if (mShareAfterEncrypt) {
// Share encrypted message/file
startActivity(sendWithChooserExcludingEncrypt(result.getResultBytes()));
} else {
// Copy to clipboard
copyToClipboard(result.getResultBytes());
result.createNotify(getActivity()).show();
// Notify.create(EncryptTextActivity.this,
// R.string.encrypt_sign_clipboard_successful, Notify.Style.OK)
// .show(getSupportFragmentManager().findFragmentById(R.id.encrypt_text_fragment));
}
}
protected SignEncryptParcel createEncryptBundle() {
protected SignEncryptParcel createOperationInput() {
if (mMessage == null || mMessage.isEmpty()) {
Notify.create(getActivity(), R.string.error_empty_text, Notify.Style.ERROR)
@@ -331,71 +316,21 @@ public class EncryptTextFragment extends CachingCryptoOperationFragment<SignEncr
return sendIntent;
}
public void cryptoOperation(boolean share) {
mShareAfterEncrypt = share;
cryptoOperation();
}
@Override
protected void cryptoOperation(CryptoInputParcel cryptoInput, SignEncryptParcel actionsParcel) {
protected void onCryptoOperationSuccess(SignEncryptResult result) {
if (actionsParcel == null) {
actionsParcel = createEncryptBundle();
// this is null if invalid, just return in that case
if (actionsParcel == null) {
// Notify was created by inputIsValid.
return;
}
cacheActionsParcel(actionsParcel);
if (mShareAfterEncrypt) {
// Share encrypted message/file
startActivity(sendWithChooserExcludingEncrypt(result.getResultBytes()));
} else {
// Copy to clipboard
copyToClipboard(result.getResultBytes());
result.createNotify(getActivity()).show();
// Notify.create(EncryptTextActivity.this,
// R.string.encrypt_sign_clipboard_successful, Notify.Style.OK)
// .show(getSupportFragmentManager().findFragmentById(R.id.encrypt_text_fragment));
}
// Send all information needed to service to edit key in other thread
Intent intent = new Intent(getActivity(), KeychainService.class);
intent.setAction(KeychainService.ACTION_SIGN_ENCRYPT);
Bundle data = new Bundle();
data.putParcelable(KeychainService.SIGN_ENCRYPT_PARCEL, actionsParcel);
data.putParcelable(KeychainService.EXTRA_CRYPTO_INPUT, cryptoInput);
intent.putExtra(KeychainService.EXTRA_DATA, data);
// Message is received after encrypting is done in KeychainService
ServiceProgressHandler serviceHandler = new ServiceProgressHandler(
getActivity(),
getString(R.string.progress_encrypting),
ProgressDialog.STYLE_HORIZONTAL
) {
@Override
public void handleMessage(Message message) {
// handle messages by standard KeychainIntentServiceHandler first
super.handleMessage(message);
if (handlePendingMessage(message)) {
return;
}
if (message.arg1 == MessageStatus.OKAY.ordinal()) {
SignEncryptResult result =
message.getData().getParcelable(SignEncryptResult.EXTRA_RESULT);
if (result.success()) {
onEncryptSuccess(result);
} else {
result.createNotify(getActivity()).show();
}
}
}
};
// Create a new Messenger for the communication back
Messenger messenger = new Messenger(serviceHandler);
intent.putExtra(KeychainService.EXTRA_MESSENGER, messenger);
// show progress dialog
serviceHandler.showProgressDialog(getActivity());
// start service with intent
getActivity().startService(intent);
}
}

View File

@@ -1,25 +1,26 @@
package org.sufficientlysecure.keychain.ui.base;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.os.Parcelable;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.operations.results.OperationResult;
import org.sufficientlysecure.keychain.service.KeychainNewService;
import org.sufficientlysecure.keychain.service.ServiceProgressHandler;
import org.sufficientlysecure.keychain.service.input.CryptoInputParcel;
public abstract class CachingCryptoOperationFragment <T extends Parcelable> extends CryptoOperationFragment {
public abstract class CachingCryptoOperationFragment <T extends Parcelable, S extends OperationResult>
extends CryptoOperationFragment<T, S> {
public static final String ARG_CACHED_ACTIONS = "cached_actions";
private T mCachedActionsParcel;
@Override
protected void cryptoOperation(CryptoInputParcel cryptoInput) {
cryptoOperation(cryptoInput, mCachedActionsParcel);
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
@@ -37,21 +38,44 @@ public abstract class CachingCryptoOperationFragment <T extends Parcelable> exte
}
@Override
public boolean handlePendingMessage(Message message) {
// see if it's an InputPendingResult, and if so don't care
if (super.handlePendingMessage(message)) {
return true;
}
// if it's a non-input-pending OKAY message, always clear the cached actions parcel
if (message.arg1 == ServiceProgressHandler.MessageStatus.OKAY.ordinal()) {
mCachedActionsParcel = null;
}
return false;
protected void onCryptoOperationResult(S result) {
super.onCryptoOperationResult(result);
mCachedActionsParcel = null;
}
protected abstract void cryptoOperation(CryptoInputParcel cryptoInput, T cachedActionsParcel);
protected abstract T createOperationInput();
protected void cryptoOperation(CryptoInputParcel cryptoInput) {
if (mCachedActionsParcel == null) {
mCachedActionsParcel = createOperationInput();
// this is null if invalid, just return in that case
if (mCachedActionsParcel == null) {
// Notify was created by createCryptoInput.
return;
}
}
// Send all information needed to service to edit key in other thread
Intent intent = new Intent(getActivity(), KeychainNewService.class);
intent.putExtra(KeychainNewService.EXTRA_OPERATION_INPUT, mCachedActionsParcel);
intent.putExtra(KeychainNewService.EXTRA_CRYPTO_INPUT, cryptoInput);
showProgressFragment(
getString(R.string.progress_start),
ProgressDialog.STYLE_HORIZONTAL,
false);
// start service with intent
getActivity().startService(intent);
}
protected T getCachedActionsParcel() {
return mCachedActionsParcel;
}
protected void cacheActionsParcel(T cachedActionsParcel) {
mCachedActionsParcel = cachedActionsParcel;

View File

@@ -19,28 +19,46 @@
package org.sufficientlysecure.keychain.ui.base;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import de.greenrobot.event.EventBus;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.operations.results.InputPendingResult;
import org.sufficientlysecure.keychain.operations.results.OperationResult;
import org.sufficientlysecure.keychain.service.ServiceProgressHandler;
import org.sufficientlysecure.keychain.service.KeychainNewService;
import org.sufficientlysecure.keychain.service.ProgressEvent;
import org.sufficientlysecure.keychain.service.input.CryptoInputParcel;
import org.sufficientlysecure.keychain.service.input.RequiredInputParcel;
import org.sufficientlysecure.keychain.ui.NfcOperationActivity;
import org.sufficientlysecure.keychain.ui.PassphraseDialogActivity;
import org.sufficientlysecure.keychain.ui.dialog.ProgressDialogFragment;
/**
* All fragments executing crypto operations need to extend this class.
*/
public abstract class CryptoOperationFragment extends Fragment {
public abstract class CryptoOperationFragment <T extends Parcelable, S extends OperationResult>
extends Fragment {
public static final int REQUEST_CODE_PASSPHRASE = 0x00008001;
public static final int REQUEST_CODE_NFC = 0x00008002;
@Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
@Override
public void onStop() {
EventBus.getDefault().unregister(this);
super.onStop();
}
private void initiateInputActivity(RequiredInputParcel requiredInput) {
switch (requiredInput.mType) {
@@ -99,35 +117,118 @@ public abstract class CryptoOperationFragment extends Fragment {
}
}
public boolean handlePendingMessage(Message message) {
protected void dismissProgress() {
if (message.arg1 == ServiceProgressHandler.MessageStatus.OKAY.ordinal()) {
Bundle data = message.getData();
ProgressDialogFragment progressDialogFragment =
(ProgressDialogFragment) getFragmentManager().findFragmentByTag("progressDialog");
OperationResult result = data.getParcelable(OperationResult.EXTRA_RESULT);
if (result == null || !(result instanceof InputPendingResult)) {
return false;
}
InputPendingResult pendingResult = (InputPendingResult) result;
if (pendingResult.isPending()) {
RequiredInputParcel requiredInput = pendingResult.getRequiredInputParcel();
initiateInputActivity(requiredInput);
return true;
}
if (progressDialogFragment == null) {
return;
}
return false;
progressDialogFragment.dismissAllowingStateLoss();
}
public void showProgressFragment(String progressDialogMessage,
int progressDialogStyle,
boolean cancelable) {
if (getFragmentManager().findFragmentByTag("progressDialog") != null) {
return;
}
ProgressDialogFragment progressDialogFragment = ProgressDialogFragment.newInstance(
progressDialogMessage, progressDialogStyle, cancelable);
FragmentManager manager = getFragmentManager();
progressDialogFragment.show(manager, "progressDialog");
}
protected abstract T createOperationInput();
protected void cryptoOperation(CryptoInputParcel cryptoInput) {
T operationInput = createOperationInput();
if (operationInput == null) {
return;
}
// Send all information needed to service to edit key in other thread
Intent intent = new Intent(getActivity(), KeychainNewService.class);
intent.putExtra(KeychainNewService.EXTRA_OPERATION_INPUT, operationInput);
intent.putExtra(KeychainNewService.EXTRA_CRYPTO_INPUT, cryptoInput);
showProgressFragment(
getString(R.string.progress_start),
ProgressDialog.STYLE_HORIZONTAL,
false);
// start service with intent
getActivity().startService(intent);
}
protected void cryptoOperation() {
cryptoOperation(new CryptoInputParcel());
}
protected abstract void cryptoOperation(CryptoInputParcel cryptoInput);
protected void onCryptoOperationCancelled() {
// Nothing to do here, in most cases
protected void onCryptoOperationResult(S result) {
if (result.success()) {
onCryptoOperationSuccess(result);
} else {
onCryptoOperationError(result);
}
}
abstract protected void onCryptoOperationSuccess(S result);
protected void onCryptoOperationError(S result) {
result.createNotify(getActivity()).show(this);
}
protected void onCryptoOperationCancelled() {
dismissProgress();
}
@SuppressWarnings("unused") // it's an EventBus method
public void onEventMainThread(OperationResult result) {
if (result instanceof InputPendingResult) {
InputPendingResult pendingResult = (InputPendingResult) result;
if (pendingResult.isPending()) {
RequiredInputParcel requiredInput = pendingResult.getRequiredInputParcel();
initiateInputActivity(requiredInput);
return;
}
}
dismissProgress();
try {
// noinspection unchecked, because type erasure :(
onCryptoOperationResult((S) result);
} catch (ClassCastException e) {
throw new AssertionError("bad return class ("
+ result.getClass().getSimpleName() + "), this is a programming error!");
}
}
@SuppressWarnings("unused") // it's an EventBus method
public void onEventMainThread(ProgressEvent event) {
ProgressDialogFragment progressDialogFragment =
(ProgressDialogFragment) getFragmentManager().findFragmentByTag("progressDialog");
if (progressDialogFragment == null) {
return;
}
progressDialogFragment.setProgress(event.mMessage, event.mProgress, event.mMax);
}
}