From b9563ff2ef881396f1b5b3c46436165fefd63464 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Sat, 30 May 2015 00:17:00 +0200 Subject: [PATCH] externalize caching functionality from CertifyKeyFragment --- .../keychain/ui/CertifyKeyFragment.java | 31 ++++----- .../base/CachingCryptoOperationFragment.java | 64 +++++++++++++++++++ 2 files changed, 75 insertions(+), 20 deletions(-) create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/base/CachingCryptoOperationFragment.java diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CertifyKeyFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CertifyKeyFragment.java index 119c5be3b..17836aa37 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CertifyKeyFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CertifyKeyFragment.java @@ -56,7 +56,7 @@ import org.sufficientlysecure.keychain.service.KeychainIntentService; import org.sufficientlysecure.keychain.service.ServiceProgressHandler; import org.sufficientlysecure.keychain.service.input.CryptoInputParcel; import org.sufficientlysecure.keychain.ui.adapter.MultiUserIdsAdapter; -import org.sufficientlysecure.keychain.ui.base.CryptoOperationFragment; +import org.sufficientlysecure.keychain.ui.base.CachingCryptoOperationFragment; import org.sufficientlysecure.keychain.ui.dialog.ProgressDialogFragment; import org.sufficientlysecure.keychain.ui.util.Notify; import org.sufficientlysecure.keychain.ui.widget.CertifyKeySpinner; @@ -65,12 +65,10 @@ import org.sufficientlysecure.keychain.util.Preferences; import java.util.ArrayList; - -public class CertifyKeyFragment extends CryptoOperationFragment +public class CertifyKeyFragment extends CachingCryptoOperationFragment implements LoaderManager.LoaderCallbacks { public static final String ARG_CHECK_STATES = "check_states"; - public static final String ARG_CACHED_ACTIONS = "cached_actions"; private CheckBox mUploadKeyCheckbox; ListView mUserIds; @@ -79,8 +77,6 @@ public class CertifyKeyFragment extends CryptoOperationFragment private long[] mPubMasterKeyIds; - private CertifyActionsParcel mCachedActionsParcel; - public static final String[] USER_IDS_PROJECTION = new String[]{ UserPackets._ID, UserPackets.MASTER_KEY_ID, @@ -113,13 +109,9 @@ public class CertifyKeyFragment extends CryptoOperationFragment ArrayList checkedStates; if (savedInstanceState != null) { - mCachedActionsParcel = savedInstanceState.getParcelable(ARG_CACHED_ACTIONS); checkedStates = (ArrayList) savedInstanceState.getSerializable(ARG_CHECK_STATES); - // key spinner and the checkbox keep their own state - } else { - mCachedActionsParcel = null; checkedStates = null; // preselect certify key id if given @@ -158,7 +150,6 @@ public class CertifyKeyFragment extends CryptoOperationFragment ArrayList states = mUserIdsAdapter.getCheckStates(); // no proper parceling method available :( outState.putSerializable(ARG_CHECK_STATES, states); - outState.putParcelable(ARG_CACHED_ACTIONS, mCachedActionsParcel); } @Override @@ -185,7 +176,7 @@ public class CertifyKeyFragment extends CryptoOperationFragment Notify.create(getActivity(), getString(R.string.select_key_to_certify), Notify.Style.ERROR).show(); } else { - cryptoOperation(new CryptoInputParcel()); + cryptoOperation(); } } }); @@ -316,11 +307,11 @@ public class CertifyKeyFragment extends CryptoOperationFragment } @Override - protected void cryptoOperation(CryptoInputParcel cryptoInput) { + protected void cryptoOperation(CryptoInputParcel cryptoInput, CertifyActionsParcel actionsParcel) { Bundle data = new Bundle(); { - if (mCachedActionsParcel == null) { + if (actionsParcel == null) { // Bail out if there is not at least one user id selected ArrayList certifyActions = mUserIdsAdapter.getSelectedCertifyActions(); if (certifyActions.isEmpty()) { @@ -332,12 +323,15 @@ public class CertifyKeyFragment extends CryptoOperationFragment long selectedKeyId = mCertifyKeySpinner.getSelectedKeyId(); // fill values for this action - mCachedActionsParcel = new CertifyActionsParcel(selectedKeyId); - mCachedActionsParcel.mCertifyActions.addAll(certifyActions); + actionsParcel = new CertifyActionsParcel(selectedKeyId); + actionsParcel.mCertifyActions.addAll(certifyActions); + + // cached for next cryptoOperation loop + cacheActionsParcel(actionsParcel); } data.putParcelable(KeychainIntentService.EXTRA_CRYPTO_INPUT, cryptoInput); - data.putParcelable(KeychainIntentService.CERTIFY_PARCEL, mCachedActionsParcel); + data.putParcelable(KeychainIntentService.CERTIFY_PARCEL, actionsParcel); if (mUploadKeyCheckbox.isChecked()) { String keyserver = Preferences.getPreferences(getActivity()).getPreferredKeyserver(); @@ -404,8 +398,5 @@ public class CertifyKeyFragment extends CryptoOperationFragment @Override protected void onCryptoOperationCancelled() { super.onCryptoOperationCancelled(); - - // forget this ever happened - mCachedActionsParcel = null; } } 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 new file mode 100644 index 000000000..d0b6f502f --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/base/CachingCryptoOperationFragment.java @@ -0,0 +1,64 @@ +package org.sufficientlysecure.keychain.ui.base; + + +import android.os.Bundle; +import android.os.Message; +import android.os.Parcelable; + +import org.sufficientlysecure.keychain.service.ServiceProgressHandler; +import org.sufficientlysecure.keychain.service.input.CryptoInputParcel; + + +public abstract class CachingCryptoOperationFragment extends CryptoOperationFragment { + + 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); + + outState.putParcelable(ARG_CACHED_ACTIONS, mCachedActionsParcel); + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + if (savedInstanceState != null) { + mCachedActionsParcel = savedInstanceState.getParcelable(ARG_CACHED_ACTIONS); + } + } + + @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 abstract void cryptoOperation(CryptoInputParcel cryptoInput, T cachedActionsParcel); + + protected void cacheActionsParcel(T cachedActionsParcel) { + mCachedActionsParcel = cachedActionsParcel; + } + + protected void onCryptoOperationCancelled() { + mCachedActionsParcel = null; + } + +}