ViewKeyMainFragment cleanup (don't use inline queries!)

This commit is contained in:
Vincent Breitmoser 2014-04-04 14:37:07 +02:00
parent 958eba1c95
commit 53e8afaee0

View File

@ -73,6 +73,7 @@ public class ViewKeyMainFragment extends Fragment implements
private ListView mUserIds; private ListView mUserIds;
private ListView mKeys; private ListView mKeys;
private static final int LOADER_ID_UNIFIED = 0;
private static final int LOADER_ID_USER_IDS = 1; private static final int LOADER_ID_USER_IDS = 1;
private static final int LOADER_ID_KEYS = 2; private static final int LOADER_ID_KEYS = 2;
@ -131,60 +132,17 @@ public class ViewKeyMainFragment extends Fragment implements
Log.i(Constants.TAG, "mDataUri: " + mDataUri.toString()); Log.i(Constants.TAG, "mDataUri: " + mDataUri.toString());
{ // label whether secret key is available, and edit button if it is
final long masterKeyId = ProviderHelper.getMasterKeyId(getActivity(), mDataUri);
// TODO do this some other way...
if (ProviderHelper.hasSecretKeyByMasterKeyId(getActivity(), masterKeyId)) {
// set this attribute. this is a LITTLE unclean, but we have the info available
// right here, so why not.
mSecretKey.setTextColor(getResources().getColor(R.color.emphasis));
mSecretKey.setText(R.string.secret_key_yes);
// certify button
// TODO this button MIGHT be useful if the user wants to
// certify a private key with another...
// mActionCertify.setVisibility(View.GONE);
// edit button
mActionEdit.setVisibility(View.VISIBLE);
mActionEdit.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent editIntent = new Intent(getActivity(), EditKeyActivity.class);
editIntent.setData(
KeyRingData.buildSecretKeyRingUri(
Long.toString(masterKeyId)));
editIntent.setAction(EditKeyActivity.ACTION_EDIT_KEY);
startActivityForResult(editIntent, 0);
}
});
} else {
mSecretKey.setTextColor(Color.BLACK);
mSecretKey.setText(getResources().getString(R.string.secret_key_no));
// certify button
mActionCertify.setVisibility(View.VISIBLE);
// edit button
mActionEdit.setVisibility(View.GONE);
}
// TODO see todo note above, doing this here for now
mActionCertify.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
certifyKey(KeyRings.buildGenericKeyRingUri(
Long.toString(masterKeyId)
));
}
});
}
mActionEncrypt.setOnClickListener(new View.OnClickListener() { mActionEncrypt.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
encryptToContact(mDataUri); encryptToContact(mDataUri);
} }
}); });
mActionCertify.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
certifyKey(mDataUri);
}
});
mUserIdsAdapter = new ViewKeyUserIdsAdapter(getActivity(), null, 0); mUserIdsAdapter = new ViewKeyUserIdsAdapter(getActivity(), null, 0);
mUserIds.setAdapter(mUserIdsAdapter); mUserIds.setAdapter(mUserIdsAdapter);
@ -194,49 +152,50 @@ public class ViewKeyMainFragment extends Fragment implements
// Prepare the loaders. Either re-connect with an existing ones, // Prepare the loaders. Either re-connect with an existing ones,
// or start new ones. // or start new ones.
getActivity().getSupportLoaderManager().initLoader(LOADER_ID_UNIFIED, null, this);
getActivity().getSupportLoaderManager().initLoader(LOADER_ID_USER_IDS, null, this); getActivity().getSupportLoaderManager().initLoader(LOADER_ID_USER_IDS, null, this);
getActivity().getSupportLoaderManager().initLoader(LOADER_ID_KEYS, null, this); getActivity().getSupportLoaderManager().initLoader(LOADER_ID_KEYS, null, this);
} }
static final String[] USER_IDS_PROJECTION = static final String[] UNIFIED_PROJECTION = new String[] {
new String[] { KeyRings._ID, KeyRings.MASTER_KEY_ID, KeyRings.HAS_SECRET,
UserIds._ID, KeyRings.USER_ID, KeyRings.FINGERPRINT,
UserIds.USER_ID, KeyRings.ALGORITHM, KeyRings.KEY_SIZE, KeyRings.CREATION, KeyRings.EXPIRY,
UserIds.RANK,
}; };
static final int INDEX_UID_UID = 1; static final int INDEX_UNIFIED_MKI = 1;
static final int INDEX_UNIFIED_HAS_SECRET = 2;
static final int INDEX_UNIFIED_UID = 3;
static final int INDEX_UNIFIED_FINGERPRINT = 4;
static final int INDEX_UNIFIED_ALGORITHM = 5;
static final int INDEX_UNIFIED_KEY_SIZE = 6;
static final int INDEX_UNIFIED_CREATION = 7;
static final int INDEX_UNIFIED_EXPIRY = 8;
static final String[] USER_IDS_PROJECTION = new String[] {
UserIds._ID, UserIds.USER_ID, UserIds.RANK,
};
static final String[] KEYS_PROJECTION = new String[] { static final String[] KEYS_PROJECTION = new String[] {
Keys._ID, Keys._ID,
Keys.KEY_ID, Keys.RANK, Keys.KEY_ID, Keys.RANK, Keys.ALGORITHM, Keys.KEY_SIZE,
Keys.ALGORITHM, Keys.KEY_SIZE, Keys.CAN_CERTIFY, Keys.CAN_ENCRYPT, Keys.CAN_SIGN, Keys.IS_REVOKED,
Keys.CAN_CERTIFY, Keys.CAN_ENCRYPT, Keys.CREATION, Keys.EXPIRY, Keys.FINGERPRINT
Keys.CAN_SIGN, Keys.IS_REVOKED,
Keys.CREATION, Keys.EXPIRY,
Keys.FINGERPRINT
}; };
static final int KEYS_INDEX_KEY_ID = 1;
static final int KEYS_INDEX_ALGORITHM = 3;
static final int KEYS_INDEX_KEY_SIZE = 4;
static final int KEYS_INDEX_CAN_ENCRYPT = 6; static final int KEYS_INDEX_CAN_ENCRYPT = 6;
static final int KEYS_INDEX_CREATION = 9;
static final int KEYS_INDEX_EXPIRY = 10;
static final int KEYS_INDEX_FINGERPRINT = 11;
public Loader<Cursor> onCreateLoader(int id, Bundle args) { public Loader<Cursor> onCreateLoader(int id, Bundle args) {
switch (id) { switch (id) {
case LOADER_ID_UNIFIED: {
Uri baseUri = KeyRings.buildUnifiedKeyRingUri(mDataUri);
return new CursorLoader(getActivity(), baseUri, UNIFIED_PROJECTION, null, null, null);
}
case LOADER_ID_USER_IDS: { case LOADER_ID_USER_IDS: {
Uri baseUri = UserIds.buildUserIdsUri(mDataUri); Uri baseUri = UserIds.buildUserIdsUri(mDataUri);
// Now create and return a CursorLoader that will take care of
// creating a Cursor for the data being displayed.
return new CursorLoader(getActivity(), baseUri, USER_IDS_PROJECTION, null, null, null); return new CursorLoader(getActivity(), baseUri, USER_IDS_PROJECTION, null, null, null);
} }
case LOADER_ID_KEYS: { case LOADER_ID_KEYS: {
Uri baseUri = Keys.buildKeysUri(mDataUri); Uri baseUri = Keys.buildKeysUri(mDataUri);
// Now create and return a CursorLoader that will take care of
// creating a Cursor for the data being displayed.
return new CursorLoader(getActivity(), baseUri, KEYS_PROJECTION, null, null, null); return new CursorLoader(getActivity(), baseUri, KEYS_PROJECTION, null, null, null);
} }
@ -249,11 +208,10 @@ public class ViewKeyMainFragment extends Fragment implements
// Swap the new cursor in. (The framework will take care of closing the // Swap the new cursor in. (The framework will take care of closing the
// old cursor once we return.) // old cursor once we return.)
switch (loader.getId()) { switch (loader.getId()) {
case LOADER_ID_USER_IDS: case LOADER_ID_UNIFIED: {
if (data.moveToFirst()) { if (data.moveToFirst()) {
// get name, email, and comment from USER_ID // get name, email, and comment from USER_ID
String[] mainUserId = PgpKeyHelper.splitUserId(data String[] mainUserId = PgpKeyHelper.splitUserId(data.getString(INDEX_UNIFIED_UID));
.getString(INDEX_UID_UID));
if (mainUserId[0] != null) { if (mainUserId[0] != null) {
getActivity().setTitle(mainUserId[0]); getActivity().setTitle(mainUserId[0]);
mName.setText(mainUserId[0]); mName.setText(mainUserId[0]);
@ -263,22 +221,43 @@ public class ViewKeyMainFragment extends Fragment implements
} }
mEmail.setText(mainUserId[1]); mEmail.setText(mainUserId[1]);
mComment.setText(mainUserId[2]); mComment.setText(mainUserId[2]);
}
mUserIdsAdapter.swapCursor(data); if (data.getInt(INDEX_UNIFIED_HAS_SECRET) > 0) {
break; // set this attribute. this is a LITTLE unclean, but we have the info available
case LOADER_ID_KEYS: // right here, so why not.
// the first key here is our master key mSecretKey.setTextColor(getResources().getColor(R.color.emphasis));
if (data.moveToFirst()) { mSecretKey.setText(R.string.secret_key_yes);
// edit button
mActionEdit.setVisibility(View.VISIBLE);
mActionEdit.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent editIntent = new Intent(getActivity(), EditKeyActivity.class);
editIntent.setData(mDataUri);
editIntent.setAction(EditKeyActivity.ACTION_EDIT_KEY);
startActivityForResult(editIntent, 0);
}
});
} else {
mSecretKey.setTextColor(Color.BLACK);
mSecretKey.setText(getResources().getString(R.string.secret_key_no));
// certify button
mActionCertify.setVisibility(View.VISIBLE);
// edit button
mActionEdit.setVisibility(View.GONE);
}
// get key id from MASTER_KEY_ID // get key id from MASTER_KEY_ID
long keyId = data.getLong(KEYS_INDEX_KEY_ID); long masterKeyId = data.getLong(INDEX_UNIFIED_MKI);
String keyIdStr = PgpKeyHelper.convertKeyIdToHex(keyId); String keyIdStr = PgpKeyHelper.convertKeyIdToHex(masterKeyId);
mKeyId.setText(keyIdStr); mKeyId.setText(keyIdStr);
// get creation date from CREATION // get creation date from CREATION
if (data.isNull(KEYS_INDEX_CREATION)) { if (data.isNull(INDEX_UNIFIED_CREATION)) {
mCreation.setText(R.string.none); mCreation.setText(R.string.none);
} else { } else {
Date creationDate = new Date(data.getLong(KEYS_INDEX_CREATION) * 1000); Date creationDate = new Date(data.getLong(INDEX_UNIFIED_CREATION) * 1000);
mCreation.setText( mCreation.setText(
DateFormat.getDateFormat(getActivity().getApplicationContext()).format( DateFormat.getDateFormat(getActivity().getApplicationContext()).format(
@ -286,10 +265,10 @@ public class ViewKeyMainFragment extends Fragment implements
} }
// get expiry date from EXPIRY // get expiry date from EXPIRY
if (data.isNull(KEYS_INDEX_EXPIRY)) { if (data.isNull(INDEX_UNIFIED_EXPIRY)) {
mExpiry.setText(R.string.none); mExpiry.setText(R.string.none);
} else { } else {
Date expiryDate = new Date(data.getLong(KEYS_INDEX_EXPIRY) * 1000); Date expiryDate = new Date(data.getLong(INDEX_UNIFIED_EXPIRY) * 1000);
mExpiry.setText( mExpiry.setText(
DateFormat.getDateFormat(getActivity().getApplicationContext()).format( DateFormat.getDateFormat(getActivity().getApplicationContext()).format(
@ -297,15 +276,22 @@ public class ViewKeyMainFragment extends Fragment implements
} }
String algorithmStr = PgpKeyHelper.getAlgorithmInfo( String algorithmStr = PgpKeyHelper.getAlgorithmInfo(
data.getInt(KEYS_INDEX_ALGORITHM), data.getInt(KEYS_INDEX_KEY_SIZE)); data.getInt(INDEX_UNIFIED_ALGORITHM), data.getInt(INDEX_UNIFIED_KEY_SIZE));
mAlgorithm.setText(algorithmStr); mAlgorithm.setText(algorithmStr);
byte[] fingerprintBlob = data.getBlob(KEYS_INDEX_FINGERPRINT); byte[] fingerprintBlob = data.getBlob(INDEX_UNIFIED_FINGERPRINT);
String fingerprint = PgpKeyHelper.convertFingerprintToHex(fingerprintBlob); String fingerprint = PgpKeyHelper.convertFingerprintToHex(fingerprintBlob);
mFingerprint.setText(PgpKeyHelper.colorizeFingerprint(fingerprint)); mFingerprint.setText(PgpKeyHelper.colorizeFingerprint(fingerprint));
}
break;
}
}
case LOADER_ID_USER_IDS:
mUserIdsAdapter.swapCursor(data);
break;
case LOADER_ID_KEYS:
// hide encrypt button if no encryption key is available // hide encrypt button if no encryption key is available
boolean canEncrypt = false; boolean canEncrypt = false;
data.moveToFirst(); data.moveToFirst();
@ -321,9 +307,6 @@ public class ViewKeyMainFragment extends Fragment implements
mKeysAdapter.swapCursor(data); mKeysAdapter.swapCursor(data);
break; break;
default:
break;
} }
getActivity().setProgressBarIndeterminateVisibility(Boolean.FALSE); getActivity().setProgressBarIndeterminateVisibility(Boolean.FALSE);
mContainer.setVisibility(View.VISIBLE); mContainer.setVisibility(View.VISIBLE);
@ -341,8 +324,6 @@ public class ViewKeyMainFragment extends Fragment implements
case LOADER_ID_KEYS: case LOADER_ID_KEYS:
mKeysAdapter.swapCursor(null); mKeysAdapter.swapCursor(null);
break; break;
default:
break;
} }
} }