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 | |
| 20 | import android.app.ActionBar; |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 21 | import android.app.SearchManager; |
| 22 | import android.app.SearchableInfo; |
Scott Banachowski | 53a615d | 2012-12-19 15:31:20 -0800 | [diff] [blame] | 23 | import android.content.ContentResolver; |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 24 | import android.content.Context; |
Vikram Aggarwal | 75d1bb1 | 2013-04-12 17:17:00 -0700 | [diff] [blame] | 25 | import android.content.res.Resources; |
Vikram Aggarwal | 12a7c04 | 2012-03-28 16:48:08 -0700 | [diff] [blame] | 26 | import android.database.Cursor; |
Vikram Aggarwal | a51ef9f | 2013-04-02 13:25:35 -0700 | [diff] [blame] | 27 | import android.net.Uri; |
| 28 | import android.os.AsyncTask; |
Paul Westbrook | 8c887ef | 2013-04-24 00:29:28 -0700 | [diff] [blame] | 29 | import android.os.Build; |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 30 | import android.os.Bundle; |
Mindy Pereira | 9b62380 | 2012-03-07 17:15:49 -0800 | [diff] [blame] | 31 | import android.os.Handler; |
Vikram Aggarwal | 75d1bb1 | 2013-04-12 17:17:00 -0700 | [diff] [blame] | 32 | import android.os.Message; |
Mindy Pereira | 0531c9f | 2012-06-25 14:12:59 -0700 | [diff] [blame] | 33 | import android.text.TextUtils; |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 34 | import android.util.AttributeSet; |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 35 | import android.view.Menu; |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 36 | import android.view.MenuItem; |
| 37 | import android.view.View; |
Scott Kennedy | b905d08 | 2013-05-07 16:44:34 -0700 | [diff] [blame] | 38 | import android.view.ViewConfiguration; |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 39 | import android.widget.LinearLayout; |
| 40 | import android.widget.SearchView; |
| 41 | import android.widget.SearchView.OnQueryTextListener; |
Vikram Aggarwal | 12a7c04 | 2012-03-28 16:48:08 -0700 | [diff] [blame] | 42 | import android.widget.SearchView.OnSuggestionListener; |
Paul Westbrook | 8c887ef | 2013-04-24 00:29:28 -0700 | [diff] [blame] | 43 | import android.widget.TextView; |
Vikram Aggarwal | a9b93f3 | 2012-02-23 14:51:58 -0800 | [diff] [blame] | 44 | |
Alice Yang | 760d956 | 2013-04-02 13:13:24 -0700 | [diff] [blame] | 45 | import com.android.mail.ConversationListContext; |
| 46 | import com.android.mail.R; |
Scott Kennedy | a85831d | 2013-04-12 15:54:22 -0700 | [diff] [blame] | 47 | import com.android.mail.preferences.MailPrefs; |
Alice Yang | 760d956 | 2013-04-02 13:13:24 -0700 | [diff] [blame] | 48 | import com.android.mail.providers.Account; |
| 49 | import com.android.mail.providers.AccountObserver; |
Alice Yang | 760d956 | 2013-04-02 13:13:24 -0700 | [diff] [blame] | 50 | import com.android.mail.providers.Conversation; |
| 51 | import com.android.mail.providers.Folder; |
| 52 | import com.android.mail.providers.FolderObserver; |
| 53 | import com.android.mail.providers.SearchRecentSuggestionsProvider; |
| 54 | import com.android.mail.providers.UIProvider; |
| 55 | import com.android.mail.providers.UIProvider.AccountCapabilities; |
| 56 | import com.android.mail.providers.UIProvider.FolderCapabilities; |
Scott Kennedy | 7ee089e | 2013-03-25 17:05:44 -0400 | [diff] [blame] | 57 | import com.android.mail.providers.UIProvider.FolderType; |
Alice Yang | 760d956 | 2013-04-02 13:13:24 -0700 | [diff] [blame] | 58 | import com.android.mail.utils.LogTag; |
| 59 | import com.android.mail.utils.LogUtils; |
| 60 | import com.android.mail.utils.Utils; |
| 61 | |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 62 | /** |
Andy Huang | 6681e54 | 2012-06-14 14:36:45 -0700 | [diff] [blame] | 63 | * View to manage the various states of the Mail Action Bar. |
| 64 | * <p> |
| 65 | * This also happens to be the custom view we supply to ActionBar. |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 66 | * |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 67 | */ |
mindyp | 6fb1b62 | 2012-09-05 09:27:08 -0700 | [diff] [blame] | 68 | public class MailActionBarView extends LinearLayout implements ViewMode.ModeChangeListener, |
Paul Westbrook | 8c887ef | 2013-04-24 00:29:28 -0700 | [diff] [blame] | 69 | OnQueryTextListener, OnSuggestionListener, MenuItem.OnActionExpandListener, |
| 70 | View.OnClickListener { |
Andy Huang | af65e73 | 2013-01-28 20:45:40 -0800 | [diff] [blame] | 71 | |
Mindy Pereira | edb135e | 2012-06-15 08:34:09 -0700 | [diff] [blame] | 72 | protected ActionBar mActionBar; |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 73 | protected ControllableActivity mActivity; |
Mindy Pereira | edb135e | 2012-06-15 08:34:09 -0700 | [diff] [blame] | 74 | protected ActivityController mController; |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 75 | /** |
Tony Mantler | bd09150 | 2013-09-16 13:59:47 -0700 | [diff] [blame^] | 76 | * The current mode of the ActionBar and Activity |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 77 | */ |
Tony Mantler | bd09150 | 2013-09-16 13:59:47 -0700 | [diff] [blame^] | 78 | private ViewMode mViewModeController; |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 79 | |
| 80 | private MenuItem mSearch; |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 81 | /** |
| 82 | * The account currently being shown |
| 83 | */ |
| 84 | private Account mAccount; |
Marc Blank | edcab01 | 2012-04-04 12:42:38 -0700 | [diff] [blame] | 85 | /** |
| 86 | * The folder currently being shown |
| 87 | */ |
| 88 | private Folder mFolder; |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 89 | |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 90 | private SearchView mSearchWidget; |
| 91 | private MenuItem mHelpItem; |
Paul Westbrook | 517743e | 2012-03-22 10:40:46 -0700 | [diff] [blame] | 92 | private MenuItem mSendFeedbackItem; |
Marc Blank | edcab01 | 2012-04-04 12:42:38 -0700 | [diff] [blame] | 93 | private MenuItem mFolderSettingsItem; |
Scott Kennedy | 7ee089e | 2013-03-25 17:05:44 -0400 | [diff] [blame] | 94 | private MenuItem mEmptyTrashItem; |
| 95 | private MenuItem mEmptySpamItem; |
Paul Westbrook | 8c887ef | 2013-04-24 00:29:28 -0700 | [diff] [blame] | 96 | private boolean mUseLegacyTitle; |
| 97 | private View mLegacyTitleContainer; |
| 98 | private TextView mLegacyTitle; |
| 99 | private TextView mLegacySubTitle; |
| 100 | |
Vikram Aggarwal | f642b81 | 2013-03-15 16:30:21 -0700 | [diff] [blame] | 101 | /** True if the current device is a tablet, false otherwise. */ |
Vikram Aggarwal | ad8b99b | 2013-04-07 16:37:28 -0700 | [diff] [blame] | 102 | protected final boolean mIsOnTablet; |
Yorke Lee | f807ba7 | 2012-09-20 17:18:05 -0700 | [diff] [blame] | 103 | private Conversation mCurrentConversation; |
Mindy Pereira | 9b62380 | 2012-03-07 17:15:49 -0800 | [diff] [blame] | 104 | |
Paul Westbrook | b334c90 | 2012-06-25 11:42:46 -0700 | [diff] [blame] | 105 | public static final String LOG_TAG = LogTag.getLogTag(); |
Andy Huang | 0d64735 | 2012-03-21 21:48:16 -0700 | [diff] [blame] | 106 | |
Vikram Aggarwal | 50ff0e5 | 2013-03-14 13:58:02 -0700 | [diff] [blame] | 107 | private FolderObserver mFolderObserver; |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 108 | |
Vikram Aggarwal | 75d1bb1 | 2013-04-12 17:17:00 -0700 | [diff] [blame] | 109 | /** A handler that changes the subtitle when it receives a message. */ |
| 110 | private final class SubtitleHandler extends Handler { |
| 111 | /** Message sent to display the account email address in the subtitle. */ |
| 112 | private static final int EMAIL = 0; |
| 113 | |
| 114 | @Override |
| 115 | public void handleMessage(Message message) { |
| 116 | assert (message.what == EMAIL); |
| 117 | final String subtitleText; |
Vikram Aggarwal | b335fb3 | 2013-04-22 17:16:15 -0700 | [diff] [blame] | 118 | if (mAccount != null) { |
Vikram Aggarwal | 75d1bb1 | 2013-04-12 17:17:00 -0700 | [diff] [blame] | 119 | // Display the account name (email address). |
| 120 | subtitleText = mAccount.name; |
| 121 | } else { |
Vikram Aggarwal | b335fb3 | 2013-04-22 17:16:15 -0700 | [diff] [blame] | 122 | subtitleText = null; |
| 123 | LogUtils.wtf(LOG_TAG, "MABV.handleMessage() has a null account!"); |
Vikram Aggarwal | 75d1bb1 | 2013-04-12 17:17:00 -0700 | [diff] [blame] | 124 | } |
Paul Westbrook | 8c887ef | 2013-04-24 00:29:28 -0700 | [diff] [blame] | 125 | setSubtitle(subtitleText); |
Vikram Aggarwal | 75d1bb1 | 2013-04-12 17:17:00 -0700 | [diff] [blame] | 126 | super.handleMessage(message); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /** Changes the subtitle to display the account name */ |
| 131 | private final SubtitleHandler mHandler = new SubtitleHandler(); |
| 132 | /** Unread count for the current folder. */ |
| 133 | private int mUnreadCount = 0; |
| 134 | /** We show the email address after this delay: 5 seconds currently */ |
| 135 | private static final int ACCOUNT_DELAY_MS = 5 * 1000; |
| 136 | /** At what point do we stop showing the unread count: 999+ currently */ |
| 137 | private final int UNREAD_LIMIT; |
| 138 | |
Vikram Aggarwal | a51ef9f | 2013-04-02 13:25:35 -0700 | [diff] [blame] | 139 | /** Updates the resolver and tells it the most recent account. */ |
| 140 | private final class UpdateProvider extends AsyncTask<Bundle, Void, Void> { |
| 141 | final Uri mAccount; |
| 142 | final ContentResolver mResolver; |
| 143 | public UpdateProvider(Uri account, ContentResolver resolver) { |
| 144 | mAccount = account; |
| 145 | mResolver = resolver; |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | protected Void doInBackground(Bundle... params) { |
| 150 | mResolver.call(mAccount, UIProvider.AccountCallMethods.SET_CURRENT_ACCOUNT, |
| 151 | mAccount.toString(), params[0]); |
| 152 | return null; |
| 153 | } |
| 154 | } |
| 155 | |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 156 | private final AccountObserver mAccountObserver = new AccountObserver() { |
| 157 | @Override |
| 158 | public void onChanged(Account newAccount) { |
Scott Banachowski | 53a615d | 2012-12-19 15:31:20 -0800 | [diff] [blame] | 159 | updateAccount(newAccount); |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 160 | } |
| 161 | }; |
Andy Huang | af65e73 | 2013-01-28 20:45:40 -0800 | [diff] [blame] | 162 | |
Andy Huang | 6681e54 | 2012-06-14 14:36:45 -0700 | [diff] [blame] | 163 | public MailActionBarView(Context context) { |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 164 | this(context, null); |
| 165 | } |
| 166 | |
Andy Huang | 6681e54 | 2012-06-14 14:36:45 -0700 | [diff] [blame] | 167 | public MailActionBarView(Context context, AttributeSet attrs) { |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 168 | this(context, attrs, 0); |
| 169 | } |
| 170 | |
Andy Huang | 6681e54 | 2012-06-14 14:36:45 -0700 | [diff] [blame] | 171 | public MailActionBarView(Context context, AttributeSet attrs, int defStyle) { |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 172 | super(context, attrs, defStyle); |
Vikram Aggarwal | 75d1bb1 | 2013-04-12 17:17:00 -0700 | [diff] [blame] | 173 | final Resources r = getResources(); |
| 174 | mIsOnTablet = Utils.useTabletUI(r); |
| 175 | UNREAD_LIMIT = r.getInteger(R.integer.maxUnreadCount); |
Andy Huang | 5895f7b | 2012-06-01 17:07:20 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Paul Westbrook | 8c887ef | 2013-04-24 00:29:28 -0700 | [diff] [blame] | 178 | private void initializeTitleViews() { |
| 179 | mLegacyTitleContainer = findViewById(R.id.legacy_title_container); |
| 180 | if (mLegacyTitleContainer != null) { |
| 181 | // Determine if this device is running on MR1.1 or later |
| 182 | final boolean runningMR11OrLater = actionBarSupportsNewMethods(mActionBar); |
| 183 | if (runningMR11OrLater || !mController.isDrawerEnabled()) { |
| 184 | // We don't need the legacy view, just hide it |
| 185 | mLegacyTitleContainer.setVisibility(View.GONE); |
| 186 | mUseLegacyTitle = false; |
| 187 | } else { |
| 188 | mUseLegacyTitle = true; |
| 189 | // We need to show the legacy title/subtitle. Set the click listener |
| 190 | mLegacyTitleContainer.setOnClickListener(this); |
| 191 | |
| 192 | mLegacyTitle = (TextView)mLegacyTitleContainer.findViewById(R.id.legacy_title); |
| 193 | mLegacySubTitle = |
| 194 | (TextView)mLegacyTitleContainer.findViewById(R.id.legacy_subtitle); |
| 195 | } |
| 196 | } |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 197 | } |
| 198 | |
Andy Huang | 313ac13 | 2013-03-04 23:40:58 -0800 | [diff] [blame] | 199 | public void expandSearch() { |
| 200 | if (mSearch != null) { |
| 201 | mSearch.expandActionView(); |
| 202 | } |
| 203 | } |
| 204 | |
Vikram Aggarwal | b17cbc0 | 2012-04-06 15:41:46 -0700 | [diff] [blame] | 205 | /** |
Vikram Aggarwal | 0dda573 | 2012-04-06 11:20:16 -0700 | [diff] [blame] | 206 | * Close the search view if it is expanded. |
Vikram Aggarwal | b17cbc0 | 2012-04-06 15:41:46 -0700 | [diff] [blame] | 207 | */ |
| 208 | public void collapseSearch() { |
| 209 | if (mSearch != null) { |
| 210 | mSearch.collapseActionView(); |
| 211 | } |
| 212 | } |
| 213 | |
Mindy Pereira | edb135e | 2012-06-15 08:34:09 -0700 | [diff] [blame] | 214 | /** |
| 215 | * Get the search menu item. |
| 216 | */ |
| 217 | protected MenuItem getSearch() { |
| 218 | return mSearch; |
| 219 | } |
| 220 | |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 221 | public boolean onCreateOptionsMenu(Menu menu) { |
| 222 | // If the mode is valid, then set the initial menu |
Tony Mantler | bd09150 | 2013-09-16 13:59:47 -0700 | [diff] [blame^] | 223 | if (getMode() == ViewMode.UNKNOWN) { |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 224 | return false; |
| 225 | } |
Mindy Pereira | 9b62380 | 2012-03-07 17:15:49 -0800 | [diff] [blame] | 226 | mSearch = menu.findItem(R.id.search); |
| 227 | if (mSearch != null) { |
| 228 | mSearchWidget = (SearchView) mSearch.getActionView(); |
Vikram Aggarwal | 0dda573 | 2012-04-06 11:20:16 -0700 | [diff] [blame] | 229 | mSearch.setOnActionExpandListener(this); |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 230 | SearchManager searchManager = (SearchManager) mActivity.getActivityContext() |
| 231 | .getSystemService(Context.SEARCH_SERVICE); |
Mindy Pereira | fb17849 | 2012-03-07 18:56:42 -0800 | [diff] [blame] | 232 | if (searchManager != null && mSearchWidget != null) { |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 233 | SearchableInfo info = searchManager.getSearchableInfo(mActivity.getComponentName()); |
| 234 | mSearchWidget.setSearchableInfo(info); |
| 235 | mSearchWidget.setOnQueryTextListener(this); |
Vikram Aggarwal | 12a7c04 | 2012-03-28 16:48:08 -0700 | [diff] [blame] | 236 | mSearchWidget.setOnSuggestionListener(this); |
Vikram Aggarwal | 0dda573 | 2012-04-06 11:20:16 -0700 | [diff] [blame] | 237 | mSearchWidget.setIconifiedByDefault(true); |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | mHelpItem = menu.findItem(R.id.help_info_menu_item); |
Paul Westbrook | 517743e | 2012-03-22 10:40:46 -0700 | [diff] [blame] | 241 | mSendFeedbackItem = menu.findItem(R.id.feedback_menu_item); |
Marc Blank | edcab01 | 2012-04-04 12:42:38 -0700 | [diff] [blame] | 242 | mFolderSettingsItem = menu.findItem(R.id.folder_options); |
Scott Kennedy | 7ee089e | 2013-03-25 17:05:44 -0400 | [diff] [blame] | 243 | mEmptyTrashItem = menu.findItem(R.id.empty_trash); |
| 244 | mEmptySpamItem = menu.findItem(R.id.empty_spam); |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 245 | return true; |
| 246 | } |
| 247 | |
| 248 | public int getOptionsMenuId() { |
Tony Mantler | bd09150 | 2013-09-16 13:59:47 -0700 | [diff] [blame^] | 249 | switch (getMode()) { |
Tony Mantler | e29b97e | 2013-08-01 16:37:54 -0700 | [diff] [blame] | 250 | case ViewMode.UNKNOWN: |
| 251 | return R.menu.conversation_list_menu; |
| 252 | case ViewMode.CONVERSATION: |
| 253 | return R.menu.conversation_actions; |
| 254 | case ViewMode.CONVERSATION_LIST: |
| 255 | return R.menu.conversation_list_menu; |
| 256 | case ViewMode.SEARCH_RESULTS_LIST: |
| 257 | return R.menu.conversation_list_search_results_actions; |
| 258 | case ViewMode.SEARCH_RESULTS_CONVERSATION: |
| 259 | return R.menu.conversation_actions; |
| 260 | case ViewMode.WAITING_FOR_ACCOUNT_INITIALIZATION: |
| 261 | return R.menu.wait_mode_actions; |
| 262 | } |
| 263 | LogUtils.wtf(LOG_TAG, "Menu requested for unknown view mode"); |
| 264 | return R.menu.conversation_list_menu; |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 265 | } |
| 266 | |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 267 | public void initialize(ControllableActivity activity, ActivityController callback, |
Vikram Aggarwal | dac89fa | 2013-03-05 16:43:09 -0800 | [diff] [blame] | 268 | ActionBar actionBar) { |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 269 | mActionBar = actionBar; |
Vikram Aggarwal | a799795 | 2012-03-09 10:30:13 -0800 | [diff] [blame] | 270 | mController = callback; |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 271 | mActivity = activity; |
Paul Westbrook | 8c887ef | 2013-04-24 00:29:28 -0700 | [diff] [blame] | 272 | initializeTitleViews(); |
| 273 | |
Vikram Aggarwal | 50ff0e5 | 2013-03-14 13:58:02 -0700 | [diff] [blame] | 274 | mFolderObserver = new FolderObserver() { |
| 275 | @Override |
| 276 | public void onChanged(Folder newFolder) { |
| 277 | onFolderUpdated(newFolder); |
| 278 | } |
| 279 | }; |
Vikram Aggarwal | 26b0bfd | 2013-03-29 13:05:08 -0700 | [diff] [blame] | 280 | // Return values are purposely discarded. Initialization happens quite early, and we don't |
| 281 | // have a valid folder, or a valid list of accounts. |
Vikram Aggarwal | 50ff0e5 | 2013-03-14 13:58:02 -0700 | [diff] [blame] | 282 | mFolderObserver.initialize(mController); |
Scott Banachowski | 53a615d | 2012-12-19 15:31:20 -0800 | [diff] [blame] | 283 | updateAccount(mAccountObserver.initialize(activity.getAccountController())); |
| 284 | } |
| 285 | |
| 286 | private void updateAccount(Account account) { |
Vikram Aggarwal | f18b117 | 2013-04-17 12:36:23 -0700 | [diff] [blame] | 287 | final boolean accountChanged = mAccount == null || !mAccount.uri.equals(account.uri); |
Scott Banachowski | 53a615d | 2012-12-19 15:31:20 -0800 | [diff] [blame] | 288 | mAccount = account; |
Vikram Aggarwal | f18b117 | 2013-04-17 12:36:23 -0700 | [diff] [blame] | 289 | if (mAccount != null && accountChanged) { |
Vikram Aggarwal | bc462ca | 2013-03-15 10:41:03 -0700 | [diff] [blame] | 290 | final ContentResolver resolver = mActivity.getActivityContext().getContentResolver(); |
| 291 | final Bundle bundle = new Bundle(1); |
Scott Banachowski | 53a615d | 2012-12-19 15:31:20 -0800 | [diff] [blame] | 292 | bundle.putParcelable(UIProvider.SetCurrentAccountColumns.ACCOUNT, account); |
Vikram Aggarwal | a51ef9f | 2013-04-02 13:25:35 -0700 | [diff] [blame] | 293 | final UpdateProvider updater = new UpdateProvider(mAccount.uri, resolver); |
| 294 | updater.execute(bundle); |
Vikram Aggarwal | f18b117 | 2013-04-17 12:36:23 -0700 | [diff] [blame] | 295 | setFolderAndAccount(false /* folderChanged */); |
Scott Banachowski | 53a615d | 2012-12-19 15:31:20 -0800 | [diff] [blame] | 296 | } |
Vikram Aggarwal | abd24d8 | 2012-04-26 13:23:14 -0700 | [diff] [blame] | 297 | } |
Andy Huang | e303b9b | 2012-04-19 16:31:09 -0700 | [diff] [blame] | 298 | |
Vikram Aggarwal | abd24d8 | 2012-04-26 13:23:14 -0700 | [diff] [blame] | 299 | /** |
Vikram Aggarwal | 75d1bb1 | 2013-04-12 17:17:00 -0700 | [diff] [blame] | 300 | * Called by the owner of the ActionBar to change the current folder. |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 301 | */ |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 302 | public void setFolder(Folder folder) { |
Marc Blank | edcab01 | 2012-04-04 12:42:38 -0700 | [diff] [blame] | 303 | mFolder = folder; |
Vikram Aggarwal | 75d1bb1 | 2013-04-12 17:17:00 -0700 | [diff] [blame] | 304 | setFolderAndAccount(true); |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 305 | } |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 306 | |
Mindy Pereira | 641de65 | 2012-08-02 15:21:50 -0700 | [diff] [blame] | 307 | public void onDestroy() { |
| 308 | if (mFolderObserver != null) { |
Vikram Aggarwal | 50ff0e5 | 2013-03-14 13:58:02 -0700 | [diff] [blame] | 309 | mFolderObserver.unregisterAndDestroy(); |
Mindy Pereira | 641de65 | 2012-08-02 15:21:50 -0700 | [diff] [blame] | 310 | mFolderObserver = null; |
| 311 | } |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 312 | mAccountObserver.unregisterAndDestroy(); |
Vikram Aggarwal | 75d1bb1 | 2013-04-12 17:17:00 -0700 | [diff] [blame] | 313 | mHandler.removeMessages(SubtitleHandler.EMAIL); |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 314 | } |
| 315 | |
Vikram Aggarwal | a799795 | 2012-03-09 10:30:13 -0800 | [diff] [blame] | 316 | @Override |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 317 | public void onViewModeChanged(int newMode) { |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 318 | mActivity.invalidateOptionsMenu(); |
Vikram Aggarwal | 75d1bb1 | 2013-04-12 17:17:00 -0700 | [diff] [blame] | 319 | mHandler.removeMessages(SubtitleHandler.EMAIL); |
Vikram Aggarwal | 1a3f3fc | 2012-07-18 14:49:08 -0700 | [diff] [blame] | 320 | // Check if we are either on a phone, or in Conversation mode on tablet. For these, the |
| 321 | // recent folders is enabled. |
Tony Mantler | bd09150 | 2013-09-16 13:59:47 -0700 | [diff] [blame^] | 322 | switch (getMode()) { |
Andy Huang | d736a38 | 2012-08-29 13:08:58 -0700 | [diff] [blame] | 323 | case ViewMode.UNKNOWN: |
Andy Huang | d736a38 | 2012-08-29 13:08:58 -0700 | [diff] [blame] | 324 | break; |
| 325 | case ViewMode.CONVERSATION_LIST: |
| 326 | showNavList(); |
| 327 | break; |
Vikram Aggarwal | ad8b99b | 2013-04-07 16:37:28 -0700 | [diff] [blame] | 328 | case ViewMode.SEARCH_RESULTS_CONVERSATION: |
Vikram Aggarwal | e875b66 | 2013-04-10 15:56:46 -0700 | [diff] [blame] | 329 | mActionBar.setDisplayHomeAsUpEnabled(true); |
| 330 | setEmptyMode(); |
| 331 | break; |
Andy Huang | d736a38 | 2012-08-29 13:08:58 -0700 | [diff] [blame] | 332 | case ViewMode.CONVERSATION: |
Andrew Sapperstein | 6c570db | 2013-08-06 17:21:36 -0700 | [diff] [blame] | 333 | case ViewMode.AD: |
Vikram Aggarwal | 49499e6 | 2013-02-08 16:39:08 -0800 | [diff] [blame] | 334 | closeSearchField(); |
Andy Huang | d736a38 | 2012-08-29 13:08:58 -0700 | [diff] [blame] | 335 | mActionBar.setDisplayHomeAsUpEnabled(true); |
Vikram Aggarwal | ad8b99b | 2013-04-07 16:37:28 -0700 | [diff] [blame] | 336 | setEmptyMode(); |
Andy Huang | d736a38 | 2012-08-29 13:08:58 -0700 | [diff] [blame] | 337 | break; |
Andy Huang | d736a38 | 2012-08-29 13:08:58 -0700 | [diff] [blame] | 338 | case ViewMode.WAITING_FOR_ACCOUNT_INITIALIZATION: |
| 339 | // We want the user to be able to switch accounts while waiting for an account |
| 340 | // to sync. |
| 341 | showNavList(); |
| 342 | break; |
| 343 | } |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 344 | } |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 345 | |
Vikram Aggarwal | 49499e6 | 2013-02-08 16:39:08 -0800 | [diff] [blame] | 346 | /** |
| 347 | * Close the search query entry field to avoid keyboard events, and to restore the actionbar |
| 348 | * to non-search mode. |
| 349 | */ |
Vikram Aggarwal | dac89fa | 2013-03-05 16:43:09 -0800 | [diff] [blame] | 350 | private void closeSearchField() { |
Vikram Aggarwal | 49499e6 | 2013-02-08 16:39:08 -0800 | [diff] [blame] | 351 | if (mSearch == null) { |
| 352 | return; |
| 353 | } |
| 354 | mSearch.collapseActionView(); |
| 355 | } |
| 356 | |
Mindy Pereira | edb135e | 2012-06-15 08:34:09 -0700 | [diff] [blame] | 357 | protected int getMode() { |
Tony Mantler | bd09150 | 2013-09-16 13:59:47 -0700 | [diff] [blame^] | 358 | if (mViewModeController != null) { |
| 359 | return mViewModeController.getMode(); |
| 360 | } else { |
| 361 | return ViewMode.UNKNOWN; |
| 362 | } |
Mindy Pereira | edb135e | 2012-06-15 08:34:09 -0700 | [diff] [blame] | 363 | } |
| 364 | |
Mindy Pereira | b849dfb | 2012-03-07 18:13:15 -0800 | [diff] [blame] | 365 | public boolean onPrepareOptionsMenu(Menu menu) { |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 366 | // We start out with every option enabled. Based on the current view, we disable actions |
| 367 | // that are possible. |
Vikram Aggarwal | 95b28ab | 2012-04-26 15:53:09 -0700 | [diff] [blame] | 368 | LogUtils.d(LOG_TAG, "ActionBarView.onPrepareOptionsMenu()."); |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 369 | |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 370 | if (mHelpItem != null) { |
| 371 | mHelpItem.setVisible(mAccount != null |
| 372 | && mAccount.supportsCapability(AccountCapabilities.HELP_CONTENT)); |
| 373 | } |
Paul Westbrook | 517743e | 2012-03-22 10:40:46 -0700 | [diff] [blame] | 374 | if (mSendFeedbackItem != null) { |
| 375 | mSendFeedbackItem.setVisible(mAccount != null |
| 376 | && mAccount.supportsCapability(AccountCapabilities.SEND_FEEDBACK)); |
| 377 | } |
Tony Mantler | b3281fb | 2013-08-20 14:30:29 -0700 | [diff] [blame] | 378 | if (mController.shouldHideMenuItems()) { |
| 379 | // Shortcut: hide all remaining menu items if the drawer is shown |
| 380 | final int size = menu.size(); |
| 381 | |
| 382 | for (int i = 0; i < size; i++) { |
| 383 | final MenuItem item = menu.getItem(i); |
| 384 | final int id = item.getItemId(); |
| 385 | if (id != R.id.settings |
| 386 | && id != R.id.feedback_menu_item |
| 387 | && id != R.id.help_info_menu_item) { |
| 388 | item.setVisible(false); |
| 389 | } |
| 390 | } |
| 391 | return false; |
| 392 | } |
Marc Blank | edcab01 | 2012-04-04 12:42:38 -0700 | [diff] [blame] | 393 | if (mFolderSettingsItem != null) { |
| 394 | mFolderSettingsItem.setVisible(mFolder != null |
| 395 | && mFolder.supportsCapability(FolderCapabilities.SUPPORTS_SETTINGS)); |
| 396 | } |
Scott Kennedy | 7ee089e | 2013-03-25 17:05:44 -0400 | [diff] [blame] | 397 | if (mEmptyTrashItem != null) { |
| 398 | mEmptyTrashItem.setVisible(mAccount != null && mFolder != null |
| 399 | && mAccount.supportsCapability(AccountCapabilities.EMPTY_TRASH) |
Andrew Sapperstein | ed5b52d | 2013-04-30 13:40:18 -0700 | [diff] [blame] | 400 | && mFolder.isTrash() && mFolder.totalCount > 0); |
Scott Kennedy | 7ee089e | 2013-03-25 17:05:44 -0400 | [diff] [blame] | 401 | } |
| 402 | if (mEmptySpamItem != null) { |
| 403 | mEmptySpamItem.setVisible(mAccount != null && mFolder != null |
| 404 | && mAccount.supportsCapability(AccountCapabilities.EMPTY_SPAM) |
Andrew Sapperstein | ed5b52d | 2013-04-30 13:40:18 -0700 | [diff] [blame] | 405 | && mFolder.isType(FolderType.SPAM) && mFolder.totalCount > 0); |
Scott Kennedy | 7ee089e | 2013-03-25 17:05:44 -0400 | [diff] [blame] | 406 | } |
Mindy Pereira | 62c7179 | 2012-07-18 09:14:29 -0700 | [diff] [blame] | 407 | |
Tony Mantler | bd09150 | 2013-09-16 13:59:47 -0700 | [diff] [blame^] | 408 | switch (getMode()) { |
Yorke Lee | f807ba7 | 2012-09-20 17:18:05 -0700 | [diff] [blame] | 409 | case ViewMode.CONVERSATION: |
| 410 | case ViewMode.SEARCH_RESULTS_CONVERSATION: |
| 411 | // We update the ActionBar options when we are entering conversation view because |
| 412 | // waiting for the AbstractConversationViewFragment to do it causes duplicate icons |
| 413 | // to show up during the time between the conversation is selected and the fragment |
| 414 | // is added. |
| 415 | setConversationModeOptions(menu); |
Scott Kennedy | b905d08 | 2013-05-07 16:44:34 -0700 | [diff] [blame] | 416 | // We want to use the user's preferred menu items here |
| 417 | final Resources resources = getResources(); |
| 418 | final int maxItems = resources.getInteger(R.integer.actionbar_max_items); |
| 419 | final int hiddenItems = resources.getInteger( |
| 420 | R.integer.actionbar_hidden_non_cab_items_no_physical_button); |
| 421 | final int totalItems = maxItems |
| 422 | - (ViewConfiguration.get(getContext()).hasPermanentMenuKey() |
| 423 | ? 0 : hiddenItems); |
| 424 | reorderMenu(getContext(), mAccount, menu, totalItems); |
Yorke Lee | f807ba7 | 2012-09-20 17:18:05 -0700 | [diff] [blame] | 425 | break; |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 426 | case ViewMode.CONVERSATION_LIST: |
Vikram Aggarwal | ad8b99b | 2013-04-07 16:37:28 -0700 | [diff] [blame] | 427 | // Show compose and search based on the account |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 428 | // The only option that needs to be disabled is search |
Mindy Pereira | 863e441 | 2012-03-23 12:21:38 -0700 | [diff] [blame] | 429 | Utils.setMenuItemVisibility(menu, R.id.search, |
Mindy Pereira | 9b62380 | 2012-03-07 17:15:49 -0800 | [diff] [blame] | 430 | mAccount.supportsCapability(AccountCapabilities.FOLDER_SERVER_SEARCH)); |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 431 | break; |
Vikram Aggarwal | ad8b99b | 2013-04-07 16:37:28 -0700 | [diff] [blame] | 432 | case ViewMode.SEARCH_RESULTS_LIST: |
Vikram Aggarwal | 4bfad14 | 2013-05-07 17:12:46 -0700 | [diff] [blame] | 433 | // Hide compose and search |
| 434 | Utils.setMenuItemVisibility(menu, R.id.compose, false); |
Vikram Aggarwal | ad8b99b | 2013-04-07 16:37:28 -0700 | [diff] [blame] | 435 | Utils.setMenuItemVisibility(menu, R.id.search, false); |
| 436 | break; |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 437 | } |
Scott Kennedy | a85831d | 2013-04-12 15:54:22 -0700 | [diff] [blame] | 438 | |
| 439 | return false; |
| 440 | } |
| 441 | |
Scott Kennedy | 26f2b30 | 2013-04-22 17:47:17 -0700 | [diff] [blame] | 442 | /** |
| 443 | * Reorders the specified {@link Menu}, taking into account the user's Archive/Delete |
| 444 | * preference. |
| 445 | */ |
Scott Kennedy | b905d08 | 2013-05-07 16:44:34 -0700 | [diff] [blame] | 446 | public static void reorderMenu(final Context context, final Account account, final Menu menu, |
| 447 | final int maxItems) { |
Scott Kennedy | d4796d7 | 2013-05-03 19:49:32 -0700 | [diff] [blame] | 448 | final String removalAction = MailPrefs.get(context).getRemovalAction( |
| 449 | account.supportsCapability(AccountCapabilities.ARCHIVE)); |
Andrew Sapperstein | 43a1cc1 | 2013-05-03 10:21:02 -0700 | [diff] [blame] | 450 | final boolean showArchive = MailPrefs.RemovalActions.ARCHIVE.equals(removalAction) || |
| 451 | MailPrefs.RemovalActions.ARCHIVE_AND_DELETE.equals(removalAction); |
| 452 | final boolean showDelete = MailPrefs.RemovalActions.DELETE.equals(removalAction) || |
| 453 | MailPrefs.RemovalActions.ARCHIVE_AND_DELETE.equals(removalAction); |
Scott Kennedy | a85831d | 2013-04-12 15:54:22 -0700 | [diff] [blame] | 454 | |
Scott Kennedy | b905d08 | 2013-05-07 16:44:34 -0700 | [diff] [blame] | 455 | // Do a first pass to extract necessary information on what is safe to display |
Scott Kennedy | ccd3ce2 | 2013-04-26 10:34:29 -0700 | [diff] [blame] | 456 | boolean archiveVisibleEnabled = false; |
| 457 | boolean deleteVisibleEnabled = false; |
| 458 | for (int i = 0; i < menu.size(); i++) { |
| 459 | final MenuItem menuItem = menu.getItem(i); |
| 460 | final int itemId = menuItem.getItemId(); |
| 461 | final boolean visible = menuItem.isVisible(); |
| 462 | final boolean enabled = menuItem.isEnabled(); |
| 463 | |
| 464 | if (itemId == R.id.archive || itemId == R.id.remove_folder) { |
| 465 | archiveVisibleEnabled |= (visible & enabled); |
| 466 | } else if (itemId == R.id.delete || itemId == R.id.discard_drafts) { |
| 467 | deleteVisibleEnabled |= (visible & enabled); |
| 468 | } |
| 469 | } |
| 470 | |
Scott Kennedy | b905d08 | 2013-05-07 16:44:34 -0700 | [diff] [blame] | 471 | int actionItems = 0; |
| 472 | |
Scott Kennedy | a85831d | 2013-04-12 15:54:22 -0700 | [diff] [blame] | 473 | for (int i = 0; i < menu.size(); i++) { |
| 474 | final MenuItem menuItem = menu.getItem(i); |
Scott Kennedy | 26f2b30 | 2013-04-22 17:47:17 -0700 | [diff] [blame] | 475 | final int itemId = menuItem.getItemId(); |
Scott Kennedy | a85831d | 2013-04-12 15:54:22 -0700 | [diff] [blame] | 476 | |
Scott Kennedy | b905d08 | 2013-05-07 16:44:34 -0700 | [diff] [blame] | 477 | // We only want to promote it if it's visible and has an icon |
| 478 | if (menuItem.isVisible() && menuItem.getIcon() != null) { |
Scott Kennedy | 2b9d80e | 2013-07-30 23:03:45 -0700 | [diff] [blame] | 479 | if (itemId == R.id.archive || itemId == R.id.remove_folder) { |
| 480 | /* |
| 481 | * If this is disabled, and we want to show both archive and delete, we will |
| 482 | * hide archive (rather than showing it disabled), and take up one of our |
| 483 | * spaces. If we only want to show archive, we'll hide it, but not take up |
| 484 | * a space. |
| 485 | */ |
| 486 | if (!menuItem.isEnabled() && showArchive) { |
| 487 | menuItem.setVisible(false); |
Andrew Sapperstein | 43a1cc1 | 2013-05-03 10:21:02 -0700 | [diff] [blame] | 488 | |
Scott Kennedy | 2b9d80e | 2013-07-30 23:03:45 -0700 | [diff] [blame] | 489 | if (showDelete) { |
| 490 | actionItems++; |
Scott Kennedy | b905d08 | 2013-05-07 16:44:34 -0700 | [diff] [blame] | 491 | } |
Scott Kennedy | 2b9d80e | 2013-07-30 23:03:45 -0700 | [diff] [blame] | 492 | } else { |
Scott Kennedy | b905d08 | 2013-05-07 16:44:34 -0700 | [diff] [blame] | 493 | /* |
| 494 | * We show this if the following are all true: |
| 495 | * 1. The user wants to display archive, or delete is not visible |
| 496 | * 2. We have room for it |
| 497 | */ |
| 498 | if ((showArchive || !deleteVisibleEnabled) && actionItems < maxItems) { |
| 499 | menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); |
| 500 | actionItems++; |
| 501 | } |
Scott Kennedy | 2b9d80e | 2013-07-30 23:03:45 -0700 | [diff] [blame] | 502 | } |
| 503 | } else if (itemId == R.id.delete || itemId == R.id.discard_drafts) { |
| 504 | /* |
| 505 | * We show this if the following are all true: |
| 506 | * 1. The user wants to display delete, or archive is not visible |
| 507 | * 2. We have room for it |
| 508 | */ |
| 509 | if ((showDelete || !archiveVisibleEnabled) && actionItems < maxItems) { |
| 510 | menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); |
Scott Kennedy | b905d08 | 2013-05-07 16:44:34 -0700 | [diff] [blame] | 511 | actionItems++; |
Scott Kennedy | 2b9d80e | 2013-07-30 23:03:45 -0700 | [diff] [blame] | 512 | } |
Andrew Sapperstein | 6c570db | 2013-08-06 17:21:36 -0700 | [diff] [blame] | 513 | } else if (itemId == R.id.change_folders) { |
Scott Kennedy | 2b9d80e | 2013-07-30 23:03:45 -0700 | [diff] [blame] | 514 | final boolean showChangeFolder = account |
| 515 | .supportsCapability(AccountCapabilities.MULTIPLE_FOLDERS_PER_CONV); |
| 516 | menuItem.setVisible(showChangeFolder); |
| 517 | |
| 518 | if (showChangeFolder && actionItems < maxItems) { |
| 519 | menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); |
| 520 | actionItems++; |
| 521 | } |
| 522 | } else if (itemId == R.id.search) { |
| 523 | menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS |
| 524 | | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW); |
| 525 | actionItems++; |
| 526 | } else { |
| 527 | if (actionItems < maxItems) { |
| 528 | menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); |
| 529 | actionItems++; |
| 530 | } |
Scott Kennedy | b905d08 | 2013-05-07 16:44:34 -0700 | [diff] [blame] | 531 | } |
Scott Kennedy | ef91f8b | 2013-05-03 19:42:53 -0700 | [diff] [blame] | 532 | } |
Scott Kennedy | a85831d | 2013-04-12 15:54:22 -0700 | [diff] [blame] | 533 | } |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 534 | } |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 535 | |
Vikram Aggarwal | abd24d8 | 2012-04-26 13:23:14 -0700 | [diff] [blame] | 536 | /** |
Vikram Aggarwal | ad8b99b | 2013-04-07 16:37:28 -0700 | [diff] [blame] | 537 | * Put the ActionBar in List navigation mode. |
Vikram Aggarwal | abd24d8 | 2012-04-26 13:23:14 -0700 | [diff] [blame] | 538 | */ |
Mindy Pereira | 72a9f39 | 2012-03-21 09:41:09 -0700 | [diff] [blame] | 539 | private void showNavList() { |
Paul Westbrook | 8c887ef | 2013-04-24 00:29:28 -0700 | [diff] [blame] | 540 | setTitleModeFlags(getActionBarTitleModeFlag()); |
Vikram Aggarwal | b642b31 | 2013-04-24 18:41:40 -0700 | [diff] [blame] | 541 | setFolderAndAccount(false); |
Mindy Pereira | 72a9f39 | 2012-03-21 09:41:09 -0700 | [diff] [blame] | 542 | } |
| 543 | |
Paul Westbrook | 8c887ef | 2013-04-24 00:29:28 -0700 | [diff] [blame] | 544 | private void setSubtitle(CharSequence subtitle) { |
Andy Huang | c499d71 | 2013-06-12 17:37:57 -0700 | [diff] [blame] | 545 | if (!TextUtils.equals(subtitle, mActionBar.getSubtitle())) { |
| 546 | mActionBar.setSubtitle(subtitle); |
| 547 | } |
Paul Westbrook | 8c887ef | 2013-04-24 00:29:28 -0700 | [diff] [blame] | 548 | if (mLegacySubTitle != null) { |
| 549 | mLegacySubTitle.setText(subtitle); |
| 550 | } |
| 551 | } |
| 552 | |
Paul Westbrook | 8c887ef | 2013-04-24 00:29:28 -0700 | [diff] [blame] | 553 | private void setTitle(CharSequence title) { |
Andy Huang | c499d71 | 2013-06-12 17:37:57 -0700 | [diff] [blame] | 554 | if (!TextUtils.equals(title, mActionBar.getTitle())) { |
| 555 | mActionBar.setTitle(title); |
| 556 | } |
Paul Westbrook | 8c887ef | 2013-04-24 00:29:28 -0700 | [diff] [blame] | 557 | if (mLegacyTitle != null) { |
| 558 | mLegacyTitle.setText(title); |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | private int getActionBarTitleModeFlag() { |
| 563 | return mUseLegacyTitle ? ActionBar.DISPLAY_SHOW_CUSTOM : ActionBar.DISPLAY_SHOW_TITLE; |
mindyp | 6fb1b62 | 2012-09-05 09:27:08 -0700 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | /** |
Vikram Aggarwal | fd7eaba | 2013-04-11 12:31:46 -0700 | [diff] [blame] | 567 | * Set the actionbar mode to empty: no title, no subtitle, no custom view. |
mindyp | 6fb1b62 | 2012-09-05 09:27:08 -0700 | [diff] [blame] | 568 | */ |
| 569 | protected void setEmptyMode() { |
Vikram Aggarwal | fd7eaba | 2013-04-11 12:31:46 -0700 | [diff] [blame] | 570 | // Disable title/subtitle and the custom view by setting the bitmask to all off. |
| 571 | setTitleModeFlags(0); |
Vikram Aggarwal | abd24d8 | 2012-04-26 13:23:14 -0700 | [diff] [blame] | 572 | } |
| 573 | |
Vikram Aggarwal | ad8b99b | 2013-04-07 16:37:28 -0700 | [diff] [blame] | 574 | /** |
| 575 | * Removes the back button from being shown |
| 576 | */ |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 577 | public void removeBackButton() { |
| 578 | if (mActionBar == null) { |
| 579 | return; |
| 580 | } |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame] | 581 | // Remove the back button but continue showing an icon. |
| 582 | final int mask = ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME; |
| 583 | mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME, mask); |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 584 | mActivity.getActionBar().setHomeButtonEnabled(false); |
| 585 | } |
Mindy Pereira | bc57bf1 | 2012-02-29 14:39:09 -0800 | [diff] [blame] | 586 | |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 587 | public void setBackButton() { |
Andy Huang | 12b3ee4 | 2013-04-24 22:49:43 -0700 | [diff] [blame] | 588 | if (mActionBar == null) { |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 589 | return; |
| 590 | } |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame] | 591 | // Show home as up, and show an icon. |
| 592 | final int mask = ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME; |
| 593 | mActionBar.setDisplayOptions(mask, mask); |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 594 | mActivity.getActionBar().setHomeButtonEnabled(true); |
| 595 | } |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 596 | |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 597 | @Override |
| 598 | public boolean onQueryTextSubmit(String query) { |
Mindy Pereira | 4ce5994 | 2012-03-08 09:57:35 -0800 | [diff] [blame] | 599 | if (mSearch != null) { |
| 600 | mSearch.collapseActionView(); |
| 601 | mSearchWidget.setQuery("", false); |
| 602 | } |
Andy Huang | 313ac13 | 2013-03-04 23:40:58 -0800 | [diff] [blame] | 603 | mController.executeSearch(query.trim()); |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 604 | return true; |
| 605 | } |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 606 | |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 607 | @Override |
| 608 | public boolean onQueryTextChange(String newText) { |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 609 | return false; |
| 610 | } |
Mindy Pereira | 9b62380 | 2012-03-07 17:15:49 -0800 | [diff] [blame] | 611 | |
Vikram Aggarwal | 12a7c04 | 2012-03-28 16:48:08 -0700 | [diff] [blame] | 612 | // Next two methods are called when search suggestions are clicked. |
| 613 | @Override |
| 614 | public boolean onSuggestionSelect(int position) { |
| 615 | return onSuggestionClick(position); |
| 616 | } |
| 617 | |
| 618 | @Override |
| 619 | public boolean onSuggestionClick(int position) { |
| 620 | final Cursor c = mSearchWidget.getSuggestionsAdapter().getCursor(); |
| 621 | final boolean haveValidQuery = (c != null) && c.moveToPosition(position); |
| 622 | if (!haveValidQuery) { |
| 623 | LogUtils.d(LOG_TAG, "onSuggestionClick: Couldn't get a search query"); |
Mindy Pereira | f8fcb80 | 2012-06-25 11:43:06 -0700 | [diff] [blame] | 624 | // We haven't handled this query, but the default behavior will |
| 625 | // leave EXTRA_ACCOUNT un-populated, leading to a crash. So claim |
| 626 | // that we have handled the event. |
Vikram Aggarwal | 12a7c04 | 2012-03-28 16:48:08 -0700 | [diff] [blame] | 627 | return true; |
| 628 | } |
Mindy Pereira | b466bcf | 2012-06-14 15:52:26 -0700 | [diff] [blame] | 629 | collapseSearch(); |
Mindy Pereira | 1e180dd | 2012-08-16 10:24:10 -0700 | [diff] [blame] | 630 | // what is in the text field |
Mindy Pereira | ca97b31 | 2012-06-25 14:00:20 -0700 | [diff] [blame] | 631 | String queryText = mSearchWidget.getQuery().toString(); |
Mindy Pereira | 1e180dd | 2012-08-16 10:24:10 -0700 | [diff] [blame] | 632 | // What the suggested query is |
Mindy Pereira | f8fcb80 | 2012-06-25 11:43:06 -0700 | [diff] [blame] | 633 | String query = c.getString(c.getColumnIndex(SearchManager.SUGGEST_COLUMN_QUERY)); |
mindyp | 0fc8fa5 | 2012-09-04 10:43:23 -0700 | [diff] [blame] | 634 | // If the text the user typed in is a prefix of what is in the search |
| 635 | // widget suggestion query, just take the search widget suggestion |
| 636 | // query. Otherwise, it is a suffix and we want to remove matching |
| 637 | // prefix portions. |
| 638 | if (!TextUtils.isEmpty(queryText) && query.indexOf(queryText) != 0) { |
Mindy Pereira | 5e1403c | 2012-07-18 13:22:11 -0700 | [diff] [blame] | 639 | final int queryTokenIndex = queryText |
| 640 | .lastIndexOf(SearchRecentSuggestionsProvider.QUERY_TOKEN_SEPARATOR); |
| 641 | if (queryTokenIndex > -1) { |
| 642 | queryText = queryText.substring(0, queryTokenIndex); |
Mindy Pereira | ca97b31 | 2012-06-25 14:00:20 -0700 | [diff] [blame] | 643 | } |
| 644 | // Since we auto-complete on each token in a query, if the query the |
| 645 | // user typed up until the last token is a substring of the |
| 646 | // suggestion they click, make sure we don't double include the |
| 647 | // query text. For example: |
| 648 | // user types john, that matches john palo alto |
| 649 | // User types john p, that matches john john palo alto |
| 650 | // Remove the first john |
Mindy Pereira | 5e1403c | 2012-07-18 13:22:11 -0700 | [diff] [blame] | 651 | // Only do this if we have multiple query tokens. |
| 652 | if (queryTokenIndex > -1 && !TextUtils.isEmpty(query) && query.contains(queryText) |
Mindy Pereira | ca97b31 | 2012-06-25 14:00:20 -0700 | [diff] [blame] | 653 | && queryText.length() < query.length()) { |
| 654 | int start = query.indexOf(queryText); |
| 655 | query = query.substring(0, start) + query.substring(start + queryText.length()); |
| 656 | } |
| 657 | } |
Andy Huang | 313ac13 | 2013-03-04 23:40:58 -0800 | [diff] [blame] | 658 | mController.executeSearch(query.trim()); |
Vikram Aggarwal | 12a7c04 | 2012-03-28 16:48:08 -0700 | [diff] [blame] | 659 | return true; |
| 660 | } |
Mindy Pereira | a46a57f | 2012-04-03 17:19:31 -0700 | [diff] [blame] | 661 | |
Vikram Aggarwal | f642b81 | 2013-03-15 16:30:21 -0700 | [diff] [blame] | 662 | /** |
| 663 | * 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] | 664 | * account {@link #mAccount} shown in the actionbar. Also updates the actionbar subtitle to |
| 665 | * momentarily display the unread count if it has changed. |
Vikram Aggarwal | f18b117 | 2013-04-17 12:36:23 -0700 | [diff] [blame] | 666 | * @param folderChanged true if folder changed in terms of URI |
Vikram Aggarwal | f642b81 | 2013-03-15 16:30:21 -0700 | [diff] [blame] | 667 | */ |
Vikram Aggarwal | f18b117 | 2013-04-17 12:36:23 -0700 | [diff] [blame] | 668 | private void setFolderAndAccount(final boolean folderChanged) { |
Vikram Aggarwal | 5fce992 | 2013-04-01 21:17:42 -0700 | [diff] [blame] | 669 | // Very little can be done if the actionbar or activity is null. |
| 670 | if (mActionBar == null || mActivity == null) { |
| 671 | return; |
| 672 | } |
Tony Mantler | bd09150 | 2013-09-16 13:59:47 -0700 | [diff] [blame^] | 673 | if (ViewMode.isWaitingForSync(getMode())) { |
Vikram Aggarwal | 75d1bb1 | 2013-04-12 17:17:00 -0700 | [diff] [blame] | 674 | // Account is not synced: clear title and update the subtitle. |
Paul Westbrook | 8c887ef | 2013-04-24 00:29:28 -0700 | [diff] [blame] | 675 | setTitle(""); |
Vikram Aggarwal | 9dcd483 | 2013-05-09 16:08:59 -0700 | [diff] [blame] | 676 | removeUnreadCount(true); |
Vikram Aggarwal | f642b81 | 2013-03-15 16:30:21 -0700 | [diff] [blame] | 677 | return; |
| 678 | } |
Vikram Aggarwal | 75d1bb1 | 2013-04-12 17:17:00 -0700 | [diff] [blame] | 679 | // 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^] | 680 | final boolean isShowingFolder = mIsOnTablet || ViewMode.isListMode(getMode()); |
Vikram Aggarwal | 75d1bb1 | 2013-04-12 17:17:00 -0700 | [diff] [blame] | 681 | if (!isShowingFolder) { |
Paul Westbrook | 9102495 | 2013-04-21 15:59:06 -0700 | [diff] [blame] | 682 | // It isn't necessary to set the title in this case, as the title view will |
| 683 | // be hidden |
Vikram Aggarwal | 75d1bb1 | 2013-04-12 17:17:00 -0700 | [diff] [blame] | 684 | return; |
Vikram Aggarwal | f642b81 | 2013-03-15 16:30:21 -0700 | [diff] [blame] | 685 | } |
Vikram Aggarwal | 26b0bfd | 2013-03-29 13:05:08 -0700 | [diff] [blame] | 686 | if (mFolder == null) { |
Paul Westbrook | 9102495 | 2013-04-21 15:59:06 -0700 | [diff] [blame] | 687 | // Clear the action bar title. We don't want the app name to be shown while |
| 688 | // waiting for the folder query to finish |
Paul Westbrook | 8c887ef | 2013-04-24 00:29:28 -0700 | [diff] [blame] | 689 | setTitle(""); |
Vikram Aggarwal | 26b0bfd | 2013-03-29 13:05:08 -0700 | [diff] [blame] | 690 | return; |
| 691 | } |
Paul Westbrook | 8c887ef | 2013-04-24 00:29:28 -0700 | [diff] [blame] | 692 | setTitle(mFolder.name); |
Vikram Aggarwal | 75d1bb1 | 2013-04-12 17:17:00 -0700 | [diff] [blame] | 693 | |
Vikram Aggarwal | f18b117 | 2013-04-17 12:36:23 -0700 | [diff] [blame] | 694 | final int folderUnreadCount = mFolder.isUnreadCountHidden() ? 0 : mFolder.unreadCount; |
Vikram Aggarwal | 75d1bb1 | 2013-04-12 17:17:00 -0700 | [diff] [blame] | 695 | // The user shouldn't see "999+ unread messages", and then a short while later: "999+ |
| 696 | // unread messages". So we set our unread count just past the limit. This way we can |
| 697 | // change the subtitle the first time around but not for subsequent changes as far as the |
| 698 | // unread count remains over the limit. |
Vikram Aggarwal | f18b117 | 2013-04-17 12:36:23 -0700 | [diff] [blame] | 699 | final int toDisplay = (folderUnreadCount > UNREAD_LIMIT) |
| 700 | ? (UNREAD_LIMIT + 1) : folderUnreadCount; |
Vikram Aggarwal | c590ba8 | 2013-05-02 11:28:05 -0700 | [diff] [blame] | 701 | if ((mUnreadCount != toDisplay || folderChanged) && toDisplay != 0) { |
Vikram Aggarwal | 9dcd483 | 2013-05-09 16:08:59 -0700 | [diff] [blame] | 702 | setSubtitle(Utils.getUnreadMessageString(mActivity.getApplicationContext(), toDisplay)); |
Vikram Aggarwal | 26b0bfd | 2013-03-29 13:05:08 -0700 | [diff] [blame] | 703 | } |
Vikram Aggarwal | 055b24b | 2013-05-10 15:14:20 -0700 | [diff] [blame] | 704 | // Schedule a removal of unread count for the future, if there isn't one already. If the |
| 705 | // unread count dropped to zero, remove it and show the account name right away. |
| 706 | removeUnreadCount(toDisplay == 0); |
Vikram Aggarwal | 75d1bb1 | 2013-04-12 17:17:00 -0700 | [diff] [blame] | 707 | // Remember the new value for the next run |
Vikram Aggarwal | f18b117 | 2013-04-17 12:36:23 -0700 | [diff] [blame] | 708 | mUnreadCount = toDisplay; |
| 709 | } |
| 710 | |
| 711 | /** |
| 712 | * Remove the unread count and show the account name, if required. |
Vikram Aggarwal | 9dcd483 | 2013-05-09 16:08:59 -0700 | [diff] [blame] | 713 | * @param now true if you want the change to happen immediately. False if you want to enforce |
| 714 | * it happens later. |
Vikram Aggarwal | f18b117 | 2013-04-17 12:36:23 -0700 | [diff] [blame] | 715 | */ |
Vikram Aggarwal | 9dcd483 | 2013-05-09 16:08:59 -0700 | [diff] [blame] | 716 | private void removeUnreadCount(boolean now) { |
| 717 | if (now) { |
| 718 | // Remove all previous messages which might change the subtitle |
| 719 | mHandler.removeMessages(SubtitleHandler.EMAIL); |
| 720 | // Update the subtitle: clear it or show account name. |
| 721 | mHandler.sendEmptyMessage(SubtitleHandler.EMAIL); |
| 722 | } else { |
| 723 | if (!mHandler.hasMessages(SubtitleHandler.EMAIL)) { |
| 724 | // In a short while, show the account name in its place. |
| 725 | mHandler.sendEmptyMessageDelayed(SubtitleHandler.EMAIL, ACCOUNT_DELAY_MS); |
| 726 | } |
| 727 | } |
Vikram Aggarwal | bc462ca | 2013-03-15 10:41:03 -0700 | [diff] [blame] | 728 | } |
| 729 | |
Mindy Pereira | a46a57f | 2012-04-03 17:19:31 -0700 | [diff] [blame] | 730 | /** |
| 731 | * Notify that the folder has changed. |
| 732 | */ |
| 733 | public void onFolderUpdated(Folder folder) { |
Vikram Aggarwal | 26b0bfd | 2013-03-29 13:05:08 -0700 | [diff] [blame] | 734 | if (folder == null) { |
| 735 | return; |
| 736 | } |
Vikram Aggarwal | e875b66 | 2013-04-10 15:56:46 -0700 | [diff] [blame] | 737 | /** True if we are changing folders. */ |
Scott Kennedy | 259df5b | 2013-07-11 13:24:01 -0700 | [diff] [blame] | 738 | final boolean changingFolders = (mFolder == null || !mFolder.equals(folder)); |
Vikram Aggarwal | c18c338 | 2013-04-05 15:29:13 -0700 | [diff] [blame] | 739 | mFolder = folder; |
Vikram Aggarwal | 75d1bb1 | 2013-04-12 17:17:00 -0700 | [diff] [blame] | 740 | setFolderAndAccount(changingFolders); |
Vikram Aggarwal | 49499e6 | 2013-02-08 16:39:08 -0800 | [diff] [blame] | 741 | final ConversationListContext listContext = mController == null ? null : |
| 742 | mController.getCurrentListContext(); |
Vikram Aggarwal | e875b66 | 2013-04-10 15:56:46 -0700 | [diff] [blame] | 743 | if (changingFolders && !ConversationListContext.isSearchResult(listContext)) { |
Vikram Aggarwal | 49499e6 | 2013-02-08 16:39:08 -0800 | [diff] [blame] | 744 | closeSearchField(); |
| 745 | } |
Mindy Pereira | a46a57f | 2012-04-03 17:19:31 -0700 | [diff] [blame] | 746 | } |
Vikram Aggarwal | 0dda573 | 2012-04-06 11:20:16 -0700 | [diff] [blame] | 747 | |
| 748 | @Override |
| 749 | public boolean onMenuItemActionExpand(MenuItem item) { |
| 750 | // Do nothing. Required as part of the interface, we ar only interested in |
| 751 | // onMenuItemActionCollapse(MenuItem). |
| 752 | // Have to return true here. Unlike other callbacks, the return value here is whether |
| 753 | // we want to suppress the action (rather than consume the action). We don't want to |
| 754 | // suppress the action. |
| 755 | return true; |
| 756 | } |
| 757 | |
| 758 | @Override |
| 759 | public boolean onMenuItemActionCollapse(MenuItem item) { |
Mindy Pereira | b466bcf | 2012-06-14 15:52:26 -0700 | [diff] [blame] | 760 | // Work around b/6664203 by manually forcing this view to be VISIBLE |
| 761 | // upon ActionView collapse. DISPLAY_SHOW_CUSTOM will still control its final |
| 762 | // visibility. |
Andy Huang | 5895f7b | 2012-06-01 17:07:20 -0700 | [diff] [blame] | 763 | setVisibility(VISIBLE); |
Mindy Pereira | b466bcf | 2012-06-14 15:52:26 -0700 | [diff] [blame] | 764 | // Have to return true here. Unlike other callbacks, the return value |
| 765 | // here is whether we want to suppress the action (rather than consume the action). We |
| 766 | // don't want to suppress the action. |
Vikram Aggarwal | 0dda573 | 2012-04-06 11:20:16 -0700 | [diff] [blame] | 767 | return true; |
| 768 | } |
Vikram Aggarwal | 3726397 | 2012-04-17 15:51:14 -0700 | [diff] [blame] | 769 | |
Vikram Aggarwal | fd7eaba | 2013-04-11 12:31:46 -0700 | [diff] [blame] | 770 | /** |
| 771 | * 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] | 772 | * OR'd together: {@link ActionBar#DISPLAY_SHOW_CUSTOM} and |
| 773 | * {@link ActionBar#DISPLAY_SHOW_TITLE}. To disable all, pass a zero. |
Vikram Aggarwal | fd7eaba | 2013-04-11 12:31:46 -0700 | [diff] [blame] | 774 | * @param enabledFlags |
| 775 | */ |
Andy Huang | af65e73 | 2013-01-28 20:45:40 -0800 | [diff] [blame] | 776 | private void setTitleModeFlags(int enabledFlags) { |
Paul Westbrook | 8c887ef | 2013-04-24 00:29:28 -0700 | [diff] [blame] | 777 | final int mask = ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_CUSTOM; |
Andy Huang | af65e73 | 2013-01-28 20:45:40 -0800 | [diff] [blame] | 778 | mActionBar.setDisplayOptions(enabledFlags, mask); |
| 779 | } |
| 780 | |
Yorke Lee | f807ba7 | 2012-09-20 17:18:05 -0700 | [diff] [blame] | 781 | public void setCurrentConversation(Conversation conversation) { |
| 782 | mCurrentConversation = conversation; |
| 783 | } |
| 784 | |
| 785 | //We need to do this here instead of in the fragment |
| 786 | public void setConversationModeOptions(Menu menu) { |
| 787 | if (mCurrentConversation == null) { |
| 788 | return; |
| 789 | } |
| 790 | final boolean showMarkImportant = !mCurrentConversation.isImportant(); |
| 791 | Utils.setMenuItemVisibility(menu, R.id.mark_important, showMarkImportant |
| 792 | && mAccount.supportsCapability(UIProvider.AccountCapabilities.MARK_IMPORTANT)); |
| 793 | Utils.setMenuItemVisibility(menu, R.id.mark_not_important, !showMarkImportant |
| 794 | && mAccount.supportsCapability(UIProvider.AccountCapabilities.MARK_IMPORTANT)); |
| 795 | final boolean showDelete = mFolder != null && |
| 796 | mFolder.supportsCapability(UIProvider.FolderCapabilities.DELETE); |
| 797 | Utils.setMenuItemVisibility(menu, R.id.delete, showDelete); |
| 798 | // We only want to show the discard drafts menu item if we are not showing the delete menu |
| 799 | // item, and the current folder is a draft folder and the account supports discarding |
| 800 | // drafts for a conversation |
| 801 | final boolean showDiscardDrafts = !showDelete && mFolder != null && mFolder.isDraft() && |
| 802 | mAccount.supportsCapability(AccountCapabilities.DISCARD_CONVERSATION_DRAFTS); |
| 803 | Utils.setMenuItemVisibility(menu, R.id.discard_drafts, showDiscardDrafts); |
| 804 | final boolean archiveVisible = mAccount.supportsCapability(AccountCapabilities.ARCHIVE) |
| 805 | && mFolder != null && mFolder.supportsCapability(FolderCapabilities.ARCHIVE) |
| 806 | && !mFolder.isTrash(); |
| 807 | Utils.setMenuItemVisibility(menu, R.id.archive, archiveVisible); |
| 808 | Utils.setMenuItemVisibility(menu, R.id.remove_folder, !archiveVisible && mFolder != null |
| 809 | && mFolder.supportsCapability(FolderCapabilities.CAN_ACCEPT_MOVED_MESSAGES) |
Tony Mantler | 53b3425 | 2013-09-25 16:42:25 -0700 | [diff] [blame] | 810 | && !mFolder.isProviderFolder() |
| 811 | && mAccount.supportsCapability(AccountCapabilities.ARCHIVE)); |
Scott Kennedy | 28f3d5c | 2013-04-08 16:53:50 -0700 | [diff] [blame] | 812 | Utils.setMenuItemVisibility(menu, R.id.move_to, mFolder != null |
| 813 | && mFolder.supportsCapability(FolderCapabilities.ALLOWS_REMOVE_CONVERSATION)); |
Scott Kennedy | dd2ec68 | 2013-06-03 19:16:13 -0700 | [diff] [blame] | 814 | Utils.setMenuItemVisibility(menu, R.id.move_to_inbox, mFolder != null |
| 815 | && mFolder.supportsCapability(FolderCapabilities.ALLOWS_MOVE_TO_INBOX)); |
| 816 | |
Yorke Lee | f807ba7 | 2012-09-20 17:18:05 -0700 | [diff] [blame] | 817 | final MenuItem removeFolder = menu.findItem(R.id.remove_folder); |
Alice Yang | 40698e6 | 2013-03-14 11:31:53 -0700 | [diff] [blame] | 818 | if (mFolder != null && removeFolder != null) { |
Yorke Lee | f807ba7 | 2012-09-20 17:18:05 -0700 | [diff] [blame] | 819 | removeFolder.setTitle(mActivity.getApplicationContext().getString( |
| 820 | R.string.remove_folder, mFolder.name)); |
| 821 | } |
| 822 | Utils.setMenuItemVisibility(menu, R.id.report_spam, |
| 823 | mAccount.supportsCapability(AccountCapabilities.REPORT_SPAM) && mFolder != null |
| 824 | && mFolder.supportsCapability(FolderCapabilities.REPORT_SPAM) |
| 825 | && !mCurrentConversation.spam); |
| 826 | Utils.setMenuItemVisibility(menu, R.id.mark_not_spam, |
| 827 | mAccount.supportsCapability(AccountCapabilities.REPORT_SPAM) && mFolder != null |
| 828 | && mFolder.supportsCapability(FolderCapabilities.MARK_NOT_SPAM) |
| 829 | && mCurrentConversation.spam); |
| 830 | Utils.setMenuItemVisibility(menu, R.id.report_phishing, |
| 831 | mAccount.supportsCapability(AccountCapabilities.REPORT_PHISHING) && mFolder != null |
| 832 | && mFolder.supportsCapability(FolderCapabilities.REPORT_PHISHING) |
| 833 | && !mCurrentConversation.phishing); |
| 834 | Utils.setMenuItemVisibility(menu, R.id.mute, |
| 835 | mAccount.supportsCapability(AccountCapabilities.MUTE) && mFolder != null |
| 836 | && mFolder.supportsCapability(FolderCapabilities.DESTRUCTIVE_MUTE) |
| 837 | && !mCurrentConversation.muted); |
| 838 | } |
Paul Westbrook | 8c887ef | 2013-04-24 00:29:28 -0700 | [diff] [blame] | 839 | |
| 840 | private static boolean actionBarSupportsNewMethods(ActionBar bar) { |
| 841 | // TODO(pwestbro) switch this to |
| 842 | // (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) when we switch to the |
| 843 | // latest SDK |
| 844 | if (Build.VERSION.SDK_INT > 17) { |
| 845 | return true; |
| 846 | } |
| 847 | if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN) { |
| 848 | return false; |
| 849 | } |
| 850 | boolean supportsNewApi = false; |
| 851 | try { |
| 852 | if (bar != null) { |
| 853 | supportsNewApi = (ActionBar.class.getField("DISPLAY_TITLE_MULTIPLE_LINES") != null); |
| 854 | } |
| 855 | } catch (NoSuchFieldException e) { |
| 856 | // stay false |
| 857 | } |
| 858 | return supportsNewApi; |
| 859 | } |
| 860 | |
| 861 | @Override |
| 862 | public void onClick (View v) { |
| 863 | if (v.getId() == R.id.legacy_title_container) { |
| 864 | mController.onUpPressed(); |
| 865 | } |
| 866 | } |
Tony Mantler | bd09150 | 2013-09-16 13:59:47 -0700 | [diff] [blame^] | 867 | |
| 868 | public ViewMode getViewModeController() { |
| 869 | return mViewModeController; |
| 870 | } |
| 871 | |
| 872 | public void setViewModeController(ViewMode viewModeController) { |
| 873 | mViewModeController = viewModeController; |
| 874 | mViewModeController.addListener(this); |
| 875 | } |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 876 | } |