From 539379a6297c6064c5a9e2c5450b3050958d15fd Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Mon, 6 Jul 2015 00:10:54 +0200 Subject: some cleanup and documentation --- .../keychain/provider/ProviderHelper.java | 5 +- .../keychain/ui/CreateKeyFinalFragment.java | 15 +++-- .../keychain/ui/ViewKeyYubiKeyFragment.java | 2 +- .../keychain/ui/base/CryptoOperationFragment.java | 75 +++++++++++++--------- .../keychain/ui/base/CryptoOperationHelper.java | 10 --- 5 files changed, 61 insertions(+), 46 deletions(-) (limited to 'OpenKeychain/src/main/java') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java index 4b7d679d1..95f80ebad 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java @@ -26,6 +26,7 @@ import android.content.OperationApplicationException; import android.database.Cursor; import android.net.Uri; import android.os.RemoteException; +import android.support.annotation.NonNull; import android.support.v4.util.LongSparseArray; import org.spongycastle.bcpg.CompressionAlgorithmTags; @@ -725,7 +726,7 @@ public class ProviderHelper { LongSparseArray trustedCerts = new LongSparseArray<>(); @Override - public int compareTo(UserPacketItem o) { + public int compareTo(@NonNull UserPacketItem o) { // revoked keys always come last! //noinspection DoubleNegation if ((selfRevocation != null) != (o.selfRevocation != null)) { @@ -1126,6 +1127,7 @@ public class ProviderHelper { } }); + cursor.close(); } catch (IOException e) { Log.e(Constants.TAG, "error saving secret", e); @@ -1187,6 +1189,7 @@ public class ProviderHelper { } }); + cursor.close(); } catch (IOException e) { Log.e(Constants.TAG, "error saving public", e); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyFinalFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyFinalFragment.java index 7192b7335..51c1bd079 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyFinalFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyFinalFragment.java @@ -23,6 +23,7 @@ import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentActivity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -385,14 +386,18 @@ public class CreateKeyFinalFragment extends Fragment { } public void handleResult(ExportResult result) { - // TODO: ExportOperation UPLOAD_KEYSERVER needs logs! - // TODO: then merge logs here! - //saveKeyResult.getLog().add(result, 0); + Activity activity = getActivity(); + if (activity == null) { + return; + } + + saveKeyResult.getLog().add(result, 0); Intent data = new Intent(); data.putExtra(OperationResult.EXTRA_RESULT, saveKeyResult); - getActivity().setResult(Activity.RESULT_OK, data); - getActivity().finish(); + activity.setResult(Activity.RESULT_OK, data); + activity.finish(); + } @Override diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyYubiKeyFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyYubiKeyFragment.java index f8c3b59ea..c2158650b 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyYubiKeyFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyYubiKeyFragment.java @@ -210,7 +210,7 @@ public class ViewKeyYubiKeyFragment } @Override - protected void onCryptoOperationResult(PromoteKeyResult result) { + public void onCryptoOperationSuccess(PromoteKeyResult result) { result.createNotify(getActivity()).show(); } } 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 af664fc75..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 extends Fragment implements CryptoOperationHelper.Callback { - private CryptoOperationHelper mOperationHelper; + final private CryptoOperationHelper 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 callback, int progressMessageString) { mActivity = activity; @@ -96,8 +94,6 @@ public class CryptoOperationHelper callback, int progressMessageString) { mFragment = fragment; @@ -108,8 +104,6 @@ public class CryptoOperationHelper callback) { mFragment = fragment; @@ -178,10 +172,6 @@ public class CryptoOperationHelper