save keyring in-place, fixes #228, #203

This commit is contained in:
Dominik Schürmann 2014-01-29 03:06:26 +01:00
parent ca9696ff34
commit f276455624
3 changed files with 235 additions and 201 deletions

View File

@ -55,14 +55,14 @@ public class ProviderHelper {
/**
* Private helper method to get PGPKeyRing from database
*
*
* @param context
* @param queryUri
* @return
*/
public static PGPKeyRing getPGPKeyRing(Context context, Uri queryUri) {
Cursor cursor = context.getContentResolver().query(queryUri,
new String[] { KeyRings._ID, KeyRings.KEY_RING_DATA }, null, null, null);
new String[]{KeyRings._ID, KeyRings.KEY_RING_DATA}, null, null, null);
PGPKeyRing keyRing = null;
if (cursor != null && cursor.moveToFirst()) {
@ -83,7 +83,7 @@ public class ProviderHelper {
/**
* Retrieves the actual PGPPublicKeyRing object from the database blob based on the rowId
*
*
* @param context
* @param rowId
* @return
@ -95,13 +95,13 @@ public class ProviderHelper {
/**
* Retrieves the actual PGPPublicKeyRing object from the database blob based on the maserKeyId
*
*
* @param context
* @param masterKeyId
* @return
*/
public static PGPPublicKeyRing getPGPPublicKeyRingByMasterKeyId(Context context,
long masterKeyId) {
long masterKeyId) {
Uri queryUri = KeyRings.buildPublicKeyRingsByMasterKeyIdUri(Long.toString(masterKeyId));
return (PGPPublicKeyRing) getPGPKeyRing(context, queryUri);
}
@ -109,7 +109,7 @@ public class ProviderHelper {
/**
* Retrieves the actual PGPPublicKeyRing object from the database blob associated with a key
* with this keyId
*
*
* @param context
* @param keyId
* @return
@ -122,7 +122,7 @@ public class ProviderHelper {
/**
* Retrieves the actual PGPPublicKey object from the database blob associated with a key with
* this keyId
*
*
* @param context
* @param keyId
* @return
@ -138,7 +138,7 @@ public class ProviderHelper {
/**
* Retrieves the actual PGPSecretKeyRing object from the database blob based on the rowId
*
*
* @param context
* @param rowId
* @return
@ -150,13 +150,13 @@ public class ProviderHelper {
/**
* Retrieves the actual PGPSecretKeyRing object from the database blob based on the maserKeyId
*
*
* @param context
* @param masterKeyId
* @return
*/
public static PGPSecretKeyRing getPGPSecretKeyRingByMasterKeyId(Context context,
long masterKeyId) {
long masterKeyId) {
Uri queryUri = KeyRings.buildSecretKeyRingsByMasterKeyIdUri(Long.toString(masterKeyId));
return (PGPSecretKeyRing) getPGPKeyRing(context, queryUri);
}
@ -164,7 +164,7 @@ public class ProviderHelper {
/**
* Retrieves the actual PGPSecretKeyRing object from the database blob associated with a key
* with this keyId
*
*
* @param context
* @param keyId
* @return
@ -177,7 +177,7 @@ public class ProviderHelper {
/**
* Retrieves the actual PGPSecretKey object from the database blob associated with a key with
* this keyId
*
*
* @param context
* @param keyId
* @return
@ -193,7 +193,7 @@ public class ProviderHelper {
/**
* Saves PGPPublicKeyRing with its keys and userIds in DB
*
*
* @param context
* @param keyRing
* @return
@ -205,9 +205,18 @@ public class ProviderHelper {
PGPPublicKey masterKey = keyRing.getPublicKey();
long masterKeyId = masterKey.getKeyID();
// delete old version of this keyRing, which also deletes all keys and userIds on cascade
Uri deleteUri = KeyRings.buildPublicKeyRingsByMasterKeyIdUri(Long.toString(masterKeyId));
// get current _ID of key
long currentRowId = -1;
Cursor oldQuery = context.getContentResolver().query(deleteUri, new String[]{KeyRings._ID}, null, null, null);
if (oldQuery != null && oldQuery.moveToFirst()) {
currentRowId = oldQuery.getLong(0);
} else {
Log.e(Constants.TAG, "Key could not be found! Something wrong is happening!");
}
// delete old version of this keyRing, which also deletes all keys and userIds on cascade
try {
context.getContentResolver().delete(deleteUri, null, null);
} catch (UnsupportedOperationException e) {
@ -215,6 +224,11 @@ public class ProviderHelper {
}
ContentValues values = new ContentValues();
// use exactly the same _ID again to replace key in-place.
// NOTE: If we would not use the same _ID again, getting back to the ViewKeyActivity would result in Nullpointer,
// because the currently loaded key would be gone from the database
if (currentRowId != -1)
values.put(KeyRings._ID, currentRowId);
values.put(KeyRings.MASTER_KEY_ID, masterKeyId);
values.put(KeyRings.KEY_RING_DATA, keyRing.getEncoded());
@ -249,7 +263,7 @@ public class ProviderHelper {
/**
* Saves PGPSecretKeyRing with its keys and userIds in DB
*
*
* @param context
* @param keyRing
* @return
@ -261,9 +275,18 @@ public class ProviderHelper {
PGPSecretKey masterKey = keyRing.getSecretKey();
long masterKeyId = masterKey.getKeyID();
// delete old version of this keyRing, which also deletes all keys and userIds on cascade
Uri deleteUri = KeyRings.buildSecretKeyRingsByMasterKeyIdUri(Long.toString(masterKeyId));
// get current _ID of key
long currentRowId = -1;
Cursor oldQuery = context.getContentResolver().query(deleteUri, new String[]{KeyRings._ID}, null, null, null);
if (oldQuery != null && oldQuery.moveToFirst()) {
currentRowId = oldQuery.getLong(0);
} else {
Log.e(Constants.TAG, "Key could not be found! Something wrong is happening!");
}
// delete old version of this keyRing, which also deletes all keys and userIds on cascade
try {
context.getContentResolver().delete(deleteUri, null, null);
} catch (UnsupportedOperationException e) {
@ -271,6 +294,11 @@ public class ProviderHelper {
}
ContentValues values = new ContentValues();
// use exactly the same _ID again to replace key in-place.
// NOTE: If we would not use the same _ID again, getting back to the ViewKeyActivity would result in Nullpointer,
// because the currently loaded key would be gone from the database
if (currentRowId != -1)
values.put(KeyRings._ID, currentRowId);
values.put(KeyRings.MASTER_KEY_ID, masterKeyId);
values.put(KeyRings.KEY_RING_DATA, keyRing.getEncoded());
@ -305,7 +333,7 @@ public class ProviderHelper {
/**
* Build ContentProviderOperation to add PGPPublicKey to database corresponding to a keyRing
*
*
* @param context
* @param keyRingRowId
* @param key
@ -314,7 +342,7 @@ public class ProviderHelper {
* @throws IOException
*/
private static ContentProviderOperation buildPublicKeyOperations(Context context,
long keyRingRowId, PGPPublicKey key, int rank) throws IOException {
long keyRingRowId, PGPPublicKey key, int rank) throws IOException {
ContentValues values = new ContentValues();
values.put(Keys.KEY_ID, key.getKeyID());
values.put(Keys.IS_MASTER_KEY, key.isMasterKey());
@ -339,7 +367,7 @@ public class ProviderHelper {
/**
* Build ContentProviderOperation to add PublicUserIds to database corresponding to a keyRing
*
*
* @param context
* @param keyRingRowId
* @param key
@ -348,7 +376,7 @@ public class ProviderHelper {
* @throws IOException
*/
private static ContentProviderOperation buildPublicUserIdOperations(Context context,
long keyRingRowId, String userId, int rank) {
long keyRingRowId, String userId, int rank) {
ContentValues values = new ContentValues();
values.put(UserIds.KEY_RING_ROW_ID, keyRingRowId);
values.put(UserIds.USER_ID, userId);
@ -361,7 +389,7 @@ public class ProviderHelper {
/**
* Build ContentProviderOperation to add PGPSecretKey to database corresponding to a keyRing
*
*
* @param context
* @param keyRingRowId
* @param key
@ -370,7 +398,7 @@ public class ProviderHelper {
* @throws IOException
*/
private static ContentProviderOperation buildSecretKeyOperations(Context context,
long keyRingRowId, PGPSecretKey key, int rank) throws IOException {
long keyRingRowId, PGPSecretKey key, int rank) throws IOException {
ContentValues values = new ContentValues();
boolean has_private = true;
@ -404,7 +432,7 @@ public class ProviderHelper {
/**
* Build ContentProviderOperation to add SecretUserIds to database corresponding to a keyRing
*
*
* @param context
* @param keyRingRowId
* @param key
@ -413,7 +441,7 @@ public class ProviderHelper {
* @throws IOException
*/
private static ContentProviderOperation buildSecretUserIdOperations(Context context,
long keyRingRowId, String userId, int rank) {
long keyRingRowId, String userId, int rank) {
ContentValues values = new ContentValues();
values.put(UserIds.KEY_RING_ROW_ID, keyRingRowId);
values.put(UserIds.USER_ID, userId);
@ -426,14 +454,14 @@ public class ProviderHelper {
/**
* Private helper method
*
*
* @param context
* @param queryUri
* @return
*/
private static ArrayList<Long> getKeyRingsMasterKeyIds(Context context, Uri queryUri) {
Cursor cursor = context.getContentResolver().query(queryUri,
new String[] { KeyRings.MASTER_KEY_ID }, null, null, null);
new String[]{KeyRings.MASTER_KEY_ID}, null, null, null);
ArrayList<Long> masterKeyIds = new ArrayList<Long>();
if (cursor != null) {
@ -454,7 +482,7 @@ public class ProviderHelper {
/**
* Retrieves ids of all SecretKeyRings
*
*
* @param context
* @return
*/
@ -465,7 +493,7 @@ public class ProviderHelper {
/**
* Retrieves ids of all PublicKeyRings
*
*
* @param context
* @return
*/
@ -486,7 +514,7 @@ public class ProviderHelper {
/**
* Get master key id of keyring by its row id
*
*
* @param context
* @param keyRingRowId
* @return
@ -498,7 +526,7 @@ public class ProviderHelper {
/**
* Get empty status of master key of keyring by its row id
*
*
* @param context
* @param keyRingRowId
* @return
@ -510,20 +538,20 @@ public class ProviderHelper {
/**
* Private helper method to get master key private empty status of keyring by its row id
*
*
* @param context
* @param queryUri
* @param keyRingRowId
* @return
*/
private static boolean getMasterKeyCanSign(Context context, Uri queryUri, long keyRingRowId) {
String[] projection = new String[] {
String[] projection = new String[]{
KeyRings.MASTER_KEY_ID,
"(SELECT COUNT(sign_keys." + Keys._ID + ") FROM " + Tables.KEYS
+ " AS sign_keys WHERE sign_keys." + Keys.KEY_RING_ROW_ID + " = "
+ KeychainDatabase.Tables.KEY_RINGS + "." + KeyRings._ID
+ " AND sign_keys." + Keys.CAN_SIGN + " = '1' AND " + Keys.IS_MASTER_KEY
+ " = 1) AS sign", };
+ " = 1) AS sign",};
ContentResolver cr = context.getContentResolver();
Cursor cursor = cr.query(queryUri, projection, null, null, null);
@ -544,7 +572,7 @@ public class ProviderHelper {
/**
* Get master key id of keyring by its row id
*
*
* @param context
* @param keyRingRowId
* @return
@ -556,14 +584,14 @@ public class ProviderHelper {
/**
* Private helper method to get master key id of keyring by its row id
*
*
* @param context
* @param queryUri
* @param keyRingRowId
* @return
*/
public static long getMasterKeyId(Context context, Uri queryUri) {
String[] projection = new String[] { KeyRings.MASTER_KEY_ID };
String[] projection = new String[]{KeyRings.MASTER_KEY_ID};
ContentResolver cr = context.getContentResolver();
Cursor cursor = cr.query(queryUri, projection, null, null, null);
@ -583,17 +611,17 @@ public class ProviderHelper {
}
public static ArrayList<String> getPublicKeyRingsAsArmoredString(Context context,
long[] masterKeyIds) {
long[] masterKeyIds) {
return getKeyRingsAsArmoredString(context, KeyRings.buildPublicKeyRingsUri(), masterKeyIds);
}
public static ArrayList<String> getSecretKeyRingsAsArmoredString(Context context,
long[] masterKeyIds) {
long[] masterKeyIds) {
return getKeyRingsAsArmoredString(context, KeyRings.buildSecretKeyRingsUri(), masterKeyIds);
}
public static ArrayList<String> getKeyRingsAsArmoredString(Context context, Uri uri,
long[] masterKeyIds) {
long[] masterKeyIds) {
ArrayList<String> output = new ArrayList<String>();
if (masterKeyIds != null && masterKeyIds.length > 0) {
@ -697,7 +725,7 @@ public class ProviderHelper {
}
private static Cursor getCursorWithSelectedKeyringMasterKeyIds(Context context, Uri baseUri,
long[] masterKeyIds) {
long[] masterKeyIds) {
Cursor cursor = null;
if (masterKeyIds != null && masterKeyIds.length > 0) {
@ -711,7 +739,7 @@ public class ProviderHelper {
inMasterKeyList += ")";
cursor = context.getContentResolver().query(baseUri,
new String[] { KeyRings._ID, KeyRings.MASTER_KEY_ID, KeyRings.KEY_RING_DATA },
new String[]{KeyRings._ID, KeyRings.MASTER_KEY_ID, KeyRings.KEY_RING_DATA},
inMasterKeyList, null, null);
}
@ -788,7 +816,7 @@ public class ProviderHelper {
public static byte[] getApiAppSignature(Context context, String packageName) {
Uri queryUri = KeychainContract.ApiApps.buildByPackageNameUri(packageName);
String[] projection = new String[] { ApiApps.PACKAGE_SIGNATURE };
String[] projection = new String[]{ApiApps.PACKAGE_SIGNATURE};
ContentResolver cr = context.getContentResolver();
Cursor cursor = cr.query(queryUri, projection, null, null, null);

View File

@ -236,6 +236,7 @@ public class SignKeyActivity extends SherlockFragmentActivity implements
*/
uploadKey();
} else {
setResult(RESULT_OK);
finish();
}
}
@ -278,10 +279,10 @@ public class SignKeyActivity extends SherlockFragmentActivity implements
super.handleMessage(message);
if (message.arg1 == KeychainIntentServiceHandler.MESSAGE_OKAY) {
Toast.makeText(SignKeyActivity.this, R.string.key_send_success,
Toast.LENGTH_SHORT).show();
setResult(RESULT_OK);
finish();
}
};

View File

@ -20,6 +20,7 @@ package org.sufficientlysecure.keychain.ui;
import java.util.ArrayList;
import java.util.Date;
import java.util.Objects;
import org.spongycastle.openpgp.PGPPublicKey;
import org.spongycastle.openpgp.PGPPublicKeyRing;
@ -111,16 +112,7 @@ public class ViewKeyActivity extends SherlockFragmentActivity implements
mUserIds = (ListView) findViewById(R.id.user_ids);
mKeys = (ListView) findViewById(R.id.keys);
Intent intent = getIntent();
mDataUri = intent.getData();
if (mDataUri == null) {
Log.e(Constants.TAG, "Intent data missing. Should be Uri of key!");
finish();
return;
} else {
Log.d(Constants.TAG, "uri: " + mDataUri);
loadData(mDataUri);
}
loadData(getIntent());
}
@Override
@ -133,62 +125,76 @@ public class ViewKeyActivity extends SherlockFragmentActivity implements
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent homeIntent = new Intent(this, KeyListPublicActivity.class);
homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homeIntent);
return true;
case R.id.menu_key_view_update:
updateFromKeyserver(mDataUri);
return true;
case R.id.menu_key_view_sign:
signKey(mDataUri);
return true;
case R.id.menu_key_view_export_keyserver:
uploadToKeyserver(mDataUri);
return true;
case R.id.menu_key_view_export_file:
mExportHelper.showExportKeysDialog(mDataUri, Id.type.public_key, Constants.path.APP_DIR
+ "/pubexport.asc");
return true;
case R.id.menu_key_view_share_default_fingerprint:
shareKey(mDataUri, true);
return true;
case R.id.menu_key_view_share_default:
shareKey(mDataUri, false);
return true;
case R.id.menu_key_view_share_qr_code_fingerprint:
shareKeyQrCode(mDataUri, true);
return true;
case R.id.menu_key_view_share_qr_code:
shareKeyQrCode(mDataUri, false);
return true;
case R.id.menu_key_view_share_nfc:
shareNfc();
return true;
case R.id.menu_key_view_share_clipboard:
copyToClipboard(mDataUri);
return true;
case R.id.menu_key_view_delete: {
// Message is received after key is deleted
Handler returnHandler = new Handler() {
@Override
public void handleMessage(Message message) {
if (message.what == DeleteKeyDialogFragment.MESSAGE_OKAY) {
setResult(RESULT_CANCELED);
finish();
case android.R.id.home:
Intent homeIntent = new Intent(this, KeyListPublicActivity.class);
homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homeIntent);
return true;
case R.id.menu_key_view_update:
updateFromKeyserver(mDataUri);
return true;
case R.id.menu_key_view_sign:
signKey(mDataUri);
return true;
case R.id.menu_key_view_export_keyserver:
uploadToKeyserver(mDataUri);
return true;
case R.id.menu_key_view_export_file:
mExportHelper.showExportKeysDialog(mDataUri, Id.type.public_key, Constants.path.APP_DIR
+ "/pubexport.asc");
return true;
case R.id.menu_key_view_share_default_fingerprint:
shareKey(mDataUri, true);
return true;
case R.id.menu_key_view_share_default:
shareKey(mDataUri, false);
return true;
case R.id.menu_key_view_share_qr_code_fingerprint:
shareKeyQrCode(mDataUri, true);
return true;
case R.id.menu_key_view_share_qr_code:
shareKeyQrCode(mDataUri, false);
return true;
case R.id.menu_key_view_share_nfc:
shareNfc();
return true;
case R.id.menu_key_view_share_clipboard:
copyToClipboard(mDataUri);
return true;
case R.id.menu_key_view_delete: {
// Message is received after key is deleted
Handler returnHandler = new Handler() {
@Override
public void handleMessage(Message message) {
if (message.what == DeleteKeyDialogFragment.MESSAGE_OKAY) {
setResult(RESULT_CANCELED);
finish();
}
}
}
};
};
mExportHelper.deleteKey(mDataUri, Id.type.public_key, returnHandler);
return true;
}
mExportHelper.deleteKey(mDataUri, Id.type.public_key, returnHandler);
return true;
}
}
return super.onOptionsItemSelected(item);
}
private void loadData(Uri dataUri) {
private void loadData(Intent intent) {
if (intent.getData().equals(mDataUri)) {
Log.d(Constants.TAG, "Same URI, no need to load the data again!");
return;
}
mDataUri = intent.getData();
if (mDataUri == null) {
Log.e(Constants.TAG, "Intent data missing. Should be Uri of key!");
finish();
return;
}
Log.i(Constants.TAG, "mDataUri: " + mDataUri.toString());
mActionEncrypt.setOnClickListener(new OnClickListener() {
@Override
@ -198,7 +204,7 @@ public class ViewKeyActivity extends SherlockFragmentActivity implements
ViewKeyActivity.this, mDataUri);
PGPPublicKey publicKey = ring.getPublicKey();
long[] encryptionKeyIds = new long[] { publicKey.getKeyID() };
long[] encryptionKeyIds = new long[]{publicKey.getKeyID()};
Intent intent = new Intent(ViewKeyActivity.this, EncryptActivity.class);
intent.setAction(EncryptActivity.ACTION_ENCRYPT);
intent.putExtra(EncryptActivity.EXTRA_ENCRYPTION_KEY_IDS, encryptionKeyIds);
@ -229,21 +235,21 @@ public class ViewKeyActivity extends SherlockFragmentActivity implements
getSupportLoaderManager().initLoader(LOADER_ID_KEYS, null, this);
}
static final String[] KEYRING_PROJECTION = new String[] { KeyRings._ID, KeyRings.MASTER_KEY_ID,
UserIds.USER_ID };
static final String[] KEYRING_PROJECTION = new String[]{KeyRings._ID, KeyRings.MASTER_KEY_ID,
UserIds.USER_ID};
static final int KEYRING_INDEX_ID = 0;
static final int KEYRING_INDEX_MASTER_KEY_ID = 1;
static final int KEYRING_INDEX_USER_ID = 2;
static final String[] USER_IDS_PROJECTION = new String[] { UserIds._ID, UserIds.USER_ID,
UserIds.RANK, };
static final String[] USER_IDS_PROJECTION = new String[]{UserIds._ID, UserIds.USER_ID,
UserIds.RANK,};
// not the main user id
static final String USER_IDS_SELECTION = UserIds.RANK + " > 0 ";
static final String USER_IDS_SORT_ORDER = UserIds.USER_ID + " COLLATE LOCALIZED ASC";
static final String[] KEYS_PROJECTION = new String[] { Keys._ID, Keys.KEY_ID,
static final String[] KEYS_PROJECTION = new String[]{Keys._ID, Keys.KEY_ID,
Keys.IS_MASTER_KEY, Keys.ALGORITHM, Keys.KEY_SIZE, Keys.CAN_CERTIFY, Keys.CAN_SIGN,
Keys.CAN_ENCRYPT, Keys.CREATION, Keys.EXPIRY };
Keys.CAN_ENCRYPT, Keys.CREATION, Keys.EXPIRY};
static final String KEYS_SORT_ORDER = Keys.RANK + " ASC";
static final int KEYS_INDEX_ID = 0;
static final int KEYS_INDEX_KEY_ID = 1;
@ -258,31 +264,31 @@ public class ViewKeyActivity extends SherlockFragmentActivity implements
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
switch (id) {
case LOADER_ID_KEYRING: {
Uri baseUri = mDataUri;
case LOADER_ID_KEYRING: {
Uri baseUri = mDataUri;
// Now create and return a CursorLoader that will take care of
// creating a Cursor for the data being displayed.
return new CursorLoader(this, baseUri, KEYRING_PROJECTION, null, null, null);
}
case LOADER_ID_USER_IDS: {
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(this, baseUri, KEYRING_PROJECTION, null, null, null);
}
case LOADER_ID_USER_IDS: {
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(this, baseUri, USER_IDS_PROJECTION, USER_IDS_SELECTION, null,
USER_IDS_SORT_ORDER);
}
case LOADER_ID_KEYS: {
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(this, baseUri, USER_IDS_PROJECTION, USER_IDS_SELECTION, null,
USER_IDS_SORT_ORDER);
}
case LOADER_ID_KEYS: {
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(this, baseUri, KEYS_PROJECTION, null, null, KEYS_SORT_ORDER);
}
// Now create and return a CursorLoader that will take care of
// creating a Cursor for the data being displayed.
return new CursorLoader(this, baseUri, KEYS_PROJECTION, null, null, KEYS_SORT_ORDER);
}
default:
return null;
default:
return null;
}
}
@ -290,69 +296,68 @@ public class ViewKeyActivity extends SherlockFragmentActivity implements
// Swap the new cursor in. (The framework will take care of closing the
// old cursor once we return.)
switch (loader.getId()) {
case LOADER_ID_KEYRING:
if (data.moveToFirst()) {
// get name, email, and comment from USER_ID
String[] mainUserId = PgpKeyHelper.splitUserId(data
.getString(KEYRING_INDEX_USER_ID));
setTitle(mainUserId[0]);
mName.setText(mainUserId[0]);
mEmail.setText(mainUserId[1]);
mComment.setText(mainUserId[2]);
}
break;
case LOADER_ID_USER_IDS:
mUserIdsAdapter.swapCursor(data);
break;
case LOADER_ID_KEYS:
// the first key here is our master key
if (data.moveToFirst()) {
// get key id from MASTER_KEY_ID
long keyId = data.getLong(KEYS_INDEX_KEY_ID);
String keyIdStr = "0x" + PgpKeyHelper.convertKeyIdToHex(keyId);
mKeyId.setText(keyIdStr);
// get creation date from CREATION
if (data.isNull(KEYS_INDEX_CREATION)) {
mCreation.setText(R.string.none);
} else {
Date creationDate = new Date(data.getLong(KEYS_INDEX_CREATION) * 1000);
mCreation.setText(DateFormat.getDateFormat(getApplicationContext()).format(
creationDate));
case LOADER_ID_KEYRING:
if (data.moveToFirst()) {
// get name, email, and comment from USER_ID
String[] mainUserId = PgpKeyHelper.splitUserId(data
.getString(KEYRING_INDEX_USER_ID));
setTitle(mainUserId[0]);
mName.setText(mainUserId[0]);
mEmail.setText(mainUserId[1]);
mComment.setText(mainUserId[2]);
}
// get creation date from EXPIRY
if (data.isNull(KEYS_INDEX_EXPIRY)) {
mExpiry.setText(R.string.none);
} else {
Date expiryDate = new Date(data.getLong(KEYS_INDEX_EXPIRY) * 1000);
break;
case LOADER_ID_USER_IDS:
mUserIdsAdapter.swapCursor(data);
break;
case LOADER_ID_KEYS:
// the first key here is our master key
if (data.moveToFirst()) {
// get key id from MASTER_KEY_ID
long keyId = data.getLong(KEYS_INDEX_KEY_ID);
mExpiry.setText(DateFormat.getDateFormat(getApplicationContext()).format(
expiryDate));
String keyIdStr = "0x" + PgpKeyHelper.convertKeyIdToHex(keyId);
mKeyId.setText(keyIdStr);
// get creation date from CREATION
if (data.isNull(KEYS_INDEX_CREATION)) {
mCreation.setText(R.string.none);
} else {
Date creationDate = new Date(data.getLong(KEYS_INDEX_CREATION) * 1000);
mCreation.setText(DateFormat.getDateFormat(getApplicationContext()).format(
creationDate));
}
// get creation date from EXPIRY
if (data.isNull(KEYS_INDEX_EXPIRY)) {
mExpiry.setText(R.string.none);
} else {
Date expiryDate = new Date(data.getLong(KEYS_INDEX_EXPIRY) * 1000);
mExpiry.setText(DateFormat.getDateFormat(getApplicationContext()).format(
expiryDate));
}
String algorithmStr = PgpKeyHelper.getAlgorithmInfo(
data.getInt(KEYS_INDEX_ALGORITHM), data.getInt(KEYS_INDEX_KEY_SIZE));
mAlgorithm.setText(algorithmStr);
// TODO: Can this be done better? fingerprint in db?
String fingerprint = PgpKeyHelper.getFingerPrint(this, keyId);
fingerprint = fingerprint.replace(" ", "\n");
mFingerprint.setText(fingerprint);
// TODO: get image with getUserAttributes() on key and then
// PGPUserAttributeSubpacketVector
}
String algorithmStr = PgpKeyHelper.getAlgorithmInfo(
data.getInt(KEYS_INDEX_ALGORITHM), data.getInt(KEYS_INDEX_KEY_SIZE));
mAlgorithm.setText(algorithmStr);
mKeysAdapter.swapCursor(data);
break;
// TODO: Can this be done better? fingerprint in db?
String fingerprint = PgpKeyHelper.getFingerPrint(this, keyId);
fingerprint = fingerprint.replace(" ", "\n");
mFingerprint.setText(fingerprint);
// TODO: get image with getUserAttributes() on key and then
// PGPUserAttributeSubpacketVector
}
mKeysAdapter.swapCursor(data);
break;
default:
break;
default:
break;
}
}
@ -362,17 +367,17 @@ public class ViewKeyActivity extends SherlockFragmentActivity implements
*/
public void onLoaderReset(Loader<Cursor> loader) {
switch (loader.getId()) {
case LOADER_ID_KEYRING:
// TODO?
break;
case LOADER_ID_USER_IDS:
mUserIdsAdapter.swapCursor(null);
break;
case LOADER_ID_KEYS:
mKeysAdapter.swapCursor(null);
break;
default:
break;
case LOADER_ID_KEYRING:
// TODO?
break;
case LOADER_ID_USER_IDS:
mUserIdsAdapter.swapCursor(null);
break;
case LOADER_ID_KEYS:
mKeysAdapter.swapCursor(null);
break;
default:
break;
}
}
@ -438,7 +443,7 @@ public class ViewKeyActivity extends SherlockFragmentActivity implements
// get public keyring as ascii armored string
long masterKeyId = ProviderHelper.getMasterKeyId(this, dataUri);
ArrayList<String> keyringArmored = ProviderHelper.getKeyRingsAsArmoredString(this,
dataUri, new long[] { masterKeyId });
dataUri, new long[]{masterKeyId});
content = keyringArmored.get(0);
@ -469,7 +474,7 @@ public class ViewKeyActivity extends SherlockFragmentActivity implements
// get public keyring as ascii armored string
long masterKeyId = ProviderHelper.getMasterKeyId(this, dataUri);
ArrayList<String> keyringArmored = ProviderHelper.getKeyRingsAsArmoredString(this, dataUri,
new long[] { masterKeyId });
new long[]{masterKeyId});
ClipboardReflection.copyToClipboard(this, keyringArmored.get(0));
Toast.makeText(getApplicationContext(), R.string.key_copied_to_clipboard, Toast.LENGTH_LONG)