Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 Google Inc. |
| 3 | * Licensed to The Android Open Source Project. |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
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; |
| 21 | import android.app.ActionBar.OnNavigationListener; |
| 22 | import android.content.Context; |
| 23 | import android.os.Bundle; |
| 24 | import android.util.AttributeSet; |
Vikram Aggarwal | 2175d0a | 2012-02-17 16:04:23 -0800 | [diff] [blame] | 25 | import android.util.Log; |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 26 | import android.view.Menu; |
| 27 | import android.view.MenuItem; |
| 28 | import android.view.View; |
| 29 | import android.widget.LinearLayout; |
| 30 | import android.widget.SpinnerAdapter; |
| 31 | import android.widget.TextView; |
| 32 | import android.widget.Toast; |
| 33 | |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 34 | import com.android.mail.R; |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 35 | import com.android.mail.AccountSpinnerAdapter; |
| 36 | import com.android.mail.ConversationListContext; |
Mindy Pereira | 28d5f72 | 2012-02-15 12:32:40 -0800 | [diff] [blame] | 37 | import com.android.mail.providers.Account; |
Vikram Aggarwal | 2175d0a | 2012-02-17 16:04:23 -0800 | [diff] [blame] | 38 | import com.android.mail.providers.UIProvider.AccountCapabilities; |
Mindy Pereira | 28e0c34 | 2012-02-17 15:05:13 -0800 | [diff] [blame] | 39 | import com.android.mail.providers.Folder; |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 40 | |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 41 | /** |
Vikram Aggarwal | 2175d0a | 2012-02-17 16:04:23 -0800 | [diff] [blame] | 42 | * View to manage the various states of the Mail Action Bar |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 43 | * |
Vikram Aggarwal | 2175d0a | 2012-02-17 16:04:23 -0800 | [diff] [blame] | 44 | * TODO(viki): Include ConversationSubjectDisplayer here as well. |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 45 | */ |
Vikram Aggarwal | 2175d0a | 2012-02-17 16:04:23 -0800 | [diff] [blame] | 46 | public final class MailActionBar extends LinearLayout implements ActionBarView { |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 47 | /** |
| 48 | * This interface is used to send notifications back to the calling |
| 49 | * activity. MenuHandler takes care of updating the provider, so this |
| 50 | * interface should be used for notification purposes only (such as updating |
| 51 | * the UI). |
| 52 | */ |
Vikram Aggarwal | 2175d0a | 2012-02-17 16:04:23 -0800 | [diff] [blame] | 53 | // TODO(viki): This callback is currently unused and may be entirely unnecessary in the new |
| 54 | // code, where the Actionbar is switched into navigation mode, relying on the framework for most |
| 55 | // heavy lifting. Also, we can switch ViewMode to the appropriate mode and rely on all UI |
| 56 | // components updating through ViewMode change listeners. |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 57 | public interface Callback { |
| 58 | /** |
| 59 | * Enter search mode |
| 60 | */ |
| 61 | void enterSearchMode(); |
| 62 | |
| 63 | /** |
| 64 | * Exits search mode |
| 65 | */ |
| 66 | void exitSearchMode(); |
| 67 | |
| 68 | /** |
| 69 | * Returns the current account. |
| 70 | */ |
Vikram Aggarwal | e9a8103 | 2012-02-22 13:15:35 -0800 | [diff] [blame^] | 71 | Account getCurrentAccount(); |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 72 | |
| 73 | /** |
| 74 | * Called when the TwoPaneActionBar wants to get the current conversation list context. |
| 75 | */ |
| 76 | ConversationListContext getCurrentListContext(); |
| 77 | |
| 78 | /** |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 79 | * Invoked when the user is already viewing search results |
| 80 | * and enters a new query. |
| 81 | * @param string Query |
| 82 | */ |
| 83 | void reloadSearch(String string); |
| 84 | |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 85 | void showFolderList(); |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 86 | |
| 87 | void startActionBarStatusCursorLoader(String account); |
| 88 | |
| 89 | void stopActionBarStatusCursorLoader(String account); |
| 90 | } |
| 91 | |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 92 | private String[] mAccountNames; |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 93 | private ActionBar mActionBar; |
Vikram Aggarwal | 2175d0a | 2012-02-17 16:04:23 -0800 | [diff] [blame] | 94 | private RestrictedActivity mActivity; |
Mindy Pereira | 28e0c34 | 2012-02-17 15:05:13 -0800 | [diff] [blame] | 95 | private ActivityController mCallback; |
Vikram Aggarwal | 2175d0a | 2012-02-17 16:04:23 -0800 | [diff] [blame] | 96 | private View mFolderView; |
| 97 | /** |
| 98 | * The current mode of the ActionBar. This references constants in {@link ViewMode} |
| 99 | */ |
| 100 | private int mMode = ViewMode.UNKNOWN; |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 101 | |
| 102 | private MenuItem mRefreshItem; |
| 103 | |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 104 | private MenuItem mSearch; |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 105 | SpinnerAdapter mSpinner; |
Vikram Aggarwal | 2175d0a | 2012-02-17 16:04:23 -0800 | [diff] [blame] | 106 | /** |
Vikram Aggarwal | bd6c1d3 | 2012-02-21 10:14:32 -0800 | [diff] [blame] | 107 | * The account currently being shown |
Vikram Aggarwal | 2175d0a | 2012-02-17 16:04:23 -0800 | [diff] [blame] | 108 | */ |
Vikram Aggarwal | bd6c1d3 | 2012-02-21 10:14:32 -0800 | [diff] [blame] | 109 | private Account mAccount; |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 110 | |
| 111 | // TODO(viki): This is a SnippetTextView in the Gmail source code. Resolve. |
| 112 | private TextView mSubjectView; |
| 113 | |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 114 | public MailActionBar(Context context) { |
| 115 | this(context, null); |
| 116 | } |
| 117 | |
| 118 | public MailActionBar(Context context, AttributeSet attrs) { |
| 119 | this(context, attrs, 0); |
| 120 | } |
| 121 | |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 122 | public MailActionBar(Context context, AttributeSet attrs, int defStyle) { |
| 123 | super(context, attrs, defStyle); |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 126 | @Override |
| 127 | public boolean createOptionsMenu(Menu menu) { |
| 128 | // If the mode is valid, then set the initial menu |
Mindy Pereira | f5acda4 | 2012-02-15 20:13:59 -0800 | [diff] [blame] | 129 | if (mMode == ViewMode.UNKNOWN) { |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 130 | return false; |
| 131 | } |
| 132 | mActivity.getMenuInflater().inflate(getOptionsMenuId(), menu); |
| 133 | // mSearch = menu.findItem(R.id.search); |
| 134 | // mRefreshItem = menu.findItem(R.id.refresh); |
| 135 | return true; |
| 136 | } |
| 137 | |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 138 | @Override |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 139 | public int getOptionsMenuId() { |
Vikram Aggarwal | 2175d0a | 2012-02-17 16:04:23 -0800 | [diff] [blame] | 140 | // Relies on the ordering of the view modes, since they are integer constants. |
| 141 | final int[] modeMenu = { |
| 142 | // 0: UNKNOWN |
| 143 | R.menu.conversation_list_menu, |
| 144 | // 1: CONVERSATION |
| 145 | R.menu.conversation_actions, |
| 146 | // 2: CONVERSATION_LIST |
| 147 | R.menu.conversation_list_menu, |
| 148 | // 3: FOLDER_LIST |
| 149 | R.menu.folder_list_menu, |
| 150 | // 4: SEARCH_RESULTS |
| 151 | R.menu.conversation_list_menu |
| 152 | }; |
| 153 | return modeMenu[mMode]; |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 154 | } |
| 155 | |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 156 | @Override |
| 157 | public void handleRestore(Bundle savedInstanceState) { |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 158 | } |
| 159 | |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 160 | @Override |
| 161 | public void handleSaveInstanceState(Bundle outState) { |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 162 | } |
| 163 | |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 164 | @Override |
Mindy Pereira | 28e0c34 | 2012-02-17 15:05:13 -0800 | [diff] [blame] | 165 | public void initialize(RestrictedActivity activity, ActivityController callback, ViewMode viewMode, |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 166 | ActionBar actionBar) { |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 167 | mActionBar = actionBar; |
| 168 | mCallback = callback; |
| 169 | mActivity = activity; |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 170 | |
| 171 | // Set the mode to Navigation mode |
| 172 | mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); |
| 173 | mSpinner = new AccountSpinnerAdapter(getContext()); |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 174 | mActionBar.setListNavigationCallbacks(mSpinner, this); |
| 175 | } |
| 176 | |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 177 | @Override |
Vikram Aggarwal | 2175d0a | 2012-02-17 16:04:23 -0800 | [diff] [blame] | 178 | public boolean onNavigationItemSelected(int position, long id) { |
| 179 | final int type = mSpinner.getItemViewType(position); |
Mindy Pereira | 28d5f72 | 2012-02-15 12:32:40 -0800 | [diff] [blame] | 180 | switch (type) { |
| 181 | case AccountSpinnerAdapter.TYPE_ACCOUNT: |
Mindy Pereira | 28e0c34 | 2012-02-17 15:05:13 -0800 | [diff] [blame] | 182 | mCallback.onAccountChanged((Account) mSpinner.getItem(position)); |
Vikram Aggarwal | 2175d0a | 2012-02-17 16:04:23 -0800 | [diff] [blame] | 183 | // Get the capabilities associated with this account. |
| 184 | final Object item = mSpinner.getItem(position); |
| 185 | assert (item instanceof Account); |
Vikram Aggarwal | bd6c1d3 | 2012-02-21 10:14:32 -0800 | [diff] [blame] | 186 | mAccount = (Account) item; |
Mindy Pereira | 28d5f72 | 2012-02-15 12:32:40 -0800 | [diff] [blame] | 187 | break; |
| 188 | } |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 189 | return false; |
| 190 | } |
| 191 | |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 192 | @Override |
| 193 | public void onPause() { |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 194 | } |
| 195 | |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 196 | @Override |
| 197 | public void onResume() { |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 198 | } |
| 199 | |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 200 | @Override |
| 201 | public void onStatusResult(String account, int status) { |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 202 | // Update the inbox folder if required |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 203 | mCallback.stopActionBarStatusCursorLoader(account); |
| 204 | } |
| 205 | |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 206 | @Override |
Vikram Aggarwal | 2175d0a | 2012-02-17 16:04:23 -0800 | [diff] [blame] | 207 | public void onViewModeChanged(int newMode) { |
| 208 | mMode = newMode; |
| 209 | |
| 210 | // Always update the options menu and redraw. This will read the new mode and redraw |
| 211 | // the options menu. |
| 212 | mActivity.invalidateOptionsMenu(); |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * If shouldSetView is true, then the view is made visible, otherwise its visiblity is View.GONE |
| 217 | * @param view the view whose visibility is modified |
| 218 | * @param shouldSetView if true, the view is made visible, GONE otherwise |
| 219 | */ |
| 220 | private void setVisibility(int resourceId, boolean shouldSetView) { |
| 221 | final View view = findViewById(resourceId); |
| 222 | assert (view != null); |
| 223 | final int visibility = shouldSetView ? View.VISIBLE : View.GONE; |
| 224 | view.setVisibility(visibility); |
| 225 | } |
| 226 | |
| 227 | @Override |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 228 | public boolean prepareOptionsMenu(Menu menu) { |
Vikram Aggarwal | 2175d0a | 2012-02-17 16:04:23 -0800 | [diff] [blame] | 229 | // We start out with every option enabled. Based on the current view, we disable actions |
| 230 | // that are possible. |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 231 | if (mSubjectView != null){ |
| 232 | mSubjectView.setVisibility(GONE); |
| 233 | } |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 234 | if (mFolderView != null){ |
| 235 | mFolderView.setVisibility(GONE); |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | switch (mMode){ |
Mindy Pereira | f5acda4 | 2012-02-15 20:13:59 -0800 | [diff] [blame] | 239 | case ViewMode.UNKNOWN: |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 240 | if (mSearch != null){ |
| 241 | mSearch.collapseActionView(); |
| 242 | } |
| 243 | break; |
Mindy Pereira | f5acda4 | 2012-02-15 20:13:59 -0800 | [diff] [blame] | 244 | case ViewMode.CONVERSATION_LIST: |
Vikram Aggarwal | 2175d0a | 2012-02-17 16:04:23 -0800 | [diff] [blame] | 245 | // Show compose, search, labels, and sync based on the account |
| 246 | // The only option that needs to be disabled is search |
Vikram Aggarwal | bd6c1d3 | 2012-02-21 10:14:32 -0800 | [diff] [blame] | 247 | setVisibility(R.id.search, mAccount.supportsCapability( |
| 248 | AccountCapabilities.FOLDER_SERVER_SEARCH)); |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 249 | break; |
Mindy Pereira | f5acda4 | 2012-02-15 20:13:59 -0800 | [diff] [blame] | 250 | case ViewMode.CONVERSATION: |
Vikram Aggarwal | bd6c1d3 | 2012-02-21 10:14:32 -0800 | [diff] [blame] | 251 | setVisibility(R.id.y_button, mAccount.supportsCapability( |
| 252 | AccountCapabilities.ARCHIVE)); |
| 253 | setVisibility(R.id.report_spam, mAccount.supportsCapability( |
| 254 | AccountCapabilities.REPORT_SPAM)); |
| 255 | setVisibility(R.id.mute, mAccount.supportsCapability(AccountCapabilities.MUTE)); |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 256 | break; |
Mindy Pereira | f5acda4 | 2012-02-15 20:13:59 -0800 | [diff] [blame] | 257 | case ViewMode.SEARCH_RESULTS: |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 258 | mActionBar.setDisplayHomeAsUpEnabled(true); |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 259 | if (mSearch != null) { |
| 260 | mSearch.collapseActionView(); |
| 261 | } |
Mindy Pereira | f5acda4 | 2012-02-15 20:13:59 -0800 | [diff] [blame] | 262 | case ViewMode.FOLDER_LIST: |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 263 | break; |
| 264 | } |
| 265 | return false; |
| 266 | } |
| 267 | |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 268 | @Override |
| 269 | public void removeBackButton() { |
| 270 | if (mActionBar == null) { |
| 271 | return; |
| 272 | } |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 273 | mActionBar.setDisplayOptions( |
| 274 | ActionBar.DISPLAY_SHOW_HOME, |
| 275 | ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME); |
| 276 | mActivity.getActionBar().setHomeButtonEnabled(false); |
| 277 | } |
| 278 | |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 279 | @Override |
| 280 | public void setBackButton() { |
| 281 | if (mActionBar == null){ |
| 282 | return; |
| 283 | } |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 284 | mActionBar.setDisplayOptions( |
| 285 | ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME, |
| 286 | ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME); |
| 287 | mActivity.getActionBar().setHomeButtonEnabled(true); |
| 288 | } |
| 289 | |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 290 | @Override |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 291 | public void setFolder(String folder) { |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 292 | // TODO(viki): Add this functionality to change the label. |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 293 | } |
| 294 | |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 295 | @Override |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 296 | public void updateActionBar(String[] accounts, String currentAccount) { |
| 297 | } |
Vikram Aggarwal | 5e5ac74 | 2011-12-19 08:14:16 -0800 | [diff] [blame] | 298 | } |