diff options
Diffstat (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/base')
3 files changed, 106 insertions, 65 deletions
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/base/CachingCryptoOperationFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/base/CachingCryptoOperationFragment.java index 17e4e6ede..95bc4adcb 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/base/CachingCryptoOperationFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/base/CachingCryptoOperationFragment.java @@ -31,8 +31,12 @@ public abstract class CachingCryptoOperationFragment <T extends Parcelable, S ex } @Override - protected void onCryptoOperationResult(S result) { - super.onCryptoOperationResult(result); + public void onCryptoOperationSuccess(S result) { + mCachedActionsParcel = null; + } + + @Override + public void onCryptoOperationError(S result) { mCachedActionsParcel = null; } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/base/CryptoOperationFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/base/CryptoOperationFragment.java index 115f52d56..2ab0d5fac 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/base/CryptoOperationFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/base/CryptoOperationFragment.java @@ -20,56 +20,88 @@ package org.sufficientlysecure.keychain.ui.base; import android.content.Intent; import android.os.Parcelable; +import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import org.sufficientlysecure.keychain.operations.results.OperationResult; +import org.sufficientlysecure.keychain.service.KeychainService; import org.sufficientlysecure.keychain.service.input.CryptoInputParcel; -/** - * All fragments executing crypto operations need to extend this class. +/** This is a base class for fragments which implement a cryptoOperation. + * + * Subclasses of this class can call the cryptoOperation method to execute an + * operation in KeychainService which takes a parcelable of type T as its input + * and returns an OperationResult of type S as a result. + * + * The input (of type T) is not given directly to the cryptoOperation method, + * but must be provided by the overriden createOperationInput method to be + * available upon request during execution of the cryptoOperation. + * + * After running cryptoOperation, one of the onCryptoOperation*() methods will + * be called, depending on the success status of the operation. The subclass + * must override at least onCryptoOperationSuccess to proceed after a + * successful operation. + * + * @see KeychainService + * */ public abstract class CryptoOperationFragment<T extends Parcelable, S extends OperationResult> extends Fragment implements CryptoOperationHelper.Callback<T, S> { - private CryptoOperationHelper<T, S> mOperationHelper; + final private CryptoOperationHelper<T, S> mOperationHelper; public CryptoOperationFragment() { mOperationHelper = new CryptoOperationHelper<>(this, this); } - public void setProgressMessageResource(int id) { - mOperationHelper.setProgressMessageResource(id); - } - - @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { mOperationHelper.handleActivityResult(requestCode, resultCode, data); } - @Override - public abstract T createOperationInput(); - + /** Starts execution of the cryptographic operation. + * + * During this process, the createOperationInput() method will be called, + * this input will be handed to KeychainService, where it is executed in + * the appropriate *Operation class. If the result is a PendingInputResult, + * it is handled accordingly. Otherwise, it is returned in one of the + * onCryptoOperation* callbacks. + */ protected void cryptoOperation() { - cryptoOperation(new CryptoInputParcel()); + mOperationHelper.cryptoOperation(); } protected void cryptoOperation(CryptoInputParcel cryptoInput) { - cryptoOperation(cryptoInput, true); + mOperationHelper.cryptoOperation(cryptoInput); } protected void cryptoOperation(CryptoInputParcel cryptoInput, boolean showProgress) { mOperationHelper.cryptoOperation(cryptoInput, showProgress); } + @Override @Nullable + /** Creates input for the crypto operation. Called internally after the + * crypto operation is started by a call to cryptoOperation(). Silently + * cancels operation if this method returns null. */ + public abstract T createOperationInput(); + + /** Returns false, indicating that we did not handle progress ourselves. */ public boolean onCryptoSetProgress(String msg, int progress, int max) { return false; } + public void setProgressMessageResource(int id) { + mOperationHelper.setProgressMessageResource(id); + } + + @Override + /** Called when the cryptoOperation() was successful. No default behavior + * here, this should always be implemented by a subclass! */ + abstract public void onCryptoOperationSuccess(S result); + @Override public void onCryptoOperationError(S result) { - onCryptoOperationResult(result); result.createNotify(getActivity()).show(); } @@ -77,19 +109,4 @@ public abstract class CryptoOperationFragment<T extends Parcelable, S extends Op public void onCryptoOperationCancelled() { } - @Override - public void onCryptoOperationSuccess(S result) { - onCryptoOperationResult(result); - } - - /** - * - * To be overriden by subclasses, if desired. Provides a way to access the method by the - * same name in CryptoOperationHelper, if super.onCryptoOperationSuccess and - * super.onCryptoOperationError are called at the start of the respective functions in the - * subclass overriding them - * @param result - */ - protected void onCryptoOperationResult(S result) { - } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/base/CryptoOperationHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/base/CryptoOperationHelper.java index 240dd0972..8d141ea5d 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/base/CryptoOperationHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/base/CryptoOperationHelper.java @@ -28,8 +28,8 @@ import android.os.Messenger; import android.os.Parcelable; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; - import android.support.v4.app.FragmentManager; + import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.operations.results.InputPendingResult; @@ -39,6 +39,7 @@ import org.sufficientlysecure.keychain.service.ServiceProgressHandler; 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.OrbotRequiredDialogActivity; import org.sufficientlysecure.keychain.ui.PassphraseDialogActivity; import org.sufficientlysecure.keychain.ui.dialog.ProgressDialogFragment; import org.sufficientlysecure.keychain.util.Log; @@ -52,16 +53,21 @@ import org.sufficientlysecure.keychain.util.Log; */ public class CryptoOperationHelper<T extends Parcelable, S extends OperationResult> { - public interface Callback <T extends Parcelable, S extends OperationResult> { + public interface Callback<T extends Parcelable, S extends OperationResult> { T createOperationInput(); + void onCryptoOperationSuccess(S result); + void onCryptoOperationCancelled(); + void onCryptoOperationError(S result); + boolean onCryptoSetProgress(String msg, int progress, int max); } public static final int REQUEST_CODE_PASSPHRASE = 0x00008001; public static final int REQUEST_CODE_NFC = 0x00008002; + public static final int REQUEST_CODE_ENABLE_ORBOT = 0x00008004; // keeps track of request code used to start an activity from this CryptoOperationHelper. // this is necessary when multiple CryptoOperationHelpers are used in the same fragment/activity @@ -78,8 +84,6 @@ public class CryptoOperationHelper<T extends Parcelable, S extends OperationResu /** * If OperationHelper is being integrated into an activity - * - * @param activity */ public CryptoOperationHelper(FragmentActivity activity, Callback<T, S> callback, int progressMessageString) { mActivity = activity; @@ -90,8 +94,6 @@ public class CryptoOperationHelper<T extends Parcelable, S extends OperationResu /** * if OperationHelper is being integrated into a fragment - * - * @param fragment */ public CryptoOperationHelper(Fragment fragment, Callback<T, S> callback, int progressMessageString) { mFragment = fragment; @@ -102,8 +104,6 @@ public class CryptoOperationHelper<T extends Parcelable, S extends OperationResu /** * if OperationHelper is being integrated into a fragment with default message for the progress dialog - * - * @param fragment */ public CryptoOperationHelper(Fragment fragment, Callback<T, S> callback) { mFragment = fragment; @@ -116,11 +116,14 @@ public class CryptoOperationHelper<T extends Parcelable, S extends OperationResu mProgressMessageResource = id; } - private void initiateInputActivity(RequiredInputParcel requiredInput) { + private void initiateInputActivity(RequiredInputParcel requiredInput, + CryptoInputParcel cryptoInputParcel) { Activity activity = mUseFragment ? mFragment.getActivity() : mActivity; switch (requiredInput.mType) { + // TODO: Verify that all started activities add to cryptoInputParcel if necessary (like OrbotRequiredDialogActivity) + // don't forget to set mRequestedCode! case NFC_MOVE_KEY_TO_CARD: case NFC_DECRYPT: case NFC_SIGN: { @@ -130,7 +133,7 @@ public class CryptoOperationHelper<T extends Parcelable, S extends OperationResu if (mUseFragment) { mFragment.startActivityForResult(intent, mRequestedCode); } else { - mActivity.startActivityForResult(intent, mRequestedCode); + activity.startActivityForResult(intent, mRequestedCode); } return; } @@ -143,22 +146,32 @@ public class CryptoOperationHelper<T extends Parcelable, S extends OperationResu if (mUseFragment) { mFragment.startActivityForResult(intent, mRequestedCode); } else { - mActivity.startActivityForResult(intent, mRequestedCode); + activity.startActivityForResult(intent, mRequestedCode); } return; } - } - throw new RuntimeException("Unhandled pending result!"); + case ENABLE_ORBOT: { + Intent intent = new Intent(activity, OrbotRequiredDialogActivity.class); + intent.putExtra(OrbotRequiredDialogActivity.EXTRA_CRYPTO_INPUT, cryptoInputParcel); + mRequestedCode = REQUEST_CODE_ENABLE_ORBOT; + if (mUseFragment) { + mFragment.startActivityForResult(intent, mRequestedCode); + } else { + activity.startActivityForResult(intent, mRequestedCode); + } + return; + } + + default: { + throw new RuntimeException("Unhandled pending result!"); + } + } } /** - * Attempts the result of an activity started by this helper. Returns true if requestCode is recognized, - * false otherwise. - * - * @param requestCode - * @param resultCode - * @param data + * Attempts the result of an activity started by this helper. Returns true if requestCode is + * recognized, false otherwise. * @return true if requestCode was recognized, false otherwise */ public boolean handleActivityResult(int requestCode, int resultCode, Intent data) { @@ -196,6 +209,16 @@ public class CryptoOperationHelper<T extends Parcelable, S extends OperationResu break; } + case REQUEST_CODE_ENABLE_ORBOT: { + if (resultCode == Activity.RESULT_OK && data != null) { + CryptoInputParcel cryptoInput = + data.getParcelableExtra( + OrbotRequiredDialogActivity.RESULT_CRYPTO_INPUT); + cryptoOperation(cryptoInput); + return true; + } + } + default: { return false; } @@ -225,7 +248,7 @@ public class CryptoOperationHelper<T extends Parcelable, S extends OperationResu } - public void cryptoOperation(CryptoInputParcel cryptoInput, boolean showProgress) { + public void cryptoOperation(final CryptoInputParcel cryptoInput, boolean showProgress) { FragmentActivity activity = mUseFragment ? mFragment.getActivity() : mActivity; @@ -257,14 +280,14 @@ public class CryptoOperationHelper<T extends Parcelable, S extends OperationResu final OperationResult result = returnData.getParcelable(OperationResult.EXTRA_RESULT); - onHandleResult(result); + onHandleResult(result, cryptoInput); } } @Override protected void onSetProgress(String msg, int progress, int max) { // allow handling of progress in fragment, or delegate upwards - if ( ! mCallback.onCryptoSetProgress(msg, progress, max)) { + if (!mCallback.onCryptoSetProgress(msg, progress, max)) { super.onSetProgress(msg, progress, max); } } @@ -291,15 +314,7 @@ public class CryptoOperationHelper<T extends Parcelable, S extends OperationResu cryptoOperation(new CryptoInputParcel()); } - protected void onCryptoOperationResult(S result) { - if (result.success()) { - mCallback.onCryptoOperationSuccess(result); - } else { - mCallback.onCryptoOperationError(result); - } - } - - public void onHandleResult(OperationResult result) { + public void onHandleResult(OperationResult result, CryptoInputParcel oldCryptoInput) { Log.d(Constants.TAG, "Handling result in OperationHelper success: " + result.success()); if (result instanceof InputPendingResult) { @@ -307,7 +322,7 @@ public class CryptoOperationHelper<T extends Parcelable, S extends OperationResu if (pendingResult.isPending()) { RequiredInputParcel requiredInput = pendingResult.getRequiredInputParcel(); - initiateInputActivity(requiredInput); + initiateInputActivity(requiredInput, oldCryptoInput); return; } } @@ -315,8 +330,13 @@ public class CryptoOperationHelper<T extends Parcelable, S extends OperationResu dismissProgress(); try { - // noinspection unchecked, because type erasure :( - onCryptoOperationResult((S) result); + if (result.success()) { + // noinspection unchecked, because type erasure :( + mCallback.onCryptoOperationSuccess((S) result); + } else { + // noinspection unchecked, because type erasure :( + mCallback.onCryptoOperationError((S) result); + } } catch (ClassCastException e) { throw new AssertionError("bad return class (" + result.getClass().getSimpleName() + "), this is a programming error!"); |