blob: daf701ad561931a585ef2d09c651a59000ae519a [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;
Andrew Sapperstein00179f12012-08-09 15:15:40 -070025import android.app.DialogFragment;
Vikram Aggarwal6902dcf2012-04-11 08:57:42 -070026import android.app.Fragment;
Paul Westbrook2d50bcd2012-04-10 11:53:47 -070027import android.app.FragmentManager;
Andy Huangf9a73482012-03-13 15:54:02 -070028import android.app.LoaderManager;
Vikram Aggarwale620a7a2012-03-28 13:16:14 -070029import android.app.SearchManager;
Andy Huang839ada22012-07-20 15:48:40 -070030import android.content.ContentProviderOperation;
Vikram Aggarwal80aeac52012-02-07 15:27:20 -080031import android.content.ContentResolver;
Mindy Pereira6c2663d2012-07-20 15:37:29 -070032import android.content.ContentValues;
Vikram Aggarwala55b36c2012-01-13 11:45:02 -080033import android.content.Context;
Vikram Aggarwal7dedb952012-02-16 16:10:23 -080034import android.content.CursorLoader;
Vikram Aggarwal54452ae2012-03-13 15:29:00 -070035import android.content.DialogInterface;
Vikram Aggarwala55b36c2012-01-13 11:45:02 -080036import android.content.Intent;
Vikram Aggarwal7dedb952012-02-16 16:10:23 -080037import android.content.Loader;
Vikram Aggarwal80aeac52012-02-07 15:27:20 -080038import android.database.Cursor;
Andy Huang632721e2012-04-11 16:57:26 -070039import android.database.DataSetObservable;
40import android.database.DataSetObserver;
Paul Westbrook23b74b92012-02-29 11:36:12 -080041import android.net.Uri;
Vikram Aggarwal27d89ad2012-06-12 13:38:40 -070042import android.os.AsyncTask;
Vikram Aggarwala55b36c2012-01-13 11:45:02 -080043import android.os.Bundle;
Mindy Pereira21ab4902012-03-19 18:48:03 -070044import android.os.Handler;
Vikram Aggarwale620a7a2012-03-28 13:16:14 -070045import android.provider.SearchRecentSuggestions;
Mindy Pereiraacf60392012-04-06 09:11:00 -070046import android.view.DragEvent;
Vikram Aggarwala55b36c2012-01-13 11:45:02 -080047import android.view.KeyEvent;
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -080048import android.view.LayoutInflater;
Vikram Aggarwala55b36c2012-01-13 11:45:02 -080049import android.view.Menu;
Mindy Pereira28d5f722012-02-15 12:32:40 -080050import android.view.MenuInflater;
Vikram Aggarwala55b36c2012-01-13 11:45:02 -080051import android.view.MenuItem;
52import android.view.MotionEvent;
Mindy Pereirad33674992012-06-25 16:26:30 -070053import android.view.View;
Mindy Pereirafd0c2972012-03-27 13:50:39 -070054import android.widget.Toast;
Vikram Aggarwala55b36c2012-01-13 11:45:02 -080055
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -080056import com.android.mail.ConversationListContext;
Andy Huangf9a73482012-03-13 15:54:02 -070057import com.android.mail.R;
Mindy Pereira967ede62012-03-22 09:29:09 -070058import com.android.mail.browse.ConversationCursor;
Paul Westbrookbf232c32012-04-18 03:17:41 -070059import com.android.mail.browse.ConversationPagerController;
Andy Huang839ada22012-07-20 15:48:40 -070060import com.android.mail.browse.MessageCursor.ConversationMessage;
Marc Blank7c9f6ac2012-04-02 13:27:19 -070061import com.android.mail.browse.SelectedConversationsActionMenu;
Andy Huang991f4532012-08-14 13:32:55 -070062import com.android.mail.browse.SyncErrorDialogFragment;
Mindy Pereira9b875682012-02-15 18:10:54 -080063import com.android.mail.compose.ComposeActivity;
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -080064import com.android.mail.providers.Account;
Mindy Pereira9b875682012-02-15 18:10:54 -080065import com.android.mail.providers.Conversation;
Andy Huang839ada22012-07-20 15:48:40 -070066import com.android.mail.providers.ConversationInfo;
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -080067import com.android.mail.providers.Folder;
Vikram Aggarwal7a5d95a2012-07-27 16:24:54 -070068import com.android.mail.providers.FolderWatcher;
Paul Westbrookc2074c42012-03-22 15:26:58 -070069import com.android.mail.providers.MailAppProvider;
Mindy Pereiradac00542012-03-01 10:50:33 -080070import com.android.mail.providers.Settings;
Vikram Aggarwale620a7a2012-03-28 13:16:14 -070071import com.android.mail.providers.SuggestionsProvider;
Vikram Aggarwal80aeac52012-02-07 15:27:20 -080072import com.android.mail.providers.UIProvider;
Mindy Pereira3cb28b52012-05-24 15:26:39 -070073import com.android.mail.providers.UIProvider.AccountCapabilities;
Paul Westbrook2388c5d2012-03-25 12:29:11 -070074import com.android.mail.providers.UIProvider.AccountCursorExtraKeys;
Mindy Pereirac9d59182012-03-22 16:06:46 -070075import com.android.mail.providers.UIProvider.ConversationColumns;
Vikram Aggarwal54452ae2012-03-13 15:29:00 -070076import com.android.mail.providers.UIProvider.FolderCapabilities;
Andrew Sappersteinc2c9dc12012-07-02 18:17:32 -070077import com.android.mail.ui.ActionableToastBar.ActionClickedListener;
Andy Huang839ada22012-07-20 15:48:40 -070078import com.android.mail.utils.ContentProviderTask;
Paul Westbrookb334c902012-06-25 11:42:46 -070079import com.android.mail.utils.LogTag;
Mindy Pereira5cb0c3e2012-02-22 15:22:47 -080080import com.android.mail.utils.LogUtils;
Vikram Aggarwalfa131a22012-02-02 13:56:22 -080081import com.android.mail.utils.Utils;
Paul Westbrookca08fc12012-07-31 12:01:15 -070082import com.google.common.base.Objects;
Paul Westbrook77eee622012-07-10 13:41:57 -070083import com.google.common.collect.ImmutableList;
Mindy Pereiraacf60392012-04-06 09:11:00 -070084import com.google.common.collect.Lists;
Marc Blank167faa82012-03-21 13:11:53 -070085import com.google.common.collect.Sets;
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -080086
Marc Blank167faa82012-03-21 13:11:53 -070087import java.util.ArrayList;
Mindy Pereirafbe40192012-03-20 10:40:45 -070088import java.util.Collection;
Andy Huang839ada22012-07-20 15:48:40 -070089import java.util.Collections;
Mindy Pereira8db7e402012-07-13 10:32:47 -070090import java.util.HashMap;
Paul Westbrook23b74b92012-02-29 11:36:12 -080091import java.util.Set;
Marc Blankbf128eb2012-04-18 15:58:45 -070092import java.util.TimerTask;
Paul Westbrook23b74b92012-02-29 11:36:12 -080093
94
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -080095/**
Mindy Pereira161f50d2012-02-28 15:47:19 -080096 * This is an abstract implementation of the Activity Controller. This class
97 * knows how to respond to menu items, state changes, layout changes, etc. It
98 * weaves together the views and listeners, dispatching actions to the
99 * respective underlying classes.
Vikram Aggarwala55b36c2012-01-13 11:45:02 -0800100 * <p>
Mindy Pereira161f50d2012-02-28 15:47:19 -0800101 * Even though this class is abstract, it should provide default implementations
102 * for most, if not all the methods in the ActivityController interface. This
103 * makes the task of the subclasses easier: OnePaneActivityController and
104 * TwoPaneActivityController can be concise when the common functionality is in
105 * AbstractActivityController.
106 * </p>
107 * <p>
108 * In the Gmail codebase, this was called BaseActivityController
109 * </p>
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -0800110 */
Vikram Aggarwal531488e2012-05-29 16:36:52 -0700111public abstract class AbstractActivityController implements ActivityController {
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800112 // Keys for serialization of various information in Bundles.
Vikram Aggarwalcabd3f22012-04-19 10:14:41 -0700113 /** Tag for {@link #mAccount} */
Vikram Aggarwal6c511582012-02-27 10:59:47 -0800114 private static final String SAVED_ACCOUNT = "saved-account";
Vikram Aggarwalcabd3f22012-04-19 10:14:41 -0700115 /** Tag for {@link #mFolder} */
Mindy Pereira5e478d22012-03-26 18:04:58 -0700116 private static final String SAVED_FOLDER = "saved-folder";
Vikram Aggarwalcabd3f22012-04-19 10:14:41 -0700117 /** Tag for {@link #mCurrentConversation} */
Mindy Pereira26f23fc2012-03-27 10:26:04 -0700118 private static final String SAVED_CONVERSATION = "saved-conversation";
Vikram Aggarwalcabd3f22012-04-19 10:14:41 -0700119 /** Tag for {@link #mSelectedSet} */
120 private static final String SAVED_SELECTED_SET = "saved-selected-set";
Mindy Pereirad33674992012-06-25 16:26:30 -0700121 private static final String SAVED_TOAST_BAR_OP = "saved-toast-bar-op";
Mindy Pereira148b1912012-07-17 13:39:56 -0700122 protected static final String SAVED_HIERARCHICAL_FOLDER = "saved-hierarchical-folder";
Vikram Aggarwalf3341402012-08-07 10:09:38 -0700123 /** Tag for {@link ConversationListContext#searchQuery} */
124 private static final String SAVED_QUERY = "saved-query";
Mindy Pereira5cb0c3e2012-02-22 15:22:47 -0800125
Vikram Aggarwal6902dcf2012-04-11 08:57:42 -0700126 /** Tag used when loading a wait fragment */
127 protected static final String TAG_WAIT = "wait-fragment";
128 /** Tag used when loading a conversation list fragment. */
Paul Westbrookbf232c32012-04-18 03:17:41 -0700129 public static final String TAG_CONVERSATION_LIST = "tag-conversation-list";
Vikram Aggarwal6902dcf2012-04-11 08:57:42 -0700130 /** Tag used when loading a folder list fragment. */
131 protected static final String TAG_FOLDER_LIST = "tag-folder-list";
132
Vikram Aggarwald7a12cd2012-02-03 09:36:20 -0800133 protected Account mAccount;
Mindy Pereira12a4d802012-06-22 09:24:43 -0700134 protected Folder mFolder;
Andy Huang6681e542012-06-14 14:36:45 -0700135 protected MailActionBarView mActionBarView;
Vikram Aggarwal7c401b72012-08-13 16:43:47 -0700136 protected final ControllableActivity mActivity;
Vikram Aggarwala55b36c2012-01-13 11:45:02 -0800137 protected final Context mContext;
Vikram Aggarwal6902dcf2012-04-11 08:57:42 -0700138 private final FragmentManager mFragmentManager;
Vikram Aggarwalec5cbf72012-03-08 15:10:35 -0800139 protected final RecentFolderList mRecentFolderList;
Vikram Aggarwal80aeac52012-02-07 15:27:20 -0800140 protected ConversationListContext mConvListContext;
Mindy Pereira9b875682012-02-15 18:10:54 -0800141 protected Conversation mCurrentConversation;
Vikram Aggarwald7a12cd2012-02-03 09:36:20 -0800142
Paul Westbrook6ead20d2012-03-19 14:48:14 -0700143 /** A {@link android.content.BroadcastReceiver} that suppresses new e-mail notifications. */
144 private SuppressNotificationReceiver mNewEmailReceiver = null;
145
Mindy Pereirafbe40192012-03-20 10:40:45 -0700146 protected Handler mHandler = new Handler();
Mindy Pereirafa995b42012-07-25 12:06:13 -0700147
Vikram Aggarwalfa131a22012-02-02 13:56:22 -0800148 /**
Mindy Pereira161f50d2012-02-28 15:47:19 -0800149 * The current mode of the application. All changes in mode are initiated by
150 * the activity controller. View mode changes are propagated to classes that
151 * attach themselves as listeners of view mode changes.
Vikram Aggarwalfa131a22012-02-02 13:56:22 -0800152 */
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800153 protected final ViewMode mViewMode;
Vikram Aggarwal80aeac52012-02-07 15:27:20 -0800154 protected ContentResolver mResolver;
Vikram Aggarwal7dedb952012-02-16 16:10:23 -0800155 protected boolean isLoaderInitialized = false;
Mindy Pereirab7b33e02012-02-21 15:32:19 -0800156 private AsyncRefreshTask mAsyncRefreshTask;
Mindy Pereira5cb0c3e2012-02-22 15:22:47 -0800157
Andy Huang4e0158f2012-08-07 21:06:01 -0700158 private boolean mDestroyed;
159
Paul Westbrook23b74b92012-02-29 11:36:12 -0800160 private final Set<Uri> mCurrentAccountUris = Sets.newHashSet();
Mindy Pereira967ede62012-03-22 09:29:09 -0700161 protected ConversationCursor mConversationListCursor;
Andy Huang632721e2012-04-11 16:57:26 -0700162 private final DataSetObservable mConversationListObservable = new DataSetObservable() {
163 @Override
164 public void registerObserver(DataSetObserver observer) {
165 final int count = mObservers.size();
166 super.registerObserver(observer);
Vikram Aggarwal7c401b72012-08-13 16:43:47 -0700167 LogUtils.d(LOG_TAG, "IN AAC.register(List)Observer: %s before=%d after=%d", observer,
Andy Huang632721e2012-04-11 16:57:26 -0700168 count, mObservers.size());
169 }
170 @Override
171 public void unregisterObserver(DataSetObserver observer) {
172 final int count = mObservers.size();
173 super.unregisterObserver(observer);
Vikram Aggarwal7c401b72012-08-13 16:43:47 -0700174 LogUtils.d(LOG_TAG, "IN AAC.unregister(List)Observer: %s before=%d after=%d", observer,
Andy Huang632721e2012-04-11 16:57:26 -0700175 count, mObservers.size());
176 }
177 };
Marc Blankbf128eb2012-04-18 15:58:45 -0700178
Marc Blankbf128eb2012-04-18 15:58:45 -0700179 private RefreshTimerTask mConversationListRefreshTask;
Marc Blanke1d1b072012-04-13 17:29:16 -0700180
Vikram Aggarwal7c401b72012-08-13 16:43:47 -0700181 /** Listeners that are interested in changes to the current account. */
182 private final DataSetObservable mAccountObservers = new DataSetObservable() {
183 @Override
184 public void registerObserver(DataSetObserver observer) {
185 final int count = mObservers.size();
186 super.registerObserver(observer);
187 LogUtils.d(LOG_TAG, "IN AAC.register(Account)Observer: %s before=%d after=%d",
188 observer, count, mObservers.size());
189 }
190 @Override
191 public void unregisterObserver(DataSetObserver observer) {
192 final int count = mObservers.size();
193 super.unregisterObserver(observer);
194 LogUtils.d(LOG_TAG, "IN AAC.unregister(Account)Observer: %s before=%d after=%d",
195 observer, count, mObservers.size());
196 }
197 };
Vikram Aggarwal7d816002012-04-17 17:06:41 -0700198
Mindy Pereira967ede62012-03-22 09:29:09 -0700199 /**
200 * Selected conversations, if any.
201 */
Andy Huang4556a442012-03-30 16:42:05 -0700202 private final ConversationSelectionSet mSelectedSet = new ConversationSelectionSet();
Mindy Pereiradac00542012-03-01 10:50:33 -0800203
Paul Westbrookc7a070f2012-04-12 01:46:41 -0700204 private final int mFolderItemUpdateDelayMs;
205
Vikram Aggarwal135fd022012-04-11 14:44:15 -0700206 /** Keeps track of selected and unselected conversations */
Paul Westbrook937c94f2012-08-16 13:01:18 -0700207 final protected ConversationPositionTracker mTracker;
Vikram Aggarwalaf1ee0c2012-04-12 17:13:13 -0700208
Vikram Aggarwale128fc22012-04-04 12:33:34 -0700209 /**
210 * Action menu associated with the selected set.
211 */
212 SelectedConversationsActionMenu mCabActionMenu;
Andrew Sappersteinc2c9dc12012-07-02 18:17:32 -0700213 protected ActionableToastBar mToastBar;
Andy Huang632721e2012-04-11 16:57:26 -0700214 protected ConversationPagerController mPagerController;
Mindy Pereiraab486362012-03-21 18:18:53 -0700215
Andy Huangb1c34dc2012-04-17 16:36:19 -0700216 // this is split out from the general loader dispatcher because its loader doesn't return a
217 // basic Cursor
218 private final ConversationListLoaderCallbacks mListCursorCallbacks =
219 new ConversationListLoaderCallbacks();
220
Andy Huang090db1e2012-07-25 13:25:28 -0700221 private final DataSetObservable mFolderObservable = new DataSetObservable();
222
Paul Westbrookb334c902012-06-25 11:42:46 -0700223 protected static final String LOG_TAG = LogTag.getLogTag();
Vikram Aggarwalfa4b47e2012-03-09 13:02:46 -0800224 /** Constants used to differentiate between the types of loaders. */
225 private static final int LOADER_ACCOUNT_CURSOR = 0;
Vikram Aggarwalfa4b47e2012-03-09 13:02:46 -0800226 private static final int LOADER_FOLDER_CURSOR = 2;
227 private static final int LOADER_RECENT_FOLDERS = 3;
Mindy Pereira967ede62012-03-22 09:29:09 -0700228 private static final int LOADER_CONVERSATION_LIST = 4;
Mindy Pereiraab486362012-03-21 18:18:53 -0700229 private static final int LOADER_ACCOUNT_INBOX = 5;
230 private static final int LOADER_SEARCH = 6;
Paul Westbrook2d50bcd2012-04-10 11:53:47 -0700231 private static final int LOADER_ACCOUNT_UPDATE_CURSOR = 7;
Vikram Aggarwal7a5d95a2012-07-27 16:24:54 -0700232 /**
233 * Guaranteed to be the last loader ID used by the activity. Loaders are owned by Activity or
234 * fragments, and within an activity, loader IDs need to be unique. A hack to ensure that the
235 * {@link FolderWatcher} can create its folder loaders without clashing with the IDs of those
236 * of the {@link AbstractActivityController}. Currently, the {@link FolderWatcher} is the only
237 * other class that uses this activity's LoaderManager. If another class needs activity-level
238 * loaders, consider consolidating the loaders in a central location: a UI-less fragment
239 * perhaps.
240 */
241 public static final int LAST_LOADER_ID = 100;
Vikram Aggarwala55b36c2012-01-13 11:45:02 -0800242
Paul Westbrook2388c5d2012-03-25 12:29:11 -0700243 private static final int ADD_ACCOUNT_REQUEST_CODE = 1;
Paul Westbrook122f7c22012-08-20 17:50:31 -0700244 private static final int REAUTHENTICATE_REQUEST_CODE = 2;
Paul Westbrook2388c5d2012-03-25 12:29:11 -0700245
Vikram Aggarwale8a85322012-04-24 09:01:38 -0700246 /** The pending destructive action to be carried out before swapping the conversation cursor.*/
247 private DestructiveAction mPendingDestruction;
Andrew Sappersteinc2c9dc12012-07-02 18:17:32 -0700248 protected AsyncRefreshTask mFolderSyncTask;
Mindy Pereira49e5dbe2012-07-12 11:47:54 -0700249 // Task for setting any share intents for the account to enabled.
250 // This gets cancelled if the user kills the app before it finishes, and
251 // will just run the next time the user opens the app.
252 private AsyncTask<String, Void, Void> mEnableShareIntents;
Mindy Pereirac975e842012-07-16 09:15:00 -0700253 private Folder mFolderListFolder;
mindyp5390fca2012-08-22 12:12:25 -0700254 private boolean mIsDragHappening;
mindypead50392012-08-23 11:03:53 -0700255 private int mShowUndoBarDelay;
Andrew Sapperstein00179f12012-08-09 15:15:40 -0700256 public static final String SYNC_ERROR_DIALOG_FRAGMENT_TAG = "SyncErrorDialogFragment";
Vikram Aggarwale8a85322012-04-24 09:01:38 -0700257
Vikram Aggarwala55b36c2012-01-13 11:45:02 -0800258 public AbstractActivityController(MailActivity activity, ViewMode viewMode) {
259 mActivity = activity;
Vikram Aggarwal6902dcf2012-04-11 08:57:42 -0700260 mFragmentManager = mActivity.getFragmentManager();
Vikram Aggarwala55b36c2012-01-13 11:45:02 -0800261 mViewMode = viewMode;
262 mContext = activity.getApplicationContext();
Vikram Aggarwal025eba82012-05-08 10:45:30 -0700263 mRecentFolderList = new RecentFolderList(mContext);
Paul Westbrook937c94f2012-08-16 13:01:18 -0700264 mTracker = new ConversationPositionTracker(this);
Mindy Pereira967ede62012-03-22 09:29:09 -0700265 // Allow the fragment to observe changes to its own selection set. No other object is
266 // aware of the selected set.
267 mSelectedSet.addObserver(this);
Paul Westbrookc7a070f2012-04-12 01:46:41 -0700268
269 mFolderItemUpdateDelayMs =
270 mContext.getResources().getInteger(R.integer.folder_item_refresh_delay_ms);
mindypead50392012-08-23 11:03:53 -0700271 mShowUndoBarDelay =
272 mContext.getResources().getInteger(R.integer.show_undo_bar_delay_ms);
Vikram Aggarwala55b36c2012-01-13 11:45:02 -0800273 }
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -0800274
275 @Override
Vikram Aggarwale9a81032012-02-22 13:15:35 -0800276 public Account getCurrentAccount() {
277 return mAccount;
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800278 }
279
280 @Override
281 public ConversationListContext getCurrentListContext() {
Vikram Aggarwale9a81032012-02-22 13:15:35 -0800282 return mConvListContext;
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800283 }
284
285 @Override
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -0800286 public String getHelpContext() {
Paul Westbrook30745b62012-08-19 14:10:32 -0700287 final int mode = mViewMode.getMode();
288 final int helpContextResId;
289 switch (mode) {
290 case ViewMode.WAITING_FOR_ACCOUNT_INITIALIZATION:
291 helpContextResId = R.string.wait_help_context;
292 break;
293 default:
294 helpContextResId = R.string.main_help_context;
295 }
296 return mContext.getString(helpContextResId);
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -0800297 }
298
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800299 @Override
Vikram Aggarwalef7c9922012-04-23 12:35:04 -0700300 public final ConversationCursor getConversationListCursor() {
Mindy Pereira967ede62012-03-22 09:29:09 -0700301 return mConversationListCursor;
302 }
303
Vikram Aggarwal6902dcf2012-04-11 08:57:42 -0700304 /**
Vikram Aggarwal34a65012012-04-17 12:39:06 -0700305 * Check if the fragment is attached to an activity and has a root view.
306 * @param in
307 * @return true if the fragment is valid, false otherwise
308 */
309 private static final boolean isValidFragment(Fragment in) {
310 if (in == null || in.getActivity() == null || in.getView() == null) {
311 return false;
312 }
313 return true;
314 }
315
316 /**
Vikram Aggarwal6902dcf2012-04-11 08:57:42 -0700317 * Get the conversation list fragment for this activity. If the conversation list fragment
318 * is not attached, this method returns null
Andy Huang839ada22012-07-20 15:48:40 -0700319 *
Vikram Aggarwal6902dcf2012-04-11 08:57:42 -0700320 */
321 protected ConversationListFragment getConversationListFragment() {
322 final Fragment fragment = mFragmentManager.findFragmentByTag(TAG_CONVERSATION_LIST);
Vikram Aggarwal34a65012012-04-17 12:39:06 -0700323 if (isValidFragment(fragment)) {
324 return (ConversationListFragment) fragment;
Andy Huang9585d782012-04-16 19:45:04 -0700325 }
Vikram Aggarwal34a65012012-04-17 12:39:06 -0700326 return null;
Vikram Aggarwal6902dcf2012-04-11 08:57:42 -0700327 }
328
329 /**
Vikram Aggarwal6902dcf2012-04-11 08:57:42 -0700330 * Returns the folder list fragment attached with this activity. If no such fragment is attached
331 * this method returns null.
Andy Huang839ada22012-07-20 15:48:40 -0700332 *
Vikram Aggarwal6902dcf2012-04-11 08:57:42 -0700333 */
334 protected FolderListFragment getFolderListFragment() {
335 final Fragment fragment = mFragmentManager.findFragmentByTag(TAG_FOLDER_LIST);
Vikram Aggarwal34a65012012-04-17 12:39:06 -0700336 if (isValidFragment(fragment)) {
337 return (FolderListFragment) fragment;
Andy Huang9585d782012-04-16 19:45:04 -0700338 }
Vikram Aggarwal34a65012012-04-17 12:39:06 -0700339 return null;
Vikram Aggarwal6902dcf2012-04-11 08:57:42 -0700340 }
341
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800342 /**
Mindy Pereira161f50d2012-02-28 15:47:19 -0800343 * Initialize the action bar. This is not visible to OnePaneController and
344 * TwoPaneController so they cannot override this behavior.
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800345 */
Vikram Aggarwalabd24d82012-04-26 13:23:14 -0700346 private void initializeActionBar() {
347 final ActionBar actionBar = mActivity.getActionBar();
Andy Huang5895f7b2012-06-01 17:07:20 -0700348 if (actionBar == null) {
349 return;
Vikram Aggarwalabd24d82012-04-26 13:23:14 -0700350 }
Andy Huang5895f7b2012-06-01 17:07:20 -0700351
352 // be sure to inherit from the ActionBar theme when inflating
353 final LayoutInflater inflater = LayoutInflater.from(actionBar.getThemedContext());
Mindy Pereira82faec72012-06-14 17:21:50 -0700354 final boolean isSearch = mActivity.getIntent() != null
355 && Intent.ACTION_SEARCH.equals(mActivity.getIntent().getAction());
356 mActionBarView = (MailActionBarView) inflater.inflate(
357 isSearch ? R.layout.search_actionbar_view : R.layout.actionbar_view, null);
Andy Huang5895f7b2012-06-01 17:07:20 -0700358 mActionBarView.initialize(mActivity, this, mViewMode, actionBar, mRecentFolderList);
Vikram Aggarwalabd24d82012-04-26 13:23:14 -0700359 }
360
361 /**
362 * Attach the action bar to the activity.
363 */
364 private void attachActionBar() {
365 final ActionBar actionBar = mActivity.getActionBar();
366 if (actionBar != null && mActionBarView != null) {
Vikram Aggarwalec5cbf72012-03-08 15:10:35 -0800367 actionBar.setCustomView(mActionBarView, new ActionBar.LayoutParams(
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800368 LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
369 actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM,
Vikram Aggarwal2a25d0c2012-02-21 16:43:10 -0800370 ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_TITLE);
Vikram Aggarwalabd24d82012-04-26 13:23:14 -0700371 mActionBarView.attach();
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800372 }
Vikram Aggarwalabd24d82012-04-26 13:23:14 -0700373 mViewMode.addListener(mActionBarView);
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800374 }
375
376 /**
Mindy Pereira161f50d2012-02-28 15:47:19 -0800377 * Returns whether the conversation list fragment is visible or not.
378 * Different layouts will have their own notion on the visibility of
379 * fragments, so this method needs to be overriden.
380 *
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800381 */
382 protected abstract boolean isConversationListVisible();
383
Vikram Aggarwaleb7d3292012-04-20 17:07:20 -0700384 /**
385 * Switch the current account to the one provided as an argument to the method.
Vikram Aggarwal66bc6ed2012-07-31 09:59:55 -0700386 * @param account new account
387 * @param shouldReloadInbox whether the default inbox should be reloaded.
Vikram Aggarwaleb7d3292012-04-20 17:07:20 -0700388 */
Vikram Aggarwal66bc6ed2012-07-31 09:59:55 -0700389 private void switchAccount(Account account, boolean shouldReloadInbox){
Vikram Aggarwaleb7d3292012-04-20 17:07:20 -0700390 // Current account is different from the new account, restart loaders and show
391 // the account Inbox.
392 mAccount = account;
Vikram Aggarwaledc137c2012-04-24 13:40:58 -0700393 LogUtils.d(LOG_TAG, "AbstractActivityController.switchAccount(): mAccount = %s",
394 mAccount.uri);
Vikram Aggarwaleb7d3292012-04-20 17:07:20 -0700395 cancelRefreshTask();
Vikram Aggarwal2e0a2362012-08-22 09:05:49 -0700396 // If we are currently listening to changes in conversation, the account switch is going to
397 // make us do a lot of un-necessary work. Let's stop listening to changes.
398 if (mPagerController != null) {
399 mPagerController.stopListening();
400 }
Vikram Aggarwal60069912012-07-24 14:26:09 -0700401 updateSettings();
Vikram Aggarwal66bc6ed2012-07-31 09:59:55 -0700402 if (shouldReloadInbox) {
403 loadAccountInbox();
404 }
Vikram Aggarwaleb7d3292012-04-20 17:07:20 -0700405 restartOptionalLoader(LOADER_RECENT_FOLDERS);
406 mActivity.invalidateOptionsMenu();
407 disableNotificationsOnAccountChange(mAccount);
408 restartOptionalLoader(LOADER_ACCOUNT_UPDATE_CURSOR);
409 MailAppProvider.getInstance().setLastViewedAccount(mAccount.uri.toString());
410 }
411
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800412 @Override
Mindy Pereira28e0c342012-02-17 15:05:13 -0800413 public void onAccountChanged(Account account) {
Vikram Aggarwal60069912012-07-24 14:26:09 -0700414 LogUtils.d(LOG_TAG, "onAccountChanged (%s) called.", account);
415 // Is the account or account settings different from the existing account?
Vikram Aggarwal66bc6ed2012-07-31 09:59:55 -0700416 final boolean firstLoad = mAccount == null;
417 final boolean accountChanged = firstLoad || !account.uri.equals(mAccount.uri);
418 final boolean settingsChanged = firstLoad || !account.settings.equals(mAccount.settings);
419 if (accountChanged || settingsChanged) {
Mindy Pereirafa20c1a2012-07-23 13:00:02 -0700420 if (account != null) {
Mindy Pereirafa995b42012-07-25 12:06:13 -0700421 final String accountName = account.name;
422 mHandler.post(new Runnable() {
423 @Override
424 public void run() {
425 MailActivity.setForegroundNdef(MailActivity.getMailtoNdef(accountName));
426 }
427 });
Mindy Pereirafa20c1a2012-07-23 13:00:02 -0700428 }
mindypc6adce32012-08-22 18:46:42 -0700429 if (accountChanged) {
430 commitDestructiveActions(false);
431 }
Vikram Aggarwal66bc6ed2012-07-31 09:59:55 -0700432 switchAccount(account, accountChanged);
Mindy Pereira28d5f722012-02-15 12:32:40 -0800433 }
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800434 }
435
Vikram Aggarwale6340bc2012-03-26 15:57:09 -0700436 /**
Vikram Aggarwaleb7d3292012-04-20 17:07:20 -0700437 * Changes the settings for the current account. The new settings are provided as a parameter.
438 * @param settings
439 */
Vikram Aggarwal60069912012-07-24 14:26:09 -0700440 public void updateSettings() {
Vikram Aggarwal7c401b72012-08-13 16:43:47 -0700441 mAccountObservers.notifyChanged();
Mindy Pereira12a676a2012-03-23 13:00:22 -0700442 resetActionBarIcon();
Vikram Aggarwaleb7d3292012-04-20 17:07:20 -0700443 mActivity.invalidateOptionsMenu();
444 // If the user was viewing the default Inbox here, and the new setting contains a different
Vikram Aggarwal60069912012-07-24 14:26:09 -0700445 // default Inbox, we don't want to load a different folder here. So do not change the
446 // current folder.
Mindy Pereiradac00542012-03-01 10:50:33 -0800447 }
448
Vikram Aggarwal7c401b72012-08-13 16:43:47 -0700449 /**
450 * Adds a listener interested in change in the current account. If a class is storing a
451 * reference to the current account, it should listen on changes, so it can receive updates to
452 * settings. Must happen in the UI thread.
453 */
Mindy Pereiraefe3d252012-03-01 14:20:44 -0800454 @Override
Vikram Aggarwal7c401b72012-08-13 16:43:47 -0700455 public void registerAccountObserver(DataSetObserver obs) {
456 mAccountObservers.registerObserver(obs);
Mindy Pereiraefe3d252012-03-01 14:20:44 -0800457 }
458
Vikram Aggarwal7d816002012-04-17 17:06:41 -0700459 /**
Vikram Aggarwal7c401b72012-08-13 16:43:47 -0700460 * Removes a listener from receiving current account changes.
Vikram Aggarwal7d816002012-04-17 17:06:41 -0700461 * Must happen in the UI thread.
462 */
Vikram Aggarwal7c401b72012-08-13 16:43:47 -0700463 @Override
464 public void unregisterAccountObserver(DataSetObserver obs) {
465 mAccountObservers.unregisterObserver(obs);
Vikram Aggarwal7d816002012-04-17 17:06:41 -0700466 }
467
Vikram Aggarwal7c401b72012-08-13 16:43:47 -0700468 @Override
469 public Account getAccount() {
470 return mAccount;
Vikram Aggarwal7d816002012-04-17 17:06:41 -0700471 }
472
Mindy Pereirae0828392012-03-08 10:38:40 -0800473 private void fetchSearchFolder(Intent intent) {
Mindy Pereiraab486362012-03-21 18:18:53 -0700474 Bundle args = new Bundle();
475 args.putString(ConversationListContext.EXTRA_SEARCH_QUERY, intent
Mindy Pereirae0828392012-03-08 10:38:40 -0800476 .getStringExtra(ConversationListContext.EXTRA_SEARCH_QUERY));
Mindy Pereiraab486362012-03-21 18:18:53 -0700477 mActivity.getLoaderManager().restartLoader(LOADER_SEARCH, args, this);
Mindy Pereirae0828392012-03-08 10:38:40 -0800478 }
479
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800480 @Override
Mindy Pereira28e0c342012-02-17 15:05:13 -0800481 public void onFolderChanged(Folder folder) {
Vikram Aggarwalf3341402012-08-07 10:09:38 -0700482 changeFolder(folder, null);
483 }
484
485 /**
Vikram Aggarwal203dc002012-08-23 13:59:04 -0700486 * Sets the folder state without changing view mode and without creating a list fragment, if
487 * possible.
488 * @param folder
489 */
490 private void setListContext(Folder folder, String query) {
491 updateFolder(folder);
492 if (query != null) {
493 mConvListContext = ConversationListContext.forSearchQuery(mAccount, mFolder, query);
494 } else {
495 mConvListContext = ConversationListContext.forFolder(mAccount, mFolder);
496 }
497 // Add the folder that we were viewing to the recent folders list.
498 // TODO: this may need to be fine tuned. If this is the signal that is indicating that
499 // the list is shown to the user, this could fire in one pane if the user goes directly
500 // to a conversation
501 updateRecentFolderList();
502 cancelRefreshTask();
503 }
504
505 /**
Vikram Aggarwalf3341402012-08-07 10:09:38 -0700506 * Changes the folder to the value provided here. This causes the view mode to change.
507 * @param folder the folder to change to
508 * @param query if non-null, this represents the search string that the folder represents.
509 */
510 private void changeFolder(Folder folder, String query) {
Vikram Aggarwal203dc002012-08-23 13:59:04 -0700511 if (!Objects.equal(mFolder, folder)) {
512 commitDestructiveActions(false);
513 }
Mindy Pereiraa1f99982012-07-16 16:32:15 -0700514 if (folder != null && !folder.equals(mFolder)
515 || (mViewMode.getMode() != ViewMode.CONVERSATION_LIST)) {
Vikram Aggarwal203dc002012-08-23 13:59:04 -0700516 setListContext(folder, query);
Mindy Pereira28e0c342012-02-17 15:05:13 -0800517 showConversationList(mConvListContext);
Mindy Pereira28e0c342012-02-17 15:05:13 -0800518 }
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800519 }
520
Vikram Aggarwal203dc002012-08-23 13:59:04 -0700521 /**
522 * Update the conversation list to {@link #mConvListContext} without creating a new frament if
523 * possible.
524 */
525 protected abstract void updateConversationList();
526
527 private void setFirstFolder(Folder folder, String query) {
528 setListContext(folder, query);
529 updateConversationList();
530 }
531
Mindy Pereira13c12a62012-05-31 15:41:08 -0700532 @Override
Mindy Pereira505df5f2012-06-19 17:57:17 -0700533 public void onFolderSelected(Folder folder) {
Mindy Pereira13c12a62012-05-31 15:41:08 -0700534 onFolderChanged(folder);
535 }
536
Vikram Aggarwal792ccba2012-03-27 13:46:57 -0700537 /**
538 * Update the recent folders. This only needs to be done once when accessing a new folder.
539 */
Paul Westbrook7ebdfd02012-03-21 15:55:30 -0700540 private void updateRecentFolderList() {
Mindy Pereiraab486362012-03-21 18:18:53 -0700541 if (mFolder != null) {
Marc Blank2675dbc2012-04-03 10:17:13 -0700542 mRecentFolderList.touchFolder(mFolder, mAccount);
Mindy Pereiraab486362012-03-21 18:18:53 -0700543 }
Paul Westbrook7ebdfd02012-03-21 15:55:30 -0700544 }
545
Mindy Pereiraab486362012-03-21 18:18:53 -0700546 // TODO(mindyp): set this up to store a copy of the folder as a transient
547 // field in the account.
Vikram Aggarwaleb7d3292012-04-20 17:07:20 -0700548 @Override
549 public void loadAccountInbox() {
Vikram Aggarwal94c94de2012-04-04 15:38:28 -0700550 restartOptionalLoader(LOADER_ACCOUNT_INBOX);
Mindy Pereiraf6acdad2012-03-15 11:21:13 -0700551 }
552
Vikram Aggarwalec5cbf72012-03-08 15:10:35 -0800553 /** Set the current folder */
Mindy Pereira11e35962012-06-01 14:49:46 -0700554 private void updateFolder(Folder folder) {
Mindy Pereira5cb0c3e2012-02-22 15:22:47 -0800555 // Start watching folder for sync status.
Mindy Pereirac975e842012-07-16 09:15:00 -0700556 boolean wasNull = mFolder == null;
Paul Westbrook4bfce9a2012-08-07 22:54:42 -0700557 if (folder != null && !folder.equals(mFolder) && folder.isInitialized()) {
Vikram Aggarwaleb7d3292012-04-20 17:07:20 -0700558 LogUtils.d(LOG_TAG, "AbstractActivityController.setFolder(%s)", folder.name);
Andy Huangb1c34dc2012-04-17 16:36:19 -0700559 final LoaderManager lm = mActivity.getLoaderManager();
Vikram Aggarwal7a5d95a2012-07-27 16:24:54 -0700560 mFolder = folder;
Mindy Pereiraf9323cd2012-02-29 13:47:09 -0800561 mActionBarView.setFolder(mFolder);
Andy Huangb1c34dc2012-04-17 16:36:19 -0700562
Vikram Aggarwal2e0a2362012-08-22 09:05:49 -0700563 // Stop listening to changes to the previous folder.
564 if (mPagerController != null) {
565 mPagerController.stopListening();
566 }
567
Andy Huangb1c34dc2012-04-17 16:36:19 -0700568 // Only when we switch from one folder to another do we want to restart the
569 // folder and conversation list loaders (to trigger onCreateLoader).
570 // The first time this runs when the activity is [re-]initialized, we want to re-use the
571 // previous loader's instance and data upon configuration change (e.g. rotation).
Mindy Pereira11e35962012-06-01 14:49:46 -0700572 // If there was not already an instance of the loader, init it.
573 if (lm.getLoader(LOADER_FOLDER_CURSOR) == null) {
Andy Huangb1c34dc2012-04-17 16:36:19 -0700574 lm.initLoader(LOADER_FOLDER_CURSOR, null, this);
Andy Huangb1c34dc2012-04-17 16:36:19 -0700575 } else {
576 lm.restartLoader(LOADER_FOLDER_CURSOR, null, this);
Mindy Pereirac975e842012-07-16 09:15:00 -0700577 }
578 // In this case, we are starting from no folder, which would occur
579 // the first time the app was launched or on orientation changes.
580 // We want to attach to an existing loader, if available.
581 if (wasNull || lm.getLoader(LOADER_CONVERSATION_LIST) == null) {
582 lm.initLoader(LOADER_CONVERSATION_LIST, null, mListCursorCallbacks);
583 } else {
584 // However, if there was an existing folder AND we have changed
585 // folders, we want to restart the loader to get the information
586 // for the newly selected folder
mindypfaa87d52012-08-20 09:35:03 -0700587 lm.destroyLoader(LOADER_CONVERSATION_LIST);
588 lm.initLoader(LOADER_CONVERSATION_LIST, null, mListCursorCallbacks);
Andy Huangb1c34dc2012-04-17 16:36:19 -0700589 }
Paul Westbrook4bfce9a2012-08-07 22:54:42 -0700590 } else if (!folder.isInitialized()) {
Paul Westbrook3c0f4192012-08-09 12:15:42 -0700591 LogUtils.e(LOG_TAG, new Error(), "Uninitialized Folder %s in setFolder.", folder);
Mindy Pereira5cb0c3e2012-02-22 15:22:47 -0800592 }
593 }
594
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800595 @Override
Andy Huang090db1e2012-07-25 13:25:28 -0700596 public Folder getFolder() {
597 return mFolder;
598 }
599
600 @Override
Mindy Pereirac975e842012-07-16 09:15:00 -0700601 public Folder getHierarchyFolder() {
602 return mFolderListFolder;
603 }
604
605 @Override
606 public void setHierarchyFolder(Folder folder) {
607 mFolderListFolder = folder;
Mindy Pereira23aadfd2012-05-25 11:24:33 -0700608 }
609
610 @Override
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800611 public void onActivityResult(int requestCode, int resultCode, Intent data) {
Paul Westbrook122f7c22012-08-20 17:50:31 -0700612 switch (requestCode) {
613 case ADD_ACCOUNT_REQUEST_CODE:
614 // We were waiting for the user to create an account
615 if (resultCode == Activity.RESULT_OK) {
616 // restart the loader to get the updated list of accounts
617 mActivity.getLoaderManager().initLoader(
618 LOADER_ACCOUNT_CURSOR, null, this);
619 } else {
620 // The user failed to create an account, just exit the app
621 mActivity.finish();
622 }
623 break;
624 case REAUTHENTICATE_REQUEST_CODE:
625 if (resultCode == Activity.RESULT_OK) {
626 // The user successfully authenticated, attempt to refresh the list
627 final Uri refreshUri = mFolder != null ? mFolder.refreshUri : null;
628 if (refreshUri != null) {
629 startAsyncRefreshTask(refreshUri);
630 }
631 }
632 break;
Paul Westbrook2388c5d2012-03-25 12:29:11 -0700633 }
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800634 }
635
636 @Override
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800637 public void onConversationListVisibilityChanged(boolean visible) {
Paul Westbrook9f119c72012-04-24 16:10:59 -0700638 if (mConversationListCursor != null) {
639 // The conversation list is visible.
640 Utils.setConversationCursorVisibility(mConversationListCursor, visible);
641 }
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800642 }
643
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -0800644 /**
Vikram Aggarwal7dd054e2012-05-21 14:43:10 -0700645 * Called when a conversation is visible. Child classes must call the super class implementation
646 * before performing local computation.
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -0800647 */
648 @Override
649 public void onConversationVisibilityChanged(boolean visible) {
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -0800650 return;
651 }
652
653 @Override
Vikram Aggarwald7a12cd2012-02-03 09:36:20 -0800654 public boolean onCreate(Bundle savedState) {
Vikram Aggarwalabd24d82012-04-26 13:23:14 -0700655 initializeActionBar();
Vikram Aggarwala55b36c2012-01-13 11:45:02 -0800656 // Allow shortcut keys to function for the ActionBar and menus.
657 mActivity.setDefaultKeyMode(Activity.DEFAULT_KEYS_SHORTCUT);
Vikram Aggarwal80aeac52012-02-07 15:27:20 -0800658 mResolver = mActivity.getContentResolver();
Paul Westbrook6ead20d2012-03-19 14:48:14 -0700659 mNewEmailReceiver = new SuppressNotificationReceiver();
Vikram Aggarwal7c401b72012-08-13 16:43:47 -0700660 mRecentFolderList.initialize(mActivity);
Paul Westbrook6ead20d2012-03-19 14:48:14 -0700661
Mindy Pereira161f50d2012-02-28 15:47:19 -0800662 // All the individual UI components listen for ViewMode changes. This
Mindy Pereirab849dfb2012-03-07 18:13:15 -0800663 // simplifies the amount of logic in the AbstractActivityController, but increases the
664 // possibility of timing-related bugs.
Vikram Aggarwala55b36c2012-01-13 11:45:02 -0800665 mViewMode.addListener(this);
Andy Huang632721e2012-04-11 16:57:26 -0700666 mPagerController = new ConversationPagerController(mActivity, this);
Andrew Sappersteinc2c9dc12012-07-02 18:17:32 -0700667 mToastBar = (ActionableToastBar) mActivity.findViewById(R.id.toast_bar);
Vikram Aggarwalada51782012-04-26 14:18:31 -0700668 attachActionBar();
Andy Huang632721e2012-04-11 16:57:26 -0700669
670 final Intent intent = mActivity.getIntent();
Vikram Aggarwalabd24d82012-04-26 13:23:14 -0700671 // Immediately handle a clean launch with intent, and any state restoration
Andy Huang632721e2012-04-11 16:57:26 -0700672 // that does not rely on restored fragments or loader data
673 // any state restoration that relies on those can be done later in
674 // onRestoreInstanceState, once fragments are up and loader data is re-delivered
675 if (savedState != null) {
676 if (savedState.containsKey(SAVED_ACCOUNT)) {
677 setAccount((Account) savedState.getParcelable(SAVED_ACCOUNT));
678 mActivity.invalidateOptionsMenu();
679 }
680 if (savedState.containsKey(SAVED_FOLDER)) {
Vikram Aggarwalf3341402012-08-07 10:09:38 -0700681 final Folder folder = (Folder) savedState.getParcelable(SAVED_FOLDER);
Vikram Aggarwal203dc002012-08-23 13:59:04 -0700682 final String query = savedState.getString(SAVED_QUERY, null);
683 setFirstFolder(folder, query);
Andy Huang632721e2012-04-11 16:57:26 -0700684 }
685 } else if (intent != null) {
686 handleIntent(intent);
687 }
Andy Huang632721e2012-04-11 16:57:26 -0700688 // Create the accounts loader; this loads the account switch spinner.
689 mActivity.getLoaderManager().initLoader(LOADER_ACCOUNT_CURSOR, null, this);
Andy Huang632721e2012-04-11 16:57:26 -0700690 return true;
Andy Huangb1c34dc2012-04-17 16:36:19 -0700691 }
692
693 @Override
Andrew Sapperstein00179f12012-08-09 15:15:40 -0700694 public void onRestart() {
695 DialogFragment fragment = (DialogFragment)
696 mFragmentManager.findFragmentByTag(SYNC_ERROR_DIALOG_FRAGMENT_TAG);
697 if (fragment != null) {
698 fragment.dismiss();
699 }
700 }
701
702 @Override
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800703 public Dialog onCreateDialog(int id, Bundle bundle) {
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800704 return null;
Vikram Aggarwala55b36c2012-01-13 11:45:02 -0800705 }
706
707 @Override
Vikram Aggarwal4eb52712012-06-19 16:24:50 -0700708 public final boolean onCreateOptionsMenu(Menu menu) {
Mindy Pereira28d5f722012-02-15 12:32:40 -0800709 MenuInflater inflater = mActivity.getMenuInflater();
Mindy Pereiraf5acda42012-02-15 20:13:59 -0800710 inflater.inflate(mActionBarView.getOptionsMenuId(), menu);
Mindy Pereira68f2e222012-03-07 10:36:54 -0800711 mActionBarView.onCreateOptionsMenu(menu);
Mindy Pereira28d5f722012-02-15 12:32:40 -0800712 return true;
Vikram Aggarwala55b36c2012-01-13 11:45:02 -0800713 }
714
715 @Override
Vikram Aggarwal4eb52712012-06-19 16:24:50 -0700716 public final boolean onKeyDown(int keyCode, KeyEvent event) {
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -0800717 // TODO(viki): Auto-generated method stub
718 return false;
719 }
720
721 @Override
Vikram Aggarwal4eb52712012-06-19 16:24:50 -0700722 public final boolean onOptionsItemSelected(MenuItem item) {
Vikram Aggarwal54452ae2012-03-13 15:29:00 -0700723 final int id = item.getItemId();
Vikram Aggarwal7dd054e2012-05-21 14:43:10 -0700724 LogUtils.d(LOG_TAG, "AbstractController.onOptionsItemSelected(%d) called.", id);
Mindy Pereira9b875682012-02-15 18:10:54 -0800725 boolean handled = true;
Mindy Pereiraba68fda2012-05-24 15:53:06 -0700726 final Collection<Conversation> target = Conversation.listOf(mCurrentConversation);
Vikram Aggarwal112cd162012-06-18 10:51:13 -0700727 final Settings settings = (mAccount == null) ? null : mAccount.settings;
Mindy Pereira8937bf12012-07-23 14:05:02 -0700728 // The user is choosing a new action; commit whatever they had been doing before.
mindypc6adce32012-08-22 18:46:42 -0700729 commitDestructiveActions(true);
Mindy Pereira28d5f722012-02-15 12:32:40 -0800730 switch (id) {
Mindy Pereiraba68fda2012-05-24 15:53:06 -0700731 case R.id.archive: {
732 final boolean showDialog = (settings != null && settings.confirmArchive);
733 confirmAndDelete(target, showDialog, R.plurals.confirm_archive_conversation,
734 getAction(R.id.archive, target));
735 break;
736 }
Mindy Pereira01f30502012-08-14 10:30:51 -0700737 case R.id.remove_folder:
738 delete(target, getRemoveFolder(target, mFolder, true, false, true));
739 break;
Mindy Pereiraba68fda2012-05-24 15:53:06 -0700740 case R.id.delete: {
741 final boolean showDialog = (settings != null && settings.confirmDelete);
742 confirmAndDelete(target, showDialog, R.plurals.confirm_delete_conversation,
743 getAction(R.id.delete, target));
744 break;
745 }
746 case R.id.mark_important:
Vikram Aggarwal531488e2012-05-29 16:36:52 -0700747 updateConversation(Conversation.listOf(mCurrentConversation),
748 ConversationColumns.PRIORITY, UIProvider.ConversationPriority.HIGH);
Mindy Pereiraba68fda2012-05-24 15:53:06 -0700749 break;
750 case R.id.mark_not_important:
Mindy Pereira0d03ef82012-08-15 09:05:48 -0700751 if (mFolder != null && mFolder.isImportantOnly()) {
752 delete(target, getAction(R.id.mark_not_important, target));
753 } else {
754 updateConversation(Conversation.listOf(mCurrentConversation),
755 ConversationColumns.PRIORITY, UIProvider.ConversationPriority.LOW);
756 }
Mindy Pereiraba68fda2012-05-24 15:53:06 -0700757 break;
758 case R.id.mute:
Vikram Aggarwal4f4782b2012-05-30 08:39:09 -0700759 delete(target, getAction(R.id.mute, target));
Mindy Pereiraba68fda2012-05-24 15:53:06 -0700760 break;
761 case R.id.report_spam:
Vikram Aggarwal4f4782b2012-05-30 08:39:09 -0700762 delete(target, getAction(R.id.report_spam, target));
Mindy Pereiraba68fda2012-05-24 15:53:06 -0700763 break;
Paul Westbrook77eee622012-07-10 13:41:57 -0700764 case R.id.mark_not_spam:
765 // Currently, since spam messages are only shown in list with other spam messages,
766 // marking a message not as spam is a destructive action
767 delete(target, getAction(R.id.mark_not_spam, target));
768 break;
Paul Westbrook76b20622012-07-12 11:45:43 -0700769 case R.id.report_phishing:
770 delete(target, getAction(R.id.report_phishing, target));
771 break;
Mindy Pereiraf5acda42012-02-15 20:13:59 -0800772 case android.R.id.home:
773 onUpPressed();
774 break;
Mindy Pereira9b875682012-02-15 18:10:54 -0800775 case R.id.compose:
776 ComposeActivity.compose(mActivity.getActivityContext(), mAccount);
777 break;
Mindy Pereira28d5f722012-02-15 12:32:40 -0800778 case R.id.show_all_folders:
779 showFolderList();
780 break;
Mindy Pereira28e0c342012-02-17 15:05:13 -0800781 case R.id.refresh:
782 requestFolderRefresh();
783 break;
Mindy Pereira1f936682012-03-02 11:30:33 -0800784 case R.id.settings:
785 Utils.showSettings(mActivity.getActivityContext(), mAccount);
Paul Westbrook2861b6a2012-02-15 15:25:34 -0800786 break;
Paul Westbrooke5503552012-03-28 00:35:57 -0700787 case R.id.folder_options:
788 Utils.showFolderSettings(mActivity.getActivityContext(), mAccount, mFolder);
789 break;
Paul Westbrook94e440d2012-02-24 11:03:47 -0800790 case R.id.help_info_menu_item:
Paul Westbrook30745b62012-08-19 14:10:32 -0700791 Utils.showHelp(mActivity.getActivityContext(), mAccount, getHelpContext());
Paul Westbrook94e440d2012-02-24 11:03:47 -0800792 break;
Vikram Aggarwal54452ae2012-03-13 15:29:00 -0700793 case R.id.feedback_menu_item:
Paul Westbrook17beb0b2012-08-20 13:34:37 -0700794 Utils.sendFeedback(mActivity.getActivityContext(), mAccount, false);
Mindy Pereirafbe40192012-03-20 10:40:45 -0700795 break;
Paul Westbrook18babd22012-04-09 22:17:08 -0700796 case R.id.manage_folders_item:
797 Utils.showManageFolder(mActivity.getActivityContext(), mAccount);
798 break;
Vikram Aggarwald503df42012-05-11 10:13:35 -0700799 case R.id.change_folder:
800 new FoldersSelectionDialog(mActivity.getActivityContext(), mAccount, this,
Mindy Pereiraa832d3d2012-07-27 11:19:50 -0700801 Conversation.listOf(mCurrentConversation), false, mFolder).show();
Vikram Aggarwald503df42012-05-11 10:13:35 -0700802 break;
Mindy Pereira9b875682012-02-15 18:10:54 -0800803 default:
804 handled = false;
805 break;
Mindy Pereira28d5f722012-02-15 12:32:40 -0800806 }
Mindy Pereira9b875682012-02-15 18:10:54 -0800807 return handled;
Vikram Aggarwala55b36c2012-01-13 11:45:02 -0800808 }
809
Vikram Aggarwal531488e2012-05-29 16:36:52 -0700810 @Override
Mindy Pereira6c2663d2012-07-20 15:37:29 -0700811 public void updateConversation(Collection<Conversation> target, ContentValues values) {
812 mConversationListCursor.updateValues(mContext, target, values);
813 refreshConversationList();
814 }
815
816 @Override
Vikram Aggarwal531488e2012-05-29 16:36:52 -0700817 public void updateConversation(Collection <Conversation> target, String columnName,
818 boolean value) {
819 mConversationListCursor.updateBoolean(mContext, target, columnName, value);
Vikram Aggarwal75daee52012-04-30 11:13:09 -0700820 refreshConversationList();
Vikram Aggarwal54452ae2012-03-13 15:29:00 -0700821 }
822
Vikram Aggarwal531488e2012-05-29 16:36:52 -0700823 @Override
Mindy Pereira6c2663d2012-07-20 15:37:29 -0700824 public void updateConversation(Collection <Conversation> target, String columnName,
825 int value) {
Vikram Aggarwal531488e2012-05-29 16:36:52 -0700826 mConversationListCursor.updateInt(mContext, target, columnName, value);
Vikram Aggarwal75daee52012-04-30 11:13:09 -0700827 refreshConversationList();
Mindy Pereirac9d59182012-03-22 16:06:46 -0700828 }
829
Vikram Aggarwal531488e2012-05-29 16:36:52 -0700830 @Override
831 public void updateConversation(Collection <Conversation> target, String columnName,
832 String value) {
833 mConversationListCursor.updateString(mContext, target, columnName, value);
Vikram Aggarwal75daee52012-04-30 11:13:09 -0700834 refreshConversationList();
Vikram Aggarwal54452ae2012-03-13 15:29:00 -0700835 }
836
Andy Huang839ada22012-07-20 15:48:40 -0700837 @Override
838 public void markConversationMessagesUnread(Conversation conv, Set<Uri> unreadMessageUris,
Vikram Aggarwal4a878b62012-07-31 15:09:25 -0700839 String originalConversationInfo) {
Andy Huang8f6b0062012-07-31 15:36:31 -0700840 // The only caller of this method is the conversation view, from where marking unread should
841 // *always* take you back to list mode.
842 showConversation(null);
843
Andy Huang839ada22012-07-20 15:48:40 -0700844 // locally mark conversation unread (the provider is supposed to propagate message unread
845 // to conversation unread)
846 conv.read = false;
847
Andy Huang28e31e22012-07-26 16:33:15 -0700848 // only do a granular 'mark unread' if a subset of messages are unread
849 final int unreadCount = (unreadMessageUris == null) ? 0 : unreadMessageUris.size();
Mindy Pereira0972e072012-08-01 17:43:06 -0700850 final int numMessages = conv.getNumMessages();
851 final boolean subsetIsUnread = (numMessages > 1 && unreadCount > 0
852 && unreadCount < numMessages);
Andy Huang28e31e22012-07-26 16:33:15 -0700853
854 if (!subsetIsUnread) {
Vikram Aggarwal66bc2aa2012-08-02 10:47:03 -0700855 // Conversations are neither marked read, nor viewed, and we don't want to show
856 // the next conversation.
857 markConversationsRead(Collections.singletonList(conv), false, false, false);
Andy Huang839ada22012-07-20 15:48:40 -0700858 } else {
Andy Huangdaa06ab2012-07-24 10:46:44 -0700859 mConversationListCursor.setConversationColumn(conv.uri, ConversationColumns.READ, 0);
Andy Huang839ada22012-07-20 15:48:40 -0700860
Mindy Pereira7b6d03d2012-07-30 13:03:41 -0700861 // locally update conversation's conversationInfo to revert to original version
Andy Huang28e31e22012-07-26 16:33:15 -0700862 if (originalConversationInfo != null) {
863 mConversationListCursor.setConversationColumn(conv.uri,
864 ConversationColumns.CONVERSATION_INFO, originalConversationInfo);
865 }
Andy Huang839ada22012-07-20 15:48:40 -0700866
867 // applyBatch with each CPO as an UPDATE op on each affected message uri
868 final ArrayList<ContentProviderOperation> ops = Lists.newArrayList();
869 String authority = null;
870 for (Uri messageUri : unreadMessageUris) {
871 if (authority == null) {
872 authority = messageUri.getAuthority();
873 }
874 ops.add(ContentProviderOperation.newUpdate(messageUri)
875 .withValue(UIProvider.MessageColumns.READ, 0)
876 .build());
877 }
878
879 new ContentProviderTask() {
880 @Override
881 protected void onPostExecute(Result result) {
882 // TODO: handle errors?
883 }
884 }.run(mResolver, authority, ops);
Andy Huang839ada22012-07-20 15:48:40 -0700885 }
Andy Huang839ada22012-07-20 15:48:40 -0700886 }
887
888 @Override
Vikram Aggarwal66bc2aa2012-08-02 10:47:03 -0700889 public void markConversationsRead(Collection<Conversation> targets, boolean read,
890 boolean viewed) {
891 // We want to show the next conversation if we are marking unread.
892 markConversationsRead(targets, read, viewed, true);
Andy Huang8f6b0062012-07-31 15:36:31 -0700893 }
894
895 private void markConversationsRead(Collection<Conversation> targets, boolean read,
Vikram Aggarwal66bc2aa2012-08-02 10:47:03 -0700896 boolean markViewed, boolean showNext) {
Andy Huang8f6b0062012-07-31 15:36:31 -0700897 // auto-advance if requested and the current conversation is being marked unread
898 if (showNext && !read) {
899 showNextConversation(targets);
900 }
901
Andy Huang839ada22012-07-20 15:48:40 -0700902 for (Conversation target : targets) {
Vikram Aggarwal192fac12012-07-25 16:44:55 -0700903 final ContentValues values = new ContentValues();
Andy Huang839ada22012-07-20 15:48:40 -0700904 values.put(ConversationColumns.READ, read);
Vikram Aggarwal66bc2aa2012-08-02 10:47:03 -0700905 if (markViewed) {
906 values.put(ConversationColumns.VIEWED, true);
907 }
Vikram Aggarwal192fac12012-07-25 16:44:55 -0700908 final ConversationInfo info = target.conversationInfo;
Andy Huang839ada22012-07-20 15:48:40 -0700909 if (info != null) {
Mindy Pereira5f424372012-07-30 11:49:55 -0700910 info.markRead(read);
911 values.put(ConversationColumns.CONVERSATION_INFO, ConversationInfo.toString(info));
Andy Huang839ada22012-07-20 15:48:40 -0700912 }
913 updateConversation(Conversation.listOf(target), values);
914 }
915 // Update the conversations in the selection too.
916 for (final Conversation c : targets) {
917 c.read = read;
Andy Huangcd5c5ee2012-08-12 19:03:51 -0700918 if (markViewed) {
919 c.markViewed();
920 }
Andy Huang839ada22012-07-20 15:48:40 -0700921 }
922 }
923
Andy Huang8f6b0062012-07-31 15:36:31 -0700924 /**
925 * Auto-advance to a different conversation if the currently visible conversation in
926 * conversation mode is affected (deleted, marked unread, etc.).
927 *
928 * <p>Does nothing if outside of conversation mode.
929 *
930 * @param target the set of conversations being deleted/marked unread
931 */
932 private void showNextConversation(Collection<Conversation> target) {
933 final boolean currentConversationInView = (mViewMode.getMode() == ViewMode.CONVERSATION)
934 && Conversation.contains(target, mCurrentConversation);
935 if (currentConversationInView) {
936 final Conversation next = mTracker.getNextConversation(
937 Settings.getAutoAdvanceSetting(mAccount.settings), target);
938 LogUtils.d(LOG_TAG, "showNextConversation: showing %s next.", next);
939 showConversation(next);
940 }
941 }
942
Andy Huang839ada22012-07-20 15:48:40 -0700943 @Override
944 public void starMessage(ConversationMessage msg, boolean starred) {
945 if (msg.starred == starred) {
946 return;
947 }
948
949 msg.starred = starred;
950
951 // locally propagate the change to the owning conversation
952 // (figure the provider will properly propagate the change when it commits it)
953 //
954 // when unstarring, only propagate the change if this was the only message starred
955 final boolean conversationStarred = starred || msg.isConversationStarred();
956 if (conversationStarred != msg.conversation.starred) {
957 msg.conversation.starred = conversationStarred;
Andy Huangdaa06ab2012-07-24 10:46:44 -0700958 mConversationListCursor.setConversationColumn(msg.conversation.uri,
Andy Huang839ada22012-07-20 15:48:40 -0700959 ConversationColumns.STARRED, conversationStarred);
960 }
961
962 final ContentValues values = new ContentValues(1);
963 values.put(UIProvider.MessageColumns.STARRED, starred ? 1 : 0);
964
965 new ContentProviderTask.UpdateTask() {
966 @Override
967 protected void onPostExecute(Result result) {
968 // TODO: handle errors?
969 }
970 }.run(mResolver, msg.uri, values, null /* selection*/, null /* selectionArgs */);
971 }
972
Mindy Pereira28e0c342012-02-17 15:05:13 -0800973 private void requestFolderRefresh() {
974 if (mFolder != null) {
Mindy Pereirab7b33e02012-02-21 15:32:19 -0800975 if (mAsyncRefreshTask != null) {
976 mAsyncRefreshTask.cancel(true);
977 }
Paul Westbrook7e2a2a12012-06-27 13:52:40 -0700978 mAsyncRefreshTask = new AsyncRefreshTask(mContext, mFolder.refreshUri);
Mindy Pereirab7b33e02012-02-21 15:32:19 -0800979 mAsyncRefreshTask.execute();
Mindy Pereira28e0c342012-02-17 15:05:13 -0800980 }
981 }
982
Mindy Pereirafbe40192012-03-20 10:40:45 -0700983 /**
984 * Confirm (based on user's settings) and delete a conversation from the conversation list and
985 * from the database.
Vikram Aggarwal3d7ca9d2012-05-11 14:40:36 -0700986 * @param target the conversations to act upon
987 * @param showDialog true if a confirmation dialog is to be shown, false otherwise.
988 * @param confirmResource the resource ID of the string that is shown in the confirmation dialog
989 * @param action the action to perform after animating the deletion of the conversations.
Mindy Pereirafbe40192012-03-20 10:40:45 -0700990 */
Vikram Aggarwal7f602f72012-04-30 16:04:06 -0700991 protected void confirmAndDelete(final Collection<Conversation> target, boolean showDialog,
992 int confirmResource, final DestructiveAction action) {
Mindy Pereirafbe40192012-03-20 10:40:45 -0700993 if (showDialog) {
994 final AlertDialog.OnClickListener onClick = new AlertDialog.OnClickListener() {
995 @Override
996 public void onClick(DialogInterface dialog, int which) {
Vikram Aggarwal4f4782b2012-05-30 08:39:09 -0700997 delete(target, action);
Mindy Pereirafbe40192012-03-20 10:40:45 -0700998 }
999 };
Vikram Aggarwal3d7ca9d2012-05-11 14:40:36 -07001000 final CharSequence message = Utils.formatPlural(mContext, confirmResource,
1001 target.size());
Mindy Pereirafbe40192012-03-20 10:40:45 -07001002 new AlertDialog.Builder(mActivity.getActivityContext()).setMessage(message)
1003 .setPositiveButton(R.string.ok, onClick)
1004 .setNegativeButton(R.string.cancel, null)
1005 .create().show();
1006 } else {
Vikram Aggarwal4f4782b2012-05-30 08:39:09 -07001007 delete(target, action);
Mindy Pereirafbe40192012-03-20 10:40:45 -07001008 }
1009 }
1010
Vikram Aggarwal4f4782b2012-05-30 08:39:09 -07001011 @Override
1012 public void delete(final Collection<Conversation> target, final DestructiveAction action) {
Vikram Aggarwal192fac12012-07-25 16:44:55 -07001013 // Order of events is critical! The Conversation View Fragment must be notified
1014 // of the next conversation with showConversation(next) *before* the conversation list
1015 // fragment has a chance to delete the conversation, animating it away.
1016
Vikram Aggarwald503df42012-05-11 10:13:35 -07001017 // Update the conversation fragment if the current conversation is deleted.
Andy Huang8f6b0062012-07-31 15:36:31 -07001018 showNextConversation(target);
Vikram Aggarwal192fac12012-07-25 16:44:55 -07001019 // The conversation list deletes and performs the action if it exists.
1020 final ConversationListFragment convListFragment = getConversationListFragment();
1021 if (convListFragment != null) {
1022 LogUtils.d(LOG_TAG, "AAC.requestDelete: ListFragment is handling delete.");
1023 convListFragment.requestDelete(target, action);
1024 return;
1025 }
Vikram Aggarwald503df42012-05-11 10:13:35 -07001026 // No visible UI element handled it on our behalf. Perform the action ourself.
1027 action.performAction();
1028 }
1029
1030 /**
1031 * Requests that the action be performed and the UI state is updated to reflect the new change.
1032 * @param target
1033 * @param action
1034 */
Vikram Aggarwal4f4782b2012-05-30 08:39:09 -07001035 private void requestUpdate(final Collection<Conversation> target,
Vikram Aggarwald503df42012-05-11 10:13:35 -07001036 final DestructiveAction action) {
1037 action.performAction();
1038 refreshConversationList();
Vikram Aggarwal75daee52012-04-30 11:13:09 -07001039 }
Vikram Aggarwal6fbc87a2012-03-15 15:24:00 -07001040
1041 @Override
Vikram Aggarwala55b36c2012-01-13 11:45:02 -08001042 public void onPrepareDialog(int id, Dialog dialog, Bundle bundle) {
1043 // TODO(viki): Auto-generated method stub
Vikram Aggarwala55b36c2012-01-13 11:45:02 -08001044 }
1045
1046 @Override
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -08001047 public boolean onPrepareOptionsMenu(Menu menu) {
Mindy Pereirab849dfb2012-03-07 18:13:15 -08001048 mActionBarView.onPrepareOptionsMenu(menu);
Mindy Pereira363451a2012-02-22 14:14:46 -08001049 return true;
Vikram Aggarwala55b36c2012-01-13 11:45:02 -08001050 }
1051
Mindy Pereira68f2e222012-03-07 10:36:54 -08001052 @Override
1053 public void onPause() {
1054 isLoaderInitialized = false;
Paul Westbrook6ead20d2012-03-19 14:48:14 -07001055 enableNotifications();
Paul Westbrook94e440d2012-02-24 11:03:47 -08001056 }
1057
Vikram Aggarwala55b36c2012-01-13 11:45:02 -08001058 @Override
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -08001059 public void onResume() {
Paul Westbrook6ead20d2012-03-19 14:48:14 -07001060 // Register the receiver that will prevent the status receiver from
1061 // displaying its notification icon as long as we're running.
1062 // The SupressNotificationReceiver will block the broadcast if we're looking at the folder
1063 // that the notification was received for.
1064 disableNotifications();
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -08001065 }
1066
1067 @Override
1068 public void onSaveInstanceState(Bundle outState) {
Vikram Aggarwal6c511582012-02-27 10:59:47 -08001069 if (mAccount != null) {
1070 LogUtils.d(LOG_TAG, "Saving the account now");
1071 outState.putParcelable(SAVED_ACCOUNT, mAccount);
1072 }
Mindy Pereira5e478d22012-03-26 18:04:58 -07001073 if (mFolder != null) {
1074 outState.putParcelable(SAVED_FOLDER, mFolder);
Vikram Aggarwal8b152632012-02-03 14:58:45 -08001075 }
Vikram Aggarwalf3341402012-08-07 10:09:38 -07001076 // If this is a search activity, let's store the search query term as well.
1077 if (ConversationListContext.isSearchResult(mConvListContext)) {
1078 outState.putString(SAVED_QUERY, mConvListContext.searchQuery);
1079 }
Mindy Pereira0f7ae7a2012-07-17 13:39:56 -07001080 int mode = mViewMode.getMode();
Mindy Pereira9fa43ca2012-05-17 14:18:01 -07001081 if (mCurrentConversation != null
Mindy Pereira0f7ae7a2012-07-17 13:39:56 -07001082 && (mode == ViewMode.CONVERSATION ||
Mindy Pereira9fa43ca2012-05-17 14:18:01 -07001083 mViewMode.getMode() == ViewMode.SEARCH_RESULTS_CONVERSATION)) {
Mindy Pereira26f23fc2012-03-27 10:26:04 -07001084 outState.putParcelable(SAVED_CONVERSATION, mCurrentConversation);
1085 }
Andy Huang4556a442012-03-30 16:42:05 -07001086 if (!mSelectedSet.isEmpty()) {
Vikram Aggarwalcabd3f22012-04-19 10:14:41 -07001087 outState.putParcelable(SAVED_SELECTED_SET, mSelectedSet);
Andy Huang4556a442012-03-30 16:42:05 -07001088 }
Mindy Pereirad33674992012-06-25 16:26:30 -07001089 if (mToastBar.getVisibility() == View.VISIBLE) {
1090 outState.putParcelable(SAVED_TOAST_BAR_OP, mToastBar.getOperation());
1091 }
1092 ConversationListFragment convListFragment = getConversationListFragment();
1093 if (convListFragment != null) {
Andy Huang839ada22012-07-20 15:48:40 -07001094 convListFragment.getAnimatedAdapter()
Mindy Pereirad33674992012-06-25 16:26:30 -07001095 .onSaveInstanceState(outState);
1096 }
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -08001097 }
1098
1099 @Override
Mindy Pereira68f2e222012-03-07 10:36:54 -08001100 public void onSearchRequested(String query) {
1101 Intent intent = new Intent();
1102 intent.setAction(Intent.ACTION_SEARCH);
1103 intent.putExtra(ConversationListContext.EXTRA_SEARCH_QUERY, query);
1104 intent.putExtra(Utils.EXTRA_ACCOUNT, mAccount);
1105 intent.setComponent(mActivity.getComponentName());
Vikram Aggarwalb17cbc02012-04-06 15:41:46 -07001106 mActionBarView.collapseSearch();
Mindy Pereira68f2e222012-03-07 10:36:54 -08001107 mActivity.startActivity(intent);
Vikram Aggarwala55b36c2012-01-13 11:45:02 -08001108 }
1109
1110 @Override
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -08001111 public void onStop() {
Mindy Pereira49e5dbe2012-07-12 11:47:54 -07001112 if (mEnableShareIntents != null) {
1113 mEnableShareIntents.cancel(true);
1114 }
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -08001115 }
1116
Andy Huang632721e2012-04-11 16:57:26 -07001117 @Override
1118 public void onDestroy() {
1119 // unregister the ViewPager's observer on the conversation cursor
1120 mPagerController.onDestroy();
Mindy Pereira641de652012-08-02 15:21:50 -07001121 mActionBarView.onDestroy();
Vikram Aggarwal7c401b72012-08-13 16:43:47 -07001122 mRecentFolderList.destroy();
Andy Huang4e0158f2012-08-07 21:06:01 -07001123 mDestroyed = true;
Andy Huang632721e2012-04-11 16:57:26 -07001124 }
1125
Vikram Aggarwalfa131a22012-02-02 13:56:22 -08001126 /**
Mindy Pereira161f50d2012-02-28 15:47:19 -08001127 * {@inheritDoc} Subclasses must override this to listen to mode changes
1128 * from the ViewMode. Subclasses <b>must</b> call the parent's
1129 * onViewModeChanged since the parent will handle common state changes.
Vikram Aggarwalfa131a22012-02-02 13:56:22 -08001130 */
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -08001131 @Override
Vikram Aggarwalfa131a22012-02-02 13:56:22 -08001132 public void onViewModeChanged(int newMode) {
1133 // Perform any mode specific work here.
Mindy Pereira161f50d2012-02-28 15:47:19 -08001134 // reset the action bar icon based on the mode. Why don't the individual
1135 // controllers do
Vikram Aggarwalfa131a22012-02-02 13:56:22 -08001136 // this themselves?
Vikram Aggarwala55b36c2012-01-13 11:45:02 -08001137
Mindy Pereira8937bf12012-07-23 14:05:02 -07001138 // Commit any destructive undoable actions the user may have performed.
mindypc6adce32012-08-22 18:46:42 -07001139 commitDestructiveActions(true);
Mindy Pereira8937bf12012-07-23 14:05:02 -07001140
Mindy Pereira161f50d2012-02-28 15:47:19 -08001141 // We don't want to invalidate the options menu when switching to
1142 // conversation
Vikram Aggarwalfa131a22012-02-02 13:56:22 -08001143 // mode, as it will happen when the conversation finishes loading.
1144 if (newMode != ViewMode.CONVERSATION) {
1145 mActivity.invalidateOptionsMenu();
1146 }
Vikram Aggarwala55b36c2012-01-13 11:45:02 -08001147 }
1148
Andy Huang4e0158f2012-08-07 21:06:01 -07001149 public boolean isDestroyed() {
1150 return mDestroyed;
1151 }
1152
mindypc6adce32012-08-22 18:46:42 -07001153 private void commitDestructiveActions(boolean animate) {
1154 ConversationListFragment fragment = getConversationListFragment();
Mindy Pereira1e2573b2012-04-17 14:34:36 -07001155 if (fragment != null) {
mindypc6adce32012-08-22 18:46:42 -07001156 fragment.commitDestructiveActions(animate);
Mindy Pereira1e2573b2012-04-17 14:34:36 -07001157 }
1158 }
1159
Vikram Aggarwala55b36c2012-01-13 11:45:02 -08001160 @Override
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -08001161 public void onWindowFocusChanged(boolean hasFocus) {
Paul Westbrook9f119c72012-04-24 16:10:59 -07001162 ConversationListFragment convList = getConversationListFragment();
1163 if (hasFocus && convList != null && convList.isVisible()) {
1164 // The conversation list is visible.
1165 Utils.setConversationCursorVisibility(mConversationListCursor, true);
1166 }
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -08001167 }
1168
Vikram Aggarwalca716f12012-08-20 11:11:48 -07001169 /**
1170 * Set the account, and carry out all the account-related changes that rely on this.
1171 * @param account
1172 */
1173 // TODO(viki): Two different methods do the same thing. Resolve
1174 // {@link #setAccount(Account)} and {@link #switchAccount(Account, boolean)}
Mindy Pereira75181e82012-04-18 08:17:13 -07001175 private void setAccount(Account account) {
Andy Huangb9ca9792012-05-18 15:31:49 -07001176 if (account == null) {
1177 LogUtils.w(LOG_TAG, new Error(),
1178 "AAC ignoring null (presumably invalid) account restoration");
1179 return;
1180 }
Andy Huangb1148412012-05-19 00:16:30 -07001181 LogUtils.d(LOG_TAG, "AbstractActivityController.setAccount(): account = %s", account.uri);
Vikram Aggarwal91e87372012-05-18 15:36:04 -07001182 mAccount = account;
Vikram Aggarwalca716f12012-08-20 11:11:48 -07001183 // Only change AAC state here. Do *not* modify any other object's state. The object
1184 // should listen on account changes.
1185 restartOptionalLoader(LOADER_RECENT_FOLDERS);
1186 mActivity.invalidateOptionsMenu();
1187 disableNotificationsOnAccountChange(mAccount);
1188 restartOptionalLoader(LOADER_ACCOUNT_UPDATE_CURSOR);
1189 MailAppProvider.getInstance().setLastViewedAccount(mAccount.uri.toString());
1190
Vikram Aggarwal91e87372012-05-18 15:36:04 -07001191 if (account.settings == null) {
1192 LogUtils.w(LOG_TAG, new Error(), "AAC ignoring account with null settings.");
1193 return;
1194 }
Vikram Aggarwal7c401b72012-08-13 16:43:47 -07001195 mAccountObservers.notifyChanged();
Mindy Pereira75181e82012-04-18 08:17:13 -07001196 }
1197
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -08001198 /**
Mindy Pereira161f50d2012-02-28 15:47:19 -08001199 * Restore the state from the previous bundle. Subclasses should call this
1200 * method from the parent class, since it performs important UI
1201 * initialization.
1202 *
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -08001203 * @param savedState
1204 */
Andy Huang632721e2012-04-11 16:57:26 -07001205 @Override
1206 public void onRestoreInstanceState(Bundle savedState) {
1207 LogUtils.d(LOG_TAG, "IN AAC.onRestoreInstanceState");
1208 if (savedState.containsKey(SAVED_CONVERSATION)) {
1209 // Open the conversation.
Paul Westbrook534e4a22012-04-25 03:46:29 -07001210 final Conversation conversation =
1211 (Conversation)savedState.getParcelable(SAVED_CONVERSATION);
1212 if (conversation != null && conversation.position < 0) {
1213 // Set the position to 0 on this conversation, as we don't know where it is
1214 // in the list
1215 conversation.position = 0;
1216 }
Andy Huanged4fdf02012-07-26 17:12:50 -07001217 showConversation(conversation);
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -08001218 }
Mindy Pereira967ede62012-03-22 09:29:09 -07001219
Mindy Pereirad33674992012-06-25 16:26:30 -07001220 if (savedState.containsKey(SAVED_TOAST_BAR_OP)) {
1221 ToastBarOperation op = ((ToastBarOperation) savedState
1222 .getParcelable(SAVED_TOAST_BAR_OP));
1223 if (op != null) {
1224 if (op.getType() == ToastBarOperation.UNDO) {
1225 onUndoAvailable(op);
Andrew Sapperstein9d7519d2012-07-16 14:03:53 -07001226 } else if (op.getType() == ToastBarOperation.ERROR) {
1227 onError(mFolder, true);
Mindy Pereirad33674992012-06-25 16:26:30 -07001228 }
1229 }
1230 }
Mindy Pereira0f7ae7a2012-07-17 13:39:56 -07001231
Mindy Pereirad33674992012-06-25 16:26:30 -07001232 ConversationListFragment convListFragment = getConversationListFragment();
1233 if (convListFragment != null) {
Andy Huang839ada22012-07-20 15:48:40 -07001234 convListFragment.getAnimatedAdapter()
Mindy Pereirad33674992012-06-25 16:26:30 -07001235 .onRestoreInstanceState(savedState);
1236 }
Mindy Pereira967ede62012-03-22 09:29:09 -07001237 /**
1238 * Restore the state of selected conversations. This needs to be done after the correct mode
1239 * is set and the action bar is fully initialized. If not, several key pieces of state
1240 * information will be missing, and the split views may not be initialized correctly.
1241 * @param savedState
1242 */
Andy Huang4556a442012-03-30 16:42:05 -07001243 restoreSelectedConversations(savedState);
Andy Huang632721e2012-04-11 16:57:26 -07001244 }
1245
1246 private void handleIntent(Intent intent) {
1247 boolean handled = false;
1248 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1249 if (intent.hasExtra(Utils.EXTRA_ACCOUNT)) {
Andy Huang632721e2012-04-11 16:57:26 -07001250 setAccount(Account.newinstance(intent
Mindy Pereira5ad02912012-07-09 09:57:18 -07001251 .getStringExtra(Utils.EXTRA_ACCOUNT)));
Andy Huang632721e2012-04-11 16:57:26 -07001252 }
Andy Huangb9ca9792012-05-18 15:31:49 -07001253 if (mAccount == null) {
1254 return;
Andy Huang632721e2012-04-11 16:57:26 -07001255 }
Andy Huangb9ca9792012-05-18 15:31:49 -07001256 mActivity.invalidateOptionsMenu();
Vikram Aggarwal1a249e02012-08-03 16:19:33 -07001257 final boolean isConversationMode = intent.hasExtra(Utils.EXTRA_CONVERSATION);
1258 // TODO(viki): Allow the controller to set the mode instead of a mode transition.
1259 if (isConversationMode) {
1260 mViewMode.enterConversationMode();
1261 } else {
1262 mViewMode.enterConversationListMode();
1263 }
Andy Huang632721e2012-04-11 16:57:26 -07001264
1265 Folder folder = null;
1266 if (intent.hasExtra(Utils.EXTRA_FOLDER)) {
1267 // Open the folder.
Mindy Pereira7b6d03d2012-07-30 13:03:41 -07001268 folder = Folder.fromString(intent.getStringExtra(Utils.EXTRA_FOLDER));
Andy Huang632721e2012-04-11 16:57:26 -07001269 }
1270 if (folder != null) {
1271 onFolderChanged(folder);
1272 handled = true;
1273 }
1274
Vikram Aggarwal1a249e02012-08-03 16:19:33 -07001275 if (isConversationMode) {
Andy Huang632721e2012-04-11 16:57:26 -07001276 // Open the conversation.
1277 LogUtils.d(LOG_TAG, "SHOW THE CONVERSATION at %s",
1278 intent.getParcelableExtra(Utils.EXTRA_CONVERSATION));
Paul Westbrooka9161912012-04-24 10:10:04 -07001279 final Conversation conversation =
1280 (Conversation)intent.getParcelableExtra(Utils.EXTRA_CONVERSATION);
1281 if (conversation != null && conversation.position < 0) {
1282 // Set the position to 0 on this conversation, as we don't know where it is
1283 // in the list
1284 conversation.position = 0;
1285 }
Andy Huang980aaea2012-07-26 17:22:19 -07001286 showConversation(conversation);
Andy Huang632721e2012-04-11 16:57:26 -07001287 handled = true;
1288 }
1289
1290 if (!handled) {
1291 // Nothing was saved; just load the account inbox.
1292 loadAccountInbox();
1293 }
1294 } else if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
1295 if (intent.hasExtra(Utils.EXTRA_ACCOUNT)) {
1296 // Save this search query for future suggestions.
1297 final String query = intent.getStringExtra(SearchManager.QUERY);
1298 final String authority = mContext.getString(R.string.suggestions_authority);
1299 SearchRecentSuggestions suggestions = new SearchRecentSuggestions(
1300 mContext, authority, SuggestionsProvider.MODE);
1301 suggestions.saveRecentQuery(query, null);
Mindy Pereiraac254822012-06-18 10:46:43 -07001302 if (Utils.showTwoPaneSearchResults(mActivity.getActivityContext())) {
1303 mViewMode.enterSearchResultsConversationMode();
1304 } else {
1305 mViewMode.enterSearchResultsListMode();
1306 }
Andy Huang632721e2012-04-11 16:57:26 -07001307 setAccount((Account) intent.getParcelableExtra(Utils.EXTRA_ACCOUNT));
1308 mActivity.invalidateOptionsMenu();
1309 restartOptionalLoader(LOADER_RECENT_FOLDERS);
Andy Huang632721e2012-04-11 16:57:26 -07001310 fetchSearchFolder(intent);
1311 } else {
1312 LogUtils.e(LOG_TAG, "Missing account extra from search intent. Finishing");
1313 mActivity.finish();
1314 }
1315 }
1316 if (mAccount != null) {
1317 restartOptionalLoader(LOADER_ACCOUNT_UPDATE_CURSOR);
1318 }
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -08001319 }
1320
Andy Huang4556a442012-03-30 16:42:05 -07001321 /**
1322 * Copy any selected conversations stored in the saved bundle into our selection set,
1323 * triggering {@link ConversationSetObserver} callbacks as our selection set changes.
1324 *
1325 */
Vikram Aggarwal4eb52712012-06-19 16:24:50 -07001326 private final void restoreSelectedConversations(Bundle savedState) {
Mindy Pereira967ede62012-03-22 09:29:09 -07001327 if (savedState == null) {
Andy Huang4556a442012-03-30 16:42:05 -07001328 mSelectedSet.clear();
Mindy Pereira967ede62012-03-22 09:29:09 -07001329 return;
1330 }
Vikram Aggarwalcabd3f22012-04-19 10:14:41 -07001331 final ConversationSelectionSet selectedSet = savedState.getParcelable(SAVED_SELECTED_SET);
Andy Huang4556a442012-03-30 16:42:05 -07001332 if (selectedSet == null || selectedSet.isEmpty()) {
1333 mSelectedSet.clear();
Mindy Pereira967ede62012-03-22 09:29:09 -07001334 return;
1335 }
Andy Huang632721e2012-04-11 16:57:26 -07001336
1337 // putAll will take care of calling our registered onSetPopulated method
Vikram Aggarwalcabd3f22012-04-19 10:14:41 -07001338 mSelectedSet.putAll(selectedSet);
Mindy Pereira967ede62012-03-22 09:29:09 -07001339 }
1340
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -08001341 @Override
Andy Huang5895f7b2012-06-01 17:07:20 -07001342 public SubjectDisplayChanger getSubjectDisplayChanger() {
1343 return mActionBarView;
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -08001344 }
1345
Vikram Aggarwalec5cbf72012-03-08 15:10:35 -08001346 /**
1347 * Children can override this method, but they must call super.showConversation().
1348 * {@inheritDoc}
1349 */
1350 @Override
1351 public void showConversation(Conversation conversation) {
Vikram Aggarwalc67182d2012-04-03 14:35:06 -07001352 // Set the current conversation just in case it wasn't already set.
1353 setCurrentConversation(conversation);
Vikram Aggarwalec5cbf72012-03-08 15:10:35 -08001354 }
1355
Vikram Aggarwale128fc22012-04-04 12:33:34 -07001356 /**
Paul Westbrook2d50bcd2012-04-10 11:53:47 -07001357 * Children can override this method, but they must call super.showWaitForInitialization().
1358 * {@inheritDoc}
1359 */
1360 @Override
1361 public void showWaitForInitialization() {
1362 mViewMode.enterWaitingForInitializationMode();
1363 }
1364
1365 @Override
1366 public void hideWaitForInitialization() {
1367 }
1368
1369 @Override
1370 public void updateWaitMode() {
1371 final FragmentManager manager = mActivity.getFragmentManager();
1372 final WaitFragment waitFragment =
Vikram Aggarwal6902dcf2012-04-11 08:57:42 -07001373 (WaitFragment)manager.findFragmentByTag(TAG_WAIT);
Paul Westbrook2d50bcd2012-04-10 11:53:47 -07001374 if (waitFragment != null) {
1375 waitFragment.updateAccount(mAccount);
1376 }
1377 }
1378
Vikram Aggarwal48b2a6c2012-05-29 14:09:27 -07001379 /**
1380 * Returns true if we are waiting for the account to sync, and cannot show any folders or
1381 * conversation for the current account yet.
Andy Huang839ada22012-07-20 15:48:40 -07001382 *
Vikram Aggarwal48b2a6c2012-05-29 14:09:27 -07001383 */
Paul Westbrook2d50bcd2012-04-10 11:53:47 -07001384 public boolean inWaitMode() {
1385 final FragmentManager manager = mActivity.getFragmentManager();
1386 final WaitFragment waitFragment =
Vikram Aggarwal6902dcf2012-04-11 08:57:42 -07001387 (WaitFragment)manager.findFragmentByTag(TAG_WAIT);
Paul Westbrook2d50bcd2012-04-10 11:53:47 -07001388 if (waitFragment != null) {
1389 final Account fragmentAccount = waitFragment.getAccount();
1390 return fragmentAccount.uri.equals(mAccount.uri) &&
1391 mViewMode.getMode() == ViewMode.WAITING_FOR_ACCOUNT_INITIALIZATION;
1392 }
1393 return false;
1394 }
1395
1396 /**
Vikram Aggarwale128fc22012-04-04 12:33:34 -07001397 * Children can override this method, but they must call super.showConversationList().
1398 * {@inheritDoc}
1399 */
1400 @Override
1401 public void showConversationList(ConversationListContext listContext) {
1402 }
1403
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -08001404 @Override
Mindy Pereira9b875682012-02-15 18:10:54 -08001405 public void onConversationSelected(Conversation conversation) {
Vikram Aggarwal5b7b3ab2012-04-03 15:43:55 -07001406 showConversation(conversation);
Mindy Pereira9fa43ca2012-05-17 14:18:01 -07001407 if (Intent.ACTION_SEARCH.equals(mActivity.getIntent().getAction())) {
Mindy Pereira68f2e222012-03-07 10:36:54 -08001408 mViewMode.enterSearchResultsConversationMode();
1409 } else {
1410 mViewMode.enterConversationMode();
1411 }
Vikram Aggarwal7d602882012-02-07 15:01:20 -08001412 }
Mindy Pereira555140c2012-02-15 14:55:29 -08001413
Vikram Aggarwalc67182d2012-04-03 14:35:06 -07001414 /**
1415 * Set the current conversation. This is the conversation on which all actions are performed.
1416 * Do not modify mCurrentConversation except through this method, which makes it easy to
1417 * perform common actions associated with changing the current conversation.
1418 * @param conversation
1419 */
Andy Huang632721e2012-04-11 16:57:26 -07001420 @Override
1421 public void setCurrentConversation(Conversation conversation) {
Mindy Pereira5040f1a2012-03-20 10:14:06 -07001422 mCurrentConversation = conversation;
Vikram Aggarwal135fd022012-04-11 14:44:15 -07001423 mTracker.initialize(mCurrentConversation);
Mindy Pereira5040f1a2012-03-20 10:14:06 -07001424 }
1425
Vikram Aggarwala9b93f32012-02-23 14:51:58 -08001426 /**
1427 * {@inheritDoc}
1428 */
Mindy Pereira555140c2012-02-15 14:55:29 -08001429 @Override
Vikram Aggarwal7dedb952012-02-16 16:10:23 -08001430 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
1431 // Create a loader to listen in on account changes.
Vikram Aggarwalfa4b47e2012-03-09 13:02:46 -08001432 switch (id) {
1433 case LOADER_ACCOUNT_CURSOR:
Paul Westbrookc2074c42012-03-22 15:26:58 -07001434 return new CursorLoader(mContext, MailAppProvider.getAccountsUri(),
Vikram Aggarwalfa4b47e2012-03-09 13:02:46 -08001435 UIProvider.ACCOUNTS_PROJECTION, null, null, null);
1436 case LOADER_FOLDER_CURSOR:
Paul Westbrookc7a070f2012-04-12 01:46:41 -07001437 final CursorLoader loader = new CursorLoader(mContext, mFolder.uri,
1438 UIProvider.FOLDERS_PROJECTION, null, null, null);
1439 loader.setUpdateThrottle(mFolderItemUpdateDelayMs);
1440 return loader;
Vikram Aggarwalfa4b47e2012-03-09 13:02:46 -08001441 case LOADER_RECENT_FOLDERS:
Paul Westbrook91d10502012-04-13 12:01:39 -07001442 if (mAccount != null && mAccount.recentFolderListUri != null) {
Paul Westbrookea4ee202012-03-12 14:12:33 -07001443 return new CursorLoader(mContext, mAccount.recentFolderListUri,
1444 UIProvider.FOLDERS_PROJECTION, null, null, null);
1445 }
1446 break;
Mindy Pereiraab486362012-03-21 18:18:53 -07001447 case LOADER_ACCOUNT_INBOX:
Vikram Aggarwal025eba82012-05-08 10:45:30 -07001448 final Uri defaultInbox = Settings.getDefaultInboxUri(mAccount.settings);
Vikram Aggarwal1e57e672012-05-07 14:48:24 -07001449 final Uri inboxUri = defaultInbox.equals(Uri.EMPTY) ?
1450 mAccount.folderListUri : defaultInbox;
Paul Westbrook7496e822012-04-24 09:50:54 -07001451 LogUtils.d(LOG_TAG, "Loading the default inbox: %s", inboxUri);
Paul Westbrook1220b6d2012-04-10 00:48:00 -07001452 if (inboxUri != null) {
1453 return new CursorLoader(mContext, inboxUri, UIProvider.FOLDERS_PROJECTION, null,
1454 null, null);
1455 }
1456 break;
Mindy Pereiraab486362012-03-21 18:18:53 -07001457 case LOADER_SEARCH:
1458 return Folder.forSearchResults(mAccount,
1459 args.getString(ConversationListContext.EXTRA_SEARCH_QUERY),
1460 mActivity.getActivityContext());
Paul Westbrook2d50bcd2012-04-10 11:53:47 -07001461 case LOADER_ACCOUNT_UPDATE_CURSOR:
1462 return new CursorLoader(mContext, mAccount.uri, UIProvider.ACCOUNTS_PROJECTION,
1463 null, null, null);
Vikram Aggarwalfa4b47e2012-03-09 13:02:46 -08001464 default:
Paul Westbrookad6a2752012-04-04 16:58:13 -07001465 LogUtils.wtf(LOG_TAG, "Loader returned unexpected id: %d", id);
Mindy Pereira5cb0c3e2012-02-22 15:22:47 -08001466 }
1467 return null;
Vikram Aggarwal7dedb952012-02-16 16:10:23 -08001468 }
1469
Paul Westbrookb1f573c2012-04-06 11:38:28 -07001470 @Override
1471 public void onLoaderReset(Loader<Cursor> loader) {
1472
1473 }
1474
Andy Huangf9a73482012-03-13 15:54:02 -07001475 /**
1476 * {@link LoaderManager} currently has a bug in
1477 * {@link LoaderManager#restartLoader(int, Bundle, android.app.LoaderManager.LoaderCallbacks)}
1478 * where, if a previous onCreateLoader returned a null loader, this method will NPE. Work around
1479 * this bug by destroying any loaders that may have been created as null (essentially because
1480 * they are optional loads, and may not apply to a particular account).
1481 * <p>
1482 * A simple null check before restarting a loader will not work, because that would not
1483 * give the controller a chance to invalidate UI corresponding the prior loader result.
1484 *
1485 * @param id loader ID to safely restart
Andy Huangf9a73482012-03-13 15:54:02 -07001486 */
Vikram Aggarwal94c94de2012-04-04 15:38:28 -07001487 private void restartOptionalLoader(int id) {
Andy Huangf9a73482012-03-13 15:54:02 -07001488 final LoaderManager lm = mActivity.getLoaderManager();
1489 lm.destroyLoader(id);
Vikram Aggarwal94c94de2012-04-04 15:38:28 -07001490 lm.restartLoader(id, Bundle.EMPTY, this);
1491 }
1492
Andy Huang632721e2012-04-11 16:57:26 -07001493 @Override
1494 public void registerConversationListObserver(DataSetObserver observer) {
1495 mConversationListObservable.registerObserver(observer);
1496 }
1497
1498 @Override
1499 public void unregisterConversationListObserver(DataSetObserver observer) {
1500 mConversationListObservable.unregisterObserver(observer);
1501 }
1502
Andy Huang090db1e2012-07-25 13:25:28 -07001503 @Override
1504 public void registerFolderObserver(DataSetObserver observer) {
1505 mFolderObservable.registerObserver(observer);
1506 }
1507
1508 @Override
1509 public void unregisterFolderObserver(DataSetObserver observer) {
1510 mFolderObservable.unregisterObserver(observer);
1511 }
1512
Vikram Aggarwal60069912012-07-24 14:26:09 -07001513 /**
1514 * Returns true if the number of accounts is different, or if the current account has been
1515 * removed from the device
1516 * @param accountCursor
1517 * @return
1518 */
Paul Westbrook23b74b92012-02-29 11:36:12 -08001519 private boolean accountsUpdated(Cursor accountCursor) {
1520 // Check to see if the current account hasn't been set, or the account cursor is empty
1521 if (mAccount == null || !accountCursor.moveToFirst()) {
Vikram Aggarwal6c511582012-02-27 10:59:47 -08001522 return true;
Paul Westbrook23b74b92012-02-29 11:36:12 -08001523 }
1524
1525 // Check to see if the number of accounts are different, from the number we saw on the last
1526 // updated
1527 if (mCurrentAccountUris.size() != accountCursor.getCount()) {
1528 return true;
1529 }
1530
1531 // Check to see if the account list is different or if the current account is not found in
1532 // the cursor.
1533 boolean foundCurrentAccount = false;
Vikram Aggarwal7dedb952012-02-16 16:10:23 -08001534 do {
Paul Westbrook23b74b92012-02-29 11:36:12 -08001535 final Uri accountUri =
1536 Uri.parse(accountCursor.getString(UIProvider.ACCOUNT_URI_COLUMN));
1537 if (!foundCurrentAccount && mAccount.uri.equals(accountUri)) {
1538 foundCurrentAccount = true;
1539 }
Vikram Aggarwal60069912012-07-24 14:26:09 -07001540 // Is there a new account that we do not know about?
Paul Westbrook23b74b92012-02-29 11:36:12 -08001541 if (!mCurrentAccountUris.contains(accountUri)) {
1542 return true;
1543 }
Vikram Aggarwal7dedb952012-02-16 16:10:23 -08001544 } while (accountCursor.moveToNext());
Paul Westbrook23b74b92012-02-29 11:36:12 -08001545
1546 // As long as we found the current account, the list hasn't been updated
1547 return !foundCurrentAccount;
Vikram Aggarwal7dedb952012-02-16 16:10:23 -08001548 }
1549
1550 /**
Vikram Aggarwal60069912012-07-24 14:26:09 -07001551 * Updates accounts for the app. If the current account is missing, the first
1552 * account in the list is set to the current account (we <em>have</em> to choose something).
Mindy Pereira161f50d2012-02-28 15:47:19 -08001553 *
Vikram Aggarwal6c511582012-02-27 10:59:47 -08001554 * @param accounts cursor into the AccountCache
Vikram Aggarwal7dedb952012-02-16 16:10:23 -08001555 * @return true if the update was successful, false otherwise
1556 */
Vikram Aggarwal60069912012-07-24 14:26:09 -07001557 private boolean updateAccounts(Cursor accounts) {
Vikram Aggarwal7dedb952012-02-16 16:10:23 -08001558 if (accounts == null || !accounts.moveToFirst()) {
1559 return false;
1560 }
Paul Westbrook23b74b92012-02-29 11:36:12 -08001561
Vikram Aggarwala9b93f32012-02-23 14:51:58 -08001562 final Account[] allAccounts = Account.getAllAccounts(accounts);
Vikram Aggarwal60069912012-07-24 14:26:09 -07001563 // A match for the current account's URI in the list of accounts.
1564 Account currentFromList = null;
Paul Westbrook23b74b92012-02-29 11:36:12 -08001565
1566 // Save the uris for the accounts
1567 mCurrentAccountUris.clear();
1568 for (Account account : allAccounts) {
Vikram Aggarwal60069912012-07-24 14:26:09 -07001569 LogUtils.d(LOG_TAG, "updateAccounts(%s)", account);
Paul Westbrook23b74b92012-02-29 11:36:12 -08001570 mCurrentAccountUris.add(account.uri);
Vikram Aggarwal60069912012-07-24 14:26:09 -07001571 if (mAccount != null && account.uri.equals(mAccount.uri)) {
1572 currentFromList = account;
1573 }
Paul Westbrook23b74b92012-02-29 11:36:12 -08001574 }
1575
Vikram Aggarwal60069912012-07-24 14:26:09 -07001576 // 1. current account is already set and is in allAccounts:
1577 // 1a. It has changed -> load the updated account.
1578 // 2b. It is unchanged -> no-op
Andy Huang0d647352012-03-21 21:48:16 -07001579 // 2. current account is set and is not in allAccounts -> pick first (acct was deleted?)
Vikram Aggarwal60069912012-07-24 14:26:09 -07001580 // 3. saved preference has an account -> pick that one
Andy Huang0d647352012-03-21 21:48:16 -07001581 // 4. otherwise just pick first
1582
Vikram Aggarwal60069912012-07-24 14:26:09 -07001583 boolean accountChanged = false;
1584 /// Assume case 4, initialize to first account, and see if we can find anything better.
1585 Account newAccount = allAccounts[0];
1586 if (currentFromList != null) {
1587 // Case 1: Current account exists but has changed
1588 if (!currentFromList.equals(mAccount)) {
1589 newAccount = currentFromList;
1590 accountChanged = true;
Andy Huang0d647352012-03-21 21:48:16 -07001591 }
Vikram Aggarwal60069912012-07-24 14:26:09 -07001592 // Case 1b: else, current account is unchanged: nothing to do.
Paul Westbrook23b74b92012-02-29 11:36:12 -08001593 } else {
Vikram Aggarwal60069912012-07-24 14:26:09 -07001594 // Case 2: Current account is not in allAccounts, the account needs to change.
1595 accountChanged = true;
1596 if (mAccount == null) {
1597 // Case 3: Check for last viewed account, and check if it exists in the list.
1598 final String lastAccountUri = MailAppProvider.getInstance().getLastViewedAccount();
1599 if (lastAccountUri != null) {
1600 for (final Account account : allAccounts) {
1601 if (lastAccountUri.equals(account.uri.toString())) {
1602 newAccount = account;
1603 break;
1604 }
Andy Huang0d647352012-03-21 21:48:16 -07001605 }
1606 }
1607 }
Paul Westbrook23b74b92012-02-29 11:36:12 -08001608 }
Vikram Aggarwal60069912012-07-24 14:26:09 -07001609 if (accountChanged) {
1610 onAccountChanged(newAccount);
1611 }
1612 // Whether we have updated the current account or not, we need to update the list of
1613 // accounts in the ActionBar.
Vikram Aggarwala9b93f32012-02-23 14:51:58 -08001614 mActionBarView.setAccounts(allAccounts);
1615 return (allAccounts.length > 0);
Vikram Aggarwal7dedb952012-02-16 16:10:23 -08001616 }
1617
Paul Westbrook6ead20d2012-03-19 14:48:14 -07001618 private void disableNotifications() {
1619 mNewEmailReceiver.activate(mContext, this);
1620 }
1621
1622 private void enableNotifications() {
1623 mNewEmailReceiver.deactivate();
1624 }
1625
1626 private void disableNotificationsOnAccountChange(Account account) {
1627 // If the new mail suppression receiver is activated for a different account, we want to
1628 // activate it for the new account.
1629 if (mNewEmailReceiver.activated() &&
1630 !mNewEmailReceiver.notificationsDisabledForAccount(account)) {
1631 // Deactivate the current receiver, otherwise multiple receivers may be registered.
1632 mNewEmailReceiver.deactivate();
1633 mNewEmailReceiver.activate(mContext, this);
1634 }
1635 }
1636
Vikram Aggarwala9b93f32012-02-23 14:51:58 -08001637 /**
1638 * {@inheritDoc}
1639 */
Vikram Aggarwal7dedb952012-02-16 16:10:23 -08001640 @Override
1641 public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
Mindy Pereira5cb0c3e2012-02-22 15:22:47 -08001642 // We want to reinitialize only if we haven't ever been initialized, or
1643 // if the current account has vanished.
Paul Westbrooke3e84292012-03-05 16:19:30 -08001644 if (data == null) {
Vikram Aggarwalfa4b47e2012-03-09 13:02:46 -08001645 LogUtils.e(LOG_TAG, "Received null cursor from loader id: %d", loader.getId());
Paul Westbrooke3e84292012-03-05 16:19:30 -08001646 }
Vikram Aggarwalfa4b47e2012-03-09 13:02:46 -08001647 switch (loader.getId()) {
1648 case LOADER_ACCOUNT_CURSOR:
Vikram Aggarwal27d89ad2012-06-12 13:38:40 -07001649 // If the account list is not null, and the account list cursor is empty,
Paul Westbrook2388c5d2012-03-25 12:29:11 -07001650 // we need to start the specified activity.
1651 if (data != null && data.getCount() == 0) {
1652 // If an empty cursor is returned, the MailAppProvider is indicating that
1653 // no accounts have been specified. We want to navigate to the "add account"
1654 // activity that will handle the intent returned by the MailAppProvider
1655
1656 // If the MailAppProvider believes that all accounts have been loaded, and the
1657 // account list is still empty, we want to prompt the user to add an account
1658 final Bundle extras = data.getExtras();
1659 final boolean accountsLoaded =
1660 extras.getInt(AccountCursorExtraKeys.ACCOUNTS_LOADED) != 0;
1661
1662 if (accountsLoaded) {
1663 final Intent noAccountIntent = MailAppProvider.getNoAccountIntent(mContext);
1664 if (noAccountIntent != null) {
1665 mActivity.startActivityForResult(noAccountIntent,
1666 ADD_ACCOUNT_REQUEST_CODE);
1667 }
1668 }
1669 } else {
1670 final boolean accountListUpdated = accountsUpdated(data);
1671 if (!isLoaderInitialized || accountListUpdated) {
Vikram Aggarwal60069912012-07-24 14:26:09 -07001672 isLoaderInitialized = updateAccounts(data);
Paul Westbrook2388c5d2012-03-25 12:29:11 -07001673 }
Vikram Aggarwalfa4b47e2012-03-09 13:02:46 -08001674 }
1675 break;
Paul Westbrook2d50bcd2012-04-10 11:53:47 -07001676 case LOADER_ACCOUNT_UPDATE_CURSOR:
1677 // We have gotten an update for current account.
1678
Vikram Aggarwal60069912012-07-24 14:26:09 -07001679 // Make sure that this is an update for the current account
Paul Westbrook2d50bcd2012-04-10 11:53:47 -07001680 if (data != null && data.moveToFirst()) {
1681 final Account updatedAccount = new Account(data);
1682
1683 if (updatedAccount.uri.equals(mAccount.uri)) {
Paul Westbrookca08fc12012-07-31 12:01:15 -07001684 // Keep a reference to the previous settings object
1685 final Settings previousSettings = mAccount.settings;
1686
Vikram Aggarwal7d816002012-04-17 17:06:41 -07001687 // Update the controller's reference to the current account
Paul Westbrook2d50bcd2012-04-10 11:53:47 -07001688 mAccount = updatedAccount;
Vikram Aggarwaledc137c2012-04-24 13:40:58 -07001689 LogUtils.d(LOG_TAG, "AbstractActivityController.onLoadFinished(): "
1690 + "mAccount = %s", mAccount.uri);
Paul Westbrookca08fc12012-07-31 12:01:15 -07001691
1692 // Only notify about a settings change if something differs
1693 if (!Objects.equal(mAccount.settings, previousSettings)) {
Vikram Aggarwal7c401b72012-08-13 16:43:47 -07001694 mAccountObservers.notifyChanged();
Paul Westbrookca08fc12012-07-31 12:01:15 -07001695 }
Paul Westbrook2d50bcd2012-04-10 11:53:47 -07001696
1697 // Got an update for the current account
1698 final boolean inWaitingMode = inWaitMode();
1699 if (!updatedAccount.isAccountIntialized() && !inWaitingMode) {
1700 // Transition to waiting mode
1701 showWaitForInitialization();
Mindy Pereira830bdaf2012-07-11 12:59:55 -07001702 } else if (updatedAccount.isAccountIntialized()) {
1703 if (inWaitingMode) {
1704 // Dismiss waiting mode
1705 hideWaitForInitialization();
1706 }
Paul Westbrook2d50bcd2012-04-10 11:53:47 -07001707 } else if (!updatedAccount.isAccountIntialized() && inWaitingMode) {
1708 // Update the WaitFragment's account object
1709 updateWaitMode();
1710 }
1711 } else {
1712 LogUtils.e(LOG_TAG, "Got update for account: %s with current account: %s",
1713 updatedAccount.uri, mAccount.uri);
1714 // We need to restart the loader, so the correct account information will
1715 // be returned
1716 restartOptionalLoader(LOADER_ACCOUNT_UPDATE_CURSOR);
1717 }
1718 }
1719 break;
Vikram Aggarwalfa4b47e2012-03-09 13:02:46 -08001720 case LOADER_FOLDER_CURSOR:
Vikram Aggarwalfa4b47e2012-03-09 13:02:46 -08001721 // Check status of the cursor.
Marc Blankfd9d0b82012-04-23 16:01:51 -07001722 if (data != null && data.moveToFirst()) {
Andy Huang090db1e2012-07-25 13:25:28 -07001723 final Folder folder = new Folder(data);
Marc Blankfd9d0b82012-04-23 16:01:51 -07001724 LogUtils.d(LOG_TAG, "FOLDER STATUS = %d", folder.syncStatus);
Andy Huang090db1e2012-07-25 13:25:28 -07001725
1726 mFolder = folder;
1727 mFolderObservable.notifyChanged();
1728
Paul Westbrookc808fac2012-02-22 16:42:18 -08001729 } else {
Marc Blankfd9d0b82012-04-23 16:01:51 -07001730 LogUtils.d(LOG_TAG, "Unable to get the folder %s",
1731 mFolder != null ? mAccount.name : "");
Mindy Pereira11dd5ef2012-03-10 15:10:18 -08001732 }
Vikram Aggarwalfa4b47e2012-03-09 13:02:46 -08001733 break;
Vikram Aggarwalfa4b47e2012-03-09 13:02:46 -08001734 case LOADER_RECENT_FOLDERS:
Vikram Aggarwal27d89ad2012-06-12 13:38:40 -07001735 // No recent folders and we are running on a phone? Populate the default recents.
1736 if (data != null && data.getCount() == 0 && !Utils.useTabletUI(mContext)) {
1737 final class PopulateDefault extends AsyncTask<Uri, Void, Void> {
1738 @Override
1739 protected Void doInBackground(Uri... uri) {
1740 // Asking for an update on the URI and ignore the result.
1741 final ContentResolver resolver = mContext.getContentResolver();
1742 resolver.update(uri[0], null, null, null);
1743 return null;
1744 }
1745 }
1746 final Uri uri = mAccount.defaultRecentFolderListUri;
1747 LogUtils.v(LOG_TAG, "Default recents at %s", uri);
1748 new PopulateDefault().execute(uri);
1749 break;
1750 }
1751 LogUtils.v(LOG_TAG, "Reading recent folders from the cursor.");
Vikram Aggarwalfa4b47e2012-03-09 13:02:46 -08001752 mRecentFolderList.loadFromUiProvider(data);
Vikram Aggarwal37263972012-04-17 15:51:14 -07001753 mActionBarView.requestRecentFoldersAndRedraw();
Vikram Aggarwalfa4b47e2012-03-09 13:02:46 -08001754 break;
Mindy Pereiraab486362012-03-21 18:18:53 -07001755 case LOADER_ACCOUNT_INBOX:
Marc Blankfd9d0b82012-04-23 16:01:51 -07001756 if (data != null && !data.isClosed() && data.moveToFirst()) {
Mindy Pereira0e88e9f2012-03-25 13:47:41 -07001757 Folder inbox = new Folder(data);
1758 onFolderChanged(inbox);
Mindy Pereirab4a43282012-03-23 16:20:03 -07001759 // Just want to get the inbox, don't care about updates to it
1760 // as this will be tracked by the folder change listener.
1761 mActivity.getLoaderManager().destroyLoader(LOADER_ACCOUNT_INBOX);
Mindy Pereira5ba33802012-03-26 16:30:11 -07001762 } else {
1763 LogUtils.d(LOG_TAG, "Unable to get the account inbox for account %s",
1764 mAccount != null ? mAccount.name : "");
Mindy Pereirab4a43282012-03-23 16:20:03 -07001765 }
Mindy Pereiraab486362012-03-21 18:18:53 -07001766 break;
1767 case LOADER_SEARCH:
1768 data.moveToFirst();
1769 Folder search = new Folder(data);
Mindy Pereira11e35962012-06-01 14:49:46 -07001770 updateFolder(search);
Mindy Pereiraab486362012-03-21 18:18:53 -07001771 mConvListContext = ConversationListContext.forSearchQuery(mAccount, mFolder,
Mindy Pereirad660d252012-03-26 11:48:43 -07001772 mActivity.getIntent()
1773 .getStringExtra(UIProvider.SearchQueryParameters.QUERY));
Mindy Pereiraab486362012-03-21 18:18:53 -07001774 showConversationList(mConvListContext);
1775 mActivity.invalidateOptionsMenu();
Mindy Pereira3b399222012-03-28 15:19:47 -07001776 mActivity.getLoaderManager().destroyLoader(LOADER_SEARCH);
Mindy Pereiraab486362012-03-21 18:18:53 -07001777 break;
Vikram Aggarwal7dedb952012-02-16 16:10:23 -08001778 }
1779 }
1780
Vikram Aggarwalc7694222012-04-23 13:37:01 -07001781 /**
1782 * Destructive actions on Conversations. This class should only be created by controllers, and
1783 * clients should only require {@link DestructiveAction}s, not specific implementations of the.
1784 * Only the controllers should know what kind of destructive actions are being created.
1785 */
Mindy Pereirade3e74a2012-07-24 09:43:10 -07001786 public class ConversationAction implements DestructiveAction {
Vikram Aggarwalacaa3c02012-04-24 12:45:27 -07001787 /**
1788 * The action to be performed. This is specified as the resource ID of the menu item
1789 * corresponding to this action: R.id.delete, R.id.report_spam, etc.
1790 */
Vikram Aggarwal7f602f72012-04-30 16:04:06 -07001791 private final int mAction;
Vikram Aggarwalbc67bb12012-04-30 14:05:35 -07001792 /** The action will act upon these conversations */
Paul Westbrook77eee622012-07-10 13:41:57 -07001793 private final Collection<Conversation> mTarget;
Vikram Aggarwal7f602f72012-04-30 16:04:06 -07001794 /** Whether this destructive action has already been performed */
1795 private boolean mCompleted;
Vikram Aggarwalc4113952012-05-11 14:14:56 -07001796 /** Whether this is an action on the currently selected set. */
1797 private final boolean mIsSelectedSet;
Vikram Aggarwale8a85322012-04-24 09:01:38 -07001798
Mindy Pereirafbe40192012-03-20 10:40:45 -07001799 /**
1800 * Create a listener object. action is one of four constants: R.id.y_button (archive),
1801 * R.id.delete , R.id.mute, and R.id.report_spam.
1802 * @param action
Vikram Aggarwalbc67bb12012-04-30 14:05:35 -07001803 * @param target Conversation that we want to apply the action to.
Vikram Aggarwalc4113952012-05-11 14:14:56 -07001804 * @param isBatch whether the conversations are in the currently selected batch set.
1805 */
1806 public ConversationAction(int action, Collection<Conversation> target, boolean isBatch) {
Mindy Pereirafbe40192012-03-20 10:40:45 -07001807 mAction = action;
Paul Westbrook77eee622012-07-10 13:41:57 -07001808 mTarget = ImmutableList.copyOf(target);
Vikram Aggarwalc4113952012-05-11 14:14:56 -07001809 mIsSelectedSet = isBatch;
Mindy Pereirafbe40192012-03-20 10:40:45 -07001810 }
1811
Vikram Aggarwalacaa3c02012-04-24 12:45:27 -07001812 /**
1813 * The action common to child classes. This performs the action specified in the constructor
1814 * on the conversations given here.
Vikram Aggarwalacaa3c02012-04-24 12:45:27 -07001815 */
Vikram Aggarwalbc67bb12012-04-30 14:05:35 -07001816 @Override
1817 public void performAction() {
Vikram Aggarwal7f602f72012-04-30 16:04:06 -07001818 if (isPerformed()) {
1819 return;
1820 }
Mindy Pereira3cb28b52012-05-24 15:26:39 -07001821 boolean undoEnabled = mAccount.supportsCapability(AccountCapabilities.UNDO);
Vikram Aggarwal7dd054e2012-05-21 14:43:10 -07001822
1823 // Are we destroying the currently shown conversation? Show the next one.
1824 if (LogUtils.isLoggable(LOG_TAG, LogUtils.DEBUG)){
Vikram Aggarwal8742a612012-08-13 10:22:50 -07001825 LogUtils.d(LOG_TAG, "ConversationAction.performAction():"
1826 + "\nmTarget=%s\nCurrent=%s",
Vikram Aggarwal7dd054e2012-05-21 14:43:10 -07001827 Conversation.toString(mTarget), mCurrentConversation);
1828 }
Vikram Aggarwal7dd054e2012-05-21 14:43:10 -07001829
Paul Westbrooke1221d22012-08-19 11:09:07 -07001830 if (mConversationListCursor == null) {
1831 LogUtils.e(LOG_TAG, "null ConversationCursor in ConversationAction.performAction():"
1832 + "\nmTarget=%s\nCurrent=%s",
1833 Conversation.toString(mTarget), mCurrentConversation);
1834 return;
1835 }
1836
Mindy Pereirafbe40192012-03-20 10:40:45 -07001837 switch (mAction) {
Mindy Pereira0692baf2012-03-23 17:34:31 -07001838 case R.id.archive:
Vikram Aggarwal7dd054e2012-05-21 14:43:10 -07001839 LogUtils.d(LOG_TAG, "Archiving");
Vikram Aggarwalbc67bb12012-04-30 14:05:35 -07001840 mConversationListCursor.archive(mContext, mTarget);
Mindy Pereirafbe40192012-03-20 10:40:45 -07001841 break;
1842 case R.id.delete:
Vikram Aggarwal7dd054e2012-05-21 14:43:10 -07001843 LogUtils.d(LOG_TAG, "Deleting");
Vikram Aggarwalbc67bb12012-04-30 14:05:35 -07001844 mConversationListCursor.delete(mContext, mTarget);
Mindy Pereira695d6962012-06-18 13:02:10 -07001845 if (mFolder.supportsCapability(FolderCapabilities.DELETE_ACTION_FINAL)) {
Marc Blank386243f2012-05-25 10:40:59 -07001846 undoEnabled = false;
1847 }
Mindy Pereirafbe40192012-03-20 10:40:45 -07001848 break;
1849 case R.id.mute:
Vikram Aggarwal7dd054e2012-05-21 14:43:10 -07001850 LogUtils.d(LOG_TAG, "Muting");
Vikram Aggarwalbc67bb12012-04-30 14:05:35 -07001851 if (mFolder.supportsCapability(FolderCapabilities.DESTRUCTIVE_MUTE)) {
Vikram Aggarwal7f602f72012-04-30 16:04:06 -07001852 for (Conversation c : mTarget) {
1853 c.localDeleteOnUpdate = true;
1854 }
Vikram Aggarwalbc67bb12012-04-30 14:05:35 -07001855 }
1856 mConversationListCursor.mute(mContext, mTarget);
Mindy Pereirafbe40192012-03-20 10:40:45 -07001857 break;
1858 case R.id.report_spam:
Vikram Aggarwal7dd054e2012-05-21 14:43:10 -07001859 LogUtils.d(LOG_TAG, "Reporting spam");
Vikram Aggarwalbc67bb12012-04-30 14:05:35 -07001860 mConversationListCursor.reportSpam(mContext, mTarget);
1861 break;
Paul Westbrook77eee622012-07-10 13:41:57 -07001862 case R.id.mark_not_spam:
1863 LogUtils.d(LOG_TAG, "Marking not spam");
1864 mConversationListCursor.reportNotSpam(mContext, mTarget);
1865 break;
Paul Westbrook76b20622012-07-12 11:45:43 -07001866 case R.id.report_phishing:
1867 LogUtils.d(LOG_TAG, "Reporting phishing");
1868 mConversationListCursor.reportPhishing(mContext, mTarget);
1869 break;
Vikram Aggarwalbc67bb12012-04-30 14:05:35 -07001870 case R.id.remove_star:
Vikram Aggarwal7dd054e2012-05-21 14:43:10 -07001871 LogUtils.d(LOG_TAG, "Removing star");
Vikram Aggarwalbc67bb12012-04-30 14:05:35 -07001872 // Star removal is destructive in the Starred folder.
1873 mConversationListCursor.updateBoolean(mContext, mTarget,
1874 ConversationColumns.STARRED, false);
1875 break;
1876 case R.id.mark_not_important:
Vikram Aggarwal7dd054e2012-05-21 14:43:10 -07001877 LogUtils.d(LOG_TAG, "Marking not-important");
Mindy Pereira445be212012-08-15 08:50:10 -07001878 // Marking not important is destructive in a mailbox
1879 // containing only important messages
Mindy Pereira0d03ef82012-08-15 09:05:48 -07001880 if (mFolder != null && mFolder.isImportantOnly()) {
Mindy Pereira445be212012-08-15 08:50:10 -07001881 for (Conversation conv : mTarget) {
1882 conv.localDeleteOnUpdate = true;
1883 }
1884 }
Vikram Aggarwalbc67bb12012-04-30 14:05:35 -07001885 mConversationListCursor.updateInt(mContext, mTarget,
1886 ConversationColumns.PRIORITY, UIProvider.ConversationPriority.LOW);
Mindy Pereirafbe40192012-03-20 10:40:45 -07001887 break;
Vikram Aggarwal3d7ca9d2012-05-11 14:40:36 -07001888 }
1889 if (undoEnabled) {
mindypead50392012-08-23 11:03:53 -07001890 mHandler.postDelayed(new Runnable() {
1891 @Override
1892 public void run() {
1893 onUndoAvailable(new ToastBarOperation(mTarget.size(), mAction,
1894 ToastBarOperation.UNDO));
1895 }
1896 }, mShowUndoBarDelay);
Vikram Aggarwal3d7ca9d2012-05-11 14:40:36 -07001897 }
Vikram Aggarwalbc67bb12012-04-30 14:05:35 -07001898 refreshConversationList();
Vikram Aggarwalc4113952012-05-11 14:14:56 -07001899 if (mIsSelectedSet) {
Vikram Aggarwalc4113952012-05-11 14:14:56 -07001900 mSelectedSet.clear();
1901 }
Mindy Pereirafbe40192012-03-20 10:40:45 -07001902 }
Vikram Aggarwal7f602f72012-04-30 16:04:06 -07001903
1904 /**
1905 * Returns true if this action has been performed, false otherwise.
Andy Huang839ada22012-07-20 15:48:40 -07001906 *
Vikram Aggarwal7f602f72012-04-30 16:04:06 -07001907 */
1908 private synchronized boolean isPerformed() {
1909 if (mCompleted) {
1910 return true;
1911 }
1912 mCompleted = true;
1913 return false;
1914 }
Mindy Pereirafbe40192012-03-20 10:40:45 -07001915 }
Mindy Pereirae5f4dc02012-03-21 16:08:53 -07001916
Vikram Aggarwal3d7ca9d2012-05-11 14:40:36 -07001917 /**
1918 * Get a destructive action for a menu action.
1919 * This is a temporary method, to control the profusion of {@link DestructiveAction} classes
1920 * that are created. Please do not copy this paradigm.
1921 * @param action the resource ID of the menu action: R.id.delete, for example
1922 * @param target the conversations to act upon.
1923 * @return a {@link DestructiveAction} that performs the specified action.
1924 */
Vikram Aggarwal531488e2012-05-29 16:36:52 -07001925 private final DestructiveAction getAction(int action, Collection<Conversation> target) {
Vikram Aggarwal3d7ca9d2012-05-11 14:40:36 -07001926 final DestructiveAction da = new ConversationAction(action, target, false);
1927 registerDestructiveAction(da);
1928 return da;
1929 }
1930
Vikram Aggarwald503df42012-05-11 10:13:35 -07001931 // Called from the FolderSelectionDialog after a user is done selecting folders to assign the
1932 // conversations to.
Mindy Pereirae5f4dc02012-03-21 16:08:53 -07001933 @Override
Mindy Pereira8db7e402012-07-13 10:32:47 -07001934 public final void assignFolder(Collection<FolderOperation> folderOps,
1935 Collection<Conversation> target, boolean batch, boolean showUndo) {
1936 // Actions are destructive only when the current folder can be assigned
1937 // to (which is the same as being able to un-assign a conversation from the folder) and
1938 // when the list of folders contains the current folder.
1939 final boolean isDestructive = mFolder
1940 .supportsCapability(FolderCapabilities.CAN_ACCEPT_MOVED_MESSAGES)
1941 && FolderOperation.isDestructive(folderOps, mFolder);
Vikram Aggarwald503df42012-05-11 10:13:35 -07001942 LogUtils.d(LOG_TAG, "onFolderChangesCommit: isDestructive = %b", isDestructive);
1943 if (isDestructive) {
1944 for (final Conversation c : target) {
1945 c.localDeleteOnUpdate = true;
Mindy Pereira6778f462012-03-23 18:01:55 -07001946 }
Mindy Pereirae5f4dc02012-03-21 16:08:53 -07001947 }
Mindy Pereira8db7e402012-07-13 10:32:47 -07001948 final DestructiveAction folderChange = getFolderChange(target, folderOps, isDestructive,
Mindy Pereira06642fa2012-07-12 16:23:27 -07001949 batch, showUndo);
Vikram Aggarwald503df42012-05-11 10:13:35 -07001950 // Update the UI elements depending no their visibility and availability
Vikram Aggarwal3d7ca9d2012-05-11 14:40:36 -07001951 // TODO(viki): Consolidate this into a single method requestDelete.
Vikram Aggarwald503df42012-05-11 10:13:35 -07001952 if (isDestructive) {
Vikram Aggarwal4f4782b2012-05-30 08:39:09 -07001953 delete(target, folderChange);
Vikram Aggarwal6902dcf2012-04-11 08:57:42 -07001954 } else {
Vikram Aggarwald503df42012-05-11 10:13:35 -07001955 requestUpdate(target, folderChange);
Mindy Pereirae5f4dc02012-03-21 16:08:53 -07001956 }
1957 }
1958
Mindy Pereira967ede62012-03-22 09:29:09 -07001959 @Override
Vikram Aggarwal3d7ca9d2012-05-11 14:40:36 -07001960 public final void onRefreshRequired() {
mindyp5390fca2012-08-22 12:12:25 -07001961 if (isAnimating() || isDragging()) {
Mindy Pereira69e88dd2012-08-10 09:30:18 -07001962 LogUtils.d(LOG_TAG, "onRefreshRequired: delay until animating done");
1963 return;
1964 }
Marc Blankbf128eb2012-04-18 15:58:45 -07001965 // Refresh the query in the background
Paul Westbrookcff1aea2012-08-10 11:51:00 -07001966 if (mConversationListCursor.isRefreshRequired()) {
1967 mConversationListCursor.refresh();
1968 }
Marc Blankbf128eb2012-04-18 15:58:45 -07001969 }
1970
mindyp5390fca2012-08-22 12:12:25 -07001971 @Override
1972 public void startDragMode() {
1973 mIsDragHappening = true;
1974 }
1975
1976 @Override
1977 public void stopDragMode() {
1978 mIsDragHappening = false;
1979 if (mConversationListCursor.isRefreshReady()) {
1980 LogUtils.d(LOG_TAG, "Stopped animating: try sync");
1981 onRefreshReady();
1982 }
1983
1984 if (mConversationListCursor.isRefreshRequired()) {
1985 LogUtils.d(LOG_TAG, "Stopped animating: refresh");
1986 mConversationListCursor.refresh();
1987 }
1988 }
1989
1990 private boolean isDragging() {
1991 return mIsDragHappening;
1992 }
1993
Mindy Pereira69e88dd2012-08-10 09:30:18 -07001994 private boolean isAnimating() {
1995 boolean isAnimating = false;
1996 ConversationListFragment convListFragment = getConversationListFragment();
1997 if (convListFragment != null) {
1998 AnimatedAdapter adapter = convListFragment.getAnimatedAdapter();
1999 if (adapter != null) {
2000 isAnimating = adapter.isAnimating();
2001 }
2002 }
2003 return isAnimating;
2004 }
2005
Marc Blankbf128eb2012-04-18 15:58:45 -07002006 /**
2007 * Called when the {@link ConversationCursor} is changed or has new data in it.
2008 * <p>
2009 * {@inheritDoc}
2010 */
2011 @Override
Vikram Aggarwal3d7ca9d2012-05-11 14:40:36 -07002012 public final void onRefreshReady() {
Paul Westbrookcff1aea2012-08-10 11:51:00 -07002013 if (!isAnimating()) {
Marc Blankbf128eb2012-04-18 15:58:45 -07002014 // Swap cursors
2015 mConversationListCursor.sync();
Marc Blankbf128eb2012-04-18 15:58:45 -07002016 }
Paul Westbrook937c94f2012-08-16 13:01:18 -07002017 mTracker.onCursorUpdated();
Marc Blankbf128eb2012-04-18 15:58:45 -07002018 }
2019
2020 @Override
Vikram Aggarwal3d7ca9d2012-05-11 14:40:36 -07002021 public final void onDataSetChanged() {
Paul Westbrook9f119c72012-04-24 16:10:59 -07002022 updateConversationListFragment();
Andy Huang632721e2012-04-11 16:57:26 -07002023 mConversationListObservable.notifyChanged();
Marc Blankbf128eb2012-04-18 15:58:45 -07002024 }
2025
Vikram Aggarwal3d7ca9d2012-05-11 14:40:36 -07002026 /**
2027 * If the Conversation List Fragment is visible, updates the fragment.
2028 */
2029 private final void updateConversationListFragment() {
Marc Blankbf128eb2012-04-18 15:58:45 -07002030 final ConversationListFragment convList = getConversationListFragment();
2031 if (convList != null) {
Vikram Aggarwal75daee52012-04-30 11:13:09 -07002032 refreshConversationList();
Paul Westbrook9f119c72012-04-24 16:10:59 -07002033 if (convList.isVisible()) {
Paul Westbrook9f119c72012-04-24 16:10:59 -07002034 Utils.setConversationCursorVisibility(mConversationListCursor, true);
2035 }
Marc Blankbf128eb2012-04-18 15:58:45 -07002036 }
2037 }
2038
2039 /**
2040 * This class handles throttled refresh of the conversation list
2041 */
2042 static class RefreshTimerTask extends TimerTask {
2043 final Handler mHandler;
2044 final AbstractActivityController mController;
2045
2046 RefreshTimerTask(AbstractActivityController controller, Handler handler) {
2047 mHandler = handler;
2048 mController = controller;
2049 }
2050
2051 @Override
2052 public void run() {
2053 mHandler.post(new Runnable() {
2054 @Override
2055 public void run() {
2056 LogUtils.d(LOG_TAG, "Delay done... calling onRefreshRequired");
2057 mController.onRefreshRequired();
2058 }});
2059 }
2060 }
2061
2062 /**
2063 * Cancel the refresh task, if it's running
2064 */
2065 private void cancelRefreshTask () {
2066 if (mConversationListRefreshTask != null) {
2067 mConversationListRefreshTask.cancel();
2068 mConversationListRefreshTask = null;
2069 }
2070 }
2071
2072 @Override
Paul Westbrookcff1aea2012-08-10 11:51:00 -07002073 public void onAnimationEnd(AnimatedAdapter animatedAdapter) {
2074 if (mConversationListCursor.isRefreshReady()) {
mindyp52544862012-08-20 12:05:36 -07002075 LogUtils.d(LOG_TAG, "Stopped animating: try sync");
Paul Westbrookcff1aea2012-08-10 11:51:00 -07002076 onRefreshReady();
Marc Blankbf128eb2012-04-18 15:58:45 -07002077 }
Marc Blankbf128eb2012-04-18 15:58:45 -07002078
Paul Westbrookcff1aea2012-08-10 11:51:00 -07002079 if (mConversationListCursor.isRefreshRequired()) {
mindyp52544862012-08-20 12:05:36 -07002080 LogUtils.d(LOG_TAG, "Stopped animating: refresh");
Paul Westbrookcff1aea2012-08-10 11:51:00 -07002081 mConversationListCursor.refresh();
2082 }
Marc Blankbf128eb2012-04-18 15:58:45 -07002083 }
2084
2085 @Override
Mindy Pereira967ede62012-03-22 09:29:09 -07002086 public void onSetEmpty() {
Mindy Pereira967ede62012-03-22 09:29:09 -07002087 }
2088
2089 @Override
2090 public void onSetPopulated(ConversationSelectionSet set) {
Vikram Aggarwal6902dcf2012-04-11 08:57:42 -07002091 final ConversationListFragment convList = getConversationListFragment();
2092 if (convList == null) {
2093 return;
2094 }
Vikram Aggarwal7c401b72012-08-13 16:43:47 -07002095 mCabActionMenu = new SelectedConversationsActionMenu(mActivity, set, mFolder,
Vikram Aggarwal531488e2012-05-29 16:36:52 -07002096 (SwipeableListView) convList.getListView());
Vikram Aggarwale128fc22012-04-04 12:33:34 -07002097 enableCabMode();
Mindy Pereira967ede62012-03-22 09:29:09 -07002098 }
2099
Mindy Pereira967ede62012-03-22 09:29:09 -07002100 @Override
2101 public void onSetChanged(ConversationSelectionSet set) {
2102 // Do nothing. We don't care about changes to the set.
2103 }
2104
2105 @Override
2106 public ConversationSelectionSet getSelectedSet() {
2107 return mSelectedSet;
2108 }
2109
Vikram Aggarwale128fc22012-04-04 12:33:34 -07002110 /**
2111 * Disable the Contextual Action Bar (CAB). The selected set is not changed.
2112 */
2113 protected void disableCabMode() {
Mindy Pereira8937bf12012-07-23 14:05:02 -07002114 // Commit any previous destructive actions when entering/ exiting CAB mode.
mindypc6adce32012-08-22 18:46:42 -07002115 commitDestructiveActions(true);
Vikram Aggarwale128fc22012-04-04 12:33:34 -07002116 if (mCabActionMenu != null) {
2117 mCabActionMenu.deactivate();
2118 }
2119 }
2120
2121 /**
2122 * Re-enable the CAB menu if required. The selection set is not changed.
2123 */
2124 protected void enableCabMode() {
Mindy Pereira8937bf12012-07-23 14:05:02 -07002125 // Commit any previous destructive actions when entering/ exiting CAB mode.
mindypc6adce32012-08-22 18:46:42 -07002126 commitDestructiveActions(true);
Vikram Aggarwale128fc22012-04-04 12:33:34 -07002127 if (mCabActionMenu != null) {
2128 mCabActionMenu.activate();
2129 }
2130 }
2131
Vikram Aggarwal4eb52712012-06-19 16:24:50 -07002132 /**
2133 * Unselect conversations and exit CAB mode.
2134 */
2135 protected final void exitCabMode() {
2136 mSelectedSet.clear();
2137 }
2138
Mindy Pereira967ede62012-03-22 09:29:09 -07002139 @Override
Mindy Pereirafd0c2972012-03-27 13:50:39 -07002140 public void startSearch() {
Vikram Aggarwal35f19d72012-04-24 13:24:48 -07002141 if (mAccount == null) {
2142 // We cannot search if there is no account. Drop the request to the floor.
2143 LogUtils.d(LOG_TAG, "AbstractActivityController.startSearch(): null account");
2144 return;
2145 }
Mindy Pereirafd0c2972012-03-27 13:50:39 -07002146 if (mAccount.supportsCapability(UIProvider.AccountCapabilities.LOCAL_SEARCH)
2147 | mAccount.supportsCapability(UIProvider.AccountCapabilities.SERVER_SEARCH)) {
Vikram Aggarwal70f130e2012-04-03 12:32:14 -07002148 onSearchRequested(mActionBarView.getQuery());
Mindy Pereirafd0c2972012-03-27 13:50:39 -07002149 } else {
2150 Toast.makeText(mActivity.getActivityContext(), mActivity.getActivityContext()
Mindy Pereiraa46c2992012-03-27 14:12:39 -07002151 .getString(R.string.search_unsupported), Toast.LENGTH_SHORT).show();
Mindy Pereirafd0c2972012-03-27 13:50:39 -07002152 }
2153 }
Mindy Pereiraacf60392012-04-06 09:11:00 -07002154
Vikram Aggarwal0dda5732012-04-06 11:20:16 -07002155 @Override
2156 public void exitSearchMode() {
2157 if (mViewMode.getMode() == ViewMode.SEARCH_RESULTS_LIST) {
2158 mActivity.finish();
2159 }
2160 }
2161
Mindy Pereiraacf60392012-04-06 09:11:00 -07002162 /**
2163 * Supports dragging conversations to a folder.
2164 */
2165 @Override
2166 public boolean supportsDrag(DragEvent event, Folder folder) {
2167 return (folder != null
2168 && event != null
2169 && event.getClipDescription() != null
2170 && folder.supportsCapability
2171 (UIProvider.FolderCapabilities.CAN_ACCEPT_MOVED_MESSAGES)
2172 && folder.supportsCapability
2173 (UIProvider.FolderCapabilities.CAN_HOLD_MAIL)
2174 && !mFolder.uri.equals(folder.uri));
2175 }
2176
2177 /**
Mindy Pereira6c2663d2012-07-20 15:37:29 -07002178 * Handles dropping conversations to a folder.
Mindy Pereiraacf60392012-04-06 09:11:00 -07002179 */
2180 @Override
2181 public void handleDrop(DragEvent event, final Folder folder) {
Mindy Pereiraacf60392012-04-06 09:11:00 -07002182 if (!supportsDrag(event, folder)) {
2183 return;
2184 }
Mindy Pereiraacf60392012-04-06 09:11:00 -07002185 final Collection<Conversation> conversations = mSelectedSet.values();
Mindy Pereira8db7e402012-07-13 10:32:47 -07002186 final Collection<FolderOperation> dropTarget = FolderOperation.listOf(new FolderOperation(
2187 folder, true));
2188 // Drag and drop is destructive: we remove conversations from the
2189 // current folder.
Mindy Pereira06642fa2012-07-12 16:23:27 -07002190 final DestructiveAction action = getFolderChange(conversations, dropTarget, true, true,
2191 true);
Vikram Aggarwal4f4782b2012-05-30 08:39:09 -07002192 delete(conversations, action);
Mindy Pereiraacf60392012-04-06 09:11:00 -07002193 }
Mindy Pereira0963ef82012-04-10 11:43:01 -07002194
2195 @Override
Mindy Pereira0963ef82012-04-10 11:43:01 -07002196 public void onTouchEvent(MotionEvent event) {
2197 if (event.getAction() == MotionEvent.ACTION_DOWN) {
Andrew Sappersteinc2c9dc12012-07-02 18:17:32 -07002198 if (mToastBar != null && !mToastBar.isEventInToastBar(event)) {
2199 mToastBar.hide(true);
Mindy Pereira0963ef82012-04-10 11:43:01 -07002200 }
2201 }
2202 }
Andy Huangb1c34dc2012-04-17 16:36:19 -07002203
Andy Huang632721e2012-04-11 16:57:26 -07002204 @Override
2205 public void onConversationSeen(Conversation conv) {
2206 mPagerController.onConversationSeen(conv);
2207 }
2208
Andy Huangb1c34dc2012-04-17 16:36:19 -07002209 private class ConversationListLoaderCallbacks implements
2210 LoaderManager.LoaderCallbacks<ConversationCursor> {
2211
2212 @Override
2213 public Loader<ConversationCursor> onCreateLoader(int id, Bundle args) {
2214 Loader<ConversationCursor> result = new ConversationCursorLoader((Activity) mActivity,
Paul Westbrook9a70e912012-08-17 15:53:20 -07002215 mAccount, mFolder.conversationListUri, mFolder.name);
Andy Huangb1c34dc2012-04-17 16:36:19 -07002216 return result;
2217 }
2218
2219 @Override
2220 public void onLoadFinished(Loader<ConversationCursor> loader, ConversationCursor data) {
Andy Huang632721e2012-04-11 16:57:26 -07002221 LogUtils.d(LOG_TAG, "IN AAC.ConversationCursor.onLoadFinished, data=%s loader=%s",
2222 data, loader);
Vikram Aggarwale8a85322012-04-24 09:01:38 -07002223 // Clear our all pending destructive actions before swapping the conversation cursor
2224 destroyPending(null);
Andy Huangb1c34dc2012-04-17 16:36:19 -07002225 mConversationListCursor = data;
Paul Westbrookbf232c32012-04-18 03:17:41 -07002226 mConversationListCursor.addListener(AbstractActivityController.this);
Andy Huangb1c34dc2012-04-17 16:36:19 -07002227
Paul Westbrook937c94f2012-08-16 13:01:18 -07002228 mTracker.onCursorUpdated();
2229
Andy Huange3df1ad2012-04-24 17:15:23 -07002230 mConversationListObservable.notifyChanged();
Andy Huangb1c34dc2012-04-17 16:36:19 -07002231 // Register the AbstractActivityController as a listener to changes in
2232 // data in the cursor.
2233 final ConversationListFragment convList = getConversationListFragment();
2234 if (convList != null) {
2235 convList.onCursorUpdated();
Paul Westbrook9f119c72012-04-24 16:10:59 -07002236
2237 if (convList.isVisible()) {
2238 // The conversation list is visible.
2239 Utils.setConversationCursorVisibility(mConversationListCursor, true);
2240 }
Andy Huangb1c34dc2012-04-17 16:36:19 -07002241 }
2242 // Shown for search results in two-pane mode only.
2243 if (shouldShowFirstConversation()) {
2244 if (mConversationListCursor.getCount() > 0) {
2245 mConversationListCursor.moveToPosition(0);
Andy Huangb1c34dc2012-04-17 16:36:19 -07002246 final Conversation conv = new Conversation(mConversationListCursor);
2247 conv.position = 0;
2248 onConversationSelected(conv);
2249 }
2250 }
2251 }
2252
2253 @Override
2254 public void onLoaderReset(Loader<ConversationCursor> loader) {
Paul Westbrook9a70e912012-08-17 15:53:20 -07002255 LogUtils.d(LOG_TAG, "IN AAC.ConversationCursor.onLoaderReset, data=%s loader=%s",
2256 mConversationListCursor, loader);
2257
2258 if (mConversationListCursor != null) {
2259 // Unregister the listener
2260 mConversationListCursor.removeListener(AbstractActivityController.this);
2261 mConversationListCursor = null;
2262
2263 // Inform anyone who is interested about the change
2264 mTracker.onCursorUpdated();
2265 mConversationListObservable.notifyChanged();
2266
2267 final ConversationListFragment convList = getConversationListFragment();
2268 if (convList != null) {
2269 convList.onCursorUpdated();
2270 }
Andy Huangb1c34dc2012-04-17 16:36:19 -07002271 }
Andy Huangb1c34dc2012-04-17 16:36:19 -07002272 }
Paul Westbrookbf232c32012-04-18 03:17:41 -07002273 }
Andy Huangb1c34dc2012-04-17 16:36:19 -07002274
Vikram Aggarwale8a85322012-04-24 09:01:38 -07002275 /**
2276 * Destroy the pending {@link DestructiveAction} till now and assign the given action as the
2277 * next destructive action..
2278 * @param nextAction the next destructive action to be performed. This can be null.
2279 */
2280 private final void destroyPending(DestructiveAction nextAction) {
2281 // If there is a pending action, perform that first.
2282 if (mPendingDestruction != null) {
2283 mPendingDestruction.performAction();
2284 }
2285 mPendingDestruction = nextAction;
2286 }
2287
2288 /**
2289 * Register a destructive action with the controller. This performs the previous destructive
Vikram Aggarwalacaa3c02012-04-24 12:45:27 -07002290 * 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 -07002291 * embellish this method any more.
Vikram Aggarwale8a85322012-04-24 09:01:38 -07002292 * @param action
2293 */
Vikram Aggarwal3d7ca9d2012-05-11 14:40:36 -07002294 private final void registerDestructiveAction(DestructiveAction action) {
Vikram Aggarwale8a85322012-04-24 09:01:38 -07002295 // TODO(viki): This is not a good idea. The best solution is for clients to request a
2296 // destructive action from the controller and for the controller to own the action. This is
2297 // a half-way solution while refactoring DestructiveAction.
2298 destroyPending(action);
2299 return;
2300 }
2301
Vikram Aggarwal531488e2012-05-29 16:36:52 -07002302 @Override
2303 public final DestructiveAction getBatchAction(int action) {
Vikram Aggarwalc4113952012-05-11 14:14:56 -07002304 final DestructiveAction da = new ConversationAction(action, mSelectedSet.values(), true);
Vikram Aggarwale8a85322012-04-24 09:01:38 -07002305 registerDestructiveAction(da);
2306 return da;
2307 }
Vikram Aggarwal41e6e712012-04-24 11:22:57 -07002308
Mindy Pereirade3e74a2012-07-24 09:43:10 -07002309 @Override
2310 public final DestructiveAction getDeferredBatchAction(int action) {
2311 final DestructiveAction da = new ConversationAction(action, mSelectedSet.values(), true);
2312 return da;
2313 }
2314
Vikram Aggarwalbc67bb12012-04-30 14:05:35 -07002315 /**
2316 * Class to change the folders that are assigned to a set of conversations. This is destructive
2317 * because the user can remove the current folder from the conversation, in which case it has
2318 * to be animated away from the current folder.
2319 */
Vikram Aggarwal41e6e712012-04-24 11:22:57 -07002320 private class FolderDestruction implements DestructiveAction {
Paul Westbrook77eee622012-07-10 13:41:57 -07002321 private final Collection<Conversation> mTarget;
Mindy Pereira8db7e402012-07-13 10:32:47 -07002322 private final ArrayList<FolderOperation> mFolderOps = new ArrayList<FolderOperation>();
Vikram Aggarwal41e6e712012-04-24 11:22:57 -07002323 private final boolean mIsDestructive;
Vikram Aggarwal7f602f72012-04-30 16:04:06 -07002324 /** Whether this destructive action has already been performed */
2325 private boolean mCompleted;
Mindy Pereiraf3a45562012-05-24 16:30:19 -07002326 private boolean mIsSelectedSet;
Mindy Pereira06642fa2012-07-12 16:23:27 -07002327 private boolean mShowUndo;
Mindy Pereira01f30502012-08-14 10:30:51 -07002328 private int mAction;
Vikram Aggarwal41e6e712012-04-24 11:22:57 -07002329
2330 /**
2331 * Create a new folder destruction object to act on the given conversations.
2332 * @param target
2333 */
Vikram Aggarwal7f602f72012-04-30 16:04:06 -07002334 private FolderDestruction(final Collection<Conversation> target,
Mindy Pereira8db7e402012-07-13 10:32:47 -07002335 final Collection<FolderOperation> folders, boolean isDestructive, boolean isBatch,
Mindy Pereira01f30502012-08-14 10:30:51 -07002336 boolean showUndo, int action) {
Paul Westbrook77eee622012-07-10 13:41:57 -07002337 mTarget = ImmutableList.copyOf(target);
Mindy Pereira8db7e402012-07-13 10:32:47 -07002338 mFolderOps.addAll(folders);
Vikram Aggarwal41e6e712012-04-24 11:22:57 -07002339 mIsDestructive = isDestructive;
Mindy Pereiraf3a45562012-05-24 16:30:19 -07002340 mIsSelectedSet = isBatch;
Mindy Pereira06642fa2012-07-12 16:23:27 -07002341 mShowUndo = showUndo;
Mindy Pereira01f30502012-08-14 10:30:51 -07002342 mAction = action;
Vikram Aggarwal41e6e712012-04-24 11:22:57 -07002343 }
2344
Vikram Aggarwal41e6e712012-04-24 11:22:57 -07002345 @Override
2346 public void performAction() {
Vikram Aggarwal7f602f72012-04-30 16:04:06 -07002347 if (isPerformed()) {
2348 return;
2349 }
Mindy Pereira06642fa2012-07-12 16:23:27 -07002350 if (mIsDestructive && mShowUndo) {
Mindy Pereirad33674992012-06-25 16:26:30 -07002351 ToastBarOperation undoOp = new ToastBarOperation(mTarget.size(),
Mindy Pereira01f30502012-08-14 10:30:51 -07002352 mAction, ToastBarOperation.UNDO);
Vikram Aggarwal41e6e712012-04-24 11:22:57 -07002353 onUndoAvailable(undoOp);
2354 }
Mindy Pereira8db7e402012-07-13 10:32:47 -07002355 // For each conversation, for each operation, add/ remove the
2356 // appropriate folders.
2357 for (Conversation target : mTarget) {
2358 HashMap<Uri, Folder> targetFolders = Folder
Mindy Pereira68f83842012-07-27 09:43:31 -07002359 .hashMapForFolders(target.getRawFolders());
Mindy Pereira01f30502012-08-14 10:30:51 -07002360 if (mIsDestructive) {
2361 target.localDeleteOnUpdate = true;
2362 }
Mindy Pereira8db7e402012-07-13 10:32:47 -07002363 for (FolderOperation op : mFolderOps) {
2364 if (op.mAdd) {
2365 targetFolders.put(op.mFolder.uri, op.mFolder);
2366 } else {
2367 targetFolders.remove(op.mFolder.uri);
2368 }
2369 }
Mindy Pereira85c4a772012-07-30 10:47:26 -07002370 target.setRawFolders(Folder.getSerializedFolderString(targetFolders.values()));
Mindy Pereira00ffece2012-07-27 08:49:56 -07002371 mConversationListCursor.updateString(mContext, Conversation.listOf(target),
Mindy Pereira85c4a772012-07-30 10:47:26 -07002372 Conversation.UPDATE_FOLDER_COLUMN, target.getRawFoldersString());
Mindy Pereira8db7e402012-07-13 10:32:47 -07002373 }
Vikram Aggarwal7f602f72012-04-30 16:04:06 -07002374 refreshConversationList();
Mindy Pereiraf3a45562012-05-24 16:30:19 -07002375 if (mIsSelectedSet) {
2376 mSelectedSet.clear();
2377 }
Vikram Aggarwal7f602f72012-04-30 16:04:06 -07002378 }
Mindy Pereira8db7e402012-07-13 10:32:47 -07002379
Vikram Aggarwal7f602f72012-04-30 16:04:06 -07002380 /**
2381 * Returns true if this action has been performed, false otherwise.
Andy Huang839ada22012-07-20 15:48:40 -07002382 *
Vikram Aggarwal7f602f72012-04-30 16:04:06 -07002383 */
2384 private synchronized boolean isPerformed() {
2385 if (mCompleted) {
2386 return true;
Vikram Aggarwal41e6e712012-04-24 11:22:57 -07002387 }
Vikram Aggarwal7f602f72012-04-30 16:04:06 -07002388 mCompleted = true;
2389 return false;
Vikram Aggarwal41e6e712012-04-24 11:22:57 -07002390 }
2391 }
Vikram Aggarwal7f602f72012-04-30 16:04:06 -07002392
Vikram Aggarwald503df42012-05-11 10:13:35 -07002393 private final DestructiveAction getFolderChange(Collection<Conversation> target,
Mindy Pereira8db7e402012-07-13 10:32:47 -07002394 Collection<FolderOperation> folders, boolean isDestructive, boolean isBatch,
2395 boolean showUndo) {
Mindy Pereira06642fa2012-07-12 16:23:27 -07002396 final DestructiveAction da = new FolderDestruction(target, folders, isDestructive, isBatch,
Mindy Pereira01f30502012-08-14 10:30:51 -07002397 showUndo, R.id.change_folder);
2398 registerDestructiveAction(da);
2399 return da;
2400 }
2401
2402 @Override
2403 public final DestructiveAction getDeferredRemoveFolder(Collection<Conversation> target,
2404 Folder toRemove, boolean isDestructive, boolean isBatch,
2405 boolean showUndo) {
2406 Collection<FolderOperation> folderOps = new ArrayList<FolderOperation>();
2407 folderOps.add(new FolderOperation(toRemove, false));
2408 return new FolderDestruction(target, folderOps, isDestructive, isBatch,
2409 showUndo, R.id.remove_folder);
2410 }
2411
2412 private final DestructiveAction getRemoveFolder(Collection<Conversation> target,
2413 Folder toRemove, boolean isDestructive, boolean isBatch,
2414 boolean showUndo) {
2415 Collection<FolderOperation> folderOps = new ArrayList<FolderOperation>();
2416 folderOps.add(new FolderOperation(toRemove, false));
2417 DestructiveAction da = new FolderDestruction(target, folderOps, isDestructive, isBatch,
2418 showUndo, R.id.remove_folder);
Vikram Aggarwal41e6e712012-04-24 11:22:57 -07002419 registerDestructiveAction(da);
2420 return da;
2421 }
Vikram Aggarwal75daee52012-04-30 11:13:09 -07002422
Vikram Aggarwal4f4782b2012-05-30 08:39:09 -07002423 @Override
2424 public final void refreshConversationList() {
Vikram Aggarwal75daee52012-04-30 11:13:09 -07002425 final ConversationListFragment convList = getConversationListFragment();
2426 if (convList == null) {
2427 return;
2428 }
2429 convList.requestListRefresh();
2430 }
Andrew Sappersteinc2c9dc12012-07-02 18:17:32 -07002431
2432 protected final ActionClickedListener getUndoClickedListener(
2433 final AnimatedAdapter listAdapter) {
2434 return new ActionClickedListener() {
2435 @Override
2436 public void onActionClicked() {
2437 if (mAccount.undoUri != null) {
2438 // NOTE: We might want undo to return the messages affected, in which case
2439 // the resulting cursor might be interesting...
2440 // TODO: Use UIProvider.SEQUENCE_QUERY_PARAMETER to indicate the set of
2441 // commands to undo
2442 if (mConversationListCursor != null) {
2443 mConversationListCursor.undo(
2444 mActivity.getActivityContext(), mAccount.undoUri);
2445 }
2446 if (listAdapter != null) {
2447 listAdapter.setUndo(true);
2448 }
2449 }
2450 }
2451 };
2452 }
2453
Andrew Sapperstein9d7519d2012-07-16 14:03:53 -07002454 protected final void showErrorToast(final Folder folder, boolean replaceVisibleToast) {
Andrew Sappersteinc2c9dc12012-07-02 18:17:32 -07002455 mToastBar.setConversationMode(false);
Andrew Sapperstein00179f12012-08-09 15:15:40 -07002456
2457 ActionClickedListener listener = null;
2458 int actionTextResourceId;
2459 final int lastSyncResult = folder.lastSyncResult;
2460 switch (lastSyncResult) {
2461 case UIProvider.LastSyncResult.CONNECTION_ERROR:
2462 listener = getRetryClickedListener(folder);
2463 actionTextResourceId = R.string.retry;
2464 break;
2465 case UIProvider.LastSyncResult.AUTH_ERROR:
2466 listener = getSignInClickedListener();
2467 actionTextResourceId = R.string.signin;
2468 break;
2469 case UIProvider.LastSyncResult.SECURITY_ERROR:
2470 return; // Currently we do nothing for security errors.
2471 case UIProvider.LastSyncResult.STORAGE_ERROR:
2472 listener = getStorageErrorClickedListener();
2473 actionTextResourceId = R.string.info;
2474 break;
2475 case UIProvider.LastSyncResult.INTERNAL_ERROR:
2476 listener = getInternalErrorClickedListener();
2477 actionTextResourceId = R.string.report;
2478 break;
2479 default:
2480 return;
2481 }
Andrew Sappersteinc2c9dc12012-07-02 18:17:32 -07002482 mToastBar.show(
Andrew Sapperstein00179f12012-08-09 15:15:40 -07002483 listener,
Andrew Sapperstein5d420962012-07-12 16:43:10 -07002484 R.drawable.ic_alert_white,
Andrew Sappersteinc2c9dc12012-07-02 18:17:32 -07002485 Utils.getSyncStatusText(mActivity.getActivityContext(),
Andrew Sapperstein00179f12012-08-09 15:15:40 -07002486 lastSyncResult),
Andrew Sappersteinc2c9dc12012-07-02 18:17:32 -07002487 false, /* showActionIcon */
Andrew Sapperstein00179f12012-08-09 15:15:40 -07002488 actionTextResourceId,
Andrew Sapperstein9d7519d2012-07-16 14:03:53 -07002489 replaceVisibleToast,
2490 new ToastBarOperation(1, 0, ToastBarOperation.ERROR));
Andrew Sappersteinc2c9dc12012-07-02 18:17:32 -07002491 }
2492
Andrew Sapperstein00179f12012-08-09 15:15:40 -07002493 private ActionClickedListener getRetryClickedListener(final Folder folder) {
Andrew Sappersteinc2c9dc12012-07-02 18:17:32 -07002494 return new ActionClickedListener() {
2495 @Override
2496 public void onActionClicked() {
2497 final Uri uri = folder.refreshUri;
2498
2499 if (uri != null) {
Paul Westbrook4969e0c2012-08-20 14:38:39 -07002500 startAsyncRefreshTask(uri);
Andrew Sappersteinc2c9dc12012-07-02 18:17:32 -07002501 }
2502 }
2503 };
2504 }
Andrew Sapperstein00179f12012-08-09 15:15:40 -07002505
2506 private ActionClickedListener getSignInClickedListener() {
2507 return new ActionClickedListener() {
2508 @Override
2509 public void onActionClicked() {
Paul Westbrook122f7c22012-08-20 17:50:31 -07002510 promptUserForAuthentication(mAccount);
Andrew Sapperstein00179f12012-08-09 15:15:40 -07002511 }
2512 };
2513 }
2514
2515 private ActionClickedListener getStorageErrorClickedListener() {
2516 return new ActionClickedListener() {
2517 @Override
2518 public void onActionClicked() {
Paul Westbrook4969e0c2012-08-20 14:38:39 -07002519 showStorageErrorDialog();
Andrew Sapperstein00179f12012-08-09 15:15:40 -07002520 }
2521 };
2522 }
2523
Paul Westbrook4969e0c2012-08-20 14:38:39 -07002524 private void showStorageErrorDialog() {
2525 DialogFragment fragment = (DialogFragment)
2526 mFragmentManager.findFragmentByTag(SYNC_ERROR_DIALOG_FRAGMENT_TAG);
2527 if (fragment == null) {
2528 fragment = SyncErrorDialogFragment.newInstance();
2529 }
2530 fragment.show(mFragmentManager, SYNC_ERROR_DIALOG_FRAGMENT_TAG);
2531 }
2532
Andrew Sapperstein00179f12012-08-09 15:15:40 -07002533 private ActionClickedListener getInternalErrorClickedListener() {
2534 return new ActionClickedListener() {
2535 @Override
2536 public void onActionClicked() {
Paul Westbrook17beb0b2012-08-20 13:34:37 -07002537 Utils.sendFeedback(
2538 mActivity.getActivityContext(), mAccount, true /* reportingProblem */);
Andrew Sapperstein00179f12012-08-09 15:15:40 -07002539 }
2540 };
2541 }
Paul Westbrook4969e0c2012-08-20 14:38:39 -07002542
2543 @Override
2544 public void onFooterViewErrorActionClick(Folder folder, int errorStatus) {
2545 Uri uri = null;
2546 switch (errorStatus) {
2547 case UIProvider.LastSyncResult.CONNECTION_ERROR:
2548 if (folder != null && folder.refreshUri != null) {
2549 uri = folder.refreshUri;
2550 }
2551 break;
2552 case UIProvider.LastSyncResult.AUTH_ERROR:
Paul Westbrook122f7c22012-08-20 17:50:31 -07002553 promptUserForAuthentication(mAccount);
Paul Westbrook4969e0c2012-08-20 14:38:39 -07002554 return;
2555 case UIProvider.LastSyncResult.SECURITY_ERROR:
2556 return; // Currently we do nothing for security errors.
2557 case UIProvider.LastSyncResult.STORAGE_ERROR:
2558 showStorageErrorDialog();
2559 return;
2560 case UIProvider.LastSyncResult.INTERNAL_ERROR:
2561 Utils.sendFeedback(
2562 mActivity.getActivityContext(), mAccount, true /* reportingProblem */);
2563 return;
2564 default:
2565 return;
2566 }
2567
2568 if (uri != null) {
2569 startAsyncRefreshTask(uri);
2570 }
2571 }
2572
2573 @Override
2574 public void onFooterViewLoadMoreClick(Folder folder) {
2575 if (folder != null && folder.loadMoreUri != null) {
2576 startAsyncRefreshTask(folder.loadMoreUri);
2577 }
2578 }
2579
2580 private void startAsyncRefreshTask(Uri uri) {
2581 if (mFolderSyncTask != null) {
2582 mFolderSyncTask.cancel(true);
2583 }
2584 mFolderSyncTask = new AsyncRefreshTask(mActivity.getActivityContext(), uri);
2585 mFolderSyncTask.execute();
2586 }
Paul Westbrook122f7c22012-08-20 17:50:31 -07002587
2588 private void promptUserForAuthentication(Account account) {
2589 if (account != null && account.reauthenticationIntentUri != null) {
2590 final Intent authenticationIntent =
2591 new Intent(Intent.ACTION_VIEW, account.reauthenticationIntentUri);
2592 mActivity.startActivityForResult(authenticationIntent, REAUTHENTICATE_REQUEST_CODE);
2593 }
2594 }
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -08002595}