preserve state in KeySpinner, and some lint fixes

This commit is contained in:
Vincent Breitmoser 2015-05-27 22:07:34 +02:00
parent 8f3e0a9110
commit 7c20c8df93
5 changed files with 48 additions and 22 deletions

View File

@ -58,7 +58,7 @@ public class AccountSettingsFragment extends Fragment {
this.mAccSettings = accountSettings; this.mAccSettings = accountSettings;
mAccNameView.setText(accountSettings.getAccountName()); 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 (resultCode == Activity.RESULT_OK) {
if (data != null && data.hasExtra(OperationResult.EXTRA_RESULT)) { if (data != null && data.hasExtra(OperationResult.EXTRA_RESULT)) {
EditKeyResult result = data.getParcelableExtra(OperationResult.EXTRA_RESULT); EditKeyResult result = data.getParcelableExtra(OperationResult.EXTRA_RESULT);
mSelectKeySpinner.setSelectedKeyId(result.mMasterKeyId); mSelectKeySpinner.setPreSelectedKeyId(result.mMasterKeyId);
} else { } else {
Log.e(Constants.TAG, "missing result!"); Log.e(Constants.TAG, "missing result!");
} }

View File

@ -60,7 +60,6 @@ import org.sufficientlysecure.keychain.ui.base.CryptoOperationFragment;
import org.sufficientlysecure.keychain.ui.dialog.ProgressDialogFragment; import org.sufficientlysecure.keychain.ui.dialog.ProgressDialogFragment;
import org.sufficientlysecure.keychain.ui.util.Notify; import org.sufficientlysecure.keychain.ui.util.Notify;
import org.sufficientlysecure.keychain.ui.widget.CertifyKeySpinner; 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.Log;
import org.sufficientlysecure.keychain.util.Preferences; import org.sufficientlysecure.keychain.util.Preferences;
@ -113,7 +112,7 @@ public class CertifyKeyFragment extends CryptoOperationFragment
try { try {
CachedPublicKeyRing key = (new ProviderHelper(getActivity())).getCachedPublicKeyRing(certifyKeyId); CachedPublicKeyRing key = (new ProviderHelper(getActivity())).getCachedPublicKeyRing(certifyKeyId);
if (key.canCertify()) { if (key.canCertify()) {
mCertifyKeySpinner.setSelectedKeyId(certifyKeyId); mCertifyKeySpinner.setPreSelectedKeyId(certifyKeyId);
} }
} catch (PgpKeyNotFoundException e) { } catch (PgpKeyNotFoundException e) {
Log.e(Constants.TAG, "certify certify check failed", e); Log.e(Constants.TAG, "certify certify check failed", e);

View File

@ -84,10 +84,15 @@ public class EncryptModeAsymmetricFragment extends EncryptModeFragment {
super.onActivityCreated(savedInstanceState); super.onActivityCreated(savedInstanceState);
mProviderHelper = new ProviderHelper(getActivity()); mProviderHelper = new ProviderHelper(getActivity());
// preselect keys given // preselect keys given, from state or arguments
long signatureKeyId = getArguments().getLong(ARG_SINGATURE_KEY_ID); long signatureKeyId, encryptionKeyIds[];
long[] encryptionKeyIds = getArguments().getLongArray(ARG_ENCRYPTION_KEY_IDS);
preselectKeys(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( CachedPublicKeyRing keyring = mProviderHelper.getCachedPublicKeyRing(
KeyRings.buildUnifiedKeyRingUri(signatureKeyId)); KeyRings.buildUnifiedKeyRingUri(signatureKeyId));
if (keyring.hasAnySecret()) { if (keyring.hasAnySecret()) {
mSignKeySpinner.setSelectedKeyId(signatureKeyId); mSignKeySpinner.setPreSelectedKeyId(signatureKeyId);
} }
} catch (PgpKeyNotFoundException e) { } catch (PgpKeyNotFoundException e) {
Log.e(Constants.TAG, "key not found!", e); Log.e(Constants.TAG, "key not found!", e);

View File

@ -98,7 +98,7 @@ public class CertifyKeySpinner extends KeySpinner {
// - there are actually keys (not just "none" entry) // - there are actually keys (not just "none" entry)
// Then: // Then:
// - select key that is capable of certifying, but only if there is only one key capable of it // - 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 // preselect if key can certify
int selection = -1; int selection = -1;
while (data.moveToNext()) { while (data.moveToNext()) {

View File

@ -17,6 +17,7 @@
package org.sufficientlysecure.keychain.ui.widget; package org.sufficientlysecure.keychain.ui.widget;
import android.annotation.SuppressLint;
import android.content.Context; import android.content.Context;
import android.database.Cursor; import android.database.Cursor;
import android.graphics.Color; import android.graphics.Color;
@ -52,13 +53,13 @@ public abstract class KeySpinner extends AppCompatSpinner implements
LoaderManager.LoaderCallbacks<Cursor> { LoaderManager.LoaderCallbacks<Cursor> {
public static final String ARG_SUPER_STATE = "super_state"; 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 { public interface OnKeyChangedListener {
void onKeyChanged(long masterKeyId); void onKeyChanged(long masterKeyId);
} }
protected long mSelectedKeyId = Constants.key.none; protected long mPreSelectedKeyId = Constants.key.none;
protected SelectKeyAdapter mAdapter = new SelectKeyAdapter(); protected SelectKeyAdapter mAdapter = new SelectKeyAdapter();
protected OnKeyChangedListener mListener; protected OnKeyChangedListener mListener;
@ -85,17 +86,15 @@ public abstract class KeySpinner extends AppCompatSpinner implements
super.setOnItemSelectedListener(new OnItemSelectedListener() { super.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override @Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
mSelectedKeyId = id;
if (mListener != null) { if (mListener != null) {
mListener.onKeyChanged(mSelectedKeyId); mListener.onKeyChanged(id);
} }
} }
@Override @Override
public void onNothingSelected(AdapterView<?> parent) { public void onNothingSelected(AdapterView<?> parent) {
mSelectedKeyId = Constants.key.none;
if (mListener != null) { 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() { public long getSelectedKeyId() {
return mSelectedKeyId; return getSelectedItemId();
} }
public void setSelectedKeyId(long selectedKeyId) { public void setPreSelectedKeyId(long selectedKeyId) {
mSelectedKeyId = selectedKeyId; mPreSelectedKeyId = selectedKeyId;
} }
protected class SelectKeyAdapter extends BaseAdapter implements SpinnerAdapter { 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); mIndexMasterKeyId = newCursor.getColumnIndex(KeychainContract.KeyRings.MASTER_KEY_ID);
mIndexCreationDate = newCursor.getColumnIndex(KeychainContract.KeyRings.CREATION); mIndexCreationDate = newCursor.getColumnIndex(KeychainContract.KeyRings.CREATION);
// pre-select key if mSelectedKeyId is given // pre-select key if mPreSelectedKeyId is given
if (mSelectedKeyId != Constants.key.none && newCursor.moveToFirst()) { if (mPreSelectedKeyId != Constants.key.none && newCursor.moveToFirst()) {
do { do {
if (newCursor.getLong(mIndexMasterKeyId) == mSelectedKeyId) { if (newCursor.getLong(mIndexMasterKeyId) == mPreSelectedKeyId) {
setSelection(newCursor.getPosition() + 1); setSelection(newCursor.getPosition() + 1);
} }
} while (newCursor.moveToNext()); } while (newCursor.moveToNext());
@ -260,6 +259,7 @@ public abstract class KeySpinner extends AppCompatSpinner implements
return inner.getItemId(position - 1); return inner.getItemId(position - 1);
} }
@SuppressLint("ViewHolder") // inflate call is for the preview only
@Override @Override
public View getView(int position, View convertView, ViewGroup parent) { public View getView(int position, View convertView, ViewGroup parent) {
try { try {
@ -304,4 +304,26 @@ public abstract class KeySpinner extends AppCompatSpinner implements
return true; 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;
}
} }