blob: fd81b7043fd65e3a597d573b69830a06094f3365 [file] [log] [blame]
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -08001/*******************************************************************************
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
18package com.android.mail.ui;
19
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -080020import android.app.ActionBar;
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -080021import android.app.ActionBar.LayoutParams;
Vikram Aggarwala55b36c2012-01-13 11:45:02 -080022import android.app.Activity;
Vikram Aggarwal54452ae2012-03-13 15:29:00 -070023import android.app.AlertDialog;
Vikram Aggarwala55b36c2012-01-13 11:45:02 -080024import android.app.Dialog;
Vikram Aggarwal6902dcf2012-04-11 08:57:42 -070025import android.app.Fragment;
Paul Westbrook2d50bcd2012-04-10 11:53:47 -070026import android.app.FragmentManager;
Andy Huangf9a73482012-03-13 15:54:02 -070027import android.app.LoaderManager;
Vikram Aggarwale620a7a2012-03-28 13:16:14 -070028import android.app.SearchManager;
Vikram Aggarwal80aeac52012-02-07 15:27:20 -080029import android.content.ContentResolver;
Vikram Aggarwala55b36c2012-01-13 11:45:02 -080030import android.content.Context;
Vikram Aggarwal7dedb952012-02-16 16:10:23 -080031import android.content.CursorLoader;
Vikram Aggarwal54452ae2012-03-13 15:29:00 -070032import android.content.DialogInterface;
Vikram Aggarwala55b36c2012-01-13 11:45:02 -080033import android.content.Intent;
Vikram Aggarwal7dedb952012-02-16 16:10:23 -080034import android.content.Loader;
Vikram Aggarwal80aeac52012-02-07 15:27:20 -080035import android.database.Cursor;
Andy Huang632721e2012-04-11 16:57:26 -070036import android.database.DataSetObservable;
37import android.database.DataSetObserver;
Paul Westbrook23b74b92012-02-29 11:36:12 -080038import android.net.Uri;
Vikram Aggarwal27d89ad2012-06-12 13:38:40 -070039import android.os.AsyncTask;
Vikram Aggarwala55b36c2012-01-13 11:45:02 -080040import android.os.Bundle;
Mindy Pereira21ab4902012-03-19 18:48:03 -070041import android.os.Handler;
Vikram Aggarwale620a7a2012-03-28 13:16:14 -070042import android.provider.SearchRecentSuggestions;
Mindy Pereiraacf60392012-04-06 09:11:00 -070043import android.view.DragEvent;
Vikram Aggarwala55b36c2012-01-13 11:45:02 -080044import android.view.KeyEvent;
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -080045import android.view.LayoutInflater;
Vikram Aggarwala55b36c2012-01-13 11:45:02 -080046import android.view.Menu;
Mindy Pereira28d5f722012-02-15 12:32:40 -080047import android.view.MenuInflater;
Vikram Aggarwala55b36c2012-01-13 11:45:02 -080048import android.view.MenuItem;
49import android.view.MotionEvent;
Marc Blankbf128eb2012-04-18 15:58:45 -070050import android.widget.AbsListView;
51import android.widget.AbsListView.OnScrollListener;
Mindy Pereirafd0c2972012-03-27 13:50:39 -070052import android.widget.Toast;
Vikram Aggarwala55b36c2012-01-13 11:45:02 -080053
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -080054import com.android.mail.ConversationListContext;
Andy Huangf9a73482012-03-13 15:54:02 -070055import com.android.mail.R;
Mindy Pereira967ede62012-03-22 09:29:09 -070056import com.android.mail.browse.ConversationCursor;
Paul Westbrookbf232c32012-04-18 03:17:41 -070057import com.android.mail.browse.ConversationPagerController;
Marc Blank7c9f6ac2012-04-02 13:27:19 -070058import com.android.mail.browse.SelectedConversationsActionMenu;
Mindy Pereira9b875682012-02-15 18:10:54 -080059import com.android.mail.compose.ComposeActivity;
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -080060import com.android.mail.providers.Account;
Mindy Pereira9b875682012-02-15 18:10:54 -080061import com.android.mail.providers.Conversation;
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -080062import com.android.mail.providers.Folder;
Paul Westbrookc2074c42012-03-22 15:26:58 -070063import com.android.mail.providers.MailAppProvider;
Mindy Pereiradac00542012-03-01 10:50:33 -080064import com.android.mail.providers.Settings;
Vikram Aggarwale620a7a2012-03-28 13:16:14 -070065import com.android.mail.providers.SuggestionsProvider;
Vikram Aggarwal80aeac52012-02-07 15:27:20 -080066import com.android.mail.providers.UIProvider;
Mindy Pereira3cb28b52012-05-24 15:26:39 -070067import com.android.mail.providers.UIProvider.AccountCapabilities;
Paul Westbrook2388c5d2012-03-25 12:29:11 -070068import com.android.mail.providers.UIProvider.AccountCursorExtraKeys;
Mindy Pereiraa0c24442012-05-24 15:39:17 -070069import com.android.mail.providers.UIProvider.AutoAdvance;
Mindy Pereirac9d59182012-03-22 16:06:46 -070070import com.android.mail.providers.UIProvider.ConversationColumns;
Vikram Aggarwal54452ae2012-03-13 15:29:00 -070071import com.android.mail.providers.UIProvider.FolderCapabilities;
Mindy Pereira5cb0c3e2012-02-22 15:22:47 -080072import com.android.mail.utils.LogUtils;
Vikram Aggarwalfa131a22012-02-02 13:56:22 -080073import com.android.mail.utils.Utils;
Mindy Pereiraacf60392012-04-06 09:11:00 -070074import com.google.common.collect.Lists;
Marc Blank167faa82012-03-21 13:11:53 -070075import com.google.common.collect.Sets;
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -080076
Vikram Aggarwal27d89ad2012-06-12 13:38:40 -070077import java.net.URI;
Marc Blank167faa82012-03-21 13:11:53 -070078import java.util.ArrayList;
Mindy Pereirafbe40192012-03-20 10:40:45 -070079import java.util.Collection;
Paul Westbrook23b74b92012-02-29 11:36:12 -080080import java.util.Set;
Marc Blankbf128eb2012-04-18 15:58:45 -070081import java.util.TimerTask;
Paul Westbrook23b74b92012-02-29 11:36:12 -080082
83
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -080084/**
Mindy Pereira161f50d2012-02-28 15:47:19 -080085 * This is an abstract implementation of the Activity Controller. This class
86 * knows how to respond to menu items, state changes, layout changes, etc. It
87 * weaves together the views and listeners, dispatching actions to the
88 * respective underlying classes.
Vikram Aggarwala55b36c2012-01-13 11:45:02 -080089 * <p>
Mindy Pereira161f50d2012-02-28 15:47:19 -080090 * Even though this class is abstract, it should provide default implementations
91 * for most, if not all the methods in the ActivityController interface. This
92 * makes the task of the subclasses easier: OnePaneActivityController and
93 * TwoPaneActivityController can be concise when the common functionality is in
94 * AbstractActivityController.
95 * </p>
96 * <p>
97 * In the Gmail codebase, this was called BaseActivityController
98 * </p>
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -080099 */
Vikram Aggarwal531488e2012-05-29 16:36:52 -0700100public abstract class AbstractActivityController implements ActivityController {
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800101 // Keys for serialization of various information in Bundles.
Vikram Aggarwalcabd3f22012-04-19 10:14:41 -0700102 /** Tag for {@link #mAccount} */
Vikram Aggarwal6c511582012-02-27 10:59:47 -0800103 private static final String SAVED_ACCOUNT = "saved-account";
Vikram Aggarwalcabd3f22012-04-19 10:14:41 -0700104 /** Tag for {@link #mFolder} */
Mindy Pereira5e478d22012-03-26 18:04:58 -0700105 private static final String SAVED_FOLDER = "saved-folder";
Vikram Aggarwalcabd3f22012-04-19 10:14:41 -0700106 /** Tag for {@link #mCurrentConversation} */
Mindy Pereira26f23fc2012-03-27 10:26:04 -0700107 private static final String SAVED_CONVERSATION = "saved-conversation";
Vikram Aggarwalcabd3f22012-04-19 10:14:41 -0700108 /** Tag for {@link #mSelectedSet} */
109 private static final String SAVED_SELECTED_SET = "saved-selected-set";
Mindy Pereira5cb0c3e2012-02-22 15:22:47 -0800110
Vikram Aggarwal6902dcf2012-04-11 08:57:42 -0700111 /** Tag used when loading a wait fragment */
112 protected static final String TAG_WAIT = "wait-fragment";
113 /** Tag used when loading a conversation list fragment. */
Paul Westbrookbf232c32012-04-18 03:17:41 -0700114 public static final String TAG_CONVERSATION_LIST = "tag-conversation-list";
115 /** Tag used when loading a conversation fragment. */
116 public static final String TAG_CONVERSATION = "tag-conversation";
Vikram Aggarwal6902dcf2012-04-11 08:57:42 -0700117 /** Tag used when loading a folder list fragment. */
118 protected static final String TAG_FOLDER_LIST = "tag-folder-list";
119
Vikram Aggarwald7a12cd2012-02-03 09:36:20 -0800120 protected Account mAccount;
Mindy Pereira11e35962012-06-01 14:49:46 -0700121 private Folder mFolder;
Andy Huang6681e542012-06-14 14:36:45 -0700122 protected MailActionBarView mActionBarView;
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800123 protected final RestrictedActivity mActivity;
Vikram Aggarwala55b36c2012-01-13 11:45:02 -0800124 protected final Context mContext;
Vikram Aggarwal6902dcf2012-04-11 08:57:42 -0700125 private final FragmentManager mFragmentManager;
Vikram Aggarwalec5cbf72012-03-08 15:10:35 -0800126 protected final RecentFolderList mRecentFolderList;
Vikram Aggarwal80aeac52012-02-07 15:27:20 -0800127 protected ConversationListContext mConvListContext;
Mindy Pereira9b875682012-02-15 18:10:54 -0800128 protected Conversation mCurrentConversation;
Vikram Aggarwald7a12cd2012-02-03 09:36:20 -0800129
Paul Westbrook6ead20d2012-03-19 14:48:14 -0700130 /** A {@link android.content.BroadcastReceiver} that suppresses new e-mail notifications. */
131 private SuppressNotificationReceiver mNewEmailReceiver = null;
132
Mindy Pereirafbe40192012-03-20 10:40:45 -0700133 protected Handler mHandler = new Handler();
Vikram Aggarwalfa131a22012-02-02 13:56:22 -0800134 /**
Mindy Pereira161f50d2012-02-28 15:47:19 -0800135 * The current mode of the application. All changes in mode are initiated by
136 * the activity controller. View mode changes are propagated to classes that
137 * attach themselves as listeners of view mode changes.
Vikram Aggarwalfa131a22012-02-02 13:56:22 -0800138 */
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800139 protected final ViewMode mViewMode;
Vikram Aggarwal80aeac52012-02-07 15:27:20 -0800140 protected ContentResolver mResolver;
Vikram Aggarwal7dedb952012-02-16 16:10:23 -0800141 protected boolean isLoaderInitialized = false;
Mindy Pereirab7b33e02012-02-21 15:32:19 -0800142 private AsyncRefreshTask mAsyncRefreshTask;
Mindy Pereira5cb0c3e2012-02-22 15:22:47 -0800143
Paul Westbrook23b74b92012-02-29 11:36:12 -0800144 private final Set<Uri> mCurrentAccountUris = Sets.newHashSet();
Mindy Pereira967ede62012-03-22 09:29:09 -0700145 protected ConversationCursor mConversationListCursor;
Andy Huang632721e2012-04-11 16:57:26 -0700146 private final DataSetObservable mConversationListObservable = new DataSetObservable() {
147 @Override
148 public void registerObserver(DataSetObserver observer) {
149 final int count = mObservers.size();
150 super.registerObserver(observer);
151 LogUtils.d(LOG_TAG, "IN AAC.registerListObserver: %s before=%d after=%d", observer,
152 count, mObservers.size());
153 }
154 @Override
155 public void unregisterObserver(DataSetObserver observer) {
156 final int count = mObservers.size();
157 super.unregisterObserver(observer);
158 LogUtils.d(LOG_TAG, "IN AAC.unregisterListObserver: %s before=%d after=%d", observer,
159 count, mObservers.size());
160 }
161 };
Marc Blankbf128eb2012-04-18 15:58:45 -0700162 protected boolean mConversationListenerAdded = false;
163
164 private boolean mIsConversationListScrolling = false;
165 private long mConversationListRefreshTime = 0;
Marc Blankbf128eb2012-04-18 15:58:45 -0700166 private RefreshTimerTask mConversationListRefreshTask;
Marc Blanke1d1b072012-04-13 17:29:16 -0700167
Vikram Aggarwalc7694222012-04-23 13:37:01 -0700168 /** Listeners that are interested in changes to current account settings. */
Vikram Aggarwal7d816002012-04-17 17:06:41 -0700169 private final ArrayList<Settings.ChangeListener> mSettingsListeners = Lists.newArrayList();
170
Mindy Pereira967ede62012-03-22 09:29:09 -0700171 /**
172 * Selected conversations, if any.
173 */
Andy Huang4556a442012-03-30 16:42:05 -0700174 private final ConversationSelectionSet mSelectedSet = new ConversationSelectionSet();
Mindy Pereiradac00542012-03-01 10:50:33 -0800175
Paul Westbrookc7a070f2012-04-12 01:46:41 -0700176 private final int mFolderItemUpdateDelayMs;
177
Vikram Aggarwal135fd022012-04-11 14:44:15 -0700178 /** Keeps track of selected and unselected conversations */
Vikram Aggarwal7dd054e2012-05-21 14:43:10 -0700179 final protected ConversationPositionTracker mTracker = new ConversationPositionTracker();
Vikram Aggarwalaf1ee0c2012-04-12 17:13:13 -0700180
Vikram Aggarwale128fc22012-04-04 12:33:34 -0700181 /**
182 * Action menu associated with the selected set.
183 */
184 SelectedConversationsActionMenu mCabActionMenu;
Mindy Pereira0963ef82012-04-10 11:43:01 -0700185 protected UndoBarView mUndoBarView;
Andy Huang632721e2012-04-11 16:57:26 -0700186 protected ConversationPagerController mPagerController;
Mindy Pereiraab486362012-03-21 18:18:53 -0700187
Andy Huangb1c34dc2012-04-17 16:36:19 -0700188 // this is split out from the general loader dispatcher because its loader doesn't return a
189 // basic Cursor
190 private final ConversationListLoaderCallbacks mListCursorCallbacks =
191 new ConversationListLoaderCallbacks();
192
Vikram Aggarwal04ff99c2012-02-28 15:29:13 -0800193 protected static final String LOG_TAG = new LogUtils().getLogTag();
Vikram Aggarwalfa4b47e2012-03-09 13:02:46 -0800194 /** Constants used to differentiate between the types of loaders. */
195 private static final int LOADER_ACCOUNT_CURSOR = 0;
Vikram Aggarwalfa4b47e2012-03-09 13:02:46 -0800196 private static final int LOADER_FOLDER_CURSOR = 2;
197 private static final int LOADER_RECENT_FOLDERS = 3;
Mindy Pereira967ede62012-03-22 09:29:09 -0700198 private static final int LOADER_CONVERSATION_LIST = 4;
Mindy Pereiraab486362012-03-21 18:18:53 -0700199 private static final int LOADER_ACCOUNT_INBOX = 5;
200 private static final int LOADER_SEARCH = 6;
Paul Westbrook2d50bcd2012-04-10 11:53:47 -0700201 private static final int LOADER_ACCOUNT_UPDATE_CURSOR = 7;
Vikram Aggarwala55b36c2012-01-13 11:45:02 -0800202
Paul Westbrook2388c5d2012-03-25 12:29:11 -0700203 private static final int ADD_ACCOUNT_REQUEST_CODE = 1;
204
Vikram Aggarwale8a85322012-04-24 09:01:38 -0700205 /** The pending destructive action to be carried out before swapping the conversation cursor.*/
206 private DestructiveAction mPendingDestruction;
Vikram Aggarwal7dd054e2012-05-21 14:43:10 -0700207 /** Indicates if a conversation view is visible. */
208 private boolean mIsConversationVisible;
Vikram Aggarwale8a85322012-04-24 09:01:38 -0700209
Vikram Aggarwala55b36c2012-01-13 11:45:02 -0800210 public AbstractActivityController(MailActivity activity, ViewMode viewMode) {
211 mActivity = activity;
Vikram Aggarwal6902dcf2012-04-11 08:57:42 -0700212 mFragmentManager = mActivity.getFragmentManager();
Vikram Aggarwala55b36c2012-01-13 11:45:02 -0800213 mViewMode = viewMode;
214 mContext = activity.getApplicationContext();
Vikram Aggarwal025eba82012-05-08 10:45:30 -0700215 mRecentFolderList = new RecentFolderList(mContext);
Mindy Pereira967ede62012-03-22 09:29:09 -0700216 // Allow the fragment to observe changes to its own selection set. No other object is
217 // aware of the selected set.
218 mSelectedSet.addObserver(this);
Paul Westbrookc7a070f2012-04-12 01:46:41 -0700219
220 mFolderItemUpdateDelayMs =
221 mContext.getResources().getInteger(R.integer.folder_item_refresh_delay_ms);
Vikram Aggarwala55b36c2012-01-13 11:45:02 -0800222 }
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -0800223
224 @Override
Vikram Aggarwale9a81032012-02-22 13:15:35 -0800225 public Account getCurrentAccount() {
226 return mAccount;
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800227 }
228
229 @Override
230 public ConversationListContext getCurrentListContext() {
Vikram Aggarwale9a81032012-02-22 13:15:35 -0800231 return mConvListContext;
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800232 }
233
234 @Override
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -0800235 public String getHelpContext() {
Vikram Aggarwala55b36c2012-01-13 11:45:02 -0800236 return "Mail";
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -0800237 }
238
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800239 @Override
Vikram Aggarwalef7c9922012-04-23 12:35:04 -0700240 public final ConversationCursor getConversationListCursor() {
Mindy Pereira967ede62012-03-22 09:29:09 -0700241 return mConversationListCursor;
242 }
243
Vikram Aggarwal6902dcf2012-04-11 08:57:42 -0700244 /**
Vikram Aggarwal34a65012012-04-17 12:39:06 -0700245 * Check if the fragment is attached to an activity and has a root view.
246 * @param in
247 * @return true if the fragment is valid, false otherwise
248 */
249 private static final boolean isValidFragment(Fragment in) {
250 if (in == null || in.getActivity() == null || in.getView() == null) {
251 return false;
252 }
253 return true;
254 }
255
256 /**
Vikram Aggarwal6902dcf2012-04-11 08:57:42 -0700257 * Get the conversation list fragment for this activity. If the conversation list fragment
258 * is not attached, this method returns null
259 * @return
260 */
261 protected ConversationListFragment getConversationListFragment() {
262 final Fragment fragment = mFragmentManager.findFragmentByTag(TAG_CONVERSATION_LIST);
Vikram Aggarwal34a65012012-04-17 12:39:06 -0700263 if (isValidFragment(fragment)) {
264 return (ConversationListFragment) fragment;
Andy Huang9585d782012-04-16 19:45:04 -0700265 }
Vikram Aggarwal34a65012012-04-17 12:39:06 -0700266 return null;
Vikram Aggarwal6902dcf2012-04-11 08:57:42 -0700267 }
268
269 /**
Vikram Aggarwald503df42012-05-11 10:13:35 -0700270 * Get the conversation view fragment for this activity. If the conversation view fragment
271 * is not attached, this method returns null
272 * @return
273 */
274 protected ConversationViewFragment getConversationViewFragment() {
275 final Fragment fragment = mFragmentManager.findFragmentByTag(TAG_CONVERSATION);
276 if (isValidFragment(fragment)) {
277 return (ConversationViewFragment) fragment;
278 }
279 return null;
280 }
281
282 /**
Vikram Aggarwal6902dcf2012-04-11 08:57:42 -0700283 * Returns the folder list fragment attached with this activity. If no such fragment is attached
284 * this method returns null.
285 * @return
286 */
287 protected FolderListFragment getFolderListFragment() {
288 final Fragment fragment = mFragmentManager.findFragmentByTag(TAG_FOLDER_LIST);
Vikram Aggarwal34a65012012-04-17 12:39:06 -0700289 if (isValidFragment(fragment)) {
290 return (FolderListFragment) fragment;
Andy Huang9585d782012-04-16 19:45:04 -0700291 }
Vikram Aggarwal34a65012012-04-17 12:39:06 -0700292 return null;
Vikram Aggarwal6902dcf2012-04-11 08:57:42 -0700293 }
294
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800295 /**
Mindy Pereira161f50d2012-02-28 15:47:19 -0800296 * Initialize the action bar. This is not visible to OnePaneController and
297 * TwoPaneController so they cannot override this behavior.
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800298 */
Vikram Aggarwalabd24d82012-04-26 13:23:14 -0700299 private void initializeActionBar() {
300 final ActionBar actionBar = mActivity.getActionBar();
Andy Huang5895f7b2012-06-01 17:07:20 -0700301 if (actionBar == null) {
302 return;
Vikram Aggarwalabd24d82012-04-26 13:23:14 -0700303 }
Andy Huang5895f7b2012-06-01 17:07:20 -0700304
305 // be sure to inherit from the ActionBar theme when inflating
306 final LayoutInflater inflater = LayoutInflater.from(actionBar.getThemedContext());
Mindy Pereira82faec72012-06-14 17:21:50 -0700307 final boolean isSearch = mActivity.getIntent() != null
308 && Intent.ACTION_SEARCH.equals(mActivity.getIntent().getAction());
309 mActionBarView = (MailActionBarView) inflater.inflate(
310 isSearch ? R.layout.search_actionbar_view : R.layout.actionbar_view, null);
Andy Huang5895f7b2012-06-01 17:07:20 -0700311 // Why have a different variable for the same thing? We should apply
312 // the same actions
313 // on mActionBarView instead.
314 mActionBarView.initialize(mActivity, this, mViewMode, actionBar, mRecentFolderList);
Vikram Aggarwalabd24d82012-04-26 13:23:14 -0700315 }
316
317 /**
318 * Attach the action bar to the activity.
319 */
320 private void attachActionBar() {
321 final ActionBar actionBar = mActivity.getActionBar();
322 if (actionBar != null && mActionBarView != null) {
Vikram Aggarwalec5cbf72012-03-08 15:10:35 -0800323 actionBar.setCustomView(mActionBarView, new ActionBar.LayoutParams(
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800324 LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
325 actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM,
Vikram Aggarwal2a25d0c2012-02-21 16:43:10 -0800326 ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_TITLE);
Vikram Aggarwalabd24d82012-04-26 13:23:14 -0700327 mActionBarView.attach();
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800328 }
Vikram Aggarwalabd24d82012-04-26 13:23:14 -0700329 mViewMode.addListener(mActionBarView);
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800330 }
331
332 /**
Mindy Pereira161f50d2012-02-28 15:47:19 -0800333 * Returns whether the conversation list fragment is visible or not.
334 * Different layouts will have their own notion on the visibility of
335 * fragments, so this method needs to be overriden.
336 *
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800337 * @return
338 */
339 protected abstract boolean isConversationListVisible();
340
Vikram Aggarwaleb7d3292012-04-20 17:07:20 -0700341 /**
342 * Switch the current account to the one provided as an argument to the method.
343 * @param account
344 */
345 private void switchAccount(Account account){
346 // Current account is different from the new account, restart loaders and show
347 // the account Inbox.
348 mAccount = account;
Vikram Aggarwaledc137c2012-04-24 13:40:58 -0700349 LogUtils.d(LOG_TAG, "AbstractActivityController.switchAccount(): mAccount = %s",
350 mAccount.uri);
Vikram Aggarwaleb7d3292012-04-20 17:07:20 -0700351 cancelRefreshTask();
352 onSettingsChanged(mAccount.settings);
353 mActionBarView.setAccount(mAccount);
354 loadAccountInbox();
355
356 mRecentFolderList.setCurrentAccount(account);
357 restartOptionalLoader(LOADER_RECENT_FOLDERS);
358 mActivity.invalidateOptionsMenu();
359 disableNotificationsOnAccountChange(mAccount);
360 restartOptionalLoader(LOADER_ACCOUNT_UPDATE_CURSOR);
361 MailAppProvider.getInstance().setLastViewedAccount(mAccount.uri.toString());
362 }
363
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800364 @Override
Mindy Pereira28e0c342012-02-17 15:05:13 -0800365 public void onAccountChanged(Account account) {
Vikram Aggarwaleb7d3292012-04-20 17:07:20 -0700366 LogUtils.d(LOG_TAG, "onAccountChanged (%s) called.", account.uri);
367 final boolean accountChanged = (mAccount == null) || !account.uri.equals(mAccount.uri);
368 if (accountChanged) {
369 switchAccount(account);
370 return;
371 }
372 // Current account is the same as the new account, but the settings might be different.
373 if (!account.settings.equals(mAccount.settings)){
374 onSettingsChanged(account.settings);
375 return;
Mindy Pereira28d5f722012-02-15 12:32:40 -0800376 }
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800377 }
378
Vikram Aggarwale6340bc2012-03-26 15:57:09 -0700379 /**
Vikram Aggarwaleb7d3292012-04-20 17:07:20 -0700380 * Changes the settings for the current account. The new settings are provided as a parameter.
381 * @param settings
382 */
Mindy Pereiradac00542012-03-01 10:50:33 -0800383 public void onSettingsChanged(Settings settings) {
Vikram Aggarwal7d816002012-04-17 17:06:41 -0700384 dispatchSettingsChange(settings);
Mindy Pereira12a676a2012-03-23 13:00:22 -0700385 resetActionBarIcon();
Vikram Aggarwaleb7d3292012-04-20 17:07:20 -0700386 mActivity.invalidateOptionsMenu();
387 // If the user was viewing the default Inbox here, and the new setting contains a different
388 // default Inbox, we don't want to load a different folder here.
Mindy Pereiradac00542012-03-01 10:50:33 -0800389 }
390
Mindy Pereiraefe3d252012-03-01 14:20:44 -0800391 @Override
392 public Settings getSettings() {
Vikram Aggarwal025eba82012-05-08 10:45:30 -0700393 return mAccount.settings;
Mindy Pereiraefe3d252012-03-01 14:20:44 -0800394 }
395
Vikram Aggarwal7d816002012-04-17 17:06:41 -0700396 /**
397 * Adds a listener interested in change in settings. If a class is storing a reference to
398 * Settings, it should listen on changes, so it can receive updates to settings.
399 * Must happen in the UI thread.
400 */
401 public void addSettingsListener(Settings.ChangeListener listener) {
402 mSettingsListeners.add(listener);
403 }
404
405 /**
406 * Removes a listener from receiving settings changes.
407 * Must happen in the UI thread.
408 */
409 public void removeSettingsListener(Settings.ChangeListener listener) {
410 mSettingsListeners.remove(listener);
411 }
412
413 /**
414 * Method that lets the settings listeners know when the settings got changed.
415 */
416 private void dispatchSettingsChange(Settings updatedSettings) {
Vikram Aggarwal7d816002012-04-17 17:06:41 -0700417 // Copy the list of current listeners so that
418 final ArrayList<Settings.ChangeListener> allListeners =
419 new ArrayList<Settings.ChangeListener>(mSettingsListeners);
420 for (Settings.ChangeListener listener : allListeners) {
421 if (listener != null) {
Vikram Aggarwal025eba82012-05-08 10:45:30 -0700422 listener.onSettingsChanged(updatedSettings);
Vikram Aggarwal7d816002012-04-17 17:06:41 -0700423 }
424 }
425 // And we know that the ConversationListFragment is interested in changes to settings,
426 // though it hasn't registered itself with us.
427 final ConversationListFragment convList = getConversationListFragment();
428 if (convList != null) {
Vikram Aggarwal025eba82012-05-08 10:45:30 -0700429 convList.onSettingsChanged(updatedSettings);
Vikram Aggarwal7d816002012-04-17 17:06:41 -0700430 }
431 }
432
Mindy Pereirae0828392012-03-08 10:38:40 -0800433 private void fetchSearchFolder(Intent intent) {
Mindy Pereiraab486362012-03-21 18:18:53 -0700434 Bundle args = new Bundle();
435 args.putString(ConversationListContext.EXTRA_SEARCH_QUERY, intent
Mindy Pereirae0828392012-03-08 10:38:40 -0800436 .getStringExtra(ConversationListContext.EXTRA_SEARCH_QUERY));
Mindy Pereiraab486362012-03-21 18:18:53 -0700437 mActivity.getLoaderManager().restartLoader(LOADER_SEARCH, args, this);
Mindy Pereirae0828392012-03-08 10:38:40 -0800438 }
439
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800440 @Override
Mindy Pereira28e0c342012-02-17 15:05:13 -0800441 public void onFolderChanged(Folder folder) {
Mindy Pereira7a3471f2012-03-06 12:23:41 -0800442 if (folder != null && !folder.equals(mFolder)) {
Mindy Pereira11e35962012-06-01 14:49:46 -0700443 updateFolder(folder);
Mindy Pereira161f50d2012-02-28 15:47:19 -0800444 mConvListContext = ConversationListContext.forFolder(mContext, mAccount, mFolder);
Mindy Pereira28e0c342012-02-17 15:05:13 -0800445 showConversationList(mConvListContext);
Paul Westbrook9024b6d2012-03-19 13:57:55 -0700446
447 // Add the folder that we were viewing to the recent folders list.
448 // TODO: this may need to be fine tuned. If this is the signal that is indicating that
449 // the list is shown to the user, this could fire in one pane if the user goes directly
450 // to a conversation
Paul Westbrook7ebdfd02012-03-21 15:55:30 -0700451 updateRecentFolderList();
Marc Blankbf128eb2012-04-18 15:58:45 -0700452 cancelRefreshTask();
Mindy Pereira28e0c342012-02-17 15:05:13 -0800453 }
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800454 }
455
Mindy Pereira13c12a62012-05-31 15:41:08 -0700456 @Override
457 public void onFolderSelected(Folder folder, boolean childView) {
458 onFolderChanged(folder);
459 }
460
Vikram Aggarwal792ccba2012-03-27 13:46:57 -0700461 /**
462 * Update the recent folders. This only needs to be done once when accessing a new folder.
463 */
Paul Westbrook7ebdfd02012-03-21 15:55:30 -0700464 private void updateRecentFolderList() {
Mindy Pereiraab486362012-03-21 18:18:53 -0700465 if (mFolder != null) {
Marc Blank2675dbc2012-04-03 10:17:13 -0700466 mRecentFolderList.touchFolder(mFolder, mAccount);
Mindy Pereiraab486362012-03-21 18:18:53 -0700467 }
Paul Westbrook7ebdfd02012-03-21 15:55:30 -0700468 }
469
Mindy Pereiraab486362012-03-21 18:18:53 -0700470 // TODO(mindyp): set this up to store a copy of the folder as a transient
471 // field in the account.
Vikram Aggarwaleb7d3292012-04-20 17:07:20 -0700472 @Override
473 public void loadAccountInbox() {
Vikram Aggarwal94c94de2012-04-04 15:38:28 -0700474 restartOptionalLoader(LOADER_ACCOUNT_INBOX);
Mindy Pereiraf6acdad2012-03-15 11:21:13 -0700475 }
476
Vikram Aggarwalec5cbf72012-03-08 15:10:35 -0800477 /** Set the current folder */
Mindy Pereira11e35962012-06-01 14:49:46 -0700478 private void updateFolder(Folder folder) {
Mindy Pereira5cb0c3e2012-02-22 15:22:47 -0800479 // Start watching folder for sync status.
Mindy Pereirae0458e82012-03-06 11:54:55 -0800480 if (folder != null && !folder.equals(mFolder)) {
Vikram Aggarwaleb7d3292012-04-20 17:07:20 -0700481 LogUtils.d(LOG_TAG, "AbstractActivityController.setFolder(%s)", folder.name);
Andy Huangb1c34dc2012-04-17 16:36:19 -0700482 final LoaderManager lm = mActivity.getLoaderManager();
Mindy Pereira9b623802012-03-07 17:15:49 -0800483 mActionBarView.setRefreshInProgress(false);
Mindy Pereira11e35962012-06-01 14:49:46 -0700484 setFolder(folder);
Mindy Pereiraf9323cd2012-02-29 13:47:09 -0800485 mActionBarView.setFolder(mFolder);
Andy Huangb1c34dc2012-04-17 16:36:19 -0700486
487 // Only when we switch from one folder to another do we want to restart the
488 // folder and conversation list loaders (to trigger onCreateLoader).
489 // The first time this runs when the activity is [re-]initialized, we want to re-use the
490 // previous loader's instance and data upon configuration change (e.g. rotation).
Mindy Pereira11e35962012-06-01 14:49:46 -0700491 // If there was not already an instance of the loader, init it.
492 if (lm.getLoader(LOADER_FOLDER_CURSOR) == null) {
Andy Huangb1c34dc2012-04-17 16:36:19 -0700493 lm.initLoader(LOADER_FOLDER_CURSOR, null, this);
494 lm.initLoader(LOADER_CONVERSATION_LIST, null, mListCursorCallbacks);
495 } else {
496 lm.restartLoader(LOADER_FOLDER_CURSOR, null, this);
497 lm.restartLoader(LOADER_CONVERSATION_LIST, null, mListCursorCallbacks);
498 }
Mindy Pereirae0458e82012-03-06 11:54:55 -0800499 } else if (folder == null) {
500 LogUtils.wtf(LOG_TAG, "Folder in setFolder is null");
Mindy Pereira5cb0c3e2012-02-22 15:22:47 -0800501 }
502 }
503
Mindy Pereira11e35962012-06-01 14:49:46 -0700504 /**
505 * Set the folder that is used for all current operations, including what
506 * conversation list to show (if applicable), what item to select in the
507 * FolderListFragment.
508 *
509 * @param folder
510 */
511 public void setFolder(Folder folder) {
512 mFolder = folder;
513 }
514
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800515 @Override
Mindy Pereira23aadfd2012-05-25 11:24:33 -0700516 public Folder getFolder() {
517 return mFolder;
518 }
519
520 @Override
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800521 public void onActivityResult(int requestCode, int resultCode, Intent data) {
Paul Westbrook2388c5d2012-03-25 12:29:11 -0700522 if (requestCode == ADD_ACCOUNT_REQUEST_CODE) {
523 // We were waiting for the user to create an account
524 if (resultCode == Activity.RESULT_OK) {
525 // restart the loader to get the updated list of accounts
526 mActivity.getLoaderManager().initLoader(
527 LOADER_ACCOUNT_CURSOR, null, this);
528 } else {
529 // The user failed to create an account, just exit the app
530 mActivity.finish();
531 }
532 }
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800533 }
534
535 @Override
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800536 public void onConversationListVisibilityChanged(boolean visible) {
Paul Westbrook9f119c72012-04-24 16:10:59 -0700537 if (mConversationListCursor != null) {
538 // The conversation list is visible.
539 Utils.setConversationCursorVisibility(mConversationListCursor, visible);
540 }
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800541 }
542
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -0800543 /**
Vikram Aggarwal7dd054e2012-05-21 14:43:10 -0700544 * Called when a conversation is visible. Child classes must call the super class implementation
545 * before performing local computation.
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -0800546 */
547 @Override
548 public void onConversationVisibilityChanged(boolean visible) {
Vikram Aggarwal7dd054e2012-05-21 14:43:10 -0700549 mIsConversationVisible = visible;
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -0800550 return;
551 }
552
553 @Override
Vikram Aggarwald7a12cd2012-02-03 09:36:20 -0800554 public boolean onCreate(Bundle savedState) {
Vikram Aggarwalabd24d82012-04-26 13:23:14 -0700555 initializeActionBar();
Vikram Aggarwala55b36c2012-01-13 11:45:02 -0800556 // Allow shortcut keys to function for the ActionBar and menus.
557 mActivity.setDefaultKeyMode(Activity.DEFAULT_KEYS_SHORTCUT);
Vikram Aggarwal80aeac52012-02-07 15:27:20 -0800558 mResolver = mActivity.getContentResolver();
Paul Westbrook6ead20d2012-03-19 14:48:14 -0700559 mNewEmailReceiver = new SuppressNotificationReceiver();
560
Mindy Pereira161f50d2012-02-28 15:47:19 -0800561 // All the individual UI components listen for ViewMode changes. This
Mindy Pereirab849dfb2012-03-07 18:13:15 -0800562 // simplifies the amount of logic in the AbstractActivityController, but increases the
563 // possibility of timing-related bugs.
Vikram Aggarwala55b36c2012-01-13 11:45:02 -0800564 mViewMode.addListener(this);
Andy Huang632721e2012-04-11 16:57:26 -0700565 mPagerController = new ConversationPagerController(mActivity, this);
Andy Huang632721e2012-04-11 16:57:26 -0700566 mUndoBarView = (UndoBarView) mActivity.findViewById(R.id.undo_view);
Vikram Aggarwalada51782012-04-26 14:18:31 -0700567 attachActionBar();
Andy Huang632721e2012-04-11 16:57:26 -0700568
569 final Intent intent = mActivity.getIntent();
Vikram Aggarwalabd24d82012-04-26 13:23:14 -0700570 // Immediately handle a clean launch with intent, and any state restoration
Andy Huang632721e2012-04-11 16:57:26 -0700571 // that does not rely on restored fragments or loader data
572 // any state restoration that relies on those can be done later in
573 // onRestoreInstanceState, once fragments are up and loader data is re-delivered
574 if (savedState != null) {
575 if (savedState.containsKey(SAVED_ACCOUNT)) {
576 setAccount((Account) savedState.getParcelable(SAVED_ACCOUNT));
577 mActivity.invalidateOptionsMenu();
578 }
579 if (savedState.containsKey(SAVED_FOLDER)) {
580 // Open the folder.
581 onFolderChanged((Folder) savedState.getParcelable(SAVED_FOLDER));
582 }
583 } else if (intent != null) {
584 handleIntent(intent);
585 }
Andy Huang632721e2012-04-11 16:57:26 -0700586 // Create the accounts loader; this loads the account switch spinner.
587 mActivity.getLoaderManager().initLoader(LOADER_ACCOUNT_CURSOR, null, this);
Andy Huang632721e2012-04-11 16:57:26 -0700588 return true;
Andy Huangb1c34dc2012-04-17 16:36:19 -0700589 }
590
591 @Override
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800592 public Dialog onCreateDialog(int id, Bundle bundle) {
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800593 return null;
Vikram Aggarwala55b36c2012-01-13 11:45:02 -0800594 }
595
596 @Override
597 public boolean onCreateOptionsMenu(Menu menu) {
Mindy Pereira28d5f722012-02-15 12:32:40 -0800598 MenuInflater inflater = mActivity.getMenuInflater();
Mindy Pereiraf5acda42012-02-15 20:13:59 -0800599 inflater.inflate(mActionBarView.getOptionsMenuId(), menu);
Mindy Pereira68f2e222012-03-07 10:36:54 -0800600 mActionBarView.onCreateOptionsMenu(menu);
Mindy Pereira28d5f722012-02-15 12:32:40 -0800601 return true;
Vikram Aggarwala55b36c2012-01-13 11:45:02 -0800602 }
603
604 @Override
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -0800605 public boolean onKeyDown(int keyCode, KeyEvent event) {
606 // TODO(viki): Auto-generated method stub
607 return false;
608 }
609
610 @Override
Vikram Aggarwala55b36c2012-01-13 11:45:02 -0800611 public boolean onOptionsItemSelected(MenuItem item) {
Vikram Aggarwal54452ae2012-03-13 15:29:00 -0700612 final int id = item.getItemId();
Vikram Aggarwal7dd054e2012-05-21 14:43:10 -0700613 LogUtils.d(LOG_TAG, "AbstractController.onOptionsItemSelected(%d) called.", id);
Mindy Pereira9b875682012-02-15 18:10:54 -0800614 boolean handled = true;
Mindy Pereiraba68fda2012-05-24 15:53:06 -0700615 final Collection<Conversation> target = Conversation.listOf(mCurrentConversation);
Vikram Aggarwal112cd162012-06-18 10:51:13 -0700616 final Settings settings = (mAccount == null) ? null : mAccount.settings;
Mindy Pereira28d5f722012-02-15 12:32:40 -0800617 switch (id) {
Mindy Pereiraba68fda2012-05-24 15:53:06 -0700618 case R.id.archive: {
619 final boolean showDialog = (settings != null && settings.confirmArchive);
620 confirmAndDelete(target, showDialog, R.plurals.confirm_archive_conversation,
621 getAction(R.id.archive, target));
622 break;
623 }
624 case R.id.delete: {
625 final boolean showDialog = (settings != null && settings.confirmDelete);
626 confirmAndDelete(target, showDialog, R.plurals.confirm_delete_conversation,
627 getAction(R.id.delete, target));
628 break;
629 }
630 case R.id.mark_important:
Vikram Aggarwal531488e2012-05-29 16:36:52 -0700631 updateConversation(Conversation.listOf(mCurrentConversation),
632 ConversationColumns.PRIORITY, UIProvider.ConversationPriority.HIGH);
Mindy Pereiraba68fda2012-05-24 15:53:06 -0700633 break;
634 case R.id.mark_not_important:
Vikram Aggarwal531488e2012-05-29 16:36:52 -0700635 updateConversation(Conversation.listOf(mCurrentConversation),
636 ConversationColumns.PRIORITY, UIProvider.ConversationPriority.LOW);
Mindy Pereiraba68fda2012-05-24 15:53:06 -0700637 break;
638 case R.id.mute:
Vikram Aggarwal4f4782b2012-05-30 08:39:09 -0700639 delete(target, getAction(R.id.mute, target));
Mindy Pereiraba68fda2012-05-24 15:53:06 -0700640 break;
641 case R.id.report_spam:
Vikram Aggarwal4f4782b2012-05-30 08:39:09 -0700642 delete(target, getAction(R.id.report_spam, target));
Mindy Pereiraba68fda2012-05-24 15:53:06 -0700643 break;
644 case R.id.inside_conversation_unread:
Vikram Aggarwal531488e2012-05-29 16:36:52 -0700645 // TODO(viki): This is strange, and potentially incorrect. READ is an int column
646 // in the provider.
647 updateConversation(Conversation.listOf(mCurrentConversation),
648 ConversationColumns.READ, false);
Mindy Pereiraba68fda2012-05-24 15:53:06 -0700649 mViewMode.enterConversationListMode();
650 break;
Mindy Pereiraf5acda42012-02-15 20:13:59 -0800651 case android.R.id.home:
652 onUpPressed();
653 break;
Mindy Pereira9b875682012-02-15 18:10:54 -0800654 case R.id.compose:
655 ComposeActivity.compose(mActivity.getActivityContext(), mAccount);
656 break;
Mindy Pereira28d5f722012-02-15 12:32:40 -0800657 case R.id.show_all_folders:
658 showFolderList();
659 break;
Mindy Pereira28e0c342012-02-17 15:05:13 -0800660 case R.id.refresh:
661 requestFolderRefresh();
662 break;
Mindy Pereira1f936682012-03-02 11:30:33 -0800663 case R.id.settings:
664 Utils.showSettings(mActivity.getActivityContext(), mAccount);
Paul Westbrook2861b6a2012-02-15 15:25:34 -0800665 break;
Paul Westbrooke5503552012-03-28 00:35:57 -0700666 case R.id.folder_options:
667 Utils.showFolderSettings(mActivity.getActivityContext(), mAccount, mFolder);
668 break;
Paul Westbrook94e440d2012-02-24 11:03:47 -0800669 case R.id.help_info_menu_item:
670 // TODO: enable context sensitive help
Paul Westbrook498e76d2012-04-12 16:33:02 -0700671 Utils.showHelp(mActivity.getActivityContext(), mAccount, null);
Paul Westbrook94e440d2012-02-24 11:03:47 -0800672 break;
Vikram Aggarwal54452ae2012-03-13 15:29:00 -0700673 case R.id.feedback_menu_item:
Mindy Pereirafbe40192012-03-20 10:40:45 -0700674 Utils.sendFeedback(mActivity.getActivityContext(), mAccount);
675 break;
Paul Westbrook18babd22012-04-09 22:17:08 -0700676 case R.id.manage_folders_item:
677 Utils.showManageFolder(mActivity.getActivityContext(), mAccount);
678 break;
Vikram Aggarwald503df42012-05-11 10:13:35 -0700679 case R.id.change_folder:
680 new FoldersSelectionDialog(mActivity.getActivityContext(), mAccount, this,
Mindy Pereiraf3a45562012-05-24 16:30:19 -0700681 Conversation.listOf(mCurrentConversation), false).show();
Vikram Aggarwald503df42012-05-11 10:13:35 -0700682 break;
Mindy Pereira9b875682012-02-15 18:10:54 -0800683 default:
684 handled = false;
685 break;
Mindy Pereira28d5f722012-02-15 12:32:40 -0800686 }
Mindy Pereira9b875682012-02-15 18:10:54 -0800687 return handled;
Vikram Aggarwala55b36c2012-01-13 11:45:02 -0800688 }
689
Vikram Aggarwal531488e2012-05-29 16:36:52 -0700690 @Override
691 public void updateConversation(Collection <Conversation> target, String columnName,
692 boolean value) {
693 mConversationListCursor.updateBoolean(mContext, target, columnName, value);
Vikram Aggarwal75daee52012-04-30 11:13:09 -0700694 refreshConversationList();
Vikram Aggarwal54452ae2012-03-13 15:29:00 -0700695 }
696
Vikram Aggarwal531488e2012-05-29 16:36:52 -0700697 @Override
698 public void updateConversation(Collection <Conversation> target, String columnName, int value) {
699 mConversationListCursor.updateInt(mContext, target, columnName, value);
Vikram Aggarwal75daee52012-04-30 11:13:09 -0700700 refreshConversationList();
Mindy Pereirac9d59182012-03-22 16:06:46 -0700701 }
702
Vikram Aggarwal531488e2012-05-29 16:36:52 -0700703 @Override
704 public void updateConversation(Collection <Conversation> target, String columnName,
705 String value) {
706 mConversationListCursor.updateString(mContext, target, columnName, value);
Vikram Aggarwal75daee52012-04-30 11:13:09 -0700707 refreshConversationList();
Vikram Aggarwal54452ae2012-03-13 15:29:00 -0700708 }
709
Mindy Pereira28e0c342012-02-17 15:05:13 -0800710 private void requestFolderRefresh() {
711 if (mFolder != null) {
Mindy Pereirab7b33e02012-02-21 15:32:19 -0800712 if (mAsyncRefreshTask != null) {
713 mAsyncRefreshTask.cancel(true);
714 }
Mindy Pereira5cb0c3e2012-02-22 15:22:47 -0800715 mAsyncRefreshTask = new AsyncRefreshTask(mContext, mFolder);
Mindy Pereirab7b33e02012-02-21 15:32:19 -0800716 mAsyncRefreshTask.execute();
Mindy Pereira28e0c342012-02-17 15:05:13 -0800717 }
718 }
719
Mindy Pereirafbe40192012-03-20 10:40:45 -0700720 /**
721 * Confirm (based on user's settings) and delete a conversation from the conversation list and
722 * from the database.
Vikram Aggarwal3d7ca9d2012-05-11 14:40:36 -0700723 * @param target the conversations to act upon
724 * @param showDialog true if a confirmation dialog is to be shown, false otherwise.
725 * @param confirmResource the resource ID of the string that is shown in the confirmation dialog
726 * @param action the action to perform after animating the deletion of the conversations.
Mindy Pereirafbe40192012-03-20 10:40:45 -0700727 */
Vikram Aggarwal7f602f72012-04-30 16:04:06 -0700728 protected void confirmAndDelete(final Collection<Conversation> target, boolean showDialog,
729 int confirmResource, final DestructiveAction action) {
Mindy Pereirafbe40192012-03-20 10:40:45 -0700730 if (showDialog) {
731 final AlertDialog.OnClickListener onClick = new AlertDialog.OnClickListener() {
732 @Override
733 public void onClick(DialogInterface dialog, int which) {
Vikram Aggarwal4f4782b2012-05-30 08:39:09 -0700734 delete(target, action);
Mindy Pereirafbe40192012-03-20 10:40:45 -0700735 }
736 };
Vikram Aggarwal3d7ca9d2012-05-11 14:40:36 -0700737 final CharSequence message = Utils.formatPlural(mContext, confirmResource,
738 target.size());
Mindy Pereirafbe40192012-03-20 10:40:45 -0700739 new AlertDialog.Builder(mActivity.getActivityContext()).setMessage(message)
740 .setPositiveButton(R.string.ok, onClick)
741 .setNegativeButton(R.string.cancel, null)
742 .create().show();
743 } else {
Vikram Aggarwal4f4782b2012-05-30 08:39:09 -0700744 delete(target, action);
Mindy Pereirafbe40192012-03-20 10:40:45 -0700745 }
746 }
747
Vikram Aggarwal4f4782b2012-05-30 08:39:09 -0700748 @Override
749 public void delete(final Collection<Conversation> target, final DestructiveAction action) {
Vikram Aggarwald503df42012-05-11 10:13:35 -0700750 // The conversation list handles deletion if it exists.
751 final ConversationListFragment convList = getConversationListFragment();
752 if (convList != null) {
Vikram Aggarwalc30fe412012-05-18 11:58:58 -0700753 LogUtils.d(LOG_TAG, "AAC.requestDelete: ListFragment is handling delete.");
Vikram Aggarwald503df42012-05-11 10:13:35 -0700754 convList.requestDelete(target, action);
755 return;
756 }
757 // Update the conversation fragment if the current conversation is deleted.
758 if (getConversationViewFragment() != null &&
759 !Conversation.contains(target, mCurrentConversation)) {
760 final Conversation next = mTracker.getNextConversation(
Vikram Aggarwal7dd054e2012-05-21 14:43:10 -0700761 Settings.getAutoAdvanceSetting(mAccount.settings), target,
762 mCurrentConversation);
Vikram Aggarwal3d7ca9d2012-05-11 14:40:36 -0700763 LogUtils.d(LOG_TAG, "requestDelete: showing %s next.", next);
764 showConversation(next);
Vikram Aggarwald503df42012-05-11 10:13:35 -0700765 }
766 // No visible UI element handled it on our behalf. Perform the action ourself.
767 action.performAction();
768 }
769
770 /**
771 * Requests that the action be performed and the UI state is updated to reflect the new change.
772 * @param target
773 * @param action
774 */
Vikram Aggarwal4f4782b2012-05-30 08:39:09 -0700775 private void requestUpdate(final Collection<Conversation> target,
Vikram Aggarwald503df42012-05-11 10:13:35 -0700776 final DestructiveAction action) {
777 action.performAction();
778 refreshConversationList();
Vikram Aggarwal75daee52012-04-30 11:13:09 -0700779 }
Vikram Aggarwal6fbc87a2012-03-15 15:24:00 -0700780
781 @Override
Vikram Aggarwala55b36c2012-01-13 11:45:02 -0800782 public void onPrepareDialog(int id, Dialog dialog, Bundle bundle) {
783 // TODO(viki): Auto-generated method stub
Vikram Aggarwala55b36c2012-01-13 11:45:02 -0800784 }
785
786 @Override
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800787 public boolean onPrepareOptionsMenu(Menu menu) {
Mindy Pereirab849dfb2012-03-07 18:13:15 -0800788 mActionBarView.onPrepareOptionsMenu(menu);
Mindy Pereira363451a2012-02-22 14:14:46 -0800789 return true;
Vikram Aggarwala55b36c2012-01-13 11:45:02 -0800790 }
791
Mindy Pereira68f2e222012-03-07 10:36:54 -0800792 @Override
793 public void onPause() {
794 isLoaderInitialized = false;
Paul Westbrook6ead20d2012-03-19 14:48:14 -0700795 enableNotifications();
Mindy Pereira1e2573b2012-04-17 14:34:36 -0700796 commitLeaveBehindItems();
Paul Westbrook94e440d2012-02-24 11:03:47 -0800797 }
798
Vikram Aggarwala55b36c2012-01-13 11:45:02 -0800799 @Override
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800800 public void onResume() {
Paul Westbrook6ead20d2012-03-19 14:48:14 -0700801 // Register the receiver that will prevent the status receiver from
802 // displaying its notification icon as long as we're running.
803 // The SupressNotificationReceiver will block the broadcast if we're looking at the folder
804 // that the notification was received for.
805 disableNotifications();
806
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800807 if (mActionBarView != null) {
808 mActionBarView.onResume();
809 }
Paul Westbrook6ead20d2012-03-19 14:48:14 -0700810
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800811 }
812
813 @Override
814 public void onSaveInstanceState(Bundle outState) {
Vikram Aggarwal6c511582012-02-27 10:59:47 -0800815 if (mAccount != null) {
816 LogUtils.d(LOG_TAG, "Saving the account now");
817 outState.putParcelable(SAVED_ACCOUNT, mAccount);
818 }
Mindy Pereira5e478d22012-03-26 18:04:58 -0700819 if (mFolder != null) {
820 outState.putParcelable(SAVED_FOLDER, mFolder);
Vikram Aggarwal8b152632012-02-03 14:58:45 -0800821 }
Mindy Pereira9fa43ca2012-05-17 14:18:01 -0700822 if (mCurrentConversation != null
823 && (mViewMode.getMode() == ViewMode.CONVERSATION ||
824 mViewMode.getMode() == ViewMode.SEARCH_RESULTS_CONVERSATION)) {
Mindy Pereira26f23fc2012-03-27 10:26:04 -0700825 outState.putParcelable(SAVED_CONVERSATION, mCurrentConversation);
826 }
Andy Huang4556a442012-03-30 16:42:05 -0700827 if (!mSelectedSet.isEmpty()) {
Vikram Aggarwalcabd3f22012-04-19 10:14:41 -0700828 outState.putParcelable(SAVED_SELECTED_SET, mSelectedSet);
Andy Huang4556a442012-03-30 16:42:05 -0700829 }
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800830 }
831
832 @Override
Mindy Pereira68f2e222012-03-07 10:36:54 -0800833 public void onSearchRequested(String query) {
834 Intent intent = new Intent();
835 intent.setAction(Intent.ACTION_SEARCH);
836 intent.putExtra(ConversationListContext.EXTRA_SEARCH_QUERY, query);
837 intent.putExtra(Utils.EXTRA_ACCOUNT, mAccount);
838 intent.setComponent(mActivity.getComponentName());
Vikram Aggarwalb17cbc02012-04-06 15:41:46 -0700839 mActionBarView.collapseSearch();
Mindy Pereira68f2e222012-03-07 10:36:54 -0800840 mActivity.startActivity(intent);
Vikram Aggarwala55b36c2012-01-13 11:45:02 -0800841 }
842
843 @Override
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800844 public void onStop() {
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800845 // TODO(viki): Auto-generated method stub
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800846 }
847
Andy Huang632721e2012-04-11 16:57:26 -0700848 @Override
849 public void onDestroy() {
850 // unregister the ViewPager's observer on the conversation cursor
851 mPagerController.onDestroy();
852 }
853
Vikram Aggarwalfa131a22012-02-02 13:56:22 -0800854 /**
Mindy Pereira161f50d2012-02-28 15:47:19 -0800855 * {@inheritDoc} Subclasses must override this to listen to mode changes
856 * from the ViewMode. Subclasses <b>must</b> call the parent's
857 * onViewModeChanged since the parent will handle common state changes.
Vikram Aggarwalfa131a22012-02-02 13:56:22 -0800858 */
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800859 @Override
Vikram Aggarwalfa131a22012-02-02 13:56:22 -0800860 public void onViewModeChanged(int newMode) {
861 // Perform any mode specific work here.
Mindy Pereira161f50d2012-02-28 15:47:19 -0800862 // reset the action bar icon based on the mode. Why don't the individual
863 // controllers do
Vikram Aggarwalfa131a22012-02-02 13:56:22 -0800864 // this themselves?
Vikram Aggarwala55b36c2012-01-13 11:45:02 -0800865
Mindy Pereira161f50d2012-02-28 15:47:19 -0800866 // We don't want to invalidate the options menu when switching to
867 // conversation
Vikram Aggarwalfa131a22012-02-02 13:56:22 -0800868 // mode, as it will happen when the conversation finishes loading.
869 if (newMode != ViewMode.CONVERSATION) {
870 mActivity.invalidateOptionsMenu();
871 }
Vikram Aggarwala55b36c2012-01-13 11:45:02 -0800872 }
873
Mindy Pereira1e2573b2012-04-17 14:34:36 -0700874 protected void commitLeaveBehindItems() {
875 ConversationListFragment fragment = getConversationListFragment();
876 if (fragment != null) {
877 fragment.commitLeaveBehindItems();
878 }
879 }
880
Vikram Aggarwala55b36c2012-01-13 11:45:02 -0800881 @Override
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800882 public void onWindowFocusChanged(boolean hasFocus) {
Paul Westbrook9f119c72012-04-24 16:10:59 -0700883 ConversationListFragment convList = getConversationListFragment();
884 if (hasFocus && convList != null && convList.isVisible()) {
885 // The conversation list is visible.
886 Utils.setConversationCursorVisibility(mConversationListCursor, true);
887 }
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800888 }
889
Mindy Pereira75181e82012-04-18 08:17:13 -0700890 private void setAccount(Account account) {
Andy Huangb9ca9792012-05-18 15:31:49 -0700891 if (account == null) {
892 LogUtils.w(LOG_TAG, new Error(),
893 "AAC ignoring null (presumably invalid) account restoration");
894 return;
895 }
Andy Huangb1148412012-05-19 00:16:30 -0700896 LogUtils.d(LOG_TAG, "AbstractActivityController.setAccount(): account = %s", account.uri);
Vikram Aggarwal91e87372012-05-18 15:36:04 -0700897 mAccount = account;
Mindy Pereira75181e82012-04-18 08:17:13 -0700898 mActionBarView.setAccount(mAccount);
Vikram Aggarwal91e87372012-05-18 15:36:04 -0700899 if (account.settings == null) {
900 LogUtils.w(LOG_TAG, new Error(), "AAC ignoring account with null settings.");
901 return;
902 }
903 dispatchSettingsChange(mAccount.settings);
Mindy Pereira75181e82012-04-18 08:17:13 -0700904 }
905
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800906 /**
Mindy Pereira161f50d2012-02-28 15:47:19 -0800907 * Restore the state from the previous bundle. Subclasses should call this
908 * method from the parent class, since it performs important UI
909 * initialization.
910 *
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800911 * @param savedState
912 */
Andy Huang632721e2012-04-11 16:57:26 -0700913 @Override
914 public void onRestoreInstanceState(Bundle savedState) {
915 LogUtils.d(LOG_TAG, "IN AAC.onRestoreInstanceState");
916 if (savedState.containsKey(SAVED_CONVERSATION)) {
917 // Open the conversation.
Paul Westbrook534e4a22012-04-25 03:46:29 -0700918 final Conversation conversation =
919 (Conversation)savedState.getParcelable(SAVED_CONVERSATION);
920 if (conversation != null && conversation.position < 0) {
921 // Set the position to 0 on this conversation, as we don't know where it is
922 // in the list
923 conversation.position = 0;
924 }
925 setCurrentConversation(conversation);
Andy Huang632721e2012-04-11 16:57:26 -0700926 showConversation(mCurrentConversation);
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800927 }
Mindy Pereira967ede62012-03-22 09:29:09 -0700928
929 /**
930 * Restore the state of selected conversations. This needs to be done after the correct mode
931 * is set and the action bar is fully initialized. If not, several key pieces of state
932 * information will be missing, and the split views may not be initialized correctly.
933 * @param savedState
934 */
Andy Huang4556a442012-03-30 16:42:05 -0700935 restoreSelectedConversations(savedState);
Andy Huang632721e2012-04-11 16:57:26 -0700936 }
937
938 private void handleIntent(Intent intent) {
939 boolean handled = false;
940 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
941 if (intent.hasExtra(Utils.EXTRA_ACCOUNT)) {
942 setAccount((Account) intent.getParcelableExtra(Utils.EXTRA_ACCOUNT));
943 } else if (intent.hasExtra(Utils.EXTRA_ACCOUNT_STRING)) {
944 setAccount(Account.newinstance(intent
945 .getStringExtra(Utils.EXTRA_ACCOUNT_STRING)));
946 }
Andy Huangb9ca9792012-05-18 15:31:49 -0700947 if (mAccount == null) {
948 return;
Andy Huang632721e2012-04-11 16:57:26 -0700949 }
Andy Huangb9ca9792012-05-18 15:31:49 -0700950 mActivity.invalidateOptionsMenu();
Andy Huang632721e2012-04-11 16:57:26 -0700951
952 Folder folder = null;
953 if (intent.hasExtra(Utils.EXTRA_FOLDER)) {
954 // Open the folder.
955 LogUtils.d(LOG_TAG, "SHOW THE FOLDER at %s",
956 intent.getParcelableExtra(Utils.EXTRA_FOLDER));
957 folder = (Folder) intent.getParcelableExtra(Utils.EXTRA_FOLDER);
958
959 } else if (intent.hasExtra(Utils.EXTRA_FOLDER_STRING)) {
960 // Open the folder.
961 folder = new Folder(intent.getStringExtra(Utils.EXTRA_FOLDER_STRING));
962 }
963 if (folder != null) {
964 onFolderChanged(folder);
965 handled = true;
966 }
967
968 if (intent.hasExtra(Utils.EXTRA_CONVERSATION)) {
969 // Open the conversation.
970 LogUtils.d(LOG_TAG, "SHOW THE CONVERSATION at %s",
971 intent.getParcelableExtra(Utils.EXTRA_CONVERSATION));
Paul Westbrooka9161912012-04-24 10:10:04 -0700972 final Conversation conversation =
973 (Conversation)intent.getParcelableExtra(Utils.EXTRA_CONVERSATION);
974 if (conversation != null && conversation.position < 0) {
975 // Set the position to 0 on this conversation, as we don't know where it is
976 // in the list
977 conversation.position = 0;
978 }
979 setCurrentConversation(conversation);
Andy Huang632721e2012-04-11 16:57:26 -0700980 showConversation(mCurrentConversation);
981 handled = true;
982 }
983
984 if (!handled) {
985 // Nothing was saved; just load the account inbox.
986 loadAccountInbox();
987 }
988 } else if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
989 if (intent.hasExtra(Utils.EXTRA_ACCOUNT)) {
990 // Save this search query for future suggestions.
991 final String query = intent.getStringExtra(SearchManager.QUERY);
992 final String authority = mContext.getString(R.string.suggestions_authority);
993 SearchRecentSuggestions suggestions = new SearchRecentSuggestions(
994 mContext, authority, SuggestionsProvider.MODE);
995 suggestions.saveRecentQuery(query, null);
996
997 mViewMode.enterSearchResultsListMode();
998 setAccount((Account) intent.getParcelableExtra(Utils.EXTRA_ACCOUNT));
999 mActivity.invalidateOptionsMenu();
1000 restartOptionalLoader(LOADER_RECENT_FOLDERS);
1001 mRecentFolderList.setCurrentAccount(mAccount);
1002 fetchSearchFolder(intent);
1003 } else {
1004 LogUtils.e(LOG_TAG, "Missing account extra from search intent. Finishing");
1005 mActivity.finish();
1006 }
1007 }
1008 if (mAccount != null) {
1009 restartOptionalLoader(LOADER_ACCOUNT_UPDATE_CURSOR);
1010 }
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -08001011 }
1012
Andy Huang4556a442012-03-30 16:42:05 -07001013 /**
1014 * Copy any selected conversations stored in the saved bundle into our selection set,
1015 * triggering {@link ConversationSetObserver} callbacks as our selection set changes.
1016 *
1017 */
Mindy Pereira967ede62012-03-22 09:29:09 -07001018 private void restoreSelectedConversations(Bundle savedState) {
1019 if (savedState == null) {
Andy Huang4556a442012-03-30 16:42:05 -07001020 mSelectedSet.clear();
Mindy Pereira967ede62012-03-22 09:29:09 -07001021 return;
1022 }
Vikram Aggarwalcabd3f22012-04-19 10:14:41 -07001023 final ConversationSelectionSet selectedSet = savedState.getParcelable(SAVED_SELECTED_SET);
Andy Huang4556a442012-03-30 16:42:05 -07001024 if (selectedSet == null || selectedSet.isEmpty()) {
1025 mSelectedSet.clear();
Mindy Pereira967ede62012-03-22 09:29:09 -07001026 return;
1027 }
Andy Huang632721e2012-04-11 16:57:26 -07001028
1029 // putAll will take care of calling our registered onSetPopulated method
Vikram Aggarwalcabd3f22012-04-19 10:14:41 -07001030 mSelectedSet.putAll(selectedSet);
Mindy Pereira967ede62012-03-22 09:29:09 -07001031 }
1032
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -08001033 @Override
Andy Huang5895f7b2012-06-01 17:07:20 -07001034 public SubjectDisplayChanger getSubjectDisplayChanger() {
1035 return mActionBarView;
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -08001036 }
1037
Vikram Aggarwalec5cbf72012-03-08 15:10:35 -08001038 /**
1039 * Children can override this method, but they must call super.showConversation().
1040 * {@inheritDoc}
1041 */
1042 @Override
1043 public void showConversation(Conversation conversation) {
Vikram Aggarwalc67182d2012-04-03 14:35:06 -07001044 // Set the current conversation just in case it wasn't already set.
1045 setCurrentConversation(conversation);
Vikram Aggarwalec5cbf72012-03-08 15:10:35 -08001046 }
1047
Vikram Aggarwale128fc22012-04-04 12:33:34 -07001048 /**
Paul Westbrook2d50bcd2012-04-10 11:53:47 -07001049 * Children can override this method, but they must call super.showWaitForInitialization().
1050 * {@inheritDoc}
1051 */
1052 @Override
1053 public void showWaitForInitialization() {
1054 mViewMode.enterWaitingForInitializationMode();
1055 }
1056
1057 @Override
1058 public void hideWaitForInitialization() {
1059 }
1060
1061 @Override
1062 public void updateWaitMode() {
1063 final FragmentManager manager = mActivity.getFragmentManager();
1064 final WaitFragment waitFragment =
Vikram Aggarwal6902dcf2012-04-11 08:57:42 -07001065 (WaitFragment)manager.findFragmentByTag(TAG_WAIT);
Paul Westbrook2d50bcd2012-04-10 11:53:47 -07001066 if (waitFragment != null) {
1067 waitFragment.updateAccount(mAccount);
1068 }
1069 }
1070
Vikram Aggarwal48b2a6c2012-05-29 14:09:27 -07001071 /**
1072 * Returns true if we are waiting for the account to sync, and cannot show any folders or
1073 * conversation for the current account yet.
1074 * @return
1075 */
Paul Westbrook2d50bcd2012-04-10 11:53:47 -07001076 public boolean inWaitMode() {
1077 final FragmentManager manager = mActivity.getFragmentManager();
1078 final WaitFragment waitFragment =
Vikram Aggarwal6902dcf2012-04-11 08:57:42 -07001079 (WaitFragment)manager.findFragmentByTag(TAG_WAIT);
Paul Westbrook2d50bcd2012-04-10 11:53:47 -07001080 if (waitFragment != null) {
1081 final Account fragmentAccount = waitFragment.getAccount();
1082 return fragmentAccount.uri.equals(mAccount.uri) &&
1083 mViewMode.getMode() == ViewMode.WAITING_FOR_ACCOUNT_INITIALIZATION;
1084 }
1085 return false;
1086 }
1087
1088 /**
Vikram Aggarwale128fc22012-04-04 12:33:34 -07001089 * Children can override this method, but they must call super.showConversationList().
1090 * {@inheritDoc}
1091 */
1092 @Override
1093 public void showConversationList(ConversationListContext listContext) {
1094 }
1095
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -08001096 @Override
Mindy Pereira9b875682012-02-15 18:10:54 -08001097 public void onConversationSelected(Conversation conversation) {
Vikram Aggarwal5b7b3ab2012-04-03 15:43:55 -07001098 showConversation(conversation);
Mindy Pereira9fa43ca2012-05-17 14:18:01 -07001099 if (Intent.ACTION_SEARCH.equals(mActivity.getIntent().getAction())) {
Mindy Pereira68f2e222012-03-07 10:36:54 -08001100 mViewMode.enterSearchResultsConversationMode();
1101 } else {
1102 mViewMode.enterConversationMode();
1103 }
Vikram Aggarwal7d602882012-02-07 15:01:20 -08001104 }
Mindy Pereira555140c2012-02-15 14:55:29 -08001105
Vikram Aggarwalc67182d2012-04-03 14:35:06 -07001106 /**
1107 * Set the current conversation. This is the conversation on which all actions are performed.
1108 * Do not modify mCurrentConversation except through this method, which makes it easy to
1109 * perform common actions associated with changing the current conversation.
1110 * @param conversation
1111 */
Andy Huang632721e2012-04-11 16:57:26 -07001112 @Override
1113 public void setCurrentConversation(Conversation conversation) {
Mindy Pereira5040f1a2012-03-20 10:14:06 -07001114 mCurrentConversation = conversation;
Vikram Aggarwal135fd022012-04-11 14:44:15 -07001115 mTracker.initialize(mCurrentConversation);
Mindy Pereira5040f1a2012-03-20 10:14:06 -07001116 }
1117
Vikram Aggarwala9b93f32012-02-23 14:51:58 -08001118 /**
1119 * {@inheritDoc}
1120 */
Mindy Pereira555140c2012-02-15 14:55:29 -08001121 @Override
Vikram Aggarwal7dedb952012-02-16 16:10:23 -08001122 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
1123 // Create a loader to listen in on account changes.
Vikram Aggarwalfa4b47e2012-03-09 13:02:46 -08001124 switch (id) {
1125 case LOADER_ACCOUNT_CURSOR:
Paul Westbrookc2074c42012-03-22 15:26:58 -07001126 return new CursorLoader(mContext, MailAppProvider.getAccountsUri(),
Vikram Aggarwalfa4b47e2012-03-09 13:02:46 -08001127 UIProvider.ACCOUNTS_PROJECTION, null, null, null);
1128 case LOADER_FOLDER_CURSOR:
Paul Westbrookc7a070f2012-04-12 01:46:41 -07001129 final CursorLoader loader = new CursorLoader(mContext, mFolder.uri,
1130 UIProvider.FOLDERS_PROJECTION, null, null, null);
1131 loader.setUpdateThrottle(mFolderItemUpdateDelayMs);
1132 return loader;
Vikram Aggarwalfa4b47e2012-03-09 13:02:46 -08001133 case LOADER_RECENT_FOLDERS:
Paul Westbrook91d10502012-04-13 12:01:39 -07001134 if (mAccount != null && mAccount.recentFolderListUri != null) {
Paul Westbrookea4ee202012-03-12 14:12:33 -07001135 return new CursorLoader(mContext, mAccount.recentFolderListUri,
1136 UIProvider.FOLDERS_PROJECTION, null, null, null);
1137 }
1138 break;
Mindy Pereiraab486362012-03-21 18:18:53 -07001139 case LOADER_ACCOUNT_INBOX:
Vikram Aggarwal025eba82012-05-08 10:45:30 -07001140 final Uri defaultInbox = Settings.getDefaultInboxUri(mAccount.settings);
Vikram Aggarwal1e57e672012-05-07 14:48:24 -07001141 final Uri inboxUri = defaultInbox.equals(Uri.EMPTY) ?
1142 mAccount.folderListUri : defaultInbox;
Paul Westbrook7496e822012-04-24 09:50:54 -07001143 LogUtils.d(LOG_TAG, "Loading the default inbox: %s", inboxUri);
Paul Westbrook1220b6d2012-04-10 00:48:00 -07001144 if (inboxUri != null) {
1145 return new CursorLoader(mContext, inboxUri, UIProvider.FOLDERS_PROJECTION, null,
1146 null, null);
1147 }
1148 break;
Mindy Pereiraab486362012-03-21 18:18:53 -07001149 case LOADER_SEARCH:
1150 return Folder.forSearchResults(mAccount,
1151 args.getString(ConversationListContext.EXTRA_SEARCH_QUERY),
1152 mActivity.getActivityContext());
Paul Westbrook2d50bcd2012-04-10 11:53:47 -07001153 case LOADER_ACCOUNT_UPDATE_CURSOR:
1154 return new CursorLoader(mContext, mAccount.uri, UIProvider.ACCOUNTS_PROJECTION,
1155 null, null, null);
Vikram Aggarwalfa4b47e2012-03-09 13:02:46 -08001156 default:
Paul Westbrookad6a2752012-04-04 16:58:13 -07001157 LogUtils.wtf(LOG_TAG, "Loader returned unexpected id: %d", id);
Mindy Pereira5cb0c3e2012-02-22 15:22:47 -08001158 }
1159 return null;
Vikram Aggarwal7dedb952012-02-16 16:10:23 -08001160 }
1161
Paul Westbrookb1f573c2012-04-06 11:38:28 -07001162 @Override
1163 public void onLoaderReset(Loader<Cursor> loader) {
1164
1165 }
1166
Andy Huangf9a73482012-03-13 15:54:02 -07001167 /**
1168 * {@link LoaderManager} currently has a bug in
1169 * {@link LoaderManager#restartLoader(int, Bundle, android.app.LoaderManager.LoaderCallbacks)}
1170 * where, if a previous onCreateLoader returned a null loader, this method will NPE. Work around
1171 * this bug by destroying any loaders that may have been created as null (essentially because
1172 * they are optional loads, and may not apply to a particular account).
1173 * <p>
1174 * A simple null check before restarting a loader will not work, because that would not
1175 * give the controller a chance to invalidate UI corresponding the prior loader result.
1176 *
1177 * @param id loader ID to safely restart
Andy Huangf9a73482012-03-13 15:54:02 -07001178 */
Vikram Aggarwal94c94de2012-04-04 15:38:28 -07001179 private void restartOptionalLoader(int id) {
Andy Huangf9a73482012-03-13 15:54:02 -07001180 final LoaderManager lm = mActivity.getLoaderManager();
1181 lm.destroyLoader(id);
Vikram Aggarwal94c94de2012-04-04 15:38:28 -07001182 lm.restartLoader(id, Bundle.EMPTY, this);
1183 }
1184
Andy Huang632721e2012-04-11 16:57:26 -07001185 @Override
1186 public void registerConversationListObserver(DataSetObserver observer) {
1187 mConversationListObservable.registerObserver(observer);
1188 }
1189
1190 @Override
1191 public void unregisterConversationListObserver(DataSetObserver observer) {
1192 mConversationListObservable.unregisterObserver(observer);
1193 }
1194
Paul Westbrook23b74b92012-02-29 11:36:12 -08001195 private boolean accountsUpdated(Cursor accountCursor) {
1196 // Check to see if the current account hasn't been set, or the account cursor is empty
1197 if (mAccount == null || !accountCursor.moveToFirst()) {
Vikram Aggarwal6c511582012-02-27 10:59:47 -08001198 return true;
Paul Westbrook23b74b92012-02-29 11:36:12 -08001199 }
1200
1201 // Check to see if the number of accounts are different, from the number we saw on the last
1202 // updated
1203 if (mCurrentAccountUris.size() != accountCursor.getCount()) {
1204 return true;
1205 }
1206
1207 // Check to see if the account list is different or if the current account is not found in
1208 // the cursor.
1209 boolean foundCurrentAccount = false;
Vikram Aggarwal7dedb952012-02-16 16:10:23 -08001210 do {
Paul Westbrook23b74b92012-02-29 11:36:12 -08001211 final Uri accountUri =
1212 Uri.parse(accountCursor.getString(UIProvider.ACCOUNT_URI_COLUMN));
1213 if (!foundCurrentAccount && mAccount.uri.equals(accountUri)) {
1214 foundCurrentAccount = true;
1215 }
1216
1217 if (!mCurrentAccountUris.contains(accountUri)) {
1218 return true;
1219 }
Vikram Aggarwal7dedb952012-02-16 16:10:23 -08001220 } while (accountCursor.moveToNext());
Paul Westbrook23b74b92012-02-29 11:36:12 -08001221
1222 // As long as we found the current account, the list hasn't been updated
1223 return !foundCurrentAccount;
Vikram Aggarwal7dedb952012-02-16 16:10:23 -08001224 }
1225
1226 /**
Mindy Pereira161f50d2012-02-28 15:47:19 -08001227 * Update the accounts on the device. This currently loads the first account
1228 * in the list.
1229 *
Vikram Aggarwal7dedb952012-02-16 16:10:23 -08001230 * @param loader
Vikram Aggarwal6c511582012-02-27 10:59:47 -08001231 * @param accounts cursor into the AccountCache
Vikram Aggarwal7dedb952012-02-16 16:10:23 -08001232 * @return true if the update was successful, false otherwise
1233 */
Paul Westbrook23b74b92012-02-29 11:36:12 -08001234 private boolean updateAccounts(Loader<Cursor> loader, Cursor accounts) {
Vikram Aggarwal7dedb952012-02-16 16:10:23 -08001235 if (accounts == null || !accounts.moveToFirst()) {
1236 return false;
1237 }
Paul Westbrook23b74b92012-02-29 11:36:12 -08001238
Vikram Aggarwala9b93f32012-02-23 14:51:58 -08001239 final Account[] allAccounts = Account.getAllAccounts(accounts);
Paul Westbrook23b74b92012-02-29 11:36:12 -08001240
1241 // Save the uris for the accounts
1242 mCurrentAccountUris.clear();
1243 for (Account account : allAccounts) {
1244 mCurrentAccountUris.add(account.uri);
1245 }
1246
Andy Huang0d647352012-03-21 21:48:16 -07001247 // 1. current account is already set and is in allAccounts -> no-op
1248 // 2. current account is set and is not in allAccounts -> pick first (acct was deleted?)
1249 // 3. saved pref has an account -> pick that one
1250 // 4. otherwise just pick first
1251
1252 Account newAccount = null;
1253
1254 if (mAccount != null) {
1255 if (!mCurrentAccountUris.contains(mAccount.uri)) {
1256 newAccount = allAccounts[0];
1257 } else {
1258 newAccount = mAccount;
1259 }
Paul Westbrook23b74b92012-02-29 11:36:12 -08001260 } else {
Vikram Aggarwal135fd022012-04-11 14:44:15 -07001261 final String lastAccountUri = MailAppProvider.getInstance().getLastViewedAccount();
Andy Huang0d647352012-03-21 21:48:16 -07001262 if (lastAccountUri != null) {
1263 for (int i = 0; i < allAccounts.length; i++) {
1264 final Account acct = allAccounts[i];
1265 if (lastAccountUri.equals(acct.uri.toString())) {
1266 newAccount = acct;
1267 break;
1268 }
1269 }
1270 }
1271 if (newAccount == null) {
1272 newAccount = allAccounts[0];
1273 }
Paul Westbrook23b74b92012-02-29 11:36:12 -08001274 }
Andy Huang0d647352012-03-21 21:48:16 -07001275
Paul Westbrook23b74b92012-02-29 11:36:12 -08001276 onAccountChanged(newAccount);
Vikram Aggarwala9b93f32012-02-23 14:51:58 -08001277 mActionBarView.setAccounts(allAccounts);
1278 return (allAccounts.length > 0);
Vikram Aggarwal7dedb952012-02-16 16:10:23 -08001279 }
1280
Paul Westbrook6ead20d2012-03-19 14:48:14 -07001281 private void disableNotifications() {
1282 mNewEmailReceiver.activate(mContext, this);
1283 }
1284
1285 private void enableNotifications() {
1286 mNewEmailReceiver.deactivate();
1287 }
1288
1289 private void disableNotificationsOnAccountChange(Account account) {
1290 // If the new mail suppression receiver is activated for a different account, we want to
1291 // activate it for the new account.
1292 if (mNewEmailReceiver.activated() &&
1293 !mNewEmailReceiver.notificationsDisabledForAccount(account)) {
1294 // Deactivate the current receiver, otherwise multiple receivers may be registered.
1295 mNewEmailReceiver.deactivate();
1296 mNewEmailReceiver.activate(mContext, this);
1297 }
1298 }
1299
Vikram Aggarwala9b93f32012-02-23 14:51:58 -08001300 /**
1301 * {@inheritDoc}
1302 */
Vikram Aggarwal7dedb952012-02-16 16:10:23 -08001303 @Override
1304 public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
Mindy Pereira5cb0c3e2012-02-22 15:22:47 -08001305 // We want to reinitialize only if we haven't ever been initialized, or
1306 // if the current account has vanished.
Paul Westbrooke3e84292012-03-05 16:19:30 -08001307 if (data == null) {
Vikram Aggarwalfa4b47e2012-03-09 13:02:46 -08001308 LogUtils.e(LOG_TAG, "Received null cursor from loader id: %d", loader.getId());
Paul Westbrooke3e84292012-03-05 16:19:30 -08001309 }
Vikram Aggarwalfa4b47e2012-03-09 13:02:46 -08001310 switch (loader.getId()) {
1311 case LOADER_ACCOUNT_CURSOR:
Vikram Aggarwal27d89ad2012-06-12 13:38:40 -07001312 // If the account list is not null, and the account list cursor is empty,
Paul Westbrook2388c5d2012-03-25 12:29:11 -07001313 // we need to start the specified activity.
1314 if (data != null && data.getCount() == 0) {
1315 // If an empty cursor is returned, the MailAppProvider is indicating that
1316 // no accounts have been specified. We want to navigate to the "add account"
1317 // activity that will handle the intent returned by the MailAppProvider
1318
1319 // If the MailAppProvider believes that all accounts have been loaded, and the
1320 // account list is still empty, we want to prompt the user to add an account
1321 final Bundle extras = data.getExtras();
1322 final boolean accountsLoaded =
1323 extras.getInt(AccountCursorExtraKeys.ACCOUNTS_LOADED) != 0;
1324
1325 if (accountsLoaded) {
1326 final Intent noAccountIntent = MailAppProvider.getNoAccountIntent(mContext);
1327 if (noAccountIntent != null) {
1328 mActivity.startActivityForResult(noAccountIntent,
1329 ADD_ACCOUNT_REQUEST_CODE);
1330 }
1331 }
1332 } else {
1333 final boolean accountListUpdated = accountsUpdated(data);
1334 if (!isLoaderInitialized || accountListUpdated) {
1335 isLoaderInitialized = updateAccounts(loader, data);
1336 }
Vikram Aggarwalfa4b47e2012-03-09 13:02:46 -08001337 }
1338 break;
Paul Westbrook2d50bcd2012-04-10 11:53:47 -07001339 case LOADER_ACCOUNT_UPDATE_CURSOR:
1340 // We have gotten an update for current account.
1341
1342 // Make sure that this is an update for what is the current account
1343 if (data != null && data.moveToFirst()) {
1344 final Account updatedAccount = new Account(data);
1345
1346 if (updatedAccount.uri.equals(mAccount.uri)) {
Vikram Aggarwal7d816002012-04-17 17:06:41 -07001347 // Update the controller's reference to the current account
Paul Westbrook2d50bcd2012-04-10 11:53:47 -07001348 mAccount = updatedAccount;
Vikram Aggarwaledc137c2012-04-24 13:40:58 -07001349 LogUtils.d(LOG_TAG, "AbstractActivityController.onLoadFinished(): "
1350 + "mAccount = %s", mAccount.uri);
Vikram Aggarwal7d816002012-04-17 17:06:41 -07001351 dispatchSettingsChange(mAccount.settings);
Paul Westbrook2d50bcd2012-04-10 11:53:47 -07001352
1353 // Got an update for the current account
1354 final boolean inWaitingMode = inWaitMode();
1355 if (!updatedAccount.isAccountIntialized() && !inWaitingMode) {
1356 // Transition to waiting mode
1357 showWaitForInitialization();
1358 } else if (updatedAccount.isAccountIntialized() && inWaitingMode) {
1359 // Dismiss waiting mode
1360 hideWaitForInitialization();
1361 } else if (!updatedAccount.isAccountIntialized() && inWaitingMode) {
1362 // Update the WaitFragment's account object
1363 updateWaitMode();
1364 }
1365 } else {
1366 LogUtils.e(LOG_TAG, "Got update for account: %s with current account: %s",
1367 updatedAccount.uri, mAccount.uri);
1368 // We need to restart the loader, so the correct account information will
1369 // be returned
1370 restartOptionalLoader(LOADER_ACCOUNT_UPDATE_CURSOR);
1371 }
1372 }
1373 break;
Vikram Aggarwalfa4b47e2012-03-09 13:02:46 -08001374 case LOADER_FOLDER_CURSOR:
Vikram Aggarwalfa4b47e2012-03-09 13:02:46 -08001375 // Check status of the cursor.
Marc Blankfd9d0b82012-04-23 16:01:51 -07001376 if (data != null && data.moveToFirst()) {
1377 Folder folder = new Folder(data);
1378 if (folder.isSyncInProgress()) {
1379 mActionBarView.onRefreshStarted();
1380 } else {
1381 // Stop the spinner here.
1382 mActionBarView.onRefreshStopped(folder.lastSyncResult);
1383 }
1384 mActionBarView.onFolderUpdated(folder);
1385 final ConversationListFragment convList = getConversationListFragment();
1386 if (convList != null) {
1387 convList.onFolderUpdated(folder);
1388 }
1389 LogUtils.d(LOG_TAG, "FOLDER STATUS = %d", folder.syncStatus);
Paul Westbrookc808fac2012-02-22 16:42:18 -08001390 } else {
Marc Blankfd9d0b82012-04-23 16:01:51 -07001391 LogUtils.d(LOG_TAG, "Unable to get the folder %s",
1392 mFolder != null ? mAccount.name : "");
Mindy Pereira11dd5ef2012-03-10 15:10:18 -08001393 }
Vikram Aggarwalfa4b47e2012-03-09 13:02:46 -08001394 break;
Vikram Aggarwalfa4b47e2012-03-09 13:02:46 -08001395 case LOADER_RECENT_FOLDERS:
Vikram Aggarwal27d89ad2012-06-12 13:38:40 -07001396 // No recent folders and we are running on a phone? Populate the default recents.
1397 if (data != null && data.getCount() == 0 && !Utils.useTabletUI(mContext)) {
1398 final class PopulateDefault extends AsyncTask<Uri, Void, Void> {
1399 @Override
1400 protected Void doInBackground(Uri... uri) {
1401 // Asking for an update on the URI and ignore the result.
1402 final ContentResolver resolver = mContext.getContentResolver();
1403 resolver.update(uri[0], null, null, null);
1404 return null;
1405 }
1406 }
1407 final Uri uri = mAccount.defaultRecentFolderListUri;
1408 LogUtils.v(LOG_TAG, "Default recents at %s", uri);
1409 new PopulateDefault().execute(uri);
1410 break;
1411 }
1412 LogUtils.v(LOG_TAG, "Reading recent folders from the cursor.");
Vikram Aggarwalfa4b47e2012-03-09 13:02:46 -08001413 mRecentFolderList.loadFromUiProvider(data);
Vikram Aggarwal37263972012-04-17 15:51:14 -07001414 mActionBarView.requestRecentFoldersAndRedraw();
Vikram Aggarwalfa4b47e2012-03-09 13:02:46 -08001415 break;
Mindy Pereiraab486362012-03-21 18:18:53 -07001416 case LOADER_ACCOUNT_INBOX:
Marc Blankfd9d0b82012-04-23 16:01:51 -07001417 if (data != null && !data.isClosed() && data.moveToFirst()) {
Mindy Pereira0e88e9f2012-03-25 13:47:41 -07001418 Folder inbox = new Folder(data);
1419 onFolderChanged(inbox);
Mindy Pereirab4a43282012-03-23 16:20:03 -07001420 // Just want to get the inbox, don't care about updates to it
1421 // as this will be tracked by the folder change listener.
1422 mActivity.getLoaderManager().destroyLoader(LOADER_ACCOUNT_INBOX);
Mindy Pereira5ba33802012-03-26 16:30:11 -07001423 } else {
1424 LogUtils.d(LOG_TAG, "Unable to get the account inbox for account %s",
1425 mAccount != null ? mAccount.name : "");
Mindy Pereirab4a43282012-03-23 16:20:03 -07001426 }
Mindy Pereiraab486362012-03-21 18:18:53 -07001427 break;
1428 case LOADER_SEARCH:
1429 data.moveToFirst();
1430 Folder search = new Folder(data);
Mindy Pereira11e35962012-06-01 14:49:46 -07001431 updateFolder(search);
Mindy Pereiraab486362012-03-21 18:18:53 -07001432 mConvListContext = ConversationListContext.forSearchQuery(mAccount, mFolder,
Mindy Pereirad660d252012-03-26 11:48:43 -07001433 mActivity.getIntent()
1434 .getStringExtra(UIProvider.SearchQueryParameters.QUERY));
Mindy Pereiraab486362012-03-21 18:18:53 -07001435 showConversationList(mConvListContext);
1436 mActivity.invalidateOptionsMenu();
Mindy Pereira3b399222012-03-28 15:19:47 -07001437 mActivity.getLoaderManager().destroyLoader(LOADER_SEARCH);
Mindy Pereiraab486362012-03-21 18:18:53 -07001438 break;
Vikram Aggarwal7dedb952012-02-16 16:10:23 -08001439 }
1440 }
1441
Vikram Aggarwalc7694222012-04-23 13:37:01 -07001442 /**
1443 * Destructive actions on Conversations. This class should only be created by controllers, and
1444 * clients should only require {@link DestructiveAction}s, not specific implementations of the.
1445 * Only the controllers should know what kind of destructive actions are being created.
1446 */
Vikram Aggarwal531488e2012-05-29 16:36:52 -07001447 private class ConversationAction implements DestructiveAction {
Vikram Aggarwalacaa3c02012-04-24 12:45:27 -07001448 /**
1449 * The action to be performed. This is specified as the resource ID of the menu item
1450 * corresponding to this action: R.id.delete, R.id.report_spam, etc.
1451 */
Vikram Aggarwal7f602f72012-04-30 16:04:06 -07001452 private final int mAction;
Vikram Aggarwalbc67bb12012-04-30 14:05:35 -07001453 /** The action will act upon these conversations */
1454 private final Collection<Conversation> mTarget = new ArrayList<Conversation>();
Vikram Aggarwal7f602f72012-04-30 16:04:06 -07001455 /** Whether this destructive action has already been performed */
1456 private boolean mCompleted;
Vikram Aggarwalc4113952012-05-11 14:14:56 -07001457 /** Whether this is an action on the currently selected set. */
1458 private final boolean mIsSelectedSet;
Vikram Aggarwale8a85322012-04-24 09:01:38 -07001459
Mindy Pereirafbe40192012-03-20 10:40:45 -07001460 /**
1461 * Create a listener object. action is one of four constants: R.id.y_button (archive),
1462 * R.id.delete , R.id.mute, and R.id.report_spam.
1463 * @param action
Vikram Aggarwalbc67bb12012-04-30 14:05:35 -07001464 * @param target Conversation that we want to apply the action to.
Vikram Aggarwalc4113952012-05-11 14:14:56 -07001465 * @param isBatch whether the conversations are in the currently selected batch set.
1466 */
1467 public ConversationAction(int action, Collection<Conversation> target, boolean isBatch) {
Mindy Pereirafbe40192012-03-20 10:40:45 -07001468 mAction = action;
Vikram Aggarwalbc67bb12012-04-30 14:05:35 -07001469 mTarget.addAll(target);
Vikram Aggarwalc4113952012-05-11 14:14:56 -07001470 mIsSelectedSet = isBatch;
Mindy Pereirafbe40192012-03-20 10:40:45 -07001471 }
1472
Vikram Aggarwalacaa3c02012-04-24 12:45:27 -07001473 /**
1474 * The action common to child classes. This performs the action specified in the constructor
1475 * on the conversations given here.
Vikram Aggarwalacaa3c02012-04-24 12:45:27 -07001476 */
Vikram Aggarwalbc67bb12012-04-30 14:05:35 -07001477 @Override
1478 public void performAction() {
Vikram Aggarwal7f602f72012-04-30 16:04:06 -07001479 if (isPerformed()) {
1480 return;
1481 }
Vikram Aggarwal3d7ca9d2012-05-11 14:40:36 -07001482 // Certain actions force a return to list.
Mindy Pereira3cb28b52012-05-24 15:26:39 -07001483 boolean undoEnabled = mAccount.supportsCapability(AccountCapabilities.UNDO);
Vikram Aggarwal7dd054e2012-05-21 14:43:10 -07001484
1485 // Are we destroying the currently shown conversation? Show the next one.
1486 if (LogUtils.isLoggable(LOG_TAG, LogUtils.DEBUG)){
1487 LogUtils.d(LOG_TAG, "ConversationAction.performAction(): mIsConversationVisible=%b"
1488 + "\nmTarget=%s\nCurrent=%s", mIsConversationVisible,
1489 Conversation.toString(mTarget), mCurrentConversation);
1490 }
1491 if (mIsConversationVisible && Conversation.contains(mTarget, mCurrentConversation)) {
Mindy Pereiraa0c24442012-05-24 15:39:17 -07001492 int advance = Settings.getAutoAdvanceSetting(mAccount.settings);
1493 final Conversation next = advance == AutoAdvance.LIST ? null : mTracker
1494 .getNextConversation(advance, mTarget, mCurrentConversation);
Vikram Aggarwal7dd054e2012-05-21 14:43:10 -07001495 LogUtils.d(LOG_TAG, "Next conversation is: %s", next);
1496 showConversation(next);
1497 }
1498
Mindy Pereirafbe40192012-03-20 10:40:45 -07001499 switch (mAction) {
Mindy Pereira0692baf2012-03-23 17:34:31 -07001500 case R.id.archive:
Vikram Aggarwal7dd054e2012-05-21 14:43:10 -07001501 LogUtils.d(LOG_TAG, "Archiving");
Vikram Aggarwalbc67bb12012-04-30 14:05:35 -07001502 mConversationListCursor.archive(mContext, mTarget);
Mindy Pereirafbe40192012-03-20 10:40:45 -07001503 break;
1504 case R.id.delete:
Vikram Aggarwal7dd054e2012-05-21 14:43:10 -07001505 LogUtils.d(LOG_TAG, "Deleting");
Vikram Aggarwalbc67bb12012-04-30 14:05:35 -07001506 mConversationListCursor.delete(mContext, mTarget);
Marc Blank386243f2012-05-25 10:40:59 -07001507 if (!mFolder.supportsCapability(FolderCapabilities.DELETE_ACTION_FINAL)) {
1508 undoEnabled = false;
1509 }
Mindy Pereirafbe40192012-03-20 10:40:45 -07001510 break;
1511 case R.id.mute:
Vikram Aggarwal7dd054e2012-05-21 14:43:10 -07001512 LogUtils.d(LOG_TAG, "Muting");
Vikram Aggarwalbc67bb12012-04-30 14:05:35 -07001513 if (mFolder.supportsCapability(FolderCapabilities.DESTRUCTIVE_MUTE)) {
Vikram Aggarwal7f602f72012-04-30 16:04:06 -07001514 for (Conversation c : mTarget) {
1515 c.localDeleteOnUpdate = true;
1516 }
Vikram Aggarwalbc67bb12012-04-30 14:05:35 -07001517 }
1518 mConversationListCursor.mute(mContext, mTarget);
Mindy Pereirafbe40192012-03-20 10:40:45 -07001519 break;
1520 case R.id.report_spam:
Vikram Aggarwal7dd054e2012-05-21 14:43:10 -07001521 LogUtils.d(LOG_TAG, "Reporting spam");
Vikram Aggarwalbc67bb12012-04-30 14:05:35 -07001522 mConversationListCursor.reportSpam(mContext, mTarget);
1523 break;
1524 case R.id.remove_star:
Vikram Aggarwal7dd054e2012-05-21 14:43:10 -07001525 LogUtils.d(LOG_TAG, "Removing star");
Vikram Aggarwalbc67bb12012-04-30 14:05:35 -07001526 // Star removal is destructive in the Starred folder.
1527 mConversationListCursor.updateBoolean(mContext, mTarget,
1528 ConversationColumns.STARRED, false);
1529 break;
1530 case R.id.mark_not_important:
Vikram Aggarwal7dd054e2012-05-21 14:43:10 -07001531 LogUtils.d(LOG_TAG, "Marking not-important");
Vikram Aggarwalbc67bb12012-04-30 14:05:35 -07001532 // Marking not important is destructive in a mailbox containing only important
1533 // messages
1534 mConversationListCursor.updateInt(mContext, mTarget,
1535 ConversationColumns.PRIORITY, UIProvider.ConversationPriority.LOW);
Mindy Pereirafbe40192012-03-20 10:40:45 -07001536 break;
Vikram Aggarwal3d7ca9d2012-05-11 14:40:36 -07001537 }
1538 if (undoEnabled) {
1539 onUndoAvailable(new UndoOperation(mTarget.size(), mAction));
1540 }
Vikram Aggarwalbc67bb12012-04-30 14:05:35 -07001541 refreshConversationList();
Vikram Aggarwalc4113952012-05-11 14:14:56 -07001542 if (mIsSelectedSet) {
Vikram Aggarwalc4113952012-05-11 14:14:56 -07001543 mSelectedSet.clear();
1544 }
Mindy Pereirafbe40192012-03-20 10:40:45 -07001545 }
Vikram Aggarwal7f602f72012-04-30 16:04:06 -07001546
1547 /**
1548 * Returns true if this action has been performed, false otherwise.
1549 * @return
1550 */
1551 private synchronized boolean isPerformed() {
1552 if (mCompleted) {
1553 return true;
1554 }
1555 mCompleted = true;
1556 return false;
1557 }
Mindy Pereirafbe40192012-03-20 10:40:45 -07001558 }
Mindy Pereirae5f4dc02012-03-21 16:08:53 -07001559
Vikram Aggarwal3d7ca9d2012-05-11 14:40:36 -07001560 /**
1561 * Get a destructive action for a menu action.
1562 * This is a temporary method, to control the profusion of {@link DestructiveAction} classes
1563 * that are created. Please do not copy this paradigm.
1564 * @param action the resource ID of the menu action: R.id.delete, for example
1565 * @param target the conversations to act upon.
1566 * @return a {@link DestructiveAction} that performs the specified action.
1567 */
Vikram Aggarwal531488e2012-05-29 16:36:52 -07001568 private final DestructiveAction getAction(int action, Collection<Conversation> target) {
Vikram Aggarwal3d7ca9d2012-05-11 14:40:36 -07001569 final DestructiveAction da = new ConversationAction(action, target, false);
1570 registerDestructiveAction(da);
1571 return da;
1572 }
1573
Vikram Aggarwald503df42012-05-11 10:13:35 -07001574 // Called from the FolderSelectionDialog after a user is done selecting folders to assign the
1575 // conversations to.
Mindy Pereirae5f4dc02012-03-21 16:08:53 -07001576 @Override
Vikram Aggarwal531488e2012-05-29 16:36:52 -07001577 public final void assignFolder(
Mindy Pereiraf3a45562012-05-24 16:30:19 -07001578 Collection<Folder> folders, Collection<Conversation> target, boolean batch) {
Vikram Aggarwal3d7ca9d2012-05-11 14:40:36 -07001579 final boolean isDestructive = !Folder.containerIncludes(folders, mFolder);
Vikram Aggarwald503df42012-05-11 10:13:35 -07001580 LogUtils.d(LOG_TAG, "onFolderChangesCommit: isDestructive = %b", isDestructive);
1581 if (isDestructive) {
1582 for (final Conversation c : target) {
1583 c.localDeleteOnUpdate = true;
Mindy Pereira6778f462012-03-23 18:01:55 -07001584 }
Mindy Pereirae5f4dc02012-03-21 16:08:53 -07001585 }
Mindy Pereiraf3a45562012-05-24 16:30:19 -07001586 final DestructiveAction folderChange = getFolderChange(target, folders, isDestructive,
1587 batch);
Vikram Aggarwald503df42012-05-11 10:13:35 -07001588 // Update the UI elements depending no their visibility and availability
Vikram Aggarwal3d7ca9d2012-05-11 14:40:36 -07001589 // TODO(viki): Consolidate this into a single method requestDelete.
Vikram Aggarwald503df42012-05-11 10:13:35 -07001590 if (isDestructive) {
Vikram Aggarwal4f4782b2012-05-30 08:39:09 -07001591 delete(target, folderChange);
Vikram Aggarwal6902dcf2012-04-11 08:57:42 -07001592 } else {
Vikram Aggarwald503df42012-05-11 10:13:35 -07001593 requestUpdate(target, folderChange);
Mindy Pereirae5f4dc02012-03-21 16:08:53 -07001594 }
1595 }
1596
Mindy Pereira967ede62012-03-22 09:29:09 -07001597 @Override
Vikram Aggarwal3d7ca9d2012-05-11 14:40:36 -07001598 public final void onRefreshRequired() {
Marc Blankbf128eb2012-04-18 15:58:45 -07001599 if (mIsConversationListScrolling) {
1600 LogUtils.d(LOG_TAG, "onRefreshRequired: delay until scrolling done");
1601 return;
1602 }
1603 // Refresh the query in the background
Vikram Aggarwalb9fb2c12012-05-11 14:25:25 -07001604 final long now = System.currentTimeMillis();
1605 final long sinceLastRefresh = now - mConversationListRefreshTime;
Vikram Aggarwalef7c9922012-04-23 12:35:04 -07001606 if (mConversationListCursor.isRefreshRequired()) {
1607 mConversationListCursor.refresh();
Marc Blankbf128eb2012-04-18 15:58:45 -07001608 mTracker.updateCursor(mConversationListCursor);
1609 mConversationListRefreshTime = now;
1610 }
Marc Blankbf128eb2012-04-18 15:58:45 -07001611 }
1612
1613 /**
1614 * Called when the {@link ConversationCursor} is changed or has new data in it.
1615 * <p>
1616 * {@inheritDoc}
1617 */
1618 @Override
Vikram Aggarwal3d7ca9d2012-05-11 14:40:36 -07001619 public final void onRefreshReady() {
Vikram Aggarwal7f602f72012-04-30 16:04:06 -07001620 if (!mIsConversationListScrolling) {
Marc Blankbf128eb2012-04-18 15:58:45 -07001621 // Swap cursors
1622 mConversationListCursor.sync();
Marc Blankbf128eb2012-04-18 15:58:45 -07001623 }
1624 mTracker.updateCursor(mConversationListCursor);
1625 }
1626
1627 @Override
Vikram Aggarwal3d7ca9d2012-05-11 14:40:36 -07001628 public final void onDataSetChanged() {
Paul Westbrook9f119c72012-04-24 16:10:59 -07001629 updateConversationListFragment();
Andy Huang632721e2012-04-11 16:57:26 -07001630 mConversationListObservable.notifyChanged();
Marc Blankbf128eb2012-04-18 15:58:45 -07001631 }
1632
Vikram Aggarwal3d7ca9d2012-05-11 14:40:36 -07001633 /**
1634 * If the Conversation List Fragment is visible, updates the fragment.
1635 */
1636 private final void updateConversationListFragment() {
Marc Blankbf128eb2012-04-18 15:58:45 -07001637 final ConversationListFragment convList = getConversationListFragment();
1638 if (convList != null) {
Vikram Aggarwal75daee52012-04-30 11:13:09 -07001639 refreshConversationList();
Paul Westbrook9f119c72012-04-24 16:10:59 -07001640 if (convList.isVisible()) {
Paul Westbrook9f119c72012-04-24 16:10:59 -07001641 Utils.setConversationCursorVisibility(mConversationListCursor, true);
1642 }
Marc Blankbf128eb2012-04-18 15:58:45 -07001643 }
1644 }
1645
1646 /**
1647 * This class handles throttled refresh of the conversation list
1648 */
1649 static class RefreshTimerTask extends TimerTask {
1650 final Handler mHandler;
1651 final AbstractActivityController mController;
1652
1653 RefreshTimerTask(AbstractActivityController controller, Handler handler) {
1654 mHandler = handler;
1655 mController = controller;
1656 }
1657
1658 @Override
1659 public void run() {
1660 mHandler.post(new Runnable() {
1661 @Override
1662 public void run() {
1663 LogUtils.d(LOG_TAG, "Delay done... calling onRefreshRequired");
1664 mController.onRefreshRequired();
1665 }});
1666 }
1667 }
1668
1669 /**
1670 * Cancel the refresh task, if it's running
1671 */
1672 private void cancelRefreshTask () {
1673 if (mConversationListRefreshTask != null) {
1674 mConversationListRefreshTask.cancel();
1675 mConversationListRefreshTask = null;
1676 }
1677 }
1678
1679 @Override
1680 public void onScrollStateChanged(AbsListView view, int scrollState) {
1681 boolean isScrolling = (scrollState != OnScrollListener.SCROLL_STATE_IDLE);
1682 if (!isScrolling) {
Vikram Aggarwalef7c9922012-04-23 12:35:04 -07001683 if (mConversationListCursor.isRefreshRequired()) {
Marc Blankbf128eb2012-04-18 15:58:45 -07001684 LogUtils.d(LOG_TAG, "Stop scrolling: refresh");
Vikram Aggarwalef7c9922012-04-23 12:35:04 -07001685 mConversationListCursor.refresh();
1686 } else if (mConversationListCursor.isRefreshReady()) {
Marc Blankbf128eb2012-04-18 15:58:45 -07001687 LogUtils.d(LOG_TAG, "Stop scrolling: try sync");
1688 onRefreshReady();
1689 }
1690 }
1691 mIsConversationListScrolling = isScrolling;
1692 }
1693
1694 @Override
1695 public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
1696 int totalItemCount) {
1697 }
1698
1699 @Override
Mindy Pereira967ede62012-03-22 09:29:09 -07001700 public void onSetEmpty() {
Mindy Pereira967ede62012-03-22 09:29:09 -07001701 }
1702
1703 @Override
1704 public void onSetPopulated(ConversationSelectionSet set) {
Vikram Aggarwal6902dcf2012-04-11 08:57:42 -07001705 final ConversationListFragment convList = getConversationListFragment();
1706 if (convList == null) {
1707 return;
1708 }
Vikram Aggarwal4f4782b2012-05-30 08:39:09 -07001709 mCabActionMenu = new SelectedConversationsActionMenu(mActivity, set, mAccount, mFolder,
Vikram Aggarwal531488e2012-05-29 16:36:52 -07001710 (SwipeableListView) convList.getListView());
Vikram Aggarwale128fc22012-04-04 12:33:34 -07001711 enableCabMode();
Mindy Pereira967ede62012-03-22 09:29:09 -07001712 }
1713
Mindy Pereira967ede62012-03-22 09:29:09 -07001714 @Override
1715 public void onSetChanged(ConversationSelectionSet set) {
1716 // Do nothing. We don't care about changes to the set.
1717 }
1718
1719 @Override
1720 public ConversationSelectionSet getSelectedSet() {
1721 return mSelectedSet;
1722 }
1723
Vikram Aggarwale128fc22012-04-04 12:33:34 -07001724 /**
1725 * Disable the Contextual Action Bar (CAB). The selected set is not changed.
1726 */
1727 protected void disableCabMode() {
1728 if (mCabActionMenu != null) {
1729 mCabActionMenu.deactivate();
1730 }
1731 }
1732
1733 /**
1734 * Re-enable the CAB menu if required. The selection set is not changed.
1735 */
1736 protected void enableCabMode() {
1737 if (mCabActionMenu != null) {
1738 mCabActionMenu.activate();
1739 }
1740 }
1741
Mindy Pereira967ede62012-03-22 09:29:09 -07001742 @Override
Mindy Pereirafd0c2972012-03-27 13:50:39 -07001743 public void startSearch() {
Vikram Aggarwal35f19d72012-04-24 13:24:48 -07001744 if (mAccount == null) {
1745 // We cannot search if there is no account. Drop the request to the floor.
1746 LogUtils.d(LOG_TAG, "AbstractActivityController.startSearch(): null account");
1747 return;
1748 }
Mindy Pereirafd0c2972012-03-27 13:50:39 -07001749 if (mAccount.supportsCapability(UIProvider.AccountCapabilities.LOCAL_SEARCH)
1750 | mAccount.supportsCapability(UIProvider.AccountCapabilities.SERVER_SEARCH)) {
Vikram Aggarwal70f130e2012-04-03 12:32:14 -07001751 onSearchRequested(mActionBarView.getQuery());
Mindy Pereirafd0c2972012-03-27 13:50:39 -07001752 } else {
1753 Toast.makeText(mActivity.getActivityContext(), mActivity.getActivityContext()
Mindy Pereiraa46c2992012-03-27 14:12:39 -07001754 .getString(R.string.search_unsupported), Toast.LENGTH_SHORT).show();
Mindy Pereirafd0c2972012-03-27 13:50:39 -07001755 }
1756 }
Mindy Pereiraacf60392012-04-06 09:11:00 -07001757
Vikram Aggarwal0dda5732012-04-06 11:20:16 -07001758 @Override
1759 public void exitSearchMode() {
1760 if (mViewMode.getMode() == ViewMode.SEARCH_RESULTS_LIST) {
1761 mActivity.finish();
1762 }
1763 }
1764
Mindy Pereiraacf60392012-04-06 09:11:00 -07001765 /**
1766 * Supports dragging conversations to a folder.
1767 */
1768 @Override
1769 public boolean supportsDrag(DragEvent event, Folder folder) {
1770 return (folder != null
1771 && event != null
1772 && event.getClipDescription() != null
1773 && folder.supportsCapability
1774 (UIProvider.FolderCapabilities.CAN_ACCEPT_MOVED_MESSAGES)
1775 && folder.supportsCapability
1776 (UIProvider.FolderCapabilities.CAN_HOLD_MAIL)
1777 && !mFolder.uri.equals(folder.uri));
1778 }
1779
1780 /**
1781 * Handles dropping conversations to a label.
1782 */
1783 @Override
1784 public void handleDrop(DragEvent event, final Folder folder) {
Mindy Pereiraacf60392012-04-06 09:11:00 -07001785 if (!supportsDrag(event, folder)) {
1786 return;
1787 }
Mindy Pereiraacf60392012-04-06 09:11:00 -07001788 final Collection<Conversation> conversations = mSelectedSet.values();
Vikram Aggarwal440fe792012-05-10 17:23:15 -07001789 final Collection<Folder> dropTarget = Folder.listOf(folder);
Vikram Aggarwalbc67bb12012-04-30 14:05:35 -07001790 // Drag and drop is destructive: we remove conversations from the current folder.
Mindy Pereiraf3a45562012-05-24 16:30:19 -07001791 final DestructiveAction action = getFolderChange(conversations, dropTarget, true, true);
Vikram Aggarwal4f4782b2012-05-30 08:39:09 -07001792 delete(conversations, action);
Mindy Pereiraacf60392012-04-06 09:11:00 -07001793 }
Mindy Pereira0963ef82012-04-10 11:43:01 -07001794
1795 @Override
1796 public void onUndoCancel() {
1797 mUndoBarView.hide(false);
1798 }
1799
Paul Westbrookbf232c32012-04-18 03:17:41 -07001800
Mindy Pereira0963ef82012-04-10 11:43:01 -07001801 @Override
1802 public void onTouchEvent(MotionEvent event) {
1803 if (event.getAction() == MotionEvent.ACTION_DOWN) {
1804 if (mUndoBarView != null && !mUndoBarView.isEventInUndo(event)) {
1805 mUndoBarView.hide(true);
1806 }
1807 }
1808 }
Andy Huangb1c34dc2012-04-17 16:36:19 -07001809
Andy Huang632721e2012-04-11 16:57:26 -07001810 @Override
1811 public void onConversationSeen(Conversation conv) {
1812 mPagerController.onConversationSeen(conv);
1813 }
1814
Andy Huangb1c34dc2012-04-17 16:36:19 -07001815 private class ConversationListLoaderCallbacks implements
1816 LoaderManager.LoaderCallbacks<ConversationCursor> {
1817
1818 @Override
1819 public Loader<ConversationCursor> onCreateLoader(int id, Bundle args) {
1820 Loader<ConversationCursor> result = new ConversationCursorLoader((Activity) mActivity,
Paul Westbrookbf232c32012-04-18 03:17:41 -07001821 mAccount, mFolder.conversationListUri, mFolder.name);
Andy Huangb1c34dc2012-04-17 16:36:19 -07001822 return result;
1823 }
1824
1825 @Override
1826 public void onLoadFinished(Loader<ConversationCursor> loader, ConversationCursor data) {
Andy Huang632721e2012-04-11 16:57:26 -07001827 LogUtils.d(LOG_TAG, "IN AAC.ConversationCursor.onLoadFinished, data=%s loader=%s",
1828 data, loader);
Vikram Aggarwale8a85322012-04-24 09:01:38 -07001829 // Clear our all pending destructive actions before swapping the conversation cursor
1830 destroyPending(null);
Andy Huangb1c34dc2012-04-17 16:36:19 -07001831 mConversationListCursor = data;
Paul Westbrookbf232c32012-04-18 03:17:41 -07001832 mConversationListCursor.addListener(AbstractActivityController.this);
Andy Huangb1c34dc2012-04-17 16:36:19 -07001833
Andy Huange3df1ad2012-04-24 17:15:23 -07001834 mConversationListObservable.notifyChanged();
1835
Andy Huangb1c34dc2012-04-17 16:36:19 -07001836 // Register the AbstractActivityController as a listener to changes in
1837 // data in the cursor.
1838 final ConversationListFragment convList = getConversationListFragment();
1839 if (convList != null) {
1840 convList.onCursorUpdated();
Paul Westbrookbf232c32012-04-18 03:17:41 -07001841 convList.getListView().setOnScrollListener(AbstractActivityController.this);
Paul Westbrook9f119c72012-04-24 16:10:59 -07001842
1843 if (convList.isVisible()) {
1844 // The conversation list is visible.
1845 Utils.setConversationCursorVisibility(mConversationListCursor, true);
1846 }
Andy Huangb1c34dc2012-04-17 16:36:19 -07001847 }
1848 // Shown for search results in two-pane mode only.
1849 if (shouldShowFirstConversation()) {
1850 if (mConversationListCursor.getCount() > 0) {
1851 mConversationListCursor.moveToPosition(0);
1852 if (convList != null) {
1853 convList.getListView().setItemChecked(0, true);
1854 }
1855 final Conversation conv = new Conversation(mConversationListCursor);
1856 conv.position = 0;
1857 onConversationSelected(conv);
1858 }
1859 }
1860 }
1861
1862 @Override
1863 public void onLoaderReset(Loader<ConversationCursor> loader) {
1864 final ConversationListFragment convList = getConversationListFragment();
1865 if (convList == null) {
1866 return;
1867 }
1868 convList.onCursorUpdated();
1869 }
Paul Westbrookbf232c32012-04-18 03:17:41 -07001870 }
Andy Huangb1c34dc2012-04-17 16:36:19 -07001871
Paul Westbrookbf232c32012-04-18 03:17:41 -07001872 @Override
1873 public void sendConversationRead(String toFragment, Conversation conversation, boolean state,
1874 boolean local) {
1875 if (toFragment.equals(TAG_CONVERSATION_LIST)) {
Vikram Aggarwalef7c9922012-04-23 12:35:04 -07001876 if (mConversationListCursor != null) {
Paul Westbrookbf232c32012-04-18 03:17:41 -07001877 if (local) {
Vikram Aggarwalef7c9922012-04-23 12:35:04 -07001878 mConversationListCursor.setConversationColumn(conversation.uri.toString(), ConversationColumns.READ,
Paul Westbrookbf232c32012-04-18 03:17:41 -07001879 state);
1880 } else {
Vikram Aggarwalef7c9922012-04-23 12:35:04 -07001881 mConversationListCursor.markRead(mContext, state, conversation);
Paul Westbrookbf232c32012-04-18 03:17:41 -07001882 }
1883 }
1884 } else if (toFragment.equals(TAG_CONVERSATION)) {
1885 // TODO Handle setting read in conversation view
1886 }
1887 }
1888
1889 @Override
1890 public void sendConversationUriStarred(String toFragment, String conversationUri,
1891 boolean state, boolean local) {
1892 if (toFragment.equals(TAG_CONVERSATION_LIST)) {
Vikram Aggarwalef7c9922012-04-23 12:35:04 -07001893 if (mConversationListCursor != null) {
Paul Westbrookbf232c32012-04-18 03:17:41 -07001894 if (local) {
Vikram Aggarwalef7c9922012-04-23 12:35:04 -07001895 mConversationListCursor.setConversationColumn(conversationUri, ConversationColumns.STARRED, state);
Paul Westbrookbf232c32012-04-18 03:17:41 -07001896 } else {
Vikram Aggarwalef7c9922012-04-23 12:35:04 -07001897 mConversationListCursor.updateBoolean(mContext, conversationUri, ConversationColumns.STARRED, state);
Paul Westbrookbf232c32012-04-18 03:17:41 -07001898 }
1899 }
1900 } else if (toFragment.equals(TAG_CONVERSATION)) {
1901 // TODO Handle setting starred in conversation view
1902 }
Andy Huangb1c34dc2012-04-17 16:36:19 -07001903 }
Vikram Aggarwale8a85322012-04-24 09:01:38 -07001904
1905 /**
1906 * Destroy the pending {@link DestructiveAction} till now and assign the given action as the
1907 * next destructive action..
1908 * @param nextAction the next destructive action to be performed. This can be null.
1909 */
1910 private final void destroyPending(DestructiveAction nextAction) {
1911 // If there is a pending action, perform that first.
1912 if (mPendingDestruction != null) {
1913 mPendingDestruction.performAction();
1914 }
1915 mPendingDestruction = nextAction;
1916 }
1917
1918 /**
1919 * Register a destructive action with the controller. This performs the previous destructive
Vikram Aggarwalacaa3c02012-04-24 12:45:27 -07001920 * action as a side effect. This method is final because we don't want the child classes to
Vikram Aggarwal3d7ca9d2012-05-11 14:40:36 -07001921 * embellish this method any more.
Vikram Aggarwale8a85322012-04-24 09:01:38 -07001922 * @param action
1923 */
Vikram Aggarwal3d7ca9d2012-05-11 14:40:36 -07001924 private final void registerDestructiveAction(DestructiveAction action) {
Vikram Aggarwale8a85322012-04-24 09:01:38 -07001925 // TODO(viki): This is not a good idea. The best solution is for clients to request a
1926 // destructive action from the controller and for the controller to own the action. This is
1927 // a half-way solution while refactoring DestructiveAction.
1928 destroyPending(action);
1929 return;
1930 }
1931
Vikram Aggarwal531488e2012-05-29 16:36:52 -07001932 @Override
1933 public final DestructiveAction getBatchAction(int action) {
Vikram Aggarwalc4113952012-05-11 14:14:56 -07001934 final DestructiveAction da = new ConversationAction(action, mSelectedSet.values(), true);
Vikram Aggarwale8a85322012-04-24 09:01:38 -07001935 registerDestructiveAction(da);
1936 return da;
1937 }
Vikram Aggarwal41e6e712012-04-24 11:22:57 -07001938
Vikram Aggarwalbc67bb12012-04-30 14:05:35 -07001939 /**
1940 * Class to change the folders that are assigned to a set of conversations. This is destructive
1941 * because the user can remove the current folder from the conversation, in which case it has
1942 * to be animated away from the current folder.
1943 */
Vikram Aggarwal41e6e712012-04-24 11:22:57 -07001944 private class FolderDestruction implements DestructiveAction {
1945 private final Collection<Conversation> mTarget = new ArrayList<Conversation>();
1946 private final ArrayList<Folder> mFolderList = new ArrayList<Folder>();
1947 private final boolean mIsDestructive;
Vikram Aggarwal7f602f72012-04-30 16:04:06 -07001948 /** Whether this destructive action has already been performed */
1949 private boolean mCompleted;
Mindy Pereiraf3a45562012-05-24 16:30:19 -07001950 private boolean mIsSelectedSet;
Vikram Aggarwal41e6e712012-04-24 11:22:57 -07001951
1952 /**
1953 * Create a new folder destruction object to act on the given conversations.
1954 * @param target
1955 */
Vikram Aggarwal7f602f72012-04-30 16:04:06 -07001956 private FolderDestruction(final Collection<Conversation> target,
Mindy Pereiraf3a45562012-05-24 16:30:19 -07001957 final Collection<Folder> folders, boolean isDestructive, boolean isBatch) {
Vikram Aggarwal41e6e712012-04-24 11:22:57 -07001958 mTarget.addAll(target);
1959 mFolderList.addAll(folders);
1960 mIsDestructive = isDestructive;
Mindy Pereiraf3a45562012-05-24 16:30:19 -07001961 mIsSelectedSet = isBatch;
Vikram Aggarwal41e6e712012-04-24 11:22:57 -07001962 }
1963
Vikram Aggarwal41e6e712012-04-24 11:22:57 -07001964 @Override
1965 public void performAction() {
Vikram Aggarwal7f602f72012-04-30 16:04:06 -07001966 if (isPerformed()) {
1967 return;
1968 }
Vikram Aggarwal41e6e712012-04-24 11:22:57 -07001969 if (mIsDestructive) {
Vikram Aggarwal41e6e712012-04-24 11:22:57 -07001970 UndoOperation undoOp = new UndoOperation(mTarget.size(), R.id.change_folder);
1971 onUndoAvailable(undoOp);
1972 }
Vikram Aggarwal41e6e712012-04-24 11:22:57 -07001973 mConversationListCursor.updateString(mContext, mTarget,
Vikram Aggarwalbc67bb12012-04-30 14:05:35 -07001974 ConversationColumns.FOLDER_LIST, Folder.getUriString(mFolderList));
Vikram Aggarwal41e6e712012-04-24 11:22:57 -07001975 mConversationListCursor.updateString(mContext, mTarget,
1976 ConversationColumns.RAW_FOLDERS,
1977 Folder.getSerializedFolderString(mFolder, mFolderList));
Vikram Aggarwal7f602f72012-04-30 16:04:06 -07001978 refreshConversationList();
Mindy Pereiraf3a45562012-05-24 16:30:19 -07001979 if (mIsSelectedSet) {
1980 mSelectedSet.clear();
1981 }
Vikram Aggarwal7f602f72012-04-30 16:04:06 -07001982 }
1983 /**
1984 * Returns true if this action has been performed, false otherwise.
1985 * @return
1986 */
1987 private synchronized boolean isPerformed() {
1988 if (mCompleted) {
1989 return true;
Vikram Aggarwal41e6e712012-04-24 11:22:57 -07001990 }
Vikram Aggarwal7f602f72012-04-30 16:04:06 -07001991 mCompleted = true;
1992 return false;
Vikram Aggarwal41e6e712012-04-24 11:22:57 -07001993 }
1994 }
Vikram Aggarwal7f602f72012-04-30 16:04:06 -07001995
Vikram Aggarwald503df42012-05-11 10:13:35 -07001996 private final DestructiveAction getFolderChange(Collection<Conversation> target,
Mindy Pereiraf3a45562012-05-24 16:30:19 -07001997 Collection<Folder> folders, boolean isDestructive, boolean isBatch){
1998 final DestructiveAction da = new FolderDestruction(target, folders, isDestructive, isBatch);
Vikram Aggarwal41e6e712012-04-24 11:22:57 -07001999 registerDestructiveAction(da);
2000 return da;
2001 }
Vikram Aggarwal75daee52012-04-30 11:13:09 -07002002
Vikram Aggarwal4f4782b2012-05-30 08:39:09 -07002003 @Override
2004 public final void refreshConversationList() {
Vikram Aggarwal75daee52012-04-30 11:13:09 -07002005 final ConversationListFragment convList = getConversationListFragment();
2006 if (convList == null) {
2007 return;
2008 }
2009 convList.requestListRefresh();
2010 }
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -08002011}