blob: 38fbb3ca315942f6750e5ed6dafee4d7307aab7d [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;
Vikram Aggarwal649b9ea2012-08-27 12:15:20 -070046import android.text.TextUtils;
Mindy Pereiraacf60392012-04-06 09:11:00 -070047import android.view.DragEvent;
Vikram Aggarwala55b36c2012-01-13 11:45:02 -080048import android.view.KeyEvent;
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -080049import android.view.LayoutInflater;
Vikram Aggarwala55b36c2012-01-13 11:45:02 -080050import android.view.Menu;
Mindy Pereira28d5f722012-02-15 12:32:40 -080051import android.view.MenuInflater;
Vikram Aggarwala55b36c2012-01-13 11:45:02 -080052import android.view.MenuItem;
53import android.view.MotionEvent;
Mindy Pereirad33674992012-06-25 16:26:30 -070054import android.view.View;
Mindy Pereirafd0c2972012-03-27 13:50:39 -070055import android.widget.Toast;
Vikram Aggarwala55b36c2012-01-13 11:45:02 -080056
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -080057import com.android.mail.ConversationListContext;
Andy Huangf9a73482012-03-13 15:54:02 -070058import com.android.mail.R;
Mindy Pereira967ede62012-03-22 09:29:09 -070059import com.android.mail.browse.ConversationCursor;
Paul Westbrookbf232c32012-04-18 03:17:41 -070060import com.android.mail.browse.ConversationPagerController;
Andy Huang839ada22012-07-20 15:48:40 -070061import com.android.mail.browse.MessageCursor.ConversationMessage;
Marc Blank7c9f6ac2012-04-02 13:27:19 -070062import com.android.mail.browse.SelectedConversationsActionMenu;
Andy Huang991f4532012-08-14 13:32:55 -070063import com.android.mail.browse.SyncErrorDialogFragment;
Mindy Pereira9b875682012-02-15 18:10:54 -080064import com.android.mail.compose.ComposeActivity;
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -080065import com.android.mail.providers.Account;
Mindy Pereira9b875682012-02-15 18:10:54 -080066import com.android.mail.providers.Conversation;
Andy Huang839ada22012-07-20 15:48:40 -070067import com.android.mail.providers.ConversationInfo;
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -080068import com.android.mail.providers.Folder;
Vikram Aggarwal7a5d95a2012-07-27 16:24:54 -070069import com.android.mail.providers.FolderWatcher;
Paul Westbrookc2074c42012-03-22 15:26:58 -070070import com.android.mail.providers.MailAppProvider;
Mindy Pereiradac00542012-03-01 10:50:33 -080071import com.android.mail.providers.Settings;
Vikram Aggarwale620a7a2012-03-28 13:16:14 -070072import com.android.mail.providers.SuggestionsProvider;
Vikram Aggarwal80aeac52012-02-07 15:27:20 -080073import com.android.mail.providers.UIProvider;
Mindy Pereira3cb28b52012-05-24 15:26:39 -070074import com.android.mail.providers.UIProvider.AccountCapabilities;
Paul Westbrook2388c5d2012-03-25 12:29:11 -070075import com.android.mail.providers.UIProvider.AccountCursorExtraKeys;
Mindy Pereirac9d59182012-03-22 16:06:46 -070076import com.android.mail.providers.UIProvider.ConversationColumns;
Vikram Aggarwal54452ae2012-03-13 15:29:00 -070077import com.android.mail.providers.UIProvider.FolderCapabilities;
Andrew Sappersteinc2c9dc12012-07-02 18:17:32 -070078import com.android.mail.ui.ActionableToastBar.ActionClickedListener;
Andy Huang839ada22012-07-20 15:48:40 -070079import com.android.mail.utils.ContentProviderTask;
Paul Westbrookb334c902012-06-25 11:42:46 -070080import com.android.mail.utils.LogTag;
Mindy Pereira5cb0c3e2012-02-22 15:22:47 -080081import com.android.mail.utils.LogUtils;
Vikram Aggarwalfa131a22012-02-02 13:56:22 -080082import com.android.mail.utils.Utils;
Paul Westbrookca08fc12012-07-31 12:01:15 -070083import com.google.common.base.Objects;
Paul Westbrook77eee622012-07-10 13:41:57 -070084import com.google.common.collect.ImmutableList;
Mindy Pereiraacf60392012-04-06 09:11:00 -070085import com.google.common.collect.Lists;
Marc Blank167faa82012-03-21 13:11:53 -070086import com.google.common.collect.Sets;
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -080087
Marc Blank167faa82012-03-21 13:11:53 -070088import java.util.ArrayList;
Mindy Pereirafbe40192012-03-20 10:40:45 -070089import java.util.Collection;
Andy Huang839ada22012-07-20 15:48:40 -070090import java.util.Collections;
Mindy Pereira8db7e402012-07-13 10:32:47 -070091import java.util.HashMap;
Paul Westbrook23b74b92012-02-29 11:36:12 -080092import java.util.Set;
Marc Blankbf128eb2012-04-18 15:58:45 -070093import java.util.TimerTask;
Paul Westbrook23b74b92012-02-29 11:36:12 -080094
95
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -080096/**
Mindy Pereira161f50d2012-02-28 15:47:19 -080097 * This is an abstract implementation of the Activity Controller. This class
98 * knows how to respond to menu items, state changes, layout changes, etc. It
99 * weaves together the views and listeners, dispatching actions to the
100 * respective underlying classes.
Vikram Aggarwala55b36c2012-01-13 11:45:02 -0800101 * <p>
Mindy Pereira161f50d2012-02-28 15:47:19 -0800102 * Even though this class is abstract, it should provide default implementations
103 * for most, if not all the methods in the ActivityController interface. This
104 * makes the task of the subclasses easier: OnePaneActivityController and
105 * TwoPaneActivityController can be concise when the common functionality is in
106 * AbstractActivityController.
107 * </p>
108 * <p>
109 * In the Gmail codebase, this was called BaseActivityController
110 * </p>
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -0800111 */
Vikram Aggarwal531488e2012-05-29 16:36:52 -0700112public abstract class AbstractActivityController implements ActivityController {
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800113 // Keys for serialization of various information in Bundles.
Vikram Aggarwalcabd3f22012-04-19 10:14:41 -0700114 /** Tag for {@link #mAccount} */
Vikram Aggarwal6c511582012-02-27 10:59:47 -0800115 private static final String SAVED_ACCOUNT = "saved-account";
Vikram Aggarwalcabd3f22012-04-19 10:14:41 -0700116 /** Tag for {@link #mFolder} */
Mindy Pereira5e478d22012-03-26 18:04:58 -0700117 private static final String SAVED_FOLDER = "saved-folder";
Vikram Aggarwalcabd3f22012-04-19 10:14:41 -0700118 /** Tag for {@link #mCurrentConversation} */
Mindy Pereira26f23fc2012-03-27 10:26:04 -0700119 private static final String SAVED_CONVERSATION = "saved-conversation";
Vikram Aggarwalcabd3f22012-04-19 10:14:41 -0700120 /** Tag for {@link #mSelectedSet} */
121 private static final String SAVED_SELECTED_SET = "saved-selected-set";
Vikram Aggarwal649b9ea2012-08-27 12:15:20 -0700122 /** Tag for {@link ActionableToastBar#getOperation()} */
Mindy Pereirad33674992012-06-25 16:26:30 -0700123 private static final String SAVED_TOAST_BAR_OP = "saved-toast-bar-op";
Vikram Aggarwal649b9ea2012-08-27 12:15:20 -0700124 /** Tag for {@link #mFolderListFolder} */
125 private static final String SAVED_HIERARCHICAL_FOLDER = "saved-hierarchical-folder";
Vikram Aggarwalf3341402012-08-07 10:09:38 -0700126 /** Tag for {@link ConversationListContext#searchQuery} */
127 private static final String SAVED_QUERY = "saved-query";
Mindy Pereira5cb0c3e2012-02-22 15:22:47 -0800128
Vikram Aggarwal6902dcf2012-04-11 08:57:42 -0700129 /** Tag used when loading a wait fragment */
130 protected static final String TAG_WAIT = "wait-fragment";
131 /** Tag used when loading a conversation list fragment. */
Paul Westbrookbf232c32012-04-18 03:17:41 -0700132 public static final String TAG_CONVERSATION_LIST = "tag-conversation-list";
Vikram Aggarwal6902dcf2012-04-11 08:57:42 -0700133 /** Tag used when loading a folder list fragment. */
134 protected static final String TAG_FOLDER_LIST = "tag-folder-list";
135
Vikram Aggarwald7a12cd2012-02-03 09:36:20 -0800136 protected Account mAccount;
Mindy Pereira12a4d802012-06-22 09:24:43 -0700137 protected Folder mFolder;
Andy Huang6681e542012-06-14 14:36:45 -0700138 protected MailActionBarView mActionBarView;
Vikram Aggarwal7c401b72012-08-13 16:43:47 -0700139 protected final ControllableActivity mActivity;
Vikram Aggarwala55b36c2012-01-13 11:45:02 -0800140 protected final Context mContext;
Vikram Aggarwal6902dcf2012-04-11 08:57:42 -0700141 private final FragmentManager mFragmentManager;
Vikram Aggarwalec5cbf72012-03-08 15:10:35 -0800142 protected final RecentFolderList mRecentFolderList;
Vikram Aggarwal80aeac52012-02-07 15:27:20 -0800143 protected ConversationListContext mConvListContext;
Mindy Pereira9b875682012-02-15 18:10:54 -0800144 protected Conversation mCurrentConversation;
Vikram Aggarwald7a12cd2012-02-03 09:36:20 -0800145
Paul Westbrook6ead20d2012-03-19 14:48:14 -0700146 /** A {@link android.content.BroadcastReceiver} that suppresses new e-mail notifications. */
147 private SuppressNotificationReceiver mNewEmailReceiver = null;
148
Mindy Pereirafbe40192012-03-20 10:40:45 -0700149 protected Handler mHandler = new Handler();
Mindy Pereirafa995b42012-07-25 12:06:13 -0700150
Vikram Aggarwalfa131a22012-02-02 13:56:22 -0800151 /**
Mindy Pereira161f50d2012-02-28 15:47:19 -0800152 * The current mode of the application. All changes in mode are initiated by
153 * the activity controller. View mode changes are propagated to classes that
154 * attach themselves as listeners of view mode changes.
Vikram Aggarwalfa131a22012-02-02 13:56:22 -0800155 */
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800156 protected final ViewMode mViewMode;
Vikram Aggarwal80aeac52012-02-07 15:27:20 -0800157 protected ContentResolver mResolver;
Vikram Aggarwal7dedb952012-02-16 16:10:23 -0800158 protected boolean isLoaderInitialized = false;
Mindy Pereirab7b33e02012-02-21 15:32:19 -0800159 private AsyncRefreshTask mAsyncRefreshTask;
Mindy Pereira5cb0c3e2012-02-22 15:22:47 -0800160
Andy Huang4e0158f2012-08-07 21:06:01 -0700161 private boolean mDestroyed;
162
Andy Huang1ee96b22012-08-24 20:19:53 -0700163 /**
164 * Are we in a point in the Activity/Fragment lifecycle where it's safe to execute fragment
165 * transactions? (including back stack manipulation)
166 * <p>
167 * Per docs in {@link FragmentManager#beginTransaction()}, this flag starts out true, switches
168 * to false after {@link Activity#onSaveInstanceState}, and becomes true again in both onStart
169 * and onResume.
170 */
171 private boolean mSafeToModifyFragments = true;
172
Paul Westbrook23b74b92012-02-29 11:36:12 -0800173 private final Set<Uri> mCurrentAccountUris = Sets.newHashSet();
Mindy Pereira967ede62012-03-22 09:29:09 -0700174 protected ConversationCursor mConversationListCursor;
Andy Huang632721e2012-04-11 16:57:26 -0700175 private final DataSetObservable mConversationListObservable = new DataSetObservable() {
176 @Override
177 public void registerObserver(DataSetObserver observer) {
178 final int count = mObservers.size();
179 super.registerObserver(observer);
Vikram Aggarwal7c401b72012-08-13 16:43:47 -0700180 LogUtils.d(LOG_TAG, "IN AAC.register(List)Observer: %s before=%d after=%d", observer,
Andy Huang632721e2012-04-11 16:57:26 -0700181 count, mObservers.size());
182 }
183 @Override
184 public void unregisterObserver(DataSetObserver observer) {
185 final int count = mObservers.size();
186 super.unregisterObserver(observer);
Vikram Aggarwal7c401b72012-08-13 16:43:47 -0700187 LogUtils.d(LOG_TAG, "IN AAC.unregister(List)Observer: %s before=%d after=%d", observer,
Andy Huang632721e2012-04-11 16:57:26 -0700188 count, mObservers.size());
189 }
190 };
Marc Blankbf128eb2012-04-18 15:58:45 -0700191
Marc Blankbf128eb2012-04-18 15:58:45 -0700192 private RefreshTimerTask mConversationListRefreshTask;
Marc Blanke1d1b072012-04-13 17:29:16 -0700193
Vikram Aggarwal7c401b72012-08-13 16:43:47 -0700194 /** Listeners that are interested in changes to the current account. */
195 private final DataSetObservable mAccountObservers = new DataSetObservable() {
196 @Override
197 public void registerObserver(DataSetObserver observer) {
198 final int count = mObservers.size();
199 super.registerObserver(observer);
200 LogUtils.d(LOG_TAG, "IN AAC.register(Account)Observer: %s before=%d after=%d",
201 observer, count, mObservers.size());
202 }
203 @Override
204 public void unregisterObserver(DataSetObserver observer) {
205 final int count = mObservers.size();
206 super.unregisterObserver(observer);
207 LogUtils.d(LOG_TAG, "IN AAC.unregister(Account)Observer: %s before=%d after=%d",
208 observer, count, mObservers.size());
209 }
210 };
Vikram Aggarwal7d816002012-04-17 17:06:41 -0700211
Mindy Pereira967ede62012-03-22 09:29:09 -0700212 /**
213 * Selected conversations, if any.
214 */
Andy Huang4556a442012-03-30 16:42:05 -0700215 private final ConversationSelectionSet mSelectedSet = new ConversationSelectionSet();
Mindy Pereiradac00542012-03-01 10:50:33 -0800216
Paul Westbrookc7a070f2012-04-12 01:46:41 -0700217 private final int mFolderItemUpdateDelayMs;
218
Vikram Aggarwal135fd022012-04-11 14:44:15 -0700219 /** Keeps track of selected and unselected conversations */
Paul Westbrook937c94f2012-08-16 13:01:18 -0700220 final protected ConversationPositionTracker mTracker;
Vikram Aggarwalaf1ee0c2012-04-12 17:13:13 -0700221
Vikram Aggarwale128fc22012-04-04 12:33:34 -0700222 /**
223 * Action menu associated with the selected set.
224 */
225 SelectedConversationsActionMenu mCabActionMenu;
Andrew Sappersteinc2c9dc12012-07-02 18:17:32 -0700226 protected ActionableToastBar mToastBar;
Andy Huang632721e2012-04-11 16:57:26 -0700227 protected ConversationPagerController mPagerController;
Mindy Pereiraab486362012-03-21 18:18:53 -0700228
Andy Huangb1c34dc2012-04-17 16:36:19 -0700229 // this is split out from the general loader dispatcher because its loader doesn't return a
230 // basic Cursor
231 private final ConversationListLoaderCallbacks mListCursorCallbacks =
232 new ConversationListLoaderCallbacks();
233
Andy Huang090db1e2012-07-25 13:25:28 -0700234 private final DataSetObservable mFolderObservable = new DataSetObservable();
235
Paul Westbrookb334c902012-06-25 11:42:46 -0700236 protected static final String LOG_TAG = LogTag.getLogTag();
Vikram Aggarwalfa4b47e2012-03-09 13:02:46 -0800237 /** Constants used to differentiate between the types of loaders. */
238 private static final int LOADER_ACCOUNT_CURSOR = 0;
Vikram Aggarwalfa4b47e2012-03-09 13:02:46 -0800239 private static final int LOADER_FOLDER_CURSOR = 2;
240 private static final int LOADER_RECENT_FOLDERS = 3;
Mindy Pereira967ede62012-03-22 09:29:09 -0700241 private static final int LOADER_CONVERSATION_LIST = 4;
Mindy Pereiraab486362012-03-21 18:18:53 -0700242 private static final int LOADER_ACCOUNT_INBOX = 5;
243 private static final int LOADER_SEARCH = 6;
Paul Westbrook2d50bcd2012-04-10 11:53:47 -0700244 private static final int LOADER_ACCOUNT_UPDATE_CURSOR = 7;
Vikram Aggarwal7a5d95a2012-07-27 16:24:54 -0700245 /**
246 * Guaranteed to be the last loader ID used by the activity. Loaders are owned by Activity or
247 * fragments, and within an activity, loader IDs need to be unique. A hack to ensure that the
248 * {@link FolderWatcher} can create its folder loaders without clashing with the IDs of those
249 * of the {@link AbstractActivityController}. Currently, the {@link FolderWatcher} is the only
250 * other class that uses this activity's LoaderManager. If another class needs activity-level
251 * loaders, consider consolidating the loaders in a central location: a UI-less fragment
252 * perhaps.
253 */
254 public static final int LAST_LOADER_ID = 100;
Vikram Aggarwala55b36c2012-01-13 11:45:02 -0800255
Paul Westbrook2388c5d2012-03-25 12:29:11 -0700256 private static final int ADD_ACCOUNT_REQUEST_CODE = 1;
Paul Westbrook122f7c22012-08-20 17:50:31 -0700257 private static final int REAUTHENTICATE_REQUEST_CODE = 2;
Paul Westbrook2388c5d2012-03-25 12:29:11 -0700258
Vikram Aggarwale8a85322012-04-24 09:01:38 -0700259 /** The pending destructive action to be carried out before swapping the conversation cursor.*/
260 private DestructiveAction mPendingDestruction;
Andrew Sappersteinc2c9dc12012-07-02 18:17:32 -0700261 protected AsyncRefreshTask mFolderSyncTask;
Mindy Pereira49e5dbe2012-07-12 11:47:54 -0700262 // Task for setting any share intents for the account to enabled.
263 // This gets cancelled if the user kills the app before it finishes, and
264 // will just run the next time the user opens the app.
265 private AsyncTask<String, Void, Void> mEnableShareIntents;
Mindy Pereirac975e842012-07-16 09:15:00 -0700266 private Folder mFolderListFolder;
mindyp5390fca2012-08-22 12:12:25 -0700267 private boolean mIsDragHappening;
mindypead50392012-08-23 11:03:53 -0700268 private int mShowUndoBarDelay;
Andrew Sapperstein00179f12012-08-09 15:15:40 -0700269 public static final String SYNC_ERROR_DIALOG_FRAGMENT_TAG = "SyncErrorDialogFragment";
Vikram Aggarwale8a85322012-04-24 09:01:38 -0700270
Vikram Aggarwala55b36c2012-01-13 11:45:02 -0800271 public AbstractActivityController(MailActivity activity, ViewMode viewMode) {
272 mActivity = activity;
Vikram Aggarwal6902dcf2012-04-11 08:57:42 -0700273 mFragmentManager = mActivity.getFragmentManager();
Vikram Aggarwala55b36c2012-01-13 11:45:02 -0800274 mViewMode = viewMode;
275 mContext = activity.getApplicationContext();
Vikram Aggarwal025eba82012-05-08 10:45:30 -0700276 mRecentFolderList = new RecentFolderList(mContext);
Paul Westbrook937c94f2012-08-16 13:01:18 -0700277 mTracker = new ConversationPositionTracker(this);
Mindy Pereira967ede62012-03-22 09:29:09 -0700278 // Allow the fragment to observe changes to its own selection set. No other object is
279 // aware of the selected set.
280 mSelectedSet.addObserver(this);
Paul Westbrookc7a070f2012-04-12 01:46:41 -0700281
282 mFolderItemUpdateDelayMs =
283 mContext.getResources().getInteger(R.integer.folder_item_refresh_delay_ms);
mindypead50392012-08-23 11:03:53 -0700284 mShowUndoBarDelay =
285 mContext.getResources().getInteger(R.integer.show_undo_bar_delay_ms);
Vikram Aggarwala55b36c2012-01-13 11:45:02 -0800286 }
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -0800287
288 @Override
Vikram Aggarwale9a81032012-02-22 13:15:35 -0800289 public Account getCurrentAccount() {
290 return mAccount;
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800291 }
292
293 @Override
294 public ConversationListContext getCurrentListContext() {
Vikram Aggarwale9a81032012-02-22 13:15:35 -0800295 return mConvListContext;
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800296 }
297
298 @Override
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -0800299 public String getHelpContext() {
Paul Westbrook30745b62012-08-19 14:10:32 -0700300 final int mode = mViewMode.getMode();
301 final int helpContextResId;
302 switch (mode) {
303 case ViewMode.WAITING_FOR_ACCOUNT_INITIALIZATION:
304 helpContextResId = R.string.wait_help_context;
305 break;
306 default:
307 helpContextResId = R.string.main_help_context;
308 }
309 return mContext.getString(helpContextResId);
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -0800310 }
311
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800312 @Override
Vikram Aggarwalef7c9922012-04-23 12:35:04 -0700313 public final ConversationCursor getConversationListCursor() {
Mindy Pereira967ede62012-03-22 09:29:09 -0700314 return mConversationListCursor;
315 }
316
Vikram Aggarwal6902dcf2012-04-11 08:57:42 -0700317 /**
Vikram Aggarwal34a65012012-04-17 12:39:06 -0700318 * Check if the fragment is attached to an activity and has a root view.
319 * @param in
320 * @return true if the fragment is valid, false otherwise
321 */
322 private static final boolean isValidFragment(Fragment in) {
323 if (in == null || in.getActivity() == null || in.getView() == null) {
324 return false;
325 }
326 return true;
327 }
328
329 /**
Vikram Aggarwal6902dcf2012-04-11 08:57:42 -0700330 * Get the conversation list fragment for this activity. If the conversation list fragment
331 * is not attached, this method returns null
Andy Huang839ada22012-07-20 15:48:40 -0700332 *
Vikram Aggarwal6902dcf2012-04-11 08:57:42 -0700333 */
334 protected ConversationListFragment getConversationListFragment() {
335 final Fragment fragment = mFragmentManager.findFragmentByTag(TAG_CONVERSATION_LIST);
Vikram Aggarwal34a65012012-04-17 12:39:06 -0700336 if (isValidFragment(fragment)) {
337 return (ConversationListFragment) 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
342 /**
Vikram Aggarwal6902dcf2012-04-11 08:57:42 -0700343 * Returns the folder list fragment attached with this activity. If no such fragment is attached
344 * this method returns null.
Andy Huang839ada22012-07-20 15:48:40 -0700345 *
Vikram Aggarwal6902dcf2012-04-11 08:57:42 -0700346 */
347 protected FolderListFragment getFolderListFragment() {
348 final Fragment fragment = mFragmentManager.findFragmentByTag(TAG_FOLDER_LIST);
Vikram Aggarwal34a65012012-04-17 12:39:06 -0700349 if (isValidFragment(fragment)) {
350 return (FolderListFragment) fragment;
Andy Huang9585d782012-04-16 19:45:04 -0700351 }
Vikram Aggarwal34a65012012-04-17 12:39:06 -0700352 return null;
Vikram Aggarwal6902dcf2012-04-11 08:57:42 -0700353 }
354
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800355 /**
Mindy Pereira161f50d2012-02-28 15:47:19 -0800356 * Initialize the action bar. This is not visible to OnePaneController and
357 * TwoPaneController so they cannot override this behavior.
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800358 */
Vikram Aggarwalabd24d82012-04-26 13:23:14 -0700359 private void initializeActionBar() {
360 final ActionBar actionBar = mActivity.getActionBar();
Andy Huang5895f7b2012-06-01 17:07:20 -0700361 if (actionBar == null) {
362 return;
Vikram Aggarwalabd24d82012-04-26 13:23:14 -0700363 }
Andy Huang5895f7b2012-06-01 17:07:20 -0700364
365 // be sure to inherit from the ActionBar theme when inflating
366 final LayoutInflater inflater = LayoutInflater.from(actionBar.getThemedContext());
Mindy Pereira82faec72012-06-14 17:21:50 -0700367 final boolean isSearch = mActivity.getIntent() != null
368 && Intent.ACTION_SEARCH.equals(mActivity.getIntent().getAction());
369 mActionBarView = (MailActionBarView) inflater.inflate(
370 isSearch ? R.layout.search_actionbar_view : R.layout.actionbar_view, null);
Andy Huang5895f7b2012-06-01 17:07:20 -0700371 mActionBarView.initialize(mActivity, this, mViewMode, actionBar, mRecentFolderList);
Vikram Aggarwalabd24d82012-04-26 13:23:14 -0700372 }
373
374 /**
375 * Attach the action bar to the activity.
376 */
377 private void attachActionBar() {
378 final ActionBar actionBar = mActivity.getActionBar();
379 if (actionBar != null && mActionBarView != null) {
Vikram Aggarwalec5cbf72012-03-08 15:10:35 -0800380 actionBar.setCustomView(mActionBarView, new ActionBar.LayoutParams(
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800381 LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
Vikram Aggarwal649b9ea2012-08-27 12:15:20 -0700382 // Show a custom view and home icon, but remove the title
383 final int mask = ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_TITLE
384 | ActionBar.DISPLAY_SHOW_HOME;
385 final int enabled = ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME;
386 actionBar.setDisplayOptions(enabled, mask);
Vikram Aggarwalabd24d82012-04-26 13:23:14 -0700387 mActionBarView.attach();
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800388 }
Vikram Aggarwalabd24d82012-04-26 13:23:14 -0700389 mViewMode.addListener(mActionBarView);
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800390 }
391
392 /**
Mindy Pereira161f50d2012-02-28 15:47:19 -0800393 * Returns whether the conversation list fragment is visible or not.
394 * Different layouts will have their own notion on the visibility of
395 * fragments, so this method needs to be overriden.
396 *
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800397 */
398 protected abstract boolean isConversationListVisible();
399
Vikram Aggarwaleb7d3292012-04-20 17:07:20 -0700400 /**
401 * Switch the current account to the one provided as an argument to the method.
Vikram Aggarwal66bc6ed2012-07-31 09:59:55 -0700402 * @param account new account
403 * @param shouldReloadInbox whether the default inbox should be reloaded.
Vikram Aggarwaleb7d3292012-04-20 17:07:20 -0700404 */
Vikram Aggarwal66bc6ed2012-07-31 09:59:55 -0700405 private void switchAccount(Account account, boolean shouldReloadInbox){
Vikram Aggarwaleb7d3292012-04-20 17:07:20 -0700406 // Current account is different from the new account, restart loaders and show
407 // the account Inbox.
408 mAccount = account;
Vikram Aggarwaledc137c2012-04-24 13:40:58 -0700409 LogUtils.d(LOG_TAG, "AbstractActivityController.switchAccount(): mAccount = %s",
410 mAccount.uri);
Vikram Aggarwaleb7d3292012-04-20 17:07:20 -0700411 cancelRefreshTask();
Vikram Aggarwal2e0a2362012-08-22 09:05:49 -0700412 // If we are currently listening to changes in conversation, the account switch is going to
413 // make us do a lot of un-necessary work. Let's stop listening to changes.
414 if (mPagerController != null) {
415 mPagerController.stopListening();
416 }
Vikram Aggarwal60069912012-07-24 14:26:09 -0700417 updateSettings();
Vikram Aggarwal66bc6ed2012-07-31 09:59:55 -0700418 if (shouldReloadInbox) {
419 loadAccountInbox();
420 }
Vikram Aggarwaleb7d3292012-04-20 17:07:20 -0700421 restartOptionalLoader(LOADER_RECENT_FOLDERS);
422 mActivity.invalidateOptionsMenu();
423 disableNotificationsOnAccountChange(mAccount);
424 restartOptionalLoader(LOADER_ACCOUNT_UPDATE_CURSOR);
425 MailAppProvider.getInstance().setLastViewedAccount(mAccount.uri.toString());
426 }
427
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800428 @Override
Mindy Pereira28e0c342012-02-17 15:05:13 -0800429 public void onAccountChanged(Account account) {
Vikram Aggarwal60069912012-07-24 14:26:09 -0700430 LogUtils.d(LOG_TAG, "onAccountChanged (%s) called.", account);
431 // Is the account or account settings different from the existing account?
Vikram Aggarwal66bc6ed2012-07-31 09:59:55 -0700432 final boolean firstLoad = mAccount == null;
433 final boolean accountChanged = firstLoad || !account.uri.equals(mAccount.uri);
434 final boolean settingsChanged = firstLoad || !account.settings.equals(mAccount.settings);
435 if (accountChanged || settingsChanged) {
Mindy Pereirafa20c1a2012-07-23 13:00:02 -0700436 if (account != null) {
Mindy Pereirafa995b42012-07-25 12:06:13 -0700437 final String accountName = account.name;
438 mHandler.post(new Runnable() {
439 @Override
440 public void run() {
441 MailActivity.setForegroundNdef(MailActivity.getMailtoNdef(accountName));
442 }
443 });
Mindy Pereirafa20c1a2012-07-23 13:00:02 -0700444 }
mindypc6adce32012-08-22 18:46:42 -0700445 if (accountChanged) {
446 commitDestructiveActions(false);
447 }
Vikram Aggarwal66bc6ed2012-07-31 09:59:55 -0700448 switchAccount(account, accountChanged);
Mindy Pereira28d5f722012-02-15 12:32:40 -0800449 }
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800450 }
451
Vikram Aggarwale6340bc2012-03-26 15:57:09 -0700452 /**
Vikram Aggarwaleb7d3292012-04-20 17:07:20 -0700453 * Changes the settings for the current account. The new settings are provided as a parameter.
454 * @param settings
455 */
Vikram Aggarwal60069912012-07-24 14:26:09 -0700456 public void updateSettings() {
Vikram Aggarwal7c401b72012-08-13 16:43:47 -0700457 mAccountObservers.notifyChanged();
Mindy Pereira12a676a2012-03-23 13:00:22 -0700458 resetActionBarIcon();
Vikram Aggarwaleb7d3292012-04-20 17:07:20 -0700459 mActivity.invalidateOptionsMenu();
460 // If the user was viewing the default Inbox here, and the new setting contains a different
Vikram Aggarwal60069912012-07-24 14:26:09 -0700461 // default Inbox, we don't want to load a different folder here. So do not change the
462 // current folder.
Mindy Pereiradac00542012-03-01 10:50:33 -0800463 }
464
Vikram Aggarwal7c401b72012-08-13 16:43:47 -0700465 /**
466 * Adds a listener interested in change in the current account. If a class is storing a
467 * reference to the current account, it should listen on changes, so it can receive updates to
468 * settings. Must happen in the UI thread.
469 */
Mindy Pereiraefe3d252012-03-01 14:20:44 -0800470 @Override
Vikram Aggarwal7c401b72012-08-13 16:43:47 -0700471 public void registerAccountObserver(DataSetObserver obs) {
472 mAccountObservers.registerObserver(obs);
Mindy Pereiraefe3d252012-03-01 14:20:44 -0800473 }
474
Vikram Aggarwal7d816002012-04-17 17:06:41 -0700475 /**
Vikram Aggarwal7c401b72012-08-13 16:43:47 -0700476 * Removes a listener from receiving current account changes.
Vikram Aggarwal7d816002012-04-17 17:06:41 -0700477 * Must happen in the UI thread.
478 */
Vikram Aggarwal7c401b72012-08-13 16:43:47 -0700479 @Override
480 public void unregisterAccountObserver(DataSetObserver obs) {
481 mAccountObservers.unregisterObserver(obs);
Vikram Aggarwal7d816002012-04-17 17:06:41 -0700482 }
483
Vikram Aggarwal7c401b72012-08-13 16:43:47 -0700484 @Override
485 public Account getAccount() {
486 return mAccount;
Vikram Aggarwal7d816002012-04-17 17:06:41 -0700487 }
488
Mindy Pereirae0828392012-03-08 10:38:40 -0800489 private void fetchSearchFolder(Intent intent) {
Mindy Pereiraab486362012-03-21 18:18:53 -0700490 Bundle args = new Bundle();
491 args.putString(ConversationListContext.EXTRA_SEARCH_QUERY, intent
Mindy Pereirae0828392012-03-08 10:38:40 -0800492 .getStringExtra(ConversationListContext.EXTRA_SEARCH_QUERY));
Mindy Pereiraab486362012-03-21 18:18:53 -0700493 mActivity.getLoaderManager().restartLoader(LOADER_SEARCH, args, this);
Mindy Pereirae0828392012-03-08 10:38:40 -0800494 }
495
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800496 @Override
Mindy Pereira28e0c342012-02-17 15:05:13 -0800497 public void onFolderChanged(Folder folder) {
Vikram Aggarwalf3341402012-08-07 10:09:38 -0700498 changeFolder(folder, null);
499 }
500
501 /**
Vikram Aggarwal203dc002012-08-23 13:59:04 -0700502 * Sets the folder state without changing view mode and without creating a list fragment, if
503 * possible.
504 * @param folder
505 */
506 private void setListContext(Folder folder, String query) {
507 updateFolder(folder);
508 if (query != null) {
509 mConvListContext = ConversationListContext.forSearchQuery(mAccount, mFolder, query);
510 } else {
511 mConvListContext = ConversationListContext.forFolder(mAccount, mFolder);
512 }
Vikram Aggarwal203dc002012-08-23 13:59:04 -0700513 cancelRefreshTask();
514 }
515
516 /**
Vikram Aggarwalf3341402012-08-07 10:09:38 -0700517 * Changes the folder to the value provided here. This causes the view mode to change.
518 * @param folder the folder to change to
519 * @param query if non-null, this represents the search string that the folder represents.
520 */
521 private void changeFolder(Folder folder, String query) {
Vikram Aggarwal203dc002012-08-23 13:59:04 -0700522 if (!Objects.equal(mFolder, folder)) {
523 commitDestructiveActions(false);
524 }
Mindy Pereiraa1f99982012-07-16 16:32:15 -0700525 if (folder != null && !folder.equals(mFolder)
526 || (mViewMode.getMode() != ViewMode.CONVERSATION_LIST)) {
Vikram Aggarwal203dc002012-08-23 13:59:04 -0700527 setListContext(folder, query);
Mindy Pereira28e0c342012-02-17 15:05:13 -0800528 showConversationList(mConvListContext);
Mindy Pereira28e0c342012-02-17 15:05:13 -0800529 }
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800530 }
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);
Vikram Aggarwal649b9ea2012-08-27 12:15:20 -0700683 setListContext(folder, query);
Andy Huang632721e2012-04-11 16:57:26 -0700684 }
Vikram Aggarwal649b9ea2012-08-27 12:15:20 -0700685 mViewMode.handleRestore(savedState);
Andy Huang632721e2012-04-11 16:57:26 -0700686 } else if (intent != null) {
687 handleIntent(intent);
688 }
Andy Huang632721e2012-04-11 16:57:26 -0700689 // Create the accounts loader; this loads the account switch spinner.
690 mActivity.getLoaderManager().initLoader(LOADER_ACCOUNT_CURSOR, null, this);
Andy Huang632721e2012-04-11 16:57:26 -0700691 return true;
Andy Huangb1c34dc2012-04-17 16:36:19 -0700692 }
693
694 @Override
Andy Huang1ee96b22012-08-24 20:19:53 -0700695 public void onStart() {
696 mSafeToModifyFragments = true;
697 }
698
699 @Override
Andrew Sapperstein00179f12012-08-09 15:15:40 -0700700 public void onRestart() {
701 DialogFragment fragment = (DialogFragment)
702 mFragmentManager.findFragmentByTag(SYNC_ERROR_DIALOG_FRAGMENT_TAG);
703 if (fragment != null) {
704 fragment.dismiss();
705 }
mindypea04f932012-08-27 14:17:59 -0700706 // When the user places the app in the background by pressing "home",
707 // dismiss the toast bar. However, since there is no way to determine if
708 // home was pressed, just dismiss any existing toast bar when restarting
709 // the app.
710 if (mToastBar != null) {
711 mToastBar.hide(false);
712 }
Andrew Sapperstein00179f12012-08-09 15:15:40 -0700713 }
714
715 @Override
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800716 public Dialog onCreateDialog(int id, Bundle bundle) {
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -0800717 return null;
Vikram Aggarwala55b36c2012-01-13 11:45:02 -0800718 }
719
720 @Override
Vikram Aggarwal4eb52712012-06-19 16:24:50 -0700721 public final boolean onCreateOptionsMenu(Menu menu) {
Mindy Pereira28d5f722012-02-15 12:32:40 -0800722 MenuInflater inflater = mActivity.getMenuInflater();
Mindy Pereiraf5acda42012-02-15 20:13:59 -0800723 inflater.inflate(mActionBarView.getOptionsMenuId(), menu);
Mindy Pereira68f2e222012-03-07 10:36:54 -0800724 mActionBarView.onCreateOptionsMenu(menu);
Mindy Pereira28d5f722012-02-15 12:32:40 -0800725 return true;
Vikram Aggarwala55b36c2012-01-13 11:45:02 -0800726 }
727
728 @Override
Vikram Aggarwal4eb52712012-06-19 16:24:50 -0700729 public final boolean onKeyDown(int keyCode, KeyEvent event) {
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -0800730 // TODO(viki): Auto-generated method stub
731 return false;
732 }
733
734 @Override
Vikram Aggarwal4eb52712012-06-19 16:24:50 -0700735 public final boolean onOptionsItemSelected(MenuItem item) {
Vikram Aggarwal54452ae2012-03-13 15:29:00 -0700736 final int id = item.getItemId();
Vikram Aggarwal7dd054e2012-05-21 14:43:10 -0700737 LogUtils.d(LOG_TAG, "AbstractController.onOptionsItemSelected(%d) called.", id);
Mindy Pereira9b875682012-02-15 18:10:54 -0800738 boolean handled = true;
Mindy Pereiraba68fda2012-05-24 15:53:06 -0700739 final Collection<Conversation> target = Conversation.listOf(mCurrentConversation);
Vikram Aggarwal112cd162012-06-18 10:51:13 -0700740 final Settings settings = (mAccount == null) ? null : mAccount.settings;
Mindy Pereira8937bf12012-07-23 14:05:02 -0700741 // The user is choosing a new action; commit whatever they had been doing before.
mindypc6adce32012-08-22 18:46:42 -0700742 commitDestructiveActions(true);
Mindy Pereira28d5f722012-02-15 12:32:40 -0800743 switch (id) {
Mindy Pereiraba68fda2012-05-24 15:53:06 -0700744 case R.id.archive: {
745 final boolean showDialog = (settings != null && settings.confirmArchive);
746 confirmAndDelete(target, showDialog, R.plurals.confirm_archive_conversation,
747 getAction(R.id.archive, target));
748 break;
749 }
Mindy Pereira01f30502012-08-14 10:30:51 -0700750 case R.id.remove_folder:
751 delete(target, getRemoveFolder(target, mFolder, true, false, true));
752 break;
Mindy Pereiraba68fda2012-05-24 15:53:06 -0700753 case R.id.delete: {
754 final boolean showDialog = (settings != null && settings.confirmDelete);
755 confirmAndDelete(target, showDialog, R.plurals.confirm_delete_conversation,
756 getAction(R.id.delete, target));
757 break;
758 }
Paul Westbrookef362542012-08-27 14:53:32 -0700759 case R.id.discard_drafts:
760 final boolean showDialog = (settings != null && settings.confirmDelete);
761 confirmAndDelete(target, showDialog, R.plurals.confirm_discard_drafts_conversation,
762 getAction(R.id.discard_drafts, target));
763 break;
Mindy Pereiraba68fda2012-05-24 15:53:06 -0700764 case R.id.mark_important:
Vikram Aggarwal531488e2012-05-29 16:36:52 -0700765 updateConversation(Conversation.listOf(mCurrentConversation),
766 ConversationColumns.PRIORITY, UIProvider.ConversationPriority.HIGH);
Mindy Pereiraba68fda2012-05-24 15:53:06 -0700767 break;
768 case R.id.mark_not_important:
Mindy Pereira0d03ef82012-08-15 09:05:48 -0700769 if (mFolder != null && mFolder.isImportantOnly()) {
770 delete(target, getAction(R.id.mark_not_important, target));
771 } else {
772 updateConversation(Conversation.listOf(mCurrentConversation),
773 ConversationColumns.PRIORITY, UIProvider.ConversationPriority.LOW);
774 }
Mindy Pereiraba68fda2012-05-24 15:53:06 -0700775 break;
776 case R.id.mute:
Vikram Aggarwal4f4782b2012-05-30 08:39:09 -0700777 delete(target, getAction(R.id.mute, target));
Mindy Pereiraba68fda2012-05-24 15:53:06 -0700778 break;
779 case R.id.report_spam:
Vikram Aggarwal4f4782b2012-05-30 08:39:09 -0700780 delete(target, getAction(R.id.report_spam, target));
Mindy Pereiraba68fda2012-05-24 15:53:06 -0700781 break;
Paul Westbrook77eee622012-07-10 13:41:57 -0700782 case R.id.mark_not_spam:
783 // Currently, since spam messages are only shown in list with other spam messages,
784 // marking a message not as spam is a destructive action
785 delete(target, getAction(R.id.mark_not_spam, target));
786 break;
Paul Westbrook76b20622012-07-12 11:45:43 -0700787 case R.id.report_phishing:
788 delete(target, getAction(R.id.report_phishing, target));
789 break;
Mindy Pereiraf5acda42012-02-15 20:13:59 -0800790 case android.R.id.home:
791 onUpPressed();
792 break;
Mindy Pereira9b875682012-02-15 18:10:54 -0800793 case R.id.compose:
794 ComposeActivity.compose(mActivity.getActivityContext(), mAccount);
795 break;
Mindy Pereira28d5f722012-02-15 12:32:40 -0800796 case R.id.show_all_folders:
797 showFolderList();
798 break;
Mindy Pereira28e0c342012-02-17 15:05:13 -0800799 case R.id.refresh:
800 requestFolderRefresh();
801 break;
Mindy Pereira1f936682012-03-02 11:30:33 -0800802 case R.id.settings:
803 Utils.showSettings(mActivity.getActivityContext(), mAccount);
Paul Westbrook2861b6a2012-02-15 15:25:34 -0800804 break;
Paul Westbrooke5503552012-03-28 00:35:57 -0700805 case R.id.folder_options:
806 Utils.showFolderSettings(mActivity.getActivityContext(), mAccount, mFolder);
807 break;
Paul Westbrook94e440d2012-02-24 11:03:47 -0800808 case R.id.help_info_menu_item:
Paul Westbrook30745b62012-08-19 14:10:32 -0700809 Utils.showHelp(mActivity.getActivityContext(), mAccount, getHelpContext());
Paul Westbrook94e440d2012-02-24 11:03:47 -0800810 break;
Vikram Aggarwal54452ae2012-03-13 15:29:00 -0700811 case R.id.feedback_menu_item:
Paul Westbrook17beb0b2012-08-20 13:34:37 -0700812 Utils.sendFeedback(mActivity.getActivityContext(), mAccount, false);
Mindy Pereirafbe40192012-03-20 10:40:45 -0700813 break;
Paul Westbrook18babd22012-04-09 22:17:08 -0700814 case R.id.manage_folders_item:
815 Utils.showManageFolder(mActivity.getActivityContext(), mAccount);
816 break;
Vikram Aggarwald503df42012-05-11 10:13:35 -0700817 case R.id.change_folder:
818 new FoldersSelectionDialog(mActivity.getActivityContext(), mAccount, this,
Mindy Pereiraa832d3d2012-07-27 11:19:50 -0700819 Conversation.listOf(mCurrentConversation), false, mFolder).show();
Vikram Aggarwald503df42012-05-11 10:13:35 -0700820 break;
Mindy Pereira9b875682012-02-15 18:10:54 -0800821 default:
822 handled = false;
823 break;
Mindy Pereira28d5f722012-02-15 12:32:40 -0800824 }
Mindy Pereira9b875682012-02-15 18:10:54 -0800825 return handled;
Vikram Aggarwala55b36c2012-01-13 11:45:02 -0800826 }
827
Vikram Aggarwal531488e2012-05-29 16:36:52 -0700828 @Override
Mindy Pereira6c2663d2012-07-20 15:37:29 -0700829 public void updateConversation(Collection<Conversation> target, ContentValues values) {
830 mConversationListCursor.updateValues(mContext, target, values);
831 refreshConversationList();
832 }
833
834 @Override
Vikram Aggarwal531488e2012-05-29 16:36:52 -0700835 public void updateConversation(Collection <Conversation> target, String columnName,
836 boolean value) {
837 mConversationListCursor.updateBoolean(mContext, target, columnName, value);
Vikram Aggarwal75daee52012-04-30 11:13:09 -0700838 refreshConversationList();
Vikram Aggarwal54452ae2012-03-13 15:29:00 -0700839 }
840
Vikram Aggarwal531488e2012-05-29 16:36:52 -0700841 @Override
Mindy Pereira6c2663d2012-07-20 15:37:29 -0700842 public void updateConversation(Collection <Conversation> target, String columnName,
843 int value) {
Vikram Aggarwal531488e2012-05-29 16:36:52 -0700844 mConversationListCursor.updateInt(mContext, target, columnName, value);
Vikram Aggarwal75daee52012-04-30 11:13:09 -0700845 refreshConversationList();
Mindy Pereirac9d59182012-03-22 16:06:46 -0700846 }
847
Vikram Aggarwal531488e2012-05-29 16:36:52 -0700848 @Override
849 public void updateConversation(Collection <Conversation> target, String columnName,
850 String value) {
851 mConversationListCursor.updateString(mContext, target, columnName, value);
Vikram Aggarwal75daee52012-04-30 11:13:09 -0700852 refreshConversationList();
Vikram Aggarwal54452ae2012-03-13 15:29:00 -0700853 }
854
Andy Huang839ada22012-07-20 15:48:40 -0700855 @Override
856 public void markConversationMessagesUnread(Conversation conv, Set<Uri> unreadMessageUris,
Vikram Aggarwal4a878b62012-07-31 15:09:25 -0700857 String originalConversationInfo) {
Andy Huang8f6b0062012-07-31 15:36:31 -0700858 // The only caller of this method is the conversation view, from where marking unread should
859 // *always* take you back to list mode.
860 showConversation(null);
861
Andy Huang839ada22012-07-20 15:48:40 -0700862 // locally mark conversation unread (the provider is supposed to propagate message unread
863 // to conversation unread)
864 conv.read = false;
865
Andy Huang28e31e22012-07-26 16:33:15 -0700866 // only do a granular 'mark unread' if a subset of messages are unread
867 final int unreadCount = (unreadMessageUris == null) ? 0 : unreadMessageUris.size();
Mindy Pereira0972e072012-08-01 17:43:06 -0700868 final int numMessages = conv.getNumMessages();
869 final boolean subsetIsUnread = (numMessages > 1 && unreadCount > 0
870 && unreadCount < numMessages);
Andy Huang28e31e22012-07-26 16:33:15 -0700871
872 if (!subsetIsUnread) {
Vikram Aggarwal66bc2aa2012-08-02 10:47:03 -0700873 // Conversations are neither marked read, nor viewed, and we don't want to show
874 // the next conversation.
875 markConversationsRead(Collections.singletonList(conv), false, false, false);
Andy Huang839ada22012-07-20 15:48:40 -0700876 } else {
Andy Huangdaa06ab2012-07-24 10:46:44 -0700877 mConversationListCursor.setConversationColumn(conv.uri, ConversationColumns.READ, 0);
Andy Huang839ada22012-07-20 15:48:40 -0700878
Mindy Pereira7b6d03d2012-07-30 13:03:41 -0700879 // locally update conversation's conversationInfo to revert to original version
Andy Huang28e31e22012-07-26 16:33:15 -0700880 if (originalConversationInfo != null) {
881 mConversationListCursor.setConversationColumn(conv.uri,
882 ConversationColumns.CONVERSATION_INFO, originalConversationInfo);
883 }
Andy Huang839ada22012-07-20 15:48:40 -0700884
885 // applyBatch with each CPO as an UPDATE op on each affected message uri
886 final ArrayList<ContentProviderOperation> ops = Lists.newArrayList();
887 String authority = null;
888 for (Uri messageUri : unreadMessageUris) {
889 if (authority == null) {
890 authority = messageUri.getAuthority();
891 }
892 ops.add(ContentProviderOperation.newUpdate(messageUri)
893 .withValue(UIProvider.MessageColumns.READ, 0)
894 .build());
895 }
896
897 new ContentProviderTask() {
898 @Override
899 protected void onPostExecute(Result result) {
900 // TODO: handle errors?
901 }
902 }.run(mResolver, authority, ops);
Andy Huang839ada22012-07-20 15:48:40 -0700903 }
Andy Huang839ada22012-07-20 15:48:40 -0700904 }
905
906 @Override
Vikram Aggarwal66bc2aa2012-08-02 10:47:03 -0700907 public void markConversationsRead(Collection<Conversation> targets, boolean read,
908 boolean viewed) {
909 // We want to show the next conversation if we are marking unread.
910 markConversationsRead(targets, read, viewed, true);
Andy Huang8f6b0062012-07-31 15:36:31 -0700911 }
912
913 private void markConversationsRead(Collection<Conversation> targets, boolean read,
Vikram Aggarwal66bc2aa2012-08-02 10:47:03 -0700914 boolean markViewed, boolean showNext) {
Andy Huang8f6b0062012-07-31 15:36:31 -0700915 // auto-advance if requested and the current conversation is being marked unread
916 if (showNext && !read) {
917 showNextConversation(targets);
918 }
919
Andy Huang839ada22012-07-20 15:48:40 -0700920 for (Conversation target : targets) {
Vikram Aggarwal192fac12012-07-25 16:44:55 -0700921 final ContentValues values = new ContentValues();
Andy Huang839ada22012-07-20 15:48:40 -0700922 values.put(ConversationColumns.READ, read);
Vikram Aggarwal66bc2aa2012-08-02 10:47:03 -0700923 if (markViewed) {
924 values.put(ConversationColumns.VIEWED, true);
925 }
Vikram Aggarwal192fac12012-07-25 16:44:55 -0700926 final ConversationInfo info = target.conversationInfo;
Andy Huang839ada22012-07-20 15:48:40 -0700927 if (info != null) {
Mindy Pereira5f424372012-07-30 11:49:55 -0700928 info.markRead(read);
929 values.put(ConversationColumns.CONVERSATION_INFO, ConversationInfo.toString(info));
Andy Huang839ada22012-07-20 15:48:40 -0700930 }
931 updateConversation(Conversation.listOf(target), values);
932 }
933 // Update the conversations in the selection too.
934 for (final Conversation c : targets) {
935 c.read = read;
Andy Huangcd5c5ee2012-08-12 19:03:51 -0700936 if (markViewed) {
937 c.markViewed();
938 }
Andy Huang839ada22012-07-20 15:48:40 -0700939 }
940 }
941
Andy Huang8f6b0062012-07-31 15:36:31 -0700942 /**
943 * Auto-advance to a different conversation if the currently visible conversation in
944 * conversation mode is affected (deleted, marked unread, etc.).
945 *
946 * <p>Does nothing if outside of conversation mode.
947 *
948 * @param target the set of conversations being deleted/marked unread
949 */
950 private void showNextConversation(Collection<Conversation> target) {
951 final boolean currentConversationInView = (mViewMode.getMode() == ViewMode.CONVERSATION)
952 && Conversation.contains(target, mCurrentConversation);
953 if (currentConversationInView) {
954 final Conversation next = mTracker.getNextConversation(
955 Settings.getAutoAdvanceSetting(mAccount.settings), target);
956 LogUtils.d(LOG_TAG, "showNextConversation: showing %s next.", next);
957 showConversation(next);
958 }
959 }
960
Andy Huang839ada22012-07-20 15:48:40 -0700961 @Override
962 public void starMessage(ConversationMessage msg, boolean starred) {
963 if (msg.starred == starred) {
964 return;
965 }
966
967 msg.starred = starred;
968
969 // locally propagate the change to the owning conversation
970 // (figure the provider will properly propagate the change when it commits it)
971 //
972 // when unstarring, only propagate the change if this was the only message starred
973 final boolean conversationStarred = starred || msg.isConversationStarred();
974 if (conversationStarred != msg.conversation.starred) {
975 msg.conversation.starred = conversationStarred;
Andy Huangdaa06ab2012-07-24 10:46:44 -0700976 mConversationListCursor.setConversationColumn(msg.conversation.uri,
Andy Huang839ada22012-07-20 15:48:40 -0700977 ConversationColumns.STARRED, conversationStarred);
978 }
979
980 final ContentValues values = new ContentValues(1);
981 values.put(UIProvider.MessageColumns.STARRED, starred ? 1 : 0);
982
983 new ContentProviderTask.UpdateTask() {
984 @Override
985 protected void onPostExecute(Result result) {
986 // TODO: handle errors?
987 }
988 }.run(mResolver, msg.uri, values, null /* selection*/, null /* selectionArgs */);
989 }
990
Mindy Pereira28e0c342012-02-17 15:05:13 -0800991 private void requestFolderRefresh() {
992 if (mFolder != null) {
Mindy Pereirab7b33e02012-02-21 15:32:19 -0800993 if (mAsyncRefreshTask != null) {
994 mAsyncRefreshTask.cancel(true);
995 }
Paul Westbrook7e2a2a12012-06-27 13:52:40 -0700996 mAsyncRefreshTask = new AsyncRefreshTask(mContext, mFolder.refreshUri);
Mindy Pereirab7b33e02012-02-21 15:32:19 -0800997 mAsyncRefreshTask.execute();
Mindy Pereira28e0c342012-02-17 15:05:13 -0800998 }
999 }
1000
Mindy Pereirafbe40192012-03-20 10:40:45 -07001001 /**
1002 * Confirm (based on user's settings) and delete a conversation from the conversation list and
1003 * from the database.
Vikram Aggarwal3d7ca9d2012-05-11 14:40:36 -07001004 * @param target the conversations to act upon
1005 * @param showDialog true if a confirmation dialog is to be shown, false otherwise.
1006 * @param confirmResource the resource ID of the string that is shown in the confirmation dialog
1007 * @param action the action to perform after animating the deletion of the conversations.
Mindy Pereirafbe40192012-03-20 10:40:45 -07001008 */
Vikram Aggarwal7f602f72012-04-30 16:04:06 -07001009 protected void confirmAndDelete(final Collection<Conversation> target, boolean showDialog,
1010 int confirmResource, final DestructiveAction action) {
Mindy Pereirafbe40192012-03-20 10:40:45 -07001011 if (showDialog) {
1012 final AlertDialog.OnClickListener onClick = new AlertDialog.OnClickListener() {
1013 @Override
1014 public void onClick(DialogInterface dialog, int which) {
Vikram Aggarwal4f4782b2012-05-30 08:39:09 -07001015 delete(target, action);
Mindy Pereirafbe40192012-03-20 10:40:45 -07001016 }
1017 };
Vikram Aggarwal3d7ca9d2012-05-11 14:40:36 -07001018 final CharSequence message = Utils.formatPlural(mContext, confirmResource,
1019 target.size());
Mindy Pereirafbe40192012-03-20 10:40:45 -07001020 new AlertDialog.Builder(mActivity.getActivityContext()).setMessage(message)
1021 .setPositiveButton(R.string.ok, onClick)
1022 .setNegativeButton(R.string.cancel, null)
1023 .create().show();
1024 } else {
Vikram Aggarwal4f4782b2012-05-30 08:39:09 -07001025 delete(target, action);
Mindy Pereirafbe40192012-03-20 10:40:45 -07001026 }
1027 }
1028
Vikram Aggarwal4f4782b2012-05-30 08:39:09 -07001029 @Override
1030 public void delete(final Collection<Conversation> target, final DestructiveAction action) {
Vikram Aggarwal192fac12012-07-25 16:44:55 -07001031 // Order of events is critical! The Conversation View Fragment must be notified
1032 // of the next conversation with showConversation(next) *before* the conversation list
1033 // fragment has a chance to delete the conversation, animating it away.
1034
Vikram Aggarwald503df42012-05-11 10:13:35 -07001035 // Update the conversation fragment if the current conversation is deleted.
Andy Huang8f6b0062012-07-31 15:36:31 -07001036 showNextConversation(target);
Vikram Aggarwal192fac12012-07-25 16:44:55 -07001037 // The conversation list deletes and performs the action if it exists.
1038 final ConversationListFragment convListFragment = getConversationListFragment();
1039 if (convListFragment != null) {
1040 LogUtils.d(LOG_TAG, "AAC.requestDelete: ListFragment is handling delete.");
1041 convListFragment.requestDelete(target, action);
1042 return;
1043 }
Vikram Aggarwald503df42012-05-11 10:13:35 -07001044 // No visible UI element handled it on our behalf. Perform the action ourself.
1045 action.performAction();
1046 }
1047
1048 /**
1049 * Requests that the action be performed and the UI state is updated to reflect the new change.
1050 * @param target
1051 * @param action
1052 */
Vikram Aggarwal4f4782b2012-05-30 08:39:09 -07001053 private void requestUpdate(final Collection<Conversation> target,
Vikram Aggarwald503df42012-05-11 10:13:35 -07001054 final DestructiveAction action) {
1055 action.performAction();
1056 refreshConversationList();
Vikram Aggarwal75daee52012-04-30 11:13:09 -07001057 }
Vikram Aggarwal6fbc87a2012-03-15 15:24:00 -07001058
1059 @Override
Vikram Aggarwala55b36c2012-01-13 11:45:02 -08001060 public void onPrepareDialog(int id, Dialog dialog, Bundle bundle) {
1061 // TODO(viki): Auto-generated method stub
Vikram Aggarwala55b36c2012-01-13 11:45:02 -08001062 }
1063
1064 @Override
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -08001065 public boolean onPrepareOptionsMenu(Menu menu) {
Mindy Pereirab849dfb2012-03-07 18:13:15 -08001066 mActionBarView.onPrepareOptionsMenu(menu);
Mindy Pereira363451a2012-02-22 14:14:46 -08001067 return true;
Vikram Aggarwala55b36c2012-01-13 11:45:02 -08001068 }
1069
Mindy Pereira68f2e222012-03-07 10:36:54 -08001070 @Override
1071 public void onPause() {
1072 isLoaderInitialized = false;
Paul Westbrook6ead20d2012-03-19 14:48:14 -07001073 enableNotifications();
Paul Westbrook94e440d2012-02-24 11:03:47 -08001074 }
1075
Vikram Aggarwala55b36c2012-01-13 11:45:02 -08001076 @Override
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -08001077 public void onResume() {
Paul Westbrook6ead20d2012-03-19 14:48:14 -07001078 // Register the receiver that will prevent the status receiver from
1079 // displaying its notification icon as long as we're running.
1080 // The SupressNotificationReceiver will block the broadcast if we're looking at the folder
1081 // that the notification was received for.
1082 disableNotifications();
Andy Huang1ee96b22012-08-24 20:19:53 -07001083
1084 mSafeToModifyFragments = true;
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -08001085 }
1086
1087 @Override
1088 public void onSaveInstanceState(Bundle outState) {
Vikram Aggarwal649b9ea2012-08-27 12:15:20 -07001089 mViewMode.handleSaveInstanceState(outState);
Vikram Aggarwal6c511582012-02-27 10:59:47 -08001090 if (mAccount != null) {
1091 LogUtils.d(LOG_TAG, "Saving the account now");
1092 outState.putParcelable(SAVED_ACCOUNT, mAccount);
1093 }
Mindy Pereira5e478d22012-03-26 18:04:58 -07001094 if (mFolder != null) {
1095 outState.putParcelable(SAVED_FOLDER, mFolder);
Vikram Aggarwal8b152632012-02-03 14:58:45 -08001096 }
Vikram Aggarwalf3341402012-08-07 10:09:38 -07001097 // If this is a search activity, let's store the search query term as well.
1098 if (ConversationListContext.isSearchResult(mConvListContext)) {
1099 outState.putString(SAVED_QUERY, mConvListContext.searchQuery);
1100 }
Vikram Aggarwal649b9ea2012-08-27 12:15:20 -07001101 final int mode = mViewMode.getMode();
Mindy Pereira9fa43ca2012-05-17 14:18:01 -07001102 if (mCurrentConversation != null
Mindy Pereira0f7ae7a2012-07-17 13:39:56 -07001103 && (mode == ViewMode.CONVERSATION ||
Mindy Pereira9fa43ca2012-05-17 14:18:01 -07001104 mViewMode.getMode() == ViewMode.SEARCH_RESULTS_CONVERSATION)) {
Mindy Pereira26f23fc2012-03-27 10:26:04 -07001105 outState.putParcelable(SAVED_CONVERSATION, mCurrentConversation);
1106 }
Andy Huang4556a442012-03-30 16:42:05 -07001107 if (!mSelectedSet.isEmpty()) {
Vikram Aggarwalcabd3f22012-04-19 10:14:41 -07001108 outState.putParcelable(SAVED_SELECTED_SET, mSelectedSet);
Andy Huang4556a442012-03-30 16:42:05 -07001109 }
Mindy Pereirad33674992012-06-25 16:26:30 -07001110 if (mToastBar.getVisibility() == View.VISIBLE) {
1111 outState.putParcelable(SAVED_TOAST_BAR_OP, mToastBar.getOperation());
1112 }
Vikram Aggarwal649b9ea2012-08-27 12:15:20 -07001113 final ConversationListFragment convListFragment = getConversationListFragment();
Mindy Pereirad33674992012-06-25 16:26:30 -07001114 if (convListFragment != null) {
Vikram Aggarwal649b9ea2012-08-27 12:15:20 -07001115 convListFragment.getAnimatedAdapter().onSaveInstanceState(outState);
Mindy Pereirad33674992012-06-25 16:26:30 -07001116 }
Andy Huang1ee96b22012-08-24 20:19:53 -07001117 mSafeToModifyFragments = false;
Vikram Aggarwal649b9ea2012-08-27 12:15:20 -07001118 outState.putString(SAVED_HIERARCHICAL_FOLDER,
1119 (mFolderListFolder != null) ? Folder.toString(mFolderListFolder) : null);
Andy Huang1ee96b22012-08-24 20:19:53 -07001120 }
1121
1122 /**
1123 * @see #mSafeToModifyFragments
1124 */
1125 protected boolean safeToModifyFragments() {
1126 return mSafeToModifyFragments;
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -08001127 }
1128
1129 @Override
Mindy Pereira68f2e222012-03-07 10:36:54 -08001130 public void onSearchRequested(String query) {
1131 Intent intent = new Intent();
1132 intent.setAction(Intent.ACTION_SEARCH);
1133 intent.putExtra(ConversationListContext.EXTRA_SEARCH_QUERY, query);
1134 intent.putExtra(Utils.EXTRA_ACCOUNT, mAccount);
1135 intent.setComponent(mActivity.getComponentName());
Vikram Aggarwalb17cbc02012-04-06 15:41:46 -07001136 mActionBarView.collapseSearch();
Mindy Pereira68f2e222012-03-07 10:36:54 -08001137 mActivity.startActivity(intent);
Vikram Aggarwala55b36c2012-01-13 11:45:02 -08001138 }
1139
1140 @Override
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -08001141 public void onStop() {
Mindy Pereira49e5dbe2012-07-12 11:47:54 -07001142 if (mEnableShareIntents != null) {
1143 mEnableShareIntents.cancel(true);
1144 }
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -08001145 }
1146
Andy Huang632721e2012-04-11 16:57:26 -07001147 @Override
1148 public void onDestroy() {
1149 // unregister the ViewPager's observer on the conversation cursor
1150 mPagerController.onDestroy();
Mindy Pereira641de652012-08-02 15:21:50 -07001151 mActionBarView.onDestroy();
Vikram Aggarwal7c401b72012-08-13 16:43:47 -07001152 mRecentFolderList.destroy();
Andy Huang4e0158f2012-08-07 21:06:01 -07001153 mDestroyed = true;
Andy Huang632721e2012-04-11 16:57:26 -07001154 }
1155
Vikram Aggarwalfa131a22012-02-02 13:56:22 -08001156 /**
Mindy Pereira161f50d2012-02-28 15:47:19 -08001157 * {@inheritDoc} Subclasses must override this to listen to mode changes
1158 * from the ViewMode. Subclasses <b>must</b> call the parent's
1159 * onViewModeChanged since the parent will handle common state changes.
Vikram Aggarwalfa131a22012-02-02 13:56:22 -08001160 */
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -08001161 @Override
Vikram Aggarwalfa131a22012-02-02 13:56:22 -08001162 public void onViewModeChanged(int newMode) {
1163 // Perform any mode specific work here.
Mindy Pereira161f50d2012-02-28 15:47:19 -08001164 // reset the action bar icon based on the mode. Why don't the individual
1165 // controllers do
Vikram Aggarwalfa131a22012-02-02 13:56:22 -08001166 // this themselves?
Vikram Aggarwala55b36c2012-01-13 11:45:02 -08001167
Mindy Pereira8937bf12012-07-23 14:05:02 -07001168 // Commit any destructive undoable actions the user may have performed.
mindypc6adce32012-08-22 18:46:42 -07001169 commitDestructiveActions(true);
Mindy Pereira8937bf12012-07-23 14:05:02 -07001170
Mindy Pereira161f50d2012-02-28 15:47:19 -08001171 // We don't want to invalidate the options menu when switching to
1172 // conversation
Vikram Aggarwalfa131a22012-02-02 13:56:22 -08001173 // mode, as it will happen when the conversation finishes loading.
1174 if (newMode != ViewMode.CONVERSATION) {
1175 mActivity.invalidateOptionsMenu();
1176 }
Vikram Aggarwala55b36c2012-01-13 11:45:02 -08001177 }
1178
Andy Huang4e0158f2012-08-07 21:06:01 -07001179 public boolean isDestroyed() {
1180 return mDestroyed;
1181 }
1182
mindyp54f120f2012-08-28 13:10:33 -07001183 @Override
1184 public void commitDestructiveActions(boolean animate) {
mindypc6adce32012-08-22 18:46:42 -07001185 ConversationListFragment fragment = getConversationListFragment();
Mindy Pereira1e2573b2012-04-17 14:34:36 -07001186 if (fragment != null) {
mindypc6adce32012-08-22 18:46:42 -07001187 fragment.commitDestructiveActions(animate);
Mindy Pereira1e2573b2012-04-17 14:34:36 -07001188 }
1189 }
1190
Vikram Aggarwala55b36c2012-01-13 11:45:02 -08001191 @Override
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -08001192 public void onWindowFocusChanged(boolean hasFocus) {
Paul Westbrook9f119c72012-04-24 16:10:59 -07001193 ConversationListFragment convList = getConversationListFragment();
1194 if (hasFocus && convList != null && convList.isVisible()) {
1195 // The conversation list is visible.
1196 Utils.setConversationCursorVisibility(mConversationListCursor, true);
1197 }
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -08001198 }
1199
Vikram Aggarwalca716f12012-08-20 11:11:48 -07001200 /**
1201 * Set the account, and carry out all the account-related changes that rely on this.
1202 * @param account
1203 */
1204 // TODO(viki): Two different methods do the same thing. Resolve
1205 // {@link #setAccount(Account)} and {@link #switchAccount(Account, boolean)}
Mindy Pereira75181e82012-04-18 08:17:13 -07001206 private void setAccount(Account account) {
Andy Huangb9ca9792012-05-18 15:31:49 -07001207 if (account == null) {
1208 LogUtils.w(LOG_TAG, new Error(),
1209 "AAC ignoring null (presumably invalid) account restoration");
1210 return;
1211 }
Andy Huangb1148412012-05-19 00:16:30 -07001212 LogUtils.d(LOG_TAG, "AbstractActivityController.setAccount(): account = %s", account.uri);
Vikram Aggarwal91e87372012-05-18 15:36:04 -07001213 mAccount = account;
Vikram Aggarwalca716f12012-08-20 11:11:48 -07001214 // Only change AAC state here. Do *not* modify any other object's state. The object
1215 // should listen on account changes.
1216 restartOptionalLoader(LOADER_RECENT_FOLDERS);
1217 mActivity.invalidateOptionsMenu();
1218 disableNotificationsOnAccountChange(mAccount);
1219 restartOptionalLoader(LOADER_ACCOUNT_UPDATE_CURSOR);
1220 MailAppProvider.getInstance().setLastViewedAccount(mAccount.uri.toString());
1221
Vikram Aggarwal91e87372012-05-18 15:36:04 -07001222 if (account.settings == null) {
1223 LogUtils.w(LOG_TAG, new Error(), "AAC ignoring account with null settings.");
1224 return;
1225 }
Vikram Aggarwal7c401b72012-08-13 16:43:47 -07001226 mAccountObservers.notifyChanged();
Mindy Pereira75181e82012-04-18 08:17:13 -07001227 }
1228
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -08001229 /**
Mindy Pereira161f50d2012-02-28 15:47:19 -08001230 * Restore the state from the previous bundle. Subclasses should call this
1231 * method from the parent class, since it performs important UI
1232 * initialization.
1233 *
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -08001234 * @param savedState
1235 */
Andy Huang632721e2012-04-11 16:57:26 -07001236 @Override
1237 public void onRestoreInstanceState(Bundle savedState) {
1238 LogUtils.d(LOG_TAG, "IN AAC.onRestoreInstanceState");
1239 if (savedState.containsKey(SAVED_CONVERSATION)) {
1240 // Open the conversation.
Paul Westbrook534e4a22012-04-25 03:46:29 -07001241 final Conversation conversation =
1242 (Conversation)savedState.getParcelable(SAVED_CONVERSATION);
1243 if (conversation != null && conversation.position < 0) {
1244 // Set the position to 0 on this conversation, as we don't know where it is
1245 // in the list
1246 conversation.position = 0;
1247 }
Andy Huanged4fdf02012-07-26 17:12:50 -07001248 showConversation(conversation);
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -08001249 }
Mindy Pereira967ede62012-03-22 09:29:09 -07001250
Mindy Pereirad33674992012-06-25 16:26:30 -07001251 if (savedState.containsKey(SAVED_TOAST_BAR_OP)) {
1252 ToastBarOperation op = ((ToastBarOperation) savedState
1253 .getParcelable(SAVED_TOAST_BAR_OP));
1254 if (op != null) {
1255 if (op.getType() == ToastBarOperation.UNDO) {
1256 onUndoAvailable(op);
Andrew Sapperstein9d7519d2012-07-16 14:03:53 -07001257 } else if (op.getType() == ToastBarOperation.ERROR) {
1258 onError(mFolder, true);
Mindy Pereirad33674992012-06-25 16:26:30 -07001259 }
1260 }
1261 }
Vikram Aggarwal649b9ea2012-08-27 12:15:20 -07001262 final String folderString = savedState.getString(SAVED_HIERARCHICAL_FOLDER, null);
1263 if (!TextUtils.isEmpty(folderString)) {
1264 mFolderListFolder = Folder.fromString(folderString);
1265 }
1266 final ConversationListFragment convListFragment = getConversationListFragment();
Mindy Pereirad33674992012-06-25 16:26:30 -07001267 if (convListFragment != null) {
Vikram Aggarwal649b9ea2012-08-27 12:15:20 -07001268 convListFragment.getAnimatedAdapter().onRestoreInstanceState(savedState);
Mindy Pereirad33674992012-06-25 16:26:30 -07001269 }
Mindy Pereira967ede62012-03-22 09:29:09 -07001270 /**
1271 * Restore the state of selected conversations. This needs to be done after the correct mode
1272 * is set and the action bar is fully initialized. If not, several key pieces of state
1273 * information will be missing, and the split views may not be initialized correctly.
1274 * @param savedState
1275 */
Andy Huang4556a442012-03-30 16:42:05 -07001276 restoreSelectedConversations(savedState);
Andy Huang632721e2012-04-11 16:57:26 -07001277 }
1278
Vikram Aggarwal649b9ea2012-08-27 12:15:20 -07001279 /**
1280 * Handle an intent to open the app. This method is called only when there is no saved state,
1281 * so we need to set state that wasn't set before. It is correct to change the viewmode here
1282 * since it has not been previously set.
1283 * @param intent
1284 */
Andy Huang632721e2012-04-11 16:57:26 -07001285 private void handleIntent(Intent intent) {
1286 boolean handled = false;
1287 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1288 if (intent.hasExtra(Utils.EXTRA_ACCOUNT)) {
Vikram Aggarwal649b9ea2012-08-27 12:15:20 -07001289 setAccount(Account.newinstance(intent.getStringExtra(Utils.EXTRA_ACCOUNT)));
Andy Huang632721e2012-04-11 16:57:26 -07001290 }
Andy Huangb9ca9792012-05-18 15:31:49 -07001291 if (mAccount == null) {
1292 return;
Andy Huang632721e2012-04-11 16:57:26 -07001293 }
Andy Huangb9ca9792012-05-18 15:31:49 -07001294 mActivity.invalidateOptionsMenu();
Vikram Aggarwal1a249e02012-08-03 16:19:33 -07001295 final boolean isConversationMode = intent.hasExtra(Utils.EXTRA_CONVERSATION);
Vikram Aggarwal649b9ea2012-08-27 12:15:20 -07001296 if (isConversationMode && mViewMode.getMode() == ViewMode.UNKNOWN) {
Vikram Aggarwal1a249e02012-08-03 16:19:33 -07001297 mViewMode.enterConversationMode();
1298 } else {
1299 mViewMode.enterConversationListMode();
1300 }
Andy Huang632721e2012-04-11 16:57:26 -07001301
1302 Folder folder = null;
1303 if (intent.hasExtra(Utils.EXTRA_FOLDER)) {
1304 // Open the folder.
Mindy Pereira7b6d03d2012-07-30 13:03:41 -07001305 folder = Folder.fromString(intent.getStringExtra(Utils.EXTRA_FOLDER));
Andy Huang632721e2012-04-11 16:57:26 -07001306 }
1307 if (folder != null) {
1308 onFolderChanged(folder);
1309 handled = true;
1310 }
1311
Vikram Aggarwal1a249e02012-08-03 16:19:33 -07001312 if (isConversationMode) {
Andy Huang632721e2012-04-11 16:57:26 -07001313 // Open the conversation.
1314 LogUtils.d(LOG_TAG, "SHOW THE CONVERSATION at %s",
1315 intent.getParcelableExtra(Utils.EXTRA_CONVERSATION));
Paul Westbrooka9161912012-04-24 10:10:04 -07001316 final Conversation conversation =
1317 (Conversation)intent.getParcelableExtra(Utils.EXTRA_CONVERSATION);
1318 if (conversation != null && conversation.position < 0) {
1319 // Set the position to 0 on this conversation, as we don't know where it is
1320 // in the list
1321 conversation.position = 0;
1322 }
Andy Huang980aaea2012-07-26 17:22:19 -07001323 showConversation(conversation);
Andy Huang632721e2012-04-11 16:57:26 -07001324 handled = true;
1325 }
1326
1327 if (!handled) {
1328 // Nothing was saved; just load the account inbox.
1329 loadAccountInbox();
1330 }
1331 } else if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
1332 if (intent.hasExtra(Utils.EXTRA_ACCOUNT)) {
1333 // Save this search query for future suggestions.
1334 final String query = intent.getStringExtra(SearchManager.QUERY);
1335 final String authority = mContext.getString(R.string.suggestions_authority);
1336 SearchRecentSuggestions suggestions = new SearchRecentSuggestions(
1337 mContext, authority, SuggestionsProvider.MODE);
1338 suggestions.saveRecentQuery(query, null);
Mindy Pereiraac254822012-06-18 10:46:43 -07001339 if (Utils.showTwoPaneSearchResults(mActivity.getActivityContext())) {
1340 mViewMode.enterSearchResultsConversationMode();
1341 } else {
1342 mViewMode.enterSearchResultsListMode();
1343 }
Andy Huang632721e2012-04-11 16:57:26 -07001344 setAccount((Account) intent.getParcelableExtra(Utils.EXTRA_ACCOUNT));
1345 mActivity.invalidateOptionsMenu();
1346 restartOptionalLoader(LOADER_RECENT_FOLDERS);
Andy Huang632721e2012-04-11 16:57:26 -07001347 fetchSearchFolder(intent);
1348 } else {
1349 LogUtils.e(LOG_TAG, "Missing account extra from search intent. Finishing");
1350 mActivity.finish();
1351 }
1352 }
1353 if (mAccount != null) {
1354 restartOptionalLoader(LOADER_ACCOUNT_UPDATE_CURSOR);
1355 }
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -08001356 }
1357
Andy Huang4556a442012-03-30 16:42:05 -07001358 /**
1359 * Copy any selected conversations stored in the saved bundle into our selection set,
1360 * triggering {@link ConversationSetObserver} callbacks as our selection set changes.
1361 *
1362 */
Vikram Aggarwal4eb52712012-06-19 16:24:50 -07001363 private final void restoreSelectedConversations(Bundle savedState) {
Mindy Pereira967ede62012-03-22 09:29:09 -07001364 if (savedState == null) {
Andy Huang4556a442012-03-30 16:42:05 -07001365 mSelectedSet.clear();
Mindy Pereira967ede62012-03-22 09:29:09 -07001366 return;
1367 }
Vikram Aggarwalcabd3f22012-04-19 10:14:41 -07001368 final ConversationSelectionSet selectedSet = savedState.getParcelable(SAVED_SELECTED_SET);
Andy Huang4556a442012-03-30 16:42:05 -07001369 if (selectedSet == null || selectedSet.isEmpty()) {
1370 mSelectedSet.clear();
Mindy Pereira967ede62012-03-22 09:29:09 -07001371 return;
1372 }
Andy Huang632721e2012-04-11 16:57:26 -07001373
1374 // putAll will take care of calling our registered onSetPopulated method
Vikram Aggarwalcabd3f22012-04-19 10:14:41 -07001375 mSelectedSet.putAll(selectedSet);
Mindy Pereira967ede62012-03-22 09:29:09 -07001376 }
1377
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -08001378 @Override
Andy Huang5895f7b2012-06-01 17:07:20 -07001379 public SubjectDisplayChanger getSubjectDisplayChanger() {
1380 return mActionBarView;
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -08001381 }
1382
Andy Huang1ee96b22012-08-24 20:19:53 -07001383 private void showConversation(Conversation conversation) {
1384 showConversation(conversation, false /* inLoaderCallbacks */);
1385 }
1386
Vikram Aggarwalec5cbf72012-03-08 15:10:35 -08001387 /**
1388 * Children can override this method, but they must call super.showConversation().
Andy Huang1ee96b22012-08-24 20:19:53 -07001389 *
Vikram Aggarwalec5cbf72012-03-08 15:10:35 -08001390 */
Andy Huang1ee96b22012-08-24 20:19:53 -07001391 protected void showConversation(Conversation conversation, boolean inLoaderCallbacks) {
Vikram Aggarwalc67182d2012-04-03 14:35:06 -07001392 // Set the current conversation just in case it wasn't already set.
1393 setCurrentConversation(conversation);
Vikram Aggarwal0f142732012-08-24 09:39:34 -07001394 // Add the folder that we were viewing to the recent folders list.
1395 // TODO: this may need to be fine tuned. If this is the signal that is indicating that
1396 // the list is shown to the user, this could fire in one pane if the user goes directly
1397 // to a conversation
1398 updateRecentFolderList();
Vikram Aggarwalec5cbf72012-03-08 15:10:35 -08001399 }
1400
Vikram Aggarwale128fc22012-04-04 12:33:34 -07001401 /**
Paul Westbrook2d50bcd2012-04-10 11:53:47 -07001402 * Children can override this method, but they must call super.showWaitForInitialization().
1403 * {@inheritDoc}
1404 */
1405 @Override
1406 public void showWaitForInitialization() {
1407 mViewMode.enterWaitingForInitializationMode();
1408 }
1409
1410 @Override
1411 public void hideWaitForInitialization() {
1412 }
1413
1414 @Override
1415 public void updateWaitMode() {
1416 final FragmentManager manager = mActivity.getFragmentManager();
1417 final WaitFragment waitFragment =
Vikram Aggarwal6902dcf2012-04-11 08:57:42 -07001418 (WaitFragment)manager.findFragmentByTag(TAG_WAIT);
Paul Westbrook2d50bcd2012-04-10 11:53:47 -07001419 if (waitFragment != null) {
1420 waitFragment.updateAccount(mAccount);
1421 }
1422 }
1423
Vikram Aggarwal48b2a6c2012-05-29 14:09:27 -07001424 /**
1425 * Returns true if we are waiting for the account to sync, and cannot show any folders or
1426 * conversation for the current account yet.
Andy Huang839ada22012-07-20 15:48:40 -07001427 *
Vikram Aggarwal48b2a6c2012-05-29 14:09:27 -07001428 */
Paul Westbrook2d50bcd2012-04-10 11:53:47 -07001429 public boolean inWaitMode() {
1430 final FragmentManager manager = mActivity.getFragmentManager();
1431 final WaitFragment waitFragment =
Vikram Aggarwal6902dcf2012-04-11 08:57:42 -07001432 (WaitFragment)manager.findFragmentByTag(TAG_WAIT);
Paul Westbrook2d50bcd2012-04-10 11:53:47 -07001433 if (waitFragment != null) {
1434 final Account fragmentAccount = waitFragment.getAccount();
1435 return fragmentAccount.uri.equals(mAccount.uri) &&
1436 mViewMode.getMode() == ViewMode.WAITING_FOR_ACCOUNT_INITIALIZATION;
1437 }
1438 return false;
1439 }
1440
1441 /**
Vikram Aggarwale128fc22012-04-04 12:33:34 -07001442 * Children can override this method, but they must call super.showConversationList().
1443 * {@inheritDoc}
1444 */
1445 @Override
1446 public void showConversationList(ConversationListContext listContext) {
1447 }
1448
Vikram Aggarwal1ddcf0f2012-01-13 11:45:02 -08001449 @Override
Andy Huang1ee96b22012-08-24 20:19:53 -07001450 public void onConversationSelected(Conversation conversation, boolean inLoaderCallbacks) {
mindypaa55bc92012-08-24 09:49:56 -07001451 // Only animate destructive actions if we are going to be showing the
1452 // conversation list when we show the next conversation.
1453 commitDestructiveActions(Utils.useTabletUI(mContext));
Andy Huang1ee96b22012-08-24 20:19:53 -07001454 showConversation(conversation, inLoaderCallbacks);
1455 }
1456
1457 @Override
1458 public Conversation getCurrentConversation() {
1459 return mCurrentConversation;
Vikram Aggarwal7d602882012-02-07 15:01:20 -08001460 }
Mindy Pereira555140c2012-02-15 14:55:29 -08001461
Vikram Aggarwalc67182d2012-04-03 14:35:06 -07001462 /**
1463 * Set the current conversation. This is the conversation on which all actions are performed.
1464 * Do not modify mCurrentConversation except through this method, which makes it easy to
1465 * perform common actions associated with changing the current conversation.
1466 * @param conversation
1467 */
Andy Huang632721e2012-04-11 16:57:26 -07001468 @Override
1469 public void setCurrentConversation(Conversation conversation) {
Mindy Pereira5040f1a2012-03-20 10:14:06 -07001470 mCurrentConversation = conversation;
Vikram Aggarwal135fd022012-04-11 14:44:15 -07001471 mTracker.initialize(mCurrentConversation);
Mindy Pereira5040f1a2012-03-20 10:14:06 -07001472 }
1473
Vikram Aggarwala9b93f32012-02-23 14:51:58 -08001474 /**
1475 * {@inheritDoc}
1476 */
Mindy Pereira555140c2012-02-15 14:55:29 -08001477 @Override
Vikram Aggarwal7dedb952012-02-16 16:10:23 -08001478 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
1479 // Create a loader to listen in on account changes.
Vikram Aggarwalfa4b47e2012-03-09 13:02:46 -08001480 switch (id) {
1481 case LOADER_ACCOUNT_CURSOR:
Paul Westbrookc2074c42012-03-22 15:26:58 -07001482 return new CursorLoader(mContext, MailAppProvider.getAccountsUri(),
Vikram Aggarwalfa4b47e2012-03-09 13:02:46 -08001483 UIProvider.ACCOUNTS_PROJECTION, null, null, null);
1484 case LOADER_FOLDER_CURSOR:
Paul Westbrookc7a070f2012-04-12 01:46:41 -07001485 final CursorLoader loader = new CursorLoader(mContext, mFolder.uri,
1486 UIProvider.FOLDERS_PROJECTION, null, null, null);
1487 loader.setUpdateThrottle(mFolderItemUpdateDelayMs);
1488 return loader;
Vikram Aggarwalfa4b47e2012-03-09 13:02:46 -08001489 case LOADER_RECENT_FOLDERS:
Paul Westbrook91d10502012-04-13 12:01:39 -07001490 if (mAccount != null && mAccount.recentFolderListUri != null) {
Paul Westbrookea4ee202012-03-12 14:12:33 -07001491 return new CursorLoader(mContext, mAccount.recentFolderListUri,
1492 UIProvider.FOLDERS_PROJECTION, null, null, null);
1493 }
1494 break;
Mindy Pereiraab486362012-03-21 18:18:53 -07001495 case LOADER_ACCOUNT_INBOX:
Vikram Aggarwal025eba82012-05-08 10:45:30 -07001496 final Uri defaultInbox = Settings.getDefaultInboxUri(mAccount.settings);
Vikram Aggarwal1e57e672012-05-07 14:48:24 -07001497 final Uri inboxUri = defaultInbox.equals(Uri.EMPTY) ?
1498 mAccount.folderListUri : defaultInbox;
Paul Westbrook7496e822012-04-24 09:50:54 -07001499 LogUtils.d(LOG_TAG, "Loading the default inbox: %s", inboxUri);
Paul Westbrook1220b6d2012-04-10 00:48:00 -07001500 if (inboxUri != null) {
1501 return new CursorLoader(mContext, inboxUri, UIProvider.FOLDERS_PROJECTION, null,
1502 null, null);
1503 }
1504 break;
Mindy Pereiraab486362012-03-21 18:18:53 -07001505 case LOADER_SEARCH:
1506 return Folder.forSearchResults(mAccount,
1507 args.getString(ConversationListContext.EXTRA_SEARCH_QUERY),
1508 mActivity.getActivityContext());
Paul Westbrook2d50bcd2012-04-10 11:53:47 -07001509 case LOADER_ACCOUNT_UPDATE_CURSOR:
1510 return new CursorLoader(mContext, mAccount.uri, UIProvider.ACCOUNTS_PROJECTION,
1511 null, null, null);
Vikram Aggarwalfa4b47e2012-03-09 13:02:46 -08001512 default:
Paul Westbrookad6a2752012-04-04 16:58:13 -07001513 LogUtils.wtf(LOG_TAG, "Loader returned unexpected id: %d", id);
Mindy Pereira5cb0c3e2012-02-22 15:22:47 -08001514 }
1515 return null;
Vikram Aggarwal7dedb952012-02-16 16:10:23 -08001516 }
1517
Paul Westbrookb1f573c2012-04-06 11:38:28 -07001518 @Override
1519 public void onLoaderReset(Loader<Cursor> loader) {
1520
1521 }
1522
Andy Huangf9a73482012-03-13 15:54:02 -07001523 /**
1524 * {@link LoaderManager} currently has a bug in
1525 * {@link LoaderManager#restartLoader(int, Bundle, android.app.LoaderManager.LoaderCallbacks)}
1526 * where, if a previous onCreateLoader returned a null loader, this method will NPE. Work around
1527 * this bug by destroying any loaders that may have been created as null (essentially because
1528 * they are optional loads, and may not apply to a particular account).
1529 * <p>
1530 * A simple null check before restarting a loader will not work, because that would not
1531 * give the controller a chance to invalidate UI corresponding the prior loader result.
1532 *
1533 * @param id loader ID to safely restart
Andy Huangf9a73482012-03-13 15:54:02 -07001534 */
Vikram Aggarwal94c94de2012-04-04 15:38:28 -07001535 private void restartOptionalLoader(int id) {
Andy Huangf9a73482012-03-13 15:54:02 -07001536 final LoaderManager lm = mActivity.getLoaderManager();
1537 lm.destroyLoader(id);
Vikram Aggarwal94c94de2012-04-04 15:38:28 -07001538 lm.restartLoader(id, Bundle.EMPTY, this);
1539 }
1540
Andy Huang632721e2012-04-11 16:57:26 -07001541 @Override
1542 public void registerConversationListObserver(DataSetObserver observer) {
1543 mConversationListObservable.registerObserver(observer);
1544 }
1545
1546 @Override
1547 public void unregisterConversationListObserver(DataSetObserver observer) {
1548 mConversationListObservable.unregisterObserver(observer);
1549 }
1550
Andy Huang090db1e2012-07-25 13:25:28 -07001551 @Override
1552 public void registerFolderObserver(DataSetObserver observer) {
1553 mFolderObservable.registerObserver(observer);
1554 }
1555
1556 @Override
1557 public void unregisterFolderObserver(DataSetObserver observer) {
1558 mFolderObservable.unregisterObserver(observer);
1559 }
1560
Vikram Aggarwal60069912012-07-24 14:26:09 -07001561 /**
1562 * Returns true if the number of accounts is different, or if the current account has been
1563 * removed from the device
1564 * @param accountCursor
1565 * @return
1566 */
Paul Westbrook23b74b92012-02-29 11:36:12 -08001567 private boolean accountsUpdated(Cursor accountCursor) {
1568 // Check to see if the current account hasn't been set, or the account cursor is empty
1569 if (mAccount == null || !accountCursor.moveToFirst()) {
Vikram Aggarwal6c511582012-02-27 10:59:47 -08001570 return true;
Paul Westbrook23b74b92012-02-29 11:36:12 -08001571 }
1572
1573 // Check to see if the number of accounts are different, from the number we saw on the last
1574 // updated
1575 if (mCurrentAccountUris.size() != accountCursor.getCount()) {
1576 return true;
1577 }
1578
1579 // Check to see if the account list is different or if the current account is not found in
1580 // the cursor.
1581 boolean foundCurrentAccount = false;
Vikram Aggarwal7dedb952012-02-16 16:10:23 -08001582 do {
Paul Westbrook23b74b92012-02-29 11:36:12 -08001583 final Uri accountUri =
1584 Uri.parse(accountCursor.getString(UIProvider.ACCOUNT_URI_COLUMN));
1585 if (!foundCurrentAccount && mAccount.uri.equals(accountUri)) {
1586 foundCurrentAccount = true;
1587 }
Vikram Aggarwal60069912012-07-24 14:26:09 -07001588 // Is there a new account that we do not know about?
Paul Westbrook23b74b92012-02-29 11:36:12 -08001589 if (!mCurrentAccountUris.contains(accountUri)) {
1590 return true;
1591 }
Vikram Aggarwal7dedb952012-02-16 16:10:23 -08001592 } while (accountCursor.moveToNext());
Paul Westbrook23b74b92012-02-29 11:36:12 -08001593
1594 // As long as we found the current account, the list hasn't been updated
1595 return !foundCurrentAccount;
Vikram Aggarwal7dedb952012-02-16 16:10:23 -08001596 }
1597
1598 /**
Vikram Aggarwal60069912012-07-24 14:26:09 -07001599 * Updates accounts for the app. If the current account is missing, the first
1600 * account in the list is set to the current account (we <em>have</em> to choose something).
Mindy Pereira161f50d2012-02-28 15:47:19 -08001601 *
Vikram Aggarwal6c511582012-02-27 10:59:47 -08001602 * @param accounts cursor into the AccountCache
Vikram Aggarwal7dedb952012-02-16 16:10:23 -08001603 * @return true if the update was successful, false otherwise
1604 */
Vikram Aggarwal60069912012-07-24 14:26:09 -07001605 private boolean updateAccounts(Cursor accounts) {
Vikram Aggarwal7dedb952012-02-16 16:10:23 -08001606 if (accounts == null || !accounts.moveToFirst()) {
1607 return false;
1608 }
Paul Westbrook23b74b92012-02-29 11:36:12 -08001609
Vikram Aggarwala9b93f32012-02-23 14:51:58 -08001610 final Account[] allAccounts = Account.getAllAccounts(accounts);
Vikram Aggarwal60069912012-07-24 14:26:09 -07001611 // A match for the current account's URI in the list of accounts.
1612 Account currentFromList = null;
Paul Westbrook23b74b92012-02-29 11:36:12 -08001613
1614 // Save the uris for the accounts
1615 mCurrentAccountUris.clear();
1616 for (Account account : allAccounts) {
Vikram Aggarwal60069912012-07-24 14:26:09 -07001617 LogUtils.d(LOG_TAG, "updateAccounts(%s)", account);
Paul Westbrook23b74b92012-02-29 11:36:12 -08001618 mCurrentAccountUris.add(account.uri);
Vikram Aggarwal60069912012-07-24 14:26:09 -07001619 if (mAccount != null && account.uri.equals(mAccount.uri)) {
1620 currentFromList = account;
1621 }
Paul Westbrook23b74b92012-02-29 11:36:12 -08001622 }
1623
Vikram Aggarwal60069912012-07-24 14:26:09 -07001624 // 1. current account is already set and is in allAccounts:
1625 // 1a. It has changed -> load the updated account.
1626 // 2b. It is unchanged -> no-op
Andy Huang0d647352012-03-21 21:48:16 -07001627 // 2. current account is set and is not in allAccounts -> pick first (acct was deleted?)
Vikram Aggarwal60069912012-07-24 14:26:09 -07001628 // 3. saved preference has an account -> pick that one
Andy Huang0d647352012-03-21 21:48:16 -07001629 // 4. otherwise just pick first
1630
Vikram Aggarwal60069912012-07-24 14:26:09 -07001631 boolean accountChanged = false;
1632 /// Assume case 4, initialize to first account, and see if we can find anything better.
1633 Account newAccount = allAccounts[0];
1634 if (currentFromList != null) {
1635 // Case 1: Current account exists but has changed
1636 if (!currentFromList.equals(mAccount)) {
1637 newAccount = currentFromList;
1638 accountChanged = true;
Andy Huang0d647352012-03-21 21:48:16 -07001639 }
Vikram Aggarwal60069912012-07-24 14:26:09 -07001640 // Case 1b: else, current account is unchanged: nothing to do.
Paul Westbrook23b74b92012-02-29 11:36:12 -08001641 } else {
Vikram Aggarwal60069912012-07-24 14:26:09 -07001642 // Case 2: Current account is not in allAccounts, the account needs to change.
1643 accountChanged = true;
1644 if (mAccount == null) {
1645 // Case 3: Check for last viewed account, and check if it exists in the list.
1646 final String lastAccountUri = MailAppProvider.getInstance().getLastViewedAccount();
1647 if (lastAccountUri != null) {
1648 for (final Account account : allAccounts) {
1649 if (lastAccountUri.equals(account.uri.toString())) {
1650 newAccount = account;
1651 break;
1652 }
Andy Huang0d647352012-03-21 21:48:16 -07001653 }
1654 }
1655 }
Paul Westbrook23b74b92012-02-29 11:36:12 -08001656 }
Vikram Aggarwal60069912012-07-24 14:26:09 -07001657 if (accountChanged) {
1658 onAccountChanged(newAccount);
1659 }
1660 // Whether we have updated the current account or not, we need to update the list of
1661 // accounts in the ActionBar.
Vikram Aggarwala9b93f32012-02-23 14:51:58 -08001662 mActionBarView.setAccounts(allAccounts);
1663 return (allAccounts.length > 0);
Vikram Aggarwal7dedb952012-02-16 16:10:23 -08001664 }
1665
Paul Westbrook6ead20d2012-03-19 14:48:14 -07001666 private void disableNotifications() {
1667 mNewEmailReceiver.activate(mContext, this);
1668 }
1669
1670 private void enableNotifications() {
1671 mNewEmailReceiver.deactivate();
1672 }
1673
1674 private void disableNotificationsOnAccountChange(Account account) {
1675 // If the new mail suppression receiver is activated for a different account, we want to
1676 // activate it for the new account.
1677 if (mNewEmailReceiver.activated() &&
1678 !mNewEmailReceiver.notificationsDisabledForAccount(account)) {
1679 // Deactivate the current receiver, otherwise multiple receivers may be registered.
1680 mNewEmailReceiver.deactivate();
1681 mNewEmailReceiver.activate(mContext, this);
1682 }
1683 }
1684
Vikram Aggarwala9b93f32012-02-23 14:51:58 -08001685 /**
1686 * {@inheritDoc}
1687 */
Vikram Aggarwal7dedb952012-02-16 16:10:23 -08001688 @Override
1689 public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
Mindy Pereira5cb0c3e2012-02-22 15:22:47 -08001690 // We want to reinitialize only if we haven't ever been initialized, or
1691 // if the current account has vanished.
Paul Westbrooke3e84292012-03-05 16:19:30 -08001692 if (data == null) {
Vikram Aggarwalfa4b47e2012-03-09 13:02:46 -08001693 LogUtils.e(LOG_TAG, "Received null cursor from loader id: %d", loader.getId());
Paul Westbrooke3e84292012-03-05 16:19:30 -08001694 }
Vikram Aggarwalfa4b47e2012-03-09 13:02:46 -08001695 switch (loader.getId()) {
1696 case LOADER_ACCOUNT_CURSOR:
Vikram Aggarwal27d89ad2012-06-12 13:38:40 -07001697 // If the account list is not null, and the account list cursor is empty,
Paul Westbrook2388c5d2012-03-25 12:29:11 -07001698 // we need to start the specified activity.
1699 if (data != null && data.getCount() == 0) {
1700 // If an empty cursor is returned, the MailAppProvider is indicating that
1701 // no accounts have been specified. We want to navigate to the "add account"
1702 // activity that will handle the intent returned by the MailAppProvider
1703
1704 // If the MailAppProvider believes that all accounts have been loaded, and the
1705 // account list is still empty, we want to prompt the user to add an account
1706 final Bundle extras = data.getExtras();
1707 final boolean accountsLoaded =
1708 extras.getInt(AccountCursorExtraKeys.ACCOUNTS_LOADED) != 0;
1709
1710 if (accountsLoaded) {
1711 final Intent noAccountIntent = MailAppProvider.getNoAccountIntent(mContext);
1712 if (noAccountIntent != null) {
1713 mActivity.startActivityForResult(noAccountIntent,
1714 ADD_ACCOUNT_REQUEST_CODE);
1715 }
1716 }
1717 } else {
1718 final boolean accountListUpdated = accountsUpdated(data);
1719 if (!isLoaderInitialized || accountListUpdated) {
Vikram Aggarwal60069912012-07-24 14:26:09 -07001720 isLoaderInitialized = updateAccounts(data);
Paul Westbrook2388c5d2012-03-25 12:29:11 -07001721 }
Vikram Aggarwalfa4b47e2012-03-09 13:02:46 -08001722 }
1723 break;
Paul Westbrook2d50bcd2012-04-10 11:53:47 -07001724 case LOADER_ACCOUNT_UPDATE_CURSOR:
1725 // We have gotten an update for current account.
1726
Vikram Aggarwal60069912012-07-24 14:26:09 -07001727 // Make sure that this is an update for the current account
Paul Westbrook2d50bcd2012-04-10 11:53:47 -07001728 if (data != null && data.moveToFirst()) {
1729 final Account updatedAccount = new Account(data);
1730
1731 if (updatedAccount.uri.equals(mAccount.uri)) {
Paul Westbrookca08fc12012-07-31 12:01:15 -07001732 // Keep a reference to the previous settings object
1733 final Settings previousSettings = mAccount.settings;
1734
Vikram Aggarwal7d816002012-04-17 17:06:41 -07001735 // Update the controller's reference to the current account
Paul Westbrook2d50bcd2012-04-10 11:53:47 -07001736 mAccount = updatedAccount;
Vikram Aggarwaledc137c2012-04-24 13:40:58 -07001737 LogUtils.d(LOG_TAG, "AbstractActivityController.onLoadFinished(): "
1738 + "mAccount = %s", mAccount.uri);
Paul Westbrookca08fc12012-07-31 12:01:15 -07001739
1740 // Only notify about a settings change if something differs
1741 if (!Objects.equal(mAccount.settings, previousSettings)) {
Vikram Aggarwal7c401b72012-08-13 16:43:47 -07001742 mAccountObservers.notifyChanged();
Paul Westbrookca08fc12012-07-31 12:01:15 -07001743 }
Paul Westbrook2d50bcd2012-04-10 11:53:47 -07001744
1745 // Got an update for the current account
1746 final boolean inWaitingMode = inWaitMode();
1747 if (!updatedAccount.isAccountIntialized() && !inWaitingMode) {
1748 // Transition to waiting mode
1749 showWaitForInitialization();
Mindy Pereira830bdaf2012-07-11 12:59:55 -07001750 } else if (updatedAccount.isAccountIntialized()) {
1751 if (inWaitingMode) {
1752 // Dismiss waiting mode
1753 hideWaitForInitialization();
1754 }
Paul Westbrook2d50bcd2012-04-10 11:53:47 -07001755 } else if (!updatedAccount.isAccountIntialized() && inWaitingMode) {
1756 // Update the WaitFragment's account object
1757 updateWaitMode();
1758 }
1759 } else {
1760 LogUtils.e(LOG_TAG, "Got update for account: %s with current account: %s",
1761 updatedAccount.uri, mAccount.uri);
1762 // We need to restart the loader, so the correct account information will
1763 // be returned
1764 restartOptionalLoader(LOADER_ACCOUNT_UPDATE_CURSOR);
1765 }
1766 }
1767 break;
Vikram Aggarwalfa4b47e2012-03-09 13:02:46 -08001768 case LOADER_FOLDER_CURSOR:
Vikram Aggarwalfa4b47e2012-03-09 13:02:46 -08001769 // Check status of the cursor.
Marc Blankfd9d0b82012-04-23 16:01:51 -07001770 if (data != null && data.moveToFirst()) {
Andy Huang090db1e2012-07-25 13:25:28 -07001771 final Folder folder = new Folder(data);
Marc Blankfd9d0b82012-04-23 16:01:51 -07001772 LogUtils.d(LOG_TAG, "FOLDER STATUS = %d", folder.syncStatus);
Andy Huang090db1e2012-07-25 13:25:28 -07001773
1774 mFolder = folder;
1775 mFolderObservable.notifyChanged();
1776
Paul Westbrookc808fac2012-02-22 16:42:18 -08001777 } else {
Marc Blankfd9d0b82012-04-23 16:01:51 -07001778 LogUtils.d(LOG_TAG, "Unable to get the folder %s",
1779 mFolder != null ? mAccount.name : "");
Mindy Pereira11dd5ef2012-03-10 15:10:18 -08001780 }
Vikram Aggarwalfa4b47e2012-03-09 13:02:46 -08001781 break;
Vikram Aggarwalfa4b47e2012-03-09 13:02:46 -08001782 case LOADER_RECENT_FOLDERS:
Vikram Aggarwal27d89ad2012-06-12 13:38:40 -07001783 // No recent folders and we are running on a phone? Populate the default recents.
1784 if (data != null && data.getCount() == 0 && !Utils.useTabletUI(mContext)) {
1785 final class PopulateDefault extends AsyncTask<Uri, Void, Void> {
1786 @Override
1787 protected Void doInBackground(Uri... uri) {
1788 // Asking for an update on the URI and ignore the result.
1789 final ContentResolver resolver = mContext.getContentResolver();
1790 resolver.update(uri[0], null, null, null);
1791 return null;
1792 }
1793 }
1794 final Uri uri = mAccount.defaultRecentFolderListUri;
1795 LogUtils.v(LOG_TAG, "Default recents at %s", uri);
1796 new PopulateDefault().execute(uri);
1797 break;
1798 }
1799 LogUtils.v(LOG_TAG, "Reading recent folders from the cursor.");
Vikram Aggarwalfa4b47e2012-03-09 13:02:46 -08001800 mRecentFolderList.loadFromUiProvider(data);
Vikram Aggarwal37263972012-04-17 15:51:14 -07001801 mActionBarView.requestRecentFoldersAndRedraw();
Vikram Aggarwalfa4b47e2012-03-09 13:02:46 -08001802 break;
Mindy Pereiraab486362012-03-21 18:18:53 -07001803 case LOADER_ACCOUNT_INBOX:
Marc Blankfd9d0b82012-04-23 16:01:51 -07001804 if (data != null && !data.isClosed() && data.moveToFirst()) {
Mindy Pereira0e88e9f2012-03-25 13:47:41 -07001805 Folder inbox = new Folder(data);
1806 onFolderChanged(inbox);
Mindy Pereirab4a43282012-03-23 16:20:03 -07001807 // Just want to get the inbox, don't care about updates to it
1808 // as this will be tracked by the folder change listener.
1809 mActivity.getLoaderManager().destroyLoader(LOADER_ACCOUNT_INBOX);
Mindy Pereira5ba33802012-03-26 16:30:11 -07001810 } else {
1811 LogUtils.d(LOG_TAG, "Unable to get the account inbox for account %s",
1812 mAccount != null ? mAccount.name : "");
Mindy Pereirab4a43282012-03-23 16:20:03 -07001813 }
Mindy Pereiraab486362012-03-21 18:18:53 -07001814 break;
1815 case LOADER_SEARCH:
1816 data.moveToFirst();
1817 Folder search = new Folder(data);
Mindy Pereira11e35962012-06-01 14:49:46 -07001818 updateFolder(search);
Mindy Pereiraab486362012-03-21 18:18:53 -07001819 mConvListContext = ConversationListContext.forSearchQuery(mAccount, mFolder,
Mindy Pereirad660d252012-03-26 11:48:43 -07001820 mActivity.getIntent()
1821 .getStringExtra(UIProvider.SearchQueryParameters.QUERY));
Mindy Pereiraab486362012-03-21 18:18:53 -07001822 showConversationList(mConvListContext);
1823 mActivity.invalidateOptionsMenu();
Mindy Pereira3b399222012-03-28 15:19:47 -07001824 mActivity.getLoaderManager().destroyLoader(LOADER_SEARCH);
Mindy Pereiraab486362012-03-21 18:18:53 -07001825 break;
Vikram Aggarwal7dedb952012-02-16 16:10:23 -08001826 }
1827 }
1828
Vikram Aggarwalc7694222012-04-23 13:37:01 -07001829 /**
1830 * Destructive actions on Conversations. This class should only be created by controllers, and
1831 * clients should only require {@link DestructiveAction}s, not specific implementations of the.
1832 * Only the controllers should know what kind of destructive actions are being created.
1833 */
Mindy Pereirade3e74a2012-07-24 09:43:10 -07001834 public class ConversationAction implements DestructiveAction {
Vikram Aggarwalacaa3c02012-04-24 12:45:27 -07001835 /**
1836 * The action to be performed. This is specified as the resource ID of the menu item
1837 * corresponding to this action: R.id.delete, R.id.report_spam, etc.
1838 */
Vikram Aggarwal7f602f72012-04-30 16:04:06 -07001839 private final int mAction;
Vikram Aggarwalbc67bb12012-04-30 14:05:35 -07001840 /** The action will act upon these conversations */
Paul Westbrook77eee622012-07-10 13:41:57 -07001841 private final Collection<Conversation> mTarget;
Vikram Aggarwal7f602f72012-04-30 16:04:06 -07001842 /** Whether this destructive action has already been performed */
1843 private boolean mCompleted;
Vikram Aggarwalc4113952012-05-11 14:14:56 -07001844 /** Whether this is an action on the currently selected set. */
1845 private final boolean mIsSelectedSet;
Vikram Aggarwale8a85322012-04-24 09:01:38 -07001846
Mindy Pereirafbe40192012-03-20 10:40:45 -07001847 /**
1848 * Create a listener object. action is one of four constants: R.id.y_button (archive),
1849 * R.id.delete , R.id.mute, and R.id.report_spam.
1850 * @param action
Vikram Aggarwalbc67bb12012-04-30 14:05:35 -07001851 * @param target Conversation that we want to apply the action to.
Vikram Aggarwalc4113952012-05-11 14:14:56 -07001852 * @param isBatch whether the conversations are in the currently selected batch set.
1853 */
1854 public ConversationAction(int action, Collection<Conversation> target, boolean isBatch) {
Mindy Pereirafbe40192012-03-20 10:40:45 -07001855 mAction = action;
Paul Westbrook77eee622012-07-10 13:41:57 -07001856 mTarget = ImmutableList.copyOf(target);
Vikram Aggarwalc4113952012-05-11 14:14:56 -07001857 mIsSelectedSet = isBatch;
Mindy Pereirafbe40192012-03-20 10:40:45 -07001858 }
1859
Vikram Aggarwalacaa3c02012-04-24 12:45:27 -07001860 /**
1861 * The action common to child classes. This performs the action specified in the constructor
1862 * on the conversations given here.
Vikram Aggarwalacaa3c02012-04-24 12:45:27 -07001863 */
Vikram Aggarwalbc67bb12012-04-30 14:05:35 -07001864 @Override
1865 public void performAction() {
Vikram Aggarwal7f602f72012-04-30 16:04:06 -07001866 if (isPerformed()) {
1867 return;
1868 }
Mindy Pereira3cb28b52012-05-24 15:26:39 -07001869 boolean undoEnabled = mAccount.supportsCapability(AccountCapabilities.UNDO);
Vikram Aggarwal7dd054e2012-05-21 14:43:10 -07001870
1871 // Are we destroying the currently shown conversation? Show the next one.
1872 if (LogUtils.isLoggable(LOG_TAG, LogUtils.DEBUG)){
Vikram Aggarwal8742a612012-08-13 10:22:50 -07001873 LogUtils.d(LOG_TAG, "ConversationAction.performAction():"
1874 + "\nmTarget=%s\nCurrent=%s",
Vikram Aggarwal7dd054e2012-05-21 14:43:10 -07001875 Conversation.toString(mTarget), mCurrentConversation);
1876 }
Vikram Aggarwal7dd054e2012-05-21 14:43:10 -07001877
Paul Westbrooke1221d22012-08-19 11:09:07 -07001878 if (mConversationListCursor == null) {
1879 LogUtils.e(LOG_TAG, "null ConversationCursor in ConversationAction.performAction():"
1880 + "\nmTarget=%s\nCurrent=%s",
1881 Conversation.toString(mTarget), mCurrentConversation);
1882 return;
1883 }
1884
Mindy Pereirafbe40192012-03-20 10:40:45 -07001885 switch (mAction) {
Mindy Pereira0692baf2012-03-23 17:34:31 -07001886 case R.id.archive:
Vikram Aggarwal7dd054e2012-05-21 14:43:10 -07001887 LogUtils.d(LOG_TAG, "Archiving");
Vikram Aggarwalbc67bb12012-04-30 14:05:35 -07001888 mConversationListCursor.archive(mContext, mTarget);
Mindy Pereirafbe40192012-03-20 10:40:45 -07001889 break;
1890 case R.id.delete:
Vikram Aggarwal7dd054e2012-05-21 14:43:10 -07001891 LogUtils.d(LOG_TAG, "Deleting");
Vikram Aggarwalbc67bb12012-04-30 14:05:35 -07001892 mConversationListCursor.delete(mContext, mTarget);
Mindy Pereira695d6962012-06-18 13:02:10 -07001893 if (mFolder.supportsCapability(FolderCapabilities.DELETE_ACTION_FINAL)) {
Marc Blank386243f2012-05-25 10:40:59 -07001894 undoEnabled = false;
1895 }
Mindy Pereirafbe40192012-03-20 10:40:45 -07001896 break;
1897 case R.id.mute:
Vikram Aggarwal7dd054e2012-05-21 14:43:10 -07001898 LogUtils.d(LOG_TAG, "Muting");
Vikram Aggarwalbc67bb12012-04-30 14:05:35 -07001899 if (mFolder.supportsCapability(FolderCapabilities.DESTRUCTIVE_MUTE)) {
Vikram Aggarwal7f602f72012-04-30 16:04:06 -07001900 for (Conversation c : mTarget) {
1901 c.localDeleteOnUpdate = true;
1902 }
Vikram Aggarwalbc67bb12012-04-30 14:05:35 -07001903 }
1904 mConversationListCursor.mute(mContext, mTarget);
Mindy Pereirafbe40192012-03-20 10:40:45 -07001905 break;
1906 case R.id.report_spam:
Vikram Aggarwal7dd054e2012-05-21 14:43:10 -07001907 LogUtils.d(LOG_TAG, "Reporting spam");
Vikram Aggarwalbc67bb12012-04-30 14:05:35 -07001908 mConversationListCursor.reportSpam(mContext, mTarget);
1909 break;
Paul Westbrook77eee622012-07-10 13:41:57 -07001910 case R.id.mark_not_spam:
1911 LogUtils.d(LOG_TAG, "Marking not spam");
1912 mConversationListCursor.reportNotSpam(mContext, mTarget);
1913 break;
Paul Westbrook76b20622012-07-12 11:45:43 -07001914 case R.id.report_phishing:
1915 LogUtils.d(LOG_TAG, "Reporting phishing");
1916 mConversationListCursor.reportPhishing(mContext, mTarget);
1917 break;
Vikram Aggarwalbc67bb12012-04-30 14:05:35 -07001918 case R.id.remove_star:
Vikram Aggarwal7dd054e2012-05-21 14:43:10 -07001919 LogUtils.d(LOG_TAG, "Removing star");
Vikram Aggarwalbc67bb12012-04-30 14:05:35 -07001920 // Star removal is destructive in the Starred folder.
1921 mConversationListCursor.updateBoolean(mContext, mTarget,
1922 ConversationColumns.STARRED, false);
1923 break;
1924 case R.id.mark_not_important:
Vikram Aggarwal7dd054e2012-05-21 14:43:10 -07001925 LogUtils.d(LOG_TAG, "Marking not-important");
Mindy Pereira445be212012-08-15 08:50:10 -07001926 // Marking not important is destructive in a mailbox
1927 // containing only important messages
Mindy Pereira0d03ef82012-08-15 09:05:48 -07001928 if (mFolder != null && mFolder.isImportantOnly()) {
Mindy Pereira445be212012-08-15 08:50:10 -07001929 for (Conversation conv : mTarget) {
1930 conv.localDeleteOnUpdate = true;
1931 }
1932 }
Vikram Aggarwalbc67bb12012-04-30 14:05:35 -07001933 mConversationListCursor.updateInt(mContext, mTarget,
1934 ConversationColumns.PRIORITY, UIProvider.ConversationPriority.LOW);
Mindy Pereirafbe40192012-03-20 10:40:45 -07001935 break;
Paul Westbrookef362542012-08-27 14:53:32 -07001936 case R.id.discard_drafts:
1937 LogUtils.d(LOG_TAG, "Discarding draft messages");
1938 // Discarding draft messages is destructive in a "draft" mailbox
1939 if (mFolder != null && mFolder.isDraft()) {
1940 for (Conversation conv : mTarget) {
1941 conv.localDeleteOnUpdate = true;
1942 }
1943 }
1944 mConversationListCursor.discardDrafts(mContext, mTarget);
1945 // We don't support undoing discarding drafts
1946 undoEnabled = false;
1947 break;
Vikram Aggarwal3d7ca9d2012-05-11 14:40:36 -07001948 }
1949 if (undoEnabled) {
mindypead50392012-08-23 11:03:53 -07001950 mHandler.postDelayed(new Runnable() {
1951 @Override
1952 public void run() {
1953 onUndoAvailable(new ToastBarOperation(mTarget.size(), mAction,
1954 ToastBarOperation.UNDO));
1955 }
1956 }, mShowUndoBarDelay);
Vikram Aggarwal3d7ca9d2012-05-11 14:40:36 -07001957 }
Vikram Aggarwalbc67bb12012-04-30 14:05:35 -07001958 refreshConversationList();
Vikram Aggarwalc4113952012-05-11 14:14:56 -07001959 if (mIsSelectedSet) {
Vikram Aggarwalc4113952012-05-11 14:14:56 -07001960 mSelectedSet.clear();
1961 }
Mindy Pereirafbe40192012-03-20 10:40:45 -07001962 }
Vikram Aggarwal7f602f72012-04-30 16:04:06 -07001963
1964 /**
1965 * Returns true if this action has been performed, false otherwise.
Andy Huang839ada22012-07-20 15:48:40 -07001966 *
Vikram Aggarwal7f602f72012-04-30 16:04:06 -07001967 */
1968 private synchronized boolean isPerformed() {
1969 if (mCompleted) {
1970 return true;
1971 }
1972 mCompleted = true;
1973 return false;
1974 }
Mindy Pereirafbe40192012-03-20 10:40:45 -07001975 }
Mindy Pereirae5f4dc02012-03-21 16:08:53 -07001976
Vikram Aggarwal3d7ca9d2012-05-11 14:40:36 -07001977 /**
1978 * Get a destructive action for a menu action.
1979 * This is a temporary method, to control the profusion of {@link DestructiveAction} classes
1980 * that are created. Please do not copy this paradigm.
1981 * @param action the resource ID of the menu action: R.id.delete, for example
1982 * @param target the conversations to act upon.
1983 * @return a {@link DestructiveAction} that performs the specified action.
1984 */
Vikram Aggarwal531488e2012-05-29 16:36:52 -07001985 private final DestructiveAction getAction(int action, Collection<Conversation> target) {
Vikram Aggarwal3d7ca9d2012-05-11 14:40:36 -07001986 final DestructiveAction da = new ConversationAction(action, target, false);
1987 registerDestructiveAction(da);
1988 return da;
1989 }
1990
Vikram Aggarwald503df42012-05-11 10:13:35 -07001991 // Called from the FolderSelectionDialog after a user is done selecting folders to assign the
1992 // conversations to.
Mindy Pereirae5f4dc02012-03-21 16:08:53 -07001993 @Override
Mindy Pereira8db7e402012-07-13 10:32:47 -07001994 public final void assignFolder(Collection<FolderOperation> folderOps,
1995 Collection<Conversation> target, boolean batch, boolean showUndo) {
1996 // Actions are destructive only when the current folder can be assigned
1997 // to (which is the same as being able to un-assign a conversation from the folder) and
1998 // when the list of folders contains the current folder.
1999 final boolean isDestructive = mFolder
2000 .supportsCapability(FolderCapabilities.CAN_ACCEPT_MOVED_MESSAGES)
2001 && FolderOperation.isDestructive(folderOps, mFolder);
Vikram Aggarwald503df42012-05-11 10:13:35 -07002002 LogUtils.d(LOG_TAG, "onFolderChangesCommit: isDestructive = %b", isDestructive);
2003 if (isDestructive) {
2004 for (final Conversation c : target) {
2005 c.localDeleteOnUpdate = true;
Mindy Pereira6778f462012-03-23 18:01:55 -07002006 }
Mindy Pereirae5f4dc02012-03-21 16:08:53 -07002007 }
Mindy Pereira8db7e402012-07-13 10:32:47 -07002008 final DestructiveAction folderChange = getFolderChange(target, folderOps, isDestructive,
Mindy Pereira06642fa2012-07-12 16:23:27 -07002009 batch, showUndo);
Vikram Aggarwald503df42012-05-11 10:13:35 -07002010 // Update the UI elements depending no their visibility and availability
Vikram Aggarwal3d7ca9d2012-05-11 14:40:36 -07002011 // TODO(viki): Consolidate this into a single method requestDelete.
Vikram Aggarwald503df42012-05-11 10:13:35 -07002012 if (isDestructive) {
Vikram Aggarwal4f4782b2012-05-30 08:39:09 -07002013 delete(target, folderChange);
Vikram Aggarwal6902dcf2012-04-11 08:57:42 -07002014 } else {
Vikram Aggarwald503df42012-05-11 10:13:35 -07002015 requestUpdate(target, folderChange);
Mindy Pereirae5f4dc02012-03-21 16:08:53 -07002016 }
2017 }
2018
Mindy Pereira967ede62012-03-22 09:29:09 -07002019 @Override
Vikram Aggarwal3d7ca9d2012-05-11 14:40:36 -07002020 public final void onRefreshRequired() {
mindyp5390fca2012-08-22 12:12:25 -07002021 if (isAnimating() || isDragging()) {
Mindy Pereira69e88dd2012-08-10 09:30:18 -07002022 LogUtils.d(LOG_TAG, "onRefreshRequired: delay until animating done");
2023 return;
2024 }
Marc Blankbf128eb2012-04-18 15:58:45 -07002025 // Refresh the query in the background
Paul Westbrookcff1aea2012-08-10 11:51:00 -07002026 if (mConversationListCursor.isRefreshRequired()) {
2027 mConversationListCursor.refresh();
2028 }
Marc Blankbf128eb2012-04-18 15:58:45 -07002029 }
2030
mindyp5390fca2012-08-22 12:12:25 -07002031 @Override
2032 public void startDragMode() {
2033 mIsDragHappening = true;
2034 }
2035
2036 @Override
2037 public void stopDragMode() {
2038 mIsDragHappening = false;
2039 if (mConversationListCursor.isRefreshReady()) {
2040 LogUtils.d(LOG_TAG, "Stopped animating: try sync");
2041 onRefreshReady();
2042 }
2043
2044 if (mConversationListCursor.isRefreshRequired()) {
2045 LogUtils.d(LOG_TAG, "Stopped animating: refresh");
2046 mConversationListCursor.refresh();
2047 }
2048 }
2049
2050 private boolean isDragging() {
2051 return mIsDragHappening;
2052 }
2053
Mindy Pereira69e88dd2012-08-10 09:30:18 -07002054 private boolean isAnimating() {
2055 boolean isAnimating = false;
2056 ConversationListFragment convListFragment = getConversationListFragment();
2057 if (convListFragment != null) {
2058 AnimatedAdapter adapter = convListFragment.getAnimatedAdapter();
2059 if (adapter != null) {
2060 isAnimating = adapter.isAnimating();
2061 }
2062 }
2063 return isAnimating;
2064 }
2065
Marc Blankbf128eb2012-04-18 15:58:45 -07002066 /**
2067 * Called when the {@link ConversationCursor} is changed or has new data in it.
2068 * <p>
2069 * {@inheritDoc}
2070 */
2071 @Override
Vikram Aggarwal3d7ca9d2012-05-11 14:40:36 -07002072 public final void onRefreshReady() {
Paul Westbrookcff1aea2012-08-10 11:51:00 -07002073 if (!isAnimating()) {
Marc Blankbf128eb2012-04-18 15:58:45 -07002074 // Swap cursors
2075 mConversationListCursor.sync();
Marc Blankbf128eb2012-04-18 15:58:45 -07002076 }
Paul Westbrook937c94f2012-08-16 13:01:18 -07002077 mTracker.onCursorUpdated();
Marc Blankbf128eb2012-04-18 15:58:45 -07002078 }
2079
2080 @Override
Vikram Aggarwal3d7ca9d2012-05-11 14:40:36 -07002081 public final void onDataSetChanged() {
Paul Westbrook9f119c72012-04-24 16:10:59 -07002082 updateConversationListFragment();
Andy Huang632721e2012-04-11 16:57:26 -07002083 mConversationListObservable.notifyChanged();
Marc Blankbf128eb2012-04-18 15:58:45 -07002084 }
2085
Vikram Aggarwal3d7ca9d2012-05-11 14:40:36 -07002086 /**
2087 * If the Conversation List Fragment is visible, updates the fragment.
2088 */
2089 private final void updateConversationListFragment() {
Marc Blankbf128eb2012-04-18 15:58:45 -07002090 final ConversationListFragment convList = getConversationListFragment();
2091 if (convList != null) {
Vikram Aggarwal75daee52012-04-30 11:13:09 -07002092 refreshConversationList();
Paul Westbrook9f119c72012-04-24 16:10:59 -07002093 if (convList.isVisible()) {
Paul Westbrook9f119c72012-04-24 16:10:59 -07002094 Utils.setConversationCursorVisibility(mConversationListCursor, true);
2095 }
Marc Blankbf128eb2012-04-18 15:58:45 -07002096 }
2097 }
2098
2099 /**
2100 * This class handles throttled refresh of the conversation list
2101 */
2102 static class RefreshTimerTask extends TimerTask {
2103 final Handler mHandler;
2104 final AbstractActivityController mController;
2105
2106 RefreshTimerTask(AbstractActivityController controller, Handler handler) {
2107 mHandler = handler;
2108 mController = controller;
2109 }
2110
2111 @Override
2112 public void run() {
2113 mHandler.post(new Runnable() {
2114 @Override
2115 public void run() {
2116 LogUtils.d(LOG_TAG, "Delay done... calling onRefreshRequired");
2117 mController.onRefreshRequired();
2118 }});
2119 }
2120 }
2121
2122 /**
2123 * Cancel the refresh task, if it's running
2124 */
2125 private void cancelRefreshTask () {
2126 if (mConversationListRefreshTask != null) {
2127 mConversationListRefreshTask.cancel();
2128 mConversationListRefreshTask = null;
2129 }
2130 }
2131
2132 @Override
Paul Westbrookcff1aea2012-08-10 11:51:00 -07002133 public void onAnimationEnd(AnimatedAdapter animatedAdapter) {
2134 if (mConversationListCursor.isRefreshReady()) {
mindyp52544862012-08-20 12:05:36 -07002135 LogUtils.d(LOG_TAG, "Stopped animating: try sync");
Paul Westbrookcff1aea2012-08-10 11:51:00 -07002136 onRefreshReady();
Marc Blankbf128eb2012-04-18 15:58:45 -07002137 }
Marc Blankbf128eb2012-04-18 15:58:45 -07002138
Paul Westbrookcff1aea2012-08-10 11:51:00 -07002139 if (mConversationListCursor.isRefreshRequired()) {
mindyp52544862012-08-20 12:05:36 -07002140 LogUtils.d(LOG_TAG, "Stopped animating: refresh");
Paul Westbrookcff1aea2012-08-10 11:51:00 -07002141 mConversationListCursor.refresh();
2142 }
Marc Blankbf128eb2012-04-18 15:58:45 -07002143 }
2144
2145 @Override
Mindy Pereira967ede62012-03-22 09:29:09 -07002146 public void onSetEmpty() {
Mindy Pereira967ede62012-03-22 09:29:09 -07002147 }
2148
2149 @Override
2150 public void onSetPopulated(ConversationSelectionSet set) {
Vikram Aggarwal6902dcf2012-04-11 08:57:42 -07002151 final ConversationListFragment convList = getConversationListFragment();
2152 if (convList == null) {
2153 return;
2154 }
Vikram Aggarwal7c401b72012-08-13 16:43:47 -07002155 mCabActionMenu = new SelectedConversationsActionMenu(mActivity, set, mFolder,
Vikram Aggarwal531488e2012-05-29 16:36:52 -07002156 (SwipeableListView) convList.getListView());
Vikram Aggarwale128fc22012-04-04 12:33:34 -07002157 enableCabMode();
Mindy Pereira967ede62012-03-22 09:29:09 -07002158 }
2159
Mindy Pereira967ede62012-03-22 09:29:09 -07002160 @Override
2161 public void onSetChanged(ConversationSelectionSet set) {
2162 // Do nothing. We don't care about changes to the set.
2163 }
2164
2165 @Override
2166 public ConversationSelectionSet getSelectedSet() {
2167 return mSelectedSet;
2168 }
2169
Vikram Aggarwale128fc22012-04-04 12:33:34 -07002170 /**
2171 * Disable the Contextual Action Bar (CAB). The selected set is not changed.
2172 */
2173 protected void disableCabMode() {
Mindy Pereira8937bf12012-07-23 14:05:02 -07002174 // Commit any previous destructive actions when entering/ exiting CAB mode.
mindypc6adce32012-08-22 18:46:42 -07002175 commitDestructiveActions(true);
Vikram Aggarwale128fc22012-04-04 12:33:34 -07002176 if (mCabActionMenu != null) {
2177 mCabActionMenu.deactivate();
2178 }
2179 }
2180
2181 /**
2182 * Re-enable the CAB menu if required. The selection set is not changed.
2183 */
2184 protected void enableCabMode() {
Mindy Pereira8937bf12012-07-23 14:05:02 -07002185 // Commit any previous destructive actions when entering/ exiting CAB mode.
mindypc6adce32012-08-22 18:46:42 -07002186 commitDestructiveActions(true);
Vikram Aggarwale128fc22012-04-04 12:33:34 -07002187 if (mCabActionMenu != null) {
2188 mCabActionMenu.activate();
2189 }
2190 }
2191
Vikram Aggarwal4eb52712012-06-19 16:24:50 -07002192 /**
2193 * Unselect conversations and exit CAB mode.
2194 */
2195 protected final void exitCabMode() {
2196 mSelectedSet.clear();
2197 }
2198
Mindy Pereira967ede62012-03-22 09:29:09 -07002199 @Override
Mindy Pereirafd0c2972012-03-27 13:50:39 -07002200 public void startSearch() {
Vikram Aggarwal35f19d72012-04-24 13:24:48 -07002201 if (mAccount == null) {
2202 // We cannot search if there is no account. Drop the request to the floor.
2203 LogUtils.d(LOG_TAG, "AbstractActivityController.startSearch(): null account");
2204 return;
2205 }
Mindy Pereirafd0c2972012-03-27 13:50:39 -07002206 if (mAccount.supportsCapability(UIProvider.AccountCapabilities.LOCAL_SEARCH)
2207 | mAccount.supportsCapability(UIProvider.AccountCapabilities.SERVER_SEARCH)) {
Vikram Aggarwal70f130e2012-04-03 12:32:14 -07002208 onSearchRequested(mActionBarView.getQuery());
Mindy Pereirafd0c2972012-03-27 13:50:39 -07002209 } else {
2210 Toast.makeText(mActivity.getActivityContext(), mActivity.getActivityContext()
Mindy Pereiraa46c2992012-03-27 14:12:39 -07002211 .getString(R.string.search_unsupported), Toast.LENGTH_SHORT).show();
Mindy Pereirafd0c2972012-03-27 13:50:39 -07002212 }
2213 }
Mindy Pereiraacf60392012-04-06 09:11:00 -07002214
Vikram Aggarwal0dda5732012-04-06 11:20:16 -07002215 @Override
2216 public void exitSearchMode() {
2217 if (mViewMode.getMode() == ViewMode.SEARCH_RESULTS_LIST) {
2218 mActivity.finish();
2219 }
2220 }
2221
Mindy Pereiraacf60392012-04-06 09:11:00 -07002222 /**
2223 * Supports dragging conversations to a folder.
2224 */
2225 @Override
2226 public boolean supportsDrag(DragEvent event, Folder folder) {
2227 return (folder != null
2228 && event != null
2229 && event.getClipDescription() != null
2230 && folder.supportsCapability
2231 (UIProvider.FolderCapabilities.CAN_ACCEPT_MOVED_MESSAGES)
2232 && folder.supportsCapability
2233 (UIProvider.FolderCapabilities.CAN_HOLD_MAIL)
2234 && !mFolder.uri.equals(folder.uri));
2235 }
2236
2237 /**
Mindy Pereira6c2663d2012-07-20 15:37:29 -07002238 * Handles dropping conversations to a folder.
Mindy Pereiraacf60392012-04-06 09:11:00 -07002239 */
2240 @Override
2241 public void handleDrop(DragEvent event, final Folder folder) {
Mindy Pereiraacf60392012-04-06 09:11:00 -07002242 if (!supportsDrag(event, folder)) {
2243 return;
2244 }
Mindy Pereiraacf60392012-04-06 09:11:00 -07002245 final Collection<Conversation> conversations = mSelectedSet.values();
Mindy Pereira8db7e402012-07-13 10:32:47 -07002246 final Collection<FolderOperation> dropTarget = FolderOperation.listOf(new FolderOperation(
2247 folder, true));
2248 // Drag and drop is destructive: we remove conversations from the
2249 // current folder.
Mindy Pereira06642fa2012-07-12 16:23:27 -07002250 final DestructiveAction action = getFolderChange(conversations, dropTarget, true, true,
2251 true);
Vikram Aggarwal4f4782b2012-05-30 08:39:09 -07002252 delete(conversations, action);
Mindy Pereiraacf60392012-04-06 09:11:00 -07002253 }
Mindy Pereira0963ef82012-04-10 11:43:01 -07002254
2255 @Override
Mindy Pereira0963ef82012-04-10 11:43:01 -07002256 public void onTouchEvent(MotionEvent event) {
2257 if (event.getAction() == MotionEvent.ACTION_DOWN) {
Andrew Sappersteinc2c9dc12012-07-02 18:17:32 -07002258 if (mToastBar != null && !mToastBar.isEventInToastBar(event)) {
2259 mToastBar.hide(true);
Mindy Pereira0963ef82012-04-10 11:43:01 -07002260 }
2261 }
2262 }
Andy Huangb1c34dc2012-04-17 16:36:19 -07002263
Andy Huang632721e2012-04-11 16:57:26 -07002264 @Override
2265 public void onConversationSeen(Conversation conv) {
2266 mPagerController.onConversationSeen(conv);
2267 }
2268
Andy Huangb1c34dc2012-04-17 16:36:19 -07002269 private class ConversationListLoaderCallbacks implements
2270 LoaderManager.LoaderCallbacks<ConversationCursor> {
2271
2272 @Override
2273 public Loader<ConversationCursor> onCreateLoader(int id, Bundle args) {
2274 Loader<ConversationCursor> result = new ConversationCursorLoader((Activity) mActivity,
Paul Westbrook9a70e912012-08-17 15:53:20 -07002275 mAccount, mFolder.conversationListUri, mFolder.name);
Andy Huangb1c34dc2012-04-17 16:36:19 -07002276 return result;
2277 }
2278
2279 @Override
2280 public void onLoadFinished(Loader<ConversationCursor> loader, ConversationCursor data) {
Andy Huang632721e2012-04-11 16:57:26 -07002281 LogUtils.d(LOG_TAG, "IN AAC.ConversationCursor.onLoadFinished, data=%s loader=%s",
2282 data, loader);
Vikram Aggarwale8a85322012-04-24 09:01:38 -07002283 // Clear our all pending destructive actions before swapping the conversation cursor
2284 destroyPending(null);
Andy Huangb1c34dc2012-04-17 16:36:19 -07002285 mConversationListCursor = data;
Paul Westbrookbf232c32012-04-18 03:17:41 -07002286 mConversationListCursor.addListener(AbstractActivityController.this);
Andy Huangb1c34dc2012-04-17 16:36:19 -07002287
Paul Westbrook937c94f2012-08-16 13:01:18 -07002288 mTracker.onCursorUpdated();
2289
Andy Huange3df1ad2012-04-24 17:15:23 -07002290 mConversationListObservable.notifyChanged();
Andy Huangb1c34dc2012-04-17 16:36:19 -07002291 // Register the AbstractActivityController as a listener to changes in
2292 // data in the cursor.
2293 final ConversationListFragment convList = getConversationListFragment();
2294 if (convList != null) {
2295 convList.onCursorUpdated();
Paul Westbrook9f119c72012-04-24 16:10:59 -07002296
2297 if (convList.isVisible()) {
2298 // The conversation list is visible.
2299 Utils.setConversationCursorVisibility(mConversationListCursor, true);
2300 }
Andy Huangb1c34dc2012-04-17 16:36:19 -07002301 }
2302 // Shown for search results in two-pane mode only.
2303 if (shouldShowFirstConversation()) {
2304 if (mConversationListCursor.getCount() > 0) {
2305 mConversationListCursor.moveToPosition(0);
Andy Huangb1c34dc2012-04-17 16:36:19 -07002306 final Conversation conv = new Conversation(mConversationListCursor);
2307 conv.position = 0;
Andy Huang1ee96b22012-08-24 20:19:53 -07002308 onConversationSelected(conv, true /* checkSafeToModifyFragments */);
Andy Huangb1c34dc2012-04-17 16:36:19 -07002309 }
2310 }
2311 }
2312
2313 @Override
2314 public void onLoaderReset(Loader<ConversationCursor> loader) {
Paul Westbrook9a70e912012-08-17 15:53:20 -07002315 LogUtils.d(LOG_TAG, "IN AAC.ConversationCursor.onLoaderReset, data=%s loader=%s",
2316 mConversationListCursor, loader);
2317
2318 if (mConversationListCursor != null) {
2319 // Unregister the listener
2320 mConversationListCursor.removeListener(AbstractActivityController.this);
2321 mConversationListCursor = null;
2322
2323 // Inform anyone who is interested about the change
2324 mTracker.onCursorUpdated();
2325 mConversationListObservable.notifyChanged();
2326
2327 final ConversationListFragment convList = getConversationListFragment();
2328 if (convList != null) {
2329 convList.onCursorUpdated();
2330 }
Andy Huangb1c34dc2012-04-17 16:36:19 -07002331 }
Andy Huangb1c34dc2012-04-17 16:36:19 -07002332 }
Paul Westbrookbf232c32012-04-18 03:17:41 -07002333 }
Andy Huangb1c34dc2012-04-17 16:36:19 -07002334
Vikram Aggarwale8a85322012-04-24 09:01:38 -07002335 /**
2336 * Destroy the pending {@link DestructiveAction} till now and assign the given action as the
2337 * next destructive action..
2338 * @param nextAction the next destructive action to be performed. This can be null.
2339 */
2340 private final void destroyPending(DestructiveAction nextAction) {
2341 // If there is a pending action, perform that first.
2342 if (mPendingDestruction != null) {
2343 mPendingDestruction.performAction();
2344 }
2345 mPendingDestruction = nextAction;
2346 }
2347
2348 /**
2349 * Register a destructive action with the controller. This performs the previous destructive
Vikram Aggarwalacaa3c02012-04-24 12:45:27 -07002350 * 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 -07002351 * embellish this method any more.
Vikram Aggarwale8a85322012-04-24 09:01:38 -07002352 * @param action
2353 */
Vikram Aggarwal3d7ca9d2012-05-11 14:40:36 -07002354 private final void registerDestructiveAction(DestructiveAction action) {
Vikram Aggarwale8a85322012-04-24 09:01:38 -07002355 // TODO(viki): This is not a good idea. The best solution is for clients to request a
2356 // destructive action from the controller and for the controller to own the action. This is
2357 // a half-way solution while refactoring DestructiveAction.
2358 destroyPending(action);
2359 return;
2360 }
2361
Vikram Aggarwal531488e2012-05-29 16:36:52 -07002362 @Override
2363 public final DestructiveAction getBatchAction(int action) {
Vikram Aggarwalc4113952012-05-11 14:14:56 -07002364 final DestructiveAction da = new ConversationAction(action, mSelectedSet.values(), true);
Vikram Aggarwale8a85322012-04-24 09:01:38 -07002365 registerDestructiveAction(da);
2366 return da;
2367 }
Vikram Aggarwal41e6e712012-04-24 11:22:57 -07002368
Mindy Pereirade3e74a2012-07-24 09:43:10 -07002369 @Override
2370 public final DestructiveAction getDeferredBatchAction(int action) {
2371 final DestructiveAction da = new ConversationAction(action, mSelectedSet.values(), true);
2372 return da;
2373 }
2374
Vikram Aggarwalbc67bb12012-04-30 14:05:35 -07002375 /**
2376 * Class to change the folders that are assigned to a set of conversations. This is destructive
2377 * because the user can remove the current folder from the conversation, in which case it has
2378 * to be animated away from the current folder.
2379 */
Vikram Aggarwal41e6e712012-04-24 11:22:57 -07002380 private class FolderDestruction implements DestructiveAction {
Paul Westbrook77eee622012-07-10 13:41:57 -07002381 private final Collection<Conversation> mTarget;
Mindy Pereira8db7e402012-07-13 10:32:47 -07002382 private final ArrayList<FolderOperation> mFolderOps = new ArrayList<FolderOperation>();
Vikram Aggarwal41e6e712012-04-24 11:22:57 -07002383 private final boolean mIsDestructive;
Vikram Aggarwal7f602f72012-04-30 16:04:06 -07002384 /** Whether this destructive action has already been performed */
2385 private boolean mCompleted;
Mindy Pereiraf3a45562012-05-24 16:30:19 -07002386 private boolean mIsSelectedSet;
Mindy Pereira06642fa2012-07-12 16:23:27 -07002387 private boolean mShowUndo;
Mindy Pereira01f30502012-08-14 10:30:51 -07002388 private int mAction;
Vikram Aggarwal41e6e712012-04-24 11:22:57 -07002389
2390 /**
2391 * Create a new folder destruction object to act on the given conversations.
2392 * @param target
2393 */
Vikram Aggarwal7f602f72012-04-30 16:04:06 -07002394 private FolderDestruction(final Collection<Conversation> target,
Mindy Pereira8db7e402012-07-13 10:32:47 -07002395 final Collection<FolderOperation> folders, boolean isDestructive, boolean isBatch,
Mindy Pereira01f30502012-08-14 10:30:51 -07002396 boolean showUndo, int action) {
Paul Westbrook77eee622012-07-10 13:41:57 -07002397 mTarget = ImmutableList.copyOf(target);
Mindy Pereira8db7e402012-07-13 10:32:47 -07002398 mFolderOps.addAll(folders);
Vikram Aggarwal41e6e712012-04-24 11:22:57 -07002399 mIsDestructive = isDestructive;
Mindy Pereiraf3a45562012-05-24 16:30:19 -07002400 mIsSelectedSet = isBatch;
Mindy Pereira06642fa2012-07-12 16:23:27 -07002401 mShowUndo = showUndo;
Mindy Pereira01f30502012-08-14 10:30:51 -07002402 mAction = action;
Vikram Aggarwal41e6e712012-04-24 11:22:57 -07002403 }
2404
Vikram Aggarwal41e6e712012-04-24 11:22:57 -07002405 @Override
2406 public void performAction() {
Vikram Aggarwal7f602f72012-04-30 16:04:06 -07002407 if (isPerformed()) {
2408 return;
2409 }
Mindy Pereira06642fa2012-07-12 16:23:27 -07002410 if (mIsDestructive && mShowUndo) {
Mindy Pereirad33674992012-06-25 16:26:30 -07002411 ToastBarOperation undoOp = new ToastBarOperation(mTarget.size(),
Mindy Pereira01f30502012-08-14 10:30:51 -07002412 mAction, ToastBarOperation.UNDO);
Vikram Aggarwal41e6e712012-04-24 11:22:57 -07002413 onUndoAvailable(undoOp);
2414 }
Mindy Pereira8db7e402012-07-13 10:32:47 -07002415 // For each conversation, for each operation, add/ remove the
2416 // appropriate folders.
2417 for (Conversation target : mTarget) {
2418 HashMap<Uri, Folder> targetFolders = Folder
Mindy Pereira68f83842012-07-27 09:43:31 -07002419 .hashMapForFolders(target.getRawFolders());
Mindy Pereira01f30502012-08-14 10:30:51 -07002420 if (mIsDestructive) {
2421 target.localDeleteOnUpdate = true;
2422 }
Mindy Pereira8db7e402012-07-13 10:32:47 -07002423 for (FolderOperation op : mFolderOps) {
2424 if (op.mAdd) {
2425 targetFolders.put(op.mFolder.uri, op.mFolder);
2426 } else {
2427 targetFolders.remove(op.mFolder.uri);
2428 }
2429 }
Mindy Pereira85c4a772012-07-30 10:47:26 -07002430 target.setRawFolders(Folder.getSerializedFolderString(targetFolders.values()));
Mindy Pereira00ffece2012-07-27 08:49:56 -07002431 mConversationListCursor.updateString(mContext, Conversation.listOf(target),
Mindy Pereira85c4a772012-07-30 10:47:26 -07002432 Conversation.UPDATE_FOLDER_COLUMN, target.getRawFoldersString());
Mindy Pereira8db7e402012-07-13 10:32:47 -07002433 }
Vikram Aggarwal7f602f72012-04-30 16:04:06 -07002434 refreshConversationList();
Mindy Pereiraf3a45562012-05-24 16:30:19 -07002435 if (mIsSelectedSet) {
2436 mSelectedSet.clear();
2437 }
Vikram Aggarwal7f602f72012-04-30 16:04:06 -07002438 }
Mindy Pereira8db7e402012-07-13 10:32:47 -07002439
Vikram Aggarwal7f602f72012-04-30 16:04:06 -07002440 /**
2441 * Returns true if this action has been performed, false otherwise.
Andy Huang839ada22012-07-20 15:48:40 -07002442 *
Vikram Aggarwal7f602f72012-04-30 16:04:06 -07002443 */
2444 private synchronized boolean isPerformed() {
2445 if (mCompleted) {
2446 return true;
Vikram Aggarwal41e6e712012-04-24 11:22:57 -07002447 }
Vikram Aggarwal7f602f72012-04-30 16:04:06 -07002448 mCompleted = true;
2449 return false;
Vikram Aggarwal41e6e712012-04-24 11:22:57 -07002450 }
2451 }
Vikram Aggarwal7f602f72012-04-30 16:04:06 -07002452
Vikram Aggarwald503df42012-05-11 10:13:35 -07002453 private final DestructiveAction getFolderChange(Collection<Conversation> target,
Mindy Pereira8db7e402012-07-13 10:32:47 -07002454 Collection<FolderOperation> folders, boolean isDestructive, boolean isBatch,
2455 boolean showUndo) {
Mindy Pereira06642fa2012-07-12 16:23:27 -07002456 final DestructiveAction da = new FolderDestruction(target, folders, isDestructive, isBatch,
Mindy Pereira01f30502012-08-14 10:30:51 -07002457 showUndo, R.id.change_folder);
2458 registerDestructiveAction(da);
2459 return da;
2460 }
2461
2462 @Override
2463 public final DestructiveAction getDeferredRemoveFolder(Collection<Conversation> target,
2464 Folder toRemove, boolean isDestructive, boolean isBatch,
2465 boolean showUndo) {
2466 Collection<FolderOperation> folderOps = new ArrayList<FolderOperation>();
2467 folderOps.add(new FolderOperation(toRemove, false));
2468 return new FolderDestruction(target, folderOps, isDestructive, isBatch,
2469 showUndo, R.id.remove_folder);
2470 }
2471
2472 private final DestructiveAction getRemoveFolder(Collection<Conversation> target,
2473 Folder toRemove, boolean isDestructive, boolean isBatch,
2474 boolean showUndo) {
2475 Collection<FolderOperation> folderOps = new ArrayList<FolderOperation>();
2476 folderOps.add(new FolderOperation(toRemove, false));
2477 DestructiveAction da = new FolderDestruction(target, folderOps, isDestructive, isBatch,
2478 showUndo, R.id.remove_folder);
Vikram Aggarwal41e6e712012-04-24 11:22:57 -07002479 registerDestructiveAction(da);
2480 return da;
2481 }
Vikram Aggarwal75daee52012-04-30 11:13:09 -07002482
Vikram Aggarwal4f4782b2012-05-30 08:39:09 -07002483 @Override
2484 public final void refreshConversationList() {
Vikram Aggarwal75daee52012-04-30 11:13:09 -07002485 final ConversationListFragment convList = getConversationListFragment();
2486 if (convList == null) {
2487 return;
2488 }
2489 convList.requestListRefresh();
2490 }
Andrew Sappersteinc2c9dc12012-07-02 18:17:32 -07002491
2492 protected final ActionClickedListener getUndoClickedListener(
2493 final AnimatedAdapter listAdapter) {
2494 return new ActionClickedListener() {
2495 @Override
2496 public void onActionClicked() {
2497 if (mAccount.undoUri != null) {
2498 // NOTE: We might want undo to return the messages affected, in which case
2499 // the resulting cursor might be interesting...
2500 // TODO: Use UIProvider.SEQUENCE_QUERY_PARAMETER to indicate the set of
2501 // commands to undo
2502 if (mConversationListCursor != null) {
2503 mConversationListCursor.undo(
2504 mActivity.getActivityContext(), mAccount.undoUri);
2505 }
2506 if (listAdapter != null) {
2507 listAdapter.setUndo(true);
2508 }
2509 }
2510 }
2511 };
2512 }
2513
Andrew Sapperstein9d7519d2012-07-16 14:03:53 -07002514 protected final void showErrorToast(final Folder folder, boolean replaceVisibleToast) {
Andrew Sappersteinc2c9dc12012-07-02 18:17:32 -07002515 mToastBar.setConversationMode(false);
Andrew Sapperstein00179f12012-08-09 15:15:40 -07002516
2517 ActionClickedListener listener = null;
2518 int actionTextResourceId;
2519 final int lastSyncResult = folder.lastSyncResult;
2520 switch (lastSyncResult) {
2521 case UIProvider.LastSyncResult.CONNECTION_ERROR:
2522 listener = getRetryClickedListener(folder);
2523 actionTextResourceId = R.string.retry;
2524 break;
2525 case UIProvider.LastSyncResult.AUTH_ERROR:
2526 listener = getSignInClickedListener();
2527 actionTextResourceId = R.string.signin;
2528 break;
2529 case UIProvider.LastSyncResult.SECURITY_ERROR:
2530 return; // Currently we do nothing for security errors.
2531 case UIProvider.LastSyncResult.STORAGE_ERROR:
2532 listener = getStorageErrorClickedListener();
2533 actionTextResourceId = R.string.info;
2534 break;
2535 case UIProvider.LastSyncResult.INTERNAL_ERROR:
2536 listener = getInternalErrorClickedListener();
2537 actionTextResourceId = R.string.report;
2538 break;
2539 default:
2540 return;
2541 }
Andrew Sappersteinc2c9dc12012-07-02 18:17:32 -07002542 mToastBar.show(
Andrew Sapperstein00179f12012-08-09 15:15:40 -07002543 listener,
Andrew Sapperstein5d420962012-07-12 16:43:10 -07002544 R.drawable.ic_alert_white,
Andrew Sappersteinc2c9dc12012-07-02 18:17:32 -07002545 Utils.getSyncStatusText(mActivity.getActivityContext(),
Andrew Sapperstein00179f12012-08-09 15:15:40 -07002546 lastSyncResult),
Andrew Sappersteinc2c9dc12012-07-02 18:17:32 -07002547 false, /* showActionIcon */
Andrew Sapperstein00179f12012-08-09 15:15:40 -07002548 actionTextResourceId,
Andrew Sapperstein9d7519d2012-07-16 14:03:53 -07002549 replaceVisibleToast,
2550 new ToastBarOperation(1, 0, ToastBarOperation.ERROR));
Andrew Sappersteinc2c9dc12012-07-02 18:17:32 -07002551 }
2552
Andrew Sapperstein00179f12012-08-09 15:15:40 -07002553 private ActionClickedListener getRetryClickedListener(final Folder folder) {
Andrew Sappersteinc2c9dc12012-07-02 18:17:32 -07002554 return new ActionClickedListener() {
2555 @Override
2556 public void onActionClicked() {
2557 final Uri uri = folder.refreshUri;
2558
2559 if (uri != null) {
Paul Westbrook4969e0c2012-08-20 14:38:39 -07002560 startAsyncRefreshTask(uri);
Andrew Sappersteinc2c9dc12012-07-02 18:17:32 -07002561 }
2562 }
2563 };
2564 }
Andrew Sapperstein00179f12012-08-09 15:15:40 -07002565
2566 private ActionClickedListener getSignInClickedListener() {
2567 return new ActionClickedListener() {
2568 @Override
2569 public void onActionClicked() {
Paul Westbrook122f7c22012-08-20 17:50:31 -07002570 promptUserForAuthentication(mAccount);
Andrew Sapperstein00179f12012-08-09 15:15:40 -07002571 }
2572 };
2573 }
2574
2575 private ActionClickedListener getStorageErrorClickedListener() {
2576 return new ActionClickedListener() {
2577 @Override
2578 public void onActionClicked() {
Paul Westbrook4969e0c2012-08-20 14:38:39 -07002579 showStorageErrorDialog();
Andrew Sapperstein00179f12012-08-09 15:15:40 -07002580 }
2581 };
2582 }
2583
Paul Westbrook4969e0c2012-08-20 14:38:39 -07002584 private void showStorageErrorDialog() {
2585 DialogFragment fragment = (DialogFragment)
2586 mFragmentManager.findFragmentByTag(SYNC_ERROR_DIALOG_FRAGMENT_TAG);
2587 if (fragment == null) {
2588 fragment = SyncErrorDialogFragment.newInstance();
2589 }
2590 fragment.show(mFragmentManager, SYNC_ERROR_DIALOG_FRAGMENT_TAG);
2591 }
2592
Andrew Sapperstein00179f12012-08-09 15:15:40 -07002593 private ActionClickedListener getInternalErrorClickedListener() {
2594 return new ActionClickedListener() {
2595 @Override
2596 public void onActionClicked() {
Paul Westbrook17beb0b2012-08-20 13:34:37 -07002597 Utils.sendFeedback(
2598 mActivity.getActivityContext(), mAccount, true /* reportingProblem */);
Andrew Sapperstein00179f12012-08-09 15:15:40 -07002599 }
2600 };
2601 }
Paul Westbrook4969e0c2012-08-20 14:38:39 -07002602
2603 @Override
2604 public void onFooterViewErrorActionClick(Folder folder, int errorStatus) {
2605 Uri uri = null;
2606 switch (errorStatus) {
2607 case UIProvider.LastSyncResult.CONNECTION_ERROR:
2608 if (folder != null && folder.refreshUri != null) {
2609 uri = folder.refreshUri;
2610 }
2611 break;
2612 case UIProvider.LastSyncResult.AUTH_ERROR:
Paul Westbrook122f7c22012-08-20 17:50:31 -07002613 promptUserForAuthentication(mAccount);
Paul Westbrook4969e0c2012-08-20 14:38:39 -07002614 return;
2615 case UIProvider.LastSyncResult.SECURITY_ERROR:
2616 return; // Currently we do nothing for security errors.
2617 case UIProvider.LastSyncResult.STORAGE_ERROR:
2618 showStorageErrorDialog();
2619 return;
2620 case UIProvider.LastSyncResult.INTERNAL_ERROR:
2621 Utils.sendFeedback(
2622 mActivity.getActivityContext(), mAccount, true /* reportingProblem */);
2623 return;
2624 default:
2625 return;
2626 }
2627
2628 if (uri != null) {
2629 startAsyncRefreshTask(uri);
2630 }
2631 }
2632
2633 @Override
2634 public void onFooterViewLoadMoreClick(Folder folder) {
2635 if (folder != null && folder.loadMoreUri != null) {
2636 startAsyncRefreshTask(folder.loadMoreUri);
2637 }
2638 }
2639
2640 private void startAsyncRefreshTask(Uri uri) {
2641 if (mFolderSyncTask != null) {
2642 mFolderSyncTask.cancel(true);
2643 }
2644 mFolderSyncTask = new AsyncRefreshTask(mActivity.getActivityContext(), uri);
2645 mFolderSyncTask.execute();
2646 }
Paul Westbrook122f7c22012-08-20 17:50:31 -07002647
2648 private void promptUserForAuthentication(Account account) {
Paul Westbrook429399b2012-08-24 11:19:17 -07002649 if (account != null && !Utils.isEmpty(account.reauthenticationIntentUri)) {
Paul Westbrook122f7c22012-08-20 17:50:31 -07002650 final Intent authenticationIntent =
2651 new Intent(Intent.ACTION_VIEW, account.reauthenticationIntentUri);
2652 mActivity.startActivityForResult(authenticationIntent, REAUTHENTICATE_REQUEST_CODE);
2653 }
2654 }
Vikram Aggarwal4a5c5302012-01-12 15:07:13 -08002655}