Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * Copyright (C) 2012 Google Inc. |
| 3 | * Licensed to The Android Open Source Project. |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | *******************************************************************************/ |
| 17 | |
| 18 | package com.android.mail.ui; |
| 19 | |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 20 | import android.app.ActionBar; |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 21 | import android.app.ActionBar.LayoutParams; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 22 | import android.app.Activity; |
Vikram Aggarwal | 54452ae | 2012-03-13 15:29:00 -0700 | [diff] [blame] | 23 | import android.app.AlertDialog; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 24 | import android.app.Dialog; |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 25 | import android.app.DialogFragment; |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 26 | import android.app.Fragment; |
Paul Westbrook | 2d50bcd | 2012-04-10 11:53:47 -0700 | [diff] [blame] | 27 | import android.app.FragmentManager; |
Andy Huang | f9a7348 | 2012-03-13 15:54:02 -0700 | [diff] [blame] | 28 | import android.app.LoaderManager; |
Vikram Aggarwal | e620a7a | 2012-03-28 13:16:14 -0700 | [diff] [blame] | 29 | import android.app.SearchManager; |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 30 | import android.content.ContentProviderOperation; |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 31 | import android.content.ContentResolver; |
Mindy Pereira | 6c2663d | 2012-07-20 15:37:29 -0700 | [diff] [blame] | 32 | import android.content.ContentValues; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 33 | import android.content.Context; |
Vikram Aggarwal | 7dedb95 | 2012-02-16 16:10:23 -0800 | [diff] [blame] | 34 | import android.content.CursorLoader; |
Vikram Aggarwal | 54452ae | 2012-03-13 15:29:00 -0700 | [diff] [blame] | 35 | import android.content.DialogInterface; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 36 | import android.content.Intent; |
Vikram Aggarwal | 7dedb95 | 2012-02-16 16:10:23 -0800 | [diff] [blame] | 37 | import android.content.Loader; |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 38 | import android.database.Cursor; |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 39 | import android.database.DataSetObservable; |
| 40 | import android.database.DataSetObserver; |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 41 | import android.net.Uri; |
Vikram Aggarwal | 27d89ad | 2012-06-12 13:38:40 -0700 | [diff] [blame] | 42 | import android.os.AsyncTask; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 43 | import android.os.Bundle; |
Mindy Pereira | 21ab490 | 2012-03-19 18:48:03 -0700 | [diff] [blame] | 44 | import android.os.Handler; |
Vikram Aggarwal | e620a7a | 2012-03-28 13:16:14 -0700 | [diff] [blame] | 45 | import android.provider.SearchRecentSuggestions; |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame^] | 46 | import android.text.TextUtils; |
Mindy Pereira | acf6039 | 2012-04-06 09:11:00 -0700 | [diff] [blame] | 47 | import android.view.DragEvent; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 48 | import android.view.KeyEvent; |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 49 | import android.view.LayoutInflater; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 50 | import android.view.Menu; |
Mindy Pereira | 28d5f72 | 2012-02-15 12:32:40 -0800 | [diff] [blame] | 51 | import android.view.MenuInflater; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 52 | import android.view.MenuItem; |
| 53 | import android.view.MotionEvent; |
Mindy Pereira | d3367499 | 2012-06-25 16:26:30 -0700 | [diff] [blame] | 54 | import android.view.View; |
Mindy Pereira | fd0c297 | 2012-03-27 13:50:39 -0700 | [diff] [blame] | 55 | import android.widget.Toast; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 56 | |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 57 | import com.android.mail.ConversationListContext; |
Andy Huang | f9a7348 | 2012-03-13 15:54:02 -0700 | [diff] [blame] | 58 | import com.android.mail.R; |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 59 | import com.android.mail.browse.ConversationCursor; |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 60 | import com.android.mail.browse.ConversationPagerController; |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 61 | import com.android.mail.browse.MessageCursor.ConversationMessage; |
Marc Blank | 7c9f6ac | 2012-04-02 13:27:19 -0700 | [diff] [blame] | 62 | import com.android.mail.browse.SelectedConversationsActionMenu; |
Andy Huang | 991f453 | 2012-08-14 13:32:55 -0700 | [diff] [blame] | 63 | import com.android.mail.browse.SyncErrorDialogFragment; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 64 | import com.android.mail.compose.ComposeActivity; |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 65 | import com.android.mail.providers.Account; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 66 | import com.android.mail.providers.Conversation; |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 67 | import com.android.mail.providers.ConversationInfo; |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 68 | import com.android.mail.providers.Folder; |
Vikram Aggarwal | 7a5d95a | 2012-07-27 16:24:54 -0700 | [diff] [blame] | 69 | import com.android.mail.providers.FolderWatcher; |
Paul Westbrook | c2074c4 | 2012-03-22 15:26:58 -0700 | [diff] [blame] | 70 | import com.android.mail.providers.MailAppProvider; |
Mindy Pereira | dac0054 | 2012-03-01 10:50:33 -0800 | [diff] [blame] | 71 | import com.android.mail.providers.Settings; |
Vikram Aggarwal | e620a7a | 2012-03-28 13:16:14 -0700 | [diff] [blame] | 72 | import com.android.mail.providers.SuggestionsProvider; |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 73 | import com.android.mail.providers.UIProvider; |
Mindy Pereira | 3cb28b5 | 2012-05-24 15:26:39 -0700 | [diff] [blame] | 74 | import com.android.mail.providers.UIProvider.AccountCapabilities; |
Paul Westbrook | 2388c5d | 2012-03-25 12:29:11 -0700 | [diff] [blame] | 75 | import com.android.mail.providers.UIProvider.AccountCursorExtraKeys; |
Mindy Pereira | c9d5918 | 2012-03-22 16:06:46 -0700 | [diff] [blame] | 76 | import com.android.mail.providers.UIProvider.ConversationColumns; |
Vikram Aggarwal | 54452ae | 2012-03-13 15:29:00 -0700 | [diff] [blame] | 77 | import com.android.mail.providers.UIProvider.FolderCapabilities; |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 78 | import com.android.mail.ui.ActionableToastBar.ActionClickedListener; |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 79 | import com.android.mail.utils.ContentProviderTask; |
Paul Westbrook | b334c90 | 2012-06-25 11:42:46 -0700 | [diff] [blame] | 80 | import com.android.mail.utils.LogTag; |
Mindy Pereira | 5cb0c3e | 2012-02-22 15:22:47 -0800 | [diff] [blame] | 81 | import com.android.mail.utils.LogUtils; |
Vikram Aggarwal | fa131a2 | 2012-02-02 13:56:22 -0800 | [diff] [blame] | 82 | import com.android.mail.utils.Utils; |
Paul Westbrook | ca08fc1 | 2012-07-31 12:01:15 -0700 | [diff] [blame] | 83 | import com.google.common.base.Objects; |
Paul Westbrook | 77eee62 | 2012-07-10 13:41:57 -0700 | [diff] [blame] | 84 | import com.google.common.collect.ImmutableList; |
Mindy Pereira | acf6039 | 2012-04-06 09:11:00 -0700 | [diff] [blame] | 85 | import com.google.common.collect.Lists; |
Marc Blank | 167faa8 | 2012-03-21 13:11:53 -0700 | [diff] [blame] | 86 | import com.google.common.collect.Sets; |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 87 | |
Marc Blank | 167faa8 | 2012-03-21 13:11:53 -0700 | [diff] [blame] | 88 | import java.util.ArrayList; |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 89 | import java.util.Collection; |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 90 | import java.util.Collections; |
Mindy Pereira | 8db7e40 | 2012-07-13 10:32:47 -0700 | [diff] [blame] | 91 | import java.util.HashMap; |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 92 | import java.util.Set; |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 93 | import java.util.TimerTask; |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 94 | |
| 95 | |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 96 | /** |
Mindy Pereira | 161f50d | 2012-02-28 15:47:19 -0800 | [diff] [blame] | 97 | * 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 Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 101 | * <p> |
Mindy Pereira | 161f50d | 2012-02-28 15:47:19 -0800 | [diff] [blame] | 102 | * 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 Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 111 | */ |
Vikram Aggarwal | 531488e | 2012-05-29 16:36:52 -0700 | [diff] [blame] | 112 | public abstract class AbstractActivityController implements ActivityController { |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 113 | // Keys for serialization of various information in Bundles. |
Vikram Aggarwal | cabd3f2 | 2012-04-19 10:14:41 -0700 | [diff] [blame] | 114 | /** Tag for {@link #mAccount} */ |
Vikram Aggarwal | 6c51158 | 2012-02-27 10:59:47 -0800 | [diff] [blame] | 115 | private static final String SAVED_ACCOUNT = "saved-account"; |
Vikram Aggarwal | cabd3f2 | 2012-04-19 10:14:41 -0700 | [diff] [blame] | 116 | /** Tag for {@link #mFolder} */ |
Mindy Pereira | 5e478d2 | 2012-03-26 18:04:58 -0700 | [diff] [blame] | 117 | private static final String SAVED_FOLDER = "saved-folder"; |
Vikram Aggarwal | cabd3f2 | 2012-04-19 10:14:41 -0700 | [diff] [blame] | 118 | /** Tag for {@link #mCurrentConversation} */ |
Mindy Pereira | 26f23fc | 2012-03-27 10:26:04 -0700 | [diff] [blame] | 119 | private static final String SAVED_CONVERSATION = "saved-conversation"; |
Vikram Aggarwal | cabd3f2 | 2012-04-19 10:14:41 -0700 | [diff] [blame] | 120 | /** Tag for {@link #mSelectedSet} */ |
| 121 | private static final String SAVED_SELECTED_SET = "saved-selected-set"; |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame^] | 122 | /** Tag for {@link ActionableToastBar#getOperation()} */ |
Mindy Pereira | d3367499 | 2012-06-25 16:26:30 -0700 | [diff] [blame] | 123 | private static final String SAVED_TOAST_BAR_OP = "saved-toast-bar-op"; |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame^] | 124 | /** Tag for {@link #mFolderListFolder} */ |
| 125 | private static final String SAVED_HIERARCHICAL_FOLDER = "saved-hierarchical-folder"; |
Vikram Aggarwal | f334140 | 2012-08-07 10:09:38 -0700 | [diff] [blame] | 126 | /** Tag for {@link ConversationListContext#searchQuery} */ |
| 127 | private static final String SAVED_QUERY = "saved-query"; |
Mindy Pereira | 5cb0c3e | 2012-02-22 15:22:47 -0800 | [diff] [blame] | 128 | |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 129 | /** 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 Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 132 | public static final String TAG_CONVERSATION_LIST = "tag-conversation-list"; |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 133 | /** Tag used when loading a folder list fragment. */ |
| 134 | protected static final String TAG_FOLDER_LIST = "tag-folder-list"; |
| 135 | |
Vikram Aggarwal | d7a12cd | 2012-02-03 09:36:20 -0800 | [diff] [blame] | 136 | protected Account mAccount; |
Mindy Pereira | 12a4d80 | 2012-06-22 09:24:43 -0700 | [diff] [blame] | 137 | protected Folder mFolder; |
Andy Huang | 6681e54 | 2012-06-14 14:36:45 -0700 | [diff] [blame] | 138 | protected MailActionBarView mActionBarView; |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 139 | protected final ControllableActivity mActivity; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 140 | protected final Context mContext; |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 141 | private final FragmentManager mFragmentManager; |
Vikram Aggarwal | ec5cbf7 | 2012-03-08 15:10:35 -0800 | [diff] [blame] | 142 | protected final RecentFolderList mRecentFolderList; |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 143 | protected ConversationListContext mConvListContext; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 144 | protected Conversation mCurrentConversation; |
Vikram Aggarwal | d7a12cd | 2012-02-03 09:36:20 -0800 | [diff] [blame] | 145 | |
Paul Westbrook | 6ead20d | 2012-03-19 14:48:14 -0700 | [diff] [blame] | 146 | /** A {@link android.content.BroadcastReceiver} that suppresses new e-mail notifications. */ |
| 147 | private SuppressNotificationReceiver mNewEmailReceiver = null; |
| 148 | |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 149 | protected Handler mHandler = new Handler(); |
Mindy Pereira | fa995b4 | 2012-07-25 12:06:13 -0700 | [diff] [blame] | 150 | |
Vikram Aggarwal | fa131a2 | 2012-02-02 13:56:22 -0800 | [diff] [blame] | 151 | /** |
Mindy Pereira | 161f50d | 2012-02-28 15:47:19 -0800 | [diff] [blame] | 152 | * 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 Aggarwal | fa131a2 | 2012-02-02 13:56:22 -0800 | [diff] [blame] | 155 | */ |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 156 | protected final ViewMode mViewMode; |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 157 | protected ContentResolver mResolver; |
Vikram Aggarwal | 7dedb95 | 2012-02-16 16:10:23 -0800 | [diff] [blame] | 158 | protected boolean isLoaderInitialized = false; |
Mindy Pereira | b7b33e0 | 2012-02-21 15:32:19 -0800 | [diff] [blame] | 159 | private AsyncRefreshTask mAsyncRefreshTask; |
Mindy Pereira | 5cb0c3e | 2012-02-22 15:22:47 -0800 | [diff] [blame] | 160 | |
Andy Huang | 4e0158f | 2012-08-07 21:06:01 -0700 | [diff] [blame] | 161 | private boolean mDestroyed; |
| 162 | |
Andy Huang | 1ee96b2 | 2012-08-24 20:19:53 -0700 | [diff] [blame] | 163 | /** |
| 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 Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 173 | private final Set<Uri> mCurrentAccountUris = Sets.newHashSet(); |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 174 | protected ConversationCursor mConversationListCursor; |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 175 | 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 Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 180 | LogUtils.d(LOG_TAG, "IN AAC.register(List)Observer: %s before=%d after=%d", observer, |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 181 | count, mObservers.size()); |
| 182 | } |
| 183 | @Override |
| 184 | public void unregisterObserver(DataSetObserver observer) { |
| 185 | final int count = mObservers.size(); |
| 186 | super.unregisterObserver(observer); |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 187 | LogUtils.d(LOG_TAG, "IN AAC.unregister(List)Observer: %s before=%d after=%d", observer, |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 188 | count, mObservers.size()); |
| 189 | } |
| 190 | }; |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 191 | |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 192 | private RefreshTimerTask mConversationListRefreshTask; |
Marc Blank | e1d1b07 | 2012-04-13 17:29:16 -0700 | [diff] [blame] | 193 | |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 194 | /** 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 Aggarwal | 7d81600 | 2012-04-17 17:06:41 -0700 | [diff] [blame] | 211 | |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 212 | /** |
| 213 | * Selected conversations, if any. |
| 214 | */ |
Andy Huang | 4556a44 | 2012-03-30 16:42:05 -0700 | [diff] [blame] | 215 | private final ConversationSelectionSet mSelectedSet = new ConversationSelectionSet(); |
Mindy Pereira | dac0054 | 2012-03-01 10:50:33 -0800 | [diff] [blame] | 216 | |
Paul Westbrook | c7a070f | 2012-04-12 01:46:41 -0700 | [diff] [blame] | 217 | private final int mFolderItemUpdateDelayMs; |
| 218 | |
Vikram Aggarwal | 135fd02 | 2012-04-11 14:44:15 -0700 | [diff] [blame] | 219 | /** Keeps track of selected and unselected conversations */ |
Paul Westbrook | 937c94f | 2012-08-16 13:01:18 -0700 | [diff] [blame] | 220 | final protected ConversationPositionTracker mTracker; |
Vikram Aggarwal | af1ee0c | 2012-04-12 17:13:13 -0700 | [diff] [blame] | 221 | |
Vikram Aggarwal | e128fc2 | 2012-04-04 12:33:34 -0700 | [diff] [blame] | 222 | /** |
| 223 | * Action menu associated with the selected set. |
| 224 | */ |
| 225 | SelectedConversationsActionMenu mCabActionMenu; |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 226 | protected ActionableToastBar mToastBar; |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 227 | protected ConversationPagerController mPagerController; |
Mindy Pereira | ab48636 | 2012-03-21 18:18:53 -0700 | [diff] [blame] | 228 | |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 229 | // 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 Huang | 090db1e | 2012-07-25 13:25:28 -0700 | [diff] [blame] | 234 | private final DataSetObservable mFolderObservable = new DataSetObservable(); |
| 235 | |
Paul Westbrook | b334c90 | 2012-06-25 11:42:46 -0700 | [diff] [blame] | 236 | protected static final String LOG_TAG = LogTag.getLogTag(); |
Vikram Aggarwal | fa4b47e | 2012-03-09 13:02:46 -0800 | [diff] [blame] | 237 | /** Constants used to differentiate between the types of loaders. */ |
| 238 | private static final int LOADER_ACCOUNT_CURSOR = 0; |
Vikram Aggarwal | fa4b47e | 2012-03-09 13:02:46 -0800 | [diff] [blame] | 239 | private static final int LOADER_FOLDER_CURSOR = 2; |
| 240 | private static final int LOADER_RECENT_FOLDERS = 3; |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 241 | private static final int LOADER_CONVERSATION_LIST = 4; |
Mindy Pereira | ab48636 | 2012-03-21 18:18:53 -0700 | [diff] [blame] | 242 | private static final int LOADER_ACCOUNT_INBOX = 5; |
| 243 | private static final int LOADER_SEARCH = 6; |
Paul Westbrook | 2d50bcd | 2012-04-10 11:53:47 -0700 | [diff] [blame] | 244 | private static final int LOADER_ACCOUNT_UPDATE_CURSOR = 7; |
Vikram Aggarwal | 7a5d95a | 2012-07-27 16:24:54 -0700 | [diff] [blame] | 245 | /** |
| 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 Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 255 | |
Paul Westbrook | 2388c5d | 2012-03-25 12:29:11 -0700 | [diff] [blame] | 256 | private static final int ADD_ACCOUNT_REQUEST_CODE = 1; |
Paul Westbrook | 122f7c2 | 2012-08-20 17:50:31 -0700 | [diff] [blame] | 257 | private static final int REAUTHENTICATE_REQUEST_CODE = 2; |
Paul Westbrook | 2388c5d | 2012-03-25 12:29:11 -0700 | [diff] [blame] | 258 | |
Vikram Aggarwal | e8a8532 | 2012-04-24 09:01:38 -0700 | [diff] [blame] | 259 | /** The pending destructive action to be carried out before swapping the conversation cursor.*/ |
| 260 | private DestructiveAction mPendingDestruction; |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 261 | protected AsyncRefreshTask mFolderSyncTask; |
Mindy Pereira | 49e5dbe | 2012-07-12 11:47:54 -0700 | [diff] [blame] | 262 | // 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 Pereira | c975e84 | 2012-07-16 09:15:00 -0700 | [diff] [blame] | 266 | private Folder mFolderListFolder; |
mindyp | 5390fca | 2012-08-22 12:12:25 -0700 | [diff] [blame] | 267 | private boolean mIsDragHappening; |
mindyp | ead5039 | 2012-08-23 11:03:53 -0700 | [diff] [blame] | 268 | private int mShowUndoBarDelay; |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 269 | public static final String SYNC_ERROR_DIALOG_FRAGMENT_TAG = "SyncErrorDialogFragment"; |
Vikram Aggarwal | e8a8532 | 2012-04-24 09:01:38 -0700 | [diff] [blame] | 270 | |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 271 | public AbstractActivityController(MailActivity activity, ViewMode viewMode) { |
| 272 | mActivity = activity; |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 273 | mFragmentManager = mActivity.getFragmentManager(); |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 274 | mViewMode = viewMode; |
| 275 | mContext = activity.getApplicationContext(); |
Vikram Aggarwal | 025eba8 | 2012-05-08 10:45:30 -0700 | [diff] [blame] | 276 | mRecentFolderList = new RecentFolderList(mContext); |
Paul Westbrook | 937c94f | 2012-08-16 13:01:18 -0700 | [diff] [blame] | 277 | mTracker = new ConversationPositionTracker(this); |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 278 | // 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 Westbrook | c7a070f | 2012-04-12 01:46:41 -0700 | [diff] [blame] | 281 | |
| 282 | mFolderItemUpdateDelayMs = |
| 283 | mContext.getResources().getInteger(R.integer.folder_item_refresh_delay_ms); |
mindyp | ead5039 | 2012-08-23 11:03:53 -0700 | [diff] [blame] | 284 | mShowUndoBarDelay = |
| 285 | mContext.getResources().getInteger(R.integer.show_undo_bar_delay_ms); |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 286 | } |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 287 | |
| 288 | @Override |
Vikram Aggarwal | e9a8103 | 2012-02-22 13:15:35 -0800 | [diff] [blame] | 289 | public Account getCurrentAccount() { |
| 290 | return mAccount; |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | @Override |
| 294 | public ConversationListContext getCurrentListContext() { |
Vikram Aggarwal | e9a8103 | 2012-02-22 13:15:35 -0800 | [diff] [blame] | 295 | return mConvListContext; |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | @Override |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 299 | public String getHelpContext() { |
Paul Westbrook | 30745b6 | 2012-08-19 14:10:32 -0700 | [diff] [blame] | 300 | 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 Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 310 | } |
| 311 | |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 312 | @Override |
Vikram Aggarwal | ef7c992 | 2012-04-23 12:35:04 -0700 | [diff] [blame] | 313 | public final ConversationCursor getConversationListCursor() { |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 314 | return mConversationListCursor; |
| 315 | } |
| 316 | |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 317 | /** |
Vikram Aggarwal | 34a6501 | 2012-04-17 12:39:06 -0700 | [diff] [blame] | 318 | * 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 Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 330 | * Get the conversation list fragment for this activity. If the conversation list fragment |
| 331 | * is not attached, this method returns null |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 332 | * |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 333 | */ |
| 334 | protected ConversationListFragment getConversationListFragment() { |
| 335 | final Fragment fragment = mFragmentManager.findFragmentByTag(TAG_CONVERSATION_LIST); |
Vikram Aggarwal | 34a6501 | 2012-04-17 12:39:06 -0700 | [diff] [blame] | 336 | if (isValidFragment(fragment)) { |
| 337 | return (ConversationListFragment) fragment; |
Andy Huang | 9585d78 | 2012-04-16 19:45:04 -0700 | [diff] [blame] | 338 | } |
Vikram Aggarwal | 34a6501 | 2012-04-17 12:39:06 -0700 | [diff] [blame] | 339 | return null; |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | /** |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 343 | * Returns the folder list fragment attached with this activity. If no such fragment is attached |
| 344 | * this method returns null. |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 345 | * |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 346 | */ |
| 347 | protected FolderListFragment getFolderListFragment() { |
| 348 | final Fragment fragment = mFragmentManager.findFragmentByTag(TAG_FOLDER_LIST); |
Vikram Aggarwal | 34a6501 | 2012-04-17 12:39:06 -0700 | [diff] [blame] | 349 | if (isValidFragment(fragment)) { |
| 350 | return (FolderListFragment) fragment; |
Andy Huang | 9585d78 | 2012-04-16 19:45:04 -0700 | [diff] [blame] | 351 | } |
Vikram Aggarwal | 34a6501 | 2012-04-17 12:39:06 -0700 | [diff] [blame] | 352 | return null; |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 355 | /** |
Mindy Pereira | 161f50d | 2012-02-28 15:47:19 -0800 | [diff] [blame] | 356 | * Initialize the action bar. This is not visible to OnePaneController and |
| 357 | * TwoPaneController so they cannot override this behavior. |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 358 | */ |
Vikram Aggarwal | abd24d8 | 2012-04-26 13:23:14 -0700 | [diff] [blame] | 359 | private void initializeActionBar() { |
| 360 | final ActionBar actionBar = mActivity.getActionBar(); |
Andy Huang | 5895f7b | 2012-06-01 17:07:20 -0700 | [diff] [blame] | 361 | if (actionBar == null) { |
| 362 | return; |
Vikram Aggarwal | abd24d8 | 2012-04-26 13:23:14 -0700 | [diff] [blame] | 363 | } |
Andy Huang | 5895f7b | 2012-06-01 17:07:20 -0700 | [diff] [blame] | 364 | |
| 365 | // be sure to inherit from the ActionBar theme when inflating |
| 366 | final LayoutInflater inflater = LayoutInflater.from(actionBar.getThemedContext()); |
Mindy Pereira | 82faec7 | 2012-06-14 17:21:50 -0700 | [diff] [blame] | 367 | 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 Huang | 5895f7b | 2012-06-01 17:07:20 -0700 | [diff] [blame] | 371 | mActionBarView.initialize(mActivity, this, mViewMode, actionBar, mRecentFolderList); |
Vikram Aggarwal | abd24d8 | 2012-04-26 13:23:14 -0700 | [diff] [blame] | 372 | } |
| 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 Aggarwal | ec5cbf7 | 2012-03-08 15:10:35 -0800 | [diff] [blame] | 380 | actionBar.setCustomView(mActionBarView, new ActionBar.LayoutParams( |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 381 | LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame^] | 382 | // 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 Aggarwal | abd24d8 | 2012-04-26 13:23:14 -0700 | [diff] [blame] | 387 | mActionBarView.attach(); |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 388 | } |
Vikram Aggarwal | abd24d8 | 2012-04-26 13:23:14 -0700 | [diff] [blame] | 389 | mViewMode.addListener(mActionBarView); |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | /** |
Mindy Pereira | 161f50d | 2012-02-28 15:47:19 -0800 | [diff] [blame] | 393 | * 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 Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 397 | */ |
| 398 | protected abstract boolean isConversationListVisible(); |
| 399 | |
Vikram Aggarwal | eb7d329 | 2012-04-20 17:07:20 -0700 | [diff] [blame] | 400 | /** |
| 401 | * Switch the current account to the one provided as an argument to the method. |
Vikram Aggarwal | 66bc6ed | 2012-07-31 09:59:55 -0700 | [diff] [blame] | 402 | * @param account new account |
| 403 | * @param shouldReloadInbox whether the default inbox should be reloaded. |
Vikram Aggarwal | eb7d329 | 2012-04-20 17:07:20 -0700 | [diff] [blame] | 404 | */ |
Vikram Aggarwal | 66bc6ed | 2012-07-31 09:59:55 -0700 | [diff] [blame] | 405 | private void switchAccount(Account account, boolean shouldReloadInbox){ |
Vikram Aggarwal | eb7d329 | 2012-04-20 17:07:20 -0700 | [diff] [blame] | 406 | // Current account is different from the new account, restart loaders and show |
| 407 | // the account Inbox. |
| 408 | mAccount = account; |
Vikram Aggarwal | edc137c | 2012-04-24 13:40:58 -0700 | [diff] [blame] | 409 | LogUtils.d(LOG_TAG, "AbstractActivityController.switchAccount(): mAccount = %s", |
| 410 | mAccount.uri); |
Vikram Aggarwal | eb7d329 | 2012-04-20 17:07:20 -0700 | [diff] [blame] | 411 | cancelRefreshTask(); |
Vikram Aggarwal | 2e0a236 | 2012-08-22 09:05:49 -0700 | [diff] [blame] | 412 | // 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 Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 417 | updateSettings(); |
Vikram Aggarwal | 66bc6ed | 2012-07-31 09:59:55 -0700 | [diff] [blame] | 418 | if (shouldReloadInbox) { |
| 419 | loadAccountInbox(); |
| 420 | } |
Vikram Aggarwal | eb7d329 | 2012-04-20 17:07:20 -0700 | [diff] [blame] | 421 | 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 Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 428 | @Override |
Mindy Pereira | 28e0c34 | 2012-02-17 15:05:13 -0800 | [diff] [blame] | 429 | public void onAccountChanged(Account account) { |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 430 | LogUtils.d(LOG_TAG, "onAccountChanged (%s) called.", account); |
| 431 | // Is the account or account settings different from the existing account? |
Vikram Aggarwal | 66bc6ed | 2012-07-31 09:59:55 -0700 | [diff] [blame] | 432 | 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 Pereira | fa20c1a | 2012-07-23 13:00:02 -0700 | [diff] [blame] | 436 | if (account != null) { |
Mindy Pereira | fa995b4 | 2012-07-25 12:06:13 -0700 | [diff] [blame] | 437 | 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 Pereira | fa20c1a | 2012-07-23 13:00:02 -0700 | [diff] [blame] | 444 | } |
mindyp | c6adce3 | 2012-08-22 18:46:42 -0700 | [diff] [blame] | 445 | if (accountChanged) { |
| 446 | commitDestructiveActions(false); |
| 447 | } |
Vikram Aggarwal | 66bc6ed | 2012-07-31 09:59:55 -0700 | [diff] [blame] | 448 | switchAccount(account, accountChanged); |
Mindy Pereira | 28d5f72 | 2012-02-15 12:32:40 -0800 | [diff] [blame] | 449 | } |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 450 | } |
| 451 | |
Vikram Aggarwal | e6340bc | 2012-03-26 15:57:09 -0700 | [diff] [blame] | 452 | /** |
Vikram Aggarwal | eb7d329 | 2012-04-20 17:07:20 -0700 | [diff] [blame] | 453 | * Changes the settings for the current account. The new settings are provided as a parameter. |
| 454 | * @param settings |
| 455 | */ |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 456 | public void updateSettings() { |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 457 | mAccountObservers.notifyChanged(); |
Mindy Pereira | 12a676a | 2012-03-23 13:00:22 -0700 | [diff] [blame] | 458 | resetActionBarIcon(); |
Vikram Aggarwal | eb7d329 | 2012-04-20 17:07:20 -0700 | [diff] [blame] | 459 | mActivity.invalidateOptionsMenu(); |
| 460 | // If the user was viewing the default Inbox here, and the new setting contains a different |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 461 | // default Inbox, we don't want to load a different folder here. So do not change the |
| 462 | // current folder. |
Mindy Pereira | dac0054 | 2012-03-01 10:50:33 -0800 | [diff] [blame] | 463 | } |
| 464 | |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 465 | /** |
| 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 Pereira | efe3d25 | 2012-03-01 14:20:44 -0800 | [diff] [blame] | 470 | @Override |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 471 | public void registerAccountObserver(DataSetObserver obs) { |
| 472 | mAccountObservers.registerObserver(obs); |
Mindy Pereira | efe3d25 | 2012-03-01 14:20:44 -0800 | [diff] [blame] | 473 | } |
| 474 | |
Vikram Aggarwal | 7d81600 | 2012-04-17 17:06:41 -0700 | [diff] [blame] | 475 | /** |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 476 | * Removes a listener from receiving current account changes. |
Vikram Aggarwal | 7d81600 | 2012-04-17 17:06:41 -0700 | [diff] [blame] | 477 | * Must happen in the UI thread. |
| 478 | */ |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 479 | @Override |
| 480 | public void unregisterAccountObserver(DataSetObserver obs) { |
| 481 | mAccountObservers.unregisterObserver(obs); |
Vikram Aggarwal | 7d81600 | 2012-04-17 17:06:41 -0700 | [diff] [blame] | 482 | } |
| 483 | |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 484 | @Override |
| 485 | public Account getAccount() { |
| 486 | return mAccount; |
Vikram Aggarwal | 7d81600 | 2012-04-17 17:06:41 -0700 | [diff] [blame] | 487 | } |
| 488 | |
Mindy Pereira | e082839 | 2012-03-08 10:38:40 -0800 | [diff] [blame] | 489 | private void fetchSearchFolder(Intent intent) { |
Mindy Pereira | ab48636 | 2012-03-21 18:18:53 -0700 | [diff] [blame] | 490 | Bundle args = new Bundle(); |
| 491 | args.putString(ConversationListContext.EXTRA_SEARCH_QUERY, intent |
Mindy Pereira | e082839 | 2012-03-08 10:38:40 -0800 | [diff] [blame] | 492 | .getStringExtra(ConversationListContext.EXTRA_SEARCH_QUERY)); |
Mindy Pereira | ab48636 | 2012-03-21 18:18:53 -0700 | [diff] [blame] | 493 | mActivity.getLoaderManager().restartLoader(LOADER_SEARCH, args, this); |
Mindy Pereira | e082839 | 2012-03-08 10:38:40 -0800 | [diff] [blame] | 494 | } |
| 495 | |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 496 | @Override |
Mindy Pereira | 28e0c34 | 2012-02-17 15:05:13 -0800 | [diff] [blame] | 497 | public void onFolderChanged(Folder folder) { |
Vikram Aggarwal | f334140 | 2012-08-07 10:09:38 -0700 | [diff] [blame] | 498 | changeFolder(folder, null); |
| 499 | } |
| 500 | |
| 501 | /** |
Vikram Aggarwal | 203dc00 | 2012-08-23 13:59:04 -0700 | [diff] [blame] | 502 | * 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 Aggarwal | 203dc00 | 2012-08-23 13:59:04 -0700 | [diff] [blame] | 513 | cancelRefreshTask(); |
| 514 | } |
| 515 | |
| 516 | /** |
Vikram Aggarwal | f334140 | 2012-08-07 10:09:38 -0700 | [diff] [blame] | 517 | * 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 Aggarwal | 203dc00 | 2012-08-23 13:59:04 -0700 | [diff] [blame] | 522 | if (!Objects.equal(mFolder, folder)) { |
| 523 | commitDestructiveActions(false); |
| 524 | } |
Mindy Pereira | a1f9998 | 2012-07-16 16:32:15 -0700 | [diff] [blame] | 525 | if (folder != null && !folder.equals(mFolder) |
| 526 | || (mViewMode.getMode() != ViewMode.CONVERSATION_LIST)) { |
Vikram Aggarwal | 203dc00 | 2012-08-23 13:59:04 -0700 | [diff] [blame] | 527 | setListContext(folder, query); |
Mindy Pereira | 28e0c34 | 2012-02-17 15:05:13 -0800 | [diff] [blame] | 528 | showConversationList(mConvListContext); |
Mindy Pereira | 28e0c34 | 2012-02-17 15:05:13 -0800 | [diff] [blame] | 529 | } |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 530 | } |
| 531 | |
Mindy Pereira | 13c12a6 | 2012-05-31 15:41:08 -0700 | [diff] [blame] | 532 | @Override |
Mindy Pereira | 505df5f | 2012-06-19 17:57:17 -0700 | [diff] [blame] | 533 | public void onFolderSelected(Folder folder) { |
Mindy Pereira | 13c12a6 | 2012-05-31 15:41:08 -0700 | [diff] [blame] | 534 | onFolderChanged(folder); |
| 535 | } |
| 536 | |
Vikram Aggarwal | 792ccba | 2012-03-27 13:46:57 -0700 | [diff] [blame] | 537 | /** |
| 538 | * Update the recent folders. This only needs to be done once when accessing a new folder. |
| 539 | */ |
Paul Westbrook | 7ebdfd0 | 2012-03-21 15:55:30 -0700 | [diff] [blame] | 540 | private void updateRecentFolderList() { |
Mindy Pereira | ab48636 | 2012-03-21 18:18:53 -0700 | [diff] [blame] | 541 | if (mFolder != null) { |
Marc Blank | 2675dbc | 2012-04-03 10:17:13 -0700 | [diff] [blame] | 542 | mRecentFolderList.touchFolder(mFolder, mAccount); |
Mindy Pereira | ab48636 | 2012-03-21 18:18:53 -0700 | [diff] [blame] | 543 | } |
Paul Westbrook | 7ebdfd0 | 2012-03-21 15:55:30 -0700 | [diff] [blame] | 544 | } |
| 545 | |
Mindy Pereira | ab48636 | 2012-03-21 18:18:53 -0700 | [diff] [blame] | 546 | // TODO(mindyp): set this up to store a copy of the folder as a transient |
| 547 | // field in the account. |
Vikram Aggarwal | eb7d329 | 2012-04-20 17:07:20 -0700 | [diff] [blame] | 548 | @Override |
| 549 | public void loadAccountInbox() { |
Vikram Aggarwal | 94c94de | 2012-04-04 15:38:28 -0700 | [diff] [blame] | 550 | restartOptionalLoader(LOADER_ACCOUNT_INBOX); |
Mindy Pereira | f6acdad | 2012-03-15 11:21:13 -0700 | [diff] [blame] | 551 | } |
| 552 | |
Vikram Aggarwal | ec5cbf7 | 2012-03-08 15:10:35 -0800 | [diff] [blame] | 553 | /** Set the current folder */ |
Mindy Pereira | 11e3596 | 2012-06-01 14:49:46 -0700 | [diff] [blame] | 554 | private void updateFolder(Folder folder) { |
Mindy Pereira | 5cb0c3e | 2012-02-22 15:22:47 -0800 | [diff] [blame] | 555 | // Start watching folder for sync status. |
Mindy Pereira | c975e84 | 2012-07-16 09:15:00 -0700 | [diff] [blame] | 556 | boolean wasNull = mFolder == null; |
Paul Westbrook | 4bfce9a | 2012-08-07 22:54:42 -0700 | [diff] [blame] | 557 | if (folder != null && !folder.equals(mFolder) && folder.isInitialized()) { |
Vikram Aggarwal | eb7d329 | 2012-04-20 17:07:20 -0700 | [diff] [blame] | 558 | LogUtils.d(LOG_TAG, "AbstractActivityController.setFolder(%s)", folder.name); |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 559 | final LoaderManager lm = mActivity.getLoaderManager(); |
Vikram Aggarwal | 7a5d95a | 2012-07-27 16:24:54 -0700 | [diff] [blame] | 560 | mFolder = folder; |
Mindy Pereira | f9323cd | 2012-02-29 13:47:09 -0800 | [diff] [blame] | 561 | mActionBarView.setFolder(mFolder); |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 562 | |
Vikram Aggarwal | 2e0a236 | 2012-08-22 09:05:49 -0700 | [diff] [blame] | 563 | // Stop listening to changes to the previous folder. |
| 564 | if (mPagerController != null) { |
| 565 | mPagerController.stopListening(); |
| 566 | } |
| 567 | |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 568 | // 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 Pereira | 11e3596 | 2012-06-01 14:49:46 -0700 | [diff] [blame] | 572 | // If there was not already an instance of the loader, init it. |
| 573 | if (lm.getLoader(LOADER_FOLDER_CURSOR) == null) { |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 574 | lm.initLoader(LOADER_FOLDER_CURSOR, null, this); |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 575 | } else { |
| 576 | lm.restartLoader(LOADER_FOLDER_CURSOR, null, this); |
Mindy Pereira | c975e84 | 2012-07-16 09:15:00 -0700 | [diff] [blame] | 577 | } |
| 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 |
mindyp | faa87d5 | 2012-08-20 09:35:03 -0700 | [diff] [blame] | 587 | lm.destroyLoader(LOADER_CONVERSATION_LIST); |
| 588 | lm.initLoader(LOADER_CONVERSATION_LIST, null, mListCursorCallbacks); |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 589 | } |
Paul Westbrook | 4bfce9a | 2012-08-07 22:54:42 -0700 | [diff] [blame] | 590 | } else if (!folder.isInitialized()) { |
Paul Westbrook | 3c0f419 | 2012-08-09 12:15:42 -0700 | [diff] [blame] | 591 | LogUtils.e(LOG_TAG, new Error(), "Uninitialized Folder %s in setFolder.", folder); |
Mindy Pereira | 5cb0c3e | 2012-02-22 15:22:47 -0800 | [diff] [blame] | 592 | } |
| 593 | } |
| 594 | |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 595 | @Override |
Andy Huang | 090db1e | 2012-07-25 13:25:28 -0700 | [diff] [blame] | 596 | public Folder getFolder() { |
| 597 | return mFolder; |
| 598 | } |
| 599 | |
| 600 | @Override |
Mindy Pereira | c975e84 | 2012-07-16 09:15:00 -0700 | [diff] [blame] | 601 | public Folder getHierarchyFolder() { |
| 602 | return mFolderListFolder; |
| 603 | } |
| 604 | |
| 605 | @Override |
| 606 | public void setHierarchyFolder(Folder folder) { |
| 607 | mFolderListFolder = folder; |
Mindy Pereira | 23aadfd | 2012-05-25 11:24:33 -0700 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | @Override |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 611 | public void onActivityResult(int requestCode, int resultCode, Intent data) { |
Paul Westbrook | 122f7c2 | 2012-08-20 17:50:31 -0700 | [diff] [blame] | 612 | 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 Westbrook | 2388c5d | 2012-03-25 12:29:11 -0700 | [diff] [blame] | 633 | } |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | @Override |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 637 | public void onConversationListVisibilityChanged(boolean visible) { |
Paul Westbrook | 9f119c7 | 2012-04-24 16:10:59 -0700 | [diff] [blame] | 638 | if (mConversationListCursor != null) { |
| 639 | // The conversation list is visible. |
| 640 | Utils.setConversationCursorVisibility(mConversationListCursor, visible); |
| 641 | } |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 642 | } |
| 643 | |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 644 | /** |
Vikram Aggarwal | 7dd054e | 2012-05-21 14:43:10 -0700 | [diff] [blame] | 645 | * Called when a conversation is visible. Child classes must call the super class implementation |
| 646 | * before performing local computation. |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 647 | */ |
| 648 | @Override |
| 649 | public void onConversationVisibilityChanged(boolean visible) { |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 650 | return; |
| 651 | } |
| 652 | |
| 653 | @Override |
Vikram Aggarwal | d7a12cd | 2012-02-03 09:36:20 -0800 | [diff] [blame] | 654 | public boolean onCreate(Bundle savedState) { |
Vikram Aggarwal | abd24d8 | 2012-04-26 13:23:14 -0700 | [diff] [blame] | 655 | initializeActionBar(); |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 656 | // Allow shortcut keys to function for the ActionBar and menus. |
| 657 | mActivity.setDefaultKeyMode(Activity.DEFAULT_KEYS_SHORTCUT); |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 658 | mResolver = mActivity.getContentResolver(); |
Paul Westbrook | 6ead20d | 2012-03-19 14:48:14 -0700 | [diff] [blame] | 659 | mNewEmailReceiver = new SuppressNotificationReceiver(); |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 660 | mRecentFolderList.initialize(mActivity); |
Paul Westbrook | 6ead20d | 2012-03-19 14:48:14 -0700 | [diff] [blame] | 661 | |
Mindy Pereira | 161f50d | 2012-02-28 15:47:19 -0800 | [diff] [blame] | 662 | // All the individual UI components listen for ViewMode changes. This |
Mindy Pereira | b849dfb | 2012-03-07 18:13:15 -0800 | [diff] [blame] | 663 | // simplifies the amount of logic in the AbstractActivityController, but increases the |
| 664 | // possibility of timing-related bugs. |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 665 | mViewMode.addListener(this); |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 666 | mPagerController = new ConversationPagerController(mActivity, this); |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 667 | mToastBar = (ActionableToastBar) mActivity.findViewById(R.id.toast_bar); |
Vikram Aggarwal | ada5178 | 2012-04-26 14:18:31 -0700 | [diff] [blame] | 668 | attachActionBar(); |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 669 | |
| 670 | final Intent intent = mActivity.getIntent(); |
Vikram Aggarwal | abd24d8 | 2012-04-26 13:23:14 -0700 | [diff] [blame] | 671 | // Immediately handle a clean launch with intent, and any state restoration |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 672 | // 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 Aggarwal | f334140 | 2012-08-07 10:09:38 -0700 | [diff] [blame] | 681 | final Folder folder = (Folder) savedState.getParcelable(SAVED_FOLDER); |
Vikram Aggarwal | 203dc00 | 2012-08-23 13:59:04 -0700 | [diff] [blame] | 682 | final String query = savedState.getString(SAVED_QUERY, null); |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame^] | 683 | setListContext(folder, query); |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 684 | } |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame^] | 685 | mViewMode.handleRestore(savedState); |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 686 | } else if (intent != null) { |
| 687 | handleIntent(intent); |
| 688 | } |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 689 | // Create the accounts loader; this loads the account switch spinner. |
| 690 | mActivity.getLoaderManager().initLoader(LOADER_ACCOUNT_CURSOR, null, this); |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 691 | return true; |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | @Override |
Andy Huang | 1ee96b2 | 2012-08-24 20:19:53 -0700 | [diff] [blame] | 695 | public void onStart() { |
| 696 | mSafeToModifyFragments = true; |
| 697 | } |
| 698 | |
| 699 | @Override |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 700 | public void onRestart() { |
| 701 | DialogFragment fragment = (DialogFragment) |
| 702 | mFragmentManager.findFragmentByTag(SYNC_ERROR_DIALOG_FRAGMENT_TAG); |
| 703 | if (fragment != null) { |
| 704 | fragment.dismiss(); |
| 705 | } |
mindyp | ea04f93 | 2012-08-27 14:17:59 -0700 | [diff] [blame] | 706 | // 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 Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | @Override |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 716 | public Dialog onCreateDialog(int id, Bundle bundle) { |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 717 | return null; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | @Override |
Vikram Aggarwal | 4eb5271 | 2012-06-19 16:24:50 -0700 | [diff] [blame] | 721 | public final boolean onCreateOptionsMenu(Menu menu) { |
Mindy Pereira | 28d5f72 | 2012-02-15 12:32:40 -0800 | [diff] [blame] | 722 | MenuInflater inflater = mActivity.getMenuInflater(); |
Mindy Pereira | f5acda4 | 2012-02-15 20:13:59 -0800 | [diff] [blame] | 723 | inflater.inflate(mActionBarView.getOptionsMenuId(), menu); |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 724 | mActionBarView.onCreateOptionsMenu(menu); |
Mindy Pereira | 28d5f72 | 2012-02-15 12:32:40 -0800 | [diff] [blame] | 725 | return true; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | @Override |
Vikram Aggarwal | 4eb5271 | 2012-06-19 16:24:50 -0700 | [diff] [blame] | 729 | public final boolean onKeyDown(int keyCode, KeyEvent event) { |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 730 | // TODO(viki): Auto-generated method stub |
| 731 | return false; |
| 732 | } |
| 733 | |
| 734 | @Override |
Vikram Aggarwal | 4eb5271 | 2012-06-19 16:24:50 -0700 | [diff] [blame] | 735 | public final boolean onOptionsItemSelected(MenuItem item) { |
Vikram Aggarwal | 54452ae | 2012-03-13 15:29:00 -0700 | [diff] [blame] | 736 | final int id = item.getItemId(); |
Vikram Aggarwal | 7dd054e | 2012-05-21 14:43:10 -0700 | [diff] [blame] | 737 | LogUtils.d(LOG_TAG, "AbstractController.onOptionsItemSelected(%d) called.", id); |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 738 | boolean handled = true; |
Mindy Pereira | ba68fda | 2012-05-24 15:53:06 -0700 | [diff] [blame] | 739 | final Collection<Conversation> target = Conversation.listOf(mCurrentConversation); |
Vikram Aggarwal | 112cd16 | 2012-06-18 10:51:13 -0700 | [diff] [blame] | 740 | final Settings settings = (mAccount == null) ? null : mAccount.settings; |
Mindy Pereira | 8937bf1 | 2012-07-23 14:05:02 -0700 | [diff] [blame] | 741 | // The user is choosing a new action; commit whatever they had been doing before. |
mindyp | c6adce3 | 2012-08-22 18:46:42 -0700 | [diff] [blame] | 742 | commitDestructiveActions(true); |
Mindy Pereira | 28d5f72 | 2012-02-15 12:32:40 -0800 | [diff] [blame] | 743 | switch (id) { |
Mindy Pereira | ba68fda | 2012-05-24 15:53:06 -0700 | [diff] [blame] | 744 | 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 Pereira | 01f3050 | 2012-08-14 10:30:51 -0700 | [diff] [blame] | 750 | case R.id.remove_folder: |
| 751 | delete(target, getRemoveFolder(target, mFolder, true, false, true)); |
| 752 | break; |
Mindy Pereira | ba68fda | 2012-05-24 15:53:06 -0700 | [diff] [blame] | 753 | 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 Westbrook | ef36254 | 2012-08-27 14:53:32 -0700 | [diff] [blame] | 759 | 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 Pereira | ba68fda | 2012-05-24 15:53:06 -0700 | [diff] [blame] | 764 | case R.id.mark_important: |
Vikram Aggarwal | 531488e | 2012-05-29 16:36:52 -0700 | [diff] [blame] | 765 | updateConversation(Conversation.listOf(mCurrentConversation), |
| 766 | ConversationColumns.PRIORITY, UIProvider.ConversationPriority.HIGH); |
Mindy Pereira | ba68fda | 2012-05-24 15:53:06 -0700 | [diff] [blame] | 767 | break; |
| 768 | case R.id.mark_not_important: |
Mindy Pereira | 0d03ef8 | 2012-08-15 09:05:48 -0700 | [diff] [blame] | 769 | 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 Pereira | ba68fda | 2012-05-24 15:53:06 -0700 | [diff] [blame] | 775 | break; |
| 776 | case R.id.mute: |
Vikram Aggarwal | 4f4782b | 2012-05-30 08:39:09 -0700 | [diff] [blame] | 777 | delete(target, getAction(R.id.mute, target)); |
Mindy Pereira | ba68fda | 2012-05-24 15:53:06 -0700 | [diff] [blame] | 778 | break; |
| 779 | case R.id.report_spam: |
Vikram Aggarwal | 4f4782b | 2012-05-30 08:39:09 -0700 | [diff] [blame] | 780 | delete(target, getAction(R.id.report_spam, target)); |
Mindy Pereira | ba68fda | 2012-05-24 15:53:06 -0700 | [diff] [blame] | 781 | break; |
Paul Westbrook | 77eee62 | 2012-07-10 13:41:57 -0700 | [diff] [blame] | 782 | 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 Westbrook | 76b2062 | 2012-07-12 11:45:43 -0700 | [diff] [blame] | 787 | case R.id.report_phishing: |
| 788 | delete(target, getAction(R.id.report_phishing, target)); |
| 789 | break; |
Mindy Pereira | f5acda4 | 2012-02-15 20:13:59 -0800 | [diff] [blame] | 790 | case android.R.id.home: |
| 791 | onUpPressed(); |
| 792 | break; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 793 | case R.id.compose: |
| 794 | ComposeActivity.compose(mActivity.getActivityContext(), mAccount); |
| 795 | break; |
Mindy Pereira | 28d5f72 | 2012-02-15 12:32:40 -0800 | [diff] [blame] | 796 | case R.id.show_all_folders: |
| 797 | showFolderList(); |
| 798 | break; |
Mindy Pereira | 28e0c34 | 2012-02-17 15:05:13 -0800 | [diff] [blame] | 799 | case R.id.refresh: |
| 800 | requestFolderRefresh(); |
| 801 | break; |
Mindy Pereira | 1f93668 | 2012-03-02 11:30:33 -0800 | [diff] [blame] | 802 | case R.id.settings: |
| 803 | Utils.showSettings(mActivity.getActivityContext(), mAccount); |
Paul Westbrook | 2861b6a | 2012-02-15 15:25:34 -0800 | [diff] [blame] | 804 | break; |
Paul Westbrook | e550355 | 2012-03-28 00:35:57 -0700 | [diff] [blame] | 805 | case R.id.folder_options: |
| 806 | Utils.showFolderSettings(mActivity.getActivityContext(), mAccount, mFolder); |
| 807 | break; |
Paul Westbrook | 94e440d | 2012-02-24 11:03:47 -0800 | [diff] [blame] | 808 | case R.id.help_info_menu_item: |
Paul Westbrook | 30745b6 | 2012-08-19 14:10:32 -0700 | [diff] [blame] | 809 | Utils.showHelp(mActivity.getActivityContext(), mAccount, getHelpContext()); |
Paul Westbrook | 94e440d | 2012-02-24 11:03:47 -0800 | [diff] [blame] | 810 | break; |
Vikram Aggarwal | 54452ae | 2012-03-13 15:29:00 -0700 | [diff] [blame] | 811 | case R.id.feedback_menu_item: |
Paul Westbrook | 17beb0b | 2012-08-20 13:34:37 -0700 | [diff] [blame] | 812 | Utils.sendFeedback(mActivity.getActivityContext(), mAccount, false); |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 813 | break; |
Paul Westbrook | 18babd2 | 2012-04-09 22:17:08 -0700 | [diff] [blame] | 814 | case R.id.manage_folders_item: |
| 815 | Utils.showManageFolder(mActivity.getActivityContext(), mAccount); |
| 816 | break; |
Vikram Aggarwal | d503df4 | 2012-05-11 10:13:35 -0700 | [diff] [blame] | 817 | case R.id.change_folder: |
| 818 | new FoldersSelectionDialog(mActivity.getActivityContext(), mAccount, this, |
Mindy Pereira | a832d3d | 2012-07-27 11:19:50 -0700 | [diff] [blame] | 819 | Conversation.listOf(mCurrentConversation), false, mFolder).show(); |
Vikram Aggarwal | d503df4 | 2012-05-11 10:13:35 -0700 | [diff] [blame] | 820 | break; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 821 | default: |
| 822 | handled = false; |
| 823 | break; |
Mindy Pereira | 28d5f72 | 2012-02-15 12:32:40 -0800 | [diff] [blame] | 824 | } |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 825 | return handled; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 826 | } |
| 827 | |
Vikram Aggarwal | 531488e | 2012-05-29 16:36:52 -0700 | [diff] [blame] | 828 | @Override |
Mindy Pereira | 6c2663d | 2012-07-20 15:37:29 -0700 | [diff] [blame] | 829 | public void updateConversation(Collection<Conversation> target, ContentValues values) { |
| 830 | mConversationListCursor.updateValues(mContext, target, values); |
| 831 | refreshConversationList(); |
| 832 | } |
| 833 | |
| 834 | @Override |
Vikram Aggarwal | 531488e | 2012-05-29 16:36:52 -0700 | [diff] [blame] | 835 | public void updateConversation(Collection <Conversation> target, String columnName, |
| 836 | boolean value) { |
| 837 | mConversationListCursor.updateBoolean(mContext, target, columnName, value); |
Vikram Aggarwal | 75daee5 | 2012-04-30 11:13:09 -0700 | [diff] [blame] | 838 | refreshConversationList(); |
Vikram Aggarwal | 54452ae | 2012-03-13 15:29:00 -0700 | [diff] [blame] | 839 | } |
| 840 | |
Vikram Aggarwal | 531488e | 2012-05-29 16:36:52 -0700 | [diff] [blame] | 841 | @Override |
Mindy Pereira | 6c2663d | 2012-07-20 15:37:29 -0700 | [diff] [blame] | 842 | public void updateConversation(Collection <Conversation> target, String columnName, |
| 843 | int value) { |
Vikram Aggarwal | 531488e | 2012-05-29 16:36:52 -0700 | [diff] [blame] | 844 | mConversationListCursor.updateInt(mContext, target, columnName, value); |
Vikram Aggarwal | 75daee5 | 2012-04-30 11:13:09 -0700 | [diff] [blame] | 845 | refreshConversationList(); |
Mindy Pereira | c9d5918 | 2012-03-22 16:06:46 -0700 | [diff] [blame] | 846 | } |
| 847 | |
Vikram Aggarwal | 531488e | 2012-05-29 16:36:52 -0700 | [diff] [blame] | 848 | @Override |
| 849 | public void updateConversation(Collection <Conversation> target, String columnName, |
| 850 | String value) { |
| 851 | mConversationListCursor.updateString(mContext, target, columnName, value); |
Vikram Aggarwal | 75daee5 | 2012-04-30 11:13:09 -0700 | [diff] [blame] | 852 | refreshConversationList(); |
Vikram Aggarwal | 54452ae | 2012-03-13 15:29:00 -0700 | [diff] [blame] | 853 | } |
| 854 | |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 855 | @Override |
| 856 | public void markConversationMessagesUnread(Conversation conv, Set<Uri> unreadMessageUris, |
Vikram Aggarwal | 4a878b6 | 2012-07-31 15:09:25 -0700 | [diff] [blame] | 857 | String originalConversationInfo) { |
Andy Huang | 8f6b006 | 2012-07-31 15:36:31 -0700 | [diff] [blame] | 858 | // 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 Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 862 | // locally mark conversation unread (the provider is supposed to propagate message unread |
| 863 | // to conversation unread) |
| 864 | conv.read = false; |
| 865 | |
Andy Huang | 28e31e2 | 2012-07-26 16:33:15 -0700 | [diff] [blame] | 866 | // only do a granular 'mark unread' if a subset of messages are unread |
| 867 | final int unreadCount = (unreadMessageUris == null) ? 0 : unreadMessageUris.size(); |
Mindy Pereira | 0972e07 | 2012-08-01 17:43:06 -0700 | [diff] [blame] | 868 | final int numMessages = conv.getNumMessages(); |
| 869 | final boolean subsetIsUnread = (numMessages > 1 && unreadCount > 0 |
| 870 | && unreadCount < numMessages); |
Andy Huang | 28e31e2 | 2012-07-26 16:33:15 -0700 | [diff] [blame] | 871 | |
| 872 | if (!subsetIsUnread) { |
Vikram Aggarwal | 66bc2aa | 2012-08-02 10:47:03 -0700 | [diff] [blame] | 873 | // 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 Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 876 | } else { |
Andy Huang | daa06ab | 2012-07-24 10:46:44 -0700 | [diff] [blame] | 877 | mConversationListCursor.setConversationColumn(conv.uri, ConversationColumns.READ, 0); |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 878 | |
Mindy Pereira | 7b6d03d | 2012-07-30 13:03:41 -0700 | [diff] [blame] | 879 | // locally update conversation's conversationInfo to revert to original version |
Andy Huang | 28e31e2 | 2012-07-26 16:33:15 -0700 | [diff] [blame] | 880 | if (originalConversationInfo != null) { |
| 881 | mConversationListCursor.setConversationColumn(conv.uri, |
| 882 | ConversationColumns.CONVERSATION_INFO, originalConversationInfo); |
| 883 | } |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 884 | |
| 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 Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 903 | } |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | @Override |
Vikram Aggarwal | 66bc2aa | 2012-08-02 10:47:03 -0700 | [diff] [blame] | 907 | 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 Huang | 8f6b006 | 2012-07-31 15:36:31 -0700 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | private void markConversationsRead(Collection<Conversation> targets, boolean read, |
Vikram Aggarwal | 66bc2aa | 2012-08-02 10:47:03 -0700 | [diff] [blame] | 914 | boolean markViewed, boolean showNext) { |
Andy Huang | 8f6b006 | 2012-07-31 15:36:31 -0700 | [diff] [blame] | 915 | // auto-advance if requested and the current conversation is being marked unread |
| 916 | if (showNext && !read) { |
| 917 | showNextConversation(targets); |
| 918 | } |
| 919 | |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 920 | for (Conversation target : targets) { |
Vikram Aggarwal | 192fac1 | 2012-07-25 16:44:55 -0700 | [diff] [blame] | 921 | final ContentValues values = new ContentValues(); |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 922 | values.put(ConversationColumns.READ, read); |
Vikram Aggarwal | 66bc2aa | 2012-08-02 10:47:03 -0700 | [diff] [blame] | 923 | if (markViewed) { |
| 924 | values.put(ConversationColumns.VIEWED, true); |
| 925 | } |
Vikram Aggarwal | 192fac1 | 2012-07-25 16:44:55 -0700 | [diff] [blame] | 926 | final ConversationInfo info = target.conversationInfo; |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 927 | if (info != null) { |
Mindy Pereira | 5f42437 | 2012-07-30 11:49:55 -0700 | [diff] [blame] | 928 | info.markRead(read); |
| 929 | values.put(ConversationColumns.CONVERSATION_INFO, ConversationInfo.toString(info)); |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 930 | } |
| 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 Huang | cd5c5ee | 2012-08-12 19:03:51 -0700 | [diff] [blame] | 936 | if (markViewed) { |
| 937 | c.markViewed(); |
| 938 | } |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 939 | } |
| 940 | } |
| 941 | |
Andy Huang | 8f6b006 | 2012-07-31 15:36:31 -0700 | [diff] [blame] | 942 | /** |
| 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 Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 961 | @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 Huang | daa06ab | 2012-07-24 10:46:44 -0700 | [diff] [blame] | 976 | mConversationListCursor.setConversationColumn(msg.conversation.uri, |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 977 | 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 Pereira | 28e0c34 | 2012-02-17 15:05:13 -0800 | [diff] [blame] | 991 | private void requestFolderRefresh() { |
| 992 | if (mFolder != null) { |
Mindy Pereira | b7b33e0 | 2012-02-21 15:32:19 -0800 | [diff] [blame] | 993 | if (mAsyncRefreshTask != null) { |
| 994 | mAsyncRefreshTask.cancel(true); |
| 995 | } |
Paul Westbrook | 7e2a2a1 | 2012-06-27 13:52:40 -0700 | [diff] [blame] | 996 | mAsyncRefreshTask = new AsyncRefreshTask(mContext, mFolder.refreshUri); |
Mindy Pereira | b7b33e0 | 2012-02-21 15:32:19 -0800 | [diff] [blame] | 997 | mAsyncRefreshTask.execute(); |
Mindy Pereira | 28e0c34 | 2012-02-17 15:05:13 -0800 | [diff] [blame] | 998 | } |
| 999 | } |
| 1000 | |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 1001 | /** |
| 1002 | * Confirm (based on user's settings) and delete a conversation from the conversation list and |
| 1003 | * from the database. |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 1004 | * @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 Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 1008 | */ |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 1009 | protected void confirmAndDelete(final Collection<Conversation> target, boolean showDialog, |
| 1010 | int confirmResource, final DestructiveAction action) { |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 1011 | if (showDialog) { |
| 1012 | final AlertDialog.OnClickListener onClick = new AlertDialog.OnClickListener() { |
| 1013 | @Override |
| 1014 | public void onClick(DialogInterface dialog, int which) { |
Vikram Aggarwal | 4f4782b | 2012-05-30 08:39:09 -0700 | [diff] [blame] | 1015 | delete(target, action); |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 1016 | } |
| 1017 | }; |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 1018 | final CharSequence message = Utils.formatPlural(mContext, confirmResource, |
| 1019 | target.size()); |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 1020 | 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 Aggarwal | 4f4782b | 2012-05-30 08:39:09 -0700 | [diff] [blame] | 1025 | delete(target, action); |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 1026 | } |
| 1027 | } |
| 1028 | |
Vikram Aggarwal | 4f4782b | 2012-05-30 08:39:09 -0700 | [diff] [blame] | 1029 | @Override |
| 1030 | public void delete(final Collection<Conversation> target, final DestructiveAction action) { |
Vikram Aggarwal | 192fac1 | 2012-07-25 16:44:55 -0700 | [diff] [blame] | 1031 | // 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 Aggarwal | d503df4 | 2012-05-11 10:13:35 -0700 | [diff] [blame] | 1035 | // Update the conversation fragment if the current conversation is deleted. |
Andy Huang | 8f6b006 | 2012-07-31 15:36:31 -0700 | [diff] [blame] | 1036 | showNextConversation(target); |
Vikram Aggarwal | 192fac1 | 2012-07-25 16:44:55 -0700 | [diff] [blame] | 1037 | // 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 Aggarwal | d503df4 | 2012-05-11 10:13:35 -0700 | [diff] [blame] | 1044 | // 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 Aggarwal | 4f4782b | 2012-05-30 08:39:09 -0700 | [diff] [blame] | 1053 | private void requestUpdate(final Collection<Conversation> target, |
Vikram Aggarwal | d503df4 | 2012-05-11 10:13:35 -0700 | [diff] [blame] | 1054 | final DestructiveAction action) { |
| 1055 | action.performAction(); |
| 1056 | refreshConversationList(); |
Vikram Aggarwal | 75daee5 | 2012-04-30 11:13:09 -0700 | [diff] [blame] | 1057 | } |
Vikram Aggarwal | 6fbc87a | 2012-03-15 15:24:00 -0700 | [diff] [blame] | 1058 | |
| 1059 | @Override |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1060 | public void onPrepareDialog(int id, Dialog dialog, Bundle bundle) { |
| 1061 | // TODO(viki): Auto-generated method stub |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1062 | } |
| 1063 | |
| 1064 | @Override |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1065 | public boolean onPrepareOptionsMenu(Menu menu) { |
Mindy Pereira | b849dfb | 2012-03-07 18:13:15 -0800 | [diff] [blame] | 1066 | mActionBarView.onPrepareOptionsMenu(menu); |
Mindy Pereira | 363451a | 2012-02-22 14:14:46 -0800 | [diff] [blame] | 1067 | return true; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1068 | } |
| 1069 | |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 1070 | @Override |
| 1071 | public void onPause() { |
| 1072 | isLoaderInitialized = false; |
Paul Westbrook | 6ead20d | 2012-03-19 14:48:14 -0700 | [diff] [blame] | 1073 | enableNotifications(); |
Paul Westbrook | 94e440d | 2012-02-24 11:03:47 -0800 | [diff] [blame] | 1074 | } |
| 1075 | |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1076 | @Override |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1077 | public void onResume() { |
Paul Westbrook | 6ead20d | 2012-03-19 14:48:14 -0700 | [diff] [blame] | 1078 | // 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 Huang | 1ee96b2 | 2012-08-24 20:19:53 -0700 | [diff] [blame] | 1083 | |
| 1084 | mSafeToModifyFragments = true; |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | @Override |
| 1088 | public void onSaveInstanceState(Bundle outState) { |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame^] | 1089 | mViewMode.handleSaveInstanceState(outState); |
Vikram Aggarwal | 6c51158 | 2012-02-27 10:59:47 -0800 | [diff] [blame] | 1090 | if (mAccount != null) { |
| 1091 | LogUtils.d(LOG_TAG, "Saving the account now"); |
| 1092 | outState.putParcelable(SAVED_ACCOUNT, mAccount); |
| 1093 | } |
Mindy Pereira | 5e478d2 | 2012-03-26 18:04:58 -0700 | [diff] [blame] | 1094 | if (mFolder != null) { |
| 1095 | outState.putParcelable(SAVED_FOLDER, mFolder); |
Vikram Aggarwal | 8b15263 | 2012-02-03 14:58:45 -0800 | [diff] [blame] | 1096 | } |
Vikram Aggarwal | f334140 | 2012-08-07 10:09:38 -0700 | [diff] [blame] | 1097 | // 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 Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame^] | 1101 | final int mode = mViewMode.getMode(); |
Mindy Pereira | 9fa43ca | 2012-05-17 14:18:01 -0700 | [diff] [blame] | 1102 | if (mCurrentConversation != null |
Mindy Pereira | 0f7ae7a | 2012-07-17 13:39:56 -0700 | [diff] [blame] | 1103 | && (mode == ViewMode.CONVERSATION || |
Mindy Pereira | 9fa43ca | 2012-05-17 14:18:01 -0700 | [diff] [blame] | 1104 | mViewMode.getMode() == ViewMode.SEARCH_RESULTS_CONVERSATION)) { |
Mindy Pereira | 26f23fc | 2012-03-27 10:26:04 -0700 | [diff] [blame] | 1105 | outState.putParcelable(SAVED_CONVERSATION, mCurrentConversation); |
| 1106 | } |
Andy Huang | 4556a44 | 2012-03-30 16:42:05 -0700 | [diff] [blame] | 1107 | if (!mSelectedSet.isEmpty()) { |
Vikram Aggarwal | cabd3f2 | 2012-04-19 10:14:41 -0700 | [diff] [blame] | 1108 | outState.putParcelable(SAVED_SELECTED_SET, mSelectedSet); |
Andy Huang | 4556a44 | 2012-03-30 16:42:05 -0700 | [diff] [blame] | 1109 | } |
Mindy Pereira | d3367499 | 2012-06-25 16:26:30 -0700 | [diff] [blame] | 1110 | if (mToastBar.getVisibility() == View.VISIBLE) { |
| 1111 | outState.putParcelable(SAVED_TOAST_BAR_OP, mToastBar.getOperation()); |
| 1112 | } |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame^] | 1113 | final ConversationListFragment convListFragment = getConversationListFragment(); |
Mindy Pereira | d3367499 | 2012-06-25 16:26:30 -0700 | [diff] [blame] | 1114 | if (convListFragment != null) { |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame^] | 1115 | convListFragment.getAnimatedAdapter().onSaveInstanceState(outState); |
Mindy Pereira | d3367499 | 2012-06-25 16:26:30 -0700 | [diff] [blame] | 1116 | } |
Andy Huang | 1ee96b2 | 2012-08-24 20:19:53 -0700 | [diff] [blame] | 1117 | mSafeToModifyFragments = false; |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame^] | 1118 | outState.putString(SAVED_HIERARCHICAL_FOLDER, |
| 1119 | (mFolderListFolder != null) ? Folder.toString(mFolderListFolder) : null); |
Andy Huang | 1ee96b2 | 2012-08-24 20:19:53 -0700 | [diff] [blame] | 1120 | } |
| 1121 | |
| 1122 | /** |
| 1123 | * @see #mSafeToModifyFragments |
| 1124 | */ |
| 1125 | protected boolean safeToModifyFragments() { |
| 1126 | return mSafeToModifyFragments; |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1127 | } |
| 1128 | |
| 1129 | @Override |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 1130 | 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 Aggarwal | b17cbc0 | 2012-04-06 15:41:46 -0700 | [diff] [blame] | 1136 | mActionBarView.collapseSearch(); |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 1137 | mActivity.startActivity(intent); |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | @Override |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1141 | public void onStop() { |
Mindy Pereira | 49e5dbe | 2012-07-12 11:47:54 -0700 | [diff] [blame] | 1142 | if (mEnableShareIntents != null) { |
| 1143 | mEnableShareIntents.cancel(true); |
| 1144 | } |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1145 | } |
| 1146 | |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1147 | @Override |
| 1148 | public void onDestroy() { |
| 1149 | // unregister the ViewPager's observer on the conversation cursor |
| 1150 | mPagerController.onDestroy(); |
Mindy Pereira | 641de65 | 2012-08-02 15:21:50 -0700 | [diff] [blame] | 1151 | mActionBarView.onDestroy(); |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 1152 | mRecentFolderList.destroy(); |
Andy Huang | 4e0158f | 2012-08-07 21:06:01 -0700 | [diff] [blame] | 1153 | mDestroyed = true; |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1154 | } |
| 1155 | |
Vikram Aggarwal | fa131a2 | 2012-02-02 13:56:22 -0800 | [diff] [blame] | 1156 | /** |
Mindy Pereira | 161f50d | 2012-02-28 15:47:19 -0800 | [diff] [blame] | 1157 | * {@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 Aggarwal | fa131a2 | 2012-02-02 13:56:22 -0800 | [diff] [blame] | 1160 | */ |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1161 | @Override |
Vikram Aggarwal | fa131a2 | 2012-02-02 13:56:22 -0800 | [diff] [blame] | 1162 | public void onViewModeChanged(int newMode) { |
| 1163 | // Perform any mode specific work here. |
Mindy Pereira | 161f50d | 2012-02-28 15:47:19 -0800 | [diff] [blame] | 1164 | // reset the action bar icon based on the mode. Why don't the individual |
| 1165 | // controllers do |
Vikram Aggarwal | fa131a2 | 2012-02-02 13:56:22 -0800 | [diff] [blame] | 1166 | // this themselves? |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1167 | |
Mindy Pereira | 8937bf1 | 2012-07-23 14:05:02 -0700 | [diff] [blame] | 1168 | // Commit any destructive undoable actions the user may have performed. |
mindyp | c6adce3 | 2012-08-22 18:46:42 -0700 | [diff] [blame] | 1169 | commitDestructiveActions(true); |
Mindy Pereira | 8937bf1 | 2012-07-23 14:05:02 -0700 | [diff] [blame] | 1170 | |
Mindy Pereira | 161f50d | 2012-02-28 15:47:19 -0800 | [diff] [blame] | 1171 | // We don't want to invalidate the options menu when switching to |
| 1172 | // conversation |
Vikram Aggarwal | fa131a2 | 2012-02-02 13:56:22 -0800 | [diff] [blame] | 1173 | // mode, as it will happen when the conversation finishes loading. |
| 1174 | if (newMode != ViewMode.CONVERSATION) { |
| 1175 | mActivity.invalidateOptionsMenu(); |
| 1176 | } |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1177 | } |
| 1178 | |
Andy Huang | 4e0158f | 2012-08-07 21:06:01 -0700 | [diff] [blame] | 1179 | public boolean isDestroyed() { |
| 1180 | return mDestroyed; |
| 1181 | } |
| 1182 | |
mindyp | 54f120f | 2012-08-28 13:10:33 -0700 | [diff] [blame] | 1183 | @Override |
| 1184 | public void commitDestructiveActions(boolean animate) { |
mindyp | c6adce3 | 2012-08-22 18:46:42 -0700 | [diff] [blame] | 1185 | ConversationListFragment fragment = getConversationListFragment(); |
Mindy Pereira | 1e2573b | 2012-04-17 14:34:36 -0700 | [diff] [blame] | 1186 | if (fragment != null) { |
mindyp | c6adce3 | 2012-08-22 18:46:42 -0700 | [diff] [blame] | 1187 | fragment.commitDestructiveActions(animate); |
Mindy Pereira | 1e2573b | 2012-04-17 14:34:36 -0700 | [diff] [blame] | 1188 | } |
| 1189 | } |
| 1190 | |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1191 | @Override |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1192 | public void onWindowFocusChanged(boolean hasFocus) { |
Paul Westbrook | 9f119c7 | 2012-04-24 16:10:59 -0700 | [diff] [blame] | 1193 | ConversationListFragment convList = getConversationListFragment(); |
| 1194 | if (hasFocus && convList != null && convList.isVisible()) { |
| 1195 | // The conversation list is visible. |
| 1196 | Utils.setConversationCursorVisibility(mConversationListCursor, true); |
| 1197 | } |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1198 | } |
| 1199 | |
Vikram Aggarwal | ca716f1 | 2012-08-20 11:11:48 -0700 | [diff] [blame] | 1200 | /** |
| 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 Pereira | 75181e8 | 2012-04-18 08:17:13 -0700 | [diff] [blame] | 1206 | private void setAccount(Account account) { |
Andy Huang | b9ca979 | 2012-05-18 15:31:49 -0700 | [diff] [blame] | 1207 | if (account == null) { |
| 1208 | LogUtils.w(LOG_TAG, new Error(), |
| 1209 | "AAC ignoring null (presumably invalid) account restoration"); |
| 1210 | return; |
| 1211 | } |
Andy Huang | b114841 | 2012-05-19 00:16:30 -0700 | [diff] [blame] | 1212 | LogUtils.d(LOG_TAG, "AbstractActivityController.setAccount(): account = %s", account.uri); |
Vikram Aggarwal | 91e8737 | 2012-05-18 15:36:04 -0700 | [diff] [blame] | 1213 | mAccount = account; |
Vikram Aggarwal | ca716f1 | 2012-08-20 11:11:48 -0700 | [diff] [blame] | 1214 | // 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 Aggarwal | 91e8737 | 2012-05-18 15:36:04 -0700 | [diff] [blame] | 1222 | if (account.settings == null) { |
| 1223 | LogUtils.w(LOG_TAG, new Error(), "AAC ignoring account with null settings."); |
| 1224 | return; |
| 1225 | } |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 1226 | mAccountObservers.notifyChanged(); |
Mindy Pereira | 75181e8 | 2012-04-18 08:17:13 -0700 | [diff] [blame] | 1227 | } |
| 1228 | |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1229 | /** |
Mindy Pereira | 161f50d | 2012-02-28 15:47:19 -0800 | [diff] [blame] | 1230 | * 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 Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1234 | * @param savedState |
| 1235 | */ |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1236 | @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 Westbrook | 534e4a2 | 2012-04-25 03:46:29 -0700 | [diff] [blame] | 1241 | 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 Huang | ed4fdf0 | 2012-07-26 17:12:50 -0700 | [diff] [blame] | 1248 | showConversation(conversation); |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1249 | } |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 1250 | |
Mindy Pereira | d3367499 | 2012-06-25 16:26:30 -0700 | [diff] [blame] | 1251 | 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 Sapperstein | 9d7519d | 2012-07-16 14:03:53 -0700 | [diff] [blame] | 1257 | } else if (op.getType() == ToastBarOperation.ERROR) { |
| 1258 | onError(mFolder, true); |
Mindy Pereira | d3367499 | 2012-06-25 16:26:30 -0700 | [diff] [blame] | 1259 | } |
| 1260 | } |
| 1261 | } |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame^] | 1262 | 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 Pereira | d3367499 | 2012-06-25 16:26:30 -0700 | [diff] [blame] | 1267 | if (convListFragment != null) { |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame^] | 1268 | convListFragment.getAnimatedAdapter().onRestoreInstanceState(savedState); |
Mindy Pereira | d3367499 | 2012-06-25 16:26:30 -0700 | [diff] [blame] | 1269 | } |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 1270 | /** |
| 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 Huang | 4556a44 | 2012-03-30 16:42:05 -0700 | [diff] [blame] | 1276 | restoreSelectedConversations(savedState); |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1277 | } |
| 1278 | |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame^] | 1279 | /** |
| 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 Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1285 | 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 Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame^] | 1289 | setAccount(Account.newinstance(intent.getStringExtra(Utils.EXTRA_ACCOUNT))); |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1290 | } |
Andy Huang | b9ca979 | 2012-05-18 15:31:49 -0700 | [diff] [blame] | 1291 | if (mAccount == null) { |
| 1292 | return; |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1293 | } |
Andy Huang | b9ca979 | 2012-05-18 15:31:49 -0700 | [diff] [blame] | 1294 | mActivity.invalidateOptionsMenu(); |
Vikram Aggarwal | 1a249e0 | 2012-08-03 16:19:33 -0700 | [diff] [blame] | 1295 | final boolean isConversationMode = intent.hasExtra(Utils.EXTRA_CONVERSATION); |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame^] | 1296 | if (isConversationMode && mViewMode.getMode() == ViewMode.UNKNOWN) { |
Vikram Aggarwal | 1a249e0 | 2012-08-03 16:19:33 -0700 | [diff] [blame] | 1297 | mViewMode.enterConversationMode(); |
| 1298 | } else { |
| 1299 | mViewMode.enterConversationListMode(); |
| 1300 | } |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1301 | |
| 1302 | Folder folder = null; |
| 1303 | if (intent.hasExtra(Utils.EXTRA_FOLDER)) { |
| 1304 | // Open the folder. |
Mindy Pereira | 7b6d03d | 2012-07-30 13:03:41 -0700 | [diff] [blame] | 1305 | folder = Folder.fromString(intent.getStringExtra(Utils.EXTRA_FOLDER)); |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1306 | } |
| 1307 | if (folder != null) { |
| 1308 | onFolderChanged(folder); |
| 1309 | handled = true; |
| 1310 | } |
| 1311 | |
Vikram Aggarwal | 1a249e0 | 2012-08-03 16:19:33 -0700 | [diff] [blame] | 1312 | if (isConversationMode) { |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1313 | // Open the conversation. |
| 1314 | LogUtils.d(LOG_TAG, "SHOW THE CONVERSATION at %s", |
| 1315 | intent.getParcelableExtra(Utils.EXTRA_CONVERSATION)); |
Paul Westbrook | a916191 | 2012-04-24 10:10:04 -0700 | [diff] [blame] | 1316 | 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 Huang | 980aaea | 2012-07-26 17:22:19 -0700 | [diff] [blame] | 1323 | showConversation(conversation); |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1324 | 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 Pereira | ac25482 | 2012-06-18 10:46:43 -0700 | [diff] [blame] | 1339 | if (Utils.showTwoPaneSearchResults(mActivity.getActivityContext())) { |
| 1340 | mViewMode.enterSearchResultsConversationMode(); |
| 1341 | } else { |
| 1342 | mViewMode.enterSearchResultsListMode(); |
| 1343 | } |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1344 | setAccount((Account) intent.getParcelableExtra(Utils.EXTRA_ACCOUNT)); |
| 1345 | mActivity.invalidateOptionsMenu(); |
| 1346 | restartOptionalLoader(LOADER_RECENT_FOLDERS); |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1347 | 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 Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1356 | } |
| 1357 | |
Andy Huang | 4556a44 | 2012-03-30 16:42:05 -0700 | [diff] [blame] | 1358 | /** |
| 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 Aggarwal | 4eb5271 | 2012-06-19 16:24:50 -0700 | [diff] [blame] | 1363 | private final void restoreSelectedConversations(Bundle savedState) { |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 1364 | if (savedState == null) { |
Andy Huang | 4556a44 | 2012-03-30 16:42:05 -0700 | [diff] [blame] | 1365 | mSelectedSet.clear(); |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 1366 | return; |
| 1367 | } |
Vikram Aggarwal | cabd3f2 | 2012-04-19 10:14:41 -0700 | [diff] [blame] | 1368 | final ConversationSelectionSet selectedSet = savedState.getParcelable(SAVED_SELECTED_SET); |
Andy Huang | 4556a44 | 2012-03-30 16:42:05 -0700 | [diff] [blame] | 1369 | if (selectedSet == null || selectedSet.isEmpty()) { |
| 1370 | mSelectedSet.clear(); |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 1371 | return; |
| 1372 | } |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1373 | |
| 1374 | // putAll will take care of calling our registered onSetPopulated method |
Vikram Aggarwal | cabd3f2 | 2012-04-19 10:14:41 -0700 | [diff] [blame] | 1375 | mSelectedSet.putAll(selectedSet); |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 1376 | } |
| 1377 | |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1378 | @Override |
Andy Huang | 5895f7b | 2012-06-01 17:07:20 -0700 | [diff] [blame] | 1379 | public SubjectDisplayChanger getSubjectDisplayChanger() { |
| 1380 | return mActionBarView; |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1381 | } |
| 1382 | |
Andy Huang | 1ee96b2 | 2012-08-24 20:19:53 -0700 | [diff] [blame] | 1383 | private void showConversation(Conversation conversation) { |
| 1384 | showConversation(conversation, false /* inLoaderCallbacks */); |
| 1385 | } |
| 1386 | |
Vikram Aggarwal | ec5cbf7 | 2012-03-08 15:10:35 -0800 | [diff] [blame] | 1387 | /** |
| 1388 | * Children can override this method, but they must call super.showConversation(). |
Andy Huang | 1ee96b2 | 2012-08-24 20:19:53 -0700 | [diff] [blame] | 1389 | * |
Vikram Aggarwal | ec5cbf7 | 2012-03-08 15:10:35 -0800 | [diff] [blame] | 1390 | */ |
Andy Huang | 1ee96b2 | 2012-08-24 20:19:53 -0700 | [diff] [blame] | 1391 | protected void showConversation(Conversation conversation, boolean inLoaderCallbacks) { |
Vikram Aggarwal | c67182d | 2012-04-03 14:35:06 -0700 | [diff] [blame] | 1392 | // Set the current conversation just in case it wasn't already set. |
| 1393 | setCurrentConversation(conversation); |
Vikram Aggarwal | 0f14273 | 2012-08-24 09:39:34 -0700 | [diff] [blame] | 1394 | // 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 Aggarwal | ec5cbf7 | 2012-03-08 15:10:35 -0800 | [diff] [blame] | 1399 | } |
| 1400 | |
Vikram Aggarwal | e128fc2 | 2012-04-04 12:33:34 -0700 | [diff] [blame] | 1401 | /** |
Paul Westbrook | 2d50bcd | 2012-04-10 11:53:47 -0700 | [diff] [blame] | 1402 | * 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 Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 1418 | (WaitFragment)manager.findFragmentByTag(TAG_WAIT); |
Paul Westbrook | 2d50bcd | 2012-04-10 11:53:47 -0700 | [diff] [blame] | 1419 | if (waitFragment != null) { |
| 1420 | waitFragment.updateAccount(mAccount); |
| 1421 | } |
| 1422 | } |
| 1423 | |
Vikram Aggarwal | 48b2a6c | 2012-05-29 14:09:27 -0700 | [diff] [blame] | 1424 | /** |
| 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 Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 1427 | * |
Vikram Aggarwal | 48b2a6c | 2012-05-29 14:09:27 -0700 | [diff] [blame] | 1428 | */ |
Paul Westbrook | 2d50bcd | 2012-04-10 11:53:47 -0700 | [diff] [blame] | 1429 | public boolean inWaitMode() { |
| 1430 | final FragmentManager manager = mActivity.getFragmentManager(); |
| 1431 | final WaitFragment waitFragment = |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 1432 | (WaitFragment)manager.findFragmentByTag(TAG_WAIT); |
Paul Westbrook | 2d50bcd | 2012-04-10 11:53:47 -0700 | [diff] [blame] | 1433 | 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 Aggarwal | e128fc2 | 2012-04-04 12:33:34 -0700 | [diff] [blame] | 1442 | * 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 Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1449 | @Override |
Andy Huang | 1ee96b2 | 2012-08-24 20:19:53 -0700 | [diff] [blame] | 1450 | public void onConversationSelected(Conversation conversation, boolean inLoaderCallbacks) { |
mindyp | aa55bc9 | 2012-08-24 09:49:56 -0700 | [diff] [blame] | 1451 | // 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 Huang | 1ee96b2 | 2012-08-24 20:19:53 -0700 | [diff] [blame] | 1454 | showConversation(conversation, inLoaderCallbacks); |
| 1455 | } |
| 1456 | |
| 1457 | @Override |
| 1458 | public Conversation getCurrentConversation() { |
| 1459 | return mCurrentConversation; |
Vikram Aggarwal | 7d60288 | 2012-02-07 15:01:20 -0800 | [diff] [blame] | 1460 | } |
Mindy Pereira | 555140c | 2012-02-15 14:55:29 -0800 | [diff] [blame] | 1461 | |
Vikram Aggarwal | c67182d | 2012-04-03 14:35:06 -0700 | [diff] [blame] | 1462 | /** |
| 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 Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1468 | @Override |
| 1469 | public void setCurrentConversation(Conversation conversation) { |
Mindy Pereira | 5040f1a | 2012-03-20 10:14:06 -0700 | [diff] [blame] | 1470 | mCurrentConversation = conversation; |
Vikram Aggarwal | 135fd02 | 2012-04-11 14:44:15 -0700 | [diff] [blame] | 1471 | mTracker.initialize(mCurrentConversation); |
Mindy Pereira | 5040f1a | 2012-03-20 10:14:06 -0700 | [diff] [blame] | 1472 | } |
| 1473 | |
Vikram Aggarwal | a9b93f3 | 2012-02-23 14:51:58 -0800 | [diff] [blame] | 1474 | /** |
| 1475 | * {@inheritDoc} |
| 1476 | */ |
Mindy Pereira | 555140c | 2012-02-15 14:55:29 -0800 | [diff] [blame] | 1477 | @Override |
Vikram Aggarwal | 7dedb95 | 2012-02-16 16:10:23 -0800 | [diff] [blame] | 1478 | public Loader<Cursor> onCreateLoader(int id, Bundle args) { |
| 1479 | // Create a loader to listen in on account changes. |
Vikram Aggarwal | fa4b47e | 2012-03-09 13:02:46 -0800 | [diff] [blame] | 1480 | switch (id) { |
| 1481 | case LOADER_ACCOUNT_CURSOR: |
Paul Westbrook | c2074c4 | 2012-03-22 15:26:58 -0700 | [diff] [blame] | 1482 | return new CursorLoader(mContext, MailAppProvider.getAccountsUri(), |
Vikram Aggarwal | fa4b47e | 2012-03-09 13:02:46 -0800 | [diff] [blame] | 1483 | UIProvider.ACCOUNTS_PROJECTION, null, null, null); |
| 1484 | case LOADER_FOLDER_CURSOR: |
Paul Westbrook | c7a070f | 2012-04-12 01:46:41 -0700 | [diff] [blame] | 1485 | final CursorLoader loader = new CursorLoader(mContext, mFolder.uri, |
| 1486 | UIProvider.FOLDERS_PROJECTION, null, null, null); |
| 1487 | loader.setUpdateThrottle(mFolderItemUpdateDelayMs); |
| 1488 | return loader; |
Vikram Aggarwal | fa4b47e | 2012-03-09 13:02:46 -0800 | [diff] [blame] | 1489 | case LOADER_RECENT_FOLDERS: |
Paul Westbrook | 91d1050 | 2012-04-13 12:01:39 -0700 | [diff] [blame] | 1490 | if (mAccount != null && mAccount.recentFolderListUri != null) { |
Paul Westbrook | ea4ee20 | 2012-03-12 14:12:33 -0700 | [diff] [blame] | 1491 | return new CursorLoader(mContext, mAccount.recentFolderListUri, |
| 1492 | UIProvider.FOLDERS_PROJECTION, null, null, null); |
| 1493 | } |
| 1494 | break; |
Mindy Pereira | ab48636 | 2012-03-21 18:18:53 -0700 | [diff] [blame] | 1495 | case LOADER_ACCOUNT_INBOX: |
Vikram Aggarwal | 025eba8 | 2012-05-08 10:45:30 -0700 | [diff] [blame] | 1496 | final Uri defaultInbox = Settings.getDefaultInboxUri(mAccount.settings); |
Vikram Aggarwal | 1e57e67 | 2012-05-07 14:48:24 -0700 | [diff] [blame] | 1497 | final Uri inboxUri = defaultInbox.equals(Uri.EMPTY) ? |
| 1498 | mAccount.folderListUri : defaultInbox; |
Paul Westbrook | 7496e82 | 2012-04-24 09:50:54 -0700 | [diff] [blame] | 1499 | LogUtils.d(LOG_TAG, "Loading the default inbox: %s", inboxUri); |
Paul Westbrook | 1220b6d | 2012-04-10 00:48:00 -0700 | [diff] [blame] | 1500 | if (inboxUri != null) { |
| 1501 | return new CursorLoader(mContext, inboxUri, UIProvider.FOLDERS_PROJECTION, null, |
| 1502 | null, null); |
| 1503 | } |
| 1504 | break; |
Mindy Pereira | ab48636 | 2012-03-21 18:18:53 -0700 | [diff] [blame] | 1505 | case LOADER_SEARCH: |
| 1506 | return Folder.forSearchResults(mAccount, |
| 1507 | args.getString(ConversationListContext.EXTRA_SEARCH_QUERY), |
| 1508 | mActivity.getActivityContext()); |
Paul Westbrook | 2d50bcd | 2012-04-10 11:53:47 -0700 | [diff] [blame] | 1509 | case LOADER_ACCOUNT_UPDATE_CURSOR: |
| 1510 | return new CursorLoader(mContext, mAccount.uri, UIProvider.ACCOUNTS_PROJECTION, |
| 1511 | null, null, null); |
Vikram Aggarwal | fa4b47e | 2012-03-09 13:02:46 -0800 | [diff] [blame] | 1512 | default: |
Paul Westbrook | ad6a275 | 2012-04-04 16:58:13 -0700 | [diff] [blame] | 1513 | LogUtils.wtf(LOG_TAG, "Loader returned unexpected id: %d", id); |
Mindy Pereira | 5cb0c3e | 2012-02-22 15:22:47 -0800 | [diff] [blame] | 1514 | } |
| 1515 | return null; |
Vikram Aggarwal | 7dedb95 | 2012-02-16 16:10:23 -0800 | [diff] [blame] | 1516 | } |
| 1517 | |
Paul Westbrook | b1f573c | 2012-04-06 11:38:28 -0700 | [diff] [blame] | 1518 | @Override |
| 1519 | public void onLoaderReset(Loader<Cursor> loader) { |
| 1520 | |
| 1521 | } |
| 1522 | |
Andy Huang | f9a7348 | 2012-03-13 15:54:02 -0700 | [diff] [blame] | 1523 | /** |
| 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 Huang | f9a7348 | 2012-03-13 15:54:02 -0700 | [diff] [blame] | 1534 | */ |
Vikram Aggarwal | 94c94de | 2012-04-04 15:38:28 -0700 | [diff] [blame] | 1535 | private void restartOptionalLoader(int id) { |
Andy Huang | f9a7348 | 2012-03-13 15:54:02 -0700 | [diff] [blame] | 1536 | final LoaderManager lm = mActivity.getLoaderManager(); |
| 1537 | lm.destroyLoader(id); |
Vikram Aggarwal | 94c94de | 2012-04-04 15:38:28 -0700 | [diff] [blame] | 1538 | lm.restartLoader(id, Bundle.EMPTY, this); |
| 1539 | } |
| 1540 | |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1541 | @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 Huang | 090db1e | 2012-07-25 13:25:28 -0700 | [diff] [blame] | 1551 | @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 Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 1561 | /** |
| 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 Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 1567 | 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 Aggarwal | 6c51158 | 2012-02-27 10:59:47 -0800 | [diff] [blame] | 1570 | return true; |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 1571 | } |
| 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 Aggarwal | 7dedb95 | 2012-02-16 16:10:23 -0800 | [diff] [blame] | 1582 | do { |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 1583 | final Uri accountUri = |
| 1584 | Uri.parse(accountCursor.getString(UIProvider.ACCOUNT_URI_COLUMN)); |
| 1585 | if (!foundCurrentAccount && mAccount.uri.equals(accountUri)) { |
| 1586 | foundCurrentAccount = true; |
| 1587 | } |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 1588 | // Is there a new account that we do not know about? |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 1589 | if (!mCurrentAccountUris.contains(accountUri)) { |
| 1590 | return true; |
| 1591 | } |
Vikram Aggarwal | 7dedb95 | 2012-02-16 16:10:23 -0800 | [diff] [blame] | 1592 | } while (accountCursor.moveToNext()); |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 1593 | |
| 1594 | // As long as we found the current account, the list hasn't been updated |
| 1595 | return !foundCurrentAccount; |
Vikram Aggarwal | 7dedb95 | 2012-02-16 16:10:23 -0800 | [diff] [blame] | 1596 | } |
| 1597 | |
| 1598 | /** |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 1599 | * 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 Pereira | 161f50d | 2012-02-28 15:47:19 -0800 | [diff] [blame] | 1601 | * |
Vikram Aggarwal | 6c51158 | 2012-02-27 10:59:47 -0800 | [diff] [blame] | 1602 | * @param accounts cursor into the AccountCache |
Vikram Aggarwal | 7dedb95 | 2012-02-16 16:10:23 -0800 | [diff] [blame] | 1603 | * @return true if the update was successful, false otherwise |
| 1604 | */ |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 1605 | private boolean updateAccounts(Cursor accounts) { |
Vikram Aggarwal | 7dedb95 | 2012-02-16 16:10:23 -0800 | [diff] [blame] | 1606 | if (accounts == null || !accounts.moveToFirst()) { |
| 1607 | return false; |
| 1608 | } |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 1609 | |
Vikram Aggarwal | a9b93f3 | 2012-02-23 14:51:58 -0800 | [diff] [blame] | 1610 | final Account[] allAccounts = Account.getAllAccounts(accounts); |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 1611 | // A match for the current account's URI in the list of accounts. |
| 1612 | Account currentFromList = null; |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 1613 | |
| 1614 | // Save the uris for the accounts |
| 1615 | mCurrentAccountUris.clear(); |
| 1616 | for (Account account : allAccounts) { |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 1617 | LogUtils.d(LOG_TAG, "updateAccounts(%s)", account); |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 1618 | mCurrentAccountUris.add(account.uri); |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 1619 | if (mAccount != null && account.uri.equals(mAccount.uri)) { |
| 1620 | currentFromList = account; |
| 1621 | } |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 1622 | } |
| 1623 | |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 1624 | // 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 Huang | 0d64735 | 2012-03-21 21:48:16 -0700 | [diff] [blame] | 1627 | // 2. current account is set and is not in allAccounts -> pick first (acct was deleted?) |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 1628 | // 3. saved preference has an account -> pick that one |
Andy Huang | 0d64735 | 2012-03-21 21:48:16 -0700 | [diff] [blame] | 1629 | // 4. otherwise just pick first |
| 1630 | |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 1631 | 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 Huang | 0d64735 | 2012-03-21 21:48:16 -0700 | [diff] [blame] | 1639 | } |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 1640 | // Case 1b: else, current account is unchanged: nothing to do. |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 1641 | } else { |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 1642 | // 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 Huang | 0d64735 | 2012-03-21 21:48:16 -0700 | [diff] [blame] | 1653 | } |
| 1654 | } |
| 1655 | } |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 1656 | } |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 1657 | 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 Aggarwal | a9b93f3 | 2012-02-23 14:51:58 -0800 | [diff] [blame] | 1662 | mActionBarView.setAccounts(allAccounts); |
| 1663 | return (allAccounts.length > 0); |
Vikram Aggarwal | 7dedb95 | 2012-02-16 16:10:23 -0800 | [diff] [blame] | 1664 | } |
| 1665 | |
Paul Westbrook | 6ead20d | 2012-03-19 14:48:14 -0700 | [diff] [blame] | 1666 | 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 Aggarwal | a9b93f3 | 2012-02-23 14:51:58 -0800 | [diff] [blame] | 1685 | /** |
| 1686 | * {@inheritDoc} |
| 1687 | */ |
Vikram Aggarwal | 7dedb95 | 2012-02-16 16:10:23 -0800 | [diff] [blame] | 1688 | @Override |
| 1689 | public void onLoadFinished(Loader<Cursor> loader, Cursor data) { |
Mindy Pereira | 5cb0c3e | 2012-02-22 15:22:47 -0800 | [diff] [blame] | 1690 | // We want to reinitialize only if we haven't ever been initialized, or |
| 1691 | // if the current account has vanished. |
Paul Westbrook | e3e8429 | 2012-03-05 16:19:30 -0800 | [diff] [blame] | 1692 | if (data == null) { |
Vikram Aggarwal | fa4b47e | 2012-03-09 13:02:46 -0800 | [diff] [blame] | 1693 | LogUtils.e(LOG_TAG, "Received null cursor from loader id: %d", loader.getId()); |
Paul Westbrook | e3e8429 | 2012-03-05 16:19:30 -0800 | [diff] [blame] | 1694 | } |
Vikram Aggarwal | fa4b47e | 2012-03-09 13:02:46 -0800 | [diff] [blame] | 1695 | switch (loader.getId()) { |
| 1696 | case LOADER_ACCOUNT_CURSOR: |
Vikram Aggarwal | 27d89ad | 2012-06-12 13:38:40 -0700 | [diff] [blame] | 1697 | // If the account list is not null, and the account list cursor is empty, |
Paul Westbrook | 2388c5d | 2012-03-25 12:29:11 -0700 | [diff] [blame] | 1698 | // 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 Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 1720 | isLoaderInitialized = updateAccounts(data); |
Paul Westbrook | 2388c5d | 2012-03-25 12:29:11 -0700 | [diff] [blame] | 1721 | } |
Vikram Aggarwal | fa4b47e | 2012-03-09 13:02:46 -0800 | [diff] [blame] | 1722 | } |
| 1723 | break; |
Paul Westbrook | 2d50bcd | 2012-04-10 11:53:47 -0700 | [diff] [blame] | 1724 | case LOADER_ACCOUNT_UPDATE_CURSOR: |
| 1725 | // We have gotten an update for current account. |
| 1726 | |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 1727 | // Make sure that this is an update for the current account |
Paul Westbrook | 2d50bcd | 2012-04-10 11:53:47 -0700 | [diff] [blame] | 1728 | if (data != null && data.moveToFirst()) { |
| 1729 | final Account updatedAccount = new Account(data); |
| 1730 | |
| 1731 | if (updatedAccount.uri.equals(mAccount.uri)) { |
Paul Westbrook | ca08fc1 | 2012-07-31 12:01:15 -0700 | [diff] [blame] | 1732 | // Keep a reference to the previous settings object |
| 1733 | final Settings previousSettings = mAccount.settings; |
| 1734 | |
Vikram Aggarwal | 7d81600 | 2012-04-17 17:06:41 -0700 | [diff] [blame] | 1735 | // Update the controller's reference to the current account |
Paul Westbrook | 2d50bcd | 2012-04-10 11:53:47 -0700 | [diff] [blame] | 1736 | mAccount = updatedAccount; |
Vikram Aggarwal | edc137c | 2012-04-24 13:40:58 -0700 | [diff] [blame] | 1737 | LogUtils.d(LOG_TAG, "AbstractActivityController.onLoadFinished(): " |
| 1738 | + "mAccount = %s", mAccount.uri); |
Paul Westbrook | ca08fc1 | 2012-07-31 12:01:15 -0700 | [diff] [blame] | 1739 | |
| 1740 | // Only notify about a settings change if something differs |
| 1741 | if (!Objects.equal(mAccount.settings, previousSettings)) { |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 1742 | mAccountObservers.notifyChanged(); |
Paul Westbrook | ca08fc1 | 2012-07-31 12:01:15 -0700 | [diff] [blame] | 1743 | } |
Paul Westbrook | 2d50bcd | 2012-04-10 11:53:47 -0700 | [diff] [blame] | 1744 | |
| 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 Pereira | 830bdaf | 2012-07-11 12:59:55 -0700 | [diff] [blame] | 1750 | } else if (updatedAccount.isAccountIntialized()) { |
| 1751 | if (inWaitingMode) { |
| 1752 | // Dismiss waiting mode |
| 1753 | hideWaitForInitialization(); |
| 1754 | } |
Paul Westbrook | 2d50bcd | 2012-04-10 11:53:47 -0700 | [diff] [blame] | 1755 | } 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 Aggarwal | fa4b47e | 2012-03-09 13:02:46 -0800 | [diff] [blame] | 1768 | case LOADER_FOLDER_CURSOR: |
Vikram Aggarwal | fa4b47e | 2012-03-09 13:02:46 -0800 | [diff] [blame] | 1769 | // Check status of the cursor. |
Marc Blank | fd9d0b8 | 2012-04-23 16:01:51 -0700 | [diff] [blame] | 1770 | if (data != null && data.moveToFirst()) { |
Andy Huang | 090db1e | 2012-07-25 13:25:28 -0700 | [diff] [blame] | 1771 | final Folder folder = new Folder(data); |
Marc Blank | fd9d0b8 | 2012-04-23 16:01:51 -0700 | [diff] [blame] | 1772 | LogUtils.d(LOG_TAG, "FOLDER STATUS = %d", folder.syncStatus); |
Andy Huang | 090db1e | 2012-07-25 13:25:28 -0700 | [diff] [blame] | 1773 | |
| 1774 | mFolder = folder; |
| 1775 | mFolderObservable.notifyChanged(); |
| 1776 | |
Paul Westbrook | c808fac | 2012-02-22 16:42:18 -0800 | [diff] [blame] | 1777 | } else { |
Marc Blank | fd9d0b8 | 2012-04-23 16:01:51 -0700 | [diff] [blame] | 1778 | LogUtils.d(LOG_TAG, "Unable to get the folder %s", |
| 1779 | mFolder != null ? mAccount.name : ""); |
Mindy Pereira | 11dd5ef | 2012-03-10 15:10:18 -0800 | [diff] [blame] | 1780 | } |
Vikram Aggarwal | fa4b47e | 2012-03-09 13:02:46 -0800 | [diff] [blame] | 1781 | break; |
Vikram Aggarwal | fa4b47e | 2012-03-09 13:02:46 -0800 | [diff] [blame] | 1782 | case LOADER_RECENT_FOLDERS: |
Vikram Aggarwal | 27d89ad | 2012-06-12 13:38:40 -0700 | [diff] [blame] | 1783 | // 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 Aggarwal | fa4b47e | 2012-03-09 13:02:46 -0800 | [diff] [blame] | 1800 | mRecentFolderList.loadFromUiProvider(data); |
Vikram Aggarwal | 3726397 | 2012-04-17 15:51:14 -0700 | [diff] [blame] | 1801 | mActionBarView.requestRecentFoldersAndRedraw(); |
Vikram Aggarwal | fa4b47e | 2012-03-09 13:02:46 -0800 | [diff] [blame] | 1802 | break; |
Mindy Pereira | ab48636 | 2012-03-21 18:18:53 -0700 | [diff] [blame] | 1803 | case LOADER_ACCOUNT_INBOX: |
Marc Blank | fd9d0b8 | 2012-04-23 16:01:51 -0700 | [diff] [blame] | 1804 | if (data != null && !data.isClosed() && data.moveToFirst()) { |
Mindy Pereira | 0e88e9f | 2012-03-25 13:47:41 -0700 | [diff] [blame] | 1805 | Folder inbox = new Folder(data); |
| 1806 | onFolderChanged(inbox); |
Mindy Pereira | b4a4328 | 2012-03-23 16:20:03 -0700 | [diff] [blame] | 1807 | // 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 Pereira | 5ba3380 | 2012-03-26 16:30:11 -0700 | [diff] [blame] | 1810 | } else { |
| 1811 | LogUtils.d(LOG_TAG, "Unable to get the account inbox for account %s", |
| 1812 | mAccount != null ? mAccount.name : ""); |
Mindy Pereira | b4a4328 | 2012-03-23 16:20:03 -0700 | [diff] [blame] | 1813 | } |
Mindy Pereira | ab48636 | 2012-03-21 18:18:53 -0700 | [diff] [blame] | 1814 | break; |
| 1815 | case LOADER_SEARCH: |
| 1816 | data.moveToFirst(); |
| 1817 | Folder search = new Folder(data); |
Mindy Pereira | 11e3596 | 2012-06-01 14:49:46 -0700 | [diff] [blame] | 1818 | updateFolder(search); |
Mindy Pereira | ab48636 | 2012-03-21 18:18:53 -0700 | [diff] [blame] | 1819 | mConvListContext = ConversationListContext.forSearchQuery(mAccount, mFolder, |
Mindy Pereira | d660d25 | 2012-03-26 11:48:43 -0700 | [diff] [blame] | 1820 | mActivity.getIntent() |
| 1821 | .getStringExtra(UIProvider.SearchQueryParameters.QUERY)); |
Mindy Pereira | ab48636 | 2012-03-21 18:18:53 -0700 | [diff] [blame] | 1822 | showConversationList(mConvListContext); |
| 1823 | mActivity.invalidateOptionsMenu(); |
Mindy Pereira | 3b39922 | 2012-03-28 15:19:47 -0700 | [diff] [blame] | 1824 | mActivity.getLoaderManager().destroyLoader(LOADER_SEARCH); |
Mindy Pereira | ab48636 | 2012-03-21 18:18:53 -0700 | [diff] [blame] | 1825 | break; |
Vikram Aggarwal | 7dedb95 | 2012-02-16 16:10:23 -0800 | [diff] [blame] | 1826 | } |
| 1827 | } |
| 1828 | |
Vikram Aggarwal | c769422 | 2012-04-23 13:37:01 -0700 | [diff] [blame] | 1829 | /** |
| 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 Pereira | de3e74a | 2012-07-24 09:43:10 -0700 | [diff] [blame] | 1834 | public class ConversationAction implements DestructiveAction { |
Vikram Aggarwal | acaa3c0 | 2012-04-24 12:45:27 -0700 | [diff] [blame] | 1835 | /** |
| 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 Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 1839 | private final int mAction; |
Vikram Aggarwal | bc67bb1 | 2012-04-30 14:05:35 -0700 | [diff] [blame] | 1840 | /** The action will act upon these conversations */ |
Paul Westbrook | 77eee62 | 2012-07-10 13:41:57 -0700 | [diff] [blame] | 1841 | private final Collection<Conversation> mTarget; |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 1842 | /** Whether this destructive action has already been performed */ |
| 1843 | private boolean mCompleted; |
Vikram Aggarwal | c411395 | 2012-05-11 14:14:56 -0700 | [diff] [blame] | 1844 | /** Whether this is an action on the currently selected set. */ |
| 1845 | private final boolean mIsSelectedSet; |
Vikram Aggarwal | e8a8532 | 2012-04-24 09:01:38 -0700 | [diff] [blame] | 1846 | |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 1847 | /** |
| 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 Aggarwal | bc67bb1 | 2012-04-30 14:05:35 -0700 | [diff] [blame] | 1851 | * @param target Conversation that we want to apply the action to. |
Vikram Aggarwal | c411395 | 2012-05-11 14:14:56 -0700 | [diff] [blame] | 1852 | * @param isBatch whether the conversations are in the currently selected batch set. |
| 1853 | */ |
| 1854 | public ConversationAction(int action, Collection<Conversation> target, boolean isBatch) { |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 1855 | mAction = action; |
Paul Westbrook | 77eee62 | 2012-07-10 13:41:57 -0700 | [diff] [blame] | 1856 | mTarget = ImmutableList.copyOf(target); |
Vikram Aggarwal | c411395 | 2012-05-11 14:14:56 -0700 | [diff] [blame] | 1857 | mIsSelectedSet = isBatch; |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 1858 | } |
| 1859 | |
Vikram Aggarwal | acaa3c0 | 2012-04-24 12:45:27 -0700 | [diff] [blame] | 1860 | /** |
| 1861 | * The action common to child classes. This performs the action specified in the constructor |
| 1862 | * on the conversations given here. |
Vikram Aggarwal | acaa3c0 | 2012-04-24 12:45:27 -0700 | [diff] [blame] | 1863 | */ |
Vikram Aggarwal | bc67bb1 | 2012-04-30 14:05:35 -0700 | [diff] [blame] | 1864 | @Override |
| 1865 | public void performAction() { |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 1866 | if (isPerformed()) { |
| 1867 | return; |
| 1868 | } |
Mindy Pereira | 3cb28b5 | 2012-05-24 15:26:39 -0700 | [diff] [blame] | 1869 | boolean undoEnabled = mAccount.supportsCapability(AccountCapabilities.UNDO); |
Vikram Aggarwal | 7dd054e | 2012-05-21 14:43:10 -0700 | [diff] [blame] | 1870 | |
| 1871 | // Are we destroying the currently shown conversation? Show the next one. |
| 1872 | if (LogUtils.isLoggable(LOG_TAG, LogUtils.DEBUG)){ |
Vikram Aggarwal | 8742a61 | 2012-08-13 10:22:50 -0700 | [diff] [blame] | 1873 | LogUtils.d(LOG_TAG, "ConversationAction.performAction():" |
| 1874 | + "\nmTarget=%s\nCurrent=%s", |
Vikram Aggarwal | 7dd054e | 2012-05-21 14:43:10 -0700 | [diff] [blame] | 1875 | Conversation.toString(mTarget), mCurrentConversation); |
| 1876 | } |
Vikram Aggarwal | 7dd054e | 2012-05-21 14:43:10 -0700 | [diff] [blame] | 1877 | |
Paul Westbrook | e1221d2 | 2012-08-19 11:09:07 -0700 | [diff] [blame] | 1878 | 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 Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 1885 | switch (mAction) { |
Mindy Pereira | 0692baf | 2012-03-23 17:34:31 -0700 | [diff] [blame] | 1886 | case R.id.archive: |
Vikram Aggarwal | 7dd054e | 2012-05-21 14:43:10 -0700 | [diff] [blame] | 1887 | LogUtils.d(LOG_TAG, "Archiving"); |
Vikram Aggarwal | bc67bb1 | 2012-04-30 14:05:35 -0700 | [diff] [blame] | 1888 | mConversationListCursor.archive(mContext, mTarget); |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 1889 | break; |
| 1890 | case R.id.delete: |
Vikram Aggarwal | 7dd054e | 2012-05-21 14:43:10 -0700 | [diff] [blame] | 1891 | LogUtils.d(LOG_TAG, "Deleting"); |
Vikram Aggarwal | bc67bb1 | 2012-04-30 14:05:35 -0700 | [diff] [blame] | 1892 | mConversationListCursor.delete(mContext, mTarget); |
Mindy Pereira | 695d696 | 2012-06-18 13:02:10 -0700 | [diff] [blame] | 1893 | if (mFolder.supportsCapability(FolderCapabilities.DELETE_ACTION_FINAL)) { |
Marc Blank | 386243f | 2012-05-25 10:40:59 -0700 | [diff] [blame] | 1894 | undoEnabled = false; |
| 1895 | } |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 1896 | break; |
| 1897 | case R.id.mute: |
Vikram Aggarwal | 7dd054e | 2012-05-21 14:43:10 -0700 | [diff] [blame] | 1898 | LogUtils.d(LOG_TAG, "Muting"); |
Vikram Aggarwal | bc67bb1 | 2012-04-30 14:05:35 -0700 | [diff] [blame] | 1899 | if (mFolder.supportsCapability(FolderCapabilities.DESTRUCTIVE_MUTE)) { |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 1900 | for (Conversation c : mTarget) { |
| 1901 | c.localDeleteOnUpdate = true; |
| 1902 | } |
Vikram Aggarwal | bc67bb1 | 2012-04-30 14:05:35 -0700 | [diff] [blame] | 1903 | } |
| 1904 | mConversationListCursor.mute(mContext, mTarget); |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 1905 | break; |
| 1906 | case R.id.report_spam: |
Vikram Aggarwal | 7dd054e | 2012-05-21 14:43:10 -0700 | [diff] [blame] | 1907 | LogUtils.d(LOG_TAG, "Reporting spam"); |
Vikram Aggarwal | bc67bb1 | 2012-04-30 14:05:35 -0700 | [diff] [blame] | 1908 | mConversationListCursor.reportSpam(mContext, mTarget); |
| 1909 | break; |
Paul Westbrook | 77eee62 | 2012-07-10 13:41:57 -0700 | [diff] [blame] | 1910 | case R.id.mark_not_spam: |
| 1911 | LogUtils.d(LOG_TAG, "Marking not spam"); |
| 1912 | mConversationListCursor.reportNotSpam(mContext, mTarget); |
| 1913 | break; |
Paul Westbrook | 76b2062 | 2012-07-12 11:45:43 -0700 | [diff] [blame] | 1914 | case R.id.report_phishing: |
| 1915 | LogUtils.d(LOG_TAG, "Reporting phishing"); |
| 1916 | mConversationListCursor.reportPhishing(mContext, mTarget); |
| 1917 | break; |
Vikram Aggarwal | bc67bb1 | 2012-04-30 14:05:35 -0700 | [diff] [blame] | 1918 | case R.id.remove_star: |
Vikram Aggarwal | 7dd054e | 2012-05-21 14:43:10 -0700 | [diff] [blame] | 1919 | LogUtils.d(LOG_TAG, "Removing star"); |
Vikram Aggarwal | bc67bb1 | 2012-04-30 14:05:35 -0700 | [diff] [blame] | 1920 | // 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 Aggarwal | 7dd054e | 2012-05-21 14:43:10 -0700 | [diff] [blame] | 1925 | LogUtils.d(LOG_TAG, "Marking not-important"); |
Mindy Pereira | 445be21 | 2012-08-15 08:50:10 -0700 | [diff] [blame] | 1926 | // Marking not important is destructive in a mailbox |
| 1927 | // containing only important messages |
Mindy Pereira | 0d03ef8 | 2012-08-15 09:05:48 -0700 | [diff] [blame] | 1928 | if (mFolder != null && mFolder.isImportantOnly()) { |
Mindy Pereira | 445be21 | 2012-08-15 08:50:10 -0700 | [diff] [blame] | 1929 | for (Conversation conv : mTarget) { |
| 1930 | conv.localDeleteOnUpdate = true; |
| 1931 | } |
| 1932 | } |
Vikram Aggarwal | bc67bb1 | 2012-04-30 14:05:35 -0700 | [diff] [blame] | 1933 | mConversationListCursor.updateInt(mContext, mTarget, |
| 1934 | ConversationColumns.PRIORITY, UIProvider.ConversationPriority.LOW); |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 1935 | break; |
Paul Westbrook | ef36254 | 2012-08-27 14:53:32 -0700 | [diff] [blame] | 1936 | 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 Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 1948 | } |
| 1949 | if (undoEnabled) { |
mindyp | ead5039 | 2012-08-23 11:03:53 -0700 | [diff] [blame] | 1950 | mHandler.postDelayed(new Runnable() { |
| 1951 | @Override |
| 1952 | public void run() { |
| 1953 | onUndoAvailable(new ToastBarOperation(mTarget.size(), mAction, |
| 1954 | ToastBarOperation.UNDO)); |
| 1955 | } |
| 1956 | }, mShowUndoBarDelay); |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 1957 | } |
Vikram Aggarwal | bc67bb1 | 2012-04-30 14:05:35 -0700 | [diff] [blame] | 1958 | refreshConversationList(); |
Vikram Aggarwal | c411395 | 2012-05-11 14:14:56 -0700 | [diff] [blame] | 1959 | if (mIsSelectedSet) { |
Vikram Aggarwal | c411395 | 2012-05-11 14:14:56 -0700 | [diff] [blame] | 1960 | mSelectedSet.clear(); |
| 1961 | } |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 1962 | } |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 1963 | |
| 1964 | /** |
| 1965 | * Returns true if this action has been performed, false otherwise. |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 1966 | * |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 1967 | */ |
| 1968 | private synchronized boolean isPerformed() { |
| 1969 | if (mCompleted) { |
| 1970 | return true; |
| 1971 | } |
| 1972 | mCompleted = true; |
| 1973 | return false; |
| 1974 | } |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 1975 | } |
Mindy Pereira | e5f4dc0 | 2012-03-21 16:08:53 -0700 | [diff] [blame] | 1976 | |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 1977 | /** |
| 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 Aggarwal | 531488e | 2012-05-29 16:36:52 -0700 | [diff] [blame] | 1985 | private final DestructiveAction getAction(int action, Collection<Conversation> target) { |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 1986 | final DestructiveAction da = new ConversationAction(action, target, false); |
| 1987 | registerDestructiveAction(da); |
| 1988 | return da; |
| 1989 | } |
| 1990 | |
Vikram Aggarwal | d503df4 | 2012-05-11 10:13:35 -0700 | [diff] [blame] | 1991 | // Called from the FolderSelectionDialog after a user is done selecting folders to assign the |
| 1992 | // conversations to. |
Mindy Pereira | e5f4dc0 | 2012-03-21 16:08:53 -0700 | [diff] [blame] | 1993 | @Override |
Mindy Pereira | 8db7e40 | 2012-07-13 10:32:47 -0700 | [diff] [blame] | 1994 | 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 Aggarwal | d503df4 | 2012-05-11 10:13:35 -0700 | [diff] [blame] | 2002 | LogUtils.d(LOG_TAG, "onFolderChangesCommit: isDestructive = %b", isDestructive); |
| 2003 | if (isDestructive) { |
| 2004 | for (final Conversation c : target) { |
| 2005 | c.localDeleteOnUpdate = true; |
Mindy Pereira | 6778f46 | 2012-03-23 18:01:55 -0700 | [diff] [blame] | 2006 | } |
Mindy Pereira | e5f4dc0 | 2012-03-21 16:08:53 -0700 | [diff] [blame] | 2007 | } |
Mindy Pereira | 8db7e40 | 2012-07-13 10:32:47 -0700 | [diff] [blame] | 2008 | final DestructiveAction folderChange = getFolderChange(target, folderOps, isDestructive, |
Mindy Pereira | 06642fa | 2012-07-12 16:23:27 -0700 | [diff] [blame] | 2009 | batch, showUndo); |
Vikram Aggarwal | d503df4 | 2012-05-11 10:13:35 -0700 | [diff] [blame] | 2010 | // Update the UI elements depending no their visibility and availability |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 2011 | // TODO(viki): Consolidate this into a single method requestDelete. |
Vikram Aggarwal | d503df4 | 2012-05-11 10:13:35 -0700 | [diff] [blame] | 2012 | if (isDestructive) { |
Vikram Aggarwal | 4f4782b | 2012-05-30 08:39:09 -0700 | [diff] [blame] | 2013 | delete(target, folderChange); |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 2014 | } else { |
Vikram Aggarwal | d503df4 | 2012-05-11 10:13:35 -0700 | [diff] [blame] | 2015 | requestUpdate(target, folderChange); |
Mindy Pereira | e5f4dc0 | 2012-03-21 16:08:53 -0700 | [diff] [blame] | 2016 | } |
| 2017 | } |
| 2018 | |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 2019 | @Override |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 2020 | public final void onRefreshRequired() { |
mindyp | 5390fca | 2012-08-22 12:12:25 -0700 | [diff] [blame] | 2021 | if (isAnimating() || isDragging()) { |
Mindy Pereira | 69e88dd | 2012-08-10 09:30:18 -0700 | [diff] [blame] | 2022 | LogUtils.d(LOG_TAG, "onRefreshRequired: delay until animating done"); |
| 2023 | return; |
| 2024 | } |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 2025 | // Refresh the query in the background |
Paul Westbrook | cff1aea | 2012-08-10 11:51:00 -0700 | [diff] [blame] | 2026 | if (mConversationListCursor.isRefreshRequired()) { |
| 2027 | mConversationListCursor.refresh(); |
| 2028 | } |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 2029 | } |
| 2030 | |
mindyp | 5390fca | 2012-08-22 12:12:25 -0700 | [diff] [blame] | 2031 | @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 Pereira | 69e88dd | 2012-08-10 09:30:18 -0700 | [diff] [blame] | 2054 | 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 Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 2066 | /** |
| 2067 | * Called when the {@link ConversationCursor} is changed or has new data in it. |
| 2068 | * <p> |
| 2069 | * {@inheritDoc} |
| 2070 | */ |
| 2071 | @Override |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 2072 | public final void onRefreshReady() { |
Paul Westbrook | cff1aea | 2012-08-10 11:51:00 -0700 | [diff] [blame] | 2073 | if (!isAnimating()) { |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 2074 | // Swap cursors |
| 2075 | mConversationListCursor.sync(); |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 2076 | } |
Paul Westbrook | 937c94f | 2012-08-16 13:01:18 -0700 | [diff] [blame] | 2077 | mTracker.onCursorUpdated(); |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 2078 | } |
| 2079 | |
| 2080 | @Override |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 2081 | public final void onDataSetChanged() { |
Paul Westbrook | 9f119c7 | 2012-04-24 16:10:59 -0700 | [diff] [blame] | 2082 | updateConversationListFragment(); |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 2083 | mConversationListObservable.notifyChanged(); |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 2084 | } |
| 2085 | |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 2086 | /** |
| 2087 | * If the Conversation List Fragment is visible, updates the fragment. |
| 2088 | */ |
| 2089 | private final void updateConversationListFragment() { |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 2090 | final ConversationListFragment convList = getConversationListFragment(); |
| 2091 | if (convList != null) { |
Vikram Aggarwal | 75daee5 | 2012-04-30 11:13:09 -0700 | [diff] [blame] | 2092 | refreshConversationList(); |
Paul Westbrook | 9f119c7 | 2012-04-24 16:10:59 -0700 | [diff] [blame] | 2093 | if (convList.isVisible()) { |
Paul Westbrook | 9f119c7 | 2012-04-24 16:10:59 -0700 | [diff] [blame] | 2094 | Utils.setConversationCursorVisibility(mConversationListCursor, true); |
| 2095 | } |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 2096 | } |
| 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 Westbrook | cff1aea | 2012-08-10 11:51:00 -0700 | [diff] [blame] | 2133 | public void onAnimationEnd(AnimatedAdapter animatedAdapter) { |
| 2134 | if (mConversationListCursor.isRefreshReady()) { |
mindyp | 5254486 | 2012-08-20 12:05:36 -0700 | [diff] [blame] | 2135 | LogUtils.d(LOG_TAG, "Stopped animating: try sync"); |
Paul Westbrook | cff1aea | 2012-08-10 11:51:00 -0700 | [diff] [blame] | 2136 | onRefreshReady(); |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 2137 | } |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 2138 | |
Paul Westbrook | cff1aea | 2012-08-10 11:51:00 -0700 | [diff] [blame] | 2139 | if (mConversationListCursor.isRefreshRequired()) { |
mindyp | 5254486 | 2012-08-20 12:05:36 -0700 | [diff] [blame] | 2140 | LogUtils.d(LOG_TAG, "Stopped animating: refresh"); |
Paul Westbrook | cff1aea | 2012-08-10 11:51:00 -0700 | [diff] [blame] | 2141 | mConversationListCursor.refresh(); |
| 2142 | } |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 2143 | } |
| 2144 | |
| 2145 | @Override |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 2146 | public void onSetEmpty() { |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 2147 | } |
| 2148 | |
| 2149 | @Override |
| 2150 | public void onSetPopulated(ConversationSelectionSet set) { |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 2151 | final ConversationListFragment convList = getConversationListFragment(); |
| 2152 | if (convList == null) { |
| 2153 | return; |
| 2154 | } |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 2155 | mCabActionMenu = new SelectedConversationsActionMenu(mActivity, set, mFolder, |
Vikram Aggarwal | 531488e | 2012-05-29 16:36:52 -0700 | [diff] [blame] | 2156 | (SwipeableListView) convList.getListView()); |
Vikram Aggarwal | e128fc2 | 2012-04-04 12:33:34 -0700 | [diff] [blame] | 2157 | enableCabMode(); |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 2158 | } |
| 2159 | |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 2160 | @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 Aggarwal | e128fc2 | 2012-04-04 12:33:34 -0700 | [diff] [blame] | 2170 | /** |
| 2171 | * Disable the Contextual Action Bar (CAB). The selected set is not changed. |
| 2172 | */ |
| 2173 | protected void disableCabMode() { |
Mindy Pereira | 8937bf1 | 2012-07-23 14:05:02 -0700 | [diff] [blame] | 2174 | // Commit any previous destructive actions when entering/ exiting CAB mode. |
mindyp | c6adce3 | 2012-08-22 18:46:42 -0700 | [diff] [blame] | 2175 | commitDestructiveActions(true); |
Vikram Aggarwal | e128fc2 | 2012-04-04 12:33:34 -0700 | [diff] [blame] | 2176 | 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 Pereira | 8937bf1 | 2012-07-23 14:05:02 -0700 | [diff] [blame] | 2185 | // Commit any previous destructive actions when entering/ exiting CAB mode. |
mindyp | c6adce3 | 2012-08-22 18:46:42 -0700 | [diff] [blame] | 2186 | commitDestructiveActions(true); |
Vikram Aggarwal | e128fc2 | 2012-04-04 12:33:34 -0700 | [diff] [blame] | 2187 | if (mCabActionMenu != null) { |
| 2188 | mCabActionMenu.activate(); |
| 2189 | } |
| 2190 | } |
| 2191 | |
Vikram Aggarwal | 4eb5271 | 2012-06-19 16:24:50 -0700 | [diff] [blame] | 2192 | /** |
| 2193 | * Unselect conversations and exit CAB mode. |
| 2194 | */ |
| 2195 | protected final void exitCabMode() { |
| 2196 | mSelectedSet.clear(); |
| 2197 | } |
| 2198 | |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 2199 | @Override |
Mindy Pereira | fd0c297 | 2012-03-27 13:50:39 -0700 | [diff] [blame] | 2200 | public void startSearch() { |
Vikram Aggarwal | 35f19d7 | 2012-04-24 13:24:48 -0700 | [diff] [blame] | 2201 | 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 Pereira | fd0c297 | 2012-03-27 13:50:39 -0700 | [diff] [blame] | 2206 | if (mAccount.supportsCapability(UIProvider.AccountCapabilities.LOCAL_SEARCH) |
| 2207 | | mAccount.supportsCapability(UIProvider.AccountCapabilities.SERVER_SEARCH)) { |
Vikram Aggarwal | 70f130e | 2012-04-03 12:32:14 -0700 | [diff] [blame] | 2208 | onSearchRequested(mActionBarView.getQuery()); |
Mindy Pereira | fd0c297 | 2012-03-27 13:50:39 -0700 | [diff] [blame] | 2209 | } else { |
| 2210 | Toast.makeText(mActivity.getActivityContext(), mActivity.getActivityContext() |
Mindy Pereira | a46c299 | 2012-03-27 14:12:39 -0700 | [diff] [blame] | 2211 | .getString(R.string.search_unsupported), Toast.LENGTH_SHORT).show(); |
Mindy Pereira | fd0c297 | 2012-03-27 13:50:39 -0700 | [diff] [blame] | 2212 | } |
| 2213 | } |
Mindy Pereira | acf6039 | 2012-04-06 09:11:00 -0700 | [diff] [blame] | 2214 | |
Vikram Aggarwal | 0dda573 | 2012-04-06 11:20:16 -0700 | [diff] [blame] | 2215 | @Override |
| 2216 | public void exitSearchMode() { |
| 2217 | if (mViewMode.getMode() == ViewMode.SEARCH_RESULTS_LIST) { |
| 2218 | mActivity.finish(); |
| 2219 | } |
| 2220 | } |
| 2221 | |
Mindy Pereira | acf6039 | 2012-04-06 09:11:00 -0700 | [diff] [blame] | 2222 | /** |
| 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 Pereira | 6c2663d | 2012-07-20 15:37:29 -0700 | [diff] [blame] | 2238 | * Handles dropping conversations to a folder. |
Mindy Pereira | acf6039 | 2012-04-06 09:11:00 -0700 | [diff] [blame] | 2239 | */ |
| 2240 | @Override |
| 2241 | public void handleDrop(DragEvent event, final Folder folder) { |
Mindy Pereira | acf6039 | 2012-04-06 09:11:00 -0700 | [diff] [blame] | 2242 | if (!supportsDrag(event, folder)) { |
| 2243 | return; |
| 2244 | } |
Mindy Pereira | acf6039 | 2012-04-06 09:11:00 -0700 | [diff] [blame] | 2245 | final Collection<Conversation> conversations = mSelectedSet.values(); |
Mindy Pereira | 8db7e40 | 2012-07-13 10:32:47 -0700 | [diff] [blame] | 2246 | 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 Pereira | 06642fa | 2012-07-12 16:23:27 -0700 | [diff] [blame] | 2250 | final DestructiveAction action = getFolderChange(conversations, dropTarget, true, true, |
| 2251 | true); |
Vikram Aggarwal | 4f4782b | 2012-05-30 08:39:09 -0700 | [diff] [blame] | 2252 | delete(conversations, action); |
Mindy Pereira | acf6039 | 2012-04-06 09:11:00 -0700 | [diff] [blame] | 2253 | } |
Mindy Pereira | 0963ef8 | 2012-04-10 11:43:01 -0700 | [diff] [blame] | 2254 | |
| 2255 | @Override |
Mindy Pereira | 0963ef8 | 2012-04-10 11:43:01 -0700 | [diff] [blame] | 2256 | public void onTouchEvent(MotionEvent event) { |
| 2257 | if (event.getAction() == MotionEvent.ACTION_DOWN) { |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 2258 | if (mToastBar != null && !mToastBar.isEventInToastBar(event)) { |
| 2259 | mToastBar.hide(true); |
Mindy Pereira | 0963ef8 | 2012-04-10 11:43:01 -0700 | [diff] [blame] | 2260 | } |
| 2261 | } |
| 2262 | } |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 2263 | |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 2264 | @Override |
| 2265 | public void onConversationSeen(Conversation conv) { |
| 2266 | mPagerController.onConversationSeen(conv); |
| 2267 | } |
| 2268 | |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 2269 | 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 Westbrook | 9a70e91 | 2012-08-17 15:53:20 -0700 | [diff] [blame] | 2275 | mAccount, mFolder.conversationListUri, mFolder.name); |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 2276 | return result; |
| 2277 | } |
| 2278 | |
| 2279 | @Override |
| 2280 | public void onLoadFinished(Loader<ConversationCursor> loader, ConversationCursor data) { |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 2281 | LogUtils.d(LOG_TAG, "IN AAC.ConversationCursor.onLoadFinished, data=%s loader=%s", |
| 2282 | data, loader); |
Vikram Aggarwal | e8a8532 | 2012-04-24 09:01:38 -0700 | [diff] [blame] | 2283 | // Clear our all pending destructive actions before swapping the conversation cursor |
| 2284 | destroyPending(null); |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 2285 | mConversationListCursor = data; |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 2286 | mConversationListCursor.addListener(AbstractActivityController.this); |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 2287 | |
Paul Westbrook | 937c94f | 2012-08-16 13:01:18 -0700 | [diff] [blame] | 2288 | mTracker.onCursorUpdated(); |
| 2289 | |
Andy Huang | e3df1ad | 2012-04-24 17:15:23 -0700 | [diff] [blame] | 2290 | mConversationListObservable.notifyChanged(); |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 2291 | // 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 Westbrook | 9f119c7 | 2012-04-24 16:10:59 -0700 | [diff] [blame] | 2296 | |
| 2297 | if (convList.isVisible()) { |
| 2298 | // The conversation list is visible. |
| 2299 | Utils.setConversationCursorVisibility(mConversationListCursor, true); |
| 2300 | } |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 2301 | } |
| 2302 | // Shown for search results in two-pane mode only. |
| 2303 | if (shouldShowFirstConversation()) { |
| 2304 | if (mConversationListCursor.getCount() > 0) { |
| 2305 | mConversationListCursor.moveToPosition(0); |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 2306 | final Conversation conv = new Conversation(mConversationListCursor); |
| 2307 | conv.position = 0; |
Andy Huang | 1ee96b2 | 2012-08-24 20:19:53 -0700 | [diff] [blame] | 2308 | onConversationSelected(conv, true /* checkSafeToModifyFragments */); |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 2309 | } |
| 2310 | } |
| 2311 | } |
| 2312 | |
| 2313 | @Override |
| 2314 | public void onLoaderReset(Loader<ConversationCursor> loader) { |
Paul Westbrook | 9a70e91 | 2012-08-17 15:53:20 -0700 | [diff] [blame] | 2315 | 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 Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 2331 | } |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 2332 | } |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 2333 | } |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 2334 | |
Vikram Aggarwal | e8a8532 | 2012-04-24 09:01:38 -0700 | [diff] [blame] | 2335 | /** |
| 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 Aggarwal | acaa3c0 | 2012-04-24 12:45:27 -0700 | [diff] [blame] | 2350 | * action as a side effect. This method is final because we don't want the child classes to |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 2351 | * embellish this method any more. |
Vikram Aggarwal | e8a8532 | 2012-04-24 09:01:38 -0700 | [diff] [blame] | 2352 | * @param action |
| 2353 | */ |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 2354 | private final void registerDestructiveAction(DestructiveAction action) { |
Vikram Aggarwal | e8a8532 | 2012-04-24 09:01:38 -0700 | [diff] [blame] | 2355 | // 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 Aggarwal | 531488e | 2012-05-29 16:36:52 -0700 | [diff] [blame] | 2362 | @Override |
| 2363 | public final DestructiveAction getBatchAction(int action) { |
Vikram Aggarwal | c411395 | 2012-05-11 14:14:56 -0700 | [diff] [blame] | 2364 | final DestructiveAction da = new ConversationAction(action, mSelectedSet.values(), true); |
Vikram Aggarwal | e8a8532 | 2012-04-24 09:01:38 -0700 | [diff] [blame] | 2365 | registerDestructiveAction(da); |
| 2366 | return da; |
| 2367 | } |
Vikram Aggarwal | 41e6e71 | 2012-04-24 11:22:57 -0700 | [diff] [blame] | 2368 | |
Mindy Pereira | de3e74a | 2012-07-24 09:43:10 -0700 | [diff] [blame] | 2369 | @Override |
| 2370 | public final DestructiveAction getDeferredBatchAction(int action) { |
| 2371 | final DestructiveAction da = new ConversationAction(action, mSelectedSet.values(), true); |
| 2372 | return da; |
| 2373 | } |
| 2374 | |
Vikram Aggarwal | bc67bb1 | 2012-04-30 14:05:35 -0700 | [diff] [blame] | 2375 | /** |
| 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 Aggarwal | 41e6e71 | 2012-04-24 11:22:57 -0700 | [diff] [blame] | 2380 | private class FolderDestruction implements DestructiveAction { |
Paul Westbrook | 77eee62 | 2012-07-10 13:41:57 -0700 | [diff] [blame] | 2381 | private final Collection<Conversation> mTarget; |
Mindy Pereira | 8db7e40 | 2012-07-13 10:32:47 -0700 | [diff] [blame] | 2382 | private final ArrayList<FolderOperation> mFolderOps = new ArrayList<FolderOperation>(); |
Vikram Aggarwal | 41e6e71 | 2012-04-24 11:22:57 -0700 | [diff] [blame] | 2383 | private final boolean mIsDestructive; |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 2384 | /** Whether this destructive action has already been performed */ |
| 2385 | private boolean mCompleted; |
Mindy Pereira | f3a4556 | 2012-05-24 16:30:19 -0700 | [diff] [blame] | 2386 | private boolean mIsSelectedSet; |
Mindy Pereira | 06642fa | 2012-07-12 16:23:27 -0700 | [diff] [blame] | 2387 | private boolean mShowUndo; |
Mindy Pereira | 01f3050 | 2012-08-14 10:30:51 -0700 | [diff] [blame] | 2388 | private int mAction; |
Vikram Aggarwal | 41e6e71 | 2012-04-24 11:22:57 -0700 | [diff] [blame] | 2389 | |
| 2390 | /** |
| 2391 | * Create a new folder destruction object to act on the given conversations. |
| 2392 | * @param target |
| 2393 | */ |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 2394 | private FolderDestruction(final Collection<Conversation> target, |
Mindy Pereira | 8db7e40 | 2012-07-13 10:32:47 -0700 | [diff] [blame] | 2395 | final Collection<FolderOperation> folders, boolean isDestructive, boolean isBatch, |
Mindy Pereira | 01f3050 | 2012-08-14 10:30:51 -0700 | [diff] [blame] | 2396 | boolean showUndo, int action) { |
Paul Westbrook | 77eee62 | 2012-07-10 13:41:57 -0700 | [diff] [blame] | 2397 | mTarget = ImmutableList.copyOf(target); |
Mindy Pereira | 8db7e40 | 2012-07-13 10:32:47 -0700 | [diff] [blame] | 2398 | mFolderOps.addAll(folders); |
Vikram Aggarwal | 41e6e71 | 2012-04-24 11:22:57 -0700 | [diff] [blame] | 2399 | mIsDestructive = isDestructive; |
Mindy Pereira | f3a4556 | 2012-05-24 16:30:19 -0700 | [diff] [blame] | 2400 | mIsSelectedSet = isBatch; |
Mindy Pereira | 06642fa | 2012-07-12 16:23:27 -0700 | [diff] [blame] | 2401 | mShowUndo = showUndo; |
Mindy Pereira | 01f3050 | 2012-08-14 10:30:51 -0700 | [diff] [blame] | 2402 | mAction = action; |
Vikram Aggarwal | 41e6e71 | 2012-04-24 11:22:57 -0700 | [diff] [blame] | 2403 | } |
| 2404 | |
Vikram Aggarwal | 41e6e71 | 2012-04-24 11:22:57 -0700 | [diff] [blame] | 2405 | @Override |
| 2406 | public void performAction() { |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 2407 | if (isPerformed()) { |
| 2408 | return; |
| 2409 | } |
Mindy Pereira | 06642fa | 2012-07-12 16:23:27 -0700 | [diff] [blame] | 2410 | if (mIsDestructive && mShowUndo) { |
Mindy Pereira | d3367499 | 2012-06-25 16:26:30 -0700 | [diff] [blame] | 2411 | ToastBarOperation undoOp = new ToastBarOperation(mTarget.size(), |
Mindy Pereira | 01f3050 | 2012-08-14 10:30:51 -0700 | [diff] [blame] | 2412 | mAction, ToastBarOperation.UNDO); |
Vikram Aggarwal | 41e6e71 | 2012-04-24 11:22:57 -0700 | [diff] [blame] | 2413 | onUndoAvailable(undoOp); |
| 2414 | } |
Mindy Pereira | 8db7e40 | 2012-07-13 10:32:47 -0700 | [diff] [blame] | 2415 | // For each conversation, for each operation, add/ remove the |
| 2416 | // appropriate folders. |
| 2417 | for (Conversation target : mTarget) { |
| 2418 | HashMap<Uri, Folder> targetFolders = Folder |
Mindy Pereira | 68f8384 | 2012-07-27 09:43:31 -0700 | [diff] [blame] | 2419 | .hashMapForFolders(target.getRawFolders()); |
Mindy Pereira | 01f3050 | 2012-08-14 10:30:51 -0700 | [diff] [blame] | 2420 | if (mIsDestructive) { |
| 2421 | target.localDeleteOnUpdate = true; |
| 2422 | } |
Mindy Pereira | 8db7e40 | 2012-07-13 10:32:47 -0700 | [diff] [blame] | 2423 | 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 Pereira | 85c4a77 | 2012-07-30 10:47:26 -0700 | [diff] [blame] | 2430 | target.setRawFolders(Folder.getSerializedFolderString(targetFolders.values())); |
Mindy Pereira | 00ffece | 2012-07-27 08:49:56 -0700 | [diff] [blame] | 2431 | mConversationListCursor.updateString(mContext, Conversation.listOf(target), |
Mindy Pereira | 85c4a77 | 2012-07-30 10:47:26 -0700 | [diff] [blame] | 2432 | Conversation.UPDATE_FOLDER_COLUMN, target.getRawFoldersString()); |
Mindy Pereira | 8db7e40 | 2012-07-13 10:32:47 -0700 | [diff] [blame] | 2433 | } |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 2434 | refreshConversationList(); |
Mindy Pereira | f3a4556 | 2012-05-24 16:30:19 -0700 | [diff] [blame] | 2435 | if (mIsSelectedSet) { |
| 2436 | mSelectedSet.clear(); |
| 2437 | } |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 2438 | } |
Mindy Pereira | 8db7e40 | 2012-07-13 10:32:47 -0700 | [diff] [blame] | 2439 | |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 2440 | /** |
| 2441 | * Returns true if this action has been performed, false otherwise. |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 2442 | * |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 2443 | */ |
| 2444 | private synchronized boolean isPerformed() { |
| 2445 | if (mCompleted) { |
| 2446 | return true; |
Vikram Aggarwal | 41e6e71 | 2012-04-24 11:22:57 -0700 | [diff] [blame] | 2447 | } |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 2448 | mCompleted = true; |
| 2449 | return false; |
Vikram Aggarwal | 41e6e71 | 2012-04-24 11:22:57 -0700 | [diff] [blame] | 2450 | } |
| 2451 | } |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 2452 | |
Vikram Aggarwal | d503df4 | 2012-05-11 10:13:35 -0700 | [diff] [blame] | 2453 | private final DestructiveAction getFolderChange(Collection<Conversation> target, |
Mindy Pereira | 8db7e40 | 2012-07-13 10:32:47 -0700 | [diff] [blame] | 2454 | Collection<FolderOperation> folders, boolean isDestructive, boolean isBatch, |
| 2455 | boolean showUndo) { |
Mindy Pereira | 06642fa | 2012-07-12 16:23:27 -0700 | [diff] [blame] | 2456 | final DestructiveAction da = new FolderDestruction(target, folders, isDestructive, isBatch, |
Mindy Pereira | 01f3050 | 2012-08-14 10:30:51 -0700 | [diff] [blame] | 2457 | 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 Aggarwal | 41e6e71 | 2012-04-24 11:22:57 -0700 | [diff] [blame] | 2479 | registerDestructiveAction(da); |
| 2480 | return da; |
| 2481 | } |
Vikram Aggarwal | 75daee5 | 2012-04-30 11:13:09 -0700 | [diff] [blame] | 2482 | |
Vikram Aggarwal | 4f4782b | 2012-05-30 08:39:09 -0700 | [diff] [blame] | 2483 | @Override |
| 2484 | public final void refreshConversationList() { |
Vikram Aggarwal | 75daee5 | 2012-04-30 11:13:09 -0700 | [diff] [blame] | 2485 | final ConversationListFragment convList = getConversationListFragment(); |
| 2486 | if (convList == null) { |
| 2487 | return; |
| 2488 | } |
| 2489 | convList.requestListRefresh(); |
| 2490 | } |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 2491 | |
| 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 Sapperstein | 9d7519d | 2012-07-16 14:03:53 -0700 | [diff] [blame] | 2514 | protected final void showErrorToast(final Folder folder, boolean replaceVisibleToast) { |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 2515 | mToastBar.setConversationMode(false); |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 2516 | |
| 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 Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 2542 | mToastBar.show( |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 2543 | listener, |
Andrew Sapperstein | 5d42096 | 2012-07-12 16:43:10 -0700 | [diff] [blame] | 2544 | R.drawable.ic_alert_white, |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 2545 | Utils.getSyncStatusText(mActivity.getActivityContext(), |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 2546 | lastSyncResult), |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 2547 | false, /* showActionIcon */ |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 2548 | actionTextResourceId, |
Andrew Sapperstein | 9d7519d | 2012-07-16 14:03:53 -0700 | [diff] [blame] | 2549 | replaceVisibleToast, |
| 2550 | new ToastBarOperation(1, 0, ToastBarOperation.ERROR)); |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 2551 | } |
| 2552 | |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 2553 | private ActionClickedListener getRetryClickedListener(final Folder folder) { |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 2554 | return new ActionClickedListener() { |
| 2555 | @Override |
| 2556 | public void onActionClicked() { |
| 2557 | final Uri uri = folder.refreshUri; |
| 2558 | |
| 2559 | if (uri != null) { |
Paul Westbrook | 4969e0c | 2012-08-20 14:38:39 -0700 | [diff] [blame] | 2560 | startAsyncRefreshTask(uri); |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 2561 | } |
| 2562 | } |
| 2563 | }; |
| 2564 | } |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 2565 | |
| 2566 | private ActionClickedListener getSignInClickedListener() { |
| 2567 | return new ActionClickedListener() { |
| 2568 | @Override |
| 2569 | public void onActionClicked() { |
Paul Westbrook | 122f7c2 | 2012-08-20 17:50:31 -0700 | [diff] [blame] | 2570 | promptUserForAuthentication(mAccount); |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 2571 | } |
| 2572 | }; |
| 2573 | } |
| 2574 | |
| 2575 | private ActionClickedListener getStorageErrorClickedListener() { |
| 2576 | return new ActionClickedListener() { |
| 2577 | @Override |
| 2578 | public void onActionClicked() { |
Paul Westbrook | 4969e0c | 2012-08-20 14:38:39 -0700 | [diff] [blame] | 2579 | showStorageErrorDialog(); |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 2580 | } |
| 2581 | }; |
| 2582 | } |
| 2583 | |
Paul Westbrook | 4969e0c | 2012-08-20 14:38:39 -0700 | [diff] [blame] | 2584 | 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 Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 2593 | private ActionClickedListener getInternalErrorClickedListener() { |
| 2594 | return new ActionClickedListener() { |
| 2595 | @Override |
| 2596 | public void onActionClicked() { |
Paul Westbrook | 17beb0b | 2012-08-20 13:34:37 -0700 | [diff] [blame] | 2597 | Utils.sendFeedback( |
| 2598 | mActivity.getActivityContext(), mAccount, true /* reportingProblem */); |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 2599 | } |
| 2600 | }; |
| 2601 | } |
Paul Westbrook | 4969e0c | 2012-08-20 14:38:39 -0700 | [diff] [blame] | 2602 | |
| 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 Westbrook | 122f7c2 | 2012-08-20 17:50:31 -0700 | [diff] [blame] | 2613 | promptUserForAuthentication(mAccount); |
Paul Westbrook | 4969e0c | 2012-08-20 14:38:39 -0700 | [diff] [blame] | 2614 | 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 Westbrook | 122f7c2 | 2012-08-20 17:50:31 -0700 | [diff] [blame] | 2647 | |
| 2648 | private void promptUserForAuthentication(Account account) { |
Paul Westbrook | 429399b | 2012-08-24 11:19:17 -0700 | [diff] [blame] | 2649 | if (account != null && !Utils.isEmpty(account.reauthenticationIntentUri)) { |
Paul Westbrook | 122f7c2 | 2012-08-20 17:50:31 -0700 | [diff] [blame] | 2650 | final Intent authenticationIntent = |
| 2651 | new Intent(Intent.ACTION_VIEW, account.reauthenticationIntentUri); |
| 2652 | mActivity.startActivityForResult(authenticationIntent, REAUTHENTICATE_REQUEST_CODE); |
| 2653 | } |
| 2654 | } |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 2655 | } |