Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- }
Add Comment
Please, Sign In to add comment