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; |
Vikram Aggarwal | 2175d0a | 2012-02-17 16:04:23 -0800 | [diff] [blame] | 23 | import android.app.ActionBar.OnNavigationListener; |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 24 | import android.content.Context; |
Vikram Aggarwal | 12a7c04 | 2012-03-28 16:48:08 -0700 | [diff] [blame] | 25 | import android.database.Cursor; |
Vikram Aggarwal | 1e57e67 | 2012-05-07 14:48:24 -0700 | [diff] [blame] | 26 | import android.net.Uri; |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 27 | import android.os.Bundle; |
Mindy Pereira | 9b62380 | 2012-03-07 17:15:49 -0800 | [diff] [blame] | 28 | import android.os.Handler; |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 29 | import android.util.AttributeSet; |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 30 | import android.view.Menu; |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 31 | import android.view.MenuItem; |
| 32 | import android.view.View; |
| 33 | import android.widget.LinearLayout; |
| 34 | import android.widget.SearchView; |
| 35 | import android.widget.SearchView.OnQueryTextListener; |
Vikram Aggarwal | 12a7c04 | 2012-03-28 16:48:08 -0700 | [diff] [blame] | 36 | import android.widget.SearchView.OnSuggestionListener; |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 37 | import android.widget.TextView; |
| 38 | import android.widget.Toast; |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 39 | |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 40 | import com.android.mail.R; |
| 41 | import com.android.mail.AccountSpinnerAdapter; |
| 42 | import com.android.mail.ConversationListContext; |
Vikram Aggarwal | a9b93f3 | 2012-02-23 14:51:58 -0800 | [diff] [blame] | 43 | import com.android.mail.providers.Account; |
Vikram Aggarwal | 1e57e67 | 2012-05-07 14:48:24 -0700 | [diff] [blame] | 44 | import com.android.mail.providers.Settings; |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 45 | import com.android.mail.providers.UIProvider.AccountCapabilities; |
Marc Blank | edcab01 | 2012-04-04 12:42:38 -0700 | [diff] [blame] | 46 | import com.android.mail.providers.UIProvider.FolderCapabilities; |
Mindy Pereira | 9b62380 | 2012-03-07 17:15:49 -0800 | [diff] [blame] | 47 | import com.android.mail.providers.UIProvider.LastSyncResult; |
Mindy Pereira | f9323cd | 2012-02-29 13:47:09 -0800 | [diff] [blame] | 48 | import com.android.mail.providers.Folder; |
Andy Huang | 0d64735 | 2012-03-21 21:48:16 -0700 | [diff] [blame] | 49 | import com.android.mail.utils.LogUtils; |
Mindy Pereira | 9b62380 | 2012-03-07 17:15:49 -0800 | [diff] [blame] | 50 | import com.android.mail.utils.Utils; |
Vikram Aggarwal | a9b93f3 | 2012-02-23 14:51:58 -0800 | [diff] [blame] | 51 | |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 52 | /** |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 53 | * View to manage the various states of the Mail Action Bar |
| 54 | * |
| 55 | * TODO(viki): Include ConversationSubjectDisplayer here as well. |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 56 | */ |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 57 | public final class ActionBarView extends LinearLayout implements OnNavigationListener, |
Vikram Aggarwal | 0dda573 | 2012-04-06 11:20:16 -0700 | [diff] [blame] | 58 | ViewMode.ModeChangeListener, OnQueryTextListener, OnSuggestionListener, |
| 59 | MenuItem.OnActionExpandListener { |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 60 | private ActionBar mActionBar; |
| 61 | private RestrictedActivity mActivity; |
Vikram Aggarwal | a799795 | 2012-03-09 10:30:13 -0800 | [diff] [blame] | 62 | private ActivityController mController; |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 63 | private View mFolderView; |
| 64 | /** |
| 65 | * The current mode of the ActionBar. This references constants in {@link ViewMode} |
| 66 | */ |
| 67 | private int mMode = ViewMode.UNKNOWN; |
| 68 | |
| 69 | private MenuItem mSearch; |
Vikram Aggarwal | 3726397 | 2012-04-17 15:51:14 -0700 | [diff] [blame] | 70 | private AccountSpinnerAdapter mSpinner; |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 71 | /** |
| 72 | * The account currently being shown |
| 73 | */ |
| 74 | private Account mAccount; |
Marc Blank | edcab01 | 2012-04-04 12:42:38 -0700 | [diff] [blame] | 75 | /** |
| 76 | * The folder currently being shown |
| 77 | */ |
| 78 | private Folder mFolder; |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 79 | |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 80 | private TextView mSubjectView; |
| 81 | private SearchView mSearchWidget; |
| 82 | private MenuItem mHelpItem; |
Paul Westbrook | 517743e | 2012-03-22 10:40:46 -0700 | [diff] [blame] | 83 | private MenuItem mSendFeedbackItem; |
Mindy Pereira | 9b62380 | 2012-03-07 17:15:49 -0800 | [diff] [blame] | 84 | private MenuItem mRefreshItem; |
Marc Blank | edcab01 | 2012-04-04 12:42:38 -0700 | [diff] [blame] | 85 | private MenuItem mFolderSettingsItem; |
Mindy Pereira | 9b62380 | 2012-03-07 17:15:49 -0800 | [diff] [blame] | 86 | private View mRefreshActionView; |
| 87 | private boolean mRefreshInProgress; |
| 88 | |
Andy Huang | 0d64735 | 2012-03-21 21:48:16 -0700 | [diff] [blame] | 89 | public static final String LOG_TAG = new LogUtils().getLogTag(); |
| 90 | |
Mindy Pereira | 9b62380 | 2012-03-07 17:15:49 -0800 | [diff] [blame] | 91 | private final Handler mHandler = new Handler(); |
| 92 | private final Runnable mInvalidateMenu = new Runnable() { |
| 93 | @Override |
| 94 | public void run() { |
| 95 | mActivity.invalidateOptionsMenu(); |
| 96 | } |
| 97 | }; |
Vikram Aggarwal | abd24d8 | 2012-04-26 13:23:14 -0700 | [diff] [blame] | 98 | /** |
| 99 | * Whether the first navigation event should be ignored. The {@link #ignoreFirstNavigation(int)} |
| 100 | * method talks about why this is required. |
| 101 | */ |
| 102 | private boolean mIgnoreFirstNavigation = true; |
Mindy Pereira | 205ae50 | 2012-05-17 08:43:59 -0700 | [diff] [blame^] | 103 | private Boolean mShowConversationSubject; |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 104 | |
| 105 | public ActionBarView(Context context) { |
| 106 | this(context, null); |
| 107 | } |
| 108 | |
| 109 | public ActionBarView(Context context, AttributeSet attrs) { |
| 110 | this(context, attrs, 0); |
| 111 | } |
| 112 | |
| 113 | public ActionBarView(Context context, AttributeSet attrs, int defStyle) { |
| 114 | super(context, attrs, defStyle); |
| 115 | } |
| 116 | |
Vikram Aggarwal | b17cbc0 | 2012-04-06 15:41:46 -0700 | [diff] [blame] | 117 | /** |
Vikram Aggarwal | 0dda573 | 2012-04-06 11:20:16 -0700 | [diff] [blame] | 118 | * Close the search view if it is expanded. |
Vikram Aggarwal | b17cbc0 | 2012-04-06 15:41:46 -0700 | [diff] [blame] | 119 | */ |
| 120 | public void collapseSearch() { |
| 121 | if (mSearch != null) { |
| 122 | mSearch.collapseActionView(); |
| 123 | } |
| 124 | } |
| 125 | |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 126 | public boolean onCreateOptionsMenu(Menu menu) { |
| 127 | // If the mode is valid, then set the initial menu |
| 128 | if (mMode == ViewMode.UNKNOWN) { |
| 129 | return false; |
| 130 | } |
Mindy Pereira | 9b62380 | 2012-03-07 17:15:49 -0800 | [diff] [blame] | 131 | mSearch = menu.findItem(R.id.search); |
| 132 | if (mSearch != null) { |
| 133 | mSearchWidget = (SearchView) mSearch.getActionView(); |
Vikram Aggarwal | 0dda573 | 2012-04-06 11:20:16 -0700 | [diff] [blame] | 134 | mSearch.setOnActionExpandListener(this); |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 135 | SearchManager searchManager = (SearchManager) mActivity.getActivityContext() |
| 136 | .getSystemService(Context.SEARCH_SERVICE); |
Mindy Pereira | fb17849 | 2012-03-07 18:56:42 -0800 | [diff] [blame] | 137 | if (searchManager != null && mSearchWidget != null) { |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 138 | SearchableInfo info = searchManager.getSearchableInfo(mActivity.getComponentName()); |
| 139 | mSearchWidget.setSearchableInfo(info); |
| 140 | mSearchWidget.setOnQueryTextListener(this); |
Vikram Aggarwal | 12a7c04 | 2012-03-28 16:48:08 -0700 | [diff] [blame] | 141 | mSearchWidget.setOnSuggestionListener(this); |
Vikram Aggarwal | 0dda573 | 2012-04-06 11:20:16 -0700 | [diff] [blame] | 142 | mSearchWidget.setIconifiedByDefault(true); |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | mHelpItem = menu.findItem(R.id.help_info_menu_item); |
Paul Westbrook | 517743e | 2012-03-22 10:40:46 -0700 | [diff] [blame] | 146 | mSendFeedbackItem = menu.findItem(R.id.feedback_menu_item); |
Mindy Pereira | 9b62380 | 2012-03-07 17:15:49 -0800 | [diff] [blame] | 147 | mRefreshItem = menu.findItem(R.id.refresh); |
Marc Blank | edcab01 | 2012-04-04 12:42:38 -0700 | [diff] [blame] | 148 | mFolderSettingsItem = menu.findItem(R.id.folder_options); |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 149 | return true; |
| 150 | } |
| 151 | |
| 152 | public int getOptionsMenuId() { |
| 153 | // Relies on the ordering of the view modes, since they are integer constants. |
| 154 | final int[] modeMenu = { |
| 155 | // 0: UNKNOWN |
| 156 | R.menu.conversation_list_menu, |
| 157 | // 1: CONVERSATION |
| 158 | R.menu.conversation_actions, |
| 159 | // 2: CONVERSATION_LIST |
| 160 | R.menu.conversation_list_menu, |
| 161 | // 3: FOLDER_LIST |
| 162 | R.menu.folder_list_menu, |
| 163 | // 4: SEARCH_RESULTS_LIST |
| 164 | R.menu.conversation_list_search_results_actions, |
| 165 | // 5: SEARCH_RESULTS_CONVERSATION |
Paul Westbrook | 2d50bcd | 2012-04-10 11:53:47 -0700 | [diff] [blame] | 166 | R.menu.conversation_search_results_actions, |
| 167 | // 6: WAITING_FOR_ACCOUNT_INITIALIZATION |
| 168 | R.menu.wait_mode_actions |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 169 | }; |
| 170 | return modeMenu[mMode]; |
| 171 | } |
| 172 | |
| 173 | public void handleRestore(Bundle savedInstanceState) { |
| 174 | } |
| 175 | |
| 176 | public void handleSaveInstanceState(Bundle outState) { |
| 177 | } |
| 178 | |
| 179 | public void initialize(RestrictedActivity activity, ActivityController callback, |
Vikram Aggarwal | ec5cbf7 | 2012-03-08 15:10:35 -0800 | [diff] [blame] | 180 | ViewMode viewMode, ActionBar actionBar, RecentFolderList recentFolders) { |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 181 | mActionBar = actionBar; |
Vikram Aggarwal | a799795 | 2012-03-09 10:30:13 -0800 | [diff] [blame] | 182 | mController = callback; |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 183 | mActivity = activity; |
Paul Westbrook | 904b5f2 | 2012-04-12 00:29:58 -0700 | [diff] [blame] | 184 | // We don't want to include the "Show all folders" menu item on tablet devices |
| 185 | final boolean showAllFolders = !Utils.useTabletUI(getContext()); |
| 186 | mSpinner = new AccountSpinnerAdapter(getContext(), recentFolders, showAllFolders); |
Vikram Aggarwal | abd24d8 | 2012-04-26 13:23:14 -0700 | [diff] [blame] | 187 | } |
Andy Huang | e303b9b | 2012-04-19 16:31:09 -0700 | [diff] [blame] | 188 | |
Vikram Aggarwal | abd24d8 | 2012-04-26 13:23:14 -0700 | [diff] [blame] | 189 | /** |
| 190 | * Attach the action bar to the view. |
| 191 | */ |
| 192 | public void attach() { |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 193 | mActionBar.setListNavigationCallbacks(mSpinner, this); |
| 194 | } |
| 195 | |
| 196 | public void setAccounts(Account[] accounts) { |
Vikram Aggarwal | 630489a | 2012-04-11 17:08:09 -0700 | [diff] [blame] | 197 | final Account currentAccount = mController.getCurrentAccount(); |
Vikram Aggarwal | abd24d8 | 2012-04-26 13:23:14 -0700 | [diff] [blame] | 198 | mSpinner.setAccountArray(accounts); |
Andy Huang | 0d64735 | 2012-03-21 21:48:16 -0700 | [diff] [blame] | 199 | |
| 200 | int position; |
| 201 | for (position = 0; position < accounts.length; position++) { |
Vikram Aggarwal | 630489a | 2012-04-11 17:08:09 -0700 | [diff] [blame] | 202 | if (accounts[position].uri.equals(currentAccount.uri)) { |
Andy Huang | 0d64735 | 2012-03-21 21:48:16 -0700 | [diff] [blame] | 203 | break; |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 204 | } |
| 205 | } |
Andy Huang | 0d64735 | 2012-03-21 21:48:16 -0700 | [diff] [blame] | 206 | if (position >= accounts.length) { |
| 207 | position = 0; |
| 208 | LogUtils.w(LOG_TAG, "IN actionbarview setAccounts, account not found, using first."); |
| 209 | } |
Vikram Aggarwal | 1e57e67 | 2012-05-07 14:48:24 -0700 | [diff] [blame] | 210 | final Uri defaultInbox = Settings.getDefaultInboxUri(mAccount.settings); |
Vikram Aggarwal | c323f5d | 2012-04-26 16:26:07 -0700 | [diff] [blame] | 211 | final boolean viewingDefaultInbox = |
| 212 | (mFolder == null || mAccount == null || mAccount.settings == null) ? false : |
Vikram Aggarwal | 1e57e67 | 2012-05-07 14:48:24 -0700 | [diff] [blame] | 213 | mFolder.uri.equals(defaultInbox); |
Vikram Aggarwal | 95b28ab | 2012-04-26 15:53:09 -0700 | [diff] [blame] | 214 | final boolean accountInSpinner = (position >= 0); |
| 215 | if (accountInSpinner && viewingDefaultInbox) { |
| 216 | // This position corresponds to current account and default Inbox. Select it. |
| 217 | setSelectedPosition(position); |
| 218 | } else { |
| 219 | // Set the selected position to a dead spacer. The user is either viewing a different |
| 220 | // folder, or the account is missing from the spinner. |
| 221 | setSelectedPosition(mSpinner.getSpacerPosition()); |
| 222 | } |
Vikram Aggarwal | 3a10473 | 2012-03-27 11:35:17 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Sets the selected navigation position in the spinner to the position given here. |
| 227 | * @param position |
| 228 | */ |
| 229 | private void setSelectedPosition(int position) { |
| 230 | // Only change the position if we are in the correct mode. |
Vikram Aggarwal | eb7d329 | 2012-04-20 17:07:20 -0700 | [diff] [blame] | 231 | if (mActionBar.getNavigationMode() != ActionBar.NAVIGATION_MODE_LIST) { |
Vikram Aggarwal | 3a10473 | 2012-03-27 11:35:17 -0700 | [diff] [blame] | 232 | return; |
Vikram Aggarwal | eb7d329 | 2012-04-20 17:07:20 -0700 | [diff] [blame] | 233 | } |
| 234 | LogUtils.d(LOG_TAG, "ActionBarView.setSelectedNavigationItem(%d)", position); |
Andy Huang | 0d64735 | 2012-03-21 21:48:16 -0700 | [diff] [blame] | 235 | mActionBar.setSelectedNavigationItem(position); |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 236 | } |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 237 | |
| 238 | /** |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 239 | * Called by the owner of the ActionBar to set the |
| 240 | * folder that is currently being displayed. |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 241 | */ |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 242 | public void setFolder(Folder folder) { |
Vikram Aggarwal | e6340bc | 2012-03-26 15:57:09 -0700 | [diff] [blame] | 243 | // Change the currently selected item to an element which is a spacer: valid but not useful |
| 244 | // This allows us to receive a tap on the account name when the user taps on it, and we can |
| 245 | // take the user to the default inbox. |
Vikram Aggarwal | 3a10473 | 2012-03-27 11:35:17 -0700 | [diff] [blame] | 246 | setSelectedPosition(mSpinner.getSpacerPosition()); |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 247 | mSpinner.setCurrentFolder(folder); |
| 248 | mSpinner.notifyDataSetChanged(); |
Marc Blank | edcab01 | 2012-04-04 12:42:38 -0700 | [diff] [blame] | 249 | mFolder = folder; |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 250 | } |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 251 | |
| 252 | /** |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 253 | * Called by the owner of the ActionBar to set the |
| 254 | * account that is currently being displayed. |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 255 | */ |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 256 | public void setAccount(Account account) { |
Mindy Pereira | 9b62380 | 2012-03-07 17:15:49 -0800 | [diff] [blame] | 257 | mAccount = account; |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 258 | mSpinner.setCurrentAccount(account); |
| 259 | mSpinner.notifyDataSetChanged(); |
| 260 | } |
| 261 | |
Vikram Aggarwal | abd24d8 | 2012-04-26 13:23:14 -0700 | [diff] [blame] | 262 | /** |
| 263 | * Returns true if this list navigation event is erroneous and should be ignored. |
| 264 | * |
| 265 | * Rationale: When a spinner is brought up for the first time, and it has never been brought up |
| 266 | * before, it shows the 0th element. This is fine in most cases, since the navigation mode has |
| 267 | * to select something. However, if we already have an account: for example if we went from the |
| 268 | * widget to Conversation view, and the spinner never got a chance to initialize, it needs to |
| 269 | * ignore this first navigation. If the spinner has ever been shown, then we will allow |
| 270 | * subsequent calls to onNavigationItemSelected. |
| 271 | * @param position the position selected in the drop down. |
| 272 | */ |
| 273 | private boolean ignoreFirstNavigation(int position) { |
| 274 | if (mIgnoreFirstNavigation && position == 0 && mAccount != null) { |
| 275 | // Ignore the first navigation item selected because it is the list initializing |
| 276 | // We already have an account. |
| 277 | LogUtils.d(LOG_TAG, "ignoreFirstNavigation: Ignoring navigation to position 0." |
| 278 | + " mAccount = %s", mAccount.uri); |
| 279 | // All user taps are now valid: even a tap on the current account to take the user to |
| 280 | // the default inbox. |
| 281 | mIgnoreFirstNavigation = false; |
| 282 | setSelectedPosition(mSpinner.getSpacerPosition()); |
| 283 | // Yes, we want to ignore this navigation. It is not a user-initiated navigation. |
| 284 | return true; |
| 285 | } |
| 286 | // Spinner was correctly initialized and is receiving a valid first tap. All subsequent |
| 287 | // taps are user events. |
| 288 | mIgnoreFirstNavigation = false; |
| 289 | // No, we don't want to ignore this navigation. |
| 290 | return false; |
| 291 | } |
| 292 | |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 293 | @Override |
| 294 | public boolean onNavigationItemSelected(int position, long id) { |
Vikram Aggarwal | abd24d8 | 2012-04-26 13:23:14 -0700 | [diff] [blame] | 295 | if (ignoreFirstNavigation(position)) { |
| 296 | return false; |
| 297 | } |
Vikram Aggarwal | eb7d329 | 2012-04-20 17:07:20 -0700 | [diff] [blame] | 298 | LogUtils.d(LOG_TAG, "onNavigationItemSelected(%d, %d) called", position, id); |
Marc Blank | 41e764d | 2012-03-20 20:46:28 -0700 | [diff] [blame] | 299 | final int type = mSpinner.getType(position); |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 300 | switch (type) { |
| 301 | case AccountSpinnerAdapter.TYPE_ACCOUNT: |
| 302 | // Get the capabilities associated with this account. |
Vikram Aggarwal | eb7d329 | 2012-04-20 17:07:20 -0700 | [diff] [blame] | 303 | final Account account = (Account) mSpinner.getItem(position); |
| 304 | LogUtils.d(LOG_TAG, "onNavigationItemSelected: Selecting account: %s", |
| 305 | account.name); |
| 306 | if (mAccount.uri.equals(account.uri)) { |
| 307 | // The selected account is the same, let's load the default inbox. |
| 308 | mController.loadAccountInbox(); |
| 309 | } else { |
| 310 | // Switching accounts. |
| 311 | mController.onAccountChanged(account); |
| 312 | } |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 313 | break; |
| 314 | case AccountSpinnerAdapter.TYPE_FOLDER: |
| 315 | final Object folder = mSpinner.getItem(position); |
| 316 | assert (folder instanceof Folder); |
Vikram Aggarwal | eb7d329 | 2012-04-20 17:07:20 -0700 | [diff] [blame] | 317 | LogUtils.d(LOG_TAG, "onNavigationItemSelected: Selecting folder: %s", |
| 318 | ((Folder)folder).name); |
Vikram Aggarwal | a799795 | 2012-03-09 10:30:13 -0800 | [diff] [blame] | 319 | mController.onFolderChanged((Folder) folder); |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 320 | break; |
Paul Westbrook | 904b5f2 | 2012-04-12 00:29:58 -0700 | [diff] [blame] | 321 | case AccountSpinnerAdapter.TYPE_ALL_FOLDERS: |
| 322 | // Change the currently selected item to an element which is a spacer: valid |
| 323 | // but not useful. This allows us to receive subsequent taps on the |
| 324 | // "show all folders" menu item. |
| 325 | setSelectedPosition(mSpinner.getSpacerPosition()); |
| 326 | mController.showFolderList(); |
| 327 | break; |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 328 | } |
| 329 | return false; |
| 330 | } |
| 331 | |
| 332 | public void onPause() { |
| 333 | } |
| 334 | |
| 335 | public void onResume() { |
| 336 | } |
| 337 | |
Vikram Aggarwal | a799795 | 2012-03-09 10:30:13 -0800 | [diff] [blame] | 338 | @Override |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 339 | public void onViewModeChanged(int newMode) { |
| 340 | mMode = newMode; |
| 341 | // Always update the options menu and redraw. This will read the new mode and redraw |
| 342 | // the options menu. |
| 343 | mActivity.invalidateOptionsMenu(); |
| 344 | } |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 345 | |
Mindy Pereira | b849dfb | 2012-03-07 18:13:15 -0800 | [diff] [blame] | 346 | public boolean onPrepareOptionsMenu(Menu menu) { |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 347 | // We start out with every option enabled. Based on the current view, we disable actions |
| 348 | // that are possible. |
Vikram Aggarwal | 95b28ab | 2012-04-26 15:53:09 -0700 | [diff] [blame] | 349 | LogUtils.d(LOG_TAG, "ActionBarView.onPrepareOptionsMenu()."); |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 350 | if (mSubjectView != null){ |
| 351 | mSubjectView.setVisibility(GONE); |
| 352 | } |
| 353 | if (mFolderView != null){ |
| 354 | mFolderView.setVisibility(GONE); |
| 355 | } |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 356 | |
Mindy Pereira | 9b62380 | 2012-03-07 17:15:49 -0800 | [diff] [blame] | 357 | if (mRefreshInProgress) { |
| 358 | if (mRefreshItem != null) { |
| 359 | if (mRefreshActionView == null) { |
| 360 | mRefreshItem.setActionView(R.layout.action_bar_indeterminate_progress); |
| 361 | mRefreshActionView = mRefreshItem.getActionView(); |
| 362 | } else { |
| 363 | mRefreshItem.setActionView(mRefreshActionView); |
| 364 | } |
| 365 | } |
| 366 | } else { |
| 367 | if (mRefreshItem != null) { |
| 368 | mRefreshItem.setActionView(null); |
| 369 | } |
| 370 | } |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 371 | if (mHelpItem != null) { |
| 372 | mHelpItem.setVisible(mAccount != null |
| 373 | && mAccount.supportsCapability(AccountCapabilities.HELP_CONTENT)); |
| 374 | } |
Paul Westbrook | 517743e | 2012-03-22 10:40:46 -0700 | [diff] [blame] | 375 | if (mSendFeedbackItem != null) { |
| 376 | mSendFeedbackItem.setVisible(mAccount != null |
| 377 | && mAccount.supportsCapability(AccountCapabilities.SEND_FEEDBACK)); |
| 378 | } |
Marc Blank | edcab01 | 2012-04-04 12:42:38 -0700 | [diff] [blame] | 379 | if (mFolderSettingsItem != null) { |
| 380 | mFolderSettingsItem.setVisible(mFolder != null |
| 381 | && mFolder.supportsCapability(FolderCapabilities.SUPPORTS_SETTINGS)); |
| 382 | } |
Mindy Pereira | 9b62380 | 2012-03-07 17:15:49 -0800 | [diff] [blame] | 383 | switch (mMode) { |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 384 | case ViewMode.UNKNOWN: |
Mindy Pereira | 9b62380 | 2012-03-07 17:15:49 -0800 | [diff] [blame] | 385 | if (mSearch != null) { |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 386 | mSearch.collapseActionView(); |
| 387 | } |
| 388 | break; |
| 389 | case ViewMode.CONVERSATION_LIST: |
Mindy Pereira | 30fd47b | 2012-03-09 09:24:00 -0800 | [diff] [blame] | 390 | // Show compose, search, folders, and sync based on the account |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 391 | // The only option that needs to be disabled is search |
Mindy Pereira | 72a9f39 | 2012-03-21 09:41:09 -0700 | [diff] [blame] | 392 | showNavList(); |
Mindy Pereira | 863e441 | 2012-03-23 12:21:38 -0700 | [diff] [blame] | 393 | Utils.setMenuItemVisibility(menu, R.id.search, |
Mindy Pereira | 9b62380 | 2012-03-07 17:15:49 -0800 | [diff] [blame] | 394 | mAccount.supportsCapability(AccountCapabilities.FOLDER_SERVER_SEARCH)); |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 395 | break; |
| 396 | case ViewMode.CONVERSATION: |
Mindy Pereira | 72a9f39 | 2012-03-21 09:41:09 -0700 | [diff] [blame] | 397 | mActionBar.setDisplayHomeAsUpEnabled(true); |
Andy Huang | e303b9b | 2012-04-19 16:31:09 -0700 | [diff] [blame] | 398 | // FIXME: use a resource to have fine-grained control over whether the spinner |
| 399 | // or the subject appears |
Mindy Pereira | 205ae50 | 2012-05-17 08:43:59 -0700 | [diff] [blame^] | 400 | if (Utils.useTabletUI(mActivity.getActivityContext()) |
| 401 | && !showConversationSubject()) { |
Andy Huang | e303b9b | 2012-04-19 16:31:09 -0700 | [diff] [blame] | 402 | showNavList(); |
| 403 | } else { |
Vikram Aggarwal | abd24d8 | 2012-04-26 13:23:14 -0700 | [diff] [blame] | 404 | setStandardMode(); |
Andy Huang | e303b9b | 2012-04-19 16:31:09 -0700 | [diff] [blame] | 405 | } |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 406 | break; |
| 407 | case ViewMode.SEARCH_RESULTS_LIST: |
Vikram Aggarwal | abd24d8 | 2012-04-26 13:23:14 -0700 | [diff] [blame] | 408 | setStandardMode(); |
Mindy Pereira | b849dfb | 2012-03-07 18:13:15 -0800 | [diff] [blame] | 409 | setPopulatedSearchView(); |
Vikram Aggarwal | 8c35500 | 2012-04-09 09:07:40 -0700 | [diff] [blame] | 410 | // Remove focus from the search action menu in search results mode so the IME and |
| 411 | // the suggestions don't get in the way. |
| 412 | if (mMode == ViewMode.SEARCH_RESULTS_LIST && mSearch != null) { |
| 413 | mSearchWidget = (SearchView) mSearch.getActionView(); |
| 414 | mSearchWidget.clearFocus(); |
| 415 | } |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 416 | break; |
| 417 | case ViewMode.SEARCH_RESULTS_CONVERSATION: |
| 418 | mActionBar.setDisplayHomeAsUpEnabled(true); |
Vikram Aggarwal | abd24d8 | 2012-04-26 13:23:14 -0700 | [diff] [blame] | 419 | setStandardMode(); |
Mindy Pereira | b849dfb | 2012-03-07 18:13:15 -0800 | [diff] [blame] | 420 | if (Utils.useTabletUI(mActivity.getActivityContext())) { |
| 421 | setPopulatedSearchView(); |
| 422 | } |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 423 | break; |
| 424 | case ViewMode.FOLDER_LIST: |
Mindy Pereira | 72a9f39 | 2012-03-21 09:41:09 -0700 | [diff] [blame] | 425 | mActionBar.setDisplayHomeAsUpEnabled(true); |
| 426 | mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE, |
| 427 | ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_CUSTOM); |
Vikram Aggarwal | abd24d8 | 2012-04-26 13:23:14 -0700 | [diff] [blame] | 428 | setStandardMode(); |
Mindy Pereira | 72a9f39 | 2012-03-21 09:41:09 -0700 | [diff] [blame] | 429 | mActionBar.setTitle(R.string.folder_list_title); |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 430 | break; |
| 431 | } |
| 432 | return false; |
| 433 | } |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 434 | |
Mindy Pereira | 205ae50 | 2012-05-17 08:43:59 -0700 | [diff] [blame^] | 435 | private boolean showConversationSubject() { |
| 436 | if (mShowConversationSubject == null) { |
| 437 | mShowConversationSubject = new Boolean(mActivity.getActivityContext().getResources() |
| 438 | .getBoolean(R.bool.show_conversation_subject)); |
| 439 | } |
| 440 | return mShowConversationSubject; |
| 441 | } |
| 442 | |
Vikram Aggarwal | abd24d8 | 2012-04-26 13:23:14 -0700 | [diff] [blame] | 443 | /** |
| 444 | * Put the ActionBar in List navigation mode. This starts the spinner up if it is missing. |
| 445 | */ |
Mindy Pereira | 72a9f39 | 2012-03-21 09:41:09 -0700 | [diff] [blame] | 446 | private void showNavList() { |
| 447 | mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); |
| 448 | mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM, |
| 449 | ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_CUSTOM); |
| 450 | } |
| 451 | |
Vikram Aggarwal | abd24d8 | 2012-04-26 13:23:14 -0700 | [diff] [blame] | 452 | /** |
| 453 | * Set the actionbar mode to standard mode: no list navigation. |
| 454 | */ |
| 455 | private void setStandardMode() { |
| 456 | mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); |
| 457 | } |
| 458 | |
Mindy Pereira | b849dfb | 2012-03-07 18:13:15 -0800 | [diff] [blame] | 459 | private void setPopulatedSearchView() { |
| 460 | if (mSearch != null) { |
| 461 | mSearch.expandActionView(); |
Vikram Aggarwal | a799795 | 2012-03-09 10:30:13 -0800 | [diff] [blame] | 462 | ConversationListContext context = mController.getCurrentListContext(); |
Mindy Pereira | b849dfb | 2012-03-07 18:13:15 -0800 | [diff] [blame] | 463 | if (context != null) { |
| 464 | mSearchWidget.setQuery(context.searchQuery, false); |
| 465 | } |
| 466 | } |
| 467 | } |
| 468 | |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 469 | public void removeBackButton() { |
| 470 | if (mActionBar == null) { |
| 471 | return; |
| 472 | } |
| 473 | mActionBar.setDisplayOptions( |
| 474 | ActionBar.DISPLAY_SHOW_HOME, |
| 475 | ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME); |
| 476 | mActivity.getActionBar().setHomeButtonEnabled(false); |
| 477 | } |
Mindy Pereira | bc57bf1 | 2012-02-29 14:39:09 -0800 | [diff] [blame] | 478 | |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 479 | public void setBackButton() { |
| 480 | if (mActionBar == null){ |
| 481 | return; |
| 482 | } |
| 483 | mActionBar.setDisplayOptions( |
| 484 | ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME, |
| 485 | ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME); |
| 486 | mActivity.getActionBar().setHomeButtonEnabled(true); |
| 487 | } |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 488 | |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 489 | @Override |
| 490 | public boolean onQueryTextSubmit(String query) { |
Mindy Pereira | 4ce5994 | 2012-03-08 09:57:35 -0800 | [diff] [blame] | 491 | if (mSearch != null) { |
| 492 | mSearch.collapseActionView(); |
| 493 | mSearchWidget.setQuery("", false); |
| 494 | } |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 495 | mActivity.onSearchRequested(query); |
| 496 | return true; |
| 497 | } |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 498 | |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 499 | @Override |
| 500 | public boolean onQueryTextChange(String newText) { |
| 501 | // TODO Auto-generated method stub |
| 502 | return false; |
| 503 | } |
Mindy Pereira | 9b62380 | 2012-03-07 17:15:49 -0800 | [diff] [blame] | 504 | |
| 505 | public boolean setRefreshInProgress(boolean inProgress) { |
| 506 | if (inProgress != mRefreshInProgress) { |
| 507 | mRefreshInProgress = inProgress; |
| 508 | if (mSearch == null || !mSearch.isActionViewExpanded()) { |
| 509 | mHandler.post(mInvalidateMenu); |
| 510 | } |
| 511 | return true; |
| 512 | } |
| 513 | return false; |
| 514 | } |
| 515 | |
| 516 | public void onRefreshStarted() { |
| 517 | setRefreshInProgress(true); |
| 518 | } |
| 519 | |
| 520 | public void onRefreshStopped(int status) { |
| 521 | if (setRefreshInProgress(false)) { |
| 522 | switch (status) { |
| 523 | case LastSyncResult.SUCCESS: |
| 524 | break; |
| 525 | default: |
| 526 | Context context = mActivity.getActivityContext(); |
| 527 | Toast.makeText(context, Utils.getSyncStatusText(context, status), |
| 528 | Toast.LENGTH_LONG).show(); |
| 529 | break; |
| 530 | } |
| 531 | } |
| 532 | } |
Mindy Pereira | fd0c297 | 2012-03-27 13:50:39 -0700 | [diff] [blame] | 533 | |
| 534 | /** |
| 535 | * Get the query text the user entered in the search widget, or empty string |
| 536 | * if there is none. |
| 537 | */ |
| 538 | public String getQuery() { |
| 539 | return mSearchWidget != null ? mSearchWidget.getQuery().toString() : ""; |
| 540 | } |
Vikram Aggarwal | 12a7c04 | 2012-03-28 16:48:08 -0700 | [diff] [blame] | 541 | |
| 542 | // Next two methods are called when search suggestions are clicked. |
| 543 | @Override |
| 544 | public boolean onSuggestionSelect(int position) { |
| 545 | return onSuggestionClick(position); |
| 546 | } |
| 547 | |
| 548 | @Override |
| 549 | public boolean onSuggestionClick(int position) { |
| 550 | final Cursor c = mSearchWidget.getSuggestionsAdapter().getCursor(); |
| 551 | final boolean haveValidQuery = (c != null) && c.moveToPosition(position); |
| 552 | if (!haveValidQuery) { |
| 553 | LogUtils.d(LOG_TAG, "onSuggestionClick: Couldn't get a search query"); |
| 554 | // We haven't handled this query, but the default behavior will leave EXTRA_ACCOUNT |
| 555 | // un-populated, leading to a crash. So claim that we have handled the event. |
| 556 | return true; |
| 557 | } |
| 558 | final String query = c.getString(c.getColumnIndex(SearchManager.SUGGEST_COLUMN_QUERY)); |
| 559 | mController.onSearchRequested(query); |
| 560 | return true; |
| 561 | } |
Mindy Pereira | a46a57f | 2012-04-03 17:19:31 -0700 | [diff] [blame] | 562 | |
| 563 | /** |
| 564 | * Notify that the folder has changed. |
| 565 | */ |
| 566 | public void onFolderUpdated(Folder folder) { |
| 567 | mSpinner.onFolderUpdated(folder); |
| 568 | } |
Vikram Aggarwal | 0dda573 | 2012-04-06 11:20:16 -0700 | [diff] [blame] | 569 | |
| 570 | @Override |
| 571 | public boolean onMenuItemActionExpand(MenuItem item) { |
| 572 | // Do nothing. Required as part of the interface, we ar only interested in |
| 573 | // onMenuItemActionCollapse(MenuItem). |
| 574 | // Have to return true here. Unlike other callbacks, the return value here is whether |
| 575 | // we want to suppress the action (rather than consume the action). We don't want to |
| 576 | // suppress the action. |
| 577 | return true; |
| 578 | } |
| 579 | |
| 580 | @Override |
| 581 | public boolean onMenuItemActionCollapse(MenuItem item) { |
| 582 | // When the action menu is collapsed, we have performed a search, pop the search fragment. |
| 583 | mController.exitSearchMode(); |
| 584 | // Have to return true here. Unlike other callbacks, the return value here is whether |
| 585 | // we want to suppress the action (rather than consume the action). We don't want to |
| 586 | // suppress the action. |
| 587 | return true; |
| 588 | } |
Vikram Aggarwal | 3726397 | 2012-04-17 15:51:14 -0700 | [diff] [blame] | 589 | |
| 590 | /** |
| 591 | * Request the Accounts spinner to redraw itself in light of new data that it needs to request. |
| 592 | */ |
| 593 | public void requestRecentFoldersAndRedraw() { |
| 594 | mSpinner.requestRecentFoldersAndRedraw(); |
| 595 | } |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 596 | } |