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

@@ -205,9 +205,18 @@ public class ProviderHelper {
PGPPublicKey masterKey = keyRing.getPublicKey(); PGPPublicKey masterKey = keyRing.getPublicKey();
long masterKeyId = masterKey.getKeyID(); 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)); 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 { try {
context.getContentResolver().delete(deleteUri, null, null); context.getContentResolver().delete(deleteUri, null, null);
} catch (UnsupportedOperationException e) { } catch (UnsupportedOperationException e) {
@@ -215,6 +224,11 @@ public class ProviderHelper {
} }
ContentValues values = new ContentValues(); 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.MASTER_KEY_ID, masterKeyId);
values.put(KeyRings.KEY_RING_DATA, keyRing.getEncoded()); values.put(KeyRings.KEY_RING_DATA, keyRing.getEncoded());
@@ -261,9 +275,18 @@ public class ProviderHelper {
PGPSecretKey masterKey = keyRing.getSecretKey(); PGPSecretKey masterKey = keyRing.getSecretKey();
long masterKeyId = masterKey.getKeyID(); 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)); 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 { try {
context.getContentResolver().delete(deleteUri, null, null); context.getContentResolver().delete(deleteUri, null, null);
} catch (UnsupportedOperationException e) { } catch (UnsupportedOperationException e) {
@@ -271,6 +294,11 @@ public class ProviderHelper {
} }
ContentValues values = new ContentValues(); 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.MASTER_KEY_ID, masterKeyId);
values.put(KeyRings.KEY_RING_DATA, keyRing.getEncoded()); values.put(KeyRings.KEY_RING_DATA, keyRing.getEncoded());

View File

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

View File

@@ -20,6 +20,7 @@ package org.sufficientlysecure.keychain.ui;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.Objects;
import org.spongycastle.openpgp.PGPPublicKey; import org.spongycastle.openpgp.PGPPublicKey;
import org.spongycastle.openpgp.PGPPublicKeyRing; import org.spongycastle.openpgp.PGPPublicKeyRing;
@@ -111,16 +112,7 @@ public class ViewKeyActivity extends SherlockFragmentActivity implements
mUserIds = (ListView) findViewById(R.id.user_ids); mUserIds = (ListView) findViewById(R.id.user_ids);
mKeys = (ListView) findViewById(R.id.keys); mKeys = (ListView) findViewById(R.id.keys);
Intent intent = getIntent(); loadData(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);
}
} }
@Override @Override
@@ -188,7 +180,21 @@ public class ViewKeyActivity extends SherlockFragmentActivity implements
return super.onOptionsItemSelected(item); 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() { mActionEncrypt.setOnClickListener(new OnClickListener() {
@Override @Override
@@ -340,7 +346,6 @@ public class ViewKeyActivity extends SherlockFragmentActivity implements
// TODO: Can this be done better? fingerprint in db? // TODO: Can this be done better? fingerprint in db?
String fingerprint = PgpKeyHelper.getFingerPrint(this, keyId); String fingerprint = PgpKeyHelper.getFingerPrint(this, keyId);
fingerprint = fingerprint.replace(" ", "\n"); fingerprint = fingerprint.replace(" ", "\n");
mFingerprint.setText(fingerprint); mFingerprint.setText(fingerprint);