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; |
Mindy Pereira | acf6039 | 2012-04-06 09:11:00 -0700 | [diff] [blame] | 46 | import android.view.DragEvent; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 47 | import android.view.KeyEvent; |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 48 | import android.view.LayoutInflater; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 49 | import android.view.Menu; |
Mindy Pereira | 28d5f72 | 2012-02-15 12:32:40 -0800 | [diff] [blame] | 50 | import android.view.MenuInflater; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 51 | import android.view.MenuItem; |
| 52 | import android.view.MotionEvent; |
Mindy Pereira | d3367499 | 2012-06-25 16:26:30 -0700 | [diff] [blame] | 53 | import android.view.View; |
Mindy Pereira | fd0c297 | 2012-03-27 13:50:39 -0700 | [diff] [blame] | 54 | import android.widget.Toast; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 55 | |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 56 | import com.android.mail.ConversationListContext; |
Andy Huang | f9a7348 | 2012-03-13 15:54:02 -0700 | [diff] [blame] | 57 | import com.android.mail.R; |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 58 | import com.android.mail.browse.ConversationCursor; |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 59 | import com.android.mail.browse.ConversationPagerController; |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 60 | import com.android.mail.browse.MessageCursor.ConversationMessage; |
Marc Blank | 7c9f6ac | 2012-04-02 13:27:19 -0700 | [diff] [blame] | 61 | import com.android.mail.browse.SelectedConversationsActionMenu; |
Andy Huang | 991f453 | 2012-08-14 13:32:55 -0700 | [diff] [blame] | 62 | import com.android.mail.browse.SyncErrorDialogFragment; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 63 | import com.android.mail.compose.ComposeActivity; |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 64 | import com.android.mail.providers.Account; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 65 | import com.android.mail.providers.Conversation; |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 66 | import com.android.mail.providers.ConversationInfo; |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 67 | import com.android.mail.providers.Folder; |
Vikram Aggarwal | 7a5d95a | 2012-07-27 16:24:54 -0700 | [diff] [blame] | 68 | import com.android.mail.providers.FolderWatcher; |
Paul Westbrook | c2074c4 | 2012-03-22 15:26:58 -0700 | [diff] [blame] | 69 | import com.android.mail.providers.MailAppProvider; |
Mindy Pereira | dac0054 | 2012-03-01 10:50:33 -0800 | [diff] [blame] | 70 | import com.android.mail.providers.Settings; |
Vikram Aggarwal | e620a7a | 2012-03-28 13:16:14 -0700 | [diff] [blame] | 71 | import com.android.mail.providers.SuggestionsProvider; |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 72 | import com.android.mail.providers.UIProvider; |
Mindy Pereira | 3cb28b5 | 2012-05-24 15:26:39 -0700 | [diff] [blame] | 73 | import com.android.mail.providers.UIProvider.AccountCapabilities; |
Paul Westbrook | 2388c5d | 2012-03-25 12:29:11 -0700 | [diff] [blame] | 74 | import com.android.mail.providers.UIProvider.AccountCursorExtraKeys; |
Mindy Pereira | c9d5918 | 2012-03-22 16:06:46 -0700 | [diff] [blame] | 75 | import com.android.mail.providers.UIProvider.ConversationColumns; |
Vikram Aggarwal | 54452ae | 2012-03-13 15:29:00 -0700 | [diff] [blame] | 76 | import com.android.mail.providers.UIProvider.FolderCapabilities; |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 77 | import com.android.mail.ui.ActionableToastBar.ActionClickedListener; |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 78 | import com.android.mail.utils.ContentProviderTask; |
Paul Westbrook | b334c90 | 2012-06-25 11:42:46 -0700 | [diff] [blame] | 79 | import com.android.mail.utils.LogTag; |
Mindy Pereira | 5cb0c3e | 2012-02-22 15:22:47 -0800 | [diff] [blame] | 80 | import com.android.mail.utils.LogUtils; |
Vikram Aggarwal | fa131a2 | 2012-02-02 13:56:22 -0800 | [diff] [blame] | 81 | import com.android.mail.utils.Utils; |
Paul Westbrook | ca08fc1 | 2012-07-31 12:01:15 -0700 | [diff] [blame] | 82 | import com.google.common.base.Objects; |
Paul Westbrook | 77eee62 | 2012-07-10 13:41:57 -0700 | [diff] [blame] | 83 | import com.google.common.collect.ImmutableList; |
Mindy Pereira | acf6039 | 2012-04-06 09:11:00 -0700 | [diff] [blame] | 84 | import com.google.common.collect.Lists; |
Marc Blank | 167faa8 | 2012-03-21 13:11:53 -0700 | [diff] [blame] | 85 | import com.google.common.collect.Sets; |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 86 | |
Marc Blank | 167faa8 | 2012-03-21 13:11:53 -0700 | [diff] [blame] | 87 | import java.util.ArrayList; |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 88 | import java.util.Collection; |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 89 | import java.util.Collections; |
Mindy Pereira | 8db7e40 | 2012-07-13 10:32:47 -0700 | [diff] [blame] | 90 | import java.util.HashMap; |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 91 | import java.util.Set; |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 92 | import java.util.TimerTask; |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 93 | |
| 94 | |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 95 | /** |
Mindy Pereira | 161f50d | 2012-02-28 15:47:19 -0800 | [diff] [blame] | 96 | * This is an abstract implementation of the Activity Controller. This class |
| 97 | * knows how to respond to menu items, state changes, layout changes, etc. It |
| 98 | * weaves together the views and listeners, dispatching actions to the |
| 99 | * respective underlying classes. |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 100 | * <p> |
Mindy Pereira | 161f50d | 2012-02-28 15:47:19 -0800 | [diff] [blame] | 101 | * Even though this class is abstract, it should provide default implementations |
| 102 | * for most, if not all the methods in the ActivityController interface. This |
| 103 | * makes the task of the subclasses easier: OnePaneActivityController and |
| 104 | * TwoPaneActivityController can be concise when the common functionality is in |
| 105 | * AbstractActivityController. |
| 106 | * </p> |
| 107 | * <p> |
| 108 | * In the Gmail codebase, this was called BaseActivityController |
| 109 | * </p> |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 110 | */ |
Vikram Aggarwal | 531488e | 2012-05-29 16:36:52 -0700 | [diff] [blame] | 111 | public abstract class AbstractActivityController implements ActivityController { |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 112 | // Keys for serialization of various information in Bundles. |
Vikram Aggarwal | cabd3f2 | 2012-04-19 10:14:41 -0700 | [diff] [blame] | 113 | /** Tag for {@link #mAccount} */ |
Vikram Aggarwal | 6c51158 | 2012-02-27 10:59:47 -0800 | [diff] [blame] | 114 | private static final String SAVED_ACCOUNT = "saved-account"; |
Vikram Aggarwal | cabd3f2 | 2012-04-19 10:14:41 -0700 | [diff] [blame] | 115 | /** Tag for {@link #mFolder} */ |
Mindy Pereira | 5e478d2 | 2012-03-26 18:04:58 -0700 | [diff] [blame] | 116 | private static final String SAVED_FOLDER = "saved-folder"; |
Vikram Aggarwal | cabd3f2 | 2012-04-19 10:14:41 -0700 | [diff] [blame] | 117 | /** Tag for {@link #mCurrentConversation} */ |
Mindy Pereira | 26f23fc | 2012-03-27 10:26:04 -0700 | [diff] [blame] | 118 | private static final String SAVED_CONVERSATION = "saved-conversation"; |
Vikram Aggarwal | cabd3f2 | 2012-04-19 10:14:41 -0700 | [diff] [blame] | 119 | /** Tag for {@link #mSelectedSet} */ |
| 120 | private static final String SAVED_SELECTED_SET = "saved-selected-set"; |
Mindy Pereira | d3367499 | 2012-06-25 16:26:30 -0700 | [diff] [blame] | 121 | private static final String SAVED_TOAST_BAR_OP = "saved-toast-bar-op"; |
Mindy Pereira | 148b191 | 2012-07-17 13:39:56 -0700 | [diff] [blame] | 122 | protected static final String SAVED_HIERARCHICAL_FOLDER = "saved-hierarchical-folder"; |
Vikram Aggarwal | f334140 | 2012-08-07 10:09:38 -0700 | [diff] [blame] | 123 | /** Tag for {@link ConversationListContext#searchQuery} */ |
| 124 | private static final String SAVED_QUERY = "saved-query"; |
Mindy Pereira | 5cb0c3e | 2012-02-22 15:22:47 -0800 | [diff] [blame] | 125 | |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 126 | /** Tag used when loading a wait fragment */ |
| 127 | protected static final String TAG_WAIT = "wait-fragment"; |
| 128 | /** Tag used when loading a conversation list fragment. */ |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 129 | public static final String TAG_CONVERSATION_LIST = "tag-conversation-list"; |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 130 | /** Tag used when loading a folder list fragment. */ |
| 131 | protected static final String TAG_FOLDER_LIST = "tag-folder-list"; |
| 132 | |
Vikram Aggarwal | d7a12cd | 2012-02-03 09:36:20 -0800 | [diff] [blame] | 133 | protected Account mAccount; |
Mindy Pereira | 12a4d80 | 2012-06-22 09:24:43 -0700 | [diff] [blame] | 134 | protected Folder mFolder; |
Andy Huang | 6681e54 | 2012-06-14 14:36:45 -0700 | [diff] [blame] | 135 | protected MailActionBarView mActionBarView; |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 136 | protected final ControllableActivity mActivity; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 137 | protected final Context mContext; |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 138 | private final FragmentManager mFragmentManager; |
Vikram Aggarwal | ec5cbf7 | 2012-03-08 15:10:35 -0800 | [diff] [blame] | 139 | protected final RecentFolderList mRecentFolderList; |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 140 | protected ConversationListContext mConvListContext; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 141 | protected Conversation mCurrentConversation; |
Vikram Aggarwal | d7a12cd | 2012-02-03 09:36:20 -0800 | [diff] [blame] | 142 | |
Paul Westbrook | 6ead20d | 2012-03-19 14:48:14 -0700 | [diff] [blame] | 143 | /** A {@link android.content.BroadcastReceiver} that suppresses new e-mail notifications. */ |
| 144 | private SuppressNotificationReceiver mNewEmailReceiver = null; |
| 145 | |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 146 | protected Handler mHandler = new Handler(); |
Mindy Pereira | fa995b4 | 2012-07-25 12:06:13 -0700 | [diff] [blame] | 147 | |
Vikram Aggarwal | fa131a2 | 2012-02-02 13:56:22 -0800 | [diff] [blame] | 148 | /** |
Mindy Pereira | 161f50d | 2012-02-28 15:47:19 -0800 | [diff] [blame] | 149 | * The current mode of the application. All changes in mode are initiated by |
| 150 | * the activity controller. View mode changes are propagated to classes that |
| 151 | * attach themselves as listeners of view mode changes. |
Vikram Aggarwal | fa131a2 | 2012-02-02 13:56:22 -0800 | [diff] [blame] | 152 | */ |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 153 | protected final ViewMode mViewMode; |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 154 | protected ContentResolver mResolver; |
Vikram Aggarwal | 7dedb95 | 2012-02-16 16:10:23 -0800 | [diff] [blame] | 155 | protected boolean isLoaderInitialized = false; |
Mindy Pereira | b7b33e0 | 2012-02-21 15:32:19 -0800 | [diff] [blame] | 156 | private AsyncRefreshTask mAsyncRefreshTask; |
Mindy Pereira | 5cb0c3e | 2012-02-22 15:22:47 -0800 | [diff] [blame] | 157 | |
Andy Huang | 4e0158f | 2012-08-07 21:06:01 -0700 | [diff] [blame] | 158 | private boolean mDestroyed; |
| 159 | |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 160 | private final Set<Uri> mCurrentAccountUris = Sets.newHashSet(); |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 161 | protected ConversationCursor mConversationListCursor; |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 162 | private final DataSetObservable mConversationListObservable = new DataSetObservable() { |
| 163 | @Override |
| 164 | public void registerObserver(DataSetObserver observer) { |
| 165 | final int count = mObservers.size(); |
| 166 | super.registerObserver(observer); |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 167 | 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] | 168 | count, mObservers.size()); |
| 169 | } |
| 170 | @Override |
| 171 | public void unregisterObserver(DataSetObserver observer) { |
| 172 | final int count = mObservers.size(); |
| 173 | super.unregisterObserver(observer); |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 174 | 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] | 175 | count, mObservers.size()); |
| 176 | } |
| 177 | }; |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 178 | |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 179 | private RefreshTimerTask mConversationListRefreshTask; |
Marc Blank | e1d1b07 | 2012-04-13 17:29:16 -0700 | [diff] [blame] | 180 | |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 181 | /** Listeners that are interested in changes to the current account. */ |
| 182 | private final DataSetObservable mAccountObservers = new DataSetObservable() { |
| 183 | @Override |
| 184 | public void registerObserver(DataSetObserver observer) { |
| 185 | final int count = mObservers.size(); |
| 186 | super.registerObserver(observer); |
| 187 | LogUtils.d(LOG_TAG, "IN AAC.register(Account)Observer: %s before=%d after=%d", |
| 188 | observer, count, mObservers.size()); |
| 189 | } |
| 190 | @Override |
| 191 | public void unregisterObserver(DataSetObserver observer) { |
| 192 | final int count = mObservers.size(); |
| 193 | super.unregisterObserver(observer); |
| 194 | LogUtils.d(LOG_TAG, "IN AAC.unregister(Account)Observer: %s before=%d after=%d", |
| 195 | observer, count, mObservers.size()); |
| 196 | } |
| 197 | }; |
Vikram Aggarwal | 7d81600 | 2012-04-17 17:06:41 -0700 | [diff] [blame] | 198 | |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 199 | /** |
| 200 | * Selected conversations, if any. |
| 201 | */ |
Andy Huang | 4556a44 | 2012-03-30 16:42:05 -0700 | [diff] [blame] | 202 | private final ConversationSelectionSet mSelectedSet = new ConversationSelectionSet(); |
Mindy Pereira | dac0054 | 2012-03-01 10:50:33 -0800 | [diff] [blame] | 203 | |
Paul Westbrook | c7a070f | 2012-04-12 01:46:41 -0700 | [diff] [blame] | 204 | private final int mFolderItemUpdateDelayMs; |
| 205 | |
Vikram Aggarwal | 135fd02 | 2012-04-11 14:44:15 -0700 | [diff] [blame] | 206 | /** Keeps track of selected and unselected conversations */ |
Paul Westbrook | 937c94f | 2012-08-16 13:01:18 -0700 | [diff] [blame] | 207 | final protected ConversationPositionTracker mTracker; |
Vikram Aggarwal | af1ee0c | 2012-04-12 17:13:13 -0700 | [diff] [blame] | 208 | |
Vikram Aggarwal | e128fc2 | 2012-04-04 12:33:34 -0700 | [diff] [blame] | 209 | /** |
| 210 | * Action menu associated with the selected set. |
| 211 | */ |
| 212 | SelectedConversationsActionMenu mCabActionMenu; |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 213 | protected ActionableToastBar mToastBar; |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 214 | protected ConversationPagerController mPagerController; |
Mindy Pereira | ab48636 | 2012-03-21 18:18:53 -0700 | [diff] [blame] | 215 | |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 216 | // this is split out from the general loader dispatcher because its loader doesn't return a |
| 217 | // basic Cursor |
| 218 | private final ConversationListLoaderCallbacks mListCursorCallbacks = |
| 219 | new ConversationListLoaderCallbacks(); |
| 220 | |
Andy Huang | 090db1e | 2012-07-25 13:25:28 -0700 | [diff] [blame] | 221 | private final DataSetObservable mFolderObservable = new DataSetObservable(); |
| 222 | |
Paul Westbrook | b334c90 | 2012-06-25 11:42:46 -0700 | [diff] [blame] | 223 | protected static final String LOG_TAG = LogTag.getLogTag(); |
Vikram Aggarwal | fa4b47e | 2012-03-09 13:02:46 -0800 | [diff] [blame] | 224 | /** Constants used to differentiate between the types of loaders. */ |
| 225 | private static final int LOADER_ACCOUNT_CURSOR = 0; |
Vikram Aggarwal | fa4b47e | 2012-03-09 13:02:46 -0800 | [diff] [blame] | 226 | private static final int LOADER_FOLDER_CURSOR = 2; |
| 227 | private static final int LOADER_RECENT_FOLDERS = 3; |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 228 | private static final int LOADER_CONVERSATION_LIST = 4; |
Mindy Pereira | ab48636 | 2012-03-21 18:18:53 -0700 | [diff] [blame] | 229 | private static final int LOADER_ACCOUNT_INBOX = 5; |
| 230 | private static final int LOADER_SEARCH = 6; |
Paul Westbrook | 2d50bcd | 2012-04-10 11:53:47 -0700 | [diff] [blame] | 231 | private static final int LOADER_ACCOUNT_UPDATE_CURSOR = 7; |
Vikram Aggarwal | 7a5d95a | 2012-07-27 16:24:54 -0700 | [diff] [blame] | 232 | /** |
| 233 | * Guaranteed to be the last loader ID used by the activity. Loaders are owned by Activity or |
| 234 | * fragments, and within an activity, loader IDs need to be unique. A hack to ensure that the |
| 235 | * {@link FolderWatcher} can create its folder loaders without clashing with the IDs of those |
| 236 | * of the {@link AbstractActivityController}. Currently, the {@link FolderWatcher} is the only |
| 237 | * other class that uses this activity's LoaderManager. If another class needs activity-level |
| 238 | * loaders, consider consolidating the loaders in a central location: a UI-less fragment |
| 239 | * perhaps. |
| 240 | */ |
| 241 | public static final int LAST_LOADER_ID = 100; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 242 | |
Paul Westbrook | 2388c5d | 2012-03-25 12:29:11 -0700 | [diff] [blame] | 243 | private static final int ADD_ACCOUNT_REQUEST_CODE = 1; |
Paul Westbrook | 122f7c2 | 2012-08-20 17:50:31 -0700 | [diff] [blame] | 244 | private static final int REAUTHENTICATE_REQUEST_CODE = 2; |
Paul Westbrook | 2388c5d | 2012-03-25 12:29:11 -0700 | [diff] [blame] | 245 | |
Vikram Aggarwal | e8a8532 | 2012-04-24 09:01:38 -0700 | [diff] [blame] | 246 | /** The pending destructive action to be carried out before swapping the conversation cursor.*/ |
| 247 | private DestructiveAction mPendingDestruction; |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 248 | protected AsyncRefreshTask mFolderSyncTask; |
Mindy Pereira | 49e5dbe | 2012-07-12 11:47:54 -0700 | [diff] [blame] | 249 | // Task for setting any share intents for the account to enabled. |
| 250 | // This gets cancelled if the user kills the app before it finishes, and |
| 251 | // will just run the next time the user opens the app. |
| 252 | private AsyncTask<String, Void, Void> mEnableShareIntents; |
Mindy Pereira | c975e84 | 2012-07-16 09:15:00 -0700 | [diff] [blame] | 253 | private Folder mFolderListFolder; |
mindyp | 5390fca | 2012-08-22 12:12:25 -0700 | [diff] [blame] | 254 | private boolean mIsDragHappening; |
mindyp | ead5039 | 2012-08-23 11:03:53 -0700 | [diff] [blame] | 255 | private int mShowUndoBarDelay; |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 256 | public static final String SYNC_ERROR_DIALOG_FRAGMENT_TAG = "SyncErrorDialogFragment"; |
Vikram Aggarwal | e8a8532 | 2012-04-24 09:01:38 -0700 | [diff] [blame] | 257 | |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 258 | public AbstractActivityController(MailActivity activity, ViewMode viewMode) { |
| 259 | mActivity = activity; |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 260 | mFragmentManager = mActivity.getFragmentManager(); |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 261 | mViewMode = viewMode; |
| 262 | mContext = activity.getApplicationContext(); |
Vikram Aggarwal | 025eba8 | 2012-05-08 10:45:30 -0700 | [diff] [blame] | 263 | mRecentFolderList = new RecentFolderList(mContext); |
Paul Westbrook | 937c94f | 2012-08-16 13:01:18 -0700 | [diff] [blame] | 264 | mTracker = new ConversationPositionTracker(this); |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 265 | // Allow the fragment to observe changes to its own selection set. No other object is |
| 266 | // aware of the selected set. |
| 267 | mSelectedSet.addObserver(this); |
Paul Westbrook | c7a070f | 2012-04-12 01:46:41 -0700 | [diff] [blame] | 268 | |
| 269 | mFolderItemUpdateDelayMs = |
| 270 | mContext.getResources().getInteger(R.integer.folder_item_refresh_delay_ms); |
mindyp | ead5039 | 2012-08-23 11:03:53 -0700 | [diff] [blame] | 271 | mShowUndoBarDelay = |
| 272 | mContext.getResources().getInteger(R.integer.show_undo_bar_delay_ms); |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 273 | } |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 274 | |
| 275 | @Override |
Vikram Aggarwal | e9a8103 | 2012-02-22 13:15:35 -0800 | [diff] [blame] | 276 | public Account getCurrentAccount() { |
| 277 | return mAccount; |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | @Override |
| 281 | public ConversationListContext getCurrentListContext() { |
Vikram Aggarwal | e9a8103 | 2012-02-22 13:15:35 -0800 | [diff] [blame] | 282 | return mConvListContext; |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | @Override |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 286 | public String getHelpContext() { |
Paul Westbrook | 30745b6 | 2012-08-19 14:10:32 -0700 | [diff] [blame] | 287 | final int mode = mViewMode.getMode(); |
| 288 | final int helpContextResId; |
| 289 | switch (mode) { |
| 290 | case ViewMode.WAITING_FOR_ACCOUNT_INITIALIZATION: |
| 291 | helpContextResId = R.string.wait_help_context; |
| 292 | break; |
| 293 | default: |
| 294 | helpContextResId = R.string.main_help_context; |
| 295 | } |
| 296 | return mContext.getString(helpContextResId); |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 297 | } |
| 298 | |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 299 | @Override |
Vikram Aggarwal | ef7c992 | 2012-04-23 12:35:04 -0700 | [diff] [blame] | 300 | public final ConversationCursor getConversationListCursor() { |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 301 | return mConversationListCursor; |
| 302 | } |
| 303 | |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 304 | /** |
Vikram Aggarwal | 34a6501 | 2012-04-17 12:39:06 -0700 | [diff] [blame] | 305 | * Check if the fragment is attached to an activity and has a root view. |
| 306 | * @param in |
| 307 | * @return true if the fragment is valid, false otherwise |
| 308 | */ |
| 309 | private static final boolean isValidFragment(Fragment in) { |
| 310 | if (in == null || in.getActivity() == null || in.getView() == null) { |
| 311 | return false; |
| 312 | } |
| 313 | return true; |
| 314 | } |
| 315 | |
| 316 | /** |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 317 | * Get the conversation list fragment for this activity. If the conversation list fragment |
| 318 | * is not attached, this method returns null |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 319 | * |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 320 | */ |
| 321 | protected ConversationListFragment getConversationListFragment() { |
| 322 | final Fragment fragment = mFragmentManager.findFragmentByTag(TAG_CONVERSATION_LIST); |
Vikram Aggarwal | 34a6501 | 2012-04-17 12:39:06 -0700 | [diff] [blame] | 323 | if (isValidFragment(fragment)) { |
| 324 | return (ConversationListFragment) fragment; |
Andy Huang | 9585d78 | 2012-04-16 19:45:04 -0700 | [diff] [blame] | 325 | } |
Vikram Aggarwal | 34a6501 | 2012-04-17 12:39:06 -0700 | [diff] [blame] | 326 | return null; |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | /** |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 330 | * Returns the folder list fragment attached with this activity. If no such fragment is attached |
| 331 | * 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 FolderListFragment getFolderListFragment() { |
| 335 | final Fragment fragment = mFragmentManager.findFragmentByTag(TAG_FOLDER_LIST); |
Vikram Aggarwal | 34a6501 | 2012-04-17 12:39:06 -0700 | [diff] [blame] | 336 | if (isValidFragment(fragment)) { |
| 337 | return (FolderListFragment) 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 | |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 342 | /** |
Mindy Pereira | 161f50d | 2012-02-28 15:47:19 -0800 | [diff] [blame] | 343 | * Initialize the action bar. This is not visible to OnePaneController and |
| 344 | * TwoPaneController so they cannot override this behavior. |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 345 | */ |
Vikram Aggarwal | abd24d8 | 2012-04-26 13:23:14 -0700 | [diff] [blame] | 346 | private void initializeActionBar() { |
| 347 | final ActionBar actionBar = mActivity.getActionBar(); |
Andy Huang | 5895f7b | 2012-06-01 17:07:20 -0700 | [diff] [blame] | 348 | if (actionBar == null) { |
| 349 | return; |
Vikram Aggarwal | abd24d8 | 2012-04-26 13:23:14 -0700 | [diff] [blame] | 350 | } |
Andy Huang | 5895f7b | 2012-06-01 17:07:20 -0700 | [diff] [blame] | 351 | |
| 352 | // be sure to inherit from the ActionBar theme when inflating |
| 353 | final LayoutInflater inflater = LayoutInflater.from(actionBar.getThemedContext()); |
Mindy Pereira | 82faec7 | 2012-06-14 17:21:50 -0700 | [diff] [blame] | 354 | final boolean isSearch = mActivity.getIntent() != null |
| 355 | && Intent.ACTION_SEARCH.equals(mActivity.getIntent().getAction()); |
| 356 | mActionBarView = (MailActionBarView) inflater.inflate( |
| 357 | isSearch ? R.layout.search_actionbar_view : R.layout.actionbar_view, null); |
Andy Huang | 5895f7b | 2012-06-01 17:07:20 -0700 | [diff] [blame] | 358 | mActionBarView.initialize(mActivity, this, mViewMode, actionBar, mRecentFolderList); |
Vikram Aggarwal | abd24d8 | 2012-04-26 13:23:14 -0700 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | /** |
| 362 | * Attach the action bar to the activity. |
| 363 | */ |
| 364 | private void attachActionBar() { |
| 365 | final ActionBar actionBar = mActivity.getActionBar(); |
| 366 | if (actionBar != null && mActionBarView != null) { |
Vikram Aggarwal | ec5cbf7 | 2012-03-08 15:10:35 -0800 | [diff] [blame] | 367 | actionBar.setCustomView(mActionBarView, new ActionBar.LayoutParams( |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 368 | LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); |
| 369 | actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM, |
Vikram Aggarwal | 2a25d0c | 2012-02-21 16:43:10 -0800 | [diff] [blame] | 370 | ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_TITLE); |
Vikram Aggarwal | abd24d8 | 2012-04-26 13:23:14 -0700 | [diff] [blame] | 371 | mActionBarView.attach(); |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 372 | } |
Vikram Aggarwal | abd24d8 | 2012-04-26 13:23:14 -0700 | [diff] [blame] | 373 | mViewMode.addListener(mActionBarView); |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | /** |
Mindy Pereira | 161f50d | 2012-02-28 15:47:19 -0800 | [diff] [blame] | 377 | * Returns whether the conversation list fragment is visible or not. |
| 378 | * Different layouts will have their own notion on the visibility of |
| 379 | * fragments, so this method needs to be overriden. |
| 380 | * |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 381 | */ |
| 382 | protected abstract boolean isConversationListVisible(); |
| 383 | |
Vikram Aggarwal | eb7d329 | 2012-04-20 17:07:20 -0700 | [diff] [blame] | 384 | /** |
| 385 | * 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] | 386 | * @param account new account |
| 387 | * @param shouldReloadInbox whether the default inbox should be reloaded. |
Vikram Aggarwal | eb7d329 | 2012-04-20 17:07:20 -0700 | [diff] [blame] | 388 | */ |
Vikram Aggarwal | 66bc6ed | 2012-07-31 09:59:55 -0700 | [diff] [blame] | 389 | private void switchAccount(Account account, boolean shouldReloadInbox){ |
Vikram Aggarwal | eb7d329 | 2012-04-20 17:07:20 -0700 | [diff] [blame] | 390 | // Current account is different from the new account, restart loaders and show |
| 391 | // the account Inbox. |
| 392 | mAccount = account; |
Vikram Aggarwal | edc137c | 2012-04-24 13:40:58 -0700 | [diff] [blame] | 393 | LogUtils.d(LOG_TAG, "AbstractActivityController.switchAccount(): mAccount = %s", |
| 394 | mAccount.uri); |
Vikram Aggarwal | eb7d329 | 2012-04-20 17:07:20 -0700 | [diff] [blame] | 395 | cancelRefreshTask(); |
Vikram Aggarwal | 2e0a236 | 2012-08-22 09:05:49 -0700 | [diff] [blame] | 396 | // If we are currently listening to changes in conversation, the account switch is going to |
| 397 | // make us do a lot of un-necessary work. Let's stop listening to changes. |
| 398 | if (mPagerController != null) { |
| 399 | mPagerController.stopListening(); |
| 400 | } |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 401 | updateSettings(); |
Vikram Aggarwal | 66bc6ed | 2012-07-31 09:59:55 -0700 | [diff] [blame] | 402 | if (shouldReloadInbox) { |
| 403 | loadAccountInbox(); |
| 404 | } |
Vikram Aggarwal | eb7d329 | 2012-04-20 17:07:20 -0700 | [diff] [blame] | 405 | restartOptionalLoader(LOADER_RECENT_FOLDERS); |
| 406 | mActivity.invalidateOptionsMenu(); |
| 407 | disableNotificationsOnAccountChange(mAccount); |
| 408 | restartOptionalLoader(LOADER_ACCOUNT_UPDATE_CURSOR); |
| 409 | MailAppProvider.getInstance().setLastViewedAccount(mAccount.uri.toString()); |
| 410 | } |
| 411 | |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 412 | @Override |
Mindy Pereira | 28e0c34 | 2012-02-17 15:05:13 -0800 | [diff] [blame] | 413 | public void onAccountChanged(Account account) { |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 414 | LogUtils.d(LOG_TAG, "onAccountChanged (%s) called.", account); |
| 415 | // Is the account or account settings different from the existing account? |
Vikram Aggarwal | 66bc6ed | 2012-07-31 09:59:55 -0700 | [diff] [blame] | 416 | final boolean firstLoad = mAccount == null; |
| 417 | final boolean accountChanged = firstLoad || !account.uri.equals(mAccount.uri); |
| 418 | final boolean settingsChanged = firstLoad || !account.settings.equals(mAccount.settings); |
| 419 | if (accountChanged || settingsChanged) { |
Mindy Pereira | fa20c1a | 2012-07-23 13:00:02 -0700 | [diff] [blame] | 420 | if (account != null) { |
Mindy Pereira | fa995b4 | 2012-07-25 12:06:13 -0700 | [diff] [blame] | 421 | final String accountName = account.name; |
| 422 | mHandler.post(new Runnable() { |
| 423 | @Override |
| 424 | public void run() { |
| 425 | MailActivity.setForegroundNdef(MailActivity.getMailtoNdef(accountName)); |
| 426 | } |
| 427 | }); |
Mindy Pereira | fa20c1a | 2012-07-23 13:00:02 -0700 | [diff] [blame] | 428 | } |
mindyp | c6adce3 | 2012-08-22 18:46:42 -0700 | [diff] [blame] | 429 | if (accountChanged) { |
| 430 | commitDestructiveActions(false); |
| 431 | } |
Vikram Aggarwal | 66bc6ed | 2012-07-31 09:59:55 -0700 | [diff] [blame] | 432 | switchAccount(account, accountChanged); |
Mindy Pereira | 28d5f72 | 2012-02-15 12:32:40 -0800 | [diff] [blame] | 433 | } |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 434 | } |
| 435 | |
Vikram Aggarwal | e6340bc | 2012-03-26 15:57:09 -0700 | [diff] [blame] | 436 | /** |
Vikram Aggarwal | eb7d329 | 2012-04-20 17:07:20 -0700 | [diff] [blame] | 437 | * Changes the settings for the current account. The new settings are provided as a parameter. |
| 438 | * @param settings |
| 439 | */ |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 440 | public void updateSettings() { |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 441 | mAccountObservers.notifyChanged(); |
Mindy Pereira | 12a676a | 2012-03-23 13:00:22 -0700 | [diff] [blame] | 442 | resetActionBarIcon(); |
Vikram Aggarwal | eb7d329 | 2012-04-20 17:07:20 -0700 | [diff] [blame] | 443 | mActivity.invalidateOptionsMenu(); |
| 444 | // 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] | 445 | // default Inbox, we don't want to load a different folder here. So do not change the |
| 446 | // current folder. |
Mindy Pereira | dac0054 | 2012-03-01 10:50:33 -0800 | [diff] [blame] | 447 | } |
| 448 | |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 449 | /** |
| 450 | * Adds a listener interested in change in the current account. If a class is storing a |
| 451 | * reference to the current account, it should listen on changes, so it can receive updates to |
| 452 | * settings. Must happen in the UI thread. |
| 453 | */ |
Mindy Pereira | efe3d25 | 2012-03-01 14:20:44 -0800 | [diff] [blame] | 454 | @Override |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 455 | public void registerAccountObserver(DataSetObserver obs) { |
| 456 | mAccountObservers.registerObserver(obs); |
Mindy Pereira | efe3d25 | 2012-03-01 14:20:44 -0800 | [diff] [blame] | 457 | } |
| 458 | |
Vikram Aggarwal | 7d81600 | 2012-04-17 17:06:41 -0700 | [diff] [blame] | 459 | /** |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 460 | * Removes a listener from receiving current account changes. |
Vikram Aggarwal | 7d81600 | 2012-04-17 17:06:41 -0700 | [diff] [blame] | 461 | * Must happen in the UI thread. |
| 462 | */ |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 463 | @Override |
| 464 | public void unregisterAccountObserver(DataSetObserver obs) { |
| 465 | mAccountObservers.unregisterObserver(obs); |
Vikram Aggarwal | 7d81600 | 2012-04-17 17:06:41 -0700 | [diff] [blame] | 466 | } |
| 467 | |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 468 | @Override |
| 469 | public Account getAccount() { |
| 470 | return mAccount; |
Vikram Aggarwal | 7d81600 | 2012-04-17 17:06:41 -0700 | [diff] [blame] | 471 | } |
| 472 | |
Mindy Pereira | e082839 | 2012-03-08 10:38:40 -0800 | [diff] [blame] | 473 | private void fetchSearchFolder(Intent intent) { |
Mindy Pereira | ab48636 | 2012-03-21 18:18:53 -0700 | [diff] [blame] | 474 | Bundle args = new Bundle(); |
| 475 | args.putString(ConversationListContext.EXTRA_SEARCH_QUERY, intent |
Mindy Pereira | e082839 | 2012-03-08 10:38:40 -0800 | [diff] [blame] | 476 | .getStringExtra(ConversationListContext.EXTRA_SEARCH_QUERY)); |
Mindy Pereira | ab48636 | 2012-03-21 18:18:53 -0700 | [diff] [blame] | 477 | mActivity.getLoaderManager().restartLoader(LOADER_SEARCH, args, this); |
Mindy Pereira | e082839 | 2012-03-08 10:38:40 -0800 | [diff] [blame] | 478 | } |
| 479 | |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 480 | @Override |
Mindy Pereira | 28e0c34 | 2012-02-17 15:05:13 -0800 | [diff] [blame] | 481 | public void onFolderChanged(Folder folder) { |
Vikram Aggarwal | f334140 | 2012-08-07 10:09:38 -0700 | [diff] [blame] | 482 | changeFolder(folder, null); |
| 483 | } |
| 484 | |
| 485 | /** |
Vikram Aggarwal | 203dc00 | 2012-08-23 13:59:04 -0700 | [diff] [blame^] | 486 | * Sets the folder state without changing view mode and without creating a list fragment, if |
| 487 | * possible. |
| 488 | * @param folder |
| 489 | */ |
| 490 | private void setListContext(Folder folder, String query) { |
| 491 | updateFolder(folder); |
| 492 | if (query != null) { |
| 493 | mConvListContext = ConversationListContext.forSearchQuery(mAccount, mFolder, query); |
| 494 | } else { |
| 495 | mConvListContext = ConversationListContext.forFolder(mAccount, mFolder); |
| 496 | } |
| 497 | // Add the folder that we were viewing to the recent folders list. |
| 498 | // TODO: this may need to be fine tuned. If this is the signal that is indicating that |
| 499 | // the list is shown to the user, this could fire in one pane if the user goes directly |
| 500 | // to a conversation |
| 501 | updateRecentFolderList(); |
| 502 | cancelRefreshTask(); |
| 503 | } |
| 504 | |
| 505 | /** |
Vikram Aggarwal | f334140 | 2012-08-07 10:09:38 -0700 | [diff] [blame] | 506 | * Changes the folder to the value provided here. This causes the view mode to change. |
| 507 | * @param folder the folder to change to |
| 508 | * @param query if non-null, this represents the search string that the folder represents. |
| 509 | */ |
| 510 | private void changeFolder(Folder folder, String query) { |
Vikram Aggarwal | 203dc00 | 2012-08-23 13:59:04 -0700 | [diff] [blame^] | 511 | if (!Objects.equal(mFolder, folder)) { |
| 512 | commitDestructiveActions(false); |
| 513 | } |
Mindy Pereira | a1f9998 | 2012-07-16 16:32:15 -0700 | [diff] [blame] | 514 | if (folder != null && !folder.equals(mFolder) |
| 515 | || (mViewMode.getMode() != ViewMode.CONVERSATION_LIST)) { |
Vikram Aggarwal | 203dc00 | 2012-08-23 13:59:04 -0700 | [diff] [blame^] | 516 | setListContext(folder, query); |
Mindy Pereira | 28e0c34 | 2012-02-17 15:05:13 -0800 | [diff] [blame] | 517 | showConversationList(mConvListContext); |
Mindy Pereira | 28e0c34 | 2012-02-17 15:05:13 -0800 | [diff] [blame] | 518 | } |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 519 | } |
| 520 | |
Vikram Aggarwal | 203dc00 | 2012-08-23 13:59:04 -0700 | [diff] [blame^] | 521 | /** |
| 522 | * Update the conversation list to {@link #mConvListContext} without creating a new frament if |
| 523 | * possible. |
| 524 | */ |
| 525 | protected abstract void updateConversationList(); |
| 526 | |
| 527 | private void setFirstFolder(Folder folder, String query) { |
| 528 | setListContext(folder, query); |
| 529 | updateConversationList(); |
| 530 | } |
| 531 | |
Mindy 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); |
| 683 | setFirstFolder(folder, query); |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 684 | } |
| 685 | } else if (intent != null) { |
| 686 | handleIntent(intent); |
| 687 | } |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 688 | // Create the accounts loader; this loads the account switch spinner. |
| 689 | mActivity.getLoaderManager().initLoader(LOADER_ACCOUNT_CURSOR, null, this); |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 690 | return true; |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | @Override |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 694 | public void onRestart() { |
| 695 | DialogFragment fragment = (DialogFragment) |
| 696 | mFragmentManager.findFragmentByTag(SYNC_ERROR_DIALOG_FRAGMENT_TAG); |
| 697 | if (fragment != null) { |
| 698 | fragment.dismiss(); |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | @Override |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 703 | public Dialog onCreateDialog(int id, Bundle bundle) { |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 704 | return null; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | @Override |
Vikram Aggarwal | 4eb5271 | 2012-06-19 16:24:50 -0700 | [diff] [blame] | 708 | public final boolean onCreateOptionsMenu(Menu menu) { |
Mindy Pereira | 28d5f72 | 2012-02-15 12:32:40 -0800 | [diff] [blame] | 709 | MenuInflater inflater = mActivity.getMenuInflater(); |
Mindy Pereira | f5acda4 | 2012-02-15 20:13:59 -0800 | [diff] [blame] | 710 | inflater.inflate(mActionBarView.getOptionsMenuId(), menu); |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 711 | mActionBarView.onCreateOptionsMenu(menu); |
Mindy Pereira | 28d5f72 | 2012-02-15 12:32:40 -0800 | [diff] [blame] | 712 | return true; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | @Override |
Vikram Aggarwal | 4eb5271 | 2012-06-19 16:24:50 -0700 | [diff] [blame] | 716 | public final boolean onKeyDown(int keyCode, KeyEvent event) { |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 717 | // TODO(viki): Auto-generated method stub |
| 718 | return false; |
| 719 | } |
| 720 | |
| 721 | @Override |
Vikram Aggarwal | 4eb5271 | 2012-06-19 16:24:50 -0700 | [diff] [blame] | 722 | public final boolean onOptionsItemSelected(MenuItem item) { |
Vikram Aggarwal | 54452ae | 2012-03-13 15:29:00 -0700 | [diff] [blame] | 723 | final int id = item.getItemId(); |
Vikram Aggarwal | 7dd054e | 2012-05-21 14:43:10 -0700 | [diff] [blame] | 724 | LogUtils.d(LOG_TAG, "AbstractController.onOptionsItemSelected(%d) called.", id); |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 725 | boolean handled = true; |
Mindy Pereira | ba68fda | 2012-05-24 15:53:06 -0700 | [diff] [blame] | 726 | final Collection<Conversation> target = Conversation.listOf(mCurrentConversation); |
Vikram Aggarwal | 112cd16 | 2012-06-18 10:51:13 -0700 | [diff] [blame] | 727 | final Settings settings = (mAccount == null) ? null : mAccount.settings; |
Mindy Pereira | 8937bf1 | 2012-07-23 14:05:02 -0700 | [diff] [blame] | 728 | // 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] | 729 | commitDestructiveActions(true); |
Mindy Pereira | 28d5f72 | 2012-02-15 12:32:40 -0800 | [diff] [blame] | 730 | switch (id) { |
Mindy Pereira | ba68fda | 2012-05-24 15:53:06 -0700 | [diff] [blame] | 731 | case R.id.archive: { |
| 732 | final boolean showDialog = (settings != null && settings.confirmArchive); |
| 733 | confirmAndDelete(target, showDialog, R.plurals.confirm_archive_conversation, |
| 734 | getAction(R.id.archive, target)); |
| 735 | break; |
| 736 | } |
Mindy Pereira | 01f3050 | 2012-08-14 10:30:51 -0700 | [diff] [blame] | 737 | case R.id.remove_folder: |
| 738 | delete(target, getRemoveFolder(target, mFolder, true, false, true)); |
| 739 | break; |
Mindy Pereira | ba68fda | 2012-05-24 15:53:06 -0700 | [diff] [blame] | 740 | case R.id.delete: { |
| 741 | final boolean showDialog = (settings != null && settings.confirmDelete); |
| 742 | confirmAndDelete(target, showDialog, R.plurals.confirm_delete_conversation, |
| 743 | getAction(R.id.delete, target)); |
| 744 | break; |
| 745 | } |
| 746 | case R.id.mark_important: |
Vikram Aggarwal | 531488e | 2012-05-29 16:36:52 -0700 | [diff] [blame] | 747 | updateConversation(Conversation.listOf(mCurrentConversation), |
| 748 | ConversationColumns.PRIORITY, UIProvider.ConversationPriority.HIGH); |
Mindy Pereira | ba68fda | 2012-05-24 15:53:06 -0700 | [diff] [blame] | 749 | break; |
| 750 | case R.id.mark_not_important: |
Mindy Pereira | 0d03ef8 | 2012-08-15 09:05:48 -0700 | [diff] [blame] | 751 | if (mFolder != null && mFolder.isImportantOnly()) { |
| 752 | delete(target, getAction(R.id.mark_not_important, target)); |
| 753 | } else { |
| 754 | updateConversation(Conversation.listOf(mCurrentConversation), |
| 755 | ConversationColumns.PRIORITY, UIProvider.ConversationPriority.LOW); |
| 756 | } |
Mindy Pereira | ba68fda | 2012-05-24 15:53:06 -0700 | [diff] [blame] | 757 | break; |
| 758 | case R.id.mute: |
Vikram Aggarwal | 4f4782b | 2012-05-30 08:39:09 -0700 | [diff] [blame] | 759 | delete(target, getAction(R.id.mute, target)); |
Mindy Pereira | ba68fda | 2012-05-24 15:53:06 -0700 | [diff] [blame] | 760 | break; |
| 761 | case R.id.report_spam: |
Vikram Aggarwal | 4f4782b | 2012-05-30 08:39:09 -0700 | [diff] [blame] | 762 | delete(target, getAction(R.id.report_spam, target)); |
Mindy Pereira | ba68fda | 2012-05-24 15:53:06 -0700 | [diff] [blame] | 763 | break; |
Paul Westbrook | 77eee62 | 2012-07-10 13:41:57 -0700 | [diff] [blame] | 764 | case R.id.mark_not_spam: |
| 765 | // Currently, since spam messages are only shown in list with other spam messages, |
| 766 | // marking a message not as spam is a destructive action |
| 767 | delete(target, getAction(R.id.mark_not_spam, target)); |
| 768 | break; |
Paul Westbrook | 76b2062 | 2012-07-12 11:45:43 -0700 | [diff] [blame] | 769 | case R.id.report_phishing: |
| 770 | delete(target, getAction(R.id.report_phishing, target)); |
| 771 | break; |
Mindy Pereira | f5acda4 | 2012-02-15 20:13:59 -0800 | [diff] [blame] | 772 | case android.R.id.home: |
| 773 | onUpPressed(); |
| 774 | break; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 775 | case R.id.compose: |
| 776 | ComposeActivity.compose(mActivity.getActivityContext(), mAccount); |
| 777 | break; |
Mindy Pereira | 28d5f72 | 2012-02-15 12:32:40 -0800 | [diff] [blame] | 778 | case R.id.show_all_folders: |
| 779 | showFolderList(); |
| 780 | break; |
Mindy Pereira | 28e0c34 | 2012-02-17 15:05:13 -0800 | [diff] [blame] | 781 | case R.id.refresh: |
| 782 | requestFolderRefresh(); |
| 783 | break; |
Mindy Pereira | 1f93668 | 2012-03-02 11:30:33 -0800 | [diff] [blame] | 784 | case R.id.settings: |
| 785 | Utils.showSettings(mActivity.getActivityContext(), mAccount); |
Paul Westbrook | 2861b6a | 2012-02-15 15:25:34 -0800 | [diff] [blame] | 786 | break; |
Paul Westbrook | e550355 | 2012-03-28 00:35:57 -0700 | [diff] [blame] | 787 | case R.id.folder_options: |
| 788 | Utils.showFolderSettings(mActivity.getActivityContext(), mAccount, mFolder); |
| 789 | break; |
Paul Westbrook | 94e440d | 2012-02-24 11:03:47 -0800 | [diff] [blame] | 790 | case R.id.help_info_menu_item: |
Paul Westbrook | 30745b6 | 2012-08-19 14:10:32 -0700 | [diff] [blame] | 791 | Utils.showHelp(mActivity.getActivityContext(), mAccount, getHelpContext()); |
Paul Westbrook | 94e440d | 2012-02-24 11:03:47 -0800 | [diff] [blame] | 792 | break; |
Vikram Aggarwal | 54452ae | 2012-03-13 15:29:00 -0700 | [diff] [blame] | 793 | case R.id.feedback_menu_item: |
Paul Westbrook | 17beb0b | 2012-08-20 13:34:37 -0700 | [diff] [blame] | 794 | Utils.sendFeedback(mActivity.getActivityContext(), mAccount, false); |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 795 | break; |
Paul Westbrook | 18babd2 | 2012-04-09 22:17:08 -0700 | [diff] [blame] | 796 | case R.id.manage_folders_item: |
| 797 | Utils.showManageFolder(mActivity.getActivityContext(), mAccount); |
| 798 | break; |
Vikram Aggarwal | d503df4 | 2012-05-11 10:13:35 -0700 | [diff] [blame] | 799 | case R.id.change_folder: |
| 800 | new FoldersSelectionDialog(mActivity.getActivityContext(), mAccount, this, |
Mindy Pereira | a832d3d | 2012-07-27 11:19:50 -0700 | [diff] [blame] | 801 | Conversation.listOf(mCurrentConversation), false, mFolder).show(); |
Vikram Aggarwal | d503df4 | 2012-05-11 10:13:35 -0700 | [diff] [blame] | 802 | break; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 803 | default: |
| 804 | handled = false; |
| 805 | break; |
Mindy Pereira | 28d5f72 | 2012-02-15 12:32:40 -0800 | [diff] [blame] | 806 | } |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 807 | return handled; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 808 | } |
| 809 | |
Vikram Aggarwal | 531488e | 2012-05-29 16:36:52 -0700 | [diff] [blame] | 810 | @Override |
Mindy Pereira | 6c2663d | 2012-07-20 15:37:29 -0700 | [diff] [blame] | 811 | public void updateConversation(Collection<Conversation> target, ContentValues values) { |
| 812 | mConversationListCursor.updateValues(mContext, target, values); |
| 813 | refreshConversationList(); |
| 814 | } |
| 815 | |
| 816 | @Override |
Vikram Aggarwal | 531488e | 2012-05-29 16:36:52 -0700 | [diff] [blame] | 817 | public void updateConversation(Collection <Conversation> target, String columnName, |
| 818 | boolean value) { |
| 819 | mConversationListCursor.updateBoolean(mContext, target, columnName, value); |
Vikram Aggarwal | 75daee5 | 2012-04-30 11:13:09 -0700 | [diff] [blame] | 820 | refreshConversationList(); |
Vikram Aggarwal | 54452ae | 2012-03-13 15:29:00 -0700 | [diff] [blame] | 821 | } |
| 822 | |
Vikram Aggarwal | 531488e | 2012-05-29 16:36:52 -0700 | [diff] [blame] | 823 | @Override |
Mindy Pereira | 6c2663d | 2012-07-20 15:37:29 -0700 | [diff] [blame] | 824 | public void updateConversation(Collection <Conversation> target, String columnName, |
| 825 | int value) { |
Vikram Aggarwal | 531488e | 2012-05-29 16:36:52 -0700 | [diff] [blame] | 826 | mConversationListCursor.updateInt(mContext, target, columnName, value); |
Vikram Aggarwal | 75daee5 | 2012-04-30 11:13:09 -0700 | [diff] [blame] | 827 | refreshConversationList(); |
Mindy Pereira | c9d5918 | 2012-03-22 16:06:46 -0700 | [diff] [blame] | 828 | } |
| 829 | |
Vikram Aggarwal | 531488e | 2012-05-29 16:36:52 -0700 | [diff] [blame] | 830 | @Override |
| 831 | public void updateConversation(Collection <Conversation> target, String columnName, |
| 832 | String value) { |
| 833 | mConversationListCursor.updateString(mContext, target, columnName, value); |
Vikram Aggarwal | 75daee5 | 2012-04-30 11:13:09 -0700 | [diff] [blame] | 834 | refreshConversationList(); |
Vikram Aggarwal | 54452ae | 2012-03-13 15:29:00 -0700 | [diff] [blame] | 835 | } |
| 836 | |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 837 | @Override |
| 838 | public void markConversationMessagesUnread(Conversation conv, Set<Uri> unreadMessageUris, |
Vikram Aggarwal | 4a878b6 | 2012-07-31 15:09:25 -0700 | [diff] [blame] | 839 | String originalConversationInfo) { |
Andy Huang | 8f6b006 | 2012-07-31 15:36:31 -0700 | [diff] [blame] | 840 | // The only caller of this method is the conversation view, from where marking unread should |
| 841 | // *always* take you back to list mode. |
| 842 | showConversation(null); |
| 843 | |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 844 | // locally mark conversation unread (the provider is supposed to propagate message unread |
| 845 | // to conversation unread) |
| 846 | conv.read = false; |
| 847 | |
Andy Huang | 28e31e2 | 2012-07-26 16:33:15 -0700 | [diff] [blame] | 848 | // only do a granular 'mark unread' if a subset of messages are unread |
| 849 | final int unreadCount = (unreadMessageUris == null) ? 0 : unreadMessageUris.size(); |
Mindy Pereira | 0972e07 | 2012-08-01 17:43:06 -0700 | [diff] [blame] | 850 | final int numMessages = conv.getNumMessages(); |
| 851 | final boolean subsetIsUnread = (numMessages > 1 && unreadCount > 0 |
| 852 | && unreadCount < numMessages); |
Andy Huang | 28e31e2 | 2012-07-26 16:33:15 -0700 | [diff] [blame] | 853 | |
| 854 | if (!subsetIsUnread) { |
Vikram Aggarwal | 66bc2aa | 2012-08-02 10:47:03 -0700 | [diff] [blame] | 855 | // Conversations are neither marked read, nor viewed, and we don't want to show |
| 856 | // the next conversation. |
| 857 | markConversationsRead(Collections.singletonList(conv), false, false, false); |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 858 | } else { |
Andy Huang | daa06ab | 2012-07-24 10:46:44 -0700 | [diff] [blame] | 859 | mConversationListCursor.setConversationColumn(conv.uri, ConversationColumns.READ, 0); |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 860 | |
Mindy Pereira | 7b6d03d | 2012-07-30 13:03:41 -0700 | [diff] [blame] | 861 | // locally update conversation's conversationInfo to revert to original version |
Andy Huang | 28e31e2 | 2012-07-26 16:33:15 -0700 | [diff] [blame] | 862 | if (originalConversationInfo != null) { |
| 863 | mConversationListCursor.setConversationColumn(conv.uri, |
| 864 | ConversationColumns.CONVERSATION_INFO, originalConversationInfo); |
| 865 | } |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 866 | |
| 867 | // applyBatch with each CPO as an UPDATE op on each affected message uri |
| 868 | final ArrayList<ContentProviderOperation> ops = Lists.newArrayList(); |
| 869 | String authority = null; |
| 870 | for (Uri messageUri : unreadMessageUris) { |
| 871 | if (authority == null) { |
| 872 | authority = messageUri.getAuthority(); |
| 873 | } |
| 874 | ops.add(ContentProviderOperation.newUpdate(messageUri) |
| 875 | .withValue(UIProvider.MessageColumns.READ, 0) |
| 876 | .build()); |
| 877 | } |
| 878 | |
| 879 | new ContentProviderTask() { |
| 880 | @Override |
| 881 | protected void onPostExecute(Result result) { |
| 882 | // TODO: handle errors? |
| 883 | } |
| 884 | }.run(mResolver, authority, ops); |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 885 | } |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | @Override |
Vikram Aggarwal | 66bc2aa | 2012-08-02 10:47:03 -0700 | [diff] [blame] | 889 | public void markConversationsRead(Collection<Conversation> targets, boolean read, |
| 890 | boolean viewed) { |
| 891 | // We want to show the next conversation if we are marking unread. |
| 892 | markConversationsRead(targets, read, viewed, true); |
Andy Huang | 8f6b006 | 2012-07-31 15:36:31 -0700 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | private void markConversationsRead(Collection<Conversation> targets, boolean read, |
Vikram Aggarwal | 66bc2aa | 2012-08-02 10:47:03 -0700 | [diff] [blame] | 896 | boolean markViewed, boolean showNext) { |
Andy Huang | 8f6b006 | 2012-07-31 15:36:31 -0700 | [diff] [blame] | 897 | // auto-advance if requested and the current conversation is being marked unread |
| 898 | if (showNext && !read) { |
| 899 | showNextConversation(targets); |
| 900 | } |
| 901 | |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 902 | for (Conversation target : targets) { |
Vikram Aggarwal | 192fac1 | 2012-07-25 16:44:55 -0700 | [diff] [blame] | 903 | final ContentValues values = new ContentValues(); |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 904 | values.put(ConversationColumns.READ, read); |
Vikram Aggarwal | 66bc2aa | 2012-08-02 10:47:03 -0700 | [diff] [blame] | 905 | if (markViewed) { |
| 906 | values.put(ConversationColumns.VIEWED, true); |
| 907 | } |
Vikram Aggarwal | 192fac1 | 2012-07-25 16:44:55 -0700 | [diff] [blame] | 908 | final ConversationInfo info = target.conversationInfo; |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 909 | if (info != null) { |
Mindy Pereira | 5f42437 | 2012-07-30 11:49:55 -0700 | [diff] [blame] | 910 | info.markRead(read); |
| 911 | values.put(ConversationColumns.CONVERSATION_INFO, ConversationInfo.toString(info)); |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 912 | } |
| 913 | updateConversation(Conversation.listOf(target), values); |
| 914 | } |
| 915 | // Update the conversations in the selection too. |
| 916 | for (final Conversation c : targets) { |
| 917 | c.read = read; |
Andy Huang | cd5c5ee | 2012-08-12 19:03:51 -0700 | [diff] [blame] | 918 | if (markViewed) { |
| 919 | c.markViewed(); |
| 920 | } |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 921 | } |
| 922 | } |
| 923 | |
Andy Huang | 8f6b006 | 2012-07-31 15:36:31 -0700 | [diff] [blame] | 924 | /** |
| 925 | * Auto-advance to a different conversation if the currently visible conversation in |
| 926 | * conversation mode is affected (deleted, marked unread, etc.). |
| 927 | * |
| 928 | * <p>Does nothing if outside of conversation mode. |
| 929 | * |
| 930 | * @param target the set of conversations being deleted/marked unread |
| 931 | */ |
| 932 | private void showNextConversation(Collection<Conversation> target) { |
| 933 | final boolean currentConversationInView = (mViewMode.getMode() == ViewMode.CONVERSATION) |
| 934 | && Conversation.contains(target, mCurrentConversation); |
| 935 | if (currentConversationInView) { |
| 936 | final Conversation next = mTracker.getNextConversation( |
| 937 | Settings.getAutoAdvanceSetting(mAccount.settings), target); |
| 938 | LogUtils.d(LOG_TAG, "showNextConversation: showing %s next.", next); |
| 939 | showConversation(next); |
| 940 | } |
| 941 | } |
| 942 | |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 943 | @Override |
| 944 | public void starMessage(ConversationMessage msg, boolean starred) { |
| 945 | if (msg.starred == starred) { |
| 946 | return; |
| 947 | } |
| 948 | |
| 949 | msg.starred = starred; |
| 950 | |
| 951 | // locally propagate the change to the owning conversation |
| 952 | // (figure the provider will properly propagate the change when it commits it) |
| 953 | // |
| 954 | // when unstarring, only propagate the change if this was the only message starred |
| 955 | final boolean conversationStarred = starred || msg.isConversationStarred(); |
| 956 | if (conversationStarred != msg.conversation.starred) { |
| 957 | msg.conversation.starred = conversationStarred; |
Andy Huang | daa06ab | 2012-07-24 10:46:44 -0700 | [diff] [blame] | 958 | mConversationListCursor.setConversationColumn(msg.conversation.uri, |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 959 | ConversationColumns.STARRED, conversationStarred); |
| 960 | } |
| 961 | |
| 962 | final ContentValues values = new ContentValues(1); |
| 963 | values.put(UIProvider.MessageColumns.STARRED, starred ? 1 : 0); |
| 964 | |
| 965 | new ContentProviderTask.UpdateTask() { |
| 966 | @Override |
| 967 | protected void onPostExecute(Result result) { |
| 968 | // TODO: handle errors? |
| 969 | } |
| 970 | }.run(mResolver, msg.uri, values, null /* selection*/, null /* selectionArgs */); |
| 971 | } |
| 972 | |
Mindy Pereira | 28e0c34 | 2012-02-17 15:05:13 -0800 | [diff] [blame] | 973 | private void requestFolderRefresh() { |
| 974 | if (mFolder != null) { |
Mindy Pereira | b7b33e0 | 2012-02-21 15:32:19 -0800 | [diff] [blame] | 975 | if (mAsyncRefreshTask != null) { |
| 976 | mAsyncRefreshTask.cancel(true); |
| 977 | } |
Paul Westbrook | 7e2a2a1 | 2012-06-27 13:52:40 -0700 | [diff] [blame] | 978 | mAsyncRefreshTask = new AsyncRefreshTask(mContext, mFolder.refreshUri); |
Mindy Pereira | b7b33e0 | 2012-02-21 15:32:19 -0800 | [diff] [blame] | 979 | mAsyncRefreshTask.execute(); |
Mindy Pereira | 28e0c34 | 2012-02-17 15:05:13 -0800 | [diff] [blame] | 980 | } |
| 981 | } |
| 982 | |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 983 | /** |
| 984 | * Confirm (based on user's settings) and delete a conversation from the conversation list and |
| 985 | * from the database. |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 986 | * @param target the conversations to act upon |
| 987 | * @param showDialog true if a confirmation dialog is to be shown, false otherwise. |
| 988 | * @param confirmResource the resource ID of the string that is shown in the confirmation dialog |
| 989 | * @param action the action to perform after animating the deletion of the conversations. |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 990 | */ |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 991 | protected void confirmAndDelete(final Collection<Conversation> target, boolean showDialog, |
| 992 | int confirmResource, final DestructiveAction action) { |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 993 | if (showDialog) { |
| 994 | final AlertDialog.OnClickListener onClick = new AlertDialog.OnClickListener() { |
| 995 | @Override |
| 996 | public void onClick(DialogInterface dialog, int which) { |
Vikram Aggarwal | 4f4782b | 2012-05-30 08:39:09 -0700 | [diff] [blame] | 997 | delete(target, action); |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 998 | } |
| 999 | }; |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 1000 | final CharSequence message = Utils.formatPlural(mContext, confirmResource, |
| 1001 | target.size()); |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 1002 | new AlertDialog.Builder(mActivity.getActivityContext()).setMessage(message) |
| 1003 | .setPositiveButton(R.string.ok, onClick) |
| 1004 | .setNegativeButton(R.string.cancel, null) |
| 1005 | .create().show(); |
| 1006 | } else { |
Vikram Aggarwal | 4f4782b | 2012-05-30 08:39:09 -0700 | [diff] [blame] | 1007 | delete(target, action); |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 1008 | } |
| 1009 | } |
| 1010 | |
Vikram Aggarwal | 4f4782b | 2012-05-30 08:39:09 -0700 | [diff] [blame] | 1011 | @Override |
| 1012 | public void delete(final Collection<Conversation> target, final DestructiveAction action) { |
Vikram Aggarwal | 192fac1 | 2012-07-25 16:44:55 -0700 | [diff] [blame] | 1013 | // Order of events is critical! The Conversation View Fragment must be notified |
| 1014 | // of the next conversation with showConversation(next) *before* the conversation list |
| 1015 | // fragment has a chance to delete the conversation, animating it away. |
| 1016 | |
Vikram Aggarwal | d503df4 | 2012-05-11 10:13:35 -0700 | [diff] [blame] | 1017 | // Update the conversation fragment if the current conversation is deleted. |
Andy Huang | 8f6b006 | 2012-07-31 15:36:31 -0700 | [diff] [blame] | 1018 | showNextConversation(target); |
Vikram Aggarwal | 192fac1 | 2012-07-25 16:44:55 -0700 | [diff] [blame] | 1019 | // The conversation list deletes and performs the action if it exists. |
| 1020 | final ConversationListFragment convListFragment = getConversationListFragment(); |
| 1021 | if (convListFragment != null) { |
| 1022 | LogUtils.d(LOG_TAG, "AAC.requestDelete: ListFragment is handling delete."); |
| 1023 | convListFragment.requestDelete(target, action); |
| 1024 | return; |
| 1025 | } |
Vikram Aggarwal | d503df4 | 2012-05-11 10:13:35 -0700 | [diff] [blame] | 1026 | // No visible UI element handled it on our behalf. Perform the action ourself. |
| 1027 | action.performAction(); |
| 1028 | } |
| 1029 | |
| 1030 | /** |
| 1031 | * Requests that the action be performed and the UI state is updated to reflect the new change. |
| 1032 | * @param target |
| 1033 | * @param action |
| 1034 | */ |
Vikram Aggarwal | 4f4782b | 2012-05-30 08:39:09 -0700 | [diff] [blame] | 1035 | private void requestUpdate(final Collection<Conversation> target, |
Vikram Aggarwal | d503df4 | 2012-05-11 10:13:35 -0700 | [diff] [blame] | 1036 | final DestructiveAction action) { |
| 1037 | action.performAction(); |
| 1038 | refreshConversationList(); |
Vikram Aggarwal | 75daee5 | 2012-04-30 11:13:09 -0700 | [diff] [blame] | 1039 | } |
Vikram Aggarwal | 6fbc87a | 2012-03-15 15:24:00 -0700 | [diff] [blame] | 1040 | |
| 1041 | @Override |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1042 | public void onPrepareDialog(int id, Dialog dialog, Bundle bundle) { |
| 1043 | // TODO(viki): Auto-generated method stub |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1044 | } |
| 1045 | |
| 1046 | @Override |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1047 | public boolean onPrepareOptionsMenu(Menu menu) { |
Mindy Pereira | b849dfb | 2012-03-07 18:13:15 -0800 | [diff] [blame] | 1048 | mActionBarView.onPrepareOptionsMenu(menu); |
Mindy Pereira | 363451a | 2012-02-22 14:14:46 -0800 | [diff] [blame] | 1049 | return true; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1050 | } |
| 1051 | |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 1052 | @Override |
| 1053 | public void onPause() { |
| 1054 | isLoaderInitialized = false; |
Paul Westbrook | 6ead20d | 2012-03-19 14:48:14 -0700 | [diff] [blame] | 1055 | enableNotifications(); |
Paul Westbrook | 94e440d | 2012-02-24 11:03:47 -0800 | [diff] [blame] | 1056 | } |
| 1057 | |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1058 | @Override |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1059 | public void onResume() { |
Paul Westbrook | 6ead20d | 2012-03-19 14:48:14 -0700 | [diff] [blame] | 1060 | // Register the receiver that will prevent the status receiver from |
| 1061 | // displaying its notification icon as long as we're running. |
| 1062 | // The SupressNotificationReceiver will block the broadcast if we're looking at the folder |
| 1063 | // that the notification was received for. |
| 1064 | disableNotifications(); |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | @Override |
| 1068 | public void onSaveInstanceState(Bundle outState) { |
Vikram Aggarwal | 6c51158 | 2012-02-27 10:59:47 -0800 | [diff] [blame] | 1069 | if (mAccount != null) { |
| 1070 | LogUtils.d(LOG_TAG, "Saving the account now"); |
| 1071 | outState.putParcelable(SAVED_ACCOUNT, mAccount); |
| 1072 | } |
Mindy Pereira | 5e478d2 | 2012-03-26 18:04:58 -0700 | [diff] [blame] | 1073 | if (mFolder != null) { |
| 1074 | outState.putParcelable(SAVED_FOLDER, mFolder); |
Vikram Aggarwal | 8b15263 | 2012-02-03 14:58:45 -0800 | [diff] [blame] | 1075 | } |
Vikram Aggarwal | f334140 | 2012-08-07 10:09:38 -0700 | [diff] [blame] | 1076 | // If this is a search activity, let's store the search query term as well. |
| 1077 | if (ConversationListContext.isSearchResult(mConvListContext)) { |
| 1078 | outState.putString(SAVED_QUERY, mConvListContext.searchQuery); |
| 1079 | } |
Mindy Pereira | 0f7ae7a | 2012-07-17 13:39:56 -0700 | [diff] [blame] | 1080 | int mode = mViewMode.getMode(); |
Mindy Pereira | 9fa43ca | 2012-05-17 14:18:01 -0700 | [diff] [blame] | 1081 | if (mCurrentConversation != null |
Mindy Pereira | 0f7ae7a | 2012-07-17 13:39:56 -0700 | [diff] [blame] | 1082 | && (mode == ViewMode.CONVERSATION || |
Mindy Pereira | 9fa43ca | 2012-05-17 14:18:01 -0700 | [diff] [blame] | 1083 | mViewMode.getMode() == ViewMode.SEARCH_RESULTS_CONVERSATION)) { |
Mindy Pereira | 26f23fc | 2012-03-27 10:26:04 -0700 | [diff] [blame] | 1084 | outState.putParcelable(SAVED_CONVERSATION, mCurrentConversation); |
| 1085 | } |
Andy Huang | 4556a44 | 2012-03-30 16:42:05 -0700 | [diff] [blame] | 1086 | if (!mSelectedSet.isEmpty()) { |
Vikram Aggarwal | cabd3f2 | 2012-04-19 10:14:41 -0700 | [diff] [blame] | 1087 | outState.putParcelable(SAVED_SELECTED_SET, mSelectedSet); |
Andy Huang | 4556a44 | 2012-03-30 16:42:05 -0700 | [diff] [blame] | 1088 | } |
Mindy Pereira | d3367499 | 2012-06-25 16:26:30 -0700 | [diff] [blame] | 1089 | if (mToastBar.getVisibility() == View.VISIBLE) { |
| 1090 | outState.putParcelable(SAVED_TOAST_BAR_OP, mToastBar.getOperation()); |
| 1091 | } |
| 1092 | ConversationListFragment convListFragment = getConversationListFragment(); |
| 1093 | if (convListFragment != null) { |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 1094 | convListFragment.getAnimatedAdapter() |
Mindy Pereira | d3367499 | 2012-06-25 16:26:30 -0700 | [diff] [blame] | 1095 | .onSaveInstanceState(outState); |
| 1096 | } |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | @Override |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 1100 | public void onSearchRequested(String query) { |
| 1101 | Intent intent = new Intent(); |
| 1102 | intent.setAction(Intent.ACTION_SEARCH); |
| 1103 | intent.putExtra(ConversationListContext.EXTRA_SEARCH_QUERY, query); |
| 1104 | intent.putExtra(Utils.EXTRA_ACCOUNT, mAccount); |
| 1105 | intent.setComponent(mActivity.getComponentName()); |
Vikram Aggarwal | b17cbc0 | 2012-04-06 15:41:46 -0700 | [diff] [blame] | 1106 | mActionBarView.collapseSearch(); |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 1107 | mActivity.startActivity(intent); |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1108 | } |
| 1109 | |
| 1110 | @Override |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1111 | public void onStop() { |
Mindy Pereira | 49e5dbe | 2012-07-12 11:47:54 -0700 | [diff] [blame] | 1112 | if (mEnableShareIntents != null) { |
| 1113 | mEnableShareIntents.cancel(true); |
| 1114 | } |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1115 | } |
| 1116 | |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1117 | @Override |
| 1118 | public void onDestroy() { |
| 1119 | // unregister the ViewPager's observer on the conversation cursor |
| 1120 | mPagerController.onDestroy(); |
Mindy Pereira | 641de65 | 2012-08-02 15:21:50 -0700 | [diff] [blame] | 1121 | mActionBarView.onDestroy(); |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 1122 | mRecentFolderList.destroy(); |
Andy Huang | 4e0158f | 2012-08-07 21:06:01 -0700 | [diff] [blame] | 1123 | mDestroyed = true; |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1124 | } |
| 1125 | |
Vikram Aggarwal | fa131a2 | 2012-02-02 13:56:22 -0800 | [diff] [blame] | 1126 | /** |
Mindy Pereira | 161f50d | 2012-02-28 15:47:19 -0800 | [diff] [blame] | 1127 | * {@inheritDoc} Subclasses must override this to listen to mode changes |
| 1128 | * from the ViewMode. Subclasses <b>must</b> call the parent's |
| 1129 | * onViewModeChanged since the parent will handle common state changes. |
Vikram Aggarwal | fa131a2 | 2012-02-02 13:56:22 -0800 | [diff] [blame] | 1130 | */ |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1131 | @Override |
Vikram Aggarwal | fa131a2 | 2012-02-02 13:56:22 -0800 | [diff] [blame] | 1132 | public void onViewModeChanged(int newMode) { |
| 1133 | // Perform any mode specific work here. |
Mindy Pereira | 161f50d | 2012-02-28 15:47:19 -0800 | [diff] [blame] | 1134 | // reset the action bar icon based on the mode. Why don't the individual |
| 1135 | // controllers do |
Vikram Aggarwal | fa131a2 | 2012-02-02 13:56:22 -0800 | [diff] [blame] | 1136 | // this themselves? |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1137 | |
Mindy Pereira | 8937bf1 | 2012-07-23 14:05:02 -0700 | [diff] [blame] | 1138 | // Commit any destructive undoable actions the user may have performed. |
mindyp | c6adce3 | 2012-08-22 18:46:42 -0700 | [diff] [blame] | 1139 | commitDestructiveActions(true); |
Mindy Pereira | 8937bf1 | 2012-07-23 14:05:02 -0700 | [diff] [blame] | 1140 | |
Mindy Pereira | 161f50d | 2012-02-28 15:47:19 -0800 | [diff] [blame] | 1141 | // We don't want to invalidate the options menu when switching to |
| 1142 | // conversation |
Vikram Aggarwal | fa131a2 | 2012-02-02 13:56:22 -0800 | [diff] [blame] | 1143 | // mode, as it will happen when the conversation finishes loading. |
| 1144 | if (newMode != ViewMode.CONVERSATION) { |
| 1145 | mActivity.invalidateOptionsMenu(); |
| 1146 | } |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1147 | } |
| 1148 | |
Andy Huang | 4e0158f | 2012-08-07 21:06:01 -0700 | [diff] [blame] | 1149 | public boolean isDestroyed() { |
| 1150 | return mDestroyed; |
| 1151 | } |
| 1152 | |
mindyp | c6adce3 | 2012-08-22 18:46:42 -0700 | [diff] [blame] | 1153 | private void commitDestructiveActions(boolean animate) { |
| 1154 | ConversationListFragment fragment = getConversationListFragment(); |
Mindy Pereira | 1e2573b | 2012-04-17 14:34:36 -0700 | [diff] [blame] | 1155 | if (fragment != null) { |
mindyp | c6adce3 | 2012-08-22 18:46:42 -0700 | [diff] [blame] | 1156 | fragment.commitDestructiveActions(animate); |
Mindy Pereira | 1e2573b | 2012-04-17 14:34:36 -0700 | [diff] [blame] | 1157 | } |
| 1158 | } |
| 1159 | |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1160 | @Override |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1161 | public void onWindowFocusChanged(boolean hasFocus) { |
Paul Westbrook | 9f119c7 | 2012-04-24 16:10:59 -0700 | [diff] [blame] | 1162 | ConversationListFragment convList = getConversationListFragment(); |
| 1163 | if (hasFocus && convList != null && convList.isVisible()) { |
| 1164 | // The conversation list is visible. |
| 1165 | Utils.setConversationCursorVisibility(mConversationListCursor, true); |
| 1166 | } |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1167 | } |
| 1168 | |
Vikram Aggarwal | ca716f1 | 2012-08-20 11:11:48 -0700 | [diff] [blame] | 1169 | /** |
| 1170 | * Set the account, and carry out all the account-related changes that rely on this. |
| 1171 | * @param account |
| 1172 | */ |
| 1173 | // TODO(viki): Two different methods do the same thing. Resolve |
| 1174 | // {@link #setAccount(Account)} and {@link #switchAccount(Account, boolean)} |
Mindy Pereira | 75181e8 | 2012-04-18 08:17:13 -0700 | [diff] [blame] | 1175 | private void setAccount(Account account) { |
Andy Huang | b9ca979 | 2012-05-18 15:31:49 -0700 | [diff] [blame] | 1176 | if (account == null) { |
| 1177 | LogUtils.w(LOG_TAG, new Error(), |
| 1178 | "AAC ignoring null (presumably invalid) account restoration"); |
| 1179 | return; |
| 1180 | } |
Andy Huang | b114841 | 2012-05-19 00:16:30 -0700 | [diff] [blame] | 1181 | LogUtils.d(LOG_TAG, "AbstractActivityController.setAccount(): account = %s", account.uri); |
Vikram Aggarwal | 91e8737 | 2012-05-18 15:36:04 -0700 | [diff] [blame] | 1182 | mAccount = account; |
Vikram Aggarwal | ca716f1 | 2012-08-20 11:11:48 -0700 | [diff] [blame] | 1183 | // Only change AAC state here. Do *not* modify any other object's state. The object |
| 1184 | // should listen on account changes. |
| 1185 | restartOptionalLoader(LOADER_RECENT_FOLDERS); |
| 1186 | mActivity.invalidateOptionsMenu(); |
| 1187 | disableNotificationsOnAccountChange(mAccount); |
| 1188 | restartOptionalLoader(LOADER_ACCOUNT_UPDATE_CURSOR); |
| 1189 | MailAppProvider.getInstance().setLastViewedAccount(mAccount.uri.toString()); |
| 1190 | |
Vikram Aggarwal | 91e8737 | 2012-05-18 15:36:04 -0700 | [diff] [blame] | 1191 | if (account.settings == null) { |
| 1192 | LogUtils.w(LOG_TAG, new Error(), "AAC ignoring account with null settings."); |
| 1193 | return; |
| 1194 | } |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 1195 | mAccountObservers.notifyChanged(); |
Mindy Pereira | 75181e8 | 2012-04-18 08:17:13 -0700 | [diff] [blame] | 1196 | } |
| 1197 | |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1198 | /** |
Mindy Pereira | 161f50d | 2012-02-28 15:47:19 -0800 | [diff] [blame] | 1199 | * Restore the state from the previous bundle. Subclasses should call this |
| 1200 | * method from the parent class, since it performs important UI |
| 1201 | * initialization. |
| 1202 | * |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1203 | * @param savedState |
| 1204 | */ |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1205 | @Override |
| 1206 | public void onRestoreInstanceState(Bundle savedState) { |
| 1207 | LogUtils.d(LOG_TAG, "IN AAC.onRestoreInstanceState"); |
| 1208 | if (savedState.containsKey(SAVED_CONVERSATION)) { |
| 1209 | // Open the conversation. |
Paul Westbrook | 534e4a2 | 2012-04-25 03:46:29 -0700 | [diff] [blame] | 1210 | final Conversation conversation = |
| 1211 | (Conversation)savedState.getParcelable(SAVED_CONVERSATION); |
| 1212 | if (conversation != null && conversation.position < 0) { |
| 1213 | // Set the position to 0 on this conversation, as we don't know where it is |
| 1214 | // in the list |
| 1215 | conversation.position = 0; |
| 1216 | } |
Andy Huang | ed4fdf0 | 2012-07-26 17:12:50 -0700 | [diff] [blame] | 1217 | showConversation(conversation); |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1218 | } |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 1219 | |
Mindy Pereira | d3367499 | 2012-06-25 16:26:30 -0700 | [diff] [blame] | 1220 | if (savedState.containsKey(SAVED_TOAST_BAR_OP)) { |
| 1221 | ToastBarOperation op = ((ToastBarOperation) savedState |
| 1222 | .getParcelable(SAVED_TOAST_BAR_OP)); |
| 1223 | if (op != null) { |
| 1224 | if (op.getType() == ToastBarOperation.UNDO) { |
| 1225 | onUndoAvailable(op); |
Andrew Sapperstein | 9d7519d | 2012-07-16 14:03:53 -0700 | [diff] [blame] | 1226 | } else if (op.getType() == ToastBarOperation.ERROR) { |
| 1227 | onError(mFolder, true); |
Mindy Pereira | d3367499 | 2012-06-25 16:26:30 -0700 | [diff] [blame] | 1228 | } |
| 1229 | } |
| 1230 | } |
Mindy Pereira | 0f7ae7a | 2012-07-17 13:39:56 -0700 | [diff] [blame] | 1231 | |
Mindy Pereira | d3367499 | 2012-06-25 16:26:30 -0700 | [diff] [blame] | 1232 | ConversationListFragment convListFragment = getConversationListFragment(); |
| 1233 | if (convListFragment != null) { |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 1234 | convListFragment.getAnimatedAdapter() |
Mindy Pereira | d3367499 | 2012-06-25 16:26:30 -0700 | [diff] [blame] | 1235 | .onRestoreInstanceState(savedState); |
| 1236 | } |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 1237 | /** |
| 1238 | * Restore the state of selected conversations. This needs to be done after the correct mode |
| 1239 | * is set and the action bar is fully initialized. If not, several key pieces of state |
| 1240 | * information will be missing, and the split views may not be initialized correctly. |
| 1241 | * @param savedState |
| 1242 | */ |
Andy Huang | 4556a44 | 2012-03-30 16:42:05 -0700 | [diff] [blame] | 1243 | restoreSelectedConversations(savedState); |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | private void handleIntent(Intent intent) { |
| 1247 | boolean handled = false; |
| 1248 | if (Intent.ACTION_VIEW.equals(intent.getAction())) { |
| 1249 | if (intent.hasExtra(Utils.EXTRA_ACCOUNT)) { |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1250 | setAccount(Account.newinstance(intent |
Mindy Pereira | 5ad0291 | 2012-07-09 09:57:18 -0700 | [diff] [blame] | 1251 | .getStringExtra(Utils.EXTRA_ACCOUNT))); |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1252 | } |
Andy Huang | b9ca979 | 2012-05-18 15:31:49 -0700 | [diff] [blame] | 1253 | if (mAccount == null) { |
| 1254 | return; |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1255 | } |
Andy Huang | b9ca979 | 2012-05-18 15:31:49 -0700 | [diff] [blame] | 1256 | mActivity.invalidateOptionsMenu(); |
Vikram Aggarwal | 1a249e0 | 2012-08-03 16:19:33 -0700 | [diff] [blame] | 1257 | final boolean isConversationMode = intent.hasExtra(Utils.EXTRA_CONVERSATION); |
| 1258 | // TODO(viki): Allow the controller to set the mode instead of a mode transition. |
| 1259 | if (isConversationMode) { |
| 1260 | mViewMode.enterConversationMode(); |
| 1261 | } else { |
| 1262 | mViewMode.enterConversationListMode(); |
| 1263 | } |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1264 | |
| 1265 | Folder folder = null; |
| 1266 | if (intent.hasExtra(Utils.EXTRA_FOLDER)) { |
| 1267 | // Open the folder. |
Mindy Pereira | 7b6d03d | 2012-07-30 13:03:41 -0700 | [diff] [blame] | 1268 | folder = Folder.fromString(intent.getStringExtra(Utils.EXTRA_FOLDER)); |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1269 | } |
| 1270 | if (folder != null) { |
| 1271 | onFolderChanged(folder); |
| 1272 | handled = true; |
| 1273 | } |
| 1274 | |
Vikram Aggarwal | 1a249e0 | 2012-08-03 16:19:33 -0700 | [diff] [blame] | 1275 | if (isConversationMode) { |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1276 | // Open the conversation. |
| 1277 | LogUtils.d(LOG_TAG, "SHOW THE CONVERSATION at %s", |
| 1278 | intent.getParcelableExtra(Utils.EXTRA_CONVERSATION)); |
Paul Westbrook | a916191 | 2012-04-24 10:10:04 -0700 | [diff] [blame] | 1279 | final Conversation conversation = |
| 1280 | (Conversation)intent.getParcelableExtra(Utils.EXTRA_CONVERSATION); |
| 1281 | if (conversation != null && conversation.position < 0) { |
| 1282 | // Set the position to 0 on this conversation, as we don't know where it is |
| 1283 | // in the list |
| 1284 | conversation.position = 0; |
| 1285 | } |
Andy Huang | 980aaea | 2012-07-26 17:22:19 -0700 | [diff] [blame] | 1286 | showConversation(conversation); |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1287 | handled = true; |
| 1288 | } |
| 1289 | |
| 1290 | if (!handled) { |
| 1291 | // Nothing was saved; just load the account inbox. |
| 1292 | loadAccountInbox(); |
| 1293 | } |
| 1294 | } else if (Intent.ACTION_SEARCH.equals(intent.getAction())) { |
| 1295 | if (intent.hasExtra(Utils.EXTRA_ACCOUNT)) { |
| 1296 | // Save this search query for future suggestions. |
| 1297 | final String query = intent.getStringExtra(SearchManager.QUERY); |
| 1298 | final String authority = mContext.getString(R.string.suggestions_authority); |
| 1299 | SearchRecentSuggestions suggestions = new SearchRecentSuggestions( |
| 1300 | mContext, authority, SuggestionsProvider.MODE); |
| 1301 | suggestions.saveRecentQuery(query, null); |
Mindy Pereira | ac25482 | 2012-06-18 10:46:43 -0700 | [diff] [blame] | 1302 | if (Utils.showTwoPaneSearchResults(mActivity.getActivityContext())) { |
| 1303 | mViewMode.enterSearchResultsConversationMode(); |
| 1304 | } else { |
| 1305 | mViewMode.enterSearchResultsListMode(); |
| 1306 | } |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1307 | setAccount((Account) intent.getParcelableExtra(Utils.EXTRA_ACCOUNT)); |
| 1308 | mActivity.invalidateOptionsMenu(); |
| 1309 | restartOptionalLoader(LOADER_RECENT_FOLDERS); |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1310 | fetchSearchFolder(intent); |
| 1311 | } else { |
| 1312 | LogUtils.e(LOG_TAG, "Missing account extra from search intent. Finishing"); |
| 1313 | mActivity.finish(); |
| 1314 | } |
| 1315 | } |
| 1316 | if (mAccount != null) { |
| 1317 | restartOptionalLoader(LOADER_ACCOUNT_UPDATE_CURSOR); |
| 1318 | } |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1319 | } |
| 1320 | |
Andy Huang | 4556a44 | 2012-03-30 16:42:05 -0700 | [diff] [blame] | 1321 | /** |
| 1322 | * Copy any selected conversations stored in the saved bundle into our selection set, |
| 1323 | * triggering {@link ConversationSetObserver} callbacks as our selection set changes. |
| 1324 | * |
| 1325 | */ |
Vikram Aggarwal | 4eb5271 | 2012-06-19 16:24:50 -0700 | [diff] [blame] | 1326 | private final void restoreSelectedConversations(Bundle savedState) { |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 1327 | if (savedState == null) { |
Andy Huang | 4556a44 | 2012-03-30 16:42:05 -0700 | [diff] [blame] | 1328 | mSelectedSet.clear(); |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 1329 | return; |
| 1330 | } |
Vikram Aggarwal | cabd3f2 | 2012-04-19 10:14:41 -0700 | [diff] [blame] | 1331 | final ConversationSelectionSet selectedSet = savedState.getParcelable(SAVED_SELECTED_SET); |
Andy Huang | 4556a44 | 2012-03-30 16:42:05 -0700 | [diff] [blame] | 1332 | if (selectedSet == null || selectedSet.isEmpty()) { |
| 1333 | mSelectedSet.clear(); |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 1334 | return; |
| 1335 | } |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1336 | |
| 1337 | // putAll will take care of calling our registered onSetPopulated method |
Vikram Aggarwal | cabd3f2 | 2012-04-19 10:14:41 -0700 | [diff] [blame] | 1338 | mSelectedSet.putAll(selectedSet); |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 1339 | } |
| 1340 | |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1341 | @Override |
Andy Huang | 5895f7b | 2012-06-01 17:07:20 -0700 | [diff] [blame] | 1342 | public SubjectDisplayChanger getSubjectDisplayChanger() { |
| 1343 | return mActionBarView; |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1344 | } |
| 1345 | |
Vikram Aggarwal | ec5cbf7 | 2012-03-08 15:10:35 -0800 | [diff] [blame] | 1346 | /** |
| 1347 | * Children can override this method, but they must call super.showConversation(). |
| 1348 | * {@inheritDoc} |
| 1349 | */ |
| 1350 | @Override |
| 1351 | public void showConversation(Conversation conversation) { |
Vikram Aggarwal | c67182d | 2012-04-03 14:35:06 -0700 | [diff] [blame] | 1352 | // Set the current conversation just in case it wasn't already set. |
| 1353 | setCurrentConversation(conversation); |
Vikram Aggarwal | ec5cbf7 | 2012-03-08 15:10:35 -0800 | [diff] [blame] | 1354 | } |
| 1355 | |
Vikram Aggarwal | e128fc2 | 2012-04-04 12:33:34 -0700 | [diff] [blame] | 1356 | /** |
Paul Westbrook | 2d50bcd | 2012-04-10 11:53:47 -0700 | [diff] [blame] | 1357 | * Children can override this method, but they must call super.showWaitForInitialization(). |
| 1358 | * {@inheritDoc} |
| 1359 | */ |
| 1360 | @Override |
| 1361 | public void showWaitForInitialization() { |
| 1362 | mViewMode.enterWaitingForInitializationMode(); |
| 1363 | } |
| 1364 | |
| 1365 | @Override |
| 1366 | public void hideWaitForInitialization() { |
| 1367 | } |
| 1368 | |
| 1369 | @Override |
| 1370 | public void updateWaitMode() { |
| 1371 | final FragmentManager manager = mActivity.getFragmentManager(); |
| 1372 | final WaitFragment waitFragment = |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 1373 | (WaitFragment)manager.findFragmentByTag(TAG_WAIT); |
Paul Westbrook | 2d50bcd | 2012-04-10 11:53:47 -0700 | [diff] [blame] | 1374 | if (waitFragment != null) { |
| 1375 | waitFragment.updateAccount(mAccount); |
| 1376 | } |
| 1377 | } |
| 1378 | |
Vikram Aggarwal | 48b2a6c | 2012-05-29 14:09:27 -0700 | [diff] [blame] | 1379 | /** |
| 1380 | * Returns true if we are waiting for the account to sync, and cannot show any folders or |
| 1381 | * conversation for the current account yet. |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 1382 | * |
Vikram Aggarwal | 48b2a6c | 2012-05-29 14:09:27 -0700 | [diff] [blame] | 1383 | */ |
Paul Westbrook | 2d50bcd | 2012-04-10 11:53:47 -0700 | [diff] [blame] | 1384 | public boolean inWaitMode() { |
| 1385 | final FragmentManager manager = mActivity.getFragmentManager(); |
| 1386 | final WaitFragment waitFragment = |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 1387 | (WaitFragment)manager.findFragmentByTag(TAG_WAIT); |
Paul Westbrook | 2d50bcd | 2012-04-10 11:53:47 -0700 | [diff] [blame] | 1388 | if (waitFragment != null) { |
| 1389 | final Account fragmentAccount = waitFragment.getAccount(); |
| 1390 | return fragmentAccount.uri.equals(mAccount.uri) && |
| 1391 | mViewMode.getMode() == ViewMode.WAITING_FOR_ACCOUNT_INITIALIZATION; |
| 1392 | } |
| 1393 | return false; |
| 1394 | } |
| 1395 | |
| 1396 | /** |
Vikram Aggarwal | e128fc2 | 2012-04-04 12:33:34 -0700 | [diff] [blame] | 1397 | * Children can override this method, but they must call super.showConversationList(). |
| 1398 | * {@inheritDoc} |
| 1399 | */ |
| 1400 | @Override |
| 1401 | public void showConversationList(ConversationListContext listContext) { |
| 1402 | } |
| 1403 | |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1404 | @Override |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 1405 | public void onConversationSelected(Conversation conversation) { |
Vikram Aggarwal | 5b7b3ab | 2012-04-03 15:43:55 -0700 | [diff] [blame] | 1406 | showConversation(conversation); |
Mindy Pereira | 9fa43ca | 2012-05-17 14:18:01 -0700 | [diff] [blame] | 1407 | if (Intent.ACTION_SEARCH.equals(mActivity.getIntent().getAction())) { |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 1408 | mViewMode.enterSearchResultsConversationMode(); |
| 1409 | } else { |
| 1410 | mViewMode.enterConversationMode(); |
| 1411 | } |
Vikram Aggarwal | 7d60288 | 2012-02-07 15:01:20 -0800 | [diff] [blame] | 1412 | } |
Mindy Pereira | 555140c | 2012-02-15 14:55:29 -0800 | [diff] [blame] | 1413 | |
Vikram Aggarwal | c67182d | 2012-04-03 14:35:06 -0700 | [diff] [blame] | 1414 | /** |
| 1415 | * Set the current conversation. This is the conversation on which all actions are performed. |
| 1416 | * Do not modify mCurrentConversation except through this method, which makes it easy to |
| 1417 | * perform common actions associated with changing the current conversation. |
| 1418 | * @param conversation |
| 1419 | */ |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1420 | @Override |
| 1421 | public void setCurrentConversation(Conversation conversation) { |
Mindy Pereira | 5040f1a | 2012-03-20 10:14:06 -0700 | [diff] [blame] | 1422 | mCurrentConversation = conversation; |
Vikram Aggarwal | 135fd02 | 2012-04-11 14:44:15 -0700 | [diff] [blame] | 1423 | mTracker.initialize(mCurrentConversation); |
Mindy Pereira | 5040f1a | 2012-03-20 10:14:06 -0700 | [diff] [blame] | 1424 | } |
| 1425 | |
Vikram Aggarwal | a9b93f3 | 2012-02-23 14:51:58 -0800 | [diff] [blame] | 1426 | /** |
| 1427 | * {@inheritDoc} |
| 1428 | */ |
Mindy Pereira | 555140c | 2012-02-15 14:55:29 -0800 | [diff] [blame] | 1429 | @Override |
Vikram Aggarwal | 7dedb95 | 2012-02-16 16:10:23 -0800 | [diff] [blame] | 1430 | public Loader<Cursor> onCreateLoader(int id, Bundle args) { |
| 1431 | // Create a loader to listen in on account changes. |
Vikram Aggarwal | fa4b47e | 2012-03-09 13:02:46 -0800 | [diff] [blame] | 1432 | switch (id) { |
| 1433 | case LOADER_ACCOUNT_CURSOR: |
Paul Westbrook | c2074c4 | 2012-03-22 15:26:58 -0700 | [diff] [blame] | 1434 | return new CursorLoader(mContext, MailAppProvider.getAccountsUri(), |
Vikram Aggarwal | fa4b47e | 2012-03-09 13:02:46 -0800 | [diff] [blame] | 1435 | UIProvider.ACCOUNTS_PROJECTION, null, null, null); |
| 1436 | case LOADER_FOLDER_CURSOR: |
Paul Westbrook | c7a070f | 2012-04-12 01:46:41 -0700 | [diff] [blame] | 1437 | final CursorLoader loader = new CursorLoader(mContext, mFolder.uri, |
| 1438 | UIProvider.FOLDERS_PROJECTION, null, null, null); |
| 1439 | loader.setUpdateThrottle(mFolderItemUpdateDelayMs); |
| 1440 | return loader; |
Vikram Aggarwal | fa4b47e | 2012-03-09 13:02:46 -0800 | [diff] [blame] | 1441 | case LOADER_RECENT_FOLDERS: |
Paul Westbrook | 91d1050 | 2012-04-13 12:01:39 -0700 | [diff] [blame] | 1442 | if (mAccount != null && mAccount.recentFolderListUri != null) { |
Paul Westbrook | ea4ee20 | 2012-03-12 14:12:33 -0700 | [diff] [blame] | 1443 | return new CursorLoader(mContext, mAccount.recentFolderListUri, |
| 1444 | UIProvider.FOLDERS_PROJECTION, null, null, null); |
| 1445 | } |
| 1446 | break; |
Mindy Pereira | ab48636 | 2012-03-21 18:18:53 -0700 | [diff] [blame] | 1447 | case LOADER_ACCOUNT_INBOX: |
Vikram Aggarwal | 025eba8 | 2012-05-08 10:45:30 -0700 | [diff] [blame] | 1448 | final Uri defaultInbox = Settings.getDefaultInboxUri(mAccount.settings); |
Vikram Aggarwal | 1e57e67 | 2012-05-07 14:48:24 -0700 | [diff] [blame] | 1449 | final Uri inboxUri = defaultInbox.equals(Uri.EMPTY) ? |
| 1450 | mAccount.folderListUri : defaultInbox; |
Paul Westbrook | 7496e82 | 2012-04-24 09:50:54 -0700 | [diff] [blame] | 1451 | LogUtils.d(LOG_TAG, "Loading the default inbox: %s", inboxUri); |
Paul Westbrook | 1220b6d | 2012-04-10 00:48:00 -0700 | [diff] [blame] | 1452 | if (inboxUri != null) { |
| 1453 | return new CursorLoader(mContext, inboxUri, UIProvider.FOLDERS_PROJECTION, null, |
| 1454 | null, null); |
| 1455 | } |
| 1456 | break; |
Mindy Pereira | ab48636 | 2012-03-21 18:18:53 -0700 | [diff] [blame] | 1457 | case LOADER_SEARCH: |
| 1458 | return Folder.forSearchResults(mAccount, |
| 1459 | args.getString(ConversationListContext.EXTRA_SEARCH_QUERY), |
| 1460 | mActivity.getActivityContext()); |
Paul Westbrook | 2d50bcd | 2012-04-10 11:53:47 -0700 | [diff] [blame] | 1461 | case LOADER_ACCOUNT_UPDATE_CURSOR: |
| 1462 | return new CursorLoader(mContext, mAccount.uri, UIProvider.ACCOUNTS_PROJECTION, |
| 1463 | null, null, null); |
Vikram Aggarwal | fa4b47e | 2012-03-09 13:02:46 -0800 | [diff] [blame] | 1464 | default: |
Paul Westbrook | ad6a275 | 2012-04-04 16:58:13 -0700 | [diff] [blame] | 1465 | LogUtils.wtf(LOG_TAG, "Loader returned unexpected id: %d", id); |
Mindy Pereira | 5cb0c3e | 2012-02-22 15:22:47 -0800 | [diff] [blame] | 1466 | } |
| 1467 | return null; |
Vikram Aggarwal | 7dedb95 | 2012-02-16 16:10:23 -0800 | [diff] [blame] | 1468 | } |
| 1469 | |
Paul Westbrook | b1f573c | 2012-04-06 11:38:28 -0700 | [diff] [blame] | 1470 | @Override |
| 1471 | public void onLoaderReset(Loader<Cursor> loader) { |
| 1472 | |
| 1473 | } |
| 1474 | |
Andy Huang | f9a7348 | 2012-03-13 15:54:02 -0700 | [diff] [blame] | 1475 | /** |
| 1476 | * {@link LoaderManager} currently has a bug in |
| 1477 | * {@link LoaderManager#restartLoader(int, Bundle, android.app.LoaderManager.LoaderCallbacks)} |
| 1478 | * where, if a previous onCreateLoader returned a null loader, this method will NPE. Work around |
| 1479 | * this bug by destroying any loaders that may have been created as null (essentially because |
| 1480 | * they are optional loads, and may not apply to a particular account). |
| 1481 | * <p> |
| 1482 | * A simple null check before restarting a loader will not work, because that would not |
| 1483 | * give the controller a chance to invalidate UI corresponding the prior loader result. |
| 1484 | * |
| 1485 | * @param id loader ID to safely restart |
Andy Huang | f9a7348 | 2012-03-13 15:54:02 -0700 | [diff] [blame] | 1486 | */ |
Vikram Aggarwal | 94c94de | 2012-04-04 15:38:28 -0700 | [diff] [blame] | 1487 | private void restartOptionalLoader(int id) { |
Andy Huang | f9a7348 | 2012-03-13 15:54:02 -0700 | [diff] [blame] | 1488 | final LoaderManager lm = mActivity.getLoaderManager(); |
| 1489 | lm.destroyLoader(id); |
Vikram Aggarwal | 94c94de | 2012-04-04 15:38:28 -0700 | [diff] [blame] | 1490 | lm.restartLoader(id, Bundle.EMPTY, this); |
| 1491 | } |
| 1492 | |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1493 | @Override |
| 1494 | public void registerConversationListObserver(DataSetObserver observer) { |
| 1495 | mConversationListObservable.registerObserver(observer); |
| 1496 | } |
| 1497 | |
| 1498 | @Override |
| 1499 | public void unregisterConversationListObserver(DataSetObserver observer) { |
| 1500 | mConversationListObservable.unregisterObserver(observer); |
| 1501 | } |
| 1502 | |
Andy Huang | 090db1e | 2012-07-25 13:25:28 -0700 | [diff] [blame] | 1503 | @Override |
| 1504 | public void registerFolderObserver(DataSetObserver observer) { |
| 1505 | mFolderObservable.registerObserver(observer); |
| 1506 | } |
| 1507 | |
| 1508 | @Override |
| 1509 | public void unregisterFolderObserver(DataSetObserver observer) { |
| 1510 | mFolderObservable.unregisterObserver(observer); |
| 1511 | } |
| 1512 | |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 1513 | /** |
| 1514 | * Returns true if the number of accounts is different, or if the current account has been |
| 1515 | * removed from the device |
| 1516 | * @param accountCursor |
| 1517 | * @return |
| 1518 | */ |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 1519 | private boolean accountsUpdated(Cursor accountCursor) { |
| 1520 | // Check to see if the current account hasn't been set, or the account cursor is empty |
| 1521 | if (mAccount == null || !accountCursor.moveToFirst()) { |
Vikram Aggarwal | 6c51158 | 2012-02-27 10:59:47 -0800 | [diff] [blame] | 1522 | return true; |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 1523 | } |
| 1524 | |
| 1525 | // Check to see if the number of accounts are different, from the number we saw on the last |
| 1526 | // updated |
| 1527 | if (mCurrentAccountUris.size() != accountCursor.getCount()) { |
| 1528 | return true; |
| 1529 | } |
| 1530 | |
| 1531 | // Check to see if the account list is different or if the current account is not found in |
| 1532 | // the cursor. |
| 1533 | boolean foundCurrentAccount = false; |
Vikram Aggarwal | 7dedb95 | 2012-02-16 16:10:23 -0800 | [diff] [blame] | 1534 | do { |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 1535 | final Uri accountUri = |
| 1536 | Uri.parse(accountCursor.getString(UIProvider.ACCOUNT_URI_COLUMN)); |
| 1537 | if (!foundCurrentAccount && mAccount.uri.equals(accountUri)) { |
| 1538 | foundCurrentAccount = true; |
| 1539 | } |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 1540 | // Is there a new account that we do not know about? |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 1541 | if (!mCurrentAccountUris.contains(accountUri)) { |
| 1542 | return true; |
| 1543 | } |
Vikram Aggarwal | 7dedb95 | 2012-02-16 16:10:23 -0800 | [diff] [blame] | 1544 | } while (accountCursor.moveToNext()); |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 1545 | |
| 1546 | // As long as we found the current account, the list hasn't been updated |
| 1547 | return !foundCurrentAccount; |
Vikram Aggarwal | 7dedb95 | 2012-02-16 16:10:23 -0800 | [diff] [blame] | 1548 | } |
| 1549 | |
| 1550 | /** |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 1551 | * Updates accounts for the app. If the current account is missing, the first |
| 1552 | * account in the list is set to the current account (we <em>have</em> to choose something). |
Mindy Pereira | 161f50d | 2012-02-28 15:47:19 -0800 | [diff] [blame] | 1553 | * |
Vikram Aggarwal | 6c51158 | 2012-02-27 10:59:47 -0800 | [diff] [blame] | 1554 | * @param accounts cursor into the AccountCache |
Vikram Aggarwal | 7dedb95 | 2012-02-16 16:10:23 -0800 | [diff] [blame] | 1555 | * @return true if the update was successful, false otherwise |
| 1556 | */ |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 1557 | private boolean updateAccounts(Cursor accounts) { |
Vikram Aggarwal | 7dedb95 | 2012-02-16 16:10:23 -0800 | [diff] [blame] | 1558 | if (accounts == null || !accounts.moveToFirst()) { |
| 1559 | return false; |
| 1560 | } |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 1561 | |
Vikram Aggarwal | a9b93f3 | 2012-02-23 14:51:58 -0800 | [diff] [blame] | 1562 | final Account[] allAccounts = Account.getAllAccounts(accounts); |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 1563 | // A match for the current account's URI in the list of accounts. |
| 1564 | Account currentFromList = null; |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 1565 | |
| 1566 | // Save the uris for the accounts |
| 1567 | mCurrentAccountUris.clear(); |
| 1568 | for (Account account : allAccounts) { |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 1569 | LogUtils.d(LOG_TAG, "updateAccounts(%s)", account); |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 1570 | mCurrentAccountUris.add(account.uri); |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 1571 | if (mAccount != null && account.uri.equals(mAccount.uri)) { |
| 1572 | currentFromList = account; |
| 1573 | } |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 1574 | } |
| 1575 | |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 1576 | // 1. current account is already set and is in allAccounts: |
| 1577 | // 1a. It has changed -> load the updated account. |
| 1578 | // 2b. It is unchanged -> no-op |
Andy Huang | 0d64735 | 2012-03-21 21:48:16 -0700 | [diff] [blame] | 1579 | // 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] | 1580 | // 3. saved preference has an account -> pick that one |
Andy Huang | 0d64735 | 2012-03-21 21:48:16 -0700 | [diff] [blame] | 1581 | // 4. otherwise just pick first |
| 1582 | |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 1583 | boolean accountChanged = false; |
| 1584 | /// Assume case 4, initialize to first account, and see if we can find anything better. |
| 1585 | Account newAccount = allAccounts[0]; |
| 1586 | if (currentFromList != null) { |
| 1587 | // Case 1: Current account exists but has changed |
| 1588 | if (!currentFromList.equals(mAccount)) { |
| 1589 | newAccount = currentFromList; |
| 1590 | accountChanged = true; |
Andy Huang | 0d64735 | 2012-03-21 21:48:16 -0700 | [diff] [blame] | 1591 | } |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 1592 | // Case 1b: else, current account is unchanged: nothing to do. |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 1593 | } else { |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 1594 | // Case 2: Current account is not in allAccounts, the account needs to change. |
| 1595 | accountChanged = true; |
| 1596 | if (mAccount == null) { |
| 1597 | // Case 3: Check for last viewed account, and check if it exists in the list. |
| 1598 | final String lastAccountUri = MailAppProvider.getInstance().getLastViewedAccount(); |
| 1599 | if (lastAccountUri != null) { |
| 1600 | for (final Account account : allAccounts) { |
| 1601 | if (lastAccountUri.equals(account.uri.toString())) { |
| 1602 | newAccount = account; |
| 1603 | break; |
| 1604 | } |
Andy Huang | 0d64735 | 2012-03-21 21:48:16 -0700 | [diff] [blame] | 1605 | } |
| 1606 | } |
| 1607 | } |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 1608 | } |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 1609 | if (accountChanged) { |
| 1610 | onAccountChanged(newAccount); |
| 1611 | } |
| 1612 | // Whether we have updated the current account or not, we need to update the list of |
| 1613 | // accounts in the ActionBar. |
Vikram Aggarwal | a9b93f3 | 2012-02-23 14:51:58 -0800 | [diff] [blame] | 1614 | mActionBarView.setAccounts(allAccounts); |
| 1615 | return (allAccounts.length > 0); |
Vikram Aggarwal | 7dedb95 | 2012-02-16 16:10:23 -0800 | [diff] [blame] | 1616 | } |
| 1617 | |
Paul Westbrook | 6ead20d | 2012-03-19 14:48:14 -0700 | [diff] [blame] | 1618 | private void disableNotifications() { |
| 1619 | mNewEmailReceiver.activate(mContext, this); |
| 1620 | } |
| 1621 | |
| 1622 | private void enableNotifications() { |
| 1623 | mNewEmailReceiver.deactivate(); |
| 1624 | } |
| 1625 | |
| 1626 | private void disableNotificationsOnAccountChange(Account account) { |
| 1627 | // If the new mail suppression receiver is activated for a different account, we want to |
| 1628 | // activate it for the new account. |
| 1629 | if (mNewEmailReceiver.activated() && |
| 1630 | !mNewEmailReceiver.notificationsDisabledForAccount(account)) { |
| 1631 | // Deactivate the current receiver, otherwise multiple receivers may be registered. |
| 1632 | mNewEmailReceiver.deactivate(); |
| 1633 | mNewEmailReceiver.activate(mContext, this); |
| 1634 | } |
| 1635 | } |
| 1636 | |
Vikram Aggarwal | a9b93f3 | 2012-02-23 14:51:58 -0800 | [diff] [blame] | 1637 | /** |
| 1638 | * {@inheritDoc} |
| 1639 | */ |
Vikram Aggarwal | 7dedb95 | 2012-02-16 16:10:23 -0800 | [diff] [blame] | 1640 | @Override |
| 1641 | public void onLoadFinished(Loader<Cursor> loader, Cursor data) { |
Mindy Pereira | 5cb0c3e | 2012-02-22 15:22:47 -0800 | [diff] [blame] | 1642 | // We want to reinitialize only if we haven't ever been initialized, or |
| 1643 | // if the current account has vanished. |
Paul Westbrook | e3e8429 | 2012-03-05 16:19:30 -0800 | [diff] [blame] | 1644 | if (data == null) { |
Vikram Aggarwal | fa4b47e | 2012-03-09 13:02:46 -0800 | [diff] [blame] | 1645 | 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] | 1646 | } |
Vikram Aggarwal | fa4b47e | 2012-03-09 13:02:46 -0800 | [diff] [blame] | 1647 | switch (loader.getId()) { |
| 1648 | case LOADER_ACCOUNT_CURSOR: |
Vikram Aggarwal | 27d89ad | 2012-06-12 13:38:40 -0700 | [diff] [blame] | 1649 | // 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] | 1650 | // we need to start the specified activity. |
| 1651 | if (data != null && data.getCount() == 0) { |
| 1652 | // If an empty cursor is returned, the MailAppProvider is indicating that |
| 1653 | // no accounts have been specified. We want to navigate to the "add account" |
| 1654 | // activity that will handle the intent returned by the MailAppProvider |
| 1655 | |
| 1656 | // If the MailAppProvider believes that all accounts have been loaded, and the |
| 1657 | // account list is still empty, we want to prompt the user to add an account |
| 1658 | final Bundle extras = data.getExtras(); |
| 1659 | final boolean accountsLoaded = |
| 1660 | extras.getInt(AccountCursorExtraKeys.ACCOUNTS_LOADED) != 0; |
| 1661 | |
| 1662 | if (accountsLoaded) { |
| 1663 | final Intent noAccountIntent = MailAppProvider.getNoAccountIntent(mContext); |
| 1664 | if (noAccountIntent != null) { |
| 1665 | mActivity.startActivityForResult(noAccountIntent, |
| 1666 | ADD_ACCOUNT_REQUEST_CODE); |
| 1667 | } |
| 1668 | } |
| 1669 | } else { |
| 1670 | final boolean accountListUpdated = accountsUpdated(data); |
| 1671 | if (!isLoaderInitialized || accountListUpdated) { |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 1672 | isLoaderInitialized = updateAccounts(data); |
Paul Westbrook | 2388c5d | 2012-03-25 12:29:11 -0700 | [diff] [blame] | 1673 | } |
Vikram Aggarwal | fa4b47e | 2012-03-09 13:02:46 -0800 | [diff] [blame] | 1674 | } |
| 1675 | break; |
Paul Westbrook | 2d50bcd | 2012-04-10 11:53:47 -0700 | [diff] [blame] | 1676 | case LOADER_ACCOUNT_UPDATE_CURSOR: |
| 1677 | // We have gotten an update for current account. |
| 1678 | |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 1679 | // Make sure that this is an update for the current account |
Paul Westbrook | 2d50bcd | 2012-04-10 11:53:47 -0700 | [diff] [blame] | 1680 | if (data != null && data.moveToFirst()) { |
| 1681 | final Account updatedAccount = new Account(data); |
| 1682 | |
| 1683 | if (updatedAccount.uri.equals(mAccount.uri)) { |
Paul Westbrook | ca08fc1 | 2012-07-31 12:01:15 -0700 | [diff] [blame] | 1684 | // Keep a reference to the previous settings object |
| 1685 | final Settings previousSettings = mAccount.settings; |
| 1686 | |
Vikram Aggarwal | 7d81600 | 2012-04-17 17:06:41 -0700 | [diff] [blame] | 1687 | // Update the controller's reference to the current account |
Paul Westbrook | 2d50bcd | 2012-04-10 11:53:47 -0700 | [diff] [blame] | 1688 | mAccount = updatedAccount; |
Vikram Aggarwal | edc137c | 2012-04-24 13:40:58 -0700 | [diff] [blame] | 1689 | LogUtils.d(LOG_TAG, "AbstractActivityController.onLoadFinished(): " |
| 1690 | + "mAccount = %s", mAccount.uri); |
Paul Westbrook | ca08fc1 | 2012-07-31 12:01:15 -0700 | [diff] [blame] | 1691 | |
| 1692 | // Only notify about a settings change if something differs |
| 1693 | if (!Objects.equal(mAccount.settings, previousSettings)) { |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 1694 | mAccountObservers.notifyChanged(); |
Paul Westbrook | ca08fc1 | 2012-07-31 12:01:15 -0700 | [diff] [blame] | 1695 | } |
Paul Westbrook | 2d50bcd | 2012-04-10 11:53:47 -0700 | [diff] [blame] | 1696 | |
| 1697 | // Got an update for the current account |
| 1698 | final boolean inWaitingMode = inWaitMode(); |
| 1699 | if (!updatedAccount.isAccountIntialized() && !inWaitingMode) { |
| 1700 | // Transition to waiting mode |
| 1701 | showWaitForInitialization(); |
Mindy Pereira | 830bdaf | 2012-07-11 12:59:55 -0700 | [diff] [blame] | 1702 | } else if (updatedAccount.isAccountIntialized()) { |
| 1703 | if (inWaitingMode) { |
| 1704 | // Dismiss waiting mode |
| 1705 | hideWaitForInitialization(); |
| 1706 | } |
Paul Westbrook | 2d50bcd | 2012-04-10 11:53:47 -0700 | [diff] [blame] | 1707 | } else if (!updatedAccount.isAccountIntialized() && inWaitingMode) { |
| 1708 | // Update the WaitFragment's account object |
| 1709 | updateWaitMode(); |
| 1710 | } |
| 1711 | } else { |
| 1712 | LogUtils.e(LOG_TAG, "Got update for account: %s with current account: %s", |
| 1713 | updatedAccount.uri, mAccount.uri); |
| 1714 | // We need to restart the loader, so the correct account information will |
| 1715 | // be returned |
| 1716 | restartOptionalLoader(LOADER_ACCOUNT_UPDATE_CURSOR); |
| 1717 | } |
| 1718 | } |
| 1719 | break; |
Vikram Aggarwal | fa4b47e | 2012-03-09 13:02:46 -0800 | [diff] [blame] | 1720 | case LOADER_FOLDER_CURSOR: |
Vikram Aggarwal | fa4b47e | 2012-03-09 13:02:46 -0800 | [diff] [blame] | 1721 | // Check status of the cursor. |
Marc Blank | fd9d0b8 | 2012-04-23 16:01:51 -0700 | [diff] [blame] | 1722 | if (data != null && data.moveToFirst()) { |
Andy Huang | 090db1e | 2012-07-25 13:25:28 -0700 | [diff] [blame] | 1723 | final Folder folder = new Folder(data); |
Marc Blank | fd9d0b8 | 2012-04-23 16:01:51 -0700 | [diff] [blame] | 1724 | LogUtils.d(LOG_TAG, "FOLDER STATUS = %d", folder.syncStatus); |
Andy Huang | 090db1e | 2012-07-25 13:25:28 -0700 | [diff] [blame] | 1725 | |
| 1726 | mFolder = folder; |
| 1727 | mFolderObservable.notifyChanged(); |
| 1728 | |
Paul Westbrook | c808fac | 2012-02-22 16:42:18 -0800 | [diff] [blame] | 1729 | } else { |
Marc Blank | fd9d0b8 | 2012-04-23 16:01:51 -0700 | [diff] [blame] | 1730 | LogUtils.d(LOG_TAG, "Unable to get the folder %s", |
| 1731 | mFolder != null ? mAccount.name : ""); |
Mindy Pereira | 11dd5ef | 2012-03-10 15:10:18 -0800 | [diff] [blame] | 1732 | } |
Vikram Aggarwal | fa4b47e | 2012-03-09 13:02:46 -0800 | [diff] [blame] | 1733 | break; |
Vikram Aggarwal | fa4b47e | 2012-03-09 13:02:46 -0800 | [diff] [blame] | 1734 | case LOADER_RECENT_FOLDERS: |
Vikram Aggarwal | 27d89ad | 2012-06-12 13:38:40 -0700 | [diff] [blame] | 1735 | // No recent folders and we are running on a phone? Populate the default recents. |
| 1736 | if (data != null && data.getCount() == 0 && !Utils.useTabletUI(mContext)) { |
| 1737 | final class PopulateDefault extends AsyncTask<Uri, Void, Void> { |
| 1738 | @Override |
| 1739 | protected Void doInBackground(Uri... uri) { |
| 1740 | // Asking for an update on the URI and ignore the result. |
| 1741 | final ContentResolver resolver = mContext.getContentResolver(); |
| 1742 | resolver.update(uri[0], null, null, null); |
| 1743 | return null; |
| 1744 | } |
| 1745 | } |
| 1746 | final Uri uri = mAccount.defaultRecentFolderListUri; |
| 1747 | LogUtils.v(LOG_TAG, "Default recents at %s", uri); |
| 1748 | new PopulateDefault().execute(uri); |
| 1749 | break; |
| 1750 | } |
| 1751 | LogUtils.v(LOG_TAG, "Reading recent folders from the cursor."); |
Vikram Aggarwal | fa4b47e | 2012-03-09 13:02:46 -0800 | [diff] [blame] | 1752 | mRecentFolderList.loadFromUiProvider(data); |
Vikram Aggarwal | 3726397 | 2012-04-17 15:51:14 -0700 | [diff] [blame] | 1753 | mActionBarView.requestRecentFoldersAndRedraw(); |
Vikram Aggarwal | fa4b47e | 2012-03-09 13:02:46 -0800 | [diff] [blame] | 1754 | break; |
Mindy Pereira | ab48636 | 2012-03-21 18:18:53 -0700 | [diff] [blame] | 1755 | case LOADER_ACCOUNT_INBOX: |
Marc Blank | fd9d0b8 | 2012-04-23 16:01:51 -0700 | [diff] [blame] | 1756 | if (data != null && !data.isClosed() && data.moveToFirst()) { |
Mindy Pereira | 0e88e9f | 2012-03-25 13:47:41 -0700 | [diff] [blame] | 1757 | Folder inbox = new Folder(data); |
| 1758 | onFolderChanged(inbox); |
Mindy Pereira | b4a4328 | 2012-03-23 16:20:03 -0700 | [diff] [blame] | 1759 | // Just want to get the inbox, don't care about updates to it |
| 1760 | // as this will be tracked by the folder change listener. |
| 1761 | mActivity.getLoaderManager().destroyLoader(LOADER_ACCOUNT_INBOX); |
Mindy Pereira | 5ba3380 | 2012-03-26 16:30:11 -0700 | [diff] [blame] | 1762 | } else { |
| 1763 | LogUtils.d(LOG_TAG, "Unable to get the account inbox for account %s", |
| 1764 | mAccount != null ? mAccount.name : ""); |
Mindy Pereira | b4a4328 | 2012-03-23 16:20:03 -0700 | [diff] [blame] | 1765 | } |
Mindy Pereira | ab48636 | 2012-03-21 18:18:53 -0700 | [diff] [blame] | 1766 | break; |
| 1767 | case LOADER_SEARCH: |
| 1768 | data.moveToFirst(); |
| 1769 | Folder search = new Folder(data); |
Mindy Pereira | 11e3596 | 2012-06-01 14:49:46 -0700 | [diff] [blame] | 1770 | updateFolder(search); |
Mindy Pereira | ab48636 | 2012-03-21 18:18:53 -0700 | [diff] [blame] | 1771 | mConvListContext = ConversationListContext.forSearchQuery(mAccount, mFolder, |
Mindy Pereira | d660d25 | 2012-03-26 11:48:43 -0700 | [diff] [blame] | 1772 | mActivity.getIntent() |
| 1773 | .getStringExtra(UIProvider.SearchQueryParameters.QUERY)); |
Mindy Pereira | ab48636 | 2012-03-21 18:18:53 -0700 | [diff] [blame] | 1774 | showConversationList(mConvListContext); |
| 1775 | mActivity.invalidateOptionsMenu(); |
Mindy Pereira | 3b39922 | 2012-03-28 15:19:47 -0700 | [diff] [blame] | 1776 | mActivity.getLoaderManager().destroyLoader(LOADER_SEARCH); |
Mindy Pereira | ab48636 | 2012-03-21 18:18:53 -0700 | [diff] [blame] | 1777 | break; |
Vikram Aggarwal | 7dedb95 | 2012-02-16 16:10:23 -0800 | [diff] [blame] | 1778 | } |
| 1779 | } |
| 1780 | |
Vikram Aggarwal | c769422 | 2012-04-23 13:37:01 -0700 | [diff] [blame] | 1781 | /** |
| 1782 | * Destructive actions on Conversations. This class should only be created by controllers, and |
| 1783 | * clients should only require {@link DestructiveAction}s, not specific implementations of the. |
| 1784 | * Only the controllers should know what kind of destructive actions are being created. |
| 1785 | */ |
Mindy Pereira | de3e74a | 2012-07-24 09:43:10 -0700 | [diff] [blame] | 1786 | public class ConversationAction implements DestructiveAction { |
Vikram Aggarwal | acaa3c0 | 2012-04-24 12:45:27 -0700 | [diff] [blame] | 1787 | /** |
| 1788 | * The action to be performed. This is specified as the resource ID of the menu item |
| 1789 | * corresponding to this action: R.id.delete, R.id.report_spam, etc. |
| 1790 | */ |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 1791 | private final int mAction; |
Vikram Aggarwal | bc67bb1 | 2012-04-30 14:05:35 -0700 | [diff] [blame] | 1792 | /** The action will act upon these conversations */ |
Paul Westbrook | 77eee62 | 2012-07-10 13:41:57 -0700 | [diff] [blame] | 1793 | private final Collection<Conversation> mTarget; |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 1794 | /** Whether this destructive action has already been performed */ |
| 1795 | private boolean mCompleted; |
Vikram Aggarwal | c411395 | 2012-05-11 14:14:56 -0700 | [diff] [blame] | 1796 | /** Whether this is an action on the currently selected set. */ |
| 1797 | private final boolean mIsSelectedSet; |
Vikram Aggarwal | e8a8532 | 2012-04-24 09:01:38 -0700 | [diff] [blame] | 1798 | |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 1799 | /** |
| 1800 | * Create a listener object. action is one of four constants: R.id.y_button (archive), |
| 1801 | * R.id.delete , R.id.mute, and R.id.report_spam. |
| 1802 | * @param action |
Vikram Aggarwal | bc67bb1 | 2012-04-30 14:05:35 -0700 | [diff] [blame] | 1803 | * @param target Conversation that we want to apply the action to. |
Vikram Aggarwal | c411395 | 2012-05-11 14:14:56 -0700 | [diff] [blame] | 1804 | * @param isBatch whether the conversations are in the currently selected batch set. |
| 1805 | */ |
| 1806 | public ConversationAction(int action, Collection<Conversation> target, boolean isBatch) { |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 1807 | mAction = action; |
Paul Westbrook | 77eee62 | 2012-07-10 13:41:57 -0700 | [diff] [blame] | 1808 | mTarget = ImmutableList.copyOf(target); |
Vikram Aggarwal | c411395 | 2012-05-11 14:14:56 -0700 | [diff] [blame] | 1809 | mIsSelectedSet = isBatch; |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 1810 | } |
| 1811 | |
Vikram Aggarwal | acaa3c0 | 2012-04-24 12:45:27 -0700 | [diff] [blame] | 1812 | /** |
| 1813 | * The action common to child classes. This performs the action specified in the constructor |
| 1814 | * on the conversations given here. |
Vikram Aggarwal | acaa3c0 | 2012-04-24 12:45:27 -0700 | [diff] [blame] | 1815 | */ |
Vikram Aggarwal | bc67bb1 | 2012-04-30 14:05:35 -0700 | [diff] [blame] | 1816 | @Override |
| 1817 | public void performAction() { |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 1818 | if (isPerformed()) { |
| 1819 | return; |
| 1820 | } |
Mindy Pereira | 3cb28b5 | 2012-05-24 15:26:39 -0700 | [diff] [blame] | 1821 | boolean undoEnabled = mAccount.supportsCapability(AccountCapabilities.UNDO); |
Vikram Aggarwal | 7dd054e | 2012-05-21 14:43:10 -0700 | [diff] [blame] | 1822 | |
| 1823 | // Are we destroying the currently shown conversation? Show the next one. |
| 1824 | if (LogUtils.isLoggable(LOG_TAG, LogUtils.DEBUG)){ |
Vikram Aggarwal | 8742a61 | 2012-08-13 10:22:50 -0700 | [diff] [blame] | 1825 | LogUtils.d(LOG_TAG, "ConversationAction.performAction():" |
| 1826 | + "\nmTarget=%s\nCurrent=%s", |
Vikram Aggarwal | 7dd054e | 2012-05-21 14:43:10 -0700 | [diff] [blame] | 1827 | Conversation.toString(mTarget), mCurrentConversation); |
| 1828 | } |
Vikram Aggarwal | 7dd054e | 2012-05-21 14:43:10 -0700 | [diff] [blame] | 1829 | |
Paul Westbrook | e1221d2 | 2012-08-19 11:09:07 -0700 | [diff] [blame] | 1830 | if (mConversationListCursor == null) { |
| 1831 | LogUtils.e(LOG_TAG, "null ConversationCursor in ConversationAction.performAction():" |
| 1832 | + "\nmTarget=%s\nCurrent=%s", |
| 1833 | Conversation.toString(mTarget), mCurrentConversation); |
| 1834 | return; |
| 1835 | } |
| 1836 | |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 1837 | switch (mAction) { |
Mindy Pereira | 0692baf | 2012-03-23 17:34:31 -0700 | [diff] [blame] | 1838 | case R.id.archive: |
Vikram Aggarwal | 7dd054e | 2012-05-21 14:43:10 -0700 | [diff] [blame] | 1839 | LogUtils.d(LOG_TAG, "Archiving"); |
Vikram Aggarwal | bc67bb1 | 2012-04-30 14:05:35 -0700 | [diff] [blame] | 1840 | mConversationListCursor.archive(mContext, mTarget); |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 1841 | break; |
| 1842 | case R.id.delete: |
Vikram Aggarwal | 7dd054e | 2012-05-21 14:43:10 -0700 | [diff] [blame] | 1843 | LogUtils.d(LOG_TAG, "Deleting"); |
Vikram Aggarwal | bc67bb1 | 2012-04-30 14:05:35 -0700 | [diff] [blame] | 1844 | mConversationListCursor.delete(mContext, mTarget); |
Mindy Pereira | 695d696 | 2012-06-18 13:02:10 -0700 | [diff] [blame] | 1845 | if (mFolder.supportsCapability(FolderCapabilities.DELETE_ACTION_FINAL)) { |
Marc Blank | 386243f | 2012-05-25 10:40:59 -0700 | [diff] [blame] | 1846 | undoEnabled = false; |
| 1847 | } |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 1848 | break; |
| 1849 | case R.id.mute: |
Vikram Aggarwal | 7dd054e | 2012-05-21 14:43:10 -0700 | [diff] [blame] | 1850 | LogUtils.d(LOG_TAG, "Muting"); |
Vikram Aggarwal | bc67bb1 | 2012-04-30 14:05:35 -0700 | [diff] [blame] | 1851 | if (mFolder.supportsCapability(FolderCapabilities.DESTRUCTIVE_MUTE)) { |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 1852 | for (Conversation c : mTarget) { |
| 1853 | c.localDeleteOnUpdate = true; |
| 1854 | } |
Vikram Aggarwal | bc67bb1 | 2012-04-30 14:05:35 -0700 | [diff] [blame] | 1855 | } |
| 1856 | mConversationListCursor.mute(mContext, mTarget); |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 1857 | break; |
| 1858 | case R.id.report_spam: |
Vikram Aggarwal | 7dd054e | 2012-05-21 14:43:10 -0700 | [diff] [blame] | 1859 | LogUtils.d(LOG_TAG, "Reporting spam"); |
Vikram Aggarwal | bc67bb1 | 2012-04-30 14:05:35 -0700 | [diff] [blame] | 1860 | mConversationListCursor.reportSpam(mContext, mTarget); |
| 1861 | break; |
Paul Westbrook | 77eee62 | 2012-07-10 13:41:57 -0700 | [diff] [blame] | 1862 | case R.id.mark_not_spam: |
| 1863 | LogUtils.d(LOG_TAG, "Marking not spam"); |
| 1864 | mConversationListCursor.reportNotSpam(mContext, mTarget); |
| 1865 | break; |
Paul Westbrook | 76b2062 | 2012-07-12 11:45:43 -0700 | [diff] [blame] | 1866 | case R.id.report_phishing: |
| 1867 | LogUtils.d(LOG_TAG, "Reporting phishing"); |
| 1868 | mConversationListCursor.reportPhishing(mContext, mTarget); |
| 1869 | break; |
Vikram Aggarwal | bc67bb1 | 2012-04-30 14:05:35 -0700 | [diff] [blame] | 1870 | case R.id.remove_star: |
Vikram Aggarwal | 7dd054e | 2012-05-21 14:43:10 -0700 | [diff] [blame] | 1871 | LogUtils.d(LOG_TAG, "Removing star"); |
Vikram Aggarwal | bc67bb1 | 2012-04-30 14:05:35 -0700 | [diff] [blame] | 1872 | // Star removal is destructive in the Starred folder. |
| 1873 | mConversationListCursor.updateBoolean(mContext, mTarget, |
| 1874 | ConversationColumns.STARRED, false); |
| 1875 | break; |
| 1876 | case R.id.mark_not_important: |
Vikram Aggarwal | 7dd054e | 2012-05-21 14:43:10 -0700 | [diff] [blame] | 1877 | LogUtils.d(LOG_TAG, "Marking not-important"); |
Mindy Pereira | 445be21 | 2012-08-15 08:50:10 -0700 | [diff] [blame] | 1878 | // Marking not important is destructive in a mailbox |
| 1879 | // containing only important messages |
Mindy Pereira | 0d03ef8 | 2012-08-15 09:05:48 -0700 | [diff] [blame] | 1880 | if (mFolder != null && mFolder.isImportantOnly()) { |
Mindy Pereira | 445be21 | 2012-08-15 08:50:10 -0700 | [diff] [blame] | 1881 | for (Conversation conv : mTarget) { |
| 1882 | conv.localDeleteOnUpdate = true; |
| 1883 | } |
| 1884 | } |
Vikram Aggarwal | bc67bb1 | 2012-04-30 14:05:35 -0700 | [diff] [blame] | 1885 | mConversationListCursor.updateInt(mContext, mTarget, |
| 1886 | ConversationColumns.PRIORITY, UIProvider.ConversationPriority.LOW); |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 1887 | break; |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 1888 | } |
| 1889 | if (undoEnabled) { |
mindyp | ead5039 | 2012-08-23 11:03:53 -0700 | [diff] [blame] | 1890 | mHandler.postDelayed(new Runnable() { |
| 1891 | @Override |
| 1892 | public void run() { |
| 1893 | onUndoAvailable(new ToastBarOperation(mTarget.size(), mAction, |
| 1894 | ToastBarOperation.UNDO)); |
| 1895 | } |
| 1896 | }, mShowUndoBarDelay); |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 1897 | } |
Vikram Aggarwal | bc67bb1 | 2012-04-30 14:05:35 -0700 | [diff] [blame] | 1898 | refreshConversationList(); |
Vikram Aggarwal | c411395 | 2012-05-11 14:14:56 -0700 | [diff] [blame] | 1899 | if (mIsSelectedSet) { |
Vikram Aggarwal | c411395 | 2012-05-11 14:14:56 -0700 | [diff] [blame] | 1900 | mSelectedSet.clear(); |
| 1901 | } |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 1902 | } |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 1903 | |
| 1904 | /** |
| 1905 | * Returns true if this action has been performed, false otherwise. |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 1906 | * |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 1907 | */ |
| 1908 | private synchronized boolean isPerformed() { |
| 1909 | if (mCompleted) { |
| 1910 | return true; |
| 1911 | } |
| 1912 | mCompleted = true; |
| 1913 | return false; |
| 1914 | } |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 1915 | } |
Mindy Pereira | e5f4dc0 | 2012-03-21 16:08:53 -0700 | [diff] [blame] | 1916 | |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 1917 | /** |
| 1918 | * Get a destructive action for a menu action. |
| 1919 | * This is a temporary method, to control the profusion of {@link DestructiveAction} classes |
| 1920 | * that are created. Please do not copy this paradigm. |
| 1921 | * @param action the resource ID of the menu action: R.id.delete, for example |
| 1922 | * @param target the conversations to act upon. |
| 1923 | * @return a {@link DestructiveAction} that performs the specified action. |
| 1924 | */ |
Vikram Aggarwal | 531488e | 2012-05-29 16:36:52 -0700 | [diff] [blame] | 1925 | private final DestructiveAction getAction(int action, Collection<Conversation> target) { |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 1926 | final DestructiveAction da = new ConversationAction(action, target, false); |
| 1927 | registerDestructiveAction(da); |
| 1928 | return da; |
| 1929 | } |
| 1930 | |
Vikram Aggarwal | d503df4 | 2012-05-11 10:13:35 -0700 | [diff] [blame] | 1931 | // Called from the FolderSelectionDialog after a user is done selecting folders to assign the |
| 1932 | // conversations to. |
Mindy Pereira | e5f4dc0 | 2012-03-21 16:08:53 -0700 | [diff] [blame] | 1933 | @Override |
Mindy Pereira | 8db7e40 | 2012-07-13 10:32:47 -0700 | [diff] [blame] | 1934 | public final void assignFolder(Collection<FolderOperation> folderOps, |
| 1935 | Collection<Conversation> target, boolean batch, boolean showUndo) { |
| 1936 | // Actions are destructive only when the current folder can be assigned |
| 1937 | // to (which is the same as being able to un-assign a conversation from the folder) and |
| 1938 | // when the list of folders contains the current folder. |
| 1939 | final boolean isDestructive = mFolder |
| 1940 | .supportsCapability(FolderCapabilities.CAN_ACCEPT_MOVED_MESSAGES) |
| 1941 | && FolderOperation.isDestructive(folderOps, mFolder); |
Vikram Aggarwal | d503df4 | 2012-05-11 10:13:35 -0700 | [diff] [blame] | 1942 | LogUtils.d(LOG_TAG, "onFolderChangesCommit: isDestructive = %b", isDestructive); |
| 1943 | if (isDestructive) { |
| 1944 | for (final Conversation c : target) { |
| 1945 | c.localDeleteOnUpdate = true; |
Mindy Pereira | 6778f46 | 2012-03-23 18:01:55 -0700 | [diff] [blame] | 1946 | } |
Mindy Pereira | e5f4dc0 | 2012-03-21 16:08:53 -0700 | [diff] [blame] | 1947 | } |
Mindy Pereira | 8db7e40 | 2012-07-13 10:32:47 -0700 | [diff] [blame] | 1948 | final DestructiveAction folderChange = getFolderChange(target, folderOps, isDestructive, |
Mindy Pereira | 06642fa | 2012-07-12 16:23:27 -0700 | [diff] [blame] | 1949 | batch, showUndo); |
Vikram Aggarwal | d503df4 | 2012-05-11 10:13:35 -0700 | [diff] [blame] | 1950 | // Update the UI elements depending no their visibility and availability |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 1951 | // TODO(viki): Consolidate this into a single method requestDelete. |
Vikram Aggarwal | d503df4 | 2012-05-11 10:13:35 -0700 | [diff] [blame] | 1952 | if (isDestructive) { |
Vikram Aggarwal | 4f4782b | 2012-05-30 08:39:09 -0700 | [diff] [blame] | 1953 | delete(target, folderChange); |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 1954 | } else { |
Vikram Aggarwal | d503df4 | 2012-05-11 10:13:35 -0700 | [diff] [blame] | 1955 | requestUpdate(target, folderChange); |
Mindy Pereira | e5f4dc0 | 2012-03-21 16:08:53 -0700 | [diff] [blame] | 1956 | } |
| 1957 | } |
| 1958 | |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 1959 | @Override |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 1960 | public final void onRefreshRequired() { |
mindyp | 5390fca | 2012-08-22 12:12:25 -0700 | [diff] [blame] | 1961 | if (isAnimating() || isDragging()) { |
Mindy Pereira | 69e88dd | 2012-08-10 09:30:18 -0700 | [diff] [blame] | 1962 | LogUtils.d(LOG_TAG, "onRefreshRequired: delay until animating done"); |
| 1963 | return; |
| 1964 | } |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 1965 | // Refresh the query in the background |
Paul Westbrook | cff1aea | 2012-08-10 11:51:00 -0700 | [diff] [blame] | 1966 | if (mConversationListCursor.isRefreshRequired()) { |
| 1967 | mConversationListCursor.refresh(); |
| 1968 | } |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 1969 | } |
| 1970 | |
mindyp | 5390fca | 2012-08-22 12:12:25 -0700 | [diff] [blame] | 1971 | @Override |
| 1972 | public void startDragMode() { |
| 1973 | mIsDragHappening = true; |
| 1974 | } |
| 1975 | |
| 1976 | @Override |
| 1977 | public void stopDragMode() { |
| 1978 | mIsDragHappening = false; |
| 1979 | if (mConversationListCursor.isRefreshReady()) { |
| 1980 | LogUtils.d(LOG_TAG, "Stopped animating: try sync"); |
| 1981 | onRefreshReady(); |
| 1982 | } |
| 1983 | |
| 1984 | if (mConversationListCursor.isRefreshRequired()) { |
| 1985 | LogUtils.d(LOG_TAG, "Stopped animating: refresh"); |
| 1986 | mConversationListCursor.refresh(); |
| 1987 | } |
| 1988 | } |
| 1989 | |
| 1990 | private boolean isDragging() { |
| 1991 | return mIsDragHappening; |
| 1992 | } |
| 1993 | |
Mindy Pereira | 69e88dd | 2012-08-10 09:30:18 -0700 | [diff] [blame] | 1994 | private boolean isAnimating() { |
| 1995 | boolean isAnimating = false; |
| 1996 | ConversationListFragment convListFragment = getConversationListFragment(); |
| 1997 | if (convListFragment != null) { |
| 1998 | AnimatedAdapter adapter = convListFragment.getAnimatedAdapter(); |
| 1999 | if (adapter != null) { |
| 2000 | isAnimating = adapter.isAnimating(); |
| 2001 | } |
| 2002 | } |
| 2003 | return isAnimating; |
| 2004 | } |
| 2005 | |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 2006 | /** |
| 2007 | * Called when the {@link ConversationCursor} is changed or has new data in it. |
| 2008 | * <p> |
| 2009 | * {@inheritDoc} |
| 2010 | */ |
| 2011 | @Override |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 2012 | public final void onRefreshReady() { |
Paul Westbrook | cff1aea | 2012-08-10 11:51:00 -0700 | [diff] [blame] | 2013 | if (!isAnimating()) { |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 2014 | // Swap cursors |
| 2015 | mConversationListCursor.sync(); |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 2016 | } |
Paul Westbrook | 937c94f | 2012-08-16 13:01:18 -0700 | [diff] [blame] | 2017 | mTracker.onCursorUpdated(); |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 2018 | } |
| 2019 | |
| 2020 | @Override |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 2021 | public final void onDataSetChanged() { |
Paul Westbrook | 9f119c7 | 2012-04-24 16:10:59 -0700 | [diff] [blame] | 2022 | updateConversationListFragment(); |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 2023 | mConversationListObservable.notifyChanged(); |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 2024 | } |
| 2025 | |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 2026 | /** |
| 2027 | * If the Conversation List Fragment is visible, updates the fragment. |
| 2028 | */ |
| 2029 | private final void updateConversationListFragment() { |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 2030 | final ConversationListFragment convList = getConversationListFragment(); |
| 2031 | if (convList != null) { |
Vikram Aggarwal | 75daee5 | 2012-04-30 11:13:09 -0700 | [diff] [blame] | 2032 | refreshConversationList(); |
Paul Westbrook | 9f119c7 | 2012-04-24 16:10:59 -0700 | [diff] [blame] | 2033 | if (convList.isVisible()) { |
Paul Westbrook | 9f119c7 | 2012-04-24 16:10:59 -0700 | [diff] [blame] | 2034 | Utils.setConversationCursorVisibility(mConversationListCursor, true); |
| 2035 | } |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 2036 | } |
| 2037 | } |
| 2038 | |
| 2039 | /** |
| 2040 | * This class handles throttled refresh of the conversation list |
| 2041 | */ |
| 2042 | static class RefreshTimerTask extends TimerTask { |
| 2043 | final Handler mHandler; |
| 2044 | final AbstractActivityController mController; |
| 2045 | |
| 2046 | RefreshTimerTask(AbstractActivityController controller, Handler handler) { |
| 2047 | mHandler = handler; |
| 2048 | mController = controller; |
| 2049 | } |
| 2050 | |
| 2051 | @Override |
| 2052 | public void run() { |
| 2053 | mHandler.post(new Runnable() { |
| 2054 | @Override |
| 2055 | public void run() { |
| 2056 | LogUtils.d(LOG_TAG, "Delay done... calling onRefreshRequired"); |
| 2057 | mController.onRefreshRequired(); |
| 2058 | }}); |
| 2059 | } |
| 2060 | } |
| 2061 | |
| 2062 | /** |
| 2063 | * Cancel the refresh task, if it's running |
| 2064 | */ |
| 2065 | private void cancelRefreshTask () { |
| 2066 | if (mConversationListRefreshTask != null) { |
| 2067 | mConversationListRefreshTask.cancel(); |
| 2068 | mConversationListRefreshTask = null; |
| 2069 | } |
| 2070 | } |
| 2071 | |
| 2072 | @Override |
Paul Westbrook | cff1aea | 2012-08-10 11:51:00 -0700 | [diff] [blame] | 2073 | public void onAnimationEnd(AnimatedAdapter animatedAdapter) { |
| 2074 | if (mConversationListCursor.isRefreshReady()) { |
mindyp | 5254486 | 2012-08-20 12:05:36 -0700 | [diff] [blame] | 2075 | LogUtils.d(LOG_TAG, "Stopped animating: try sync"); |
Paul Westbrook | cff1aea | 2012-08-10 11:51:00 -0700 | [diff] [blame] | 2076 | onRefreshReady(); |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 2077 | } |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 2078 | |
Paul Westbrook | cff1aea | 2012-08-10 11:51:00 -0700 | [diff] [blame] | 2079 | if (mConversationListCursor.isRefreshRequired()) { |
mindyp | 5254486 | 2012-08-20 12:05:36 -0700 | [diff] [blame] | 2080 | LogUtils.d(LOG_TAG, "Stopped animating: refresh"); |
Paul Westbrook | cff1aea | 2012-08-10 11:51:00 -0700 | [diff] [blame] | 2081 | mConversationListCursor.refresh(); |
| 2082 | } |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 2083 | } |
| 2084 | |
| 2085 | @Override |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 2086 | public void onSetEmpty() { |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 2087 | } |
| 2088 | |
| 2089 | @Override |
| 2090 | public void onSetPopulated(ConversationSelectionSet set) { |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 2091 | final ConversationListFragment convList = getConversationListFragment(); |
| 2092 | if (convList == null) { |
| 2093 | return; |
| 2094 | } |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 2095 | mCabActionMenu = new SelectedConversationsActionMenu(mActivity, set, mFolder, |
Vikram Aggarwal | 531488e | 2012-05-29 16:36:52 -0700 | [diff] [blame] | 2096 | (SwipeableListView) convList.getListView()); |
Vikram Aggarwal | e128fc2 | 2012-04-04 12:33:34 -0700 | [diff] [blame] | 2097 | enableCabMode(); |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 2098 | } |
| 2099 | |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 2100 | @Override |
| 2101 | public void onSetChanged(ConversationSelectionSet set) { |
| 2102 | // Do nothing. We don't care about changes to the set. |
| 2103 | } |
| 2104 | |
| 2105 | @Override |
| 2106 | public ConversationSelectionSet getSelectedSet() { |
| 2107 | return mSelectedSet; |
| 2108 | } |
| 2109 | |
Vikram Aggarwal | e128fc2 | 2012-04-04 12:33:34 -0700 | [diff] [blame] | 2110 | /** |
| 2111 | * Disable the Contextual Action Bar (CAB). The selected set is not changed. |
| 2112 | */ |
| 2113 | protected void disableCabMode() { |
Mindy Pereira | 8937bf1 | 2012-07-23 14:05:02 -0700 | [diff] [blame] | 2114 | // Commit any previous destructive actions when entering/ exiting CAB mode. |
mindyp | c6adce3 | 2012-08-22 18:46:42 -0700 | [diff] [blame] | 2115 | commitDestructiveActions(true); |
Vikram Aggarwal | e128fc2 | 2012-04-04 12:33:34 -0700 | [diff] [blame] | 2116 | if (mCabActionMenu != null) { |
| 2117 | mCabActionMenu.deactivate(); |
| 2118 | } |
| 2119 | } |
| 2120 | |
| 2121 | /** |
| 2122 | * Re-enable the CAB menu if required. The selection set is not changed. |
| 2123 | */ |
| 2124 | protected void enableCabMode() { |
Mindy Pereira | 8937bf1 | 2012-07-23 14:05:02 -0700 | [diff] [blame] | 2125 | // Commit any previous destructive actions when entering/ exiting CAB mode. |
mindyp | c6adce3 | 2012-08-22 18:46:42 -0700 | [diff] [blame] | 2126 | commitDestructiveActions(true); |
Vikram Aggarwal | e128fc2 | 2012-04-04 12:33:34 -0700 | [diff] [blame] | 2127 | if (mCabActionMenu != null) { |
| 2128 | mCabActionMenu.activate(); |
| 2129 | } |
| 2130 | } |
| 2131 | |
Vikram Aggarwal | 4eb5271 | 2012-06-19 16:24:50 -0700 | [diff] [blame] | 2132 | /** |
| 2133 | * Unselect conversations and exit CAB mode. |
| 2134 | */ |
| 2135 | protected final void exitCabMode() { |
| 2136 | mSelectedSet.clear(); |
| 2137 | } |
| 2138 | |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 2139 | @Override |
Mindy Pereira | fd0c297 | 2012-03-27 13:50:39 -0700 | [diff] [blame] | 2140 | public void startSearch() { |
Vikram Aggarwal | 35f19d7 | 2012-04-24 13:24:48 -0700 | [diff] [blame] | 2141 | if (mAccount == null) { |
| 2142 | // We cannot search if there is no account. Drop the request to the floor. |
| 2143 | LogUtils.d(LOG_TAG, "AbstractActivityController.startSearch(): null account"); |
| 2144 | return; |
| 2145 | } |
Mindy Pereira | fd0c297 | 2012-03-27 13:50:39 -0700 | [diff] [blame] | 2146 | if (mAccount.supportsCapability(UIProvider.AccountCapabilities.LOCAL_SEARCH) |
| 2147 | | mAccount.supportsCapability(UIProvider.AccountCapabilities.SERVER_SEARCH)) { |
Vikram Aggarwal | 70f130e | 2012-04-03 12:32:14 -0700 | [diff] [blame] | 2148 | onSearchRequested(mActionBarView.getQuery()); |
Mindy Pereira | fd0c297 | 2012-03-27 13:50:39 -0700 | [diff] [blame] | 2149 | } else { |
| 2150 | Toast.makeText(mActivity.getActivityContext(), mActivity.getActivityContext() |
Mindy Pereira | a46c299 | 2012-03-27 14:12:39 -0700 | [diff] [blame] | 2151 | .getString(R.string.search_unsupported), Toast.LENGTH_SHORT).show(); |
Mindy Pereira | fd0c297 | 2012-03-27 13:50:39 -0700 | [diff] [blame] | 2152 | } |
| 2153 | } |
Mindy Pereira | acf6039 | 2012-04-06 09:11:00 -0700 | [diff] [blame] | 2154 | |
Vikram Aggarwal | 0dda573 | 2012-04-06 11:20:16 -0700 | [diff] [blame] | 2155 | @Override |
| 2156 | public void exitSearchMode() { |
| 2157 | if (mViewMode.getMode() == ViewMode.SEARCH_RESULTS_LIST) { |
| 2158 | mActivity.finish(); |
| 2159 | } |
| 2160 | } |
| 2161 | |
Mindy Pereira | acf6039 | 2012-04-06 09:11:00 -0700 | [diff] [blame] | 2162 | /** |
| 2163 | * Supports dragging conversations to a folder. |
| 2164 | */ |
| 2165 | @Override |
| 2166 | public boolean supportsDrag(DragEvent event, Folder folder) { |
| 2167 | return (folder != null |
| 2168 | && event != null |
| 2169 | && event.getClipDescription() != null |
| 2170 | && folder.supportsCapability |
| 2171 | (UIProvider.FolderCapabilities.CAN_ACCEPT_MOVED_MESSAGES) |
| 2172 | && folder.supportsCapability |
| 2173 | (UIProvider.FolderCapabilities.CAN_HOLD_MAIL) |
| 2174 | && !mFolder.uri.equals(folder.uri)); |
| 2175 | } |
| 2176 | |
| 2177 | /** |
Mindy Pereira | 6c2663d | 2012-07-20 15:37:29 -0700 | [diff] [blame] | 2178 | * Handles dropping conversations to a folder. |
Mindy Pereira | acf6039 | 2012-04-06 09:11:00 -0700 | [diff] [blame] | 2179 | */ |
| 2180 | @Override |
| 2181 | public void handleDrop(DragEvent event, final Folder folder) { |
Mindy Pereira | acf6039 | 2012-04-06 09:11:00 -0700 | [diff] [blame] | 2182 | if (!supportsDrag(event, folder)) { |
| 2183 | return; |
| 2184 | } |
Mindy Pereira | acf6039 | 2012-04-06 09:11:00 -0700 | [diff] [blame] | 2185 | final Collection<Conversation> conversations = mSelectedSet.values(); |
Mindy Pereira | 8db7e40 | 2012-07-13 10:32:47 -0700 | [diff] [blame] | 2186 | final Collection<FolderOperation> dropTarget = FolderOperation.listOf(new FolderOperation( |
| 2187 | folder, true)); |
| 2188 | // Drag and drop is destructive: we remove conversations from the |
| 2189 | // current folder. |
Mindy Pereira | 06642fa | 2012-07-12 16:23:27 -0700 | [diff] [blame] | 2190 | final DestructiveAction action = getFolderChange(conversations, dropTarget, true, true, |
| 2191 | true); |
Vikram Aggarwal | 4f4782b | 2012-05-30 08:39:09 -0700 | [diff] [blame] | 2192 | delete(conversations, action); |
Mindy Pereira | acf6039 | 2012-04-06 09:11:00 -0700 | [diff] [blame] | 2193 | } |
Mindy Pereira | 0963ef8 | 2012-04-10 11:43:01 -0700 | [diff] [blame] | 2194 | |
| 2195 | @Override |
Mindy Pereira | 0963ef8 | 2012-04-10 11:43:01 -0700 | [diff] [blame] | 2196 | public void onTouchEvent(MotionEvent event) { |
| 2197 | if (event.getAction() == MotionEvent.ACTION_DOWN) { |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 2198 | if (mToastBar != null && !mToastBar.isEventInToastBar(event)) { |
| 2199 | mToastBar.hide(true); |
Mindy Pereira | 0963ef8 | 2012-04-10 11:43:01 -0700 | [diff] [blame] | 2200 | } |
| 2201 | } |
| 2202 | } |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 2203 | |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 2204 | @Override |
| 2205 | public void onConversationSeen(Conversation conv) { |
| 2206 | mPagerController.onConversationSeen(conv); |
| 2207 | } |
| 2208 | |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 2209 | private class ConversationListLoaderCallbacks implements |
| 2210 | LoaderManager.LoaderCallbacks<ConversationCursor> { |
| 2211 | |
| 2212 | @Override |
| 2213 | public Loader<ConversationCursor> onCreateLoader(int id, Bundle args) { |
| 2214 | Loader<ConversationCursor> result = new ConversationCursorLoader((Activity) mActivity, |
Paul Westbrook | 9a70e91 | 2012-08-17 15:53:20 -0700 | [diff] [blame] | 2215 | mAccount, mFolder.conversationListUri, mFolder.name); |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 2216 | return result; |
| 2217 | } |
| 2218 | |
| 2219 | @Override |
| 2220 | public void onLoadFinished(Loader<ConversationCursor> loader, ConversationCursor data) { |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 2221 | LogUtils.d(LOG_TAG, "IN AAC.ConversationCursor.onLoadFinished, data=%s loader=%s", |
| 2222 | data, loader); |
Vikram Aggarwal | e8a8532 | 2012-04-24 09:01:38 -0700 | [diff] [blame] | 2223 | // Clear our all pending destructive actions before swapping the conversation cursor |
| 2224 | destroyPending(null); |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 2225 | mConversationListCursor = data; |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 2226 | mConversationListCursor.addListener(AbstractActivityController.this); |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 2227 | |
Paul Westbrook | 937c94f | 2012-08-16 13:01:18 -0700 | [diff] [blame] | 2228 | mTracker.onCursorUpdated(); |
| 2229 | |
Andy Huang | e3df1ad | 2012-04-24 17:15:23 -0700 | [diff] [blame] | 2230 | mConversationListObservable.notifyChanged(); |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 2231 | // Register the AbstractActivityController as a listener to changes in |
| 2232 | // data in the cursor. |
| 2233 | final ConversationListFragment convList = getConversationListFragment(); |
| 2234 | if (convList != null) { |
| 2235 | convList.onCursorUpdated(); |
Paul Westbrook | 9f119c7 | 2012-04-24 16:10:59 -0700 | [diff] [blame] | 2236 | |
| 2237 | if (convList.isVisible()) { |
| 2238 | // The conversation list is visible. |
| 2239 | Utils.setConversationCursorVisibility(mConversationListCursor, true); |
| 2240 | } |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 2241 | } |
| 2242 | // Shown for search results in two-pane mode only. |
| 2243 | if (shouldShowFirstConversation()) { |
| 2244 | if (mConversationListCursor.getCount() > 0) { |
| 2245 | mConversationListCursor.moveToPosition(0); |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 2246 | final Conversation conv = new Conversation(mConversationListCursor); |
| 2247 | conv.position = 0; |
| 2248 | onConversationSelected(conv); |
| 2249 | } |
| 2250 | } |
| 2251 | } |
| 2252 | |
| 2253 | @Override |
| 2254 | public void onLoaderReset(Loader<ConversationCursor> loader) { |
Paul Westbrook | 9a70e91 | 2012-08-17 15:53:20 -0700 | [diff] [blame] | 2255 | LogUtils.d(LOG_TAG, "IN AAC.ConversationCursor.onLoaderReset, data=%s loader=%s", |
| 2256 | mConversationListCursor, loader); |
| 2257 | |
| 2258 | if (mConversationListCursor != null) { |
| 2259 | // Unregister the listener |
| 2260 | mConversationListCursor.removeListener(AbstractActivityController.this); |
| 2261 | mConversationListCursor = null; |
| 2262 | |
| 2263 | // Inform anyone who is interested about the change |
| 2264 | mTracker.onCursorUpdated(); |
| 2265 | mConversationListObservable.notifyChanged(); |
| 2266 | |
| 2267 | final ConversationListFragment convList = getConversationListFragment(); |
| 2268 | if (convList != null) { |
| 2269 | convList.onCursorUpdated(); |
| 2270 | } |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 2271 | } |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 2272 | } |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 2273 | } |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 2274 | |
Vikram Aggarwal | e8a8532 | 2012-04-24 09:01:38 -0700 | [diff] [blame] | 2275 | /** |
| 2276 | * Destroy the pending {@link DestructiveAction} till now and assign the given action as the |
| 2277 | * next destructive action.. |
| 2278 | * @param nextAction the next destructive action to be performed. This can be null. |
| 2279 | */ |
| 2280 | private final void destroyPending(DestructiveAction nextAction) { |
| 2281 | // If there is a pending action, perform that first. |
| 2282 | if (mPendingDestruction != null) { |
| 2283 | mPendingDestruction.performAction(); |
| 2284 | } |
| 2285 | mPendingDestruction = nextAction; |
| 2286 | } |
| 2287 | |
| 2288 | /** |
| 2289 | * Register a destructive action with the controller. This performs the previous destructive |
Vikram Aggarwal | acaa3c0 | 2012-04-24 12:45:27 -0700 | [diff] [blame] | 2290 | * 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] | 2291 | * embellish this method any more. |
Vikram Aggarwal | e8a8532 | 2012-04-24 09:01:38 -0700 | [diff] [blame] | 2292 | * @param action |
| 2293 | */ |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 2294 | private final void registerDestructiveAction(DestructiveAction action) { |
Vikram Aggarwal | e8a8532 | 2012-04-24 09:01:38 -0700 | [diff] [blame] | 2295 | // TODO(viki): This is not a good idea. The best solution is for clients to request a |
| 2296 | // destructive action from the controller and for the controller to own the action. This is |
| 2297 | // a half-way solution while refactoring DestructiveAction. |
| 2298 | destroyPending(action); |
| 2299 | return; |
| 2300 | } |
| 2301 | |
Vikram Aggarwal | 531488e | 2012-05-29 16:36:52 -0700 | [diff] [blame] | 2302 | @Override |
| 2303 | public final DestructiveAction getBatchAction(int action) { |
Vikram Aggarwal | c411395 | 2012-05-11 14:14:56 -0700 | [diff] [blame] | 2304 | final DestructiveAction da = new ConversationAction(action, mSelectedSet.values(), true); |
Vikram Aggarwal | e8a8532 | 2012-04-24 09:01:38 -0700 | [diff] [blame] | 2305 | registerDestructiveAction(da); |
| 2306 | return da; |
| 2307 | } |
Vikram Aggarwal | 41e6e71 | 2012-04-24 11:22:57 -0700 | [diff] [blame] | 2308 | |
Mindy Pereira | de3e74a | 2012-07-24 09:43:10 -0700 | [diff] [blame] | 2309 | @Override |
| 2310 | public final DestructiveAction getDeferredBatchAction(int action) { |
| 2311 | final DestructiveAction da = new ConversationAction(action, mSelectedSet.values(), true); |
| 2312 | return da; |
| 2313 | } |
| 2314 | |
Vikram Aggarwal | bc67bb1 | 2012-04-30 14:05:35 -0700 | [diff] [blame] | 2315 | /** |
| 2316 | * Class to change the folders that are assigned to a set of conversations. This is destructive |
| 2317 | * because the user can remove the current folder from the conversation, in which case it has |
| 2318 | * to be animated away from the current folder. |
| 2319 | */ |
Vikram Aggarwal | 41e6e71 | 2012-04-24 11:22:57 -0700 | [diff] [blame] | 2320 | private class FolderDestruction implements DestructiveAction { |
Paul Westbrook | 77eee62 | 2012-07-10 13:41:57 -0700 | [diff] [blame] | 2321 | private final Collection<Conversation> mTarget; |
Mindy Pereira | 8db7e40 | 2012-07-13 10:32:47 -0700 | [diff] [blame] | 2322 | private final ArrayList<FolderOperation> mFolderOps = new ArrayList<FolderOperation>(); |
Vikram Aggarwal | 41e6e71 | 2012-04-24 11:22:57 -0700 | [diff] [blame] | 2323 | private final boolean mIsDestructive; |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 2324 | /** Whether this destructive action has already been performed */ |
| 2325 | private boolean mCompleted; |
Mindy Pereira | f3a4556 | 2012-05-24 16:30:19 -0700 | [diff] [blame] | 2326 | private boolean mIsSelectedSet; |
Mindy Pereira | 06642fa | 2012-07-12 16:23:27 -0700 | [diff] [blame] | 2327 | private boolean mShowUndo; |
Mindy Pereira | 01f3050 | 2012-08-14 10:30:51 -0700 | [diff] [blame] | 2328 | private int mAction; |
Vikram Aggarwal | 41e6e71 | 2012-04-24 11:22:57 -0700 | [diff] [blame] | 2329 | |
| 2330 | /** |
| 2331 | * Create a new folder destruction object to act on the given conversations. |
| 2332 | * @param target |
| 2333 | */ |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 2334 | private FolderDestruction(final Collection<Conversation> target, |
Mindy Pereira | 8db7e40 | 2012-07-13 10:32:47 -0700 | [diff] [blame] | 2335 | final Collection<FolderOperation> folders, boolean isDestructive, boolean isBatch, |
Mindy Pereira | 01f3050 | 2012-08-14 10:30:51 -0700 | [diff] [blame] | 2336 | boolean showUndo, int action) { |
Paul Westbrook | 77eee62 | 2012-07-10 13:41:57 -0700 | [diff] [blame] | 2337 | mTarget = ImmutableList.copyOf(target); |
Mindy Pereira | 8db7e40 | 2012-07-13 10:32:47 -0700 | [diff] [blame] | 2338 | mFolderOps.addAll(folders); |
Vikram Aggarwal | 41e6e71 | 2012-04-24 11:22:57 -0700 | [diff] [blame] | 2339 | mIsDestructive = isDestructive; |
Mindy Pereira | f3a4556 | 2012-05-24 16:30:19 -0700 | [diff] [blame] | 2340 | mIsSelectedSet = isBatch; |
Mindy Pereira | 06642fa | 2012-07-12 16:23:27 -0700 | [diff] [blame] | 2341 | mShowUndo = showUndo; |
Mindy Pereira | 01f3050 | 2012-08-14 10:30:51 -0700 | [diff] [blame] | 2342 | mAction = action; |
Vikram Aggarwal | 41e6e71 | 2012-04-24 11:22:57 -0700 | [diff] [blame] | 2343 | } |
| 2344 | |
Vikram Aggarwal | 41e6e71 | 2012-04-24 11:22:57 -0700 | [diff] [blame] | 2345 | @Override |
| 2346 | public void performAction() { |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 2347 | if (isPerformed()) { |
| 2348 | return; |
| 2349 | } |
Mindy Pereira | 06642fa | 2012-07-12 16:23:27 -0700 | [diff] [blame] | 2350 | if (mIsDestructive && mShowUndo) { |
Mindy Pereira | d3367499 | 2012-06-25 16:26:30 -0700 | [diff] [blame] | 2351 | ToastBarOperation undoOp = new ToastBarOperation(mTarget.size(), |
Mindy Pereira | 01f3050 | 2012-08-14 10:30:51 -0700 | [diff] [blame] | 2352 | mAction, ToastBarOperation.UNDO); |
Vikram Aggarwal | 41e6e71 | 2012-04-24 11:22:57 -0700 | [diff] [blame] | 2353 | onUndoAvailable(undoOp); |
| 2354 | } |
Mindy Pereira | 8db7e40 | 2012-07-13 10:32:47 -0700 | [diff] [blame] | 2355 | // For each conversation, for each operation, add/ remove the |
| 2356 | // appropriate folders. |
| 2357 | for (Conversation target : mTarget) { |
| 2358 | HashMap<Uri, Folder> targetFolders = Folder |
Mindy Pereira | 68f8384 | 2012-07-27 09:43:31 -0700 | [diff] [blame] | 2359 | .hashMapForFolders(target.getRawFolders()); |
Mindy Pereira | 01f3050 | 2012-08-14 10:30:51 -0700 | [diff] [blame] | 2360 | if (mIsDestructive) { |
| 2361 | target.localDeleteOnUpdate = true; |
| 2362 | } |
Mindy Pereira | 8db7e40 | 2012-07-13 10:32:47 -0700 | [diff] [blame] | 2363 | for (FolderOperation op : mFolderOps) { |
| 2364 | if (op.mAdd) { |
| 2365 | targetFolders.put(op.mFolder.uri, op.mFolder); |
| 2366 | } else { |
| 2367 | targetFolders.remove(op.mFolder.uri); |
| 2368 | } |
| 2369 | } |
Mindy Pereira | 85c4a77 | 2012-07-30 10:47:26 -0700 | [diff] [blame] | 2370 | target.setRawFolders(Folder.getSerializedFolderString(targetFolders.values())); |
Mindy Pereira | 00ffece | 2012-07-27 08:49:56 -0700 | [diff] [blame] | 2371 | mConversationListCursor.updateString(mContext, Conversation.listOf(target), |
Mindy Pereira | 85c4a77 | 2012-07-30 10:47:26 -0700 | [diff] [blame] | 2372 | Conversation.UPDATE_FOLDER_COLUMN, target.getRawFoldersString()); |
Mindy Pereira | 8db7e40 | 2012-07-13 10:32:47 -0700 | [diff] [blame] | 2373 | } |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 2374 | refreshConversationList(); |
Mindy Pereira | f3a4556 | 2012-05-24 16:30:19 -0700 | [diff] [blame] | 2375 | if (mIsSelectedSet) { |
| 2376 | mSelectedSet.clear(); |
| 2377 | } |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 2378 | } |
Mindy Pereira | 8db7e40 | 2012-07-13 10:32:47 -0700 | [diff] [blame] | 2379 | |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 2380 | /** |
| 2381 | * Returns true if this action has been performed, false otherwise. |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 2382 | * |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 2383 | */ |
| 2384 | private synchronized boolean isPerformed() { |
| 2385 | if (mCompleted) { |
| 2386 | return true; |
Vikram Aggarwal | 41e6e71 | 2012-04-24 11:22:57 -0700 | [diff] [blame] | 2387 | } |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 2388 | mCompleted = true; |
| 2389 | return false; |
Vikram Aggarwal | 41e6e71 | 2012-04-24 11:22:57 -0700 | [diff] [blame] | 2390 | } |
| 2391 | } |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 2392 | |
Vikram Aggarwal | d503df4 | 2012-05-11 10:13:35 -0700 | [diff] [blame] | 2393 | private final DestructiveAction getFolderChange(Collection<Conversation> target, |
Mindy Pereira | 8db7e40 | 2012-07-13 10:32:47 -0700 | [diff] [blame] | 2394 | Collection<FolderOperation> folders, boolean isDestructive, boolean isBatch, |
| 2395 | boolean showUndo) { |
Mindy Pereira | 06642fa | 2012-07-12 16:23:27 -0700 | [diff] [blame] | 2396 | final DestructiveAction da = new FolderDestruction(target, folders, isDestructive, isBatch, |
Mindy Pereira | 01f3050 | 2012-08-14 10:30:51 -0700 | [diff] [blame] | 2397 | showUndo, R.id.change_folder); |
| 2398 | registerDestructiveAction(da); |
| 2399 | return da; |
| 2400 | } |
| 2401 | |
| 2402 | @Override |
| 2403 | public final DestructiveAction getDeferredRemoveFolder(Collection<Conversation> target, |
| 2404 | Folder toRemove, boolean isDestructive, boolean isBatch, |
| 2405 | boolean showUndo) { |
| 2406 | Collection<FolderOperation> folderOps = new ArrayList<FolderOperation>(); |
| 2407 | folderOps.add(new FolderOperation(toRemove, false)); |
| 2408 | return new FolderDestruction(target, folderOps, isDestructive, isBatch, |
| 2409 | showUndo, R.id.remove_folder); |
| 2410 | } |
| 2411 | |
| 2412 | private final DestructiveAction getRemoveFolder(Collection<Conversation> target, |
| 2413 | Folder toRemove, boolean isDestructive, boolean isBatch, |
| 2414 | boolean showUndo) { |
| 2415 | Collection<FolderOperation> folderOps = new ArrayList<FolderOperation>(); |
| 2416 | folderOps.add(new FolderOperation(toRemove, false)); |
| 2417 | DestructiveAction da = new FolderDestruction(target, folderOps, isDestructive, isBatch, |
| 2418 | showUndo, R.id.remove_folder); |
Vikram Aggarwal | 41e6e71 | 2012-04-24 11:22:57 -0700 | [diff] [blame] | 2419 | registerDestructiveAction(da); |
| 2420 | return da; |
| 2421 | } |
Vikram Aggarwal | 75daee5 | 2012-04-30 11:13:09 -0700 | [diff] [blame] | 2422 | |
Vikram Aggarwal | 4f4782b | 2012-05-30 08:39:09 -0700 | [diff] [blame] | 2423 | @Override |
| 2424 | public final void refreshConversationList() { |
Vikram Aggarwal | 75daee5 | 2012-04-30 11:13:09 -0700 | [diff] [blame] | 2425 | final ConversationListFragment convList = getConversationListFragment(); |
| 2426 | if (convList == null) { |
| 2427 | return; |
| 2428 | } |
| 2429 | convList.requestListRefresh(); |
| 2430 | } |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 2431 | |
| 2432 | protected final ActionClickedListener getUndoClickedListener( |
| 2433 | final AnimatedAdapter listAdapter) { |
| 2434 | return new ActionClickedListener() { |
| 2435 | @Override |
| 2436 | public void onActionClicked() { |
| 2437 | if (mAccount.undoUri != null) { |
| 2438 | // NOTE: We might want undo to return the messages affected, in which case |
| 2439 | // the resulting cursor might be interesting... |
| 2440 | // TODO: Use UIProvider.SEQUENCE_QUERY_PARAMETER to indicate the set of |
| 2441 | // commands to undo |
| 2442 | if (mConversationListCursor != null) { |
| 2443 | mConversationListCursor.undo( |
| 2444 | mActivity.getActivityContext(), mAccount.undoUri); |
| 2445 | } |
| 2446 | if (listAdapter != null) { |
| 2447 | listAdapter.setUndo(true); |
| 2448 | } |
| 2449 | } |
| 2450 | } |
| 2451 | }; |
| 2452 | } |
| 2453 | |
Andrew Sapperstein | 9d7519d | 2012-07-16 14:03:53 -0700 | [diff] [blame] | 2454 | protected final void showErrorToast(final Folder folder, boolean replaceVisibleToast) { |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 2455 | mToastBar.setConversationMode(false); |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 2456 | |
| 2457 | ActionClickedListener listener = null; |
| 2458 | int actionTextResourceId; |
| 2459 | final int lastSyncResult = folder.lastSyncResult; |
| 2460 | switch (lastSyncResult) { |
| 2461 | case UIProvider.LastSyncResult.CONNECTION_ERROR: |
| 2462 | listener = getRetryClickedListener(folder); |
| 2463 | actionTextResourceId = R.string.retry; |
| 2464 | break; |
| 2465 | case UIProvider.LastSyncResult.AUTH_ERROR: |
| 2466 | listener = getSignInClickedListener(); |
| 2467 | actionTextResourceId = R.string.signin; |
| 2468 | break; |
| 2469 | case UIProvider.LastSyncResult.SECURITY_ERROR: |
| 2470 | return; // Currently we do nothing for security errors. |
| 2471 | case UIProvider.LastSyncResult.STORAGE_ERROR: |
| 2472 | listener = getStorageErrorClickedListener(); |
| 2473 | actionTextResourceId = R.string.info; |
| 2474 | break; |
| 2475 | case UIProvider.LastSyncResult.INTERNAL_ERROR: |
| 2476 | listener = getInternalErrorClickedListener(); |
| 2477 | actionTextResourceId = R.string.report; |
| 2478 | break; |
| 2479 | default: |
| 2480 | return; |
| 2481 | } |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 2482 | mToastBar.show( |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 2483 | listener, |
Andrew Sapperstein | 5d42096 | 2012-07-12 16:43:10 -0700 | [diff] [blame] | 2484 | R.drawable.ic_alert_white, |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 2485 | Utils.getSyncStatusText(mActivity.getActivityContext(), |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 2486 | lastSyncResult), |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 2487 | false, /* showActionIcon */ |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 2488 | actionTextResourceId, |
Andrew Sapperstein | 9d7519d | 2012-07-16 14:03:53 -0700 | [diff] [blame] | 2489 | replaceVisibleToast, |
| 2490 | new ToastBarOperation(1, 0, ToastBarOperation.ERROR)); |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 2491 | } |
| 2492 | |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 2493 | private ActionClickedListener getRetryClickedListener(final Folder folder) { |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 2494 | return new ActionClickedListener() { |
| 2495 | @Override |
| 2496 | public void onActionClicked() { |
| 2497 | final Uri uri = folder.refreshUri; |
| 2498 | |
| 2499 | if (uri != null) { |
Paul Westbrook | 4969e0c | 2012-08-20 14:38:39 -0700 | [diff] [blame] | 2500 | startAsyncRefreshTask(uri); |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 2501 | } |
| 2502 | } |
| 2503 | }; |
| 2504 | } |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 2505 | |
| 2506 | private ActionClickedListener getSignInClickedListener() { |
| 2507 | return new ActionClickedListener() { |
| 2508 | @Override |
| 2509 | public void onActionClicked() { |
Paul Westbrook | 122f7c2 | 2012-08-20 17:50:31 -0700 | [diff] [blame] | 2510 | promptUserForAuthentication(mAccount); |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 2511 | } |
| 2512 | }; |
| 2513 | } |
| 2514 | |
| 2515 | private ActionClickedListener getStorageErrorClickedListener() { |
| 2516 | return new ActionClickedListener() { |
| 2517 | @Override |
| 2518 | public void onActionClicked() { |
Paul Westbrook | 4969e0c | 2012-08-20 14:38:39 -0700 | [diff] [blame] | 2519 | showStorageErrorDialog(); |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 2520 | } |
| 2521 | }; |
| 2522 | } |
| 2523 | |
Paul Westbrook | 4969e0c | 2012-08-20 14:38:39 -0700 | [diff] [blame] | 2524 | private void showStorageErrorDialog() { |
| 2525 | DialogFragment fragment = (DialogFragment) |
| 2526 | mFragmentManager.findFragmentByTag(SYNC_ERROR_DIALOG_FRAGMENT_TAG); |
| 2527 | if (fragment == null) { |
| 2528 | fragment = SyncErrorDialogFragment.newInstance(); |
| 2529 | } |
| 2530 | fragment.show(mFragmentManager, SYNC_ERROR_DIALOG_FRAGMENT_TAG); |
| 2531 | } |
| 2532 | |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 2533 | private ActionClickedListener getInternalErrorClickedListener() { |
| 2534 | return new ActionClickedListener() { |
| 2535 | @Override |
| 2536 | public void onActionClicked() { |
Paul Westbrook | 17beb0b | 2012-08-20 13:34:37 -0700 | [diff] [blame] | 2537 | Utils.sendFeedback( |
| 2538 | mActivity.getActivityContext(), mAccount, true /* reportingProblem */); |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 2539 | } |
| 2540 | }; |
| 2541 | } |
Paul Westbrook | 4969e0c | 2012-08-20 14:38:39 -0700 | [diff] [blame] | 2542 | |
| 2543 | @Override |
| 2544 | public void onFooterViewErrorActionClick(Folder folder, int errorStatus) { |
| 2545 | Uri uri = null; |
| 2546 | switch (errorStatus) { |
| 2547 | case UIProvider.LastSyncResult.CONNECTION_ERROR: |
| 2548 | if (folder != null && folder.refreshUri != null) { |
| 2549 | uri = folder.refreshUri; |
| 2550 | } |
| 2551 | break; |
| 2552 | case UIProvider.LastSyncResult.AUTH_ERROR: |
Paul Westbrook | 122f7c2 | 2012-08-20 17:50:31 -0700 | [diff] [blame] | 2553 | promptUserForAuthentication(mAccount); |
Paul Westbrook | 4969e0c | 2012-08-20 14:38:39 -0700 | [diff] [blame] | 2554 | return; |
| 2555 | case UIProvider.LastSyncResult.SECURITY_ERROR: |
| 2556 | return; // Currently we do nothing for security errors. |
| 2557 | case UIProvider.LastSyncResult.STORAGE_ERROR: |
| 2558 | showStorageErrorDialog(); |
| 2559 | return; |
| 2560 | case UIProvider.LastSyncResult.INTERNAL_ERROR: |
| 2561 | Utils.sendFeedback( |
| 2562 | mActivity.getActivityContext(), mAccount, true /* reportingProblem */); |
| 2563 | return; |
| 2564 | default: |
| 2565 | return; |
| 2566 | } |
| 2567 | |
| 2568 | if (uri != null) { |
| 2569 | startAsyncRefreshTask(uri); |
| 2570 | } |
| 2571 | } |
| 2572 | |
| 2573 | @Override |
| 2574 | public void onFooterViewLoadMoreClick(Folder folder) { |
| 2575 | if (folder != null && folder.loadMoreUri != null) { |
| 2576 | startAsyncRefreshTask(folder.loadMoreUri); |
| 2577 | } |
| 2578 | } |
| 2579 | |
| 2580 | private void startAsyncRefreshTask(Uri uri) { |
| 2581 | if (mFolderSyncTask != null) { |
| 2582 | mFolderSyncTask.cancel(true); |
| 2583 | } |
| 2584 | mFolderSyncTask = new AsyncRefreshTask(mActivity.getActivityContext(), uri); |
| 2585 | mFolderSyncTask.execute(); |
| 2586 | } |
Paul Westbrook | 122f7c2 | 2012-08-20 17:50:31 -0700 | [diff] [blame] | 2587 | |
| 2588 | private void promptUserForAuthentication(Account account) { |
| 2589 | if (account != null && account.reauthenticationIntentUri != null) { |
| 2590 | final Intent authenticationIntent = |
| 2591 | new Intent(Intent.ACTION_VIEW, account.reauthenticationIntentUri); |
| 2592 | mActivity.startActivityForResult(authenticationIntent, REAUTHENTICATE_REQUEST_CODE); |
| 2593 | } |
| 2594 | } |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 2595 | } |