Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 Google Inc. |
| 3 | * Licensed to The Android Open Source Project. |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 4 | * |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 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 |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 8 | * |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 10 | * |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 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 | */ |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 17 | |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 18 | package com.android.mail.ui; |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 19 | |
Scott Banachowski | 53a615d | 2012-12-19 15:31:20 -0800 | [diff] [blame] | 20 | import android.content.ContentResolver; |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 21 | import android.content.Context; |
Vikram Aggarwal | a51ef9f | 2013-04-02 13:25:35 -0700 | [diff] [blame] | 22 | import android.net.Uri; |
| 23 | import android.os.AsyncTask; |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 24 | import android.os.Bundle; |
Andrew Sapperstein | 52882ff | 2014-07-27 12:30:18 -0700 | [diff] [blame] | 25 | import android.support.v7.app.ActionBar; |
Mindy Pereira | 0531c9f | 2012-06-25 14:12:59 -0700 | [diff] [blame] | 26 | import android.text.TextUtils; |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 27 | import android.view.Menu; |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 28 | import android.view.MenuItem; |
Vikram Aggarwal | a9b93f3 | 2012-02-23 14:51:58 -0800 | [diff] [blame] | 29 | |
Alice Yang | 760d956 | 2013-04-02 13:13:24 -0700 | [diff] [blame] | 30 | import com.android.mail.R; |
Alice Yang | 760d956 | 2013-04-02 13:13:24 -0700 | [diff] [blame] | 31 | import com.android.mail.providers.Account; |
| 32 | import com.android.mail.providers.AccountObserver; |
Alice Yang | 760d956 | 2013-04-02 13:13:24 -0700 | [diff] [blame] | 33 | import com.android.mail.providers.Conversation; |
| 34 | import com.android.mail.providers.Folder; |
| 35 | import com.android.mail.providers.FolderObserver; |
Alice Yang | 760d956 | 2013-04-02 13:13:24 -0700 | [diff] [blame] | 36 | import com.android.mail.providers.UIProvider; |
| 37 | import com.android.mail.providers.UIProvider.AccountCapabilities; |
| 38 | import com.android.mail.providers.UIProvider.FolderCapabilities; |
Scott Kennedy | 7ee089e | 2013-03-25 17:05:44 -0400 | [diff] [blame] | 39 | import com.android.mail.providers.UIProvider.FolderType; |
Alice Yang | 760d956 | 2013-04-02 13:13:24 -0700 | [diff] [blame] | 40 | import com.android.mail.utils.LogTag; |
| 41 | import com.android.mail.utils.LogUtils; |
| 42 | import com.android.mail.utils.Utils; |
| 43 | |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 44 | /** |
Andrew Sapperstein | 2d86d11 | 2014-07-24 20:27:37 -0700 | [diff] [blame] | 45 | * Controller to manage the various states of the {@link android.app.ActionBar}. |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 46 | */ |
Jin Cao | c6801eb | 2014-08-12 18:16:57 -0700 | [diff] [blame] | 47 | public class ActionBarController implements ViewMode.ModeChangeListener { |
Andrew Sapperstein | 2d86d11 | 2014-07-24 20:27:37 -0700 | [diff] [blame] | 48 | |
| 49 | private final Context mContext; |
Andy Huang | af65e73 | 2013-01-28 20:45:40 -0800 | [diff] [blame] | 50 | |
Mindy Pereira | edb135e | 2012-06-15 08:34:09 -0700 | [diff] [blame] | 51 | protected ActionBar mActionBar; |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 52 | protected ControllableActivity mActivity; |
Mindy Pereira | edb135e | 2012-06-15 08:34:09 -0700 | [diff] [blame] | 53 | protected ActivityController mController; |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 54 | /** |
Tony Mantler | bd09150 | 2013-09-16 13:59:47 -0700 | [diff] [blame] | 55 | * The current mode of the ActionBar and Activity |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 56 | */ |
Tony Mantler | bd09150 | 2013-09-16 13:59:47 -0700 | [diff] [blame] | 57 | private ViewMode mViewModeController; |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 58 | |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 59 | /** |
| 60 | * The account currently being shown |
| 61 | */ |
| 62 | private Account mAccount; |
Marc Blank | edcab01 | 2012-04-04 12:42:38 -0700 | [diff] [blame] | 63 | /** |
| 64 | * The folder currently being shown |
| 65 | */ |
| 66 | private Folder mFolder; |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 67 | |
Scott Kennedy | 7ee089e | 2013-03-25 17:05:44 -0400 | [diff] [blame] | 68 | private MenuItem mEmptyTrashItem; |
| 69 | private MenuItem mEmptySpamItem; |
Paul Westbrook | 8c887ef | 2013-04-24 00:29:28 -0700 | [diff] [blame] | 70 | |
Vikram Aggarwal | f642b81 | 2013-03-15 16:30:21 -0700 | [diff] [blame] | 71 | /** True if the current device is a tablet, false otherwise. */ |
Vikram Aggarwal | ad8b99b | 2013-04-07 16:37:28 -0700 | [diff] [blame] | 72 | protected final boolean mIsOnTablet; |
Yorke Lee | f807ba7 | 2012-09-20 17:18:05 -0700 | [diff] [blame] | 73 | private Conversation mCurrentConversation; |
Mindy Pereira | 9b62380 | 2012-03-07 17:15:49 -0800 | [diff] [blame] | 74 | |
Paul Westbrook | b334c90 | 2012-06-25 11:42:46 -0700 | [diff] [blame] | 75 | public static final String LOG_TAG = LogTag.getLogTag(); |
Andy Huang | 0d64735 | 2012-03-21 21:48:16 -0700 | [diff] [blame] | 76 | |
Vikram Aggarwal | 50ff0e5 | 2013-03-14 13:58:02 -0700 | [diff] [blame] | 77 | private FolderObserver mFolderObserver; |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 78 | |
Vikram Aggarwal | a51ef9f | 2013-04-02 13:25:35 -0700 | [diff] [blame] | 79 | /** Updates the resolver and tells it the most recent account. */ |
| 80 | private final class UpdateProvider extends AsyncTask<Bundle, Void, Void> { |
| 81 | final Uri mAccount; |
| 82 | final ContentResolver mResolver; |
| 83 | public UpdateProvider(Uri account, ContentResolver resolver) { |
| 84 | mAccount = account; |
| 85 | mResolver = resolver; |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | protected Void doInBackground(Bundle... params) { |
| 90 | mResolver.call(mAccount, UIProvider.AccountCallMethods.SET_CURRENT_ACCOUNT, |
| 91 | mAccount.toString(), params[0]); |
| 92 | return null; |
| 93 | } |
| 94 | } |
| 95 | |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 96 | private final AccountObserver mAccountObserver = new AccountObserver() { |
| 97 | @Override |
| 98 | public void onChanged(Account newAccount) { |
Scott Banachowski | 53a615d | 2012-12-19 15:31:20 -0800 | [diff] [blame] | 99 | updateAccount(newAccount); |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 100 | } |
| 101 | }; |
Andy Huang | af65e73 | 2013-01-28 20:45:40 -0800 | [diff] [blame] | 102 | |
Andrew Sapperstein | 2d86d11 | 2014-07-24 20:27:37 -0700 | [diff] [blame] | 103 | public ActionBarController(Context context) { |
| 104 | mContext = context; |
| 105 | mIsOnTablet = Utils.useTabletUI(context.getResources()); |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | public boolean onCreateOptionsMenu(Menu menu) { |
James Lemieux | 640741d | 2014-03-20 13:56:08 -0700 | [diff] [blame] | 109 | mEmptyTrashItem = menu.findItem(R.id.empty_trash); |
| 110 | mEmptySpamItem = menu.findItem(R.id.empty_spam); |
James Lemieux | 640741d | 2014-03-20 13:56:08 -0700 | [diff] [blame] | 111 | |
| 112 | // the menu should be displayed if the mode is known |
| 113 | return getMode() != ViewMode.UNKNOWN; |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | public int getOptionsMenuId() { |
Tony Mantler | bd09150 | 2013-09-16 13:59:47 -0700 | [diff] [blame] | 117 | switch (getMode()) { |
Tony Mantler | e29b97e | 2013-08-01 16:37:54 -0700 | [diff] [blame] | 118 | case ViewMode.UNKNOWN: |
| 119 | return R.menu.conversation_list_menu; |
| 120 | case ViewMode.CONVERSATION: |
| 121 | return R.menu.conversation_actions; |
| 122 | case ViewMode.CONVERSATION_LIST: |
| 123 | return R.menu.conversation_list_menu; |
| 124 | case ViewMode.SEARCH_RESULTS_LIST: |
| 125 | return R.menu.conversation_list_search_results_actions; |
| 126 | case ViewMode.SEARCH_RESULTS_CONVERSATION: |
| 127 | return R.menu.conversation_actions; |
| 128 | case ViewMode.WAITING_FOR_ACCOUNT_INITIALIZATION: |
| 129 | return R.menu.wait_mode_actions; |
| 130 | } |
| 131 | LogUtils.wtf(LOG_TAG, "Menu requested for unknown view mode"); |
| 132 | return R.menu.conversation_list_menu; |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 135 | public void initialize(ControllableActivity activity, ActivityController callback, |
Vikram Aggarwal | dac89fa | 2013-03-05 16:43:09 -0800 | [diff] [blame] | 136 | ActionBar actionBar) { |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 137 | mActionBar = actionBar; |
Vikram Aggarwal | a799795 | 2012-03-09 10:30:13 -0800 | [diff] [blame] | 138 | mController = callback; |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 139 | mActivity = activity; |
Paul Westbrook | 8c887ef | 2013-04-24 00:29:28 -0700 | [diff] [blame] | 140 | |
Vikram Aggarwal | 50ff0e5 | 2013-03-14 13:58:02 -0700 | [diff] [blame] | 141 | mFolderObserver = new FolderObserver() { |
| 142 | @Override |
| 143 | public void onChanged(Folder newFolder) { |
| 144 | onFolderUpdated(newFolder); |
| 145 | } |
| 146 | }; |
Vikram Aggarwal | 26b0bfd | 2013-03-29 13:05:08 -0700 | [diff] [blame] | 147 | // Return values are purposely discarded. Initialization happens quite early, and we don't |
| 148 | // have a valid folder, or a valid list of accounts. |
Vikram Aggarwal | 50ff0e5 | 2013-03-14 13:58:02 -0700 | [diff] [blame] | 149 | mFolderObserver.initialize(mController); |
Scott Banachowski | 53a615d | 2012-12-19 15:31:20 -0800 | [diff] [blame] | 150 | updateAccount(mAccountObserver.initialize(activity.getAccountController())); |
| 151 | } |
| 152 | |
| 153 | private void updateAccount(Account account) { |
Vikram Aggarwal | f18b117 | 2013-04-17 12:36:23 -0700 | [diff] [blame] | 154 | final boolean accountChanged = mAccount == null || !mAccount.uri.equals(account.uri); |
Scott Banachowski | 53a615d | 2012-12-19 15:31:20 -0800 | [diff] [blame] | 155 | mAccount = account; |
Vikram Aggarwal | f18b117 | 2013-04-17 12:36:23 -0700 | [diff] [blame] | 156 | if (mAccount != null && accountChanged) { |
Vikram Aggarwal | bc462ca | 2013-03-15 10:41:03 -0700 | [diff] [blame] | 157 | final ContentResolver resolver = mActivity.getActivityContext().getContentResolver(); |
| 158 | final Bundle bundle = new Bundle(1); |
Scott Banachowski | 53a615d | 2012-12-19 15:31:20 -0800 | [diff] [blame] | 159 | bundle.putParcelable(UIProvider.SetCurrentAccountColumns.ACCOUNT, account); |
Vikram Aggarwal | a51ef9f | 2013-04-02 13:25:35 -0700 | [diff] [blame] | 160 | final UpdateProvider updater = new UpdateProvider(mAccount.uri, resolver); |
| 161 | updater.execute(bundle); |
Andrew Sapperstein | 2d86d11 | 2014-07-24 20:27:37 -0700 | [diff] [blame] | 162 | setFolderAndAccount(); |
Scott Banachowski | 53a615d | 2012-12-19 15:31:20 -0800 | [diff] [blame] | 163 | } |
Vikram Aggarwal | abd24d8 | 2012-04-26 13:23:14 -0700 | [diff] [blame] | 164 | } |
Andy Huang | e303b9b | 2012-04-19 16:31:09 -0700 | [diff] [blame] | 165 | |
Vikram Aggarwal | abd24d8 | 2012-04-26 13:23:14 -0700 | [diff] [blame] | 166 | /** |
Vikram Aggarwal | 75d1bb1 | 2013-04-12 17:17:00 -0700 | [diff] [blame] | 167 | * Called by the owner of the ActionBar to change the current folder. |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 168 | */ |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 169 | public void setFolder(Folder folder) { |
Marc Blank | edcab01 | 2012-04-04 12:42:38 -0700 | [diff] [blame] | 170 | mFolder = folder; |
Andrew Sapperstein | 2d86d11 | 2014-07-24 20:27:37 -0700 | [diff] [blame] | 171 | setFolderAndAccount(); |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 172 | } |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 173 | |
Mindy Pereira | 641de65 | 2012-08-02 15:21:50 -0700 | [diff] [blame] | 174 | public void onDestroy() { |
| 175 | if (mFolderObserver != null) { |
Vikram Aggarwal | 50ff0e5 | 2013-03-14 13:58:02 -0700 | [diff] [blame] | 176 | mFolderObserver.unregisterAndDestroy(); |
Mindy Pereira | 641de65 | 2012-08-02 15:21:50 -0700 | [diff] [blame] | 177 | mFolderObserver = null; |
| 178 | } |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 179 | mAccountObserver.unregisterAndDestroy(); |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 180 | } |
| 181 | |
Vikram Aggarwal | a799795 | 2012-03-09 10:30:13 -0800 | [diff] [blame] | 182 | @Override |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 183 | public void onViewModeChanged(int newMode) { |
Rohan Shah | 77db7fd | 2014-08-27 14:18:29 -0700 | [diff] [blame] | 184 | final boolean mIsTabletLandscape = |
| 185 | mContext.getResources().getBoolean(R.bool.is_tablet_landscape); |
| 186 | |
Andrew Sapperstein | 52882ff | 2014-07-27 12:30:18 -0700 | [diff] [blame] | 187 | mActivity.supportInvalidateOptionsMenu(); |
Vikram Aggarwal | 1a3f3fc | 2012-07-18 14:49:08 -0700 | [diff] [blame] | 188 | // Check if we are either on a phone, or in Conversation mode on tablet. For these, the |
| 189 | // recent folders is enabled. |
Tony Mantler | bd09150 | 2013-09-16 13:59:47 -0700 | [diff] [blame] | 190 | switch (getMode()) { |
Andy Huang | d736a38 | 2012-08-29 13:08:58 -0700 | [diff] [blame] | 191 | case ViewMode.UNKNOWN: |
Andy Huang | d736a38 | 2012-08-29 13:08:58 -0700 | [diff] [blame] | 192 | break; |
| 193 | case ViewMode.CONVERSATION_LIST: |
| 194 | showNavList(); |
| 195 | break; |
Vikram Aggarwal | ad8b99b | 2013-04-07 16:37:28 -0700 | [diff] [blame] | 196 | case ViewMode.SEARCH_RESULTS_CONVERSATION: |
Vikram Aggarwal | e875b66 | 2013-04-10 15:56:46 -0700 | [diff] [blame] | 197 | mActionBar.setDisplayHomeAsUpEnabled(true); |
| 198 | setEmptyMode(); |
| 199 | break; |
Andy Huang | d736a38 | 2012-08-29 13:08:58 -0700 | [diff] [blame] | 200 | case ViewMode.CONVERSATION: |
Rohan Shah | 77db7fd | 2014-08-27 14:18:29 -0700 | [diff] [blame] | 201 | // If on tablet landscape, show current folder instead of emptying the action bar |
| 202 | if (mIsTabletLandscape) { |
| 203 | mActionBar.setDisplayHomeAsUpEnabled(true); |
| 204 | showNavList(); |
| 205 | break; |
| 206 | } |
| 207 | // Otherwise, fall through to default behavior, shared with Ads ViewMode. |
Andrew Sapperstein | 6c570db | 2013-08-06 17:21:36 -0700 | [diff] [blame] | 208 | case ViewMode.AD: |
Andy Huang | d736a38 | 2012-08-29 13:08:58 -0700 | [diff] [blame] | 209 | mActionBar.setDisplayHomeAsUpEnabled(true); |
Vikram Aggarwal | ad8b99b | 2013-04-07 16:37:28 -0700 | [diff] [blame] | 210 | setEmptyMode(); |
Andy Huang | d736a38 | 2012-08-29 13:08:58 -0700 | [diff] [blame] | 211 | break; |
Andy Huang | d736a38 | 2012-08-29 13:08:58 -0700 | [diff] [blame] | 212 | case ViewMode.WAITING_FOR_ACCOUNT_INITIALIZATION: |
| 213 | // We want the user to be able to switch accounts while waiting for an account |
| 214 | // to sync. |
| 215 | showNavList(); |
| 216 | break; |
| 217 | } |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 218 | } |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 219 | |
Mindy Pereira | edb135e | 2012-06-15 08:34:09 -0700 | [diff] [blame] | 220 | protected int getMode() { |
Tony Mantler | bd09150 | 2013-09-16 13:59:47 -0700 | [diff] [blame] | 221 | if (mViewModeController != null) { |
| 222 | return mViewModeController.getMode(); |
| 223 | } else { |
| 224 | return ViewMode.UNKNOWN; |
| 225 | } |
Mindy Pereira | edb135e | 2012-06-15 08:34:09 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Jin Cao | e9f0783 | 2014-04-15 13:59:37 -0700 | [diff] [blame] | 228 | /** |
| 229 | * Helper function to ensure that the menu items that are prone to variable changes and race |
| 230 | * conditions are properly set to the correct visibility |
| 231 | */ |
| 232 | public void validateVolatileMenuOptionVisibility() { |
Jin Cao | e9f0783 | 2014-04-15 13:59:37 -0700 | [diff] [blame] | 233 | if (mEmptyTrashItem != null) { |
| 234 | mEmptyTrashItem.setVisible(mAccount != null && mFolder != null |
| 235 | && mAccount.supportsCapability(AccountCapabilities.EMPTY_TRASH) |
Jin Cao | 3f386e8 | 2014-04-18 11:29:54 -0700 | [diff] [blame] | 236 | && mFolder.isTrash() && mFolder.totalCount > 0 |
| 237 | && (mController.getConversationListCursor() == null |
| 238 | || mController.getConversationListCursor().getCount() > 0)); |
Jin Cao | e9f0783 | 2014-04-15 13:59:37 -0700 | [diff] [blame] | 239 | } |
| 240 | if (mEmptySpamItem != null) { |
| 241 | mEmptySpamItem.setVisible(mAccount != null && mFolder != null |
| 242 | && mAccount.supportsCapability(AccountCapabilities.EMPTY_SPAM) |
Jin Cao | 3f386e8 | 2014-04-18 11:29:54 -0700 | [diff] [blame] | 243 | && mFolder.isType(FolderType.SPAM) && mFolder.totalCount > 0 |
| 244 | && (mController.getConversationListCursor() == null |
| 245 | || mController.getConversationListCursor().getCount() > 0)); |
Jin Cao | e9f0783 | 2014-04-15 13:59:37 -0700 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | |
Mindy Pereira | b849dfb | 2012-03-07 18:13:15 -0800 | [diff] [blame] | 249 | public boolean onPrepareOptionsMenu(Menu menu) { |
Jin Cao | 1af562a | 2014-09-09 14:49:53 -0700 | [diff] [blame] | 250 | menu.setQwertyMode(true); |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 251 | // We start out with every option enabled. Based on the current view, we disable actions |
| 252 | // that are possible. |
Vikram Aggarwal | 95b28ab | 2012-04-26 15:53:09 -0700 | [diff] [blame] | 253 | LogUtils.d(LOG_TAG, "ActionBarView.onPrepareOptionsMenu()."); |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 254 | |
Tony Mantler | b3281fb | 2013-08-20 14:30:29 -0700 | [diff] [blame] | 255 | if (mController.shouldHideMenuItems()) { |
Ray Chen | a57da3c | 2014-06-10 16:01:40 +0200 | [diff] [blame] | 256 | // Shortcut: hide all menu items if the drawer is shown |
Tony Mantler | b3281fb | 2013-08-20 14:30:29 -0700 | [diff] [blame] | 257 | final int size = menu.size(); |
| 258 | |
| 259 | for (int i = 0; i < size; i++) { |
| 260 | final MenuItem item = menu.getItem(i); |
Ray Chen | a57da3c | 2014-06-10 16:01:40 +0200 | [diff] [blame] | 261 | item.setVisible(false); |
Tony Mantler | b3281fb | 2013-08-20 14:30:29 -0700 | [diff] [blame] | 262 | } |
| 263 | return false; |
| 264 | } |
Jin Cao | e9f0783 | 2014-04-15 13:59:37 -0700 | [diff] [blame] | 265 | validateVolatileMenuOptionVisibility(); |
Mindy Pereira | 62c7179 | 2012-07-18 09:14:29 -0700 | [diff] [blame] | 266 | |
Tony Mantler | bd09150 | 2013-09-16 13:59:47 -0700 | [diff] [blame] | 267 | switch (getMode()) { |
Yorke Lee | f807ba7 | 2012-09-20 17:18:05 -0700 | [diff] [blame] | 268 | case ViewMode.CONVERSATION: |
| 269 | case ViewMode.SEARCH_RESULTS_CONVERSATION: |
| 270 | // We update the ActionBar options when we are entering conversation view because |
| 271 | // waiting for the AbstractConversationViewFragment to do it causes duplicate icons |
| 272 | // to show up during the time between the conversation is selected and the fragment |
| 273 | // is added. |
| 274 | setConversationModeOptions(menu); |
| 275 | break; |
Jin Cao | d033473 | 2014-09-01 22:33:14 -0700 | [diff] [blame] | 276 | case ViewMode.CONVERSATION_LIST: |
Jin Cao | a6b4521 | 2014-09-18 13:46:54 -0700 | [diff] [blame] | 277 | case ViewMode.SEARCH_RESULTS_LIST: |
Jin Cao | d033473 | 2014-09-01 22:33:14 -0700 | [diff] [blame] | 278 | // The search menu item should only be visible for non-tablet devices |
| 279 | Utils.setMenuItemVisibility(menu, R.id.search, |
| 280 | mAccount.supportsSearch() && !mIsOnTablet); |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 281 | } |
Scott Kennedy | a85831d | 2013-04-12 15:54:22 -0700 | [diff] [blame] | 282 | |
| 283 | return false; |
| 284 | } |
| 285 | |
Scott Kennedy | 26f2b30 | 2013-04-22 17:47:17 -0700 | [diff] [blame] | 286 | /** |
Vikram Aggarwal | ad8b99b | 2013-04-07 16:37:28 -0700 | [diff] [blame] | 287 | * Put the ActionBar in List navigation mode. |
Vikram Aggarwal | abd24d8 | 2012-04-26 13:23:14 -0700 | [diff] [blame] | 288 | */ |
Mindy Pereira | 72a9f39 | 2012-03-21 09:41:09 -0700 | [diff] [blame] | 289 | private void showNavList() { |
Andrew Sapperstein | 2d86d11 | 2014-07-24 20:27:37 -0700 | [diff] [blame] | 290 | setTitleModeFlags(ActionBar.DISPLAY_SHOW_TITLE); |
| 291 | setFolderAndAccount(); |
Paul Westbrook | 8c887ef | 2013-04-24 00:29:28 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Andrew Sapperstein | 0a0795c | 2014-01-27 16:52:45 -0800 | [diff] [blame] | 294 | private void setTitle(String title) { |
Andy Huang | c499d71 | 2013-06-12 17:37:57 -0700 | [diff] [blame] | 295 | if (!TextUtils.equals(title, mActionBar.getTitle())) { |
| 296 | mActionBar.setTitle(title); |
| 297 | } |
mindyp | 6fb1b62 | 2012-09-05 09:27:08 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | /** |
Vikram Aggarwal | fd7eaba | 2013-04-11 12:31:46 -0700 | [diff] [blame] | 301 | * Set the actionbar mode to empty: no title, no subtitle, no custom view. |
mindyp | 6fb1b62 | 2012-09-05 09:27:08 -0700 | [diff] [blame] | 302 | */ |
| 303 | protected void setEmptyMode() { |
Vikram Aggarwal | fd7eaba | 2013-04-11 12:31:46 -0700 | [diff] [blame] | 304 | // Disable title/subtitle and the custom view by setting the bitmask to all off. |
| 305 | setTitleModeFlags(0); |
Vikram Aggarwal | abd24d8 | 2012-04-26 13:23:14 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Vikram Aggarwal | ad8b99b | 2013-04-07 16:37:28 -0700 | [diff] [blame] | 308 | /** |
| 309 | * Removes the back button from being shown |
| 310 | */ |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 311 | public void removeBackButton() { |
| 312 | if (mActionBar == null) { |
| 313 | return; |
| 314 | } |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame] | 315 | // Remove the back button but continue showing an icon. |
Andrew Sapperstein | 2d86d11 | 2014-07-24 20:27:37 -0700 | [diff] [blame] | 316 | final int mask = ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME; |
| 317 | mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME, mask); |
Andrew Sapperstein | 52882ff | 2014-07-27 12:30:18 -0700 | [diff] [blame] | 318 | mActionBar.setHomeButtonEnabled(false); |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 319 | } |
Mindy Pereira | bc57bf1 | 2012-02-29 14:39:09 -0800 | [diff] [blame] | 320 | |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 321 | public void setBackButton() { |
Andy Huang | 12b3ee4 | 2013-04-24 22:49:43 -0700 | [diff] [blame] | 322 | if (mActionBar == null) { |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 323 | return; |
| 324 | } |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame] | 325 | // Show home as up, and show an icon. |
Andrew Sapperstein | 2d86d11 | 2014-07-24 20:27:37 -0700 | [diff] [blame] | 326 | final int mask = ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME; |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame] | 327 | mActionBar.setDisplayOptions(mask, mask); |
Andrew Sapperstein | 52882ff | 2014-07-27 12:30:18 -0700 | [diff] [blame] | 328 | mActionBar.setHomeButtonEnabled(true); |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 329 | } |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 330 | |
Vikram Aggarwal | f642b81 | 2013-03-15 16:30:21 -0700 | [diff] [blame] | 331 | /** |
| 332 | * Uses the current state to update the current folder {@link #mFolder} and the current |
Vikram Aggarwal | 75d1bb1 | 2013-04-12 17:17:00 -0700 | [diff] [blame] | 333 | * account {@link #mAccount} shown in the actionbar. Also updates the actionbar subtitle to |
| 334 | * momentarily display the unread count if it has changed. |
Vikram Aggarwal | f642b81 | 2013-03-15 16:30:21 -0700 | [diff] [blame] | 335 | */ |
Andrew Sapperstein | 2d86d11 | 2014-07-24 20:27:37 -0700 | [diff] [blame] | 336 | private void setFolderAndAccount() { |
Vikram Aggarwal | 5fce992 | 2013-04-01 21:17:42 -0700 | [diff] [blame] | 337 | // Very little can be done if the actionbar or activity is null. |
| 338 | if (mActionBar == null || mActivity == null) { |
| 339 | return; |
| 340 | } |
Tony Mantler | bd09150 | 2013-09-16 13:59:47 -0700 | [diff] [blame] | 341 | if (ViewMode.isWaitingForSync(getMode())) { |
Vikram Aggarwal | 75d1bb1 | 2013-04-12 17:17:00 -0700 | [diff] [blame] | 342 | // Account is not synced: clear title and update the subtitle. |
Paul Westbrook | 8c887ef | 2013-04-24 00:29:28 -0700 | [diff] [blame] | 343 | setTitle(""); |
Vikram Aggarwal | f642b81 | 2013-03-15 16:30:21 -0700 | [diff] [blame] | 344 | return; |
| 345 | } |
Vikram Aggarwal | 75d1bb1 | 2013-04-12 17:17:00 -0700 | [diff] [blame] | 346 | // Check if we should be changing the actionbar at all, and back off if not. |
Tony Mantler | bd09150 | 2013-09-16 13:59:47 -0700 | [diff] [blame] | 347 | final boolean isShowingFolder = mIsOnTablet || ViewMode.isListMode(getMode()); |
Vikram Aggarwal | 75d1bb1 | 2013-04-12 17:17:00 -0700 | [diff] [blame] | 348 | if (!isShowingFolder) { |
Paul Westbrook | 9102495 | 2013-04-21 15:59:06 -0700 | [diff] [blame] | 349 | // It isn't necessary to set the title in this case, as the title view will |
| 350 | // be hidden |
Vikram Aggarwal | 75d1bb1 | 2013-04-12 17:17:00 -0700 | [diff] [blame] | 351 | return; |
Vikram Aggarwal | f642b81 | 2013-03-15 16:30:21 -0700 | [diff] [blame] | 352 | } |
Vikram Aggarwal | 26b0bfd | 2013-03-29 13:05:08 -0700 | [diff] [blame] | 353 | if (mFolder == null) { |
Paul Westbrook | 9102495 | 2013-04-21 15:59:06 -0700 | [diff] [blame] | 354 | // Clear the action bar title. We don't want the app name to be shown while |
| 355 | // waiting for the folder query to finish |
Paul Westbrook | 8c887ef | 2013-04-24 00:29:28 -0700 | [diff] [blame] | 356 | setTitle(""); |
Vikram Aggarwal | 26b0bfd | 2013-03-29 13:05:08 -0700 | [diff] [blame] | 357 | return; |
| 358 | } |
Paul Westbrook | 8c887ef | 2013-04-24 00:29:28 -0700 | [diff] [blame] | 359 | setTitle(mFolder.name); |
Vikram Aggarwal | f18b117 | 2013-04-17 12:36:23 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Vikram Aggarwal | bc462ca | 2013-03-15 10:41:03 -0700 | [diff] [blame] | 362 | |
Mindy Pereira | a46a57f | 2012-04-03 17:19:31 -0700 | [diff] [blame] | 363 | /** |
| 364 | * Notify that the folder has changed. |
| 365 | */ |
| 366 | public void onFolderUpdated(Folder folder) { |
Vikram Aggarwal | 26b0bfd | 2013-03-29 13:05:08 -0700 | [diff] [blame] | 367 | if (folder == null) { |
| 368 | return; |
| 369 | } |
Vikram Aggarwal | e875b66 | 2013-04-10 15:56:46 -0700 | [diff] [blame] | 370 | /** True if we are changing folders. */ |
Vikram Aggarwal | c18c338 | 2013-04-05 15:29:13 -0700 | [diff] [blame] | 371 | mFolder = folder; |
Andrew Sapperstein | 2d86d11 | 2014-07-24 20:27:37 -0700 | [diff] [blame] | 372 | setFolderAndAccount(); |
Jin Cao | e9f0783 | 2014-04-15 13:59:37 -0700 | [diff] [blame] | 373 | // make sure that we re-validate the optional menu items |
| 374 | validateVolatileMenuOptionVisibility(); |
Mindy Pereira | a46a57f | 2012-04-03 17:19:31 -0700 | [diff] [blame] | 375 | } |
Vikram Aggarwal | 0dda573 | 2012-04-06 11:20:16 -0700 | [diff] [blame] | 376 | |
Vikram Aggarwal | fd7eaba | 2013-04-11 12:31:46 -0700 | [diff] [blame] | 377 | /** |
| 378 | * Sets the actionbar mode: Pass it an integer which contains each of these values, perhaps |
Paul Westbrook | 8c887ef | 2013-04-24 00:29:28 -0700 | [diff] [blame] | 379 | * OR'd together: {@link ActionBar#DISPLAY_SHOW_CUSTOM} and |
| 380 | * {@link ActionBar#DISPLAY_SHOW_TITLE}. To disable all, pass a zero. |
Vikram Aggarwal | fd7eaba | 2013-04-11 12:31:46 -0700 | [diff] [blame] | 381 | * @param enabledFlags |
| 382 | */ |
Andy Huang | af65e73 | 2013-01-28 20:45:40 -0800 | [diff] [blame] | 383 | private void setTitleModeFlags(int enabledFlags) { |
Paul Westbrook | 8c887ef | 2013-04-24 00:29:28 -0700 | [diff] [blame] | 384 | final int mask = ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_CUSTOM; |
Andy Huang | af65e73 | 2013-01-28 20:45:40 -0800 | [diff] [blame] | 385 | mActionBar.setDisplayOptions(enabledFlags, mask); |
| 386 | } |
| 387 | |
Yorke Lee | f807ba7 | 2012-09-20 17:18:05 -0700 | [diff] [blame] | 388 | public void setCurrentConversation(Conversation conversation) { |
| 389 | mCurrentConversation = conversation; |
| 390 | } |
| 391 | |
| 392 | //We need to do this here instead of in the fragment |
| 393 | public void setConversationModeOptions(Menu menu) { |
| 394 | if (mCurrentConversation == null) { |
| 395 | return; |
| 396 | } |
| 397 | final boolean showMarkImportant = !mCurrentConversation.isImportant(); |
| 398 | Utils.setMenuItemVisibility(menu, R.id.mark_important, showMarkImportant |
| 399 | && mAccount.supportsCapability(UIProvider.AccountCapabilities.MARK_IMPORTANT)); |
| 400 | Utils.setMenuItemVisibility(menu, R.id.mark_not_important, !showMarkImportant |
| 401 | && mAccount.supportsCapability(UIProvider.AccountCapabilities.MARK_IMPORTANT)); |
Jin Cao | 512821c | 2014-05-30 15:54:04 -0700 | [diff] [blame] | 402 | final boolean isOutbox = mFolder.isType(FolderType.OUTBOX); |
| 403 | final boolean showDiscardOutbox = mFolder != null && isOutbox && |
| 404 | mCurrentConversation.sendingState == UIProvider.ConversationSendingState.SEND_ERROR; |
| 405 | Utils.setMenuItemVisibility(menu, R.id.discard_outbox, showDiscardOutbox); |
| 406 | final boolean showDelete = !isOutbox && mFolder != null && |
Yorke Lee | f807ba7 | 2012-09-20 17:18:05 -0700 | [diff] [blame] | 407 | mFolder.supportsCapability(UIProvider.FolderCapabilities.DELETE); |
| 408 | Utils.setMenuItemVisibility(menu, R.id.delete, showDelete); |
| 409 | // We only want to show the discard drafts menu item if we are not showing the delete menu |
| 410 | // item, and the current folder is a draft folder and the account supports discarding |
| 411 | // drafts for a conversation |
| 412 | final boolean showDiscardDrafts = !showDelete && mFolder != null && mFolder.isDraft() && |
| 413 | mAccount.supportsCapability(AccountCapabilities.DISCARD_CONVERSATION_DRAFTS); |
| 414 | Utils.setMenuItemVisibility(menu, R.id.discard_drafts, showDiscardDrafts); |
| 415 | final boolean archiveVisible = mAccount.supportsCapability(AccountCapabilities.ARCHIVE) |
| 416 | && mFolder != null && mFolder.supportsCapability(FolderCapabilities.ARCHIVE) |
| 417 | && !mFolder.isTrash(); |
| 418 | Utils.setMenuItemVisibility(menu, R.id.archive, archiveVisible); |
| 419 | Utils.setMenuItemVisibility(menu, R.id.remove_folder, !archiveVisible && mFolder != null |
| 420 | && mFolder.supportsCapability(FolderCapabilities.CAN_ACCEPT_MOVED_MESSAGES) |
Tony Mantler | 53b3425 | 2013-09-25 16:42:25 -0700 | [diff] [blame] | 421 | && !mFolder.isProviderFolder() |
| 422 | && mAccount.supportsCapability(AccountCapabilities.ARCHIVE)); |
Scott Kennedy | 28f3d5c | 2013-04-08 16:53:50 -0700 | [diff] [blame] | 423 | Utils.setMenuItemVisibility(menu, R.id.move_to, mFolder != null |
| 424 | && mFolder.supportsCapability(FolderCapabilities.ALLOWS_REMOVE_CONVERSATION)); |
Scott Kennedy | dd2ec68 | 2013-06-03 19:16:13 -0700 | [diff] [blame] | 425 | Utils.setMenuItemVisibility(menu, R.id.move_to_inbox, mFolder != null |
| 426 | && mFolder.supportsCapability(FolderCapabilities.ALLOWS_MOVE_TO_INBOX)); |
Rohan Shah | 69419bb | 2014-08-15 15:53:29 -0700 | [diff] [blame] | 427 | Utils.setMenuItemVisibility(menu, R.id.change_folders, mAccount.supportsCapability( |
| 428 | UIProvider.AccountCapabilities.MULTIPLE_FOLDERS_PER_CONV)); |
Scott Kennedy | dd2ec68 | 2013-06-03 19:16:13 -0700 | [diff] [blame] | 429 | |
Yorke Lee | f807ba7 | 2012-09-20 17:18:05 -0700 | [diff] [blame] | 430 | final MenuItem removeFolder = menu.findItem(R.id.remove_folder); |
Alice Yang | 40698e6 | 2013-03-14 11:31:53 -0700 | [diff] [blame] | 431 | if (mFolder != null && removeFolder != null) { |
Yorke Lee | f807ba7 | 2012-09-20 17:18:05 -0700 | [diff] [blame] | 432 | removeFolder.setTitle(mActivity.getApplicationContext().getString( |
| 433 | R.string.remove_folder, mFolder.name)); |
| 434 | } |
| 435 | Utils.setMenuItemVisibility(menu, R.id.report_spam, |
| 436 | mAccount.supportsCapability(AccountCapabilities.REPORT_SPAM) && mFolder != null |
| 437 | && mFolder.supportsCapability(FolderCapabilities.REPORT_SPAM) |
| 438 | && !mCurrentConversation.spam); |
| 439 | Utils.setMenuItemVisibility(menu, R.id.mark_not_spam, |
| 440 | mAccount.supportsCapability(AccountCapabilities.REPORT_SPAM) && mFolder != null |
| 441 | && mFolder.supportsCapability(FolderCapabilities.MARK_NOT_SPAM) |
| 442 | && mCurrentConversation.spam); |
| 443 | Utils.setMenuItemVisibility(menu, R.id.report_phishing, |
| 444 | mAccount.supportsCapability(AccountCapabilities.REPORT_PHISHING) && mFolder != null |
| 445 | && mFolder.supportsCapability(FolderCapabilities.REPORT_PHISHING) |
| 446 | && !mCurrentConversation.phishing); |
| 447 | Utils.setMenuItemVisibility(menu, R.id.mute, |
| 448 | mAccount.supportsCapability(AccountCapabilities.MUTE) && mFolder != null |
| 449 | && mFolder.supportsCapability(FolderCapabilities.DESTRUCTIVE_MUTE) |
| 450 | && !mCurrentConversation.muted); |
| 451 | } |
Paul Westbrook | 8c887ef | 2013-04-24 00:29:28 -0700 | [diff] [blame] | 452 | |
Tony Mantler | bd09150 | 2013-09-16 13:59:47 -0700 | [diff] [blame] | 453 | public void setViewModeController(ViewMode viewModeController) { |
| 454 | mViewModeController = viewModeController; |
| 455 | mViewModeController.addListener(this); |
| 456 | } |
Andrew Sapperstein | 2d86d11 | 2014-07-24 20:27:37 -0700 | [diff] [blame] | 457 | |
| 458 | public Context getContext() { |
| 459 | return mContext; |
| 460 | } |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 461 | } |