Merge pull request #1270 from adithyaphilip/linked-contact-crash

Fixed sync on key import, crash for 'me' linked contact
This commit is contained in:
Dominik Schürmann 2015-05-09 18:17:37 +02:00
commit b1ccfc6276
3 changed files with 52 additions and 1 deletions

View File

@ -376,6 +376,8 @@ public class ImportExportOperation extends BaseOperation {
log.add(LogType.MSG_IMPORT_ERROR, 1);
}
ContactSyncAdapterService.requestSync();
return new ImportKeyResult(resultType, log, newKeys, updatedKeys, badKeys, secret,
importedMasterKeyIdsArray);
}

View File

@ -133,6 +133,8 @@ public class ViewKeyFragment extends LoaderFragment implements
* @param contactId
*/
private void loadLinkedSystemContact(final long contactId) {
// contact doesn't exist, stop
if(contactId == -1) return;
final Context context = mSystemContactName.getContext();
final ContentResolver resolver = context.getContentResolver();

View File

@ -446,6 +446,13 @@ public class ContactHelper {
writeKeysToMainProfileContact(context, resolver);
writeKeysToNormalContacts(context, resolver);
}
private static void writeKeysToNormalContacts(Context context, ContentResolver resolver) {
// delete raw contacts flagged for deletion by user so they can be reinserted
deleteFlaggedNormalRawContacts(resolver);
Set<Long> deletedKeys = getRawContactMasterKeyIds(resolver);
// Load all public Keys from OK
@ -519,6 +526,9 @@ public class ContactHelper {
* @param context
*/
public static void writeKeysToMainProfileContact(Context context, ContentResolver resolver) {
// deletes contacts hidden by the user so they can be reinserted if necessary
deleteFlaggedMainProfileRawContacts(resolver);
Set<Long> keysToDelete = getMainProfileMasterKeyIds(resolver);
// get all keys which have associated secret keys
@ -585,7 +595,7 @@ public class ContactHelper {
*
* @param resolver
* @param masterKeyId
* @return
* @return number of rows deleted
*/
private static int deleteMainProfileRawContactByMasterKeyId(ContentResolver resolver,
long masterKeyId) {
@ -602,6 +612,28 @@ public class ContactHelper {
});
}
/**
* deletes all raw contact entries in the "me" contact flagged for deletion ('hidden'),
* presumably by the user
*
* @param resolver
* @return number of raw contacts deleted
*/
private static int deleteFlaggedMainProfileRawContacts(ContentResolver resolver) {
// CALLER_IS_SYNCADAPTER allows us to actually wipe the RawContact from the device, otherwise
// would be just flagged for deletion
Uri deleteUri = ContactsContract.Profile.CONTENT_RAW_CONTACTS_URI.buildUpon().
appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true").build();
return resolver.delete(deleteUri,
ContactsContract.RawContacts.ACCOUNT_TYPE + "=? AND " +
ContactsContract.RawContacts.DELETED + "=?",
new String[]{
Constants.ACCOUNT_TYPE,
"1"
});
}
/**
* Delete all raw contacts associated to OpenKeychain, including those from "me" contact
* defined by ContactsContract.Profile
@ -677,6 +709,21 @@ public class ContactHelper {
});
}
private static int deleteFlaggedNormalRawContacts(ContentResolver resolver) {
// CALLER_IS_SYNCADAPTER allows us to actually wipe the RawContact from the device, otherwise
// would be just flagged for deletion
Uri deleteUri = ContactsContract.RawContacts.CONTENT_URI.buildUpon().
appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true").build();
return resolver.delete(deleteUri,
ContactsContract.RawContacts.ACCOUNT_TYPE + "=? AND " +
ContactsContract.RawContacts.DELETED + "=?",
new String[]{
Constants.ACCOUNT_TYPE,
"1"
});
}
/**
* @return a set of all key master key ids currently present in the contact db
*/