Use master key id instead of fingerprint in sync adapter, use IS_EXPIRED instead of EXPIRY where possible

This commit is contained in:
Dominik Schürmann 2015-03-04 15:02:51 +01:00
parent b10fe01b82
commit ff42dfc9db
9 changed files with 130 additions and 120 deletions

View File

@ -77,7 +77,6 @@ import org.sufficientlysecure.keychain.util.Log;
import org.sufficientlysecure.keychain.util.Preferences; import org.sufficientlysecure.keychain.util.Preferences;
import java.io.IOException; import java.io.IOException;
import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import se.emilsjolander.stickylistheaders.StickyListHeadersAdapter; import se.emilsjolander.stickylistheaders.StickyListHeadersAdapter;
@ -268,7 +267,7 @@ public class KeyListFragment extends LoaderFragment
KeyRings.MASTER_KEY_ID, KeyRings.MASTER_KEY_ID,
KeyRings.USER_ID, KeyRings.USER_ID,
KeyRings.IS_REVOKED, KeyRings.IS_REVOKED,
KeyRings.EXPIRY, KeyRings.IS_EXPIRED,
KeyRings.VERIFIED, KeyRings.VERIFIED,
KeyRings.HAS_ANY_SECRET KeyRings.HAS_ANY_SECRET
}; };
@ -276,7 +275,7 @@ public class KeyListFragment extends LoaderFragment
static final int INDEX_MASTER_KEY_ID = 1; static final int INDEX_MASTER_KEY_ID = 1;
static final int INDEX_USER_ID = 2; static final int INDEX_USER_ID = 2;
static final int INDEX_IS_REVOKED = 3; static final int INDEX_IS_REVOKED = 3;
static final int INDEX_EXPIRY = 4; static final int INDEX_IS_EXPIRED = 4;
static final int INDEX_VERIFIED = 5; static final int INDEX_VERIFIED = 5;
static final int INDEX_HAS_ANY_SECRET = 6; static final int INDEX_HAS_ANY_SECRET = 6;
@ -708,8 +707,7 @@ public class KeyListFragment extends LoaderFragment
long masterKeyId = cursor.getLong(INDEX_MASTER_KEY_ID); long masterKeyId = cursor.getLong(INDEX_MASTER_KEY_ID);
boolean isSecret = cursor.getInt(INDEX_HAS_ANY_SECRET) != 0; boolean isSecret = cursor.getInt(INDEX_HAS_ANY_SECRET) != 0;
boolean isRevoked = cursor.getInt(INDEX_IS_REVOKED) > 0; boolean isRevoked = cursor.getInt(INDEX_IS_REVOKED) > 0;
boolean isExpired = !cursor.isNull(INDEX_EXPIRY) boolean isExpired = cursor.getInt(INDEX_IS_EXPIRED) != 0;
&& new Date(cursor.getLong(INDEX_EXPIRY) * 1000).before(new Date());
boolean isVerified = cursor.getInt(INDEX_VERIFIED) > 0; boolean isVerified = cursor.getInt(INDEX_VERIFIED) > 0;
h.mMasterKeyId = masterKeyId; h.mMasterKeyId = masterKeyId;

View File

