diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AccountSettingsFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AccountSettingsFragment.java index 81181d61d..18afd2f23 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AccountSettingsFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AccountSettingsFragment.java @@ -58,7 +58,7 @@ public class AccountSettingsFragment extends Fragment { this.mAccSettings = accountSettings; mAccNameView.setText(accountSettings.getAccountName()); - mSelectKeySpinner.setSelectedKeyId(accountSettings.getKeyId()); + mSelectKeySpinner.setPreSelectedKeyId(accountSettings.getKeyId()); } /** @@ -107,7 +107,7 @@ public class AccountSettingsFragment extends Fragment { if (resultCode == Activity.RESULT_OK) { if (data != null && data.hasExtra(OperationResult.EXTRA_RESULT)) { EditKeyResult result = data.getParcelableExtra(OperationResult.EXTRA_RESULT); - mSelectKeySpinner.setSelectedKeyId(result.mMasterKeyId); + mSelectKeySpinner.setPreSelectedKeyId(result.mMasterKeyId); } else { Log.e(Constants.TAG, "missing result!"); } 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 df755edef..4506d5efa 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CertifyKeyFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CertifyKeyFragment.java @@ -60,7 +60,6 @@ import org.sufficientlysecure.keychain.ui.base.CryptoOperationFragment; import org.sufficientlysecure.keychain.ui.dialog.ProgressDialogFragment; import org.sufficientlysecure.keychain.ui.util.Notify; import org.sufficientlysecure.keychain.ui.widget.CertifyKeySpinner; -import org.sufficientlysecure.keychain.ui.widget.KeySpinner; import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.Preferences; @@ -113,7 +112,7 @@ public class CertifyKeyFragment extends CryptoOperationFragment try { CachedPublicKeyRing key = (new ProviderHelper(getActivity())).getCachedPublicKeyRing(certifyKeyId); if (key.canCertify()) { - mCertifyKeySpinner.setSelectedKeyId(certifyKeyId); + mCertifyKeySpinner.setPreSelectedKeyId(certifyKeyId); } } catch (PgpKeyNotFoundException e) { Log.e(Constants.TAG, "certify certify check failed", e); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptModeAsymmetricFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptModeAsymmetricFragment.java index 758d5f51a..3b4db6c08 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptModeAsymmetricFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptModeAsymmetricFragment.java @@ -84,10 +84,15 @@ public class EncryptModeAsymmetricFragment extends EncryptModeFragment { super.onActivityCreated(savedInstanceState); mProviderHelper = new ProviderHelper(getActivity()); - // preselect keys given - long signatureKeyId = getArguments().getLong(ARG_SINGATURE_KEY_ID); - long[] encryptionKeyIds = getArguments().getLongArray(ARG_ENCRYPTION_KEY_IDS); - preselectKeys(signatureKeyId, encryptionKeyIds); + // preselect keys given, from state or arguments + long signatureKeyId, encryptionKeyIds[]; + + if (savedInstanceState == null) { + signatureKeyId = getArguments().getLong(ARG_SINGATURE_KEY_ID); + encryptionKeyIds = getArguments().getLongArray(ARG_ENCRYPTION_KEY_IDS); + preselectKeys(signatureKeyId, encryptionKeyIds); + } + } /** @@ -99,7 +104,7 @@ public class EncryptModeAsymmetricFragment extends EncryptModeFragment { CachedPublicKeyRing keyring = mProviderHelper.getCachedPublicKeyRing( KeyRings.buildUnifiedKeyRingUri(signatureKeyId)); if (keyring.hasAnySecret()) { - mSignKeySpinner.setSelectedKeyId(signatureKeyId); + mSignKeySpinner.setPreSelectedKeyId(signatureKeyId); } } catch (PgpKeyNotFoundException e) { Log.e(Constants.TAG, "key not found!", e); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/CertifyKeySpinner.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/CertifyKeySpinner.java index 460163a47..845b35512 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/CertifyKeySpinner.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/CertifyKeySpinner.java @@ -98,7 +98,7 @@ public class CertifyKeySpinner extends KeySpinner { // - there are actually keys (not just "none" entry) // Then: // - select key that is capable of certifying, but only if there is only one key capable of it - if (mSelectedKeyId == Constants.key.none && mAdapter.getCount() > 1) { + if (mPreSelectedKeyId == Constants.key.none && mAdapter.getCount() > 1) { // preselect if key can certify int selection = -1; while (data.moveToNext()) { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/KeySpinner.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/KeySpinner.java index 579f37db2..ad1a14a33 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/KeySpinner.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/KeySpinner.java @@ -17,6 +17,7 @@ package org.sufficientlysecure.keychain.ui.widget; +import android.annotation.SuppressLint; import android.content.Context; import android.database.Cursor; import android.graphics.Color; @@ -52,13 +53,13 @@ public abstract class KeySpinner extends AppCompatSpinner implements LoaderManager.LoaderCallbacks { public static final String ARG_SUPER_STATE = "super_state"; - public static final String ARG_SELECTED_KEY_ID = "select_key_id"; + public static final String ARG_KEY_ID = "key_id"; public interface OnKeyChangedListener { void onKeyChanged(long masterKeyId); } - protected long mSelectedKeyId = Constants.key.none; + protected long mPreSelectedKeyId = Constants.key.none; protected SelectKeyAdapter mAdapter = new SelectKeyAdapter(); protected OnKeyChangedListener mListener; @@ -85,17 +86,15 @@ public abstract class KeySpinner extends AppCompatSpinner implements super.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView parent, View view, int position, long id) { - mSelectedKeyId = id; if (mListener != null) { - mListener.onKeyChanged(mSelectedKeyId); + mListener.onKeyChanged(id); } } @Override public void onNothingSelected(AdapterView parent) { - mSelectedKeyId = Constants.key.none; if (mListener != null) { - mListener.onKeyChanged(mSelectedKeyId); + mListener.onKeyChanged(Constants.key.none); } } }); @@ -139,11 +138,11 @@ public abstract class KeySpinner extends AppCompatSpinner implements } public long getSelectedKeyId() { - return mSelectedKeyId; + return getSelectedItemId(); } - public void setSelectedKeyId(long selectedKeyId) { - mSelectedKeyId = selectedKeyId; + public void setPreSelectedKeyId(long selectedKeyId) { + mPreSelectedKeyId = selectedKeyId; } protected class SelectKeyAdapter extends BaseAdapter implements SpinnerAdapter { @@ -232,10 +231,10 @@ public abstract class KeySpinner extends AppCompatSpinner implements mIndexMasterKeyId = newCursor.getColumnIndex(KeychainContract.KeyRings.MASTER_KEY_ID); mIndexCreationDate = newCursor.getColumnIndex(KeychainContract.KeyRings.CREATION); - // pre-select key if mSelectedKeyId is given - if (mSelectedKeyId != Constants.key.none && newCursor.moveToFirst()) { + // pre-select key if mPreSelectedKeyId is given + if (mPreSelectedKeyId != Constants.key.none && newCursor.moveToFirst()) { do { - if (newCursor.getLong(mIndexMasterKeyId) == mSelectedKeyId) { + if (newCursor.getLong(mIndexMasterKeyId) == mPreSelectedKeyId) { setSelection(newCursor.getPosition() + 1); } } while (newCursor.moveToNext()); @@ -260,6 +259,7 @@ public abstract class KeySpinner extends AppCompatSpinner implements return inner.getItemId(position - 1); } + @SuppressLint("ViewHolder") // inflate call is for the preview only @Override public View getView(int position, View convertView, ViewGroup parent) { try { @@ -304,4 +304,26 @@ public abstract class KeySpinner extends AppCompatSpinner implements return true; } + @Override + public void onRestoreInstanceState(Parcelable state) { + Bundle bundle = (Bundle) state; + + mPreSelectedKeyId = bundle.getLong(ARG_KEY_ID); + + // restore super state + super.onRestoreInstanceState(bundle.getParcelable(ARG_SUPER_STATE)); + + } + + @NonNull + @Override + public Parcelable onSaveInstanceState() { + Bundle bundle = new Bundle(); + + // save super state + bundle.putParcelable(ARG_SUPER_STATE, super.onSaveInstanceState()); + + bundle.putLong(ARG_KEY_ID, getSelectedKeyId()); + return bundle; + } }