Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * Copyright (C) 2012 Google Inc. |
| 3 | * Licensed to The Android Open Source Project. |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | *******************************************************************************/ |
| 17 | |
| 18 | package com.android.mail.browse; |
| 19 | |
Marc Blank | 48eba7a | 2012-01-27 16:16:19 -0800 | [diff] [blame] | 20 | import android.app.Activity; |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 21 | import android.content.ContentProvider; |
Marc Blank | 8d69d4e | 2012-01-25 12:04:28 -0800 | [diff] [blame] | 22 | import android.content.ContentProviderOperation; |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 23 | import android.content.ContentResolver; |
| 24 | import android.content.ContentValues; |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 25 | import android.content.Context; |
Marc Blank | 8d69d4e | 2012-01-25 12:04:28 -0800 | [diff] [blame] | 26 | import android.content.OperationApplicationException; |
Marc Blank | 48eba7a | 2012-01-27 16:16:19 -0800 | [diff] [blame] | 27 | import android.database.CharArrayBuffer; |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 28 | import android.database.ContentObserver; |
| 29 | import android.database.Cursor; |
Paul Westbrook | 0a22d44 | 2012-11-08 23:32:43 -0800 | [diff] [blame] | 30 | import android.database.CursorWrapper; |
Marc Blank | 48eba7a | 2012-01-27 16:16:19 -0800 | [diff] [blame] | 31 | import android.database.DataSetObserver; |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 32 | import android.net.Uri; |
Marc Blank | e3d3679 | 2012-04-02 09:30:14 -0700 | [diff] [blame] | 33 | import android.os.AsyncTask; |
Marc Blank | 48eba7a | 2012-01-27 16:16:19 -0800 | [diff] [blame] | 34 | import android.os.Bundle; |
Marc Blank | e77d526 | 2012-08-19 17:58:24 -0700 | [diff] [blame] | 35 | import android.os.Handler; |
Marc Blank | 8d69d4e | 2012-01-25 12:04:28 -0800 | [diff] [blame] | 36 | import android.os.Looper; |
| 37 | import android.os.RemoteException; |
mindyp | cb0b30e | 2012-11-30 10:16:35 -0800 | [diff] [blame] | 38 | import android.text.TextUtils; |
Marc Blank | 44dc449 | 2012-08-03 10:23:33 -0700 | [diff] [blame] | 39 | import android.util.Log; |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 40 | |
Marc Blank | f892f0a | 2012-01-30 13:04:10 -0800 | [diff] [blame] | 41 | import com.android.mail.providers.Conversation; |
Paul Westbrook | 26746eb | 2012-12-06 14:44:01 -0800 | [diff] [blame] | 42 | import com.android.mail.providers.Folder; |
Andy Huang | b2033d8 | 2012-12-07 19:30:57 -0800 | [diff] [blame] | 43 | import com.android.mail.providers.FolderList; |
Marc Blank | 4015c18 | 2012-01-31 12:38:36 -0800 | [diff] [blame] | 44 | import com.android.mail.providers.UIProvider; |
Marc Blank | 5114494 | 2012-03-20 13:59:32 -0700 | [diff] [blame] | 45 | import com.android.mail.providers.UIProvider.ConversationListQueryParameters; |
Paul Westbrook | 334e64a | 2012-02-23 13:26:35 -0800 | [diff] [blame] | 46 | import com.android.mail.providers.UIProvider.ConversationOperations; |
Vikram Aggarwal | 81a4f08 | 2012-09-28 09:19:04 -0700 | [diff] [blame] | 47 | import com.android.mail.ui.ConversationListFragment; |
mindyp | 5c1d835 | 2012-11-05 10:12:44 -0800 | [diff] [blame] | 48 | import com.android.mail.utils.LogTag; |
Marc Blank | 03fa19a | 2012-02-29 13:16:27 -0800 | [diff] [blame] | 49 | import com.android.mail.utils.LogUtils; |
mindyp | f74147f | 2012-11-01 11:19:15 -0700 | [diff] [blame] | 50 | import com.android.mail.utils.Utils; |
Marc Blank | 248b1b4 | 2012-02-07 13:43:02 -0800 | [diff] [blame] | 51 | import com.google.common.annotations.VisibleForTesting; |
Paul Westbrook | c8f2a3c | 2013-01-08 13:57:24 -0800 | [diff] [blame] | 52 | import com.google.common.collect.ImmutableMap; |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 53 | import com.google.common.collect.Lists; |
Paul Westbrook | fe7f5bb | 2012-09-09 17:59:35 -0700 | [diff] [blame] | 54 | import com.google.common.collect.Sets; |
Marc Blank | f892f0a | 2012-01-30 13:04:10 -0800 | [diff] [blame] | 55 | |
Marc Blank | 97bca7b | 2012-01-24 11:17:00 -0800 | [diff] [blame] | 56 | import java.util.ArrayList; |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 57 | import java.util.Arrays; |
| 58 | import java.util.Collection; |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 59 | import java.util.HashMap; |
Marc Blank | 48eba7a | 2012-01-27 16:16:19 -0800 | [diff] [blame] | 60 | import java.util.Iterator; |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 61 | import java.util.List; |
Paul Westbrook | c8f2a3c | 2013-01-08 13:57:24 -0800 | [diff] [blame] | 62 | import java.util.Map; |
Paul Westbrook | fe7f5bb | 2012-09-09 17:59:35 -0700 | [diff] [blame] | 63 | import java.util.Set; |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * ConversationCursor is a wrapper around a conversation list cursor that provides update/delete |
| 67 | * caching for quick UI response. This is effectively a singleton class, as the cache is |
| 68 | * implemented as a static HashMap. |
| 69 | */ |
Marc Blank | 48eba7a | 2012-01-27 16:16:19 -0800 | [diff] [blame] | 70 | public final class ConversationCursor implements Cursor { |
mindyp | 5c1d835 | 2012-11-05 10:12:44 -0800 | [diff] [blame] | 71 | private static final String LOG_TAG = LogTag.getLogTag(); |
Vikram Aggarwal | 8d9313b | 2013-01-03 16:15:10 -0800 | [diff] [blame] | 72 | /** Turn to true for debugging. */ |
| 73 | private static final boolean DEBUG = false; |
| 74 | /** A deleted row is indicated by the presence of DELETED_COLUMN in the cache map */ |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 75 | private static final String DELETED_COLUMN = "__deleted__"; |
Vikram Aggarwal | 8d9313b | 2013-01-03 16:15:10 -0800 | [diff] [blame] | 76 | /** An row cached during a requery is indicated by the presence of REQUERY_COLUMN in the map */ |
mindyp | 7f55c68 | 2012-10-04 11:38:27 -0700 | [diff] [blame] | 77 | private static final String UPDATE_TIME_COLUMN = "__updatetime__"; |
Vikram Aggarwal | 8d9313b | 2013-01-03 16:15:10 -0800 | [diff] [blame] | 78 | /** |
| 79 | * A sentinel value for the "index" of the deleted column; it's an int that is otherwise invalid |
| 80 | */ |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 81 | private static final int DELETED_COLUMN_INDEX = -1; |
Vikram Aggarwal | 8d9313b | 2013-01-03 16:15:10 -0800 | [diff] [blame] | 82 | /** |
| 83 | * If a cached value within 10 seconds of a refresh(), preserve it. This time has been |
| 84 | * chosen empirically (long enough for UI changes to propagate in any reasonable case) |
| 85 | */ |
mindyp | 7f55c68 | 2012-10-04 11:38:27 -0700 | [diff] [blame] | 86 | private static final long REQUERY_ALLOWANCE_TIME = 10000L; |
Vikram Aggarwal | 8d9313b | 2013-01-03 16:15:10 -0800 | [diff] [blame] | 87 | |
| 88 | /** |
| 89 | * The index of the Uri whose data is reflected in the cached row. Updates/Deletes to this Uri |
| 90 | * are cached |
| 91 | */ |
| 92 | private static final int URI_COLUMN_INDEX = UIProvider.CONVERSATION_URI_COLUMN; |
| 93 | |
Vikram Aggarwal | 7460a1c | 2012-12-28 15:58:58 -0800 | [diff] [blame] | 94 | /** The resolver for the cursor instantiator's context */ |
| 95 | private final ContentResolver mResolver; |
| 96 | |
Vikram Aggarwal | 8d9313b | 2013-01-03 16:15:10 -0800 | [diff] [blame] | 97 | /** Our sequence count (for changes sent to underlying provider) */ |
| 98 | private static int sSequence = 0; |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 99 | @VisibleForTesting |
| 100 | static ConversationProvider sProvider; |
| 101 | |
Vikram Aggarwal | 8d9313b | 2013-01-03 16:15:10 -0800 | [diff] [blame] | 102 | /** The cursor underlying the caching cursor */ |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 103 | @VisibleForTesting |
Paul Westbrook | 0a22d44 | 2012-11-08 23:32:43 -0800 | [diff] [blame] | 104 | UnderlyingCursorWrapper mUnderlyingCursor; |
Vikram Aggarwal | 8d9313b | 2013-01-03 16:15:10 -0800 | [diff] [blame] | 105 | /** The new cursor obtained via a requery */ |
Paul Westbrook | 0a22d44 | 2012-11-08 23:32:43 -0800 | [diff] [blame] | 106 | private volatile UnderlyingCursorWrapper mRequeryCursor; |
Vikram Aggarwal | 8d9313b | 2013-01-03 16:15:10 -0800 | [diff] [blame] | 107 | /** A mapping from Uri to updated ContentValues */ |
| 108 | private final HashMap<String, ContentValues> mCacheMap = new HashMap<String, ContentValues>(); |
| 109 | /** Cache map lock (will be used only very briefly - few ms at most) */ |
| 110 | private final Object mCacheMapLock = new Object(); |
| 111 | /** The listeners registered for this cursor */ |
| 112 | private final List<ConversationListener> mListeners = Lists.newArrayList(); |
| 113 | /** |
| 114 | * The ConversationProvider instance // The runnable executing a refresh (query of underlying |
| 115 | * provider) |
| 116 | */ |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 117 | private RefreshTask mRefreshTask; |
Vikram Aggarwal | 8d9313b | 2013-01-03 16:15:10 -0800 | [diff] [blame] | 118 | /** Set when we've sent refreshReady() to listeners */ |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 119 | private boolean mRefreshReady = false; |
Vikram Aggarwal | 8d9313b | 2013-01-03 16:15:10 -0800 | [diff] [blame] | 120 | /** Set when we've sent refreshRequired() to listeners */ |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 121 | private boolean mRefreshRequired = false; |
Vikram Aggarwal | 8d9313b | 2013-01-03 16:15:10 -0800 | [diff] [blame] | 122 | /** Whether our first query on this cursor should include a limit */ |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 123 | private boolean mInitialConversationLimit = false; |
Vikram Aggarwal | 8d9313b | 2013-01-03 16:15:10 -0800 | [diff] [blame] | 124 | /** A list of mostly-dead items */ |
| 125 | private final List<Conversation> mMostlyDead = Lists.newArrayList(); |
| 126 | /** The name of the loader */ |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 127 | private final String mName; |
Vikram Aggarwal | 8d9313b | 2013-01-03 16:15:10 -0800 | [diff] [blame] | 128 | /** Column names for this cursor */ |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 129 | private String[] mColumnNames; |
Vikram Aggarwal | 8d9313b | 2013-01-03 16:15:10 -0800 | [diff] [blame] | 130 | /** An observer on the underlying cursor (so we can detect changes from outside the UI) */ |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 131 | private final CursorObserver mCursorObserver; |
Vikram Aggarwal | 8d9313b | 2013-01-03 16:15:10 -0800 | [diff] [blame] | 132 | /** Whether our observer is currently registered with the underlying cursor */ |
Marc Blank | 97bca7b | 2012-01-24 11:17:00 -0800 | [diff] [blame] | 133 | private boolean mCursorObserverRegistered = false; |
Vikram Aggarwal | 8d9313b | 2013-01-03 16:15:10 -0800 | [diff] [blame] | 134 | /** Whether our loader is paused */ |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 135 | private boolean mPaused = false; |
Vikram Aggarwal | 8d9313b | 2013-01-03 16:15:10 -0800 | [diff] [blame] | 136 | /** Whether or not sync from underlying provider should be deferred */ |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 137 | private boolean mDeferSync = false; |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 138 | |
Vikram Aggarwal | 8d9313b | 2013-01-03 16:15:10 -0800 | [diff] [blame] | 139 | /** The current position of the cursor */ |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 140 | private int mPosition = -1; |
Andy Huang | 397621b | 2012-03-14 20:52:39 -0700 | [diff] [blame] | 141 | |
Vikram Aggarwal | 8d9313b | 2013-01-03 16:15:10 -0800 | [diff] [blame] | 142 | /** |
| 143 | * The number of cached deletions from this cursor (used to quickly generate an accurate count) |
| 144 | */ |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 145 | private int mDeletedCount = 0; |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 146 | |
Vikram Aggarwal | 8d9313b | 2013-01-03 16:15:10 -0800 | [diff] [blame] | 147 | /** Parameters passed to the underlying query */ |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 148 | private Uri qUri; |
| 149 | private String[] qProjection; |
Marc Blank | 48eba7a | 2012-01-27 16:16:19 -0800 | [diff] [blame] | 150 | |
Paul Westbrook | 0a22d44 | 2012-11-08 23:32:43 -0800 | [diff] [blame] | 151 | private void setCursor(UnderlyingCursorWrapper cursor) { |
Marc Blank | c16be93 | 2012-02-24 12:43:48 -0800 | [diff] [blame] | 152 | // If we have an existing underlying cursor, make sure it's closed |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 153 | if (mUnderlyingCursor != null) { |
Paul Westbrook | 04e0dc7 | 2012-08-01 17:34:48 -0700 | [diff] [blame] | 154 | close(); |
Marc Blank | c16be93 | 2012-02-24 12:43:48 -0800 | [diff] [blame] | 155 | } |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 156 | mColumnNames = cursor.getColumnNames(); |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 157 | mRefreshRequired = false; |
| 158 | mRefreshReady = false; |
| 159 | mRefreshTask = null; |
| 160 | resetCursor(cursor); |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 161 | } |
| 162 | |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 163 | public ConversationCursor(Activity activity, Uri uri, boolean initialConversationLimit, |
| 164 | String name) { |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 165 | mInitialConversationLimit = initialConversationLimit; |
Vikram Aggarwal | 7460a1c | 2012-12-28 15:58:58 -0800 | [diff] [blame] | 166 | mResolver = activity.getApplicationContext().getContentResolver(); |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 167 | qUri = uri; |
| 168 | mName = name; |
| 169 | qProjection = UIProvider.CONVERSATION_PROJECTION; |
Marc Blank | e77d526 | 2012-08-19 17:58:24 -0700 | [diff] [blame] | 170 | mCursorObserver = new CursorObserver(new Handler(Looper.getMainLooper())); |
Paul Westbrook | ff5c757 | 2012-03-16 13:43:18 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | /** |
Marc Blank | 48eba7a | 2012-01-27 16:16:19 -0800 | [diff] [blame] | 174 | * Create a ConversationCursor; this should be called by the ListActivity using that cursor |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 175 | */ |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 176 | public void load() { |
| 177 | synchronized (mCacheMapLock) { |
Marc Blank | 5114494 | 2012-03-20 13:59:32 -0700 | [diff] [blame] | 178 | try { |
Marc Blank | 5114494 | 2012-03-20 13:59:32 -0700 | [diff] [blame] | 179 | // Create new ConversationCursor |
mindyp | 5c1d835 | 2012-11-05 10:12:44 -0800 | [diff] [blame] | 180 | LogUtils.i(LOG_TAG, "Create: initial creation"); |
mindyp | 7f55c68 | 2012-10-04 11:38:27 -0700 | [diff] [blame] | 181 | setCursor(doQuery(mInitialConversationLimit)); |
Marc Blank | 5114494 | 2012-03-20 13:59:32 -0700 | [diff] [blame] | 182 | } finally { |
| 183 | // If we used a limit, queue up a query without limit |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 184 | if (mInitialConversationLimit) { |
| 185 | mInitialConversationLimit = false; |
| 186 | refresh(); |
Marc Blank | 5114494 | 2012-03-20 13:59:32 -0700 | [diff] [blame] | 187 | } |
Marc Blank | c16be93 | 2012-02-24 12:43:48 -0800 | [diff] [blame] | 188 | } |
| 189 | } |
Marc Blank | 948985b | 2012-02-29 11:26:40 -0800 | [diff] [blame] | 190 | } |
| 191 | |
Marc Blank | 6ca57e8 | 2012-03-20 19:09:12 -0700 | [diff] [blame] | 192 | /** |
Marc Blank | e1d1b07 | 2012-04-13 17:29:16 -0700 | [diff] [blame] | 193 | * Pause notifications to UI |
| 194 | */ |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 195 | public void pause() { |
Marc Blank | e1d1b07 | 2012-04-13 17:29:16 -0700 | [diff] [blame] | 196 | if (DEBUG) { |
mindyp | 5c1d835 | 2012-11-05 10:12:44 -0800 | [diff] [blame] | 197 | LogUtils.i(LOG_TAG, "[Paused: %s]", mName); |
Marc Blank | e1d1b07 | 2012-04-13 17:29:16 -0700 | [diff] [blame] | 198 | } |
Marc Blank | 1c39117 | 2012-04-23 09:23:17 -0700 | [diff] [blame] | 199 | mPaused = true; |
Marc Blank | e1d1b07 | 2012-04-13 17:29:16 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Resume notifications to UI; if any are pending, send them |
| 204 | */ |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 205 | public void resume() { |
Marc Blank | e1d1b07 | 2012-04-13 17:29:16 -0700 | [diff] [blame] | 206 | if (DEBUG) { |
mindyp | 5c1d835 | 2012-11-05 10:12:44 -0800 | [diff] [blame] | 207 | LogUtils.i(LOG_TAG, "[Resumed: %s]", mName); |
Marc Blank | e1d1b07 | 2012-04-13 17:29:16 -0700 | [diff] [blame] | 208 | } |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 209 | mPaused = false; |
Marc Blank | e1d1b07 | 2012-04-13 17:29:16 -0700 | [diff] [blame] | 210 | checkNotifyUI(); |
| 211 | } |
| 212 | |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 213 | private void checkNotifyUI() { |
mindyp | 5c1d835 | 2012-11-05 10:12:44 -0800 | [diff] [blame] | 214 | LogUtils.d( |
| 215 | LOG_TAG, |
| 216 | "Received notify ui callback and sending a notification is enabled?" + |
| 217 | " %s and refresh ready ? %s", |
| 218 | (!mPaused && !mDeferSync), |
| 219 | (mRefreshReady || (mRefreshRequired && mRefreshTask == null))); |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 220 | if (!mPaused && !mDeferSync) { |
| 221 | if (mRefreshRequired && (mRefreshTask == null)) { |
| 222 | notifyRefreshRequired(); |
| 223 | } else if (mRefreshReady) { |
| 224 | notifyRefreshReady(); |
Marc Blank | e1d1b07 | 2012-04-13 17:29:16 -0700 | [diff] [blame] | 225 | } |
| 226 | } else { |
mindyp | 5c1d835 | 2012-11-05 10:12:44 -0800 | [diff] [blame] | 227 | LogUtils.i(LOG_TAG, "[checkNotifyUI: %s%s", |
Paul Westbrook | e2bde3a | 2012-07-31 17:35:43 -0700 | [diff] [blame] | 228 | (mPaused ? "Paused " : ""), (mDeferSync ? "Defer" : "")); |
Marc Blank | e1d1b07 | 2012-04-13 17:29:16 -0700 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | |
Paul Westbrook | 0a22d44 | 2012-11-08 23:32:43 -0800 | [diff] [blame] | 232 | public Set<Long> getConversationIds() { |
| 233 | return mUnderlyingCursor != null ? mUnderlyingCursor.conversationIds() : null; |
| 234 | } |
| 235 | |
Marc Blank | e1d1b07 | 2012-04-13 17:29:16 -0700 | [diff] [blame] | 236 | /** |
mindyp | f74147f | 2012-11-01 11:19:15 -0700 | [diff] [blame] | 237 | * Simple wrapper for a cursor that provides methods for quickly determining |
| 238 | * the existence of a row. |
| 239 | */ |
Paul Westbrook | 0a22d44 | 2012-11-08 23:32:43 -0800 | [diff] [blame] | 240 | private class UnderlyingCursorWrapper extends CursorWrapper { |
| 241 | // Ideally these two objects could be combined into a Map from |
| 242 | // conversationId -> position, but the cached values uses the conversation |
| 243 | // uri as a key. |
Paul Westbrook | c8f2a3c | 2013-01-08 13:57:24 -0800 | [diff] [blame] | 244 | private final Map<String, Integer> mConversationUriPositionMap; |
| 245 | private final Map<Long, Integer> mConversationIdPositionMap; |
mindyp | f74147f | 2012-11-01 11:19:15 -0700 | [diff] [blame] | 246 | |
Paul Westbrook | 0a22d44 | 2012-11-08 23:32:43 -0800 | [diff] [blame] | 247 | public UnderlyingCursorWrapper(Cursor result) { |
| 248 | super(result); |
Paul Westbrook | c8f2a3c | 2013-01-08 13:57:24 -0800 | [diff] [blame] | 249 | final ImmutableMap.Builder<String, Integer> conversationUriPositionMapBuilder = |
| 250 | new ImmutableMap.Builder<String, Integer>(); |
| 251 | final ImmutableMap.Builder<Long, Integer> conversationIdPositionMapBuilder = |
| 252 | new ImmutableMap.Builder<Long, Integer>(); |
Paul Westbrook | 0a22d44 | 2012-11-08 23:32:43 -0800 | [diff] [blame] | 253 | if (result != null && result.moveToFirst()) { |
| 254 | // We don't want iterating over this cursor to trigger a network |
mindyp | f74147f | 2012-11-01 11:19:15 -0700 | [diff] [blame] | 255 | // request |
Paul Westbrook | 0a22d44 | 2012-11-08 23:32:43 -0800 | [diff] [blame] | 256 | final boolean networkWasEnabled = |
| 257 | Utils.disableConversationCursorNetworkAccess(result); |
mindyp | f74147f | 2012-11-01 11:19:15 -0700 | [diff] [blame] | 258 | do { |
Paul Westbrook | c8f2a3c | 2013-01-08 13:57:24 -0800 | [diff] [blame] | 259 | final int position = result.getPosition(); |
| 260 | conversationUriPositionMapBuilder.put( |
| 261 | result.getString(URI_COLUMN_INDEX), position); |
| 262 | conversationIdPositionMapBuilder.put( |
| 263 | result.getLong(UIProvider.CONVERSATION_ID_COLUMN), position); |
Paul Westbrook | 0a22d44 | 2012-11-08 23:32:43 -0800 | [diff] [blame] | 264 | } while (result.moveToNext()); |
mindyp | f74147f | 2012-11-01 11:19:15 -0700 | [diff] [blame] | 265 | |
| 266 | if (networkWasEnabled) { |
Paul Westbrook | 0a22d44 | 2012-11-08 23:32:43 -0800 | [diff] [blame] | 267 | Utils.enableConversationCursorNetworkAccess(result); |
mindyp | f74147f | 2012-11-01 11:19:15 -0700 | [diff] [blame] | 268 | } |
| 269 | } |
Paul Westbrook | c8f2a3c | 2013-01-08 13:57:24 -0800 | [diff] [blame] | 270 | mConversationUriPositionMap = conversationUriPositionMapBuilder.build(); |
| 271 | mConversationIdPositionMap = conversationIdPositionMapBuilder.build(); |
mindyp | f74147f | 2012-11-01 11:19:15 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | public boolean contains(String uri) { |
Paul Westbrook | c8f2a3c | 2013-01-08 13:57:24 -0800 | [diff] [blame] | 275 | return mConversationUriPositionMap.containsKey(uri); |
mindyp | f74147f | 2012-11-01 11:19:15 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Paul Westbrook | 0a22d44 | 2012-11-08 23:32:43 -0800 | [diff] [blame] | 278 | public Set<Long> conversationIds() { |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame^] | 279 | return mConversationIdPositionMap.keySet(); |
Paul Westbrook | c8f2a3c | 2013-01-08 13:57:24 -0800 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | public int getPosition(long conversationId) { |
| 283 | final Integer position = mConversationIdPositionMap.get(conversationId); |
| 284 | return position != null ? position.intValue() : -1; |
| 285 | } |
| 286 | |
| 287 | public int getPosition(String conversationUri) { |
| 288 | final Integer position = mConversationUriPositionMap.get(conversationUri); |
| 289 | return position != null ? position.intValue() : -1; |
mindyp | f74147f | 2012-11-01 11:19:15 -0700 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | |
| 293 | /** |
Marc Blank | e3d3679 | 2012-04-02 09:30:14 -0700 | [diff] [blame] | 294 | * Runnable that performs the query on the underlying provider |
| 295 | */ |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 296 | private class RefreshTask extends AsyncTask<Void, Void, Void> { |
Paul Westbrook | 0a22d44 | 2012-11-08 23:32:43 -0800 | [diff] [blame] | 297 | private UnderlyingCursorWrapper mCursor = null; |
Marc Blank | e3d3679 | 2012-04-02 09:30:14 -0700 | [diff] [blame] | 298 | |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 299 | private RefreshTask() { |
Marc Blank | e3d3679 | 2012-04-02 09:30:14 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | @Override |
| 303 | protected Void doInBackground(Void... params) { |
| 304 | if (DEBUG) { |
mindyp | 5c1d835 | 2012-11-05 10:12:44 -0800 | [diff] [blame] | 305 | LogUtils.i(LOG_TAG, "[Start refresh of %s: %d]", mName, hashCode()); |
Marc Blank | e3d3679 | 2012-04-02 09:30:14 -0700 | [diff] [blame] | 306 | } |
| 307 | // Get new data |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 308 | mCursor = doQuery(false); |
Marc Blank | 5ef5e0f | 2012-09-16 10:31:51 -0700 | [diff] [blame] | 309 | // Make sure window is full |
| 310 | mCursor.getCount(); |
Marc Blank | e3d3679 | 2012-04-02 09:30:14 -0700 | [diff] [blame] | 311 | return null; |
| 312 | } |
| 313 | |
| 314 | @Override |
| 315 | protected void onPostExecute(Void param) { |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 316 | synchronized(mCacheMapLock) { |
mindyp | 5c1d835 | 2012-11-05 10:12:44 -0800 | [diff] [blame] | 317 | LogUtils.d( |
| 318 | LOG_TAG, |
| 319 | "Received notify ui callback and sending a notification is enabled? %s", |
| 320 | (!mPaused && !mDeferSync)); |
Marc Blank | e602ae1 | 2012-08-22 10:02:40 -0700 | [diff] [blame] | 321 | // If cursor got closed (e.g. reset loader) in the meantime, cancel the refresh |
| 322 | if (isClosed()) { |
| 323 | onCancelled(); |
| 324 | return; |
| 325 | } |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 326 | mRequeryCursor = mCursor; |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 327 | mRefreshReady = true; |
Marc Blank | e3d3679 | 2012-04-02 09:30:14 -0700 | [diff] [blame] | 328 | if (DEBUG) { |
mindyp | 5c1d835 | 2012-11-05 10:12:44 -0800 | [diff] [blame] | 329 | LogUtils.i(LOG_TAG, "[Query done %s: %d]", mName, hashCode()); |
Marc Blank | e3d3679 | 2012-04-02 09:30:14 -0700 | [diff] [blame] | 330 | } |
Marc Blank | 1c39117 | 2012-04-23 09:23:17 -0700 | [diff] [blame] | 331 | if (!mDeferSync && !mPaused) { |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 332 | notifyRefreshReady(); |
Marc Blank | 1c39117 | 2012-04-23 09:23:17 -0700 | [diff] [blame] | 333 | } |
Marc Blank | e3d3679 | 2012-04-02 09:30:14 -0700 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | |
| 337 | @Override |
| 338 | protected void onCancelled() { |
| 339 | if (DEBUG) { |
mindyp | 5c1d835 | 2012-11-05 10:12:44 -0800 | [diff] [blame] | 340 | LogUtils.i(LOG_TAG, "[Ignoring refresh result: %d]", hashCode()); |
Marc Blank | e3d3679 | 2012-04-02 09:30:14 -0700 | [diff] [blame] | 341 | } |
| 342 | if (mCursor != null) { |
| 343 | mCursor.close(); |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | |
Paul Westbrook | 0a22d44 | 2012-11-08 23:32:43 -0800 | [diff] [blame] | 348 | private UnderlyingCursorWrapper doQuery(boolean withLimit) { |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 349 | Uri uri = qUri; |
Marc Blank | 5114494 | 2012-03-20 13:59:32 -0700 | [diff] [blame] | 350 | if (withLimit) { |
| 351 | uri = uri.buildUpon().appendQueryParameter(ConversationListQueryParameters.LIMIT, |
| 352 | ConversationListQueryParameters.DEFAULT_LIMIT).build(); |
| 353 | } |
| 354 | long time = System.currentTimeMillis(); |
Paul Westbrook | dac6580 | 2012-03-23 16:59:15 -0700 | [diff] [blame] | 355 | |
Vikram Aggarwal | 7460a1c | 2012-12-28 15:58:58 -0800 | [diff] [blame] | 356 | final Cursor result = mResolver.query(uri, qProjection, null, null, null); |
mindyp | 7f55c68 | 2012-10-04 11:38:27 -0700 | [diff] [blame] | 357 | if (result == null) { |
mindyp | 5c1d835 | 2012-11-05 10:12:44 -0800 | [diff] [blame] | 358 | Log.w(LOG_TAG, "doQuery returning null cursor, uri: " + uri); |
Marc Blank | 44dc449 | 2012-08-03 10:23:33 -0700 | [diff] [blame] | 359 | } else if (DEBUG) { |
Marc Blank | 5114494 | 2012-03-20 13:59:32 -0700 | [diff] [blame] | 360 | time = System.currentTimeMillis() - time; |
mindyp | 5c1d835 | 2012-11-05 10:12:44 -0800 | [diff] [blame] | 361 | LogUtils.i(LOG_TAG, "ConversationCursor query: %s, %dms, %d results", |
Paul Westbrook | dac6580 | 2012-03-23 16:59:15 -0700 | [diff] [blame] | 362 | uri, time, result.getCount()); |
Marc Blank | 5114494 | 2012-03-20 13:59:32 -0700 | [diff] [blame] | 363 | } |
Paul Westbrook | 0a22d44 | 2012-11-08 23:32:43 -0800 | [diff] [blame] | 364 | return new UnderlyingCursorWrapper(result); |
Marc Blank | 48eba7a | 2012-01-27 16:16:19 -0800 | [diff] [blame] | 365 | } |
| 366 | |
Marc Blank | 3232a96 | 2012-03-08 15:32:37 -0800 | [diff] [blame] | 367 | static boolean offUiThread() { |
| 368 | return Looper.getMainLooper().getThread() != Thread.currentThread(); |
| 369 | } |
| 370 | |
Marc Blank | 48eba7a | 2012-01-27 16:16:19 -0800 | [diff] [blame] | 371 | /** |
| 372 | * Reset the cursor; this involves clearing out our cache map and resetting our various counts |
| 373 | * The cursor should be reset whenever we get fresh data from the underlying cursor. The cache |
| 374 | * is locked during the reset, which will block the UI, but for only a very short time |
| 375 | * (estimated at a few ms, but we can profile this; remember that the cache will usually |
| 376 | * be empty or have a few entries) |
| 377 | */ |
Paul Westbrook | 0a22d44 | 2012-11-08 23:32:43 -0800 | [diff] [blame] | 378 | private void resetCursor(UnderlyingCursorWrapper newCursorWrapper) { |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 379 | synchronized (mCacheMapLock) { |
mindyp | 7f55c68 | 2012-10-04 11:38:27 -0700 | [diff] [blame] | 380 | // Walk through the cache |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame^] | 381 | final Iterator<Map.Entry<String, ContentValues>> iter = |
Paul Westbrook | 4de145b | 2012-12-19 16:43:25 -0800 | [diff] [blame] | 382 | mCacheMap.entrySet().iterator(); |
Paul Westbrook | d9e49da | 2012-10-26 19:17:10 -0700 | [diff] [blame] | 383 | final long now = System.currentTimeMillis(); |
Marc Blank | 48eba7a | 2012-01-27 16:16:19 -0800 | [diff] [blame] | 384 | while (iter.hasNext()) { |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame^] | 385 | Map.Entry<String, ContentValues> entry = iter.next(); |
Paul Westbrook | d9e49da | 2012-10-26 19:17:10 -0700 | [diff] [blame] | 386 | final ContentValues values = entry.getValue(); |
| 387 | final String key = entry.getKey(); |
mindyp | f74147f | 2012-11-01 11:19:15 -0700 | [diff] [blame] | 388 | boolean withinTimeWindow = false; |
| 389 | boolean removed = false; |
Paul Westbrook | d9e49da | 2012-10-26 19:17:10 -0700 | [diff] [blame] | 390 | if (values != null) { |
| 391 | Long updateTime = values.getAsLong(UPDATE_TIME_COLUMN); |
| 392 | if (updateTime != null && ((now - updateTime) < REQUERY_ALLOWANCE_TIME)) { |
mindyp | 5c1d835 | 2012-11-05 10:12:44 -0800 | [diff] [blame] | 393 | LogUtils.i(LOG_TAG, "IN resetCursor, keep recent changes to %s", key); |
mindyp | f74147f | 2012-11-01 11:19:15 -0700 | [diff] [blame] | 394 | withinTimeWindow = true; |
Paul Westbrook | d9e49da | 2012-10-26 19:17:10 -0700 | [diff] [blame] | 395 | } else if (updateTime == null) { |
mindyp | 5c1d835 | 2012-11-05 10:12:44 -0800 | [diff] [blame] | 396 | LogUtils.e(LOG_TAG, "null updateTime from mCacheMap for key: %s", key); |
Paul Westbrook | d9e49da | 2012-10-26 19:17:10 -0700 | [diff] [blame] | 397 | } |
| 398 | if (values.containsKey(DELETED_COLUMN)) { |
mindyp | f74147f | 2012-11-01 11:19:15 -0700 | [diff] [blame] | 399 | // Item is deleted locally AND deleted in the new cursor. |
| 400 | if (!newCursorWrapper.contains(key)) { |
| 401 | // Keep the deleted count up-to-date; remove the |
| 402 | // cache entry |
| 403 | mDeletedCount--; |
| 404 | removed = true; |
mindyp | 5c1d835 | 2012-11-05 10:12:44 -0800 | [diff] [blame] | 405 | LogUtils.i(LOG_TAG, |
mindyp | f74147f | 2012-11-01 11:19:15 -0700 | [diff] [blame] | 406 | "IN resetCursor, sDeletedCount decremented to: %d by %s", |
| 407 | mDeletedCount, key); |
| 408 | } |
Paul Westbrook | d9e49da | 2012-10-26 19:17:10 -0700 | [diff] [blame] | 409 | } |
| 410 | } else { |
mindyp | 5c1d835 | 2012-11-05 10:12:44 -0800 | [diff] [blame] | 411 | LogUtils.e(LOG_TAG, "null ContentValues from mCacheMap for key: %s", key); |
mindyp | 7f55c68 | 2012-10-04 11:38:27 -0700 | [diff] [blame] | 412 | } |
mindyp | f74147f | 2012-11-01 11:19:15 -0700 | [diff] [blame] | 413 | // Remove the entry if it was time for an update or the item was deleted by the user. |
| 414 | if (!withinTimeWindow || removed) { |
| 415 | iter.remove(); |
| 416 | } |
Marc Blank | 48eba7a | 2012-01-27 16:16:19 -0800 | [diff] [blame] | 417 | } |
Paul Westbrook | c33ec4c | 2012-08-09 01:47:25 -0700 | [diff] [blame] | 418 | |
Marc Blank | 28cb53d | 2012-08-19 17:58:24 -0700 | [diff] [blame] | 419 | // Swap cursor |
| 420 | if (mUnderlyingCursor != null) { |
| 421 | close(); |
| 422 | } |
Paul Westbrook | 0a22d44 | 2012-11-08 23:32:43 -0800 | [diff] [blame] | 423 | mUnderlyingCursor = newCursorWrapper; |
Paul Westbrook | c33ec4c | 2012-08-09 01:47:25 -0700 | [diff] [blame] | 424 | |
Marc Blank | 28cb53d | 2012-08-19 17:58:24 -0700 | [diff] [blame] | 425 | mPosition = -1; |
| 426 | mUnderlyingCursor.moveToPosition(mPosition); |
| 427 | if (!mCursorObserverRegistered) { |
| 428 | mUnderlyingCursor.registerContentObserver(mCursorObserver); |
| 429 | mCursorObserverRegistered = true; |
| 430 | } |
| 431 | mRefreshRequired = false; |
Paul Westbrook | c33ec4c | 2012-08-09 01:47:25 -0700 | [diff] [blame] | 432 | } |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | /** |
Paul Westbrook | fe7f5bb | 2012-09-09 17:59:35 -0700 | [diff] [blame] | 436 | * Returns the conversation uris for the Conversations that the ConversationCursor is treating |
| 437 | * as deleted. This is an optimization to allow clients to determine if an item has been |
| 438 | * removed, without having to iterate through the whole cursor |
| 439 | */ |
| 440 | public Set<String> getDeletedItems() { |
| 441 | synchronized (mCacheMapLock) { |
| 442 | // Walk through the cache and return the list of uris that have been deleted |
| 443 | final Set<String> deletedItems = Sets.newHashSet(); |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame^] | 444 | final Iterator<Map.Entry<String, ContentValues>> iter = |
Paul Westbrook | fe7f5bb | 2012-09-09 17:59:35 -0700 | [diff] [blame] | 445 | mCacheMap.entrySet().iterator(); |
| 446 | while (iter.hasNext()) { |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame^] | 447 | final Map.Entry<String, ContentValues> entry = iter.next(); |
Paul Westbrook | fe7f5bb | 2012-09-09 17:59:35 -0700 | [diff] [blame] | 448 | final ContentValues values = entry.getValue(); |
| 449 | if (values.containsKey(DELETED_COLUMN)) { |
| 450 | // Since clients of the conversation cursor see conversation ConversationCursor |
| 451 | // provider uris, we need to make sure that this also returns these uris |
| 452 | final Uri conversationUri = Uri.parse(entry.getKey()); |
| 453 | deletedItems.add(uriToCachingUriString(conversationUri)) ; |
| 454 | } |
| 455 | } |
| 456 | return deletedItems; |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | /** |
Paul Westbrook | c8f2a3c | 2013-01-08 13:57:24 -0800 | [diff] [blame] | 461 | * Returns the position, in the ConversationCursor, of the Conversation with the specified id. |
| 462 | * The returned posision will take into account any items that have been deleted. |
| 463 | */ |
| 464 | public int getConversationPosition(long conversationId) { |
| 465 | final int underlyingPosition = mUnderlyingCursor.getPosition(conversationId); |
| 466 | if (underlyingPosition < 0) { |
| 467 | // The conversation wasn't found in the underlying cursor, return the underlying result. |
| 468 | return underlyingPosition; |
| 469 | } |
| 470 | |
| 471 | // Walk through each of the deleted items. If the deleted item is before the underlying |
| 472 | // position, decrement the position |
| 473 | synchronized (mCacheMapLock) { |
| 474 | int updatedPosition = underlyingPosition; |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame^] | 475 | final Iterator<Map.Entry<String, ContentValues>> iter = |
Paul Westbrook | c8f2a3c | 2013-01-08 13:57:24 -0800 | [diff] [blame] | 476 | mCacheMap.entrySet().iterator(); |
| 477 | while (iter.hasNext()) { |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame^] | 478 | final Map.Entry<String, ContentValues> entry = iter.next(); |
Paul Westbrook | c8f2a3c | 2013-01-08 13:57:24 -0800 | [diff] [blame] | 479 | final ContentValues values = entry.getValue(); |
| 480 | if (values.containsKey(DELETED_COLUMN)) { |
| 481 | // Since clients of the conversation cursor see conversation ConversationCursor |
| 482 | // provider uris, we need to make sure that this also returns these uris |
| 483 | final String conversationUri = entry.getKey(); |
| 484 | final int deletedItemPosition = mUnderlyingCursor.getPosition(conversationUri); |
| 485 | if (deletedItemPosition == underlyingPosition) { |
| 486 | // The requested items has been deleted. |
| 487 | return -1; |
| 488 | } |
| 489 | |
| 490 | if (deletedItemPosition >= 0 && deletedItemPosition < underlyingPosition) { |
| 491 | // This item has been deleted, but is still in the underlying cursor, at |
| 492 | // a position before the requested item. Decrement the position of the |
| 493 | // requested item. |
| 494 | updatedPosition--; |
| 495 | } |
| 496 | } |
| 497 | } |
| 498 | return updatedPosition; |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | /** |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 503 | * Add a listener for this cursor; we'll notify it when our data changes |
| 504 | */ |
| 505 | public void addListener(ConversationListener listener) { |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 506 | synchronized (mListeners) { |
| 507 | if (!mListeners.contains(listener)) { |
| 508 | mListeners.add(listener); |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 509 | } else { |
mindyp | 5c1d835 | 2012-11-05 10:12:44 -0800 | [diff] [blame] | 510 | LogUtils.i(LOG_TAG, "Ignoring duplicate add of listener"); |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 511 | } |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | /** |
| 516 | * Remove a listener for this cursor |
| 517 | */ |
| 518 | public void removeListener(ConversationListener listener) { |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 519 | synchronized(mListeners) { |
| 520 | mListeners.remove(listener); |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 521 | } |
| 522 | } |
| 523 | |
| 524 | /** |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 525 | * Generate a forwarding Uri to ConversationProvider from an original Uri. We do this by |
| 526 | * changing the authority to ours, but otherwise leaving the Uri intact. |
| 527 | * NOTE: This won't handle query parameters, so the functionality will need to be added if |
| 528 | * parameters are used in the future |
| 529 | * @param uri the uri |
| 530 | * @return a forwarding uri to ConversationProvider |
| 531 | */ |
| 532 | private static String uriToCachingUriString (Uri uri) { |
Paul Westbrook | fe7f5bb | 2012-09-09 17:59:35 -0700 | [diff] [blame] | 533 | final String provider = uri.getAuthority(); |
Paul Westbrook | 77177b1 | 2012-02-07 15:23:42 -0800 | [diff] [blame] | 534 | return uri.getScheme() + "://" + ConversationProvider.AUTHORITY |
| 535 | + "/" + provider + uri.getPath(); |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | /** |
| 539 | * Regenerate the original Uri from a forwarding (ConversationProvider) Uri |
| 540 | * NOTE: See note above for uriToCachingUri |
| 541 | * @param uri the forwarding Uri |
| 542 | * @return the original Uri |
| 543 | */ |
| 544 | private static Uri uriFromCachingUri(Uri uri) { |
Marc Blank | d978715 | 2012-03-15 09:43:12 -0700 | [diff] [blame] | 545 | String authority = uri.getAuthority(); |
| 546 | // Don't modify uri's that aren't ours |
| 547 | if (!authority.equals(ConversationProvider.AUTHORITY)) { |
| 548 | return uri; |
| 549 | } |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 550 | List<String> path = uri.getPathSegments(); |
| 551 | Uri.Builder builder = new Uri.Builder().scheme(uri.getScheme()).authority(path.get(0)); |
| 552 | for (int i = 1; i < path.size(); i++) { |
| 553 | builder.appendPath(path.get(i)); |
| 554 | } |
| 555 | return builder.build(); |
| 556 | } |
| 557 | |
Marc Blank | e1d1b07 | 2012-04-13 17:29:16 -0700 | [diff] [blame] | 558 | private static String uriStringFromCachingUri(Uri uri) { |
| 559 | Uri underlyingUri = uriFromCachingUri(uri); |
| 560 | // Remember to decode the underlying Uri as it might be encoded (as w/ Gmail) |
| 561 | return Uri.decode(underlyingUri.toString()); |
| 562 | } |
| 563 | |
Andy Huang | daa06ab | 2012-07-24 10:46:44 -0700 | [diff] [blame] | 564 | public void setConversationColumn(Uri conversationUri, String columnName, Object value) { |
| 565 | final String uriStr = uriStringFromCachingUri(conversationUri); |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 566 | synchronized (mCacheMapLock) { |
Andy Huang | daa06ab | 2012-07-24 10:46:44 -0700 | [diff] [blame] | 567 | cacheValue(uriStr, columnName, value); |
Marc Blank | bec5115 | 2012-03-22 19:27:34 -0700 | [diff] [blame] | 568 | } |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 569 | notifyDataChanged(); |
Marc Blank | 958bf4d | 2012-04-05 18:10:55 -0700 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | /** |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 573 | * Cache a column name/value pair for a given Uri |
| 574 | * @param uriString the Uri for which the column name/value pair applies |
| 575 | * @param columnName the column name |
| 576 | * @param value the value to be cached |
| 577 | */ |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 578 | private void cacheValue(String uriString, String columnName, Object value) { |
Andy Huang | 489dd22 | 2012-03-29 14:52:55 -0700 | [diff] [blame] | 579 | // Calling this method off the UI thread will mess with ListView's reading of the cursor's |
| 580 | // count |
| 581 | if (offUiThread()) { |
mindyp | 5c1d835 | 2012-11-05 10:12:44 -0800 | [diff] [blame] | 582 | LogUtils.e(LOG_TAG, new Error(), |
| 583 | "cacheValue incorrectly being called from non-UI thread"); |
Andy Huang | 489dd22 | 2012-03-29 14:52:55 -0700 | [diff] [blame] | 584 | } |
| 585 | |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 586 | synchronized (mCacheMapLock) { |
Marc Blank | 958bf4d | 2012-04-05 18:10:55 -0700 | [diff] [blame] | 587 | // Get the map for our uri |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 588 | ContentValues map = mCacheMap.get(uriString); |
Marc Blank | 958bf4d | 2012-04-05 18:10:55 -0700 | [diff] [blame] | 589 | // Create one if necessary |
| 590 | if (map == null) { |
| 591 | map = new ContentValues(); |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 592 | mCacheMap.put(uriString, map); |
Marc Blank | 958bf4d | 2012-04-05 18:10:55 -0700 | [diff] [blame] | 593 | } |
| 594 | // If we're caching a deletion, add to our count |
| 595 | if (columnName == DELETED_COLUMN) { |
| 596 | final boolean state = (Boolean)value; |
| 597 | final boolean hasValue = map.get(columnName) != null; |
| 598 | if (state && !hasValue) { |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 599 | mDeletedCount++; |
Marc Blank | 958bf4d | 2012-04-05 18:10:55 -0700 | [diff] [blame] | 600 | if (DEBUG) { |
mindyp | 5c1d835 | 2012-11-05 10:12:44 -0800 | [diff] [blame] | 601 | LogUtils.i(LOG_TAG, "Deleted %s, incremented deleted count=%d", uriString, |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 602 | mDeletedCount); |
Paul Westbrook | 9c87fe3 | 2012-03-28 22:14:42 -0700 | [diff] [blame] | 603 | } |
Marc Blank | 958bf4d | 2012-04-05 18:10:55 -0700 | [diff] [blame] | 604 | } else if (!state && hasValue) { |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 605 | mDeletedCount--; |
Marc Blank | 958bf4d | 2012-04-05 18:10:55 -0700 | [diff] [blame] | 606 | map.remove(columnName); |
| 607 | if (DEBUG) { |
mindyp | 5c1d835 | 2012-11-05 10:12:44 -0800 | [diff] [blame] | 608 | LogUtils.i(LOG_TAG, "Undeleted %s, decremented deleted count=%d", uriString, |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 609 | mDeletedCount); |
Paul Westbrook | 9c87fe3 | 2012-03-28 22:14:42 -0700 | [diff] [blame] | 610 | } |
Marc Blank | 958bf4d | 2012-04-05 18:10:55 -0700 | [diff] [blame] | 611 | return; |
| 612 | } else if (!state) { |
| 613 | // Trying to undelete, but it's not deleted; just return |
| 614 | if (DEBUG) { |
mindyp | 5c1d835 | 2012-11-05 10:12:44 -0800 | [diff] [blame] | 615 | LogUtils.i(LOG_TAG, "Undeleted %s, IGNORING, deleted count=%d", uriString, |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 616 | mDeletedCount); |
Marc Blank | 958bf4d | 2012-04-05 18:10:55 -0700 | [diff] [blame] | 617 | } |
| 618 | return; |
Paul Westbrook | 9c87fe3 | 2012-03-28 22:14:42 -0700 | [diff] [blame] | 619 | } |
Marc Blank | 48eba7a | 2012-01-27 16:16:19 -0800 | [diff] [blame] | 620 | } |
Marc Blank | 958bf4d | 2012-04-05 18:10:55 -0700 | [diff] [blame] | 621 | // ContentValues has no generic "put", so we must test. For now, the only classes |
Andy Huang | 351ad4e | 2012-12-06 16:04:58 -0800 | [diff] [blame] | 622 | // of values implemented are Boolean/Integer/String/Blob, though others are trivially |
Marc Blank | 958bf4d | 2012-04-05 18:10:55 -0700 | [diff] [blame] | 623 | // added |
| 624 | if (value instanceof Boolean) { |
| 625 | map.put(columnName, ((Boolean) value).booleanValue() ? 1 : 0); |
| 626 | } else if (value instanceof Integer) { |
| 627 | map.put(columnName, (Integer) value); |
| 628 | } else if (value instanceof String) { |
| 629 | map.put(columnName, (String) value); |
Andy Huang | 351ad4e | 2012-12-06 16:04:58 -0800 | [diff] [blame] | 630 | } else if (value instanceof byte[]) { |
| 631 | map.put(columnName, (byte[])value); |
Marc Blank | 958bf4d | 2012-04-05 18:10:55 -0700 | [diff] [blame] | 632 | } else { |
| 633 | final String cname = value.getClass().getName(); |
| 634 | throw new IllegalArgumentException("Value class not compatible with cache: " |
| 635 | + cname); |
| 636 | } |
mindyp | 7f55c68 | 2012-10-04 11:38:27 -0700 | [diff] [blame] | 637 | map.put(UPDATE_TIME_COLUMN, System.currentTimeMillis()); |
Marc Blank | 958bf4d | 2012-04-05 18:10:55 -0700 | [diff] [blame] | 638 | if (DEBUG && (columnName != DELETED_COLUMN)) { |
mindyp | 5c1d835 | 2012-11-05 10:12:44 -0800 | [diff] [blame] | 639 | LogUtils.i(LOG_TAG, "Caching value for %s: %s", uriString, columnName); |
Marc Blank | 958bf4d | 2012-04-05 18:10:55 -0700 | [diff] [blame] | 640 | } |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 641 | } |
| 642 | } |
| 643 | |
| 644 | /** |
| 645 | * Get the cached value for the provided column; we special case -1 as the "deleted" column |
| 646 | * @param columnIndex the index of the column whose cached value we want to retrieve |
| 647 | * @return the cached value for this column, or null if there is none |
| 648 | */ |
| 649 | private Object getCachedValue(int columnIndex) { |
Vikram Aggarwal | 8d9313b | 2013-01-03 16:15:10 -0800 | [diff] [blame] | 650 | String uri = mUnderlyingCursor.getString(URI_COLUMN_INDEX); |
Marc Blank | e1d1b07 | 2012-04-13 17:29:16 -0700 | [diff] [blame] | 651 | return getCachedValue(uri, columnIndex); |
| 652 | } |
| 653 | |
| 654 | private Object getCachedValue(String uri, int columnIndex) { |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 655 | ContentValues uriMap = mCacheMap.get(uri); |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 656 | if (uriMap != null) { |
| 657 | String columnName; |
| 658 | if (columnIndex == DELETED_COLUMN_INDEX) { |
| 659 | columnName = DELETED_COLUMN; |
| 660 | } else { |
| 661 | columnName = mColumnNames[columnIndex]; |
| 662 | } |
| 663 | return uriMap.get(columnName); |
| 664 | } |
| 665 | return null; |
| 666 | } |
| 667 | |
| 668 | /** |
Marc Blank | 97bca7b | 2012-01-24 11:17:00 -0800 | [diff] [blame] | 669 | * When the underlying cursor changes, we want to alert the listener |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 670 | */ |
| 671 | private void underlyingChanged() { |
Paul Westbrook | c33ec4c | 2012-08-09 01:47:25 -0700 | [diff] [blame] | 672 | synchronized(mCacheMapLock) { |
Marc Blank | 28cb53d | 2012-08-19 17:58:24 -0700 | [diff] [blame] | 673 | if (mCursorObserverRegistered) { |
| 674 | try { |
| 675 | mUnderlyingCursor.unregisterContentObserver(mCursorObserver); |
| 676 | } catch (IllegalStateException e) { |
| 677 | // Maybe the cursor was GC'd? |
| 678 | } |
| 679 | mCursorObserverRegistered = false; |
| 680 | } |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 681 | mRefreshRequired = true; |
| 682 | if (!mPaused) { |
Marc Blank | e1d1b07 | 2012-04-13 17:29:16 -0700 | [diff] [blame] | 683 | notifyRefreshRequired(); |
| 684 | } |
Marc Blank | 97bca7b | 2012-01-24 11:17:00 -0800 | [diff] [blame] | 685 | } |
Marc Blank | e1d1b07 | 2012-04-13 17:29:16 -0700 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | /** |
| 689 | * Must be called on UI thread; notify listeners that a refresh is required |
| 690 | */ |
| 691 | private void notifyRefreshRequired() { |
Marc Blank | b600a83 | 2012-02-16 09:20:18 -0800 | [diff] [blame] | 692 | if (DEBUG) { |
mindyp | 5c1d835 | 2012-11-05 10:12:44 -0800 | [diff] [blame] | 693 | LogUtils.i(LOG_TAG, "[Notify %s: onRefreshRequired()]", mName); |
Marc Blank | b600a83 | 2012-02-16 09:20:18 -0800 | [diff] [blame] | 694 | } |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 695 | if (!mDeferSync) { |
| 696 | synchronized(mListeners) { |
| 697 | for (ConversationListener listener: mListeners) { |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 698 | listener.onRefreshRequired(); |
| 699 | } |
| 700 | } |
Marc Blank | b600a83 | 2012-02-16 09:20:18 -0800 | [diff] [blame] | 701 | } |
Marc Blank | e1d1b07 | 2012-04-13 17:29:16 -0700 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | /** |
| 705 | * Must be called on UI thread; notify listeners that a new cursor is ready |
| 706 | */ |
| 707 | private void notifyRefreshReady() { |
| 708 | if (DEBUG) { |
mindyp | 5c1d835 | 2012-11-05 10:12:44 -0800 | [diff] [blame] | 709 | LogUtils.i(LOG_TAG, "[Notify %s: onRefreshReady(), %d listeners]", |
Paul Westbrook | e2bde3a | 2012-07-31 17:35:43 -0700 | [diff] [blame] | 710 | mName, mListeners.size()); |
Marc Blank | e1d1b07 | 2012-04-13 17:29:16 -0700 | [diff] [blame] | 711 | } |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 712 | synchronized(mListeners) { |
| 713 | for (ConversationListener listener: mListeners) { |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 714 | listener.onRefreshReady(); |
| 715 | } |
| 716 | } |
Marc Blank | e1d1b07 | 2012-04-13 17:29:16 -0700 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | /** |
| 720 | * Must be called on UI thread; notify listeners that data has changed |
| 721 | */ |
| 722 | private void notifyDataChanged() { |
| 723 | if (DEBUG) { |
mindyp | 5c1d835 | 2012-11-05 10:12:44 -0800 | [diff] [blame] | 724 | LogUtils.i(LOG_TAG, "[Notify %s: onDataSetChanged()]", mName); |
Marc Blank | e1d1b07 | 2012-04-13 17:29:16 -0700 | [diff] [blame] | 725 | } |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 726 | synchronized(mListeners) { |
| 727 | for (ConversationListener listener: mListeners) { |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 728 | listener.onDataSetChanged(); |
| 729 | } |
| 730 | } |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 731 | } |
| 732 | |
Marc Blank | 4015c18 | 2012-01-31 12:38:36 -0800 | [diff] [blame] | 733 | /** |
| 734 | * Put the refreshed cursor in place (called by the UI) |
| 735 | */ |
Marc Blank | 4e25c94 | 2012-02-02 19:41:14 -0800 | [diff] [blame] | 736 | public void sync() { |
Marc Blank | 28cb53d | 2012-08-19 17:58:24 -0700 | [diff] [blame] | 737 | if (mRequeryCursor == null) { |
Marc Blank | 09b3238 | 2012-03-20 12:12:17 -0700 | [diff] [blame] | 738 | // This can happen during an animated deletion, if the UI isn't keeping track, or |
| 739 | // if a new query intervened (i.e. user changed folders) |
Marc Blank | 948985b | 2012-02-29 11:26:40 -0800 | [diff] [blame] | 740 | if (DEBUG) { |
mindyp | 5c1d835 | 2012-11-05 10:12:44 -0800 | [diff] [blame] | 741 | LogUtils.i(LOG_TAG, "[sync() %s; no requery cursor]", mName); |
Marc Blank | 948985b | 2012-02-29 11:26:40 -0800 | [diff] [blame] | 742 | } |
Marc Blank | 09b3238 | 2012-03-20 12:12:17 -0700 | [diff] [blame] | 743 | return; |
| 744 | } |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 745 | synchronized(mCacheMapLock) { |
Marc Blank | 28cb53d | 2012-08-19 17:58:24 -0700 | [diff] [blame] | 746 | if (DEBUG) { |
mindyp | 5c1d835 | 2012-11-05 10:12:44 -0800 | [diff] [blame] | 747 | LogUtils.i(LOG_TAG, "[sync() %s]", mName); |
Marc Blank | 28cb53d | 2012-08-19 17:58:24 -0700 | [diff] [blame] | 748 | } |
| 749 | resetCursor(mRequeryCursor); |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 750 | mRequeryCursor = null; |
| 751 | mRefreshTask = null; |
| 752 | mRefreshReady = false; |
Marc Blank | 3f1eb85 | 2012-02-03 15:38:01 -0800 | [diff] [blame] | 753 | } |
Paul Westbrook | 66150d7 | 2012-04-18 04:45:16 -0700 | [diff] [blame] | 754 | notifyDataChanged(); |
Marc Blank | 4e25c94 | 2012-02-02 19:41:14 -0800 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | public boolean isRefreshRequired() { |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 758 | return mRefreshRequired; |
Marc Blank | 4e25c94 | 2012-02-02 19:41:14 -0800 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | public boolean isRefreshReady() { |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 762 | return mRefreshReady; |
Marc Blank | 4015c18 | 2012-01-31 12:38:36 -0800 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | /** |
| 766 | * Cancel a refresh in progress |
| 767 | */ |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 768 | public void cancelRefresh() { |
Marc Blank | 3f1eb85 | 2012-02-03 15:38:01 -0800 | [diff] [blame] | 769 | if (DEBUG) { |
mindyp | 5c1d835 | 2012-11-05 10:12:44 -0800 | [diff] [blame] | 770 | LogUtils.i(LOG_TAG, "[cancelRefresh() %s]", mName); |
Marc Blank | 3f1eb85 | 2012-02-03 15:38:01 -0800 | [diff] [blame] | 771 | } |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 772 | synchronized(mCacheMapLock) { |
| 773 | if (mRefreshTask != null) { |
| 774 | mRefreshTask.cancel(true); |
| 775 | mRefreshTask = null; |
Marc Blank | e3d3679 | 2012-04-02 09:30:14 -0700 | [diff] [blame] | 776 | } |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 777 | mRefreshReady = false; |
Marc Blank | 4015c18 | 2012-01-31 12:38:36 -0800 | [diff] [blame] | 778 | // If we have the cursor, close it; otherwise, it will get closed when the query |
Marc Blank | 09b3238 | 2012-03-20 12:12:17 -0700 | [diff] [blame] | 779 | // finishes (it checks sRefreshInProgress) |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 780 | if (mRequeryCursor != null) { |
| 781 | mRequeryCursor.close(); |
| 782 | mRequeryCursor = null; |
Marc Blank | 4015c18 | 2012-01-31 12:38:36 -0800 | [diff] [blame] | 783 | } |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | /** |
Marc Blank | 48eba7a | 2012-01-27 16:16:19 -0800 | [diff] [blame] | 788 | * When we get a requery from the UI, we'll do it, but also clear the cache. The listener is |
| 789 | * notified when the requery is complete |
Marc Blank | 97bca7b | 2012-01-24 11:17:00 -0800 | [diff] [blame] | 790 | * NOTE: This will have to change, of course, when we start using loaders... |
| 791 | */ |
Marc Blank | 48eba7a | 2012-01-27 16:16:19 -0800 | [diff] [blame] | 792 | public boolean refresh() { |
Marc Blank | 3f1eb85 | 2012-02-03 15:38:01 -0800 | [diff] [blame] | 793 | if (DEBUG) { |
mindyp | 5c1d835 | 2012-11-05 10:12:44 -0800 | [diff] [blame] | 794 | LogUtils.i(LOG_TAG, "[refresh() %s]", mName); |
Marc Blank | 3f1eb85 | 2012-02-03 15:38:01 -0800 | [diff] [blame] | 795 | } |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 796 | synchronized(mCacheMapLock) { |
| 797 | if (mRefreshTask != null) { |
Marc Blank | e3d3679 | 2012-04-02 09:30:14 -0700 | [diff] [blame] | 798 | if (DEBUG) { |
mindyp | 5c1d835 | 2012-11-05 10:12:44 -0800 | [diff] [blame] | 799 | LogUtils.i(LOG_TAG, "[refresh() %s returning; already running %d]", |
Paul Westbrook | e2bde3a | 2012-07-31 17:35:43 -0700 | [diff] [blame] | 800 | mName, mRefreshTask.hashCode()); |
Marc Blank | 4015c18 | 2012-01-31 12:38:36 -0800 | [diff] [blame] | 801 | } |
Marc Blank | e3d3679 | 2012-04-02 09:30:14 -0700 | [diff] [blame] | 802 | return false; |
Marc Blank | 48eba7a | 2012-01-27 16:16:19 -0800 | [diff] [blame] | 803 | } |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 804 | mRefreshTask = new RefreshTask(); |
| 805 | mRefreshTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); |
Marc Blank | e3d3679 | 2012-04-02 09:30:14 -0700 | [diff] [blame] | 806 | } |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 807 | return true; |
| 808 | } |
| 809 | |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 810 | public void disable() { |
| 811 | close(); |
| 812 | mCacheMap.clear(); |
| 813 | mListeners.clear(); |
| 814 | mUnderlyingCursor = null; |
| 815 | } |
| 816 | |
Marc Blank | b600a83 | 2012-02-16 09:20:18 -0800 | [diff] [blame] | 817 | @Override |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 818 | public void close() { |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 819 | if (mUnderlyingCursor != null && !mUnderlyingCursor.isClosed()) { |
Marc Blank | f9d8719 | 2012-02-16 10:50:41 -0800 | [diff] [blame] | 820 | // Unregister our observer on the underlying cursor and close as usual |
| 821 | if (mCursorObserverRegistered) { |
| 822 | try { |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 823 | mUnderlyingCursor.unregisterContentObserver(mCursorObserver); |
Marc Blank | f9d8719 | 2012-02-16 10:50:41 -0800 | [diff] [blame] | 824 | } catch (IllegalStateException e) { |
| 825 | // Maybe the cursor got GC'd? |
| 826 | } |
| 827 | mCursorObserverRegistered = false; |
| 828 | } |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 829 | mUnderlyingCursor.close(); |
Marc Blank | dd10bc8 | 2012-02-01 19:10:46 -0800 | [diff] [blame] | 830 | } |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | /** |
| 834 | * Move to the next not-deleted item in the conversation |
| 835 | */ |
Marc Blank | b600a83 | 2012-02-16 09:20:18 -0800 | [diff] [blame] | 836 | @Override |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 837 | public boolean moveToNext() { |
| 838 | while (true) { |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 839 | boolean ret = mUnderlyingCursor.moveToNext(); |
Marc Blank | 5d8b1fb | 2012-07-20 17:12:36 -0700 | [diff] [blame] | 840 | if (!ret) { |
Marc Blank | 44d5f0a | 2012-09-21 14:20:55 -0700 | [diff] [blame] | 841 | mPosition = getCount(); |
Marc Blank | e5884c7 | 2012-08-18 10:03:59 -0700 | [diff] [blame] | 842 | // STOPSHIP |
mindyp | 5c1d835 | 2012-11-05 10:12:44 -0800 | [diff] [blame] | 843 | LogUtils.i(LOG_TAG, "*** moveToNext returns false; pos = %d, und = %d, del = %d", |
Marc Blank | e5884c7 | 2012-08-18 10:03:59 -0700 | [diff] [blame] | 844 | mPosition, mUnderlyingCursor.getPosition(), mDeletedCount); |
Marc Blank | 5d8b1fb | 2012-07-20 17:12:36 -0700 | [diff] [blame] | 845 | return false; |
| 846 | } |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 847 | if (getCachedValue(DELETED_COLUMN_INDEX) instanceof Integer) continue; |
| 848 | mPosition++; |
| 849 | return true; |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | /** |
| 854 | * Move to the previous not-deleted item in the conversation |
| 855 | */ |
Marc Blank | b600a83 | 2012-02-16 09:20:18 -0800 | [diff] [blame] | 856 | @Override |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 857 | public boolean moveToPrevious() { |
| 858 | while (true) { |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 859 | boolean ret = mUnderlyingCursor.moveToPrevious(); |
Marc Blank | 5d8b1fb | 2012-07-20 17:12:36 -0700 | [diff] [blame] | 860 | if (!ret) { |
| 861 | // Make sure we're before the first position |
| 862 | mPosition = -1; |
| 863 | return false; |
| 864 | } |
Marc Blank | ec7c4da | 2012-03-22 20:28:55 -0700 | [diff] [blame] | 865 | if (getCachedValue(DELETED_COLUMN_INDEX) instanceof Integer) continue; |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 866 | mPosition--; |
| 867 | return true; |
| 868 | } |
| 869 | } |
| 870 | |
Marc Blank | b600a83 | 2012-02-16 09:20:18 -0800 | [diff] [blame] | 871 | @Override |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 872 | public int getPosition() { |
| 873 | return mPosition; |
| 874 | } |
| 875 | |
| 876 | /** |
| 877 | * The actual cursor's count must be decremented by the number we've deleted from the UI |
| 878 | */ |
Marc Blank | b600a83 | 2012-02-16 09:20:18 -0800 | [diff] [blame] | 879 | @Override |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 880 | public int getCount() { |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 881 | if (mUnderlyingCursor == null) { |
| 882 | throw new IllegalStateException( |
| 883 | "getCount() on disabled cursor: " + mName + "(" + qUri + ")"); |
| 884 | } |
| 885 | return mUnderlyingCursor.getCount() - mDeletedCount; |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 886 | } |
| 887 | |
Marc Blank | b600a83 | 2012-02-16 09:20:18 -0800 | [diff] [blame] | 888 | @Override |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 889 | public boolean moveToFirst() { |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 890 | if (mUnderlyingCursor == null) { |
| 891 | throw new IllegalStateException( |
| 892 | "moveToFirst() on disabled cursor: " + mName + "(" + qUri + ")"); |
| 893 | } |
| 894 | mUnderlyingCursor.moveToPosition(-1); |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 895 | mPosition = -1; |
| 896 | return moveToNext(); |
| 897 | } |
| 898 | |
Marc Blank | b600a83 | 2012-02-16 09:20:18 -0800 | [diff] [blame] | 899 | @Override |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 900 | public boolean moveToPosition(int pos) { |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 901 | if (mUnderlyingCursor == null) { |
| 902 | throw new IllegalStateException( |
| 903 | "moveToPosition() on disabled cursor: " + mName + "(" + qUri + ")"); |
| 904 | } |
Marc Blank | 7ffbaaa | 2012-07-30 16:19:19 -0700 | [diff] [blame] | 905 | // Handle the "move to first" case before anything else; moveToPosition(0) in an empty |
| 906 | // SQLiteCursor moves the position to 0 when returning false, which we will mirror. |
| 907 | // But we don't want to return true on a subsequent "move to first", which we would if we |
| 908 | // check pos vs mPosition first |
mindyp | 35f6bba | 2012-09-25 17:39:04 -0700 | [diff] [blame] | 909 | if (mUnderlyingCursor.getPosition() == -1) { |
mindyp | 5c1d835 | 2012-11-05 10:12:44 -0800 | [diff] [blame] | 910 | LogUtils.i(LOG_TAG, "*** Underlying cursor position is -1 asking to move from %d to %d", |
mindyp | 35f6bba | 2012-09-25 17:39:04 -0700 | [diff] [blame] | 911 | mPosition, pos); |
| 912 | } |
Marc Blank | 7ffbaaa | 2012-07-30 16:19:19 -0700 | [diff] [blame] | 913 | if (pos == 0) { |
| 914 | return moveToFirst(); |
Paul Westbrook | c84d251 | 2012-09-26 00:18:28 -0700 | [diff] [blame] | 915 | } else if (pos < 0) { |
| 916 | mPosition = -1; |
| 917 | mUnderlyingCursor.moveToPosition(mPosition); |
| 918 | return false; |
Marc Blank | 7ffbaaa | 2012-07-30 16:19:19 -0700 | [diff] [blame] | 919 | } else if (pos == mPosition) { |
Marc Blank | 9735cdc | 2012-09-22 17:17:57 -0700 | [diff] [blame] | 920 | // Return false if we're past the end of the cursor |
| 921 | return pos < getCount(); |
Marc Blank | 7ffbaaa | 2012-07-30 16:19:19 -0700 | [diff] [blame] | 922 | } else if (pos > mPosition) { |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 923 | while (pos > mPosition) { |
| 924 | if (!moveToNext()) { |
| 925 | return false; |
| 926 | } |
| 927 | } |
| 928 | return true; |
Marc Blank | 18a9717 | 2012-08-22 14:59:10 -0700 | [diff] [blame] | 929 | } else if ((pos >= 0) && (mPosition - pos) > pos) { |
Marc Blank | e5884c7 | 2012-08-18 10:03:59 -0700 | [diff] [blame] | 930 | // Optimization if it's easier to move forward to position instead of backward |
| 931 | // STOPSHIP (Remove logging) |
mindyp | 5c1d835 | 2012-11-05 10:12:44 -0800 | [diff] [blame] | 932 | LogUtils.i(LOG_TAG, "*** Move from %d to %d, starting from first", mPosition, pos); |
Marc Blank | e5884c7 | 2012-08-18 10:03:59 -0700 | [diff] [blame] | 933 | moveToFirst(); |
| 934 | return moveToPosition(pos); |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 935 | } else { |
| 936 | while (pos < mPosition) { |
| 937 | if (!moveToPrevious()) { |
| 938 | return false; |
| 939 | } |
| 940 | } |
| 941 | return true; |
| 942 | } |
| 943 | } |
| 944 | |
Marc Blank | 93b3a15 | 2012-04-11 15:53:19 -0700 | [diff] [blame] | 945 | /** |
| 946 | * Make sure mPosition is correct after locally deleting/undeleting items |
| 947 | */ |
| 948 | private void recalibratePosition() { |
Paul Westbrook | c84d251 | 2012-09-26 00:18:28 -0700 | [diff] [blame] | 949 | final int pos = mPosition; |
Marc Blank | 93b3a15 | 2012-04-11 15:53:19 -0700 | [diff] [blame] | 950 | moveToFirst(); |
| 951 | moveToPosition(pos); |
| 952 | } |
| 953 | |
Marc Blank | b600a83 | 2012-02-16 09:20:18 -0800 | [diff] [blame] | 954 | @Override |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 955 | public boolean moveToLast() { |
| 956 | throw new UnsupportedOperationException("moveToLast unsupported!"); |
| 957 | } |
| 958 | |
Marc Blank | b600a83 | 2012-02-16 09:20:18 -0800 | [diff] [blame] | 959 | @Override |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 960 | public boolean move(int offset) { |
| 961 | throw new UnsupportedOperationException("move unsupported!"); |
| 962 | } |
| 963 | |
| 964 | /** |
| 965 | * We need to override all of the getters to make sure they look at cached values before using |
| 966 | * the values in the underlying cursor |
| 967 | */ |
| 968 | @Override |
| 969 | public double getDouble(int columnIndex) { |
| 970 | Object obj = getCachedValue(columnIndex); |
| 971 | if (obj != null) return (Double)obj; |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 972 | return mUnderlyingCursor.getDouble(columnIndex); |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | @Override |
| 976 | public float getFloat(int columnIndex) { |
| 977 | Object obj = getCachedValue(columnIndex); |
| 978 | if (obj != null) return (Float)obj; |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 979 | return mUnderlyingCursor.getFloat(columnIndex); |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | @Override |
| 983 | public int getInt(int columnIndex) { |
| 984 | Object obj = getCachedValue(columnIndex); |
| 985 | if (obj != null) return (Integer)obj; |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 986 | return mUnderlyingCursor.getInt(columnIndex); |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | @Override |
| 990 | public long getLong(int columnIndex) { |
Marc Blank | e1d1b07 | 2012-04-13 17:29:16 -0700 | [diff] [blame] | 991 | Object obj = getCachedValue(columnIndex); |
| 992 | if (obj != null) return (Long)obj; |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 993 | return mUnderlyingCursor.getLong(columnIndex); |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | @Override |
| 997 | public short getShort(int columnIndex) { |
| 998 | Object obj = getCachedValue(columnIndex); |
| 999 | if (obj != null) return (Short)obj; |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1000 | return mUnderlyingCursor.getShort(columnIndex); |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | @Override |
| 1004 | public String getString(int columnIndex) { |
| 1005 | // If we're asking for the Uri for the conversation list, we return a forwarding URI |
| 1006 | // so that we can intercept update/delete and handle it ourselves |
Vikram Aggarwal | 8d9313b | 2013-01-03 16:15:10 -0800 | [diff] [blame] | 1007 | if (columnIndex == URI_COLUMN_INDEX) { |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1008 | Uri uri = Uri.parse(mUnderlyingCursor.getString(columnIndex)); |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 1009 | return uriToCachingUriString(uri); |
| 1010 | } |
| 1011 | Object obj = getCachedValue(columnIndex); |
| 1012 | if (obj != null) return (String)obj; |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1013 | return mUnderlyingCursor.getString(columnIndex); |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | @Override |
| 1017 | public byte[] getBlob(int columnIndex) { |
| 1018 | Object obj = getCachedValue(columnIndex); |
| 1019 | if (obj != null) return (byte[])obj; |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1020 | return mUnderlyingCursor.getBlob(columnIndex); |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | /** |
| 1024 | * Observer of changes to underlying data |
| 1025 | */ |
| 1026 | private class CursorObserver extends ContentObserver { |
Marc Blank | e77d526 | 2012-08-19 17:58:24 -0700 | [diff] [blame] | 1027 | public CursorObserver(Handler handler) { |
| 1028 | super(handler); |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | @Override |
| 1032 | public void onChange(boolean selfChange) { |
| 1033 | // If we're here, then something outside of the UI has changed the data, and we |
Marc Blank | e77d526 | 2012-08-19 17:58:24 -0700 | [diff] [blame] | 1034 | // must query the underlying provider for that data; |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 1035 | ConversationCursor.this.underlyingChanged(); |
| 1036 | } |
| 1037 | } |
| 1038 | |
| 1039 | /** |
| 1040 | * ConversationProvider is the ContentProvider for our forwarding Uri's; it passes queries |
| 1041 | * and inserts directly, and caches updates/deletes before passing them through. The caching |
| 1042 | * will cause a redraw of the list with updated values. |
| 1043 | */ |
Paul Westbrook | 77177b1 | 2012-02-07 15:23:42 -0800 | [diff] [blame] | 1044 | public abstract static class ConversationProvider extends ContentProvider { |
| 1045 | public static String AUTHORITY; |
Vikram Aggarwal | 7460a1c | 2012-12-28 15:58:58 -0800 | [diff] [blame] | 1046 | private ContentResolver mResolver; |
Paul Westbrook | 77177b1 | 2012-02-07 15:23:42 -0800 | [diff] [blame] | 1047 | |
| 1048 | /** |
Vikram Aggarwal | 6a62146 | 2012-04-02 14:42:40 -0700 | [diff] [blame] | 1049 | * Allows the implementing provider to specify the authority that should be used. |
Paul Westbrook | 77177b1 | 2012-02-07 15:23:42 -0800 | [diff] [blame] | 1050 | */ |
| 1051 | protected abstract String getAuthority(); |
Marc Blank | 8d69d4e | 2012-01-25 12:04:28 -0800 | [diff] [blame] | 1052 | |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 1053 | @Override |
| 1054 | public boolean onCreate() { |
Marc Blank | 8d69d4e | 2012-01-25 12:04:28 -0800 | [diff] [blame] | 1055 | sProvider = this; |
Paul Westbrook | 77177b1 | 2012-02-07 15:23:42 -0800 | [diff] [blame] | 1056 | AUTHORITY = getAuthority(); |
Vikram Aggarwal | 7460a1c | 2012-12-28 15:58:58 -0800 | [diff] [blame] | 1057 | mResolver = getContext().getContentResolver(); |
Marc Blank | 8d69d4e | 2012-01-25 12:04:28 -0800 | [diff] [blame] | 1058 | return true; |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | @Override |
| 1062 | public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, |
| 1063 | String sortOrder) { |
Vikram Aggarwal | 7460a1c | 2012-12-28 15:58:58 -0800 | [diff] [blame] | 1064 | return mResolver.query( |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 1065 | uriFromCachingUri(uri), projection, selection, selectionArgs, sortOrder); |
| 1066 | } |
| 1067 | |
| 1068 | @Override |
Marc Blank | f892f0a | 2012-01-30 13:04:10 -0800 | [diff] [blame] | 1069 | public Uri insert(Uri uri, ContentValues values) { |
| 1070 | insertLocal(uri, values); |
Vikram Aggarwal | 7460a1c | 2012-12-28 15:58:58 -0800 | [diff] [blame] | 1071 | return ProviderExecute.opInsert(mResolver, uri, values); |
Marc Blank | f892f0a | 2012-01-30 13:04:10 -0800 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | @Override |
| 1075 | public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1076 | throw new IllegalStateException("Unexpected call to ConversationProvider.delete"); |
Marc Blank | f892f0a | 2012-01-30 13:04:10 -0800 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | @Override |
| 1080 | public int delete(Uri uri, String selection, String[] selectionArgs) { |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1081 | throw new IllegalStateException("Unexpected call to ConversationProvider.delete"); |
Marc Blank | f892f0a | 2012-01-30 13:04:10 -0800 | [diff] [blame] | 1082 | } |
| 1083 | |
| 1084 | @Override |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 1085 | public String getType(Uri uri) { |
| 1086 | return null; |
| 1087 | } |
| 1088 | |
| 1089 | /** |
| 1090 | * Quick and dirty class that executes underlying provider CRUD operations on a background |
| 1091 | * thread. |
| 1092 | */ |
| 1093 | static class ProviderExecute implements Runnable { |
| 1094 | static final int DELETE = 0; |
| 1095 | static final int INSERT = 1; |
| 1096 | static final int UPDATE = 2; |
| 1097 | |
| 1098 | final int mCode; |
| 1099 | final Uri mUri; |
| 1100 | final ContentValues mValues; //HEHEH |
Vikram Aggarwal | 7460a1c | 2012-12-28 15:58:58 -0800 | [diff] [blame] | 1101 | final ContentResolver mResolver; |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 1102 | |
Vikram Aggarwal | 7460a1c | 2012-12-28 15:58:58 -0800 | [diff] [blame] | 1103 | ProviderExecute(int code, ContentResolver resolver, Uri uri, ContentValues values) { |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 1104 | mCode = code; |
| 1105 | mUri = uriFromCachingUri(uri); |
| 1106 | mValues = values; |
Vikram Aggarwal | 7460a1c | 2012-12-28 15:58:58 -0800 | [diff] [blame] | 1107 | mResolver = resolver; |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 1108 | } |
| 1109 | |
Vikram Aggarwal | 7460a1c | 2012-12-28 15:58:58 -0800 | [diff] [blame] | 1110 | static Uri opInsert(ContentResolver resolver, Uri uri, ContentValues values) { |
| 1111 | ProviderExecute e = new ProviderExecute(INSERT, resolver, uri, values); |
Marc Blank | 8d69d4e | 2012-01-25 12:04:28 -0800 | [diff] [blame] | 1112 | if (offUiThread()) return (Uri)e.go(); |
| 1113 | new Thread(e).start(); |
| 1114 | return null; |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 1115 | } |
| 1116 | |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 1117 | @Override |
| 1118 | public void run() { |
Marc Blank | 8d69d4e | 2012-01-25 12:04:28 -0800 | [diff] [blame] | 1119 | go(); |
| 1120 | } |
| 1121 | |
| 1122 | public Object go() { |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 1123 | switch(mCode) { |
| 1124 | case DELETE: |
Vikram Aggarwal | 7460a1c | 2012-12-28 15:58:58 -0800 | [diff] [blame] | 1125 | return mResolver.delete(mUri, null, null); |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 1126 | case INSERT: |
Vikram Aggarwal | 7460a1c | 2012-12-28 15:58:58 -0800 | [diff] [blame] | 1127 | return mResolver.insert(mUri, mValues); |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 1128 | case UPDATE: |
Vikram Aggarwal | 7460a1c | 2012-12-28 15:58:58 -0800 | [diff] [blame] | 1129 | return mResolver.update(mUri, mValues, null, null); |
Marc Blank | 8d69d4e | 2012-01-25 12:04:28 -0800 | [diff] [blame] | 1130 | default: |
| 1131 | return null; |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 1132 | } |
| 1133 | } |
| 1134 | } |
| 1135 | |
Marc Blank | 8d69d4e | 2012-01-25 12:04:28 -0800 | [diff] [blame] | 1136 | private void insertLocal(Uri uri, ContentValues values) { |
| 1137 | // Placeholder for now; there's no local insert |
| 1138 | } |
| 1139 | |
Marc Blank | 2596f00 | 2012-03-22 10:26:26 -0700 | [diff] [blame] | 1140 | private int mUndoSequence = 0; |
| 1141 | private ArrayList<Uri> mUndoDeleteUris = new ArrayList<Uri>(); |
| 1142 | |
Marc Blank | e1d1b07 | 2012-04-13 17:29:16 -0700 | [diff] [blame] | 1143 | void addToUndoSequence(Uri uri) { |
Marc Blank | 2596f00 | 2012-03-22 10:26:26 -0700 | [diff] [blame] | 1144 | if (sSequence != mUndoSequence) { |
| 1145 | mUndoSequence = sSequence; |
| 1146 | mUndoDeleteUris.clear(); |
| 1147 | } |
| 1148 | mUndoDeleteUris.add(uri); |
| 1149 | } |
| 1150 | |
| 1151 | @VisibleForTesting |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1152 | void deleteLocal(Uri uri, ConversationCursor conversationCursor) { |
Marc Blank | e1d1b07 | 2012-04-13 17:29:16 -0700 | [diff] [blame] | 1153 | String uriString = uriStringFromCachingUri(uri); |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1154 | conversationCursor.cacheValue(uriString, DELETED_COLUMN, true); |
Marc Blank | e1d1b07 | 2012-04-13 17:29:16 -0700 | [diff] [blame] | 1155 | addToUndoSequence(uri); |
| 1156 | } |
| 1157 | |
| 1158 | @VisibleForTesting |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1159 | void undeleteLocal(Uri uri, ConversationCursor conversationCursor) { |
Marc Blank | e1d1b07 | 2012-04-13 17:29:16 -0700 | [diff] [blame] | 1160 | String uriString = uriStringFromCachingUri(uri); |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1161 | conversationCursor.cacheValue(uriString, DELETED_COLUMN, false); |
Marc Blank | 2596f00 | 2012-03-22 10:26:26 -0700 | [diff] [blame] | 1162 | } |
| 1163 | |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1164 | void setMostlyDead(Conversation conv, ConversationCursor conversationCursor) { |
Marc Blank | e1d1b07 | 2012-04-13 17:29:16 -0700 | [diff] [blame] | 1165 | Uri uri = conv.uri; |
| 1166 | String uriString = uriStringFromCachingUri(uri); |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1167 | conversationCursor.setMostlyDead(uriString, conv); |
Marc Blank | e1d1b07 | 2012-04-13 17:29:16 -0700 | [diff] [blame] | 1168 | addToUndoSequence(uri); |
Marc Blank | e1d1b07 | 2012-04-13 17:29:16 -0700 | [diff] [blame] | 1169 | } |
| 1170 | |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1171 | void commitMostlyDead(Conversation conv, ConversationCursor conversationCursor) { |
| 1172 | conversationCursor.commitMostlyDead(conv); |
Marc Blank | e1d1b07 | 2012-04-13 17:29:16 -0700 | [diff] [blame] | 1173 | } |
| 1174 | |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1175 | boolean clearMostlyDead(Uri uri, ConversationCursor conversationCursor) { |
Marc Blank | e1d1b07 | 2012-04-13 17:29:16 -0700 | [diff] [blame] | 1176 | String uriString = uriStringFromCachingUri(uri); |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1177 | return conversationCursor.clearMostlyDead(uriString); |
Marc Blank | e1d1b07 | 2012-04-13 17:29:16 -0700 | [diff] [blame] | 1178 | } |
| 1179 | |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1180 | public void undo(ConversationCursor conversationCursor) { |
Marc Blank | 2596f00 | 2012-03-22 10:26:26 -0700 | [diff] [blame] | 1181 | if (sSequence == mUndoSequence) { |
| 1182 | for (Uri uri: mUndoDeleteUris) { |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1183 | if (!clearMostlyDead(uri, conversationCursor)) { |
| 1184 | undeleteLocal(uri, conversationCursor); |
Marc Blank | e1d1b07 | 2012-04-13 17:29:16 -0700 | [diff] [blame] | 1185 | } |
Marc Blank | 2596f00 | 2012-03-22 10:26:26 -0700 | [diff] [blame] | 1186 | } |
| 1187 | mUndoSequence = 0; |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1188 | conversationCursor.recalibratePosition(); |
mindyp | 6b5b299 | 2012-09-10 10:12:02 -0700 | [diff] [blame] | 1189 | // Notify listeners that there was a change to the underlying |
| 1190 | // cursor to add back in some items. |
| 1191 | conversationCursor.notifyDataChanged(); |
Marc Blank | 2596f00 | 2012-03-22 10:26:26 -0700 | [diff] [blame] | 1192 | } |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 1193 | } |
| 1194 | |
Marc Blank | 248b1b4 | 2012-02-07 13:43:02 -0800 | [diff] [blame] | 1195 | @VisibleForTesting |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1196 | void updateLocal(Uri uri, ContentValues values, ConversationCursor conversationCursor) { |
Mindy Pereira | f98b318 | 2012-02-22 11:07:13 -0800 | [diff] [blame] | 1197 | if (values == null) { |
| 1198 | return; |
| 1199 | } |
Marc Blank | e1d1b07 | 2012-04-13 17:29:16 -0700 | [diff] [blame] | 1200 | String uriString = uriStringFromCachingUri(uri); |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 1201 | for (String columnName: values.keySet()) { |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1202 | conversationCursor.cacheValue(uriString, columnName, values.get(columnName)); |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 1203 | } |
Marc Blank | 8d69d4e | 2012-01-25 12:04:28 -0800 | [diff] [blame] | 1204 | } |
| 1205 | |
Vikram Aggarwal | cc754b1 | 2012-08-30 14:04:21 -0700 | [diff] [blame] | 1206 | public int apply(Collection<ConversationOperation> ops, |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1207 | ConversationCursor conversationCursor) { |
Marc Blank | 8d69d4e | 2012-01-25 12:04:28 -0800 | [diff] [blame] | 1208 | final HashMap<String, ArrayList<ContentProviderOperation>> batchMap = |
| 1209 | new HashMap<String, ArrayList<ContentProviderOperation>>(); |
Marc Blank | b31ab5a | 2012-02-01 12:28:29 -0800 | [diff] [blame] | 1210 | // Increment sequence count |
| 1211 | sSequence++; |
Marc Blank | 93b3a15 | 2012-04-11 15:53:19 -0700 | [diff] [blame] | 1212 | |
Marc Blank | f892f0a | 2012-01-30 13:04:10 -0800 | [diff] [blame] | 1213 | // Execute locally and build CPO's for underlying provider |
Marc Blank | 93b3a15 | 2012-04-11 15:53:19 -0700 | [diff] [blame] | 1214 | boolean recalibrateRequired = false; |
Marc Blank | 8d69d4e | 2012-01-25 12:04:28 -0800 | [diff] [blame] | 1215 | for (ConversationOperation op: ops) { |
| 1216 | Uri underlyingUri = uriFromCachingUri(op.mUri); |
| 1217 | String authority = underlyingUri.getAuthority(); |
| 1218 | ArrayList<ContentProviderOperation> authOps = batchMap.get(authority); |
| 1219 | if (authOps == null) { |
| 1220 | authOps = new ArrayList<ContentProviderOperation>(); |
| 1221 | batchMap.put(authority, authOps); |
| 1222 | } |
Marc Blank | e1d1b07 | 2012-04-13 17:29:16 -0700 | [diff] [blame] | 1223 | ContentProviderOperation cpo = op.execute(underlyingUri); |
| 1224 | if (cpo != null) { |
| 1225 | authOps.add(cpo); |
| 1226 | } |
Marc Blank | 93b3a15 | 2012-04-11 15:53:19 -0700 | [diff] [blame] | 1227 | // Keep track of whether our operations require recalibrating the cursor position |
| 1228 | if (op.mRecalibrateRequired) { |
| 1229 | recalibrateRequired = true; |
| 1230 | } |
Marc Blank | f892f0a | 2012-01-30 13:04:10 -0800 | [diff] [blame] | 1231 | } |
| 1232 | |
Marc Blank | 93b3a15 | 2012-04-11 15:53:19 -0700 | [diff] [blame] | 1233 | // Recalibrate cursor position if required |
| 1234 | if (recalibrateRequired) { |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1235 | conversationCursor.recalibratePosition(); |
Marc Blank | 93b3a15 | 2012-04-11 15:53:19 -0700 | [diff] [blame] | 1236 | } |
Marc Blank | 958bf4d | 2012-04-05 18:10:55 -0700 | [diff] [blame] | 1237 | |
Marc Blank | fde56a7 | 2012-07-19 17:18:21 -0700 | [diff] [blame] | 1238 | // Notify listeners that data has changed |
| 1239 | conversationCursor.notifyDataChanged(); |
| 1240 | |
Marc Blank | f892f0a | 2012-01-30 13:04:10 -0800 | [diff] [blame] | 1241 | // Send changes to underlying provider |
Vikram Aggarwal | 0823542 | 2012-11-13 10:00:11 -0800 | [diff] [blame] | 1242 | final boolean notUiThread = offUiThread(); |
| 1243 | for (final String authority: batchMap.keySet()) { |
| 1244 | final ArrayList<ContentProviderOperation> opList = batchMap.get(authority); |
| 1245 | if (notUiThread) { |
| 1246 | try { |
Vikram Aggarwal | 7460a1c | 2012-12-28 15:58:58 -0800 | [diff] [blame] | 1247 | mResolver.applyBatch(authority, opList); |
Vikram Aggarwal | 0823542 | 2012-11-13 10:00:11 -0800 | [diff] [blame] | 1248 | } catch (RemoteException e) { |
| 1249 | } catch (OperationApplicationException e) { |
Marc Blank | 8d69d4e | 2012-01-25 12:04:28 -0800 | [diff] [blame] | 1250 | } |
Vikram Aggarwal | 0823542 | 2012-11-13 10:00:11 -0800 | [diff] [blame] | 1251 | } else { |
| 1252 | new Thread(new Runnable() { |
| 1253 | @Override |
| 1254 | public void run() { |
| 1255 | try { |
Vikram Aggarwal | 7460a1c | 2012-12-28 15:58:58 -0800 | [diff] [blame] | 1256 | mResolver.applyBatch(authority, opList); |
Vikram Aggarwal | 0823542 | 2012-11-13 10:00:11 -0800 | [diff] [blame] | 1257 | } catch (RemoteException e) { |
| 1258 | } catch (OperationApplicationException e) { |
| 1259 | } |
| 1260 | } |
| 1261 | }).start(); |
Marc Blank | 8d69d4e | 2012-01-25 12:04:28 -0800 | [diff] [blame] | 1262 | } |
| 1263 | } |
Marc Blank | 1b9efd9 | 2012-02-01 14:27:55 -0800 | [diff] [blame] | 1264 | return sSequence; |
Marc Blank | 8d69d4e | 2012-01-25 12:04:28 -0800 | [diff] [blame] | 1265 | } |
| 1266 | } |
| 1267 | |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1268 | void setMostlyDead(String uriString, Conversation conv) { |
mindyp | 5c1d835 | 2012-11-05 10:12:44 -0800 | [diff] [blame] | 1269 | LogUtils.i(LOG_TAG, "[Mostly dead, deferring: %s] ", uriString); |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1270 | cacheValue(uriString, |
| 1271 | UIProvider.ConversationColumns.FLAGS, Conversation.FLAG_MOSTLY_DEAD); |
| 1272 | conv.convFlags |= Conversation.FLAG_MOSTLY_DEAD; |
Vikram Aggarwal | 8d9313b | 2013-01-03 16:15:10 -0800 | [diff] [blame] | 1273 | mMostlyDead.add(conv); |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1274 | mDeferSync = true; |
| 1275 | } |
| 1276 | |
| 1277 | void commitMostlyDead(Conversation conv) { |
| 1278 | conv.convFlags &= ~Conversation.FLAG_MOSTLY_DEAD; |
Vikram Aggarwal | 8d9313b | 2013-01-03 16:15:10 -0800 | [diff] [blame] | 1279 | mMostlyDead.remove(conv); |
mindyp | 5c1d835 | 2012-11-05 10:12:44 -0800 | [diff] [blame] | 1280 | LogUtils.i(LOG_TAG, "[All dead: %s]", conv.uri); |
Vikram Aggarwal | 8d9313b | 2013-01-03 16:15:10 -0800 | [diff] [blame] | 1281 | if (mMostlyDead.isEmpty()) { |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1282 | mDeferSync = false; |
| 1283 | checkNotifyUI(); |
| 1284 | } |
| 1285 | } |
| 1286 | |
| 1287 | boolean clearMostlyDead(String uriString) { |
| 1288 | Object val = getCachedValue(uriString, |
| 1289 | UIProvider.CONVERSATION_FLAGS_COLUMN); |
| 1290 | if (val != null) { |
| 1291 | int flags = ((Integer)val).intValue(); |
| 1292 | if ((flags & Conversation.FLAG_MOSTLY_DEAD) != 0) { |
| 1293 | cacheValue(uriString, UIProvider.ConversationColumns.FLAGS, |
| 1294 | flags &= ~Conversation.FLAG_MOSTLY_DEAD); |
| 1295 | return true; |
| 1296 | } |
| 1297 | } |
| 1298 | return false; |
| 1299 | } |
| 1300 | |
| 1301 | |
| 1302 | |
| 1303 | |
Marc Blank | 8d69d4e | 2012-01-25 12:04:28 -0800 | [diff] [blame] | 1304 | /** |
| 1305 | * ConversationOperation is the encapsulation of a ContentProvider operation to be performed |
| 1306 | * atomically as part of a "batch" operation. |
| 1307 | */ |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1308 | public class ConversationOperation { |
Marc Blank | e1d1b07 | 2012-04-13 17:29:16 -0700 | [diff] [blame] | 1309 | private static final int MOSTLY = 0x80; |
Marc Blank | 8d69d4e | 2012-01-25 12:04:28 -0800 | [diff] [blame] | 1310 | public static final int DELETE = 0; |
| 1311 | public static final int INSERT = 1; |
| 1312 | public static final int UPDATE = 2; |
Mindy Pereira | f98b318 | 2012-02-22 11:07:13 -0800 | [diff] [blame] | 1313 | public static final int ARCHIVE = 3; |
Mindy Pereira | 830c00f | 2012-02-22 11:43:49 -0800 | [diff] [blame] | 1314 | public static final int MUTE = 4; |
| 1315 | public static final int REPORT_SPAM = 5; |
Paul Westbrook | 77eee62 | 2012-07-10 13:41:57 -0700 | [diff] [blame] | 1316 | public static final int REPORT_NOT_SPAM = 6; |
Paul Westbrook | 76b2062 | 2012-07-12 11:45:43 -0700 | [diff] [blame] | 1317 | public static final int REPORT_PHISHING = 7; |
Paul Westbrook | ef36254 | 2012-08-27 14:53:32 -0700 | [diff] [blame] | 1318 | public static final int DISCARD_DRAFTS = 8; |
Marc Blank | e1d1b07 | 2012-04-13 17:29:16 -0700 | [diff] [blame] | 1319 | public static final int MOSTLY_ARCHIVE = MOSTLY | ARCHIVE; |
| 1320 | public static final int MOSTLY_DELETE = MOSTLY | DELETE; |
Mindy Pereira | 06642fa | 2012-07-12 16:23:27 -0700 | [diff] [blame] | 1321 | public static final int MOSTLY_DESTRUCTIVE_UPDATE = MOSTLY | UPDATE; |
Marc Blank | 8d69d4e | 2012-01-25 12:04:28 -0800 | [diff] [blame] | 1322 | |
| 1323 | private final int mType; |
| 1324 | private final Uri mUri; |
Marc Blank | e1d1b07 | 2012-04-13 17:29:16 -0700 | [diff] [blame] | 1325 | private final Conversation mConversation; |
Marc Blank | 8d69d4e | 2012-01-25 12:04:28 -0800 | [diff] [blame] | 1326 | private final ContentValues mValues; |
Marc Blank | ce53818 | 2012-02-03 13:04:27 -0800 | [diff] [blame] | 1327 | // True if an updated item should be removed locally (from ConversationCursor) |
Mindy Pereira | 30fd47b | 2012-03-09 09:24:00 -0800 | [diff] [blame] | 1328 | // This would be the case for a folder change in which the conversation is no longer |
Marc Blank | ce53818 | 2012-02-03 13:04:27 -0800 | [diff] [blame] | 1329 | // in the folder represented by the ConversationCursor |
| 1330 | private final boolean mLocalDeleteOnUpdate; |
Marc Blank | 93b3a15 | 2012-04-11 15:53:19 -0700 | [diff] [blame] | 1331 | // After execution, this indicates whether or not the operation requires recalibration of |
| 1332 | // the current cursor position (i.e. it removed or added items locally) |
| 1333 | private boolean mRecalibrateRequired = true; |
Marc Blank | e1d1b07 | 2012-04-13 17:29:16 -0700 | [diff] [blame] | 1334 | // Whether this item is already mostly dead |
| 1335 | private final boolean mMostlyDead; |
Marc Blank | 8d69d4e | 2012-01-25 12:04:28 -0800 | [diff] [blame] | 1336 | |
Marc Blank | f892f0a | 2012-01-30 13:04:10 -0800 | [diff] [blame] | 1337 | public ConversationOperation(int type, Conversation conv) { |
Andy Huang | 2c4e6dc | 2012-07-25 18:02:59 -0700 | [diff] [blame] | 1338 | this(type, conv, null); |
Marc Blank | 8d69d4e | 2012-01-25 12:04:28 -0800 | [diff] [blame] | 1339 | } |
| 1340 | |
Andy Huang | 2c4e6dc | 2012-07-25 18:02:59 -0700 | [diff] [blame] | 1341 | public ConversationOperation(int type, Conversation conv, ContentValues values) { |
Marc Blank | 8d69d4e | 2012-01-25 12:04:28 -0800 | [diff] [blame] | 1342 | mType = type; |
Marc Blank | c43bc0a | 2012-02-02 11:25:18 -0800 | [diff] [blame] | 1343 | mUri = conv.uri; |
Marc Blank | e1d1b07 | 2012-04-13 17:29:16 -0700 | [diff] [blame] | 1344 | mConversation = conv; |
Marc Blank | 8d69d4e | 2012-01-25 12:04:28 -0800 | [diff] [blame] | 1345 | mValues = values; |
Marc Blank | ce53818 | 2012-02-03 13:04:27 -0800 | [diff] [blame] | 1346 | mLocalDeleteOnUpdate = conv.localDeleteOnUpdate; |
Marc Blank | e1d1b07 | 2012-04-13 17:29:16 -0700 | [diff] [blame] | 1347 | mMostlyDead = conv.isMostlyDead(); |
Marc Blank | 8d69d4e | 2012-01-25 12:04:28 -0800 | [diff] [blame] | 1348 | } |
| 1349 | |
| 1350 | private ContentProviderOperation execute(Uri underlyingUri) { |
Marc Blank | b31ab5a | 2012-02-01 12:28:29 -0800 | [diff] [blame] | 1351 | Uri uri = underlyingUri.buildUpon() |
Marc Blank | dd10bc8 | 2012-02-01 19:10:46 -0800 | [diff] [blame] | 1352 | .appendQueryParameter(UIProvider.SEQUENCE_QUERY_PARAMETER, |
| 1353 | Integer.toString(sSequence)) |
Marc Blank | b31ab5a | 2012-02-01 12:28:29 -0800 | [diff] [blame] | 1354 | .build(); |
Marc Blank | e1d1b07 | 2012-04-13 17:29:16 -0700 | [diff] [blame] | 1355 | ContentProviderOperation op = null; |
Marc Blank | 8d69d4e | 2012-01-25 12:04:28 -0800 | [diff] [blame] | 1356 | switch(mType) { |
Marc Blank | 8d69d4e | 2012-01-25 12:04:28 -0800 | [diff] [blame] | 1357 | case UPDATE: |
Marc Blank | ce53818 | 2012-02-03 13:04:27 -0800 | [diff] [blame] | 1358 | if (mLocalDeleteOnUpdate) { |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1359 | sProvider.deleteLocal(mUri, ConversationCursor.this); |
Marc Blank | ce53818 | 2012-02-03 13:04:27 -0800 | [diff] [blame] | 1360 | } else { |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1361 | sProvider.updateLocal(mUri, mValues, ConversationCursor.this); |
Marc Blank | 93b3a15 | 2012-04-11 15:53:19 -0700 | [diff] [blame] | 1362 | mRecalibrateRequired = false; |
Marc Blank | ce53818 | 2012-02-03 13:04:27 -0800 | [diff] [blame] | 1363 | } |
Mindy Pereira | 06642fa | 2012-07-12 16:23:27 -0700 | [diff] [blame] | 1364 | if (!mMostlyDead) { |
| 1365 | op = ContentProviderOperation.newUpdate(uri) |
| 1366 | .withValues(mValues) |
| 1367 | .build(); |
| 1368 | } else { |
| 1369 | sProvider.commitMostlyDead(mConversation, ConversationCursor.this); |
| 1370 | } |
| 1371 | break; |
| 1372 | case MOSTLY_DESTRUCTIVE_UPDATE: |
| 1373 | sProvider.setMostlyDead(mConversation, ConversationCursor.this); |
| 1374 | op = ContentProviderOperation.newUpdate(uri).withValues(mValues).build(); |
Andy Huang | 397621b | 2012-03-14 20:52:39 -0700 | [diff] [blame] | 1375 | break; |
Marc Blank | 8d69d4e | 2012-01-25 12:04:28 -0800 | [diff] [blame] | 1376 | case INSERT: |
| 1377 | sProvider.insertLocal(mUri, mValues); |
Andy Huang | 397621b | 2012-03-14 20:52:39 -0700 | [diff] [blame] | 1378 | op = ContentProviderOperation.newInsert(uri) |
Marc Blank | 8d69d4e | 2012-01-25 12:04:28 -0800 | [diff] [blame] | 1379 | .withValues(mValues).build(); |
Andy Huang | 397621b | 2012-03-14 20:52:39 -0700 | [diff] [blame] | 1380 | break; |
Marc Blank | e1d1b07 | 2012-04-13 17:29:16 -0700 | [diff] [blame] | 1381 | // Destructive actions below! |
| 1382 | // "Mostly" operations are reflected globally, but not locally, except to set |
| 1383 | // FLAG_MOSTLY_DEAD in the conversation itself |
| 1384 | case DELETE: |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1385 | sProvider.deleteLocal(mUri, ConversationCursor.this); |
Marc Blank | e1d1b07 | 2012-04-13 17:29:16 -0700 | [diff] [blame] | 1386 | if (!mMostlyDead) { |
| 1387 | op = ContentProviderOperation.newDelete(uri).build(); |
| 1388 | } else { |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1389 | sProvider.commitMostlyDead(mConversation, ConversationCursor.this); |
Marc Blank | e1d1b07 | 2012-04-13 17:29:16 -0700 | [diff] [blame] | 1390 | } |
| 1391 | break; |
| 1392 | case MOSTLY_DELETE: |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1393 | sProvider.setMostlyDead(mConversation,ConversationCursor.this); |
Marc Blank | e1d1b07 | 2012-04-13 17:29:16 -0700 | [diff] [blame] | 1394 | op = ContentProviderOperation.newDelete(uri).build(); |
| 1395 | break; |
Mindy Pereira | f98b318 | 2012-02-22 11:07:13 -0800 | [diff] [blame] | 1396 | case ARCHIVE: |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1397 | sProvider.deleteLocal(mUri, ConversationCursor.this); |
Marc Blank | e1d1b07 | 2012-04-13 17:29:16 -0700 | [diff] [blame] | 1398 | if (!mMostlyDead) { |
| 1399 | // Create an update operation that represents archive |
| 1400 | op = ContentProviderOperation.newUpdate(uri).withValue( |
| 1401 | ConversationOperations.OPERATION_KEY, |
| 1402 | ConversationOperations.ARCHIVE) |
| 1403 | .build(); |
| 1404 | } else { |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1405 | sProvider.commitMostlyDead(mConversation, ConversationCursor.this); |
Marc Blank | e1d1b07 | 2012-04-13 17:29:16 -0700 | [diff] [blame] | 1406 | } |
| 1407 | break; |
| 1408 | case MOSTLY_ARCHIVE: |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1409 | sProvider.setMostlyDead(mConversation, ConversationCursor.this); |
Paul Westbrook | 334e64a | 2012-02-23 13:26:35 -0800 | [diff] [blame] | 1410 | // Create an update operation that represents archive |
Andy Huang | 397621b | 2012-03-14 20:52:39 -0700 | [diff] [blame] | 1411 | op = ContentProviderOperation.newUpdate(uri).withValue( |
Paul Westbrook | 334e64a | 2012-02-23 13:26:35 -0800 | [diff] [blame] | 1412 | ConversationOperations.OPERATION_KEY, ConversationOperations.ARCHIVE) |
| 1413 | .build(); |
Andy Huang | 397621b | 2012-03-14 20:52:39 -0700 | [diff] [blame] | 1414 | break; |
Mindy Pereira | 830c00f | 2012-02-22 11:43:49 -0800 | [diff] [blame] | 1415 | case MUTE: |
Paul Westbrook | 334e64a | 2012-02-23 13:26:35 -0800 | [diff] [blame] | 1416 | if (mLocalDeleteOnUpdate) { |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1417 | sProvider.deleteLocal(mUri, ConversationCursor.this); |
Paul Westbrook | 334e64a | 2012-02-23 13:26:35 -0800 | [diff] [blame] | 1418 | } |
| 1419 | |
| 1420 | // Create an update operation that represents mute |
Andy Huang | 397621b | 2012-03-14 20:52:39 -0700 | [diff] [blame] | 1421 | op = ContentProviderOperation.newUpdate(uri).withValue( |
Paul Westbrook | 334e64a | 2012-02-23 13:26:35 -0800 | [diff] [blame] | 1422 | ConversationOperations.OPERATION_KEY, ConversationOperations.MUTE) |
| 1423 | .build(); |
Andy Huang | 397621b | 2012-03-14 20:52:39 -0700 | [diff] [blame] | 1424 | break; |
Mindy Pereira | 830c00f | 2012-02-22 11:43:49 -0800 | [diff] [blame] | 1425 | case REPORT_SPAM: |
Paul Westbrook | 77eee62 | 2012-07-10 13:41:57 -0700 | [diff] [blame] | 1426 | case REPORT_NOT_SPAM: |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1427 | sProvider.deleteLocal(mUri, ConversationCursor.this); |
Paul Westbrook | 334e64a | 2012-02-23 13:26:35 -0800 | [diff] [blame] | 1428 | |
Paul Westbrook | 77eee62 | 2012-07-10 13:41:57 -0700 | [diff] [blame] | 1429 | final String operation = mType == REPORT_SPAM ? |
| 1430 | ConversationOperations.REPORT_SPAM : |
| 1431 | ConversationOperations.REPORT_NOT_SPAM; |
| 1432 | |
Paul Westbrook | 334e64a | 2012-02-23 13:26:35 -0800 | [diff] [blame] | 1433 | // Create an update operation that represents report spam |
Andy Huang | 397621b | 2012-03-14 20:52:39 -0700 | [diff] [blame] | 1434 | op = ContentProviderOperation.newUpdate(uri).withValue( |
Paul Westbrook | 77eee62 | 2012-07-10 13:41:57 -0700 | [diff] [blame] | 1435 | ConversationOperations.OPERATION_KEY, operation).build(); |
Andy Huang | 397621b | 2012-03-14 20:52:39 -0700 | [diff] [blame] | 1436 | break; |
Paul Westbrook | 76b2062 | 2012-07-12 11:45:43 -0700 | [diff] [blame] | 1437 | case REPORT_PHISHING: |
| 1438 | sProvider.deleteLocal(mUri, ConversationCursor.this); |
| 1439 | |
Paul Westbrook | ef36254 | 2012-08-27 14:53:32 -0700 | [diff] [blame] | 1440 | // Create an update operation that represents report phishing |
Paul Westbrook | 76b2062 | 2012-07-12 11:45:43 -0700 | [diff] [blame] | 1441 | op = ContentProviderOperation.newUpdate(uri).withValue( |
| 1442 | ConversationOperations.OPERATION_KEY, |
| 1443 | ConversationOperations.REPORT_PHISHING).build(); |
| 1444 | break; |
Paul Westbrook | ef36254 | 2012-08-27 14:53:32 -0700 | [diff] [blame] | 1445 | case DISCARD_DRAFTS: |
| 1446 | sProvider.deleteLocal(mUri, ConversationCursor.this); |
| 1447 | |
| 1448 | // Create an update operation that represents discarding drafts |
| 1449 | op = ContentProviderOperation.newUpdate(uri).withValue( |
| 1450 | ConversationOperations.OPERATION_KEY, |
| 1451 | ConversationOperations.DISCARD_DRAFTS).build(); |
| 1452 | break; |
Marc Blank | 8d69d4e | 2012-01-25 12:04:28 -0800 | [diff] [blame] | 1453 | default: |
| 1454 | throw new UnsupportedOperationException( |
| 1455 | "No such ConversationOperation type: " + mType); |
| 1456 | } |
Andy Huang | 397621b | 2012-03-14 20:52:39 -0700 | [diff] [blame] | 1457 | |
Andy Huang | 397621b | 2012-03-14 20:52:39 -0700 | [diff] [blame] | 1458 | return op; |
Marc Blank | c8a9942 | 2012-01-19 14:27:47 -0800 | [diff] [blame] | 1459 | } |
| 1460 | } |
Marc Blank | 97bca7b | 2012-01-24 11:17:00 -0800 | [diff] [blame] | 1461 | |
| 1462 | /** |
| 1463 | * For now, a single listener can be associated with the cursor, and for now we'll just |
| 1464 | * notify on deletions |
| 1465 | */ |
| 1466 | public interface ConversationListener { |
Marc Blank | bec5115 | 2012-03-22 19:27:34 -0700 | [diff] [blame] | 1467 | /** |
| 1468 | * Data in the underlying provider has changed; a refresh is required to sync up |
| 1469 | */ |
Marc Blank | 48eba7a | 2012-01-27 16:16:19 -0800 | [diff] [blame] | 1470 | public void onRefreshRequired(); |
Marc Blank | bec5115 | 2012-03-22 19:27:34 -0700 | [diff] [blame] | 1471 | /** |
| 1472 | * We've completed a requested refresh of the underlying cursor |
| 1473 | */ |
Marc Blank | 48eba7a | 2012-01-27 16:16:19 -0800 | [diff] [blame] | 1474 | public void onRefreshReady(); |
Marc Blank | bec5115 | 2012-03-22 19:27:34 -0700 | [diff] [blame] | 1475 | /** |
| 1476 | * The data underlying the cursor has changed; the UI should redraw the list |
| 1477 | */ |
| 1478 | public void onDataSetChanged(); |
Marc Blank | 48eba7a | 2012-01-27 16:16:19 -0800 | [diff] [blame] | 1479 | } |
| 1480 | |
| 1481 | @Override |
| 1482 | public boolean isFirst() { |
| 1483 | throw new UnsupportedOperationException(); |
| 1484 | } |
| 1485 | |
| 1486 | @Override |
| 1487 | public boolean isLast() { |
| 1488 | throw new UnsupportedOperationException(); |
| 1489 | } |
| 1490 | |
| 1491 | @Override |
| 1492 | public boolean isBeforeFirst() { |
| 1493 | throw new UnsupportedOperationException(); |
| 1494 | } |
| 1495 | |
| 1496 | @Override |
| 1497 | public boolean isAfterLast() { |
| 1498 | throw new UnsupportedOperationException(); |
| 1499 | } |
| 1500 | |
| 1501 | @Override |
| 1502 | public int getColumnIndex(String columnName) { |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1503 | return mUnderlyingCursor.getColumnIndex(columnName); |
Marc Blank | 48eba7a | 2012-01-27 16:16:19 -0800 | [diff] [blame] | 1504 | } |
| 1505 | |
| 1506 | @Override |
| 1507 | public int getColumnIndexOrThrow(String columnName) throws IllegalArgumentException { |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1508 | return mUnderlyingCursor.getColumnIndexOrThrow(columnName); |
Marc Blank | 48eba7a | 2012-01-27 16:16:19 -0800 | [diff] [blame] | 1509 | } |
| 1510 | |
| 1511 | @Override |
| 1512 | public String getColumnName(int columnIndex) { |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1513 | return mUnderlyingCursor.getColumnName(columnIndex); |
Marc Blank | 48eba7a | 2012-01-27 16:16:19 -0800 | [diff] [blame] | 1514 | } |
| 1515 | |
| 1516 | @Override |
| 1517 | public String[] getColumnNames() { |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1518 | return mUnderlyingCursor.getColumnNames(); |
Marc Blank | 48eba7a | 2012-01-27 16:16:19 -0800 | [diff] [blame] | 1519 | } |
| 1520 | |
| 1521 | @Override |
| 1522 | public int getColumnCount() { |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1523 | return mUnderlyingCursor.getColumnCount(); |
Marc Blank | 48eba7a | 2012-01-27 16:16:19 -0800 | [diff] [blame] | 1524 | } |
| 1525 | |
| 1526 | @Override |
| 1527 | public void copyStringToBuffer(int columnIndex, CharArrayBuffer buffer) { |
| 1528 | throw new UnsupportedOperationException(); |
| 1529 | } |
| 1530 | |
| 1531 | @Override |
| 1532 | public int getType(int columnIndex) { |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1533 | return mUnderlyingCursor.getType(columnIndex); |
Marc Blank | 48eba7a | 2012-01-27 16:16:19 -0800 | [diff] [blame] | 1534 | } |
| 1535 | |
| 1536 | @Override |
| 1537 | public boolean isNull(int columnIndex) { |
| 1538 | throw new UnsupportedOperationException(); |
| 1539 | } |
| 1540 | |
| 1541 | @Override |
| 1542 | public void deactivate() { |
| 1543 | throw new UnsupportedOperationException(); |
| 1544 | } |
| 1545 | |
| 1546 | @Override |
| 1547 | public boolean isClosed() { |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1548 | return mUnderlyingCursor == null || mUnderlyingCursor.isClosed(); |
Marc Blank | 48eba7a | 2012-01-27 16:16:19 -0800 | [diff] [blame] | 1549 | } |
| 1550 | |
| 1551 | @Override |
| 1552 | public void registerContentObserver(ContentObserver observer) { |
Andy Huang | 397621b | 2012-03-14 20:52:39 -0700 | [diff] [blame] | 1553 | // Nope. We never notify of underlying changes on this channel, since the cursor watches |
| 1554 | // internally and offers onRefreshRequired/onRefreshReady to accomplish the same thing. |
Marc Blank | 48eba7a | 2012-01-27 16:16:19 -0800 | [diff] [blame] | 1555 | } |
| 1556 | |
| 1557 | @Override |
| 1558 | public void unregisterContentObserver(ContentObserver observer) { |
Andy Huang | 397621b | 2012-03-14 20:52:39 -0700 | [diff] [blame] | 1559 | // See above. |
Marc Blank | 48eba7a | 2012-01-27 16:16:19 -0800 | [diff] [blame] | 1560 | } |
| 1561 | |
| 1562 | @Override |
| 1563 | public void registerDataSetObserver(DataSetObserver observer) { |
Andy Huang | 2c4e6dc | 2012-07-25 18:02:59 -0700 | [diff] [blame] | 1564 | // Nope. We use ConversationListener to accomplish this. |
Marc Blank | 48eba7a | 2012-01-27 16:16:19 -0800 | [diff] [blame] | 1565 | } |
| 1566 | |
| 1567 | @Override |
| 1568 | public void unregisterDataSetObserver(DataSetObserver observer) { |
Andy Huang | 2c4e6dc | 2012-07-25 18:02:59 -0700 | [diff] [blame] | 1569 | // See above. |
Marc Blank | 48eba7a | 2012-01-27 16:16:19 -0800 | [diff] [blame] | 1570 | } |
| 1571 | |
| 1572 | @Override |
| 1573 | public void setNotificationUri(ContentResolver cr, Uri uri) { |
| 1574 | throw new UnsupportedOperationException(); |
| 1575 | } |
| 1576 | |
| 1577 | @Override |
| 1578 | public boolean getWantsAllOnMoveCalls() { |
| 1579 | throw new UnsupportedOperationException(); |
| 1580 | } |
| 1581 | |
| 1582 | @Override |
| 1583 | public Bundle getExtras() { |
Mindy Pereira | 70a70c9 | 2012-08-02 08:39:45 -0700 | [diff] [blame] | 1584 | return mUnderlyingCursor != null ? mUnderlyingCursor.getExtras() : Bundle.EMPTY; |
Marc Blank | 48eba7a | 2012-01-27 16:16:19 -0800 | [diff] [blame] | 1585 | } |
| 1586 | |
| 1587 | @Override |
| 1588 | public Bundle respond(Bundle extras) { |
Paul Westbrook | 606a6a1 | 2012-04-24 21:40:31 -0700 | [diff] [blame] | 1589 | if (mUnderlyingCursor != null) { |
| 1590 | return mUnderlyingCursor.respond(extras); |
| 1591 | } |
| 1592 | return Bundle.EMPTY; |
Marc Blank | 48eba7a | 2012-01-27 16:16:19 -0800 | [diff] [blame] | 1593 | } |
| 1594 | |
| 1595 | @Override |
| 1596 | public boolean requery() { |
| 1597 | return true; |
Marc Blank | 97bca7b | 2012-01-24 11:17:00 -0800 | [diff] [blame] | 1598 | } |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1599 | |
| 1600 | // Below are methods that update Conversation data (update/delete) |
| 1601 | |
| 1602 | public int updateBoolean(Context context, Conversation conversation, String columnName, |
| 1603 | boolean value) { |
| 1604 | return updateBoolean(context, Arrays.asList(conversation), columnName, value); |
| 1605 | } |
| 1606 | |
| 1607 | /** |
| 1608 | * Update an integer column for a group of conversations (see updateValues below) |
| 1609 | */ |
| 1610 | public int updateInt(Context context, Collection<Conversation> conversations, |
| 1611 | String columnName, int value) { |
| 1612 | ContentValues cv = new ContentValues(); |
| 1613 | cv.put(columnName, value); |
| 1614 | return updateValues(context, conversations, cv); |
| 1615 | } |
| 1616 | |
| 1617 | /** |
| 1618 | * Update a string column for a group of conversations (see updateValues below) |
| 1619 | */ |
| 1620 | public int updateBoolean(Context context, Collection<Conversation> conversations, |
| 1621 | String columnName, boolean value) { |
| 1622 | ContentValues cv = new ContentValues(); |
| 1623 | cv.put(columnName, value); |
| 1624 | return updateValues(context, conversations, cv); |
| 1625 | } |
| 1626 | |
| 1627 | /** |
| 1628 | * Update a string column for a group of conversations (see updateValues below) |
| 1629 | */ |
| 1630 | public int updateString(Context context, Collection<Conversation> conversations, |
| 1631 | String columnName, String value) { |
Paul Westbrook | 26746eb | 2012-12-06 14:44:01 -0800 | [diff] [blame] | 1632 | return updateStrings(context, conversations, new String[]{ |
| 1633 | columnName |
| 1634 | }, new String[]{ |
| 1635 | value |
Mindy Pereira | ebdfd98 | 2012-07-13 09:22:01 -0700 | [diff] [blame] | 1636 | }); |
| 1637 | } |
| 1638 | |
| 1639 | /** |
| 1640 | * Update a string columns for a group of conversations (see updateValues below) |
| 1641 | */ |
| 1642 | public int updateStrings(Context context, Collection<Conversation> conversations, |
mindyp | 389f0b2 | 2012-08-29 11:12:54 -0700 | [diff] [blame] | 1643 | String columnName, ArrayList<String> values) { |
| 1644 | ArrayList<ConversationOperation> operations = new ArrayList<ConversationOperation>(); |
| 1645 | int i = 0; |
| 1646 | ContentValues cv = new ContentValues(); |
| 1647 | for (Conversation c : conversations) { |
| 1648 | cv.put(columnName, values.get(i)); |
| 1649 | operations.add(getOperationForConversation(c, ConversationOperation.UPDATE, cv)); |
| 1650 | } |
| 1651 | return apply(context, operations); |
| 1652 | } |
| 1653 | |
| 1654 | /** |
| 1655 | * Update a string columns for a group of conversations (see updateValues below) |
| 1656 | */ |
| 1657 | public int updateStrings(Context context, Collection<Conversation> conversations, |
Mindy Pereira | ebdfd98 | 2012-07-13 09:22:01 -0700 | [diff] [blame] | 1658 | String[] columnNames, String[] values) { |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1659 | ContentValues cv = new ContentValues(); |
Mindy Pereira | ebdfd98 | 2012-07-13 09:22:01 -0700 | [diff] [blame] | 1660 | for (int i = 0; i < columnNames.length; i++) { |
| 1661 | cv.put(columnNames[i], values[i]); |
| 1662 | } |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1663 | return updateValues(context, conversations, cv); |
| 1664 | } |
| 1665 | |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1666 | /** |
| 1667 | * Update a boolean column for a group of conversations, immediately in the UI and in a single |
| 1668 | * transaction in the underlying provider |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1669 | * @param context the caller's context |
Andy Huang | 2c4e6dc | 2012-07-25 18:02:59 -0700 | [diff] [blame] | 1670 | * @param conversations a collection of conversations |
| 1671 | * @param values the data to update |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1672 | * @return the sequence number of the operation (for undo) |
| 1673 | */ |
Mindy Pereira | 6c2663d | 2012-07-20 15:37:29 -0700 | [diff] [blame] | 1674 | public int updateValues(Context context, Collection<Conversation> conversations, |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1675 | ContentValues values) { |
| 1676 | return apply(context, |
| 1677 | getOperationsForConversations(conversations, ConversationOperation.UPDATE, values)); |
| 1678 | } |
| 1679 | |
Vikram Aggarwal | cc754b1 | 2012-08-30 14:04:21 -0700 | [diff] [blame] | 1680 | /** |
| 1681 | * Apply many operations in a single batch transaction. |
| 1682 | * @param context the caller's context |
| 1683 | * @param op the collection of operations obtained through successive calls to |
| 1684 | * {@link #getOperationForConversation(Conversation, int, ContentValues)}. |
| 1685 | * @return the sequence number of the operation (for undo) |
| 1686 | */ |
| 1687 | public int updateBulkValues(Context context, Collection<ConversationOperation> op) { |
| 1688 | return apply(context, op); |
| 1689 | } |
| 1690 | |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1691 | private ArrayList<ConversationOperation> getOperationsForConversations( |
Andy Huang | 2c4e6dc | 2012-07-25 18:02:59 -0700 | [diff] [blame] | 1692 | Collection<Conversation> conversations, int type, ContentValues values) { |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1693 | final ArrayList<ConversationOperation> ops = Lists.newArrayList(); |
| 1694 | for (Conversation conv: conversations) { |
mindyp | 389f0b2 | 2012-08-29 11:12:54 -0700 | [diff] [blame] | 1695 | ops.add(getOperationForConversation(conv, type, values)); |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1696 | } |
| 1697 | return ops; |
| 1698 | } |
| 1699 | |
Vikram Aggarwal | cc754b1 | 2012-08-30 14:04:21 -0700 | [diff] [blame] | 1700 | public ConversationOperation getOperationForConversation(Conversation conv, int type, |
mindyp | 389f0b2 | 2012-08-29 11:12:54 -0700 | [diff] [blame] | 1701 | ContentValues values) { |
| 1702 | return new ConversationOperation(type, conv, values); |
| 1703 | } |
| 1704 | |
mindyp | 5cc0ab2 | 2012-12-11 08:47:35 -0800 | [diff] [blame] | 1705 | public void addFolderUpdates(ArrayList<Uri> folderUris, ArrayList<Boolean> add, |
Paul Westbrook | 26746eb | 2012-12-06 14:44:01 -0800 | [diff] [blame] | 1706 | ContentValues values) { |
mindyp | cb0b30e | 2012-11-30 10:16:35 -0800 | [diff] [blame] | 1707 | ArrayList<String> folders = new ArrayList<String>(); |
| 1708 | for (int i = 0; i < folderUris.size(); i++) { |
| 1709 | folders.add(folderUris.get(i).buildUpon().appendPath(add.get(i) + "").toString()); |
| 1710 | } |
| 1711 | values.put(ConversationOperations.FOLDERS_UPDATED, |
| 1712 | TextUtils.join(ConversationOperations.FOLDERS_UPDATED_SPLIT_PATTERN, folders)); |
Paul Westbrook | 26746eb | 2012-12-06 14:44:01 -0800 | [diff] [blame] | 1713 | } |
| 1714 | |
mindyp | 5cc0ab2 | 2012-12-11 08:47:35 -0800 | [diff] [blame] | 1715 | public void addTargetFolders(Collection<Folder> targetFolders, ContentValues values) { |
Andy Huang | b2033d8 | 2012-12-07 19:30:57 -0800 | [diff] [blame] | 1716 | values.put(Conversation.UPDATE_FOLDER_COLUMN, FolderList.copyOf(targetFolders).toBlob()); |
Paul Westbrook | 26746eb | 2012-12-06 14:44:01 -0800 | [diff] [blame] | 1717 | } |
| 1718 | |
| 1719 | public ConversationOperation getConversationFolderOperation(Conversation conv, |
| 1720 | ArrayList<Uri> folderUris, ArrayList<Boolean> add, Collection<Folder> targetFolders) { |
mindyp | 5cc0ab2 | 2012-12-11 08:47:35 -0800 | [diff] [blame] | 1721 | return getConversationFolderOperation(conv, folderUris, add, targetFolders, |
| 1722 | new ContentValues()); |
| 1723 | } |
| 1724 | |
| 1725 | public ConversationOperation getConversationFolderOperation(Conversation conv, |
| 1726 | ArrayList<Uri> folderUris, ArrayList<Boolean> add, Collection<Folder> targetFolders, |
| 1727 | ContentValues values) { |
Paul Westbrook | 26746eb | 2012-12-06 14:44:01 -0800 | [diff] [blame] | 1728 | addFolderUpdates(folderUris, add, values); |
Paul Westbrook | 26746eb | 2012-12-06 14:44:01 -0800 | [diff] [blame] | 1729 | addTargetFolders(targetFolders, values); |
| 1730 | return getOperationForConversation(conv, ConversationOperation.UPDATE, values); |
mindyp | cb0b30e | 2012-11-30 10:16:35 -0800 | [diff] [blame] | 1731 | } |
| 1732 | |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1733 | /** |
| 1734 | * Delete a single conversation |
| 1735 | * @param context the caller's context |
| 1736 | * @return the sequence number of the operation (for undo) |
| 1737 | */ |
| 1738 | public int delete(Context context, Conversation conversation) { |
| 1739 | ArrayList<Conversation> conversations = new ArrayList<Conversation>(); |
| 1740 | conversations.add(conversation); |
| 1741 | return delete(context, conversations); |
| 1742 | } |
| 1743 | |
| 1744 | /** |
| 1745 | * Delete a single conversation |
| 1746 | * @param context the caller's context |
| 1747 | * @return the sequence number of the operation (for undo) |
| 1748 | */ |
| 1749 | public int mostlyArchive(Context context, Conversation conversation) { |
| 1750 | ArrayList<Conversation> conversations = new ArrayList<Conversation>(); |
| 1751 | conversations.add(conversation); |
| 1752 | return archive(context, conversations); |
| 1753 | } |
| 1754 | |
| 1755 | /** |
| 1756 | * Delete a single conversation |
| 1757 | * @param context the caller's context |
| 1758 | * @return the sequence number of the operation (for undo) |
| 1759 | */ |
| 1760 | public int mostlyDelete(Context context, Conversation conversation) { |
| 1761 | ArrayList<Conversation> conversations = new ArrayList<Conversation>(); |
| 1762 | conversations.add(conversation); |
| 1763 | return delete(context, conversations); |
| 1764 | } |
| 1765 | |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1766 | // Convenience methods |
Vikram Aggarwal | cc754b1 | 2012-08-30 14:04:21 -0700 | [diff] [blame] | 1767 | private int apply(Context context, Collection<ConversationOperation> operations) { |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1768 | return sProvider.apply(operations, this); |
| 1769 | } |
| 1770 | |
| 1771 | private void undoLocal() { |
| 1772 | sProvider.undo(this); |
| 1773 | } |
| 1774 | |
| 1775 | public void undo(final Context context, final Uri undoUri) { |
| 1776 | new Thread(new Runnable() { |
| 1777 | @Override |
| 1778 | public void run() { |
| 1779 | Cursor c = context.getContentResolver().query(undoUri, UIProvider.UNDO_PROJECTION, |
| 1780 | null, null, null); |
| 1781 | if (c != null) { |
| 1782 | c.close(); |
| 1783 | } |
| 1784 | } |
| 1785 | }).start(); |
| 1786 | undoLocal(); |
| 1787 | } |
| 1788 | |
| 1789 | /** |
| 1790 | * Delete a group of conversations immediately in the UI and in a single transaction in the |
| 1791 | * underlying provider. See applyAction for argument descriptions |
| 1792 | */ |
| 1793 | public int delete(Context context, Collection<Conversation> conversations) { |
| 1794 | return applyAction(context, conversations, ConversationOperation.DELETE); |
| 1795 | } |
| 1796 | |
| 1797 | /** |
| 1798 | * As above, for archive |
| 1799 | */ |
| 1800 | public int archive(Context context, Collection<Conversation> conversations) { |
| 1801 | return applyAction(context, conversations, ConversationOperation.ARCHIVE); |
| 1802 | } |
| 1803 | |
| 1804 | /** |
| 1805 | * As above, for mute |
| 1806 | */ |
| 1807 | public int mute(Context context, Collection<Conversation> conversations) { |
| 1808 | return applyAction(context, conversations, ConversationOperation.MUTE); |
| 1809 | } |
| 1810 | |
| 1811 | /** |
| 1812 | * As above, for report spam |
| 1813 | */ |
| 1814 | public int reportSpam(Context context, Collection<Conversation> conversations) { |
| 1815 | return applyAction(context, conversations, ConversationOperation.REPORT_SPAM); |
| 1816 | } |
| 1817 | |
| 1818 | /** |
Paul Westbrook | 77eee62 | 2012-07-10 13:41:57 -0700 | [diff] [blame] | 1819 | * As above, for report not spam |
| 1820 | */ |
| 1821 | public int reportNotSpam(Context context, Collection<Conversation> conversations) { |
| 1822 | return applyAction(context, conversations, ConversationOperation.REPORT_NOT_SPAM); |
| 1823 | } |
| 1824 | |
| 1825 | /** |
Paul Westbrook | 76b2062 | 2012-07-12 11:45:43 -0700 | [diff] [blame] | 1826 | * As above, for report phishing |
| 1827 | */ |
| 1828 | public int reportPhishing(Context context, Collection<Conversation> conversations) { |
| 1829 | return applyAction(context, conversations, ConversationOperation.REPORT_PHISHING); |
| 1830 | } |
| 1831 | |
| 1832 | /** |
Paul Westbrook | ef36254 | 2012-08-27 14:53:32 -0700 | [diff] [blame] | 1833 | * Discard the drafts in the specified conversations |
| 1834 | */ |
| 1835 | public int discardDrafts(Context context, Collection<Conversation> conversations) { |
| 1836 | return applyAction(context, conversations, ConversationOperation.DISCARD_DRAFTS); |
| 1837 | } |
| 1838 | |
| 1839 | /** |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1840 | * As above, for mostly archive |
| 1841 | */ |
| 1842 | public int mostlyArchive(Context context, Collection<Conversation> conversations) { |
| 1843 | return applyAction(context, conversations, ConversationOperation.MOSTLY_ARCHIVE); |
| 1844 | } |
| 1845 | |
| 1846 | /** |
| 1847 | * As above, for mostly delete |
| 1848 | */ |
| 1849 | public int mostlyDelete(Context context, Collection<Conversation> conversations) { |
| 1850 | return applyAction(context, conversations, ConversationOperation.MOSTLY_DELETE); |
| 1851 | } |
| 1852 | |
| 1853 | /** |
Mindy Pereira | ebdfd98 | 2012-07-13 09:22:01 -0700 | [diff] [blame] | 1854 | * As above, for mostly destructive updates. |
| 1855 | */ |
| 1856 | public int mostlyDestructiveUpdate(Context context, Collection<Conversation> conversations, |
Andy Huang | b2033d8 | 2012-12-07 19:30:57 -0800 | [diff] [blame] | 1857 | ContentValues values) { |
Mindy Pereira | 06642fa | 2012-07-12 16:23:27 -0700 | [diff] [blame] | 1858 | return apply( |
| 1859 | context, |
| 1860 | getOperationsForConversations(conversations, |
Andy Huang | b2033d8 | 2012-12-07 19:30:57 -0800 | [diff] [blame] | 1861 | ConversationOperation.MOSTLY_DESTRUCTIVE_UPDATE, values)); |
Mindy Pereira | 06642fa | 2012-07-12 16:23:27 -0700 | [diff] [blame] | 1862 | } |
| 1863 | |
| 1864 | /** |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 1865 | * Convenience method for performing an operation on a group of conversations |
| 1866 | * @param context the caller's context |
| 1867 | * @param conversations the conversations to be affected |
| 1868 | * @param opAction the action to take |
| 1869 | * @return the sequence number of the operation applied in CC |
| 1870 | */ |
| 1871 | private int applyAction(Context context, Collection<Conversation> conversations, |
| 1872 | int opAction) { |
| 1873 | ArrayList<ConversationOperation> ops = Lists.newArrayList(); |
| 1874 | for (Conversation conv: conversations) { |
| 1875 | ConversationOperation op = |
| 1876 | new ConversationOperation(opAction, conv); |
| 1877 | ops.add(op); |
| 1878 | } |
| 1879 | return apply(context, ops); |
| 1880 | } |
| 1881 | |
Vikram Aggarwal | 81a4f08 | 2012-09-28 09:19:04 -0700 | [diff] [blame] | 1882 | /** |
| 1883 | * Do not make this method dependent on the internal mechanism of the cursor. |
| 1884 | * Currently just calls the parent implementation. If this is ever overriden, take care to |
| 1885 | * ensure that two references map to the same hashcode. If |
| 1886 | * ConversationCursor first == ConversationCursor second, |
| 1887 | * then |
| 1888 | * first.hashCode() == second.hashCode(). |
| 1889 | * The {@link ConversationListFragment} relies on this behavior of |
| 1890 | * {@link ConversationCursor#hashCode()} to avoid storing dangerous references to the cursor. |
| 1891 | * {@inheritDoc} |
| 1892 | */ |
| 1893 | @Override |
| 1894 | public int hashCode() { |
| 1895 | return super.hashCode(); |
| 1896 | } |
Vikram Aggarwal | 55d664a | 2012-11-13 10:25:56 -0800 | [diff] [blame] | 1897 | } |