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 | |
Andy Huang | 12b3ee4 | 2013-04-24 22:49:43 -0700 | [diff] [blame] | 20 | import android.animation.ValueAnimator; |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 21 | import android.app.ActionBar; |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 22 | import android.app.ActionBar.LayoutParams; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 23 | import android.app.Activity; |
Vikram Aggarwal | 54452ae | 2012-03-13 15:29:00 -0700 | [diff] [blame] | 24 | import android.app.AlertDialog; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 25 | import android.app.Dialog; |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 26 | import android.app.DialogFragment; |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 27 | import android.app.Fragment; |
Paul Westbrook | 2d50bcd | 2012-04-10 11:53:47 -0700 | [diff] [blame] | 28 | import android.app.FragmentManager; |
Andy Huang | f9a7348 | 2012-03-13 15:54:02 -0700 | [diff] [blame] | 29 | import android.app.LoaderManager; |
Vikram Aggarwal | e620a7a | 2012-03-28 13:16:14 -0700 | [diff] [blame] | 30 | import android.app.SearchManager; |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 31 | import android.content.ContentProviderOperation; |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 32 | import android.content.ContentResolver; |
Mindy Pereira | 6c2663d | 2012-07-20 15:37:29 -0700 | [diff] [blame] | 33 | import android.content.ContentValues; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 34 | import android.content.Context; |
Vikram Aggarwal | 54452ae | 2012-03-13 15:29:00 -0700 | [diff] [blame] | 35 | import android.content.DialogInterface; |
Vikram Aggarwal | 6cadbfc | 2012-12-27 09:17:05 -0800 | [diff] [blame] | 36 | import android.content.DialogInterface.OnClickListener; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 37 | import android.content.Intent; |
Vikram Aggarwal | 7dedb95 | 2012-02-16 16:10:23 -0800 | [diff] [blame] | 38 | import android.content.Loader; |
Paul Westbrook | 57246a4 | 2013-04-21 09:40:22 -0700 | [diff] [blame] | 39 | import android.content.res.Configuration; |
Vikram Aggarwal | bcb16b9 | 2013-01-28 18:05:03 -0800 | [diff] [blame] | 40 | import android.content.res.Resources; |
Scott Kennedy | ad41814 | 2013-07-10 17:18:22 -0700 | [diff] [blame] | 41 | import android.database.Cursor; |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 42 | import android.database.DataSetObservable; |
| 43 | import android.database.DataSetObserver; |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 44 | import android.net.Uri; |
Vikram Aggarwal | 27d89ad | 2012-06-12 13:38:40 -0700 | [diff] [blame] | 45 | import android.os.AsyncTask; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 46 | import android.os.Bundle; |
Mindy Pereira | 21ab490 | 2012-03-19 18:48:03 -0700 | [diff] [blame] | 47 | import android.os.Handler; |
Scott Kennedy | f77806e | 2013-08-30 11:38:15 -0700 | [diff] [blame^] | 48 | import android.os.Parcelable; |
Vikram Aggarwal | e620a7a | 2012-03-28 13:16:14 -0700 | [diff] [blame] | 49 | import android.provider.SearchRecentSuggestions; |
Andy Huang | 12b3ee4 | 2013-04-24 22:49:43 -0700 | [diff] [blame] | 50 | import android.support.v4.app.ActionBarDrawerToggle; |
| 51 | import android.support.v4.widget.DrawerLayout; |
Mindy Pereira | acf6039 | 2012-04-06 09:11:00 -0700 | [diff] [blame] | 52 | import android.view.DragEvent; |
Andy Huang | 12b3ee4 | 2013-04-24 22:49:43 -0700 | [diff] [blame] | 53 | import android.view.Gravity; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 54 | import android.view.KeyEvent; |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 55 | import android.view.LayoutInflater; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 56 | import android.view.Menu; |
Mindy Pereira | 28d5f72 | 2012-02-15 12:32:40 -0800 | [diff] [blame] | 57 | import android.view.MenuInflater; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 58 | import android.view.MenuItem; |
| 59 | import android.view.MotionEvent; |
Mindy Pereira | d3367499 | 2012-06-25 16:26:30 -0700 | [diff] [blame] | 60 | import android.view.View; |
Andy Huang | 12b3ee4 | 2013-04-24 22:49:43 -0700 | [diff] [blame] | 61 | import android.widget.ListView; |
Mindy Pereira | fd0c297 | 2012-03-27 13:50:39 -0700 | [diff] [blame] | 62 | import android.widget.Toast; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 63 | |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 64 | import com.android.mail.ConversationListContext; |
Vikram Aggarwal | 59f741f | 2013-03-01 15:55:40 -0800 | [diff] [blame] | 65 | import com.android.mail.MailLogService; |
Andy Huang | f9a7348 | 2012-03-13 15:54:02 -0700 | [diff] [blame] | 66 | import com.android.mail.R; |
Andy Huang | 761522c | 2013-08-08 13:09:11 -0700 | [diff] [blame] | 67 | import com.android.mail.analytics.Analytics; |
| 68 | import com.android.mail.analytics.AnalyticsUtils; |
Vikram Aggarwal | 6cadbfc | 2012-12-27 09:17:05 -0800 | [diff] [blame] | 69 | import com.android.mail.browse.ConfirmDialogFragment; |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 70 | import com.android.mail.browse.ConversationCursor; |
Yu Ping Hu | 7c909c7 | 2013-01-18 11:58:01 -0800 | [diff] [blame] | 71 | import com.android.mail.browse.ConversationCursor.ConversationOperation; |
mindyp | ca87de4 | 2012-09-28 15:02:39 -0700 | [diff] [blame] | 72 | import com.android.mail.browse.ConversationItemViewModel; |
Andrew Sapperstein | 8812d3c | 2013-06-04 17:06:41 -0700 | [diff] [blame] | 73 | import com.android.mail.browse.ConversationMessage; |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 74 | import com.android.mail.browse.ConversationPagerController; |
Marc Blank | 7c9f6ac | 2012-04-02 13:27:19 -0700 | [diff] [blame] | 75 | import com.android.mail.browse.SelectedConversationsActionMenu; |
Andy Huang | 991f453 | 2012-08-14 13:32:55 -0700 | [diff] [blame] | 76 | import com.android.mail.browse.SyncErrorDialogFragment; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 77 | import com.android.mail.compose.ComposeActivity; |
Vikram Aggarwal | 177097f | 2013-03-08 11:19:53 -0800 | [diff] [blame] | 78 | import com.android.mail.content.CursorCreator; |
| 79 | import com.android.mail.content.ObjectCursor; |
| 80 | import com.android.mail.content.ObjectCursorLoader; |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 81 | import com.android.mail.providers.Account; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 82 | import com.android.mail.providers.Conversation; |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 83 | import com.android.mail.providers.ConversationInfo; |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 84 | import com.android.mail.providers.Folder; |
Vikram Aggarwal | 7a5d95a | 2012-07-27 16:24:54 -0700 | [diff] [blame] | 85 | import com.android.mail.providers.FolderWatcher; |
Paul Westbrook | c2074c4 | 2012-03-22 15:26:58 -0700 | [diff] [blame] | 86 | import com.android.mail.providers.MailAppProvider; |
Mindy Pereira | dac0054 | 2012-03-01 10:50:33 -0800 | [diff] [blame] | 87 | import com.android.mail.providers.Settings; |
Vikram Aggarwal | e620a7a | 2012-03-28 13:16:14 -0700 | [diff] [blame] | 88 | import com.android.mail.providers.SuggestionsProvider; |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 89 | import com.android.mail.providers.UIProvider; |
Mindy Pereira | 3cb28b5 | 2012-05-24 15:26:39 -0700 | [diff] [blame] | 90 | import com.android.mail.providers.UIProvider.AccountCapabilities; |
Scott Kennedy | 0d0f8b0 | 2012-10-12 15:18:18 -0700 | [diff] [blame] | 91 | import com.android.mail.providers.UIProvider.AccountColumns; |
Paul Westbrook | 2388c5d | 2012-03-25 12:29:11 -0700 | [diff] [blame] | 92 | import com.android.mail.providers.UIProvider.AccountCursorExtraKeys; |
Scott Kennedy | 0d0f8b0 | 2012-10-12 15:18:18 -0700 | [diff] [blame] | 93 | import com.android.mail.providers.UIProvider.AutoAdvance; |
Mindy Pereira | c9d5918 | 2012-03-22 16:06:46 -0700 | [diff] [blame] | 94 | import com.android.mail.providers.UIProvider.ConversationColumns; |
Paul Westbrook | 5109c51 | 2012-11-05 11:00:30 -0800 | [diff] [blame] | 95 | import com.android.mail.providers.UIProvider.ConversationOperations; |
Vikram Aggarwal | 54452ae | 2012-03-13 15:29:00 -0700 | [diff] [blame] | 96 | import com.android.mail.providers.UIProvider.FolderCapabilities; |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 97 | import com.android.mail.ui.ActionableToastBar.ActionClickedListener; |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 98 | import com.android.mail.utils.ContentProviderTask; |
Andy Huang | 144bfe7 | 2013-06-11 13:27:52 -0700 | [diff] [blame] | 99 | import com.android.mail.utils.DrawIdler; |
Paul Westbrook | b334c90 | 2012-06-25 11:42:46 -0700 | [diff] [blame] | 100 | import com.android.mail.utils.LogTag; |
Mindy Pereira | 5cb0c3e | 2012-02-22 15:22:47 -0800 | [diff] [blame] | 101 | import com.android.mail.utils.LogUtils; |
Scott Kennedy | cb85aea | 2013-02-25 13:08:32 -0800 | [diff] [blame] | 102 | import com.android.mail.utils.NotificationActionUtils; |
Vikram Aggarwal | 07dbaa6 | 2013-03-12 15:21:04 -0700 | [diff] [blame] | 103 | import com.android.mail.utils.Observable; |
Vikram Aggarwal | fa131a2 | 2012-02-02 13:56:22 -0800 | [diff] [blame] | 104 | import com.android.mail.utils.Utils; |
Vikram Aggarwal | 69a6cdf | 2013-01-08 16:05:17 -0800 | [diff] [blame] | 105 | import com.android.mail.utils.VeiledAddressMatcher; |
Paul Westbrook | ca08fc1 | 2012-07-31 12:01:15 -0700 | [diff] [blame] | 106 | import com.google.common.base.Objects; |
Paul Westbrook | 77eee62 | 2012-07-10 13:41:57 -0700 | [diff] [blame] | 107 | import com.google.common.collect.ImmutableList; |
Mindy Pereira | acf6039 | 2012-04-06 09:11:00 -0700 | [diff] [blame] | 108 | import com.google.common.collect.Lists; |
Marc Blank | 167faa8 | 2012-03-21 13:11:53 -0700 | [diff] [blame] | 109 | import com.google.common.collect.Sets; |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 110 | |
Marc Blank | 167faa8 | 2012-03-21 13:11:53 -0700 | [diff] [blame] | 111 | import java.util.ArrayList; |
Andy Huang | 9e4ca79 | 2013-02-28 14:33:43 -0800 | [diff] [blame] | 112 | import java.util.Arrays; |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 113 | import java.util.Collection; |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 114 | import java.util.Collections; |
Andy Huang | c1fb9a9 | 2013-02-11 13:09:12 -0800 | [diff] [blame] | 115 | import java.util.Deque; |
Mindy Pereira | 8db7e40 | 2012-07-13 10:32:47 -0700 | [diff] [blame] | 116 | import java.util.HashMap; |
Vikram Aggarwal | cc754b1 | 2012-08-30 14:04:21 -0700 | [diff] [blame] | 117 | import java.util.List; |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 118 | import java.util.Set; |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 119 | import java.util.TimerTask; |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 120 | |
| 121 | |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 122 | /** |
Mindy Pereira | 161f50d | 2012-02-28 15:47:19 -0800 | [diff] [blame] | 123 | * This is an abstract implementation of the Activity Controller. This class |
| 124 | * knows how to respond to menu items, state changes, layout changes, etc. It |
| 125 | * weaves together the views and listeners, dispatching actions to the |
| 126 | * respective underlying classes. |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 127 | * <p> |
Mindy Pereira | 161f50d | 2012-02-28 15:47:19 -0800 | [diff] [blame] | 128 | * Even though this class is abstract, it should provide default implementations |
| 129 | * for most, if not all the methods in the ActivityController interface. This |
| 130 | * makes the task of the subclasses easier: OnePaneActivityController and |
| 131 | * TwoPaneActivityController can be concise when the common functionality is in |
| 132 | * AbstractActivityController. |
| 133 | * </p> |
| 134 | * <p> |
| 135 | * In the Gmail codebase, this was called BaseActivityController |
| 136 | * </p> |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 137 | */ |
Andrew Sapperstein | ed5b52d | 2013-04-30 13:40:18 -0700 | [diff] [blame] | 138 | public abstract class AbstractActivityController implements ActivityController, |
| 139 | EmptyFolderDialogFragment.EmptyFolderDialogFragmentListener { |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 140 | // Keys for serialization of various information in Bundles. |
Vikram Aggarwal | cabd3f2 | 2012-04-19 10:14:41 -0700 | [diff] [blame] | 141 | /** Tag for {@link #mAccount} */ |
Vikram Aggarwal | 6c51158 | 2012-02-27 10:59:47 -0800 | [diff] [blame] | 142 | private static final String SAVED_ACCOUNT = "saved-account"; |
Vikram Aggarwal | cabd3f2 | 2012-04-19 10:14:41 -0700 | [diff] [blame] | 143 | /** Tag for {@link #mFolder} */ |
Mindy Pereira | 5e478d2 | 2012-03-26 18:04:58 -0700 | [diff] [blame] | 144 | private static final String SAVED_FOLDER = "saved-folder"; |
Vikram Aggarwal | cabd3f2 | 2012-04-19 10:14:41 -0700 | [diff] [blame] | 145 | /** Tag for {@link #mCurrentConversation} */ |
Mindy Pereira | 26f23fc | 2012-03-27 10:26:04 -0700 | [diff] [blame] | 146 | private static final String SAVED_CONVERSATION = "saved-conversation"; |
Vikram Aggarwal | cabd3f2 | 2012-04-19 10:14:41 -0700 | [diff] [blame] | 147 | /** Tag for {@link #mSelectedSet} */ |
| 148 | private static final String SAVED_SELECTED_SET = "saved-selected-set"; |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame] | 149 | /** Tag for {@link ActionableToastBar#getOperation()} */ |
Mindy Pereira | d3367499 | 2012-06-25 16:26:30 -0700 | [diff] [blame] | 150 | private static final String SAVED_TOAST_BAR_OP = "saved-toast-bar-op"; |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame] | 151 | /** Tag for {@link #mFolderListFolder} */ |
| 152 | private static final String SAVED_HIERARCHICAL_FOLDER = "saved-hierarchical-folder"; |
Vikram Aggarwal | f334140 | 2012-08-07 10:09:38 -0700 | [diff] [blame] | 153 | /** Tag for {@link ConversationListContext#searchQuery} */ |
| 154 | private static final String SAVED_QUERY = "saved-query"; |
Vikram Aggarwal | 6cadbfc | 2012-12-27 09:17:05 -0800 | [diff] [blame] | 155 | /** Tag for {@link #mDialogAction} */ |
| 156 | private static final String SAVED_ACTION = "saved-action"; |
Vikram Aggarwal | b8c3171 | 2013-01-03 17:03:19 -0800 | [diff] [blame] | 157 | /** Tag for {@link #mDialogFromSelectedSet} */ |
| 158 | private static final String SAVED_ACTION_FROM_SELECTED = "saved-action-from-selected"; |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame] | 159 | /** Tag for {@link #mDetachedConvUri} */ |
| 160 | private static final String SAVED_DETACHED_CONV_URI = "saved-detached-conv-uri"; |
Scott Kennedy | ad41814 | 2013-07-10 17:18:22 -0700 | [diff] [blame] | 161 | /** Key to store {@link #mInbox}. */ |
Scott Kennedy | f77806e | 2013-08-30 11:38:15 -0700 | [diff] [blame^] | 162 | private static final String SAVED_INBOX_KEY = "m-inbox"; |
| 163 | /** Key to store {@link #mConversationListScrollPositions} */ |
| 164 | private static final String SAVED_CONVERSATION_LIST_SCROLL_POSITIONS = |
| 165 | "saved-conversation-list-scroll-positions"; |
Mindy Pereira | 5cb0c3e | 2012-02-22 15:22:47 -0800 | [diff] [blame] | 166 | |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 167 | /** Tag used when loading a wait fragment */ |
| 168 | protected static final String TAG_WAIT = "wait-fragment"; |
| 169 | /** Tag used when loading a conversation list fragment. */ |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 170 | public static final String TAG_CONVERSATION_LIST = "tag-conversation-list"; |
Scott Kennedy | 103319a | 2013-07-26 13:35:35 -0700 | [diff] [blame] | 171 | /** Tag used when loading a custom fragment. */ |
| 172 | protected static final String TAG_CUSTOM_FRAGMENT = "tag-custom-fragment"; |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 173 | |
Vikram Aggarwal | a1b59dc | 2013-04-30 15:45:49 -0700 | [diff] [blame] | 174 | /** Key to store an account in a bundle */ |
| 175 | private final String BUNDLE_ACCOUNT_KEY = "account"; |
| 176 | /** Key to store a folder in a bundle */ |
| 177 | private final String BUNDLE_FOLDER_KEY = "folder"; |
| 178 | |
Vikram Aggarwal | d7a12cd | 2012-02-03 09:36:20 -0800 | [diff] [blame] | 179 | protected Account mAccount; |
Mindy Pereira | 12a4d80 | 2012-06-22 09:24:43 -0700 | [diff] [blame] | 180 | protected Folder mFolder; |
Scott Kennedy | ad41814 | 2013-07-10 17:18:22 -0700 | [diff] [blame] | 181 | protected Folder mInbox; |
Vikram Aggarwal | 69b5c30 | 2012-09-05 11:11:13 -0700 | [diff] [blame] | 182 | /** True when {@link #mFolder} is first shown to the user. */ |
| 183 | private boolean mFolderChanged = false; |
Andy Huang | 6681e54 | 2012-06-14 14:36:45 -0700 | [diff] [blame] | 184 | protected MailActionBarView mActionBarView; |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 185 | protected final ControllableActivity mActivity; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 186 | protected final Context mContext; |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 187 | private final FragmentManager mFragmentManager; |
Vikram Aggarwal | ec5cbf7 | 2012-03-08 15:10:35 -0800 | [diff] [blame] | 188 | protected final RecentFolderList mRecentFolderList; |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 189 | protected ConversationListContext mConvListContext; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 190 | protected Conversation mCurrentConversation; |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame] | 191 | /** |
| 192 | * The hash of {@link #mCurrentConversation} in detached mode. 0 if we are not in detached mode. |
| 193 | */ |
| 194 | private Uri mDetachedConvUri; |
Vikram Aggarwal | d7a12cd | 2012-02-03 09:36:20 -0800 | [diff] [blame] | 195 | |
Scott Kennedy | f77806e | 2013-08-30 11:38:15 -0700 | [diff] [blame^] | 196 | /** A map of {@link Folder} {@link Uri} to scroll position in the conversation list. */ |
| 197 | private final Bundle mConversationListScrollPositions = new Bundle(); |
| 198 | |
Paul Westbrook | 6ead20d | 2012-03-19 14:48:14 -0700 | [diff] [blame] | 199 | /** A {@link android.content.BroadcastReceiver} that suppresses new e-mail notifications. */ |
| 200 | private SuppressNotificationReceiver mNewEmailReceiver = null; |
| 201 | |
Vikram Aggarwal | 59f741f | 2013-03-01 15:55:40 -0800 | [diff] [blame] | 202 | /** Handler for all our local runnables. */ |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 203 | protected Handler mHandler = new Handler(); |
Mindy Pereira | fa995b4 | 2012-07-25 12:06:13 -0700 | [diff] [blame] | 204 | |
Vikram Aggarwal | fa131a2 | 2012-02-02 13:56:22 -0800 | [diff] [blame] | 205 | /** |
Mindy Pereira | 161f50d | 2012-02-28 15:47:19 -0800 | [diff] [blame] | 206 | * The current mode of the application. All changes in mode are initiated by |
| 207 | * the activity controller. View mode changes are propagated to classes that |
| 208 | * attach themselves as listeners of view mode changes. |
Vikram Aggarwal | fa131a2 | 2012-02-02 13:56:22 -0800 | [diff] [blame] | 209 | */ |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 210 | protected final ViewMode mViewMode; |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 211 | protected ContentResolver mResolver; |
Vikram Aggarwal | c04cf7e | 2013-05-13 15:38:42 -0700 | [diff] [blame] | 212 | protected boolean mHaveAccountList = false; |
Mindy Pereira | b7b33e0 | 2012-02-21 15:32:19 -0800 | [diff] [blame] | 213 | private AsyncRefreshTask mAsyncRefreshTask; |
Mindy Pereira | 5cb0c3e | 2012-02-22 15:22:47 -0800 | [diff] [blame] | 214 | |
Andy Huang | 4e0158f | 2012-08-07 21:06:01 -0700 | [diff] [blame] | 215 | private boolean mDestroyed; |
| 216 | |
Vikram Aggarwal | bcb16b9 | 2013-01-28 18:05:03 -0800 | [diff] [blame] | 217 | /** True if running on tablet */ |
| 218 | private final boolean mIsTablet; |
| 219 | |
Andy Huang | 1ee96b2 | 2012-08-24 20:19:53 -0700 | [diff] [blame] | 220 | /** |
| 221 | * Are we in a point in the Activity/Fragment lifecycle where it's safe to execute fragment |
| 222 | * transactions? (including back stack manipulation) |
| 223 | * <p> |
| 224 | * Per docs in {@link FragmentManager#beginTransaction()}, this flag starts out true, switches |
| 225 | * to false after {@link Activity#onSaveInstanceState}, and becomes true again in both onStart |
| 226 | * and onResume. |
| 227 | */ |
| 228 | private boolean mSafeToModifyFragments = true; |
| 229 | |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 230 | private final Set<Uri> mCurrentAccountUris = Sets.newHashSet(); |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 231 | protected ConversationCursor mConversationListCursor; |
Vikram Aggarwal | 07dbaa6 | 2013-03-12 15:21:04 -0700 | [diff] [blame] | 232 | private final DataSetObservable mConversationListObservable = new Observable("List"); |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 233 | |
Vikram Aggarwal | 59f741f | 2013-03-01 15:55:40 -0800 | [diff] [blame] | 234 | /** Runnable that checks the logging level to enable/disable the logging service. */ |
| 235 | private Runnable mLogServiceChecker = null; |
Vikram Aggarwal | de60c9d | 2013-04-10 12:58:56 -0700 | [diff] [blame] | 236 | /** List of all accounts currently known to the controller. This is never null. */ |
| 237 | private Account[] mAllAccounts = new Account[0]; |
Vikram Aggarwal | 59f741f | 2013-03-01 15:55:40 -0800 | [diff] [blame] | 238 | |
Vikram Aggarwal | 77ee0ce | 2013-04-28 15:32:36 -0700 | [diff] [blame] | 239 | private FolderWatcher mFolderWatcher; |
| 240 | |
Yu Ping Hu | 7c909c7 | 2013-01-18 11:58:01 -0800 | [diff] [blame] | 241 | /** |
| 242 | * Interface for actions that are deferred until after a load completes. This is for handling |
| 243 | * user actions which affect cursors (e.g. marking messages read or unread) that happen before |
| 244 | * that cursor is loaded. |
| 245 | */ |
| 246 | private interface LoadFinishedCallback { |
| 247 | void onLoadFinished(); |
| 248 | } |
| 249 | |
| 250 | /** The deferred actions to execute when mConversationListCursor load completes. */ |
| 251 | private final ArrayList<LoadFinishedCallback> mConversationListLoadFinishedCallbacks = |
| 252 | new ArrayList<LoadFinishedCallback>(); |
| 253 | |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 254 | private RefreshTimerTask mConversationListRefreshTask; |
Marc Blank | e1d1b07 | 2012-04-13 17:29:16 -0700 | [diff] [blame] | 255 | |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 256 | /** Listeners that are interested in changes to the current account. */ |
Vikram Aggarwal | 07dbaa6 | 2013-03-12 15:21:04 -0700 | [diff] [blame] | 257 | private final DataSetObservable mAccountObservers = new Observable("Account"); |
Vikram Aggarwal | 58cad2e | 2012-08-28 16:18:23 -0700 | [diff] [blame] | 258 | /** Listeners that are interested in changes to the recent folders. */ |
Vikram Aggarwal | 07dbaa6 | 2013-03-12 15:21:04 -0700 | [diff] [blame] | 259 | private final DataSetObservable mRecentFolderObservers = new Observable("RecentFolder"); |
| 260 | /** Listeners that are interested in changes to the list of all accounts. */ |
| 261 | private final DataSetObservable mAllAccountObservers = new Observable("AllAccounts"); |
Vikram Aggarwal | 50ff0e5 | 2013-03-14 13:58:02 -0700 | [diff] [blame] | 262 | /** Listeners that are interested in changes to the current folder. */ |
| 263 | private final DataSetObservable mFolderObservable = new Observable("CurrentFolder"); |
Rohan Shah | 0f73d90 | 2013-04-19 17:06:37 -0700 | [diff] [blame] | 264 | /** Listeners that are interested in changes to the drawer state. */ |
| 265 | private final DataSetObservable mDrawerObservers = new Observable("Drawer"); |
Vikram Aggarwal | 58cad2e | 2012-08-28 16:18:23 -0700 | [diff] [blame] | 266 | |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 267 | /** |
| 268 | * Selected conversations, if any. |
| 269 | */ |
Andy Huang | 4556a44 | 2012-03-30 16:42:05 -0700 | [diff] [blame] | 270 | private final ConversationSelectionSet mSelectedSet = new ConversationSelectionSet(); |
Mindy Pereira | dac0054 | 2012-03-01 10:50:33 -0800 | [diff] [blame] | 271 | |
Paul Westbrook | c7a070f | 2012-04-12 01:46:41 -0700 | [diff] [blame] | 272 | private final int mFolderItemUpdateDelayMs; |
| 273 | |
Vikram Aggarwal | 135fd02 | 2012-04-11 14:44:15 -0700 | [diff] [blame] | 274 | /** Keeps track of selected and unselected conversations */ |
Paul Westbrook | 937c94f | 2012-08-16 13:01:18 -0700 | [diff] [blame] | 275 | final protected ConversationPositionTracker mTracker; |
Vikram Aggarwal | af1ee0c | 2012-04-12 17:13:13 -0700 | [diff] [blame] | 276 | |
Vikram Aggarwal | e128fc2 | 2012-04-04 12:33:34 -0700 | [diff] [blame] | 277 | /** |
| 278 | * Action menu associated with the selected set. |
| 279 | */ |
| 280 | SelectedConversationsActionMenu mCabActionMenu; |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 281 | protected ActionableToastBar mToastBar; |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 282 | protected ConversationPagerController mPagerController; |
Mindy Pereira | ab48636 | 2012-03-21 18:18:53 -0700 | [diff] [blame] | 283 | |
Vikram Aggarwal | d70fe49 | 2013-06-04 12:52:07 -0700 | [diff] [blame] | 284 | // This is split out from the general loader dispatcher because its loader doesn't return a |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 285 | // basic Cursor |
Vikram Aggarwal | d70fe49 | 2013-06-04 12:52:07 -0700 | [diff] [blame] | 286 | /** Handles loader callbacks to create a convesation cursor. */ |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 287 | private final ConversationListLoaderCallbacks mListCursorCallbacks = |
| 288 | new ConversationListLoaderCallbacks(); |
| 289 | |
Vikram Aggarwal | 177097f | 2013-03-08 11:19:53 -0800 | [diff] [blame] | 290 | /** Object that listens to all LoaderCallbacks that result in {@link Folder} creation. */ |
| 291 | private final FolderLoads mFolderCallbacks = new FolderLoads(); |
| 292 | /** Object that listens to all LoaderCallbacks that result in {@link Account} creation. */ |
| 293 | private final AccountLoads mAccountCallbacks = new AccountLoads(); |
| 294 | |
Vikram Aggarwal | 69a6cdf | 2013-01-08 16:05:17 -0800 | [diff] [blame] | 295 | /** |
| 296 | * Matched addresses that must be shielded from users because they are temporary. Even though |
| 297 | * this is instantiated from settings, this matcher is valid for all accounts, and is expected |
| 298 | * to live past the life of an account. |
| 299 | */ |
| 300 | private final VeiledAddressMatcher mVeiledMatcher; |
| 301 | |
Paul Westbrook | b334c90 | 2012-06-25 11:42:46 -0700 | [diff] [blame] | 302 | protected static final String LOG_TAG = LogTag.getLogTag(); |
Vikram Aggarwal | d70fe49 | 2013-06-04 12:52:07 -0700 | [diff] [blame] | 303 | |
Vikram Aggarwal | aa941d7 | 2013-06-04 15:34:28 -0700 | [diff] [blame] | 304 | // Loader constants: Accounts |
| 305 | /** |
| 306 | * The list of accounts. This loader is started early in the application life-cycle since |
| 307 | * the list of accounts is central to all other data the application needs: unread counts for |
| 308 | * folders, critical UI settings like show/hide checkboxes, ... |
| 309 | * The loader is started when the application is created: both in |
| 310 | * {@link #onCreate(Bundle)} and in {@link #onActivityResult(int, int, Intent)}. It is never |
| 311 | * destroyed since the cursor is needed through the life of the application. When the list of |
| 312 | * accounts changes, we notify {@link #mAllAccountObservers}. |
| 313 | */ |
Vikram Aggarwal | fa4b47e | 2012-03-09 13:02:46 -0800 | [diff] [blame] | 314 | private static final int LOADER_ACCOUNT_CURSOR = 0; |
Vikram Aggarwal | aa941d7 | 2013-06-04 15:34:28 -0700 | [diff] [blame] | 315 | |
| 316 | /** |
| 317 | * The current account. This loader is started when we have an account. The mail application |
| 318 | * <b>needs</b> a valid account to function. As soon as we set {@link #mAccount}, |
| 319 | * we start a loader to observe for changes on the current account. |
| 320 | * The loader is always restarted when an account is set in {@link #setAccount(Account)}. |
| 321 | * When the current account object changes, we notify {@link #mAccountObservers}. |
| 322 | * A possible performance improvement would be to listen purely on |
| 323 | * {@link #LOADER_ACCOUNT_CURSOR}. The current account is guaranteed to be in the list, |
| 324 | * and would avoid two updates when a single setting on the current account changes. |
| 325 | */ |
Paul Westbrook | 2d50bcd | 2012-04-10 11:53:47 -0700 | [diff] [blame] | 326 | private static final int LOADER_ACCOUNT_UPDATE_CURSOR = 7; |
Vikram Aggarwal | d70fe49 | 2013-06-04 12:52:07 -0700 | [diff] [blame] | 327 | |
Vikram Aggarwal | aa941d7 | 2013-06-04 15:34:28 -0700 | [diff] [blame] | 328 | // Loader constants: Folders |
| 329 | /** The current folder. This loader watches for updates to the current folder in a manner |
| 330 | * analogous to the {@link #LOADER_ACCOUNT_UPDATE_CURSOR}. Updates to the current folder |
| 331 | * might be due to server-side changes (unread count), or local changes (sync window or sync |
| 332 | * status change). |
| 333 | * The change of current folder calls {@link #updateFolder(Folder)}. |
| 334 | * This is responsible for restarting a loader using the URI of the provided folder. When the |
| 335 | * loader returns, the current folder is updated and consumers, if any, are notified. |
| 336 | * When the current folder changes, we notify {@link #mFolderObservable} |
| 337 | */ |
Vikram Aggarwal | d70fe49 | 2013-06-04 12:52:07 -0700 | [diff] [blame] | 338 | private static final int LOADER_FOLDER_CURSOR = 2; |
Vikram Aggarwal | aa941d7 | 2013-06-04 15:34:28 -0700 | [diff] [blame] | 339 | /** |
| 340 | * The list of recent folders. Recent folders are shown in the DrawerFragment. The recent |
| 341 | * folders are tied to the current account being viewed. When the account is changed, |
| 342 | * we restart this loader to retrieve the recent accounts. Recents are pre-populated for |
| 343 | * phones historically, when they were displayed in the spinner. On the tablet, |
| 344 | * they showed in the {@link FolderListFragment} and were not-populated. The code to |
| 345 | * pre-populate the recents is somewhat convoluted: when the loader returns a short list of |
| 346 | * recent folders, it issues an update on the Recent Folder URI. The underlying provider then |
| 347 | * does the appropriate thing to populate recent folders, and notify of a change on the cursor. |
| 348 | * Recent folders are needed for the life of the current account. |
| 349 | * When the recent folders change, we notify {@link #mRecentFolderObservers}. |
| 350 | */ |
Vikram Aggarwal | d70fe49 | 2013-06-04 12:52:07 -0700 | [diff] [blame] | 351 | private static final int LOADER_RECENT_FOLDERS = 3; |
Vikram Aggarwal | aa941d7 | 2013-06-04 15:34:28 -0700 | [diff] [blame] | 352 | /** |
| 353 | * The primary inbox for the current account. The mechanism to load the default inbox for the |
| 354 | * current account is (sadly) different from loading other folders. The method |
| 355 | * {@link #loadAccountInbox()} is called, and it restarts this loader. When the loader returns |
| 356 | * a valid cursor, we create a folder, call {@link #onFolderChanged{Folder)} eventually |
| 357 | * calling {@link #updateFolder(Folder)} which starts a loader {@link #LOADER_FOLDER_CURSOR} |
| 358 | * over the current folder. |
| 359 | * When we have a valid cursor, we destroy this loader, This convoluted flow is historical. |
| 360 | */ |
Vikram Aggarwal | d70fe49 | 2013-06-04 12:52:07 -0700 | [diff] [blame] | 361 | private static final int LOADER_ACCOUNT_INBOX = 5; |
Vikram Aggarwal | aa941d7 | 2013-06-04 15:34:28 -0700 | [diff] [blame] | 362 | /** |
| 363 | * The fake folder of search results for a term. When we search for a term, |
| 364 | * a new activity is created with {@link Intent#ACTION_SEARCH}. For this new activity, |
| 365 | * we start a loader which returns conversations that match the user-provided query. |
| 366 | * We destroy the loader when we obtain a valid cursor since subsequent searches will create |
| 367 | * a new activity. |
| 368 | */ |
Vikram Aggarwal | d70fe49 | 2013-06-04 12:52:07 -0700 | [diff] [blame] | 369 | private static final int LOADER_SEARCH = 6; |
Vikram Aggarwal | aa941d7 | 2013-06-04 15:34:28 -0700 | [diff] [blame] | 370 | /** |
| 371 | * The initial folder at app start. When the application is launched from an intent that |
| 372 | * specifies the initial folder (notifications/widgets/shortcuts), |
| 373 | * then we extract the folder URI from the intent, but we cannot trust the folder object. Since |
| 374 | * shortcuts and widgets persist past application update, they might have incorrect |
| 375 | * information encoded in them. So, to obtain a {@link Folder} object from a {@link Uri}, |
| 376 | * we need to start another loader. Upon obtaining a valid cursor, the loader is destroyed. |
| 377 | * An additional complication arises if we have to view a specific conversation within this |
| 378 | * folder. This is the case when launching the app from a single conversation notification |
| 379 | * or tapping on a specific conversation in the widget. In these cases, the conversation is |
| 380 | * saved in {@link #mConversationToShow} and is retrieved when the loader returns. |
| 381 | */ |
Vikram Aggarwal | 177097f | 2013-03-08 11:19:53 -0800 | [diff] [blame] | 382 | public static final int LOADER_FIRST_FOLDER = 8; |
| 383 | |
Vikram Aggarwal | aa941d7 | 2013-06-04 15:34:28 -0700 | [diff] [blame] | 384 | // Loader constants: Conversations |
| 385 | /** The conversation cursor over the current conversation list. This loader provides |
| 386 | * a cursor over conversation entries from a folder to display a conversation |
| 387 | * list. |
| 388 | * This loader is started when the user switches folders (in {@link #updateFolder(Folder)}, |
| 389 | * or when the controller is told that a folder/account change is imminent |
| 390 | * (in {@link #preloadConvList(Account, Folder)}. The loader is maintained for the life of |
| 391 | * the current folder. When the user switches folders, the old loader is destroyed and a new |
| 392 | * one is created. |
| 393 | * |
| 394 | * When the conversation list changes, we notify {@link #mConversationListObservable}. |
| 395 | */ |
Vikram Aggarwal | d70fe49 | 2013-06-04 12:52:07 -0700 | [diff] [blame] | 396 | private static final int LOADER_CONVERSATION_LIST = 4; |
| 397 | |
Vikram Aggarwal | 7a5d95a | 2012-07-27 16:24:54 -0700 | [diff] [blame] | 398 | /** |
| 399 | * Guaranteed to be the last loader ID used by the activity. Loaders are owned by Activity or |
| 400 | * fragments, and within an activity, loader IDs need to be unique. A hack to ensure that the |
| 401 | * {@link FolderWatcher} can create its folder loaders without clashing with the IDs of those |
| 402 | * of the {@link AbstractActivityController}. Currently, the {@link FolderWatcher} is the only |
| 403 | * other class that uses this activity's LoaderManager. If another class needs activity-level |
| 404 | * loaders, consider consolidating the loaders in a central location: a UI-less fragment |
| 405 | * perhaps. |
| 406 | */ |
| 407 | public static final int LAST_LOADER_ID = 100; |
Scott Kennedy | 7c8325d | 2013-02-28 10:46:10 -0800 | [diff] [blame] | 408 | /** |
| 409 | * Guaranteed to be the last loader ID used by the Fragment. Loaders are owned by Activity or |
| 410 | * fragments, and within an activity, loader IDs need to be unique. Currently, |
| 411 | * {@link SectionedInboxTeaserView} is the only class that uses the |
| 412 | * {@link ConversationListFragment}'s LoaderManager. |
| 413 | */ |
| 414 | public static final int LAST_FRAGMENT_LOADER_ID = 1000; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 415 | |
Vikram Aggarwal | 6aca689 | 2013-06-04 13:53:27 -0700 | [diff] [blame] | 416 | /** Code returned after an account has been added. */ |
Paul Westbrook | 2388c5d | 2012-03-25 12:29:11 -0700 | [diff] [blame] | 417 | private static final int ADD_ACCOUNT_REQUEST_CODE = 1; |
Vikram Aggarwal | 6aca689 | 2013-06-04 13:53:27 -0700 | [diff] [blame] | 418 | /** Code returned when the user has to enter the new password on an existing account. */ |
Paul Westbrook | 122f7c2 | 2012-08-20 17:50:31 -0700 | [diff] [blame] | 419 | private static final int REAUTHENTICATE_REQUEST_CODE = 2; |
Paul Westbrook | 2388c5d | 2012-03-25 12:29:11 -0700 | [diff] [blame] | 420 | |
Vikram Aggarwal | e8a8532 | 2012-04-24 09:01:38 -0700 | [diff] [blame] | 421 | /** The pending destructive action to be carried out before swapping the conversation cursor.*/ |
| 422 | private DestructiveAction mPendingDestruction; |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 423 | protected AsyncRefreshTask mFolderSyncTask; |
Mindy Pereira | c975e84 | 2012-07-16 09:15:00 -0700 | [diff] [blame] | 424 | private Folder mFolderListFolder; |
mindyp | 5390fca | 2012-08-22 12:12:25 -0700 | [diff] [blame] | 425 | private boolean mIsDragHappening; |
mindyp | ead5039 | 2012-08-23 11:03:53 -0700 | [diff] [blame] | 426 | private int mShowUndoBarDelay; |
mindyp | 6f54e1b | 2012-10-09 09:54:08 -0700 | [diff] [blame] | 427 | private boolean mRecentsDataUpdated; |
Vikram Aggarwal | a3f43d4 | 2012-10-25 16:21:30 -0700 | [diff] [blame] | 428 | /** A wait fragment we added, if any. */ |
| 429 | private WaitFragment mWaitFragment; |
Vikram Aggarwal | f0ef230 | 2012-11-07 14:53:34 -0800 | [diff] [blame] | 430 | /** True if we have results from a search query */ |
| 431 | private boolean mHaveSearchResults = false; |
Vikram Aggarwal | 6cadbfc | 2012-12-27 09:17:05 -0800 | [diff] [blame] | 432 | /** If a confirmation dialog is being show, the listener for the positive action. */ |
| 433 | private OnClickListener mDialogListener; |
| 434 | /** |
| 435 | * If a confirmation dialog is being show, the resource of the action: R.id.delete, etc. This |
| 436 | * is used to create a new {@link #mDialogListener} on orientation changes. |
| 437 | */ |
| 438 | private int mDialogAction = -1; |
Vikram Aggarwal | b8c3171 | 2013-01-03 17:03:19 -0800 | [diff] [blame] | 439 | /** |
| 440 | * If a confirmation dialog is being shown, this is true if the dialog acts on the selected set |
| 441 | * and false if it acts on the currently selected conversation |
| 442 | */ |
| 443 | private boolean mDialogFromSelectedSet; |
Vikram Aggarwal | 6cadbfc | 2012-12-27 09:17:05 -0800 | [diff] [blame] | 444 | |
Vikram Aggarwal | 177097f | 2013-03-08 11:19:53 -0800 | [diff] [blame] | 445 | /** Which conversation to show, if started from widget/notification. */ |
| 446 | private Conversation mConversationToShow = null; |
| 447 | |
Andy Huang | c94d07f | 2013-06-03 16:19:35 -0700 | [diff] [blame] | 448 | /** |
| 449 | * A temporary reference to the pending destructive action that was deferred due to an |
| 450 | * auto-advance transition in progress. |
| 451 | * <p> |
| 452 | * In detail: when auto-advance triggers a mode change, we must wait until the transition |
| 453 | * completes before executing the destructive action to ensure a smooth mode change transition. |
| 454 | * This member variable houses the pending destructive action work to be run upon completion. |
| 455 | */ |
| 456 | private Runnable mAutoAdvanceOp = null; |
| 457 | |
Andy Huang | c1fb9a9 | 2013-02-11 13:09:12 -0800 | [diff] [blame] | 458 | private final Deque<UpOrBackHandler> mUpOrBackHandlers = Lists.newLinkedList(); |
| 459 | |
Andy Huang | 12b3ee4 | 2013-04-24 22:49:43 -0700 | [diff] [blame] | 460 | protected DrawerLayout mDrawerContainer; |
| 461 | protected View mDrawerPullout; |
| 462 | protected ActionBarDrawerToggle mDrawerToggle; |
| 463 | protected ListView mListViewForAnimating; |
| 464 | protected boolean mHasNewAccountOrFolder; |
Andrew Sapperstein | a3ce678 | 2013-05-09 15:14:49 -0700 | [diff] [blame] | 465 | private boolean mConversationListLoadFinishedIgnored; |
| 466 | protected MailDrawerListener mDrawerListener; |
Andrew Sapperstein | 5747e15 | 2013-05-13 14:13:08 -0700 | [diff] [blame] | 467 | private boolean mHideMenuItems; |
Andy Huang | 12b3ee4 | 2013-04-24 22:49:43 -0700 | [diff] [blame] | 468 | |
Andy Huang | 144bfe7 | 2013-06-11 13:27:52 -0700 | [diff] [blame] | 469 | private final DrawIdler mDrawIdler = new DrawIdler(); |
| 470 | |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 471 | public static final String SYNC_ERROR_DIALOG_FRAGMENT_TAG = "SyncErrorDialogFragment"; |
Vikram Aggarwal | e8a8532 | 2012-04-24 09:01:38 -0700 | [diff] [blame] | 472 | |
Scott Kennedy | cb85aea | 2013-02-25 13:08:32 -0800 | [diff] [blame] | 473 | private final DataSetObserver mUndoNotificationObserver = new DataSetObserver() { |
| 474 | @Override |
| 475 | public void onChanged() { |
| 476 | super.onChanged(); |
| 477 | |
| 478 | if (mConversationListCursor != null) { |
| 479 | mConversationListCursor.handleNotificationActions(); |
| 480 | } |
| 481 | } |
| 482 | }; |
| 483 | |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 484 | public AbstractActivityController(MailActivity activity, ViewMode viewMode) { |
| 485 | mActivity = activity; |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 486 | mFragmentManager = mActivity.getFragmentManager(); |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 487 | mViewMode = viewMode; |
| 488 | mContext = activity.getApplicationContext(); |
Vikram Aggarwal | 025eba8 | 2012-05-08 10:45:30 -0700 | [diff] [blame] | 489 | mRecentFolderList = new RecentFolderList(mContext); |
Paul Westbrook | 937c94f | 2012-08-16 13:01:18 -0700 | [diff] [blame] | 490 | mTracker = new ConversationPositionTracker(this); |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 491 | // Allow the fragment to observe changes to its own selection set. No other object is |
| 492 | // aware of the selected set. |
| 493 | mSelectedSet.addObserver(this); |
Paul Westbrook | c7a070f | 2012-04-12 01:46:41 -0700 | [diff] [blame] | 494 | |
Vikram Aggarwal | bcb16b9 | 2013-01-28 18:05:03 -0800 | [diff] [blame] | 495 | final Resources r = mContext.getResources(); |
| 496 | mFolderItemUpdateDelayMs = r.getInteger(R.integer.folder_item_refresh_delay_ms); |
| 497 | mShowUndoBarDelay = r.getInteger(R.integer.show_undo_bar_delay_ms); |
Vikram Aggarwal | 69a6cdf | 2013-01-08 16:05:17 -0800 | [diff] [blame] | 498 | mVeiledMatcher = VeiledAddressMatcher.newInstance(activity.getResources()); |
Vikram Aggarwal | bcb16b9 | 2013-01-28 18:05:03 -0800 | [diff] [blame] | 499 | mIsTablet = Utils.useTabletUI(r); |
Andrew Sapperstein | a3ce678 | 2013-05-09 15:14:49 -0700 | [diff] [blame] | 500 | mConversationListLoadFinishedIgnored = false; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 501 | } |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 502 | |
| 503 | @Override |
Vikram Aggarwal | e9a8103 | 2012-02-22 13:15:35 -0800 | [diff] [blame] | 504 | public Account getCurrentAccount() { |
| 505 | return mAccount; |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | @Override |
| 509 | public ConversationListContext getCurrentListContext() { |
Vikram Aggarwal | e9a8103 | 2012-02-22 13:15:35 -0800 | [diff] [blame] | 510 | return mConvListContext; |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | @Override |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 514 | public String getHelpContext() { |
Paul Westbrook | 30745b6 | 2012-08-19 14:10:32 -0700 | [diff] [blame] | 515 | final int mode = mViewMode.getMode(); |
| 516 | final int helpContextResId; |
| 517 | switch (mode) { |
| 518 | case ViewMode.WAITING_FOR_ACCOUNT_INITIALIZATION: |
| 519 | helpContextResId = R.string.wait_help_context; |
| 520 | break; |
| 521 | default: |
| 522 | helpContextResId = R.string.main_help_context; |
| 523 | } |
| 524 | return mContext.getString(helpContextResId); |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 525 | } |
| 526 | |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 527 | @Override |
Vikram Aggarwal | ef7c992 | 2012-04-23 12:35:04 -0700 | [diff] [blame] | 528 | public final ConversationCursor getConversationListCursor() { |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 529 | return mConversationListCursor; |
| 530 | } |
| 531 | |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 532 | /** |
Vikram Aggarwal | 34a6501 | 2012-04-17 12:39:06 -0700 | [diff] [blame] | 533 | * Check if the fragment is attached to an activity and has a root view. |
Vikram Aggarwal | 715f83d | 2013-03-05 17:04:56 -0800 | [diff] [blame] | 534 | * @param in fragment to be checked |
Vikram Aggarwal | 34a6501 | 2012-04-17 12:39:06 -0700 | [diff] [blame] | 535 | * @return true if the fragment is valid, false otherwise |
| 536 | */ |
Vikram Aggarwal | 715f83d | 2013-03-05 17:04:56 -0800 | [diff] [blame] | 537 | private static boolean isValidFragment(Fragment in) { |
| 538 | return !(in == null || in.getActivity() == null || in.getView() == null); |
Vikram Aggarwal | 34a6501 | 2012-04-17 12:39:06 -0700 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | /** |
Vikram Aggarwal | 49e0e99 | 2012-09-21 13:53:15 -0700 | [diff] [blame] | 542 | * Get the conversation list fragment for this activity. If the conversation list fragment is |
| 543 | * not attached, this method returns null. |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 544 | * |
Vikram Aggarwal | 49e0e99 | 2012-09-21 13:53:15 -0700 | [diff] [blame] | 545 | * Caution! This method returns the {@link ConversationListFragment} after the fragment has been |
| 546 | * added, <b>and</b> after the {@link FragmentManager} has run through its queue to add the |
| 547 | * fragment. There is a non-trivial amount of time after the fragment is instantiated and before |
| 548 | * this call returns a non-null value, depending on the {@link FragmentManager}. If you |
| 549 | * need the fragment immediately after adding it, consider making the fragment an observer of |
| 550 | * the controller and perform the task immediately on {@link Fragment#onActivityCreated(Bundle)} |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 551 | */ |
| 552 | protected ConversationListFragment getConversationListFragment() { |
| 553 | final Fragment fragment = mFragmentManager.findFragmentByTag(TAG_CONVERSATION_LIST); |
Vikram Aggarwal | 34a6501 | 2012-04-17 12:39:06 -0700 | [diff] [blame] | 554 | if (isValidFragment(fragment)) { |
| 555 | return (ConversationListFragment) fragment; |
Andy Huang | 9585d78 | 2012-04-16 19:45:04 -0700 | [diff] [blame] | 556 | } |
Vikram Aggarwal | 34a6501 | 2012-04-17 12:39:06 -0700 | [diff] [blame] | 557 | return null; |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | /** |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 561 | * Returns the folder list fragment attached with this activity. If no such fragment is attached |
| 562 | * this method returns null. |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 563 | * |
Vikram Aggarwal | 49e0e99 | 2012-09-21 13:53:15 -0700 | [diff] [blame] | 564 | * Caution! This method returns the {@link FolderListFragment} after the fragment has been |
| 565 | * added, <b>and</b> after the {@link FragmentManager} has run through its queue to add the |
| 566 | * fragment. There is a non-trivial amount of time after the fragment is instantiated and before |
| 567 | * this call returns a non-null value, depending on the {@link FragmentManager}. If you |
| 568 | * need the fragment immediately after adding it, consider making the fragment an observer of |
| 569 | * the controller and perform the task immediately on {@link Fragment#onActivityCreated(Bundle)} |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 570 | */ |
| 571 | protected FolderListFragment getFolderListFragment() { |
Tony Mantler | b29890a | 2013-07-30 15:12:05 -0700 | [diff] [blame] | 572 | final Fragment fragment = mFragmentManager.findFragmentById(R.id.drawer_pullout); |
Vikram Aggarwal | 34a6501 | 2012-04-17 12:39:06 -0700 | [diff] [blame] | 573 | if (isValidFragment(fragment)) { |
| 574 | return (FolderListFragment) fragment; |
Andy Huang | 9585d78 | 2012-04-16 19:45:04 -0700 | [diff] [blame] | 575 | } |
Vikram Aggarwal | 34a6501 | 2012-04-17 12:39:06 -0700 | [diff] [blame] | 576 | return null; |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 577 | } |
| 578 | |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 579 | /** |
Mindy Pereira | 161f50d | 2012-02-28 15:47:19 -0800 | [diff] [blame] | 580 | * Initialize the action bar. This is not visible to OnePaneController and |
| 581 | * TwoPaneController so they cannot override this behavior. |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 582 | */ |
Vikram Aggarwal | abd24d8 | 2012-04-26 13:23:14 -0700 | [diff] [blame] | 583 | private void initializeActionBar() { |
| 584 | final ActionBar actionBar = mActivity.getActionBar(); |
Andy Huang | 5895f7b | 2012-06-01 17:07:20 -0700 | [diff] [blame] | 585 | if (actionBar == null) { |
| 586 | return; |
Vikram Aggarwal | abd24d8 | 2012-04-26 13:23:14 -0700 | [diff] [blame] | 587 | } |
Andy Huang | 5895f7b | 2012-06-01 17:07:20 -0700 | [diff] [blame] | 588 | |
| 589 | // be sure to inherit from the ActionBar theme when inflating |
| 590 | final LayoutInflater inflater = LayoutInflater.from(actionBar.getThemedContext()); |
Mindy Pereira | 82faec7 | 2012-06-14 17:21:50 -0700 | [diff] [blame] | 591 | final boolean isSearch = mActivity.getIntent() != null |
| 592 | && Intent.ACTION_SEARCH.equals(mActivity.getIntent().getAction()); |
| 593 | mActionBarView = (MailActionBarView) inflater.inflate( |
| 594 | isSearch ? R.layout.search_actionbar_view : R.layout.actionbar_view, null); |
Vikram Aggarwal | dac89fa | 2013-03-05 16:43:09 -0800 | [diff] [blame] | 595 | mActionBarView.initialize(mActivity, this, actionBar); |
Rohan Shah | 1dd054f | 2013-04-01 11:23:44 -0700 | [diff] [blame] | 596 | |
Andy Huang | 12b3ee4 | 2013-04-24 22:49:43 -0700 | [diff] [blame] | 597 | // init the action bar to allow the 'up' affordance. |
| 598 | // any configurations that disallow 'up' should do that later. |
| 599 | mActionBarView.setBackButton(); |
Vikram Aggarwal | abd24d8 | 2012-04-26 13:23:14 -0700 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | /** |
| 603 | * Attach the action bar to the activity. |
| 604 | */ |
| 605 | private void attachActionBar() { |
| 606 | final ActionBar actionBar = mActivity.getActionBar(); |
| 607 | if (actionBar != null && mActionBarView != null) { |
Vikram Aggarwal | ec5cbf7 | 2012-03-08 15:10:35 -0800 | [diff] [blame] | 608 | actionBar.setCustomView(mActionBarView, new ActionBar.LayoutParams( |
Vikram Aggarwal | 26b0bfd | 2013-03-29 13:05:08 -0700 | [diff] [blame] | 609 | LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT)); |
Vikram Aggarwal | bc462ca | 2013-03-15 10:41:03 -0700 | [diff] [blame] | 610 | // Show a custom view and home icon, keep the title and subttitle |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame] | 611 | final int mask = ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_TITLE |
| 612 | | ActionBar.DISPLAY_SHOW_HOME; |
Vikram Aggarwal | bc462ca | 2013-03-15 10:41:03 -0700 | [diff] [blame] | 613 | actionBar.setDisplayOptions(mask, mask); |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 614 | } |
Vikram Aggarwal | abd24d8 | 2012-04-26 13:23:14 -0700 | [diff] [blame] | 615 | mViewMode.addListener(mActionBarView); |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | /** |
Mindy Pereira | 161f50d | 2012-02-28 15:47:19 -0800 | [diff] [blame] | 619 | * Returns whether the conversation list fragment is visible or not. |
| 620 | * Different layouts will have their own notion on the visibility of |
| 621 | * fragments, so this method needs to be overriden. |
| 622 | * |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 623 | */ |
| 624 | protected abstract boolean isConversationListVisible(); |
| 625 | |
Vikram Aggarwal | eb7d329 | 2012-04-20 17:07:20 -0700 | [diff] [blame] | 626 | /** |
Vikram Aggarwal | 34f7b23 | 2012-10-17 13:32:23 -0700 | [diff] [blame] | 627 | * If required, starts wait mode for the current account. |
Vikram Aggarwal | 34f7b23 | 2012-10-17 13:32:23 -0700 | [diff] [blame] | 628 | */ |
| 629 | final void perhapsEnterWaitMode() { |
| 630 | // If the account is not initialized, then show the wait fragment, since nothing can be |
| 631 | // shown. |
| 632 | if (mAccount.isAccountInitializationRequired()) { |
| 633 | showWaitForInitialization(); |
| 634 | return; |
| 635 | } |
| 636 | |
| 637 | final boolean inWaitingMode = inWaitMode(); |
| 638 | final boolean isSyncRequired = mAccount.isAccountSyncRequired(); |
| 639 | if (isSyncRequired) { |
| 640 | if (inWaitingMode) { |
| 641 | // Update the WaitFragment's account object |
| 642 | updateWaitMode(); |
| 643 | } else { |
| 644 | // Transition to waiting mode |
| 645 | showWaitForInitialization(); |
| 646 | } |
| 647 | } else if (inWaitingMode) { |
| 648 | // Dismiss waiting mode |
| 649 | hideWaitForInitialization(); |
| 650 | } |
| 651 | } |
| 652 | |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 653 | @Override |
Andrew Sapperstein | 4cbb0da | 2013-04-19 11:28:02 -0700 | [diff] [blame] | 654 | public void switchToDefaultInboxOrChangeAccount(Account account) { |
| 655 | LogUtils.d(LOG_TAG, "AAC.switchToDefaultAccount(%s)", account); |
Vikram Aggarwal | 66bc6ed | 2012-07-31 09:59:55 -0700 | [diff] [blame] | 656 | final boolean firstLoad = mAccount == null; |
Andrew Sapperstein | 252684a | 2013-04-17 14:43:26 -0700 | [diff] [blame] | 657 | final boolean switchToDefaultInbox = !firstLoad && account.uri.equals(mAccount.uri); |
Vikram Aggarwal | d70fe49 | 2013-06-04 12:52:07 -0700 | [diff] [blame] | 658 | // If the active account has been clicked in the drawer, go to default inbox |
Andrew Sapperstein | 252684a | 2013-04-17 14:43:26 -0700 | [diff] [blame] | 659 | if (switchToDefaultInbox) { |
| 660 | loadAccountInbox(); |
| 661 | return; |
| 662 | } |
Andrew Sapperstein | 4cbb0da | 2013-04-19 11:28:02 -0700 | [diff] [blame] | 663 | changeAccount(account); |
| 664 | } |
| 665 | |
| 666 | @Override |
| 667 | public void changeAccount(Account account) { |
| 668 | LogUtils.d(LOG_TAG, "AAC.changeAccount(%s)", account); |
| 669 | // Is the account or account settings different from the existing account? |
| 670 | final boolean firstLoad = mAccount == null; |
| 671 | final boolean accountChanged = firstLoad || !account.uri.equals(mAccount.uri); |
| 672 | |
Vikram Aggarwal | 472e536 | 2012-11-05 14:17:23 -0800 | [diff] [blame] | 673 | // If nothing has changed, return early without wasting any more time. |
| 674 | if (!accountChanged && !account.settingsDiffer(mAccount)) { |
| 675 | return; |
| 676 | } |
| 677 | // We also don't want to do anything if the new account is null |
| 678 | if (account == null) { |
Vikram Aggarwal | 5fd8afd | 2013-03-13 15:28:47 -0700 | [diff] [blame] | 679 | LogUtils.e(LOG_TAG, "AAC.changeAccount(null) called."); |
Vikram Aggarwal | 472e536 | 2012-11-05 14:17:23 -0800 | [diff] [blame] | 680 | return; |
| 681 | } |
| 682 | final String accountName = account.name; |
| 683 | mHandler.post(new Runnable() { |
| 684 | @Override |
| 685 | public void run() { |
Vikram Aggarwal | f6c00b8 | 2013-01-03 10:02:50 -0800 | [diff] [blame] | 686 | MailActivity.setNfcMessage(accountName); |
Mindy Pereira | fa20c1a | 2012-07-23 13:00:02 -0700 | [diff] [blame] | 687 | } |
Vikram Aggarwal | 472e536 | 2012-11-05 14:17:23 -0800 | [diff] [blame] | 688 | }); |
| 689 | if (accountChanged) { |
| 690 | commitDestructiveActions(false); |
| 691 | } |
Andy Huang | 761522c | 2013-08-08 13:09:11 -0700 | [diff] [blame] | 692 | Analytics.getInstance().setCustomDimension(Analytics.CD_INDEX_ACCOUNT_TYPE, |
| 693 | AnalyticsUtils.getAccountTypeForAccount(accountName)); |
Vikram Aggarwal | 472e536 | 2012-11-05 14:17:23 -0800 | [diff] [blame] | 694 | // Change the account here |
| 695 | setAccount(account); |
| 696 | // And carry out associated actions. |
| 697 | cancelRefreshTask(); |
| 698 | if (accountChanged) { |
| 699 | loadAccountInbox(); |
| 700 | } |
| 701 | // Check if we need to force setting up an account before proceeding. |
| 702 | if (mAccount != null && !Uri.EMPTY.equals(mAccount.settings.setupIntentUri)) { |
| 703 | // Launch the intent! |
| 704 | final Intent intent = new Intent(Intent.ACTION_EDIT); |
| 705 | intent.setData(mAccount.settings.setupIntentUri); |
| 706 | mActivity.startActivity(intent); |
Mindy Pereira | 28d5f72 | 2012-02-15 12:32:40 -0800 | [diff] [blame] | 707 | } |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 708 | } |
| 709 | |
Vikram Aggarwal | e6340bc | 2012-03-26 15:57:09 -0700 | [diff] [blame] | 710 | /** |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 711 | * Adds a listener interested in change in the current account. If a class is storing a |
| 712 | * reference to the current account, it should listen on changes, so it can receive updates to |
| 713 | * settings. Must happen in the UI thread. |
| 714 | */ |
Mindy Pereira | efe3d25 | 2012-03-01 14:20:44 -0800 | [diff] [blame] | 715 | @Override |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 716 | public void registerAccountObserver(DataSetObserver obs) { |
| 717 | mAccountObservers.registerObserver(obs); |
Mindy Pereira | efe3d25 | 2012-03-01 14:20:44 -0800 | [diff] [blame] | 718 | } |
| 719 | |
Vikram Aggarwal | 7d81600 | 2012-04-17 17:06:41 -0700 | [diff] [blame] | 720 | /** |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 721 | * Removes a listener from receiving current account changes. |
Vikram Aggarwal | 7d81600 | 2012-04-17 17:06:41 -0700 | [diff] [blame] | 722 | * Must happen in the UI thread. |
| 723 | */ |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 724 | @Override |
| 725 | public void unregisterAccountObserver(DataSetObserver obs) { |
| 726 | mAccountObservers.unregisterObserver(obs); |
Vikram Aggarwal | 7d81600 | 2012-04-17 17:06:41 -0700 | [diff] [blame] | 727 | } |
| 728 | |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 729 | @Override |
Vikram Aggarwal | 07dbaa6 | 2013-03-12 15:21:04 -0700 | [diff] [blame] | 730 | public void registerAllAccountObserver(DataSetObserver observer) { |
| 731 | mAllAccountObservers.registerObserver(observer); |
| 732 | } |
| 733 | |
| 734 | @Override |
| 735 | public void unregisterAllAccountObserver(DataSetObserver observer) { |
| 736 | mAllAccountObservers.unregisterObserver(observer); |
| 737 | } |
| 738 | |
| 739 | @Override |
| 740 | public Account[] getAllAccounts() { |
| 741 | return mAllAccounts; |
| 742 | } |
| 743 | |
| 744 | @Override |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 745 | public Account getAccount() { |
| 746 | return mAccount; |
Vikram Aggarwal | 7d81600 | 2012-04-17 17:06:41 -0700 | [diff] [blame] | 747 | } |
| 748 | |
Rohan Shah | 0f73d90 | 2013-04-19 17:06:37 -0700 | [diff] [blame] | 749 | @Override |
| 750 | public void registerDrawerClosedObserver(final DataSetObserver observer) { |
| 751 | mDrawerObservers.registerObserver(observer); |
| 752 | } |
| 753 | |
| 754 | @Override |
| 755 | public void unregisterDrawerClosedObserver(final DataSetObserver observer) { |
| 756 | mDrawerObservers.unregisterObserver(observer); |
| 757 | } |
| 758 | |
| 759 | /** |
Andy Huang | 12b3ee4 | 2013-04-24 22:49:43 -0700 | [diff] [blame] | 760 | * If the drawer is open, the function locks the drawer to the closed, thereby sliding in |
| 761 | * the drawer to the left edge, disabling events, and refreshing it once it's either closed |
| 762 | * or put in an idle state. |
Rohan Shah | 0f73d90 | 2013-04-19 17:06:37 -0700 | [diff] [blame] | 763 | */ |
| 764 | @Override |
Vikram Aggarwal | a1b59dc | 2013-04-30 15:45:49 -0700 | [diff] [blame] | 765 | public void closeDrawer(final boolean hasNewFolderOrAccount, Account nextAccount, |
| 766 | Folder nextFolder) { |
Andy Huang | 12b3ee4 | 2013-04-24 22:49:43 -0700 | [diff] [blame] | 767 | if (!isDrawerEnabled()) { |
| 768 | mDrawerObservers.notifyChanged(); |
| 769 | return; |
| 770 | } |
Andy Huang | 12b3ee4 | 2013-04-24 22:49:43 -0700 | [diff] [blame] | 771 | // If there are no new folders or accounts to switch to, just close the drawer |
| 772 | if (!hasNewFolderOrAccount) { |
| 773 | mDrawerContainer.closeDrawers(); |
| 774 | return; |
| 775 | } |
Vikram Aggarwal | 2f9d394 | 2013-05-03 12:31:39 -0700 | [diff] [blame] | 776 | // Otherwise, start preloading the conversation list for the new folder. |
| 777 | if (nextFolder != null) { |
| 778 | preloadConvList(nextAccount, nextFolder); |
| 779 | } |
| 780 | // Remember if the conversation list view is animating |
Andy Huang | 12b3ee4 | 2013-04-24 22:49:43 -0700 | [diff] [blame] | 781 | final ConversationListFragment conversationList = getConversationListFragment(); |
| 782 | if (conversationList != null) { |
| 783 | mListViewForAnimating = conversationList.getListView(); |
| 784 | } else { |
| 785 | // There is no conversation list to animate, so just set it to null |
| 786 | mListViewForAnimating = null; |
| 787 | } |
| 788 | |
| 789 | if (mDrawerContainer.isDrawerOpen(mDrawerPullout)) { |
| 790 | // Lets the drawer listener update the drawer contents and notify the FolderListFragment |
| 791 | mHasNewAccountOrFolder = true; |
| 792 | mDrawerContainer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED); |
| 793 | } else { |
| 794 | // Drawer is already closed, notify observers that is the case. |
| 795 | mDrawerObservers.notifyChanged(); |
| 796 | } |
Rohan Shah | 0f73d90 | 2013-04-19 17:06:37 -0700 | [diff] [blame] | 797 | } |
| 798 | |
Vikram Aggarwal | a1b59dc | 2013-04-30 15:45:49 -0700 | [diff] [blame] | 799 | /** |
Vikram Aggarwal | aa941d7 | 2013-06-04 15:34:28 -0700 | [diff] [blame] | 800 | * Load the conversation list early for the given folder. This happens when some UI element |
| 801 | * (usually the drawer) instructs the controller that an account change or folder change is |
| 802 | * imminent. While the UI element is animating, the controller can preload the conversation |
| 803 | * list for the default inbox of the account provided here or to the folder provided here. |
| 804 | * |
| 805 | * @param nextAccount The account which the app will switch to shortly, possibly null. |
| 806 | * @param nextFolder The folder which the app will switch to shortly, possibly null. |
Vikram Aggarwal | a1b59dc | 2013-04-30 15:45:49 -0700 | [diff] [blame] | 807 | */ |
| 808 | protected void preloadConvList(Account nextAccount, Folder nextFolder) { |
| 809 | // Fire off the conversation list loader for this account already with a fake |
| 810 | // listener. |
Paul Westbrook | e2be97a | 2013-05-21 02:12:09 -0700 | [diff] [blame] | 811 | final Bundle args = new Bundle(2); |
Vikram Aggarwal | a1b59dc | 2013-04-30 15:45:49 -0700 | [diff] [blame] | 812 | if (nextAccount != null) { |
| 813 | args.putParcelable(BUNDLE_ACCOUNT_KEY, nextAccount); |
| 814 | } else { |
| 815 | args.putParcelable(BUNDLE_ACCOUNT_KEY, mAccount); |
| 816 | } |
| 817 | if (nextFolder != null) { |
| 818 | args.putParcelable(BUNDLE_FOLDER_KEY, nextFolder); |
Vikram Aggarwal | 2dc8504 | 2013-05-01 13:57:09 -0700 | [diff] [blame] | 819 | } else { |
| 820 | LogUtils.e(LOG_TAG, new Error(), "AAC.preloadConvList(): Got an empty folder"); |
Vikram Aggarwal | a1b59dc | 2013-04-30 15:45:49 -0700 | [diff] [blame] | 821 | } |
| 822 | mFolder = null; |
| 823 | final LoaderManager lm = mActivity.getLoaderManager(); |
| 824 | lm.destroyLoader(LOADER_CONVERSATION_LIST); |
| 825 | lm.initLoader(LOADER_CONVERSATION_LIST, args, mListCursorCallbacks); |
| 826 | } |
| 827 | |
Vikram Aggarwal | aa941d7 | 2013-06-04 15:34:28 -0700 | [diff] [blame] | 828 | /** |
| 829 | * Initiates the async request to create a fake search folder, which returns conversations that |
| 830 | * match the query term provided by the user. Returns immediately. |
| 831 | * @param intent Intent that the app was started with. This intent contains the search query. |
| 832 | */ |
Mindy Pereira | e082839 | 2012-03-08 10:38:40 -0800 | [diff] [blame] | 833 | private void fetchSearchFolder(Intent intent) { |
Paul Westbrook | e2be97a | 2013-05-21 02:12:09 -0700 | [diff] [blame] | 834 | final Bundle args = new Bundle(1); |
Mindy Pereira | ab48636 | 2012-03-21 18:18:53 -0700 | [diff] [blame] | 835 | args.putString(ConversationListContext.EXTRA_SEARCH_QUERY, intent |
Mindy Pereira | e082839 | 2012-03-08 10:38:40 -0800 | [diff] [blame] | 836 | .getStringExtra(ConversationListContext.EXTRA_SEARCH_QUERY)); |
Vikram Aggarwal | 177097f | 2013-03-08 11:19:53 -0800 | [diff] [blame] | 837 | mActivity.getLoaderManager().restartLoader(LOADER_SEARCH, args, mFolderCallbacks); |
Mindy Pereira | e082839 | 2012-03-08 10:38:40 -0800 | [diff] [blame] | 838 | } |
| 839 | |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 840 | @Override |
Scott Kennedy | ad41814 | 2013-07-10 17:18:22 -0700 | [diff] [blame] | 841 | public void onFolderChanged(Folder folder, final boolean force) { |
Vikram Aggarwal | dfbc698 | 2013-07-03 14:39:31 -0700 | [diff] [blame] | 842 | /** If the folder doesn't exist, or its parent URI is empty, |
| 843 | * this is not a child folder */ |
| 844 | final boolean isTopLevel = (folder == null) || (folder.parent == Uri.EMPTY); |
| 845 | final int mode = mViewMode.getMode(); |
| 846 | mDrawerToggle.setDrawerIndicatorEnabled( |
| 847 | getShouldShowDrawerIndicator(mode, isTopLevel)); |
Scott Kennedy | 2b254dc | 2013-07-10 11:40:27 -0700 | [diff] [blame] | 848 | mDrawerContainer.setDrawerLockMode(getShouldAllowDrawerPull(mode) |
Vikram Aggarwal | dfbc698 | 2013-07-03 14:39:31 -0700 | [diff] [blame] | 849 | ? DrawerLayout.LOCK_MODE_UNLOCKED : DrawerLayout.LOCK_MODE_LOCKED_CLOSED); |
| 850 | |
Andy Huang | 12b3ee4 | 2013-04-24 22:49:43 -0700 | [diff] [blame] | 851 | mDrawerContainer.closeDrawers(); |
Scott Kennedy | d2a0c39 | 2013-08-29 13:21:05 -0700 | [diff] [blame] | 852 | |
| 853 | if (mFolder == null || !mFolder.equals(folder)) { |
| 854 | // We are actually changing the folder, so exit cab mode |
| 855 | exitCabMode(); |
| 856 | } |
| 857 | |
Scott Kennedy | ad41814 | 2013-07-10 17:18:22 -0700 | [diff] [blame] | 858 | changeFolder(folder, null, force); |
Vikram Aggarwal | f334140 | 2012-08-07 10:09:38 -0700 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | /** |
Vikram Aggarwal | 203dc00 | 2012-08-23 13:59:04 -0700 | [diff] [blame] | 862 | * Sets the folder state without changing view mode and without creating a list fragment, if |
| 863 | * possible. |
Vikram Aggarwal | 715f83d | 2013-03-05 17:04:56 -0800 | [diff] [blame] | 864 | * @param folder the folder whose list of conversations are to be shown |
| 865 | * @param query the query string for a list of conversations matching a search |
Vikram Aggarwal | 203dc00 | 2012-08-23 13:59:04 -0700 | [diff] [blame] | 866 | */ |
| 867 | private void setListContext(Folder folder, String query) { |
| 868 | updateFolder(folder); |
| 869 | if (query != null) { |
| 870 | mConvListContext = ConversationListContext.forSearchQuery(mAccount, mFolder, query); |
| 871 | } else { |
| 872 | mConvListContext = ConversationListContext.forFolder(mAccount, mFolder); |
| 873 | } |
Vikram Aggarwal | 203dc00 | 2012-08-23 13:59:04 -0700 | [diff] [blame] | 874 | cancelRefreshTask(); |
| 875 | } |
| 876 | |
| 877 | /** |
Vikram Aggarwal | f334140 | 2012-08-07 10:09:38 -0700 | [diff] [blame] | 878 | * Changes the folder to the value provided here. This causes the view mode to change. |
| 879 | * @param folder the folder to change to |
| 880 | * @param query if non-null, this represents the search string that the folder represents. |
Scott Kennedy | ad41814 | 2013-07-10 17:18:22 -0700 | [diff] [blame] | 881 | * @param force <code>true</code> to force a folder change, <code>false</code> to disallow |
| 882 | * changing to the current folder |
Vikram Aggarwal | f334140 | 2012-08-07 10:09:38 -0700 | [diff] [blame] | 883 | */ |
Scott Kennedy | ad41814 | 2013-07-10 17:18:22 -0700 | [diff] [blame] | 884 | private void changeFolder(Folder folder, String query, final boolean force) { |
Vikram Aggarwal | 203dc00 | 2012-08-23 13:59:04 -0700 | [diff] [blame] | 885 | if (!Objects.equal(mFolder, folder)) { |
| 886 | commitDestructiveActions(false); |
| 887 | } |
Scott Kennedy | ad41814 | 2013-07-10 17:18:22 -0700 | [diff] [blame] | 888 | if (folder != null && (!folder.equals(mFolder) || force) |
Mindy Pereira | a1f9998 | 2012-07-16 16:32:15 -0700 | [diff] [blame] | 889 | || (mViewMode.getMode() != ViewMode.CONVERSATION_LIST)) { |
Vikram Aggarwal | 203dc00 | 2012-08-23 13:59:04 -0700 | [diff] [blame] | 890 | setListContext(folder, query); |
Mindy Pereira | 28e0c34 | 2012-02-17 15:05:13 -0800 | [diff] [blame] | 891 | showConversationList(mConvListContext); |
Vikram Aggarwal | 58ccd69 | 2013-03-28 11:29:22 -0700 | [diff] [blame] | 892 | // Touch the current folder: it is different, and it has been accessed. |
| 893 | mRecentFolderList.touchFolder(mFolder, mAccount); |
Mindy Pereira | 28e0c34 | 2012-02-17 15:05:13 -0800 | [diff] [blame] | 894 | } |
Vikram Aggarwal | 93dc202 | 2012-11-05 13:36:57 -0800 | [diff] [blame] | 895 | resetActionBarIcon(); |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 896 | } |
| 897 | |
Mindy Pereira | 13c12a6 | 2012-05-31 15:41:08 -0700 | [diff] [blame] | 898 | @Override |
Mindy Pereira | 505df5f | 2012-06-19 17:57:17 -0700 | [diff] [blame] | 899 | public void onFolderSelected(Folder folder) { |
Scott Kennedy | ad41814 | 2013-07-10 17:18:22 -0700 | [diff] [blame] | 900 | onFolderChanged(folder, false /* force */); |
Mindy Pereira | 13c12a6 | 2012-05-31 15:41:08 -0700 | [diff] [blame] | 901 | } |
| 902 | |
Vikram Aggarwal | 792ccba | 2012-03-27 13:46:57 -0700 | [diff] [blame] | 903 | /** |
Vikram Aggarwal | 58cad2e | 2012-08-28 16:18:23 -0700 | [diff] [blame] | 904 | * Adds a listener interested in change in the recent folders. If a class is storing a |
| 905 | * reference to the recent folders, it should listen on changes, so it can receive updates. |
| 906 | * Must happen in the UI thread. |
| 907 | */ |
| 908 | @Override |
| 909 | public void registerRecentFolderObserver(DataSetObserver obs) { |
| 910 | mRecentFolderObservers.registerObserver(obs); |
| 911 | } |
| 912 | |
| 913 | /** |
| 914 | * Removes a listener from receiving recent folder changes. |
| 915 | * Must happen in the UI thread. |
| 916 | */ |
| 917 | @Override |
| 918 | public void unregisterRecentFolderObserver(DataSetObserver obs) { |
| 919 | mRecentFolderObservers.unregisterObserver(obs); |
| 920 | } |
| 921 | |
| 922 | @Override |
| 923 | public RecentFolderList getRecentFolders() { |
| 924 | return mRecentFolderList; |
| 925 | } |
| 926 | |
Vikram Aggarwal | eb7d329 | 2012-04-20 17:07:20 -0700 | [diff] [blame] | 927 | @Override |
| 928 | public void loadAccountInbox() { |
Vikram Aggarwal | 77ee0ce | 2013-04-28 15:32:36 -0700 | [diff] [blame] | 929 | boolean handled = false; |
| 930 | if (mFolderWatcher != null) { |
| 931 | final Folder inbox = mFolderWatcher.getDefaultInbox(mAccount); |
| 932 | if (inbox != null) { |
Scott Kennedy | ad41814 | 2013-07-10 17:18:22 -0700 | [diff] [blame] | 933 | onFolderChanged(inbox, false /* force */); |
Vikram Aggarwal | 77ee0ce | 2013-04-28 15:32:36 -0700 | [diff] [blame] | 934 | handled = true; |
| 935 | } |
| 936 | } |
| 937 | if (!handled) { |
| 938 | LogUtils.w(LOG_TAG, "Starting a LOADER_ACCOUNT_INBOX for %s", mAccount); |
| 939 | restartOptionalLoader(LOADER_ACCOUNT_INBOX, mFolderCallbacks, Bundle.EMPTY); |
| 940 | } |
Vikram Aggarwal | 8cbf281 | 2013-04-11 17:23:45 -0700 | [diff] [blame] | 941 | final int mode = mViewMode.getMode(); |
| 942 | if (mode == ViewMode.UNKNOWN || mode == ViewMode.WAITING_FOR_ACCOUNT_INITIALIZATION) { |
Andy Huang | e645942 | 2013-04-01 16:32:18 -0700 | [diff] [blame] | 943 | mViewMode.enterConversationListMode(); |
| 944 | } |
Mindy Pereira | f6acdad | 2012-03-15 11:21:13 -0700 | [diff] [blame] | 945 | } |
| 946 | |
Vikram Aggarwal | 77ee0ce | 2013-04-28 15:32:36 -0700 | [diff] [blame] | 947 | @Override |
| 948 | public void setFolderWatcher(FolderWatcher watcher) { |
| 949 | mFolderWatcher = watcher; |
| 950 | } |
| 951 | |
Vikram Aggarwal | 69b5c30 | 2012-09-05 11:11:13 -0700 | [diff] [blame] | 952 | /** |
Vikram Aggarwal | d00229d | 2012-09-20 12:31:44 -0700 | [diff] [blame] | 953 | * Marks the {@link #mFolderChanged} value if the newFolder is different from the existing |
| 954 | * {@link #mFolder}. This should be called immediately <b>before</b> assigning newFolder to |
| 955 | * mFolder. |
Vikram Aggarwal | 715f83d | 2013-03-05 17:04:56 -0800 | [diff] [blame] | 956 | * @param newFolder the new folder we are switching to. |
Vikram Aggarwal | d00229d | 2012-09-20 12:31:44 -0700 | [diff] [blame] | 957 | */ |
Vikram Aggarwal | 715f83d | 2013-03-05 17:04:56 -0800 | [diff] [blame] | 958 | private void setHasFolderChanged(final Folder newFolder) { |
Vikram Aggarwal | d00229d | 2012-09-20 12:31:44 -0700 | [diff] [blame] | 959 | // We should never try to assign a null folder. But in the rare event that we do, we should |
| 960 | // only set the bit when we have a valid folder, and null is not valid. |
| 961 | if (newFolder == null) { |
| 962 | return; |
| 963 | } |
| 964 | // If the previous folder was null, or if the two folders represent different data, then we |
| 965 | // consider that the folder has changed. |
Scott Kennedy | 259df5b | 2013-07-11 13:24:01 -0700 | [diff] [blame] | 966 | if (mFolder == null || !newFolder.equals(mFolder)) { |
Vikram Aggarwal | d00229d | 2012-09-20 12:31:44 -0700 | [diff] [blame] | 967 | mFolderChanged = true; |
| 968 | } |
| 969 | } |
| 970 | |
| 971 | /** |
Vikram Aggarwal | 69b5c30 | 2012-09-05 11:11:13 -0700 | [diff] [blame] | 972 | * Sets the current folder if it is different from the object provided here. This method does |
| 973 | * NOT notify the folder observers that a change has happened. Observers are notified when we |
| 974 | * get an updated folder from the loaders, which will happen as a consequence of this method |
| 975 | * (since this method starts/restarts the loaders). |
| 976 | * @param folder The folder to assign |
| 977 | */ |
Mindy Pereira | 11e3596 | 2012-06-01 14:49:46 -0700 | [diff] [blame] | 978 | private void updateFolder(Folder folder) { |
Vikram Aggarwal | 1672ff8 | 2012-09-21 10:15:22 -0700 | [diff] [blame] | 979 | if (folder == null || !folder.isInitialized()) { |
| 980 | LogUtils.e(LOG_TAG, new Error(), "AAC.setFolder(%s): Bad input", folder); |
| 981 | return; |
| 982 | } |
| 983 | if (folder.equals(mFolder)) { |
| 984 | LogUtils.d(LOG_TAG, "AAC.setFolder(%s): Input matches mFolder", folder); |
| 985 | return; |
| 986 | } |
| 987 | final boolean wasNull = mFolder == null; |
| 988 | LogUtils.d(LOG_TAG, "AbstractActivityController.setFolder(%s)", folder.name); |
| 989 | final LoaderManager lm = mActivity.getLoaderManager(); |
| 990 | // updateFolder is called from AAC.onLoadFinished() on folder changes. We need to |
| 991 | // ensure that the folder is different from the previous folder before marking the |
| 992 | // folder changed. |
| 993 | setHasFolderChanged(folder); |
| 994 | mFolder = folder; |
Vikram Aggarwal | 69b5c30 | 2012-09-05 11:11:13 -0700 | [diff] [blame] | 995 | |
Vikram Aggarwal | 1672ff8 | 2012-09-21 10:15:22 -0700 | [diff] [blame] | 996 | // We do not need to notify folder observers yet. Instead we start the loaders and |
| 997 | // when the load finishes, we will get an updated folder. Then, we notify the |
| 998 | // folderObservers in onLoadFinished. |
| 999 | mActionBarView.setFolder(mFolder); |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 1000 | |
Vikram Aggarwal | 1672ff8 | 2012-09-21 10:15:22 -0700 | [diff] [blame] | 1001 | // Only when we switch from one folder to another do we want to restart the |
| 1002 | // folder and conversation list loaders (to trigger onCreateLoader). |
| 1003 | // The first time this runs when the activity is [re-]initialized, we want to re-use the |
| 1004 | // previous loader's instance and data upon configuration change (e.g. rotation). |
| 1005 | // If there was not already an instance of the loader, init it. |
| 1006 | if (lm.getLoader(LOADER_FOLDER_CURSOR) == null) { |
Vikram Aggarwal | 177097f | 2013-03-08 11:19:53 -0800 | [diff] [blame] | 1007 | lm.initLoader(LOADER_FOLDER_CURSOR, Bundle.EMPTY, mFolderCallbacks); |
Vikram Aggarwal | 1672ff8 | 2012-09-21 10:15:22 -0700 | [diff] [blame] | 1008 | } else { |
Vikram Aggarwal | 177097f | 2013-03-08 11:19:53 -0800 | [diff] [blame] | 1009 | lm.restartLoader(LOADER_FOLDER_CURSOR, Bundle.EMPTY, mFolderCallbacks); |
Vikram Aggarwal | 1672ff8 | 2012-09-21 10:15:22 -0700 | [diff] [blame] | 1010 | } |
Vikram Aggarwal | a1b59dc | 2013-04-30 15:45:49 -0700 | [diff] [blame] | 1011 | if (!wasNull && lm.getLoader(LOADER_CONVERSATION_LIST) != null) { |
| 1012 | // If there was an existing folder AND we have changed |
Vikram Aggarwal | 1672ff8 | 2012-09-21 10:15:22 -0700 | [diff] [blame] | 1013 | // folders, we want to restart the loader to get the information |
| 1014 | // for the newly selected folder |
| 1015 | lm.destroyLoader(LOADER_CONVERSATION_LIST); |
Mindy Pereira | 5cb0c3e | 2012-02-22 15:22:47 -0800 | [diff] [blame] | 1016 | } |
Paul Westbrook | e2be97a | 2013-05-21 02:12:09 -0700 | [diff] [blame] | 1017 | final Bundle args = new Bundle(2); |
Vikram Aggarwal | 2dc8504 | 2013-05-01 13:57:09 -0700 | [diff] [blame] | 1018 | args.putParcelable(BUNDLE_ACCOUNT_KEY, mAccount); |
Vikram Aggarwal | a1b59dc | 2013-04-30 15:45:49 -0700 | [diff] [blame] | 1019 | args.putParcelable(BUNDLE_FOLDER_KEY, mFolder); |
| 1020 | lm.initLoader(LOADER_CONVERSATION_LIST, args, mListCursorCallbacks); |
Mindy Pereira | 5cb0c3e | 2012-02-22 15:22:47 -0800 | [diff] [blame] | 1021 | } |
| 1022 | |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1023 | @Override |
Andy Huang | 090db1e | 2012-07-25 13:25:28 -0700 | [diff] [blame] | 1024 | public Folder getFolder() { |
| 1025 | return mFolder; |
| 1026 | } |
| 1027 | |
| 1028 | @Override |
Mindy Pereira | c975e84 | 2012-07-16 09:15:00 -0700 | [diff] [blame] | 1029 | public Folder getHierarchyFolder() { |
| 1030 | return mFolderListFolder; |
| 1031 | } |
| 1032 | |
| 1033 | @Override |
| 1034 | public void setHierarchyFolder(Folder folder) { |
| 1035 | mFolderListFolder = folder; |
Mindy Pereira | 23aadfd | 2012-05-25 11:24:33 -0700 | [diff] [blame] | 1036 | } |
| 1037 | |
Vikram Aggarwal | 6aca689 | 2013-06-04 13:53:27 -0700 | [diff] [blame] | 1038 | /** |
| 1039 | * The mail activity calls other activities for two specific reasons: |
| 1040 | * <ul> |
| 1041 | * <li>To add an account. And receives the result {@link #ADD_ACCOUNT_REQUEST_CODE}</li> |
| 1042 | * <li>To update the password on a current account. The result {@link |
| 1043 | * #REAUTHENTICATE_REQUEST_CODE} is received.</li> |
| 1044 | * </ul> |
| 1045 | * @param requestCode |
| 1046 | * @param resultCode |
| 1047 | * @param data |
| 1048 | */ |
Mindy Pereira | 23aadfd | 2012-05-25 11:24:33 -0700 | [diff] [blame] | 1049 | @Override |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1050 | public void onActivityResult(int requestCode, int resultCode, Intent data) { |
Paul Westbrook | 122f7c2 | 2012-08-20 17:50:31 -0700 | [diff] [blame] | 1051 | switch (requestCode) { |
| 1052 | case ADD_ACCOUNT_REQUEST_CODE: |
| 1053 | // We were waiting for the user to create an account |
| 1054 | if (resultCode == Activity.RESULT_OK) { |
| 1055 | // restart the loader to get the updated list of accounts |
Vikram Aggarwal | 177097f | 2013-03-08 11:19:53 -0800 | [diff] [blame] | 1056 | mActivity.getLoaderManager().initLoader(LOADER_ACCOUNT_CURSOR, Bundle.EMPTY, |
| 1057 | mAccountCallbacks); |
Paul Westbrook | 122f7c2 | 2012-08-20 17:50:31 -0700 | [diff] [blame] | 1058 | } else { |
| 1059 | // The user failed to create an account, just exit the app |
| 1060 | mActivity.finish(); |
| 1061 | } |
| 1062 | break; |
| 1063 | case REAUTHENTICATE_REQUEST_CODE: |
| 1064 | if (resultCode == Activity.RESULT_OK) { |
| 1065 | // The user successfully authenticated, attempt to refresh the list |
| 1066 | final Uri refreshUri = mFolder != null ? mFolder.refreshUri : null; |
| 1067 | if (refreshUri != null) { |
| 1068 | startAsyncRefreshTask(refreshUri); |
| 1069 | } |
| 1070 | } |
| 1071 | break; |
Paul Westbrook | 2388c5d | 2012-03-25 12:29:11 -0700 | [diff] [blame] | 1072 | } |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1073 | } |
| 1074 | |
Vikram Aggarwal | 69b5c30 | 2012-09-05 11:11:13 -0700 | [diff] [blame] | 1075 | /** |
| 1076 | * Inform the conversation cursor that there has been a visibility change. |
Vikram Aggarwal | 715f83d | 2013-03-05 17:04:56 -0800 | [diff] [blame] | 1077 | * @param visible true if the conversation list is visible, false otherwise. |
Vikram Aggarwal | 69b5c30 | 2012-09-05 11:11:13 -0700 | [diff] [blame] | 1078 | */ |
| 1079 | protected synchronized void informCursorVisiblity(boolean visible) { |
| 1080 | if (mConversationListCursor != null) { |
| 1081 | Utils.setConversationCursorVisibility(mConversationListCursor, visible, mFolderChanged); |
| 1082 | // We have informed the cursor. Subsequent visibility changes should not tell it that |
| 1083 | // the folder has changed. |
| 1084 | mFolderChanged = false; |
| 1085 | } |
| 1086 | } |
| 1087 | |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1088 | @Override |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1089 | public void onConversationListVisibilityChanged(boolean visible) { |
Vikram Aggarwal | 69b5c30 | 2012-09-05 11:11:13 -0700 | [diff] [blame] | 1090 | informCursorVisiblity(visible); |
Andy Huang | c94d07f | 2013-06-03 16:19:35 -0700 | [diff] [blame] | 1091 | commitAutoAdvanceOperation(); |
Scott Kennedy | 32ddb84 | 2013-08-28 17:38:22 -0700 | [diff] [blame] | 1092 | |
| 1093 | // Notify special views |
| 1094 | final ConversationListFragment convListFragment = getConversationListFragment(); |
| 1095 | if (convListFragment != null && convListFragment.getAnimatedAdapter() != null) { |
| 1096 | convListFragment.getAnimatedAdapter().onConversationListVisibilityChanged(visible); |
| 1097 | } |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1098 | } |
| 1099 | |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 1100 | /** |
Vikram Aggarwal | 7dd054e | 2012-05-21 14:43:10 -0700 | [diff] [blame] | 1101 | * Called when a conversation is visible. Child classes must call the super class implementation |
| 1102 | * before performing local computation. |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 1103 | */ |
| 1104 | @Override |
| 1105 | public void onConversationVisibilityChanged(boolean visible) { |
Andy Huang | c94d07f | 2013-06-03 16:19:35 -0700 | [diff] [blame] | 1106 | commitAutoAdvanceOperation(); |
| 1107 | } |
| 1108 | |
| 1109 | /** |
| 1110 | * Commits any pending destructive action that was earlier deferred by an auto-advance |
| 1111 | * mode-change transition. |
| 1112 | */ |
| 1113 | private void commitAutoAdvanceOperation() { |
| 1114 | if (mAutoAdvanceOp != null) { |
| 1115 | mAutoAdvanceOp.run(); |
| 1116 | mAutoAdvanceOp = null; |
| 1117 | } |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 1118 | } |
| 1119 | |
Vikram Aggarwal | 59f741f | 2013-03-01 15:55:40 -0800 | [diff] [blame] | 1120 | /** |
| 1121 | * Initialize development time logging. This can potentially log a lot of PII, and we don't want |
| 1122 | * to turn it on for shipped versions. |
| 1123 | */ |
| 1124 | private void initializeDevLoggingService() { |
| 1125 | if (!MailLogService.DEBUG_ENABLED) { |
| 1126 | return; |
| 1127 | } |
| 1128 | // Check every 5 minutes. |
| 1129 | final int WAIT_TIME = 5 * 60 * 1000; |
| 1130 | // Start a runnable that periodically checks the log level and starts/stops the service. |
| 1131 | mLogServiceChecker = new Runnable() { |
| 1132 | /** True if currently logging. */ |
| 1133 | private boolean mCurrentlyLogging = false; |
| 1134 | |
| 1135 | /** |
| 1136 | * If the logging level has been changed since the previous run, start or stop the |
| 1137 | * service. |
| 1138 | */ |
| 1139 | private void startOrStopService() { |
| 1140 | // If the log level is already high, start the service. |
| 1141 | final Intent i = new Intent(mContext, MailLogService.class); |
| 1142 | final boolean loggingEnabled = MailLogService.isLoggingLevelHighEnough(); |
| 1143 | if (mCurrentlyLogging == loggingEnabled) { |
| 1144 | // No change since previous run, just return; |
| 1145 | return; |
| 1146 | } |
| 1147 | if (loggingEnabled) { |
| 1148 | LogUtils.e(LOG_TAG, "Starting MailLogService"); |
| 1149 | mContext.startService(i); |
| 1150 | } else { |
| 1151 | LogUtils.e(LOG_TAG, "Stopping MailLogService"); |
| 1152 | mContext.stopService(i); |
| 1153 | } |
| 1154 | mCurrentlyLogging = loggingEnabled; |
| 1155 | } |
| 1156 | |
| 1157 | @Override |
| 1158 | public void run() { |
| 1159 | startOrStopService(); |
| 1160 | mHandler.postDelayed(this, WAIT_TIME); |
| 1161 | } |
| 1162 | }; |
| 1163 | // Start the runnable right away. |
| 1164 | mHandler.post(mLogServiceChecker); |
| 1165 | } |
| 1166 | |
Vikram Aggarwal | 6aca689 | 2013-06-04 13:53:27 -0700 | [diff] [blame] | 1167 | /** |
| 1168 | * The application can be started from the following entry points: |
| 1169 | * <ul> |
| 1170 | * <li>Launcher: you tap on the Gmail icon in the launcher. This is what most users think of |
| 1171 | * as “Starting the app”.</li> |
| 1172 | * <li>Shortcut: Users can make a shortcut to take them directly to a label.</li> |
| 1173 | * <li>Widget: Shows the contents of a synced label, and allows: |
| 1174 | * <ul> |
| 1175 | * <li>Viewing the list (tapping on the title)</li> |
| 1176 | * <li>Composing a new message (tapping on the new message icon in the title. This |
| 1177 | * launches the {@link ComposeActivity}. |
| 1178 | * </li> |
| 1179 | * <li>Viewing a single message (tapping on a list element)</li> |
| 1180 | * </ul> |
| 1181 | * |
| 1182 | * </li> |
| 1183 | * <li>Tapping on a notification: |
| 1184 | * <ul> |
| 1185 | * <li>Shows message list if more than one message</li> |
| 1186 | * <li>Shows the conversation if the notification is for a single message</li> |
| 1187 | * </ul> |
| 1188 | * </li> |
| 1189 | * <li>...and most importantly, the activity life cycle can tear down the application and |
| 1190 | * restart it: |
| 1191 | * <ul> |
| 1192 | * <li>Rotate the application: it is destroyed and recreated.</li> |
| 1193 | * <li>Navigate away, and return from recent applications.</li> |
| 1194 | * </ul> |
| 1195 | * </li> |
| 1196 | * <li>Add a new account: fires off an intent to add an account, |
| 1197 | * and returns in {@link #onActivityResult(int, int, android.content.Intent)} .</li> |
| 1198 | * <li>Re-authenticate your account: again returns in onActivityResult().</li> |
| 1199 | * <li>Composing can happen from many entry points: third party applications fire off an |
| 1200 | * intent to compose email, and launch directly into the {@link ComposeActivity} |
| 1201 | * .</li> |
| 1202 | * </ul> |
| 1203 | * {@inheritDoc} |
| 1204 | */ |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 1205 | @Override |
Vikram Aggarwal | d7a12cd | 2012-02-03 09:36:20 -0800 | [diff] [blame] | 1206 | public boolean onCreate(Bundle savedState) { |
Vikram Aggarwal | abd24d8 | 2012-04-26 13:23:14 -0700 | [diff] [blame] | 1207 | initializeActionBar(); |
Vikram Aggarwal | 59f741f | 2013-03-01 15:55:40 -0800 | [diff] [blame] | 1208 | initializeDevLoggingService(); |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1209 | // Allow shortcut keys to function for the ActionBar and menus. |
| 1210 | mActivity.setDefaultKeyMode(Activity.DEFAULT_KEYS_SHORTCUT); |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 1211 | mResolver = mActivity.getContentResolver(); |
Paul Westbrook | 6ead20d | 2012-03-19 14:48:14 -0700 | [diff] [blame] | 1212 | mNewEmailReceiver = new SuppressNotificationReceiver(); |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 1213 | mRecentFolderList.initialize(mActivity); |
Vikram Aggarwal | 69a6cdf | 2013-01-08 16:05:17 -0800 | [diff] [blame] | 1214 | mVeiledMatcher.initialize(this); |
Paul Westbrook | 6ead20d | 2012-03-19 14:48:14 -0700 | [diff] [blame] | 1215 | |
Tony Mantler | c6691fe | 2013-07-15 15:46:12 -0700 | [diff] [blame] | 1216 | // the "open drawer description" argument is for when the drawer is open |
| 1217 | // so tell the user that interacting will cause the drawer to close |
| 1218 | // and vice versa for the "close drawer description" argument |
Andy Huang | 12b3ee4 | 2013-04-24 22:49:43 -0700 | [diff] [blame] | 1219 | mDrawerToggle = new ActionBarDrawerToggle((Activity) mActivity, mDrawerContainer, |
Tony Mantler | c6691fe | 2013-07-15 15:46:12 -0700 | [diff] [blame] | 1220 | R.drawable.ic_drawer, R.string.drawer_close, R.string.drawer_open); |
Andrew Sapperstein | a3ce678 | 2013-05-09 15:14:49 -0700 | [diff] [blame] | 1221 | mDrawerListener = new MailDrawerListener(); |
| 1222 | mDrawerContainer.setDrawerListener(mDrawerListener); |
Andy Huang | 12b3ee4 | 2013-04-24 22:49:43 -0700 | [diff] [blame] | 1223 | mDrawerContainer.setDrawerShadow( |
| 1224 | mContext.getResources().getDrawable(R.drawable.drawer_shadow), Gravity.START); |
| 1225 | |
| 1226 | mDrawerToggle.setDrawerIndicatorEnabled(isDrawerEnabled()); |
| 1227 | |
Mindy Pereira | 161f50d | 2012-02-28 15:47:19 -0800 | [diff] [blame] | 1228 | // All the individual UI components listen for ViewMode changes. This |
Mindy Pereira | b849dfb | 2012-03-07 18:13:15 -0800 | [diff] [blame] | 1229 | // simplifies the amount of logic in the AbstractActivityController, but increases the |
| 1230 | // possibility of timing-related bugs. |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1231 | mViewMode.addListener(this); |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1232 | mPagerController = new ConversationPagerController(mActivity, this); |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 1233 | mToastBar = (ActionableToastBar) mActivity.findViewById(R.id.toast_bar); |
Vikram Aggarwal | ada5178 | 2012-04-26 14:18:31 -0700 | [diff] [blame] | 1234 | attachActionBar(); |
Mark Wei | 9eb1c9a | 2012-10-01 12:54:50 -0700 | [diff] [blame] | 1235 | FolderSelectionDialog.setDialogDismissed(); |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1236 | |
Andy Huang | 144bfe7 | 2013-06-11 13:27:52 -0700 | [diff] [blame] | 1237 | mDrawIdler.setRootView(mActivity.getWindow().getDecorView()); |
| 1238 | |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1239 | final Intent intent = mActivity.getIntent(); |
Andy Huang | 4fe0af8 | 2013-08-20 17:24:51 -0700 | [diff] [blame] | 1240 | |
Vikram Aggarwal | abd24d8 | 2012-04-26 13:23:14 -0700 | [diff] [blame] | 1241 | // Immediately handle a clean launch with intent, and any state restoration |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1242 | // that does not rely on restored fragments or loader data |
| 1243 | // any state restoration that relies on those can be done later in |
| 1244 | // onRestoreInstanceState, once fragments are up and loader data is re-delivered |
| 1245 | if (savedState != null) { |
| 1246 | if (savedState.containsKey(SAVED_ACCOUNT)) { |
| 1247 | setAccount((Account) savedState.getParcelable(SAVED_ACCOUNT)); |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1248 | } |
| 1249 | if (savedState.containsKey(SAVED_FOLDER)) { |
Andy Huang | 2bc8bc1 | 2012-11-12 17:24:25 -0800 | [diff] [blame] | 1250 | final Folder folder = savedState.getParcelable(SAVED_FOLDER); |
Vikram Aggarwal | 203dc00 | 2012-08-23 13:59:04 -0700 | [diff] [blame] | 1251 | final String query = savedState.getString(SAVED_QUERY, null); |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame] | 1252 | setListContext(folder, query); |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1253 | } |
Vikram Aggarwal | 6cadbfc | 2012-12-27 09:17:05 -0800 | [diff] [blame] | 1254 | if (savedState.containsKey(SAVED_ACTION)) { |
| 1255 | mDialogAction = savedState.getInt(SAVED_ACTION); |
| 1256 | } |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame] | 1257 | mDialogFromSelectedSet = savedState.getBoolean(SAVED_ACTION_FROM_SELECTED, false); |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame] | 1258 | mViewMode.handleRestore(savedState); |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1259 | } else if (intent != null) { |
| 1260 | handleIntent(intent); |
| 1261 | } |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1262 | // Create the accounts loader; this loads the account switch spinner. |
Vikram Aggarwal | 177097f | 2013-03-08 11:19:53 -0800 | [diff] [blame] | 1263 | mActivity.getLoaderManager().initLoader(LOADER_ACCOUNT_CURSOR, Bundle.EMPTY, |
| 1264 | mAccountCallbacks); |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1265 | return true; |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 1266 | } |
| 1267 | |
| 1268 | @Override |
Paul Westbrook | 57246a4 | 2013-04-21 09:40:22 -0700 | [diff] [blame] | 1269 | public void onPostCreate(Bundle savedState) { |
Andy Huang | 12b3ee4 | 2013-04-24 22:49:43 -0700 | [diff] [blame] | 1270 | // Sync the toggle state after onRestoreInstanceState has occurred. |
| 1271 | mDrawerToggle.syncState(); |
Andrew Sapperstein | 5747e15 | 2013-05-13 14:13:08 -0700 | [diff] [blame] | 1272 | |
Vikram Aggarwal | d70fe49 | 2013-06-04 12:52:07 -0700 | [diff] [blame] | 1273 | mHideMenuItems = isDrawerEnabled() && mDrawerContainer.isDrawerOpen(mDrawerPullout); |
Paul Westbrook | 57246a4 | 2013-04-21 09:40:22 -0700 | [diff] [blame] | 1274 | } |
| 1275 | |
| 1276 | @Override |
| 1277 | public void onConfigurationChanged(Configuration newConfig) { |
Andy Huang | 12b3ee4 | 2013-04-24 22:49:43 -0700 | [diff] [blame] | 1278 | mDrawerToggle.onConfigurationChanged(newConfig); |
| 1279 | } |
| 1280 | |
| 1281 | /** |
| 1282 | * If drawer is open/visible (even partially), close it. |
| 1283 | */ |
| 1284 | protected void closeDrawerIfOpen() { |
| 1285 | if (!isDrawerEnabled()) { |
| 1286 | return; |
| 1287 | } |
| 1288 | if(mDrawerContainer.isDrawerOpen(mDrawerPullout)) { |
| 1289 | mDrawerContainer.closeDrawers(); |
| 1290 | } |
Paul Westbrook | 57246a4 | 2013-04-21 09:40:22 -0700 | [diff] [blame] | 1291 | } |
| 1292 | |
| 1293 | @Override |
Andy Huang | 1ee96b2 | 2012-08-24 20:19:53 -0700 | [diff] [blame] | 1294 | public void onStart() { |
| 1295 | mSafeToModifyFragments = true; |
Scott Kennedy | cb85aea | 2013-02-25 13:08:32 -0800 | [diff] [blame] | 1296 | |
| 1297 | NotificationActionUtils.registerUndoNotificationObserver(mUndoNotificationObserver); |
Andy Huang | 761522c | 2013-08-08 13:09:11 -0700 | [diff] [blame] | 1298 | |
| 1299 | if (mViewMode.getMode() != ViewMode.UNKNOWN) { |
| 1300 | Analytics.getInstance().sendView("MainActivity" + mViewMode.toString()); |
| 1301 | } |
Andy Huang | 1ee96b2 | 2012-08-24 20:19:53 -0700 | [diff] [blame] | 1302 | } |
| 1303 | |
| 1304 | @Override |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 1305 | public void onRestart() { |
Vikram Aggarwal | 177097f | 2013-03-08 11:19:53 -0800 | [diff] [blame] | 1306 | final DialogFragment fragment = (DialogFragment) |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 1307 | mFragmentManager.findFragmentByTag(SYNC_ERROR_DIALOG_FRAGMENT_TAG); |
| 1308 | if (fragment != null) { |
| 1309 | fragment.dismiss(); |
| 1310 | } |
mindyp | ea04f93 | 2012-08-27 14:17:59 -0700 | [diff] [blame] | 1311 | // When the user places the app in the background by pressing "home", |
| 1312 | // dismiss the toast bar. However, since there is no way to determine if |
| 1313 | // home was pressed, just dismiss any existing toast bar when restarting |
| 1314 | // the app. |
| 1315 | if (mToastBar != null) { |
Andrew Sapperstein | f8ccdcf | 2013-08-08 19:30:38 -0700 | [diff] [blame] | 1316 | mToastBar.hide(false, false /* actionClicked */); |
mindyp | ea04f93 | 2012-08-27 14:17:59 -0700 | [diff] [blame] | 1317 | } |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 1318 | } |
| 1319 | |
| 1320 | @Override |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1321 | public Dialog onCreateDialog(int id, Bundle bundle) { |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1322 | return null; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1323 | } |
| 1324 | |
| 1325 | @Override |
Vikram Aggarwal | 4eb5271 | 2012-06-19 16:24:50 -0700 | [diff] [blame] | 1326 | public final boolean onCreateOptionsMenu(Menu menu) { |
Andrew Sapperstein | 0fa9efd | 2013-08-07 14:09:31 -0700 | [diff] [blame] | 1327 | if (mViewMode.isAdMode()) { |
| 1328 | return false; |
| 1329 | } |
Vikram Aggarwal | e5e917c | 2012-09-20 16:27:41 -0700 | [diff] [blame] | 1330 | final MenuInflater inflater = mActivity.getMenuInflater(); |
Mindy Pereira | f5acda4 | 2012-02-15 20:13:59 -0800 | [diff] [blame] | 1331 | inflater.inflate(mActionBarView.getOptionsMenuId(), menu); |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 1332 | mActionBarView.onCreateOptionsMenu(menu); |
Mindy Pereira | 28d5f72 | 2012-02-15 12:32:40 -0800 | [diff] [blame] | 1333 | return true; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | @Override |
Vikram Aggarwal | 4eb5271 | 2012-06-19 16:24:50 -0700 | [diff] [blame] | 1337 | public final boolean onKeyDown(int keyCode, KeyEvent event) { |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 1338 | return false; |
| 1339 | } |
| 1340 | |
mindyp | 17a8e78 | 2012-11-29 14:56:17 -0800 | [diff] [blame] | 1341 | public abstract boolean doesActionChangeConversationListVisibility(int action); |
| 1342 | |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 1343 | @Override |
Paul Westbrook | 57246a4 | 2013-04-21 09:40:22 -0700 | [diff] [blame] | 1344 | public boolean onOptionsItemSelected(MenuItem item) { |
Andy Huang | 761522c | 2013-08-08 13:09:11 -0700 | [diff] [blame] | 1345 | |
Andy Huang | 12b3ee4 | 2013-04-24 22:49:43 -0700 | [diff] [blame] | 1346 | /* |
| 1347 | * The action bar home/up action should open or close the drawer. |
| 1348 | * mDrawerToggle will take care of this. |
| 1349 | */ |
| 1350 | if (mDrawerToggle.onOptionsItemSelected(item)) { |
Andy Huang | 042a530 | 2013-08-13 12:39:08 -0700 | [diff] [blame] | 1351 | Analytics.getInstance().sendEvent(Analytics.EVENT_CATEGORY_MENU_ITEM, "drawer_toggle", |
| 1352 | null, 0); |
Andy Huang | 12b3ee4 | 2013-04-24 22:49:43 -0700 | [diff] [blame] | 1353 | return true; |
| 1354 | } |
| 1355 | |
Andy Huang | 2b55549 | 2013-08-14 21:06:21 -0700 | [diff] [blame] | 1356 | Analytics.getInstance().sendMenuItemEvent(Analytics.EVENT_CATEGORY_MENU_ITEM, |
| 1357 | item.getItemId(), "action_bar", 0); |
Andy Huang | 042a530 | 2013-08-13 12:39:08 -0700 | [diff] [blame] | 1358 | |
Vikram Aggarwal | 54452ae | 2012-03-13 15:29:00 -0700 | [diff] [blame] | 1359 | final int id = item.getItemId(); |
Vikram Aggarwal | 7dd054e | 2012-05-21 14:43:10 -0700 | [diff] [blame] | 1360 | LogUtils.d(LOG_TAG, "AbstractController.onOptionsItemSelected(%d) called.", id); |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 1361 | boolean handled = true; |
Vikram Aggarwal | 84fe994 | 2013-04-17 11:20:58 -0700 | [diff] [blame] | 1362 | /** This is NOT a batch action. */ |
| 1363 | final boolean isBatch = false; |
Mindy Pereira | ba68fda | 2012-05-24 15:53:06 -0700 | [diff] [blame] | 1364 | final Collection<Conversation> target = Conversation.listOf(mCurrentConversation); |
Vikram Aggarwal | 112cd16 | 2012-06-18 10:51:13 -0700 | [diff] [blame] | 1365 | final Settings settings = (mAccount == null) ? null : mAccount.settings; |
mindyp | 84f7d32 | 2012-10-01 17:14:40 -0700 | [diff] [blame] | 1366 | // The user is choosing a new action; commit whatever they had been |
mindyp | 17a8e78 | 2012-11-29 14:56:17 -0800 | [diff] [blame] | 1367 | // doing before. Don't animate if we are launching a new screen. |
| 1368 | commitDestructiveActions(!doesActionChangeConversationListVisibility(id)); |
Scott Kennedy | 2b9d80e | 2013-07-30 23:03:45 -0700 | [diff] [blame] | 1369 | if (id == R.id.archive) { |
| 1370 | final boolean showDialog = (settings != null && settings.confirmArchive); |
| 1371 | confirmAndDelete(id, target, showDialog, R.plurals.confirm_archive_conversation); |
| 1372 | } else if (id == R.id.remove_folder) { |
| 1373 | delete(R.id.remove_folder, target, |
| 1374 | getDeferredRemoveFolder(target, mFolder, true, isBatch, true), isBatch); |
| 1375 | } else if (id == R.id.delete) { |
| 1376 | final boolean showDialog = (settings != null && settings.confirmDelete); |
| 1377 | confirmAndDelete(id, target, showDialog, R.plurals.confirm_delete_conversation); |
| 1378 | } else if (id == R.id.discard_drafts) { |
Andy Huang | 121c8b8 | 2013-08-05 11:52:55 -0700 | [diff] [blame] | 1379 | // drafts are lost forever, so always confirm |
| 1380 | confirmAndDelete(id, target, true /* showDialog */, |
Scott Kennedy | 2b9d80e | 2013-07-30 23:03:45 -0700 | [diff] [blame] | 1381 | R.plurals.confirm_discard_drafts_conversation); |
| 1382 | } else if (id == R.id.mark_important) { |
| 1383 | updateConversation(Conversation.listOf(mCurrentConversation), |
| 1384 | ConversationColumns.PRIORITY, UIProvider.ConversationPriority.HIGH); |
| 1385 | } else if (id == R.id.mark_not_important) { |
| 1386 | if (mFolder != null && mFolder.isImportantOnly()) { |
| 1387 | delete(R.id.mark_not_important, target, |
| 1388 | getDeferredAction(R.id.mark_not_important, target, isBatch), isBatch); |
| 1389 | } else { |
Vikram Aggarwal | 531488e | 2012-05-29 16:36:52 -0700 | [diff] [blame] | 1390 | updateConversation(Conversation.listOf(mCurrentConversation), |
Scott Kennedy | 2b9d80e | 2013-07-30 23:03:45 -0700 | [diff] [blame] | 1391 | ConversationColumns.PRIORITY, UIProvider.ConversationPriority.LOW); |
| 1392 | } |
| 1393 | } else if (id == R.id.mute) { |
| 1394 | delete(R.id.mute, target, getDeferredAction(R.id.mute, target, isBatch), isBatch); |
| 1395 | } else if (id == R.id.report_spam) { |
| 1396 | delete(R.id.report_spam, target, |
| 1397 | getDeferredAction(R.id.report_spam, target, isBatch), isBatch); |
| 1398 | } else if (id == R.id.mark_not_spam) { |
| 1399 | // Currently, since spam messages are only shown in list with |
| 1400 | // other spam messages, |
| 1401 | // marking a message not as spam is a destructive action |
| 1402 | delete(R.id.mark_not_spam, target, |
| 1403 | getDeferredAction(R.id.mark_not_spam, target, isBatch), isBatch); |
| 1404 | } else if (id == R.id.report_phishing) { |
| 1405 | delete(R.id.report_phishing, target, |
| 1406 | getDeferredAction(R.id.report_phishing, target, isBatch), isBatch); |
| 1407 | } else if (id == android.R.id.home) { |
| 1408 | onUpPressed(); |
| 1409 | } else if (id == R.id.compose) { |
| 1410 | ComposeActivity.compose(mActivity.getActivityContext(), mAccount); |
| 1411 | } else if (id == R.id.refresh) { |
| 1412 | requestFolderRefresh(); |
| 1413 | } else if (id == R.id.settings) { |
| 1414 | Utils.showSettings(mActivity.getActivityContext(), mAccount); |
| 1415 | } else if (id == R.id.folder_options) { |
| 1416 | Utils.showFolderSettings(mActivity.getActivityContext(), mAccount, mFolder); |
| 1417 | } else if (id == R.id.help_info_menu_item) { |
| 1418 | Utils.showHelp(mActivity.getActivityContext(), mAccount, getHelpContext()); |
| 1419 | } else if (id == R.id.feedback_menu_item) { |
| 1420 | Utils.sendFeedback(mActivity, mAccount, false); |
| 1421 | } else if (id == R.id.manage_folders_item) { |
| 1422 | Utils.showManageFolder(mActivity.getActivityContext(), mAccount); |
Andrew Sapperstein | 6c570db | 2013-08-06 17:21:36 -0700 | [diff] [blame] | 1423 | } else if (id == R.id.move_to || id == R.id.change_folders) { |
Scott Kennedy | 2b9d80e | 2013-07-30 23:03:45 -0700 | [diff] [blame] | 1424 | final FolderSelectionDialog dialog = FolderSelectionDialog.getInstance( |
| 1425 | mActivity.getActivityContext(), mAccount, this, |
| 1426 | Conversation.listOf(mCurrentConversation), isBatch, mFolder, |
| 1427 | id == R.id.move_to); |
| 1428 | if (dialog != null) { |
| 1429 | dialog.show(); |
| 1430 | } |
| 1431 | } else if (id == R.id.move_to_inbox) { |
| 1432 | new AsyncTask<Void, Void, Folder>() { |
| 1433 | @Override |
| 1434 | protected Folder doInBackground(final Void... params) { |
| 1435 | // Get the "move to" inbox |
| 1436 | return Utils.getFolder(mContext, mAccount.settings.moveToInbox, |
| 1437 | true /* allowHidden */); |
Mindy Pereira | 0d03ef8 | 2012-08-15 09:05:48 -0700 | [diff] [blame] | 1438 | } |
Scott Kennedy | dd2ec68 | 2013-06-03 19:16:13 -0700 | [diff] [blame] | 1439 | |
Scott Kennedy | 2b9d80e | 2013-07-30 23:03:45 -0700 | [diff] [blame] | 1440 | @Override |
| 1441 | protected void onPostExecute(final Folder moveToInbox) { |
| 1442 | final List<FolderOperation> ops = Lists.newArrayListWithCapacity(1); |
| 1443 | // Add inbox |
| 1444 | ops.add(new FolderOperation(moveToInbox, true)); |
| 1445 | assignFolder(ops, Conversation.listOf(mCurrentConversation), true, |
| 1446 | true /* showUndo */, false /* isMoveTo */); |
| 1447 | } |
| 1448 | }.execute((Void[]) null); |
| 1449 | } else if (id == R.id.empty_trash) { |
| 1450 | showEmptyDialog(); |
| 1451 | } else if (id == R.id.empty_spam) { |
| 1452 | showEmptyDialog(); |
| 1453 | } else { |
| 1454 | handled = false; |
Mindy Pereira | 28d5f72 | 2012-02-15 12:32:40 -0800 | [diff] [blame] | 1455 | } |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 1456 | return handled; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1457 | } |
| 1458 | |
Andrew Sapperstein | ed5b52d | 2013-04-30 13:40:18 -0700 | [diff] [blame] | 1459 | /** |
| 1460 | * Opens an {@link EmptyFolderDialogFragment} for the current folder. |
| 1461 | */ |
| 1462 | private void showEmptyDialog() { |
| 1463 | if (mFolder != null) { |
| 1464 | final EmptyFolderDialogFragment fragment = |
| 1465 | EmptyFolderDialogFragment.newInstance(mFolder.totalCount, mFolder.type); |
| 1466 | fragment.setListener(this); |
| 1467 | fragment.show(mActivity.getFragmentManager(), EmptyFolderDialogFragment.FRAGMENT_TAG); |
| 1468 | } |
| 1469 | } |
| 1470 | |
| 1471 | @Override |
| 1472 | public void onFolderEmptied() { |
| 1473 | emptyFolder(); |
| 1474 | } |
| 1475 | |
| 1476 | /** |
| 1477 | * Performs the work of emptying the currently visible folder. |
| 1478 | */ |
Scott Kennedy | 7ee089e | 2013-03-25 17:05:44 -0400 | [diff] [blame] | 1479 | private void emptyFolder() { |
Yorke Lee | 1d0f1f8 | 2013-04-26 14:10:27 -0700 | [diff] [blame] | 1480 | if (mConversationListCursor != null) { |
| 1481 | mConversationListCursor.emptyFolder(); |
| 1482 | } |
Scott Kennedy | 7ee089e | 2013-03-25 17:05:44 -0400 | [diff] [blame] | 1483 | } |
| 1484 | |
Andrew Sapperstein | ed5b52d | 2013-04-30 13:40:18 -0700 | [diff] [blame] | 1485 | private void attachEmptyFolderDialogFragmentListener() { |
| 1486 | final EmptyFolderDialogFragment fragment = |
| 1487 | (EmptyFolderDialogFragment) mActivity.getFragmentManager() |
| 1488 | .findFragmentByTag(EmptyFolderDialogFragment.FRAGMENT_TAG); |
| 1489 | |
| 1490 | if (fragment != null) { |
| 1491 | fragment.setListener(this); |
| 1492 | } |
| 1493 | } |
| 1494 | |
Rohan Shah | 8e65c6d | 2013-03-07 16:47:25 -0800 | [diff] [blame] | 1495 | /** |
Andy Huang | 12b3ee4 | 2013-04-24 22:49:43 -0700 | [diff] [blame] | 1496 | * Toggles the drawer pullout. If it was open (Fully extended), the |
| 1497 | * drawer will be closed. Otherwise, the drawer will be opened. This should |
| 1498 | * only be called when used with a toggle item. Other cases should be handled |
| 1499 | * explicitly with just closeDrawers() or openDrawer(View drawerView); |
Rohan Shah | 8e65c6d | 2013-03-07 16:47:25 -0800 | [diff] [blame] | 1500 | */ |
Tony Mantler | d9eaca8 | 2013-08-05 11:42:29 -0700 | [diff] [blame] | 1501 | protected void toggleDrawerState() { |
Andy Huang | 12b3ee4 | 2013-04-24 22:49:43 -0700 | [diff] [blame] | 1502 | if (!isDrawerEnabled()) { |
| 1503 | return; |
| 1504 | } |
| 1505 | if(mDrawerContainer.isDrawerOpen(mDrawerPullout)) { |
| 1506 | mDrawerContainer.closeDrawers(); |
| 1507 | } else { |
| 1508 | mDrawerContainer.openDrawer(mDrawerPullout); |
| 1509 | } |
Rohan Shah | 8e65c6d | 2013-03-07 16:47:25 -0800 | [diff] [blame] | 1510 | } |
| 1511 | |
Vikram Aggarwal | 531488e | 2012-05-29 16:36:52 -0700 | [diff] [blame] | 1512 | @Override |
Andy Huang | c1fb9a9 | 2013-02-11 13:09:12 -0800 | [diff] [blame] | 1513 | public final boolean onUpPressed() { |
| 1514 | for (UpOrBackHandler h : mUpOrBackHandlers) { |
| 1515 | if (h.onUpPressed()) { |
| 1516 | return true; |
| 1517 | } |
| 1518 | } |
| 1519 | return handleUpPress(); |
| 1520 | } |
| 1521 | |
| 1522 | @Override |
| 1523 | public final boolean onBackPressed() { |
| 1524 | for (UpOrBackHandler h : mUpOrBackHandlers) { |
| 1525 | if (h.onBackPressed()) { |
| 1526 | return true; |
| 1527 | } |
| 1528 | } |
Andy Huang | 12b3ee4 | 2013-04-24 22:49:43 -0700 | [diff] [blame] | 1529 | |
| 1530 | if (isDrawerEnabled() && mDrawerContainer.isDrawerVisible(mDrawerPullout)) { |
| 1531 | mDrawerContainer.closeDrawers(); |
| 1532 | return true; |
| 1533 | } |
| 1534 | |
Andy Huang | c1fb9a9 | 2013-02-11 13:09:12 -0800 | [diff] [blame] | 1535 | return handleBackPress(); |
| 1536 | } |
| 1537 | |
| 1538 | protected abstract boolean handleBackPress(); |
| 1539 | protected abstract boolean handleUpPress(); |
| 1540 | |
| 1541 | @Override |
| 1542 | public void addUpOrBackHandler(UpOrBackHandler handler) { |
| 1543 | if (mUpOrBackHandlers.contains(handler)) { |
| 1544 | return; |
| 1545 | } |
| 1546 | mUpOrBackHandlers.addFirst(handler); |
| 1547 | } |
| 1548 | |
| 1549 | @Override |
| 1550 | public void removeUpOrBackHandler(UpOrBackHandler handler) { |
| 1551 | mUpOrBackHandlers.remove(handler); |
| 1552 | } |
| 1553 | |
| 1554 | @Override |
Mindy Pereira | 6c2663d | 2012-07-20 15:37:29 -0700 | [diff] [blame] | 1555 | public void updateConversation(Collection<Conversation> target, ContentValues values) { |
Scott Kennedy | 9e2d407 | 2013-03-21 21:46:01 -0700 | [diff] [blame] | 1556 | mConversationListCursor.updateValues(target, values); |
Mindy Pereira | 6c2663d | 2012-07-20 15:37:29 -0700 | [diff] [blame] | 1557 | refreshConversationList(); |
| 1558 | } |
| 1559 | |
| 1560 | @Override |
Vikram Aggarwal | 531488e | 2012-05-29 16:36:52 -0700 | [diff] [blame] | 1561 | public void updateConversation(Collection <Conversation> target, String columnName, |
| 1562 | boolean value) { |
Scott Kennedy | 9e2d407 | 2013-03-21 21:46:01 -0700 | [diff] [blame] | 1563 | mConversationListCursor.updateBoolean(target, columnName, value); |
Vikram Aggarwal | 75daee5 | 2012-04-30 11:13:09 -0700 | [diff] [blame] | 1564 | refreshConversationList(); |
Vikram Aggarwal | 54452ae | 2012-03-13 15:29:00 -0700 | [diff] [blame] | 1565 | } |
| 1566 | |
Vikram Aggarwal | 531488e | 2012-05-29 16:36:52 -0700 | [diff] [blame] | 1567 | @Override |
Mindy Pereira | 6c2663d | 2012-07-20 15:37:29 -0700 | [diff] [blame] | 1568 | public void updateConversation(Collection <Conversation> target, String columnName, |
| 1569 | int value) { |
Scott Kennedy | 9e2d407 | 2013-03-21 21:46:01 -0700 | [diff] [blame] | 1570 | mConversationListCursor.updateInt(target, columnName, value); |
Vikram Aggarwal | 75daee5 | 2012-04-30 11:13:09 -0700 | [diff] [blame] | 1571 | refreshConversationList(); |
Mindy Pereira | c9d5918 | 2012-03-22 16:06:46 -0700 | [diff] [blame] | 1572 | } |
| 1573 | |
Vikram Aggarwal | 531488e | 2012-05-29 16:36:52 -0700 | [diff] [blame] | 1574 | @Override |
| 1575 | public void updateConversation(Collection <Conversation> target, String columnName, |
| 1576 | String value) { |
Scott Kennedy | 9e2d407 | 2013-03-21 21:46:01 -0700 | [diff] [blame] | 1577 | mConversationListCursor.updateString(target, columnName, value); |
Vikram Aggarwal | 75daee5 | 2012-04-30 11:13:09 -0700 | [diff] [blame] | 1578 | refreshConversationList(); |
Vikram Aggarwal | 54452ae | 2012-03-13 15:29:00 -0700 | [diff] [blame] | 1579 | } |
| 1580 | |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 1581 | @Override |
Yu Ping Hu | 7c909c7 | 2013-01-18 11:58:01 -0800 | [diff] [blame] | 1582 | public void markConversationMessagesUnread(final Conversation conv, |
| 1583 | final Set<Uri> unreadMessageUris, final byte[] originalConversationInfo) { |
Andy Huang | 8f6b006 | 2012-07-31 15:36:31 -0700 | [diff] [blame] | 1584 | // The only caller of this method is the conversation view, from where marking unread should |
| 1585 | // *always* take you back to list mode. |
| 1586 | showConversation(null); |
| 1587 | |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 1588 | // locally mark conversation unread (the provider is supposed to propagate message unread |
| 1589 | // to conversation unread) |
| 1590 | conv.read = false; |
Paul Westbrook | 1faf93d | 2012-10-16 08:58:07 -0700 | [diff] [blame] | 1591 | if (mConversationListCursor == null) { |
Vikram Aggarwal | 81230ee | 2013-02-26 15:26:44 -0800 | [diff] [blame] | 1592 | LogUtils.d(LOG_TAG, "markConversationMessagesUnread(id=%d), deferring", conv.id); |
Paul Westbrook | 1faf93d | 2012-10-16 08:58:07 -0700 | [diff] [blame] | 1593 | |
Yu Ping Hu | 7c909c7 | 2013-01-18 11:58:01 -0800 | [diff] [blame] | 1594 | mConversationListLoadFinishedCallbacks.add(new LoadFinishedCallback() { |
| 1595 | @Override |
| 1596 | public void onLoadFinished() { |
| 1597 | doMarkConversationMessagesUnread(conv, unreadMessageUris, |
| 1598 | originalConversationInfo); |
| 1599 | } |
| 1600 | }); |
| 1601 | } else { |
Vikram Aggarwal | 81230ee | 2013-02-26 15:26:44 -0800 | [diff] [blame] | 1602 | LogUtils.d(LOG_TAG, "markConversationMessagesUnread(id=%d), performing", conv.id); |
Yu Ping Hu | 7c909c7 | 2013-01-18 11:58:01 -0800 | [diff] [blame] | 1603 | doMarkConversationMessagesUnread(conv, unreadMessageUris, originalConversationInfo); |
| 1604 | } |
| 1605 | } |
| 1606 | |
| 1607 | private void doMarkConversationMessagesUnread(Conversation conv, Set<Uri> unreadMessageUris, |
| 1608 | byte[] originalConversationInfo) { |
Vikram Aggarwal | 81230ee | 2013-02-26 15:26:44 -0800 | [diff] [blame] | 1609 | // Only do a granular 'mark unread' if a subset of messages are unread |
Andy Huang | 28e31e2 | 2012-07-26 16:33:15 -0700 | [diff] [blame] | 1610 | final int unreadCount = (unreadMessageUris == null) ? 0 : unreadMessageUris.size(); |
Mindy Pereira | 0972e07 | 2012-08-01 17:43:06 -0700 | [diff] [blame] | 1611 | final int numMessages = conv.getNumMessages(); |
| 1612 | final boolean subsetIsUnread = (numMessages > 1 && unreadCount > 0 |
| 1613 | && unreadCount < numMessages); |
Andy Huang | 28e31e2 | 2012-07-26 16:33:15 -0700 | [diff] [blame] | 1614 | |
Andy Huang | 9e4ca79 | 2013-02-28 14:33:43 -0800 | [diff] [blame] | 1615 | LogUtils.d(LOG_TAG, "markConversationMessagesUnread(conv=%s)" |
Vikram Aggarwal | 81230ee | 2013-02-26 15:26:44 -0800 | [diff] [blame] | 1616 | + ", numMessages=%d, unreadCount=%d, subsetIsUnread=%b", |
Andy Huang | 9e4ca79 | 2013-02-28 14:33:43 -0800 | [diff] [blame] | 1617 | conv, numMessages, unreadCount, subsetIsUnread); |
Andy Huang | 28e31e2 | 2012-07-26 16:33:15 -0700 | [diff] [blame] | 1618 | if (!subsetIsUnread) { |
Vikram Aggarwal | 66bc2aa | 2012-08-02 10:47:03 -0700 | [diff] [blame] | 1619 | // Conversations are neither marked read, nor viewed, and we don't want to show |
| 1620 | // the next conversation. |
Vikram Aggarwal | 81230ee | 2013-02-26 15:26:44 -0800 | [diff] [blame] | 1621 | LogUtils.d(LOG_TAG, ". . doing full mark unread"); |
Scott Kennedy | caaeed3 | 2013-06-12 13:39:16 -0700 | [diff] [blame] | 1622 | markConversationsRead(Collections.singletonList(conv), false, false, false); |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 1623 | } else { |
Vikram Aggarwal | 81230ee | 2013-02-26 15:26:44 -0800 | [diff] [blame] | 1624 | if (LogUtils.isLoggable(LOG_TAG, LogUtils.DEBUG)) { |
| 1625 | final ConversationInfo info = ConversationInfo.fromBlob(originalConversationInfo); |
| 1626 | LogUtils.d(LOG_TAG, ". . doing subset mark unread, originalConversationInfo = %s", |
| 1627 | info); |
| 1628 | } |
Andy Huang | daa06ab | 2012-07-24 10:46:44 -0700 | [diff] [blame] | 1629 | mConversationListCursor.setConversationColumn(conv.uri, ConversationColumns.READ, 0); |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 1630 | |
Vikram Aggarwal | 81230ee | 2013-02-26 15:26:44 -0800 | [diff] [blame] | 1631 | // Locally update conversation's conversationInfo to revert to original version |
Andy Huang | 28e31e2 | 2012-07-26 16:33:15 -0700 | [diff] [blame] | 1632 | if (originalConversationInfo != null) { |
| 1633 | mConversationListCursor.setConversationColumn(conv.uri, |
| 1634 | ConversationColumns.CONVERSATION_INFO, originalConversationInfo); |
| 1635 | } |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 1636 | |
| 1637 | // applyBatch with each CPO as an UPDATE op on each affected message uri |
| 1638 | final ArrayList<ContentProviderOperation> ops = Lists.newArrayList(); |
| 1639 | String authority = null; |
| 1640 | for (Uri messageUri : unreadMessageUris) { |
| 1641 | if (authority == null) { |
| 1642 | authority = messageUri.getAuthority(); |
| 1643 | } |
| 1644 | ops.add(ContentProviderOperation.newUpdate(messageUri) |
| 1645 | .withValue(UIProvider.MessageColumns.READ, 0) |
| 1646 | .build()); |
Vikram Aggarwal | 81230ee | 2013-02-26 15:26:44 -0800 | [diff] [blame] | 1647 | LogUtils.d(LOG_TAG, ". . Adding op: read=0, uri=%s", messageUri); |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 1648 | } |
Vikram Aggarwal | 81230ee | 2013-02-26 15:26:44 -0800 | [diff] [blame] | 1649 | LogUtils.d(LOG_TAG, ". . operations = %s", ops); |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 1650 | new ContentProviderTask() { |
| 1651 | @Override |
| 1652 | protected void onPostExecute(Result result) { |
Vikram Aggarwal | d9895b6 | 2013-02-27 16:52:27 -0800 | [diff] [blame] | 1653 | if (result.exception != null) { |
| 1654 | LogUtils.e(LOG_TAG, result.exception, "ContentProviderTask() ERROR."); |
| 1655 | } else { |
Andy Huang | 9e4ca79 | 2013-02-28 14:33:43 -0800 | [diff] [blame] | 1656 | LogUtils.d(LOG_TAG, "ContentProviderTask(): success %s", |
| 1657 | Arrays.toString(result.results)); |
Vikram Aggarwal | d9895b6 | 2013-02-27 16:52:27 -0800 | [diff] [blame] | 1658 | } |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 1659 | } |
| 1660 | }.run(mResolver, authority, ops); |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 1661 | } |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 1662 | } |
| 1663 | |
| 1664 | @Override |
Yu Ping Hu | 7c909c7 | 2013-01-18 11:58:01 -0800 | [diff] [blame] | 1665 | public void markConversationsRead(final Collection<Conversation> targets, final boolean read, |
| 1666 | final boolean viewed) { |
Scott Kennedy | 919d01a | 2013-05-07 16:13:29 -0700 | [diff] [blame] | 1667 | LogUtils.d(LOG_TAG, "markConversationsRead(targets=%s)", targets.toArray()); |
| 1668 | |
Yu Ping Hu | 7c909c7 | 2013-01-18 11:58:01 -0800 | [diff] [blame] | 1669 | if (mConversationListCursor == null) { |
Vikram Aggarwal | 81230ee | 2013-02-26 15:26:44 -0800 | [diff] [blame] | 1670 | if (LogUtils.isLoggable(LOG_TAG, LogUtils.DEBUG)) { |
| 1671 | LogUtils.d(LOG_TAG, "markConversationsRead(targets=%s), deferring", |
| 1672 | targets.toArray()); |
| 1673 | } |
Yu Ping Hu | 7c909c7 | 2013-01-18 11:58:01 -0800 | [diff] [blame] | 1674 | mConversationListLoadFinishedCallbacks.add(new LoadFinishedCallback() { |
| 1675 | @Override |
| 1676 | public void onLoadFinished() { |
Scott Kennedy | caaeed3 | 2013-06-12 13:39:16 -0700 | [diff] [blame] | 1677 | markConversationsRead(targets, read, viewed, true); |
Yu Ping Hu | 7c909c7 | 2013-01-18 11:58:01 -0800 | [diff] [blame] | 1678 | } |
| 1679 | }); |
| 1680 | } else { |
| 1681 | // We want to show the next conversation if we are marking unread. |
Scott Kennedy | caaeed3 | 2013-06-12 13:39:16 -0700 | [diff] [blame] | 1682 | markConversationsRead(targets, read, viewed, true); |
Yu Ping Hu | 7c909c7 | 2013-01-18 11:58:01 -0800 | [diff] [blame] | 1683 | } |
Andy Huang | 8f6b006 | 2012-07-31 15:36:31 -0700 | [diff] [blame] | 1684 | } |
| 1685 | |
Scott Kennedy | 0d0f8b0 | 2012-10-12 15:18:18 -0700 | [diff] [blame] | 1686 | private void markConversationsRead(final Collection<Conversation> targets, final boolean read, |
Scott Kennedy | caaeed3 | 2013-06-12 13:39:16 -0700 | [diff] [blame] | 1687 | final boolean markViewed, final boolean showNext) { |
Yu Ping Hu | 7c909c7 | 2013-01-18 11:58:01 -0800 | [diff] [blame] | 1688 | LogUtils.d(LOG_TAG, "performing markConversationsRead"); |
Vikram Aggarwal | cc754b1 | 2012-08-30 14:04:21 -0700 | [diff] [blame] | 1689 | // Auto-advance if requested and the current conversation is being marked unread |
Andy Huang | 8f6b006 | 2012-07-31 15:36:31 -0700 | [diff] [blame] | 1690 | if (showNext && !read) { |
Scott Kennedy | 0d0f8b0 | 2012-10-12 15:18:18 -0700 | [diff] [blame] | 1691 | final Runnable operation = new Runnable() { |
| 1692 | @Override |
| 1693 | public void run() { |
Scott Kennedy | caaeed3 | 2013-06-12 13:39:16 -0700 | [diff] [blame] | 1694 | markConversationsRead(targets, read, markViewed, showNext); |
Scott Kennedy | 0d0f8b0 | 2012-10-12 15:18:18 -0700 | [diff] [blame] | 1695 | } |
| 1696 | }; |
| 1697 | |
Scott Kennedy | caaeed3 | 2013-06-12 13:39:16 -0700 | [diff] [blame] | 1698 | if (!showNextConversation(targets, operation)) { |
Scott Kennedy | 0d0f8b0 | 2012-10-12 15:18:18 -0700 | [diff] [blame] | 1699 | // This method will be called again if the user selects an autoadvance option |
| 1700 | return; |
| 1701 | } |
Andy Huang | 8f6b006 | 2012-07-31 15:36:31 -0700 | [diff] [blame] | 1702 | } |
Scott Kennedy | 0d0f8b0 | 2012-10-12 15:18:18 -0700 | [diff] [blame] | 1703 | |
Vikram Aggarwal | cc754b1 | 2012-08-30 14:04:21 -0700 | [diff] [blame] | 1704 | final int size = targets.size(); |
| 1705 | final List<ConversationOperation> opList = new ArrayList<ConversationOperation>(size); |
| 1706 | for (final Conversation target : targets) { |
Paul Westbrook | e2be97a | 2013-05-21 02:12:09 -0700 | [diff] [blame] | 1707 | final ContentValues value = new ContentValues(4); |
Vikram Aggarwal | cc754b1 | 2012-08-30 14:04:21 -0700 | [diff] [blame] | 1708 | value.put(ConversationColumns.READ, read); |
Paul Westbrook | 5109c51 | 2012-11-05 11:00:30 -0800 | [diff] [blame] | 1709 | |
Scott Kennedy | d5edd2d | 2012-12-05 11:11:32 -0800 | [diff] [blame] | 1710 | // We never want to mark unseen here, but we do want to mark it seen |
| 1711 | if (read || markViewed) { |
| 1712 | value.put(ConversationColumns.SEEN, Boolean.TRUE); |
| 1713 | } |
| 1714 | |
Paul Westbrook | 5109c51 | 2012-11-05 11:00:30 -0800 | [diff] [blame] | 1715 | // The mark read/unread/viewed operations do not show an undo bar |
| 1716 | value.put(ConversationOperations.Parameters.SUPPRESS_UNDO, true); |
Vikram Aggarwal | 66bc2aa | 2012-08-02 10:47:03 -0700 | [diff] [blame] | 1717 | if (markViewed) { |
Vikram Aggarwal | cc754b1 | 2012-08-30 14:04:21 -0700 | [diff] [blame] | 1718 | value.put(ConversationColumns.VIEWED, true); |
Vikram Aggarwal | 66bc2aa | 2012-08-02 10:47:03 -0700 | [diff] [blame] | 1719 | } |
Vikram Aggarwal | 192fac1 | 2012-07-25 16:44:55 -0700 | [diff] [blame] | 1720 | final ConversationInfo info = target.conversationInfo; |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 1721 | if (info != null) { |
mindyp | 7f55c68 | 2012-10-04 11:38:27 -0700 | [diff] [blame] | 1722 | boolean changed = info.markRead(read); |
| 1723 | if (changed) { |
Vikram Aggarwal | 81230ee | 2013-02-26 15:26:44 -0800 | [diff] [blame] | 1724 | value.put(ConversationColumns.CONVERSATION_INFO, info.toBlob()); |
mindyp | 7f55c68 | 2012-10-04 11:38:27 -0700 | [diff] [blame] | 1725 | } |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 1726 | } |
Vikram Aggarwal | cc754b1 | 2012-08-30 14:04:21 -0700 | [diff] [blame] | 1727 | opList.add(mConversationListCursor.getOperationForConversation( |
| 1728 | target, ConversationOperation.UPDATE, value)); |
| 1729 | // Update the local conversation objects so they immediately change state. |
| 1730 | target.read = read; |
Andy Huang | cd5c5ee | 2012-08-12 19:03:51 -0700 | [diff] [blame] | 1731 | if (markViewed) { |
Vikram Aggarwal | cc754b1 | 2012-08-30 14:04:21 -0700 | [diff] [blame] | 1732 | target.markViewed(); |
Andy Huang | cd5c5ee | 2012-08-12 19:03:51 -0700 | [diff] [blame] | 1733 | } |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 1734 | } |
Scott Kennedy | 9e2d407 | 2013-03-21 21:46:01 -0700 | [diff] [blame] | 1735 | mConversationListCursor.updateBulkValues(opList); |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 1736 | } |
| 1737 | |
Andy Huang | 8f6b006 | 2012-07-31 15:36:31 -0700 | [diff] [blame] | 1738 | /** |
| 1739 | * Auto-advance to a different conversation if the currently visible conversation in |
| 1740 | * conversation mode is affected (deleted, marked unread, etc.). |
| 1741 | * |
Scott Kennedy | 0d0f8b0 | 2012-10-12 15:18:18 -0700 | [diff] [blame] | 1742 | * <p>Does nothing if outside of conversation mode.</p> |
Andy Huang | 8f6b006 | 2012-07-31 15:36:31 -0700 | [diff] [blame] | 1743 | * |
| 1744 | * @param target the set of conversations being deleted/marked unread |
| 1745 | */ |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 1746 | @Override |
Scott Kennedy | 0d0f8b0 | 2012-10-12 15:18:18 -0700 | [diff] [blame] | 1747 | public void showNextConversation(final Collection<Conversation> target) { |
Scott Kennedy | caaeed3 | 2013-06-12 13:39:16 -0700 | [diff] [blame] | 1748 | showNextConversation(target, null); |
Scott Kennedy | 0d0f8b0 | 2012-10-12 15:18:18 -0700 | [diff] [blame] | 1749 | } |
| 1750 | |
| 1751 | /** |
| 1752 | * Auto-advance to a different conversation if the currently visible conversation in |
| 1753 | * conversation mode is affected (deleted, marked unread, etc.). |
| 1754 | * |
| 1755 | * <p>Does nothing if outside of conversation mode.</p> |
Andy Huang | c94d07f | 2013-06-03 16:19:35 -0700 | [diff] [blame] | 1756 | * <p> |
| 1757 | * Clients may pass an operation to execute on the target that this method will run after |
| 1758 | * auto-advance is complete. The operation, if provided, may run immediately, or it may run |
| 1759 | * later, or not at all. Reasons it may run later include: |
| 1760 | * <ul> |
| 1761 | * <li>the auto-advance setting is uninitialized and we need to wait for the user to set it</li> |
| 1762 | * <li>auto-advance in this configuration requires a mode change, and we need to wait for the |
| 1763 | * mode change transition to finish</li> |
| 1764 | * </ul> |
| 1765 | * <p>If the current conversation is not in the target collection, this method will do nothing, |
| 1766 | * and will not execute the operation. |
Scott Kennedy | 0d0f8b0 | 2012-10-12 15:18:18 -0700 | [diff] [blame] | 1767 | * |
| 1768 | * @param target the set of conversations being deleted/marked unread |
Andy Huang | c94d07f | 2013-06-03 16:19:35 -0700 | [diff] [blame] | 1769 | * @param operation (optional) the operation to execute after advancing |
| 1770 | * @return <code>false</code> if this method handled or will execute the operation, |
| 1771 | * <code>true</code> otherwise. |
Scott Kennedy | 0d0f8b0 | 2012-10-12 15:18:18 -0700 | [diff] [blame] | 1772 | */ |
| 1773 | private boolean showNextConversation(final Collection<Conversation> target, |
Scott Kennedy | caaeed3 | 2013-06-12 13:39:16 -0700 | [diff] [blame] | 1774 | final Runnable operation) { |
Scott Kennedy | 8fb8e26 | 2012-11-28 15:48:03 -0800 | [diff] [blame] | 1775 | final int viewMode = mViewMode.getMode(); |
| 1776 | final boolean currentConversationInView = (viewMode == ViewMode.CONVERSATION |
| 1777 | || viewMode == ViewMode.SEARCH_RESULTS_CONVERSATION) |
Andy Huang | 8f6b006 | 2012-07-31 15:36:31 -0700 | [diff] [blame] | 1778 | && Conversation.contains(target, mCurrentConversation); |
Scott Kennedy | 0d0f8b0 | 2012-10-12 15:18:18 -0700 | [diff] [blame] | 1779 | |
Andy Huang | 8f6b006 | 2012-07-31 15:36:31 -0700 | [diff] [blame] | 1780 | if (currentConversationInView) { |
Scott Kennedy | 0d0f8b0 | 2012-10-12 15:18:18 -0700 | [diff] [blame] | 1781 | final int autoAdvanceSetting = mAccount.settings.getAutoAdvanceSetting(); |
| 1782 | |
Scott Kennedy | caaeed3 | 2013-06-12 13:39:16 -0700 | [diff] [blame] | 1783 | if (autoAdvanceSetting == AutoAdvance.UNSET && mIsTablet) { |
Scott Kennedy | 0d0f8b0 | 2012-10-12 15:18:18 -0700 | [diff] [blame] | 1784 | displayAutoAdvanceDialogAndPerformAction(operation); |
| 1785 | return false; |
| 1786 | } else { |
| 1787 | // If we don't have one set, but we're here, just take the default |
Vikram Aggarwal | 82d3750 | 2013-01-10 16:18:49 -0800 | [diff] [blame] | 1788 | final int autoAdvance = (autoAdvanceSetting == AutoAdvance.UNSET) ? |
| 1789 | AutoAdvance.DEFAULT : autoAdvanceSetting; |
Scott Kennedy | 0d0f8b0 | 2012-10-12 15:18:18 -0700 | [diff] [blame] | 1790 | |
| 1791 | final Conversation next = mTracker.getNextConversation(autoAdvance, target); |
| 1792 | LogUtils.d(LOG_TAG, "showNextConversation: showing %s next.", next); |
Andy Huang | c94d07f | 2013-06-03 16:19:35 -0700 | [diff] [blame] | 1793 | // Set mAutoAdvanceOp *before* showConversation() to ensure that it runs when the |
| 1794 | // transition doesn't run (i.e. it "completes" immediately). |
| 1795 | mAutoAdvanceOp = operation; |
Scott Kennedy | 0d0f8b0 | 2012-10-12 15:18:18 -0700 | [diff] [blame] | 1796 | showConversation(next); |
Andy Huang | c94d07f | 2013-06-03 16:19:35 -0700 | [diff] [blame] | 1797 | return (mAutoAdvanceOp == null); |
Scott Kennedy | 0d0f8b0 | 2012-10-12 15:18:18 -0700 | [diff] [blame] | 1798 | } |
Andy Huang | 8f6b006 | 2012-07-31 15:36:31 -0700 | [diff] [blame] | 1799 | } |
Scott Kennedy | 0d0f8b0 | 2012-10-12 15:18:18 -0700 | [diff] [blame] | 1800 | |
| 1801 | return true; |
| 1802 | } |
| 1803 | |
| 1804 | /** |
| 1805 | * Displays a the auto-advance dialog, and when the user makes a selection, the preference is |
| 1806 | * stored, and the specified operation is run. |
| 1807 | */ |
| 1808 | private void displayAutoAdvanceDialogAndPerformAction(final Runnable operation) { |
| 1809 | final String[] autoAdvanceDisplayOptions = |
| 1810 | mContext.getResources().getStringArray(R.array.prefEntries_autoAdvance); |
| 1811 | final String[] autoAdvanceOptionValues = |
| 1812 | mContext.getResources().getStringArray(R.array.prefValues_autoAdvance); |
| 1813 | |
| 1814 | final String defaultValue = mContext.getString(R.string.prefDefault_autoAdvance); |
| 1815 | int initialIndex = 0; |
| 1816 | for (int i = 0; i < autoAdvanceOptionValues.length; i++) { |
| 1817 | if (defaultValue.equals(autoAdvanceOptionValues[i])) { |
| 1818 | initialIndex = i; |
| 1819 | break; |
| 1820 | } |
| 1821 | } |
| 1822 | |
| 1823 | final DialogInterface.OnClickListener listClickListener = |
| 1824 | new DialogInterface.OnClickListener() { |
| 1825 | @Override |
| 1826 | public void onClick(DialogInterface dialog, int whichItem) { |
| 1827 | final String autoAdvanceValue = autoAdvanceOptionValues[whichItem]; |
| 1828 | final int autoAdvanceValueInt = |
| 1829 | UIProvider.AutoAdvance.getAutoAdvanceInt(autoAdvanceValue); |
| 1830 | mAccount.settings.setAutoAdvanceSetting(autoAdvanceValueInt); |
| 1831 | |
| 1832 | // Save the user's setting |
| 1833 | final ContentValues values = new ContentValues(1); |
| 1834 | values.put(AccountColumns.SettingsColumns.AUTO_ADVANCE, autoAdvanceValue); |
| 1835 | |
| 1836 | final ContentResolver resolver = mContext.getContentResolver(); |
| 1837 | resolver.update(mAccount.updateSettingsUri, values, null, null); |
| 1838 | |
| 1839 | // Dismiss the dialog, as clicking the items in the list doesn't close the |
| 1840 | // dialog. |
| 1841 | dialog.dismiss(); |
| 1842 | if (operation != null) { |
| 1843 | operation.run(); |
| 1844 | } |
| 1845 | } |
| 1846 | }; |
| 1847 | |
| 1848 | new AlertDialog.Builder(mActivity.getActivityContext()).setTitle( |
| 1849 | R.string.auto_advance_help_title) |
| 1850 | .setSingleChoiceItems(autoAdvanceDisplayOptions, initialIndex, listClickListener) |
| 1851 | .setPositiveButton(null, null) |
| 1852 | .create() |
| 1853 | .show(); |
Andy Huang | 8f6b006 | 2012-07-31 15:36:31 -0700 | [diff] [blame] | 1854 | } |
| 1855 | |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 1856 | @Override |
| 1857 | public void starMessage(ConversationMessage msg, boolean starred) { |
| 1858 | if (msg.starred == starred) { |
| 1859 | return; |
| 1860 | } |
| 1861 | |
| 1862 | msg.starred = starred; |
| 1863 | |
| 1864 | // locally propagate the change to the owning conversation |
| 1865 | // (figure the provider will properly propagate the change when it commits it) |
| 1866 | // |
| 1867 | // when unstarring, only propagate the change if this was the only message starred |
| 1868 | final boolean conversationStarred = starred || msg.isConversationStarred(); |
Andy Huang | cd12e82 | 2012-11-08 19:50:57 -0800 | [diff] [blame] | 1869 | final Conversation conv = msg.getConversation(); |
| 1870 | if (conversationStarred != conv.starred) { |
| 1871 | conv.starred = conversationStarred; |
| 1872 | mConversationListCursor.setConversationColumn(conv.uri, |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 1873 | ConversationColumns.STARRED, conversationStarred); |
| 1874 | } |
| 1875 | |
| 1876 | final ContentValues values = new ContentValues(1); |
| 1877 | values.put(UIProvider.MessageColumns.STARRED, starred ? 1 : 0); |
| 1878 | |
| 1879 | new ContentProviderTask.UpdateTask() { |
| 1880 | @Override |
| 1881 | protected void onPostExecute(Result result) { |
| 1882 | // TODO: handle errors? |
| 1883 | } |
| 1884 | }.run(mResolver, msg.uri, values, null /* selection*/, null /* selectionArgs */); |
| 1885 | } |
| 1886 | |
Andy Huang | 12b3ee4 | 2013-04-24 22:49:43 -0700 | [diff] [blame] | 1887 | @Override |
Alice Yang | 486e63e | 2013-04-05 13:01:50 -0700 | [diff] [blame] | 1888 | public void requestFolderRefresh() { |
Alice Yang | 37dda44 | 2013-03-26 22:48:53 -0700 | [diff] [blame] | 1889 | if (mFolder == null) { |
| 1890 | return; |
Mindy Pereira | 28e0c34 | 2012-02-17 15:05:13 -0800 | [diff] [blame] | 1891 | } |
Alice Yang | 37dda44 | 2013-03-26 22:48:53 -0700 | [diff] [blame] | 1892 | final ConversationListFragment convList = getConversationListFragment(); |
| 1893 | if (convList == null) { |
| 1894 | // This could happen if this account is in initial sync (user |
| 1895 | // is seeing the "your mail will appear shortly" message) |
| 1896 | return; |
| 1897 | } |
| 1898 | convList.showSyncStatusBar(); |
| 1899 | |
| 1900 | if (mAsyncRefreshTask != null) { |
| 1901 | mAsyncRefreshTask.cancel(true); |
| 1902 | } |
| 1903 | mAsyncRefreshTask = new AsyncRefreshTask(mContext, mFolder.refreshUri); |
| 1904 | mAsyncRefreshTask.execute(); |
Mindy Pereira | 28e0c34 | 2012-02-17 15:05:13 -0800 | [diff] [blame] | 1905 | } |
| 1906 | |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 1907 | /** |
| 1908 | * Confirm (based on user's settings) and delete a conversation from the conversation list and |
| 1909 | * from the database. |
Vikram Aggarwal | b8c3171 | 2013-01-03 17:03:19 -0800 | [diff] [blame] | 1910 | * @param actionId the ID of the menu item that caused the delete: R.id.delete, R.id.archive... |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 1911 | * @param target the conversations to act upon |
| 1912 | * @param showDialog true if a confirmation dialog is to be shown, false otherwise. |
| 1913 | * @param confirmResource the resource ID of the string that is shown in the confirmation dialog |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 1914 | */ |
Vikram Aggarwal | b8c3171 | 2013-01-03 17:03:19 -0800 | [diff] [blame] | 1915 | private void confirmAndDelete(int actionId, final Collection<Conversation> target, |
| 1916 | boolean showDialog, int confirmResource) { |
Vikram Aggarwal | 84fe994 | 2013-04-17 11:20:58 -0700 | [diff] [blame] | 1917 | final boolean isBatch = false; |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 1918 | if (showDialog) { |
Vikram Aggarwal | 84fe994 | 2013-04-17 11:20:58 -0700 | [diff] [blame] | 1919 | makeDialogListener(actionId, isBatch); |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 1920 | final CharSequence message = Utils.formatPlural(mContext, confirmResource, |
| 1921 | target.size()); |
Vikram Aggarwal | b8c3171 | 2013-01-03 17:03:19 -0800 | [diff] [blame] | 1922 | final ConfirmDialogFragment c = ConfirmDialogFragment.newInstance(message); |
| 1923 | c.displayDialog(mActivity.getFragmentManager()); |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 1924 | } else { |
Scott Kennedy | caaeed3 | 2013-06-12 13:39:16 -0700 | [diff] [blame] | 1925 | delete(0, target, getDeferredAction(actionId, target, isBatch), isBatch); |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 1926 | } |
| 1927 | } |
| 1928 | |
Vikram Aggarwal | 4f4782b | 2012-05-30 08:39:09 -0700 | [diff] [blame] | 1929 | @Override |
Scott Kennedy | 0d0f8b0 | 2012-10-12 15:18:18 -0700 | [diff] [blame] | 1930 | public void delete(final int actionId, final Collection<Conversation> target, |
Scott Kennedy | caaeed3 | 2013-06-12 13:39:16 -0700 | [diff] [blame] | 1931 | final DestructiveAction action, final boolean isBatch) { |
mindyp | 84f7d32 | 2012-10-01 17:14:40 -0700 | [diff] [blame] | 1932 | // Order of events is critical! The Conversation View Fragment must be |
| 1933 | // notified of the next conversation with showConversation(next) *before* the |
| 1934 | // conversation list |
Vikram Aggarwal | 192fac1 | 2012-07-25 16:44:55 -0700 | [diff] [blame] | 1935 | // fragment has a chance to delete the conversation, animating it away. |
| 1936 | |
mindyp | 84f7d32 | 2012-10-01 17:14:40 -0700 | [diff] [blame] | 1937 | // Update the conversation fragment if the current conversation is |
| 1938 | // deleted. |
Scott Kennedy | 0d0f8b0 | 2012-10-12 15:18:18 -0700 | [diff] [blame] | 1939 | final Runnable operation = new Runnable() { |
| 1940 | @Override |
| 1941 | public void run() { |
Scott Kennedy | caaeed3 | 2013-06-12 13:39:16 -0700 | [diff] [blame] | 1942 | delete(actionId, target, action, isBatch); |
Scott Kennedy | 0d0f8b0 | 2012-10-12 15:18:18 -0700 | [diff] [blame] | 1943 | } |
| 1944 | }; |
| 1945 | |
Scott Kennedy | caaeed3 | 2013-06-12 13:39:16 -0700 | [diff] [blame] | 1946 | if (!showNextConversation(target, operation)) { |
Scott Kennedy | 0d0f8b0 | 2012-10-12 15:18:18 -0700 | [diff] [blame] | 1947 | // This method will be called again if the user selects an autoadvance option |
| 1948 | return; |
| 1949 | } |
Vikram Aggarwal | ab1b5b6 | 2013-04-16 15:45:50 -0700 | [diff] [blame] | 1950 | // If the conversation is in the selected set, remove it from the set. |
Vikram Aggarwal | 84fe994 | 2013-04-17 11:20:58 -0700 | [diff] [blame] | 1951 | // Batch selections are cleared in the end of the action, so not done for batch actions. |
| 1952 | if (!isBatch) { |
| 1953 | for (final Conversation conv : target) { |
| 1954 | if (mSelectedSet.contains(conv)) { |
Vikram Aggarwal | 97ae784 | 2013-04-22 16:29:12 -0700 | [diff] [blame] | 1955 | mSelectedSet.toggle(conv); |
Vikram Aggarwal | 84fe994 | 2013-04-17 11:20:58 -0700 | [diff] [blame] | 1956 | } |
Vikram Aggarwal | ab1b5b6 | 2013-04-16 15:45:50 -0700 | [diff] [blame] | 1957 | } |
| 1958 | } |
Vikram Aggarwal | 192fac1 | 2012-07-25 16:44:55 -0700 | [diff] [blame] | 1959 | // The conversation list deletes and performs the action if it exists. |
| 1960 | final ConversationListFragment convListFragment = getConversationListFragment(); |
| 1961 | if (convListFragment != null) { |
Alice Yang | 193e05a | 2013-05-05 14:12:08 -0700 | [diff] [blame] | 1962 | LogUtils.i(LOG_TAG, "AAC.requestDelete: ListFragment is handling delete."); |
Vikram Aggarwal | 669947b | 2013-01-10 17:05:56 -0800 | [diff] [blame] | 1963 | convListFragment.requestDelete(actionId, target, action); |
Vikram Aggarwal | 192fac1 | 2012-07-25 16:44:55 -0700 | [diff] [blame] | 1964 | return; |
| 1965 | } |
mindyp | 84f7d32 | 2012-10-01 17:14:40 -0700 | [diff] [blame] | 1966 | // No visible UI element handled it on our behalf. Perform the action |
| 1967 | // ourself. |
Alice Yang | 193e05a | 2013-05-05 14:12:08 -0700 | [diff] [blame] | 1968 | LogUtils.i(LOG_TAG, "ACC.requestDelete: performing remove action ourselves"); |
Vikram Aggarwal | d503df4 | 2012-05-11 10:13:35 -0700 | [diff] [blame] | 1969 | action.performAction(); |
| 1970 | } |
| 1971 | |
| 1972 | /** |
| 1973 | * Requests that the action be performed and the UI state is updated to reflect the new change. |
Vikram Aggarwal | 715f83d | 2013-03-05 17:04:56 -0800 | [diff] [blame] | 1974 | * @param action the action to be performed, specified as a menu id: R.id.archive, ... |
Vikram Aggarwal | d503df4 | 2012-05-11 10:13:35 -0700 | [diff] [blame] | 1975 | */ |
Vikram Aggarwal | 715f83d | 2013-03-05 17:04:56 -0800 | [diff] [blame] | 1976 | private void requestUpdate(final DestructiveAction action) { |
Vikram Aggarwal | d503df4 | 2012-05-11 10:13:35 -0700 | [diff] [blame] | 1977 | action.performAction(); |
| 1978 | refreshConversationList(); |
Vikram Aggarwal | 75daee5 | 2012-04-30 11:13:09 -0700 | [diff] [blame] | 1979 | } |
Vikram Aggarwal | 6fbc87a | 2012-03-15 15:24:00 -0700 | [diff] [blame] | 1980 | |
| 1981 | @Override |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1982 | public void onPrepareDialog(int id, Dialog dialog, Bundle bundle) { |
| 1983 | // TODO(viki): Auto-generated method stub |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1984 | } |
| 1985 | |
| 1986 | @Override |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1987 | public boolean onPrepareOptionsMenu(Menu menu) { |
Andy Huang | d736a38 | 2012-08-29 13:08:58 -0700 | [diff] [blame] | 1988 | return mActionBarView.onPrepareOptionsMenu(menu); |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1989 | } |
| 1990 | |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 1991 | @Override |
| 1992 | public void onPause() { |
Vikram Aggarwal | c04cf7e | 2013-05-13 15:38:42 -0700 | [diff] [blame] | 1993 | mHaveAccountList = false; |
Paul Westbrook | 6ead20d | 2012-03-19 14:48:14 -0700 | [diff] [blame] | 1994 | enableNotifications(); |
Paul Westbrook | 94e440d | 2012-02-24 11:03:47 -0800 | [diff] [blame] | 1995 | } |
| 1996 | |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1997 | @Override |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 1998 | public void onResume() { |
Paul Westbrook | 6ead20d | 2012-03-19 14:48:14 -0700 | [diff] [blame] | 1999 | // Register the receiver that will prevent the status receiver from |
| 2000 | // displaying its notification icon as long as we're running. |
| 2001 | // The SupressNotificationReceiver will block the broadcast if we're looking at the folder |
| 2002 | // that the notification was received for. |
| 2003 | disableNotifications(); |
Andy Huang | 1ee96b2 | 2012-08-24 20:19:53 -0700 | [diff] [blame] | 2004 | |
| 2005 | mSafeToModifyFragments = true; |
Andrew Sapperstein | ed5b52d | 2013-04-30 13:40:18 -0700 | [diff] [blame] | 2006 | |
| 2007 | attachEmptyFolderDialogFragmentListener(); |
Andrew Sapperstein | 23e3313 | 2013-05-07 18:26:49 -0700 | [diff] [blame] | 2008 | |
| 2009 | // Invalidating the options menu so that when we make changes in settings, |
| 2010 | // the changes will always be updated in the action bar/options menu/ |
| 2011 | mActivity.invalidateOptionsMenu(); |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 2012 | } |
| 2013 | |
| 2014 | @Override |
| 2015 | public void onSaveInstanceState(Bundle outState) { |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame] | 2016 | mViewMode.handleSaveInstanceState(outState); |
Vikram Aggarwal | 6c51158 | 2012-02-27 10:59:47 -0800 | [diff] [blame] | 2017 | if (mAccount != null) { |
Vikram Aggarwal | 6c51158 | 2012-02-27 10:59:47 -0800 | [diff] [blame] | 2018 | outState.putParcelable(SAVED_ACCOUNT, mAccount); |
| 2019 | } |
Mindy Pereira | 5e478d2 | 2012-03-26 18:04:58 -0700 | [diff] [blame] | 2020 | if (mFolder != null) { |
| 2021 | outState.putParcelable(SAVED_FOLDER, mFolder); |
Vikram Aggarwal | 8b15263 | 2012-02-03 14:58:45 -0800 | [diff] [blame] | 2022 | } |
Vikram Aggarwal | f334140 | 2012-08-07 10:09:38 -0700 | [diff] [blame] | 2023 | // If this is a search activity, let's store the search query term as well. |
| 2024 | if (ConversationListContext.isSearchResult(mConvListContext)) { |
| 2025 | outState.putString(SAVED_QUERY, mConvListContext.searchQuery); |
| 2026 | } |
Vikram Aggarwal | 49e0e99 | 2012-09-21 13:53:15 -0700 | [diff] [blame] | 2027 | if (mCurrentConversation != null && mViewMode.isConversationMode()) { |
Mindy Pereira | 26f23fc | 2012-03-27 10:26:04 -0700 | [diff] [blame] | 2028 | outState.putParcelable(SAVED_CONVERSATION, mCurrentConversation); |
| 2029 | } |
Andy Huang | 4556a44 | 2012-03-30 16:42:05 -0700 | [diff] [blame] | 2030 | if (!mSelectedSet.isEmpty()) { |
Vikram Aggarwal | cabd3f2 | 2012-04-19 10:14:41 -0700 | [diff] [blame] | 2031 | outState.putParcelable(SAVED_SELECTED_SET, mSelectedSet); |
Andy Huang | 4556a44 | 2012-03-30 16:42:05 -0700 | [diff] [blame] | 2032 | } |
Mindy Pereira | d3367499 | 2012-06-25 16:26:30 -0700 | [diff] [blame] | 2033 | if (mToastBar.getVisibility() == View.VISIBLE) { |
| 2034 | outState.putParcelable(SAVED_TOAST_BAR_OP, mToastBar.getOperation()); |
| 2035 | } |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame] | 2036 | final ConversationListFragment convListFragment = getConversationListFragment(); |
Mindy Pereira | d3367499 | 2012-06-25 16:26:30 -0700 | [diff] [blame] | 2037 | if (convListFragment != null) { |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame] | 2038 | convListFragment.getAnimatedAdapter().onSaveInstanceState(outState); |
Mindy Pereira | d3367499 | 2012-06-25 16:26:30 -0700 | [diff] [blame] | 2039 | } |
Vikram Aggarwal | b8c3171 | 2013-01-03 17:03:19 -0800 | [diff] [blame] | 2040 | // If there is a dialog being shown, save the state so we can create a listener for it. |
Vikram Aggarwal | 6cadbfc | 2012-12-27 09:17:05 -0800 | [diff] [blame] | 2041 | if (mDialogAction != -1) { |
| 2042 | outState.putInt(SAVED_ACTION, mDialogAction); |
Vikram Aggarwal | b8c3171 | 2013-01-03 17:03:19 -0800 | [diff] [blame] | 2043 | outState.putBoolean(SAVED_ACTION_FROM_SELECTED, mDialogFromSelectedSet); |
Vikram Aggarwal | 6cadbfc | 2012-12-27 09:17:05 -0800 | [diff] [blame] | 2044 | } |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame] | 2045 | if (mDetachedConvUri != null) { |
| 2046 | outState.putParcelable(SAVED_DETACHED_CONV_URI, mDetachedConvUri); |
| 2047 | } |
Andrew Sapperstein | 5747e15 | 2013-05-13 14:13:08 -0700 | [diff] [blame] | 2048 | |
Scott Kennedy | b10212e | 2013-02-22 16:27:00 -0800 | [diff] [blame] | 2049 | outState.putParcelable(SAVED_HIERARCHICAL_FOLDER, mFolderListFolder); |
Andrew Sapperstein | 5747e15 | 2013-05-13 14:13:08 -0700 | [diff] [blame] | 2050 | mSafeToModifyFragments = false; |
Scott Kennedy | ad41814 | 2013-07-10 17:18:22 -0700 | [diff] [blame] | 2051 | |
| 2052 | outState.putParcelable(SAVED_INBOX_KEY, mInbox); |
Scott Kennedy | f77806e | 2013-08-30 11:38:15 -0700 | [diff] [blame^] | 2053 | |
| 2054 | outState.putBundle(SAVED_CONVERSATION_LIST_SCROLL_POSITIONS, |
| 2055 | mConversationListScrollPositions); |
Andy Huang | 1ee96b2 | 2012-08-24 20:19:53 -0700 | [diff] [blame] | 2056 | } |
| 2057 | |
| 2058 | /** |
| 2059 | * @see #mSafeToModifyFragments |
| 2060 | */ |
| 2061 | protected boolean safeToModifyFragments() { |
| 2062 | return mSafeToModifyFragments; |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 2063 | } |
| 2064 | |
| 2065 | @Override |
Andy Huang | 313ac13 | 2013-03-04 23:40:58 -0800 | [diff] [blame] | 2066 | public void executeSearch(String query) { |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 2067 | Intent intent = new Intent(); |
| 2068 | intent.setAction(Intent.ACTION_SEARCH); |
| 2069 | intent.putExtra(ConversationListContext.EXTRA_SEARCH_QUERY, query); |
| 2070 | intent.putExtra(Utils.EXTRA_ACCOUNT, mAccount); |
| 2071 | intent.setComponent(mActivity.getComponentName()); |
Vikram Aggarwal | b17cbc0 | 2012-04-06 15:41:46 -0700 | [diff] [blame] | 2072 | mActionBarView.collapseSearch(); |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 2073 | mActivity.startActivity(intent); |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 2074 | } |
| 2075 | |
| 2076 | @Override |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 2077 | public void onStop() { |
Scott Kennedy | cb85aea | 2013-02-25 13:08:32 -0800 | [diff] [blame] | 2078 | NotificationActionUtils.unregisterUndoNotificationObserver(mUndoNotificationObserver); |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 2079 | } |
| 2080 | |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 2081 | @Override |
| 2082 | public void onDestroy() { |
Andy Huang | b2ef9c1 | 2012-12-18 12:58:41 -0600 | [diff] [blame] | 2083 | // stop listening to the cursor on e.g. configuration changes |
| 2084 | if (mConversationListCursor != null) { |
| 2085 | mConversationListCursor.removeListener(this); |
| 2086 | } |
Andy Huang | 144bfe7 | 2013-06-11 13:27:52 -0700 | [diff] [blame] | 2087 | mDrawIdler.setListener(null); |
| 2088 | mDrawIdler.setRootView(null); |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 2089 | // unregister the ViewPager's observer on the conversation cursor |
| 2090 | mPagerController.onDestroy(); |
Mindy Pereira | 641de65 | 2012-08-02 15:21:50 -0700 | [diff] [blame] | 2091 | mActionBarView.onDestroy(); |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 2092 | mRecentFolderList.destroy(); |
Andy Huang | 4e0158f | 2012-08-07 21:06:01 -0700 | [diff] [blame] | 2093 | mDestroyed = true; |
Vikram Aggarwal | 59f741f | 2013-03-01 15:55:40 -0800 | [diff] [blame] | 2094 | mHandler.removeCallbacks(mLogServiceChecker); |
| 2095 | mLogServiceChecker = null; |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 2096 | } |
| 2097 | |
Vikram Aggarwal | fa131a2 | 2012-02-02 13:56:22 -0800 | [diff] [blame] | 2098 | /** |
Vikram Aggarwal | 93dc202 | 2012-11-05 13:36:57 -0800 | [diff] [blame] | 2099 | * Set the Action Bar icon according to the mode. The Action Bar icon can contain a back button |
| 2100 | * or not. The individual controller is responsible for changing the icon based on the mode. |
| 2101 | */ |
| 2102 | protected abstract void resetActionBarIcon(); |
| 2103 | |
| 2104 | /** |
Mindy Pereira | 161f50d | 2012-02-28 15:47:19 -0800 | [diff] [blame] | 2105 | * {@inheritDoc} Subclasses must override this to listen to mode changes |
| 2106 | * from the ViewMode. Subclasses <b>must</b> call the parent's |
| 2107 | * onViewModeChanged since the parent will handle common state changes. |
Vikram Aggarwal | fa131a2 | 2012-02-02 13:56:22 -0800 | [diff] [blame] | 2108 | */ |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 2109 | @Override |
Vikram Aggarwal | fa131a2 | 2012-02-02 13:56:22 -0800 | [diff] [blame] | 2110 | public void onViewModeChanged(int newMode) { |
Vikram Aggarwal | 49e0e99 | 2012-09-21 13:53:15 -0700 | [diff] [blame] | 2111 | // When we step away from the conversation mode, we don't have a current conversation |
| 2112 | // anymore. Let's blank it out so clients calling getCurrentConversation are not misled. |
| 2113 | if (!ViewMode.isConversationMode(newMode)) { |
| 2114 | setCurrentConversation(null); |
| 2115 | } |
Andrew Sapperstein | 6c570db | 2013-08-06 17:21:36 -0700 | [diff] [blame] | 2116 | |
Vikram Aggarwal | 93dc202 | 2012-11-05 13:36:57 -0800 | [diff] [blame] | 2117 | // If the viewmode is not set, preserve existing icon. |
| 2118 | if (newMode != ViewMode.UNKNOWN) { |
| 2119 | resetActionBarIcon(); |
| 2120 | } |
Andy Huang | 12b3ee4 | 2013-04-24 22:49:43 -0700 | [diff] [blame] | 2121 | |
| 2122 | if (isDrawerEnabled()) { |
Vikram Aggarwal | dfbc698 | 2013-07-03 14:39:31 -0700 | [diff] [blame] | 2123 | /** If the folder doesn't exist, or its parent URI is empty, |
| 2124 | * this is not a child folder */ |
| 2125 | final boolean isTopLevel = (mFolder == null) || (mFolder.parent == Uri.EMPTY); |
| 2126 | mDrawerToggle.setDrawerIndicatorEnabled( |
| 2127 | getShouldShowDrawerIndicator(newMode, isTopLevel)); |
Scott Kennedy | 2b254dc | 2013-07-10 11:40:27 -0700 | [diff] [blame] | 2128 | mDrawerContainer.setDrawerLockMode(getShouldAllowDrawerPull(newMode) |
Scott Kennedy | 8a72b85 | 2013-05-02 14:18:50 -0700 | [diff] [blame] | 2129 | ? DrawerLayout.LOCK_MODE_UNLOCKED : DrawerLayout.LOCK_MODE_LOCKED_CLOSED); |
Andy Huang | 12b3ee4 | 2013-04-24 22:49:43 -0700 | [diff] [blame] | 2130 | closeDrawerIfOpen(); |
| 2131 | } |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 2132 | } |
| 2133 | |
Vikram Aggarwal | dfbc698 | 2013-07-03 14:39:31 -0700 | [diff] [blame] | 2134 | /** |
| 2135 | * Returns true if the drawer icon is shown |
| 2136 | * @param viewMode the current view mode |
| 2137 | * @param isTopLevel true if the current folder is not a child |
| 2138 | * @return whether the drawer indicator is shown |
| 2139 | */ |
Scott Kennedy | 81b5fa0 | 2013-07-11 14:39:18 -0700 | [diff] [blame] | 2140 | private boolean getShouldShowDrawerIndicator(final int viewMode, |
Vikram Aggarwal | dfbc698 | 2013-07-03 14:39:31 -0700 | [diff] [blame] | 2141 | final boolean isTopLevel) { |
| 2142 | // If search list/conv mode: disable indicator |
| 2143 | // Indicator is enabled either in conversation list or folder list mode. |
Scott Kennedy | 81b5fa0 | 2013-07-11 14:39:18 -0700 | [diff] [blame] | 2144 | return isDrawerEnabled() && !ViewMode.isSearchMode(viewMode) |
Scott Kennedy | aded578 | 2013-07-16 14:21:53 -0700 | [diff] [blame] | 2145 | && (viewMode == ViewMode.CONVERSATION_LIST && isTopLevel); |
Scott Kennedy | 8a72b85 | 2013-05-02 14:18:50 -0700 | [diff] [blame] | 2146 | } |
| 2147 | |
Vikram Aggarwal | dfbc698 | 2013-07-03 14:39:31 -0700 | [diff] [blame] | 2148 | /** |
| 2149 | * Returns true if the left-screen swipe action (or Home icon tap) should pull a drawer out. |
| 2150 | * @param viewMode the current view mode. |
Vikram Aggarwal | dfbc698 | 2013-07-03 14:39:31 -0700 | [diff] [blame] | 2151 | * @return whether the drawer can be opened using a swipe action or action bar tap. |
| 2152 | */ |
Scott Kennedy | 2b254dc | 2013-07-10 11:40:27 -0700 | [diff] [blame] | 2153 | private static boolean getShouldAllowDrawerPull(final int viewMode) { |
Scott Kennedy | 8a72b85 | 2013-05-02 14:18:50 -0700 | [diff] [blame] | 2154 | // if search list/conv mode, disable drawer pull |
| 2155 | // allow drawer pull everywhere except conversation mode where the list is hidden |
Andrew Sapperstein | bc5e0dc | 2013-08-07 21:15:22 -0700 | [diff] [blame] | 2156 | return !ViewMode.isSearchMode(viewMode) && !ViewMode.isConversationMode(viewMode) && |
| 2157 | !ViewMode.isAdMode(viewMode); |
Vikram Aggarwal | dfbc698 | 2013-07-03 14:39:31 -0700 | [diff] [blame] | 2158 | |
| 2159 | // TODO(ath): get this to work to allow drawer pull in 2-pane conv mode. |
| 2160 | /* && !isConversationListVisible() */ |
Scott Kennedy | 8a72b85 | 2013-05-02 14:18:50 -0700 | [diff] [blame] | 2161 | } |
| 2162 | |
Andy Huang | 3825f3d | 2012-08-29 16:44:12 -0700 | [diff] [blame] | 2163 | public void disablePagerUpdates() { |
| 2164 | mPagerController.stopListening(); |
| 2165 | } |
| 2166 | |
Andy Huang | 4e0158f | 2012-08-07 21:06:01 -0700 | [diff] [blame] | 2167 | public boolean isDestroyed() { |
| 2168 | return mDestroyed; |
| 2169 | } |
| 2170 | |
mindyp | 54f120f | 2012-08-28 13:10:33 -0700 | [diff] [blame] | 2171 | @Override |
| 2172 | public void commitDestructiveActions(boolean animate) { |
mindyp | c6adce3 | 2012-08-22 18:46:42 -0700 | [diff] [blame] | 2173 | ConversationListFragment fragment = getConversationListFragment(); |
Mindy Pereira | 1e2573b | 2012-04-17 14:34:36 -0700 | [diff] [blame] | 2174 | if (fragment != null) { |
mindyp | c6adce3 | 2012-08-22 18:46:42 -0700 | [diff] [blame] | 2175 | fragment.commitDestructiveActions(animate); |
Mindy Pereira | 1e2573b | 2012-04-17 14:34:36 -0700 | [diff] [blame] | 2176 | } |
| 2177 | } |
| 2178 | |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 2179 | @Override |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 2180 | public void onWindowFocusChanged(boolean hasFocus) { |
Vikram Aggarwal | 69b5c30 | 2012-09-05 11:11:13 -0700 | [diff] [blame] | 2181 | final ConversationListFragment convList = getConversationListFragment(); |
Vikram Aggarwal | 6422b8d | 2013-01-14 10:47:41 -0800 | [diff] [blame] | 2182 | // hasFocus already ensures that the window is in focus, so we don't need to call |
| 2183 | // AAC.isFragmentVisible(convList) here. |
Paul Westbrook | 9f119c7 | 2012-04-24 16:10:59 -0700 | [diff] [blame] | 2184 | if (hasFocus && convList != null && convList.isVisible()) { |
| 2185 | // The conversation list is visible. |
Vikram Aggarwal | 69b5c30 | 2012-09-05 11:11:13 -0700 | [diff] [blame] | 2186 | informCursorVisiblity(true); |
Paul Westbrook | 9f119c7 | 2012-04-24 16:10:59 -0700 | [diff] [blame] | 2187 | } |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 2188 | } |
| 2189 | |
Vikram Aggarwal | ca716f1 | 2012-08-20 11:11:48 -0700 | [diff] [blame] | 2190 | /** |
| 2191 | * Set the account, and carry out all the account-related changes that rely on this. |
Vikram Aggarwal | 715f83d | 2013-03-05 17:04:56 -0800 | [diff] [blame] | 2192 | * @param account new account to set to. |
Vikram Aggarwal | ca716f1 | 2012-08-20 11:11:48 -0700 | [diff] [blame] | 2193 | */ |
Mindy Pereira | 75181e8 | 2012-04-18 08:17:13 -0700 | [diff] [blame] | 2194 | private void setAccount(Account account) { |
Andy Huang | b9ca979 | 2012-05-18 15:31:49 -0700 | [diff] [blame] | 2195 | if (account == null) { |
| 2196 | LogUtils.w(LOG_TAG, new Error(), |
| 2197 | "AAC ignoring null (presumably invalid) account restoration"); |
| 2198 | return; |
| 2199 | } |
Andy Huang | b114841 | 2012-05-19 00:16:30 -0700 | [diff] [blame] | 2200 | LogUtils.d(LOG_TAG, "AbstractActivityController.setAccount(): account = %s", account.uri); |
Vikram Aggarwal | 91e8737 | 2012-05-18 15:36:04 -0700 | [diff] [blame] | 2201 | mAccount = account; |
Vikram Aggarwal | ca716f1 | 2012-08-20 11:11:48 -0700 | [diff] [blame] | 2202 | // Only change AAC state here. Do *not* modify any other object's state. The object |
| 2203 | // should listen on account changes. |
Vikram Aggarwal | 177097f | 2013-03-08 11:19:53 -0800 | [diff] [blame] | 2204 | restartOptionalLoader(LOADER_RECENT_FOLDERS, mFolderCallbacks, Bundle.EMPTY); |
Vikram Aggarwal | ca716f1 | 2012-08-20 11:11:48 -0700 | [diff] [blame] | 2205 | mActivity.invalidateOptionsMenu(); |
| 2206 | disableNotificationsOnAccountChange(mAccount); |
Vikram Aggarwal | 177097f | 2013-03-08 11:19:53 -0800 | [diff] [blame] | 2207 | restartOptionalLoader(LOADER_ACCOUNT_UPDATE_CURSOR, mAccountCallbacks, Bundle.EMPTY); |
Vikram Aggarwal | 2fe343f | 2013-01-14 09:00:25 -0800 | [diff] [blame] | 2208 | // The Mail instance can be null during test runs. |
| 2209 | final MailAppProvider instance = MailAppProvider.getInstance(); |
| 2210 | if (instance != null) { |
| 2211 | instance.setLastViewedAccount(mAccount.uri.toString()); |
| 2212 | } |
Vikram Aggarwal | 91e8737 | 2012-05-18 15:36:04 -0700 | [diff] [blame] | 2213 | if (account.settings == null) { |
| 2214 | LogUtils.w(LOG_TAG, new Error(), "AAC ignoring account with null settings."); |
| 2215 | return; |
| 2216 | } |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 2217 | mAccountObservers.notifyChanged(); |
Vikram Aggarwal | 34f7b23 | 2012-10-17 13:32:23 -0700 | [diff] [blame] | 2218 | perhapsEnterWaitMode(); |
Mindy Pereira | 75181e8 | 2012-04-18 08:17:13 -0700 | [diff] [blame] | 2219 | } |
| 2220 | |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 2221 | /** |
Mindy Pereira | 161f50d | 2012-02-28 15:47:19 -0800 | [diff] [blame] | 2222 | * Restore the state from the previous bundle. Subclasses should call this |
| 2223 | * method from the parent class, since it performs important UI |
| 2224 | * initialization. |
| 2225 | * |
Vikram Aggarwal | 715f83d | 2013-03-05 17:04:56 -0800 | [diff] [blame] | 2226 | * @param savedState previous state |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 2227 | */ |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 2228 | @Override |
| 2229 | public void onRestoreInstanceState(Bundle savedState) { |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame] | 2230 | mDetachedConvUri = savedState.getParcelable(SAVED_DETACHED_CONV_URI); |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 2231 | if (savedState.containsKey(SAVED_CONVERSATION)) { |
| 2232 | // Open the conversation. |
Andy Huang | 2bc8bc1 | 2012-11-12 17:24:25 -0800 | [diff] [blame] | 2233 | final Conversation conversation = savedState.getParcelable(SAVED_CONVERSATION); |
Paul Westbrook | 534e4a2 | 2012-04-25 03:46:29 -0700 | [diff] [blame] | 2234 | if (conversation != null && conversation.position < 0) { |
| 2235 | // Set the position to 0 on this conversation, as we don't know where it is |
| 2236 | // in the list |
| 2237 | conversation.position = 0; |
| 2238 | } |
Andy Huang | ed4fdf0 | 2012-07-26 17:12:50 -0700 | [diff] [blame] | 2239 | showConversation(conversation); |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 2240 | } |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 2241 | |
Mindy Pereira | d3367499 | 2012-06-25 16:26:30 -0700 | [diff] [blame] | 2242 | if (savedState.containsKey(SAVED_TOAST_BAR_OP)) { |
Andy Huang | 2bc8bc1 | 2012-11-12 17:24:25 -0800 | [diff] [blame] | 2243 | ToastBarOperation op = savedState.getParcelable(SAVED_TOAST_BAR_OP); |
Mindy Pereira | d3367499 | 2012-06-25 16:26:30 -0700 | [diff] [blame] | 2244 | if (op != null) { |
| 2245 | if (op.getType() == ToastBarOperation.UNDO) { |
| 2246 | onUndoAvailable(op); |
Andrew Sapperstein | 9d7519d | 2012-07-16 14:03:53 -0700 | [diff] [blame] | 2247 | } else if (op.getType() == ToastBarOperation.ERROR) { |
| 2248 | onError(mFolder, true); |
Mindy Pereira | d3367499 | 2012-06-25 16:26:30 -0700 | [diff] [blame] | 2249 | } |
| 2250 | } |
| 2251 | } |
Scott Kennedy | b10212e | 2013-02-22 16:27:00 -0800 | [diff] [blame] | 2252 | mFolderListFolder = savedState.getParcelable(SAVED_HIERARCHICAL_FOLDER); |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame] | 2253 | final ConversationListFragment convListFragment = getConversationListFragment(); |
Mindy Pereira | d3367499 | 2012-06-25 16:26:30 -0700 | [diff] [blame] | 2254 | if (convListFragment != null) { |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame] | 2255 | convListFragment.getAnimatedAdapter().onRestoreInstanceState(savedState); |
Mindy Pereira | d3367499 | 2012-06-25 16:26:30 -0700 | [diff] [blame] | 2256 | } |
Vikram Aggarwal | b8c3171 | 2013-01-03 17:03:19 -0800 | [diff] [blame] | 2257 | /* |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 2258 | * Restore the state of selected conversations. This needs to be done after the correct mode |
| 2259 | * is set and the action bar is fully initialized. If not, several key pieces of state |
| 2260 | * information will be missing, and the split views may not be initialized correctly. |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 2261 | */ |
Andy Huang | 4556a44 | 2012-03-30 16:42:05 -0700 | [diff] [blame] | 2262 | restoreSelectedConversations(savedState); |
Vikram Aggarwal | b8c3171 | 2013-01-03 17:03:19 -0800 | [diff] [blame] | 2263 | // Order is important!!! |
| 2264 | // The dialog listener needs to happen *after* the selected set is restored. |
| 2265 | |
| 2266 | // If there has been an orientation change, and we need to recreate the listener for the |
| 2267 | // confirm dialog fragment (delete/archive/...), then do it here. |
| 2268 | if (mDialogAction != -1) { |
| 2269 | makeDialogListener(mDialogAction, mDialogFromSelectedSet); |
| 2270 | } |
Scott Kennedy | ad41814 | 2013-07-10 17:18:22 -0700 | [diff] [blame] | 2271 | |
| 2272 | mInbox = savedState.getParcelable(SAVED_INBOX_KEY); |
Scott Kennedy | f77806e | 2013-08-30 11:38:15 -0700 | [diff] [blame^] | 2273 | |
| 2274 | mConversationListScrollPositions.clear(); |
| 2275 | mConversationListScrollPositions.putAll( |
| 2276 | savedState.getBundle(SAVED_CONVERSATION_LIST_SCROLL_POSITIONS)); |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 2277 | } |
| 2278 | |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame] | 2279 | /** |
| 2280 | * Handle an intent to open the app. This method is called only when there is no saved state, |
| 2281 | * so we need to set state that wasn't set before. It is correct to change the viewmode here |
| 2282 | * since it has not been previously set. |
Vikram Aggarwal | 6aca689 | 2013-06-04 13:53:27 -0700 | [diff] [blame] | 2283 | * |
| 2284 | * This method is called for a subset of the reasons mentioned in |
| 2285 | * {@link #onCreate(android.os.Bundle)}. Notably, this is called when launching the app from |
| 2286 | * notifications, widgets, and shortcuts. |
Vikram Aggarwal | 715f83d | 2013-03-05 17:04:56 -0800 | [diff] [blame] | 2287 | * @param intent intent passed to the activity. |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame] | 2288 | */ |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 2289 | private void handleIntent(Intent intent) { |
Andy Huang | e645942 | 2013-04-01 16:32:18 -0700 | [diff] [blame] | 2290 | LogUtils.d(LOG_TAG, "IN AAC.handleIntent. action=%s", intent.getAction()); |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 2291 | if (Intent.ACTION_VIEW.equals(intent.getAction())) { |
| 2292 | if (intent.hasExtra(Utils.EXTRA_ACCOUNT)) { |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame] | 2293 | setAccount(Account.newinstance(intent.getStringExtra(Utils.EXTRA_ACCOUNT))); |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 2294 | } |
Andy Huang | b9ca979 | 2012-05-18 15:31:49 -0700 | [diff] [blame] | 2295 | if (mAccount == null) { |
| 2296 | return; |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 2297 | } |
Vikram Aggarwal | 1a249e0 | 2012-08-03 16:19:33 -0700 | [diff] [blame] | 2298 | final boolean isConversationMode = intent.hasExtra(Utils.EXTRA_CONVERSATION); |
Andy Huang | 4fe0af8 | 2013-08-20 17:24:51 -0700 | [diff] [blame] | 2299 | |
| 2300 | if (intent.getBooleanExtra(Utils.EXTRA_FROM_NOTIFICATION, false)) { |
| 2301 | Analytics.getInstance().setCustomDimension(Analytics.CD_INDEX_ACCOUNT_TYPE, |
| 2302 | AnalyticsUtils.getAccountTypeForAccount(mAccount.name)); |
| 2303 | Analytics.getInstance().sendEvent("notification_click", |
| 2304 | isConversationMode ? "conversation" : "conversation_list", null, 0); |
| 2305 | } |
| 2306 | |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame] | 2307 | if (isConversationMode && mViewMode.getMode() == ViewMode.UNKNOWN) { |
Vikram Aggarwal | 1a249e0 | 2012-08-03 16:19:33 -0700 | [diff] [blame] | 2308 | mViewMode.enterConversationMode(); |
| 2309 | } else { |
| 2310 | mViewMode.enterConversationListMode(); |
| 2311 | } |
Vikram Aggarwal | 177097f | 2013-03-08 11:19:53 -0800 | [diff] [blame] | 2312 | // Put the folder and conversation, and ask the loader to create this folder. |
| 2313 | final Bundle args = new Bundle(); |
Scott Kennedy | 48cfe46 | 2013-04-10 11:32:02 -0700 | [diff] [blame] | 2314 | |
| 2315 | final Uri folderUri; |
| 2316 | if (intent.hasExtra(Utils.EXTRA_FOLDER_URI)) { |
| 2317 | folderUri = (Uri) intent.getParcelableExtra(Utils.EXTRA_FOLDER_URI); |
| 2318 | } else if (intent.hasExtra(Utils.EXTRA_FOLDER)) { |
| 2319 | final Folder folder = |
| 2320 | Folder.fromString(intent.getStringExtra(Utils.EXTRA_FOLDER)); |
Scott Kennedy | 259df5b | 2013-07-11 13:24:01 -0700 | [diff] [blame] | 2321 | folderUri = folder.folderUri.fullUri; |
Scott Kennedy | 48cfe46 | 2013-04-10 11:32:02 -0700 | [diff] [blame] | 2322 | } else { |
Scott Kennedy | 72727ef | 2013-05-01 18:10:55 -0700 | [diff] [blame] | 2323 | final Bundle extras = intent.getExtras(); |
| 2324 | LogUtils.d(LOG_TAG, "Couldn't find a folder URI in the extras: %s", |
| 2325 | extras == null ? "null" : extras.toString()); |
Scott Kennedy | 48cfe46 | 2013-04-10 11:32:02 -0700 | [diff] [blame] | 2326 | folderUri = mAccount.settings.defaultInbox; |
| 2327 | } |
| 2328 | |
Scott Kennedy | 6059335 | 2013-03-13 13:45:30 -0700 | [diff] [blame] | 2329 | args.putParcelable(Utils.EXTRA_FOLDER_URI, folderUri); |
Vikram Aggarwal | 177097f | 2013-03-08 11:19:53 -0800 | [diff] [blame] | 2330 | args.putParcelable(Utils.EXTRA_CONVERSATION, |
| 2331 | intent.getParcelableExtra(Utils.EXTRA_CONVERSATION)); |
| 2332 | restartOptionalLoader(LOADER_FIRST_FOLDER, mFolderCallbacks, args); |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 2333 | } else if (Intent.ACTION_SEARCH.equals(intent.getAction())) { |
| 2334 | if (intent.hasExtra(Utils.EXTRA_ACCOUNT)) { |
Vikram Aggarwal | f0ef230 | 2012-11-07 14:53:34 -0800 | [diff] [blame] | 2335 | mHaveSearchResults = false; |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 2336 | // Save this search query for future suggestions. |
| 2337 | final String query = intent.getStringExtra(SearchManager.QUERY); |
| 2338 | final String authority = mContext.getString(R.string.suggestions_authority); |
Vikram Aggarwal | 2b703c6 | 2012-09-18 13:54:15 -0700 | [diff] [blame] | 2339 | final SearchRecentSuggestions suggestions = new SearchRecentSuggestions( |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 2340 | mContext, authority, SuggestionsProvider.MODE); |
| 2341 | suggestions.saveRecentQuery(query, null); |
Vikram Aggarwal | f0ef230 | 2012-11-07 14:53:34 -0800 | [diff] [blame] | 2342 | setAccount((Account) intent.getParcelableExtra(Utils.EXTRA_ACCOUNT)); |
| 2343 | fetchSearchFolder(intent); |
| 2344 | if (shouldEnterSearchConvMode()) { |
Mindy Pereira | ac25482 | 2012-06-18 10:46:43 -0700 | [diff] [blame] | 2345 | mViewMode.enterSearchResultsConversationMode(); |
| 2346 | } else { |
| 2347 | mViewMode.enterSearchResultsListMode(); |
| 2348 | } |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 2349 | } else { |
| 2350 | LogUtils.e(LOG_TAG, "Missing account extra from search intent. Finishing"); |
| 2351 | mActivity.finish(); |
| 2352 | } |
| 2353 | } |
| 2354 | if (mAccount != null) { |
Vikram Aggarwal | 177097f | 2013-03-08 11:19:53 -0800 | [diff] [blame] | 2355 | restartOptionalLoader(LOADER_ACCOUNT_UPDATE_CURSOR, mAccountCallbacks, Bundle.EMPTY); |
Scott Kennedy | b39aaf5 | 2013-03-06 19:17:22 -0800 | [diff] [blame] | 2356 | } |
| 2357 | } |
| 2358 | |
Andy Huang | 4556a44 | 2012-03-30 16:42:05 -0700 | [diff] [blame] | 2359 | /** |
Vikram Aggarwal | f0ef230 | 2012-11-07 14:53:34 -0800 | [diff] [blame] | 2360 | * Returns true if we should enter conversation mode with search. |
| 2361 | */ |
| 2362 | protected final boolean shouldEnterSearchConvMode() { |
| 2363 | return mHaveSearchResults && Utils.showTwoPaneSearchResults(mActivity.getActivityContext()); |
| 2364 | } |
| 2365 | |
| 2366 | /** |
Andy Huang | 4556a44 | 2012-03-30 16:42:05 -0700 | [diff] [blame] | 2367 | * Copy any selected conversations stored in the saved bundle into our selection set, |
| 2368 | * triggering {@link ConversationSetObserver} callbacks as our selection set changes. |
| 2369 | * |
| 2370 | */ |
Vikram Aggarwal | 715f83d | 2013-03-05 17:04:56 -0800 | [diff] [blame] | 2371 | private void restoreSelectedConversations(Bundle savedState) { |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 2372 | if (savedState == null) { |
Andy Huang | 4556a44 | 2012-03-30 16:42:05 -0700 | [diff] [blame] | 2373 | mSelectedSet.clear(); |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 2374 | return; |
| 2375 | } |
Vikram Aggarwal | cabd3f2 | 2012-04-19 10:14:41 -0700 | [diff] [blame] | 2376 | final ConversationSelectionSet selectedSet = savedState.getParcelable(SAVED_SELECTED_SET); |
Andy Huang | 4556a44 | 2012-03-30 16:42:05 -0700 | [diff] [blame] | 2377 | if (selectedSet == null || selectedSet.isEmpty()) { |
| 2378 | mSelectedSet.clear(); |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 2379 | return; |
| 2380 | } |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 2381 | |
| 2382 | // putAll will take care of calling our registered onSetPopulated method |
Vikram Aggarwal | cabd3f2 | 2012-04-19 10:14:41 -0700 | [diff] [blame] | 2383 | mSelectedSet.putAll(selectedSet); |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 2384 | } |
| 2385 | |
Vikram Aggarwal | 715f83d | 2013-03-05 17:04:56 -0800 | [diff] [blame] | 2386 | private void showConversation(Conversation conversation) { |
Andy Huang | 1ee96b2 | 2012-08-24 20:19:53 -0700 | [diff] [blame] | 2387 | showConversation(conversation, false /* inLoaderCallbacks */); |
| 2388 | } |
| 2389 | |
Vikram Aggarwal | ec5cbf7 | 2012-03-08 15:10:35 -0800 | [diff] [blame] | 2390 | /** |
Vikram Aggarwal | 49e0e99 | 2012-09-21 13:53:15 -0700 | [diff] [blame] | 2391 | * Show the conversation provided in the arguments. It is safe to pass a null conversation |
| 2392 | * object, which is a signal to back out of conversation view mode. |
| 2393 | * Child classes must call super.showConversation() <b>before</b> their own implementations. |
Vikram Aggarwal | 59f741f | 2013-03-01 15:55:40 -0800 | [diff] [blame] | 2394 | * @param conversation the conversation to be shown, or null if we want to back out to list |
| 2395 | * mode. |
Vikram Aggarwal | 49e0e99 | 2012-09-21 13:53:15 -0700 | [diff] [blame] | 2396 | * @param inLoaderCallbacks true if the method is called as a result of |
Vikram Aggarwal | 177097f | 2013-03-08 11:19:53 -0800 | [diff] [blame] | 2397 | * onLoadFinished(Loader, Cursor) on any callback. |
Vikram Aggarwal | ec5cbf7 | 2012-03-08 15:10:35 -0800 | [diff] [blame] | 2398 | */ |
Andy Huang | 1ee96b2 | 2012-08-24 20:19:53 -0700 | [diff] [blame] | 2399 | protected void showConversation(Conversation conversation, boolean inLoaderCallbacks) { |
Andy Huang | 243c236 | 2013-03-01 17:50:35 -0800 | [diff] [blame] | 2400 | if (conversation != null) { |
| 2401 | Utils.sConvLoadTimer.start(); |
| 2402 | } |
| 2403 | |
Andy Huang | 54e925e | 2013-03-14 13:24:18 -0700 | [diff] [blame] | 2404 | MailLogService.log("AbstractActivityController", "showConversation(%s)", conversation); |
Vikram Aggarwal | c67182d | 2012-04-03 14:35:06 -0700 | [diff] [blame] | 2405 | // Set the current conversation just in case it wasn't already set. |
| 2406 | setCurrentConversation(conversation); |
Vikram Aggarwal | ec5cbf7 | 2012-03-08 15:10:35 -0800 | [diff] [blame] | 2407 | } |
| 2408 | |
Vikram Aggarwal | e128fc2 | 2012-04-04 12:33:34 -0700 | [diff] [blame] | 2409 | /** |
Paul Westbrook | 2d50bcd | 2012-04-10 11:53:47 -0700 | [diff] [blame] | 2410 | * Children can override this method, but they must call super.showWaitForInitialization(). |
| 2411 | * {@inheritDoc} |
| 2412 | */ |
| 2413 | @Override |
| 2414 | public void showWaitForInitialization() { |
| 2415 | mViewMode.enterWaitingForInitializationMode(); |
Vikram Aggarwal | a3f43d4 | 2012-10-25 16:21:30 -0700 | [diff] [blame] | 2416 | mWaitFragment = WaitFragment.newInstance(mAccount); |
Paul Westbrook | 2d50bcd | 2012-04-10 11:53:47 -0700 | [diff] [blame] | 2417 | } |
| 2418 | |
Vikram Aggarwal | dd6a7ce | 2012-10-22 15:45:57 -0700 | [diff] [blame] | 2419 | private void updateWaitMode() { |
Paul Westbrook | 2d50bcd | 2012-04-10 11:53:47 -0700 | [diff] [blame] | 2420 | final FragmentManager manager = mActivity.getFragmentManager(); |
| 2421 | final WaitFragment waitFragment = |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 2422 | (WaitFragment)manager.findFragmentByTag(TAG_WAIT); |
Paul Westbrook | 2d50bcd | 2012-04-10 11:53:47 -0700 | [diff] [blame] | 2423 | if (waitFragment != null) { |
| 2424 | waitFragment.updateAccount(mAccount); |
| 2425 | } |
| 2426 | } |
| 2427 | |
Vikram Aggarwal | a3f43d4 | 2012-10-25 16:21:30 -0700 | [diff] [blame] | 2428 | /** |
| 2429 | * Remove the "Waiting for Initialization" fragment. Child classes are free to override this |
| 2430 | * method, though they must call the parent implementation <b>after</b> they do anything. |
| 2431 | */ |
| 2432 | protected void hideWaitForInitialization() { |
| 2433 | mWaitFragment = null; |
| 2434 | } |
| 2435 | |
| 2436 | /** |
| 2437 | * Use the instance variable and the wait fragment's tag to get the wait fragment. This is |
| 2438 | * far superior to using the value of mWaitFragment, which might be invalid or might refer |
| 2439 | * to a fragment after it has been destroyed. |
Vikram Aggarwal | 715f83d | 2013-03-05 17:04:56 -0800 | [diff] [blame] | 2440 | * @return a wait fragment that is already attached to the activity, if one exists |
Vikram Aggarwal | a3f43d4 | 2012-10-25 16:21:30 -0700 | [diff] [blame] | 2441 | */ |
| 2442 | protected final WaitFragment getWaitFragment() { |
| 2443 | final FragmentManager manager = mActivity.getFragmentManager(); |
| 2444 | final WaitFragment waitFrag = (WaitFragment) manager.findFragmentByTag(TAG_WAIT); |
| 2445 | if (waitFrag != null) { |
| 2446 | // The Fragment Manager knows better, so use its instance. |
| 2447 | mWaitFragment = waitFrag; |
| 2448 | } |
| 2449 | return mWaitFragment; |
| 2450 | } |
Vikram Aggarwal | dd6a7ce | 2012-10-22 15:45:57 -0700 | [diff] [blame] | 2451 | |
Vikram Aggarwal | 48b2a6c | 2012-05-29 14:09:27 -0700 | [diff] [blame] | 2452 | /** |
| 2453 | * Returns true if we are waiting for the account to sync, and cannot show any folders or |
| 2454 | * conversation for the current account yet. |
Vikram Aggarwal | 48b2a6c | 2012-05-29 14:09:27 -0700 | [diff] [blame] | 2455 | */ |
Vikram Aggarwal | dd6a7ce | 2012-10-22 15:45:57 -0700 | [diff] [blame] | 2456 | private boolean inWaitMode() { |
Vikram Aggarwal | a3f43d4 | 2012-10-25 16:21:30 -0700 | [diff] [blame] | 2457 | final WaitFragment waitFragment = getWaitFragment(); |
Paul Westbrook | 2d50bcd | 2012-04-10 11:53:47 -0700 | [diff] [blame] | 2458 | if (waitFragment != null) { |
| 2459 | final Account fragmentAccount = waitFragment.getAccount(); |
Paul Westbrook | 339004b | 2012-11-05 17:13:51 -0800 | [diff] [blame] | 2460 | return fragmentAccount != null && fragmentAccount.uri.equals(mAccount.uri) && |
Paul Westbrook | 2d50bcd | 2012-04-10 11:53:47 -0700 | [diff] [blame] | 2461 | mViewMode.getMode() == ViewMode.WAITING_FOR_ACCOUNT_INITIALIZATION; |
| 2462 | } |
| 2463 | return false; |
| 2464 | } |
| 2465 | |
| 2466 | /** |
Vikram Aggarwal | e128fc2 | 2012-04-04 12:33:34 -0700 | [diff] [blame] | 2467 | * Children can override this method, but they must call super.showConversationList(). |
| 2468 | * {@inheritDoc} |
| 2469 | */ |
| 2470 | @Override |
| 2471 | public void showConversationList(ConversationListContext listContext) { |
| 2472 | } |
| 2473 | |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 2474 | @Override |
Vikram Aggarwal | 17f373e | 2012-09-17 15:49:32 -0700 | [diff] [blame] | 2475 | public final void onConversationSelected(Conversation conversation, boolean inLoaderCallbacks) { |
Alice Yang | fe8e081 | 2013-04-22 13:37:26 -0700 | [diff] [blame] | 2476 | final ConversationListFragment convListFragment = getConversationListFragment(); |
| 2477 | if (convListFragment != null && convListFragment.getAnimatedAdapter() != null) { |
| 2478 | convListFragment.getAnimatedAdapter().onConversationSelected(); |
| 2479 | } |
mindyp | aa55bc9 | 2012-08-24 09:49:56 -0700 | [diff] [blame] | 2480 | // Only animate destructive actions if we are going to be showing the |
| 2481 | // conversation list when we show the next conversation. |
Vikram Aggarwal | bcb16b9 | 2013-01-28 18:05:03 -0800 | [diff] [blame] | 2482 | commitDestructiveActions(mIsTablet); |
Andy Huang | 1ee96b2 | 2012-08-24 20:19:53 -0700 | [diff] [blame] | 2483 | showConversation(conversation, inLoaderCallbacks); |
| 2484 | } |
| 2485 | |
| 2486 | @Override |
Andrew Sapperstein | 2f54287 | 2013-06-11 10:48:30 -0700 | [diff] [blame] | 2487 | public final void onCabModeEntered() { |
| 2488 | final ConversationListFragment convListFragment = getConversationListFragment(); |
| 2489 | if (convListFragment != null && convListFragment.getAnimatedAdapter() != null) { |
| 2490 | convListFragment.getAnimatedAdapter().onCabModeEntered(); |
| 2491 | } |
| 2492 | } |
| 2493 | |
| 2494 | @Override |
Scott Kennedy | cc13983 | 2013-08-19 18:03:54 -0700 | [diff] [blame] | 2495 | public final void onCabModeExited() { |
| 2496 | final ConversationListFragment convListFragment = getConversationListFragment(); |
| 2497 | if (convListFragment != null && convListFragment.getAnimatedAdapter() != null) { |
| 2498 | convListFragment.getAnimatedAdapter().onCabModeExited(); |
| 2499 | } |
| 2500 | } |
| 2501 | |
| 2502 | @Override |
Andy Huang | 1ee96b2 | 2012-08-24 20:19:53 -0700 | [diff] [blame] | 2503 | public Conversation getCurrentConversation() { |
| 2504 | return mCurrentConversation; |
Vikram Aggarwal | 7d60288 | 2012-02-07 15:01:20 -0800 | [diff] [blame] | 2505 | } |
Mindy Pereira | 555140c | 2012-02-15 14:55:29 -0800 | [diff] [blame] | 2506 | |
Vikram Aggarwal | c67182d | 2012-04-03 14:35:06 -0700 | [diff] [blame] | 2507 | /** |
| 2508 | * Set the current conversation. This is the conversation on which all actions are performed. |
| 2509 | * Do not modify mCurrentConversation except through this method, which makes it easy to |
| 2510 | * perform common actions associated with changing the current conversation. |
Vikram Aggarwal | 715f83d | 2013-03-05 17:04:56 -0800 | [diff] [blame] | 2511 | * @param conversation new conversation to view. Passing null indicates that we are backing |
| 2512 | * out to conversation list mode. |
Vikram Aggarwal | c67182d | 2012-04-03 14:35:06 -0700 | [diff] [blame] | 2513 | */ |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 2514 | @Override |
| 2515 | public void setCurrentConversation(Conversation conversation) { |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame] | 2516 | // The controller should come out of detached mode if a new conversation is viewed, or if |
Vikram Aggarwal | 81230ee | 2013-02-26 15:26:44 -0800 | [diff] [blame] | 2517 | // we are going back to conversation list mode. |
| 2518 | if (mDetachedConvUri != null && (conversation == null |
| 2519 | || !mDetachedConvUri.equals(conversation.uri))) { |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame] | 2520 | clearDetachedMode(); |
| 2521 | } |
| 2522 | |
| 2523 | // Must happen *before* setting mCurrentConversation because this sets |
| 2524 | // conversation.position if a cursor is available. |
Andy Huang | 8883f22 | 2012-11-12 19:25:00 -0800 | [diff] [blame] | 2525 | mTracker.initialize(conversation); |
Vikram Aggarwal | 17f373e | 2012-09-17 15:49:32 -0700 | [diff] [blame] | 2526 | mCurrentConversation = conversation; |
Andy Huang | 7d64612 | 2012-09-05 19:41:44 -0700 | [diff] [blame] | 2527 | |
| 2528 | if (mCurrentConversation != null) { |
Yorke Lee | f807ba7 | 2012-09-20 17:18:05 -0700 | [diff] [blame] | 2529 | mActionBarView.setCurrentConversation(mCurrentConversation); |
Yorke Lee | f807ba7 | 2012-09-20 17:18:05 -0700 | [diff] [blame] | 2530 | mActivity.invalidateOptionsMenu(); |
Andy Huang | 7d64612 | 2012-09-05 19:41:44 -0700 | [diff] [blame] | 2531 | } |
Mindy Pereira | 5040f1a | 2012-03-20 10:14:06 -0700 | [diff] [blame] | 2532 | } |
| 2533 | |
Vikram Aggarwal | a9b93f3 | 2012-02-23 14:51:58 -0800 | [diff] [blame] | 2534 | /** |
Andy Huang | f9a7348 | 2012-03-13 15:54:02 -0700 | [diff] [blame] | 2535 | * {@link LoaderManager} currently has a bug in |
| 2536 | * {@link LoaderManager#restartLoader(int, Bundle, android.app.LoaderManager.LoaderCallbacks)} |
| 2537 | * where, if a previous onCreateLoader returned a null loader, this method will NPE. Work around |
| 2538 | * this bug by destroying any loaders that may have been created as null (essentially because |
| 2539 | * they are optional loads, and may not apply to a particular account). |
| 2540 | * <p> |
| 2541 | * A simple null check before restarting a loader will not work, because that would not |
| 2542 | * give the controller a chance to invalidate UI corresponding the prior loader result. |
| 2543 | * |
| 2544 | * @param id loader ID to safely restart |
Vikram Aggarwal | 177097f | 2013-03-08 11:19:53 -0800 | [diff] [blame] | 2545 | * @param handler the LoaderCallback which will handle this loader ID. |
| 2546 | * @param args arguments, if any, to be passed to the loader. Use {@link Bundle#EMPTY} if no |
| 2547 | * arguments need to be specified. |
Andy Huang | f9a7348 | 2012-03-13 15:54:02 -0700 | [diff] [blame] | 2548 | */ |
Vikram Aggarwal | 177097f | 2013-03-08 11:19:53 -0800 | [diff] [blame] | 2549 | private void restartOptionalLoader(int id, LoaderManager.LoaderCallbacks handler, Bundle args) { |
Andy Huang | f9a7348 | 2012-03-13 15:54:02 -0700 | [diff] [blame] | 2550 | final LoaderManager lm = mActivity.getLoaderManager(); |
| 2551 | lm.destroyLoader(id); |
Vikram Aggarwal | 177097f | 2013-03-08 11:19:53 -0800 | [diff] [blame] | 2552 | lm.restartLoader(id, args, handler); |
Vikram Aggarwal | 94c94de | 2012-04-04 15:38:28 -0700 | [diff] [blame] | 2553 | } |
| 2554 | |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 2555 | @Override |
| 2556 | public void registerConversationListObserver(DataSetObserver observer) { |
| 2557 | mConversationListObservable.registerObserver(observer); |
| 2558 | } |
| 2559 | |
| 2560 | @Override |
| 2561 | public void unregisterConversationListObserver(DataSetObserver observer) { |
Alice Yang | 647aefb | 2013-03-04 19:13:11 -0800 | [diff] [blame] | 2562 | try { |
| 2563 | mConversationListObservable.unregisterObserver(observer); |
| 2564 | } catch (IllegalStateException e) { |
| 2565 | // Log instead of crash |
| 2566 | LogUtils.e(LOG_TAG, e, "unregisterConversationListObserver called for an observer that " |
| 2567 | + "hasn't been registered"); |
| 2568 | } |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 2569 | } |
| 2570 | |
Andy Huang | 090db1e | 2012-07-25 13:25:28 -0700 | [diff] [blame] | 2571 | @Override |
| 2572 | public void registerFolderObserver(DataSetObserver observer) { |
| 2573 | mFolderObservable.registerObserver(observer); |
| 2574 | } |
| 2575 | |
| 2576 | @Override |
| 2577 | public void unregisterFolderObserver(DataSetObserver observer) { |
Alice Yang | 647aefb | 2013-03-04 19:13:11 -0800 | [diff] [blame] | 2578 | try { |
| 2579 | mFolderObservable.unregisterObserver(observer); |
| 2580 | } catch (IllegalStateException e) { |
| 2581 | // Log instead of crash |
| 2582 | LogUtils.e(LOG_TAG, e, "unregisterFolderObserver called for an observer that " |
| 2583 | + "hasn't been registered"); |
| 2584 | } |
Andy Huang | 090db1e | 2012-07-25 13:25:28 -0700 | [diff] [blame] | 2585 | } |
| 2586 | |
Andy Huang | 9d3fd92 | 2012-09-26 22:23:58 -0700 | [diff] [blame] | 2587 | @Override |
| 2588 | public void registerConversationLoadedObserver(DataSetObserver observer) { |
| 2589 | mPagerController.registerConversationLoadedObserver(observer); |
| 2590 | } |
| 2591 | |
| 2592 | @Override |
| 2593 | public void unregisterConversationLoadedObserver(DataSetObserver observer) { |
Alice Yang | 647aefb | 2013-03-04 19:13:11 -0800 | [diff] [blame] | 2594 | try { |
| 2595 | mPagerController.unregisterConversationLoadedObserver(observer); |
| 2596 | } catch (IllegalStateException e) { |
| 2597 | // Log instead of crash |
| 2598 | LogUtils.e(LOG_TAG, e, "unregisterConversationLoadedObserver called for an observer " |
| 2599 | + "that hasn't been registered"); |
| 2600 | } |
Andy Huang | 9d3fd92 | 2012-09-26 22:23:58 -0700 | [diff] [blame] | 2601 | } |
| 2602 | |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 2603 | /** |
Vikram Aggarwal | c04cf7e | 2013-05-13 15:38:42 -0700 | [diff] [blame] | 2604 | * Returns true if the number of accounts is different, or if the current account has |
| 2605 | * changed. This method is meant to filter frequent changes to the list of |
| 2606 | * accounts, and only return true if the new list is substantially different from the existing |
| 2607 | * list. Returning true is safe here, it leads to more work in creating the |
| 2608 | * same account list again. |
Vikram Aggarwal | 715f83d | 2013-03-05 17:04:56 -0800 | [diff] [blame] | 2609 | * @param accountCursor the cursor which points to all the accounts. |
| 2610 | * @return true if the number of accounts is changed or current account missing from the list. |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 2611 | */ |
Vikram Aggarwal | 177097f | 2013-03-08 11:19:53 -0800 | [diff] [blame] | 2612 | private boolean accountsUpdated(ObjectCursor<Account> accountCursor) { |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 2613 | // Check to see if the current account hasn't been set, or the account cursor is empty |
| 2614 | if (mAccount == null || !accountCursor.moveToFirst()) { |
Vikram Aggarwal | 6c51158 | 2012-02-27 10:59:47 -0800 | [diff] [blame] | 2615 | return true; |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 2616 | } |
| 2617 | |
| 2618 | // Check to see if the number of accounts are different, from the number we saw on the last |
| 2619 | // updated |
| 2620 | if (mCurrentAccountUris.size() != accountCursor.getCount()) { |
| 2621 | return true; |
| 2622 | } |
| 2623 | |
| 2624 | // Check to see if the account list is different or if the current account is not found in |
| 2625 | // the cursor. |
| 2626 | boolean foundCurrentAccount = false; |
Vikram Aggarwal | 7dedb95 | 2012-02-16 16:10:23 -0800 | [diff] [blame] | 2627 | do { |
Vikram Aggarwal | c04cf7e | 2013-05-13 15:38:42 -0700 | [diff] [blame] | 2628 | final Account account = accountCursor.getModel(); |
| 2629 | if (!foundCurrentAccount && mAccount.uri.equals(account.uri)) { |
| 2630 | if (mAccount.settingsDiffer(account)) { |
| 2631 | // Settings changed, and we don't need to look any further. |
| 2632 | return true; |
| 2633 | } |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 2634 | foundCurrentAccount = true; |
| 2635 | } |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 2636 | // Is there a new account that we do not know about? |
Vikram Aggarwal | c04cf7e | 2013-05-13 15:38:42 -0700 | [diff] [blame] | 2637 | if (!mCurrentAccountUris.contains(account.uri)) { |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 2638 | return true; |
| 2639 | } |
Vikram Aggarwal | 7dedb95 | 2012-02-16 16:10:23 -0800 | [diff] [blame] | 2640 | } while (accountCursor.moveToNext()); |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 2641 | |
| 2642 | // As long as we found the current account, the list hasn't been updated |
| 2643 | return !foundCurrentAccount; |
Vikram Aggarwal | 7dedb95 | 2012-02-16 16:10:23 -0800 | [diff] [blame] | 2644 | } |
| 2645 | |
| 2646 | /** |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 2647 | * Updates accounts for the app. If the current account is missing, the first |
| 2648 | * 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] | 2649 | * |
Vikram Aggarwal | 6c51158 | 2012-02-27 10:59:47 -0800 | [diff] [blame] | 2650 | * @param accounts cursor into the AccountCache |
Vikram Aggarwal | 7dedb95 | 2012-02-16 16:10:23 -0800 | [diff] [blame] | 2651 | * @return true if the update was successful, false otherwise |
| 2652 | */ |
Vikram Aggarwal | 177097f | 2013-03-08 11:19:53 -0800 | [diff] [blame] | 2653 | private boolean updateAccounts(ObjectCursor<Account> accounts) { |
Vikram Aggarwal | 7dedb95 | 2012-02-16 16:10:23 -0800 | [diff] [blame] | 2654 | if (accounts == null || !accounts.moveToFirst()) { |
| 2655 | return false; |
| 2656 | } |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 2657 | |
Vikram Aggarwal | a9b93f3 | 2012-02-23 14:51:58 -0800 | [diff] [blame] | 2658 | final Account[] allAccounts = Account.getAllAccounts(accounts); |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 2659 | // A match for the current account's URI in the list of accounts. |
| 2660 | Account currentFromList = null; |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 2661 | |
Vikram Aggarwal | 636c1a1 | 2012-09-22 16:02:40 -0700 | [diff] [blame] | 2662 | // 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] | 2663 | mCurrentAccountUris.clear(); |
Vikram Aggarwal | 636c1a1 | 2012-09-22 16:02:40 -0700 | [diff] [blame] | 2664 | for (final Account account : allAccounts) { |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 2665 | LogUtils.d(LOG_TAG, "updateAccounts(%s)", account); |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 2666 | mCurrentAccountUris.add(account.uri); |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 2667 | if (mAccount != null && account.uri.equals(mAccount.uri)) { |
| 2668 | currentFromList = account; |
| 2669 | } |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 2670 | } |
| 2671 | |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 2672 | // 1. current account is already set and is in allAccounts: |
| 2673 | // 1a. It has changed -> load the updated account. |
| 2674 | // 2b. It is unchanged -> no-op |
Andy Huang | 0d64735 | 2012-03-21 21:48:16 -0700 | [diff] [blame] | 2675 | // 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] | 2676 | // 3. saved preference has an account -> pick that one |
Andy Huang | 0d64735 | 2012-03-21 21:48:16 -0700 | [diff] [blame] | 2677 | // 4. otherwise just pick first |
| 2678 | |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 2679 | boolean accountChanged = false; |
| 2680 | /// Assume case 4, initialize to first account, and see if we can find anything better. |
| 2681 | Account newAccount = allAccounts[0]; |
| 2682 | if (currentFromList != null) { |
| 2683 | // Case 1: Current account exists but has changed |
| 2684 | if (!currentFromList.equals(mAccount)) { |
| 2685 | newAccount = currentFromList; |
| 2686 | accountChanged = true; |
Andy Huang | 0d64735 | 2012-03-21 21:48:16 -0700 | [diff] [blame] | 2687 | } |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 2688 | // Case 1b: else, current account is unchanged: nothing to do. |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 2689 | } else { |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 2690 | // Case 2: Current account is not in allAccounts, the account needs to change. |
| 2691 | accountChanged = true; |
| 2692 | if (mAccount == null) { |
| 2693 | // Case 3: Check for last viewed account, and check if it exists in the list. |
| 2694 | final String lastAccountUri = MailAppProvider.getInstance().getLastViewedAccount(); |
| 2695 | if (lastAccountUri != null) { |
| 2696 | for (final Account account : allAccounts) { |
| 2697 | if (lastAccountUri.equals(account.uri.toString())) { |
| 2698 | newAccount = account; |
| 2699 | break; |
| 2700 | } |
Andy Huang | 0d64735 | 2012-03-21 21:48:16 -0700 | [diff] [blame] | 2701 | } |
| 2702 | } |
| 2703 | } |
Paul Westbrook | 23b74b9 | 2012-02-29 11:36:12 -0800 | [diff] [blame] | 2704 | } |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 2705 | if (accountChanged) { |
Vikram Aggarwal | 5fd8afd | 2013-03-13 15:28:47 -0700 | [diff] [blame] | 2706 | changeAccount(newAccount); |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 2707 | } |
Vikram Aggarwal | 07dbaa6 | 2013-03-12 15:21:04 -0700 | [diff] [blame] | 2708 | |
Vikram Aggarwal | 6006991 | 2012-07-24 14:26:09 -0700 | [diff] [blame] | 2709 | // Whether we have updated the current account or not, we need to update the list of |
| 2710 | // accounts in the ActionBar. |
Rohan Shah | b905f0e | 2013-04-26 09:17:37 -0700 | [diff] [blame] | 2711 | mAllAccounts = allAccounts; |
Vikram Aggarwal | 07dbaa6 | 2013-03-12 15:21:04 -0700 | [diff] [blame] | 2712 | mAllAccountObservers.notifyChanged(); |
Vikram Aggarwal | a9b93f3 | 2012-02-23 14:51:58 -0800 | [diff] [blame] | 2713 | return (allAccounts.length > 0); |
Vikram Aggarwal | 7dedb95 | 2012-02-16 16:10:23 -0800 | [diff] [blame] | 2714 | } |
| 2715 | |
Paul Westbrook | 6ead20d | 2012-03-19 14:48:14 -0700 | [diff] [blame] | 2716 | private void disableNotifications() { |
| 2717 | mNewEmailReceiver.activate(mContext, this); |
| 2718 | } |
| 2719 | |
| 2720 | private void enableNotifications() { |
| 2721 | mNewEmailReceiver.deactivate(); |
| 2722 | } |
| 2723 | |
| 2724 | private void disableNotificationsOnAccountChange(Account account) { |
| 2725 | // If the new mail suppression receiver is activated for a different account, we want to |
| 2726 | // activate it for the new account. |
| 2727 | if (mNewEmailReceiver.activated() && |
| 2728 | !mNewEmailReceiver.notificationsDisabledForAccount(account)) { |
| 2729 | // Deactivate the current receiver, otherwise multiple receivers may be registered. |
| 2730 | mNewEmailReceiver.deactivate(); |
| 2731 | mNewEmailReceiver.activate(mContext, this); |
| 2732 | } |
| 2733 | } |
| 2734 | |
Vikram Aggarwal | a9b93f3 | 2012-02-23 14:51:58 -0800 | [diff] [blame] | 2735 | /** |
Vikram Aggarwal | c769422 | 2012-04-23 13:37:01 -0700 | [diff] [blame] | 2736 | * Destructive actions on Conversations. This class should only be created by controllers, and |
| 2737 | * clients should only require {@link DestructiveAction}s, not specific implementations of the. |
| 2738 | * Only the controllers should know what kind of destructive actions are being created. |
| 2739 | */ |
Mindy Pereira | de3e74a | 2012-07-24 09:43:10 -0700 | [diff] [blame] | 2740 | public class ConversationAction implements DestructiveAction { |
Vikram Aggarwal | acaa3c0 | 2012-04-24 12:45:27 -0700 | [diff] [blame] | 2741 | /** |
| 2742 | * The action to be performed. This is specified as the resource ID of the menu item |
| 2743 | * corresponding to this action: R.id.delete, R.id.report_spam, etc. |
| 2744 | */ |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 2745 | private final int mAction; |
Vikram Aggarwal | bc67bb1 | 2012-04-30 14:05:35 -0700 | [diff] [blame] | 2746 | /** The action will act upon these conversations */ |
Paul Westbrook | 77eee62 | 2012-07-10 13:41:57 -0700 | [diff] [blame] | 2747 | private final Collection<Conversation> mTarget; |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 2748 | /** Whether this destructive action has already been performed */ |
| 2749 | private boolean mCompleted; |
Vikram Aggarwal | c411395 | 2012-05-11 14:14:56 -0700 | [diff] [blame] | 2750 | /** Whether this is an action on the currently selected set. */ |
| 2751 | private final boolean mIsSelectedSet; |
Vikram Aggarwal | e8a8532 | 2012-04-24 09:01:38 -0700 | [diff] [blame] | 2752 | |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 2753 | /** |
Vikram Aggarwal | 715f83d | 2013-03-05 17:04:56 -0800 | [diff] [blame] | 2754 | * Create a listener object. |
| 2755 | * @param action action is one of four constants: R.id.y_button (archive), |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 2756 | * R.id.delete , R.id.mute, and R.id.report_spam. |
Vikram Aggarwal | bc67bb1 | 2012-04-30 14:05:35 -0700 | [diff] [blame] | 2757 | * @param target Conversation that we want to apply the action to. |
Vikram Aggarwal | c411395 | 2012-05-11 14:14:56 -0700 | [diff] [blame] | 2758 | * @param isBatch whether the conversations are in the currently selected batch set. |
| 2759 | */ |
| 2760 | public ConversationAction(int action, Collection<Conversation> target, boolean isBatch) { |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 2761 | mAction = action; |
Paul Westbrook | 77eee62 | 2012-07-10 13:41:57 -0700 | [diff] [blame] | 2762 | mTarget = ImmutableList.copyOf(target); |
Vikram Aggarwal | c411395 | 2012-05-11 14:14:56 -0700 | [diff] [blame] | 2763 | mIsSelectedSet = isBatch; |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 2764 | } |
| 2765 | |
Vikram Aggarwal | acaa3c0 | 2012-04-24 12:45:27 -0700 | [diff] [blame] | 2766 | /** |
| 2767 | * The action common to child classes. This performs the action specified in the constructor |
| 2768 | * on the conversations given here. |
Vikram Aggarwal | acaa3c0 | 2012-04-24 12:45:27 -0700 | [diff] [blame] | 2769 | */ |
Vikram Aggarwal | bc67bb1 | 2012-04-30 14:05:35 -0700 | [diff] [blame] | 2770 | @Override |
| 2771 | public void performAction() { |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 2772 | if (isPerformed()) { |
| 2773 | return; |
| 2774 | } |
Mindy Pereira | 3cb28b5 | 2012-05-24 15:26:39 -0700 | [diff] [blame] | 2775 | boolean undoEnabled = mAccount.supportsCapability(AccountCapabilities.UNDO); |
Vikram Aggarwal | 7dd054e | 2012-05-21 14:43:10 -0700 | [diff] [blame] | 2776 | |
| 2777 | // Are we destroying the currently shown conversation? Show the next one. |
| 2778 | if (LogUtils.isLoggable(LOG_TAG, LogUtils.DEBUG)){ |
Vikram Aggarwal | 8742a61 | 2012-08-13 10:22:50 -0700 | [diff] [blame] | 2779 | LogUtils.d(LOG_TAG, "ConversationAction.performAction():" |
| 2780 | + "\nmTarget=%s\nCurrent=%s", |
Vikram Aggarwal | 7dd054e | 2012-05-21 14:43:10 -0700 | [diff] [blame] | 2781 | Conversation.toString(mTarget), mCurrentConversation); |
| 2782 | } |
Vikram Aggarwal | 7dd054e | 2012-05-21 14:43:10 -0700 | [diff] [blame] | 2783 | |
Paul Westbrook | e1221d2 | 2012-08-19 11:09:07 -0700 | [diff] [blame] | 2784 | if (mConversationListCursor == null) { |
| 2785 | LogUtils.e(LOG_TAG, "null ConversationCursor in ConversationAction.performAction():" |
| 2786 | + "\nmTarget=%s\nCurrent=%s", |
| 2787 | Conversation.toString(mTarget), mCurrentConversation); |
| 2788 | return; |
| 2789 | } |
| 2790 | |
Scott Kennedy | 2b9d80e | 2013-07-30 23:03:45 -0700 | [diff] [blame] | 2791 | if (mAction == R.id.archive) { |
| 2792 | LogUtils.d(LOG_TAG, "Archiving"); |
| 2793 | mConversationListCursor.archive(mTarget); |
| 2794 | } else if (mAction == R.id.delete) { |
| 2795 | LogUtils.d(LOG_TAG, "Deleting"); |
| 2796 | mConversationListCursor.delete(mTarget); |
| 2797 | if (mFolder.supportsCapability(FolderCapabilities.DELETE_ACTION_FINAL)) { |
Paul Westbrook | ef36254 | 2012-08-27 14:53:32 -0700 | [diff] [blame] | 2798 | undoEnabled = false; |
Scott Kennedy | 2b9d80e | 2013-07-30 23:03:45 -0700 | [diff] [blame] | 2799 | } |
| 2800 | } else if (mAction == R.id.mute) { |
| 2801 | LogUtils.d(LOG_TAG, "Muting"); |
| 2802 | if (mFolder.supportsCapability(FolderCapabilities.DESTRUCTIVE_MUTE)) { |
| 2803 | for (Conversation c : mTarget) { |
| 2804 | c.localDeleteOnUpdate = true; |
| 2805 | } |
| 2806 | } |
| 2807 | mConversationListCursor.mute(mTarget); |
| 2808 | } else if (mAction == R.id.report_spam) { |
| 2809 | LogUtils.d(LOG_TAG, "Reporting spam"); |
| 2810 | mConversationListCursor.reportSpam(mTarget); |
| 2811 | } else if (mAction == R.id.mark_not_spam) { |
| 2812 | LogUtils.d(LOG_TAG, "Marking not spam"); |
| 2813 | mConversationListCursor.reportNotSpam(mTarget); |
| 2814 | } else if (mAction == R.id.report_phishing) { |
| 2815 | LogUtils.d(LOG_TAG, "Reporting phishing"); |
| 2816 | mConversationListCursor.reportPhishing(mTarget); |
| 2817 | } else if (mAction == R.id.remove_star) { |
| 2818 | LogUtils.d(LOG_TAG, "Removing star"); |
| 2819 | // Star removal is destructive in the Starred folder. |
| 2820 | mConversationListCursor.updateBoolean(mTarget, ConversationColumns.STARRED, |
| 2821 | false); |
| 2822 | } else if (mAction == R.id.mark_not_important) { |
| 2823 | LogUtils.d(LOG_TAG, "Marking not-important"); |
| 2824 | // Marking not important is destructive in a mailbox |
| 2825 | // containing only important messages |
| 2826 | if (mFolder != null && mFolder.isImportantOnly()) { |
| 2827 | for (Conversation conv : mTarget) { |
| 2828 | conv.localDeleteOnUpdate = true; |
| 2829 | } |
| 2830 | } |
| 2831 | mConversationListCursor.updateInt(mTarget, ConversationColumns.PRIORITY, |
| 2832 | UIProvider.ConversationPriority.LOW); |
| 2833 | } else if (mAction == R.id.discard_drafts) { |
| 2834 | LogUtils.d(LOG_TAG, "Discarding draft messages"); |
| 2835 | // Discarding draft messages is destructive in a "draft" mailbox |
| 2836 | if (mFolder != null && mFolder.isDraft()) { |
| 2837 | for (Conversation conv : mTarget) { |
| 2838 | conv.localDeleteOnUpdate = true; |
| 2839 | } |
| 2840 | } |
| 2841 | mConversationListCursor.discardDrafts(mTarget); |
| 2842 | // We don't support undoing discarding drafts |
| 2843 | undoEnabled = false; |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 2844 | } |
| 2845 | if (undoEnabled) { |
mindyp | ead5039 | 2012-08-23 11:03:53 -0700 | [diff] [blame] | 2846 | mHandler.postDelayed(new Runnable() { |
| 2847 | @Override |
| 2848 | public void run() { |
| 2849 | onUndoAvailable(new ToastBarOperation(mTarget.size(), mAction, |
Scott Kennedy | 6a3d5ce | 2013-03-15 17:33:14 -0700 | [diff] [blame] | 2850 | ToastBarOperation.UNDO, mIsSelectedSet, mFolder)); |
mindyp | ead5039 | 2012-08-23 11:03:53 -0700 | [diff] [blame] | 2851 | } |
| 2852 | }, mShowUndoBarDelay); |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 2853 | } |
Vikram Aggarwal | bc67bb1 | 2012-04-30 14:05:35 -0700 | [diff] [blame] | 2854 | refreshConversationList(); |
Vikram Aggarwal | c411395 | 2012-05-11 14:14:56 -0700 | [diff] [blame] | 2855 | if (mIsSelectedSet) { |
Vikram Aggarwal | c411395 | 2012-05-11 14:14:56 -0700 | [diff] [blame] | 2856 | mSelectedSet.clear(); |
| 2857 | } |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 2858 | } |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 2859 | |
| 2860 | /** |
| 2861 | * Returns true if this action has been performed, false otherwise. |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 2862 | * |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 2863 | */ |
| 2864 | private synchronized boolean isPerformed() { |
| 2865 | if (mCompleted) { |
| 2866 | return true; |
| 2867 | } |
| 2868 | mCompleted = true; |
| 2869 | return false; |
| 2870 | } |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 2871 | } |
Mindy Pereira | e5f4dc0 | 2012-03-21 16:08:53 -0700 | [diff] [blame] | 2872 | |
Vikram Aggarwal | d503df4 | 2012-05-11 10:13:35 -0700 | [diff] [blame] | 2873 | // Called from the FolderSelectionDialog after a user is done selecting folders to assign the |
| 2874 | // conversations to. |
Mindy Pereira | e5f4dc0 | 2012-03-21 16:08:53 -0700 | [diff] [blame] | 2875 | @Override |
Mindy Pereira | 8db7e40 | 2012-07-13 10:32:47 -0700 | [diff] [blame] | 2876 | public final void assignFolder(Collection<FolderOperation> folderOps, |
Scott Kennedy | 6a3d5ce | 2013-03-15 17:33:14 -0700 | [diff] [blame] | 2877 | Collection<Conversation> target, boolean batch, boolean showUndo, |
| 2878 | final boolean isMoveTo) { |
Mindy Pereira | 8db7e40 | 2012-07-13 10:32:47 -0700 | [diff] [blame] | 2879 | // Actions are destructive only when the current folder can be assigned |
| 2880 | // to (which is the same as being able to un-assign a conversation from the folder) and |
| 2881 | // when the list of folders contains the current folder. |
| 2882 | final boolean isDestructive = mFolder |
| 2883 | .supportsCapability(FolderCapabilities.CAN_ACCEPT_MOVED_MESSAGES) |
| 2884 | && FolderOperation.isDestructive(folderOps, mFolder); |
Vikram Aggarwal | d503df4 | 2012-05-11 10:13:35 -0700 | [diff] [blame] | 2885 | LogUtils.d(LOG_TAG, "onFolderChangesCommit: isDestructive = %b", isDestructive); |
| 2886 | if (isDestructive) { |
| 2887 | for (final Conversation c : target) { |
| 2888 | c.localDeleteOnUpdate = true; |
Mindy Pereira | 6778f46 | 2012-03-23 18:01:55 -0700 | [diff] [blame] | 2889 | } |
Mindy Pereira | e5f4dc0 | 2012-03-21 16:08:53 -0700 | [diff] [blame] | 2890 | } |
mindyp | c84759c | 2012-08-29 09:51:53 -0700 | [diff] [blame] | 2891 | final DestructiveAction folderChange; |
Vikram Aggarwal | d503df4 | 2012-05-11 10:13:35 -0700 | [diff] [blame] | 2892 | // Update the UI elements depending no their visibility and availability |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 2893 | // TODO(viki): Consolidate this into a single method requestDelete. |
Vikram Aggarwal | d503df4 | 2012-05-11 10:13:35 -0700 | [diff] [blame] | 2894 | if (isDestructive) { |
Scott Kennedy | 6a3d5ce | 2013-03-15 17:33:14 -0700 | [diff] [blame] | 2895 | /* |
| 2896 | * If this is a MOVE operation, we want the action folder to be the destination folder. |
| 2897 | * Otherwise, we want it to be the current folder. |
| 2898 | * |
| 2899 | * A set of folder operations is a move if there are exactly two operations: an add and |
| 2900 | * a remove. |
| 2901 | */ |
| 2902 | final Folder actionFolder; |
| 2903 | if (folderOps.size() != 2) { |
| 2904 | actionFolder = mFolder; |
| 2905 | } else { |
| 2906 | Folder addedFolder = null; |
| 2907 | boolean hasRemove = false; |
| 2908 | for (final FolderOperation folderOperation : folderOps) { |
| 2909 | if (folderOperation.mAdd) { |
| 2910 | addedFolder = folderOperation.mFolder; |
| 2911 | } else { |
| 2912 | hasRemove = true; |
| 2913 | } |
| 2914 | } |
| 2915 | |
| 2916 | if (hasRemove && addedFolder != null) { |
| 2917 | actionFolder = addedFolder; |
| 2918 | } else { |
| 2919 | actionFolder = mFolder; |
| 2920 | } |
| 2921 | } |
| 2922 | |
mindyp | c84759c | 2012-08-29 09:51:53 -0700 | [diff] [blame] | 2923 | folderChange = getDeferredFolderChange(target, folderOps, isDestructive, |
Scott Kennedy | 6a3d5ce | 2013-03-15 17:33:14 -0700 | [diff] [blame] | 2924 | batch, showUndo, isMoveTo, actionFolder); |
Scott Kennedy | caaeed3 | 2013-06-12 13:39:16 -0700 | [diff] [blame] | 2925 | delete(0, target, folderChange, batch); |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 2926 | } else { |
mindyp | c84759c | 2012-08-29 09:51:53 -0700 | [diff] [blame] | 2927 | folderChange = getFolderChange(target, folderOps, isDestructive, |
Scott Kennedy | 6a3d5ce | 2013-03-15 17:33:14 -0700 | [diff] [blame] | 2928 | batch, showUndo, false /* isMoveTo */, mFolder); |
Vikram Aggarwal | 715f83d | 2013-03-05 17:04:56 -0800 | [diff] [blame] | 2929 | requestUpdate(folderChange); |
Mindy Pereira | e5f4dc0 | 2012-03-21 16:08:53 -0700 | [diff] [blame] | 2930 | } |
| 2931 | } |
| 2932 | |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 2933 | @Override |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 2934 | public final void onRefreshRequired() { |
mindyp | 5390fca | 2012-08-22 12:12:25 -0700 | [diff] [blame] | 2935 | if (isAnimating() || isDragging()) { |
Andy Huang | c1922a9 | 2013-05-13 14:33:05 -0700 | [diff] [blame] | 2936 | LogUtils.i(ConversationCursor.LOG_TAG, "onRefreshRequired: delay until animating done"); |
Mindy Pereira | 69e88dd | 2012-08-10 09:30:18 -0700 | [diff] [blame] | 2937 | return; |
| 2938 | } |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 2939 | // Refresh the query in the background |
Paul Westbrook | cff1aea | 2012-08-10 11:51:00 -0700 | [diff] [blame] | 2940 | if (mConversationListCursor.isRefreshRequired()) { |
| 2941 | mConversationListCursor.refresh(); |
| 2942 | } |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 2943 | } |
| 2944 | |
mindyp | 5390fca | 2012-08-22 12:12:25 -0700 | [diff] [blame] | 2945 | @Override |
| 2946 | public void startDragMode() { |
| 2947 | mIsDragHappening = true; |
| 2948 | } |
| 2949 | |
| 2950 | @Override |
| 2951 | public void stopDragMode() { |
| 2952 | mIsDragHappening = false; |
| 2953 | if (mConversationListCursor.isRefreshReady()) { |
Andy Huang | c1922a9 | 2013-05-13 14:33:05 -0700 | [diff] [blame] | 2954 | LogUtils.i(ConversationCursor.LOG_TAG, "Stopped dragging: try sync"); |
mindyp | 5390fca | 2012-08-22 12:12:25 -0700 | [diff] [blame] | 2955 | onRefreshReady(); |
| 2956 | } |
| 2957 | |
| 2958 | if (mConversationListCursor.isRefreshRequired()) { |
Andy Huang | c1922a9 | 2013-05-13 14:33:05 -0700 | [diff] [blame] | 2959 | LogUtils.i(ConversationCursor.LOG_TAG, "Stopped dragging: refresh"); |
mindyp | 5390fca | 2012-08-22 12:12:25 -0700 | [diff] [blame] | 2960 | mConversationListCursor.refresh(); |
| 2961 | } |
| 2962 | } |
| 2963 | |
| 2964 | private boolean isDragging() { |
| 2965 | return mIsDragHappening; |
| 2966 | } |
| 2967 | |
mindyp | 6f54e1b | 2012-10-09 09:54:08 -0700 | [diff] [blame] | 2968 | @Override |
| 2969 | public boolean isAnimating() { |
Mindy Pereira | 69e88dd | 2012-08-10 09:30:18 -0700 | [diff] [blame] | 2970 | boolean isAnimating = false; |
| 2971 | ConversationListFragment convListFragment = getConversationListFragment(); |
| 2972 | if (convListFragment != null) { |
Andy Huang | 48ccbc5 | 2013-06-05 20:30:47 -0700 | [diff] [blame] | 2973 | isAnimating = convListFragment.isAnimating(); |
Mindy Pereira | 69e88dd | 2012-08-10 09:30:18 -0700 | [diff] [blame] | 2974 | } |
| 2975 | return isAnimating; |
| 2976 | } |
| 2977 | |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 2978 | /** |
| 2979 | * Called when the {@link ConversationCursor} is changed or has new data in it. |
| 2980 | * <p> |
| 2981 | * {@inheritDoc} |
| 2982 | */ |
| 2983 | @Override |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 2984 | public final void onRefreshReady() { |
mindyp | 5c1d835 | 2012-11-05 10:12:44 -0800 | [diff] [blame] | 2985 | LogUtils.d(LOG_TAG, "Received refresh ready callback for folder %s", |
| 2986 | mFolder != null ? mFolder.id : "-1"); |
Andy Huang | b2ef9c1 | 2012-12-18 12:58:41 -0600 | [diff] [blame] | 2987 | |
| 2988 | if (mDestroyed) { |
| 2989 | LogUtils.i(LOG_TAG, "ignoring onRefreshReady on destroyed AAC"); |
| 2990 | return; |
| 2991 | } |
| 2992 | |
Paul Westbrook | cff1aea | 2012-08-10 11:51:00 -0700 | [diff] [blame] | 2993 | if (!isAnimating()) { |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 2994 | // Swap cursors |
| 2995 | mConversationListCursor.sync(); |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 2996 | } |
Paul Westbrook | 937c94f | 2012-08-16 13:01:18 -0700 | [diff] [blame] | 2997 | mTracker.onCursorUpdated(); |
Vikram Aggarwal | f0ef230 | 2012-11-07 14:53:34 -0800 | [diff] [blame] | 2998 | perhapsShowFirstSearchResult(); |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 2999 | } |
| 3000 | |
| 3001 | @Override |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 3002 | public final void onDataSetChanged() { |
Paul Westbrook | 9f119c7 | 2012-04-24 16:10:59 -0700 | [diff] [blame] | 3003 | updateConversationListFragment(); |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 3004 | mConversationListObservable.notifyChanged(); |
Paul Westbrook | a13b374 | 2012-09-07 16:35:06 -0700 | [diff] [blame] | 3005 | mSelectedSet.validateAgainstCursor(mConversationListCursor); |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 3006 | } |
| 3007 | |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 3008 | /** |
| 3009 | * If the Conversation List Fragment is visible, updates the fragment. |
| 3010 | */ |
Vikram Aggarwal | 715f83d | 2013-03-05 17:04:56 -0800 | [diff] [blame] | 3011 | private void updateConversationListFragment() { |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 3012 | final ConversationListFragment convList = getConversationListFragment(); |
| 3013 | if (convList != null) { |
Vikram Aggarwal | 75daee5 | 2012-04-30 11:13:09 -0700 | [diff] [blame] | 3014 | refreshConversationList(); |
Vikram Aggarwal | 6422b8d | 2013-01-14 10:47:41 -0800 | [diff] [blame] | 3015 | if (isFragmentVisible(convList)) { |
Vikram Aggarwal | 69b5c30 | 2012-09-05 11:11:13 -0700 | [diff] [blame] | 3016 | informCursorVisiblity(true); |
Paul Westbrook | 9f119c7 | 2012-04-24 16:10:59 -0700 | [diff] [blame] | 3017 | } |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 3018 | } |
| 3019 | } |
| 3020 | |
| 3021 | /** |
| 3022 | * This class handles throttled refresh of the conversation list |
| 3023 | */ |
| 3024 | static class RefreshTimerTask extends TimerTask { |
| 3025 | final Handler mHandler; |
| 3026 | final AbstractActivityController mController; |
| 3027 | |
| 3028 | RefreshTimerTask(AbstractActivityController controller, Handler handler) { |
| 3029 | mHandler = handler; |
| 3030 | mController = controller; |
| 3031 | } |
| 3032 | |
| 3033 | @Override |
| 3034 | public void run() { |
| 3035 | mHandler.post(new Runnable() { |
| 3036 | @Override |
| 3037 | public void run() { |
| 3038 | LogUtils.d(LOG_TAG, "Delay done... calling onRefreshRequired"); |
| 3039 | mController.onRefreshRequired(); |
| 3040 | }}); |
| 3041 | } |
| 3042 | } |
| 3043 | |
| 3044 | /** |
| 3045 | * Cancel the refresh task, if it's running |
| 3046 | */ |
| 3047 | private void cancelRefreshTask () { |
| 3048 | if (mConversationListRefreshTask != null) { |
| 3049 | mConversationListRefreshTask.cancel(); |
| 3050 | mConversationListRefreshTask = null; |
| 3051 | } |
| 3052 | } |
| 3053 | |
| 3054 | @Override |
Paul Westbrook | cff1aea | 2012-08-10 11:51:00 -0700 | [diff] [blame] | 3055 | public void onAnimationEnd(AnimatedAdapter animatedAdapter) { |
Paul Westbrook | 026139c | 2012-09-19 22:35:37 -0700 | [diff] [blame] | 3056 | if (mConversationListCursor == null) { |
| 3057 | LogUtils.e(LOG_TAG, "null ConversationCursor in onAnimationEnd"); |
| 3058 | return; |
| 3059 | } |
Paul Westbrook | cff1aea | 2012-08-10 11:51:00 -0700 | [diff] [blame] | 3060 | if (mConversationListCursor.isRefreshReady()) { |
Andy Huang | c1922a9 | 2013-05-13 14:33:05 -0700 | [diff] [blame] | 3061 | LogUtils.i(ConversationCursor.LOG_TAG, "Stopped animating: try sync"); |
Paul Westbrook | cff1aea | 2012-08-10 11:51:00 -0700 | [diff] [blame] | 3062 | onRefreshReady(); |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 3063 | } |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 3064 | |
Paul Westbrook | cff1aea | 2012-08-10 11:51:00 -0700 | [diff] [blame] | 3065 | if (mConversationListCursor.isRefreshRequired()) { |
Andy Huang | c1922a9 | 2013-05-13 14:33:05 -0700 | [diff] [blame] | 3066 | LogUtils.i(ConversationCursor.LOG_TAG, "Stopped animating: refresh"); |
Paul Westbrook | cff1aea | 2012-08-10 11:51:00 -0700 | [diff] [blame] | 3067 | mConversationListCursor.refresh(); |
| 3068 | } |
mindyp | 6f54e1b | 2012-10-09 09:54:08 -0700 | [diff] [blame] | 3069 | if (mRecentsDataUpdated) { |
| 3070 | mRecentsDataUpdated = false; |
| 3071 | mRecentFolderObservers.notifyChanged(); |
| 3072 | } |
Marc Blank | bf128eb | 2012-04-18 15:58:45 -0700 | [diff] [blame] | 3073 | } |
| 3074 | |
| 3075 | @Override |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 3076 | public void onSetEmpty() { |
Vikram Aggarwal | 6cadbfc | 2012-12-27 09:17:05 -0800 | [diff] [blame] | 3077 | // There are no selected conversations. Ensure that the listener and its associated actions |
| 3078 | // are blanked out. |
| 3079 | setListener(null, -1); |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 3080 | } |
| 3081 | |
| 3082 | @Override |
| 3083 | public void onSetPopulated(ConversationSelectionSet set) { |
Vikram Aggarwal | 7704d79 | 2013-01-11 15:48:24 -0800 | [diff] [blame] | 3084 | mCabActionMenu = new SelectedConversationsActionMenu(mActivity, set, mFolder); |
Vikram Aggarwal | 5b9ed2b | 2013-01-28 18:08:37 -0800 | [diff] [blame] | 3085 | if (mViewMode.isListMode() || (mIsTablet && mViewMode.isConversationMode())) { |
Vikram Aggarwal | 7704d79 | 2013-01-11 15:48:24 -0800 | [diff] [blame] | 3086 | enableCabMode(); |
Vikram Aggarwal | 6902dcf | 2012-04-11 08:57:42 -0700 | [diff] [blame] | 3087 | } |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 3088 | } |
| 3089 | |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 3090 | @Override |
| 3091 | public void onSetChanged(ConversationSelectionSet set) { |
| 3092 | // Do nothing. We don't care about changes to the set. |
| 3093 | } |
| 3094 | |
| 3095 | @Override |
| 3096 | public ConversationSelectionSet getSelectedSet() { |
| 3097 | return mSelectedSet; |
| 3098 | } |
| 3099 | |
Vikram Aggarwal | e128fc2 | 2012-04-04 12:33:34 -0700 | [diff] [blame] | 3100 | /** |
| 3101 | * Disable the Contextual Action Bar (CAB). The selected set is not changed. |
| 3102 | */ |
| 3103 | protected void disableCabMode() { |
Mindy Pereira | 8937bf1 | 2012-07-23 14:05:02 -0700 | [diff] [blame] | 3104 | // Commit any previous destructive actions when entering/ exiting CAB mode. |
mindyp | c6adce3 | 2012-08-22 18:46:42 -0700 | [diff] [blame] | 3105 | commitDestructiveActions(true); |
Vikram Aggarwal | e128fc2 | 2012-04-04 12:33:34 -0700 | [diff] [blame] | 3106 | if (mCabActionMenu != null) { |
| 3107 | mCabActionMenu.deactivate(); |
| 3108 | } |
| 3109 | } |
| 3110 | |
| 3111 | /** |
| 3112 | * Re-enable the CAB menu if required. The selection set is not changed. |
| 3113 | */ |
| 3114 | protected void enableCabMode() { |
Tony Mantler | 3d6751a | 2013-08-02 11:30:59 -0700 | [diff] [blame] | 3115 | if (mCabActionMenu != null && |
| 3116 | !(isDrawerEnabled() && mDrawerContainer.isDrawerOpen(mDrawerPullout))) { |
Vikram Aggarwal | e128fc2 | 2012-04-04 12:33:34 -0700 | [diff] [blame] | 3117 | mCabActionMenu.activate(); |
| 3118 | } |
| 3119 | } |
| 3120 | |
Vikram Aggarwal | 4eb5271 | 2012-06-19 16:24:50 -0700 | [diff] [blame] | 3121 | /** |
Tony Mantler | 43fab32 | 2013-07-26 16:38:35 -0700 | [diff] [blame] | 3122 | * Re-enable CAB mode only if we have an active selection |
| 3123 | */ |
| 3124 | protected void maybeEnableCabMode() { |
| 3125 | if (!mSelectedSet.isEmpty()) { |
Tony Mantler | a703d8a | 2013-07-31 12:00:46 -0700 | [diff] [blame] | 3126 | if (mCabActionMenu != null) { |
| 3127 | mCabActionMenu.activate(); |
| 3128 | } |
Tony Mantler | 43fab32 | 2013-07-26 16:38:35 -0700 | [diff] [blame] | 3129 | } |
| 3130 | } |
| 3131 | |
| 3132 | /** |
Vikram Aggarwal | 4eb5271 | 2012-06-19 16:24:50 -0700 | [diff] [blame] | 3133 | * Unselect conversations and exit CAB mode. |
| 3134 | */ |
| 3135 | protected final void exitCabMode() { |
| 3136 | mSelectedSet.clear(); |
| 3137 | } |
| 3138 | |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 3139 | @Override |
Mindy Pereira | fd0c297 | 2012-03-27 13:50:39 -0700 | [diff] [blame] | 3140 | public void startSearch() { |
Vikram Aggarwal | 35f19d7 | 2012-04-24 13:24:48 -0700 | [diff] [blame] | 3141 | if (mAccount == null) { |
| 3142 | // We cannot search if there is no account. Drop the request to the floor. |
| 3143 | LogUtils.d(LOG_TAG, "AbstractActivityController.startSearch(): null account"); |
| 3144 | return; |
| 3145 | } |
Mindy Pereira | fd0c297 | 2012-03-27 13:50:39 -0700 | [diff] [blame] | 3146 | if (mAccount.supportsCapability(UIProvider.AccountCapabilities.LOCAL_SEARCH) |
Andy Huang | 313ac13 | 2013-03-04 23:40:58 -0800 | [diff] [blame] | 3147 | || mAccount.supportsCapability(UIProvider.AccountCapabilities.SERVER_SEARCH)) { |
| 3148 | mActionBarView.expandSearch(); |
Mindy Pereira | fd0c297 | 2012-03-27 13:50:39 -0700 | [diff] [blame] | 3149 | } else { |
| 3150 | Toast.makeText(mActivity.getActivityContext(), mActivity.getActivityContext() |
Mindy Pereira | a46c299 | 2012-03-27 14:12:39 -0700 | [diff] [blame] | 3151 | .getString(R.string.search_unsupported), Toast.LENGTH_SHORT).show(); |
Mindy Pereira | fd0c297 | 2012-03-27 13:50:39 -0700 | [diff] [blame] | 3152 | } |
| 3153 | } |
Mindy Pereira | acf6039 | 2012-04-06 09:11:00 -0700 | [diff] [blame] | 3154 | |
Vikram Aggarwal | 0dda573 | 2012-04-06 11:20:16 -0700 | [diff] [blame] | 3155 | @Override |
| 3156 | public void exitSearchMode() { |
| 3157 | if (mViewMode.getMode() == ViewMode.SEARCH_RESULTS_LIST) { |
| 3158 | mActivity.finish(); |
| 3159 | } |
| 3160 | } |
| 3161 | |
Mindy Pereira | acf6039 | 2012-04-06 09:11:00 -0700 | [diff] [blame] | 3162 | /** |
| 3163 | * Supports dragging conversations to a folder. |
| 3164 | */ |
| 3165 | @Override |
| 3166 | public boolean supportsDrag(DragEvent event, Folder folder) { |
| 3167 | return (folder != null |
| 3168 | && event != null |
| 3169 | && event.getClipDescription() != null |
| 3170 | && folder.supportsCapability |
| 3171 | (UIProvider.FolderCapabilities.CAN_ACCEPT_MOVED_MESSAGES) |
| 3172 | && folder.supportsCapability |
| 3173 | (UIProvider.FolderCapabilities.CAN_HOLD_MAIL) |
Scott Kennedy | 259df5b | 2013-07-11 13:24:01 -0700 | [diff] [blame] | 3174 | && !mFolder.equals(folder)); |
Mindy Pereira | acf6039 | 2012-04-06 09:11:00 -0700 | [diff] [blame] | 3175 | } |
| 3176 | |
| 3177 | /** |
Mindy Pereira | 6c2663d | 2012-07-20 15:37:29 -0700 | [diff] [blame] | 3178 | * Handles dropping conversations to a folder. |
Mindy Pereira | acf6039 | 2012-04-06 09:11:00 -0700 | [diff] [blame] | 3179 | */ |
| 3180 | @Override |
| 3181 | public void handleDrop(DragEvent event, final Folder folder) { |
Mindy Pereira | acf6039 | 2012-04-06 09:11:00 -0700 | [diff] [blame] | 3182 | if (!supportsDrag(event, folder)) { |
| 3183 | return; |
| 3184 | } |
Scott Kennedy | 8c1058e | 2013-03-20 13:40:20 -0700 | [diff] [blame] | 3185 | if (folder.isType(UIProvider.FolderType.STARRED)) { |
mindyp | ae7e6a0 | 2012-11-29 13:28:10 -0800 | [diff] [blame] | 3186 | // Moving a conversation to the starred folder adds the star and |
| 3187 | // removes the current label |
| 3188 | handleDropInStarred(folder); |
| 3189 | return; |
| 3190 | } |
Scott Kennedy | 8c1058e | 2013-03-20 13:40:20 -0700 | [diff] [blame] | 3191 | if (mFolder.isType(UIProvider.FolderType.STARRED)) { |
mindyp | ae7e6a0 | 2012-11-29 13:28:10 -0800 | [diff] [blame] | 3192 | handleDragFromStarred(folder); |
| 3193 | return; |
| 3194 | } |
mindyp | a849263 | 2012-09-24 09:27:54 -0700 | [diff] [blame] | 3195 | final ArrayList<FolderOperation> dragDropOperations = new ArrayList<FolderOperation>(); |
mindyp | ae7e6a0 | 2012-11-29 13:28:10 -0800 | [diff] [blame] | 3196 | final Collection<Conversation> conversations = mSelectedSet.values(); |
mindyp | a849263 | 2012-09-24 09:27:54 -0700 | [diff] [blame] | 3197 | // Add the drop target folder. |
| 3198 | dragDropOperations.add(new FolderOperation(folder, true)); |
| 3199 | // Remove the current folder unless the user is viewing "all". |
| 3200 | // That operation should just add the new folder. |
| 3201 | boolean isDestructive = !mFolder.isViewAll() |
| 3202 | && mFolder.supportsCapability |
| 3203 | (UIProvider.FolderCapabilities.CAN_ACCEPT_MOVED_MESSAGES); |
| 3204 | if (isDestructive) { |
| 3205 | dragDropOperations.add(new FolderOperation(mFolder, false)); |
| 3206 | } |
Mindy Pereira | 8db7e40 | 2012-07-13 10:32:47 -0700 | [diff] [blame] | 3207 | // Drag and drop is destructive: we remove conversations from the |
| 3208 | // current folder. |
Scott Kennedy | 6a3d5ce | 2013-03-15 17:33:14 -0700 | [diff] [blame] | 3209 | final DestructiveAction action = |
| 3210 | getFolderChange(conversations, dragDropOperations, isDestructive, |
| 3211 | true /* isBatch */, true /* showUndo */, true /* isMoveTo */, folder); |
mindyp | a849263 | 2012-09-24 09:27:54 -0700 | [diff] [blame] | 3212 | if (isDestructive) { |
Scott Kennedy | caaeed3 | 2013-06-12 13:39:16 -0700 | [diff] [blame] | 3213 | delete(0, conversations, action, true); |
mindyp | a849263 | 2012-09-24 09:27:54 -0700 | [diff] [blame] | 3214 | } else { |
| 3215 | action.performAction(); |
| 3216 | } |
Mindy Pereira | acf6039 | 2012-04-06 09:11:00 -0700 | [diff] [blame] | 3217 | } |
Mindy Pereira | 0963ef8 | 2012-04-10 11:43:01 -0700 | [diff] [blame] | 3218 | |
mindyp | ae7e6a0 | 2012-11-29 13:28:10 -0800 | [diff] [blame] | 3219 | private void handleDragFromStarred(Folder folder) { |
| 3220 | final Collection<Conversation> conversations = mSelectedSet.values(); |
| 3221 | // The conversation list deletes and performs the action if it exists. |
| 3222 | final ConversationListFragment convListFragment = getConversationListFragment(); |
| 3223 | // There should always be a convlistfragment, or the user could not have |
| 3224 | // dragged/ dropped conversations. |
| 3225 | if (convListFragment != null) { |
| 3226 | LogUtils.d(LOG_TAG, "AAC.requestDelete: ListFragment is handling delete."); |
| 3227 | ArrayList<ConversationOperation> ops = new ArrayList<ConversationOperation>(); |
mindyp | cb0b30e | 2012-11-30 10:16:35 -0800 | [diff] [blame] | 3228 | ArrayList<Uri> folderUris; |
| 3229 | ArrayList<Boolean> adds; |
mindyp | ae7e6a0 | 2012-11-29 13:28:10 -0800 | [diff] [blame] | 3230 | for (Conversation target : conversations) { |
mindyp | cb0b30e | 2012-11-30 10:16:35 -0800 | [diff] [blame] | 3231 | folderUris = new ArrayList<Uri>(); |
| 3232 | adds = new ArrayList<Boolean>(); |
Scott Kennedy | 259df5b | 2013-07-11 13:24:01 -0700 | [diff] [blame] | 3233 | folderUris.add(folder.folderUri.fullUri); |
mindyp | cb0b30e | 2012-11-30 10:16:35 -0800 | [diff] [blame] | 3234 | adds.add(Boolean.TRUE); |
Paul Westbrook | 26746eb | 2012-12-06 14:44:01 -0800 | [diff] [blame] | 3235 | final HashMap<Uri, Folder> targetFolders = |
| 3236 | Folder.hashMapForFolders(target.getRawFolders()); |
Scott Kennedy | 259df5b | 2013-07-11 13:24:01 -0700 | [diff] [blame] | 3237 | targetFolders.put(folder.folderUri.fullUri, folder); |
Paul Westbrook | 26746eb | 2012-12-06 14:44:01 -0800 | [diff] [blame] | 3238 | ops.add(mConversationListCursor.getConversationFolderOperation(target, |
| 3239 | folderUris, adds, targetFolders.values())); |
mindyp | ae7e6a0 | 2012-11-29 13:28:10 -0800 | [diff] [blame] | 3240 | } |
| 3241 | if (mConversationListCursor != null) { |
Scott Kennedy | 9e2d407 | 2013-03-21 21:46:01 -0700 | [diff] [blame] | 3242 | mConversationListCursor.updateBulkValues(ops); |
mindyp | ae7e6a0 | 2012-11-29 13:28:10 -0800 | [diff] [blame] | 3243 | } |
| 3244 | refreshConversationList(); |
| 3245 | mSelectedSet.clear(); |
mindyp | ae7e6a0 | 2012-11-29 13:28:10 -0800 | [diff] [blame] | 3246 | } |
| 3247 | } |
| 3248 | |
| 3249 | private void handleDropInStarred(Folder folder) { |
| 3250 | final Collection<Conversation> conversations = mSelectedSet.values(); |
| 3251 | // The conversation list deletes and performs the action if it exists. |
| 3252 | final ConversationListFragment convListFragment = getConversationListFragment(); |
| 3253 | // There should always be a convlistfragment, or the user could not have |
| 3254 | // dragged/ dropped conversations. |
| 3255 | if (convListFragment != null) { |
| 3256 | LogUtils.d(LOG_TAG, "AAC.requestDelete: ListFragment is handling delete."); |
Andrew Sapperstein | 6c570db | 2013-08-06 17:21:36 -0700 | [diff] [blame] | 3257 | convListFragment.requestDelete(R.id.change_folders, conversations, |
mindyp | 5cc0ab2 | 2012-12-11 08:47:35 -0800 | [diff] [blame] | 3258 | new DroppedInStarredAction(conversations, mFolder, folder)); |
mindyp | ae7e6a0 | 2012-11-29 13:28:10 -0800 | [diff] [blame] | 3259 | } |
| 3260 | } |
| 3261 | |
| 3262 | // When dragging conversations to the starred folder, remove from the |
| 3263 | // original folder and add a star |
| 3264 | private class DroppedInStarredAction implements DestructiveAction { |
| 3265 | private Collection<Conversation> mConversations; |
| 3266 | private Folder mInitialFolder; |
mindyp | 5cc0ab2 | 2012-12-11 08:47:35 -0800 | [diff] [blame] | 3267 | private Folder mStarred; |
mindyp | ae7e6a0 | 2012-11-29 13:28:10 -0800 | [diff] [blame] | 3268 | |
mindyp | 5cc0ab2 | 2012-12-11 08:47:35 -0800 | [diff] [blame] | 3269 | public DroppedInStarredAction(Collection<Conversation> conversations, Folder initialFolder, |
| 3270 | Folder starredFolder) { |
mindyp | ae7e6a0 | 2012-11-29 13:28:10 -0800 | [diff] [blame] | 3271 | mConversations = conversations; |
mindyp | 5cc0ab2 | 2012-12-11 08:47:35 -0800 | [diff] [blame] | 3272 | mInitialFolder = initialFolder; |
| 3273 | mStarred = starredFolder; |
mindyp | ae7e6a0 | 2012-11-29 13:28:10 -0800 | [diff] [blame] | 3274 | } |
| 3275 | |
| 3276 | @Override |
| 3277 | public void performAction() { |
| 3278 | ToastBarOperation undoOp = new ToastBarOperation(mConversations.size(), |
Andrew Sapperstein | 6c570db | 2013-08-06 17:21:36 -0700 | [diff] [blame] | 3279 | R.id.change_folders, ToastBarOperation.UNDO, true /* batch */, mInitialFolder); |
mindyp | ae7e6a0 | 2012-11-29 13:28:10 -0800 | [diff] [blame] | 3280 | onUndoAvailable(undoOp); |
| 3281 | ArrayList<ConversationOperation> ops = new ArrayList<ConversationOperation>(); |
| 3282 | ContentValues values = new ContentValues(); |
mindyp | cb0b30e | 2012-11-30 10:16:35 -0800 | [diff] [blame] | 3283 | ArrayList<Uri> folderUris; |
| 3284 | ArrayList<Boolean> adds; |
mindyp | 5cc0ab2 | 2012-12-11 08:47:35 -0800 | [diff] [blame] | 3285 | ConversationOperation operation; |
mindyp | ae7e6a0 | 2012-11-29 13:28:10 -0800 | [diff] [blame] | 3286 | for (Conversation target : mConversations) { |
mindyp | cb0b30e | 2012-11-30 10:16:35 -0800 | [diff] [blame] | 3287 | folderUris = new ArrayList<Uri>(); |
| 3288 | adds = new ArrayList<Boolean>(); |
Scott Kennedy | 259df5b | 2013-07-11 13:24:01 -0700 | [diff] [blame] | 3289 | folderUris.add(mStarred.folderUri.fullUri); |
mindyp | 5cc0ab2 | 2012-12-11 08:47:35 -0800 | [diff] [blame] | 3290 | adds.add(Boolean.TRUE); |
Scott Kennedy | 259df5b | 2013-07-11 13:24:01 -0700 | [diff] [blame] | 3291 | folderUris.add(mInitialFolder.folderUri.fullUri); |
mindyp | cb0b30e | 2012-11-30 10:16:35 -0800 | [diff] [blame] | 3292 | adds.add(Boolean.FALSE); |
mindyp | 5cc0ab2 | 2012-12-11 08:47:35 -0800 | [diff] [blame] | 3293 | final HashMap<Uri, Folder> targetFolders = |
| 3294 | Folder.hashMapForFolders(target.getRawFolders()); |
Scott Kennedy | 259df5b | 2013-07-11 13:24:01 -0700 | [diff] [blame] | 3295 | targetFolders.put(mStarred.folderUri.fullUri, mStarred); |
| 3296 | targetFolders.remove(mInitialFolder.folderUri.fullUri); |
mindyp | 5cc0ab2 | 2012-12-11 08:47:35 -0800 | [diff] [blame] | 3297 | values.put(ConversationColumns.STARRED, true); |
| 3298 | operation = mConversationListCursor.getConversationFolderOperation(target, |
| 3299 | folderUris, adds, targetFolders.values(), values); |
| 3300 | ops.add(operation); |
mindyp | ae7e6a0 | 2012-11-29 13:28:10 -0800 | [diff] [blame] | 3301 | } |
| 3302 | if (mConversationListCursor != null) { |
Scott Kennedy | 9e2d407 | 2013-03-21 21:46:01 -0700 | [diff] [blame] | 3303 | mConversationListCursor.updateBulkValues(ops); |
mindyp | ae7e6a0 | 2012-11-29 13:28:10 -0800 | [diff] [blame] | 3304 | } |
| 3305 | refreshConversationList(); |
| 3306 | mSelectedSet.clear(); |
| 3307 | } |
| 3308 | } |
| 3309 | |
Mindy Pereira | 0963ef8 | 2012-04-10 11:43:01 -0700 | [diff] [blame] | 3310 | @Override |
Mindy Pereira | 0963ef8 | 2012-04-10 11:43:01 -0700 | [diff] [blame] | 3311 | public void onTouchEvent(MotionEvent event) { |
| 3312 | if (event.getAction() == MotionEvent.ACTION_DOWN) { |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 3313 | if (mToastBar != null && !mToastBar.isEventInToastBar(event)) { |
Mark Wei | d243d45 | 2012-10-31 16:24:08 -0700 | [diff] [blame] | 3314 | hideOrRepositionToastBar(true); |
Mindy Pereira | 0963ef8 | 2012-04-10 11:43:01 -0700 | [diff] [blame] | 3315 | } |
| 3316 | } |
| 3317 | } |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 3318 | |
Mark Wei | d243d45 | 2012-10-31 16:24:08 -0700 | [diff] [blame] | 3319 | protected abstract void hideOrRepositionToastBar(boolean animated); |
| 3320 | |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 3321 | @Override |
Scott Kennedy | 3b965d7 | 2013-06-25 14:36:55 -0700 | [diff] [blame] | 3322 | public void onConversationSeen() { |
| 3323 | mPagerController.onConversationSeen(); |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 3324 | } |
| 3325 | |
Andy Huang | 9d3fd92 | 2012-09-26 22:23:58 -0700 | [diff] [blame] | 3326 | @Override |
| 3327 | public boolean isInitialConversationLoading() { |
| 3328 | return mPagerController.isInitialConversationLoading(); |
| 3329 | } |
| 3330 | |
Vikram Aggarwal | 6422b8d | 2013-01-14 10:47:41 -0800 | [diff] [blame] | 3331 | /** |
| 3332 | * Check if the fragment given here is visible. Checking {@link Fragment#isVisible()} is |
| 3333 | * insufficient because that doesn't check if the window is currently in focus or not. |
| 3334 | */ |
Vikram Aggarwal | 715f83d | 2013-03-05 17:04:56 -0800 | [diff] [blame] | 3335 | private boolean isFragmentVisible(Fragment in) { |
Vikram Aggarwal | 6422b8d | 2013-01-14 10:47:41 -0800 | [diff] [blame] | 3336 | return in != null && in.isVisible() && mActivity.hasWindowFocus(); |
| 3337 | } |
| 3338 | |
Vikram Aggarwal | d70fe49 | 2013-06-04 12:52:07 -0700 | [diff] [blame] | 3339 | /** |
| 3340 | * This class handles callbacks that create a {@link ConversationCursor}. |
| 3341 | */ |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 3342 | private class ConversationListLoaderCallbacks implements |
| 3343 | LoaderManager.LoaderCallbacks<ConversationCursor> { |
| 3344 | |
| 3345 | @Override |
| 3346 | public Loader<ConversationCursor> onCreateLoader(int id, Bundle args) { |
Vikram Aggarwal | a1b59dc | 2013-04-30 15:45:49 -0700 | [diff] [blame] | 3347 | final Account account = args.getParcelable(BUNDLE_ACCOUNT_KEY); |
| 3348 | final Folder folder = args.getParcelable(BUNDLE_FOLDER_KEY); |
| 3349 | if (account == null || folder == null) { |
| 3350 | return null; |
| 3351 | } |
| 3352 | return new ConversationCursorLoader((Activity) mActivity, account, |
| 3353 | folder.conversationListUri, folder.name); |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 3354 | } |
| 3355 | |
| 3356 | @Override |
| 3357 | public void onLoadFinished(Loader<ConversationCursor> loader, ConversationCursor data) { |
Andy Huang | 144bfe7 | 2013-06-11 13:27:52 -0700 | [diff] [blame] | 3358 | LogUtils.d(LOG_TAG, |
| 3359 | "IN AAC.ConversationCursor.onLoadFinished, data=%s loader=%s this=%s", |
| 3360 | data, loader, this); |
Andrew Sapperstein | a3ce678 | 2013-05-09 15:14:49 -0700 | [diff] [blame] | 3361 | if (isDrawerEnabled() && mDrawerListener.getDrawerState() != DrawerLayout.STATE_IDLE) { |
Vikram Aggarwal | a1b59dc | 2013-04-30 15:45:49 -0700 | [diff] [blame] | 3362 | LogUtils.d(LOG_TAG, "ConversationListLoaderCallbacks.onLoadFinished: ignoring."); |
Andrew Sapperstein | a3ce678 | 2013-05-09 15:14:49 -0700 | [diff] [blame] | 3363 | mConversationListLoadFinishedIgnored = true; |
Vikram Aggarwal | a1b59dc | 2013-04-30 15:45:49 -0700 | [diff] [blame] | 3364 | return; |
| 3365 | } |
Vikram Aggarwal | e8a8532 | 2012-04-24 09:01:38 -0700 | [diff] [blame] | 3366 | // Clear our all pending destructive actions before swapping the conversation cursor |
| 3367 | destroyPending(null); |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 3368 | mConversationListCursor = data; |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 3369 | mConversationListCursor.addListener(AbstractActivityController.this); |
Andy Huang | 144bfe7 | 2013-06-11 13:27:52 -0700 | [diff] [blame] | 3370 | mDrawIdler.setListener(mConversationListCursor); |
Paul Westbrook | 937c94f | 2012-08-16 13:01:18 -0700 | [diff] [blame] | 3371 | mTracker.onCursorUpdated(); |
Andy Huang | e3df1ad | 2012-04-24 17:15:23 -0700 | [diff] [blame] | 3372 | mConversationListObservable.notifyChanged(); |
Yu Ping Hu | 7c909c7 | 2013-01-18 11:58:01 -0800 | [diff] [blame] | 3373 | // Handle actions that were deferred until after the conversation list was loaded. |
| 3374 | for (LoadFinishedCallback callback : mConversationListLoadFinishedCallbacks) { |
| 3375 | callback.onLoadFinished(); |
| 3376 | } |
| 3377 | mConversationListLoadFinishedCallbacks.clear(); |
Paul Westbrook | 9f119c7 | 2012-04-24 16:10:59 -0700 | [diff] [blame] | 3378 | |
Vikram Aggarwal | 17f373e | 2012-09-17 15:49:32 -0700 | [diff] [blame] | 3379 | final ConversationListFragment convList = getConversationListFragment(); |
Vikram Aggarwal | 6422b8d | 2013-01-14 10:47:41 -0800 | [diff] [blame] | 3380 | if (isFragmentVisible(convList)) { |
Vikram Aggarwal | 17f373e | 2012-09-17 15:49:32 -0700 | [diff] [blame] | 3381 | // The conversation list is already listening to list changes and gets notified |
| 3382 | // in the mConversationListObservable.notifyChanged() line above. We only need to |
| 3383 | // check and inform the cursor of the change in visibility here. |
| 3384 | informCursorVisiblity(true); |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 3385 | } |
Vikram Aggarwal | f0ef230 | 2012-11-07 14:53:34 -0800 | [diff] [blame] | 3386 | perhapsShowFirstSearchResult(); |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 3387 | } |
| 3388 | |
| 3389 | @Override |
| 3390 | public void onLoaderReset(Loader<ConversationCursor> loader) { |
Andy Huang | 144bfe7 | 2013-06-11 13:27:52 -0700 | [diff] [blame] | 3391 | LogUtils.d(LOG_TAG, |
| 3392 | "IN AAC.ConversationCursor.onLoaderReset, data=%s loader=%s this=%s", |
| 3393 | mConversationListCursor, loader, this); |
Paul Westbrook | 9a70e91 | 2012-08-17 15:53:20 -0700 | [diff] [blame] | 3394 | |
| 3395 | if (mConversationListCursor != null) { |
| 3396 | // Unregister the listener |
| 3397 | mConversationListCursor.removeListener(AbstractActivityController.this); |
Andy Huang | 144bfe7 | 2013-06-11 13:27:52 -0700 | [diff] [blame] | 3398 | mDrawIdler.setListener(null); |
Paul Westbrook | 9a70e91 | 2012-08-17 15:53:20 -0700 | [diff] [blame] | 3399 | mConversationListCursor = null; |
| 3400 | |
| 3401 | // Inform anyone who is interested about the change |
| 3402 | mTracker.onCursorUpdated(); |
| 3403 | mConversationListObservable.notifyChanged(); |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 3404 | } |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 3405 | } |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 3406 | } |
Andy Huang | b1c34dc | 2012-04-17 16:36:19 -0700 | [diff] [blame] | 3407 | |
Vikram Aggarwal | e8a8532 | 2012-04-24 09:01:38 -0700 | [diff] [blame] | 3408 | /** |
Vikram Aggarwal | 177097f | 2013-03-08 11:19:53 -0800 | [diff] [blame] | 3409 | * Class to perform {@link LoaderManager.LoaderCallbacks} for creating {@link Folder} objects. |
| 3410 | */ |
| 3411 | private class FolderLoads implements LoaderManager.LoaderCallbacks<ObjectCursor<Folder>> { |
| 3412 | @Override |
| 3413 | public Loader<ObjectCursor<Folder>> onCreateLoader(int id, Bundle args) { |
| 3414 | final String[] everything = UIProvider.FOLDERS_PROJECTION; |
| 3415 | switch (id) { |
| 3416 | case LOADER_FOLDER_CURSOR: |
| 3417 | LogUtils.d(LOG_TAG, "LOADER_FOLDER_CURSOR created"); |
| 3418 | final ObjectCursorLoader<Folder> loader = new |
| 3419 | ObjectCursorLoader<Folder>( |
Scott Kennedy | 259df5b | 2013-07-11 13:24:01 -0700 | [diff] [blame] | 3420 | mContext, mFolder.folderUri.fullUri, everything, Folder.FACTORY); |
Vikram Aggarwal | 177097f | 2013-03-08 11:19:53 -0800 | [diff] [blame] | 3421 | loader.setUpdateThrottle(mFolderItemUpdateDelayMs); |
| 3422 | return loader; |
| 3423 | case LOADER_RECENT_FOLDERS: |
| 3424 | LogUtils.d(LOG_TAG, "LOADER_RECENT_FOLDERS created"); |
Yu Ping Hu | f2632b9 | 2013-03-15 13:56:27 -0700 | [diff] [blame] | 3425 | if (mAccount != null && mAccount.recentFolderListUri != null |
| 3426 | && !mAccount.recentFolderListUri.equals(Uri.EMPTY)) { |
Vikram Aggarwal | 177097f | 2013-03-08 11:19:53 -0800 | [diff] [blame] | 3427 | return new ObjectCursorLoader<Folder>(mContext, |
| 3428 | mAccount.recentFolderListUri, everything, Folder.FACTORY); |
| 3429 | } |
| 3430 | break; |
| 3431 | case LOADER_ACCOUNT_INBOX: |
| 3432 | LogUtils.d(LOG_TAG, "LOADER_ACCOUNT_INBOX created"); |
| 3433 | final Uri defaultInbox = Settings.getDefaultInboxUri(mAccount.settings); |
| 3434 | final Uri inboxUri = defaultInbox.equals(Uri.EMPTY) ? |
| 3435 | mAccount.folderListUri : defaultInbox; |
| 3436 | LogUtils.d(LOG_TAG, "Loading the default inbox: %s", inboxUri); |
| 3437 | if (inboxUri != null) { |
| 3438 | return new ObjectCursorLoader<Folder>(mContext, inboxUri, |
| 3439 | everything, Folder.FACTORY); |
| 3440 | } |
| 3441 | break; |
| 3442 | case LOADER_SEARCH: |
| 3443 | LogUtils.d(LOG_TAG, "LOADER_SEARCH created"); |
| 3444 | return Folder.forSearchResults(mAccount, |
| 3445 | args.getString(ConversationListContext.EXTRA_SEARCH_QUERY), |
| 3446 | mActivity.getActivityContext()); |
| 3447 | case LOADER_FIRST_FOLDER: |
| 3448 | LogUtils.d(LOG_TAG, "LOADER_FIRST_FOLDER created"); |
| 3449 | final Uri folderUri = args.getParcelable(Utils.EXTRA_FOLDER_URI); |
| 3450 | mConversationToShow = args.getParcelable(Utils.EXTRA_CONVERSATION); |
| 3451 | if (mConversationToShow != null && mConversationToShow.position < 0){ |
| 3452 | mConversationToShow.position = 0; |
| 3453 | } |
| 3454 | return new ObjectCursorLoader<Folder>(mContext, folderUri, |
| 3455 | everything, Folder.FACTORY); |
| 3456 | default: |
| 3457 | LogUtils.wtf(LOG_TAG, "FolderLoads.onCreateLoader(%d) for invalid id", id); |
| 3458 | return null; |
| 3459 | } |
| 3460 | return null; |
| 3461 | } |
| 3462 | |
| 3463 | @Override |
| 3464 | public void onLoadFinished(Loader<ObjectCursor<Folder>> loader, ObjectCursor<Folder> data) { |
| 3465 | if (data == null) { |
| 3466 | LogUtils.e(LOG_TAG, "Received null cursor from loader id: %d", loader.getId()); |
| 3467 | } |
| 3468 | switch (loader.getId()) { |
| 3469 | case LOADER_FOLDER_CURSOR: |
| 3470 | if (data != null && data.moveToFirst()) { |
| 3471 | final Folder folder = data.getModel(); |
| 3472 | setHasFolderChanged(folder); |
| 3473 | mFolder = folder; |
| 3474 | mFolderObservable.notifyChanged(); |
| 3475 | } else { |
| 3476 | LogUtils.d(LOG_TAG, "Unable to get the folder %s", |
| 3477 | mFolder != null ? mAccount.name : ""); |
| 3478 | } |
| 3479 | break; |
| 3480 | case LOADER_RECENT_FOLDERS: |
| 3481 | // Few recent folders and we are running on a phone? Populate the default |
| 3482 | // recents. The number of default recent folders is at least 2: every provider |
| 3483 | // has at least two folders, and the recent folder count never decreases. |
| 3484 | // Having a single recent folder is an erroneous case, and we can gracefully |
| 3485 | // recover by populating default recents. The default recents will not stomp on |
| 3486 | // the existing value: it will be shown in addition to the default folders: |
| 3487 | // the max number of recent folders is more than 1+num(defaultRecents). |
| 3488 | if (data != null && data.getCount() <= 1 && !mIsTablet) { |
| 3489 | final class PopulateDefault extends AsyncTask<Uri, Void, Void> { |
| 3490 | @Override |
| 3491 | protected Void doInBackground(Uri... uri) { |
| 3492 | // Asking for an update on the URI and ignore the result. |
| 3493 | final ContentResolver resolver = mContext.getContentResolver(); |
| 3494 | resolver.update(uri[0], null, null, null); |
| 3495 | return null; |
| 3496 | } |
| 3497 | } |
| 3498 | final Uri uri = mAccount.defaultRecentFolderListUri; |
| 3499 | LogUtils.v(LOG_TAG, "Default recents at %s", uri); |
| 3500 | new PopulateDefault().execute(uri); |
| 3501 | break; |
| 3502 | } |
| 3503 | LogUtils.v(LOG_TAG, "Reading recent folders from the cursor."); |
| 3504 | mRecentFolderList.loadFromUiProvider(data); |
| 3505 | if (isAnimating()) { |
| 3506 | mRecentsDataUpdated = true; |
| 3507 | } else { |
| 3508 | mRecentFolderObservers.notifyChanged(); |
| 3509 | } |
| 3510 | break; |
| 3511 | case LOADER_ACCOUNT_INBOX: |
| 3512 | if (data != null && !data.isClosed() && data.moveToFirst()) { |
| 3513 | final Folder inbox = data.getModel(); |
Scott Kennedy | ad41814 | 2013-07-10 17:18:22 -0700 | [diff] [blame] | 3514 | onFolderChanged(inbox, false /* force */); |
Vikram Aggarwal | 177097f | 2013-03-08 11:19:53 -0800 | [diff] [blame] | 3515 | // Just want to get the inbox, don't care about updates to it |
| 3516 | // as this will be tracked by the folder change listener. |
| 3517 | mActivity.getLoaderManager().destroyLoader(LOADER_ACCOUNT_INBOX); |
| 3518 | } else { |
| 3519 | LogUtils.d(LOG_TAG, "Unable to get the account inbox for account %s", |
| 3520 | mAccount != null ? mAccount.name : ""); |
| 3521 | } |
| 3522 | break; |
| 3523 | case LOADER_SEARCH: |
| 3524 | if (data != null && data.getCount() > 0) { |
| 3525 | data.moveToFirst(); |
| 3526 | final Folder search = data.getModel(); |
| 3527 | updateFolder(search); |
| 3528 | mConvListContext = ConversationListContext.forSearchQuery(mAccount, mFolder, |
| 3529 | mActivity.getIntent() |
| 3530 | .getStringExtra(UIProvider.SearchQueryParameters.QUERY)); |
| 3531 | showConversationList(mConvListContext); |
| 3532 | mActivity.invalidateOptionsMenu(); |
| 3533 | mHaveSearchResults = search.totalCount > 0; |
| 3534 | mActivity.getLoaderManager().destroyLoader(LOADER_SEARCH); |
| 3535 | } else { |
| 3536 | LogUtils.e(LOG_TAG, "Null/empty cursor returned by LOADER_SEARCH loader"); |
| 3537 | } |
| 3538 | break; |
| 3539 | case LOADER_FIRST_FOLDER: |
| 3540 | if (data == null || data.getCount() <=0 || !data.moveToFirst()) { |
| 3541 | return; |
| 3542 | } |
| 3543 | final Folder folder = data.getModel(); |
| 3544 | boolean handled = false; |
| 3545 | if (folder != null) { |
Scott Kennedy | ad41814 | 2013-07-10 17:18:22 -0700 | [diff] [blame] | 3546 | onFolderChanged(folder, false /* force */); |
Vikram Aggarwal | 177097f | 2013-03-08 11:19:53 -0800 | [diff] [blame] | 3547 | handled = true; |
| 3548 | } |
| 3549 | if (mConversationToShow != null) { |
| 3550 | // Open the conversation. |
| 3551 | showConversation(mConversationToShow); |
| 3552 | handled = true; |
| 3553 | } |
| 3554 | if (!handled) { |
| 3555 | // We have an account, but nothing else: load the default inbox. |
| 3556 | loadAccountInbox(); |
| 3557 | } |
| 3558 | mConversationToShow = null; |
| 3559 | // And don't run this anymore. |
| 3560 | mActivity.getLoaderManager().destroyLoader(LOADER_FIRST_FOLDER); |
| 3561 | break; |
| 3562 | } |
| 3563 | } |
| 3564 | |
| 3565 | @Override |
| 3566 | public void onLoaderReset(Loader<ObjectCursor<Folder>> loader) { |
| 3567 | } |
| 3568 | } |
| 3569 | |
| 3570 | /** |
| 3571 | * Class to perform {@link LoaderManager.LoaderCallbacks} for creating {@link Account} objects. |
| 3572 | */ |
| 3573 | private class AccountLoads implements LoaderManager.LoaderCallbacks<ObjectCursor<Account>> { |
| 3574 | final String[] mProjection = UIProvider.ACCOUNTS_PROJECTION; |
| 3575 | final CursorCreator<Account> mFactory = Account.FACTORY; |
| 3576 | |
| 3577 | @Override |
| 3578 | public Loader<ObjectCursor<Account>> onCreateLoader(int id, Bundle args) { |
| 3579 | switch (id) { |
| 3580 | case LOADER_ACCOUNT_CURSOR: |
| 3581 | LogUtils.d(LOG_TAG, "LOADER_ACCOUNT_CURSOR created"); |
| 3582 | return new ObjectCursorLoader<Account>(mContext, |
| 3583 | MailAppProvider.getAccountsUri(), mProjection, mFactory); |
| 3584 | case LOADER_ACCOUNT_UPDATE_CURSOR: |
| 3585 | LogUtils.d(LOG_TAG, "LOADER_ACCOUNT_UPDATE_CURSOR created"); |
| 3586 | return new ObjectCursorLoader<Account>(mContext, mAccount.uri, mProjection, |
| 3587 | mFactory); |
| 3588 | default: |
| 3589 | LogUtils.wtf(LOG_TAG, "Got an id (%d) that I cannot create!", id); |
| 3590 | break; |
| 3591 | } |
| 3592 | return null; |
| 3593 | } |
| 3594 | |
| 3595 | @Override |
| 3596 | public void onLoadFinished(Loader<ObjectCursor<Account>> loader, |
| 3597 | ObjectCursor<Account> data) { |
| 3598 | if (data == null) { |
| 3599 | LogUtils.e(LOG_TAG, "Received null cursor from loader id: %d", loader.getId()); |
| 3600 | } |
| 3601 | switch (loader.getId()) { |
| 3602 | case LOADER_ACCOUNT_CURSOR: |
Vikram Aggarwal | d70fe49 | 2013-06-04 12:52:07 -0700 | [diff] [blame] | 3603 | // We have received an update on the list of accounts. |
Vikram Aggarwal | 177097f | 2013-03-08 11:19:53 -0800 | [diff] [blame] | 3604 | if (data == null) { |
| 3605 | // Nothing useful to do if we have no valid data. |
| 3606 | break; |
| 3607 | } |
Andy Huang | 761522c | 2013-08-08 13:09:11 -0700 | [diff] [blame] | 3608 | final long count = data.getCount(); |
| 3609 | if (count == 0) { |
Vikram Aggarwal | 177097f | 2013-03-08 11:19:53 -0800 | [diff] [blame] | 3610 | // If an empty cursor is returned, the MailAppProvider is indicating that |
| 3611 | // no accounts have been specified. We want to navigate to the |
| 3612 | // "add account" activity that will handle the intent returned by the |
| 3613 | // MailAppProvider |
| 3614 | |
| 3615 | // If the MailAppProvider believes that all accounts have been loaded, |
| 3616 | // and the account list is still empty, we want to prompt the user to add |
| 3617 | // an account. |
| 3618 | final Bundle extras = data.getExtras(); |
| 3619 | final boolean accountsLoaded = |
| 3620 | extras.getInt(AccountCursorExtraKeys.ACCOUNTS_LOADED) != 0; |
| 3621 | |
| 3622 | if (accountsLoaded) { |
| 3623 | final Intent noAccountIntent = MailAppProvider.getNoAccountIntent |
| 3624 | (mContext); |
| 3625 | if (noAccountIntent != null) { |
| 3626 | mActivity.startActivityForResult(noAccountIntent, |
| 3627 | ADD_ACCOUNT_REQUEST_CODE); |
| 3628 | } |
| 3629 | } |
| 3630 | } else { |
| 3631 | final boolean accountListUpdated = accountsUpdated(data); |
Vikram Aggarwal | c04cf7e | 2013-05-13 15:38:42 -0700 | [diff] [blame] | 3632 | if (!mHaveAccountList || accountListUpdated) { |
| 3633 | mHaveAccountList = updateAccounts(data); |
Vikram Aggarwal | 177097f | 2013-03-08 11:19:53 -0800 | [diff] [blame] | 3634 | } |
Andy Huang | 761522c | 2013-08-08 13:09:11 -0700 | [diff] [blame] | 3635 | Analytics.getInstance().setCustomDimension(Analytics.CD_INDEX_ACCOUNT_COUNT, |
| 3636 | Long.toString(count)); |
Vikram Aggarwal | 177097f | 2013-03-08 11:19:53 -0800 | [diff] [blame] | 3637 | } |
| 3638 | break; |
| 3639 | case LOADER_ACCOUNT_UPDATE_CURSOR: |
| 3640 | // We have received an update for current account. |
Vikram Aggarwal | 177097f | 2013-03-08 11:19:53 -0800 | [diff] [blame] | 3641 | if (data != null && data.moveToFirst()) { |
| 3642 | final Account updatedAccount = data.getModel(); |
Vikram Aggarwal | d70fe49 | 2013-06-04 12:52:07 -0700 | [diff] [blame] | 3643 | // Make sure that this is an update for the current account |
Vikram Aggarwal | 177097f | 2013-03-08 11:19:53 -0800 | [diff] [blame] | 3644 | if (updatedAccount.uri.equals(mAccount.uri)) { |
Vikram Aggarwal | 177097f | 2013-03-08 11:19:53 -0800 | [diff] [blame] | 3645 | final Settings previousSettings = mAccount.settings; |
| 3646 | |
| 3647 | // Update the controller's reference to the current account |
| 3648 | mAccount = updatedAccount; |
| 3649 | LogUtils.d(LOG_TAG, "AbstractActivityController.onLoadFinished(): " |
| 3650 | + "mAccount = %s", mAccount.uri); |
| 3651 | |
| 3652 | // Only notify about a settings change if something differs |
| 3653 | if (!Objects.equal(mAccount.settings, previousSettings)) { |
| 3654 | mAccountObservers.notifyChanged(); |
| 3655 | } |
| 3656 | perhapsEnterWaitMode(); |
| 3657 | } else { |
| 3658 | LogUtils.e(LOG_TAG, "Got update for account: %s with current account:" |
| 3659 | + " %s", updatedAccount.uri, mAccount.uri); |
| 3660 | // We need to restart the loader, so the correct account information |
| 3661 | // will be returned. |
| 3662 | restartOptionalLoader(LOADER_ACCOUNT_UPDATE_CURSOR, this, Bundle.EMPTY); |
| 3663 | } |
| 3664 | } |
| 3665 | break; |
| 3666 | } |
| 3667 | } |
| 3668 | |
| 3669 | @Override |
| 3670 | public void onLoaderReset(Loader<ObjectCursor<Account>> loader) { |
Vikram Aggarwal | d70fe49 | 2013-06-04 12:52:07 -0700 | [diff] [blame] | 3671 | // Do nothing. In onLoadFinished() we copy the relevant data from the cursor. |
Vikram Aggarwal | 177097f | 2013-03-08 11:19:53 -0800 | [diff] [blame] | 3672 | } |
| 3673 | } |
| 3674 | |
| 3675 | /** |
Vikram Aggarwal | f0ef230 | 2012-11-07 14:53:34 -0800 | [diff] [blame] | 3676 | * Updates controller state based on search results and shows first conversation if required. |
| 3677 | */ |
Vikram Aggarwal | 715f83d | 2013-03-05 17:04:56 -0800 | [diff] [blame] | 3678 | private void perhapsShowFirstSearchResult() { |
mindyp | d27009a | 2012-11-27 11:54:18 -0800 | [diff] [blame] | 3679 | if (mCurrentConversation == null) { |
| 3680 | // Shown for search results in two-pane mode only. |
| 3681 | mHaveSearchResults = Intent.ACTION_SEARCH.equals(mActivity.getIntent().getAction()) |
| 3682 | && mConversationListCursor.getCount() > 0; |
| 3683 | if (!shouldShowFirstConversation()) { |
| 3684 | return; |
| 3685 | } |
| 3686 | mConversationListCursor.moveToPosition(0); |
| 3687 | final Conversation conv = new Conversation(mConversationListCursor); |
| 3688 | conv.position = 0; |
| 3689 | onConversationSelected(conv, true /* checkSafeToModifyFragments */); |
Vikram Aggarwal | f0ef230 | 2012-11-07 14:53:34 -0800 | [diff] [blame] | 3690 | } |
Vikram Aggarwal | f0ef230 | 2012-11-07 14:53:34 -0800 | [diff] [blame] | 3691 | } |
| 3692 | |
| 3693 | /** |
Vikram Aggarwal | e8a8532 | 2012-04-24 09:01:38 -0700 | [diff] [blame] | 3694 | * Destroy the pending {@link DestructiveAction} till now and assign the given action as the |
| 3695 | * next destructive action.. |
| 3696 | * @param nextAction the next destructive action to be performed. This can be null. |
| 3697 | */ |
Vikram Aggarwal | 715f83d | 2013-03-05 17:04:56 -0800 | [diff] [blame] | 3698 | private void destroyPending(DestructiveAction nextAction) { |
Vikram Aggarwal | e8a8532 | 2012-04-24 09:01:38 -0700 | [diff] [blame] | 3699 | // If there is a pending action, perform that first. |
| 3700 | if (mPendingDestruction != null) { |
| 3701 | mPendingDestruction.performAction(); |
| 3702 | } |
| 3703 | mPendingDestruction = nextAction; |
| 3704 | } |
| 3705 | |
| 3706 | /** |
| 3707 | * Register a destructive action with the controller. This performs the previous destructive |
Vikram Aggarwal | acaa3c0 | 2012-04-24 12:45:27 -0700 | [diff] [blame] | 3708 | * 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] | 3709 | * embellish this method any more. |
Vikram Aggarwal | 715f83d | 2013-03-05 17:04:56 -0800 | [diff] [blame] | 3710 | * @param action the action to register. |
Vikram Aggarwal | e8a8532 | 2012-04-24 09:01:38 -0700 | [diff] [blame] | 3711 | */ |
Vikram Aggarwal | 715f83d | 2013-03-05 17:04:56 -0800 | [diff] [blame] | 3712 | private void registerDestructiveAction(DestructiveAction action) { |
Vikram Aggarwal | e8a8532 | 2012-04-24 09:01:38 -0700 | [diff] [blame] | 3713 | // TODO(viki): This is not a good idea. The best solution is for clients to request a |
| 3714 | // destructive action from the controller and for the controller to own the action. This is |
| 3715 | // a half-way solution while refactoring DestructiveAction. |
| 3716 | destroyPending(action); |
Vikram Aggarwal | e8a8532 | 2012-04-24 09:01:38 -0700 | [diff] [blame] | 3717 | } |
| 3718 | |
Vikram Aggarwal | 531488e | 2012-05-29 16:36:52 -0700 | [diff] [blame] | 3719 | @Override |
| 3720 | public final DestructiveAction getBatchAction(int action) { |
Vikram Aggarwal | c411395 | 2012-05-11 14:14:56 -0700 | [diff] [blame] | 3721 | final DestructiveAction da = new ConversationAction(action, mSelectedSet.values(), true); |
Vikram Aggarwal | e8a8532 | 2012-04-24 09:01:38 -0700 | [diff] [blame] | 3722 | registerDestructiveAction(da); |
| 3723 | return da; |
| 3724 | } |
Vikram Aggarwal | 41e6e71 | 2012-04-24 11:22:57 -0700 | [diff] [blame] | 3725 | |
Mindy Pereira | de3e74a | 2012-07-24 09:43:10 -0700 | [diff] [blame] | 3726 | @Override |
| 3727 | public final DestructiveAction getDeferredBatchAction(int action) { |
mindyp | f0656a1 | 2012-10-01 08:30:57 -0700 | [diff] [blame] | 3728 | return getDeferredAction(action, mSelectedSet.values(), true); |
| 3729 | } |
| 3730 | |
| 3731 | /** |
| 3732 | * Get a destructive action for a menu action. This is a temporary method, |
| 3733 | * to control the profusion of {@link DestructiveAction} classes that are |
| 3734 | * created. Please do not copy this paradigm. |
| 3735 | * @param action the resource ID of the menu action: R.id.delete, for |
| 3736 | * example |
| 3737 | * @param target the conversations to act upon. |
| 3738 | * @return a {@link DestructiveAction} that performs the specified action. |
| 3739 | */ |
Vikram Aggarwal | 715f83d | 2013-03-05 17:04:56 -0800 | [diff] [blame] | 3740 | private DestructiveAction getDeferredAction(int action, Collection<Conversation> target, |
mindyp | f0656a1 | 2012-10-01 08:30:57 -0700 | [diff] [blame] | 3741 | boolean batch) { |
Vikram Aggarwal | b8c3171 | 2013-01-03 17:03:19 -0800 | [diff] [blame] | 3742 | return new ConversationAction(action, target, batch); |
Mindy Pereira | de3e74a | 2012-07-24 09:43:10 -0700 | [diff] [blame] | 3743 | } |
| 3744 | |
Vikram Aggarwal | bc67bb1 | 2012-04-30 14:05:35 -0700 | [diff] [blame] | 3745 | /** |
| 3746 | * Class to change the folders that are assigned to a set of conversations. This is destructive |
| 3747 | * because the user can remove the current folder from the conversation, in which case it has |
| 3748 | * to be animated away from the current folder. |
| 3749 | */ |
Vikram Aggarwal | 41e6e71 | 2012-04-24 11:22:57 -0700 | [diff] [blame] | 3750 | private class FolderDestruction implements DestructiveAction { |
Paul Westbrook | 77eee62 | 2012-07-10 13:41:57 -0700 | [diff] [blame] | 3751 | private final Collection<Conversation> mTarget; |
Mindy Pereira | 8db7e40 | 2012-07-13 10:32:47 -0700 | [diff] [blame] | 3752 | private final ArrayList<FolderOperation> mFolderOps = new ArrayList<FolderOperation>(); |
Vikram Aggarwal | 41e6e71 | 2012-04-24 11:22:57 -0700 | [diff] [blame] | 3753 | private final boolean mIsDestructive; |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 3754 | /** Whether this destructive action has already been performed */ |
| 3755 | private boolean mCompleted; |
Mindy Pereira | f3a4556 | 2012-05-24 16:30:19 -0700 | [diff] [blame] | 3756 | private boolean mIsSelectedSet; |
Mindy Pereira | 06642fa | 2012-07-12 16:23:27 -0700 | [diff] [blame] | 3757 | private boolean mShowUndo; |
Mindy Pereira | 01f3050 | 2012-08-14 10:30:51 -0700 | [diff] [blame] | 3758 | private int mAction; |
Scott Kennedy | 6a3d5ce | 2013-03-15 17:33:14 -0700 | [diff] [blame] | 3759 | private final Folder mActionFolder; |
Vikram Aggarwal | 41e6e71 | 2012-04-24 11:22:57 -0700 | [diff] [blame] | 3760 | |
| 3761 | /** |
| 3762 | * Create a new folder destruction object to act on the given conversations. |
Vikram Aggarwal | 715f83d | 2013-03-05 17:04:56 -0800 | [diff] [blame] | 3763 | * @param target conversations to act upon. |
Scott Kennedy | 6a3d5ce | 2013-03-15 17:33:14 -0700 | [diff] [blame] | 3764 | * @param actionFolder the {@link Folder} being acted upon, used for displaying the undo bar |
Vikram Aggarwal | 41e6e71 | 2012-04-24 11:22:57 -0700 | [diff] [blame] | 3765 | */ |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 3766 | private FolderDestruction(final Collection<Conversation> target, |
Mindy Pereira | 8db7e40 | 2012-07-13 10:32:47 -0700 | [diff] [blame] | 3767 | final Collection<FolderOperation> folders, boolean isDestructive, boolean isBatch, |
Scott Kennedy | 6a3d5ce | 2013-03-15 17:33:14 -0700 | [diff] [blame] | 3768 | boolean showUndo, int action, final Folder actionFolder) { |
Paul Westbrook | 77eee62 | 2012-07-10 13:41:57 -0700 | [diff] [blame] | 3769 | mTarget = ImmutableList.copyOf(target); |
Mindy Pereira | 8db7e40 | 2012-07-13 10:32:47 -0700 | [diff] [blame] | 3770 | mFolderOps.addAll(folders); |
Vikram Aggarwal | 41e6e71 | 2012-04-24 11:22:57 -0700 | [diff] [blame] | 3771 | mIsDestructive = isDestructive; |
Mindy Pereira | f3a4556 | 2012-05-24 16:30:19 -0700 | [diff] [blame] | 3772 | mIsSelectedSet = isBatch; |
Mindy Pereira | 06642fa | 2012-07-12 16:23:27 -0700 | [diff] [blame] | 3773 | mShowUndo = showUndo; |
Mindy Pereira | 01f3050 | 2012-08-14 10:30:51 -0700 | [diff] [blame] | 3774 | mAction = action; |
Scott Kennedy | 6a3d5ce | 2013-03-15 17:33:14 -0700 | [diff] [blame] | 3775 | mActionFolder = actionFolder; |
Vikram Aggarwal | 41e6e71 | 2012-04-24 11:22:57 -0700 | [diff] [blame] | 3776 | } |
| 3777 | |
Vikram Aggarwal | 41e6e71 | 2012-04-24 11:22:57 -0700 | [diff] [blame] | 3778 | @Override |
| 3779 | public void performAction() { |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 3780 | if (isPerformed()) { |
| 3781 | return; |
| 3782 | } |
Mindy Pereira | 06642fa | 2012-07-12 16:23:27 -0700 | [diff] [blame] | 3783 | if (mIsDestructive && mShowUndo) { |
mindyp | cb0b30e | 2012-11-30 10:16:35 -0800 | [diff] [blame] | 3784 | ToastBarOperation undoOp = new ToastBarOperation(mTarget.size(), mAction, |
Scott Kennedy | 6a3d5ce | 2013-03-15 17:33:14 -0700 | [diff] [blame] | 3785 | ToastBarOperation.UNDO, mIsSelectedSet, mActionFolder); |
Vikram Aggarwal | 41e6e71 | 2012-04-24 11:22:57 -0700 | [diff] [blame] | 3786 | onUndoAvailable(undoOp); |
| 3787 | } |
Mindy Pereira | 8db7e40 | 2012-07-13 10:32:47 -0700 | [diff] [blame] | 3788 | // For each conversation, for each operation, add/ remove the |
| 3789 | // appropriate folders. |
mindyp | cb0b30e | 2012-11-30 10:16:35 -0800 | [diff] [blame] | 3790 | ArrayList<ConversationOperation> ops = new ArrayList<ConversationOperation>(); |
| 3791 | ArrayList<Uri> folderUris; |
| 3792 | ArrayList<Boolean> adds; |
Mindy Pereira | 8db7e40 | 2012-07-13 10:32:47 -0700 | [diff] [blame] | 3793 | for (Conversation target : mTarget) { |
mindyp | cb0b30e | 2012-11-30 10:16:35 -0800 | [diff] [blame] | 3794 | HashMap<Uri, Folder> targetFolders = Folder.hashMapForFolders(target |
| 3795 | .getRawFolders()); |
| 3796 | folderUris = new ArrayList<Uri>(); |
mindyp | cb0b30e | 2012-11-30 10:16:35 -0800 | [diff] [blame] | 3797 | adds = new ArrayList<Boolean>(); |
Mindy Pereira | 01f3050 | 2012-08-14 10:30:51 -0700 | [diff] [blame] | 3798 | if (mIsDestructive) { |
| 3799 | target.localDeleteOnUpdate = true; |
| 3800 | } |
Mindy Pereira | 8db7e40 | 2012-07-13 10:32:47 -0700 | [diff] [blame] | 3801 | for (FolderOperation op : mFolderOps) { |
Scott Kennedy | 259df5b | 2013-07-11 13:24:01 -0700 | [diff] [blame] | 3802 | folderUris.add(op.mFolder.folderUri.fullUri); |
mindyp | cb0b30e | 2012-11-30 10:16:35 -0800 | [diff] [blame] | 3803 | adds.add(op.mAdd ? Boolean.TRUE : Boolean.FALSE); |
Mindy Pereira | 8db7e40 | 2012-07-13 10:32:47 -0700 | [diff] [blame] | 3804 | if (op.mAdd) { |
Scott Kennedy | 259df5b | 2013-07-11 13:24:01 -0700 | [diff] [blame] | 3805 | targetFolders.put(op.mFolder.folderUri.fullUri, op.mFolder); |
Mindy Pereira | 8db7e40 | 2012-07-13 10:32:47 -0700 | [diff] [blame] | 3806 | } else { |
Scott Kennedy | 259df5b | 2013-07-11 13:24:01 -0700 | [diff] [blame] | 3807 | targetFolders.remove(op.mFolder.folderUri.fullUri); |
Mindy Pereira | 8db7e40 | 2012-07-13 10:32:47 -0700 | [diff] [blame] | 3808 | } |
| 3809 | } |
Paul Westbrook | 26746eb | 2012-12-06 14:44:01 -0800 | [diff] [blame] | 3810 | ops.add(mConversationListCursor.getConversationFolderOperation(target, |
| 3811 | folderUris, adds, targetFolders.values())); |
mindyp | 389f0b2 | 2012-08-29 11:12:54 -0700 | [diff] [blame] | 3812 | } |
| 3813 | if (mConversationListCursor != null) { |
Scott Kennedy | 9e2d407 | 2013-03-21 21:46:01 -0700 | [diff] [blame] | 3814 | mConversationListCursor.updateBulkValues(ops); |
Mindy Pereira | 8db7e40 | 2012-07-13 10:32:47 -0700 | [diff] [blame] | 3815 | } |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 3816 | refreshConversationList(); |
Mindy Pereira | f3a4556 | 2012-05-24 16:30:19 -0700 | [diff] [blame] | 3817 | if (mIsSelectedSet) { |
| 3818 | mSelectedSet.clear(); |
| 3819 | } |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 3820 | } |
Mindy Pereira | 8db7e40 | 2012-07-13 10:32:47 -0700 | [diff] [blame] | 3821 | |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 3822 | /** |
| 3823 | * Returns true if this action has been performed, false otherwise. |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 3824 | * |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 3825 | */ |
| 3826 | private synchronized boolean isPerformed() { |
| 3827 | if (mCompleted) { |
| 3828 | return true; |
Vikram Aggarwal | 41e6e71 | 2012-04-24 11:22:57 -0700 | [diff] [blame] | 3829 | } |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 3830 | mCompleted = true; |
| 3831 | return false; |
Vikram Aggarwal | 41e6e71 | 2012-04-24 11:22:57 -0700 | [diff] [blame] | 3832 | } |
| 3833 | } |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 3834 | |
mindyp | c84759c | 2012-08-29 09:51:53 -0700 | [diff] [blame] | 3835 | public final DestructiveAction getFolderChange(Collection<Conversation> target, |
| 3836 | Collection<FolderOperation> folders, boolean isDestructive, boolean isBatch, |
Scott Kennedy | 6a3d5ce | 2013-03-15 17:33:14 -0700 | [diff] [blame] | 3837 | boolean showUndo, final boolean isMoveTo, final Folder actionFolder) { |
mindyp | c84759c | 2012-08-29 09:51:53 -0700 | [diff] [blame] | 3838 | final DestructiveAction da = getDeferredFolderChange(target, folders, isDestructive, |
Scott Kennedy | 6a3d5ce | 2013-03-15 17:33:14 -0700 | [diff] [blame] | 3839 | isBatch, showUndo, isMoveTo, actionFolder); |
mindyp | c84759c | 2012-08-29 09:51:53 -0700 | [diff] [blame] | 3840 | registerDestructiveAction(da); |
| 3841 | return da; |
| 3842 | } |
| 3843 | |
| 3844 | public final DestructiveAction getDeferredFolderChange(Collection<Conversation> target, |
Mindy Pereira | 8db7e40 | 2012-07-13 10:32:47 -0700 | [diff] [blame] | 3845 | Collection<FolderOperation> folders, boolean isDestructive, boolean isBatch, |
Scott Kennedy | 6a3d5ce | 2013-03-15 17:33:14 -0700 | [diff] [blame] | 3846 | boolean showUndo, final boolean isMoveTo, final Folder actionFolder) { |
| 3847 | return new FolderDestruction(target, folders, isDestructive, isBatch, showUndo, |
Andrew Sapperstein | 6c570db | 2013-08-06 17:21:36 -0700 | [diff] [blame] | 3848 | isMoveTo ? R.id.move_folder : R.id.change_folders, actionFolder); |
Mindy Pereira | 01f3050 | 2012-08-14 10:30:51 -0700 | [diff] [blame] | 3849 | } |
| 3850 | |
| 3851 | @Override |
| 3852 | public final DestructiveAction getDeferredRemoveFolder(Collection<Conversation> target, |
| 3853 | Folder toRemove, boolean isDestructive, boolean isBatch, |
| 3854 | boolean showUndo) { |
| 3855 | Collection<FolderOperation> folderOps = new ArrayList<FolderOperation>(); |
| 3856 | folderOps.add(new FolderOperation(toRemove, false)); |
| 3857 | return new FolderDestruction(target, folderOps, isDestructive, isBatch, |
Scott Kennedy | 6a3d5ce | 2013-03-15 17:33:14 -0700 | [diff] [blame] | 3858 | showUndo, R.id.remove_folder, mFolder); |
Mindy Pereira | 01f3050 | 2012-08-14 10:30:51 -0700 | [diff] [blame] | 3859 | } |
| 3860 | |
Vikram Aggarwal | 4f4782b | 2012-05-30 08:39:09 -0700 | [diff] [blame] | 3861 | @Override |
| 3862 | public final void refreshConversationList() { |
Vikram Aggarwal | 75daee5 | 2012-04-30 11:13:09 -0700 | [diff] [blame] | 3863 | final ConversationListFragment convList = getConversationListFragment(); |
| 3864 | if (convList == null) { |
| 3865 | return; |
| 3866 | } |
| 3867 | convList.requestListRefresh(); |
| 3868 | } |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 3869 | |
| 3870 | protected final ActionClickedListener getUndoClickedListener( |
| 3871 | final AnimatedAdapter listAdapter) { |
| 3872 | return new ActionClickedListener() { |
| 3873 | @Override |
Andrew Sapperstein | f8ccdcf | 2013-08-08 19:30:38 -0700 | [diff] [blame] | 3874 | public void onActionClicked(Context context) { |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 3875 | if (mAccount.undoUri != null) { |
| 3876 | // NOTE: We might want undo to return the messages affected, in which case |
| 3877 | // the resulting cursor might be interesting... |
| 3878 | // TODO: Use UIProvider.SEQUENCE_QUERY_PARAMETER to indicate the set of |
| 3879 | // commands to undo |
| 3880 | if (mConversationListCursor != null) { |
| 3881 | mConversationListCursor.undo( |
| 3882 | mActivity.getActivityContext(), mAccount.undoUri); |
| 3883 | } |
| 3884 | if (listAdapter != null) { |
| 3885 | listAdapter.setUndo(true); |
| 3886 | } |
| 3887 | } |
| 3888 | } |
| 3889 | }; |
| 3890 | } |
| 3891 | |
Vikram Aggarwal | 41b9e8f | 2012-09-25 10:15:04 -0700 | [diff] [blame] | 3892 | /** |
| 3893 | * Shows an error toast in the bottom when a folder was not fetched successfully. |
| 3894 | * @param folder the folder which could not be fetched. |
| 3895 | * @param replaceVisibleToast if true, this should replace any currently visible toast. |
| 3896 | */ |
Andrew Sapperstein | 9d7519d | 2012-07-16 14:03:53 -0700 | [diff] [blame] | 3897 | protected final void showErrorToast(final Folder folder, boolean replaceVisibleToast) { |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 3898 | |
Vikram Aggarwal | 41b9e8f | 2012-09-25 10:15:04 -0700 | [diff] [blame] | 3899 | final ActionClickedListener listener; |
| 3900 | final int actionTextResourceId; |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 3901 | final int lastSyncResult = folder.lastSyncResult; |
Vikram Aggarwal | 41b9e8f | 2012-09-25 10:15:04 -0700 | [diff] [blame] | 3902 | switch (lastSyncResult & 0x0f) { |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 3903 | case UIProvider.LastSyncResult.CONNECTION_ERROR: |
Vikram Aggarwal | 41b9e8f | 2012-09-25 10:15:04 -0700 | [diff] [blame] | 3904 | // The sync request that caused this failure. |
| 3905 | final int syncRequest = lastSyncResult >> 4; |
| 3906 | // Show: User explicitly pressed the refresh button and there is no connection |
| 3907 | // Show: The first time the user enters the app and there is no connection |
| 3908 | // TODO(viki): Implement this. |
| 3909 | // Reference: http://b/7202801 |
| 3910 | final boolean showToast = (syncRequest & UIProvider.SyncStatus.USER_REFRESH) != 0; |
| 3911 | // Don't show: Already in the app; user switches to a synced label |
| 3912 | // Don't show: In a live label and a background sync fails |
| 3913 | final boolean avoidToast = !showToast && (folder.syncWindow > 0 |
| 3914 | || (syncRequest & UIProvider.SyncStatus.BACKGROUND_SYNC) != 0); |
| 3915 | if (avoidToast) { |
| 3916 | return; |
| 3917 | } |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 3918 | listener = getRetryClickedListener(folder); |
| 3919 | actionTextResourceId = R.string.retry; |
| 3920 | break; |
| 3921 | case UIProvider.LastSyncResult.AUTH_ERROR: |
| 3922 | listener = getSignInClickedListener(); |
| 3923 | actionTextResourceId = R.string.signin; |
| 3924 | break; |
| 3925 | case UIProvider.LastSyncResult.SECURITY_ERROR: |
| 3926 | return; // Currently we do nothing for security errors. |
| 3927 | case UIProvider.LastSyncResult.STORAGE_ERROR: |
| 3928 | listener = getStorageErrorClickedListener(); |
| 3929 | actionTextResourceId = R.string.info; |
| 3930 | break; |
| 3931 | case UIProvider.LastSyncResult.INTERNAL_ERROR: |
| 3932 | listener = getInternalErrorClickedListener(); |
| 3933 | actionTextResourceId = R.string.report; |
| 3934 | break; |
| 3935 | default: |
| 3936 | return; |
| 3937 | } |
Vikram Aggarwal | 41b9e8f | 2012-09-25 10:15:04 -0700 | [diff] [blame] | 3938 | mToastBar.show(listener, |
Andrew Sapperstein | 5d42096 | 2012-07-12 16:43:10 -0700 | [diff] [blame] | 3939 | R.drawable.ic_alert_white, |
Vikram Aggarwal | 41b9e8f | 2012-09-25 10:15:04 -0700 | [diff] [blame] | 3940 | Utils.getSyncStatusText(mActivity.getActivityContext(), lastSyncResult), |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 3941 | false, /* showActionIcon */ |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 3942 | actionTextResourceId, |
Andrew Sapperstein | 9d7519d | 2012-07-16 14:03:53 -0700 | [diff] [blame] | 3943 | replaceVisibleToast, |
Scott Kennedy | 6a3d5ce | 2013-03-15 17:33:14 -0700 | [diff] [blame] | 3944 | new ToastBarOperation(1, 0, ToastBarOperation.ERROR, false, folder)); |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 3945 | } |
| 3946 | |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 3947 | private ActionClickedListener getRetryClickedListener(final Folder folder) { |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 3948 | return new ActionClickedListener() { |
| 3949 | @Override |
Andrew Sapperstein | f8ccdcf | 2013-08-08 19:30:38 -0700 | [diff] [blame] | 3950 | public void onActionClicked(Context context) { |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 3951 | final Uri uri = folder.refreshUri; |
| 3952 | |
| 3953 | if (uri != null) { |
Paul Westbrook | 4969e0c | 2012-08-20 14:38:39 -0700 | [diff] [blame] | 3954 | startAsyncRefreshTask(uri); |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 3955 | } |
| 3956 | } |
| 3957 | }; |
| 3958 | } |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 3959 | |
| 3960 | private ActionClickedListener getSignInClickedListener() { |
| 3961 | return new ActionClickedListener() { |
| 3962 | @Override |
Andrew Sapperstein | f8ccdcf | 2013-08-08 19:30:38 -0700 | [diff] [blame] | 3963 | public void onActionClicked(Context context) { |
Paul Westbrook | 122f7c2 | 2012-08-20 17:50:31 -0700 | [diff] [blame] | 3964 | promptUserForAuthentication(mAccount); |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 3965 | } |
| 3966 | }; |
| 3967 | } |
| 3968 | |
| 3969 | private ActionClickedListener getStorageErrorClickedListener() { |
| 3970 | return new ActionClickedListener() { |
| 3971 | @Override |
Andrew Sapperstein | f8ccdcf | 2013-08-08 19:30:38 -0700 | [diff] [blame] | 3972 | public void onActionClicked(Context context) { |
Paul Westbrook | 4969e0c | 2012-08-20 14:38:39 -0700 | [diff] [blame] | 3973 | showStorageErrorDialog(); |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 3974 | } |
| 3975 | }; |
| 3976 | } |
| 3977 | |
Paul Westbrook | 4969e0c | 2012-08-20 14:38:39 -0700 | [diff] [blame] | 3978 | private void showStorageErrorDialog() { |
| 3979 | DialogFragment fragment = (DialogFragment) |
| 3980 | mFragmentManager.findFragmentByTag(SYNC_ERROR_DIALOG_FRAGMENT_TAG); |
| 3981 | if (fragment == null) { |
| 3982 | fragment = SyncErrorDialogFragment.newInstance(); |
| 3983 | } |
| 3984 | fragment.show(mFragmentManager, SYNC_ERROR_DIALOG_FRAGMENT_TAG); |
| 3985 | } |
| 3986 | |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 3987 | private ActionClickedListener getInternalErrorClickedListener() { |
| 3988 | return new ActionClickedListener() { |
| 3989 | @Override |
Andrew Sapperstein | f8ccdcf | 2013-08-08 19:30:38 -0700 | [diff] [blame] | 3990 | public void onActionClicked(Context context) { |
Paul Westbrook | 83e6b57 | 2013-02-05 16:22:42 -0800 | [diff] [blame] | 3991 | Utils.sendFeedback(mActivity, mAccount, true /* reportingProblem */); |
Andrew Sapperstein | 00179f1 | 2012-08-09 15:15:40 -0700 | [diff] [blame] | 3992 | } |
| 3993 | }; |
| 3994 | } |
Paul Westbrook | 4969e0c | 2012-08-20 14:38:39 -0700 | [diff] [blame] | 3995 | |
| 3996 | @Override |
| 3997 | public void onFooterViewErrorActionClick(Folder folder, int errorStatus) { |
| 3998 | Uri uri = null; |
| 3999 | switch (errorStatus) { |
| 4000 | case UIProvider.LastSyncResult.CONNECTION_ERROR: |
| 4001 | if (folder != null && folder.refreshUri != null) { |
| 4002 | uri = folder.refreshUri; |
| 4003 | } |
| 4004 | break; |
| 4005 | case UIProvider.LastSyncResult.AUTH_ERROR: |
Paul Westbrook | 122f7c2 | 2012-08-20 17:50:31 -0700 | [diff] [blame] | 4006 | promptUserForAuthentication(mAccount); |
Paul Westbrook | 4969e0c | 2012-08-20 14:38:39 -0700 | [diff] [blame] | 4007 | return; |
| 4008 | case UIProvider.LastSyncResult.SECURITY_ERROR: |
| 4009 | return; // Currently we do nothing for security errors. |
| 4010 | case UIProvider.LastSyncResult.STORAGE_ERROR: |
| 4011 | showStorageErrorDialog(); |
| 4012 | return; |
| 4013 | case UIProvider.LastSyncResult.INTERNAL_ERROR: |
Paul Westbrook | 83e6b57 | 2013-02-05 16:22:42 -0800 | [diff] [blame] | 4014 | Utils.sendFeedback(mActivity, mAccount, true /* reportingProblem */); |
Paul Westbrook | 4969e0c | 2012-08-20 14:38:39 -0700 | [diff] [blame] | 4015 | return; |
| 4016 | default: |
| 4017 | return; |
| 4018 | } |
| 4019 | |
| 4020 | if (uri != null) { |
| 4021 | startAsyncRefreshTask(uri); |
| 4022 | } |
| 4023 | } |
| 4024 | |
| 4025 | @Override |
| 4026 | public void onFooterViewLoadMoreClick(Folder folder) { |
| 4027 | if (folder != null && folder.loadMoreUri != null) { |
| 4028 | startAsyncRefreshTask(folder.loadMoreUri); |
| 4029 | } |
| 4030 | } |
| 4031 | |
| 4032 | private void startAsyncRefreshTask(Uri uri) { |
| 4033 | if (mFolderSyncTask != null) { |
| 4034 | mFolderSyncTask.cancel(true); |
| 4035 | } |
| 4036 | mFolderSyncTask = new AsyncRefreshTask(mActivity.getActivityContext(), uri); |
| 4037 | mFolderSyncTask.execute(); |
| 4038 | } |
Paul Westbrook | 122f7c2 | 2012-08-20 17:50:31 -0700 | [diff] [blame] | 4039 | |
| 4040 | private void promptUserForAuthentication(Account account) { |
Paul Westbrook | 429399b | 2012-08-24 11:19:17 -0700 | [diff] [blame] | 4041 | if (account != null && !Utils.isEmpty(account.reauthenticationIntentUri)) { |
Paul Westbrook | 122f7c2 | 2012-08-20 17:50:31 -0700 | [diff] [blame] | 4042 | final Intent authenticationIntent = |
| 4043 | new Intent(Intent.ACTION_VIEW, account.reauthenticationIntentUri); |
| 4044 | mActivity.startActivityForResult(authenticationIntent, REAUTHENTICATE_REQUEST_CODE); |
| 4045 | } |
| 4046 | } |
mindyp | ca87de4 | 2012-09-28 15:02:39 -0700 | [diff] [blame] | 4047 | |
| 4048 | @Override |
| 4049 | public void onAccessibilityStateChanged() { |
| 4050 | // Clear the cache of objects. |
| 4051 | ConversationItemViewModel.onAccessibilityUpdated(); |
| 4052 | // Re-render the list if it exists. |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame] | 4053 | final ConversationListFragment frag = getConversationListFragment(); |
mindyp | ca87de4 | 2012-09-28 15:02:39 -0700 | [diff] [blame] | 4054 | if (frag != null) { |
| 4055 | AnimatedAdapter adapter = frag.getAnimatedAdapter(); |
| 4056 | if (adapter != null) { |
| 4057 | adapter.notifyDataSetInvalidated(); |
| 4058 | } |
| 4059 | } |
| 4060 | } |
Vikram Aggarwal | 6cadbfc | 2012-12-27 09:17:05 -0800 | [diff] [blame] | 4061 | |
| 4062 | @Override |
Vikram Aggarwal | 84fe994 | 2013-04-17 11:20:58 -0700 | [diff] [blame] | 4063 | public void makeDialogListener (final int action, final boolean isBatch) { |
Vikram Aggarwal | b8c3171 | 2013-01-03 17:03:19 -0800 | [diff] [blame] | 4064 | final Collection<Conversation> target; |
Vikram Aggarwal | b8c3171 | 2013-01-03 17:03:19 -0800 | [diff] [blame] | 4065 | if (isBatch) { |
| 4066 | target = mSelectedSet.values(); |
Vikram Aggarwal | b8c3171 | 2013-01-03 17:03:19 -0800 | [diff] [blame] | 4067 | } else { |
| 4068 | LogUtils.d(LOG_TAG, "Will act upon %s", mCurrentConversation); |
| 4069 | target = Conversation.listOf(mCurrentConversation); |
Vikram Aggarwal | b8c3171 | 2013-01-03 17:03:19 -0800 | [diff] [blame] | 4070 | } |
| 4071 | final DestructiveAction destructiveAction = getDeferredAction(action, target, isBatch); |
Vikram Aggarwal | 6cadbfc | 2012-12-27 09:17:05 -0800 | [diff] [blame] | 4072 | mDialogAction = action; |
Vikram Aggarwal | b8c3171 | 2013-01-03 17:03:19 -0800 | [diff] [blame] | 4073 | mDialogFromSelectedSet = isBatch; |
Vikram Aggarwal | 6cadbfc | 2012-12-27 09:17:05 -0800 | [diff] [blame] | 4074 | mDialogListener = new AlertDialog.OnClickListener() { |
| 4075 | @Override |
| 4076 | public void onClick(DialogInterface dialog, int which) { |
Scott Kennedy | caaeed3 | 2013-06-12 13:39:16 -0700 | [diff] [blame] | 4077 | delete(action, target, destructiveAction, isBatch); |
Vikram Aggarwal | 6cadbfc | 2012-12-27 09:17:05 -0800 | [diff] [blame] | 4078 | // Afterwards, let's remove references to the listener and the action. |
| 4079 | setListener(null, -1); |
| 4080 | } |
| 4081 | }; |
| 4082 | } |
| 4083 | |
| 4084 | @Override |
| 4085 | public AlertDialog.OnClickListener getListener() { |
| 4086 | return mDialogListener; |
| 4087 | } |
| 4088 | |
| 4089 | /** |
| 4090 | * Sets the listener for the positive action on a confirmation dialog. Since only a single |
| 4091 | * confirmation dialog can be shown, this overwrites the previous listener. It is safe to |
| 4092 | * unset the listener; in which case action should be set to -1. |
Vikram Aggarwal | 715f83d | 2013-03-05 17:04:56 -0800 | [diff] [blame] | 4093 | * @param listener the listener that will perform the task for this dialog's positive action. |
| 4094 | * @param action the action that created this dialog. |
Vikram Aggarwal | 6cadbfc | 2012-12-27 09:17:05 -0800 | [diff] [blame] | 4095 | */ |
| 4096 | private void setListener(AlertDialog.OnClickListener listener, final int action){ |
| 4097 | mDialogListener = listener; |
| 4098 | mDialogAction = action; |
| 4099 | } |
Vikram Aggarwal | 69a6cdf | 2013-01-08 16:05:17 -0800 | [diff] [blame] | 4100 | |
| 4101 | @Override |
| 4102 | public VeiledAddressMatcher getVeiledAddressMatcher() { |
| 4103 | return mVeiledMatcher; |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame] | 4104 | } |
| 4105 | |
| 4106 | @Override |
| 4107 | public void setDetachedMode() { |
| 4108 | // Tell the conversation list not to select anything. |
| 4109 | final ConversationListFragment frag = getConversationListFragment(); |
| 4110 | if (frag != null) { |
| 4111 | frag.setChoiceNone(); |
Vikram Aggarwal | 81230ee | 2013-02-26 15:26:44 -0800 | [diff] [blame] | 4112 | } else if (mIsTablet) { |
| 4113 | // How did we ever land here? Detached mode, and no CLF on tablet??? |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame] | 4114 | LogUtils.e(LOG_TAG, "AAC.setDetachedMode(): CLF = null!"); |
| 4115 | } |
| 4116 | mDetachedConvUri = mCurrentConversation.uri; |
| 4117 | } |
| 4118 | |
| 4119 | private void clearDetachedMode() { |
| 4120 | // Tell the conversation list to go back to its usual selection behavior. |
| 4121 | final ConversationListFragment frag = getConversationListFragment(); |
| 4122 | if (frag != null) { |
| 4123 | frag.revertChoiceMode(); |
Vikram Aggarwal | 81230ee | 2013-02-26 15:26:44 -0800 | [diff] [blame] | 4124 | } else if (mIsTablet) { |
| 4125 | // How did we ever land here? Detached mode, and no CLF on tablet??? |
| 4126 | LogUtils.e(LOG_TAG, "AAC.clearDetachedMode(): CLF = null on tablet!"); |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame] | 4127 | } |
| 4128 | mDetachedConvUri = null; |
| 4129 | } |
Andy Huang | 12b3ee4 | 2013-04-24 22:49:43 -0700 | [diff] [blame] | 4130 | |
| 4131 | private class MailDrawerListener implements DrawerLayout.DrawerListener { |
Andrew Sapperstein | a3ce678 | 2013-05-09 15:14:49 -0700 | [diff] [blame] | 4132 | private int mDrawerState; |
Andrew Sapperstein | fc5be1c | 2013-05-15 17:48:10 -0700 | [diff] [blame] | 4133 | private float mOldSlideOffset; |
Andrew Sapperstein | a3ce678 | 2013-05-09 15:14:49 -0700 | [diff] [blame] | 4134 | |
| 4135 | public MailDrawerListener() { |
| 4136 | mDrawerState = DrawerLayout.STATE_IDLE; |
Andrew Sapperstein | fc5be1c | 2013-05-15 17:48:10 -0700 | [diff] [blame] | 4137 | mOldSlideOffset = 0.f; |
Andrew Sapperstein | a3ce678 | 2013-05-09 15:14:49 -0700 | [diff] [blame] | 4138 | } |
| 4139 | |
Andy Huang | 12b3ee4 | 2013-04-24 22:49:43 -0700 | [diff] [blame] | 4140 | @Override |
| 4141 | public void onDrawerOpened(View drawerView) { |
| 4142 | mDrawerToggle.onDrawerOpened(drawerView); |
| 4143 | } |
| 4144 | |
| 4145 | @Override |
| 4146 | public void onDrawerClosed(View drawerView) { |
| 4147 | mDrawerToggle.onDrawerClosed(drawerView); |
| 4148 | if (mHasNewAccountOrFolder) { |
| 4149 | refreshDrawer(); |
| 4150 | } |
Scott Kennedy | 10c99ef | 2013-08-29 13:44:17 -0700 | [diff] [blame] | 4151 | |
| 4152 | // When closed, we want to use either the burger, or up, based on where we are |
| 4153 | final int mode = mViewMode.getMode(); |
| 4154 | final boolean isTopLevel = (mFolder == null) || (mFolder.parent == Uri.EMPTY); |
| 4155 | mDrawerToggle.setDrawerIndicatorEnabled(getShouldShowDrawerIndicator(mode, isTopLevel)); |
Andy Huang | 12b3ee4 | 2013-04-24 22:49:43 -0700 | [diff] [blame] | 4156 | } |
| 4157 | |
| 4158 | /** |
| 4159 | * As part of the overriden function, it will animate the alpha of the conversation list |
| 4160 | * view along with the drawer sliding when we're in the process of switching accounts or |
| 4161 | * folders. Note, this is the same amount of work done as {@link ValueAnimator#ofFloat}. |
| 4162 | */ |
| 4163 | @Override |
| 4164 | public void onDrawerSlide(View drawerView, float slideOffset) { |
| 4165 | mDrawerToggle.onDrawerSlide(drawerView, slideOffset); |
| 4166 | if (mHasNewAccountOrFolder && mListViewForAnimating != null) { |
| 4167 | mListViewForAnimating.setAlpha(slideOffset); |
| 4168 | } |
Andrew Sapperstein | fc5be1c | 2013-05-15 17:48:10 -0700 | [diff] [blame] | 4169 | |
| 4170 | // This code handles when to change the visibility of action items |
| 4171 | // based on drawer state. The basic logic is that right when we |
| 4172 | // open the drawer, we hide the action items. We show the action items |
| 4173 | // when the drawer closes. However, due to the animation of the drawer closing, |
| 4174 | // to make the reshowing of the action items feel right, we make the items visible |
| 4175 | // slightly sooner. |
| 4176 | // |
| 4177 | // However, to make the animating behavior work properly, we have to know whether |
| 4178 | // we're animating open or closed. Only if we're animating closed do we want to |
| 4179 | // show the action items early. We save the last slide offset so that we can compare |
| 4180 | // the current slide offset to it to determine if we're opening or closing. |
| 4181 | if (mDrawerState == DrawerLayout.STATE_SETTLING) { |
| 4182 | if (mHideMenuItems && slideOffset < 0.15f && mOldSlideOffset > slideOffset) { |
| 4183 | mHideMenuItems = false; |
| 4184 | mActivity.invalidateOptionsMenu(); |
Tony Mantler | 43fab32 | 2013-07-26 16:38:35 -0700 | [diff] [blame] | 4185 | maybeEnableCabMode(); |
Andrew Sapperstein | fc5be1c | 2013-05-15 17:48:10 -0700 | [diff] [blame] | 4186 | } else if (!mHideMenuItems && slideOffset > 0.f && mOldSlideOffset < slideOffset) { |
| 4187 | mHideMenuItems = true; |
| 4188 | mActivity.invalidateOptionsMenu(); |
Tony Mantler | 43fab32 | 2013-07-26 16:38:35 -0700 | [diff] [blame] | 4189 | disableCabMode(); |
Tony Mantler | b29890a | 2013-07-30 15:12:05 -0700 | [diff] [blame] | 4190 | final FolderListFragment folderListFragment = getFolderListFragment(); |
| 4191 | if (folderListFragment != null) { |
| 4192 | folderListFragment.updateScroll(); |
| 4193 | } |
Andrew Sapperstein | fc5be1c | 2013-05-15 17:48:10 -0700 | [diff] [blame] | 4194 | } |
| 4195 | } else { |
| 4196 | if (mHideMenuItems && Float.compare(slideOffset, 0.f) == 0) { |
| 4197 | mHideMenuItems = false; |
| 4198 | mActivity.invalidateOptionsMenu(); |
Tony Mantler | 43fab32 | 2013-07-26 16:38:35 -0700 | [diff] [blame] | 4199 | maybeEnableCabMode(); |
Andrew Sapperstein | fc5be1c | 2013-05-15 17:48:10 -0700 | [diff] [blame] | 4200 | } else if (!mHideMenuItems && slideOffset > 0.f) { |
| 4201 | mHideMenuItems = true; |
| 4202 | mActivity.invalidateOptionsMenu(); |
Tony Mantler | 43fab32 | 2013-07-26 16:38:35 -0700 | [diff] [blame] | 4203 | disableCabMode(); |
Tony Mantler | b29890a | 2013-07-30 15:12:05 -0700 | [diff] [blame] | 4204 | final FolderListFragment folderListFragment = getFolderListFragment(); |
| 4205 | if (folderListFragment != null) { |
| 4206 | folderListFragment.updateScroll(); |
| 4207 | } |
Andrew Sapperstein | fc5be1c | 2013-05-15 17:48:10 -0700 | [diff] [blame] | 4208 | } |
| 4209 | } |
| 4210 | |
| 4211 | mOldSlideOffset = slideOffset; |
Scott Kennedy | 10c99ef | 2013-08-29 13:44:17 -0700 | [diff] [blame] | 4212 | |
| 4213 | // If we're sliding, we always want to show the burger |
| 4214 | mDrawerToggle.setDrawerIndicatorEnabled(true /* enable */); |
Andy Huang | 12b3ee4 | 2013-04-24 22:49:43 -0700 | [diff] [blame] | 4215 | } |
| 4216 | |
| 4217 | /** |
| 4218 | * This condition here should only be called when the drawer is stuck in a weird state |
| 4219 | * and doesn't register the onDrawerClosed, but shows up as idle. Make sure to refresh |
| 4220 | * and, more importantly, unlock the drawer when this is the case. |
| 4221 | */ |
| 4222 | @Override |
| 4223 | public void onDrawerStateChanged(int newState) { |
Andrew Sapperstein | a3ce678 | 2013-05-09 15:14:49 -0700 | [diff] [blame] | 4224 | mDrawerState = newState; |
| 4225 | mDrawerToggle.onDrawerStateChanged(mDrawerState); |
| 4226 | if (mDrawerState == DrawerLayout.STATE_IDLE) { |
| 4227 | if (mHasNewAccountOrFolder) { |
| 4228 | refreshDrawer(); |
| 4229 | } |
| 4230 | if (mConversationListLoadFinishedIgnored) { |
| 4231 | mConversationListLoadFinishedIgnored = false; |
| 4232 | final Bundle args = new Bundle(); |
| 4233 | args.putParcelable(BUNDLE_ACCOUNT_KEY, mAccount); |
| 4234 | args.putParcelable(BUNDLE_FOLDER_KEY, mFolder); |
| 4235 | mActivity.getLoaderManager().initLoader( |
| 4236 | LOADER_CONVERSATION_LIST, args, mListCursorCallbacks); |
| 4237 | } |
Andy Huang | 12b3ee4 | 2013-04-24 22:49:43 -0700 | [diff] [blame] | 4238 | } |
| 4239 | } |
| 4240 | |
| 4241 | /** |
| 4242 | * If we've reached a stable drawer state, unlock the drawer for usage, clear the |
| 4243 | * conversation list, and finish end actions. Also, make |
| 4244 | * {@link #mHasNewAccountOrFolder} false to reflect we're done changing. |
| 4245 | */ |
| 4246 | public void refreshDrawer() { |
| 4247 | mHasNewAccountOrFolder = false; |
| 4248 | mDrawerContainer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED); |
| 4249 | ConversationListFragment conversationList = getConversationListFragment(); |
| 4250 | if (conversationList != null) { |
| 4251 | conversationList.clear(); |
| 4252 | } |
| 4253 | mDrawerObservers.notifyChanged(); |
| 4254 | } |
Andrew Sapperstein | a3ce678 | 2013-05-09 15:14:49 -0700 | [diff] [blame] | 4255 | |
| 4256 | /** |
| 4257 | * Returns the most recent update of the {@link DrawerLayout}'s state provided |
| 4258 | * by {@link #onDrawerStateChanged(int)}. |
| 4259 | * @return The {@link DrawerLayout}'s current state. One of |
| 4260 | * {@link DrawerLayout#STATE_DRAGGING}, {@link DrawerLayout#STATE_IDLE}, |
| 4261 | * or {@link DrawerLayout#STATE_SETTLING}. |
| 4262 | */ |
| 4263 | public int getDrawerState() { |
| 4264 | return mDrawerState; |
| 4265 | } |
Andy Huang | 12b3ee4 | 2013-04-24 22:49:43 -0700 | [diff] [blame] | 4266 | } |
| 4267 | |
Scott Kennedy | 8a72b85 | 2013-05-02 14:18:50 -0700 | [diff] [blame] | 4268 | @Override |
| 4269 | public boolean isDrawerPullEnabled() { |
Scott Kennedy | 2b254dc | 2013-07-10 11:40:27 -0700 | [diff] [blame] | 4270 | return getShouldAllowDrawerPull(mViewMode.getMode()); |
Scott Kennedy | 8a72b85 | 2013-05-02 14:18:50 -0700 | [diff] [blame] | 4271 | } |
Andrew Sapperstein | 5747e15 | 2013-05-13 14:13:08 -0700 | [diff] [blame] | 4272 | |
| 4273 | @Override |
| 4274 | public boolean shouldHideMenuItems() { |
| 4275 | return mHideMenuItems; |
| 4276 | } |
Scott Kennedy | ad41814 | 2013-07-10 17:18:22 -0700 | [diff] [blame] | 4277 | |
| 4278 | protected void navigateUpFolderHierarchy() { |
| 4279 | new AsyncTask<Void, Void, Folder>() { |
| 4280 | @Override |
| 4281 | protected Folder doInBackground(final Void... params) { |
| 4282 | if (mInbox == null) { |
| 4283 | // We don't have an inbox, but we need it |
| 4284 | final Cursor cursor = mContext.getContentResolver().query( |
| 4285 | mAccount.settings.defaultInbox, UIProvider.FOLDERS_PROJECTION, null, |
| 4286 | null, null); |
| 4287 | |
| 4288 | if (cursor != null) { |
| 4289 | try { |
| 4290 | if (cursor.moveToFirst()) { |
| 4291 | mInbox = new Folder(cursor); |
| 4292 | } |
| 4293 | } finally { |
| 4294 | cursor.close(); |
| 4295 | } |
| 4296 | } |
| 4297 | } |
| 4298 | |
| 4299 | // Now try to load our parent |
| 4300 | final Folder folder; |
| 4301 | |
Scott Kennedy | a776c98 | 2013-08-29 11:17:13 -0700 | [diff] [blame] | 4302 | if (mFolder != null) { |
| 4303 | final Cursor cursor = mContext.getContentResolver().query(mFolder.parent, |
| 4304 | UIProvider.FOLDERS_PROJECTION, null, null, null); |
Scott Kennedy | ad41814 | 2013-07-10 17:18:22 -0700 | [diff] [blame] | 4305 | |
Scott Kennedy | a776c98 | 2013-08-29 11:17:13 -0700 | [diff] [blame] | 4306 | if (cursor == null) { |
| 4307 | // We couldn't load the parent, so use the inbox |
| 4308 | folder = mInbox; |
| 4309 | } else { |
| 4310 | try { |
| 4311 | cursor.moveToFirst(); |
| 4312 | folder = new Folder(cursor); |
| 4313 | } finally { |
| 4314 | cursor.close(); |
| 4315 | } |
Scott Kennedy | ad41814 | 2013-07-10 17:18:22 -0700 | [diff] [blame] | 4316 | } |
Scott Kennedy | a776c98 | 2013-08-29 11:17:13 -0700 | [diff] [blame] | 4317 | } else { |
| 4318 | folder = mInbox; |
Scott Kennedy | ad41814 | 2013-07-10 17:18:22 -0700 | [diff] [blame] | 4319 | } |
| 4320 | |
| 4321 | return folder; |
| 4322 | } |
| 4323 | |
| 4324 | @Override |
| 4325 | protected void onPostExecute(final Folder result) { |
| 4326 | onFolderSelected(result); |
| 4327 | } |
| 4328 | }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void[]) null); |
| 4329 | } |
Scott Kennedy | f77806e | 2013-08-30 11:38:15 -0700 | [diff] [blame^] | 4330 | |
| 4331 | @Override |
| 4332 | public Parcelable getConversationListScrollPosition(final String folderUri) { |
| 4333 | return mConversationListScrollPositions.getParcelable(folderUri); |
| 4334 | } |
| 4335 | |
| 4336 | @Override |
| 4337 | public void setConversationListScrollPosition(final String folderUri, |
| 4338 | final Parcelable savedPosition) { |
| 4339 | mConversationListScrollPositions.putParcelable(folderUri, savedPosition); |
| 4340 | } |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 4341 | } |