use KeyRepository in ContactHelper

This commit is contained in:
Vincent Breitmoser
2018-06-27 14:51:33 +02:00
parent 4ac8c275f3
commit 353c75e49c

View File

@@ -20,11 +20,9 @@ package org.sufficientlysecure.keychain.util;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@@ -49,14 +47,12 @@ import android.util.Patterns;
import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.model.SubKey.UnifiedKeyInfo;
import org.sufficientlysecure.keychain.model.UserPacket.UserId; import org.sufficientlysecure.keychain.model.UserPacket.UserId;
import org.sufficientlysecure.keychain.provider.KeyRepository; import org.sufficientlysecure.keychain.provider.KeyRepository;
import org.sufficientlysecure.keychain.provider.KeychainContract;
import timber.log.Timber; import timber.log.Timber;
public class ContactHelper { public class ContactHelper {
private static final Map<Long, Bitmap> photoCache = new HashMap<>();
private final KeyRepository keyRepository; private final KeyRepository keyRepository;
private Context mContext; private Context mContext;
@@ -396,16 +392,6 @@ public class ContactHelper {
return contactName; return contactName;
} }
private Bitmap getCachedPhotoByMasterKeyId(long masterKeyId) {
if (masterKeyId == -1) {
return null;
}
if (!photoCache.containsKey(masterKeyId)) {
photoCache.put(masterKeyId, loadPhotoByMasterKeyId(masterKeyId, false));
}
return photoCache.get(masterKeyId);
}
public Bitmap loadPhotoByMasterKeyId(long masterKeyId, boolean highRes) { public Bitmap loadPhotoByMasterKeyId(long masterKeyId, boolean highRes) {
if (!isContactsPermissionGranted()) { if (!isContactsPermissionGranted()) {
return null; return null;
@@ -449,29 +435,6 @@ public class ContactHelper {
return BitmapFactory.decodeStream(photoInputStream); return BitmapFactory.decodeStream(photoInputStream);
} }
public static final String[] KEYS_TO_CONTACT_PROJECTION = new String[]{
KeychainContract.KeyRings.MASTER_KEY_ID,
KeychainContract.KeyRings.USER_ID,
KeychainContract.KeyRings.IS_EXPIRED,
KeychainContract.KeyRings.IS_REVOKED,
KeychainContract.KeyRings.VERIFIED,
KeychainContract.KeyRings.HAS_SECRET,
KeychainContract.KeyRings.HAS_ANY_SECRET,
KeychainContract.KeyRings.NAME,
KeychainContract.KeyRings.EMAIL,
KeychainContract.KeyRings.COMMENT };
public static final int INDEX_MASTER_KEY_ID = 0;
public static final int INDEX_USER_ID = 1;
public static final int INDEX_IS_EXPIRED = 2;
public static final int INDEX_IS_REVOKED = 3;
public static final int INDEX_VERIFIED = 4;
public static final int INDEX_HAS_SECRET = 5;
public static final int INDEX_HAS_ANY_SECRET = 6;
public static final int INDEX_NAME = 7;
public static final int INDEX_EMAIL = 8;
public static final int INDEX_COMMENT = 9;
/** /**
* Write/Update the current OpenKeychain keys to the contact db * Write/Update the current OpenKeychain keys to the contact db
*/ */
@@ -506,35 +469,28 @@ public class ContactHelper {
Set<Long> deletedKeys = getRawContactMasterKeyIds(); Set<Long> deletedKeys = getRawContactMasterKeyIds();
// Load all public Keys from OK // Load all public Keys from OK
// TODO: figure out why using selectionArgs does not work in this case for (UnifiedKeyInfo keyInfo : keyRepository.getAllUnifiedKeyInfo()) {
Cursor cursor = mContentResolver.query(KeychainContract.KeyRings.buildUnifiedKeyRingsUri(), if (keyInfo.has_any_secret()) {
KEYS_TO_CONTACT_PROJECTION, continue;
KeychainContract.KeyRings.HAS_ANY_SECRET + "=0", }
null, null);
if (cursor != null) { long masterKeyId = keyInfo.master_key_id();
while (cursor.moveToNext()) { String name = keyInfo.name();
long masterKeyId = cursor.getLong(INDEX_MASTER_KEY_ID);
String name = cursor.getString(INDEX_NAME);
boolean isExpired = cursor.getInt(INDEX_IS_EXPIRED) != 0;
boolean isRevoked = cursor.getInt(INDEX_IS_REVOKED) > 0;
boolean isVerified = cursor.getInt(INDEX_VERIFIED) > 0;
Timber.d("masterKeyId: " + masterKeyId); deletedKeys.remove(masterKeyId);
deletedKeys.remove(masterKeyId); ArrayList<ContentProviderOperation> ops = new ArrayList<>();
ArrayList<ContentProviderOperation> ops = new ArrayList<>(); // Do not store expired or revoked or unverified keys in contact db - and
// remove them if they already exist. Secret keys do not reach this point
// Do not store expired or revoked or unverified keys in contact db - and if (keyInfo.is_expired() || keyInfo.is_revoked() || !keyInfo.is_verified()) {
// remove them if they already exist. Secret keys do not reach this point Timber.d("Expired or revoked or unverified: Deleting masterKeyId "
if (isExpired || isRevoked || !isVerified) { + masterKeyId);
Timber.d("Expired or revoked or unverified: Deleting masterKeyId " if (masterKeyId != -1) {
+ masterKeyId); deleteRawContactByMasterKeyId(masterKeyId);
if (masterKeyId != -1) { }
deleteRawContactByMasterKeyId(masterKeyId); } else {
} if (name != null) {
} else if (name != null) {
// get raw contact to this master key id // get raw contact to this master key id
long rawContactId = findRawContactId(masterKeyId); long rawContactId = findRawContactId(masterKeyId);
@@ -559,7 +515,6 @@ public class ContactHelper {
} }
} }
} }
cursor.close();
} }
// Delete master key ids that are no longer present in OK // Delete master key ids that are no longer present in OK
@@ -581,40 +536,29 @@ public class ContactHelper {
// get all keys which have associated secret keys // get all keys which have associated secret keys
// TODO: figure out why using selectionArgs does not work in this case // TODO: figure out why using selectionArgs does not work in this case
Cursor cursor = mContentResolver.query(KeychainContract.KeyRings.buildUnifiedKeyRingsUri(), for (UnifiedKeyInfo keyInfo : keyRepository.getAllUnifiedKeyInfoWithSecret()) {
KEYS_TO_CONTACT_PROJECTION, long masterKeyId = keyInfo.master_key_id();
KeychainContract.KeyRings.HAS_ANY_SECRET + "!=0",
null, null);
if (cursor != null) try {
while (cursor.moveToNext()) {
long masterKeyId = cursor.getLong(INDEX_MASTER_KEY_ID);
boolean isExpired = cursor.getInt(INDEX_IS_EXPIRED) != 0;
boolean isRevoked = cursor.getInt(INDEX_IS_REVOKED) > 0;
String name = cursor.getString(INDEX_NAME);
if (!isExpired && !isRevoked && name != null) { if (!keyInfo.is_expired() && !keyInfo.is_revoked() && keyInfo.name() != null) {
// if expired or revoked will not be removed from keysToDelete or inserted // if expired or revoked will not be removed from keysToDelete or inserted
// into main profile ("me" contact) // into main profile ("me" contact)
boolean existsInMainProfile = keysToDelete.remove(masterKeyId); boolean existsInMainProfile = keysToDelete.remove(masterKeyId);
if (!existsInMainProfile) { if (!existsInMainProfile) {
long rawContactId = -1;//new raw contact long rawContactId = -1;//new raw contact
Timber.d("masterKeyId with secret " + masterKeyId); Timber.d("masterKeyId with secret " + masterKeyId);
ArrayList<ContentProviderOperation> ops = new ArrayList<>(); ArrayList<ContentProviderOperation> ops = new ArrayList<>();
insertMainProfileRawContact(ops, masterKeyId); insertMainProfileRawContact(ops, masterKeyId);
writeContactKey(ops, rawContactId, masterKeyId, name); writeContactKey(ops, rawContactId, masterKeyId, keyInfo.name());
try { try {
mContentResolver.applyBatch(ContactsContract.AUTHORITY, ops); mContentResolver.applyBatch(ContactsContract.AUTHORITY, ops);
} catch (Exception e) { } catch (Exception e) {
Timber.w(e); Timber.w(e);
}
} }
} }
} }
} finally {
cursor.close();
} }
for (long masterKeyId : keysToDelete) { for (long masterKeyId : keysToDelete) {