Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * Copyright (C) 2012 Google Inc. |
| 3 | * Licensed to The Android Open Source Project. |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | *******************************************************************************/ |
| 17 | |
| 18 | package com.android.mail.ui; |
| 19 | |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 20 | import android.app.ActionBar; |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 21 | import android.app.ActionBar.LayoutParams; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 22 | import android.app.Activity; |
| 23 | import android.app.Dialog; |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 24 | import android.content.ContentResolver; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 25 | import android.content.Context; |
Vikram Aggarwal | 7dedb95 | 2012-02-16 16:10:23 -0800 | [diff] [blame] | 26 | import android.content.CursorLoader; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 27 | import android.content.Intent; |
Vikram Aggarwal | 7dedb95 | 2012-02-16 16:10:23 -0800 | [diff] [blame] | 28 | import android.content.Loader; |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 29 | import android.database.Cursor; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 30 | import android.os.Bundle; |
| 31 | import android.view.ActionMode; |
| 32 | import android.view.KeyEvent; |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 33 | import android.view.LayoutInflater; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 34 | import android.view.Menu; |
Mindy Pereira | 28d5f72 | 2012-02-15 12:32:40 -0800 | [diff] [blame] | 35 | import android.view.MenuInflater; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 36 | import android.view.MenuItem; |
| 37 | import android.view.MotionEvent; |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 38 | import android.widget.LinearLayout; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 39 | |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 40 | import com.android.mail.R; |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 41 | import com.android.mail.ConversationListContext; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 42 | import com.android.mail.compose.ComposeActivity; |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 43 | import com.android.mail.providers.Account; |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 44 | import com.android.mail.providers.AccountCacheProvider; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 45 | import com.android.mail.providers.Conversation; |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 46 | import com.android.mail.providers.Folder; |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 47 | import com.android.mail.providers.UIProvider; |
Vikram Aggarwal | fa131a2 | 2012-02-02 13:56:22 -0800 | [diff] [blame] | 48 | import com.android.mail.utils.Utils; |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 49 | |
| 50 | /** |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 51 | * This is an abstract implementation of the Activity Controller. This class knows how to |
| 52 | * respond to menu items, state changes, layout changes, etc. It weaves together the views and |
| 53 | * listeners, dispatching actions to the respective underlying classes. |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 54 | * |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 55 | * <p>Even though this class is abstract, it should provide default implementations for most, if |
| 56 | * not all the methods in the ActivityController interface. This makes the task of the subclasses |
| 57 | * easier: OnePaneActivityController and TwoPaneActivityController can be concise when the common |
| 58 | * functionality is in AbstractActivityController. |
| 59 | *</p> |
| 60 | * <p> |
| 61 | * In the Gmail codebase, this was called BaseActivityController</p> |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 62 | */ |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 63 | public abstract class AbstractActivityController implements ActivityController { |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 64 | private static final String SAVED_CONVERSATION = "saved-conversation"; |
| 65 | private static final String SAVED_CONVERSATION_POSITION = "saved-conv-pos"; |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 66 | // Keys for serialization of various information in Bundles. |
| 67 | private static final String SAVED_LIST_CONTEXT = "saved-list-context"; |
Vikram Aggarwal | d7a12cd | 2012-02-03 09:36:20 -0800 | [diff] [blame] | 68 | /** |
| 69 | * Are we on a tablet device or not. |
| 70 | */ |
| 71 | public final boolean IS_TABLET_DEVICE; |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 72 | |
Vikram Aggarwal | d7a12cd | 2012-02-03 09:36:20 -0800 | [diff] [blame] | 73 | protected Account mAccount; |
Mindy Pereira | 28e0c34 | 2012-02-17 15:05:13 -0800 | [diff] [blame^] | 74 | protected Folder mFolder; |
Vikram Aggarwal | d7a12cd | 2012-02-03 09:36:20 -0800 | [diff] [blame] | 75 | protected ActionBarView mActionBarView; |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 76 | protected final RestrictedActivity mActivity; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 77 | protected final Context mContext; |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 78 | protected ConversationListContext mConvListContext; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 79 | protected Conversation mCurrentConversation; |
Vikram Aggarwal | d7a12cd | 2012-02-03 09:36:20 -0800 | [diff] [blame] | 80 | |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 81 | protected ConversationListFragment mConversationListFragment; |
Vikram Aggarwal | fa131a2 | 2012-02-02 13:56:22 -0800 | [diff] [blame] | 82 | /** |
| 83 | * The current mode of the application. All changes in mode are initiated by the activity |
| 84 | * controller. View mode changes are propagated to classes that attach themselves as listeners |
| 85 | * of view mode changes. |
| 86 | */ |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 87 | protected final ViewMode mViewMode; |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 88 | protected ContentResolver mResolver; |
Mindy Pereira | 0825efd | 2012-02-15 17:30:32 -0800 | [diff] [blame] | 89 | protected FolderListFragment mFolderListFragment; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 90 | protected ConversationViewFragment mConversationViewFragment; |
Vikram Aggarwal | 7dedb95 | 2012-02-16 16:10:23 -0800 | [diff] [blame] | 91 | protected boolean isLoaderInitialized = false; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 92 | |
| 93 | public AbstractActivityController(MailActivity activity, ViewMode viewMode) { |
| 94 | mActivity = activity; |
| 95 | mViewMode = viewMode; |
| 96 | mContext = activity.getApplicationContext(); |
Vikram Aggarwal | d7a12cd | 2012-02-03 09:36:20 -0800 | [diff] [blame] | 97 | IS_TABLET_DEVICE = Utils.useTabletUI(mContext); |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 98 | } |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 99 | |
| 100 | @Override |
Vikram Aggarwal | 2175d0a | 2012-02-17 16:04:23 -0800 | [diff] [blame] | 101 | public synchronized void attachConversationList(ConversationListFragment fragment) { |
| 102 | // If there is an existing fragment, unregister it |
| 103 | if (mConversationListFragment != null) { |
| 104 | mViewMode.removeListener(mConversationListFragment); |
| 105 | } |
| 106 | mConversationListFragment = fragment; |
| 107 | // If the current fragment is non-null, add it as a listener. |
| 108 | if (fragment != null) { |
| 109 | mViewMode.addListener(mConversationListFragment); |
| 110 | } |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | @Override |
Vikram Aggarwal | 2175d0a | 2012-02-17 16:04:23 -0800 | [diff] [blame] | 114 | public synchronized void attachFolderList(FolderListFragment fragment) { |
| 115 | // If there is an existing fragment, unregister it |
| 116 | if (mFolderListFragment != null) { |
| 117 | mViewMode.removeListener(mFolderListFragment); |
| 118 | } |
| 119 | mFolderListFragment = fragment; |
| 120 | if (fragment != null) { |
| 121 | mViewMode.addListener(mFolderListFragment); |
| 122 | } |
Mindy Pereira | 555140c | 2012-02-15 14:55:29 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | @Override |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 126 | public void attachConversationView(ConversationViewFragment conversationViewFragment) { |
| 127 | mConversationViewFragment = conversationViewFragment; |
| 128 | } |
| 129 | |
| 130 | @Override |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 131 | public void clearSubject() { |
| 132 | // TODO(viki): Auto-generated method stub |
| 133 | |
| 134 | } |
| 135 | |
| 136 | @Override |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 137 | public void enterSearchMode() { |
| 138 | // TODO(viki): Auto-generated method stub |
| 139 | |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public void exitSearchMode() { |
| 144 | // TODO(viki): Auto-generated method stub |
| 145 | |
| 146 | } |
| 147 | |
| 148 | @Override |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 149 | public String getCurrentAccount() { |
| 150 | // TODO(viki): Auto-generated method stub |
| 151 | return null; |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public ConversationListContext getCurrentListContext() { |
| 156 | // TODO(viki): Auto-generated method stub |
| 157 | return null; |
| 158 | } |
| 159 | |
| 160 | @Override |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 161 | public String getHelpContext() { |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 162 | return "Mail"; |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 163 | } |
| 164 | |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 165 | @Override |
Vikram Aggarwal | d7a12cd | 2012-02-03 09:36:20 -0800 | [diff] [blame] | 166 | public int getMode() { |
| 167 | return mViewMode.getMode(); |
| 168 | } |
| 169 | |
| 170 | @Override |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 171 | public String getUnshownSubject(String subject) { |
| 172 | // Calculate how much of the subject is shown, and return the remaining. |
| 173 | return null; |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public void handleConversationLoadError() { |
| 178 | // TODO(viki): Auto-generated method stub |
| 179 | |
| 180 | } |
| 181 | |
| 182 | @Override |
| 183 | public void handleSearchRequested() { |
| 184 | // TODO(viki): Auto-generated method stub |
| 185 | |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Initialize the action bar. This is not visible to OnePaneController and TwoPaneController so |
| 190 | * they cannot override this behavior. |
| 191 | */ |
| 192 | private void initCustomActionBarView() { |
| 193 | ActionBar actionBar = mActivity.getActionBar(); |
| 194 | mActionBarView = (MailActionBar) LayoutInflater.from(mContext).inflate( |
| 195 | R.layout.actionbar_view, null); |
| 196 | |
| 197 | if (actionBar != null && mActionBarView != null) { |
| 198 | // Why have a different variable for the same thing? We should apply the same actions |
| 199 | // on mActionBarView instead. |
| 200 | // mSubjectDisplayer = (ConversationSubjectDisplayer) mActionBarView; |
| 201 | mActionBarView.initialize(mActivity, this, mViewMode, actionBar); |
| 202 | actionBar.setCustomView((LinearLayout) mActionBarView, new ActionBar.LayoutParams( |
| 203 | LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); |
| 204 | actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM, |
| 205 | ActionBar.DISPLAY_SHOW_CUSTOM |
| 206 | | ActionBar.DISPLAY_SHOW_TITLE); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Returns whether the conversation list fragment is visible or not. Different layouts will have |
| 212 | * their own notion on the visibility of fragments, so this method needs to be overriden. |
| 213 | * @return |
| 214 | */ |
| 215 | protected abstract boolean isConversationListVisible(); |
| 216 | |
| 217 | @Override |
Mindy Pereira | 28e0c34 | 2012-02-17 15:05:13 -0800 | [diff] [blame^] | 218 | public void onAccountChanged(Account account) { |
Mindy Pereira | 28d5f72 | 2012-02-15 12:32:40 -0800 | [diff] [blame] | 219 | if (!account.equals(mAccount)) { |
| 220 | mAccount = account; |
| 221 | |
| 222 | final Intent intent = mActivity.getIntent(); |
| 223 | // TODO(viki): Show the list context from Intent |
| 224 | mConvListContext = ConversationListContext.forIntent(mContext, mAccount, intent); |
Mindy Pereira | 28e0c34 | 2012-02-17 15:05:13 -0800 | [diff] [blame^] | 225 | mFolder = mConvListContext.mFolder; |
Mindy Pereira | 28d5f72 | 2012-02-15 12:32:40 -0800 | [diff] [blame] | 226 | showConversationList(mConvListContext); |
| 227 | mViewMode.enterConversationListMode(); |
| 228 | } |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | @Override |
Mindy Pereira | 28e0c34 | 2012-02-17 15:05:13 -0800 | [diff] [blame^] | 232 | public void onFolderChanged(Folder folder) { |
| 233 | if (!folder.equals(mFolder)) { |
| 234 | mFolder = folder; |
| 235 | final Intent intent = mActivity.getIntent(); |
| 236 | intent.putExtra(ConversationListContext.EXTRA_FOLDER, mFolder); |
| 237 | // TODO(viki): Show the list context from Intent |
| 238 | mConvListContext = ConversationListContext.forIntent(mContext, mAccount, intent); |
| 239 | // Instead of this, switch to the conversation list mode and have that do the right |
| 240 | // things automatically. |
| 241 | showConversationList(mConvListContext); |
| 242 | mViewMode.enterConversationListMode(); |
| 243 | } |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | @Override |
| 247 | public void onActionModeFinished(ActionMode mode) { |
| 248 | // TODO(viki): Auto-generated method stub |
| 249 | |
| 250 | } |
| 251 | |
| 252 | @Override |
| 253 | public void onActionModeStarted(ActionMode mode) { |
| 254 | // TODO(viki): Auto-generated method stub |
| 255 | |
| 256 | } |
| 257 | |
| 258 | @Override |
| 259 | public void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 260 | // TODO(viki): Auto-generated method stub |
| 261 | |
| 262 | } |
| 263 | |
| 264 | @Override |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 265 | public void onConversationListVisibilityChanged(boolean visible) { |
| 266 | // TODO(viki): Auto-generated method stub |
| 267 | |
| 268 | } |
| 269 | |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 270 | /** |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 271 | * By default, doing nothing is right. A two-pane controller will need to |
| 272 | * override this. |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 273 | */ |
| 274 | @Override |
| 275 | public void onConversationVisibilityChanged(boolean visible) { |
| 276 | // Do nothing. |
| 277 | return; |
| 278 | } |
| 279 | |
| 280 | @Override |
Vikram Aggarwal | d7a12cd | 2012-02-03 09:36:20 -0800 | [diff] [blame] | 281 | public boolean onCreate(Bundle savedState) { |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 282 | // Initialize the action bar view. |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 283 | initCustomActionBarView(); |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 284 | final Intent intent = mActivity.getIntent(); |
Vikram Aggarwal | 7dedb95 | 2012-02-16 16:10:23 -0800 | [diff] [blame] | 285 | // Get a Loader to the Account |
| 286 | mActivity.getLoaderManager().initLoader(0, null, this); |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 287 | // Allow shortcut keys to function for the ActionBar and menus. |
| 288 | mActivity.setDefaultKeyMode(Activity.DEFAULT_KEYS_SHORTCUT); |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 289 | mResolver = mActivity.getContentResolver(); |
Vikram Aggarwal | 2175d0a | 2012-02-17 16:04:23 -0800 | [diff] [blame] | 290 | |
| 291 | // All the individual UI components listen for ViewMode changes. This simplifies the |
| 292 | // amount of logic in the AbstractActivityController, but increases the possibility of |
| 293 | // timing-related bugs. |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 294 | mViewMode.addListener(this); |
Vikram Aggarwal | 2175d0a | 2012-02-17 16:04:23 -0800 | [diff] [blame] | 295 | assert (mActionBarView != null); |
| 296 | mViewMode.addListener(mActionBarView); |
| 297 | |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 298 | restoreState(savedState); |
Vikram Aggarwal | d7a12cd | 2012-02-03 09:36:20 -0800 | [diff] [blame] | 299 | return true; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 300 | } |
| 301 | |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 302 | @Override |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 303 | public Dialog onCreateDialog(int id, Bundle bundle) { |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 304 | // TODO(viki): Auto-generated method stub |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 305 | return null; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | @Override |
| 309 | public boolean onCreateOptionsMenu(Menu menu) { |
Mindy Pereira | 28d5f72 | 2012-02-15 12:32:40 -0800 | [diff] [blame] | 310 | MenuInflater inflater = mActivity.getMenuInflater(); |
Mindy Pereira | f5acda4 | 2012-02-15 20:13:59 -0800 | [diff] [blame] | 311 | inflater.inflate(mActionBarView.getOptionsMenuId(), menu); |
Mindy Pereira | 28d5f72 | 2012-02-15 12:32:40 -0800 | [diff] [blame] | 312 | return true; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | @Override |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 316 | public void onEndBulkOperation() { |
| 317 | // TODO(viki): Auto-generated method stub |
| 318 | |
| 319 | } |
| 320 | |
| 321 | @Override |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 322 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
| 323 | // TODO(viki): Auto-generated method stub |
| 324 | return false; |
| 325 | } |
| 326 | |
| 327 | @Override |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 328 | public boolean onOptionsItemSelected(MenuItem item) { |
Mindy Pereira | 28d5f72 | 2012-02-15 12:32:40 -0800 | [diff] [blame] | 329 | int id = item.getItemId(); |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 330 | boolean handled = true; |
Mindy Pereira | 28d5f72 | 2012-02-15 12:32:40 -0800 | [diff] [blame] | 331 | switch (id) { |
Mindy Pereira | f5acda4 | 2012-02-15 20:13:59 -0800 | [diff] [blame] | 332 | case android.R.id.home: |
| 333 | onUpPressed(); |
| 334 | break; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 335 | case R.id.compose: |
| 336 | ComposeActivity.compose(mActivity.getActivityContext(), mAccount); |
| 337 | break; |
Mindy Pereira | 28d5f72 | 2012-02-15 12:32:40 -0800 | [diff] [blame] | 338 | case R.id.show_all_folders: |
| 339 | showFolderList(); |
| 340 | break; |
Mindy Pereira | 28e0c34 | 2012-02-17 15:05:13 -0800 | [diff] [blame^] | 341 | case R.id.refresh: |
| 342 | requestFolderRefresh(); |
| 343 | break; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 344 | default: |
| 345 | handled = false; |
| 346 | break; |
Mindy Pereira | 28d5f72 | 2012-02-15 12:32:40 -0800 | [diff] [blame] | 347 | } |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 348 | return handled; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 349 | } |
| 350 | |
Mindy Pereira | 28e0c34 | 2012-02-17 15:05:13 -0800 | [diff] [blame^] | 351 | private void requestFolderRefresh() { |
| 352 | if (mFolder != null) { |
| 353 | new AsyncRefreshTask(mContext, mFolder).execute(); |
| 354 | } |
| 355 | } |
| 356 | |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 357 | @Override |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 358 | public void onPause() { |
Vikram Aggarwal | 7dedb95 | 2012-02-16 16:10:23 -0800 | [diff] [blame] | 359 | isLoaderInitialized = false; |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | @Override |
| 363 | public void onPrepareDialog(int id, Dialog dialog, Bundle bundle) { |
| 364 | // TODO(viki): Auto-generated method stub |
| 365 | |
| 366 | } |
| 367 | |
| 368 | @Override |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 369 | public boolean onPrepareOptionsMenu(Menu menu) { |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 370 | // TODO(viki): Auto-generated method stub |
| 371 | return false; |
| 372 | } |
| 373 | |
| 374 | @Override |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 375 | public void onResume() { |
| 376 | // TODO(viki): Auto-generated method stub |
Vikram Aggarwal | d247dc9 | 2012-02-10 15:49:01 -0800 | [diff] [blame] | 377 | // mBatchConversations.addObserver(this); |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 378 | if (mActionBarView != null) { |
| 379 | mActionBarView.onResume(); |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | @Override |
| 384 | public void onSaveInstanceState(Bundle outState) { |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 385 | if (mConvListContext != null) { |
| 386 | outState.putBundle(SAVED_LIST_CONTEXT, mConvListContext.toBundle()); |
Vikram Aggarwal | 8b15263 | 2012-02-03 14:58:45 -0800 | [diff] [blame] | 387 | } |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | @Override |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 391 | public void onSearchRequested() { |
| 392 | // TODO(viki): Auto-generated method stub |
| 393 | |
| 394 | } |
| 395 | |
| 396 | @Override |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 397 | public void onStartBulkOperation() { |
| 398 | // TODO(viki): Auto-generated method stub |
| 399 | |
| 400 | } |
| 401 | |
| 402 | @Override |
| 403 | public void onStartDragMode() { |
| 404 | // TODO(viki): Auto-generated method stub |
| 405 | |
| 406 | } |
| 407 | |
| 408 | @Override |
| 409 | public void onStop() { |
| 410 | // TODO(viki): Auto-generated method stub |
| 411 | |
| 412 | } |
| 413 | |
| 414 | @Override |
| 415 | public void onStopDragMode() { |
| 416 | // TODO(viki): Auto-generated method stub |
| 417 | |
| 418 | } |
| 419 | |
Vikram Aggarwal | fa131a2 | 2012-02-02 13:56:22 -0800 | [diff] [blame] | 420 | /** |
| 421 | * {@inheritDoc} |
| 422 | * |
| 423 | * Subclasses must override this to listen to mode changes from the ViewMode. Subclasses |
| 424 | * <b>must</b> call the parent's onViewModeChanged since the parent will handle common state |
| 425 | * changes. |
| 426 | */ |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 427 | @Override |
Vikram Aggarwal | fa131a2 | 2012-02-02 13:56:22 -0800 | [diff] [blame] | 428 | public void onViewModeChanged(int newMode) { |
| 429 | // Perform any mode specific work here. |
| 430 | // reset the action bar icon based on the mode. Why don't the individual controllers do |
| 431 | // this themselves? |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 432 | |
Vikram Aggarwal | fa131a2 | 2012-02-02 13:56:22 -0800 | [diff] [blame] | 433 | // On conversation list mode, clean up the conversation. |
| 434 | if (newMode == ViewMode.CONVERSATION_LIST) { |
| 435 | // Clean up the conversation here. |
| 436 | } |
| 437 | |
| 438 | // We don't want to invalidate the options menu when switching to conversation |
| 439 | // mode, as it will happen when the conversation finishes loading. |
| 440 | if (newMode != ViewMode.CONVERSATION) { |
| 441 | mActivity.invalidateOptionsMenu(); |
| 442 | } |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | @Override |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 446 | public void onWindowFocusChanged(boolean hasFocus) { |
| 447 | // TODO(viki): Auto-generated method stub |
| 448 | |
| 449 | } |
| 450 | |
| 451 | @Override |
| 452 | public void reloadSearch(String string) { |
| 453 | // TODO(viki): Auto-generated method stub |
| 454 | |
| 455 | } |
| 456 | |
| 457 | /** |
| 458 | * @param savedState |
| 459 | */ |
| 460 | protected void restoreListContext(Bundle savedState) { |
| 461 | // TODO(viki): Auto-generated method stub |
Vikram Aggarwal | 8b15263 | 2012-02-03 14:58:45 -0800 | [diff] [blame] | 462 | Bundle listContextBundle = savedState.getBundle(SAVED_LIST_CONTEXT); |
| 463 | if (listContextBundle != null) { |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 464 | mConvListContext = ConversationListContext.forBundle(listContextBundle); |
Vikram Aggarwal | 8b15263 | 2012-02-03 14:58:45 -0800 | [diff] [blame] | 465 | } |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | /** |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 469 | * Restore the state from the previous bundle. Subclasses should call this method from the |
| 470 | * parent class, since it performs important UI initialization. |
| 471 | * @param savedState |
| 472 | */ |
| 473 | protected void restoreState(Bundle savedState) { |
| 474 | if (savedState != null) { |
| 475 | restoreListContext(savedState); |
Vikram Aggarwal | 8b15263 | 2012-02-03 14:58:45 -0800 | [diff] [blame] | 476 | // Restore the list context |
| 477 | restoreListContext(savedState); |
| 478 | |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 479 | // Attach the menu handler here. |
Vikram Aggarwal | fa131a2 | 2012-02-02 13:56:22 -0800 | [diff] [blame] | 480 | |
| 481 | // Restore the view mode |
| 482 | mViewMode.handleRestore(savedState); |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 483 | } |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | @Override |
| 487 | public void setSubject(String subject) { |
| 488 | // Do something useful with the subject. This requires changing the |
| 489 | // conversation view's subject text. |
| 490 | } |
| 491 | |
| 492 | @Override |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 493 | public void startActionBarStatusCursorLoader(String account) { |
| 494 | // TODO(viki): Auto-generated method stub |
| 495 | |
| 496 | } |
| 497 | |
| 498 | @Override |
| 499 | public void stopActionBarStatusCursorLoader(String account) { |
Vikram Aggarwal | a55b36c | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 500 | // TODO(viki): Auto-generated method stub |
| 501 | |
| 502 | } |
Vikram Aggarwal | 7d60288 | 2012-02-07 15:01:20 -0800 | [diff] [blame] | 503 | |
| 504 | @Override |
| 505 | public void toggleStar(boolean toggleOn, long conversationId, long maxMessageId) { |
| 506 | // TODO(viki): Auto-generated method stub |
| 507 | |
| 508 | } |
| 509 | |
| 510 | @Override |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 511 | public void onConversationSelected(Conversation conversation) { |
| 512 | mCurrentConversation = conversation; |
| 513 | showConversation(mCurrentConversation); |
| 514 | mViewMode.enterConversationMode(); |
Vikram Aggarwal | 7d60288 | 2012-02-07 15:01:20 -0800 | [diff] [blame] | 515 | } |
Mindy Pereira | 555140c | 2012-02-15 14:55:29 -0800 | [diff] [blame] | 516 | |
| 517 | @Override |
Vikram Aggarwal | 7dedb95 | 2012-02-16 16:10:23 -0800 | [diff] [blame] | 518 | public Loader<Cursor> onCreateLoader(int id, Bundle args) { |
| 519 | // Create a loader to listen in on account changes. |
| 520 | return new CursorLoader(mContext, AccountCacheProvider.getAccountsUri(), |
| 521 | UIProvider.ACCOUNTS_PROJECTION, null, null, null); |
| 522 | } |
| 523 | |
| 524 | /** |
| 525 | * Return whether the given account exists in the cursor. |
| 526 | * @param accountCursor |
| 527 | * @param account |
| 528 | * @return true if the account exists in the account cursor, false otherwise. |
| 529 | */ |
| 530 | private boolean existsInCursor(Cursor accountCursor, Account account) { |
| 531 | accountCursor.moveToFirst(); |
| 532 | do { |
| 533 | if (account.equals(new Account(accountCursor))) |
| 534 | return true; |
| 535 | } while (accountCursor.moveToNext()); |
| 536 | return false; |
| 537 | } |
| 538 | |
| 539 | /** |
| 540 | * Update the accounts on the device. This currently loads the first account in the list. |
| 541 | * @param loader |
| 542 | * @param data |
| 543 | * @return true if the update was successful, false otherwise |
| 544 | */ |
| 545 | private boolean updateAccounts(Loader<Cursor> loader, Cursor accounts) { |
| 546 | // Load the first account in the absence of any other information. |
| 547 | if (accounts == null || !accounts.moveToFirst()) { |
| 548 | return false; |
| 549 | } |
| 550 | mAccount = new Account(accounts); |
| 551 | final Intent intent = mActivity.getIntent(); |
| 552 | mConvListContext = ConversationListContext.forIntent(mContext, mAccount, intent); |
Mindy Pereira | 28e0c34 | 2012-02-17 15:05:13 -0800 | [diff] [blame^] | 553 | mFolder = mConvListContext.mFolder; |
Vikram Aggarwal | 7dedb95 | 2012-02-16 16:10:23 -0800 | [diff] [blame] | 554 | // TODO(viki): Rely on the ViewMode transition to do the right things automatically. The |
| 555 | // next line should be unnecessary. |
| 556 | showConversationList(mConvListContext); |
| 557 | mViewMode.enterConversationListMode(); |
| 558 | return true; |
| 559 | } |
| 560 | |
| 561 | @Override |
| 562 | public void onLoadFinished(Loader<Cursor> loader, Cursor data) { |
| 563 | // We want to reinitialize only if we haven't ever been initialized, or if the current |
| 564 | // account has vanished. |
| 565 | if (!isLoaderInitialized || !existsInCursor(data, mAccount)) { |
| 566 | isLoaderInitialized = updateAccounts(loader, data); |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | @Override |
| 571 | public void onLoaderReset(Loader<Cursor> loader) { |
| 572 | // Do nothing for now, since we don't have any state. When a load is finished, the |
| 573 | // onLoadFinished will be called and we will be fine. |
| 574 | } |
Mindy Pereira | 674afa4 | 2012-02-17 14:05:24 -0800 | [diff] [blame] | 575 | |
| 576 | |
| 577 | @Override |
| 578 | public void onTouchEvent(MotionEvent event) { |
| 579 | if (event.getAction() == MotionEvent.ACTION_DOWN) { |
| 580 | int mode = mViewMode.getMode(); |
| 581 | if (mode == ViewMode.CONVERSATION_LIST) { |
| 582 | mConversationListFragment.onTouchEvent(event); |
| 583 | } else if (mode == ViewMode.CONVERSATION) { |
| 584 | mConversationViewFragment.onTouchEvent(event); |
| 585 | } |
| 586 | } |
| 587 | } |
Vikram Aggarwal | 4a5c530 | 2012-01-12 15:07:13 -0800 | [diff] [blame] | 588 | } |