Created
December 2, 2009 16:26
-
-
Save hammerdr/247327 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private int deleteInternal(Uri url, String userWhere, String[] whereArgs) { | |
String tableToChange; | |
// In some cases a given url requires that we delete rows from more than one | |
// table. The motivating example is deleting messages from both the on disk | |
// and in memory messages tables. | |
String tableToChange2 = null; | |
String idColumnName = null; | |
String changedItemId = null; | |
String provider = null; | |
String accountStr = null; | |
long account = 0; | |
String contact = null; | |
long threadId = 0; | |
StringBuilder whereClause = new StringBuilder(); | |
if(userWhere != null) { | |
whereClause.append(userWhere); | |
} | |
boolean notifyMessagesContentUri = false; | |
boolean notifyMessagesByContactContentUri = false; | |
boolean notifyMessagesByThreadIdContentUri = false; | |
boolean notifyContactListContentUri = false; | |
boolean notifyProviderAccountContentUri = false; | |
int match = mUrlMatcher.match(url); | |
boolean contactDeleted = false; | |
long deletedContactId = 0; | |
boolean backfillQuickSwitchSlots = false; | |
final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); | |
switch (match) { | |
case MATCH_PROVIDERS: | |
tableToChange = TABLE_PROVIDERS; | |
notifyProviderAccountContentUri = true; | |
break; | |
case MATCH_ACCOUNTS_BY_ID: | |
changedItemId = url.getPathSegments().get(1); | |
// fall through | |
case MATCH_ACCOUNTS: | |
tableToChange = TABLE_ACCOUNTS; | |
notifyProviderAccountContentUri = true; | |
break; | |
case MATCH_ACCOUNT_STATUS: | |
changedItemId = url.getPathSegments().get(1); | |
// fall through | |
case MATCH_ACCOUNTS_STATUS: | |
tableToChange = TABLE_ACCOUNT_STATUS; | |
notifyProviderAccountContentUri = true; | |
break; | |
case MATCH_CONTACTS: | |
case MATCH_CONTACTS_BAREBONE: | |
tableToChange = TABLE_CONTACTS; | |
contactDeleted = true; | |
break; | |
case MATCH_CONTACT: | |
tableToChange = TABLE_CONTACTS; | |
changedItemId = url.getPathSegments().get(1); | |
try { | |
deletedContactId = Long.parseLong(changedItemId); | |
} catch (NumberFormatException ex) { | |
throw new IllegalArgumentException(); | |
} | |
contactDeleted = true; | |
break; | |
case MATCH_CONTACTS_BY_PROVIDER: | |
tableToChange = TABLE_CONTACTS; | |
appendWhere(whereClause, Imps.Contacts.ACCOUNT, "=", url.getPathSegments().get(2)); | |
contactDeleted = true; | |
break; | |
case MATCH_CONTACTLISTS_BY_PROVIDER: | |
appendWhere(whereClause, Imps.ContactList.ACCOUNT, "=", | |
url.getPathSegments().get(2)); | |
// fall through | |
case MATCH_CONTACTLISTS: | |
tableToChange = TABLE_CONTACT_LIST; | |
notifyContactListContentUri = true; | |
break; | |
case MATCH_CONTACTLIST: | |
tableToChange = TABLE_CONTACT_LIST; | |
changedItemId = url.getPathSegments().get(1); | |
break; | |
case MATCH_BLOCKEDLIST: | |
tableToChange = TABLE_BLOCKED_LIST; | |
break; | |
case MATCH_BLOCKEDLIST_BY_PROVIDER: | |
tableToChange = TABLE_BLOCKED_LIST; | |
appendWhere(whereClause, Imps.BlockedList.ACCOUNT, "=", url.getPathSegments().get(2)); | |
break; | |
case MATCH_CONTACTS_ETAGS: | |
tableToChange = TABLE_CONTACTS_ETAG; | |
break; | |
case MATCH_CONTACTS_ETAG: | |
tableToChange = TABLE_CONTACTS_ETAG; | |
changedItemId = url.getPathSegments().get(1); | |
break; | |
case MATCH_MESSAGES: | |
tableToChange = TABLE_MESSAGES; | |
break; | |
case MATCH_MESSAGES_BY_CONTACT: | |
tableToChange = TABLE_MESSAGES; | |
tableToChange2 = TABLE_IN_MEMORY_MESSAGES; | |
accountStr = decodeURLSegment(url.getPathSegments().get(1)); | |
try { | |
account = Long.parseLong(accountStr); | |
} catch (NumberFormatException ex) { | |
throw new IllegalArgumentException(); | |
} | |
contact = decodeURLSegment(url.getPathSegments().get(2)); | |
appendWhere(whereClause, Imps.Messages.THREAD_ID, "=", | |
getContactId(db, accountStr, contact)); | |
notifyMessagesContentUri = true; | |
break; | |
case MATCH_MESSAGES_BY_THREAD_ID: | |
tableToChange = TABLE_MESSAGES; | |
tableToChange2 = TABLE_IN_MEMORY_MESSAGES; | |
try { | |
threadId = Long.parseLong(decodeURLSegment(url.getPathSegments().get(1))); | |
} catch (NumberFormatException ex) { | |
throw new IllegalArgumentException(); | |
} | |
appendWhere(whereClause, Imps.Messages.THREAD_ID, "=", threadId); | |
notifyMessagesContentUri = true; | |
break; | |
case MATCH_MESSAGES_BY_PROVIDER: | |
tableToChange = TABLE_MESSAGES; | |
provider = decodeURLSegment(url.getPathSegments().get(1)); | |
appendWhere(whereClause, buildContactIdSelection(Imps.Messages.THREAD_ID, | |
Imps.Contacts.PROVIDER + "='" + provider + "'")); | |
notifyMessagesContentUri = true; | |
break; | |
case MATCH_MESSAGES_BY_ACCOUNT: | |
tableToChange = TABLE_MESSAGES; | |
accountStr = decodeURLSegment(url.getPathSegments().get(1)); | |
appendWhere(whereClause, buildContactIdSelection(Imps.Messages.THREAD_ID, | |
Imps.Contacts.ACCOUNT + "='" + accountStr + "'")); | |
notifyMessagesContentUri = true; | |
break; | |
case MATCH_MESSAGE: | |
tableToChange = TABLE_MESSAGES; | |
changedItemId = url.getPathSegments().get(1); | |
notifyMessagesContentUri = true; | |
break; | |
case MATCH_OTR_MESSAGES: | |
tableToChange = TABLE_IN_MEMORY_MESSAGES; | |
break; | |
case MATCH_OTR_MESSAGES_BY_CONTACT: | |
tableToChange = TABLE_IN_MEMORY_MESSAGES; | |
accountStr = decodeURLSegment(url.getPathSegments().get(1)); | |
try { | |
account = Long.parseLong(accountStr); | |
} catch (NumberFormatException ex) { | |
throw new IllegalArgumentException(); | |
} | |
contact = decodeURLSegment(url.getPathSegments().get(2)); | |
appendWhere(whereClause, Imps.Messages.THREAD_ID, "=", | |
getContactId(db, accountStr, contact)); | |
notifyMessagesByContactContentUri = true; | |
break; | |
case MATCH_OTR_MESSAGES_BY_THREAD_ID: | |
tableToChange = TABLE_IN_MEMORY_MESSAGES; | |
try { | |
threadId = Long.parseLong(decodeURLSegment(url.getPathSegments().get(1))); | |
} catch (NumberFormatException ex) { | |
throw new IllegalArgumentException(); | |
} | |
appendWhere(whereClause, Imps.Messages.THREAD_ID, "=", threadId); | |
notifyMessagesByThreadIdContentUri = true; | |
break; | |
case MATCH_OTR_MESSAGES_BY_PROVIDER: | |
tableToChange = TABLE_IN_MEMORY_MESSAGES; | |
provider = decodeURLSegment(url.getPathSegments().get(1)); | |
appendWhere(whereClause, buildContactIdSelection(Imps.Messages.THREAD_ID, | |
Imps.Contacts.PROVIDER + "='" + provider + "'")); | |
if (DBG) log("delete (MATCH_OTR_MESSAGES_BY_PROVIDER) sel => " + whereClause); | |
notifyMessagesContentUri = true; | |
break; | |
case MATCH_OTR_MESSAGES_BY_ACCOUNT: | |
tableToChange = TABLE_IN_MEMORY_MESSAGES; | |
accountStr = decodeURLSegment(url.getPathSegments().get(1)); | |
appendWhere(whereClause, buildContactIdSelection(Imps.Messages.THREAD_ID, | |
Imps.Contacts.ACCOUNT + "='" + accountStr + "'")); | |
if (DBG) log("delete (MATCH_OTR_MESSAGES_BY_ACCOUNT) sel => " + whereClause); | |
notifyMessagesContentUri = true; | |
break; | |
case MATCH_OTR_MESSAGE: | |
tableToChange = TABLE_IN_MEMORY_MESSAGES; | |
changedItemId = url.getPathSegments().get(1); | |
notifyMessagesContentUri = true; | |
break; | |
case MATCH_GROUP_MEMBERS: | |
tableToChange = TABLE_GROUP_MEMBERS; | |
break; | |
case MATCH_GROUP_MEMBERS_BY_GROUP: | |
tableToChange = TABLE_GROUP_MEMBERS; | |
appendWhere(whereClause, Imps.GroupMembers.GROUP, "=", url.getPathSegments().get(1)); | |
break; | |
case MATCH_INVITATIONS: | |
tableToChange = TABLE_INVITATIONS; | |
break; | |
case MATCH_INVITATION: | |
tableToChange = TABLE_INVITATIONS; | |
changedItemId = url.getPathSegments().get(1); | |
break; | |
case MATCH_AVATARS: | |
tableToChange = TABLE_AVATARS; | |
break; | |
case MATCH_AVATAR: | |
tableToChange = TABLE_AVATARS; | |
changedItemId = url.getPathSegments().get(1); | |
break; | |
case MATCH_AVATAR_BY_PROVIDER: | |
tableToChange = TABLE_AVATARS; | |
changedItemId = url.getPathSegments().get(2); | |
idColumnName = Imps.Avatars.ACCOUNT; | |
break; | |
case MATCH_CHATS: | |
tableToChange = TABLE_CHATS; | |
backfillQuickSwitchSlots = true; | |
break; | |
case MATCH_CHATS_BY_ACCOUNT: | |
tableToChange = TABLE_CHATS; | |
accountStr = decodeURLSegment(url.getLastPathSegment()); | |
appendWhere(whereClause, buildContactIdSelection(Imps.Chats.CONTACT_ID, | |
Imps.Contacts.ACCOUNT + "='" + accountStr + "'")); | |
if (DBG) log("delete (MATCH_CHATS_BY_ACCOUNT) sel => " + whereClause); | |
changedItemId = null; | |
break; | |
case MATCH_CHATS_ID: | |
tableToChange = TABLE_CHATS; | |
changedItemId = url.getPathSegments().get(1); | |
idColumnName = Imps.Chats.CONTACT_ID; | |
break; | |
case MATCH_PRESENCE: | |
tableToChange = TABLE_PRESENCE; | |
break; | |
case MATCH_PRESENCE_ID: | |
tableToChange = TABLE_PRESENCE; | |
changedItemId = url.getPathSegments().get(1); | |
idColumnName = Imps.Presence.CONTACT_ID; | |
break; | |
case MATCH_PRESENCE_BY_ACCOUNT: | |
tableToChange = TABLE_PRESENCE; | |
accountStr = decodeURLSegment(url.getLastPathSegment()); | |
appendWhere(whereClause, buildContactIdSelection(Imps.Presence.CONTACT_ID, | |
Imps.Contacts.ACCOUNT + "='" + accountStr + "'")); | |
if (DBG) log("delete (MATCH_PRESENCE_BY_ACCOUNT): sel => " + whereClause); | |
changedItemId = null; | |
break; | |
case MATCH_SESSIONS: | |
tableToChange = TABLE_SESSION_COOKIES; | |
break; | |
case MATCH_SESSIONS_BY_PROVIDER: | |
tableToChange = TABLE_SESSION_COOKIES; | |
changedItemId = url.getPathSegments().get(2); | |
idColumnName = Imps.SessionCookies.ACCOUNT; | |
break; | |
case MATCH_PROVIDER_SETTINGS_BY_ID: | |
tableToChange = TABLE_PROVIDER_SETTINGS; | |
changedItemId = url.getPathSegments().get(1); | |
idColumnName = Imps.ProviderSettings.PROVIDER; | |
break; | |
case MATCH_PROVIDER_SETTINGS_BY_ID_AND_NAME: | |
tableToChange = TABLE_PROVIDER_SETTINGS; | |
String providerId = url.getPathSegments().get(1); | |
String name = url.getPathSegments().get(2); | |
appendWhere(whereClause, Imps.ProviderSettings.PROVIDER, "=", providerId); | |
appendWhere(whereClause, Imps.ProviderSettings.NAME, "=", name); | |
break; | |
case MATCH_BRANDING_RESOURCE_MAP_CACHE: | |
tableToChange = TABLE_BRANDING_RESOURCE_MAP_CACHE; | |
break; | |
// mcs/rmq stuff | |
case MATCH_OUTGOING_RMQ_MESSAGES: | |
tableToChange = TABLE_OUTGOING_RMQ_MESSAGES; | |
break; | |
case MATCH_LAST_RMQ_ID: | |
tableToChange = TABLE_LAST_RMQ_ID; | |
break; | |
case MATCH_S2D_RMQ_IDS: | |
tableToChange = TABLE_S2D_RMQ_IDS; | |
break; | |
default: | |
throw new UnsupportedOperationException("Cannot delete that URL: " + url); | |
} | |
if (idColumnName == null) { | |
idColumnName = "_id"; | |
} | |
if (changedItemId != null) { | |
appendWhere(whereClause, idColumnName, "=", changedItemId); | |
} | |
if (DBG) log("delete from " + url + " WHERE " + whereClause); | |
int count = db.delete(tableToChange, whereClause.toString(), whereArgs); | |
// see the comment at the declaration of tableToChange2 for an explanation | |
if (tableToChange2 != null){ | |
count += db.delete(tableToChange2, whereClause.toString(), whereArgs); | |
} | |
if (contactDeleted && count > 0) { | |
// since the contact cleanup triggers no longer work for cross database tables, | |
// we have to do it by hand here. | |
performContactRemovalCleanup(deletedContactId); | |
} | |
if (count > 0) { | |
ContentResolver resolver = getContext().getContentResolver(); | |
// In most case, we query contacts with presence and chats joined, thus | |
// we should also notify that contacts changes when presence or chats changed. | |
if (match == MATCH_CHATS || match == MATCH_CHATS_ID | |
|| match == MATCH_PRESENCE || match == MATCH_PRESENCE_ID | |
|| match == MATCH_CONTACTS_BAREBONE) { | |
resolver.notifyChange(Imps.Contacts.CONTENT_URI, null); | |
} | |
if (notifyMessagesContentUri) { | |
resolver.notifyChange(Imps.Messages.CONTENT_URI, null); | |
} | |
if (notifyMessagesByContactContentUri) { | |
resolver.notifyChange(Imps.Messages.CONTENT_URI, null); | |
resolver.notifyChange(Imps.Messages.getContentUriByContact(account, contact), null); | |
} | |
if (notifyMessagesByThreadIdContentUri) { | |
resolver.notifyChange(Imps.Messages.CONTENT_URI, null); | |
resolver.notifyChange(Imps.Messages.getContentUriByThreadId(threadId), null); | |
} | |
if (notifyContactListContentUri) { | |
resolver.notifyChange(Imps.ContactList.CONTENT_URI, null); | |
} | |
if (notifyProviderAccountContentUri) { | |
if (DBG) log("notify delete for " + Imps.Provider.CONTENT_URI_WITH_ACCOUNT); | |
resolver.notifyChange(Imps.Provider.CONTENT_URI_WITH_ACCOUNT, null); | |
} | |
if (backfillQuickSwitchSlots) { | |
backfillQuickSwitchSlots(); | |
} | |
} | |
return count; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private int deleteInternal(Uri url, String userWhere, String[] whereArgs) { | |
String tableToChange; | |
// In some cases a given url requires that we delete rows from more than one | |
// table. The motivating example is deleting messages from both the on disk | |
// and in memory messages tables. | |
String tableToChange2 = null; | |
String idColumnName = null; | |
String changedItemId = null; | |
String provider = null; | |
String accountStr = null; | |
long account = 0; | |
String contact = null; | |
long threadId = 0; | |
StringBuilder whereClause = getWhereClause(userWhere); | |
boolean notifyMessagesContentUri = false; | |
boolean notifyMessagesByContactContentUri = false; | |
boolean notifyMessagesByThreadIdContentUri = false; | |
boolean notifyContactListContentUri = false; | |
boolean notifyProviderAccountContentUri = false; | |
int match = mUrlMatcher.match(url); | |
boolean contactDeleted = false; | |
long deletedContactId = 0; | |
boolean backfillQuickSwitchSlots = false; | |
final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); | |
switch (match) { | |
case MATCH_PROVIDERS: | |
tableToChange = TABLE_PROVIDERS; | |
notifyProviderAccountContentUri = true; | |
break; | |
case MATCH_ACCOUNTS_BY_ID: | |
changedItemId = url.getPathSegments().get(1); | |
// fall through | |
case MATCH_ACCOUNTS: | |
tableToChange = TABLE_ACCOUNTS; | |
notifyProviderAccountContentUri = true; | |
break; | |
case MATCH_ACCOUNT_STATUS: | |
changedItemId = url.getPathSegments().get(1); | |
// fall through | |
case MATCH_ACCOUNTS_STATUS: | |
tableToChange = TABLE_ACCOUNT_STATUS; | |
notifyProviderAccountContentUri = true; | |
break; | |
case MATCH_CONTACTS: | |
case MATCH_CONTACTS_BAREBONE: | |
tableToChange = TABLE_CONTACTS; | |
contactDeleted = true; | |
break; | |
case MATCH_CONTACT: | |
tableToChange = TABLE_CONTACTS; | |
changedItemId = url.getPathSegments().get(1); | |
try { | |
deletedContactId = Long.parseLong(changedItemId); | |
} catch (NumberFormatException ex) { | |
throw new IllegalArgumentException(); | |
} | |
contactDeleted = true; | |
break; | |
case MATCH_CONTACTS_BY_PROVIDER: | |
tableToChange = TABLE_CONTACTS; | |
appendWhere(whereClause, Imps.Contacts.ACCOUNT, "=", url.getPathSegments().get(2)); | |
contactDeleted = true; | |
break; | |
case MATCH_CONTACTLISTS_BY_PROVIDER: | |
appendWhere(whereClause, Imps.ContactList.ACCOUNT, "=", | |
url.getPathSegments().get(2)); | |
// fall through | |
case MATCH_CONTACTLISTS: | |
tableToChange = TABLE_CONTACT_LIST; | |
notifyContactListContentUri = true; | |
break; | |
case MATCH_CONTACTLIST: | |
tableToChange = TABLE_CONTACT_LIST; | |
changedItemId = url.getPathSegments().get(1); | |
break; | |
case MATCH_BLOCKEDLIST: | |
tableToChange = TABLE_BLOCKED_LIST; | |
break; | |
case MATCH_BLOCKEDLIST_BY_PROVIDER: | |
tableToChange = TABLE_BLOCKED_LIST; | |
appendWhere(whereClause, Imps.BlockedList.ACCOUNT, "=", url.getPathSegments().get(2)); | |
break; | |
case MATCH_CONTACTS_ETAGS: | |
tableToChange = TABLE_CONTACTS_ETAG; | |
break; | |
case MATCH_CONTACTS_ETAG: | |
tableToChange = TABLE_CONTACTS_ETAG; | |
changedItemId = url.getPathSegments().get(1); | |
break; | |
case MATCH_MESSAGES: | |
tableToChange = TABLE_MESSAGES; | |
break; | |
case MATCH_MESSAGES_BY_CONTACT: | |
tableToChange = TABLE_MESSAGES; | |
tableToChange2 = TABLE_IN_MEMORY_MESSAGES; | |
accountStr = decodeURLSegment(url.getPathSegments().get(1)); | |
try { | |
account = Long.parseLong(accountStr); | |
} catch (NumberFormatException ex) { | |
throw new IllegalArgumentException(); | |
} | |
contact = decodeURLSegment(url.getPathSegments().get(2)); | |
appendWhere(whereClause, Imps.Messages.THREAD_ID, "=", | |
getContactId(db, accountStr, contact)); | |
notifyMessagesContentUri = true; | |
break; | |
case MATCH_MESSAGES_BY_THREAD_ID: | |
tableToChange = TABLE_MESSAGES; | |
tableToChange2 = TABLE_IN_MEMORY_MESSAGES; | |
try { | |
threadId = Long.parseLong(decodeURLSegment(url.getPathSegments().get(1))); | |
} catch (NumberFormatException ex) { | |
throw new IllegalArgumentException(); | |
} | |
appendWhere(whereClause, Imps.Messages.THREAD_ID, "=", threadId); | |
notifyMessagesContentUri = true; | |
break; | |
case MATCH_MESSAGES_BY_PROVIDER: | |
tableToChange = TABLE_MESSAGES; | |
provider = decodeURLSegment(url.getPathSegments().get(1)); | |
appendWhere(whereClause, buildContactIdSelection(Imps.Messages.THREAD_ID, | |
Imps.Contacts.PROVIDER + "='" + provider + "'")); | |
notifyMessagesContentUri = true; | |
break; | |
case MATCH_MESSAGES_BY_ACCOUNT: | |
tableToChange = TABLE_MESSAGES; | |
accountStr = decodeURLSegment(url.getPathSegments().get(1)); | |
appendWhere(whereClause, buildContactIdSelection(Imps.Messages.THREAD_ID, | |
Imps.Contacts.ACCOUNT + "='" + accountStr + "'")); | |
notifyMessagesContentUri = true; | |
break; | |
case MATCH_MESSAGE: | |
tableToChange = TABLE_MESSAGES; | |
changedItemId = url.getPathSegments().get(1); | |
notifyMessagesContentUri = true; | |
break; | |
case MATCH_OTR_MESSAGES: | |
tableToChange = TABLE_IN_MEMORY_MESSAGES; | |
break; | |
case MATCH_OTR_MESSAGES_BY_CONTACT: | |
tableToChange = TABLE_IN_MEMORY_MESSAGES; | |
accountStr = decodeURLSegment(url.getPathSegments().get(1)); | |
try { | |
account = Long.parseLong(accountStr); | |
} catch (NumberFormatException ex) { | |
throw new IllegalArgumentException(); | |
} | |
contact = decodeURLSegment(url.getPathSegments().get(2)); | |
appendWhere(whereClause, Imps.Messages.THREAD_ID, "=", | |
getContactId(db, accountStr, contact)); | |
notifyMessagesByContactContentUri = true; | |
break; | |
case MATCH_OTR_MESSAGES_BY_THREAD_ID: | |
tableToChange = TABLE_IN_MEMORY_MESSAGES; | |
try { | |
threadId = Long.parseLong(decodeURLSegment(url.getPathSegments().get(1))); | |
} catch (NumberFormatException ex) { | |
throw new IllegalArgumentException(); | |
} | |
appendWhere(whereClause, Imps.Messages.THREAD_ID, "=", threadId); | |
notifyMessagesByThreadIdContentUri = true; | |
break; | |
case MATCH_OTR_MESSAGES_BY_PROVIDER: | |
tableToChange = TABLE_IN_MEMORY_MESSAGES; | |
provider = decodeURLSegment(url.getPathSegments().get(1)); | |
appendWhere(whereClause, buildContactIdSelection(Imps.Messages.THREAD_ID, | |
Imps.Contacts.PROVIDER + "='" + provider + "'")); | |
if (DBG) log("delete (MATCH_OTR_MESSAGES_BY_PROVIDER) sel => " + whereClause); | |
notifyMessagesContentUri = true; | |
break; | |
case MATCH_OTR_MESSAGES_BY_ACCOUNT: | |
tableToChange = TABLE_IN_MEMORY_MESSAGES; | |
accountStr = decodeURLSegment(url.getPathSegments().get(1)); | |
appendWhere(whereClause, buildContactIdSelection(Imps.Messages.THREAD_ID, | |
Imps.Contacts.ACCOUNT + "='" + accountStr + "'")); | |
if (DBG) log("delete (MATCH_OTR_MESSAGES_BY_ACCOUNT) sel => " + whereClause); | |
notifyMessagesContentUri = true; | |
break; | |
case MATCH_OTR_MESSAGE: | |
tableToChange = TABLE_IN_MEMORY_MESSAGES; | |
changedItemId = url.getPathSegments().get(1); | |
notifyMessagesContentUri = true; | |
break; | |
case MATCH_GROUP_MEMBERS: | |
tableToChange = TABLE_GROUP_MEMBERS; | |
break; | |
case MATCH_GROUP_MEMBERS_BY_GROUP: | |
tableToChange = TABLE_GROUP_MEMBERS; | |
appendWhere(whereClause, Imps.GroupMembers.GROUP, "=", url.getPathSegments().get(1)); | |
break; | |
case MATCH_INVITATIONS: | |
tableToChange = TABLE_INVITATIONS; | |
break; | |
case MATCH_INVITATION: | |
tableToChange = TABLE_INVITATIONS; | |
changedItemId = url.getPathSegments().get(1); | |
break; | |
case MATCH_AVATARS: | |
tableToChange = TABLE_AVATARS; | |
break; | |
case MATCH_AVATAR: | |
tableToChange = TABLE_AVATARS; | |
changedItemId = url.getPathSegments().get(1); | |
break; | |
case MATCH_AVATAR_BY_PROVIDER: | |
tableToChange = TABLE_AVATARS; | |
changedItemId = url.getPathSegments().get(2); | |
idColumnName = Imps.Avatars.ACCOUNT; | |
break; | |
case MATCH_CHATS: | |
tableToChange = TABLE_CHATS; | |
backfillQuickSwitchSlots = true; | |
break; | |
case MATCH_CHATS_BY_ACCOUNT: | |
tableToChange = TABLE_CHATS; | |
accountStr = decodeURLSegment(url.getLastPathSegment()); | |
appendWhere(whereClause, buildContactIdSelection(Imps.Chats.CONTACT_ID, | |
Imps.Contacts.ACCOUNT + "='" + accountStr + "'")); | |
if (DBG) log("delete (MATCH_CHATS_BY_ACCOUNT) sel => " + whereClause); | |
changedItemId = null; | |
break; | |
case MATCH_CHATS_ID: | |
tableToChange = TABLE_CHATS; | |
changedItemId = url.getPathSegments().get(1); | |
idColumnName = Imps.Chats.CONTACT_ID; | |
break; | |
case MATCH_PRESENCE: | |
tableToChange = TABLE_PRESENCE; | |
break; | |
case MATCH_PRESENCE_ID: | |
tableToChange = TABLE_PRESENCE; | |
changedItemId = url.getPathSegments().get(1); | |
idColumnName = Imps.Presence.CONTACT_ID; | |
break; | |
case MATCH_PRESENCE_BY_ACCOUNT: | |
tableToChange = TABLE_PRESENCE; | |
accountStr = decodeURLSegment(url.getLastPathSegment()); | |
appendWhere(whereClause, buildContactIdSelection(Imps.Presence.CONTACT_ID, | |
Imps.Contacts.ACCOUNT + "='" + accountStr + "'")); | |
if (DBG) log("delete (MATCH_PRESENCE_BY_ACCOUNT): sel => " + whereClause); | |
changedItemId = null; | |
break; | |
case MATCH_SESSIONS: | |
tableToChange = TABLE_SESSION_COOKIES; | |
break; | |
case MATCH_SESSIONS_BY_PROVIDER: | |
tableToChange = TABLE_SESSION_COOKIES; | |
changedItemId = url.getPathSegments().get(2); | |
idColumnName = Imps.SessionCookies.ACCOUNT; | |
break; | |
case MATCH_PROVIDER_SETTINGS_BY_ID: | |
tableToChange = TABLE_PROVIDER_SETTINGS; | |
changedItemId = url.getPathSegments().get(1); | |
idColumnName = Imps.ProviderSettings.PROVIDER; | |
break; | |
case MATCH_PROVIDER_SETTINGS_BY_ID_AND_NAME: | |
tableToChange = TABLE_PROVIDER_SETTINGS; | |
String providerId = url.getPathSegments().get(1); | |
String name = url.getPathSegments().get(2); | |
appendWhere(whereClause, Imps.ProviderSettings.PROVIDER, "=", providerId); | |
appendWhere(whereClause, Imps.ProviderSettings.NAME, "=", name); | |
break; | |
case MATCH_BRANDING_RESOURCE_MAP_CACHE: | |
tableToChange = TABLE_BRANDING_RESOURCE_MAP_CACHE; | |
break; | |
// mcs/rmq stuff | |
case MATCH_OUTGOING_RMQ_MESSAGES: | |
tableToChange = TABLE_OUTGOING_RMQ_MESSAGES; | |
break; | |
case MATCH_LAST_RMQ_ID: | |
tableToChange = TABLE_LAST_RMQ_ID; | |
break; | |
case MATCH_S2D_RMQ_IDS: | |
tableToChange = TABLE_S2D_RMQ_IDS; | |
break; | |
default: | |
throw new UnsupportedOperationException("Cannot delete that URL: " + url); | |
} | |
if (idColumnName == null) { | |
idColumnName = "_id"; | |
} | |
if (changedItemId != null) { | |
appendWhere(whereClause, idColumnName, "=", changedItemId); | |
} | |
if (DBG) log("delete from " + url + " WHERE " + whereClause); | |
int count = db.delete(tableToChange, whereClause.toString(), whereArgs); | |
// see the comment at the declaration of tableToChange2 for an explanation | |
if (tableToChange2 != null){ | |
count += db.delete(tableToChange2, whereClause.toString(), whereArgs); | |
} | |
if (contactDeleted && count > 0) { | |
// since the contact cleanup triggers no longer work for cross database tables, | |
// we have to do it by hand here. | |
performContactRemovalCleanup(deletedContactId); | |
} | |
if (count > 0) { | |
ContentResolver resolver = getContext().getContentResolver(); | |
// In most case, we query contacts with presence and chats joined, thus | |
// we should also notify that contacts changes when presence or chats changed. | |
if (match == MATCH_CHATS || match == MATCH_CHATS_ID | |
|| match == MATCH_PRESENCE || match == MATCH_PRESENCE_ID | |
|| match == MATCH_CONTACTS_BAREBONE) { | |
resolver.notifyChange(Imps.Contacts.CONTENT_URI, null); | |
} | |
if (notifyMessagesContentUri) { | |
resolver.notifyChange(Imps.Messages.CONTENT_URI, null); | |
} | |
if (notifyMessagesByContactContentUri) { | |
resolver.notifyChange(Imps.Messages.CONTENT_URI, null); | |
resolver.notifyChange(Imps.Messages.getContentUriByContact(account, contact), null); | |
} | |
if (notifyMessagesByThreadIdContentUri) { | |
resolver.notifyChange(Imps.Messages.CONTENT_URI, null); | |
resolver.notifyChange(Imps.Messages.getContentUriByThreadId(threadId), null); | |
} | |
if (notifyContactListContentUri) { | |
resolver.notifyChange(Imps.ContactList.CONTENT_URI, null); | |
} | |
if (notifyProviderAccountContentUri) { | |
if (DBG) log("notify delete for " + Imps.Provider.CONTENT_URI_WITH_ACCOUNT); | |
resolver.notifyChange(Imps.Provider.CONTENT_URI_WITH_ACCOUNT, null); | |
} | |
if (backfillQuickSwitchSlots) { | |
backfillQuickSwitchSlots(); | |
} | |
} | |
return count; | |
} | |
// This was also in two spots in updateInternal! Woot! | |
private StringBuilder getWhereClause(String userWhere) { | |
StringBuilder whereClause = new StringBuilder(); | |
if(userWhere != null) { | |
whereClause.append(userWhere); | |
} | |
return whereClause; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private int deleteInternal(Uri url, String userWhere, String[] whereArgs) { | |
String tableToChange; | |
// In some cases a given url requires that we delete rows from more than one | |
// table. The motivating example is deleting messages from both the on disk | |
// and in memory messages tables. | |
String additionalTableToChange = null; | |
String idColumnName = null; | |
String changedItemId = null; | |
String provider = null; | |
String accountStr = null; | |
long account = 0; | |
String contact = null; | |
long threadId = 0; | |
StringBuilder whereClause = getWhereClause(userWhere); | |
boolean notifyMessagesContentUri = false; | |
boolean notifyMessagesByContactContentUri = false; | |
boolean notifyMessagesByThreadIdContentUri = false; | |
boolean notifyContactListContentUri = false; | |
boolean notifyProviderAccountContentUri = false; | |
int match = mUrlMatcher.match(url); | |
boolean contactDeleted = false; | |
long deletedContactId = 0; | |
boolean backfillQuickSwitchSlots = false; | |
final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); | |
switch (match) { | |
case MATCH_PROVIDERS: | |
tableToChange = TABLE_PROVIDERS; | |
notifyProviderAccountContentUri = true; | |
break; | |
case MATCH_ACCOUNTS_BY_ID: | |
changedItemId = url.getPathSegments().get(1); | |
// fall through | |
case MATCH_ACCOUNTS: | |
tableToChange = TABLE_ACCOUNTS; | |
notifyProviderAccountContentUri = true; | |
break; | |
case MATCH_ACCOUNT_STATUS: | |
changedItemId = url.getPathSegments().get(1); | |
// fall through | |
case MATCH_ACCOUNTS_STATUS: | |
tableToChange = TABLE_ACCOUNT_STATUS; | |
notifyProviderAccountContentUri = true; | |
break; | |
case MATCH_CONTACTS: | |
case MATCH_CONTACTS_BAREBONE: | |
tableToChange = TABLE_CONTACTS; | |
contactDeleted = true; | |
break; | |
case MATCH_CONTACT: | |
tableToChange = TABLE_CONTACTS; | |
changedItemId = url.getPathSegments().get(1); | |
try { | |
deletedContactId = Long.parseLong(changedItemId); | |
} catch (NumberFormatException ex) { | |
throw new IllegalArgumentException(); | |
} | |
contactDeleted = true; | |
break; | |
case MATCH_CONTACTS_BY_PROVIDER: | |
tableToChange = TABLE_CONTACTS; | |
appendWhere(whereClause, Imps.Contacts.ACCOUNT, "=", url.getPathSegments().get(2)); | |
contactDeleted = true; | |
break; | |
case MATCH_CONTACTLISTS_BY_PROVIDER: | |
appendWhere(whereClause, Imps.ContactList.ACCOUNT, "=", | |
url.getPathSegments().get(2)); | |
// fall through | |
case MATCH_CONTACTLISTS: | |
tableToChange = TABLE_CONTACT_LIST; | |
notifyContactListContentUri = true; | |
break; | |
case MATCH_CONTACTLIST: | |
tableToChange = TABLE_CONTACT_LIST; | |
changedItemId = url.getPathSegments().get(1); | |
break; | |
case MATCH_BLOCKEDLIST: | |
tableToChange = TABLE_BLOCKED_LIST; | |
break; | |
case MATCH_BLOCKEDLIST_BY_PROVIDER: | |
tableToChange = TABLE_BLOCKED_LIST; | |
appendWhere(whereClause, Imps.BlockedList.ACCOUNT, "=", url.getPathSegments().get(2)); | |
break; | |
case MATCH_CONTACTS_ETAGS: | |
tableToChange = TABLE_CONTACTS_ETAG; | |
break; | |
case MATCH_CONTACTS_ETAG: | |
tableToChange = TABLE_CONTACTS_ETAG; | |
changedItemId = url.getPathSegments().get(1); | |
break; | |
case MATCH_MESSAGES: | |
tableToChange = TABLE_MESSAGES; | |
break; | |
case MATCH_MESSAGES_BY_CONTACT: | |
tableToChange = TABLE_MESSAGES; | |
additionalTableToChange = TABLE_IN_MEMORY_MESSAGES; | |
accountStr = decodeURLSegment(url.getPathSegments().get(1)); | |
try { | |
account = Long.parseLong(accountStr); | |
} catch (NumberFormatException ex) { | |
throw new IllegalArgumentException(); | |
} | |
contact = decodeURLSegment(url.getPathSegments().get(2)); | |
appendWhere(whereClause, Imps.Messages.THREAD_ID, "=", | |
getContactId(db, accountStr, contact)); | |
notifyMessagesContentUri = true; | |
break; | |
case MATCH_MESSAGES_BY_THREAD_ID: | |
tableToChange = TABLE_MESSAGES; | |
additionalTableToChange = TABLE_IN_MEMORY_MESSAGES; | |
try { | |
threadId = Long.parseLong(decodeURLSegment(url.getPathSegments().get(1))); | |
} catch (NumberFormatException ex) { | |
throw new IllegalArgumentException(); | |
} | |
appendWhere(whereClause, Imps.Messages.THREAD_ID, "=", threadId); | |
notifyMessagesContentUri = true; | |
break; | |
case MATCH_MESSAGES_BY_PROVIDER: | |
tableToChange = TABLE_MESSAGES; | |
provider = decodeURLSegment(url.getPathSegments().get(1)); | |
appendWhere(whereClause, buildContactIdSelection(Imps.Messages.THREAD_ID, | |
Imps.Contacts.PROVIDER + "='" + provider + "'")); | |
notifyMessagesContentUri = true; | |
break; | |
case MATCH_MESSAGES_BY_ACCOUNT: | |
tableToChange = TABLE_MESSAGES; | |
accountStr = decodeURLSegment(url.getPathSegments().get(1)); | |
appendWhere(whereClause, buildContactIdSelection(Imps.Messages.THREAD_ID, | |
Imps.Contacts.ACCOUNT + "='" + accountStr + "'")); | |
notifyMessagesContentUri = true; | |
break; | |
case MATCH_MESSAGE: | |
tableToChange = TABLE_MESSAGES; | |
changedItemId = url.getPathSegments().get(1); | |
notifyMessagesContentUri = true; | |
break; | |
case MATCH_OTR_MESSAGES: | |
tableToChange = TABLE_IN_MEMORY_MESSAGES; | |
break; | |
case MATCH_OTR_MESSAGES_BY_CONTACT: | |
tableToChange = TABLE_IN_MEMORY_MESSAGES; | |
accountStr = decodeURLSegment(url.getPathSegments().get(1)); | |
try { | |
account = Long.parseLong(accountStr); | |
} catch (NumberFormatException ex) { | |
throw new IllegalArgumentException(); | |
} | |
contact = decodeURLSegment(url.getPathSegments().get(2)); | |
appendWhere(whereClause, Imps.Messages.THREAD_ID, "=", | |
getContactId(db, accountStr, contact)); | |
notifyMessagesByContactContentUri = true; | |
break; | |
case MATCH_OTR_MESSAGES_BY_THREAD_ID: | |
tableToChange = TABLE_IN_MEMORY_MESSAGES; | |
try { | |
threadId = Long.parseLong(decodeURLSegment(url.getPathSegments().get(1))); | |
} catch (NumberFormatException ex) { | |
throw new IllegalArgumentException(); | |
} | |
appendWhere(whereClause, Imps.Messages.THREAD_ID, "=", threadId); | |
notifyMessagesByThreadIdContentUri = true; | |
break; | |
case MATCH_OTR_MESSAGES_BY_PROVIDER: | |
tableToChange = TABLE_IN_MEMORY_MESSAGES; | |
provider = decodeURLSegment(url.getPathSegments().get(1)); | |
appendWhere(whereClause, buildContactIdSelection(Imps.Messages.THREAD_ID, | |
Imps.Contacts.PROVIDER + "='" + provider + "'")); | |
if (DBG) log("delete (MATCH_OTR_MESSAGES_BY_PROVIDER) sel => " + whereClause); | |
notifyMessagesContentUri = true; | |
break; | |
case MATCH_OTR_MESSAGES_BY_ACCOUNT: | |
tableToChange = TABLE_IN_MEMORY_MESSAGES; | |
accountStr = decodeURLSegment(url.getPathSegments().get(1)); | |
appendWhere(whereClause, buildContactIdSelection(Imps.Messages.THREAD_ID, | |
Imps.Contacts.ACCOUNT + "='" + accountStr + "'")); | |
if (DBG) log("delete (MATCH_OTR_MESSAGES_BY_ACCOUNT) sel => " + whereClause); | |
notifyMessagesContentUri = true; | |
break; | |
case MATCH_OTR_MESSAGE: | |
tableToChange = TABLE_IN_MEMORY_MESSAGES; | |
changedItemId = url.getPathSegments().get(1); | |
notifyMessagesContentUri = true; | |
break; | |
case MATCH_GROUP_MEMBERS: | |
tableToChange = TABLE_GROUP_MEMBERS; | |
break; | |
case MATCH_GROUP_MEMBERS_BY_GROUP: | |
tableToChange = TABLE_GROUP_MEMBERS; | |
appendWhere(whereClause, Imps.GroupMembers.GROUP, "=", url.getPathSegments().get(1)); | |
break; | |
case MATCH_INVITATIONS: | |
tableToChange = TABLE_INVITATIONS; | |
break; | |
case MATCH_INVITATION: | |
tableToChange = TABLE_INVITATIONS; | |
changedItemId = url.getPathSegments().get(1); | |
break; | |
case MATCH_AVATARS: | |
tableToChange = TABLE_AVATARS; | |
break; | |
case MATCH_AVATAR: | |
tableToChange = TABLE_AVATARS; | |
changedItemId = url.getPathSegments().get(1); | |
break; | |
case MATCH_AVATAR_BY_PROVIDER: | |
tableToChange = TABLE_AVATARS; | |
changedItemId = url.getPathSegments().get(2); | |
idColumnName = Imps.Avatars.ACCOUNT; | |
break; | |
case MATCH_CHATS: | |
tableToChange = TABLE_CHATS; | |
backfillQuickSwitchSlots = true; | |
break; | |
case MATCH_CHATS_BY_ACCOUNT: | |
tableToChange = TABLE_CHATS; | |
accountStr = decodeURLSegment(url.getLastPathSegment()); | |
appendWhere(whereClause, buildContactIdSelection(Imps.Chats.CONTACT_ID, | |
Imps.Contacts.ACCOUNT + "='" + accountStr + "'")); | |
if (DBG) log("delete (MATCH_CHATS_BY_ACCOUNT) sel => " + whereClause); | |
changedItemId = null; | |
break; | |
case MATCH_CHATS_ID: | |
tableToChange = TABLE_CHATS; | |
changedItemId = url.getPathSegments().get(1); | |
idColumnName = Imps.Chats.CONTACT_ID; | |
break; | |
case MATCH_PRESENCE: | |
tableToChange = TABLE_PRESENCE; | |
break; | |
case MATCH_PRESENCE_ID: | |
tableToChange = TABLE_PRESENCE; | |
changedItemId = url.getPathSegments().get(1); | |
idColumnName = Imps.Presence.CONTACT_ID; | |
break; | |
case MATCH_PRESENCE_BY_ACCOUNT: | |
tableToChange = TABLE_PRESENCE; | |
accountStr = decodeURLSegment(url.getLastPathSegment()); | |
appendWhere(whereClause, buildContactIdSelection(Imps.Presence.CONTACT_ID, | |
Imps.Contacts.ACCOUNT + "='" + accountStr + "'")); | |
if (DBG) log("delete (MATCH_PRESENCE_BY_ACCOUNT): sel => " + whereClause); | |
changedItemId = null; | |
break; | |
case MATCH_SESSIONS: | |
tableToChange = TABLE_SESSION_COOKIES; | |
break; | |
case MATCH_SESSIONS_BY_PROVIDER: | |
tableToChange = TABLE_SESSION_COOKIES; | |
changedItemId = url.getPathSegments().get(2); | |
idColumnName = Imps.SessionCookies.ACCOUNT; | |
break; | |
case MATCH_PROVIDER_SETTINGS_BY_ID: | |
tableToChange = TABLE_PROVIDER_SETTINGS; | |
changedItemId = url.getPathSegments().get(1); | |
idColumnName = Imps.ProviderSettings.PROVIDER; | |
break; | |
case MATCH_PROVIDER_SETTINGS_BY_ID_AND_NAME: | |
tableToChange = TABLE_PROVIDER_SETTINGS; | |
String providerId = url.getPathSegments().get(1); | |
String name = url.getPathSegments().get(2); | |
appendWhere(whereClause, Imps.ProviderSettings.PROVIDER, "=", providerId); | |
appendWhere(whereClause, Imps.ProviderSettings.NAME, "=", name); | |
break; | |
case MATCH_BRANDING_RESOURCE_MAP_CACHE: | |
tableToChange = TABLE_BRANDING_RESOURCE_MAP_CACHE; | |
break; | |
// mcs/rmq stuff | |
case MATCH_OUTGOING_RMQ_MESSAGES: | |
tableToChange = TABLE_OUTGOING_RMQ_MESSAGES; | |
break; | |
case MATCH_LAST_RMQ_ID: | |
tableToChange = TABLE_LAST_RMQ_ID; | |
break; | |
case MATCH_S2D_RMQ_IDS: | |
tableToChange = TABLE_S2D_RMQ_IDS; | |
break; | |
default: | |
throw new UnsupportedOperationException("Cannot delete that URL: " + url); | |
} | |
if (idColumnName == null) { | |
idColumnName = "_id"; | |
} | |
if (changedItemId != null) { | |
appendWhere(whereClause, idColumnName, "=", changedItemId); | |
} | |
if (DBG) log("delete from " + url + " WHERE " + whereClause); | |
int count = db.delete(tableToChange, whereClause.toString(), whereArgs); | |
// see the comment at the declaration of additionalTableToChange for an explanation | |
if (additionalTableToChange != null){ | |
count += db.delete(additionalTableToChange, whereClause.toString(), whereArgs); | |
} | |
if (contactDeleted && count > 0) { | |
// since the contact cleanup triggers no longer work for cross database tables, | |
// we have to do it by hand here. | |
performContactRemovalCleanup(deletedContactId); | |
} | |
if (count > 0) { | |
ContentResolver resolver = getContext().getContentResolver(); | |
// In most case, we query contacts with presence and chats joined, thus | |
// we should also notify that contacts changes when presence or chats changed. | |
if (match == MATCH_CHATS || match == MATCH_CHATS_ID | |
|| match == MATCH_PRESENCE || match == MATCH_PRESENCE_ID | |
|| match == MATCH_CONTACTS_BAREBONE) { | |
resolver.notifyChange(Imps.Contacts.CONTENT_URI, null); | |
} | |
if (notifyMessagesContentUri) { | |
resolver.notifyChange(Imps.Messages.CONTENT_URI, null); | |
} | |
if (notifyMessagesByContactContentUri) { | |
resolver.notifyChange(Imps.Messages.CONTENT_URI, null); | |
resolver.notifyChange(Imps.Messages.getContentUriByContact(account, contact), null); | |
} | |
if (notifyMessagesByThreadIdContentUri) { | |
resolver.notifyChange(Imps.Messages.CONTENT_URI, null); | |
resolver.notifyChange(Imps.Messages.getContentUriByThreadId(threadId), null); | |
} | |
if (notifyContactListContentUri) { | |
resolver.notifyChange(Imps.ContactList.CONTENT_URI, null); | |
} | |
if (notifyProviderAccountContentUri) { | |
if (DBG) log("notify delete for " + Imps.Provider.CONTENT_URI_WITH_ACCOUNT); | |
resolver.notifyChange(Imps.Provider.CONTENT_URI_WITH_ACCOUNT, null); | |
} | |
if (backfillQuickSwitchSlots) { | |
backfillQuickSwitchSlots(); | |
} | |
} | |
return count; | |
} | |
private StringBuilder getWhereClause(String userWhere) { | |
StringBuilder whereClause = new StringBuilder(); | |
if(userWhere != null) { | |
whereClause.append(userWhere); | |
} | |
return whereClause; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private int deleteInternal(Uri url, String userWhere, String[] whereArgs) { | |
String tableToChange; | |
// In some cases a given url requires that we delete rows from more than one | |
// table. The motivating example is deleting messages from both the on disk | |
// and in memory messages tables. | |
String additionalTableToChange = null; | |
String idColumnName = null; | |
String changedItemId = null; | |
String provider = null; | |
String accountStr = null; | |
long account = 0; | |
String contact = null; | |
long threadId = 0; | |
StringBuilder whereClause = getWhereClause(userWhere); | |
boolean notifyMessagesContentUri = false; | |
boolean notifyMessagesByContactContentUri = false; | |
boolean notifyMessagesByThreadIdContentUri = false; | |
boolean notifyContactListContentUri = false; | |
boolean notifyProviderAccountContentUri = false; | |
int match = mUrlMatcher.match(url); | |
boolean contactDeleted = false; | |
long deletedContactId = 0; | |
boolean backfillQuickSwitchSlots = false; | |
final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); | |
UrlMatch urlMatch = new UrlMatch(url, additionalTableToChange, idColumnName, changedItemId, account, contact, threadId, whereClause, notifyMessagesContentUri, notifyMessagesByContactContentUri, notifyMessagesByThreadIdContentUri, notifyContactListContentUri, notifyProviderAccountContentUri, match, contactDeleted, deletedContactId, backfillQuickSwitchSlots, db).invoke(); | |
idColumnName = urlMatch.getIdColumnName(); | |
changedItemId = urlMatch.getChangedItemId(); | |
tableToChange = urlMatch.getTableToChange(); | |
additionalTableToChange = urlMatch.getAdditionalTableToChange(); | |
contactDeleted = urlMatch.isContactDeleted(); | |
deletedContactId = urlMatch.getDeletedContactId(); | |
notifyMessagesContentUri = urlMatch.isNotifyMessagesContentUri(); | |
notifyMessagesByContactContentUri = urlMatch.isNotifyMessagesByContactContentUri(); | |
account = urlMatch.getAccount(); | |
contact = urlMatch.getContact(); | |
notifyMessagesByThreadIdContentUri = urlMatch.isNotifyMessagesByThreadIdContentUri(); | |
threadId = urlMatch.getThreadId(); | |
notifyContactListContentUri = urlMatch.isNotifyContactListContentUri(); | |
notifyProviderAccountContentUri = urlMatch.isNotifyProviderAccountContentUri(); | |
backfillQuickSwitchSlots = urlMatch.isBackfillQuickSwitchSlots(); | |
if (idColumnName == null) { | |
idColumnName = "_id"; | |
} | |
if (changedItemId != null) { | |
appendWhere(whereClause, idColumnName, "=", changedItemId); | |
} | |
if (DBG) log("delete from " + url + " WHERE " + whereClause); | |
int count = db.delete(tableToChange, whereClause.toString(), whereArgs); | |
// see the comment at the declaration of additionalTableToChange for an explanation | |
if (additionalTableToChange != null){ | |
count += db.delete(additionalTableToChange, whereClause.toString(), whereArgs); | |
} | |
if (contactDeleted && count > 0) { | |
// since the contact cleanup triggers no longer work for cross database tables, | |
// we have to do it by hand here. | |
performContactRemovalCleanup(deletedContactId); | |
} | |
if (count > 0) { | |
ContentResolver resolver = getContext().getContentResolver(); | |
// In most case, we query contacts with presence and chats joined, thus | |
// we should also notify that contacts changes when presence or chats changed. | |
if (match == MATCH_CHATS || match == MATCH_CHATS_ID | |
|| match == MATCH_PRESENCE || match == MATCH_PRESENCE_ID | |
|| match == MATCH_CONTACTS_BAREBONE) { | |
resolver.notifyChange(Imps.Contacts.CONTENT_URI, null); | |
} | |
if (notifyMessagesContentUri) { | |
resolver.notifyChange(Imps.Messages.CONTENT_URI, null); | |
} | |
if (notifyMessagesByContactContentUri) { | |
resolver.notifyChange(Imps.Messages.CONTENT_URI, null); | |
resolver.notifyChange(Imps.Messages.getContentUriByContact(account, contact), null); | |
} | |
if (notifyMessagesByThreadIdContentUri) { | |
resolver.notifyChange(Imps.Messages.CONTENT_URI, null); | |
resolver.notifyChange(Imps.Messages.getContentUriByThreadId(threadId), null); | |
} | |
if (notifyContactListContentUri) { | |
resolver.notifyChange(Imps.ContactList.CONTENT_URI, null); | |
} | |
if (notifyProviderAccountContentUri) { | |
if (DBG) log("notify delete for " + Imps.Provider.CONTENT_URI_WITH_ACCOUNT); | |
resolver.notifyChange(Imps.Provider.CONTENT_URI_WITH_ACCOUNT, null); | |
} | |
if (backfillQuickSwitchSlots) { | |
backfillQuickSwitchSlots(); | |
} | |
} | |
return count; | |
} | |
private StringBuilder getWhereClause(String userWhere) { | |
StringBuilder whereClause = new StringBuilder(); | |
if(userWhere != null) { | |
whereClause.append(userWhere); | |
} | |
return whereClause; | |
} | |
private class UrlMatch { | |
private Uri url; | |
private String additionalTableToChange; | |
private String idColumnName; | |
private String changedItemId; | |
private long account; | |
private String contact; | |
private long threadId; | |
private StringBuilder whereClause; | |
private boolean notifyMessagesContentUri; | |
private boolean notifyMessagesByContactContentUri; | |
private boolean notifyMessagesByThreadIdContentUri; | |
private boolean notifyContactListContentUri; | |
private boolean notifyProviderAccountContentUri; | |
private int match; | |
private boolean contactDeleted; | |
private long deletedContactId; | |
private boolean backfillQuickSwitchSlots; | |
private SQLiteDatabase db; | |
private String tableToChange; | |
public UrlMatch(Uri url, String additionalTableToChange, String idColumnName, String changedItemId, long account, String contact, long threadId, StringBuilder whereClause, boolean notifyMessagesContentUri, boolean notifyMessagesByContactContentUri, boolean notifyMessagesByThreadIdContentUri, boolean notifyContactListContentUri, boolean notifyProviderAccountContentUri, int match, boolean contactDeleted, long deletedContactId, boolean backfillQuickSwitchSlots, SQLiteDatabase db) { | |
this.url = url; | |
this.additionalTableToChange = additionalTableToChange; | |
this.idColumnName = idColumnName; | |
this.changedItemId = changedItemId; | |
this.account = account; | |
this.contact = contact; | |
this.threadId = threadId; | |
this.whereClause = whereClause; | |
this.notifyMessagesContentUri = notifyMessagesContentUri; | |
this.notifyMessagesByContactContentUri = notifyMessagesByContactContentUri; | |
this.notifyMessagesByThreadIdContentUri = notifyMessagesByThreadIdContentUri; | |
this.notifyContactListContentUri = notifyContactListContentUri; | |
this.notifyProviderAccountContentUri = notifyProviderAccountContentUri; | |
this.match = match; | |
this.contactDeleted = contactDeleted; | |
this.deletedContactId = deletedContactId; | |
this.backfillQuickSwitchSlots = backfillQuickSwitchSlots; | |
this.db = db; | |
} | |
public String getTableToChange() { | |
return tableToChange; | |
} | |
public String getAdditionalTableToChange() { | |
return additionalTableToChange; | |
} | |
public String getIdColumnName() { | |
return idColumnName; | |
} | |
public String getChangedItemId() { | |
return changedItemId; | |
} | |
public long getAccount() { | |
return account; | |
} | |
public String getContact() { | |
return contact; | |
} | |
public long getThreadId() { | |
return threadId; | |
} | |
public boolean isNotifyMessagesContentUri() { | |
return notifyMessagesContentUri; | |
} | |
public boolean isNotifyMessagesByContactContentUri() { | |
return notifyMessagesByContactContentUri; | |
} | |
public boolean isNotifyMessagesByThreadIdContentUri() { | |
return notifyMessagesByThreadIdContentUri; | |
} | |
public boolean isNotifyContactListContentUri() { | |
return notifyContactListContentUri; | |
} | |
public boolean isNotifyProviderAccountContentUri() { | |
return notifyProviderAccountContentUri; | |
} | |
public boolean isContactDeleted() { | |
return contactDeleted; | |
} | |
public long getDeletedContactId() { | |
return deletedContactId; | |
} | |
public boolean isBackfillQuickSwitchSlots() { | |
return backfillQuickSwitchSlots; | |
} | |
public UrlMatch invoke() { | |
String accountStr; | |
String provider; | |
switch (match) { | |
case MATCH_PROVIDERS: | |
tableToChange = TABLE_PROVIDERS; | |
notifyProviderAccountContentUri = true; | |
break; | |
case MATCH_ACCOUNTS_BY_ID: | |
changedItemId = url.getPathSegments().get(1); | |
// fall through | |
case MATCH_ACCOUNTS: | |
tableToChange = TABLE_ACCOUNTS; | |
notifyProviderAccountContentUri = true; | |
break; | |
case MATCH_ACCOUNT_STATUS: | |
changedItemId = url.getPathSegments().get(1); | |
// fall through | |
case MATCH_ACCOUNTS_STATUS: | |
tableToChange = TABLE_ACCOUNT_STATUS; | |
notifyProviderAccountContentUri = true; | |
break; | |
case MATCH_CONTACTS: | |
case MATCH_CONTACTS_BAREBONE: | |
tableToChange = TABLE_CONTACTS; | |
contactDeleted = true; | |
break; | |
case MATCH_CONTACT: | |
tableToChange = TABLE_CONTACTS; | |
changedItemId = url.getPathSegments().get(1); | |
try { | |
deletedContactId = Long.parseLong(changedItemId); | |
} catch (NumberFormatException ex) { | |
throw new IllegalArgumentException(); | |
} | |
contactDeleted = true; | |
break; | |
case MATCH_CONTACTS_BY_PROVIDER: | |
tableToChange = TABLE_CONTACTS; | |
appendWhere(whereClause, Imps.Contacts.ACCOUNT, "=", url.getPathSegments().get(2)); | |
contactDeleted = true; | |
break; | |
case MATCH_CONTACTLISTS_BY_PROVIDER: | |
appendWhere(whereClause, Imps.ContactList.ACCOUNT, "=", | |
url.getPathSegments().get(2)); | |
// fall through | |
case MATCH_CONTACTLISTS: | |
tableToChange = TABLE_CONTACT_LIST; | |
notifyContactListContentUri = true; | |
break; | |
case MATCH_CONTACTLIST: | |
tableToChange = TABLE_CONTACT_LIST; | |
changedItemId = url.getPathSegments().get(1); | |
break; | |
case MATCH_BLOCKEDLIST: | |
tableToChange = TABLE_BLOCKED_LIST; | |
break; | |
case MATCH_BLOCKEDLIST_BY_PROVIDER: | |
tableToChange = TABLE_BLOCKED_LIST; | |
appendWhere(whereClause, Imps.BlockedList.ACCOUNT, "=", url.getPathSegments().get(2)); | |
break; | |
case MATCH_CONTACTS_ETAGS: | |
tableToChange = TABLE_CONTACTS_ETAG; | |
break; | |
case MATCH_CONTACTS_ETAG: | |
tableToChange = TABLE_CONTACTS_ETAG; | |
changedItemId = url.getPathSegments().get(1); | |
break; | |
case MATCH_MESSAGES: | |
tableToChange = TABLE_MESSAGES; | |
break; | |
case MATCH_MESSAGES_BY_CONTACT: | |
tableToChange = TABLE_MESSAGES; | |
additionalTableToChange = TABLE_IN_MEMORY_MESSAGES; | |
accountStr = decodeURLSegment(url.getPathSegments().get(1)); | |
try { | |
account = Long.parseLong(accountStr); | |
} catch (NumberFormatException ex) { | |
throw new IllegalArgumentException(); | |
} | |
contact = decodeURLSegment(url.getPathSegments().get(2)); | |
appendWhere(whereClause, Imps.Messages.THREAD_ID, "=", | |
getContactId(db, accountStr, contact)); | |
notifyMessagesContentUri = true; | |
break; | |
case MATCH_MESSAGES_BY_THREAD_ID: | |
tableToChange = TABLE_MESSAGES; | |
additionalTableToChange = TABLE_IN_MEMORY_MESSAGES; | |
try { | |
threadId = Long.parseLong(decodeURLSegment(url.getPathSegments().get(1))); | |
} catch (NumberFormatException ex) { | |
throw new IllegalArgumentException(); | |
} | |
appendWhere(whereClause, Imps.Messages.THREAD_ID, "=", threadId); | |
notifyMessagesContentUri = true; | |
break; | |
case MATCH_MESSAGES_BY_PROVIDER: | |
tableToChange = TABLE_MESSAGES; | |
provider = decodeURLSegment(url.getPathSegments().get(1)); | |
appendWhere(whereClause, buildContactIdSelection(Imps.Messages.THREAD_ID, | |
Imps.Contacts.PROVIDER + "='" + provider + "'")); | |
notifyMessagesContentUri = true; | |
break; | |
case MATCH_MESSAGES_BY_ACCOUNT: | |
tableToChange = TABLE_MESSAGES; | |
accountStr = decodeURLSegment(url.getPathSegments().get(1)); | |
appendWhere(whereClause, buildContactIdSelection(Imps.Messages.THREAD_ID, | |
Imps.Contacts.ACCOUNT + "='" + accountStr + "'")); | |
notifyMessagesContentUri = true; | |
break; | |
case MATCH_MESSAGE: | |
tableToChange = TABLE_MESSAGES; | |
changedItemId = url.getPathSegments().get(1); | |
notifyMessagesContentUri = true; | |
break; | |
case MATCH_OTR_MESSAGES: | |
tableToChange = TABLE_IN_MEMORY_MESSAGES; | |
break; | |
case MATCH_OTR_MESSAGES_BY_CONTACT: | |
tableToChange = TABLE_IN_MEMORY_MESSAGES; | |
accountStr = decodeURLSegment(url.getPathSegments().get(1)); | |
try { | |
account = Long.parseLong(accountStr); | |
} catch (NumberFormatException ex) { | |
throw new IllegalArgumentException(); | |
} | |
contact = decodeURLSegment(url.getPathSegments().get(2)); | |
appendWhere(whereClause, Imps.Messages.THREAD_ID, "=", | |
getContactId(db, accountStr, contact)); | |
notifyMessagesByContactContentUri = true; | |
break; | |
case MATCH_OTR_MESSAGES_BY_THREAD_ID: | |
tableToChange = TABLE_IN_MEMORY_MESSAGES; | |
try { | |
threadId = Long.parseLong(decodeURLSegment(url.getPathSegments().get(1))); | |
} catch (NumberFormatException ex) { | |
throw new IllegalArgumentException(); | |
} | |
appendWhere(whereClause, Imps.Messages.THREAD_ID, "=", threadId); | |
notifyMessagesByThreadIdContentUri = true; | |
break; | |
case MATCH_OTR_MESSAGES_BY_PROVIDER: | |
tableToChange = TABLE_IN_MEMORY_MESSAGES; | |
provider = decodeURLSegment(url.getPathSegments().get(1)); | |
appendWhere(whereClause, buildContactIdSelection(Imps.Messages.THREAD_ID, | |
Imps.Contacts.PROVIDER + "='" + provider + "'")); | |
if (DBG) log("delete (MATCH_OTR_MESSAGES_BY_PROVIDER) sel => " + whereClause); | |
notifyMessagesContentUri = true; | |
break; | |
case MATCH_OTR_MESSAGES_BY_ACCOUNT: | |
tableToChange = TABLE_IN_MEMORY_MESSAGES; | |
accountStr = decodeURLSegment(url.getPathSegments().get(1)); | |
appendWhere(whereClause, buildContactIdSelection(Imps.Messages.THREAD_ID, | |
Imps.Contacts.ACCOUNT + "='" + accountStr + "'")); | |
if (DBG) log("delete (MATCH_OTR_MESSAGES_BY_ACCOUNT) sel => " + whereClause); | |
notifyMessagesContentUri = true; | |
break; | |
case MATCH_OTR_MESSAGE: | |
tableToChange = TABLE_IN_MEMORY_MESSAGES; | |
changedItemId = url.getPathSegments().get(1); | |
notifyMessagesContentUri = true; | |
break; | |
case MATCH_GROUP_MEMBERS: | |
tableToChange = TABLE_GROUP_MEMBERS; | |
break; | |
case MATCH_GROUP_MEMBERS_BY_GROUP: | |
tableToChange = TABLE_GROUP_MEMBERS; | |
appendWhere(whereClause, Imps.GroupMembers.GROUP, "=", url.getPathSegments().get(1)); | |
break; | |
case MATCH_INVITATIONS: | |
tableToChange = TABLE_INVITATIONS; | |
break; | |
case MATCH_INVITATION: | |
tableToChange = TABLE_INVITATIONS; | |
changedItemId = url.getPathSegments().get(1); | |
break; | |
case MATCH_AVATARS: | |
tableToChange = TABLE_AVATARS; | |
break; | |
case MATCH_AVATAR: | |
tableToChange = TABLE_AVATARS; | |
changedItemId = url.getPathSegments().get(1); | |
break; | |
case MATCH_AVATAR_BY_PROVIDER: | |
tableToChange = TABLE_AVATARS; | |
changedItemId = url.getPathSegments().get(2); | |
idColumnName = Imps.Avatars.ACCOUNT; | |
break; | |
case MATCH_CHATS: | |
tableToChange = TABLE_CHATS; | |
backfillQuickSwitchSlots = true; | |
break; | |
case MATCH_CHATS_BY_ACCOUNT: | |
tableToChange = TABLE_CHATS; | |
accountStr = decodeURLSegment(url.getLastPathSegment()); | |
appendWhere(whereClause, buildContactIdSelection(Imps.Chats.CONTACT_ID, | |
Imps.Contacts.ACCOUNT + "='" + accountStr + "'")); | |
if (DBG) log("delete (MATCH_CHATS_BY_ACCOUNT) sel => " + whereClause); | |
changedItemId = null; | |
break; | |
case MATCH_CHATS_ID: | |
tableToChange = TABLE_CHATS; | |
changedItemId = url.getPathSegments().get(1); | |
idColumnName = Imps.Chats.CONTACT_ID; | |
break; | |
case MATCH_PRESENCE: | |
tableToChange = TABLE_PRESENCE; | |
break; | |
case MATCH_PRESENCE_ID: | |
tableToChange = TABLE_PRESENCE; | |
changedItemId = url.getPathSegments().get(1); | |
idColumnName = Imps.Presence.CONTACT_ID; | |
break; | |
case MATCH_PRESENCE_BY_ACCOUNT: | |
tableToChange = TABLE_PRESENCE; | |
accountStr = decodeURLSegment(url.getLastPathSegment()); | |
appendWhere(whereClause, buildContactIdSelection(Imps.Presence.CONTACT_ID, | |
Imps.Contacts.ACCOUNT + "='" + accountStr + "'")); | |
if (DBG) log("delete (MATCH_PRESENCE_BY_ACCOUNT): sel => " + whereClause); | |
changedItemId = null; | |
break; | |
case MATCH_SESSIONS: | |
tableToChange = TABLE_SESSION_COOKIES; | |
break; | |
case MATCH_SESSIONS_BY_PROVIDER: | |
tableToChange = TABLE_SESSION_COOKIES; | |
changedItemId = url.getPathSegments().get(2); | |
idColumnName = Imps.SessionCookies.ACCOUNT; | |
break; | |
case MATCH_PROVIDER_SETTINGS_BY_ID: | |
tableToChange = TABLE_PROVIDER_SETTINGS; | |
changedItemId = url.getPathSegments().get(1); | |
idColumnName = Imps.ProviderSettings.PROVIDER; | |
break; | |
case MATCH_PROVIDER_SETTINGS_BY_ID_AND_NAME: | |
tableToChange = TABLE_PROVIDER_SETTINGS; | |
String providerId = url.getPathSegments().get(1); | |
String name = url.getPathSegments().get(2); | |
appendWhere(whereClause, Imps.ProviderSettings.PROVIDER, "=", providerId); | |
appendWhere(whereClause, Imps.ProviderSettings.NAME, "=", name); | |
break; | |
case MATCH_BRANDING_RESOURCE_MAP_CACHE: | |
tableToChange = TABLE_BRANDING_RESOURCE_MAP_CACHE; | |
break; | |
// mcs/rmq stuff | |
case MATCH_OUTGOING_RMQ_MESSAGES: | |
tableToChange = TABLE_OUTGOING_RMQ_MESSAGES; | |
break; | |
case MATCH_LAST_RMQ_ID: | |
tableToChange = TABLE_LAST_RMQ_ID; | |
break; | |
case MATCH_S2D_RMQ_IDS: | |
tableToChange = TABLE_S2D_RMQ_IDS; | |
break; | |
default: | |
throw new UnsupportedOperationException("Cannot delete that URL: " + url); | |
} | |
return this; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private int deleteInternal(Uri url, String userWhere, String[] whereArgs) { | |
String tableToChange; | |
// In some cases a given url requires that we delete rows from more than one | |
// table. The motivating example is deleting messages from both the on disk | |
// and in memory messages tables. | |
String additionalTableToChange = null; | |
String idColumnName = null; | |
String changedItemId = null; | |
String provider = null; | |
String accountStr = null; | |
long account = 0; | |
String contact = null; | |
long threadId = 0; | |
StringBuilder whereClause = getWhereClause(userWhere); | |
boolean notifyMessagesContentUri = false; | |
boolean notifyMessagesByContactContentUri = false; | |
boolean notifyMessagesByThreadIdContentUri = false; | |
boolean notifyContactListContentUri = false; | |
boolean notifyProviderAccountContentUri = false; | |
int match = mUrlMatcher.match(url); | |
boolean contactDeleted = false; | |
long deletedContactId = 0; | |
boolean backfillQuickSwitchSlots = false; | |
final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); | |
UrlMatch urlMatch = new UrlMatch(url, additionalTableToChange, idColumnName, changedItemId, account, contact, threadId, whereClause, notifyMessagesContentUri, notifyMessagesByContactContentUri, notifyMessagesByThreadIdContentUri, notifyContactListContentUri, notifyProviderAccountContentUri, match, contactDeleted, deletedContactId, backfillQuickSwitchSlots, db).invoke(); | |
idColumnName = urlMatch.getIdColumnName(); | |
changedItemId = urlMatch.getChangedItemId(); | |
tableToChange = urlMatch.getTableToChange(); | |
additionalTableToChange = urlMatch.getAdditionalTableToChange(); | |
contactDeleted = urlMatch.isContactDeleted(); | |
deletedContactId = urlMatch.getDeletedContactId(); | |
notifyMessagesContentUri = urlMatch.isNotifyMessagesContentUri(); | |
notifyMessagesByContactContentUri = urlMatch.isNotifyMessagesByContactContentUri(); | |
account = urlMatch.getAccount(); | |
contact = urlMatch.getContact(); | |
notifyMessagesByThreadIdContentUri = urlMatch.isNotifyMessagesByThreadIdContentUri(); | |
threadId = urlMatch.getThreadId(); | |
notifyContactListContentUri = urlMatch.isNotifyContactListContentUri(); | |
notifyProviderAccountContentUri = urlMatch.isNotifyProviderAccountContentUri(); | |
backfillQuickSwitchSlots = urlMatch.isBackfillQuickSwitchSlots(); | |
idColumnName = setIdColumnNameIfNull(idColumnName); | |
appendIfChangedItemIdIsNotNull(idColumnName, changedItemId, whereClause); | |
if (DBG) log("delete from " + url + " WHERE " + whereClause); | |
int count = db.delete(tableToChange, whereClause.toString(), whereArgs); | |
count = deleteRecordsFromAdditionTableToChange(whereArgs, additionalTableToChange, whereClause, db, count); | |
if (contactDeleted && count > 0) { | |
// since the contact cleanup triggers no longer work for cross database tables, | |
// we have to do it by hand here. | |
performContactRemovalCleanup(deletedContactId); | |
} | |
if (count > 0) { | |
ContentResolver resolver = getContext().getContentResolver(); | |
// In most case, we query contacts with presence and chats joined, thus | |
// we should also notify that contacts changes when presence or chats changed. | |
if (match == MATCH_CHATS || match == MATCH_CHATS_ID | |
|| match == MATCH_PRESENCE || match == MATCH_PRESENCE_ID | |
|| match == MATCH_CONTACTS_BAREBONE) { | |
resolver.notifyChange(Imps.Contacts.CONTENT_URI, null); | |
} | |
if (notifyMessagesContentUri) { | |
resolver.notifyChange(Imps.Messages.CONTENT_URI, null); | |
} | |
if (notifyMessagesByContactContentUri) { | |
resolver.notifyChange(Imps.Messages.CONTENT_URI, null); | |
resolver.notifyChange(Imps.Messages.getContentUriByContact(account, contact), null); | |
} | |
if (notifyMessagesByThreadIdContentUri) { | |
resolver.notifyChange(Imps.Messages.CONTENT_URI, null); | |
resolver.notifyChange(Imps.Messages.getContentUriByThreadId(threadId), null); | |
} | |
if (notifyContactListContentUri) { | |
resolver.notifyChange(Imps.ContactList.CONTENT_URI, null); | |
} | |
if (notifyProviderAccountContentUri) { | |
if (DBG) log("notify delete for " + Imps.Provider.CONTENT_URI_WITH_ACCOUNT); | |
resolver.notifyChange(Imps.Provider.CONTENT_URI_WITH_ACCOUNT, null); | |
} | |
if (backfillQuickSwitchSlots) { | |
backfillQuickSwitchSlots(); | |
} | |
} | |
return count; | |
} | |
private int deleteRecordsFromAdditionTableToChange(String[] whereArgs, String additionalTableToChange, StringBuilder whereClause, SQLiteDatabase db, int count) { | |
// see the comment at the declaration of additionalTableToChange for an explanation | |
if (additionalTableToChange != null){ | |
count += db.delete(additionalTableToChange, whereClause.toString(), whereArgs); | |
} | |
return count; | |
} | |
private void appendIfChangedItemIdIsNotNull(String idColumnName, String changedItemId, StringBuilder whereClause) { | |
if (changedItemId != null) { | |
appendWhere(whereClause, idColumnName, "=", changedItemId); | |
} | |
} | |
private String setIdColumnNameIfNull(String idColumnName) { | |
if (idColumnName == null) { | |
idColumnName = "_id"; | |
} | |
return idColumnName; | |
} | |
private StringBuilder getWhereClause(String userWhere) { | |
StringBuilder whereClause = new StringBuilder(); | |
if(userWhere != null) { | |
whereClause.append(userWhere); | |
} | |
return whereClause; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private int deleteInternal(Uri url, String userWhere, String[] whereArgs) { | |
String tableToChange; | |
// In some cases a given url requires that we delete rows from more than one | |
// table. The motivating example is deleting messages from both the on disk | |
// and in memory messages tables. | |
String additionalTableToChange = null; | |
String idColumnName = null; | |
String changedItemId = null; | |
long account = 0; | |
String contact = null; | |
long threadId = 0; | |
StringBuilder whereClause = getWhereClause(userWhere); | |
boolean notifyMessagesContentUri = false; | |
boolean notifyMessagesByContactContentUri = false; | |
boolean notifyMessagesByThreadIdContentUri = false; | |
boolean notifyContactListContentUri = false; | |
boolean notifyProviderAccountContentUri = false; | |
int match = mUrlMatcher.match(url); | |
boolean contactDeleted = false; | |
long deletedContactId = 0; | |
boolean backfillQuickSwitchSlots = false; | |
final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); | |
UrlMatch urlMatch = new UrlMatch(url, additionalTableToChange, idColumnName, changedItemId, account, contact, threadId, whereClause, notifyMessagesContentUri, notifyMessagesByContactContentUri, notifyMessagesByThreadIdContentUri, notifyContactListContentUri, notifyProviderAccountContentUri, match, contactDeleted, deletedContactId, backfillQuickSwitchSlots, db).invoke(); | |
idColumnName = urlMatch.getIdColumnName(); | |
changedItemId = urlMatch.getChangedItemId(); | |
tableToChange = urlMatch.getTableToChange(); | |
additionalTableToChange = urlMatch.getAdditionalTableToChange(); | |
contactDeleted = urlMatch.isContactDeleted(); | |
deletedContactId = urlMatch.getDeletedContactId(); | |
notifyMessagesContentUri = urlMatch.isNotifyMessagesContentUri(); | |
notifyMessagesByContactContentUri = urlMatch.isNotifyMessagesByContactContentUri(); | |
account = urlMatch.getAccount(); | |
contact = urlMatch.getContact(); | |
notifyMessagesByThreadIdContentUri = urlMatch.isNotifyMessagesByThreadIdContentUri(); | |
threadId = urlMatch.getThreadId(); | |
notifyContactListContentUri = urlMatch.isNotifyContactListContentUri(); | |
notifyProviderAccountContentUri = urlMatch.isNotifyProviderAccountContentUri(); | |
backfillQuickSwitchSlots = urlMatch.isBackfillQuickSwitchSlots(); | |
idColumnName = setIdColumnNameIfNull(idColumnName); | |
appendIfChangedItemIdIsNotNull(idColumnName, changedItemId, whereClause); | |
if (DBG) log("delete from " + url + " WHERE " + whereClause); | |
int count = db.delete(tableToChange, whereClause.toString(), whereArgs); | |
count = deleteRecordsFromAdditionTableToChange(whereArgs, additionalTableToChange, whereClause, db, count); | |
if (contactDeleted && count > 0) { | |
// since the contact cleanup triggers no longer work for cross database tables, | |
// we have to do it by hand here. | |
performContactRemovalCleanup(deletedContactId); | |
} | |
notifyOfDeletedRows(account, contact, threadId, notifyMessagesContentUri, notifyMessagesByContactContentUri, notifyMessagesByThreadIdContentUri, notifyContactListContentUri, notifyProviderAccountContentUri, match, backfillQuickSwitchSlots, count); | |
return count; | |
} | |
private void notifyOfDeletedRows(long account, String contact, long threadId, boolean notifyMessagesContentUri, boolean notifyMessagesByContactContentUri, boolean notifyMessagesByThreadIdContentUri, boolean notifyContactListContentUri, boolean notifyProviderAccountContentUri, int match, boolean backfillQuickSwitchSlots, int count) { | |
if (count > 0) { | |
ContentResolver resolver = getContext().getContentResolver(); | |
// In most case, we query contacts with presence and chats joined, thus | |
// we should also notify that contacts changes when presence or chats changed. | |
if (match == MATCH_CHATS || match == MATCH_CHATS_ID | |
|| match == MATCH_PRESENCE || match == MATCH_PRESENCE_ID | |
|| match == MATCH_CONTACTS_BAREBONE) { | |
resolver.notifyChange(Imps.Contacts.CONTENT_URI, null); | |
} | |
if (notifyMessagesContentUri) { | |
resolver.notifyChange(Imps.Messages.CONTENT_URI, null); | |
} | |
if (notifyMessagesByContactContentUri) { | |
resolver.notifyChange(Imps.Messages.CONTENT_URI, null); | |
resolver.notifyChange(Imps.Messages.getContentUriByContact(account, contact), null); | |
} | |
if (notifyMessagesByThreadIdContentUri) { | |
resolver.notifyChange(Imps.Messages.CONTENT_URI, null); | |
resolver.notifyChange(Imps.Messages.getContentUriByThreadId(threadId), null); | |
} | |
if (notifyContactListContentUri) { | |
resolver.notifyChange(Imps.ContactList.CONTENT_URI, null); | |
} | |
if (notifyProviderAccountContentUri) { | |
if (DBG) log("notify delete for " + Imps.Provider.CONTENT_URI_WITH_ACCOUNT); | |
resolver.notifyChange(Imps.Provider.CONTENT_URI_WITH_ACCOUNT, null); | |
} | |
if (backfillQuickSwitchSlots) { | |
backfillQuickSwitchSlots(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private int deleteInternal(Uri url, String userWhere, String[] whereArgs) { | |
StringBuilder whereClause = getWhereClause(userWhere); | |
int match = mUrlMatcher.match(url); | |
String idColumnName = null; | |
final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); | |
UrlMatch urlMatch = new UrlMatch(url, whereClause, match, db).invoke(); | |
idColumnName = urlMatch.getIdColumnName(); | |
idColumnName = setIdColumnNameIfNull(idColumnName); | |
appendIfChangedItemIdIsNotNull(idColumnName, urlMatch.getChangedItemId(), whereClause); | |
if (DBG) log("delete from " + url + " WHERE " + whereClause); | |
int count = db.delete(urlMatch.getTableToChange(), whereClause.toString(), whereArgs); | |
count = deleteRecordsFromAdditionTableToChange(whereArgs, urlMatch.getAdditionalTableToChange(), whereClause, db, count); | |
if (urlMatch.isContactDeleted() && count > 0) { | |
// since the contact cleanup triggers no longer work for cross database tables, | |
// we have to do it by hand here. | |
performContactRemovalCleanup(urlMatch.getDeletedContactId()); | |
} | |
notifyOfDeletedRows(urlMatch, match, count); | |
return count; | |
} | |
private void notifyOfDeletedRows(UrlMatch urlMatch, int match, int count) { | |
if (count > 0) { | |
ContentResolver resolver = getContext().getContentResolver(); | |
// In most case, we query contacts with presence and chats joined, thus | |
// we should also notify that contacts changes when presence or chats changed. | |
if (match == MATCH_CHATS || match == MATCH_CHATS_ID | |
|| match == MATCH_PRESENCE || match == MATCH_PRESENCE_ID | |
|| match == MATCH_CONTACTS_BAREBONE) { | |
resolver.notifyChange(Imps.Contacts.CONTENT_URI, null); | |
} | |
if (urlMatch.isNotifyMessagesContentUri()) { | |
resolver.notifyChange(Imps.Messages.CONTENT_URI, null); | |
} | |
if (urlMatch.isNotifyMessagesByContactContentUri()) { | |
resolver.notifyChange(Imps.Messages.CONTENT_URI, null); | |
resolver.notifyChange(Imps.Messages.getContentUriByContact(urlMatch.getAccount(), urlMatch.getContact()), null); | |
} | |
if (urlMatch.isNotifyMessagesByThreadIdContentUri()) { | |
resolver.notifyChange(Imps.Messages.CONTENT_URI, null); | |
resolver.notifyChange(Imps.Messages.getContentUriByThreadId(urlMatch.getThreadId()), null); | |
} | |
if (urlMatch.isNotifyContactListContentUri()) { | |
resolver.notifyChange(Imps.ContactList.CONTENT_URI, null); | |
} | |
if (urlMatch.isNotifyProviderAccountContentUri()) { | |
if (DBG) log("notify delete for " + Imps.Provider.CONTENT_URI_WITH_ACCOUNT); | |
resolver.notifyChange(Imps.Provider.CONTENT_URI_WITH_ACCOUNT, null); | |
} | |
if (urlMatch.isBackfillQuickSwitchSlots()) { | |
backfillQuickSwitchSlots(); | |
} | |
} | |
} | |
private int deleteRecordsFromAdditionTableToChange(String[] whereArgs, String additionalTableToChange, StringBuilder whereClause, SQLiteDatabase db, int count) { | |
// see the comment at the declaration of additionalTableToChange for an explanation | |
if (additionalTableToChange != null){ | |
count += db.delete(additionalTableToChange, whereClause.toString(), whereArgs); | |
} | |
return count; | |
} | |
private void appendIfChangedItemIdIsNotNull(String idColumnName, String changedItemId, StringBuilder whereClause) { | |
if (changedItemId != null) { | |
appendWhere(whereClause, idColumnName, "=", changedItemId); | |
} | |
} | |
private String setIdColumnNameIfNull(String idColumnName) { | |
if (idColumnName == null) { | |
idColumnName = "_id"; | |
} | |
return idColumnName; | |
} | |
private StringBuilder getWhereClause(String userWhere) { | |
StringBuilder whereClause = new StringBuilder(); | |
if(userWhere != null) { | |
whereClause.append(userWhere); | |
} | |
return whereClause; | |
} | |
private class UrlMatch { | |
private Uri url; | |
private String additionalTableToChange; | |
private String idColumnName; | |
private String changedItemId; | |
private long account; | |
private String contact; | |
private long threadId; | |
private StringBuilder whereClause; | |
private boolean notifyMessagesContentUri; | |
private boolean notifyMessagesByContactContentUri; | |
private boolean notifyMessagesByThreadIdContentUri; | |
private boolean notifyContactListContentUri; | |
private boolean notifyProviderAccountContentUri; | |
private int match; | |
private boolean contactDeleted; | |
private long deletedContactId; | |
private boolean backfillQuickSwitchSlots; | |
private SQLiteDatabase db; | |
private String tableToChange; | |
public UrlMatch(Uri url, StringBuilder whereClause, int match, SQLiteDatabase db) { | |
this.url = url; | |
this.whereClause = whereClause; | |
this.match = match; | |
this.db = db; | |
} | |
public String getTableToChange() { | |
return tableToChange; | |
} | |
public String getAdditionalTableToChange() { | |
return additionalTableToChange; | |
} | |
public String getIdColumnName() { | |
return idColumnName; | |
} | |
public String getChangedItemId() { | |
return changedItemId; | |
} | |
public long getAccount() { | |
return account; | |
} | |
public String getContact() { | |
return contact; | |
} | |
public long getThreadId() { | |
return threadId; | |
} | |
public boolean isNotifyMessagesContentUri() { | |
return notifyMessagesContentUri; | |
} | |
public boolean isNotifyMessagesByContactContentUri() { | |
return notifyMessagesByContactContentUri; | |
} | |
public boolean isNotifyMessagesByThreadIdContentUri() { | |
return notifyMessagesByThreadIdContentUri; | |
} | |
public boolean isNotifyContactListContentUri() { | |
return notifyContactListContentUri; | |
} | |
public boolean isNotifyProviderAccountContentUri() { | |
return notifyProviderAccountContentUri; | |
} | |
public boolean isContactDeleted() { | |
return contactDeleted; | |
} | |
public long getDeletedContactId() { | |
return deletedContactId; | |
} | |
public boolean isBackfillQuickSwitchSlots() { | |
return backfillQuickSwitchSlots; | |
} | |
public UrlMatch invoke() { | |
String accountStr; | |
String provider; | |
switch (match) { | |
case MATCH_PROVIDERS: | |
tableToChange = TABLE_PROVIDERS; | |
notifyProviderAccountContentUri = true; | |
break; | |
case MATCH_ACCOUNTS_BY_ID: | |
changedItemId = url.getPathSegments().get(1); | |
// fall through | |
case MATCH_ACCOUNTS: | |
tableToChange = TABLE_ACCOUNTS; | |
notifyProviderAccountContentUri = true; | |
break; | |
case MATCH_ACCOUNT_STATUS: | |
changedItemId = url.getPathSegments().get(1); | |
// fall through | |
case MATCH_ACCOUNTS_STATUS: | |
tableToChange = TABLE_ACCOUNT_STATUS; | |
notifyProviderAccountContentUri = true; | |
break; | |
case MATCH_CONTACTS: | |
case MATCH_CONTACTS_BAREBONE: | |
tableToChange = TABLE_CONTACTS; | |
contactDeleted = true; | |
break; | |
case MATCH_CONTACT: | |
tableToChange = TABLE_CONTACTS; | |
changedItemId = url.getPathSegments().get(1); | |
try { | |
deletedContactId = Long.parseLong(changedItemId); | |
} catch (NumberFormatException ex) { | |
throw new IllegalArgumentException(); | |
} | |
contactDeleted = true; | |
break; | |
case MATCH_CONTACTS_BY_PROVIDER: | |
tableToChange = TABLE_CONTACTS; | |
appendWhere(whereClause, Imps.Contacts.ACCOUNT, "=", url.getPathSegments().get(2)); | |
contactDeleted = true; | |
break; | |
case MATCH_CONTACTLISTS_BY_PROVIDER: | |
appendWhere(whereClause, Imps.ContactList.ACCOUNT, "=", | |
url.getPathSegments().get(2)); | |
// fall through | |
case MATCH_CONTACTLISTS: | |
tableToChange = TABLE_CONTACT_LIST; | |
notifyContactListContentUri = true; | |
break; | |
case MATCH_CONTACTLIST: | |
tableToChange = TABLE_CONTACT_LIST; | |
changedItemId = url.getPathSegments().get(1); | |
break; | |
case MATCH_BLOCKEDLIST: | |
tableToChange = TABLE_BLOCKED_LIST; | |
break; | |
case MATCH_BLOCKEDLIST_BY_PROVIDER: | |
tableToChange = TABLE_BLOCKED_LIST; | |
appendWhere(whereClause, Imps.BlockedList.ACCOUNT, "=", url.getPathSegments().get(2)); | |
break; | |
case MATCH_CONTACTS_ETAGS: | |
tableToChange = TABLE_CONTACTS_ETAG; | |
break; | |
case MATCH_CONTACTS_ETAG: | |
tableToChange = TABLE_CONTACTS_ETAG; | |
changedItemId = url.getPathSegments().get(1); | |
break; | |
case MATCH_MESSAGES: | |
tableToChange = TABLE_MESSAGES; | |
break; | |
case MATCH_MESSAGES_BY_CONTACT: | |
tableToChange = TABLE_MESSAGES; | |
additionalTableToChange = TABLE_IN_MEMORY_MESSAGES; | |
accountStr = decodeURLSegment(url.getPathSegments().get(1)); | |
try { | |
account = Long.parseLong(accountStr); | |
} catch (NumberFormatException ex) { | |
throw new IllegalArgumentException(); | |
} | |
contact = decodeURLSegment(url.getPathSegments().get(2)); | |
appendWhere(whereClause, Imps.Messages.THREAD_ID, "=", | |
getContactId(db, accountStr, contact)); | |
notifyMessagesContentUri = true; | |
break; | |
case MATCH_MESSAGES_BY_THREAD_ID: | |
tableToChange = TABLE_MESSAGES; | |
additionalTableToChange = TABLE_IN_MEMORY_MESSAGES; | |
try { | |
threadId = Long.parseLong(decodeURLSegment(url.getPathSegments().get(1))); | |
} catch (NumberFormatException ex) { | |
throw new IllegalArgumentException(); | |
} | |
appendWhere(whereClause, Imps.Messages.THREAD_ID, "=", threadId); | |
notifyMessagesContentUri = true; | |
break; | |
case MATCH_MESSAGES_BY_PROVIDER: | |
tableToChange = TABLE_MESSAGES; | |
provider = decodeURLSegment(url.getPathSegments().get(1)); | |
appendWhere(whereClause, buildContactIdSelection(Imps.Messages.THREAD_ID, | |
Imps.Contacts.PROVIDER + "='" + provider + "'")); | |
notifyMessagesContentUri = true; | |
break; | |
case MATCH_MESSAGES_BY_ACCOUNT: | |
tableToChange = TABLE_MESSAGES; | |
accountStr = decodeURLSegment(url.getPathSegments().get(1)); | |
appendWhere(whereClause, buildContactIdSelection(Imps.Messages.THREAD_ID, | |
Imps.Contacts.ACCOUNT + "='" + accountStr + "'")); | |
notifyMessagesContentUri = true; | |
break; | |
case MATCH_MESSAGE: | |
tableToChange = TABLE_MESSAGES; | |
changedItemId = url.getPathSegments().get(1); | |
notifyMessagesContentUri = true; | |
break; | |
case MATCH_OTR_MESSAGES: | |
tableToChange = TABLE_IN_MEMORY_MESSAGES; | |
break; | |
case MATCH_OTR_MESSAGES_BY_CONTACT: | |
tableToChange = TABLE_IN_MEMORY_MESSAGES; | |
accountStr = decodeURLSegment(url.getPathSegments().get(1)); | |
try { | |
account = Long.parseLong(accountStr); | |
} catch (NumberFormatException ex) { | |
throw new IllegalArgumentException(); | |
} | |
contact = decodeURLSegment(url.getPathSegments().get(2)); | |
appendWhere(whereClause, Imps.Messages.THREAD_ID, "=", | |
getContactId(db, accountStr, contact)); | |
notifyMessagesByContactContentUri = true; | |
break; | |
case MATCH_OTR_MESSAGES_BY_THREAD_ID: | |
tableToChange = TABLE_IN_MEMORY_MESSAGES; | |
try { | |
threadId = Long.parseLong(decodeURLSegment(url.getPathSegments().get(1))); | |
} catch (NumberFormatException ex) { | |
throw new IllegalArgumentException(); | |
} | |
appendWhere(whereClause, Imps.Messages.THREAD_ID, "=", threadId); | |
notifyMessagesByThreadIdContentUri = true; | |
break; | |
case MATCH_OTR_MESSAGES_BY_PROVIDER: | |
tableToChange = TABLE_IN_MEMORY_MESSAGES; | |
provider = decodeURLSegment(url.getPathSegments().get(1)); | |
appendWhere(whereClause, buildContactIdSelection(Imps.Messages.THREAD_ID, | |
Imps.Contacts.PROVIDER + "='" + provider + "'")); | |
if (DBG) log("delete (MATCH_OTR_MESSAGES_BY_PROVIDER) sel => " + whereClause); | |
notifyMessagesContentUri = true; | |
break; | |
case MATCH_OTR_MESSAGES_BY_ACCOUNT: | |
tableToChange = TABLE_IN_MEMORY_MESSAGES; | |
accountStr = decodeURLSegment(url.getPathSegments().get(1)); | |
appendWhere(whereClause, buildContactIdSelection(Imps.Messages.THREAD_ID, | |
Imps.Contacts.ACCOUNT + "='" + accountStr + "'")); | |
if (DBG) log("delete (MATCH_OTR_MESSAGES_BY_ACCOUNT) sel => " + whereClause); | |
notifyMessagesContentUri = true; | |
break; | |
case MATCH_OTR_MESSAGE: | |
tableToChange = TABLE_IN_MEMORY_MESSAGES; | |
changedItemId = url.getPathSegments().get(1); | |
notifyMessagesContentUri = true; | |
break; | |
case MATCH_GROUP_MEMBERS: | |
tableToChange = TABLE_GROUP_MEMBERS; | |
break; | |
case MATCH_GROUP_MEMBERS_BY_GROUP: | |
tableToChange = TABLE_GROUP_MEMBERS; | |
appendWhere(whereClause, Imps.GroupMembers.GROUP, "=", url.getPathSegments().get(1)); | |
break; | |
case MATCH_INVITATIONS: | |
tableToChange = TABLE_INVITATIONS; | |
break; | |
case MATCH_INVITATION: | |
tableToChange = TABLE_INVITATIONS; | |
changedItemId = url.getPathSegments().get(1); | |
break; | |
case MATCH_AVATARS: | |
tableToChange = TABLE_AVATARS; | |
break; | |
case MATCH_AVATAR: | |
tableToChange = TABLE_AVATARS; | |
changedItemId = url.getPathSegments().get(1); | |
break; | |
case MATCH_AVATAR_BY_PROVIDER: | |
tableToChange = TABLE_AVATARS; | |
changedItemId = url.getPathSegments().get(2); | |
idColumnName = Imps.Avatars.ACCOUNT; | |
break; | |
case MATCH_CHATS: | |
tableToChange = TABLE_CHATS; | |
backfillQuickSwitchSlots = true; | |
break; | |
case MATCH_CHATS_BY_ACCOUNT: | |
tableToChange = TABLE_CHATS; | |
accountStr = decodeURLSegment(url.getLastPathSegment()); | |
appendWhere(whereClause, buildContactIdSelection(Imps.Chats.CONTACT_ID, | |
Imps.Contacts.ACCOUNT + "='" + accountStr + "'")); | |
if (DBG) log("delete (MATCH_CHATS_BY_ACCOUNT) sel => " + whereClause); | |
changedItemId = null; | |
break; | |
case MATCH_CHATS_ID: | |
tableToChange = TABLE_CHATS; | |
changedItemId = url.getPathSegments().get(1); | |
idColumnName = Imps.Chats.CONTACT_ID; | |
break; | |
case MATCH_PRESENCE: | |
tableToChange = TABLE_PRESENCE; | |
break; | |
case MATCH_PRESENCE_ID: | |
tableToChange = TABLE_PRESENCE; | |
changedItemId = url.getPathSegments().get(1); | |
idColumnName = Imps.Presence.CONTACT_ID; | |
break; | |
case MATCH_PRESENCE_BY_ACCOUNT: | |
tableToChange = TABLE_PRESENCE; | |
accountStr = decodeURLSegment(url.getLastPathSegment()); | |
appendWhere(whereClause, buildContactIdSelection(Imps.Presence.CONTACT_ID, | |
Imps.Contacts.ACCOUNT + "='" + accountStr + "'")); | |
if (DBG) log("delete (MATCH_PRESENCE_BY_ACCOUNT): sel => " + whereClause); | |
changedItemId = null; | |
break; | |
case MATCH_SESSIONS: | |
tableToChange = TABLE_SESSION_COOKIES; | |
break; | |
case MATCH_SESSIONS_BY_PROVIDER: | |
tableToChange = TABLE_SESSION_COOKIES; | |
changedItemId = url.getPathSegments().get(2); | |
idColumnName = Imps.SessionCookies.ACCOUNT; | |
break; | |
case MATCH_PROVIDER_SETTINGS_BY_ID: | |
tableToChange = TABLE_PROVIDER_SETTINGS; | |
changedItemId = url.getPathSegments().get(1); | |
idColumnName = Imps.ProviderSettings.PROVIDER; | |
break; | |
case MATCH_PROVIDER_SETTINGS_BY_ID_AND_NAME: | |
tableToChange = TABLE_PROVIDER_SETTINGS; | |
String providerId = url.getPathSegments().get(1); | |
String name = url.getPathSegments().get(2); | |
appendWhere(whereClause, Imps.ProviderSettings.PROVIDER, "=", providerId); | |
appendWhere(whereClause, Imps.ProviderSettings.NAME, "=", name); | |
break; | |
case MATCH_BRANDING_RESOURCE_MAP_CACHE: | |
tableToChange = TABLE_BRANDING_RESOURCE_MAP_CACHE; | |
break; | |
// mcs/rmq stuff | |
case MATCH_OUTGOING_RMQ_MESSAGES: | |
tableToChange = TABLE_OUTGOING_RMQ_MESSAGES; | |
break; | |
case MATCH_LAST_RMQ_ID: | |
tableToChange = TABLE_LAST_RMQ_ID; | |
break; | |
case MATCH_S2D_RMQ_IDS: | |
tableToChange = TABLE_S2D_RMQ_IDS; | |
break; | |
default: | |
throw new UnsupportedOperationException("Cannot delete that URL: " + url); | |
} | |
return this; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private int deleteInternal(Uri url, String userWhere, String[] whereArgs) { | |
StringBuilder whereClause = getWhereClause(userWhere); | |
int match = mUrlMatcher.match(url); | |
String idColumnName = null; | |
final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); | |
int count = 0; | |
UrlMatch urlMatch = new UrlMatch(url, whereClause, match, db).invoke(); | |
idColumnName = urlMatch.getIdColumnName(); | |
idColumnName = setIdColumnNameIfNull(idColumnName); | |
appendIfChangedItemIdIsNotNull(idColumnName, urlMatch.getChangedItemId(), whereClause); | |
if (DBG) log("delete from " + url + " WHERE " + whereClause); | |
count = db.delete(urlMatch.getTableToChange(), whereClause.toString(), whereArgs); | |
count += deleteRecordsFromAdditionTableToChange(whereArgs, urlMatch.getAdditionalTableToChange(), whereClause, db); | |
if (urlMatch.isContactDeleted() && count > 0) { | |
// since the contact cleanup triggers no longer work for cross database tables, | |
// we have to do it by hand here. | |
performContactRemovalCleanup(urlMatch.getDeletedContactId()); | |
} | |
notifyOfDeletedRows(urlMatch, match, count); | |
return count; | |
} | |
private int deleteRecordsFromAdditionTableToChange(String[] whereArgs, String additionalTableToChange, StringBuilder whereClause, SQLiteDatabase db) { | |
int result = 0; | |
if (additionalTableToChange != null){ | |
result = db.delete(additionalTableToChange, whereClause.toString(), whereArgs); | |
} | |
return result; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private String setIdColumnNameIfNull(String originalIdColumnName) { | |
String result = originalIdColumnName; | |
if (result == null) { | |
result = "_id"; | |
} | |
return result; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private int deleteInternal(Uri url, String userWhere, String[] whereArgs) { | |
StringBuilder whereClause = getWhereClause(userWhere); | |
int match = mUrlMatcher.match(url); | |
String idColumnName = null; | |
final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); | |
int count = 0; | |
UrlMatch urlMatch = new UrlMatch(url, whereClause, match, db).invoke(); | |
idColumnName = urlMatch.getIdColumnName(); | |
idColumnName = setIdColumnNameIfNull(idColumnName); | |
appendIfChangedItemIdIsNotNull(idColumnName, urlMatch.getChangedItemId(), whereClause); | |
if (DBG) log("delete from " + url + " WHERE " + whereClause); | |
count = db.delete(urlMatch.getTableToChange(), whereClause.toString(), whereArgs); | |
count += deleteRecordsFromAdditionTableToChange(whereArgs, urlMatch.getAdditionalTableToChange(), whereClause, db); | |
if (urlMatch.isContactDeleted() && count > 0) { | |
// since the contact cleanup triggers no longer work for cross database tables, | |
// we have to do it by hand here. | |
performContactRemovalCleanup(urlMatch.getDeletedContactId()); | |
} | |
notifyOfDeletedRows(urlMatch, match, count); | |
return count; | |
} | |
private void notifyOfDeletedRows(UrlMatch urlMatch, int match, int count) { | |
if (count > 0) { | |
ContentResolver resolver = getContext().getContentResolver(); | |
// In most case, we query contacts with presence and chats joined, thus | |
// we should also notify that contacts changes when presence or chats changed. | |
if (match == MATCH_CHATS || match == MATCH_CHATS_ID | |
|| match == MATCH_PRESENCE || match == MATCH_PRESENCE_ID | |
|| match == MATCH_CONTACTS_BAREBONE) { | |
resolver.notifyChange(Imps.Contacts.CONTENT_URI, null); | |
} | |
if (urlMatch.isNotifyMessagesContentUri()) { | |
resolver.notifyChange(Imps.Messages.CONTENT_URI, null); | |
} | |
if (urlMatch.isNotifyMessagesByContactContentUri()) { | |
resolver.notifyChange(Imps.Messages.CONTENT_URI, null); | |
resolver.notifyChange(Imps.Messages.getContentUriByContact(urlMatch.getAccount(), urlMatch.getContact()), null); | |
} | |
if (urlMatch.isNotifyMessagesByThreadIdContentUri()) { | |
resolver.notifyChange(Imps.Messages.CONTENT_URI, null); | |
resolver.notifyChange(Imps.Messages.getContentUriByThreadId(urlMatch.getThreadId()), null); | |
} | |
if (urlMatch.isNotifyContactListContentUri()) { | |
resolver.notifyChange(Imps.ContactList.CONTENT_URI, null); | |
} | |
if (urlMatch.isNotifyProviderAccountContentUri()) { | |
if (DBG) log("notify delete for " + Imps.Provider.CONTENT_URI_WITH_ACCOUNT); | |
resolver.notifyChange(Imps.Provider.CONTENT_URI_WITH_ACCOUNT, null); | |
} | |
if (urlMatch.isBackfillQuickSwitchSlots()) { | |
backfillQuickSwitchSlots(); | |
} | |
} | |
} | |
private int deleteRecordsFromAdditionTableToChange(String[] whereArgs, String additionalTableToChange, StringBuilder whereClause, SQLiteDatabase db) { | |
int result = 0; | |
if (additionalTableToChange != null){ | |
result = db.delete(additionalTableToChange, whereClause.toString(), whereArgs); | |
} | |
return result; | |
} | |
private void appendIfChangedItemIdIsNotNull(String idColumnName, String changedItemId, StringBuilder whereClause) { | |
if (changedItemId != null) { | |
appendWhere(whereClause, idColumnName, "=", changedItemId); | |
} | |
} | |
private String setIdColumnNameIfNull(String originalIdColumnName) { | |
String result = originalIdColumnName; | |
if (result == null) { | |
result = "_id"; | |
} | |
return result; | |
} | |
private StringBuilder getWhereClause(String userWhere) { | |
StringBuilder whereClause = new StringBuilder(); | |
if(userWhere != null) { | |
whereClause.append(userWhere); | |
} | |
return whereClause; | |
} | |
private class UrlMatch { | |
private Uri url; | |
private String additionalTableToChange; | |
private String idColumnName; | |
private String changedItemId; | |
private long account; | |
private String contact; | |
private long threadId; | |
private StringBuilder whereClause; | |
private boolean notifyMessagesContentUri; | |
private boolean notifyMessagesByContactContentUri; | |
private boolean notifyMessagesByThreadIdContentUri; | |
private boolean notifyContactListContentUri; | |
private boolean notifyProviderAccountContentUri; | |
private int match; | |
private boolean contactDeleted; | |
private long deletedContactId; | |
private boolean backfillQuickSwitchSlots; | |
private SQLiteDatabase db; | |
private String tableToChange; | |
public UrlMatch(Uri url, StringBuilder whereClause, int match, SQLiteDatabase db) { | |
this.url = url; | |
this.whereClause = whereClause; | |
this.match = match; | |
this.db = db; | |
} | |
public String getTableToChange() { | |
return tableToChange; | |
} | |
public String getAdditionalTableToChange() { | |
return additionalTableToChange; | |
} | |
public String getIdColumnName() { | |
return idColumnName; | |
} | |
public String getChangedItemId() { | |
return changedItemId; | |
} | |
public long getAccount() { | |
return account; | |
} | |
public String getContact() { | |
return contact; | |
} | |
public long getThreadId() { | |
return threadId; | |
} | |
public boolean isNotifyMessagesContentUri() { | |
return notifyMessagesContentUri; | |
} | |
public boolean isNotifyMessagesByContactContentUri() { | |
return notifyMessagesByContactContentUri; | |
} | |
public boolean isNotifyMessagesByThreadIdContentUri() { | |
return notifyMessagesByThreadIdContentUri; | |
} | |
public boolean isNotifyContactListContentUri() { | |
return notifyContactListContentUri; | |
} | |
public boolean isNotifyProviderAccountContentUri() { | |
return notifyProviderAccountContentUri; | |
} | |
public boolean isContactDeleted() { | |
return contactDeleted; | |
} | |
public long getDeletedContactId() { | |
return deletedContactId; | |
} | |
public boolean isBackfillQuickSwitchSlots() { | |
return backfillQuickSwitchSlots; | |
} | |
public UrlMatch invoke() { | |
String accountStr; | |
String provider; | |
switch (match) { | |
case MATCH_PROVIDERS: | |
tableToChange = TABLE_PROVIDERS; | |
notifyProviderAccountContentUri = true; | |
break; | |
case MATCH_ACCOUNTS_BY_ID: | |
changedItemId = url.getPathSegments().get(1); | |
// fall through | |
case MATCH_ACCOUNTS: | |
tableToChange = TABLE_ACCOUNTS; | |
notifyProviderAccountContentUri = true; | |
break; | |
case MATCH_ACCOUNT_STATUS: | |
changedItemId = url.getPathSegments().get(1); | |
// fall through | |
case MATCH_ACCOUNTS_STATUS: | |
tableToChange = TABLE_ACCOUNT_STATUS; | |
notifyProviderAccountContentUri = true; | |
break; | |
case MATCH_CONTACTS: | |
case MATCH_CONTACTS_BAREBONE: | |
tableToChange = TABLE_CONTACTS; | |
contactDeleted = true; | |
break; | |
case MATCH_CONTACT: | |
tableToChange = TABLE_CONTACTS; | |
changedItemId = url.getPathSegments().get(1); | |
try { | |
deletedContactId = Long.parseLong(changedItemId); | |
} catch (NumberFormatException ex) { | |
throw new IllegalArgumentException(); | |
} | |
contactDeleted = true; | |
break; | |
case MATCH_CONTACTS_BY_PROVIDER: | |
tableToChange = TABLE_CONTACTS; | |
appendWhere(whereClause, Imps.Contacts.ACCOUNT, "=", url.getPathSegments().get(2)); | |
contactDeleted = true; | |
break; | |
case MATCH_CONTACTLISTS_BY_PROVIDER: | |
appendWhere(whereClause, Imps.ContactList.ACCOUNT, "=", | |
url.getPathSegments().get(2)); | |
// fall through | |
case MATCH_CONTACTLISTS: | |
tableToChange = TABLE_CONTACT_LIST; | |
notifyContactListContentUri = true; | |
break; | |
case MATCH_CONTACTLIST: | |
tableToChange = TABLE_CONTACT_LIST; | |
changedItemId = url.getPathSegments().get(1); | |
break; | |
case MATCH_BLOCKEDLIST: | |
tableToChange = TABLE_BLOCKED_LIST; | |
break; | |
case MATCH_BLOCKEDLIST_BY_PROVIDER: | |
tableToChange = TABLE_BLOCKED_LIST; | |
appendWhere(whereClause, Imps.BlockedList.ACCOUNT, "=", url.getPathSegments().get(2)); | |
break; | |
case MATCH_CONTACTS_ETAGS: | |
tableToChange = TABLE_CONTACTS_ETAG; | |
break; | |
case MATCH_CONTACTS_ETAG: | |
tableToChange = TABLE_CONTACTS_ETAG; | |
changedItemId = url.getPathSegments().get(1); | |
break; | |
case MATCH_MESSAGES: | |
tableToChange = TABLE_MESSAGES; | |
break; | |
case MATCH_MESSAGES_BY_CONTACT: | |
tableToChange = TABLE_MESSAGES; | |
additionalTableToChange = TABLE_IN_MEMORY_MESSAGES; | |
accountStr = decodeURLSegment(url.getPathSegments().get(1)); | |
try { | |
account = Long.parseLong(accountStr); | |
} catch (NumberFormatException ex) { | |
throw new IllegalArgumentException(); | |
} | |
contact = decodeURLSegment(url.getPathSegments().get(2)); | |
appendWhere(whereClause, Imps.Messages.THREAD_ID, "=", | |
getContactId(db, accountStr, contact)); | |
notifyMessagesContentUri = true; | |
break; | |
case MATCH_MESSAGES_BY_THREAD_ID: | |
tableToChange = TABLE_MESSAGES; | |
additionalTableToChange = TABLE_IN_MEMORY_MESSAGES; | |
try { | |
threadId = Long.parseLong(decodeURLSegment(url.getPathSegments().get(1))); | |
} catch (NumberFormatException ex) { | |
throw new IllegalArgumentException(); | |
} | |
appendWhere(whereClause, Imps.Messages.THREAD_ID, "=", threadId); | |
notifyMessagesContentUri = true; | |
break; | |
case MATCH_MESSAGES_BY_PROVIDER: | |
tableToChange = TABLE_MESSAGES; | |
provider = decodeURLSegment(url.getPathSegments().get(1)); | |
appendWhere(whereClause, buildContactIdSelection(Imps.Messages.THREAD_ID, | |
Imps.Contacts.PROVIDER + "='" + provider + "'")); | |
notifyMessagesContentUri = true; | |
break; | |
case MATCH_MESSAGES_BY_ACCOUNT: | |
tableToChange = TABLE_MESSAGES; | |
accountStr = decodeURLSegment(url.getPathSegments().get(1)); | |
appendWhere(whereClause, buildContactIdSelection(Imps.Messages.THREAD_ID, | |
Imps.Contacts.ACCOUNT + "='" + accountStr + "'")); | |
notifyMessagesContentUri = true; | |
break; | |
case MATCH_MESSAGE: | |
tableToChange = TABLE_MESSAGES; | |
changedItemId = url.getPathSegments().get(1); | |
notifyMessagesContentUri = true; | |
break; | |
case MATCH_OTR_MESSAGES: | |
tableToChange = TABLE_IN_MEMORY_MESSAGES; | |
break; | |
case MATCH_OTR_MESSAGES_BY_CONTACT: | |
tableToChange = TABLE_IN_MEMORY_MESSAGES; | |
accountStr = decodeURLSegment(url.getPathSegments().get(1)); | |
try { | |
account = Long.parseLong(accountStr); | |
} catch (NumberFormatException ex) { | |
throw new IllegalArgumentException(); | |
} | |
contact = decodeURLSegment(url.getPathSegments().get(2)); | |
appendWhere(whereClause, Imps.Messages.THREAD_ID, "=", | |
getContactId(db, accountStr, contact)); | |
notifyMessagesByContactContentUri = true; | |
break; | |
case MATCH_OTR_MESSAGES_BY_THREAD_ID: | |
tableToChange = TABLE_IN_MEMORY_MESSAGES; | |
try { | |
threadId = Long.parseLong(decodeURLSegment(url.getPathSegments().get(1))); | |
} catch (NumberFormatException ex) { | |
throw new IllegalArgumentException(); | |
} | |
appendWhere(whereClause, Imps.Messages.THREAD_ID, "=", threadId); | |
notifyMessagesByThreadIdContentUri = true; | |
break; | |
case MATCH_OTR_MESSAGES_BY_PROVIDER: | |
tableToChange = TABLE_IN_MEMORY_MESSAGES; | |
provider = decodeURLSegment(url.getPathSegments().get(1)); | |
appendWhere(whereClause, buildContactIdSelection(Imps.Messages.THREAD_ID, | |
Imps.Contacts.PROVIDER + "='" + provider + "'")); | |
if (DBG) log("delete (MATCH_OTR_MESSAGES_BY_PROVIDER) sel => " + whereClause); | |
notifyMessagesContentUri = true; | |
break; | |
case MATCH_OTR_MESSAGES_BY_ACCOUNT: | |
tableToChange = TABLE_IN_MEMORY_MESSAGES; | |
accountStr = decodeURLSegment(url.getPathSegments().get(1)); | |
appendWhere(whereClause, buildContactIdSelection(Imps.Messages.THREAD_ID, | |
Imps.Contacts.ACCOUNT + "='" + accountStr + "'")); | |
if (DBG) log("delete (MATCH_OTR_MESSAGES_BY_ACCOUNT) sel => " + whereClause); | |
notifyMessagesContentUri = true; | |
break; | |
case MATCH_OTR_MESSAGE: | |
tableToChange = TABLE_IN_MEMORY_MESSAGES; | |
changedItemId = url.getPathSegments().get(1); | |
notifyMessagesContentUri = true; | |
break; | |
case MATCH_GROUP_MEMBERS: | |
tableToChange = TABLE_GROUP_MEMBERS; | |
break; | |
case MATCH_GROUP_MEMBERS_BY_GROUP: | |
tableToChange = TABLE_GROUP_MEMBERS; | |
appendWhere(whereClause, Imps.GroupMembers.GROUP, "=", url.getPathSegments().get(1)); | |
break; | |
case MATCH_INVITATIONS: | |
tableToChange = TABLE_INVITATIONS; | |
break; | |
case MATCH_INVITATION: | |
tableToChange = TABLE_INVITATIONS; | |
changedItemId = url.getPathSegments().get(1); | |
break; | |
case MATCH_AVATARS: | |
tableToChange = TABLE_AVATARS; | |
break; | |
case MATCH_AVATAR: | |
tableToChange = TABLE_AVATARS; | |
changedItemId = url.getPathSegments().get(1); | |
break; | |
case MATCH_AVATAR_BY_PROVIDER: | |
tableToChange = TABLE_AVATARS; | |
changedItemId = url.getPathSegments().get(2); | |
idColumnName = Imps.Avatars.ACCOUNT; | |
break; | |
case MATCH_CHATS: | |
tableToChange = TABLE_CHATS; | |
backfillQuickSwitchSlots = true; | |
break; | |
case MATCH_CHATS_BY_ACCOUNT: | |
tableToChange = TABLE_CHATS; | |
accountStr = decodeURLSegment(url.getLastPathSegment()); | |
appendWhere(whereClause, buildContactIdSelection(Imps.Chats.CONTACT_ID, | |
Imps.Contacts.ACCOUNT + "='" + accountStr + "'")); | |
if (DBG) log("delete (MATCH_CHATS_BY_ACCOUNT) sel => " + whereClause); | |
changedItemId = null; | |
break; | |
case MATCH_CHATS_ID: | |
tableToChange = TABLE_CHATS; | |
changedItemId = url.getPathSegments().get(1); | |
idColumnName = Imps.Chats.CONTACT_ID; | |
break; | |
case MATCH_PRESENCE: | |
tableToChange = TABLE_PRESENCE; | |
break; | |
case MATCH_PRESENCE_ID: | |
tableToChange = TABLE_PRESENCE; | |
changedItemId = url.getPathSegments().get(1); | |
idColumnName = Imps.Presence.CONTACT_ID; | |
break; | |
case MATCH_PRESENCE_BY_ACCOUNT: | |
tableToChange = TABLE_PRESENCE; | |
accountStr = decodeURLSegment(url.getLastPathSegment()); | |
appendWhere(whereClause, buildContactIdSelection(Imps.Presence.CONTACT_ID, | |
Imps.Contacts.ACCOUNT + "='" + accountStr + "'")); | |
if (DBG) log("delete (MATCH_PRESENCE_BY_ACCOUNT): sel => " + whereClause); | |
changedItemId = null; | |
break; | |
case MATCH_SESSIONS: | |
tableToChange = TABLE_SESSION_COOKIES; | |
break; | |
case MATCH_SESSIONS_BY_PROVIDER: | |
tableToChange = TABLE_SESSION_COOKIES; | |
changedItemId = url.getPathSegments().get(2); | |
idColumnName = Imps.SessionCookies.ACCOUNT; | |
break; | |
case MATCH_PROVIDER_SETTINGS_BY_ID: | |
tableToChange = TABLE_PROVIDER_SETTINGS; | |
changedItemId = url.getPathSegments().get(1); | |
idColumnName = Imps.ProviderSettings.PROVIDER; | |
break; | |
case MATCH_PROVIDER_SETTINGS_BY_ID_AND_NAME: | |
tableToChange = TABLE_PROVIDER_SETTINGS; | |
String providerId = url.getPathSegments().get(1); | |
String name = url.getPathSegments().get(2); | |
appendWhere(whereClause, Imps.ProviderSettings.PROVIDER, "=", providerId); | |
appendWhere(whereClause, Imps.ProviderSettings.NAME, "=", name); | |
break; | |
case MATCH_BRANDING_RESOURCE_MAP_CACHE: | |
tableToChange = TABLE_BRANDING_RESOURCE_MAP_CACHE; | |
break; | |
// mcs/rmq stuff | |
case MATCH_OUTGOING_RMQ_MESSAGES: | |
tableToChange = TABLE_OUTGOING_RMQ_MESSAGES; | |
break; | |
case MATCH_LAST_RMQ_ID: | |
tableToChange = TABLE_LAST_RMQ_ID; | |
break; | |
case MATCH_S2D_RMQ_IDS: | |
tableToChange = TABLE_S2D_RMQ_IDS; | |
break; | |
default: | |
throw new UnsupportedOperationException("Cannot delete that URL: " + url); | |
} | |
return this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment