diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java index e1514b16f..48119a831 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java @@ -343,6 +343,7 @@ public class KeychainIntentService extends IntentService setProgress(R.string.progress_done, 100, 100); + // cache new passphrase if (saveParcel.newPassphrase != null) { PassphraseCacheService.addCachedPassphrase(this, masterKeyId, saveParcel.newPassphrase); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java index 1defa4034..8c7606cf9 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java @@ -65,7 +65,7 @@ public class EditKeyFragment extends LoaderFragment implements public static final String ARG_DATA_URI = "uri"; private ListView mUserIdsList; - private ListView mKeysList; + private ListView mSubkeysList; private ListView mUserIdsAddedList; private ListView mKeysAddedList; private View mChangePassphrase; @@ -76,7 +76,7 @@ public class EditKeyFragment extends LoaderFragment implements private static final int LOADER_ID_KEYS = 1; private UserIdsAdapter mUserIdsAdapter; - private SubkeysAdapter mKeysAdapter; + private SubkeysAdapter mSubkeysAdapter; private UserIdsAddedAdapter mUserIdsAddedAdapter; private ArrayList mUserIdsAddedData; @@ -104,7 +104,7 @@ public class EditKeyFragment extends LoaderFragment implements View view = inflater.inflate(R.layout.edit_key_fragment, getContainer()); mUserIdsList = (ListView) view.findViewById(R.id.edit_key_user_ids); - mKeysList = (ListView) view.findViewById(R.id.edit_key_keys); + mSubkeysList = (ListView) view.findViewById(R.id.edit_key_keys); mUserIdsAddedList = (ListView) view.findViewById(R.id.edit_key_user_ids_added); mKeysAddedList = (ListView) view.findViewById(R.id.edit_key_keys_added); mChangePassphrase = view.findViewById(R.id.edit_key_action_change_passphrase); @@ -191,13 +191,13 @@ public class EditKeyFragment extends LoaderFragment implements } }); - // TODO: from savedInstance?! + // TODO: mUserIdsAddedData and SaveParcel from savedInstance?! mUserIdsAddedData = new ArrayList(); mUserIdsAddedAdapter = new UserIdsAddedAdapter(getActivity(), mUserIdsAddedData); mUserIdsAddedList.setAdapter(mUserIdsAddedAdapter); - mKeysAdapter = new SubkeysAdapter(getActivity(), null, 0); - mKeysList.setAdapter(mKeysAdapter); + mSubkeysAdapter = new SubkeysAdapter(getActivity(), null, 0); + mSubkeysList.setAdapter(mSubkeysAdapter); // Prepare the loaders. Either re-connect with an existing ones, // or start new ones. @@ -235,7 +235,7 @@ public class EditKeyFragment extends LoaderFragment implements break; case LOADER_ID_KEYS: - mKeysAdapter.swapCursor(data); + mSubkeysAdapter.swapCursor(data); break; } @@ -252,7 +252,7 @@ public class EditKeyFragment extends LoaderFragment implements mUserIdsAdapter.swapCursor(null); break; case LOADER_ID_KEYS: - mKeysAdapter.swapCursor(null); + mSubkeysAdapter.swapCursor(null); break; } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/SubkeysAdapter.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/SubkeysAdapter.java index 6d8455589..f5f4b32e6 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/SubkeysAdapter.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/SubkeysAdapter.java @@ -38,22 +38,11 @@ import java.util.Date; public class SubkeysAdapter extends CursorAdapter { private LayoutInflater mInflater; - private int mIndexKeyId; - private int mIndexAlgorithm; - private int mIndexKeySize; - private int mIndexRank; - private int mIndexCanCertify; - private int mIndexCanEncrypt; - private int mIndexCanSign; - private int mIndexHasSecret; - private int mIndexRevokedKey; - private int mIndexExpiry; - private boolean hasAnySecret; private ColorStateList mDefaultTextColor; - public static final String[] KEYS_PROJECTION = new String[] { + public static final String[] KEYS_PROJECTION = new String[]{ Keys._ID, Keys.KEY_ID, Keys.RANK, @@ -68,24 +57,33 @@ public class SubkeysAdapter extends CursorAdapter { Keys.EXPIRY, Keys.FINGERPRINT }; + private static final int INDEX_ID = 0; + private static final int INDEX_KEY_ID = 1; + private static final int INDEX_RANK = 2; + private static final int INDEX_ALGORITHM = 3; + private static final int INDEX_KEY_SIZE = 4; + private static final int INDEX_HAS_SECRET = 5; + private static final int INDEX_CAN_CERTIFY = 6; + private static final int INDEX_CAN_ENCRYPT = 7; + private static final int INDEX_CAN_SIGN = 8; + private static final int INDEX_IS_REVOKED = 9; + private static final int INDEX_CREATION = 10; + private static final int INDEX_EXPIRY = 11; + private static final int INDEX_FINGERPRINT = 12; public SubkeysAdapter(Context context, Cursor c, int flags) { super(context, c, flags); mInflater = LayoutInflater.from(context); - - initIndex(c); } @Override public Cursor swapCursor(Cursor newCursor) { - initIndex(newCursor); - hasAnySecret = false; if (newCursor != null) { newCursor.moveToFirst(); do { - if (newCursor.getInt(mIndexHasSecret) != 0) { + if (newCursor.getInt(INDEX_HAS_SECRET) != 0) { hasAnySecret = true; break; } @@ -95,27 +93,6 @@ public class SubkeysAdapter extends CursorAdapter { return super.swapCursor(newCursor); } - /** - * Get column indexes for performance reasons just once in constructor and swapCursor. For a - * performance comparison see http://stackoverflow.com/a/17999582 - * - * @param cursor - */ - private void initIndex(Cursor cursor) { - if (cursor != null) { - mIndexKeyId = cursor.getColumnIndexOrThrow(Keys.KEY_ID); - mIndexAlgorithm = cursor.getColumnIndexOrThrow(Keys.ALGORITHM); - mIndexKeySize = cursor.getColumnIndexOrThrow(Keys.KEY_SIZE); - mIndexRank = cursor.getColumnIndexOrThrow(Keys.RANK); - mIndexCanCertify = cursor.getColumnIndexOrThrow(Keys.CAN_CERTIFY); - mIndexCanEncrypt = cursor.getColumnIndexOrThrow(Keys.CAN_ENCRYPT); - mIndexCanSign = cursor.getColumnIndexOrThrow(Keys.CAN_SIGN); - mIndexHasSecret = cursor.getColumnIndexOrThrow(Keys.HAS_SECRET); - mIndexRevokedKey = cursor.getColumnIndexOrThrow(Keys.IS_REVOKED); - mIndexExpiry = cursor.getColumnIndexOrThrow(Keys.EXPIRY); - } - } - @Override public void bindView(View view, Context context, Cursor cursor) { TextView keyId = (TextView) view.findViewById(R.id.keyId); @@ -127,16 +104,16 @@ public class SubkeysAdapter extends CursorAdapter { ImageView signIcon = (ImageView) view.findViewById(R.id.ic_signKey); ImageView revokedKeyIcon = (ImageView) view.findViewById(R.id.ic_revokedKey); - String keyIdStr = PgpKeyHelper.convertKeyIdToHex(cursor.getLong(mIndexKeyId)); + String keyIdStr = PgpKeyHelper.convertKeyIdToHex(cursor.getLong(INDEX_KEY_ID)); String algorithmStr = PgpKeyHelper.getAlgorithmInfo( context, - cursor.getInt(mIndexAlgorithm), - cursor.getInt(mIndexKeySize) + cursor.getInt(INDEX_ALGORITHM), + cursor.getInt(INDEX_KEY_SIZE) ); keyId.setText(keyIdStr); // may be set with additional "stripped" later on - if (hasAnySecret && cursor.getInt(mIndexHasSecret) == 0) { + if (hasAnySecret && cursor.getInt(INDEX_HAS_SECRET) == 0) { keyDetails.setText(algorithmStr + ", " + context.getString(R.string.key_stripped)); } else { @@ -144,13 +121,13 @@ public class SubkeysAdapter extends CursorAdapter { } // Set icons according to properties - masterKeyIcon.setVisibility(cursor.getInt(mIndexRank) == 0 ? View.VISIBLE : View.INVISIBLE); - certifyIcon.setVisibility(cursor.getInt(mIndexCanCertify) != 0 ? View.VISIBLE : View.GONE); - encryptIcon.setVisibility(cursor.getInt(mIndexCanEncrypt) != 0 ? View.VISIBLE : View.GONE); - signIcon.setVisibility(cursor.getInt(mIndexCanSign) != 0 ? View.VISIBLE : View.GONE); + masterKeyIcon.setVisibility(cursor.getInt(INDEX_RANK) == 0 ? View.VISIBLE : View.INVISIBLE); + certifyIcon.setVisibility(cursor.getInt(INDEX_CAN_CERTIFY) != 0 ? View.VISIBLE : View.GONE); + encryptIcon.setVisibility(cursor.getInt(INDEX_CAN_ENCRYPT) != 0 ? View.VISIBLE : View.GONE); + signIcon.setVisibility(cursor.getInt(INDEX_CAN_SIGN) != 0 ? View.VISIBLE : View.GONE); boolean valid = true; - if (cursor.getInt(mIndexRevokedKey) > 0) { + if (cursor.getInt(INDEX_IS_REVOKED) > 0) { revokedKeyIcon.setVisibility(View.VISIBLE); valid = false; @@ -162,17 +139,19 @@ public class SubkeysAdapter extends CursorAdapter { revokedKeyIcon.setVisibility(View.GONE); } - if (!cursor.isNull(mIndexExpiry)) { - Date expiryDate = new Date(cursor.getLong(mIndexExpiry) * 1000); + if (!cursor.isNull(INDEX_EXPIRY)) { + Date expiryDate = new Date(cursor.getLong(INDEX_EXPIRY) * 1000); valid = valid && expiryDate.after(new Date()); keyExpiry.setText( context.getString(R.string.label_expiry) + ": " + - DateFormat.getDateFormat(context).format(expiryDate)); + DateFormat.getDateFormat(context).format(expiryDate) + ); } else { keyExpiry.setText( context.getString(R.string.label_expiry) + ": " + - context.getString(R.string.none)); + context.getString(R.string.none) + ); } // if key is expired or revoked, strike through text diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/UserIdsAdapter.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/UserIdsAdapter.java index 7bf9334b8..47f17357a 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/UserIdsAdapter.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/UserIdsAdapter.java @@ -41,9 +41,6 @@ import java.util.ArrayList; public class UserIdsAdapter extends CursorAdapter implements AdapterView.OnItemClickListener { private LayoutInflater mInflater; - private int mIndexUserId, mIndexRank; - private int mVerifiedId, mIsRevoked, mIsPrimary; - private final ArrayList mCheckStates; private SaveKeyringParcel mSaveKeyringParcel; @@ -56,6 +53,13 @@ public class UserIdsAdapter extends CursorAdapter implements AdapterView.OnItemC UserIds.IS_PRIMARY, UserIds.IS_REVOKED }; + private static final int INDEX_ID = 0; + private static final int INDEX_USER_ID = 1; + private static final int INDEX_RANK = 2; + private static final int INDEX_VERIFIED = 3; + private static final int INDEX_IS_PRIMARY = 4; + private static final int INDEX_IS_REVOKED = 5; + public UserIdsAdapter(Context context, Cursor c, int flags, boolean showCheckBoxes, SaveKeyringParcel saveKeyringParcel) { @@ -64,8 +68,6 @@ public class UserIdsAdapter extends CursorAdapter implements AdapterView.OnItemC mCheckStates = showCheckBoxes ? new ArrayList() : null; mSaveKeyringParcel = saveKeyringParcel; - - initIndex(c); } public UserIdsAdapter(Context context, Cursor c, int flags, boolean showCheckBoxes) { @@ -82,7 +84,6 @@ public class UserIdsAdapter extends CursorAdapter implements AdapterView.OnItemC @Override public Cursor swapCursor(Cursor newCursor) { - initIndex(newCursor); if (mCheckStates != null) { mCheckStates.clear(); if (newCursor != null) { @@ -91,7 +92,7 @@ public class UserIdsAdapter extends CursorAdapter implements AdapterView.OnItemC // initialize to true (use case knowledge: we usually want to sign all uids) for (int i = 0; i < count; i++) { newCursor.moveToPosition(i); - int verified = newCursor.getInt(mVerifiedId); + int verified = newCursor.getInt(INDEX_VERIFIED); mCheckStates.add(verified != Certs.VERIFIED_SECRET); } } @@ -100,22 +101,6 @@ public class UserIdsAdapter extends CursorAdapter implements AdapterView.OnItemC return super.swapCursor(newCursor); } - /** - * Get column indexes for performance reasons just once in constructor and swapCursor. For a - * performance comparison see http://stackoverflow.com/a/17999582 - * - * @param cursor - */ - private void initIndex(Cursor cursor) { - if (cursor != null) { - mIndexUserId = cursor.getColumnIndexOrThrow(UserIds.USER_ID); - mIndexRank = cursor.getColumnIndexOrThrow(UserIds.RANK); - mVerifiedId = cursor.getColumnIndexOrThrow(UserIds.VERIFIED); - mIsRevoked = cursor.getColumnIndexOrThrow(UserIds.IS_REVOKED); - mIsPrimary = cursor.getColumnIndexOrThrow(UserIds.IS_PRIMARY); - } - } - @Override public void bindView(View view, Context context, Cursor cursor) { TextView vName = (TextView) view.findViewById(R.id.userId); @@ -125,7 +110,7 @@ public class UserIdsAdapter extends CursorAdapter implements AdapterView.OnItemC ImageView vHasChanges = (ImageView) view.findViewById(R.id.has_changes); ImageView vEditImage = (ImageView) view.findViewById(R.id.edit_image); - String userId = cursor.getString(mIndexUserId); + String userId = cursor.getString(INDEX_USER_ID); String[] splitUserId = KeyRing.splitUserId(userId); if (splitUserId[0] != null) { vName.setText(splitUserId[0]); @@ -145,8 +130,8 @@ public class UserIdsAdapter extends CursorAdapter implements AdapterView.OnItemC vComment.setVisibility(View.GONE); } - boolean isPrimary = cursor.getInt(mIsPrimary) != 0; - boolean isRevoked = cursor.getInt(mIsRevoked) > 0; + boolean isPrimary = cursor.getInt(INDEX_IS_PRIMARY) != 0; + boolean isRevoked = cursor.getInt(INDEX_IS_REVOKED) > 0; // for edit key if (mSaveKeyringParcel != null) { @@ -189,7 +174,7 @@ public class UserIdsAdapter extends CursorAdapter implements AdapterView.OnItemC // verified: has been verified // isPrimary: show small star icon for primary user ids - int verified = cursor.getInt(mVerifiedId); + int verified = cursor.getInt(INDEX_VERIFIED); switch (verified) { case Certs.VERIFIED_SECRET: vVerified.setImageResource(isPrimary @@ -237,7 +222,7 @@ public class UserIdsAdapter extends CursorAdapter implements AdapterView.OnItemC for (int i = 0; i < mCheckStates.size(); i++) { if (mCheckStates.get(i)) { mCursor.moveToPosition(i); - result.add(mCursor.getString(mIndexUserId)); + result.add(mCursor.getString(INDEX_USER_ID)); } } return result; @@ -245,7 +230,7 @@ public class UserIdsAdapter extends CursorAdapter implements AdapterView.OnItemC public String getUserId(int position) { mCursor.moveToPosition(position); - return mCursor.getString(mIndexUserId); + return mCursor.getString(INDEX_USER_ID); } @Override