@ -83,7 +83,6 @@ import org.sufficientlysecure.keychain.util.Log;
import org.sufficientlysecure.keychain.util.Preferences; import org.sufficientlysecure.keychain.util.Preferences;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
public class ViewKeyActivity extends BaseActivity implements public class ViewKeyActivity extends BaseActivity implements
@ -750,7 +749,7 @@ public class ViewKeyActivity extends BaseActivity implements
KeychainContract.KeyRings.MASTER_KEY_ID, KeychainContract.KeyRings.MASTER_KEY_ID,
KeychainContract.KeyRings.USER_ID, KeychainContract.KeyRings.USER_ID,
KeychainContract.KeyRings.IS_REVOKED, KeychainContract.KeyRings.IS_REVOKED,
KeychainContract.KeyRings.EXPIRY, KeychainContract.KeyRings.IS_EXPIRED,
KeychainContract.KeyRings.VERIFIED, KeychainContract.KeyRings.VERIFIED,
KeychainContract.KeyRings.HAS_ANY_SECRET, KeychainContract.KeyRings.HAS_ANY_SECRET,
KeychainContract.KeyRings.FINGERPRINT, KeychainContract.KeyRings.FINGERPRINT,
@ -760,7 +759,7 @@ public class ViewKeyActivity extends BaseActivity implements
static final int INDEX_MASTER_KEY_ID = 1; static final int INDEX_MASTER_KEY_ID = 1;
static final int INDEX_USER_ID = 2; static final int INDEX_USER_ID = 2;
static final int INDEX_IS_REVOKED = 3; static final int INDEX_IS_REVOKED = 3;
static final int INDEX_EXPIRY = 4; static final int INDEX_IS_EXPIRED = 4;
static final int INDEX_VERIFIED = 5; static final int INDEX_VERIFIED = 5;
static final int INDEX_HAS_ANY_SECRET = 6; static final int INDEX_HAS_ANY_SECRET = 6;
static final int INDEX_FINGERPRINT = 7; static final int INDEX_FINGERPRINT = 7;
@ -810,8 +809,7 @@ public class ViewKeyActivity extends BaseActivity implements
mIsSecret = data.getInt(INDEX_HAS_ANY_SECRET) != 0; mIsSecret = data.getInt(INDEX_HAS_ANY_SECRET) != 0;
mHasEncrypt = data.getInt(INDEX_HAS_ENCRYPT) != 0; mHasEncrypt = data.getInt(INDEX_HAS_ENCRYPT) != 0;
boolean isRevoked = data.getInt(INDEX_IS_REVOKED) > 0; boolean isRevoked = data.getInt(INDEX_IS_REVOKED) > 0;
boolean isExpired = !data.isNull(INDEX_EXPIRY) boolean isExpired = data.getInt(INDEX_IS_EXPIRED) != 0;
&& new Date(data.getLong(INDEX_EXPIRY) * 1000).before(new Date());
mIsVerified = data.getInt(INDEX_VERIFIED) > 0; mIsVerified = data.getInt(INDEX_VERIFIED) > 0;
// if the refresh animation isn't playing // if the refresh animation isn't playing
@ -821,10 +819,10 @@ public class ViewKeyActivity extends BaseActivity implements
// this is done at the end of the animation otherwise // this is done at the end of the animation otherwise
} }
AsyncTask<String, Void, Bitmap> photoTask = AsyncTask<Long, Void, Bitmap> photoTask =
new AsyncTask<String, Void, Bitmap>() { new AsyncTask<Long, Void, Bitmap>() {
protected Bitmap doInBackground(String... fingerprint) { protected Bitmap doInBackground(Long... mMasterKeyId) {
return ContactHelper.photoFromFingerprint(getContentResolver(), fingerprint[0]); return ContactHelper.photoFromMasterKeyId(getContentResolver(), mMasterKeyId[0]);
} }
protected void onPostExecute(Bitmap photo) { protected void onPostExecute(Bitmap photo) {
@ -871,7 +869,7 @@ public class ViewKeyActivity extends BaseActivity implements
if ( !mFingerprint.equals(oldFingerprint)) { if ( !mFingerprint.equals(oldFingerprint)) {
loadQrCode(mFingerprint); loadQrCode(mFingerprint);
} }
photoTask.execute(mFingerprint); photoTask.execute(mMasterKeyId);
mQrCodeLayout.setVisibility(View.VISIBLE); mQrCodeLayout.setVisibility(View.VISIBLE);
// and place leftOf qr code // and place leftOf qr code
@ -917,7 +915,7 @@ public class ViewKeyActivity extends BaseActivity implements
KeyFormattingUtils.setStatusImage(this, mStatusImage, mStatusText, KeyFormattingUtils.setStatusImage(this, mStatusImage, mStatusText,
KeyFormattingUtils.STATE_VERIFIED, R.color.icons, true); KeyFormattingUtils.STATE_VERIFIED, R.color.icons, true);
color = getResources().getColor(R.color.primary); color = getResources().getColor(R.color.primary);
photoTask.execute(mFingerprint); photoTask.execute(mMasterKeyId);
mFab.setVisibility(View.GONE); mFab.setVisibility(View.GONE);
} else { } else {

View File

@ -43,8 +43,6 @@ import org.sufficientlysecure.keychain.util.ContactHelper;
import org.sufficientlysecure.keychain.util.ExportHelper; import org.sufficientlysecure.keychain.util.ExportHelper;
import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.Log;
import java.util.Date;
public class ViewKeyAdvActivity extends BaseActivity implements public class ViewKeyAdvActivity extends BaseActivity implements
LoaderManager.LoaderCallbacks<Cursor> { LoaderManager.LoaderCallbacks<Cursor> {
@ -159,7 +157,7 @@ public class ViewKeyAdvActivity extends BaseActivity implements
KeychainContract.KeyRings.MASTER_KEY_ID, KeychainContract.KeyRings.MASTER_KEY_ID,
KeychainContract.KeyRings.USER_ID, KeychainContract.KeyRings.USER_ID,
KeychainContract.KeyRings.IS_REVOKED, KeychainContract.KeyRings.IS_REVOKED,
KeychainContract.KeyRings.EXPIRY, KeychainContract.KeyRings.IS_EXPIRED,
KeychainContract.KeyRings.VERIFIED, KeychainContract.KeyRings.VERIFIED,
KeychainContract.KeyRings.HAS_ANY_SECRET KeychainContract.KeyRings.HAS_ANY_SECRET
}; };
@ -167,7 +165,7 @@ public class ViewKeyAdvActivity extends BaseActivity implements
static final int INDEX_MASTER_KEY_ID = 1; static final int INDEX_MASTER_KEY_ID = 1;
static final int INDEX_USER_ID = 2; static final int INDEX_USER_ID = 2;
static final int INDEX_IS_REVOKED = 3; static final int INDEX_IS_REVOKED = 3;
static final int INDEX_EXPIRY = 4; static final int INDEX_IS_EXPIRED = 4;
static final int INDEX_VERIFIED = 5; static final int INDEX_VERIFIED = 5;
static final int INDEX_HAS_ANY_SECRET = 6; static final int INDEX_HAS_ANY_SECRET = 6;
@ -212,8 +210,7 @@ public class ViewKeyAdvActivity extends BaseActivity implements
boolean isSecret = data.getInt(INDEX_HAS_ANY_SECRET) != 0; boolean isSecret = data.getInt(INDEX_HAS_ANY_SECRET) != 0;
boolean isRevoked = data.getInt(INDEX_IS_REVOKED) > 0; boolean isRevoked = data.getInt(INDEX_IS_REVOKED) > 0;
boolean isExpired = !data.isNull(INDEX_EXPIRY) boolean isExpired = data.getInt(INDEX_IS_EXPIRED) != 0;
&& new Date(data.getLong(INDEX_EXPIRY) * 1000).before(new Date());
boolean isVerified = data.getInt(INDEX_VERIFIED) > 0; boolean isVerified = data.getInt(INDEX_VERIFIED) > 0;
// Note: order is important // Note: order is important

View File

@ -260,7 +260,7 @@ public class ViewKeyAdvShareFragment extends LoaderFragment implements
static final String[] UNIFIED_PROJECTION = new String[]{ static final String[] UNIFIED_PROJECTION = new String[]{
KeyRings._ID, KeyRings.MASTER_KEY_ID, KeyRings.HAS_ANY_SECRET, KeyRings._ID, KeyRings.MASTER_KEY_ID, KeyRings.HAS_ANY_SECRET,
KeyRings.USER_ID, KeyRings.FINGERPRINT, KeyRings.USER_ID, KeyRings.FINGERPRINT,
KeyRings.ALGORITHM, KeyRings.KEY_SIZE, KeyRings.CREATION, KeyRings.EXPIRY, KeyRings.ALGORITHM, KeyRings.KEY_SIZE, KeyRings.CREATION, KeyRings.IS_EXPIRED,
}; };
static final int INDEX_UNIFIED_MASTER_KEY_ID = 1; static final int INDEX_UNIFIED_MASTER_KEY_ID = 1;
@ -270,7 +270,7 @@ public class ViewKeyAdvShareFragment extends LoaderFragment implements
static final int INDEX_UNIFIED_ALGORITHM = 5; static final int INDEX_UNIFIED_ALGORITHM = 5;
static final int INDEX_UNIFIED_KEY_SIZE = 6; static final int INDEX_UNIFIED_KEY_SIZE = 6;
static final int INDEX_UNIFIED_CREATION = 7; static final int INDEX_UNIFIED_CREATION = 7;
static final int INDEX_UNIFIED_EXPIRY = 8; static final int INDEX_UNIFIED_ID_EXPIRED = 8;
public Loader<Cursor> onCreateLoader(int id, Bundle args) { public Loader<Cursor> onCreateLoader(int id, Bundle args) {
setContentShown(false); setContentShown(false);

View File

@ -114,12 +114,12 @@ public class ViewKeyAdvUserIdsFragment extends LoaderFragment implements
static final String[] UNIFIED_PROJECTION = new String[]{ static final String[] UNIFIED_PROJECTION = new String[]{
KeyRings._ID, KeyRings.MASTER_KEY_ID, KeyRings._ID, KeyRings.MASTER_KEY_ID,
KeyRings.HAS_ANY_SECRET, KeyRings.IS_REVOKED, KeyRings.EXPIRY, KeyRings.HAS_ENCRYPT KeyRings.HAS_ANY_SECRET, KeyRings.IS_REVOKED, KeyRings.IS_EXPIRED, KeyRings.HAS_ENCRYPT
}; };
static final int INDEX_UNIFIED_MASTER_KEY_ID = 1; static final int INDEX_UNIFIED_MASTER_KEY_ID = 1;
static final int INDEX_UNIFIED_HAS_ANY_SECRET = 2; static final int INDEX_UNIFIED_HAS_ANY_SECRET = 2;
static final int INDEX_UNIFIED_IS_REVOKED = 3; static final int INDEX_UNIFIED_IS_REVOKED = 3;
static final int INDEX_UNIFIED_EXPIRY = 4; static final int INDEX_UNIFIED_IS_EXPIRED = 4;
static final int INDEX_UNIFIED_HAS_ENCRYPT = 5; static final int INDEX_UNIFIED_HAS_ENCRYPT = 5;
public Loader<Cursor> onCreateLoader(int id, Bundle args) { public Loader<Cursor> onCreateLoader(int id, Bundle args) {

View File

@ -121,7 +121,7 @@ public class ViewKeyFragment extends LoaderFragment implements
KeychainContract.KeyRings.MASTER_KEY_ID, KeychainContract.KeyRings.MASTER_KEY_ID,
KeychainContract.KeyRings.USER_ID, KeychainContract.KeyRings.USER_ID,
KeychainContract.KeyRings.IS_REVOKED, KeychainContract.KeyRings.IS_REVOKED,
KeychainContract.KeyRings.EXPIRY, KeychainContract.KeyRings.IS_EXPIRED,
KeychainContract.KeyRings.VERIFIED, KeychainContract.KeyRings.VERIFIED,
KeychainContract.KeyRings.HAS_ANY_SECRET, KeychainContract.KeyRings.HAS_ANY_SECRET,
KeychainContract.KeyRings.FINGERPRINT, KeychainContract.KeyRings.FINGERPRINT,
@ -131,7 +131,7 @@ public class ViewKeyFragment extends LoaderFragment implements
static final int INDEX_MASTER_KEY_ID = 1; static final int INDEX_MASTER_KEY_ID = 1;
static final int INDEX_USER_ID = 2; static final int INDEX_USER_ID = 2;
static final int INDEX_IS_REVOKED = 3; static final int INDEX_IS_REVOKED = 3;
static final int INDEX_EXPIRY = 4; static final int INDEX_IS_EXPIRED = 4;
static final int INDEX_VERIFIED = 5; static final int INDEX_VERIFIED = 5;
static final int INDEX_HAS_ANY_SECRET = 6; static final int INDEX_HAS_ANY_SECRET = 6;
static final int INDEX_FINGERPRINT = 7; static final int INDEX_FINGERPRINT = 7;

View File

@ -115,12 +115,12 @@ public class ViewKeyTrustFragment extends LoaderFragment implements
} }
static final String[] TRUST_PROJECTION = new String[]{ static final String[] TRUST_PROJECTION = new String[]{
KeyRings._ID, KeyRings.FINGERPRINT, KeyRings.IS_REVOKED, KeyRings.EXPIRY, KeyRings._ID, KeyRings.FINGERPRINT, KeyRings.IS_REVOKED, KeyRings.IS_EXPIRED,
KeyRings.HAS_ANY_SECRET, KeyRings.VERIFIED KeyRings.HAS_ANY_SECRET, KeyRings.VERIFIED
}; };
static final int INDEX_TRUST_FINGERPRINT = 1; static final int INDEX_TRUST_FINGERPRINT = 1;
static final int INDEX_TRUST_IS_REVOKED = 2; static final int INDEX_TRUST_IS_REVOKED = 2;
static final int INDEX_TRUST_EXPIRY = 3; static final int INDEX_TRUST_IS_EXPIRED = 3;
static final int INDEX_UNIFIED_HAS_ANY_SECRET = 4; static final int INDEX_UNIFIED_HAS_ANY_SECRET = 4;
static final int INDEX_VERIFIED = 5; static final int INDEX_VERIFIED = 5;
@ -169,8 +169,7 @@ public class ViewKeyTrustFragment extends LoaderFragment implements
nothingSpecial = false; nothingSpecial = false;
} else { } else {
Date expiryDate = new Date(data.getLong(INDEX_TRUST_EXPIRY) * 1000); if (data.getInt(INDEX_TRUST_IS_EXPIRED) != 0) {
if (!data.isNull(INDEX_TRUST_EXPIRY) && expiryDate.before(new Date())) {
// if expired, dont trust it! // if expired, dont trust it!
message.append(getString(R.string.key_trust_expired)). message.append(getString(R.string.key_trust_expired)).

View File

@ -90,7 +90,7 @@ public class EncryptKeyCompletionView extends TokenCompleteTextView {
} }
private void setImageByKey(ImageView view, EncryptionKey key) { private void setImageByKey(ImageView view, EncryptionKey key) {
Bitmap photo = ContactHelper.photoFromFingerprint(getContext().getContentResolver(), key.getFingerprint()); Bitmap photo = ContactHelper.photoFromMasterKeyId(getContext().getContentResolver(), key.getKeyId());
if (photo != null) { if (photo != null) {
view.setImageBitmap(photo); view.setImageBitmap(photo);

View File

@ -20,18 +20,15 @@ package org.sufficientlysecure.keychain.util;
import android.accounts.Account; import android.accounts.Account;
import android.accounts.AccountManager; import android.accounts.AccountManager;
import android.annotation.TargetApi; import android.annotation.TargetApi;
import android.content.ContentProviderClient;
import android.content.ContentProviderOperation; import android.content.ContentProviderOperation;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.ContentUris; import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context; import android.content.Context;
import android.database.Cursor; import android.database.Cursor;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.RemoteException;
import android.provider.ContactsContract; import android.provider.ContactsContract;
import android.util.Patterns; import android.util.Patterns;
@ -44,7 +41,6 @@ import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
@ -54,39 +50,17 @@ import java.util.Set;
public class ContactHelper { public class ContactHelper {
public static final String[] KEYS_TO_CONTACT_PROJECTION = new String[]{ public static final String[] KEYS_TO_CONTACT_PROJECTION = new String[]{
KeychainContract.KeyRings.USER_ID,
KeychainContract.KeyRings.FINGERPRINT,
KeychainContract.KeyRings.KEY_ID,
KeychainContract.KeyRings.MASTER_KEY_ID, KeychainContract.KeyRings.MASTER_KEY_ID,
KeychainContract.KeyRings.EXPIRY, KeychainContract.KeyRings.USER_ID,
KeychainContract.KeyRings.IS_EXPIRED,
KeychainContract.KeyRings.IS_REVOKED}; KeychainContract.KeyRings.IS_REVOKED};
public static final int INDEX_USER_ID = 0; public static final int INDEX_MASTER_KEY_ID = 0;
public static final int INDEX_FINGERPRINT = 1; public static final int INDEX_USER_ID = 1;
public static final int INDEX_KEY_ID = 2; public static final int INDEX_IS_EXPIRED = 2;
public static final int INDEX_MASTER_KEY_ID = 3; public static final int INDEX_IS_REVOKED = 3;
public static final int INDEX_EXPIRY = 4;
public static final int INDEX_IS_REVOKED = 5;
public static final String[] USER_IDS_PROJECTION = new String[]{ private static final Map<Long, Bitmap> photoCache = new HashMap<>();
UserPackets.USER_ID
};
public static final int INDEX_USER_IDS_USER_ID = 0;
public static final String NON_REVOKED_SELECTION = UserPackets.IS_REVOKED + "=0";
public static final String[] ID_PROJECTION = new String[]{ContactsContract.RawContacts._ID};
public static final String[] SOURCE_ID_PROJECTION = new String[]{ContactsContract.RawContacts.SOURCE_ID};
public static final String ACCOUNT_TYPE_AND_SOURCE_ID_SELECTION =
ContactsContract.RawContacts.ACCOUNT_TYPE + "=? AND " + ContactsContract.RawContacts.SOURCE_ID + "=?";
public static final String ACCOUNT_TYPE_SELECTION = ContactsContract.RawContacts.ACCOUNT_TYPE + "=?";
public static final String RAW_CONTACT_AND_MIMETYPE_SELECTION =
ContactsContract.Data.RAW_CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "=?";
public static final String ID_SELECTION = ContactsContract.RawContacts._ID + "=?";
private static final Map<String, Bitmap> photoCache = new HashMap<>();
public static List<String> getPossibleUserEmails(Context context) { public static List<String> getPossibleUserEmails(Context context) {
Set<String> accountMails = getAccountEmails(context); Set<String> accountMails = getAccountEmails(context);
@ -282,20 +256,22 @@ public class ContactHelper {
return null; return null;
} }
public static Bitmap photoFromFingerprint(ContentResolver contentResolver, String fingerprint) { public static Bitmap photoFromMasterKeyId(ContentResolver contentResolver, long masterKeyId) {
if (fingerprint == null) { if (masterKeyId == -1) {
return null; return null;
} }
if (!photoCache.containsKey(fingerprint)) { if (!photoCache.containsKey(masterKeyId)) {
photoCache.put(fingerprint, loadPhotoFromFingerprint(contentResolver, fingerprint)); photoCache.put(masterKeyId, loadPhotoFromFingerprint(contentResolver, masterKeyId));
} }
return photoCache.get(fingerprint); return photoCache.get(masterKeyId);
} }
private static Bitmap loadPhotoFromFingerprint(ContentResolver contentResolver, String fingerprint) { private static Bitmap loadPhotoFromFingerprint(ContentResolver contentResolver, long masterKeyId) {
if (fingerprint == null) return null; if (masterKeyId == -1) {
return null;
}
try { try {
int rawContactId = findRawContactId(contentResolver, fingerprint); long rawContactId = findRawContactId(contentResolver, masterKeyId);
if (rawContactId == -1) { if (rawContactId == -1) {
return null; return null;
} }
@ -313,11 +289,13 @@ public class ContactHelper {
} }
/** /**
* Write the current Keychain to the contact db * Write/Update the current OpenKeychain keys to the contact db
*/ */
public static void writeKeysToContacts(Context context) { public static void writeKeysToContacts(Context context) {
ContentResolver resolver = context.getContentResolver(); ContentResolver resolver = context.getContentResolver();
Set<String> deletedKeys = getRawContactFingerprints(resolver); Set<Long> deletedKeys = getRawContactMasterKeyIds(resolver);
debugDeleteRawContacts(resolver);
// ContentProviderClient client = resolver.acquireContentProviderClient(ContactsContract.AUTHORITY_URI); // ContentProviderClient client = resolver.acquireContentProviderClient(ContactsContract.AUTHORITY_URI);
// ContentValues values = new ContentValues(); // ContentValues values = new ContentValues();
@ -336,18 +314,18 @@ public class ContactHelper {
null, null, null); null, null, null);
if (cursor != null) { if (cursor != null) {
while (cursor.moveToNext()) { while (cursor.moveToNext()) {
String[] primaryUserId = KeyRing.splitUserId(cursor.getString(INDEX_USER_ID));
String fingerprint = KeyFormattingUtils.convertFingerprintToHex(cursor.getBlob(INDEX_FINGERPRINT));
deletedKeys.remove(fingerprint);
Log.d(Constants.TAG, "fingerprint: " + fingerprint);
String keyIdShort = KeyFormattingUtils.convertKeyIdToHexShort(cursor.getLong(INDEX_KEY_ID));
long masterKeyId = cursor.getLong(INDEX_MASTER_KEY_ID); long masterKeyId = cursor.getLong(INDEX_MASTER_KEY_ID);
boolean isExpired = !cursor.isNull(INDEX_EXPIRY) String[] primaryUserId = KeyRing.splitUserId(cursor.getString(INDEX_USER_ID));
&& new Date(cursor.getLong(INDEX_EXPIRY) * 1000).before(new Date()); String keyIdShort = KeyFormattingUtils.convertKeyIdToHexShort(cursor.getLong(INDEX_MASTER_KEY_ID));
boolean isExpired = cursor.getInt(INDEX_IS_EXPIRED) != 0;
boolean isRevoked = cursor.getInt(INDEX_IS_REVOKED) > 0; boolean isRevoked = cursor.getInt(INDEX_IS_REVOKED) > 0;
int rawContactId = findRawContactId(resolver, fingerprint);
Log.d(Constants.TAG, "masterKeyId: " + masterKeyId);
deletedKeys.remove(masterKeyId);
// get raw contact to this master key id
long rawContactId = findRawContactId(resolver, masterKeyId);
ArrayList<ContentProviderOperation> ops = new ArrayList<>(); ArrayList<ContentProviderOperation> ops = new ArrayList<>();
Log.d(Constants.TAG, "raw contact id: " + rawContactId); Log.d(Constants.TAG, "raw contact id: " + rawContactId);
@ -356,16 +334,19 @@ public class ContactHelper {
if (isExpired || isRevoked) { if (isExpired || isRevoked) {
Log.d(Constants.TAG, "Expired or revoked: Deleting " + rawContactId); Log.d(Constants.TAG, "Expired or revoked: Deleting " + rawContactId);
if (rawContactId != -1) { if (rawContactId != -1) {
resolver.delete(ContactsContract.RawContacts.CONTENT_URI, ID_SELECTION, resolver.delete(ContactsContract.RawContacts.CONTENT_URI,
new String[]{Integer.toString(rawContactId)}); ContactsContract.RawContacts._ID + "=?",
new String[]{
Long.toString(rawContactId)
});
} }
} else if (primaryUserId[0] != null) { } else if (primaryUserId[0] != null) {
// Create a new rawcontact with corresponding key if it does not exist yet // Create a new rawcontact with corresponding key if it does not exist yet
if (rawContactId == -1) { if (rawContactId == -1) {
Log.d(Constants.TAG, "Insert new raw contact with fingerprint " + fingerprint); Log.d(Constants.TAG, "Insert new raw contact with masterKeyId " + masterKeyId);
insertContact(ops, context, fingerprint); insertContact(ops, context, masterKeyId);
writeContactKey(ops, context, rawContactId, masterKeyId, keyIdShort); writeContactKey(ops, context, rawContactId, masterKeyId, keyIdShort);
} }
@ -383,42 +364,72 @@ public class ContactHelper {
cursor.close(); cursor.close();
} }
// Delete fingerprints that are no longer present in OK // Delete master key ids that are no longer present in OK
for (String fingerprint : deletedKeys) { for (Long masterKeyId : deletedKeys) {
resolver.delete(ContactsContract.RawContacts.CONTENT_URI, ACCOUNT_TYPE_AND_SOURCE_ID_SELECTION, Log.d(Constants.TAG, "Delete raw contact with fingerprint " + masterKeyId);
new String[]{Constants.ACCOUNT_TYPE, fingerprint}); resolver.delete(ContactsContract.RawContacts.CONTENT_URI,
ContactsContract.RawContacts.ACCOUNT_TYPE + "=? AND " + ContactsContract.RawContacts.SOURCE_ID + "=?",
new String[]{
Constants.ACCOUNT_TYPE, Long.toString(masterKeyId)
});
} }
} }
/** /**
* @return a set of all key fingerprints currently present in the contact db * Delete all raw contacts associated to OpenKeychain.
*
* TODO: Does this work?
*/ */
private static Set<String> getRawContactFingerprints(ContentResolver resolver) { private static int debugDeleteRawContacts(ContentResolver resolver) {
HashSet<String> result = new HashSet<>(); Log.d(Constants.TAG, "Deleting all raw contacts associated to OK...");
Cursor fingerprints = resolver.query(ContactsContract.RawContacts.CONTENT_URI, SOURCE_ID_PROJECTION, return resolver.delete(ContactsContract.RawContacts.CONTENT_URI,
ACCOUNT_TYPE_SELECTION, new String[]{Constants.ACCOUNT_TYPE}, null); ContactsContract.RawContacts.ACCOUNT_TYPE + "=?",
if (fingerprints != null) { new String[]{
while (fingerprints.moveToNext()) { Constants.ACCOUNT_TYPE
result.add(fingerprints.getString(0)); });
} }
fingerprints.close();
/**
* @return a set of all key master key ids currently present in the contact db
*/
private static Set<Long> getRawContactMasterKeyIds(ContentResolver resolver) {
HashSet<Long> result = new HashSet<>();
Cursor masterKeyIds = resolver.query(ContactsContract.RawContacts.CONTENT_URI,
new String[]{
ContactsContract.RawContacts.SOURCE_ID
},
ContactsContract.RawContacts.ACCOUNT_TYPE + "=?",
new String[]{
Constants.ACCOUNT_TYPE
}, null);
if (masterKeyIds != null) {
while (masterKeyIds.moveToNext()) {
result.add(masterKeyIds.getLong(0));
}
masterKeyIds.close();
} }
return result; return result;
} }
/** /**
* This will search the contact db for a raw contact with a given fingerprint * This will search the contact db for a raw contact with a given master key id
* *
* @return raw contact id or -1 if not found * @return raw contact id or -1 if not found
*/ */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN) @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private static int findRawContactId(ContentResolver resolver, String fingerprint) { private static long findRawContactId(ContentResolver resolver, long masterKeyId) {
int rawContactId = -1; long rawContactId = -1;
Cursor raw = resolver.query(ContactsContract.RawContacts.CONTENT_URI, ID_PROJECTION, Cursor raw = resolver.query(ContactsContract.RawContacts.CONTENT_URI,
ACCOUNT_TYPE_AND_SOURCE_ID_SELECTION, new String[]{Constants.ACCOUNT_TYPE, fingerprint}, null, null); new String[]{
ContactsContract.RawContacts._ID
},
ContactsContract.RawContacts.ACCOUNT_TYPE + "=? AND " + ContactsContract.RawContacts.SOURCE_ID + "=?",
new String[]{
Constants.ACCOUNT_TYPE, Long.toString(masterKeyId)
}, null, null);
if (raw != null) { if (raw != null) {
if (raw.moveToNext()) { if (raw.moveToNext()) {
rawContactId = raw.getInt(0); rawContactId = raw.getLong(0);
} }
raw.close(); raw.close();
} }
@ -428,11 +439,11 @@ public class ContactHelper {
/** /**
* Creates a empty raw contact with a given fingerprint * Creates a empty raw contact with a given fingerprint
*/ */
private static void insertContact(ArrayList<ContentProviderOperation> ops, Context context, String fingerprint) { private static void insertContact(ArrayList<ContentProviderOperation> ops, Context context, long masterKeyId) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI) ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, Constants.ACCOUNT_NAME) .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, Constants.ACCOUNT_NAME)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, Constants.ACCOUNT_TYPE) .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, Constants.ACCOUNT_TYPE)
.withValue(ContactsContract.RawContacts.SOURCE_ID, fingerprint) .withValue(ContactsContract.RawContacts.SOURCE_ID, Long.toString(masterKeyId))
.build()); .build());
} }
@ -441,7 +452,7 @@ public class ContactHelper {
* <p/> * <p/>
* This creates the link to OK in contact details * This creates the link to OK in contact details
*/ */
private static void writeContactKey(ArrayList<ContentProviderOperation> ops, Context context, int rawContactId, private static void writeContactKey(ArrayList<ContentProviderOperation> ops, Context context, long rawContactId,
long masterKeyId, String keyIdShort) { long masterKeyId, String keyIdShort) {
ops.add(referenceRawContact(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI), rawContactId) ops.add(referenceRawContact(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI), rawContactId)
.withValue(ContactsContract.Data.MIMETYPE, Constants.CUSTOM_CONTACT_DATA_MIME_TYPE) .withValue(ContactsContract.Data.MIMETYPE, Constants.CUSTOM_CONTACT_DATA_MIME_TYPE)
@ -454,11 +465,15 @@ public class ContactHelper {
* Write all known email addresses of a key (derived from user ids) to a given raw contact * Write all known email addresses of a key (derived from user ids) to a given raw contact
*/ */
private static void writeContactEmail(ArrayList<ContentProviderOperation> ops, ContentResolver resolver, private static void writeContactEmail(ArrayList<ContentProviderOperation> ops, ContentResolver resolver,
int rawContactId, long masterKeyId) { long rawContactId, long masterKeyId) {
ops.add(selectByRawContactAndItemType(ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI), ops.add(selectByRawContactAndItemType(ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI),
rawContactId, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE).build()); rawContactId, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE).build());
Cursor ids = resolver.query(UserPackets.buildUserIdsUri(masterKeyId), Cursor ids = resolver.query(UserPackets.buildUserIdsUri(masterKeyId),
USER_IDS_PROJECTION, NON_REVOKED_SELECTION, null, null); new String[]{
UserPackets.USER_ID
},
UserPackets.IS_REVOKED + "=0",
null, null);
if (ids != null) { if (ids != null) {
while (ids.moveToNext()) { while (ids.moveToNext()) {
String[] userId = KeyRing.splitUserId(ids.getString(0)); String[] userId = KeyRing.splitUserId(ids.getString(0));
@ -475,7 +490,7 @@ public class ContactHelper {
} }
} }
private static void writeContactDisplayName(ArrayList<ContentProviderOperation> ops, int rawContactId, private static void writeContactDisplayName(ArrayList<ContentProviderOperation> ops, long rawContactId,
String displayName) { String displayName) {
if (displayName != null) { if (displayName != null) {
ops.add(insertOrUpdateForRawContact(ContactsContract.Data.CONTENT_URI, rawContactId, ops.add(insertOrUpdateForRawContact(ContactsContract.Data.CONTENT_URI, rawContactId,
@ -486,13 +501,13 @@ public class ContactHelper {
} }
private static ContentProviderOperation.Builder referenceRawContact(ContentProviderOperation.Builder builder, private static ContentProviderOperation.Builder referenceRawContact(ContentProviderOperation.Builder builder,
int rawContactId) { long rawContactId) {
return rawContactId == -1 ? return rawContactId == -1 ?
builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) : builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) :
builder.withValue(ContactsContract.Data.RAW_CONTACT_ID, rawContactId); builder.withValue(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);
} }
private static ContentProviderOperation.Builder insertOrUpdateForRawContact(Uri uri, int rawContactId, private static ContentProviderOperation.Builder insertOrUpdateForRawContact(Uri uri, long rawContactId,
String itemType) { String itemType) {
if (rawContactId == -1) { if (rawContactId == -1) {
return referenceRawContact(ContentProviderOperation.newInsert(uri), rawContactId).withValue( return referenceRawContact(ContentProviderOperation.newInsert(uri), rawContactId).withValue(
@ -503,8 +518,11 @@ public class ContactHelper {
} }
private static ContentProviderOperation.Builder selectByRawContactAndItemType( private static ContentProviderOperation.Builder selectByRawContactAndItemType(
ContentProviderOperation.Builder builder, int rawContactId, String itemType) { ContentProviderOperation.Builder builder, long rawContactId, String itemType) {
return builder.withSelection(RAW_CONTACT_AND_MIMETYPE_SELECTION, return builder.withSelection(
new String[]{Integer.toString(rawContactId), itemType}); ContactsContract.Data.RAW_CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "=?",
new String[]{
Long.toString(rawContactId), itemType
});
} }
} }