Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * Copyright (C) 2012 Google Inc. |
| 3 | * Licensed to The Android Open Source Project. |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | *******************************************************************************/ |
| 17 | |
| 18 | package com.android.mail.ui; |
| 19 | |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 20 | import android.app.ActionBar; |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 21 | import android.app.ActionBar.LayoutParams; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 22 | import android.app.Activity; |
Vikram Aggarwal | 54452ae | 2012-03-13 15:29:00 -0700 | [diff] [blame] | 23 | import android.app.AlertDialog; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 24 | import android.app.Dialog; |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 25 | import android.app.DialogFragment; |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 26 | import android.app.Fragment; |
Paul Westbrook | 2d50bcd | 2012-04-10 11:53:47 -0700 | [diff] [blame] | 27 | import android.app.FragmentManager; |
Andy Huang | f9a7348 | 2012-03-13 15:54:02 -0700 | [diff] [blame] | 28 | import android.app.LoaderManager; |
Vikram Aggarwal | e620a7a | 2012-03-28 13:16:14 -0700 | [diff] [blame] | 29 | import android.app.SearchManager; |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 30 | import android.content.ContentProviderOperation; |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 31 | import android.content.ContentResolver; |
Mindy Pereira | 6c2663d | 2012-07-20 15:37:29 -0700 | [diff] [blame] | 32 | import android.content.ContentValues; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 33 | import android.content.Context; |
Vikram Aggarwal | 7dedb95 | 2012-02-16 16:10:23 -0800 | [diff] [blame] | 34 | import android.content.CursorLoader; |
Vikram Aggarwal | 54452ae | 2012-03-13 15:29:00 -0700 | [diff] [blame] | 35 | import android.content.DialogInterface; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 36 | import android.content.Intent; |
Vikram Aggarwal | 7dedb95 | 2012-02-16 16:10:23 -0800 | [diff] [blame] | 37 | import android.content.Loader; |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 38 | import android.database.Cursor; |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 39 | import android.database.DataSetObservable; |
| 40 | import android.database.DataSetObserver; |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 41 | import android.net.Uri; |
Vikram Aggarwal | 27d89ad | 2012-06-12 13:38:40 -0700 | [diff] [blame] | 42 | import android.os.AsyncTask; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 43 | import android.os.Bundle; |
Mindy Pereira | 21ab490 | 2012-03-19 18:48:03 -0700 | [diff] [blame] | 44 | import android.os.Handler; |
Vikram Aggarwal | e620a7a | 2012-03-28 13:16:14 -0700 | [diff] [blame] | 45 | import android.provider.SearchRecentSuggestions; |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame] | 46 | import android.text.TextUtils; |
Mindy Pereira | acf6039 | 2012-04-06 09:11:00 -0700 | [diff] [blame] | 47 | import android.view.DragEvent; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 48 | import android.view.KeyEvent; |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 49 | import android.view.LayoutInflater; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 50 | import android.view.Menu; |
Mindy Pereira | 28d5f72 | 2012-02-15 12:32:40 -0800 | [diff] [blame] | 51 | import android.view.MenuInflater; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 52 | import android.view.MenuItem; |
| 53 | import android.view.MotionEvent; |
Mindy Pereira | d3367499 | 2012-06-25 16:26:30 -0700 | [diff] [blame] | 54 | import android.view.View; |
Mindy Pereira | fd0c297 | 2012-03-27 13:50:39 -0700 | [diff] [blame] | 55 | import android.widget.Toast; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 56 | |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 57 | import com.android.mail.ConversationListContext; |
Andy Huang | f9a7348 | 2012-03-13 15:54:02 -0700 | [diff] [blame] | 58 | import com.android.mail.R; |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 59 | import com.android.mail.browse.ConversationCursor; |
mindyp | 84f7d32 | 2012-10-01 17:14:40 -0700 | [diff] [blame] | 60 | import com.android.mail.browse.ConversationItemView; |
mindyp | ca87de4 | 2012-09-28 15:02:39 -0700 | [diff] [blame] | 61 | import com.android.mail.browse.ConversationItemViewModel; |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 62 | import com.android.mail.browse.ConversationPagerController; |
Vikram Aggarwal | cc754b1 | 2012-08-30 14:04:21 -0700 | [diff] [blame] | 63 | import com.android.mail.browse.ConversationCursor.ConversationOperation; |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 64 | import com.android.mail.browse.MessageCursor.ConversationMessage; |
Marc Blank | 7c9f6ac | 2012-04-02 13:27:19 -0700 | [diff] [blame] | 65 | import com.android.mail.browse.SelectedConversationsActionMenu; |
Andy Huang | 991f453 | 2012-08-14 13:32:55 -0700 | [diff] [blame] | 66 | import com.android.mail.browse.SyncErrorDialogFragment; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 67 | import com.android.mail.compose.ComposeActivity; |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 68 | import com.android.mail.providers.Account; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 69 | import com.android.mail.providers.Conversation; |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 70 | import com.android.mail.providers.ConversationInfo; |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 71 | import com.android.mail.providers.Folder; |
Vikram Aggarwal | 7a5d95a | 2012-07-27 16:24:54 -0700 | [diff] [blame] | 72 | import com.android.mail.providers.FolderWatcher; |
Paul Westbrook | c2074c4 | 2012-03-22 15:26:58 -0700 | [diff] [blame] | 73 | import com.android.mail.providers.MailAppProvider; |
Mindy Pereira | dac0054 | 2012-03-01 10:50:33 -0800 | [diff] [blame] | 74 | import com.android.mail.providers.Settings; |
Vikram Aggarwal | e620a7a | 2012-03-28 13:16:14 -0700 | [diff] [blame] | 75 | import com.android.mail.providers.SuggestionsProvider; |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 76 | import com.android.mail.providers.UIProvider; |
Mindy Pereira | 3cb28b5 | 2012-05-24 15:26:39 -0700 | [diff] [blame] | 77 | import com.android.mail.providers.UIProvider.AccountCapabilities; |
Scott Kennedy | 0d0f8b0 | 2012-10-12 15:18:18 -0700 | [diff] [blame] | 78 | import com.android.mail.providers.UIProvider.AccountColumns; |
Paul Westbrook | 2388c5d | 2012-03-25 12:29:11 -0700 | [diff] [blame] | 79 | import com.android.mail.providers.UIProvider.AccountCursorExtraKeys; |
Scott Kennedy | 0d0f8b0 | 2012-10-12 15:18:18 -0700 | [diff] [blame] | 80 | import com.android.mail.providers.UIProvider.AutoAdvance; |
Mindy Pereira | c9d5918 | 2012-03-22 16:06:46 -0700 | [diff] [blame] | 81 | import com.android.mail.providers.UIProvider.ConversationColumns; |
Paul Westbrook | 5109c51 | 2012-11-05 11:00:30 -0800 | [diff] [blame] | 82 | import com.android.mail.providers.UIProvider.ConversationOperations; |
Vikram Aggarwal | 54452ae | 2012-03-13 15:29:00 -0700 | [diff] [blame] | 83 | import com.android.mail.providers.UIProvider.FolderCapabilities; |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 84 | import com.android.mail.ui.ActionableToastBar.ActionClickedListener; |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 85 | import com.android.mail.utils.ContentProviderTask; |
Paul Westbrook | b334c90 | 2012-06-25 11:42:46 -0700 | [diff] [blame] | 86 | import com.android.mail.utils.LogTag; |
Mindy Pereira | 5cb0c3e | 2012-02-22 15:22:47 -0800 | [diff] [blame] | 87 | import com.android.mail.utils.LogUtils; |
Vikram Aggarwal | fa131a2 | 2012-02-02 13:56:22 -0800 | [diff] [blame] | 88 | import com.android.mail.utils.Utils; |
Scott Kennedy | 0d0f8b0 | 2012-10-12 15:18:18 -0700 | [diff] [blame] | 89 | |
Paul Westbrook | ca08fc1 | 2012-07-31 12:01:15 -0700 | [diff] [blame] | 90 | import com.google.common.base.Objects; |
Paul Westbrook | 77eee62 | 2012-07-10 13:41:57 -0700 | [diff] [blame] | 91 | import com.google.common.collect.ImmutableList; |
Mindy Pereira | acf6039 | 2012-04-06 09:11:00 -0700 | [diff] [blame] | 92 | import com.google.common.collect.Lists; |
Marc Blank | 167faa8 | 2012-03-21 13:11:53 -0700 | [diff] [blame] | 93 | import com.google.common.collect.Sets; |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 94 | |
Marc Blank | 167faa8 | 2012-03-21 13:11:53 -0700 | [diff] [blame] | 95 | import java.util.ArrayList; |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 96 | import java.util.Collection; |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 97 | import java.util.Collections; |
Mindy Pereira | 8db7e40 | 2012-07-13 10:32:47 -0700 | [diff] [blame] | 98 | import java.util.HashMap; |
Vikram Aggarwal | cc754b1 | 2012-08-30 14:04:21 -0700 | [diff] [blame] | 99 | import java.util.List; |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 100 | import java.util.Set; |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 101 | import java.util.TimerTask; |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 102 | |
| 103 | |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 104 | /** |
Mindy Pereira | 161f50d | 2012-02-28 15:47:19 -0800 | [diff] [blame] | 105 | * This is an abstract implementation of the Activity Controller. This class |
| 106 | * knows how to respond to menu items, state changes, layout changes, etc. It |
| 107 | * weaves together the views and listeners, dispatching actions to the |
| 108 | * respective underlying classes. |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 109 | * <p> |
Mindy Pereira | 161f50d | 2012-02-28 15:47:19 -0800 | [diff] [blame] | 110 | * Even though this class is abstract, it should provide default implementations |
| 111 | * for most, if not all the methods in the ActivityController interface. This |
| 112 | * makes the task of the subclasses easier: OnePaneActivityController and |
| 113 | * TwoPaneActivityController can be concise when the common functionality is in |
| 114 | * AbstractActivityController. |
| 115 | * </p> |
| 116 | * <p> |
| 117 | * In the Gmail codebase, this was called BaseActivityController |
| 118 | * </p> |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 119 | */ |
Vikram Aggarwal | 531488e | 2012-05-29 16:36:52 -0700 | [diff] [blame] | 120 | public abstract class AbstractActivityController implements ActivityController { |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 121 | // Keys for serialization of various information in Bundles. |
Vikram Aggarwal | cabd3f2 | 2012-04-19 10:14:41 -0700 | [diff] [blame] | 122 | /** Tag for {@link #mAccount} */ |
Vikram Aggarwal | 6c51158 | 2012-02-27 10:59:47 -0800 | [diff] [blame] | 123 | private static final String SAVED_ACCOUNT = "saved-account"; |
Vikram Aggarwal | cabd3f2 | 2012-04-19 10:14:41 -0700 | [diff] [blame] | 124 | /** Tag for {@link #mFolder} */ |
Mindy Pereira | 5e478d2 | 2012-03-26 18:04:58 -0700 | [diff] [blame] | 125 | private static final String SAVED_FOLDER = "saved-folder"; |
Vikram Aggarwal | cabd3f2 | 2012-04-19 10:14:41 -0700 | [diff] [blame] | 126 | /** Tag for {@link #mCurrentConversation} */ |
Mindy Pereira | 26f23fc | 2012-03-27 10:26:04 -0700 | [diff] [blame] | 127 | private static final String SAVED_CONVERSATION = "saved-conversation"; |
Vikram Aggarwal | cabd3f2 | 2012-04-19 10:14:41 -0700 | [diff] [blame] | 128 | /** Tag for {@link #mSelectedSet} */ |
| 129 | private static final String SAVED_SELECTED_SET = "saved-selected-set"; |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame] | 130 | /** Tag for {@link ActionableToastBar#getOperation()} */ |
Mindy Pereira | d3367499 | 2012-06-25 16:26:30 -0700 | [diff] [blame] | 131 | private static final String SAVED_TOAST_BAR_OP = "saved-toast-bar-op"; |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame] | 132 | /** Tag for {@link #mFolderListFolder} */ |
| 133 | private static final String SAVED_HIERARCHICAL_FOLDER = "saved-hierarchical-folder"; |
Vikram Aggarwal | f334140 | 2012-08-07 10:09:38 -0700 | [diff] [blame] | 134 | /** Tag for {@link ConversationListContext#searchQuery} */ |
| 135 | private static final String SAVED_QUERY = "saved-query"; |
Mindy Pereira | 5cb0c3e | 2012-02-22 15:22:47 -0800 | [diff] [blame] | 136 | |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 137 | /** Tag used when loading a wait fragment */ |
| 138 | protected static final String TAG_WAIT = "wait-fragment"; |
| 139 | /** Tag used when loading a conversation list fragment. */ |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 140 | public static final String TAG_CONVERSATION_LIST = "tag-conversation-list"; |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 141 | /** Tag used when loading a folder list fragment. */ |
| 142 | protected static final String TAG_FOLDER_LIST = "tag-folder-list"; |
| 143 | |
Vikram Aggarwal | d7a12cd | 2012-02-03 09:36:20 -0800 | [diff] [blame] | 144 | protected Account mAccount; |
Mindy Pereira | 12a4d80 | 2012-06-22 09:24:43 -0700 | [diff] [blame] | 145 | protected Folder mFolder; |
Vikram Aggarwal | 69b5c30 | 2012-09-05 11:11:13 -0700 | [diff] [blame] | 146 | /** True when {@link #mFolder} is first shown to the user. */ |
| 147 | private boolean mFolderChanged = false; |
Andy Huang | 6681e54 | 2012-06-14 14:36:45 -0700 | [diff] [blame] | 148 | protected MailActionBarView mActionBarView; |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 149 | protected final ControllableActivity mActivity; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 150 | protected final Context mContext; |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 151 | private final FragmentManager mFragmentManager; |
Vikram Aggarwal | ec5cbf7 | 2012-03-08 15:10:35 -0800 | [diff] [blame] | 152 | protected final RecentFolderList mRecentFolderList; |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 153 | protected ConversationListContext mConvListContext; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 154 | protected Conversation mCurrentConversation; |
Vikram Aggarwal | d7a12cd | 2012-02-03 09:36:20 -0800 | [diff] [blame] | 155 | |
Paul Westbrook | 6ead20d | 2012-03-19 14:48:14 -0700 | [diff] [blame] | 156 | /** A {@link android.content.BroadcastReceiver} that suppresses new e-mail notifications. */ |
| 157 | private SuppressNotificationReceiver mNewEmailReceiver = null; |
| 158 | |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 159 | protected Handler mHandler = new Handler(); |
Mindy Pereira | fa995b4 | 2012-07-25 12:06:13 -0700 | [diff] [blame] | 160 | |
Vikram Aggarwal | fa131a2 | 2012-02-02 13:56:22 -0800 | [diff] [blame] | 161 | /** |
Mindy Pereira | 161f50d | 2012-02-28 15:47:19 -0800 | [diff] [blame] | 162 | * The current mode of the application. All changes in mode are initiated by |
| 163 | * the activity controller. View mode changes are propagated to classes that |
| 164 | * attach themselves as listeners of view mode changes. |
Vikram Aggarwal | fa131a2 | 2012-02-02 13:56:22 -0800 | [diff] [blame] | 165 | */ |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 166 | protected final ViewMode mViewMode; |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 167 | protected ContentResolver mResolver; |
Vikram Aggarwal | 7dedb95 | 2012-02-16 16:10:23 -0800 | [diff] [blame] | 168 | protected boolean isLoaderInitialized = false; |
Mindy Pereira | b7b33e0 | 2012-02-21 15:32:19 -0800 | [diff] [blame] | 169 | private AsyncRefreshTask mAsyncRefreshTask; |
Mindy Pereira | 5cb0c3e | 2012-02-22 15:22:47 -0800 | [diff] [blame] | 170 | |
Andy Huang | 4e0158f | 2012-08-07 21:06:01 -0700 | [diff] [blame] | 171 | private boolean mDestroyed; |
| 172 | |
Andy Huang | 1ee96b2 | 2012-08-24 20:19:53 -0700 | [diff] [blame] | 173 | /** |
| 174 | * Are we in a point in the Activity/Fragment lifecycle where it's safe to execute fragment |
| 175 | * transactions? (including back stack manipulation) |
| 176 | * <p> |
| 177 | * Per docs in {@link FragmentManager#beginTransaction()}, this flag starts out true, switches |
| 178 | * to false after {@link Activity#onSaveInstanceState}, and becomes true again in both onStart |
| 179 | * and onResume. |
| 180 | */ |
| 181 | private boolean mSafeToModifyFragments = true; |
| 182 | |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 183 | private final Set<Uri> mCurrentAccountUris = Sets.newHashSet(); |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 184 | protected ConversationCursor mConversationListCursor; |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 185 | private final DataSetObservable mConversationListObservable = new DataSetObservable() { |
| 186 | @Override |
| 187 | public void registerObserver(DataSetObserver observer) { |
| 188 | final int count = mObservers.size(); |
| 189 | super.registerObserver(observer); |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 190 | 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] | 191 | count, mObservers.size()); |
| 192 | } |
| 193 | @Override |
| 194 | public void unregisterObserver(DataSetObserver observer) { |
| 195 | final int count = mObservers.size(); |
| 196 | super.unregisterObserver(observer); |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 197 | 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] | 198 | count, mObservers.size()); |
| 199 | } |
| 200 | }; |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 201 | |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 202 | private RefreshTimerTask mConversationListRefreshTask; |
Marc Blank | e1d1b07 | 2012-04-13 17:29:16 -0700 | [diff] [blame] | 203 | |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 204 | /** Listeners that are interested in changes to the current account. */ |
| 205 | private final DataSetObservable mAccountObservers = new DataSetObservable() { |
| 206 | @Override |
| 207 | public void registerObserver(DataSetObserver observer) { |
| 208 | final int count = mObservers.size(); |
| 209 | super.registerObserver(observer); |
| 210 | LogUtils.d(LOG_TAG, "IN AAC.register(Account)Observer: %s before=%d after=%d", |
| 211 | observer, count, mObservers.size()); |
| 212 | } |
| 213 | @Override |
| 214 | public void unregisterObserver(DataSetObserver observer) { |
| 215 | final int count = mObservers.size(); |
| 216 | super.unregisterObserver(observer); |
| 217 | LogUtils.d(LOG_TAG, "IN AAC.unregister(Account)Observer: %s before=%d after=%d", |
| 218 | observer, count, mObservers.size()); |
| 219 | } |
| 220 | }; |
Vikram Aggarwal | 7d81600 | 2012-04-17 17:06:41 -0700 | [diff] [blame] | 221 | |
Vikram Aggarwal | 58cad2e | 2012-08-28 16:18:23 -0700 | [diff] [blame] | 222 | /** Listeners that are interested in changes to the recent folders. */ |
| 223 | private final DataSetObservable mRecentFolderObservers = new DataSetObservable() { |
| 224 | @Override |
| 225 | public void registerObserver(DataSetObserver observer) { |
| 226 | final int count = mObservers.size(); |
| 227 | super.registerObserver(observer); |
| 228 | LogUtils.d(LOG_TAG, "IN AAC.register(RecentFolder)Observer: %s before=%d after=%d", |
| 229 | observer, count, mObservers.size()); |
| 230 | } |
| 231 | @Override |
| 232 | public void unregisterObserver(DataSetObserver observer) { |
| 233 | final int count = mObservers.size(); |
| 234 | super.unregisterObserver(observer); |
| 235 | LogUtils.d(LOG_TAG, "IN AAC.unregister(RecentFolder)Observer: %s before=%d after=%d", |
| 236 | observer, count, mObservers.size()); |
| 237 | } |
| 238 | }; |
| 239 | |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 240 | /** |
| 241 | * Selected conversations, if any. |
| 242 | */ |
Andy Huang | 4556a44 | 2012-03-30 16:42:05 -0700 | [diff] [blame] | 243 | private final ConversationSelectionSet mSelectedSet = new ConversationSelectionSet(); |
Mindy Pereira | dac0054 | 2012-03-01 10:50:33 -0800 | [diff] [blame] | 244 | |
Paul Westbrook | c7a070f | 2012-04-12 01:46:41 -0700 | [diff] [blame] | 245 | private final int mFolderItemUpdateDelayMs; |
| 246 | |
Vikram Aggarwal | 135fd02 | 2012-04-11 14:44:15 -0700 | [diff] [blame] | 247 | /** Keeps track of selected and unselected conversations */ |
Paul Westbrook | 937c94f | 2012-08-16 13:01:18 -0700 | [diff] [blame] | 248 | final protected ConversationPositionTracker mTracker; |
Vikram Aggarwal | af1ee0c | 2012-04-12 17:13:13 -0700 | [diff] [blame] | 249 | |
Vikram Aggarwal | e128fc2 | 2012-04-04 12:33:34 -0700 | [diff] [blame] | 250 | /** |
| 251 | * Action menu associated with the selected set. |
| 252 | */ |
| 253 | SelectedConversationsActionMenu mCabActionMenu; |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 254 | protected ActionableToastBar mToastBar; |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 255 | protected ConversationPagerController mPagerController; |
Mindy Pereira | ab48636 | 2012-03-21 18:18:53 -0700 | [diff] [blame] | 256 | |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 257 | // this is split out from the general loader dispatcher because its loader doesn't return a |
| 258 | // basic Cursor |
| 259 | private final ConversationListLoaderCallbacks mListCursorCallbacks = |
| 260 | new ConversationListLoaderCallbacks(); |
| 261 | |
Andy Huang | 090db1e | 2012-07-25 13:25:28 -0700 | [diff] [blame] | 262 | private final DataSetObservable mFolderObservable = new DataSetObservable(); |
| 263 | |
Paul Westbrook | b334c90 | 2012-06-25 11:42:46 -0700 | [diff] [blame] | 264 | protected static final String LOG_TAG = LogTag.getLogTag(); |
Vikram Aggarwal | fa4b47e | 2012-03-09 13:02:46 -0800 | [diff] [blame] | 265 | /** Constants used to differentiate between the types of loaders. */ |
| 266 | private static final int LOADER_ACCOUNT_CURSOR = 0; |
Vikram Aggarwal | fa4b47e | 2012-03-09 13:02:46 -0800 | [diff] [blame] | 267 | private static final int LOADER_FOLDER_CURSOR = 2; |
| 268 | private static final int LOADER_RECENT_FOLDERS = 3; |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 269 | private static final int LOADER_CONVERSATION_LIST = 4; |
Mindy Pereira | ab48636 | 2012-03-21 18:18:53 -0700 | [diff] [blame] | 270 | private static final int LOADER_ACCOUNT_INBOX = 5; |
| 271 | private static final int LOADER_SEARCH = 6; |
Paul Westbrook | 2d50bcd | 2012-04-10 11:53:47 -0700 | [diff] [blame] | 272 | private static final int LOADER_ACCOUNT_UPDATE_CURSOR = 7; |
Vikram Aggarwal | 7a5d95a | 2012-07-27 16:24:54 -0700 | [diff] [blame] | 273 | /** |
| 274 | * Guaranteed to be the last loader ID used by the activity. Loaders are owned by Activity or |
| 275 | * fragments, and within an activity, loader IDs need to be unique. A hack to ensure that the |
| 276 | * {@link FolderWatcher} can create its folder loaders without clashing with the IDs of those |
| 277 | * of the {@link AbstractActivityController}. Currently, the {@link FolderWatcher} is the only |
| 278 | * other class that uses this activity's LoaderManager. If another class needs activity-level |
| 279 | * loaders, consider consolidating the loaders in a central location: a UI-less fragment |
| 280 | * perhaps. |
| 281 | */ |
| 282 | public static final int LAST_LOADER_ID = 100; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 283 | |
Paul Westbrook | 2388c5d | 2012-03-25 12:29:11 -0700 | [diff] [blame] | 284 | private static final int ADD_ACCOUNT_REQUEST_CODE = 1; |
Paul Westbrook | 122f7c2 | 2012-08-20 17:50:31 -0700 | [diff] [blame] | 285 | private static final int REAUTHENTICATE_REQUEST_CODE = 2; |
Paul Westbrook | 2388c5d | 2012-03-25 12:29:11 -0700 | [diff] [blame] | 286 | |
Vikram Aggarwal | e8a8532 | 2012-04-24 09:01:38 -0700 | [diff] [blame] | 287 | /** The pending destructive action to be carried out before swapping the conversation cursor.*/ |
| 288 | private DestructiveAction mPendingDestruction; |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 289 | protected AsyncRefreshTask mFolderSyncTask; |
Mindy Pereira | 49e5dbe | 2012-07-12 11:47:54 -0700 | [diff] [blame] | 290 | // Task for setting any share intents for the account to enabled. |
| 291 | // This gets cancelled if the user kills the app before it finishes, and |
| 292 | // will just run the next time the user opens the app. |
| 293 | private AsyncTask<String, Void, Void> mEnableShareIntents; |
Mindy Pereira | c975e84 | 2012-07-16 09:15:00 -0700 | [diff] [blame] | 294 | private Folder mFolderListFolder; |
mindyp | 5390fca | 2012-08-22 12:12:25 -0700 | [diff] [blame] | 295 | private boolean mIsDragHappening; |
mindyp | ead5039 | 2012-08-23 11:03:53 -0700 | [diff] [blame] | 296 | private int mShowUndoBarDelay; |
mindyp | 6f54e1b | 2012-10-09 09:54:08 -0700 | [diff] [blame] | 297 | private boolean mRecentsDataUpdated; |
Vikram Aggarwal | a3f43d4 | 2012-10-25 16:21:30 -0700 | [diff] [blame] | 298 | /** A wait fragment we added, if any. */ |
| 299 | private WaitFragment mWaitFragment; |
Vikram Aggarwal | f0ef230 | 2012-11-07 14:53:34 -0800 | [diff] [blame] | 300 | /** True if we have results from a search query */ |
| 301 | private boolean mHaveSearchResults = false; |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 302 | public static final String SYNC_ERROR_DIALOG_FRAGMENT_TAG = "SyncErrorDialogFragment"; |
Vikram Aggarwal | e8a8532 | 2012-04-24 09:01:38 -0700 | [diff] [blame] | 303 | |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 304 | public AbstractActivityController(MailActivity activity, ViewMode viewMode) { |
| 305 | mActivity = activity; |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 306 | mFragmentManager = mActivity.getFragmentManager(); |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 307 | mViewMode = viewMode; |
| 308 | mContext = activity.getApplicationContext(); |
Vikram Aggarwal | 025eba8 | 2012-05-08 10:45:30 -0700 | [diff] [blame] | 309 | mRecentFolderList = new RecentFolderList(mContext); |
Paul Westbrook | 937c94f | 2012-08-16 13:01:18 -0700 | [diff] [blame] | 310 | mTracker = new ConversationPositionTracker(this); |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 311 | // Allow the fragment to observe changes to its own selection set. No other object is |
| 312 | // aware of the selected set. |
| 313 | mSelectedSet.addObserver(this); |
Paul Westbrook | c7a070f | 2012-04-12 01:46:41 -0700 | [diff] [blame] | 314 | |
| 315 | mFolderItemUpdateDelayMs = |
| 316 | mContext.getResources().getInteger(R.integer.folder_item_refresh_delay_ms); |
mindyp | ead5039 | 2012-08-23 11:03:53 -0700 | [diff] [blame] | 317 | mShowUndoBarDelay = |
| 318 | mContext.getResources().getInteger(R.integer.show_undo_bar_delay_ms); |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 319 | } |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 320 | |
| 321 | @Override |
Vikram Aggarwal | e9a8103 | 2012-02-22 13:15:35 -0800 | [diff] [blame] | 322 | public Account getCurrentAccount() { |
| 323 | return mAccount; |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | @Override |
| 327 | public ConversationListContext getCurrentListContext() { |
Vikram Aggarwal | e9a8103 | 2012-02-22 13:15:35 -0800 | [diff] [blame] | 328 | return mConvListContext; |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | @Override |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 332 | public String getHelpContext() { |
Paul Westbrook | 30745b6 | 2012-08-19 14:10:32 -0700 | [diff] [blame] | 333 | final int mode = mViewMode.getMode(); |
| 334 | final int helpContextResId; |
| 335 | switch (mode) { |
| 336 | case ViewMode.WAITING_FOR_ACCOUNT_INITIALIZATION: |
| 337 | helpContextResId = R.string.wait_help_context; |
| 338 | break; |
| 339 | default: |
| 340 | helpContextResId = R.string.main_help_context; |
| 341 | } |
| 342 | return mContext.getString(helpContextResId); |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 343 | } |
| 344 | |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 345 | @Override |
Vikram Aggarwal | ef7c992 | 2012-04-23 12:35:04 -0700 | [diff] [blame] | 346 | public final ConversationCursor getConversationListCursor() { |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 347 | return mConversationListCursor; |
| 348 | } |
| 349 | |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 350 | /** |
Vikram Aggarwal | 34a6501 | 2012-04-17 12:39:06 -0700 | [diff] [blame] | 351 | * Check if the fragment is attached to an activity and has a root view. |
| 352 | * @param in |
| 353 | * @return true if the fragment is valid, false otherwise |
| 354 | */ |
| 355 | private static final boolean isValidFragment(Fragment in) { |
| 356 | if (in == null || in.getActivity() == null || in.getView() == null) { |
| 357 | return false; |
| 358 | } |
| 359 | return true; |
| 360 | } |
| 361 | |
| 362 | /** |
Vikram Aggarwal | 49e0e99 | 2012-09-21 13:53:15 -0700 | [diff] [blame] | 363 | * Get the conversation list fragment for this activity. If the conversation list fragment is |
| 364 | * not attached, this method returns null. |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 365 | * |
Vikram Aggarwal | 49e0e99 | 2012-09-21 13:53:15 -0700 | [diff] [blame] | 366 | * Caution! This method returns the {@link ConversationListFragment} after the fragment has been |
| 367 | * added, <b>and</b> after the {@link FragmentManager} has run through its queue to add the |
| 368 | * fragment. There is a non-trivial amount of time after the fragment is instantiated and before |
| 369 | * this call returns a non-null value, depending on the {@link FragmentManager}. If you |
| 370 | * need the fragment immediately after adding it, consider making the fragment an observer of |
| 371 | * the controller and perform the task immediately on {@link Fragment#onActivityCreated(Bundle)} |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 372 | */ |
| 373 | protected ConversationListFragment getConversationListFragment() { |
| 374 | final Fragment fragment = mFragmentManager.findFragmentByTag(TAG_CONVERSATION_LIST); |
Vikram Aggarwal | 34a6501 | 2012-04-17 12:39:06 -0700 | [diff] [blame] | 375 | if (isValidFragment(fragment)) { |
| 376 | return (ConversationListFragment) fragment; |
Andy Huang | 9585d78 | 2012-04-16 19:45:04 -0700 | [diff] [blame] | 377 | } |
Vikram Aggarwal | 34a6501 | 2012-04-17 12:39:06 -0700 | [diff] [blame] | 378 | return null; |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | /** |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 382 | * Returns the folder list fragment attached with this activity. If no such fragment is attached |
| 383 | * this method returns null. |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 384 | * |
Vikram Aggarwal | 49e0e99 | 2012-09-21 13:53:15 -0700 | [diff] [blame] | 385 | * Caution! This method returns the {@link FolderListFragment} after the fragment has been |
| 386 | * added, <b>and</b> after the {@link FragmentManager} has run through its queue to add the |
| 387 | * fragment. There is a non-trivial amount of time after the fragment is instantiated and before |
| 388 | * this call returns a non-null value, depending on the {@link FragmentManager}. If you |
| 389 | * need the fragment immediately after adding it, consider making the fragment an observer of |
| 390 | * the controller and perform the task immediately on {@link Fragment#onActivityCreated(Bundle)} |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 391 | */ |
| 392 | protected FolderListFragment getFolderListFragment() { |
| 393 | final Fragment fragment = mFragmentManager.findFragmentByTag(TAG_FOLDER_LIST); |
Vikram Aggarwal | 34a6501 | 2012-04-17 12:39:06 -0700 | [diff] [blame] | 394 | if (isValidFragment(fragment)) { |
| 395 | return (FolderListFragment) fragment; |
Andy Huang | 9585d78 | 2012-04-16 19:45:04 -0700 | [diff] [blame] | 396 | } |
Vikram Aggarwal | 34a6501 | 2012-04-17 12:39:06 -0700 | [diff] [blame] | 397 | return null; |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 398 | } |
| 399 | |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 400 | /** |
Mindy Pereira | 161f50d | 2012-02-28 15:47:19 -0800 | [diff] [blame] | 401 | * Initialize the action bar. This is not visible to OnePaneController and |
| 402 | * TwoPaneController so they cannot override this behavior. |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 403 | */ |
Vikram Aggarwal | abd24d8 | 2012-04-26 13:23:14 -0700 | [diff] [blame] | 404 | private void initializeActionBar() { |
| 405 | final ActionBar actionBar = mActivity.getActionBar(); |
Andy Huang | 5895f7b | 2012-06-01 17:07:20 -0700 | [diff] [blame] | 406 | if (actionBar == null) { |
| 407 | return; |
Vikram Aggarwal | abd24d8 | 2012-04-26 13:23:14 -0700 | [diff] [blame] | 408 | } |
Andy Huang | 5895f7b | 2012-06-01 17:07:20 -0700 | [diff] [blame] | 409 | |
| 410 | // be sure to inherit from the ActionBar theme when inflating |
| 411 | final LayoutInflater inflater = LayoutInflater.from(actionBar.getThemedContext()); |
Mindy Pereira | 82faec7 | 2012-06-14 17:21:50 -0700 | [diff] [blame] | 412 | final boolean isSearch = mActivity.getIntent() != null |
| 413 | && Intent.ACTION_SEARCH.equals(mActivity.getIntent().getAction()); |
| 414 | mActionBarView = (MailActionBarView) inflater.inflate( |
| 415 | isSearch ? R.layout.search_actionbar_view : R.layout.actionbar_view, null); |
Andy Huang | 5895f7b | 2012-06-01 17:07:20 -0700 | [diff] [blame] | 416 | mActionBarView.initialize(mActivity, this, mViewMode, actionBar, mRecentFolderList); |
Vikram Aggarwal | abd24d8 | 2012-04-26 13:23:14 -0700 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | /** |
| 420 | * Attach the action bar to the activity. |
| 421 | */ |
| 422 | private void attachActionBar() { |
| 423 | final ActionBar actionBar = mActivity.getActionBar(); |
| 424 | if (actionBar != null && mActionBarView != null) { |
Vikram Aggarwal | ec5cbf7 | 2012-03-08 15:10:35 -0800 | [diff] [blame] | 425 | actionBar.setCustomView(mActionBarView, new ActionBar.LayoutParams( |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 426 | LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame] | 427 | // Show a custom view and home icon, but remove the title |
| 428 | final int mask = ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_TITLE |
| 429 | | ActionBar.DISPLAY_SHOW_HOME; |
| 430 | final int enabled = ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME; |
| 431 | actionBar.setDisplayOptions(enabled, mask); |
Vikram Aggarwal | abd24d8 | 2012-04-26 13:23:14 -0700 | [diff] [blame] | 432 | mActionBarView.attach(); |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 433 | } |
Vikram Aggarwal | abd24d8 | 2012-04-26 13:23:14 -0700 | [diff] [blame] | 434 | mViewMode.addListener(mActionBarView); |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | /** |
Mindy Pereira | 161f50d | 2012-02-28 15:47:19 -0800 | [diff] [blame] | 438 | * Returns whether the conversation list fragment is visible or not. |
| 439 | * Different layouts will have their own notion on the visibility of |
| 440 | * fragments, so this method needs to be overriden. |
| 441 | * |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 442 | */ |
| 443 | protected abstract boolean isConversationListVisible(); |
| 444 | |
Vikram Aggarwal | eb7d329 | 2012-04-20 17:07:20 -0700 | [diff] [blame] | 445 | /** |
Vikram Aggarwal | 34f7b23 | 2012-10-17 13:32:23 -0700 | [diff] [blame] | 446 | * If required, starts wait mode for the current account. |
Vikram Aggarwal | 34f7b23 | 2012-10-17 13:32:23 -0700 | [diff] [blame] | 447 | */ |
| 448 | final void perhapsEnterWaitMode() { |
| 449 | // If the account is not initialized, then show the wait fragment, since nothing can be |
| 450 | // shown. |
| 451 | if (mAccount.isAccountInitializationRequired()) { |
| 452 | showWaitForInitialization(); |
| 453 | return; |
| 454 | } |
| 455 | |
| 456 | final boolean inWaitingMode = inWaitMode(); |
| 457 | final boolean isSyncRequired = mAccount.isAccountSyncRequired(); |
| 458 | if (isSyncRequired) { |
| 459 | if (inWaitingMode) { |
| 460 | // Update the WaitFragment's account object |
| 461 | updateWaitMode(); |
| 462 | } else { |
| 463 | // Transition to waiting mode |
| 464 | showWaitForInitialization(); |
| 465 | } |
| 466 | } else if (inWaitingMode) { |
| 467 | // Dismiss waiting mode |
| 468 | hideWaitForInitialization(); |
| 469 | } |
| 470 | } |
| 471 | |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 472 | @Override |
Mindy Pereira | 28e0c34 | 2012-02-17 15:05:13 -0800 | [diff] [blame] | 473 | public void onAccountChanged(Account account) { |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 474 | // Is the account or account settings different from the existing account? |
Vikram Aggarwal | 66bc6ed | 2012-07-31 09:59:55 -0700 | [diff] [blame] | 475 | final boolean firstLoad = mAccount == null; |
| 476 | final boolean accountChanged = firstLoad || !account.uri.equals(mAccount.uri); |
Vikram Aggarwal | 472e536 | 2012-11-05 14:17:23 -0800 | [diff] [blame] | 477 | // If nothing has changed, return early without wasting any more time. |
| 478 | if (!accountChanged && !account.settingsDiffer(mAccount)) { |
| 479 | return; |
| 480 | } |
| 481 | // We also don't want to do anything if the new account is null |
| 482 | if (account == null) { |
| 483 | LogUtils.e(LOG_TAG, "AAC.onAccountChanged(null) called."); |
| 484 | return; |
| 485 | } |
| 486 | final String accountName = account.name; |
| 487 | mHandler.post(new Runnable() { |
| 488 | @Override |
| 489 | public void run() { |
| 490 | MailActivity.setForegroundNdef(MailActivity.getMailtoNdef(accountName)); |
Mindy Pereira | fa20c1a | 2012-07-23 13:00:02 -0700 | [diff] [blame] | 491 | } |
Vikram Aggarwal | 472e536 | 2012-11-05 14:17:23 -0800 | [diff] [blame] | 492 | }); |
| 493 | if (accountChanged) { |
| 494 | commitDestructiveActions(false); |
| 495 | } |
| 496 | // Change the account here |
| 497 | setAccount(account); |
| 498 | // And carry out associated actions. |
| 499 | cancelRefreshTask(); |
| 500 | if (accountChanged) { |
| 501 | loadAccountInbox(); |
| 502 | } |
| 503 | // Check if we need to force setting up an account before proceeding. |
| 504 | if (mAccount != null && !Uri.EMPTY.equals(mAccount.settings.setupIntentUri)) { |
| 505 | // Launch the intent! |
| 506 | final Intent intent = new Intent(Intent.ACTION_EDIT); |
| 507 | intent.setData(mAccount.settings.setupIntentUri); |
| 508 | mActivity.startActivity(intent); |
Mindy Pereira | 28d5f72 | 2012-02-15 12:32:40 -0800 | [diff] [blame] | 509 | } |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 510 | } |
| 511 | |
Vikram Aggarwal | e6340bc | 2012-03-26 15:57:09 -0700 | [diff] [blame] | 512 | /** |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 513 | * Adds a listener interested in change in the current account. If a class is storing a |
| 514 | * reference to the current account, it should listen on changes, so it can receive updates to |
| 515 | * settings. Must happen in the UI thread. |
| 516 | */ |
Mindy Pereira | efe3d25 | 2012-03-01 14:20:44 -0800 | [diff] [blame] | 517 | @Override |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 518 | public void registerAccountObserver(DataSetObserver obs) { |
| 519 | mAccountObservers.registerObserver(obs); |
Mindy Pereira | efe3d25 | 2012-03-01 14:20:44 -0800 | [diff] [blame] | 520 | } |
| 521 | |
Vikram Aggarwal | 7d81600 | 2012-04-17 17:06:41 -0700 | [diff] [blame] | 522 | /** |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 523 | * Removes a listener from receiving current account changes. |
Vikram Aggarwal | 7d81600 | 2012-04-17 17:06:41 -0700 | [diff] [blame] | 524 | * Must happen in the UI thread. |
| 525 | */ |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 526 | @Override |
| 527 | public void unregisterAccountObserver(DataSetObserver obs) { |
| 528 | mAccountObservers.unregisterObserver(obs); |
Vikram Aggarwal | 7d81600 | 2012-04-17 17:06:41 -0700 | [diff] [blame] | 529 | } |
| 530 | |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 531 | @Override |
| 532 | public Account getAccount() { |
| 533 | return mAccount; |
Vikram Aggarwal | 7d81600 | 2012-04-17 17:06:41 -0700 | [diff] [blame] | 534 | } |
| 535 | |
Mindy Pereira | e082839 | 2012-03-08 10:38:40 -0800 | [diff] [blame] | 536 | private void fetchSearchFolder(Intent intent) { |
Vikram Aggarwal | 2b703c6 | 2012-09-18 13:54:15 -0700 | [diff] [blame] | 537 | final Bundle args = new Bundle(); |
Mindy Pereira | ab48636 | 2012-03-21 18:18:53 -0700 | [diff] [blame] | 538 | args.putString(ConversationListContext.EXTRA_SEARCH_QUERY, intent |
Mindy Pereira | e082839 | 2012-03-08 10:38:40 -0800 | [diff] [blame] | 539 | .getStringExtra(ConversationListContext.EXTRA_SEARCH_QUERY)); |
Mindy Pereira | ab48636 | 2012-03-21 18:18:53 -0700 | [diff] [blame] | 540 | mActivity.getLoaderManager().restartLoader(LOADER_SEARCH, args, this); |
Mindy Pereira | e082839 | 2012-03-08 10:38:40 -0800 | [diff] [blame] | 541 | } |
| 542 | |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 543 | @Override |
Mindy Pereira | 28e0c34 | 2012-02-17 15:05:13 -0800 | [diff] [blame] | 544 | public void onFolderChanged(Folder folder) { |
Vikram Aggarwal | f334140 | 2012-08-07 10:09:38 -0700 | [diff] [blame] | 545 | changeFolder(folder, null); |
| 546 | } |
| 547 | |
| 548 | /** |
Vikram Aggarwal | 203dc00 | 2012-08-23 13:59:04 -0700 | [diff] [blame] | 549 | * Sets the folder state without changing view mode and without creating a list fragment, if |
| 550 | * possible. |
| 551 | * @param folder |
| 552 | */ |
| 553 | private void setListContext(Folder folder, String query) { |
| 554 | updateFolder(folder); |
| 555 | if (query != null) { |
| 556 | mConvListContext = ConversationListContext.forSearchQuery(mAccount, mFolder, query); |
| 557 | } else { |
| 558 | mConvListContext = ConversationListContext.forFolder(mAccount, mFolder); |
| 559 | } |
Vikram Aggarwal | 203dc00 | 2012-08-23 13:59:04 -0700 | [diff] [blame] | 560 | cancelRefreshTask(); |
| 561 | } |
| 562 | |
| 563 | /** |
Vikram Aggarwal | f334140 | 2012-08-07 10:09:38 -0700 | [diff] [blame] | 564 | * Changes the folder to the value provided here. This causes the view mode to change. |
| 565 | * @param folder the folder to change to |
| 566 | * @param query if non-null, this represents the search string that the folder represents. |
| 567 | */ |
Vikram Aggarwal | 93dc202 | 2012-11-05 13:36:57 -0800 | [diff] [blame] | 568 | private final void changeFolder(Folder folder, String query) { |
Vikram Aggarwal | 203dc00 | 2012-08-23 13:59:04 -0700 | [diff] [blame] | 569 | if (!Objects.equal(mFolder, folder)) { |
| 570 | commitDestructiveActions(false); |
| 571 | } |
Mindy Pereira | a1f9998 | 2012-07-16 16:32:15 -0700 | [diff] [blame] | 572 | if (folder != null && !folder.equals(mFolder) |
| 573 | || (mViewMode.getMode() != ViewMode.CONVERSATION_LIST)) { |
Vikram Aggarwal | 203dc00 | 2012-08-23 13:59:04 -0700 | [diff] [blame] | 574 | setListContext(folder, query); |
Mindy Pereira | 28e0c34 | 2012-02-17 15:05:13 -0800 | [diff] [blame] | 575 | showConversationList(mConvListContext); |
Mindy Pereira | 28e0c34 | 2012-02-17 15:05:13 -0800 | [diff] [blame] | 576 | } |
Vikram Aggarwal | 93dc202 | 2012-11-05 13:36:57 -0800 | [diff] [blame] | 577 | resetActionBarIcon(); |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 578 | } |
| 579 | |
Mindy Pereira | 13c12a6 | 2012-05-31 15:41:08 -0700 | [diff] [blame] | 580 | @Override |
Mindy Pereira | 505df5f | 2012-06-19 17:57:17 -0700 | [diff] [blame] | 581 | public void onFolderSelected(Folder folder) { |
Mindy Pereira | 13c12a6 | 2012-05-31 15:41:08 -0700 | [diff] [blame] | 582 | onFolderChanged(folder); |
| 583 | } |
| 584 | |
Vikram Aggarwal | 792ccba | 2012-03-27 13:46:57 -0700 | [diff] [blame] | 585 | /** |
| 586 | * Update the recent folders. This only needs to be done once when accessing a new folder. |
| 587 | */ |
Paul Westbrook | 7ebdfd0 | 2012-03-21 15:55:30 -0700 | [diff] [blame] | 588 | private void updateRecentFolderList() { |
Mindy Pereira | ab48636 | 2012-03-21 18:18:53 -0700 | [diff] [blame] | 589 | if (mFolder != null) { |
Marc Blank | 2675dbc | 2012-04-03 10:17:13 -0700 | [diff] [blame] | 590 | mRecentFolderList.touchFolder(mFolder, mAccount); |
Mindy Pereira | ab48636 | 2012-03-21 18:18:53 -0700 | [diff] [blame] | 591 | } |
Paul Westbrook | 7ebdfd0 | 2012-03-21 15:55:30 -0700 | [diff] [blame] | 592 | } |
| 593 | |
Vikram Aggarwal | 58cad2e | 2012-08-28 16:18:23 -0700 | [diff] [blame] | 594 | /** |
| 595 | * Adds a listener interested in change in the recent folders. If a class is storing a |
| 596 | * reference to the recent folders, it should listen on changes, so it can receive updates. |
| 597 | * Must happen in the UI thread. |
| 598 | */ |
| 599 | @Override |
| 600 | public void registerRecentFolderObserver(DataSetObserver obs) { |
| 601 | mRecentFolderObservers.registerObserver(obs); |
| 602 | } |
| 603 | |
| 604 | /** |
| 605 | * Removes a listener from receiving recent folder changes. |
| 606 | * Must happen in the UI thread. |
| 607 | */ |
| 608 | @Override |
| 609 | public void unregisterRecentFolderObserver(DataSetObserver obs) { |
| 610 | mRecentFolderObservers.unregisterObserver(obs); |
| 611 | } |
| 612 | |
| 613 | @Override |
| 614 | public RecentFolderList getRecentFolders() { |
| 615 | return mRecentFolderList; |
| 616 | } |
| 617 | |
Mindy Pereira | ab48636 | 2012-03-21 18:18:53 -0700 | [diff] [blame] | 618 | // TODO(mindyp): set this up to store a copy of the folder as a transient |
| 619 | // field in the account. |
Vikram Aggarwal | eb7d329 | 2012-04-20 17:07:20 -0700 | [diff] [blame] | 620 | @Override |
| 621 | public void loadAccountInbox() { |
Vikram Aggarwal | 94c94de | 2012-04-04 15:38:28 -0700 | [diff] [blame] | 622 | restartOptionalLoader(LOADER_ACCOUNT_INBOX); |
Mindy Pereira | f6acdad | 2012-03-15 11:21:13 -0700 | [diff] [blame] | 623 | } |
| 624 | |
Vikram Aggarwal | 69b5c30 | 2012-09-05 11:11:13 -0700 | [diff] [blame] | 625 | /** |
Vikram Aggarwal | d00229d | 2012-09-20 12:31:44 -0700 | [diff] [blame] | 626 | * Marks the {@link #mFolderChanged} value if the newFolder is different from the existing |
| 627 | * {@link #mFolder}. This should be called immediately <b>before</b> assigning newFolder to |
| 628 | * mFolder. |
| 629 | * @param newFolder |
| 630 | */ |
| 631 | private final void setHasFolderChanged(final Folder newFolder) { |
| 632 | // We should never try to assign a null folder. But in the rare event that we do, we should |
| 633 | // only set the bit when we have a valid folder, and null is not valid. |
| 634 | if (newFolder == null) { |
| 635 | return; |
| 636 | } |
| 637 | // If the previous folder was null, or if the two folders represent different data, then we |
| 638 | // consider that the folder has changed. |
| 639 | if (mFolder == null || !newFolder.uri.equals(mFolder.uri)) { |
| 640 | mFolderChanged = true; |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | /** |
Vikram Aggarwal | 69b5c30 | 2012-09-05 11:11:13 -0700 | [diff] [blame] | 645 | * Sets the current folder if it is different from the object provided here. This method does |
| 646 | * NOT notify the folder observers that a change has happened. Observers are notified when we |
| 647 | * get an updated folder from the loaders, which will happen as a consequence of this method |
| 648 | * (since this method starts/restarts the loaders). |
| 649 | * @param folder The folder to assign |
| 650 | */ |
Mindy Pereira | 11e3596 | 2012-06-01 14:49:46 -0700 | [diff] [blame] | 651 | private void updateFolder(Folder folder) { |
Vikram Aggarwal | 1672ff8 | 2012-09-21 10:15:22 -0700 | [diff] [blame] | 652 | if (folder == null || !folder.isInitialized()) { |
| 653 | LogUtils.e(LOG_TAG, new Error(), "AAC.setFolder(%s): Bad input", folder); |
| 654 | return; |
| 655 | } |
| 656 | if (folder.equals(mFolder)) { |
| 657 | LogUtils.d(LOG_TAG, "AAC.setFolder(%s): Input matches mFolder", folder); |
| 658 | return; |
| 659 | } |
| 660 | final boolean wasNull = mFolder == null; |
| 661 | LogUtils.d(LOG_TAG, "AbstractActivityController.setFolder(%s)", folder.name); |
| 662 | final LoaderManager lm = mActivity.getLoaderManager(); |
| 663 | // updateFolder is called from AAC.onLoadFinished() on folder changes. We need to |
| 664 | // ensure that the folder is different from the previous folder before marking the |
| 665 | // folder changed. |
| 666 | setHasFolderChanged(folder); |
| 667 | mFolder = folder; |
Vikram Aggarwal | 69b5c30 | 2012-09-05 11:11:13 -0700 | [diff] [blame] | 668 | |
Vikram Aggarwal | 1672ff8 | 2012-09-21 10:15:22 -0700 | [diff] [blame] | 669 | // We do not need to notify folder observers yet. Instead we start the loaders and |
| 670 | // when the load finishes, we will get an updated folder. Then, we notify the |
| 671 | // folderObservers in onLoadFinished. |
| 672 | mActionBarView.setFolder(mFolder); |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 673 | |
Vikram Aggarwal | 1672ff8 | 2012-09-21 10:15:22 -0700 | [diff] [blame] | 674 | // Only when we switch from one folder to another do we want to restart the |
| 675 | // folder and conversation list loaders (to trigger onCreateLoader). |
| 676 | // The first time this runs when the activity is [re-]initialized, we want to re-use the |
| 677 | // previous loader's instance and data upon configuration change (e.g. rotation). |
| 678 | // If there was not already an instance of the loader, init it. |
| 679 | if (lm.getLoader(LOADER_FOLDER_CURSOR) == null) { |
| 680 | lm.initLoader(LOADER_FOLDER_CURSOR, null, this); |
| 681 | } else { |
| 682 | lm.restartLoader(LOADER_FOLDER_CURSOR, null, this); |
| 683 | } |
| 684 | // In this case, we are starting from no folder, which would occur |
| 685 | // the first time the app was launched or on orientation changes. |
| 686 | // We want to attach to an existing loader, if available. |
| 687 | if (wasNull || lm.getLoader(LOADER_CONVERSATION_LIST) == null) { |
| 688 | lm.initLoader(LOADER_CONVERSATION_LIST, null, mListCursorCallbacks); |
| 689 | } else { |
| 690 | // However, if there was an existing folder AND we have changed |
| 691 | // folders, we want to restart the loader to get the information |
| 692 | // for the newly selected folder |
| 693 | lm.destroyLoader(LOADER_CONVERSATION_LIST); |
| 694 | lm.initLoader(LOADER_CONVERSATION_LIST, null, mListCursorCallbacks); |
Mindy Pereira | 5cb0c3e | 2012-02-22 15:22:47 -0800 | [diff] [blame] | 695 | } |
| 696 | } |
| 697 | |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 698 | @Override |
Andy Huang | 090db1e | 2012-07-25 13:25:28 -0700 | [diff] [blame] | 699 | public Folder getFolder() { |
| 700 | return mFolder; |
| 701 | } |
| 702 | |
| 703 | @Override |
Mindy Pereira | c975e84 | 2012-07-16 09:15:00 -0700 | [diff] [blame] | 704 | public Folder getHierarchyFolder() { |
| 705 | return mFolderListFolder; |
| 706 | } |
| 707 | |
| 708 | @Override |
| 709 | public void setHierarchyFolder(Folder folder) { |
| 710 | mFolderListFolder = folder; |
Mindy Pereira | 23aadfd | 2012-05-25 11:24:33 -0700 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | @Override |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 714 | public void onActivityResult(int requestCode, int resultCode, Intent data) { |
Paul Westbrook | 122f7c2 | 2012-08-20 17:50:31 -0700 | [diff] [blame] | 715 | switch (requestCode) { |
| 716 | case ADD_ACCOUNT_REQUEST_CODE: |
| 717 | // We were waiting for the user to create an account |
| 718 | if (resultCode == Activity.RESULT_OK) { |
| 719 | // restart the loader to get the updated list of accounts |
| 720 | mActivity.getLoaderManager().initLoader( |
| 721 | LOADER_ACCOUNT_CURSOR, null, this); |
| 722 | } else { |
| 723 | // The user failed to create an account, just exit the app |
| 724 | mActivity.finish(); |
| 725 | } |
| 726 | break; |
| 727 | case REAUTHENTICATE_REQUEST_CODE: |
| 728 | if (resultCode == Activity.RESULT_OK) { |
| 729 | // The user successfully authenticated, attempt to refresh the list |
| 730 | final Uri refreshUri = mFolder != null ? mFolder.refreshUri : null; |
| 731 | if (refreshUri != null) { |
| 732 | startAsyncRefreshTask(refreshUri); |
| 733 | } |
| 734 | } |
| 735 | break; |
Paul Westbrook | 2388c5d | 2012-03-25 12:29:11 -0700 | [diff] [blame] | 736 | } |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 737 | } |
| 738 | |
Vikram Aggarwal | 69b5c30 | 2012-09-05 11:11:13 -0700 | [diff] [blame] | 739 | /** |
| 740 | * Inform the conversation cursor that there has been a visibility change. |
| 741 | * @param visible |
| 742 | */ |
| 743 | protected synchronized void informCursorVisiblity(boolean visible) { |
| 744 | if (mConversationListCursor != null) { |
| 745 | Utils.setConversationCursorVisibility(mConversationListCursor, visible, mFolderChanged); |
| 746 | // We have informed the cursor. Subsequent visibility changes should not tell it that |
| 747 | // the folder has changed. |
| 748 | mFolderChanged = false; |
| 749 | } |
| 750 | } |
| 751 | |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 752 | @Override |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 753 | public void onConversationListVisibilityChanged(boolean visible) { |
Vikram Aggarwal | 69b5c30 | 2012-09-05 11:11:13 -0700 | [diff] [blame] | 754 | informCursorVisiblity(visible); |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 755 | } |
| 756 | |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 757 | /** |
Vikram Aggarwal | 7dd054e | 2012-05-21 14:43:10 -0700 | [diff] [blame] | 758 | * Called when a conversation is visible. Child classes must call the super class implementation |
| 759 | * before performing local computation. |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 760 | */ |
| 761 | @Override |
| 762 | public void onConversationVisibilityChanged(boolean visible) { |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | @Override |
Vikram Aggarwal | d7a12cd | 2012-02-03 09:36:20 -0800 | [diff] [blame] | 766 | public boolean onCreate(Bundle savedState) { |
Vikram Aggarwal | abd24d8 | 2012-04-26 13:23:14 -0700 | [diff] [blame] | 767 | initializeActionBar(); |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 768 | // Allow shortcut keys to function for the ActionBar and menus. |
| 769 | mActivity.setDefaultKeyMode(Activity.DEFAULT_KEYS_SHORTCUT); |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 770 | mResolver = mActivity.getContentResolver(); |
Paul Westbrook | 6ead20d | 2012-03-19 14:48:14 -0700 | [diff] [blame] | 771 | mNewEmailReceiver = new SuppressNotificationReceiver(); |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 772 | mRecentFolderList.initialize(mActivity); |
Paul Westbrook | 6ead20d | 2012-03-19 14:48:14 -0700 | [diff] [blame] | 773 | |
Mindy Pereira | 161f50d | 2012-02-28 15:47:19 -0800 | [diff] [blame] | 774 | // All the individual UI components listen for ViewMode changes. This |
Mindy Pereira | b849dfb | 2012-03-07 18:13:15 -0800 | [diff] [blame] | 775 | // simplifies the amount of logic in the AbstractActivityController, but increases the |
| 776 | // possibility of timing-related bugs. |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 777 | mViewMode.addListener(this); |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 778 | mPagerController = new ConversationPagerController(mActivity, this); |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 779 | mToastBar = (ActionableToastBar) mActivity.findViewById(R.id.toast_bar); |
Vikram Aggarwal | ada5178 | 2012-04-26 14:18:31 -0700 | [diff] [blame] | 780 | attachActionBar(); |
Mark Wei | 9eb1c9a | 2012-10-01 12:54:50 -0700 | [diff] [blame] | 781 | FolderSelectionDialog.setDialogDismissed(); |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 782 | |
| 783 | final Intent intent = mActivity.getIntent(); |
Vikram Aggarwal | abd24d8 | 2012-04-26 13:23:14 -0700 | [diff] [blame] | 784 | // Immediately handle a clean launch with intent, and any state restoration |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 785 | // that does not rely on restored fragments or loader data |
| 786 | // any state restoration that relies on those can be done later in |
| 787 | // onRestoreInstanceState, once fragments are up and loader data is re-delivered |
| 788 | if (savedState != null) { |
| 789 | if (savedState.containsKey(SAVED_ACCOUNT)) { |
| 790 | setAccount((Account) savedState.getParcelable(SAVED_ACCOUNT)); |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 791 | } |
| 792 | if (savedState.containsKey(SAVED_FOLDER)) { |
Andy Huang | 2bc8bc1 | 2012-11-12 17:24:25 -0800 | [diff] [blame] | 793 | final Folder folder = savedState.getParcelable(SAVED_FOLDER); |
Vikram Aggarwal | 203dc00 | 2012-08-23 13:59:04 -0700 | [diff] [blame] | 794 | final String query = savedState.getString(SAVED_QUERY, null); |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame] | 795 | setListContext(folder, query); |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 796 | } |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame] | 797 | mViewMode.handleRestore(savedState); |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 798 | } else if (intent != null) { |
| 799 | handleIntent(intent); |
| 800 | } |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 801 | // Create the accounts loader; this loads the account switch spinner. |
| 802 | mActivity.getLoaderManager().initLoader(LOADER_ACCOUNT_CURSOR, null, this); |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 803 | return true; |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | @Override |
Andy Huang | 1ee96b2 | 2012-08-24 20:19:53 -0700 | [diff] [blame] | 807 | public void onStart() { |
| 808 | mSafeToModifyFragments = true; |
| 809 | } |
| 810 | |
| 811 | @Override |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 812 | public void onRestart() { |
| 813 | DialogFragment fragment = (DialogFragment) |
| 814 | mFragmentManager.findFragmentByTag(SYNC_ERROR_DIALOG_FRAGMENT_TAG); |
| 815 | if (fragment != null) { |
| 816 | fragment.dismiss(); |
| 817 | } |
mindyp | ea04f93 | 2012-08-27 14:17:59 -0700 | [diff] [blame] | 818 | // When the user places the app in the background by pressing "home", |
| 819 | // dismiss the toast bar. However, since there is no way to determine if |
| 820 | // home was pressed, just dismiss any existing toast bar when restarting |
| 821 | // the app. |
| 822 | if (mToastBar != null) { |
| 823 | mToastBar.hide(false); |
| 824 | } |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | @Override |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 828 | public Dialog onCreateDialog(int id, Bundle bundle) { |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 829 | return null; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | @Override |
Vikram Aggarwal | 4eb5271 | 2012-06-19 16:24:50 -0700 | [diff] [blame] | 833 | public final boolean onCreateOptionsMenu(Menu menu) { |
Vikram Aggarwal | e5e917c | 2012-09-20 16:27:41 -0700 | [diff] [blame] | 834 | final MenuInflater inflater = mActivity.getMenuInflater(); |
Mindy Pereira | f5acda4 | 2012-02-15 20:13:59 -0800 | [diff] [blame] | 835 | inflater.inflate(mActionBarView.getOptionsMenuId(), menu); |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 836 | mActionBarView.onCreateOptionsMenu(menu); |
Mindy Pereira | 28d5f72 | 2012-02-15 12:32:40 -0800 | [diff] [blame] | 837 | return true; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | @Override |
Vikram Aggarwal | 4eb5271 | 2012-06-19 16:24:50 -0700 | [diff] [blame] | 841 | public final boolean onKeyDown(int keyCode, KeyEvent event) { |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 842 | // TODO(viki): Auto-generated method stub |
| 843 | return false; |
| 844 | } |
| 845 | |
| 846 | @Override |
Vikram Aggarwal | 4eb5271 | 2012-06-19 16:24:50 -0700 | [diff] [blame] | 847 | public final boolean onOptionsItemSelected(MenuItem item) { |
Vikram Aggarwal | 54452ae | 2012-03-13 15:29:00 -0700 | [diff] [blame] | 848 | final int id = item.getItemId(); |
Vikram Aggarwal | 7dd054e | 2012-05-21 14:43:10 -0700 | [diff] [blame] | 849 | LogUtils.d(LOG_TAG, "AbstractController.onOptionsItemSelected(%d) called.", id); |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 850 | boolean handled = true; |
Mindy Pereira | ba68fda | 2012-05-24 15:53:06 -0700 | [diff] [blame] | 851 | final Collection<Conversation> target = Conversation.listOf(mCurrentConversation); |
Vikram Aggarwal | 112cd16 | 2012-06-18 10:51:13 -0700 | [diff] [blame] | 852 | final Settings settings = (mAccount == null) ? null : mAccount.settings; |
mindyp | 84f7d32 | 2012-10-01 17:14:40 -0700 | [diff] [blame] | 853 | // The user is choosing a new action; commit whatever they had been |
| 854 | // doing before. |
mindyp | c6adce3 | 2012-08-22 18:46:42 -0700 | [diff] [blame] | 855 | commitDestructiveActions(true); |
Mindy Pereira | 28d5f72 | 2012-02-15 12:32:40 -0800 | [diff] [blame] | 856 | switch (id) { |
Mindy Pereira | ba68fda | 2012-05-24 15:53:06 -0700 | [diff] [blame] | 857 | case R.id.archive: { |
| 858 | final boolean showDialog = (settings != null && settings.confirmArchive); |
| 859 | confirmAndDelete(target, showDialog, R.plurals.confirm_archive_conversation, |
mindyp | f0656a1 | 2012-10-01 08:30:57 -0700 | [diff] [blame] | 860 | getDeferredAction(R.id.archive, target, false)); |
Mindy Pereira | ba68fda | 2012-05-24 15:53:06 -0700 | [diff] [blame] | 861 | break; |
| 862 | } |
Mindy Pereira | 01f3050 | 2012-08-14 10:30:51 -0700 | [diff] [blame] | 863 | case R.id.remove_folder: |
Vikram Aggarwal | a8e4318 | 2012-09-13 12:55:10 -0700 | [diff] [blame] | 864 | delete(R.id.remove_folder, target, |
mindyp | 84f7d32 | 2012-10-01 17:14:40 -0700 | [diff] [blame] | 865 | getDeferredRemoveFolder(target, mFolder, true, false, true)); |
Mindy Pereira | 01f3050 | 2012-08-14 10:30:51 -0700 | [diff] [blame] | 866 | break; |
Mindy Pereira | ba68fda | 2012-05-24 15:53:06 -0700 | [diff] [blame] | 867 | case R.id.delete: { |
| 868 | final boolean showDialog = (settings != null && settings.confirmDelete); |
| 869 | confirmAndDelete(target, showDialog, R.plurals.confirm_delete_conversation, |
mindyp | f0656a1 | 2012-10-01 08:30:57 -0700 | [diff] [blame] | 870 | getDeferredAction(R.id.delete, target, false)); |
Mindy Pereira | ba68fda | 2012-05-24 15:53:06 -0700 | [diff] [blame] | 871 | break; |
| 872 | } |
Vikram Aggarwal | a8e4318 | 2012-09-13 12:55:10 -0700 | [diff] [blame] | 873 | case R.id.discard_drafts: { |
Paul Westbrook | ef36254 | 2012-08-27 14:53:32 -0700 | [diff] [blame] | 874 | final boolean showDialog = (settings != null && settings.confirmDelete); |
| 875 | confirmAndDelete(target, showDialog, R.plurals.confirm_discard_drafts_conversation, |
mindyp | f0656a1 | 2012-10-01 08:30:57 -0700 | [diff] [blame] | 876 | getDeferredAction(R.id.discard_drafts, target, false)); |
Paul Westbrook | ef36254 | 2012-08-27 14:53:32 -0700 | [diff] [blame] | 877 | break; |
Vikram Aggarwal | a8e4318 | 2012-09-13 12:55:10 -0700 | [diff] [blame] | 878 | } |
Mindy Pereira | ba68fda | 2012-05-24 15:53:06 -0700 | [diff] [blame] | 879 | case R.id.mark_important: |
Vikram Aggarwal | 531488e | 2012-05-29 16:36:52 -0700 | [diff] [blame] | 880 | updateConversation(Conversation.listOf(mCurrentConversation), |
| 881 | ConversationColumns.PRIORITY, UIProvider.ConversationPriority.HIGH); |
Mindy Pereira | ba68fda | 2012-05-24 15:53:06 -0700 | [diff] [blame] | 882 | break; |
| 883 | case R.id.mark_not_important: |
Mindy Pereira | 0d03ef8 | 2012-08-15 09:05:48 -0700 | [diff] [blame] | 884 | if (mFolder != null && mFolder.isImportantOnly()) { |
Vikram Aggarwal | a8e4318 | 2012-09-13 12:55:10 -0700 | [diff] [blame] | 885 | delete(R.id.mark_not_important, target, |
mindyp | 84f7d32 | 2012-10-01 17:14:40 -0700 | [diff] [blame] | 886 | getDeferredAction(R.id.mark_not_important, target, false)); |
Mindy Pereira | 0d03ef8 | 2012-08-15 09:05:48 -0700 | [diff] [blame] | 887 | } else { |
| 888 | updateConversation(Conversation.listOf(mCurrentConversation), |
| 889 | ConversationColumns.PRIORITY, UIProvider.ConversationPriority.LOW); |
| 890 | } |
Mindy Pereira | ba68fda | 2012-05-24 15:53:06 -0700 | [diff] [blame] | 891 | break; |
| 892 | case R.id.mute: |
mindyp | 84f7d32 | 2012-10-01 17:14:40 -0700 | [diff] [blame] | 893 | delete(R.id.mute, target, getDeferredAction(R.id.mute, target, false)); |
Mindy Pereira | ba68fda | 2012-05-24 15:53:06 -0700 | [diff] [blame] | 894 | break; |
| 895 | case R.id.report_spam: |
mindyp | 84f7d32 | 2012-10-01 17:14:40 -0700 | [diff] [blame] | 896 | delete(R.id.report_spam, target, |
| 897 | getDeferredAction(R.id.report_spam, target, false)); |
Mindy Pereira | ba68fda | 2012-05-24 15:53:06 -0700 | [diff] [blame] | 898 | break; |
Paul Westbrook | 77eee62 | 2012-07-10 13:41:57 -0700 | [diff] [blame] | 899 | case R.id.mark_not_spam: |
mindyp | 84f7d32 | 2012-10-01 17:14:40 -0700 | [diff] [blame] | 900 | // Currently, since spam messages are only shown in list with |
| 901 | // other spam messages, |
Paul Westbrook | 77eee62 | 2012-07-10 13:41:57 -0700 | [diff] [blame] | 902 | // marking a message not as spam is a destructive action |
mindyp | 84f7d32 | 2012-10-01 17:14:40 -0700 | [diff] [blame] | 903 | delete(R.id.mark_not_spam, target, |
| 904 | getDeferredAction(R.id.mark_not_spam, target, false)); |
Paul Westbrook | 77eee62 | 2012-07-10 13:41:57 -0700 | [diff] [blame] | 905 | break; |
Paul Westbrook | 76b2062 | 2012-07-12 11:45:43 -0700 | [diff] [blame] | 906 | case R.id.report_phishing: |
mindyp | 84f7d32 | 2012-10-01 17:14:40 -0700 | [diff] [blame] | 907 | delete(R.id.report_phishing, target, |
| 908 | getDeferredAction(R.id.report_phishing, target, false)); |
Paul Westbrook | 76b2062 | 2012-07-12 11:45:43 -0700 | [diff] [blame] | 909 | break; |
Mindy Pereira | f5acda4 | 2012-02-15 20:13:59 -0800 | [diff] [blame] | 910 | case android.R.id.home: |
| 911 | onUpPressed(); |
| 912 | break; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 913 | case R.id.compose: |
| 914 | ComposeActivity.compose(mActivity.getActivityContext(), mAccount); |
| 915 | break; |
Mindy Pereira | 28d5f72 | 2012-02-15 12:32:40 -0800 | [diff] [blame] | 916 | case R.id.show_all_folders: |
| 917 | showFolderList(); |
| 918 | break; |
Mindy Pereira | 28e0c34 | 2012-02-17 15:05:13 -0800 | [diff] [blame] | 919 | case R.id.refresh: |
| 920 | requestFolderRefresh(); |
| 921 | break; |
Mindy Pereira | 1f93668 | 2012-03-02 11:30:33 -0800 | [diff] [blame] | 922 | case R.id.settings: |
| 923 | Utils.showSettings(mActivity.getActivityContext(), mAccount); |
Paul Westbrook | 2861b6a | 2012-02-15 15:25:34 -0800 | [diff] [blame] | 924 | break; |
Paul Westbrook | e550355 | 2012-03-28 00:35:57 -0700 | [diff] [blame] | 925 | case R.id.folder_options: |
| 926 | Utils.showFolderSettings(mActivity.getActivityContext(), mAccount, mFolder); |
| 927 | break; |
Paul Westbrook | 94e440d | 2012-02-24 11:03:47 -0800 | [diff] [blame] | 928 | case R.id.help_info_menu_item: |
Paul Westbrook | 30745b6 | 2012-08-19 14:10:32 -0700 | [diff] [blame] | 929 | Utils.showHelp(mActivity.getActivityContext(), mAccount, getHelpContext()); |
Paul Westbrook | 94e440d | 2012-02-24 11:03:47 -0800 | [diff] [blame] | 930 | break; |
Vikram Aggarwal | 54452ae | 2012-03-13 15:29:00 -0700 | [diff] [blame] | 931 | case R.id.feedback_menu_item: |
Paul Westbrook | 17beb0b | 2012-08-20 13:34:37 -0700 | [diff] [blame] | 932 | Utils.sendFeedback(mActivity.getActivityContext(), mAccount, false); |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 933 | break; |
Paul Westbrook | 18babd2 | 2012-04-09 22:17:08 -0700 | [diff] [blame] | 934 | case R.id.manage_folders_item: |
| 935 | Utils.showManageFolder(mActivity.getActivityContext(), mAccount); |
| 936 | break; |
Vikram Aggarwal | d503df4 | 2012-05-11 10:13:35 -0700 | [diff] [blame] | 937 | case R.id.change_folder: |
Mark Wei | 8f98ac0 | 2012-10-01 17:05:08 -0700 | [diff] [blame] | 938 | final FolderSelectionDialog dialog = FolderSelectionDialog.getInstance( |
| 939 | mActivity.getActivityContext(), mAccount, this, |
| 940 | Conversation.listOf(mCurrentConversation), false, mFolder); |
Mark Wei | 9eb1c9a | 2012-10-01 12:54:50 -0700 | [diff] [blame] | 941 | if (dialog != null) { |
| 942 | dialog.show(); |
mindyp | a7e1545 | 2012-09-18 14:22:11 -0700 | [diff] [blame] | 943 | } |
Vikram Aggarwal | d503df4 | 2012-05-11 10:13:35 -0700 | [diff] [blame] | 944 | break; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 945 | default: |
| 946 | handled = false; |
| 947 | break; |
Mindy Pereira | 28d5f72 | 2012-02-15 12:32:40 -0800 | [diff] [blame] | 948 | } |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 949 | return handled; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 950 | } |
| 951 | |
Vikram Aggarwal | 531488e | 2012-05-29 16:36:52 -0700 | [diff] [blame] | 952 | @Override |
Mindy Pereira | 6c2663d | 2012-07-20 15:37:29 -0700 | [diff] [blame] | 953 | public void updateConversation(Collection<Conversation> target, ContentValues values) { |
| 954 | mConversationListCursor.updateValues(mContext, target, values); |
| 955 | refreshConversationList(); |
| 956 | } |
| 957 | |
| 958 | @Override |
Vikram Aggarwal | 531488e | 2012-05-29 16:36:52 -0700 | [diff] [blame] | 959 | public void updateConversation(Collection <Conversation> target, String columnName, |
| 960 | boolean value) { |
| 961 | mConversationListCursor.updateBoolean(mContext, target, columnName, value); |
Vikram Aggarwal | 75daee5 | 2012-04-30 11:13:09 -0700 | [diff] [blame] | 962 | refreshConversationList(); |
Vikram Aggarwal | 54452ae | 2012-03-13 15:29:00 -0700 | [diff] [blame] | 963 | } |
| 964 | |
Vikram Aggarwal | 531488e | 2012-05-29 16:36:52 -0700 | [diff] [blame] | 965 | @Override |
Mindy Pereira | 6c2663d | 2012-07-20 15:37:29 -0700 | [diff] [blame] | 966 | public void updateConversation(Collection <Conversation> target, String columnName, |
| 967 | int value) { |
Vikram Aggarwal | 531488e | 2012-05-29 16:36:52 -0700 | [diff] [blame] | 968 | mConversationListCursor.updateInt(mContext, target, columnName, value); |
Vikram Aggarwal | 75daee5 | 2012-04-30 11:13:09 -0700 | [diff] [blame] | 969 | refreshConversationList(); |
Mindy Pereira | c9d5918 | 2012-03-22 16:06:46 -0700 | [diff] [blame] | 970 | } |
| 971 | |
Vikram Aggarwal | 531488e | 2012-05-29 16:36:52 -0700 | [diff] [blame] | 972 | @Override |
| 973 | public void updateConversation(Collection <Conversation> target, String columnName, |
| 974 | String value) { |
| 975 | mConversationListCursor.updateString(mContext, target, columnName, value); |
Vikram Aggarwal | 75daee5 | 2012-04-30 11:13:09 -0700 | [diff] [blame] | 976 | refreshConversationList(); |
Vikram Aggarwal | 54452ae | 2012-03-13 15:29:00 -0700 | [diff] [blame] | 977 | } |
| 978 | |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 979 | @Override |
| 980 | public void markConversationMessagesUnread(Conversation conv, Set<Uri> unreadMessageUris, |
Vikram Aggarwal | 4a878b6 | 2012-07-31 15:09:25 -0700 | [diff] [blame] | 981 | String originalConversationInfo) { |
Andy Huang | 8f6b006 | 2012-07-31 15:36:31 -0700 | [diff] [blame] | 982 | // The only caller of this method is the conversation view, from where marking unread should |
| 983 | // *always* take you back to list mode. |
| 984 | showConversation(null); |
| 985 | |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 986 | // locally mark conversation unread (the provider is supposed to propagate message unread |
| 987 | // to conversation unread) |
| 988 | conv.read = false; |
| 989 | |
Paul Westbrook | 1faf93d | 2012-10-16 08:58:07 -0700 | [diff] [blame] | 990 | if (mConversationListCursor == null) { |
| 991 | LogUtils.e(LOG_TAG, "null ConversationCursor in markConversationMessagesUnread"); |
| 992 | return; |
| 993 | } |
| 994 | |
Andy Huang | 28e31e2 | 2012-07-26 16:33:15 -0700 | [diff] [blame] | 995 | // only do a granular 'mark unread' if a subset of messages are unread |
| 996 | final int unreadCount = (unreadMessageUris == null) ? 0 : unreadMessageUris.size(); |
Mindy Pereira | 0972e07 | 2012-08-01 17:43:06 -0700 | [diff] [blame] | 997 | final int numMessages = conv.getNumMessages(); |
| 998 | final boolean subsetIsUnread = (numMessages > 1 && unreadCount > 0 |
| 999 | && unreadCount < numMessages); |
Andy Huang | 28e31e2 | 2012-07-26 16:33:15 -0700 | [diff] [blame] | 1000 | |
| 1001 | if (!subsetIsUnread) { |
Vikram Aggarwal | 66bc2aa | 2012-08-02 10:47:03 -0700 | [diff] [blame] | 1002 | // Conversations are neither marked read, nor viewed, and we don't want to show |
| 1003 | // the next conversation. |
| 1004 | markConversationsRead(Collections.singletonList(conv), false, false, false); |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 1005 | } else { |
Andy Huang | daa06ab | 2012-07-24 10:46:44 -0700 | [diff] [blame] | 1006 | mConversationListCursor.setConversationColumn(conv.uri, ConversationColumns.READ, 0); |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 1007 | |
Mindy Pereira | 7b6d03d | 2012-07-30 13:03:41 -0700 | [diff] [blame] | 1008 | // locally update conversation's conversationInfo to revert to original version |
Andy Huang | 28e31e2 | 2012-07-26 16:33:15 -0700 | [diff] [blame] | 1009 | if (originalConversationInfo != null) { |
| 1010 | mConversationListCursor.setConversationColumn(conv.uri, |
| 1011 | ConversationColumns.CONVERSATION_INFO, originalConversationInfo); |
| 1012 | } |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 1013 | |
| 1014 | // applyBatch with each CPO as an UPDATE op on each affected message uri |
| 1015 | final ArrayList<ContentProviderOperation> ops = Lists.newArrayList(); |
| 1016 | String authority = null; |
| 1017 | for (Uri messageUri : unreadMessageUris) { |
| 1018 | if (authority == null) { |
| 1019 | authority = messageUri.getAuthority(); |
| 1020 | } |
| 1021 | ops.add(ContentProviderOperation.newUpdate(messageUri) |
| 1022 | .withValue(UIProvider.MessageColumns.READ, 0) |
| 1023 | .build()); |
| 1024 | } |
| 1025 | |
| 1026 | new ContentProviderTask() { |
| 1027 | @Override |
| 1028 | protected void onPostExecute(Result result) { |
| 1029 | // TODO: handle errors? |
| 1030 | } |
| 1031 | }.run(mResolver, authority, ops); |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 1032 | } |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 1033 | } |
| 1034 | |
| 1035 | @Override |
Vikram Aggarwal | 66bc2aa | 2012-08-02 10:47:03 -0700 | [diff] [blame] | 1036 | public void markConversationsRead(Collection<Conversation> targets, boolean read, |
| 1037 | boolean viewed) { |
| 1038 | // We want to show the next conversation if we are marking unread. |
| 1039 | markConversationsRead(targets, read, viewed, true); |
Andy Huang | 8f6b006 | 2012-07-31 15:36:31 -0700 | [diff] [blame] | 1040 | } |
| 1041 | |
Scott Kennedy | 0d0f8b0 | 2012-10-12 15:18:18 -0700 | [diff] [blame] | 1042 | private void markConversationsRead(final Collection<Conversation> targets, final boolean read, |
| 1043 | final boolean markViewed, final boolean showNext) { |
Vikram Aggarwal | cc754b1 | 2012-08-30 14:04:21 -0700 | [diff] [blame] | 1044 | // Auto-advance if requested and the current conversation is being marked unread |
Andy Huang | 8f6b006 | 2012-07-31 15:36:31 -0700 | [diff] [blame] | 1045 | if (showNext && !read) { |
Scott Kennedy | 0d0f8b0 | 2012-10-12 15:18:18 -0700 | [diff] [blame] | 1046 | final Runnable operation = new Runnable() { |
| 1047 | @Override |
| 1048 | public void run() { |
| 1049 | markConversationsRead(targets, read, markViewed, showNext); |
| 1050 | } |
| 1051 | }; |
| 1052 | |
| 1053 | if (!showNextConversation(targets, operation)) { |
| 1054 | // This method will be called again if the user selects an autoadvance option |
| 1055 | return; |
| 1056 | } |
Andy Huang | 8f6b006 | 2012-07-31 15:36:31 -0700 | [diff] [blame] | 1057 | } |
Scott Kennedy | 0d0f8b0 | 2012-10-12 15:18:18 -0700 | [diff] [blame] | 1058 | |
Vikram Aggarwal | cc754b1 | 2012-08-30 14:04:21 -0700 | [diff] [blame] | 1059 | final int size = targets.size(); |
| 1060 | final List<ConversationOperation> opList = new ArrayList<ConversationOperation>(size); |
| 1061 | for (final Conversation target : targets) { |
| 1062 | final ContentValues value = new ContentValues(); |
| 1063 | value.put(ConversationColumns.READ, read); |
Paul Westbrook | 5109c51 | 2012-11-05 11:00:30 -0800 | [diff] [blame] | 1064 | |
| 1065 | // The mark read/unread/viewed operations do not show an undo bar |
| 1066 | value.put(ConversationOperations.Parameters.SUPPRESS_UNDO, true); |
Vikram Aggarwal | 66bc2aa | 2012-08-02 10:47:03 -0700 | [diff] [blame] | 1067 | if (markViewed) { |
Vikram Aggarwal | cc754b1 | 2012-08-30 14:04:21 -0700 | [diff] [blame] | 1068 | value.put(ConversationColumns.VIEWED, true); |
Vikram Aggarwal | 66bc2aa | 2012-08-02 10:47:03 -0700 | [diff] [blame] | 1069 | } |
Vikram Aggarwal | 192fac1 | 2012-07-25 16:44:55 -0700 | [diff] [blame] | 1070 | final ConversationInfo info = target.conversationInfo; |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 1071 | if (info != null) { |
mindyp | 7f55c68 | 2012-10-04 11:38:27 -0700 | [diff] [blame] | 1072 | boolean changed = info.markRead(read); |
| 1073 | if (changed) { |
| 1074 | value.put(ConversationColumns.CONVERSATION_INFO, |
| 1075 | ConversationInfo.toString(info)); |
| 1076 | } |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 1077 | } |
Vikram Aggarwal | cc754b1 | 2012-08-30 14:04:21 -0700 | [diff] [blame] | 1078 | opList.add(mConversationListCursor.getOperationForConversation( |
| 1079 | target, ConversationOperation.UPDATE, value)); |
| 1080 | // Update the local conversation objects so they immediately change state. |
| 1081 | target.read = read; |
Andy Huang | cd5c5ee | 2012-08-12 19:03:51 -0700 | [diff] [blame] | 1082 | if (markViewed) { |
Vikram Aggarwal | cc754b1 | 2012-08-30 14:04:21 -0700 | [diff] [blame] | 1083 | target.markViewed(); |
Andy Huang | cd5c5ee | 2012-08-12 19:03:51 -0700 | [diff] [blame] | 1084 | } |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 1085 | } |
Vikram Aggarwal | cc754b1 | 2012-08-30 14:04:21 -0700 | [diff] [blame] | 1086 | mConversationListCursor.updateBulkValues(mContext, opList); |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 1087 | } |
| 1088 | |
Andy Huang | 8f6b006 | 2012-07-31 15:36:31 -0700 | [diff] [blame] | 1089 | /** |
| 1090 | * Auto-advance to a different conversation if the currently visible conversation in |
| 1091 | * conversation mode is affected (deleted, marked unread, etc.). |
| 1092 | * |
Scott Kennedy | 0d0f8b0 | 2012-10-12 15:18:18 -0700 | [diff] [blame] | 1093 | * <p>Does nothing if outside of conversation mode.</p> |
Andy Huang | 8f6b006 | 2012-07-31 15:36:31 -0700 | [diff] [blame] | 1094 | * |
| 1095 | * @param target the set of conversations being deleted/marked unread |
| 1096 | */ |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 1097 | @Override |
Scott Kennedy | 0d0f8b0 | 2012-10-12 15:18:18 -0700 | [diff] [blame] | 1098 | public void showNextConversation(final Collection<Conversation> target) { |
| 1099 | showNextConversation(target, null); |
| 1100 | } |
| 1101 | |
| 1102 | /** |
| 1103 | * Auto-advance to a different conversation if the currently visible conversation in |
| 1104 | * conversation mode is affected (deleted, marked unread, etc.). |
| 1105 | * |
| 1106 | * <p>Does nothing if outside of conversation mode.</p> |
| 1107 | * |
| 1108 | * @param target the set of conversations being deleted/marked unread |
| 1109 | * @return <code>false</code> if we aborted because the user has not yet specified a default |
| 1110 | * action, <code>true</code> otherwise |
| 1111 | */ |
| 1112 | private boolean showNextConversation(final Collection<Conversation> target, |
| 1113 | final Runnable operation) { |
Andy Huang | 8f6b006 | 2012-07-31 15:36:31 -0700 | [diff] [blame] | 1114 | final boolean currentConversationInView = (mViewMode.getMode() == ViewMode.CONVERSATION) |
| 1115 | && Conversation.contains(target, mCurrentConversation); |
Scott Kennedy | 0d0f8b0 | 2012-10-12 15:18:18 -0700 | [diff] [blame] | 1116 | |
Andy Huang | 8f6b006 | 2012-07-31 15:36:31 -0700 | [diff] [blame] | 1117 | if (currentConversationInView) { |
Scott Kennedy | 0d0f8b0 | 2012-10-12 15:18:18 -0700 | [diff] [blame] | 1118 | final int autoAdvanceSetting = mAccount.settings.getAutoAdvanceSetting(); |
| 1119 | |
| 1120 | if (autoAdvanceSetting == AutoAdvance.UNSET && Utils.useTabletUI(mContext)) { |
| 1121 | displayAutoAdvanceDialogAndPerformAction(operation); |
| 1122 | return false; |
| 1123 | } else { |
| 1124 | // If we don't have one set, but we're here, just take the default |
| 1125 | final int autoAdvance = (autoAdvanceSetting == AutoAdvance.UNSET) ? Settings |
| 1126 | .getAutoAdvanceSetting(null) |
| 1127 | : autoAdvanceSetting; |
| 1128 | |
| 1129 | final Conversation next = mTracker.getNextConversation(autoAdvance, target); |
| 1130 | LogUtils.d(LOG_TAG, "showNextConversation: showing %s next.", next); |
| 1131 | showConversation(next); |
| 1132 | return true; |
| 1133 | } |
Andy Huang | 8f6b006 | 2012-07-31 15:36:31 -0700 | [diff] [blame] | 1134 | } |
Scott Kennedy | 0d0f8b0 | 2012-10-12 15:18:18 -0700 | [diff] [blame] | 1135 | |
| 1136 | return true; |
| 1137 | } |
| 1138 | |
| 1139 | /** |
| 1140 | * Displays a the auto-advance dialog, and when the user makes a selection, the preference is |
| 1141 | * stored, and the specified operation is run. |
| 1142 | */ |
| 1143 | private void displayAutoAdvanceDialogAndPerformAction(final Runnable operation) { |
| 1144 | final String[] autoAdvanceDisplayOptions = |
| 1145 | mContext.getResources().getStringArray(R.array.prefEntries_autoAdvance); |
| 1146 | final String[] autoAdvanceOptionValues = |
| 1147 | mContext.getResources().getStringArray(R.array.prefValues_autoAdvance); |
| 1148 | |
| 1149 | final String defaultValue = mContext.getString(R.string.prefDefault_autoAdvance); |
| 1150 | int initialIndex = 0; |
| 1151 | for (int i = 0; i < autoAdvanceOptionValues.length; i++) { |
| 1152 | if (defaultValue.equals(autoAdvanceOptionValues[i])) { |
| 1153 | initialIndex = i; |
| 1154 | break; |
| 1155 | } |
| 1156 | } |
| 1157 | |
| 1158 | final DialogInterface.OnClickListener listClickListener = |
| 1159 | new DialogInterface.OnClickListener() { |
| 1160 | @Override |
| 1161 | public void onClick(DialogInterface dialog, int whichItem) { |
| 1162 | final String autoAdvanceValue = autoAdvanceOptionValues[whichItem]; |
| 1163 | final int autoAdvanceValueInt = |
| 1164 | UIProvider.AutoAdvance.getAutoAdvanceInt(autoAdvanceValue); |
| 1165 | mAccount.settings.setAutoAdvanceSetting(autoAdvanceValueInt); |
| 1166 | |
| 1167 | // Save the user's setting |
| 1168 | final ContentValues values = new ContentValues(1); |
| 1169 | values.put(AccountColumns.SettingsColumns.AUTO_ADVANCE, autoAdvanceValue); |
| 1170 | |
| 1171 | final ContentResolver resolver = mContext.getContentResolver(); |
| 1172 | resolver.update(mAccount.updateSettingsUri, values, null, null); |
| 1173 | |
| 1174 | // Dismiss the dialog, as clicking the items in the list doesn't close the |
| 1175 | // dialog. |
| 1176 | dialog.dismiss(); |
| 1177 | if (operation != null) { |
| 1178 | operation.run(); |
| 1179 | } |
| 1180 | } |
| 1181 | }; |
| 1182 | |
| 1183 | new AlertDialog.Builder(mActivity.getActivityContext()).setTitle( |
| 1184 | R.string.auto_advance_help_title) |
| 1185 | .setSingleChoiceItems(autoAdvanceDisplayOptions, initialIndex, listClickListener) |
| 1186 | .setPositiveButton(null, null) |
| 1187 | .create() |
| 1188 | .show(); |
Andy Huang | 8f6b006 | 2012-07-31 15:36:31 -0700 | [diff] [blame] | 1189 | } |
| 1190 | |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 1191 | @Override |
| 1192 | public void starMessage(ConversationMessage msg, boolean starred) { |
| 1193 | if (msg.starred == starred) { |
| 1194 | return; |
| 1195 | } |
| 1196 | |
| 1197 | msg.starred = starred; |
| 1198 | |
| 1199 | // locally propagate the change to the owning conversation |
| 1200 | // (figure the provider will properly propagate the change when it commits it) |
| 1201 | // |
| 1202 | // when unstarring, only propagate the change if this was the only message starred |
| 1203 | final boolean conversationStarred = starred || msg.isConversationStarred(); |
Andy Huang | cd12e82 | 2012-11-08 19:50:57 -0800 | [diff] [blame] | 1204 | final Conversation conv = msg.getConversation(); |
| 1205 | if (conversationStarred != conv.starred) { |
| 1206 | conv.starred = conversationStarred; |
| 1207 | mConversationListCursor.setConversationColumn(conv.uri, |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 1208 | ConversationColumns.STARRED, conversationStarred); |
| 1209 | } |
| 1210 | |
| 1211 | final ContentValues values = new ContentValues(1); |
| 1212 | values.put(UIProvider.MessageColumns.STARRED, starred ? 1 : 0); |
| 1213 | |
| 1214 | new ContentProviderTask.UpdateTask() { |
| 1215 | @Override |
| 1216 | protected void onPostExecute(Result result) { |
| 1217 | // TODO: handle errors? |
| 1218 | } |
| 1219 | }.run(mResolver, msg.uri, values, null /* selection*/, null /* selectionArgs */); |
| 1220 | } |
| 1221 | |
Mindy Pereira | 28e0c34 | 2012-02-17 15:05:13 -0800 | [diff] [blame] | 1222 | private void requestFolderRefresh() { |
| 1223 | if (mFolder != null) { |
Mindy Pereira | b7b33e0 | 2012-02-21 15:32:19 -0800 | [diff] [blame] | 1224 | if (mAsyncRefreshTask != null) { |
| 1225 | mAsyncRefreshTask.cancel(true); |
| 1226 | } |
Paul Westbrook | 7e2a2a1 | 2012-06-27 13:52:40 -0700 | [diff] [blame] | 1227 | mAsyncRefreshTask = new AsyncRefreshTask(mContext, mFolder.refreshUri); |
Mindy Pereira | b7b33e0 | 2012-02-21 15:32:19 -0800 | [diff] [blame] | 1228 | mAsyncRefreshTask.execute(); |
Mindy Pereira | 28e0c34 | 2012-02-17 15:05:13 -0800 | [diff] [blame] | 1229 | } |
| 1230 | } |
| 1231 | |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 1232 | /** |
| 1233 | * Confirm (based on user's settings) and delete a conversation from the conversation list and |
| 1234 | * from the database. |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 1235 | * @param target the conversations to act upon |
| 1236 | * @param showDialog true if a confirmation dialog is to be shown, false otherwise. |
| 1237 | * @param confirmResource the resource ID of the string that is shown in the confirmation dialog |
| 1238 | * @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] | 1239 | */ |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 1240 | protected void confirmAndDelete(final Collection<Conversation> target, boolean showDialog, |
| 1241 | int confirmResource, final DestructiveAction action) { |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 1242 | if (showDialog) { |
| 1243 | final AlertDialog.OnClickListener onClick = new AlertDialog.OnClickListener() { |
| 1244 | @Override |
| 1245 | public void onClick(DialogInterface dialog, int which) { |
mindyp | f0656a1 | 2012-10-01 08:30:57 -0700 | [diff] [blame] | 1246 | if (which == DialogInterface.BUTTON_POSITIVE) { |
| 1247 | delete(0, target, action); |
| 1248 | } |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 1249 | } |
| 1250 | }; |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 1251 | final CharSequence message = Utils.formatPlural(mContext, confirmResource, |
| 1252 | target.size()); |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 1253 | new AlertDialog.Builder(mActivity.getActivityContext()).setMessage(message) |
| 1254 | .setPositiveButton(R.string.ok, onClick) |
| 1255 | .setNegativeButton(R.string.cancel, null) |
| 1256 | .create().show(); |
| 1257 | } else { |
Vikram Aggarwal | a8e4318 | 2012-09-13 12:55:10 -0700 | [diff] [blame] | 1258 | delete(0, target, action); |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 1259 | } |
| 1260 | } |
| 1261 | |
Vikram Aggarwal | 4f4782b | 2012-05-30 08:39:09 -0700 | [diff] [blame] | 1262 | @Override |
Scott Kennedy | 0d0f8b0 | 2012-10-12 15:18:18 -0700 | [diff] [blame] | 1263 | public void delete(final int actionId, final Collection<Conversation> target, |
mindyp | 84f7d32 | 2012-10-01 17:14:40 -0700 | [diff] [blame] | 1264 | final Collection<ConversationItemView> targetViews, final DestructiveAction action) { |
| 1265 | // Order of events is critical! The Conversation View Fragment must be |
| 1266 | // notified of the next conversation with showConversation(next) *before* the |
| 1267 | // conversation list |
Vikram Aggarwal | 192fac1 | 2012-07-25 16:44:55 -0700 | [diff] [blame] | 1268 | // fragment has a chance to delete the conversation, animating it away. |
| 1269 | |
mindyp | 84f7d32 | 2012-10-01 17:14:40 -0700 | [diff] [blame] | 1270 | // Update the conversation fragment if the current conversation is |
| 1271 | // deleted. |
Scott Kennedy | 0d0f8b0 | 2012-10-12 15:18:18 -0700 | [diff] [blame] | 1272 | final Runnable operation = new Runnable() { |
| 1273 | @Override |
| 1274 | public void run() { |
| 1275 | delete(actionId, target, targetViews, action); |
| 1276 | } |
| 1277 | }; |
| 1278 | |
| 1279 | if (!showNextConversation(target, operation)) { |
| 1280 | // This method will be called again if the user selects an autoadvance option |
| 1281 | return; |
| 1282 | } |
| 1283 | |
Vikram Aggarwal | 192fac1 | 2012-07-25 16:44:55 -0700 | [diff] [blame] | 1284 | // The conversation list deletes and performs the action if it exists. |
| 1285 | final ConversationListFragment convListFragment = getConversationListFragment(); |
| 1286 | if (convListFragment != null) { |
| 1287 | LogUtils.d(LOG_TAG, "AAC.requestDelete: ListFragment is handling delete."); |
mindyp | 84f7d32 | 2012-10-01 17:14:40 -0700 | [diff] [blame] | 1288 | convListFragment.requestDelete(actionId, target, targetViews, action); |
Vikram Aggarwal | 192fac1 | 2012-07-25 16:44:55 -0700 | [diff] [blame] | 1289 | return; |
| 1290 | } |
mindyp | 84f7d32 | 2012-10-01 17:14:40 -0700 | [diff] [blame] | 1291 | // No visible UI element handled it on our behalf. Perform the action |
| 1292 | // ourself. |
Vikram Aggarwal | d503df4 | 2012-05-11 10:13:35 -0700 | [diff] [blame] | 1293 | action.performAction(); |
| 1294 | } |
| 1295 | |
mindyp | 84f7d32 | 2012-10-01 17:14:40 -0700 | [diff] [blame] | 1296 | @Override |
| 1297 | public void delete(int actionId, final Collection<Conversation> target, |
| 1298 | final DestructiveAction action) { |
| 1299 | delete(actionId, target, null, action); |
| 1300 | } |
| 1301 | |
Vikram Aggarwal | d503df4 | 2012-05-11 10:13:35 -0700 | [diff] [blame] | 1302 | /** |
| 1303 | * Requests that the action be performed and the UI state is updated to reflect the new change. |
| 1304 | * @param target |
| 1305 | * @param action |
| 1306 | */ |
Vikram Aggarwal | 4f4782b | 2012-05-30 08:39:09 -0700 | [diff] [blame] | 1307 | private void requestUpdate(final Collection<Conversation> target, |
Vikram Aggarwal | d503df4 | 2012-05-11 10:13:35 -0700 | [diff] [blame] | 1308 | final DestructiveAction action) { |
| 1309 | action.performAction(); |
| 1310 | refreshConversationList(); |
Vikram Aggarwal | 75daee5 | 2012-04-30 11:13:09 -0700 | [diff] [blame] | 1311 | } |
Vikram Aggarwal | 6fbc87a | 2012-03-15 15:24:00 -0700 | [diff] [blame] | 1312 | |
| 1313 | @Override |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1314 | public void onPrepareDialog(int id, Dialog dialog, Bundle bundle) { |
| 1315 | // TODO(viki): Auto-generated method stub |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | @Override |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1319 | public boolean onPrepareOptionsMenu(Menu menu) { |
Andy Huang | d736a38 | 2012-08-29 13:08:58 -0700 | [diff] [blame] | 1320 | return mActionBarView.onPrepareOptionsMenu(menu); |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1321 | } |
| 1322 | |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 1323 | @Override |
| 1324 | public void onPause() { |
| 1325 | isLoaderInitialized = false; |
Paul Westbrook | 6ead20d | 2012-03-19 14:48:14 -0700 | [diff] [blame] | 1326 | enableNotifications(); |
Paul Westbrook | 94e440d | 2012-02-24 11:03:47 -0800 | [diff] [blame] | 1327 | } |
| 1328 | |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1329 | @Override |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1330 | public void onResume() { |
Paul Westbrook | 6ead20d | 2012-03-19 14:48:14 -0700 | [diff] [blame] | 1331 | // Register the receiver that will prevent the status receiver from |
| 1332 | // displaying its notification icon as long as we're running. |
| 1333 | // The SupressNotificationReceiver will block the broadcast if we're looking at the folder |
| 1334 | // that the notification was received for. |
| 1335 | disableNotifications(); |
Andy Huang | 1ee96b2 | 2012-08-24 20:19:53 -0700 | [diff] [blame] | 1336 | |
| 1337 | mSafeToModifyFragments = true; |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1338 | } |
| 1339 | |
| 1340 | @Override |
| 1341 | public void onSaveInstanceState(Bundle outState) { |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame] | 1342 | mViewMode.handleSaveInstanceState(outState); |
Vikram Aggarwal | 6c51158 | 2012-02-27 10:59:47 -0800 | [diff] [blame] | 1343 | if (mAccount != null) { |
| 1344 | LogUtils.d(LOG_TAG, "Saving the account now"); |
| 1345 | outState.putParcelable(SAVED_ACCOUNT, mAccount); |
| 1346 | } |
Mindy Pereira | 5e478d2 | 2012-03-26 18:04:58 -0700 | [diff] [blame] | 1347 | if (mFolder != null) { |
| 1348 | outState.putParcelable(SAVED_FOLDER, mFolder); |
Vikram Aggarwal | 8b15263 | 2012-02-03 14:58:45 -0800 | [diff] [blame] | 1349 | } |
Vikram Aggarwal | f334140 | 2012-08-07 10:09:38 -0700 | [diff] [blame] | 1350 | // If this is a search activity, let's store the search query term as well. |
| 1351 | if (ConversationListContext.isSearchResult(mConvListContext)) { |
| 1352 | outState.putString(SAVED_QUERY, mConvListContext.searchQuery); |
| 1353 | } |
Vikram Aggarwal | 49e0e99 | 2012-09-21 13:53:15 -0700 | [diff] [blame] | 1354 | if (mCurrentConversation != null && mViewMode.isConversationMode()) { |
Mindy Pereira | 26f23fc | 2012-03-27 10:26:04 -0700 | [diff] [blame] | 1355 | outState.putParcelable(SAVED_CONVERSATION, mCurrentConversation); |
| 1356 | } |
Andy Huang | 4556a44 | 2012-03-30 16:42:05 -0700 | [diff] [blame] | 1357 | if (!mSelectedSet.isEmpty()) { |
Vikram Aggarwal | cabd3f2 | 2012-04-19 10:14:41 -0700 | [diff] [blame] | 1358 | outState.putParcelable(SAVED_SELECTED_SET, mSelectedSet); |
Andy Huang | 4556a44 | 2012-03-30 16:42:05 -0700 | [diff] [blame] | 1359 | } |
Mindy Pereira | d3367499 | 2012-06-25 16:26:30 -0700 | [diff] [blame] | 1360 | if (mToastBar.getVisibility() == View.VISIBLE) { |
| 1361 | outState.putParcelable(SAVED_TOAST_BAR_OP, mToastBar.getOperation()); |
| 1362 | } |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame] | 1363 | final ConversationListFragment convListFragment = getConversationListFragment(); |
Mindy Pereira | d3367499 | 2012-06-25 16:26:30 -0700 | [diff] [blame] | 1364 | if (convListFragment != null) { |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame] | 1365 | convListFragment.getAnimatedAdapter().onSaveInstanceState(outState); |
Mindy Pereira | d3367499 | 2012-06-25 16:26:30 -0700 | [diff] [blame] | 1366 | } |
Andy Huang | 1ee96b2 | 2012-08-24 20:19:53 -0700 | [diff] [blame] | 1367 | mSafeToModifyFragments = false; |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame] | 1368 | outState.putString(SAVED_HIERARCHICAL_FOLDER, |
| 1369 | (mFolderListFolder != null) ? Folder.toString(mFolderListFolder) : null); |
Andy Huang | 1ee96b2 | 2012-08-24 20:19:53 -0700 | [diff] [blame] | 1370 | } |
| 1371 | |
| 1372 | /** |
| 1373 | * @see #mSafeToModifyFragments |
| 1374 | */ |
| 1375 | protected boolean safeToModifyFragments() { |
| 1376 | return mSafeToModifyFragments; |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1377 | } |
| 1378 | |
| 1379 | @Override |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 1380 | public void onSearchRequested(String query) { |
| 1381 | Intent intent = new Intent(); |
| 1382 | intent.setAction(Intent.ACTION_SEARCH); |
| 1383 | intent.putExtra(ConversationListContext.EXTRA_SEARCH_QUERY, query); |
| 1384 | intent.putExtra(Utils.EXTRA_ACCOUNT, mAccount); |
| 1385 | intent.setComponent(mActivity.getComponentName()); |
Vikram Aggarwal | b17cbc0 | 2012-04-06 15:41:46 -0700 | [diff] [blame] | 1386 | mActionBarView.collapseSearch(); |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 1387 | mActivity.startActivity(intent); |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1388 | } |
| 1389 | |
| 1390 | @Override |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1391 | public void onStop() { |
Mindy Pereira | 49e5dbe | 2012-07-12 11:47:54 -0700 | [diff] [blame] | 1392 | if (mEnableShareIntents != null) { |
| 1393 | mEnableShareIntents.cancel(true); |
| 1394 | } |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1395 | } |
| 1396 | |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1397 | @Override |
| 1398 | public void onDestroy() { |
| 1399 | // unregister the ViewPager's observer on the conversation cursor |
| 1400 | mPagerController.onDestroy(); |
Mindy Pereira | 641de65 | 2012-08-02 15:21:50 -0700 | [diff] [blame] | 1401 | mActionBarView.onDestroy(); |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 1402 | mRecentFolderList.destroy(); |
Andy Huang | 4e0158f | 2012-08-07 21:06:01 -0700 | [diff] [blame] | 1403 | mDestroyed = true; |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1404 | } |
| 1405 | |
Vikram Aggarwal | fa131a2 | 2012-02-02 13:56:22 -0800 | [diff] [blame] | 1406 | /** |
Vikram Aggarwal | 93dc202 | 2012-11-05 13:36:57 -0800 | [diff] [blame] | 1407 | * Set the Action Bar icon according to the mode. The Action Bar icon can contain a back button |
| 1408 | * or not. The individual controller is responsible for changing the icon based on the mode. |
| 1409 | */ |
| 1410 | protected abstract void resetActionBarIcon(); |
| 1411 | |
| 1412 | /** |
Mindy Pereira | 161f50d | 2012-02-28 15:47:19 -0800 | [diff] [blame] | 1413 | * {@inheritDoc} Subclasses must override this to listen to mode changes |
| 1414 | * from the ViewMode. Subclasses <b>must</b> call the parent's |
| 1415 | * onViewModeChanged since the parent will handle common state changes. |
Vikram Aggarwal | fa131a2 | 2012-02-02 13:56:22 -0800 | [diff] [blame] | 1416 | */ |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1417 | @Override |
Vikram Aggarwal | fa131a2 | 2012-02-02 13:56:22 -0800 | [diff] [blame] | 1418 | public void onViewModeChanged(int newMode) { |
Vikram Aggarwal | 49e0e99 | 2012-09-21 13:53:15 -0700 | [diff] [blame] | 1419 | // When we step away from the conversation mode, we don't have a current conversation |
| 1420 | // anymore. Let's blank it out so clients calling getCurrentConversation are not misled. |
| 1421 | if (!ViewMode.isConversationMode(newMode)) { |
| 1422 | setCurrentConversation(null); |
| 1423 | } |
Vikram Aggarwal | 93dc202 | 2012-11-05 13:36:57 -0800 | [diff] [blame] | 1424 | // If the viewmode is not set, preserve existing icon. |
| 1425 | if (newMode != ViewMode.UNKNOWN) { |
| 1426 | resetActionBarIcon(); |
| 1427 | } |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1428 | } |
| 1429 | |
Andy Huang | 3825f3d | 2012-08-29 16:44:12 -0700 | [diff] [blame] | 1430 | public void disablePagerUpdates() { |
| 1431 | mPagerController.stopListening(); |
| 1432 | } |
| 1433 | |
Andy Huang | 4e0158f | 2012-08-07 21:06:01 -0700 | [diff] [blame] | 1434 | public boolean isDestroyed() { |
| 1435 | return mDestroyed; |
| 1436 | } |
| 1437 | |
mindyp | 54f120f | 2012-08-28 13:10:33 -0700 | [diff] [blame] | 1438 | @Override |
| 1439 | public void commitDestructiveActions(boolean animate) { |
mindyp | c6adce3 | 2012-08-22 18:46:42 -0700 | [diff] [blame] | 1440 | ConversationListFragment fragment = getConversationListFragment(); |
Mindy Pereira | 1e2573b | 2012-04-17 14:34:36 -0700 | [diff] [blame] | 1441 | if (fragment != null) { |
mindyp | c6adce3 | 2012-08-22 18:46:42 -0700 | [diff] [blame] | 1442 | fragment.commitDestructiveActions(animate); |
Mindy Pereira | 1e2573b | 2012-04-17 14:34:36 -0700 | [diff] [blame] | 1443 | } |
| 1444 | } |
| 1445 | |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1446 | @Override |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1447 | public void onWindowFocusChanged(boolean hasFocus) { |
Vikram Aggarwal | 69b5c30 | 2012-09-05 11:11:13 -0700 | [diff] [blame] | 1448 | final ConversationListFragment convList = getConversationListFragment(); |
Paul Westbrook | 9f119c7 | 2012-04-24 16:10:59 -0700 | [diff] [blame] | 1449 | if (hasFocus && convList != null && convList.isVisible()) { |
| 1450 | // The conversation list is visible. |
Vikram Aggarwal | 69b5c30 | 2012-09-05 11:11:13 -0700 | [diff] [blame] | 1451 | informCursorVisiblity(true); |
Paul Westbrook | 9f119c7 | 2012-04-24 16:10:59 -0700 | [diff] [blame] | 1452 | } |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1453 | } |
| 1454 | |
Vikram Aggarwal | ca716f1 | 2012-08-20 11:11:48 -0700 | [diff] [blame] | 1455 | /** |
| 1456 | * Set the account, and carry out all the account-related changes that rely on this. |
| 1457 | * @param account |
| 1458 | */ |
Mindy Pereira | 75181e8 | 2012-04-18 08:17:13 -0700 | [diff] [blame] | 1459 | private void setAccount(Account account) { |
Andy Huang | b9ca979 | 2012-05-18 15:31:49 -0700 | [diff] [blame] | 1460 | if (account == null) { |
| 1461 | LogUtils.w(LOG_TAG, new Error(), |
| 1462 | "AAC ignoring null (presumably invalid) account restoration"); |
| 1463 | return; |
| 1464 | } |
Andy Huang | b114841 | 2012-05-19 00:16:30 -0700 | [diff] [blame] | 1465 | LogUtils.d(LOG_TAG, "AbstractActivityController.setAccount(): account = %s", account.uri); |
Vikram Aggarwal | 91e8737 | 2012-05-18 15:36:04 -0700 | [diff] [blame] | 1466 | mAccount = account; |
Vikram Aggarwal | ca716f1 | 2012-08-20 11:11:48 -0700 | [diff] [blame] | 1467 | // Only change AAC state here. Do *not* modify any other object's state. The object |
| 1468 | // should listen on account changes. |
| 1469 | restartOptionalLoader(LOADER_RECENT_FOLDERS); |
| 1470 | mActivity.invalidateOptionsMenu(); |
| 1471 | disableNotificationsOnAccountChange(mAccount); |
| 1472 | restartOptionalLoader(LOADER_ACCOUNT_UPDATE_CURSOR); |
| 1473 | MailAppProvider.getInstance().setLastViewedAccount(mAccount.uri.toString()); |
| 1474 | |
Vikram Aggarwal | 91e8737 | 2012-05-18 15:36:04 -0700 | [diff] [blame] | 1475 | if (account.settings == null) { |
| 1476 | LogUtils.w(LOG_TAG, new Error(), "AAC ignoring account with null settings."); |
| 1477 | return; |
| 1478 | } |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 1479 | mAccountObservers.notifyChanged(); |
Vikram Aggarwal | 34f7b23 | 2012-10-17 13:32:23 -0700 | [diff] [blame] | 1480 | perhapsEnterWaitMode(); |
Mindy Pereira | 75181e8 | 2012-04-18 08:17:13 -0700 | [diff] [blame] | 1481 | } |
| 1482 | |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1483 | /** |
Mindy Pereira | 161f50d | 2012-02-28 15:47:19 -0800 | [diff] [blame] | 1484 | * Restore the state from the previous bundle. Subclasses should call this |
| 1485 | * method from the parent class, since it performs important UI |
| 1486 | * initialization. |
| 1487 | * |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1488 | * @param savedState |
| 1489 | */ |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1490 | @Override |
| 1491 | public void onRestoreInstanceState(Bundle savedState) { |
| 1492 | LogUtils.d(LOG_TAG, "IN AAC.onRestoreInstanceState"); |
| 1493 | if (savedState.containsKey(SAVED_CONVERSATION)) { |
| 1494 | // Open the conversation. |
Andy Huang | 2bc8bc1 | 2012-11-12 17:24:25 -0800 | [diff] [blame] | 1495 | final Conversation conversation = savedState.getParcelable(SAVED_CONVERSATION); |
Paul Westbrook | 534e4a2 | 2012-04-25 03:46:29 -0700 | [diff] [blame] | 1496 | if (conversation != null && conversation.position < 0) { |
| 1497 | // Set the position to 0 on this conversation, as we don't know where it is |
| 1498 | // in the list |
| 1499 | conversation.position = 0; |
| 1500 | } |
Andy Huang | ed4fdf0 | 2012-07-26 17:12:50 -0700 | [diff] [blame] | 1501 | showConversation(conversation); |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1502 | } |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 1503 | |
Mindy Pereira | d3367499 | 2012-06-25 16:26:30 -0700 | [diff] [blame] | 1504 | if (savedState.containsKey(SAVED_TOAST_BAR_OP)) { |
Andy Huang | 2bc8bc1 | 2012-11-12 17:24:25 -0800 | [diff] [blame] | 1505 | ToastBarOperation op = savedState.getParcelable(SAVED_TOAST_BAR_OP); |
Mindy Pereira | d3367499 | 2012-06-25 16:26:30 -0700 | [diff] [blame] | 1506 | if (op != null) { |
| 1507 | if (op.getType() == ToastBarOperation.UNDO) { |
| 1508 | onUndoAvailable(op); |
Andrew Sapperstein | 9d7519d | 2012-07-16 14:03:53 -0700 | [diff] [blame] | 1509 | } else if (op.getType() == ToastBarOperation.ERROR) { |
| 1510 | onError(mFolder, true); |
Mindy Pereira | d3367499 | 2012-06-25 16:26:30 -0700 | [diff] [blame] | 1511 | } |
| 1512 | } |
| 1513 | } |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame] | 1514 | final String folderString = savedState.getString(SAVED_HIERARCHICAL_FOLDER, null); |
| 1515 | if (!TextUtils.isEmpty(folderString)) { |
| 1516 | mFolderListFolder = Folder.fromString(folderString); |
| 1517 | } |
| 1518 | final ConversationListFragment convListFragment = getConversationListFragment(); |
Mindy Pereira | d3367499 | 2012-06-25 16:26:30 -0700 | [diff] [blame] | 1519 | if (convListFragment != null) { |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame] | 1520 | convListFragment.getAnimatedAdapter().onRestoreInstanceState(savedState); |
Mindy Pereira | d3367499 | 2012-06-25 16:26:30 -0700 | [diff] [blame] | 1521 | } |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 1522 | /** |
| 1523 | * Restore the state of selected conversations. This needs to be done after the correct mode |
| 1524 | * is set and the action bar is fully initialized. If not, several key pieces of state |
| 1525 | * information will be missing, and the split views may not be initialized correctly. |
| 1526 | * @param savedState |
| 1527 | */ |
Andy Huang | 4556a44 | 2012-03-30 16:42:05 -0700 | [diff] [blame] | 1528 | restoreSelectedConversations(savedState); |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1529 | } |
| 1530 | |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame] | 1531 | /** |
| 1532 | * Handle an intent to open the app. This method is called only when there is no saved state, |
| 1533 | * so we need to set state that wasn't set before. It is correct to change the viewmode here |
| 1534 | * since it has not been previously set. |
| 1535 | * @param intent |
| 1536 | */ |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1537 | private void handleIntent(Intent intent) { |
| 1538 | boolean handled = false; |
| 1539 | if (Intent.ACTION_VIEW.equals(intent.getAction())) { |
| 1540 | if (intent.hasExtra(Utils.EXTRA_ACCOUNT)) { |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame] | 1541 | setAccount(Account.newinstance(intent.getStringExtra(Utils.EXTRA_ACCOUNT))); |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1542 | } |
Andy Huang | b9ca979 | 2012-05-18 15:31:49 -0700 | [diff] [blame] | 1543 | if (mAccount == null) { |
| 1544 | return; |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1545 | } |
Vikram Aggarwal | 1a249e0 | 2012-08-03 16:19:33 -0700 | [diff] [blame] | 1546 | final boolean isConversationMode = intent.hasExtra(Utils.EXTRA_CONVERSATION); |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame] | 1547 | if (isConversationMode && mViewMode.getMode() == ViewMode.UNKNOWN) { |
Vikram Aggarwal | 1a249e0 | 2012-08-03 16:19:33 -0700 | [diff] [blame] | 1548 | mViewMode.enterConversationMode(); |
| 1549 | } else { |
| 1550 | mViewMode.enterConversationListMode(); |
| 1551 | } |
Vikram Aggarwal | 1672ff8 | 2012-09-21 10:15:22 -0700 | [diff] [blame] | 1552 | final Folder folder = intent.hasExtra(Utils.EXTRA_FOLDER) ? |
| 1553 | Folder.fromString(intent.getStringExtra(Utils.EXTRA_FOLDER)) : null; |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1554 | if (folder != null) { |
| 1555 | onFolderChanged(folder); |
| 1556 | handled = true; |
| 1557 | } |
| 1558 | |
Vikram Aggarwal | 1a249e0 | 2012-08-03 16:19:33 -0700 | [diff] [blame] | 1559 | if (isConversationMode) { |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1560 | // Open the conversation. |
| 1561 | LogUtils.d(LOG_TAG, "SHOW THE CONVERSATION at %s", |
| 1562 | intent.getParcelableExtra(Utils.EXTRA_CONVERSATION)); |
Paul Westbrook | a916191 | 2012-04-24 10:10:04 -0700 | [diff] [blame] | 1563 | final Conversation conversation = |
Andy Huang | 2bc8bc1 | 2012-11-12 17:24:25 -0800 | [diff] [blame] | 1564 | intent.getParcelableExtra(Utils.EXTRA_CONVERSATION); |
Paul Westbrook | a916191 | 2012-04-24 10:10:04 -0700 | [diff] [blame] | 1565 | if (conversation != null && conversation.position < 0) { |
| 1566 | // Set the position to 0 on this conversation, as we don't know where it is |
| 1567 | // in the list |
| 1568 | conversation.position = 0; |
| 1569 | } |
Andy Huang | 980aaea | 2012-07-26 17:22:19 -0700 | [diff] [blame] | 1570 | showConversation(conversation); |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1571 | handled = true; |
| 1572 | } |
| 1573 | |
| 1574 | if (!handled) { |
Vikram Aggarwal | 0c3c205 | 2012-09-21 11:06:28 -0700 | [diff] [blame] | 1575 | // We have an account, but nothing else: load the default inbox. |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1576 | loadAccountInbox(); |
| 1577 | } |
| 1578 | } else if (Intent.ACTION_SEARCH.equals(intent.getAction())) { |
| 1579 | if (intent.hasExtra(Utils.EXTRA_ACCOUNT)) { |
Vikram Aggarwal | f0ef230 | 2012-11-07 14:53:34 -0800 | [diff] [blame] | 1580 | mHaveSearchResults = false; |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1581 | // Save this search query for future suggestions. |
| 1582 | final String query = intent.getStringExtra(SearchManager.QUERY); |
| 1583 | final String authority = mContext.getString(R.string.suggestions_authority); |
Vikram Aggarwal | 2b703c6 | 2012-09-18 13:54:15 -0700 | [diff] [blame] | 1584 | final SearchRecentSuggestions suggestions = new SearchRecentSuggestions( |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1585 | mContext, authority, SuggestionsProvider.MODE); |
| 1586 | suggestions.saveRecentQuery(query, null); |
Vikram Aggarwal | f0ef230 | 2012-11-07 14:53:34 -0800 | [diff] [blame] | 1587 | setAccount((Account) intent.getParcelableExtra(Utils.EXTRA_ACCOUNT)); |
| 1588 | fetchSearchFolder(intent); |
| 1589 | if (shouldEnterSearchConvMode()) { |
Mindy Pereira | ac25482 | 2012-06-18 10:46:43 -0700 | [diff] [blame] | 1590 | mViewMode.enterSearchResultsConversationMode(); |
| 1591 | } else { |
| 1592 | mViewMode.enterSearchResultsListMode(); |
| 1593 | } |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1594 | } else { |
| 1595 | LogUtils.e(LOG_TAG, "Missing account extra from search intent. Finishing"); |
| 1596 | mActivity.finish(); |
| 1597 | } |
| 1598 | } |
| 1599 | if (mAccount != null) { |
| 1600 | restartOptionalLoader(LOADER_ACCOUNT_UPDATE_CURSOR); |
| 1601 | } |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1602 | } |
| 1603 | |
Andy Huang | 4556a44 | 2012-03-30 16:42:05 -0700 | [diff] [blame] | 1604 | /** |
Vikram Aggarwal | f0ef230 | 2012-11-07 14:53:34 -0800 | [diff] [blame] | 1605 | * Returns true if we should enter conversation mode with search. |
| 1606 | */ |
| 1607 | protected final boolean shouldEnterSearchConvMode() { |
| 1608 | return mHaveSearchResults && Utils.showTwoPaneSearchResults(mActivity.getActivityContext()); |
| 1609 | } |
| 1610 | |
| 1611 | /** |
Andy Huang | 4556a44 | 2012-03-30 16:42:05 -0700 | [diff] [blame] | 1612 | * Copy any selected conversations stored in the saved bundle into our selection set, |
| 1613 | * triggering {@link ConversationSetObserver} callbacks as our selection set changes. |
| 1614 | * |
| 1615 | */ |
Vikram Aggarwal | 4eb5271 | 2012-06-19 16:24:50 -0700 | [diff] [blame] | 1616 | private final void restoreSelectedConversations(Bundle savedState) { |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 1617 | if (savedState == null) { |
Andy Huang | 4556a44 | 2012-03-30 16:42:05 -0700 | [diff] [blame] | 1618 | mSelectedSet.clear(); |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 1619 | return; |
| 1620 | } |
Vikram Aggarwal | cabd3f2 | 2012-04-19 10:14:41 -0700 | [diff] [blame] | 1621 | final ConversationSelectionSet selectedSet = savedState.getParcelable(SAVED_SELECTED_SET); |
Andy Huang | 4556a44 | 2012-03-30 16:42:05 -0700 | [diff] [blame] | 1622 | if (selectedSet == null || selectedSet.isEmpty()) { |
| 1623 | mSelectedSet.clear(); |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 1624 | return; |
| 1625 | } |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1626 | |
| 1627 | // putAll will take care of calling our registered onSetPopulated method |
Vikram Aggarwal | cabd3f2 | 2012-04-19 10:14:41 -0700 | [diff] [blame] | 1628 | mSelectedSet.putAll(selectedSet); |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 1629 | } |
| 1630 | |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1631 | @Override |
Andy Huang | 5895f7b | 2012-06-01 17:07:20 -0700 | [diff] [blame] | 1632 | public SubjectDisplayChanger getSubjectDisplayChanger() { |
| 1633 | return mActionBarView; |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1634 | } |
| 1635 | |
Vikram Aggarwal | 17f373e | 2012-09-17 15:49:32 -0700 | [diff] [blame] | 1636 | private final void showConversation(Conversation conversation) { |
Andy Huang | 1ee96b2 | 2012-08-24 20:19:53 -0700 | [diff] [blame] | 1637 | showConversation(conversation, false /* inLoaderCallbacks */); |
| 1638 | } |
| 1639 | |
Vikram Aggarwal | ec5cbf7 | 2012-03-08 15:10:35 -0800 | [diff] [blame] | 1640 | /** |
Vikram Aggarwal | 49e0e99 | 2012-09-21 13:53:15 -0700 | [diff] [blame] | 1641 | * Show the conversation provided in the arguments. It is safe to pass a null conversation |
| 1642 | * object, which is a signal to back out of conversation view mode. |
| 1643 | * Child classes must call super.showConversation() <b>before</b> their own implementations. |
| 1644 | * @param conversation |
| 1645 | * @param inLoaderCallbacks true if the method is called as a result of |
| 1646 | * {@link #onLoadFinished(Loader, Cursor)} |
Vikram Aggarwal | ec5cbf7 | 2012-03-08 15:10:35 -0800 | [diff] [blame] | 1647 | */ |
Andy Huang | 1ee96b2 | 2012-08-24 20:19:53 -0700 | [diff] [blame] | 1648 | protected void showConversation(Conversation conversation, boolean inLoaderCallbacks) { |
Vikram Aggarwal | c67182d | 2012-04-03 14:35:06 -0700 | [diff] [blame] | 1649 | // Set the current conversation just in case it wasn't already set. |
| 1650 | setCurrentConversation(conversation); |
Vikram Aggarwal | 0f14273 | 2012-08-24 09:39:34 -0700 | [diff] [blame] | 1651 | // Add the folder that we were viewing to the recent folders list. |
| 1652 | // TODO: this may need to be fine tuned. If this is the signal that is indicating that |
| 1653 | // the list is shown to the user, this could fire in one pane if the user goes directly |
| 1654 | // to a conversation |
| 1655 | updateRecentFolderList(); |
Vikram Aggarwal | ec5cbf7 | 2012-03-08 15:10:35 -0800 | [diff] [blame] | 1656 | } |
| 1657 | |
Vikram Aggarwal | e128fc2 | 2012-04-04 12:33:34 -0700 | [diff] [blame] | 1658 | /** |
Paul Westbrook | 2d50bcd | 2012-04-10 11:53:47 -0700 | [diff] [blame] | 1659 | * Children can override this method, but they must call super.showWaitForInitialization(). |
| 1660 | * {@inheritDoc} |
| 1661 | */ |
| 1662 | @Override |
| 1663 | public void showWaitForInitialization() { |
| 1664 | mViewMode.enterWaitingForInitializationMode(); |
Vikram Aggarwal | a3f43d4 | 2012-10-25 16:21:30 -0700 | [diff] [blame] | 1665 | mWaitFragment = WaitFragment.newInstance(mAccount); |
Paul Westbrook | 2d50bcd | 2012-04-10 11:53:47 -0700 | [diff] [blame] | 1666 | } |
| 1667 | |
Vikram Aggarwal | dd6a7ce | 2012-10-22 15:45:57 -0700 | [diff] [blame] | 1668 | private void updateWaitMode() { |
Paul Westbrook | 2d50bcd | 2012-04-10 11:53:47 -0700 | [diff] [blame] | 1669 | final FragmentManager manager = mActivity.getFragmentManager(); |
| 1670 | final WaitFragment waitFragment = |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 1671 | (WaitFragment)manager.findFragmentByTag(TAG_WAIT); |
Paul Westbrook | 2d50bcd | 2012-04-10 11:53:47 -0700 | [diff] [blame] | 1672 | if (waitFragment != null) { |
| 1673 | waitFragment.updateAccount(mAccount); |
| 1674 | } |
| 1675 | } |
| 1676 | |
Vikram Aggarwal | a3f43d4 | 2012-10-25 16:21:30 -0700 | [diff] [blame] | 1677 | /** |
| 1678 | * Remove the "Waiting for Initialization" fragment. Child classes are free to override this |
| 1679 | * method, though they must call the parent implementation <b>after</b> they do anything. |
| 1680 | */ |
| 1681 | protected void hideWaitForInitialization() { |
| 1682 | mWaitFragment = null; |
| 1683 | } |
| 1684 | |
| 1685 | /** |
| 1686 | * Use the instance variable and the wait fragment's tag to get the wait fragment. This is |
| 1687 | * far superior to using the value of mWaitFragment, which might be invalid or might refer |
| 1688 | * to a fragment after it has been destroyed. |
| 1689 | * @return |
| 1690 | */ |
| 1691 | protected final WaitFragment getWaitFragment() { |
| 1692 | final FragmentManager manager = mActivity.getFragmentManager(); |
| 1693 | final WaitFragment waitFrag = (WaitFragment) manager.findFragmentByTag(TAG_WAIT); |
| 1694 | if (waitFrag != null) { |
| 1695 | // The Fragment Manager knows better, so use its instance. |
| 1696 | mWaitFragment = waitFrag; |
| 1697 | } |
| 1698 | return mWaitFragment; |
| 1699 | } |
Vikram Aggarwal | dd6a7ce | 2012-10-22 15:45:57 -0700 | [diff] [blame] | 1700 | |
Vikram Aggarwal | 48b2a6c | 2012-05-29 14:09:27 -0700 | [diff] [blame] | 1701 | /** |
| 1702 | * Returns true if we are waiting for the account to sync, and cannot show any folders or |
| 1703 | * conversation for the current account yet. |
Vikram Aggarwal | 48b2a6c | 2012-05-29 14:09:27 -0700 | [diff] [blame] | 1704 | */ |
Vikram Aggarwal | dd6a7ce | 2012-10-22 15:45:57 -0700 | [diff] [blame] | 1705 | private boolean inWaitMode() { |
Paul Westbrook | 2d50bcd | 2012-04-10 11:53:47 -0700 | [diff] [blame] | 1706 | final FragmentManager manager = mActivity.getFragmentManager(); |
Vikram Aggarwal | a3f43d4 | 2012-10-25 16:21:30 -0700 | [diff] [blame] | 1707 | final WaitFragment waitFragment = getWaitFragment(); |
Paul Westbrook | 2d50bcd | 2012-04-10 11:53:47 -0700 | [diff] [blame] | 1708 | if (waitFragment != null) { |
| 1709 | final Account fragmentAccount = waitFragment.getAccount(); |
Paul Westbrook | 339004b | 2012-11-05 17:13:51 -0800 | [diff] [blame] | 1710 | return fragmentAccount != null && fragmentAccount.uri.equals(mAccount.uri) && |
Paul Westbrook | 2d50bcd | 2012-04-10 11:53:47 -0700 | [diff] [blame] | 1711 | mViewMode.getMode() == ViewMode.WAITING_FOR_ACCOUNT_INITIALIZATION; |
| 1712 | } |
| 1713 | return false; |
| 1714 | } |
| 1715 | |
| 1716 | /** |
Vikram Aggarwal | e128fc2 | 2012-04-04 12:33:34 -0700 | [diff] [blame] | 1717 | * Children can override this method, but they must call super.showConversationList(). |
| 1718 | * {@inheritDoc} |
| 1719 | */ |
| 1720 | @Override |
| 1721 | public void showConversationList(ConversationListContext listContext) { |
| 1722 | } |
| 1723 | |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1724 | @Override |
Vikram Aggarwal | 17f373e | 2012-09-17 15:49:32 -0700 | [diff] [blame] | 1725 | public final void onConversationSelected(Conversation conversation, boolean inLoaderCallbacks) { |
mindyp | aa55bc9 | 2012-08-24 09:49:56 -0700 | [diff] [blame] | 1726 | // Only animate destructive actions if we are going to be showing the |
| 1727 | // conversation list when we show the next conversation. |
| 1728 | commitDestructiveActions(Utils.useTabletUI(mContext)); |
Andy Huang | 1ee96b2 | 2012-08-24 20:19:53 -0700 | [diff] [blame] | 1729 | showConversation(conversation, inLoaderCallbacks); |
| 1730 | } |
| 1731 | |
| 1732 | @Override |
| 1733 | public Conversation getCurrentConversation() { |
| 1734 | return mCurrentConversation; |
Vikram Aggarwal | 7d60288 | 2012-02-07 15:01:20 -0800 | [diff] [blame] | 1735 | } |
Mindy Pereira | 555140c | 2012-02-15 14:55:29 -0800 | [diff] [blame] | 1736 | |
Vikram Aggarwal | c67182d | 2012-04-03 14:35:06 -0700 | [diff] [blame] | 1737 | /** |
| 1738 | * Set the current conversation. This is the conversation on which all actions are performed. |
| 1739 | * Do not modify mCurrentConversation except through this method, which makes it easy to |
| 1740 | * perform common actions associated with changing the current conversation. |
| 1741 | * @param conversation |
| 1742 | */ |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1743 | @Override |
| 1744 | public void setCurrentConversation(Conversation conversation) { |
Vikram Aggarwal | 17f373e | 2012-09-17 15:49:32 -0700 | [diff] [blame] | 1745 | // Must be the first call because this sets conversation.position if a cursor is available. |
Andy Huang | 8883f22 | 2012-11-12 19:25:00 -0800 | [diff] [blame] | 1746 | mTracker.initialize(conversation); |
Vikram Aggarwal | 17f373e | 2012-09-17 15:49:32 -0700 | [diff] [blame] | 1747 | mCurrentConversation = conversation; |
Andy Huang | 7d64612 | 2012-09-05 19:41:44 -0700 | [diff] [blame] | 1748 | |
| 1749 | if (mCurrentConversation != null) { |
Yorke Lee | f807ba7 | 2012-09-20 17:18:05 -0700 | [diff] [blame] | 1750 | mActionBarView.setCurrentConversation(mCurrentConversation); |
Andy Huang | 7d64612 | 2012-09-05 19:41:44 -0700 | [diff] [blame] | 1751 | getSubjectDisplayChanger().setSubject(mCurrentConversation.subject); |
Yorke Lee | f807ba7 | 2012-09-20 17:18:05 -0700 | [diff] [blame] | 1752 | mActivity.invalidateOptionsMenu(); |
Andy Huang | 7d64612 | 2012-09-05 19:41:44 -0700 | [diff] [blame] | 1753 | } |
Mindy Pereira | 5040f1a | 2012-03-20 10:14:06 -0700 | [diff] [blame] | 1754 | } |
| 1755 | |
Vikram Aggarwal | a9b93f3 | 2012-02-23 14:51:58 -0800 | [diff] [blame] | 1756 | /** |
| 1757 | * {@inheritDoc} |
| 1758 | */ |
Mindy Pereira | 555140c | 2012-02-15 14:55:29 -0800 | [diff] [blame] | 1759 | @Override |
Vikram Aggarwal | 7dedb95 | 2012-02-16 16:10:23 -0800 | [diff] [blame] | 1760 | public Loader<Cursor> onCreateLoader(int id, Bundle args) { |
Vikram Aggarwal | fa4b47e | 2012-03-09 13:02:46 -0800 | [diff] [blame] | 1761 | switch (id) { |
| 1762 | case LOADER_ACCOUNT_CURSOR: |
Paul Westbrook | c2074c4 | 2012-03-22 15:26:58 -0700 | [diff] [blame] | 1763 | return new CursorLoader(mContext, MailAppProvider.getAccountsUri(), |
Vikram Aggarwal | fa4b47e | 2012-03-09 13:02:46 -0800 | [diff] [blame] | 1764 | UIProvider.ACCOUNTS_PROJECTION, null, null, null); |
| 1765 | case LOADER_FOLDER_CURSOR: |
Paul Westbrook | c7a070f | 2012-04-12 01:46:41 -0700 | [diff] [blame] | 1766 | final CursorLoader loader = new CursorLoader(mContext, mFolder.uri, |
| 1767 | UIProvider.FOLDERS_PROJECTION, null, null, null); |
| 1768 | loader.setUpdateThrottle(mFolderItemUpdateDelayMs); |
| 1769 | return loader; |
Vikram Aggarwal | fa4b47e | 2012-03-09 13:02:46 -0800 | [diff] [blame] | 1770 | case LOADER_RECENT_FOLDERS: |
Paul Westbrook | 91d1050 | 2012-04-13 12:01:39 -0700 | [diff] [blame] | 1771 | if (mAccount != null && mAccount.recentFolderListUri != null) { |
Paul Westbrook | ea4ee20 | 2012-03-12 14:12:33 -0700 | [diff] [blame] | 1772 | return new CursorLoader(mContext, mAccount.recentFolderListUri, |
| 1773 | UIProvider.FOLDERS_PROJECTION, null, null, null); |
| 1774 | } |
| 1775 | break; |
Mindy Pereira | ab48636 | 2012-03-21 18:18:53 -0700 | [diff] [blame] | 1776 | case LOADER_ACCOUNT_INBOX: |
Vikram Aggarwal | 025eba8 | 2012-05-08 10:45:30 -0700 | [diff] [blame] | 1777 | final Uri defaultInbox = Settings.getDefaultInboxUri(mAccount.settings); |
Vikram Aggarwal | 1e57e67 | 2012-05-07 14:48:24 -0700 | [diff] [blame] | 1778 | final Uri inboxUri = defaultInbox.equals(Uri.EMPTY) ? |
| 1779 | mAccount.folderListUri : defaultInbox; |
Paul Westbrook | 7496e82 | 2012-04-24 09:50:54 -0700 | [diff] [blame] | 1780 | LogUtils.d(LOG_TAG, "Loading the default inbox: %s", inboxUri); |
Paul Westbrook | 1220b6d | 2012-04-10 00:48:00 -0700 | [diff] [blame] | 1781 | if (inboxUri != null) { |
| 1782 | return new CursorLoader(mContext, inboxUri, UIProvider.FOLDERS_PROJECTION, null, |
| 1783 | null, null); |
| 1784 | } |
| 1785 | break; |
Mindy Pereira | ab48636 | 2012-03-21 18:18:53 -0700 | [diff] [blame] | 1786 | case LOADER_SEARCH: |
| 1787 | return Folder.forSearchResults(mAccount, |
| 1788 | args.getString(ConversationListContext.EXTRA_SEARCH_QUERY), |
| 1789 | mActivity.getActivityContext()); |
Paul Westbrook | 2d50bcd | 2012-04-10 11:53:47 -0700 | [diff] [blame] | 1790 | case LOADER_ACCOUNT_UPDATE_CURSOR: |
| 1791 | return new CursorLoader(mContext, mAccount.uri, UIProvider.ACCOUNTS_PROJECTION, |
| 1792 | null, null, null); |
Vikram Aggarwal | fa4b47e | 2012-03-09 13:02:46 -0800 | [diff] [blame] | 1793 | default: |
Paul Westbrook | ad6a275 | 2012-04-04 16:58:13 -0700 | [diff] [blame] | 1794 | LogUtils.wtf(LOG_TAG, "Loader returned unexpected id: %d", id); |
Mindy Pereira | 5cb0c3e | 2012-02-22 15:22:47 -0800 | [diff] [blame] | 1795 | } |
| 1796 | return null; |
Vikram Aggarwal | 7dedb95 | 2012-02-16 16:10:23 -0800 | [diff] [blame] | 1797 | } |
| 1798 | |
Paul Westbrook | b1f573c | 2012-04-06 11:38:28 -0700 | [diff] [blame] | 1799 | @Override |
| 1800 | public void onLoaderReset(Loader<Cursor> loader) { |
| 1801 | |
| 1802 | } |
| 1803 | |
Andy Huang | f9a7348 | 2012-03-13 15:54:02 -0700 | [diff] [blame] | 1804 | /** |
| 1805 | * {@link LoaderManager} currently has a bug in |
| 1806 | * {@link LoaderManager#restartLoader(int, Bundle, android.app.LoaderManager.LoaderCallbacks)} |
| 1807 | * where, if a previous onCreateLoader returned a null loader, this method will NPE. Work around |
| 1808 | * this bug by destroying any loaders that may have been created as null (essentially because |
| 1809 | * they are optional loads, and may not apply to a particular account). |
| 1810 | * <p> |
| 1811 | * A simple null check before restarting a loader will not work, because that would not |
| 1812 | * give the controller a chance to invalidate UI corresponding the prior loader result. |
| 1813 | * |
| 1814 | * @param id loader ID to safely restart |
Andy Huang | f9a7348 | 2012-03-13 15:54:02 -0700 | [diff] [blame] | 1815 | */ |
Vikram Aggarwal | 94c94de | 2012-04-04 15:38:28 -0700 | [diff] [blame] | 1816 | private void restartOptionalLoader(int id) { |
Andy Huang | f9a7348 | 2012-03-13 15:54:02 -0700 | [diff] [blame] | 1817 | final LoaderManager lm = mActivity.getLoaderManager(); |
| 1818 | lm.destroyLoader(id); |
Vikram Aggarwal | 94c94de | 2012-04-04 15:38:28 -0700 | [diff] [blame] | 1819 | lm.restartLoader(id, Bundle.EMPTY, this); |
| 1820 | } |
| 1821 | |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1822 | @Override |
| 1823 | public void registerConversationListObserver(DataSetObserver observer) { |
| 1824 | mConversationListObservable.registerObserver(observer); |
| 1825 | } |
| 1826 | |
| 1827 | @Override |
| 1828 | public void unregisterConversationListObserver(DataSetObserver observer) { |
| 1829 | mConversationListObservable.unregisterObserver(observer); |
| 1830 | } |
| 1831 | |
Andy Huang | 090db1e | 2012-07-25 13:25:28 -0700 | [diff] [blame] | 1832 | @Override |
| 1833 | public void registerFolderObserver(DataSetObserver observer) { |
| 1834 | mFolderObservable.registerObserver(observer); |
| 1835 | } |
| 1836 | |
| 1837 | @Override |
| 1838 | public void unregisterFolderObserver(DataSetObserver observer) { |
| 1839 | mFolderObservable.unregisterObserver(observer); |
| 1840 | } |
| 1841 | |
Andy Huang | 9d3fd92 | 2012-09-26 22:23:58 -0700 | [diff] [blame] | 1842 | @Override |
| 1843 | public void registerConversationLoadedObserver(DataSetObserver observer) { |
| 1844 | mPagerController.registerConversationLoadedObserver(observer); |
| 1845 | } |
| 1846 | |
| 1847 | @Override |
| 1848 | public void unregisterConversationLoadedObserver(DataSetObserver observer) { |
| 1849 | mPagerController.unregisterConversationLoadedObserver(observer); |
| 1850 | } |
| 1851 | |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 1852 | /** |
| 1853 | * Returns true if the number of accounts is different, or if the current account has been |
| 1854 | * removed from the device |
| 1855 | * @param accountCursor |
| 1856 | * @return |
| 1857 | */ |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 1858 | private boolean accountsUpdated(Cursor accountCursor) { |
| 1859 | // Check to see if the current account hasn't been set, or the account cursor is empty |
| 1860 | if (mAccount == null || !accountCursor.moveToFirst()) { |
Vikram Aggarwal | 6c51158 | 2012-02-27 10:59:47 -0800 | [diff] [blame] | 1861 | return true; |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 1862 | } |
| 1863 | |
| 1864 | // Check to see if the number of accounts are different, from the number we saw on the last |
| 1865 | // updated |
| 1866 | if (mCurrentAccountUris.size() != accountCursor.getCount()) { |
| 1867 | return true; |
| 1868 | } |
| 1869 | |
| 1870 | // Check to see if the account list is different or if the current account is not found in |
| 1871 | // the cursor. |
| 1872 | boolean foundCurrentAccount = false; |
Vikram Aggarwal | 7dedb95 | 2012-02-16 16:10:23 -0800 | [diff] [blame] | 1873 | do { |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 1874 | final Uri accountUri = |
| 1875 | Uri.parse(accountCursor.getString(UIProvider.ACCOUNT_URI_COLUMN)); |
| 1876 | if (!foundCurrentAccount && mAccount.uri.equals(accountUri)) { |
| 1877 | foundCurrentAccount = true; |
| 1878 | } |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 1879 | // Is there a new account that we do not know about? |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 1880 | if (!mCurrentAccountUris.contains(accountUri)) { |
| 1881 | return true; |
| 1882 | } |
Vikram Aggarwal | 7dedb95 | 2012-02-16 16:10:23 -0800 | [diff] [blame] | 1883 | } while (accountCursor.moveToNext()); |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 1884 | |
| 1885 | // As long as we found the current account, the list hasn't been updated |
| 1886 | return !foundCurrentAccount; |
Vikram Aggarwal | 7dedb95 | 2012-02-16 16:10:23 -0800 | [diff] [blame] | 1887 | } |
| 1888 | |
| 1889 | /** |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 1890 | * Updates accounts for the app. If the current account is missing, the first |
| 1891 | * 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] | 1892 | * |
Vikram Aggarwal | 6c51158 | 2012-02-27 10:59:47 -0800 | [diff] [blame] | 1893 | * @param accounts cursor into the AccountCache |
Vikram Aggarwal | 7dedb95 | 2012-02-16 16:10:23 -0800 | [diff] [blame] | 1894 | * @return true if the update was successful, false otherwise |
| 1895 | */ |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 1896 | private boolean updateAccounts(Cursor accounts) { |
Vikram Aggarwal | 7dedb95 | 2012-02-16 16:10:23 -0800 | [diff] [blame] | 1897 | if (accounts == null || !accounts.moveToFirst()) { |
| 1898 | return false; |
| 1899 | } |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 1900 | |
Vikram Aggarwal | a9b93f3 | 2012-02-23 14:51:58 -0800 | [diff] [blame] | 1901 | final Account[] allAccounts = Account.getAllAccounts(accounts); |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 1902 | // A match for the current account's URI in the list of accounts. |
| 1903 | Account currentFromList = null; |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 1904 | |
Vikram Aggarwal | 636c1a1 | 2012-09-22 16:02:40 -0700 | [diff] [blame] | 1905 | // Save the uris for the accounts and find the current account in the updated cursor. |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 1906 | mCurrentAccountUris.clear(); |
Vikram Aggarwal | 636c1a1 | 2012-09-22 16:02:40 -0700 | [diff] [blame] | 1907 | for (final Account account : allAccounts) { |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 1908 | LogUtils.d(LOG_TAG, "updateAccounts(%s)", account); |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 1909 | mCurrentAccountUris.add(account.uri); |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 1910 | if (mAccount != null && account.uri.equals(mAccount.uri)) { |
| 1911 | currentFromList = account; |
| 1912 | } |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 1913 | } |
| 1914 | |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 1915 | // 1. current account is already set and is in allAccounts: |
| 1916 | // 1a. It has changed -> load the updated account. |
| 1917 | // 2b. It is unchanged -> no-op |
Andy Huang | 0d64735 | 2012-03-21 21:48:16 -0700 | [diff] [blame] | 1918 | // 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] | 1919 | // 3. saved preference has an account -> pick that one |
Andy Huang | 0d64735 | 2012-03-21 21:48:16 -0700 | [diff] [blame] | 1920 | // 4. otherwise just pick first |
| 1921 | |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 1922 | boolean accountChanged = false; |
| 1923 | /// Assume case 4, initialize to first account, and see if we can find anything better. |
| 1924 | Account newAccount = allAccounts[0]; |
| 1925 | if (currentFromList != null) { |
| 1926 | // Case 1: Current account exists but has changed |
| 1927 | if (!currentFromList.equals(mAccount)) { |
| 1928 | newAccount = currentFromList; |
| 1929 | accountChanged = true; |
Andy Huang | 0d64735 | 2012-03-21 21:48:16 -0700 | [diff] [blame] | 1930 | } |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 1931 | // Case 1b: else, current account is unchanged: nothing to do. |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 1932 | } else { |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 1933 | // Case 2: Current account is not in allAccounts, the account needs to change. |
| 1934 | accountChanged = true; |
| 1935 | if (mAccount == null) { |
| 1936 | // Case 3: Check for last viewed account, and check if it exists in the list. |
| 1937 | final String lastAccountUri = MailAppProvider.getInstance().getLastViewedAccount(); |
| 1938 | if (lastAccountUri != null) { |
| 1939 | for (final Account account : allAccounts) { |
| 1940 | if (lastAccountUri.equals(account.uri.toString())) { |
| 1941 | newAccount = account; |
| 1942 | break; |
| 1943 | } |
Andy Huang | 0d64735 | 2012-03-21 21:48:16 -0700 | [diff] [blame] | 1944 | } |
| 1945 | } |
| 1946 | } |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 1947 | } |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 1948 | if (accountChanged) { |
| 1949 | onAccountChanged(newAccount); |
| 1950 | } |
| 1951 | // Whether we have updated the current account or not, we need to update the list of |
| 1952 | // accounts in the ActionBar. |
Vikram Aggarwal | a9b93f3 | 2012-02-23 14:51:58 -0800 | [diff] [blame] | 1953 | mActionBarView.setAccounts(allAccounts); |
| 1954 | return (allAccounts.length > 0); |
Vikram Aggarwal | 7dedb95 | 2012-02-16 16:10:23 -0800 | [diff] [blame] | 1955 | } |
| 1956 | |
Paul Westbrook | 6ead20d | 2012-03-19 14:48:14 -0700 | [diff] [blame] | 1957 | private void disableNotifications() { |
| 1958 | mNewEmailReceiver.activate(mContext, this); |
| 1959 | } |
| 1960 | |
| 1961 | private void enableNotifications() { |
| 1962 | mNewEmailReceiver.deactivate(); |
| 1963 | } |
| 1964 | |
| 1965 | private void disableNotificationsOnAccountChange(Account account) { |
| 1966 | // If the new mail suppression receiver is activated for a different account, we want to |
| 1967 | // activate it for the new account. |
| 1968 | if (mNewEmailReceiver.activated() && |
| 1969 | !mNewEmailReceiver.notificationsDisabledForAccount(account)) { |
| 1970 | // Deactivate the current receiver, otherwise multiple receivers may be registered. |
| 1971 | mNewEmailReceiver.deactivate(); |
| 1972 | mNewEmailReceiver.activate(mContext, this); |
| 1973 | } |
| 1974 | } |
| 1975 | |
Vikram Aggarwal | a9b93f3 | 2012-02-23 14:51:58 -0800 | [diff] [blame] | 1976 | /** |
| 1977 | * {@inheritDoc} |
| 1978 | */ |
Vikram Aggarwal | 7dedb95 | 2012-02-16 16:10:23 -0800 | [diff] [blame] | 1979 | @Override |
| 1980 | public void onLoadFinished(Loader<Cursor> loader, Cursor data) { |
Mindy Pereira | 5cb0c3e | 2012-02-22 15:22:47 -0800 | [diff] [blame] | 1981 | // We want to reinitialize only if we haven't ever been initialized, or |
| 1982 | // if the current account has vanished. |
Paul Westbrook | e3e8429 | 2012-03-05 16:19:30 -0800 | [diff] [blame] | 1983 | if (data == null) { |
Vikram Aggarwal | fa4b47e | 2012-03-09 13:02:46 -0800 | [diff] [blame] | 1984 | 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] | 1985 | } |
Vikram Aggarwal | fa4b47e | 2012-03-09 13:02:46 -0800 | [diff] [blame] | 1986 | switch (loader.getId()) { |
| 1987 | case LOADER_ACCOUNT_CURSOR: |
Vikram Aggarwal | 636c1a1 | 2012-09-22 16:02:40 -0700 | [diff] [blame] | 1988 | if (data == null) { |
| 1989 | // Nothing useful to do if we have no valid data. |
| 1990 | break; |
| 1991 | } |
| 1992 | if (data.getCount() == 0) { |
Paul Westbrook | 2388c5d | 2012-03-25 12:29:11 -0700 | [diff] [blame] | 1993 | // If an empty cursor is returned, the MailAppProvider is indicating that |
| 1994 | // no accounts have been specified. We want to navigate to the "add account" |
| 1995 | // activity that will handle the intent returned by the MailAppProvider |
| 1996 | |
| 1997 | // If the MailAppProvider believes that all accounts have been loaded, and the |
| 1998 | // account list is still empty, we want to prompt the user to add an account |
| 1999 | final Bundle extras = data.getExtras(); |
| 2000 | final boolean accountsLoaded = |
| 2001 | extras.getInt(AccountCursorExtraKeys.ACCOUNTS_LOADED) != 0; |
| 2002 | |
| 2003 | if (accountsLoaded) { |
| 2004 | final Intent noAccountIntent = MailAppProvider.getNoAccountIntent(mContext); |
| 2005 | if (noAccountIntent != null) { |
| 2006 | mActivity.startActivityForResult(noAccountIntent, |
| 2007 | ADD_ACCOUNT_REQUEST_CODE); |
| 2008 | } |
| 2009 | } |
| 2010 | } else { |
| 2011 | final boolean accountListUpdated = accountsUpdated(data); |
| 2012 | if (!isLoaderInitialized || accountListUpdated) { |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 2013 | isLoaderInitialized = updateAccounts(data); |
Paul Westbrook | 2388c5d | 2012-03-25 12:29:11 -0700 | [diff] [blame] | 2014 | } |
Vikram Aggarwal | fa4b47e | 2012-03-09 13:02:46 -0800 | [diff] [blame] | 2015 | } |
| 2016 | break; |
Paul Westbrook | 2d50bcd | 2012-04-10 11:53:47 -0700 | [diff] [blame] | 2017 | case LOADER_ACCOUNT_UPDATE_CURSOR: |
| 2018 | // We have gotten an update for current account. |
| 2019 | |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 2020 | // Make sure that this is an update for the current account |
Paul Westbrook | 2d50bcd | 2012-04-10 11:53:47 -0700 | [diff] [blame] | 2021 | if (data != null && data.moveToFirst()) { |
| 2022 | final Account updatedAccount = new Account(data); |
| 2023 | |
| 2024 | if (updatedAccount.uri.equals(mAccount.uri)) { |
Paul Westbrook | ca08fc1 | 2012-07-31 12:01:15 -0700 | [diff] [blame] | 2025 | // Keep a reference to the previous settings object |
| 2026 | final Settings previousSettings = mAccount.settings; |
| 2027 | |
Vikram Aggarwal | 7d81600 | 2012-04-17 17:06:41 -0700 | [diff] [blame] | 2028 | // Update the controller's reference to the current account |
Paul Westbrook | 2d50bcd | 2012-04-10 11:53:47 -0700 | [diff] [blame] | 2029 | mAccount = updatedAccount; |
Vikram Aggarwal | edc137c | 2012-04-24 13:40:58 -0700 | [diff] [blame] | 2030 | LogUtils.d(LOG_TAG, "AbstractActivityController.onLoadFinished(): " |
| 2031 | + "mAccount = %s", mAccount.uri); |
Paul Westbrook | ca08fc1 | 2012-07-31 12:01:15 -0700 | [diff] [blame] | 2032 | |
| 2033 | // Only notify about a settings change if something differs |
| 2034 | if (!Objects.equal(mAccount.settings, previousSettings)) { |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 2035 | mAccountObservers.notifyChanged(); |
Paul Westbrook | ca08fc1 | 2012-07-31 12:01:15 -0700 | [diff] [blame] | 2036 | } |
Vikram Aggarwal | 34f7b23 | 2012-10-17 13:32:23 -0700 | [diff] [blame] | 2037 | perhapsEnterWaitMode(); |
Paul Westbrook | 2d50bcd | 2012-04-10 11:53:47 -0700 | [diff] [blame] | 2038 | } else { |
| 2039 | LogUtils.e(LOG_TAG, "Got update for account: %s with current account: %s", |
| 2040 | updatedAccount.uri, mAccount.uri); |
| 2041 | // We need to restart the loader, so the correct account information will |
| 2042 | // be returned |
| 2043 | restartOptionalLoader(LOADER_ACCOUNT_UPDATE_CURSOR); |
| 2044 | } |
| 2045 | } |
| 2046 | break; |
Vikram Aggarwal | fa4b47e | 2012-03-09 13:02:46 -0800 | [diff] [blame] | 2047 | case LOADER_FOLDER_CURSOR: |
Vikram Aggarwal | fa4b47e | 2012-03-09 13:02:46 -0800 | [diff] [blame] | 2048 | // Check status of the cursor. |
Marc Blank | fd9d0b8 | 2012-04-23 16:01:51 -0700 | [diff] [blame] | 2049 | if (data != null && data.moveToFirst()) { |
Andy Huang | 090db1e | 2012-07-25 13:25:28 -0700 | [diff] [blame] | 2050 | final Folder folder = new Folder(data); |
Marc Blank | fd9d0b8 | 2012-04-23 16:01:51 -0700 | [diff] [blame] | 2051 | LogUtils.d(LOG_TAG, "FOLDER STATUS = %d", folder.syncStatus); |
Vikram Aggarwal | d00229d | 2012-09-20 12:31:44 -0700 | [diff] [blame] | 2052 | setHasFolderChanged(folder); |
Andy Huang | 090db1e | 2012-07-25 13:25:28 -0700 | [diff] [blame] | 2053 | mFolder = folder; |
| 2054 | mFolderObservable.notifyChanged(); |
Paul Westbrook | c808fac | 2012-02-22 16:42:18 -0800 | [diff] [blame] | 2055 | } else { |
Marc Blank | fd9d0b8 | 2012-04-23 16:01:51 -0700 | [diff] [blame] | 2056 | LogUtils.d(LOG_TAG, "Unable to get the folder %s", |
| 2057 | mFolder != null ? mAccount.name : ""); |
Mindy Pereira | 11dd5ef | 2012-03-10 15:10:18 -0800 | [diff] [blame] | 2058 | } |
Vikram Aggarwal | fa4b47e | 2012-03-09 13:02:46 -0800 | [diff] [blame] | 2059 | break; |
Vikram Aggarwal | fa4b47e | 2012-03-09 13:02:46 -0800 | [diff] [blame] | 2060 | case LOADER_RECENT_FOLDERS: |
Vikram Aggarwal | f9ba871 | 2012-09-23 11:42:39 -0700 | [diff] [blame] | 2061 | // Few recent folders and we are running on a phone? Populate the default recents. |
| 2062 | // The number of default recent folders is at least 2: every provider has at |
| 2063 | // least two folders, and the recent folder count never decreases. Having a single |
| 2064 | // recent folder is an erroneous case, and we can gracefully recover by populating |
| 2065 | // default recents. The default recents will not stomp on the existing value: it |
| 2066 | // will be shown in addition to the default folders: the max number of recent |
| 2067 | // folders is more than 1+num(defaultRecents). |
| 2068 | if (data != null && data.getCount() <= 1 && !Utils.useTabletUI(mContext)) { |
Vikram Aggarwal | 27d89ad | 2012-06-12 13:38:40 -0700 | [diff] [blame] | 2069 | final class PopulateDefault extends AsyncTask<Uri, Void, Void> { |
| 2070 | @Override |
| 2071 | protected Void doInBackground(Uri... uri) { |
| 2072 | // Asking for an update on the URI and ignore the result. |
| 2073 | final ContentResolver resolver = mContext.getContentResolver(); |
| 2074 | resolver.update(uri[0], null, null, null); |
| 2075 | return null; |
| 2076 | } |
| 2077 | } |
| 2078 | final Uri uri = mAccount.defaultRecentFolderListUri; |
| 2079 | LogUtils.v(LOG_TAG, "Default recents at %s", uri); |
| 2080 | new PopulateDefault().execute(uri); |
| 2081 | break; |
| 2082 | } |
| 2083 | LogUtils.v(LOG_TAG, "Reading recent folders from the cursor."); |
mindyp | 6f54e1b | 2012-10-09 09:54:08 -0700 | [diff] [blame] | 2084 | loadRecentFolders(data); |
Vikram Aggarwal | fa4b47e | 2012-03-09 13:02:46 -0800 | [diff] [blame] | 2085 | break; |
Mindy Pereira | ab48636 | 2012-03-21 18:18:53 -0700 | [diff] [blame] | 2086 | case LOADER_ACCOUNT_INBOX: |
Marc Blank | fd9d0b8 | 2012-04-23 16:01:51 -0700 | [diff] [blame] | 2087 | if (data != null && !data.isClosed() && data.moveToFirst()) { |
Mindy Pereira | 0e88e9f | 2012-03-25 13:47:41 -0700 | [diff] [blame] | 2088 | Folder inbox = new Folder(data); |
| 2089 | onFolderChanged(inbox); |
Mindy Pereira | b4a4328 | 2012-03-23 16:20:03 -0700 | [diff] [blame] | 2090 | // Just want to get the inbox, don't care about updates to it |
| 2091 | // as this will be tracked by the folder change listener. |
| 2092 | mActivity.getLoaderManager().destroyLoader(LOADER_ACCOUNT_INBOX); |
Mindy Pereira | 5ba3380 | 2012-03-26 16:30:11 -0700 | [diff] [blame] | 2093 | } else { |
| 2094 | LogUtils.d(LOG_TAG, "Unable to get the account inbox for account %s", |
| 2095 | mAccount != null ? mAccount.name : ""); |
Mindy Pereira | b4a4328 | 2012-03-23 16:20:03 -0700 | [diff] [blame] | 2096 | } |
Mindy Pereira | ab48636 | 2012-03-21 18:18:53 -0700 | [diff] [blame] | 2097 | break; |
| 2098 | case LOADER_SEARCH: |
Paul Westbrook | c4845c5 | 2012-08-29 21:48:43 -0700 | [diff] [blame] | 2099 | if (data != null && data.getCount() > 0) { |
| 2100 | data.moveToFirst(); |
Vikram Aggarwal | 2b703c6 | 2012-09-18 13:54:15 -0700 | [diff] [blame] | 2101 | final Folder search = new Folder(data); |
Paul Westbrook | c4845c5 | 2012-08-29 21:48:43 -0700 | [diff] [blame] | 2102 | updateFolder(search); |
| 2103 | mConvListContext = ConversationListContext.forSearchQuery(mAccount, mFolder, |
| 2104 | mActivity.getIntent() |
| 2105 | .getStringExtra(UIProvider.SearchQueryParameters.QUERY)); |
| 2106 | showConversationList(mConvListContext); |
| 2107 | mActivity.invalidateOptionsMenu(); |
Vikram Aggarwal | f0ef230 | 2012-11-07 14:53:34 -0800 | [diff] [blame] | 2108 | mHaveSearchResults = search.totalCount > 0; |
Paul Westbrook | c4845c5 | 2012-08-29 21:48:43 -0700 | [diff] [blame] | 2109 | mActivity.getLoaderManager().destroyLoader(LOADER_SEARCH); |
| 2110 | } else { |
| 2111 | LogUtils.e(LOG_TAG, "Null or empty cursor returned by LOADER_SEARCH loader"); |
| 2112 | } |
Mindy Pereira | ab48636 | 2012-03-21 18:18:53 -0700 | [diff] [blame] | 2113 | break; |
Vikram Aggarwal | 7dedb95 | 2012-02-16 16:10:23 -0800 | [diff] [blame] | 2114 | } |
| 2115 | } |
| 2116 | |
mindyp | 6f54e1b | 2012-10-09 09:54:08 -0700 | [diff] [blame] | 2117 | |
Vikram Aggarwal | c769422 | 2012-04-23 13:37:01 -0700 | [diff] [blame] | 2118 | /** |
| 2119 | * Destructive actions on Conversations. This class should only be created by controllers, and |
| 2120 | * clients should only require {@link DestructiveAction}s, not specific implementations of the. |
| 2121 | * Only the controllers should know what kind of destructive actions are being created. |
| 2122 | */ |
Mindy Pereira | de3e74a | 2012-07-24 09:43:10 -0700 | [diff] [blame] | 2123 | public class ConversationAction implements DestructiveAction { |
Vikram Aggarwal | acaa3c0 | 2012-04-24 12:45:27 -0700 | [diff] [blame] | 2124 | /** |
| 2125 | * The action to be performed. This is specified as the resource ID of the menu item |
| 2126 | * corresponding to this action: R.id.delete, R.id.report_spam, etc. |
| 2127 | */ |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 2128 | private final int mAction; |
Vikram Aggarwal | bc67bb1 | 2012-04-30 14:05:35 -0700 | [diff] [blame] | 2129 | /** The action will act upon these conversations */ |
Paul Westbrook | 77eee62 | 2012-07-10 13:41:57 -0700 | [diff] [blame] | 2130 | private final Collection<Conversation> mTarget; |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 2131 | /** Whether this destructive action has already been performed */ |
| 2132 | private boolean mCompleted; |
Vikram Aggarwal | c411395 | 2012-05-11 14:14:56 -0700 | [diff] [blame] | 2133 | /** Whether this is an action on the currently selected set. */ |
| 2134 | private final boolean mIsSelectedSet; |
Vikram Aggarwal | e8a8532 | 2012-04-24 09:01:38 -0700 | [diff] [blame] | 2135 | |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 2136 | /** |
| 2137 | * Create a listener object. action is one of four constants: R.id.y_button (archive), |
| 2138 | * R.id.delete , R.id.mute, and R.id.report_spam. |
| 2139 | * @param action |
Vikram Aggarwal | bc67bb1 | 2012-04-30 14:05:35 -0700 | [diff] [blame] | 2140 | * @param target Conversation that we want to apply the action to. |
Vikram Aggarwal | c411395 | 2012-05-11 14:14:56 -0700 | [diff] [blame] | 2141 | * @param isBatch whether the conversations are in the currently selected batch set. |
| 2142 | */ |
| 2143 | public ConversationAction(int action, Collection<Conversation> target, boolean isBatch) { |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 2144 | mAction = action; |
Paul Westbrook | 77eee62 | 2012-07-10 13:41:57 -0700 | [diff] [blame] | 2145 | mTarget = ImmutableList.copyOf(target); |
Vikram Aggarwal | c411395 | 2012-05-11 14:14:56 -0700 | [diff] [blame] | 2146 | mIsSelectedSet = isBatch; |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 2147 | } |
| 2148 | |
Vikram Aggarwal | acaa3c0 | 2012-04-24 12:45:27 -0700 | [diff] [blame] | 2149 | /** |
| 2150 | * The action common to child classes. This performs the action specified in the constructor |
| 2151 | * on the conversations given here. |
Vikram Aggarwal | acaa3c0 | 2012-04-24 12:45:27 -0700 | [diff] [blame] | 2152 | */ |
Vikram Aggarwal | bc67bb1 | 2012-04-30 14:05:35 -0700 | [diff] [blame] | 2153 | @Override |
| 2154 | public void performAction() { |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 2155 | if (isPerformed()) { |
| 2156 | return; |
| 2157 | } |
Mindy Pereira | 3cb28b5 | 2012-05-24 15:26:39 -0700 | [diff] [blame] | 2158 | boolean undoEnabled = mAccount.supportsCapability(AccountCapabilities.UNDO); |
Vikram Aggarwal | 7dd054e | 2012-05-21 14:43:10 -0700 | [diff] [blame] | 2159 | |
| 2160 | // Are we destroying the currently shown conversation? Show the next one. |
| 2161 | if (LogUtils.isLoggable(LOG_TAG, LogUtils.DEBUG)){ |
Vikram Aggarwal | 8742a61 | 2012-08-13 10:22:50 -0700 | [diff] [blame] | 2162 | LogUtils.d(LOG_TAG, "ConversationAction.performAction():" |
| 2163 | + "\nmTarget=%s\nCurrent=%s", |
Vikram Aggarwal | 7dd054e | 2012-05-21 14:43:10 -0700 | [diff] [blame] | 2164 | Conversation.toString(mTarget), mCurrentConversation); |
| 2165 | } |
Vikram Aggarwal | 7dd054e | 2012-05-21 14:43:10 -0700 | [diff] [blame] | 2166 | |
Paul Westbrook | e1221d2 | 2012-08-19 11:09:07 -0700 | [diff] [blame] | 2167 | if (mConversationListCursor == null) { |
| 2168 | LogUtils.e(LOG_TAG, "null ConversationCursor in ConversationAction.performAction():" |
| 2169 | + "\nmTarget=%s\nCurrent=%s", |
| 2170 | Conversation.toString(mTarget), mCurrentConversation); |
| 2171 | return; |
| 2172 | } |
| 2173 | |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 2174 | switch (mAction) { |
Mindy Pereira | 0692baf | 2012-03-23 17:34:31 -0700 | [diff] [blame] | 2175 | case R.id.archive: |
Vikram Aggarwal | 7dd054e | 2012-05-21 14:43:10 -0700 | [diff] [blame] | 2176 | LogUtils.d(LOG_TAG, "Archiving"); |
Vikram Aggarwal | bc67bb1 | 2012-04-30 14:05:35 -0700 | [diff] [blame] | 2177 | mConversationListCursor.archive(mContext, mTarget); |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 2178 | break; |
| 2179 | case R.id.delete: |
Vikram Aggarwal | 7dd054e | 2012-05-21 14:43:10 -0700 | [diff] [blame] | 2180 | LogUtils.d(LOG_TAG, "Deleting"); |
Vikram Aggarwal | bc67bb1 | 2012-04-30 14:05:35 -0700 | [diff] [blame] | 2181 | mConversationListCursor.delete(mContext, mTarget); |
Mindy Pereira | 695d696 | 2012-06-18 13:02:10 -0700 | [diff] [blame] | 2182 | if (mFolder.supportsCapability(FolderCapabilities.DELETE_ACTION_FINAL)) { |
Marc Blank | 386243f | 2012-05-25 10:40:59 -0700 | [diff] [blame] | 2183 | undoEnabled = false; |
| 2184 | } |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 2185 | break; |
| 2186 | case R.id.mute: |
Vikram Aggarwal | 7dd054e | 2012-05-21 14:43:10 -0700 | [diff] [blame] | 2187 | LogUtils.d(LOG_TAG, "Muting"); |
Vikram Aggarwal | bc67bb1 | 2012-04-30 14:05:35 -0700 | [diff] [blame] | 2188 | if (mFolder.supportsCapability(FolderCapabilities.DESTRUCTIVE_MUTE)) { |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 2189 | for (Conversation c : mTarget) { |
| 2190 | c.localDeleteOnUpdate = true; |
| 2191 | } |
Vikram Aggarwal | bc67bb1 | 2012-04-30 14:05:35 -0700 | [diff] [blame] | 2192 | } |
| 2193 | mConversationListCursor.mute(mContext, mTarget); |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 2194 | break; |
| 2195 | case R.id.report_spam: |
Vikram Aggarwal | 7dd054e | 2012-05-21 14:43:10 -0700 | [diff] [blame] | 2196 | LogUtils.d(LOG_TAG, "Reporting spam"); |
Vikram Aggarwal | bc67bb1 | 2012-04-30 14:05:35 -0700 | [diff] [blame] | 2197 | mConversationListCursor.reportSpam(mContext, mTarget); |
| 2198 | break; |
Paul Westbrook | 77eee62 | 2012-07-10 13:41:57 -0700 | [diff] [blame] | 2199 | case R.id.mark_not_spam: |
| 2200 | LogUtils.d(LOG_TAG, "Marking not spam"); |
| 2201 | mConversationListCursor.reportNotSpam(mContext, mTarget); |
| 2202 | break; |
Paul Westbrook | 76b2062 | 2012-07-12 11:45:43 -0700 | [diff] [blame] | 2203 | case R.id.report_phishing: |
| 2204 | LogUtils.d(LOG_TAG, "Reporting phishing"); |
| 2205 | mConversationListCursor.reportPhishing(mContext, mTarget); |
| 2206 | break; |
Vikram Aggarwal | bc67bb1 | 2012-04-30 14:05:35 -0700 | [diff] [blame] | 2207 | case R.id.remove_star: |
Vikram Aggarwal | 7dd054e | 2012-05-21 14:43:10 -0700 | [diff] [blame] | 2208 | LogUtils.d(LOG_TAG, "Removing star"); |
Vikram Aggarwal | bc67bb1 | 2012-04-30 14:05:35 -0700 | [diff] [blame] | 2209 | // Star removal is destructive in the Starred folder. |
| 2210 | mConversationListCursor.updateBoolean(mContext, mTarget, |
| 2211 | ConversationColumns.STARRED, false); |
| 2212 | break; |
| 2213 | case R.id.mark_not_important: |
Vikram Aggarwal | 7dd054e | 2012-05-21 14:43:10 -0700 | [diff] [blame] | 2214 | LogUtils.d(LOG_TAG, "Marking not-important"); |
Mindy Pereira | 445be21 | 2012-08-15 08:50:10 -0700 | [diff] [blame] | 2215 | // Marking not important is destructive in a mailbox |
| 2216 | // containing only important messages |
Mindy Pereira | 0d03ef8 | 2012-08-15 09:05:48 -0700 | [diff] [blame] | 2217 | if (mFolder != null && mFolder.isImportantOnly()) { |
Mindy Pereira | 445be21 | 2012-08-15 08:50:10 -0700 | [diff] [blame] | 2218 | for (Conversation conv : mTarget) { |
| 2219 | conv.localDeleteOnUpdate = true; |
| 2220 | } |
| 2221 | } |
Vikram Aggarwal | bc67bb1 | 2012-04-30 14:05:35 -0700 | [diff] [blame] | 2222 | mConversationListCursor.updateInt(mContext, mTarget, |
| 2223 | ConversationColumns.PRIORITY, UIProvider.ConversationPriority.LOW); |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 2224 | break; |
Paul Westbrook | ef36254 | 2012-08-27 14:53:32 -0700 | [diff] [blame] | 2225 | case R.id.discard_drafts: |
| 2226 | LogUtils.d(LOG_TAG, "Discarding draft messages"); |
| 2227 | // Discarding draft messages is destructive in a "draft" mailbox |
| 2228 | if (mFolder != null && mFolder.isDraft()) { |
| 2229 | for (Conversation conv : mTarget) { |
| 2230 | conv.localDeleteOnUpdate = true; |
| 2231 | } |
| 2232 | } |
| 2233 | mConversationListCursor.discardDrafts(mContext, mTarget); |
| 2234 | // We don't support undoing discarding drafts |
| 2235 | undoEnabled = false; |
| 2236 | break; |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 2237 | } |
| 2238 | if (undoEnabled) { |
mindyp | ead5039 | 2012-08-23 11:03:53 -0700 | [diff] [blame] | 2239 | mHandler.postDelayed(new Runnable() { |
| 2240 | @Override |
| 2241 | public void run() { |
| 2242 | onUndoAvailable(new ToastBarOperation(mTarget.size(), mAction, |
mindyp | a59283a | 2012-09-11 17:49:06 -0700 | [diff] [blame] | 2243 | ToastBarOperation.UNDO, mIsSelectedSet)); |
mindyp | ead5039 | 2012-08-23 11:03:53 -0700 | [diff] [blame] | 2244 | } |
| 2245 | }, mShowUndoBarDelay); |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 2246 | } |
Vikram Aggarwal | bc67bb1 | 2012-04-30 14:05:35 -0700 | [diff] [blame] | 2247 | refreshConversationList(); |
Vikram Aggarwal | c411395 | 2012-05-11 14:14:56 -0700 | [diff] [blame] | 2248 | if (mIsSelectedSet) { |
Vikram Aggarwal | c411395 | 2012-05-11 14:14:56 -0700 | [diff] [blame] | 2249 | mSelectedSet.clear(); |
| 2250 | } |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 2251 | } |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 2252 | |
| 2253 | /** |
| 2254 | * Returns true if this action has been performed, false otherwise. |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 2255 | * |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 2256 | */ |
| 2257 | private synchronized boolean isPerformed() { |
| 2258 | if (mCompleted) { |
| 2259 | return true; |
| 2260 | } |
| 2261 | mCompleted = true; |
| 2262 | return false; |
| 2263 | } |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 2264 | } |
Mindy Pereira | e5f4dc0 | 2012-03-21 16:08:53 -0700 | [diff] [blame] | 2265 | |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 2266 | /** |
| 2267 | * Get a destructive action for a menu action. |
| 2268 | * This is a temporary method, to control the profusion of {@link DestructiveAction} classes |
| 2269 | * that are created. Please do not copy this paradigm. |
| 2270 | * @param action the resource ID of the menu action: R.id.delete, for example |
| 2271 | * @param target the conversations to act upon. |
| 2272 | * @return a {@link DestructiveAction} that performs the specified action. |
| 2273 | */ |
Vikram Aggarwal | 531488e | 2012-05-29 16:36:52 -0700 | [diff] [blame] | 2274 | private final DestructiveAction getAction(int action, Collection<Conversation> target) { |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 2275 | final DestructiveAction da = new ConversationAction(action, target, false); |
| 2276 | registerDestructiveAction(da); |
| 2277 | return da; |
| 2278 | } |
| 2279 | |
Vikram Aggarwal | d503df4 | 2012-05-11 10:13:35 -0700 | [diff] [blame] | 2280 | // Called from the FolderSelectionDialog after a user is done selecting folders to assign the |
| 2281 | // conversations to. |
Mindy Pereira | e5f4dc0 | 2012-03-21 16:08:53 -0700 | [diff] [blame] | 2282 | @Override |
Mindy Pereira | 8db7e40 | 2012-07-13 10:32:47 -0700 | [diff] [blame] | 2283 | public final void assignFolder(Collection<FolderOperation> folderOps, |
| 2284 | Collection<Conversation> target, boolean batch, boolean showUndo) { |
| 2285 | // Actions are destructive only when the current folder can be assigned |
| 2286 | // to (which is the same as being able to un-assign a conversation from the folder) and |
| 2287 | // when the list of folders contains the current folder. |
| 2288 | final boolean isDestructive = mFolder |
| 2289 | .supportsCapability(FolderCapabilities.CAN_ACCEPT_MOVED_MESSAGES) |
| 2290 | && FolderOperation.isDestructive(folderOps, mFolder); |
Vikram Aggarwal | d503df4 | 2012-05-11 10:13:35 -0700 | [diff] [blame] | 2291 | LogUtils.d(LOG_TAG, "onFolderChangesCommit: isDestructive = %b", isDestructive); |
| 2292 | if (isDestructive) { |
| 2293 | for (final Conversation c : target) { |
| 2294 | c.localDeleteOnUpdate = true; |
Mindy Pereira | 6778f46 | 2012-03-23 18:01:55 -0700 | [diff] [blame] | 2295 | } |
Mindy Pereira | e5f4dc0 | 2012-03-21 16:08:53 -0700 | [diff] [blame] | 2296 | } |
mindyp | c84759c | 2012-08-29 09:51:53 -0700 | [diff] [blame] | 2297 | final DestructiveAction folderChange; |
Vikram Aggarwal | d503df4 | 2012-05-11 10:13:35 -0700 | [diff] [blame] | 2298 | // Update the UI elements depending no their visibility and availability |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 2299 | // TODO(viki): Consolidate this into a single method requestDelete. |
Vikram Aggarwal | d503df4 | 2012-05-11 10:13:35 -0700 | [diff] [blame] | 2300 | if (isDestructive) { |
mindyp | c84759c | 2012-08-29 09:51:53 -0700 | [diff] [blame] | 2301 | folderChange = getDeferredFolderChange(target, folderOps, isDestructive, |
| 2302 | batch, showUndo); |
Vikram Aggarwal | a8e4318 | 2012-09-13 12:55:10 -0700 | [diff] [blame] | 2303 | delete(0, target, folderChange); |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 2304 | } else { |
mindyp | c84759c | 2012-08-29 09:51:53 -0700 | [diff] [blame] | 2305 | folderChange = getFolderChange(target, folderOps, isDestructive, |
| 2306 | batch, showUndo); |
Vikram Aggarwal | d503df4 | 2012-05-11 10:13:35 -0700 | [diff] [blame] | 2307 | requestUpdate(target, folderChange); |
Mindy Pereira | e5f4dc0 | 2012-03-21 16:08:53 -0700 | [diff] [blame] | 2308 | } |
| 2309 | } |
| 2310 | |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 2311 | @Override |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 2312 | public final void onRefreshRequired() { |
mindyp | 5390fca | 2012-08-22 12:12:25 -0700 | [diff] [blame] | 2313 | if (isAnimating() || isDragging()) { |
Mindy Pereira | 69e88dd | 2012-08-10 09:30:18 -0700 | [diff] [blame] | 2314 | LogUtils.d(LOG_TAG, "onRefreshRequired: delay until animating done"); |
| 2315 | return; |
| 2316 | } |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 2317 | // Refresh the query in the background |
Paul Westbrook | cff1aea | 2012-08-10 11:51:00 -0700 | [diff] [blame] | 2318 | if (mConversationListCursor.isRefreshRequired()) { |
| 2319 | mConversationListCursor.refresh(); |
| 2320 | } |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 2321 | } |
| 2322 | |
mindyp | 5390fca | 2012-08-22 12:12:25 -0700 | [diff] [blame] | 2323 | @Override |
| 2324 | public void startDragMode() { |
| 2325 | mIsDragHappening = true; |
| 2326 | } |
| 2327 | |
| 2328 | @Override |
| 2329 | public void stopDragMode() { |
| 2330 | mIsDragHappening = false; |
| 2331 | if (mConversationListCursor.isRefreshReady()) { |
| 2332 | LogUtils.d(LOG_TAG, "Stopped animating: try sync"); |
| 2333 | onRefreshReady(); |
| 2334 | } |
| 2335 | |
| 2336 | if (mConversationListCursor.isRefreshRequired()) { |
| 2337 | LogUtils.d(LOG_TAG, "Stopped animating: refresh"); |
| 2338 | mConversationListCursor.refresh(); |
| 2339 | } |
| 2340 | } |
| 2341 | |
| 2342 | private boolean isDragging() { |
| 2343 | return mIsDragHappening; |
| 2344 | } |
| 2345 | |
mindyp | 6f54e1b | 2012-10-09 09:54:08 -0700 | [diff] [blame] | 2346 | @Override |
| 2347 | public boolean isAnimating() { |
Mindy Pereira | 69e88dd | 2012-08-10 09:30:18 -0700 | [diff] [blame] | 2348 | boolean isAnimating = false; |
| 2349 | ConversationListFragment convListFragment = getConversationListFragment(); |
| 2350 | if (convListFragment != null) { |
| 2351 | AnimatedAdapter adapter = convListFragment.getAnimatedAdapter(); |
| 2352 | if (adapter != null) { |
| 2353 | isAnimating = adapter.isAnimating(); |
| 2354 | } |
| 2355 | } |
| 2356 | return isAnimating; |
| 2357 | } |
| 2358 | |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 2359 | /** |
| 2360 | * Called when the {@link ConversationCursor} is changed or has new data in it. |
| 2361 | * <p> |
| 2362 | * {@inheritDoc} |
| 2363 | */ |
| 2364 | @Override |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 2365 | public final void onRefreshReady() { |
mindyp | 5c1d835 | 2012-11-05 10:12:44 -0800 | [diff] [blame] | 2366 | LogUtils.d(LOG_TAG, "Received refresh ready callback for folder %s", |
| 2367 | mFolder != null ? mFolder.id : "-1"); |
Paul Westbrook | cff1aea | 2012-08-10 11:51:00 -0700 | [diff] [blame] | 2368 | if (!isAnimating()) { |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 2369 | // Swap cursors |
| 2370 | mConversationListCursor.sync(); |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 2371 | } |
Paul Westbrook | 937c94f | 2012-08-16 13:01:18 -0700 | [diff] [blame] | 2372 | mTracker.onCursorUpdated(); |
Vikram Aggarwal | f0ef230 | 2012-11-07 14:53:34 -0800 | [diff] [blame] | 2373 | perhapsShowFirstSearchResult(); |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 2374 | } |
| 2375 | |
| 2376 | @Override |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 2377 | public final void onDataSetChanged() { |
Paul Westbrook | 9f119c7 | 2012-04-24 16:10:59 -0700 | [diff] [blame] | 2378 | updateConversationListFragment(); |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 2379 | mConversationListObservable.notifyChanged(); |
Paul Westbrook | a13b374 | 2012-09-07 16:35:06 -0700 | [diff] [blame] | 2380 | mSelectedSet.validateAgainstCursor(mConversationListCursor); |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 2381 | } |
| 2382 | |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 2383 | /** |
| 2384 | * If the Conversation List Fragment is visible, updates the fragment. |
| 2385 | */ |
| 2386 | private final void updateConversationListFragment() { |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 2387 | final ConversationListFragment convList = getConversationListFragment(); |
| 2388 | if (convList != null) { |
Vikram Aggarwal | 75daee5 | 2012-04-30 11:13:09 -0700 | [diff] [blame] | 2389 | refreshConversationList(); |
Paul Westbrook | 9f119c7 | 2012-04-24 16:10:59 -0700 | [diff] [blame] | 2390 | if (convList.isVisible()) { |
Vikram Aggarwal | 69b5c30 | 2012-09-05 11:11:13 -0700 | [diff] [blame] | 2391 | informCursorVisiblity(true); |
Paul Westbrook | 9f119c7 | 2012-04-24 16:10:59 -0700 | [diff] [blame] | 2392 | } |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 2393 | } |
| 2394 | } |
| 2395 | |
| 2396 | /** |
| 2397 | * This class handles throttled refresh of the conversation list |
| 2398 | */ |
| 2399 | static class RefreshTimerTask extends TimerTask { |
| 2400 | final Handler mHandler; |
| 2401 | final AbstractActivityController mController; |
| 2402 | |
| 2403 | RefreshTimerTask(AbstractActivityController controller, Handler handler) { |
| 2404 | mHandler = handler; |
| 2405 | mController = controller; |
| 2406 | } |
| 2407 | |
| 2408 | @Override |
| 2409 | public void run() { |
| 2410 | mHandler.post(new Runnable() { |
| 2411 | @Override |
| 2412 | public void run() { |
| 2413 | LogUtils.d(LOG_TAG, "Delay done... calling onRefreshRequired"); |
| 2414 | mController.onRefreshRequired(); |
| 2415 | }}); |
| 2416 | } |
| 2417 | } |
| 2418 | |
| 2419 | /** |
| 2420 | * Cancel the refresh task, if it's running |
| 2421 | */ |
| 2422 | private void cancelRefreshTask () { |
| 2423 | if (mConversationListRefreshTask != null) { |
| 2424 | mConversationListRefreshTask.cancel(); |
| 2425 | mConversationListRefreshTask = null; |
| 2426 | } |
| 2427 | } |
| 2428 | |
mindyp | 6f54e1b | 2012-10-09 09:54:08 -0700 | [diff] [blame] | 2429 | private void loadRecentFolders(Cursor data) { |
| 2430 | mRecentFolderList.loadFromUiProvider(data); |
| 2431 | if (isAnimating()) { |
| 2432 | mRecentsDataUpdated = true; |
| 2433 | } else { |
| 2434 | mRecentFolderObservers.notifyChanged(); |
| 2435 | } |
| 2436 | } |
| 2437 | |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 2438 | @Override |
Paul Westbrook | cff1aea | 2012-08-10 11:51:00 -0700 | [diff] [blame] | 2439 | public void onAnimationEnd(AnimatedAdapter animatedAdapter) { |
Paul Westbrook | 026139c | 2012-09-19 22:35:37 -0700 | [diff] [blame] | 2440 | if (mConversationListCursor == null) { |
| 2441 | LogUtils.e(LOG_TAG, "null ConversationCursor in onAnimationEnd"); |
| 2442 | return; |
| 2443 | } |
Paul Westbrook | cff1aea | 2012-08-10 11:51:00 -0700 | [diff] [blame] | 2444 | if (mConversationListCursor.isRefreshReady()) { |
mindyp | 5254486 | 2012-08-20 12:05:36 -0700 | [diff] [blame] | 2445 | LogUtils.d(LOG_TAG, "Stopped animating: try sync"); |
Paul Westbrook | cff1aea | 2012-08-10 11:51:00 -0700 | [diff] [blame] | 2446 | onRefreshReady(); |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 2447 | } |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 2448 | |
Paul Westbrook | cff1aea | 2012-08-10 11:51:00 -0700 | [diff] [blame] | 2449 | if (mConversationListCursor.isRefreshRequired()) { |
mindyp | 5254486 | 2012-08-20 12:05:36 -0700 | [diff] [blame] | 2450 | LogUtils.d(LOG_TAG, "Stopped animating: refresh"); |
Paul Westbrook | cff1aea | 2012-08-10 11:51:00 -0700 | [diff] [blame] | 2451 | mConversationListCursor.refresh(); |
| 2452 | } |
mindyp | 6f54e1b | 2012-10-09 09:54:08 -0700 | [diff] [blame] | 2453 | if (mRecentsDataUpdated) { |
| 2454 | mRecentsDataUpdated = false; |
| 2455 | mRecentFolderObservers.notifyChanged(); |
| 2456 | } |
| 2457 | FolderListFragment frag = this.getFolderListFragment(); |
| 2458 | if (frag != null) { |
| 2459 | frag.onAnimationEnd(); |
| 2460 | } |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 2461 | } |
| 2462 | |
| 2463 | @Override |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 2464 | public void onSetEmpty() { |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 2465 | } |
| 2466 | |
| 2467 | @Override |
| 2468 | public void onSetPopulated(ConversationSelectionSet set) { |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 2469 | final ConversationListFragment convList = getConversationListFragment(); |
| 2470 | if (convList == null) { |
| 2471 | return; |
| 2472 | } |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 2473 | mCabActionMenu = new SelectedConversationsActionMenu(mActivity, set, mFolder, |
Vikram Aggarwal | 531488e | 2012-05-29 16:36:52 -0700 | [diff] [blame] | 2474 | (SwipeableListView) convList.getListView()); |
Vikram Aggarwal | e128fc2 | 2012-04-04 12:33:34 -0700 | [diff] [blame] | 2475 | enableCabMode(); |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 2476 | } |
| 2477 | |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 2478 | @Override |
| 2479 | public void onSetChanged(ConversationSelectionSet set) { |
| 2480 | // Do nothing. We don't care about changes to the set. |
| 2481 | } |
| 2482 | |
| 2483 | @Override |
| 2484 | public ConversationSelectionSet getSelectedSet() { |
| 2485 | return mSelectedSet; |
| 2486 | } |
| 2487 | |
Vikram Aggarwal | e128fc2 | 2012-04-04 12:33:34 -0700 | [diff] [blame] | 2488 | /** |
| 2489 | * Disable the Contextual Action Bar (CAB). The selected set is not changed. |
| 2490 | */ |
| 2491 | protected void disableCabMode() { |
Mindy Pereira | 8937bf1 | 2012-07-23 14:05:02 -0700 | [diff] [blame] | 2492 | // Commit any previous destructive actions when entering/ exiting CAB mode. |
mindyp | c6adce3 | 2012-08-22 18:46:42 -0700 | [diff] [blame] | 2493 | commitDestructiveActions(true); |
Vikram Aggarwal | e128fc2 | 2012-04-04 12:33:34 -0700 | [diff] [blame] | 2494 | if (mCabActionMenu != null) { |
| 2495 | mCabActionMenu.deactivate(); |
| 2496 | } |
| 2497 | } |
| 2498 | |
| 2499 | /** |
| 2500 | * Re-enable the CAB menu if required. The selection set is not changed. |
| 2501 | */ |
| 2502 | protected void enableCabMode() { |
| 2503 | if (mCabActionMenu != null) { |
| 2504 | mCabActionMenu.activate(); |
| 2505 | } |
| 2506 | } |
| 2507 | |
Vikram Aggarwal | 4eb5271 | 2012-06-19 16:24:50 -0700 | [diff] [blame] | 2508 | /** |
| 2509 | * Unselect conversations and exit CAB mode. |
| 2510 | */ |
| 2511 | protected final void exitCabMode() { |
| 2512 | mSelectedSet.clear(); |
| 2513 | } |
| 2514 | |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 2515 | @Override |
Mindy Pereira | fd0c297 | 2012-03-27 13:50:39 -0700 | [diff] [blame] | 2516 | public void startSearch() { |
Vikram Aggarwal | 35f19d7 | 2012-04-24 13:24:48 -0700 | [diff] [blame] | 2517 | if (mAccount == null) { |
| 2518 | // We cannot search if there is no account. Drop the request to the floor. |
| 2519 | LogUtils.d(LOG_TAG, "AbstractActivityController.startSearch(): null account"); |
| 2520 | return; |
| 2521 | } |
Mindy Pereira | fd0c297 | 2012-03-27 13:50:39 -0700 | [diff] [blame] | 2522 | if (mAccount.supportsCapability(UIProvider.AccountCapabilities.LOCAL_SEARCH) |
| 2523 | | mAccount.supportsCapability(UIProvider.AccountCapabilities.SERVER_SEARCH)) { |
Vikram Aggarwal | 70f130e | 2012-04-03 12:32:14 -0700 | [diff] [blame] | 2524 | onSearchRequested(mActionBarView.getQuery()); |
Mindy Pereira | fd0c297 | 2012-03-27 13:50:39 -0700 | [diff] [blame] | 2525 | } else { |
| 2526 | Toast.makeText(mActivity.getActivityContext(), mActivity.getActivityContext() |
Mindy Pereira | a46c299 | 2012-03-27 14:12:39 -0700 | [diff] [blame] | 2527 | .getString(R.string.search_unsupported), Toast.LENGTH_SHORT).show(); |
Mindy Pereira | fd0c297 | 2012-03-27 13:50:39 -0700 | [diff] [blame] | 2528 | } |
| 2529 | } |
Mindy Pereira | acf6039 | 2012-04-06 09:11:00 -0700 | [diff] [blame] | 2530 | |
Vikram Aggarwal | 0dda573 | 2012-04-06 11:20:16 -0700 | [diff] [blame] | 2531 | @Override |
| 2532 | public void exitSearchMode() { |
| 2533 | if (mViewMode.getMode() == ViewMode.SEARCH_RESULTS_LIST) { |
| 2534 | mActivity.finish(); |
| 2535 | } |
| 2536 | } |
| 2537 | |
Mindy Pereira | acf6039 | 2012-04-06 09:11:00 -0700 | [diff] [blame] | 2538 | /** |
| 2539 | * Supports dragging conversations to a folder. |
| 2540 | */ |
| 2541 | @Override |
| 2542 | public boolean supportsDrag(DragEvent event, Folder folder) { |
| 2543 | return (folder != null |
| 2544 | && event != null |
| 2545 | && event.getClipDescription() != null |
| 2546 | && folder.supportsCapability |
| 2547 | (UIProvider.FolderCapabilities.CAN_ACCEPT_MOVED_MESSAGES) |
| 2548 | && folder.supportsCapability |
| 2549 | (UIProvider.FolderCapabilities.CAN_HOLD_MAIL) |
| 2550 | && !mFolder.uri.equals(folder.uri)); |
| 2551 | } |
| 2552 | |
| 2553 | /** |
Mindy Pereira | 6c2663d | 2012-07-20 15:37:29 -0700 | [diff] [blame] | 2554 | * Handles dropping conversations to a folder. |
Mindy Pereira | acf6039 | 2012-04-06 09:11:00 -0700 | [diff] [blame] | 2555 | */ |
| 2556 | @Override |
| 2557 | public void handleDrop(DragEvent event, final Folder folder) { |
Mindy Pereira | acf6039 | 2012-04-06 09:11:00 -0700 | [diff] [blame] | 2558 | if (!supportsDrag(event, folder)) { |
| 2559 | return; |
| 2560 | } |
Mindy Pereira | acf6039 | 2012-04-06 09:11:00 -0700 | [diff] [blame] | 2561 | final Collection<Conversation> conversations = mSelectedSet.values(); |
mindyp | a849263 | 2012-09-24 09:27:54 -0700 | [diff] [blame] | 2562 | final ArrayList<FolderOperation> dragDropOperations = new ArrayList<FolderOperation>(); |
| 2563 | // Add the drop target folder. |
| 2564 | dragDropOperations.add(new FolderOperation(folder, true)); |
| 2565 | // Remove the current folder unless the user is viewing "all". |
| 2566 | // That operation should just add the new folder. |
| 2567 | boolean isDestructive = !mFolder.isViewAll() |
| 2568 | && mFolder.supportsCapability |
| 2569 | (UIProvider.FolderCapabilities.CAN_ACCEPT_MOVED_MESSAGES); |
| 2570 | if (isDestructive) { |
| 2571 | dragDropOperations.add(new FolderOperation(mFolder, false)); |
| 2572 | } |
Mindy Pereira | 8db7e40 | 2012-07-13 10:32:47 -0700 | [diff] [blame] | 2573 | // Drag and drop is destructive: we remove conversations from the |
| 2574 | // current folder. |
mindyp | a849263 | 2012-09-24 09:27:54 -0700 | [diff] [blame] | 2575 | final DestructiveAction action = getFolderChange(conversations, dragDropOperations, |
| 2576 | isDestructive, true, true); |
| 2577 | if (isDestructive) { |
| 2578 | delete(0, conversations, action); |
| 2579 | } else { |
| 2580 | action.performAction(); |
| 2581 | } |
Mindy Pereira | acf6039 | 2012-04-06 09:11:00 -0700 | [diff] [blame] | 2582 | } |
Mindy Pereira | 0963ef8 | 2012-04-10 11:43:01 -0700 | [diff] [blame] | 2583 | |
| 2584 | @Override |
Mindy Pereira | 0963ef8 | 2012-04-10 11:43:01 -0700 | [diff] [blame] | 2585 | public void onTouchEvent(MotionEvent event) { |
| 2586 | if (event.getAction() == MotionEvent.ACTION_DOWN) { |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 2587 | if (mToastBar != null && !mToastBar.isEventInToastBar(event)) { |
Mark Wei | d243d45 | 2012-10-31 16:24:08 -0700 | [diff] [blame] | 2588 | hideOrRepositionToastBar(true); |
Mindy Pereira | 0963ef8 | 2012-04-10 11:43:01 -0700 | [diff] [blame] | 2589 | } |
| 2590 | } |
| 2591 | } |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 2592 | |
Mark Wei | d243d45 | 2012-10-31 16:24:08 -0700 | [diff] [blame] | 2593 | protected abstract void hideOrRepositionToastBar(boolean animated); |
| 2594 | |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 2595 | @Override |
| 2596 | public void onConversationSeen(Conversation conv) { |
| 2597 | mPagerController.onConversationSeen(conv); |
| 2598 | } |
| 2599 | |
Andy Huang | 9d3fd92 | 2012-09-26 22:23:58 -0700 | [diff] [blame] | 2600 | @Override |
| 2601 | public boolean isInitialConversationLoading() { |
| 2602 | return mPagerController.isInitialConversationLoading(); |
| 2603 | } |
| 2604 | |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 2605 | private class ConversationListLoaderCallbacks implements |
| 2606 | LoaderManager.LoaderCallbacks<ConversationCursor> { |
| 2607 | |
| 2608 | @Override |
| 2609 | public Loader<ConversationCursor> onCreateLoader(int id, Bundle args) { |
| 2610 | Loader<ConversationCursor> result = new ConversationCursorLoader((Activity) mActivity, |
Paul Westbrook | 9a70e91 | 2012-08-17 15:53:20 -0700 | [diff] [blame] | 2611 | mAccount, mFolder.conversationListUri, mFolder.name); |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 2612 | return result; |
| 2613 | } |
| 2614 | |
| 2615 | @Override |
| 2616 | public void onLoadFinished(Loader<ConversationCursor> loader, ConversationCursor data) { |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 2617 | LogUtils.d(LOG_TAG, "IN AAC.ConversationCursor.onLoadFinished, data=%s loader=%s", |
| 2618 | data, loader); |
Vikram Aggarwal | e8a8532 | 2012-04-24 09:01:38 -0700 | [diff] [blame] | 2619 | // Clear our all pending destructive actions before swapping the conversation cursor |
| 2620 | destroyPending(null); |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 2621 | mConversationListCursor = data; |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 2622 | mConversationListCursor.addListener(AbstractActivityController.this); |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 2623 | |
Paul Westbrook | 937c94f | 2012-08-16 13:01:18 -0700 | [diff] [blame] | 2624 | mTracker.onCursorUpdated(); |
Andy Huang | e3df1ad | 2012-04-24 17:15:23 -0700 | [diff] [blame] | 2625 | mConversationListObservable.notifyChanged(); |
Paul Westbrook | 9f119c7 | 2012-04-24 16:10:59 -0700 | [diff] [blame] | 2626 | |
Vikram Aggarwal | 17f373e | 2012-09-17 15:49:32 -0700 | [diff] [blame] | 2627 | final ConversationListFragment convList = getConversationListFragment(); |
| 2628 | if (convList != null && convList.isVisible()) { |
| 2629 | // The conversation list is already listening to list changes and gets notified |
| 2630 | // in the mConversationListObservable.notifyChanged() line above. We only need to |
| 2631 | // check and inform the cursor of the change in visibility here. |
| 2632 | informCursorVisiblity(true); |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 2633 | } |
Vikram Aggarwal | f0ef230 | 2012-11-07 14:53:34 -0800 | [diff] [blame] | 2634 | perhapsShowFirstSearchResult(); |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 2635 | } |
| 2636 | |
| 2637 | @Override |
| 2638 | public void onLoaderReset(Loader<ConversationCursor> loader) { |
Paul Westbrook | 9a70e91 | 2012-08-17 15:53:20 -0700 | [diff] [blame] | 2639 | LogUtils.d(LOG_TAG, "IN AAC.ConversationCursor.onLoaderReset, data=%s loader=%s", |
| 2640 | mConversationListCursor, loader); |
| 2641 | |
| 2642 | if (mConversationListCursor != null) { |
| 2643 | // Unregister the listener |
| 2644 | mConversationListCursor.removeListener(AbstractActivityController.this); |
| 2645 | mConversationListCursor = null; |
| 2646 | |
| 2647 | // Inform anyone who is interested about the change |
| 2648 | mTracker.onCursorUpdated(); |
| 2649 | mConversationListObservable.notifyChanged(); |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 2650 | } |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 2651 | } |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 2652 | } |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 2653 | |
Vikram Aggarwal | e8a8532 | 2012-04-24 09:01:38 -0700 | [diff] [blame] | 2654 | /** |
Vikram Aggarwal | f0ef230 | 2012-11-07 14:53:34 -0800 | [diff] [blame] | 2655 | * Updates controller state based on search results and shows first conversation if required. |
| 2656 | */ |
| 2657 | private final void perhapsShowFirstSearchResult() { |
mindyp | d27009a | 2012-11-27 11:54:18 -0800 | [diff] [blame^] | 2658 | if (mCurrentConversation == null) { |
| 2659 | // Shown for search results in two-pane mode only. |
| 2660 | mHaveSearchResults = Intent.ACTION_SEARCH.equals(mActivity.getIntent().getAction()) |
| 2661 | && mConversationListCursor.getCount() > 0; |
| 2662 | if (!shouldShowFirstConversation()) { |
| 2663 | return; |
| 2664 | } |
| 2665 | mConversationListCursor.moveToPosition(0); |
| 2666 | final Conversation conv = new Conversation(mConversationListCursor); |
| 2667 | conv.position = 0; |
| 2668 | onConversationSelected(conv, true /* checkSafeToModifyFragments */); |
Vikram Aggarwal | f0ef230 | 2012-11-07 14:53:34 -0800 | [diff] [blame] | 2669 | } |
Vikram Aggarwal | f0ef230 | 2012-11-07 14:53:34 -0800 | [diff] [blame] | 2670 | } |
| 2671 | |
| 2672 | /** |
Vikram Aggarwal | e8a8532 | 2012-04-24 09:01:38 -0700 | [diff] [blame] | 2673 | * Destroy the pending {@link DestructiveAction} till now and assign the given action as the |
| 2674 | * next destructive action.. |
| 2675 | * @param nextAction the next destructive action to be performed. This can be null. |
| 2676 | */ |
| 2677 | private final void destroyPending(DestructiveAction nextAction) { |
| 2678 | // If there is a pending action, perform that first. |
| 2679 | if (mPendingDestruction != null) { |
| 2680 | mPendingDestruction.performAction(); |
| 2681 | } |
| 2682 | mPendingDestruction = nextAction; |
| 2683 | } |
| 2684 | |
| 2685 | /** |
| 2686 | * Register a destructive action with the controller. This performs the previous destructive |
Vikram Aggarwal | acaa3c0 | 2012-04-24 12:45:27 -0700 | [diff] [blame] | 2687 | * 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] | 2688 | * embellish this method any more. |
Vikram Aggarwal | e8a8532 | 2012-04-24 09:01:38 -0700 | [diff] [blame] | 2689 | * @param action |
| 2690 | */ |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 2691 | private final void registerDestructiveAction(DestructiveAction action) { |
Vikram Aggarwal | e8a8532 | 2012-04-24 09:01:38 -0700 | [diff] [blame] | 2692 | // TODO(viki): This is not a good idea. The best solution is for clients to request a |
| 2693 | // destructive action from the controller and for the controller to own the action. This is |
| 2694 | // a half-way solution while refactoring DestructiveAction. |
| 2695 | destroyPending(action); |
| 2696 | return; |
| 2697 | } |
| 2698 | |
Vikram Aggarwal | 531488e | 2012-05-29 16:36:52 -0700 | [diff] [blame] | 2699 | @Override |
| 2700 | public final DestructiveAction getBatchAction(int action) { |
Vikram Aggarwal | c411395 | 2012-05-11 14:14:56 -0700 | [diff] [blame] | 2701 | final DestructiveAction da = new ConversationAction(action, mSelectedSet.values(), true); |
Vikram Aggarwal | e8a8532 | 2012-04-24 09:01:38 -0700 | [diff] [blame] | 2702 | registerDestructiveAction(da); |
| 2703 | return da; |
| 2704 | } |
Vikram Aggarwal | 41e6e71 | 2012-04-24 11:22:57 -0700 | [diff] [blame] | 2705 | |
Mindy Pereira | de3e74a | 2012-07-24 09:43:10 -0700 | [diff] [blame] | 2706 | @Override |
| 2707 | public final DestructiveAction getDeferredBatchAction(int action) { |
mindyp | f0656a1 | 2012-10-01 08:30:57 -0700 | [diff] [blame] | 2708 | return getDeferredAction(action, mSelectedSet.values(), true); |
| 2709 | } |
| 2710 | |
| 2711 | /** |
| 2712 | * Get a destructive action for a menu action. This is a temporary method, |
| 2713 | * to control the profusion of {@link DestructiveAction} classes that are |
| 2714 | * created. Please do not copy this paradigm. |
| 2715 | * @param action the resource ID of the menu action: R.id.delete, for |
| 2716 | * example |
| 2717 | * @param target the conversations to act upon. |
| 2718 | * @return a {@link DestructiveAction} that performs the specified action. |
| 2719 | */ |
| 2720 | @Override |
| 2721 | public DestructiveAction getDeferredAction(int action, Collection<Conversation> target, |
| 2722 | boolean batch) { |
| 2723 | final DestructiveAction da = new ConversationAction(action, target, batch); |
Mindy Pereira | de3e74a | 2012-07-24 09:43:10 -0700 | [diff] [blame] | 2724 | return da; |
| 2725 | } |
| 2726 | |
Vikram Aggarwal | bc67bb1 | 2012-04-30 14:05:35 -0700 | [diff] [blame] | 2727 | /** |
| 2728 | * Class to change the folders that are assigned to a set of conversations. This is destructive |
| 2729 | * because the user can remove the current folder from the conversation, in which case it has |
| 2730 | * to be animated away from the current folder. |
| 2731 | */ |
Vikram Aggarwal | 41e6e71 | 2012-04-24 11:22:57 -0700 | [diff] [blame] | 2732 | private class FolderDestruction implements DestructiveAction { |
Paul Westbrook | 77eee62 | 2012-07-10 13:41:57 -0700 | [diff] [blame] | 2733 | private final Collection<Conversation> mTarget; |
Mindy Pereira | 8db7e40 | 2012-07-13 10:32:47 -0700 | [diff] [blame] | 2734 | private final ArrayList<FolderOperation> mFolderOps = new ArrayList<FolderOperation>(); |
Vikram Aggarwal | 41e6e71 | 2012-04-24 11:22:57 -0700 | [diff] [blame] | 2735 | private final boolean mIsDestructive; |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 2736 | /** Whether this destructive action has already been performed */ |
| 2737 | private boolean mCompleted; |
Mindy Pereira | f3a4556 | 2012-05-24 16:30:19 -0700 | [diff] [blame] | 2738 | private boolean mIsSelectedSet; |
Mindy Pereira | 06642fa | 2012-07-12 16:23:27 -0700 | [diff] [blame] | 2739 | private boolean mShowUndo; |
Mindy Pereira | 01f3050 | 2012-08-14 10:30:51 -0700 | [diff] [blame] | 2740 | private int mAction; |
Vikram Aggarwal | 41e6e71 | 2012-04-24 11:22:57 -0700 | [diff] [blame] | 2741 | |
| 2742 | /** |
| 2743 | * Create a new folder destruction object to act on the given conversations. |
| 2744 | * @param target |
| 2745 | */ |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 2746 | private FolderDestruction(final Collection<Conversation> target, |
Mindy Pereira | 8db7e40 | 2012-07-13 10:32:47 -0700 | [diff] [blame] | 2747 | final Collection<FolderOperation> folders, boolean isDestructive, boolean isBatch, |
Mindy Pereira | 01f3050 | 2012-08-14 10:30:51 -0700 | [diff] [blame] | 2748 | boolean showUndo, int action) { |
Paul Westbrook | 77eee62 | 2012-07-10 13:41:57 -0700 | [diff] [blame] | 2749 | mTarget = ImmutableList.copyOf(target); |
Mindy Pereira | 8db7e40 | 2012-07-13 10:32:47 -0700 | [diff] [blame] | 2750 | mFolderOps.addAll(folders); |
Vikram Aggarwal | 41e6e71 | 2012-04-24 11:22:57 -0700 | [diff] [blame] | 2751 | mIsDestructive = isDestructive; |
Mindy Pereira | f3a4556 | 2012-05-24 16:30:19 -0700 | [diff] [blame] | 2752 | mIsSelectedSet = isBatch; |
Mindy Pereira | 06642fa | 2012-07-12 16:23:27 -0700 | [diff] [blame] | 2753 | mShowUndo = showUndo; |
Mindy Pereira | 01f3050 | 2012-08-14 10:30:51 -0700 | [diff] [blame] | 2754 | mAction = action; |
Vikram Aggarwal | 41e6e71 | 2012-04-24 11:22:57 -0700 | [diff] [blame] | 2755 | } |
| 2756 | |
Vikram Aggarwal | 41e6e71 | 2012-04-24 11:22:57 -0700 | [diff] [blame] | 2757 | @Override |
| 2758 | public void performAction() { |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 2759 | if (isPerformed()) { |
| 2760 | return; |
| 2761 | } |
Mindy Pereira | 06642fa | 2012-07-12 16:23:27 -0700 | [diff] [blame] | 2762 | if (mIsDestructive && mShowUndo) { |
Mindy Pereira | d3367499 | 2012-06-25 16:26:30 -0700 | [diff] [blame] | 2763 | ToastBarOperation undoOp = new ToastBarOperation(mTarget.size(), |
mindyp | a59283a | 2012-09-11 17:49:06 -0700 | [diff] [blame] | 2764 | mAction, ToastBarOperation.UNDO, mIsSelectedSet); |
Vikram Aggarwal | 41e6e71 | 2012-04-24 11:22:57 -0700 | [diff] [blame] | 2765 | onUndoAvailable(undoOp); |
| 2766 | } |
Mindy Pereira | 8db7e40 | 2012-07-13 10:32:47 -0700 | [diff] [blame] | 2767 | // For each conversation, for each operation, add/ remove the |
| 2768 | // appropriate folders. |
mindyp | 389f0b2 | 2012-08-29 11:12:54 -0700 | [diff] [blame] | 2769 | ArrayList<String> updatedTargetFolders = new ArrayList<String>(mTarget.size()); |
Mindy Pereira | 8db7e40 | 2012-07-13 10:32:47 -0700 | [diff] [blame] | 2770 | for (Conversation target : mTarget) { |
| 2771 | HashMap<Uri, Folder> targetFolders = Folder |
Mindy Pereira | 68f8384 | 2012-07-27 09:43:31 -0700 | [diff] [blame] | 2772 | .hashMapForFolders(target.getRawFolders()); |
Mindy Pereira | 01f3050 | 2012-08-14 10:30:51 -0700 | [diff] [blame] | 2773 | if (mIsDestructive) { |
| 2774 | target.localDeleteOnUpdate = true; |
| 2775 | } |
Mindy Pereira | 8db7e40 | 2012-07-13 10:32:47 -0700 | [diff] [blame] | 2776 | for (FolderOperation op : mFolderOps) { |
| 2777 | if (op.mAdd) { |
| 2778 | targetFolders.put(op.mFolder.uri, op.mFolder); |
| 2779 | } else { |
| 2780 | targetFolders.remove(op.mFolder.uri); |
| 2781 | } |
| 2782 | } |
mindyp | 389f0b2 | 2012-08-29 11:12:54 -0700 | [diff] [blame] | 2783 | updatedTargetFolders.add(Folder.getSerializedFolderString(targetFolders.values())); |
| 2784 | } |
| 2785 | if (mConversationListCursor != null) { |
| 2786 | mConversationListCursor.updateStrings(mContext, mTarget, |
| 2787 | Conversation.UPDATE_FOLDER_COLUMN, updatedTargetFolders); |
Mindy Pereira | 8db7e40 | 2012-07-13 10:32:47 -0700 | [diff] [blame] | 2788 | } |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 2789 | refreshConversationList(); |
Mindy Pereira | f3a4556 | 2012-05-24 16:30:19 -0700 | [diff] [blame] | 2790 | if (mIsSelectedSet) { |
| 2791 | mSelectedSet.clear(); |
| 2792 | } |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 2793 | } |
Mindy Pereira | 8db7e40 | 2012-07-13 10:32:47 -0700 | [diff] [blame] | 2794 | |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 2795 | /** |
| 2796 | * Returns true if this action has been performed, false otherwise. |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 2797 | * |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 2798 | */ |
| 2799 | private synchronized boolean isPerformed() { |
| 2800 | if (mCompleted) { |
| 2801 | return true; |
Vikram Aggarwal | 41e6e71 | 2012-04-24 11:22:57 -0700 | [diff] [blame] | 2802 | } |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 2803 | mCompleted = true; |
| 2804 | return false; |
Vikram Aggarwal | 41e6e71 | 2012-04-24 11:22:57 -0700 | [diff] [blame] | 2805 | } |
| 2806 | } |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 2807 | |
mindyp | c84759c | 2012-08-29 09:51:53 -0700 | [diff] [blame] | 2808 | public final DestructiveAction getFolderChange(Collection<Conversation> target, |
| 2809 | Collection<FolderOperation> folders, boolean isDestructive, boolean isBatch, |
| 2810 | boolean showUndo) { |
| 2811 | final DestructiveAction da = getDeferredFolderChange(target, folders, isDestructive, |
| 2812 | isBatch, showUndo); |
| 2813 | registerDestructiveAction(da); |
| 2814 | return da; |
| 2815 | } |
| 2816 | |
| 2817 | public final DestructiveAction getDeferredFolderChange(Collection<Conversation> target, |
Mindy Pereira | 8db7e40 | 2012-07-13 10:32:47 -0700 | [diff] [blame] | 2818 | Collection<FolderOperation> folders, boolean isDestructive, boolean isBatch, |
| 2819 | boolean showUndo) { |
Mindy Pereira | 06642fa | 2012-07-12 16:23:27 -0700 | [diff] [blame] | 2820 | final DestructiveAction da = new FolderDestruction(target, folders, isDestructive, isBatch, |
Mindy Pereira | 01f3050 | 2012-08-14 10:30:51 -0700 | [diff] [blame] | 2821 | showUndo, R.id.change_folder); |
Mindy Pereira | 01f3050 | 2012-08-14 10:30:51 -0700 | [diff] [blame] | 2822 | return da; |
| 2823 | } |
| 2824 | |
| 2825 | @Override |
| 2826 | public final DestructiveAction getDeferredRemoveFolder(Collection<Conversation> target, |
| 2827 | Folder toRemove, boolean isDestructive, boolean isBatch, |
| 2828 | boolean showUndo) { |
| 2829 | Collection<FolderOperation> folderOps = new ArrayList<FolderOperation>(); |
| 2830 | folderOps.add(new FolderOperation(toRemove, false)); |
| 2831 | return new FolderDestruction(target, folderOps, isDestructive, isBatch, |
| 2832 | showUndo, R.id.remove_folder); |
| 2833 | } |
| 2834 | |
| 2835 | private final DestructiveAction getRemoveFolder(Collection<Conversation> target, |
mindyp | c84759c | 2012-08-29 09:51:53 -0700 | [diff] [blame] | 2836 | Folder toRemove, boolean isDestructive, boolean isBatch, boolean showUndo) { |
| 2837 | DestructiveAction da = getDeferredRemoveFolder(target, toRemove, isDestructive, isBatch, |
| 2838 | showUndo); |
Vikram Aggarwal | 41e6e71 | 2012-04-24 11:22:57 -0700 | [diff] [blame] | 2839 | registerDestructiveAction(da); |
| 2840 | return da; |
| 2841 | } |
Vikram Aggarwal | 75daee5 | 2012-04-30 11:13:09 -0700 | [diff] [blame] | 2842 | |
Vikram Aggarwal | 4f4782b | 2012-05-30 08:39:09 -0700 | [diff] [blame] | 2843 | @Override |
| 2844 | public final void refreshConversationList() { |
Vikram Aggarwal | 75daee5 | 2012-04-30 11:13:09 -0700 | [diff] [blame] | 2845 | final ConversationListFragment convList = getConversationListFragment(); |
| 2846 | if (convList == null) { |
| 2847 | return; |
| 2848 | } |
| 2849 | convList.requestListRefresh(); |
| 2850 | } |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 2851 | |
| 2852 | protected final ActionClickedListener getUndoClickedListener( |
| 2853 | final AnimatedAdapter listAdapter) { |
| 2854 | return new ActionClickedListener() { |
| 2855 | @Override |
| 2856 | public void onActionClicked() { |
| 2857 | if (mAccount.undoUri != null) { |
| 2858 | // NOTE: We might want undo to return the messages affected, in which case |
| 2859 | // the resulting cursor might be interesting... |
| 2860 | // TODO: Use UIProvider.SEQUENCE_QUERY_PARAMETER to indicate the set of |
| 2861 | // commands to undo |
| 2862 | if (mConversationListCursor != null) { |
| 2863 | mConversationListCursor.undo( |
| 2864 | mActivity.getActivityContext(), mAccount.undoUri); |
| 2865 | } |
| 2866 | if (listAdapter != null) { |
| 2867 | listAdapter.setUndo(true); |
| 2868 | } |
| 2869 | } |
| 2870 | } |
| 2871 | }; |
| 2872 | } |
| 2873 | |
Vikram Aggarwal | 41b9e8f | 2012-09-25 10:15:04 -0700 | [diff] [blame] | 2874 | /** |
| 2875 | * Shows an error toast in the bottom when a folder was not fetched successfully. |
| 2876 | * @param folder the folder which could not be fetched. |
| 2877 | * @param replaceVisibleToast if true, this should replace any currently visible toast. |
| 2878 | */ |
Andrew Sapperstein | 9d7519d | 2012-07-16 14:03:53 -0700 | [diff] [blame] | 2879 | protected final void showErrorToast(final Folder folder, boolean replaceVisibleToast) { |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 2880 | mToastBar.setConversationMode(false); |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 2881 | |
Vikram Aggarwal | 41b9e8f | 2012-09-25 10:15:04 -0700 | [diff] [blame] | 2882 | final ActionClickedListener listener; |
| 2883 | final int actionTextResourceId; |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 2884 | final int lastSyncResult = folder.lastSyncResult; |
Vikram Aggarwal | 41b9e8f | 2012-09-25 10:15:04 -0700 | [diff] [blame] | 2885 | switch (lastSyncResult & 0x0f) { |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 2886 | case UIProvider.LastSyncResult.CONNECTION_ERROR: |
Vikram Aggarwal | 41b9e8f | 2012-09-25 10:15:04 -0700 | [diff] [blame] | 2887 | // The sync request that caused this failure. |
| 2888 | final int syncRequest = lastSyncResult >> 4; |
| 2889 | // Show: User explicitly pressed the refresh button and there is no connection |
| 2890 | // Show: The first time the user enters the app and there is no connection |
| 2891 | // TODO(viki): Implement this. |
| 2892 | // Reference: http://b/7202801 |
| 2893 | final boolean showToast = (syncRequest & UIProvider.SyncStatus.USER_REFRESH) != 0; |
| 2894 | // Don't show: Already in the app; user switches to a synced label |
| 2895 | // Don't show: In a live label and a background sync fails |
| 2896 | final boolean avoidToast = !showToast && (folder.syncWindow > 0 |
| 2897 | || (syncRequest & UIProvider.SyncStatus.BACKGROUND_SYNC) != 0); |
| 2898 | if (avoidToast) { |
| 2899 | return; |
| 2900 | } |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 2901 | listener = getRetryClickedListener(folder); |
| 2902 | actionTextResourceId = R.string.retry; |
| 2903 | break; |
| 2904 | case UIProvider.LastSyncResult.AUTH_ERROR: |
| 2905 | listener = getSignInClickedListener(); |
| 2906 | actionTextResourceId = R.string.signin; |
| 2907 | break; |
| 2908 | case UIProvider.LastSyncResult.SECURITY_ERROR: |
| 2909 | return; // Currently we do nothing for security errors. |
| 2910 | case UIProvider.LastSyncResult.STORAGE_ERROR: |
| 2911 | listener = getStorageErrorClickedListener(); |
| 2912 | actionTextResourceId = R.string.info; |
| 2913 | break; |
| 2914 | case UIProvider.LastSyncResult.INTERNAL_ERROR: |
| 2915 | listener = getInternalErrorClickedListener(); |
| 2916 | actionTextResourceId = R.string.report; |
| 2917 | break; |
| 2918 | default: |
| 2919 | return; |
| 2920 | } |
Vikram Aggarwal | 41b9e8f | 2012-09-25 10:15:04 -0700 | [diff] [blame] | 2921 | mToastBar.show(listener, |
Andrew Sapperstein | 5d42096 | 2012-07-12 16:43:10 -0700 | [diff] [blame] | 2922 | R.drawable.ic_alert_white, |
Vikram Aggarwal | 41b9e8f | 2012-09-25 10:15:04 -0700 | [diff] [blame] | 2923 | Utils.getSyncStatusText(mActivity.getActivityContext(), lastSyncResult), |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 2924 | false, /* showActionIcon */ |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 2925 | actionTextResourceId, |
Andrew Sapperstein | 9d7519d | 2012-07-16 14:03:53 -0700 | [diff] [blame] | 2926 | replaceVisibleToast, |
mindyp | a59283a | 2012-09-11 17:49:06 -0700 | [diff] [blame] | 2927 | new ToastBarOperation(1, 0, ToastBarOperation.ERROR, false)); |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 2928 | } |
| 2929 | |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 2930 | private ActionClickedListener getRetryClickedListener(final Folder folder) { |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 2931 | return new ActionClickedListener() { |
| 2932 | @Override |
| 2933 | public void onActionClicked() { |
| 2934 | final Uri uri = folder.refreshUri; |
| 2935 | |
| 2936 | if (uri != null) { |
Paul Westbrook | 4969e0c | 2012-08-20 14:38:39 -0700 | [diff] [blame] | 2937 | startAsyncRefreshTask(uri); |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 2938 | } |
| 2939 | } |
| 2940 | }; |
| 2941 | } |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 2942 | |
| 2943 | private ActionClickedListener getSignInClickedListener() { |
| 2944 | return new ActionClickedListener() { |
| 2945 | @Override |
| 2946 | public void onActionClicked() { |
Paul Westbrook | 122f7c2 | 2012-08-20 17:50:31 -0700 | [diff] [blame] | 2947 | promptUserForAuthentication(mAccount); |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 2948 | } |
| 2949 | }; |
| 2950 | } |
| 2951 | |
| 2952 | private ActionClickedListener getStorageErrorClickedListener() { |
| 2953 | return new ActionClickedListener() { |
| 2954 | @Override |
| 2955 | public void onActionClicked() { |
Paul Westbrook | 4969e0c | 2012-08-20 14:38:39 -0700 | [diff] [blame] | 2956 | showStorageErrorDialog(); |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 2957 | } |
| 2958 | }; |
| 2959 | } |
| 2960 | |
Paul Westbrook | 4969e0c | 2012-08-20 14:38:39 -0700 | [diff] [blame] | 2961 | private void showStorageErrorDialog() { |
| 2962 | DialogFragment fragment = (DialogFragment) |
| 2963 | mFragmentManager.findFragmentByTag(SYNC_ERROR_DIALOG_FRAGMENT_TAG); |
| 2964 | if (fragment == null) { |
| 2965 | fragment = SyncErrorDialogFragment.newInstance(); |
| 2966 | } |
| 2967 | fragment.show(mFragmentManager, SYNC_ERROR_DIALOG_FRAGMENT_TAG); |
| 2968 | } |
| 2969 | |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 2970 | private ActionClickedListener getInternalErrorClickedListener() { |
| 2971 | return new ActionClickedListener() { |
| 2972 | @Override |
| 2973 | public void onActionClicked() { |
Paul Westbrook | 17beb0b | 2012-08-20 13:34:37 -0700 | [diff] [blame] | 2974 | Utils.sendFeedback( |
| 2975 | mActivity.getActivityContext(), mAccount, true /* reportingProblem */); |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 2976 | } |
| 2977 | }; |
| 2978 | } |
Paul Westbrook | 4969e0c | 2012-08-20 14:38:39 -0700 | [diff] [blame] | 2979 | |
| 2980 | @Override |
| 2981 | public void onFooterViewErrorActionClick(Folder folder, int errorStatus) { |
| 2982 | Uri uri = null; |
| 2983 | switch (errorStatus) { |
| 2984 | case UIProvider.LastSyncResult.CONNECTION_ERROR: |
| 2985 | if (folder != null && folder.refreshUri != null) { |
| 2986 | uri = folder.refreshUri; |
| 2987 | } |
| 2988 | break; |
| 2989 | case UIProvider.LastSyncResult.AUTH_ERROR: |
Paul Westbrook | 122f7c2 | 2012-08-20 17:50:31 -0700 | [diff] [blame] | 2990 | promptUserForAuthentication(mAccount); |
Paul Westbrook | 4969e0c | 2012-08-20 14:38:39 -0700 | [diff] [blame] | 2991 | return; |
| 2992 | case UIProvider.LastSyncResult.SECURITY_ERROR: |
| 2993 | return; // Currently we do nothing for security errors. |
| 2994 | case UIProvider.LastSyncResult.STORAGE_ERROR: |
| 2995 | showStorageErrorDialog(); |
| 2996 | return; |
| 2997 | case UIProvider.LastSyncResult.INTERNAL_ERROR: |
| 2998 | Utils.sendFeedback( |
| 2999 | mActivity.getActivityContext(), mAccount, true /* reportingProblem */); |
| 3000 | return; |
| 3001 | default: |
| 3002 | return; |
| 3003 | } |
| 3004 | |
| 3005 | if (uri != null) { |
| 3006 | startAsyncRefreshTask(uri); |
| 3007 | } |
| 3008 | } |
| 3009 | |
| 3010 | @Override |
| 3011 | public void onFooterViewLoadMoreClick(Folder folder) { |
| 3012 | if (folder != null && folder.loadMoreUri != null) { |
| 3013 | startAsyncRefreshTask(folder.loadMoreUri); |
| 3014 | } |
| 3015 | } |
| 3016 | |
| 3017 | private void startAsyncRefreshTask(Uri uri) { |
| 3018 | if (mFolderSyncTask != null) { |
| 3019 | mFolderSyncTask.cancel(true); |
| 3020 | } |
| 3021 | mFolderSyncTask = new AsyncRefreshTask(mActivity.getActivityContext(), uri); |
| 3022 | mFolderSyncTask.execute(); |
| 3023 | } |
Paul Westbrook | 122f7c2 | 2012-08-20 17:50:31 -0700 | [diff] [blame] | 3024 | |
| 3025 | private void promptUserForAuthentication(Account account) { |
Paul Westbrook | 429399b | 2012-08-24 11:19:17 -0700 | [diff] [blame] | 3026 | if (account != null && !Utils.isEmpty(account.reauthenticationIntentUri)) { |
Paul Westbrook | 122f7c2 | 2012-08-20 17:50:31 -0700 | [diff] [blame] | 3027 | final Intent authenticationIntent = |
| 3028 | new Intent(Intent.ACTION_VIEW, account.reauthenticationIntentUri); |
| 3029 | mActivity.startActivityForResult(authenticationIntent, REAUTHENTICATE_REQUEST_CODE); |
| 3030 | } |
| 3031 | } |
mindyp | ca87de4 | 2012-09-28 15:02:39 -0700 | [diff] [blame] | 3032 | |
| 3033 | @Override |
| 3034 | public void onAccessibilityStateChanged() { |
| 3035 | // Clear the cache of objects. |
| 3036 | ConversationItemViewModel.onAccessibilityUpdated(); |
| 3037 | // Re-render the list if it exists. |
| 3038 | ConversationListFragment frag = getConversationListFragment(); |
| 3039 | if (frag != null) { |
| 3040 | AnimatedAdapter adapter = frag.getAnimatedAdapter(); |
| 3041 | if (adapter != null) { |
| 3042 | adapter.notifyDataSetInvalidated(); |
| 3043 | } |
| 3044 | } |
| 3045 | } |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 3046 | } |