Guest User

Untitled

a guest
Apr 21st, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.41 KB | None | 0 0
  1. private int deleteInternal(Uri url, String userWhere, String[] whereArgs) {
  2. String tableToChange;
  3.  
  4. // In some cases a given url requires that we delete rows from more than one
  5. // table. The motivating example is deleting messages from both the on disk
  6. // and in memory messages tables.
  7. String tableToChange2 = null;
  8. String idColumnName = null;
  9. String changedItemId = null;
  10. String provider = null;
  11. String accountStr = null;
  12. long account = 0;
  13. String contact = null;
  14. long threadId = 0;
  15.  
  16. StringBuilder whereClause = new StringBuilder();
  17. if(userWhere != null) {
  18. whereClause.append(userWhere);
  19. }
  20.  
  21. boolean notifyMessagesContentUri = false;
  22. boolean notifyMessagesByContactContentUri = false;
  23. boolean notifyMessagesByThreadIdContentUri = false;
  24. boolean notifyContactListContentUri = false;
  25. boolean notifyProviderAccountContentUri = false;
  26. int match = mUrlMatcher.match(url);
  27.  
  28. boolean contactDeleted = false;
  29. long deletedContactId = 0;
  30.  
  31. boolean backfillQuickSwitchSlots = false;
  32.  
  33. final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
  34.  
  35. switch (match) {
  36. case MATCH_PROVIDERS:
  37. tableToChange = TABLE_PROVIDERS;
  38. notifyProviderAccountContentUri = true;
  39. break;
  40.  
  41. case MATCH_ACCOUNTS_BY_ID:
  42. changedItemId = url.getPathSegments().get(1);
  43. // fall through
  44. case MATCH_ACCOUNTS:
  45. tableToChange = TABLE_ACCOUNTS;
  46. notifyProviderAccountContentUri = true;
  47. break;
  48.  
  49. case MATCH_ACCOUNT_STATUS:
  50. changedItemId = url.getPathSegments().get(1);
  51. // fall through
  52. case MATCH_ACCOUNTS_STATUS:
  53. tableToChange = TABLE_ACCOUNT_STATUS;
  54. notifyProviderAccountContentUri = true;
  55. break;
  56.  
  57. case MATCH_CONTACTS:
  58. case MATCH_CONTACTS_BAREBONE:
  59. tableToChange = TABLE_CONTACTS;
  60. contactDeleted = true;
  61. break;
  62.  
  63. case MATCH_CONTACT:
  64. tableToChange = TABLE_CONTACTS;
  65. changedItemId = url.getPathSegments().get(1);
  66.  
  67. try {
  68. deletedContactId = Long.parseLong(changedItemId);
  69. } catch (NumberFormatException ex) {
  70. throw new IllegalArgumentException();
  71. }
  72.  
  73. contactDeleted = true;
  74. break;
  75.  
  76. case MATCH_CONTACTS_BY_PROVIDER:
  77. tableToChange = TABLE_CONTACTS;
  78. appendWhere(whereClause, Imps.Contacts.ACCOUNT, "=", url.getPathSegments().get(2));
  79. contactDeleted = true;
  80. break;
  81.  
  82. case MATCH_CONTACTLISTS_BY_PROVIDER:
  83. appendWhere(whereClause, Imps.ContactList.ACCOUNT, "=",
  84. url.getPathSegments().get(2));
  85. // fall through
  86. case MATCH_CONTACTLISTS:
  87. tableToChange = TABLE_CONTACT_LIST;
  88. notifyContactListContentUri = true;
  89. break;
  90.  
  91. case MATCH_CONTACTLIST:
  92. tableToChange = TABLE_CONTACT_LIST;
  93. changedItemId = url.getPathSegments().get(1);
  94. break;
  95.  
  96. case MATCH_BLOCKEDLIST:
  97. tableToChange = TABLE_BLOCKED_LIST;
  98. break;
  99.  
  100. case MATCH_BLOCKEDLIST_BY_PROVIDER:
  101. tableToChange = TABLE_BLOCKED_LIST;
  102. appendWhere(whereClause, Imps.BlockedList.ACCOUNT, "=", url.getPathSegments().get(2));
  103. break;
  104.  
  105. case MATCH_CONTACTS_ETAGS:
  106. tableToChange = TABLE_CONTACTS_ETAG;
  107. break;
  108.  
  109. case MATCH_CONTACTS_ETAG:
  110. tableToChange = TABLE_CONTACTS_ETAG;
  111. changedItemId = url.getPathSegments().get(1);
  112. break;
  113.  
  114. case MATCH_MESSAGES:
  115. tableToChange = TABLE_MESSAGES;
  116. break;
  117.  
  118. case MATCH_MESSAGES_BY_CONTACT:
  119. tableToChange = TABLE_MESSAGES;
  120. tableToChange2 = TABLE_IN_MEMORY_MESSAGES;
  121.  
  122. accountStr = decodeURLSegment(url.getPathSegments().get(1));
  123. try {
  124. account = Long.parseLong(accountStr);
  125. } catch (NumberFormatException ex) {
  126. throw new IllegalArgumentException();
  127. }
  128.  
  129. contact = decodeURLSegment(url.getPathSegments().get(2));
  130. appendWhere(whereClause, Imps.Messages.THREAD_ID, "=",
  131. getContactId(db, accountStr, contact));
  132.  
  133. notifyMessagesContentUri = true;
  134. break;
  135.  
  136. case MATCH_MESSAGES_BY_THREAD_ID:
  137. tableToChange = TABLE_MESSAGES;
  138. tableToChange2 = TABLE_IN_MEMORY_MESSAGES;
  139.  
  140. try {
  141. threadId = Long.parseLong(decodeURLSegment(url.getPathSegments().get(1)));
  142. } catch (NumberFormatException ex) {
  143. throw new IllegalArgumentException();
  144. }
  145.  
  146. appendWhere(whereClause, Imps.Messages.THREAD_ID, "=", threadId);
  147.  
  148. notifyMessagesContentUri = true;
  149. break;
  150.  
  151. case MATCH_MESSAGES_BY_PROVIDER:
  152. tableToChange = TABLE_MESSAGES;
  153.  
  154. provider = decodeURLSegment(url.getPathSegments().get(1));
  155. appendWhere(whereClause, buildContactIdSelection(Imps.Messages.THREAD_ID,
  156. Imps.Contacts.PROVIDER + "='" + provider + "'"));
  157.  
  158. notifyMessagesContentUri = true;
  159. break;
  160.  
  161. case MATCH_MESSAGES_BY_ACCOUNT:
  162. tableToChange = TABLE_MESSAGES;
  163.  
  164. accountStr = decodeURLSegment(url.getPathSegments().get(1));
  165. appendWhere(whereClause, buildContactIdSelection(Imps.Messages.THREAD_ID,
  166. Imps.Contacts.ACCOUNT + "='" + accountStr + "'"));
  167.  
  168. notifyMessagesContentUri = true;
  169. break;
  170.  
  171. case MATCH_MESSAGE:
  172. tableToChange = TABLE_MESSAGES;
  173. changedItemId = url.getPathSegments().get(1);
  174. notifyMessagesContentUri = true;
  175. break;
  176.  
  177. case MATCH_OTR_MESSAGES:
  178. tableToChange = TABLE_IN_MEMORY_MESSAGES;
  179. break;
  180.  
  181. case MATCH_OTR_MESSAGES_BY_CONTACT:
  182. tableToChange = TABLE_IN_MEMORY_MESSAGES;
  183.  
  184. accountStr = decodeURLSegment(url.getPathSegments().get(1));
  185. try {
  186. account = Long.parseLong(accountStr);
  187. } catch (NumberFormatException ex) {
  188. throw new IllegalArgumentException();
  189. }
  190.  
  191. contact = decodeURLSegment(url.getPathSegments().get(2));
  192. appendWhere(whereClause, Imps.Messages.THREAD_ID, "=",
  193. getContactId(db, accountStr, contact));
  194.  
  195. notifyMessagesByContactContentUri = true;
  196. break;
  197.  
  198. case MATCH_OTR_MESSAGES_BY_THREAD_ID:
  199. tableToChange = TABLE_IN_MEMORY_MESSAGES;
  200.  
  201. try {
  202. threadId = Long.parseLong(decodeURLSegment(url.getPathSegments().get(1)));
  203. } catch (NumberFormatException ex) {
  204. throw new IllegalArgumentException();
  205. }
  206.  
  207. appendWhere(whereClause, Imps.Messages.THREAD_ID, "=", threadId);
  208.  
  209. notifyMessagesByThreadIdContentUri = true;
  210. break;
  211.  
  212. case MATCH_OTR_MESSAGES_BY_PROVIDER:
  213. tableToChange = TABLE_IN_MEMORY_MESSAGES;
  214.  
  215. provider = decodeURLSegment(url.getPathSegments().get(1));
  216. appendWhere(whereClause, buildContactIdSelection(Imps.Messages.THREAD_ID,
  217. Imps.Contacts.PROVIDER + "='" + provider + "'"));
  218.  
  219. if (DBG) log("delete (MATCH_OTR_MESSAGES_BY_PROVIDER) sel => " + whereClause);
  220. notifyMessagesContentUri = true;
  221. break;
  222.  
  223. case MATCH_OTR_MESSAGES_BY_ACCOUNT:
  224. tableToChange = TABLE_IN_MEMORY_MESSAGES;
  225.  
  226. accountStr = decodeURLSegment(url.getPathSegments().get(1));
  227. appendWhere(whereClause, buildContactIdSelection(Imps.Messages.THREAD_ID,
  228. Imps.Contacts.ACCOUNT + "='" + accountStr + "'"));
  229.  
  230. if (DBG) log("delete (MATCH_OTR_MESSAGES_BY_ACCOUNT) sel => " + whereClause);
  231. notifyMessagesContentUri = true;
  232. break;
  233.  
  234. case MATCH_OTR_MESSAGE:
  235. tableToChange = TABLE_IN_MEMORY_MESSAGES;
  236. changedItemId = url.getPathSegments().get(1);
  237. notifyMessagesContentUri = true;
  238. break;
  239.  
  240. case MATCH_GROUP_MEMBERS:
  241. tableToChange = TABLE_GROUP_MEMBERS;
  242. break;
  243.  
  244. case MATCH_GROUP_MEMBERS_BY_GROUP:
  245. tableToChange = TABLE_GROUP_MEMBERS;
  246. appendWhere(whereClause, Imps.GroupMembers.GROUP, "=", url.getPathSegments().get(1));
  247. break;
  248.  
  249. case MATCH_INVITATIONS:
  250. tableToChange = TABLE_INVITATIONS;
  251. break;
  252.  
  253. case MATCH_INVITATION:
  254. tableToChange = TABLE_INVITATIONS;
  255. changedItemId = url.getPathSegments().get(1);
  256. break;
  257.  
  258. case MATCH_AVATARS:
  259. tableToChange = TABLE_AVATARS;
  260. break;
  261.  
  262. case MATCH_AVATAR:
  263. tableToChange = TABLE_AVATARS;
  264. changedItemId = url.getPathSegments().get(1);
  265. break;
  266.  
  267. case MATCH_AVATAR_BY_PROVIDER:
  268. tableToChange = TABLE_AVATARS;
  269. changedItemId = url.getPathSegments().get(2);
  270. idColumnName = Imps.Avatars.ACCOUNT;
  271. break;
  272.  
  273. case MATCH_CHATS:
  274. tableToChange = TABLE_CHATS;
  275. backfillQuickSwitchSlots = true;
  276. break;
  277.  
  278. case MATCH_CHATS_BY_ACCOUNT:
  279. tableToChange = TABLE_CHATS;
  280.  
  281. accountStr = decodeURLSegment(url.getLastPathSegment());
  282. appendWhere(whereClause, buildContactIdSelection(Imps.Chats.CONTACT_ID,
  283. Imps.Contacts.ACCOUNT + "='" + accountStr + "'"));
  284.  
  285. if (DBG) log("delete (MATCH_CHATS_BY_ACCOUNT) sel => " + whereClause);
  286.  
  287. changedItemId = null;
  288. break;
  289.  
  290. case MATCH_CHATS_ID:
  291. tableToChange = TABLE_CHATS;
  292. changedItemId = url.getPathSegments().get(1);
  293. idColumnName = Imps.Chats.CONTACT_ID;
  294. break;
  295.  
  296. case MATCH_PRESENCE:
  297. tableToChange = TABLE_PRESENCE;
  298. break;
  299.  
  300. case MATCH_PRESENCE_ID:
  301. tableToChange = TABLE_PRESENCE;
  302. changedItemId = url.getPathSegments().get(1);
  303. idColumnName = Imps.Presence.CONTACT_ID;
  304. break;
  305.  
  306. case MATCH_PRESENCE_BY_ACCOUNT:
  307. tableToChange = TABLE_PRESENCE;
  308.  
  309. accountStr = decodeURLSegment(url.getLastPathSegment());
  310. appendWhere(whereClause, buildContactIdSelection(Imps.Presence.CONTACT_ID,
  311. Imps.Contacts.ACCOUNT + "='" + accountStr + "'"));
  312.  
  313. if (DBG) log("delete (MATCH_PRESENCE_BY_ACCOUNT): sel => " + whereClause);
  314. changedItemId = null;
  315. break;
  316.  
  317. case MATCH_SESSIONS:
  318. tableToChange = TABLE_SESSION_COOKIES;
  319. break;
  320.  
  321. case MATCH_SESSIONS_BY_PROVIDER:
  322. tableToChange = TABLE_SESSION_COOKIES;
  323. changedItemId = url.getPathSegments().get(2);
  324. idColumnName = Imps.SessionCookies.ACCOUNT;
  325. break;
  326.  
  327. case MATCH_PROVIDER_SETTINGS_BY_ID:
  328. tableToChange = TABLE_PROVIDER_SETTINGS;
  329. changedItemId = url.getPathSegments().get(1);
  330. idColumnName = Imps.ProviderSettings.PROVIDER;
  331. break;
  332.  
  333. case MATCH_PROVIDER_SETTINGS_BY_ID_AND_NAME:
  334. tableToChange = TABLE_PROVIDER_SETTINGS;
  335.  
  336. String providerId = url.getPathSegments().get(1);
  337. String name = url.getPathSegments().get(2);
  338.  
  339. appendWhere(whereClause, Imps.ProviderSettings.PROVIDER, "=", providerId);
  340. appendWhere(whereClause, Imps.ProviderSettings.NAME, "=", name);
  341. break;
  342.  
  343. case MATCH_BRANDING_RESOURCE_MAP_CACHE:
  344. tableToChange = TABLE_BRANDING_RESOURCE_MAP_CACHE;
  345. break;
  346.  
  347. // mcs/rmq stuff
  348. case MATCH_OUTGOING_RMQ_MESSAGES:
  349. tableToChange = TABLE_OUTGOING_RMQ_MESSAGES;
  350. break;
  351.  
  352. case MATCH_LAST_RMQ_ID:
  353. tableToChange = TABLE_LAST_RMQ_ID;
  354. break;
  355.  
  356. case MATCH_S2D_RMQ_IDS:
  357. tableToChange = TABLE_S2D_RMQ_IDS;
  358. break;
  359.  
  360. default:
  361. throw new UnsupportedOperationException("Cannot delete that URL: " + url);
  362. }
  363.  
  364. if (idColumnName == null) {
  365. idColumnName = "_id";
  366. }
  367.  
  368. if (changedItemId != null) {
  369. appendWhere(whereClause, idColumnName, "=", changedItemId);
  370. }
  371.  
  372. if (DBG) log("delete from " + url + " WHERE " + whereClause);
  373.  
  374. int count = db.delete(tableToChange, whereClause.toString(), whereArgs);
  375.  
  376. // see the comment at the declaration of tableToChange2 for an explanation
  377. if (tableToChange2 != null){
  378. count += db.delete(tableToChange2, whereClause.toString(), whereArgs);
  379. }
  380.  
  381. if (contactDeleted && count > 0) {
  382. // since the contact cleanup triggers no longer work for cross database tables,
  383. // we have to do it by hand here.
  384. performContactRemovalCleanup(deletedContactId);
  385. }
  386.  
  387. if (count > 0) {
  388. ContentResolver resolver = getContext().getContentResolver();
  389.  
  390. // In most case, we query contacts with presence and chats joined, thus
  391. // we should also notify that contacts changes when presence or chats changed.
  392. if (match == MATCH_CHATS || match == MATCH_CHATS_ID
  393. || match == MATCH_PRESENCE || match == MATCH_PRESENCE_ID
  394. || match == MATCH_CONTACTS_BAREBONE) {
  395. resolver.notifyChange(Imps.Contacts.CONTENT_URI, null);
  396. }
  397.  
  398. if (notifyMessagesContentUri) {
  399. resolver.notifyChange(Imps.Messages.CONTENT_URI, null);
  400. }
  401.  
  402. if (notifyMessagesByContactContentUri) {
  403. resolver.notifyChange(Imps.Messages.CONTENT_URI, null);
  404. resolver.notifyChange(Imps.Messages.getContentUriByContact(account, contact), null);
  405. }
  406.  
  407. if (notifyMessagesByThreadIdContentUri) {
  408. resolver.notifyChange(Imps.Messages.CONTENT_URI, null);
  409. resolver.notifyChange(Imps.Messages.getContentUriByThreadId(threadId), null);
  410. }
  411.  
  412. if (notifyContactListContentUri) {
  413. resolver.notifyChange(Imps.ContactList.CONTENT_URI, null);
  414. }
  415.  
  416. if (notifyProviderAccountContentUri) {
  417. if (DBG) log("notify delete for " + Imps.Provider.CONTENT_URI_WITH_ACCOUNT);
  418. resolver.notifyChange(Imps.Provider.CONTENT_URI_WITH_ACCOUNT, null);
  419. }
  420.  
  421. if (backfillQuickSwitchSlots) {
  422. backfillQuickSwitchSlots();
  423. }
  424. }
  425.  
  426. return count;
  427. }
Add Comment
Please, Sign In to add comment