Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -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 | |
Mark Wei | 2102b2c | 2013-05-02 17:15:30 -0700 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableList; |
| 21 | |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 22 | import android.app.Activity; |
| 23 | import android.app.ListFragment; |
Vikram Aggarwal | 37a20ca | 2013-06-06 11:19:49 -0700 | [diff] [blame^] | 24 | import android.app.LoaderManager; |
Alice Yang | 2032316 | 2013-04-17 10:37:41 -0700 | [diff] [blame] | 25 | import android.content.Context; |
Vikram Aggarwal | 37a20ca | 2013-06-06 11:19:49 -0700 | [diff] [blame^] | 26 | import android.content.Loader; |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 27 | import android.content.res.Resources; |
Mindy Pereira | 6d8e7fe | 2012-07-26 16:02:49 -0700 | [diff] [blame] | 28 | import android.database.DataSetObserver; |
Vikram Aggarwal | 37a20ca | 2013-06-06 11:19:49 -0700 | [diff] [blame^] | 29 | import android.net.Uri; |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 30 | import android.os.Bundle; |
| 31 | import android.os.Handler; |
Alice Yang | 0d74a66 | 2013-03-25 14:01:24 -0700 | [diff] [blame] | 32 | import android.text.format.DateUtils; |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 33 | import android.view.LayoutInflater; |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 34 | import android.view.View; |
| 35 | import android.view.ViewGroup; |
| 36 | import android.view.ViewGroup.MarginLayoutParams; |
| 37 | import android.widget.AdapterView; |
| 38 | import android.widget.AdapterView.OnItemLongClickListener; |
| 39 | import android.widget.ListView; |
| 40 | import android.widget.TextView; |
| 41 | |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 42 | import com.android.mail.ConversationListContext; |
Marc Blank | f362695 | 2012-02-28 17:06:05 -0800 | [diff] [blame] | 43 | import com.android.mail.R; |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 44 | import com.android.mail.browse.ConversationCursor; |
Vikram Aggarwal | d247dc9 | 2012-02-10 15:49:01 -0800 | [diff] [blame] | 45 | import com.android.mail.browse.ConversationItemView; |
Mindy Pereira | 12fe37a | 2012-08-15 10:02:57 -0700 | [diff] [blame] | 46 | import com.android.mail.browse.ConversationItemViewModel; |
Mindy Pereira | 6681f6b | 2012-03-09 13:55:54 -0800 | [diff] [blame] | 47 | import com.android.mail.browse.ConversationListFooterView; |
Alice Yang | 6427314 | 2013-04-10 18:26:56 -0700 | [diff] [blame] | 48 | import com.android.mail.browse.ToggleableItem; |
Vikram Aggarwal | 37a20ca | 2013-06-06 11:19:49 -0700 | [diff] [blame^] | 49 | import com.android.mail.content.ObjectCursor; |
| 50 | import com.android.mail.content.ObjectCursorLoader; |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 51 | import com.android.mail.providers.Account; |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 52 | import com.android.mail.providers.AccountObserver; |
Vikram Aggarwal | d247dc9 | 2012-02-10 15:49:01 -0800 | [diff] [blame] | 53 | import com.android.mail.providers.Conversation; |
Mindy Pereira | 4f166de | 2012-02-14 13:40:58 -0800 | [diff] [blame] | 54 | import com.android.mail.providers.Folder; |
Vikram Aggarwal | 50ff0e5 | 2013-03-14 13:58:02 -0700 | [diff] [blame] | 55 | import com.android.mail.providers.FolderObserver; |
Vikram Aggarwal | 7d81600 | 2012-04-17 17:06:41 -0700 | [diff] [blame] | 56 | import com.android.mail.providers.Settings; |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 57 | import com.android.mail.providers.UIProvider; |
Mindy Pereira | 4e812f4 | 2012-07-30 16:52:49 -0700 | [diff] [blame] | 58 | import com.android.mail.providers.UIProvider.AccountCapabilities; |
Alice Yang | 6427314 | 2013-04-10 18:26:56 -0700 | [diff] [blame] | 59 | import com.android.mail.providers.UIProvider.ConversationListIcon; |
Mindy Pereira | 4e812f4 | 2012-07-30 16:52:49 -0700 | [diff] [blame] | 60 | import com.android.mail.providers.UIProvider.FolderCapabilities; |
mindyp | 77c3e1b | 2012-09-11 14:05:02 -0700 | [diff] [blame] | 61 | import com.android.mail.providers.UIProvider.FolderType; |
Mindy Pereira | e58222b | 2012-07-25 14:33:18 -0700 | [diff] [blame] | 62 | import com.android.mail.providers.UIProvider.Swipe; |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 63 | import com.android.mail.ui.SwipeableListView.ListItemSwipedListener; |
| 64 | import com.android.mail.ui.SwipeableListView.ListItemsRemovedListener; |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 65 | import com.android.mail.ui.ViewMode.ModeChangeListener; |
Paul Westbrook | b334c90 | 2012-06-25 11:42:46 -0700 | [diff] [blame] | 66 | import com.android.mail.utils.LogTag; |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 67 | import com.android.mail.utils.LogUtils; |
Vikram Aggarwal | fa131a2 | 2012-02-02 13:56:22 -0800 | [diff] [blame] | 68 | import com.android.mail.utils.Utils; |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 69 | |
Mindy Pereira | f6a6b50 | 2012-03-15 15:24:26 -0700 | [diff] [blame] | 70 | import java.util.Collection; |
Scott Kennedy | 7c8325d | 2013-02-28 10:46:10 -0800 | [diff] [blame] | 71 | import java.util.List; |
Mindy Pereira | fe06bea | 2012-02-16 08:15:14 -0800 | [diff] [blame] | 72 | |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 73 | /** |
| 74 | * The conversation list UI component. |
| 75 | */ |
Vikram Aggarwal | d247dc9 | 2012-02-10 15:49:01 -0800 | [diff] [blame] | 76 | public final class ConversationListFragment extends ListFragment implements |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 77 | OnItemLongClickListener, ModeChangeListener, ListItemSwipedListener { |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame] | 78 | /** Key used to pass data to {@link ConversationListFragment}. */ |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 79 | private static final String CONVERSATION_LIST_KEY = "conversation-list"; |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame] | 80 | /** Key used to keep track of the scroll state of the list. */ |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 81 | private static final String LIST_STATE_KEY = "list-state"; |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame] | 82 | |
Paul Westbrook | b334c90 | 2012-06-25 11:42:46 -0700 | [diff] [blame] | 83 | private static final String LOG_TAG = LogTag.getLogTag(); |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame] | 84 | /** Key used to save the ListView choice mode, since ListView doesn't save it automatically! */ |
| 85 | private static final String CHOICE_MODE_KEY = "choice-mode-key"; |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 86 | |
Vikram Aggarwal | d7a12cd | 2012-02-03 09:36:20 -0800 | [diff] [blame] | 87 | // True if we are on a tablet device |
| 88 | private static boolean mTabletDevice; |
| 89 | |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 90 | /** |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 91 | * Frequency of update of timestamps. Initialized in |
| 92 | * {@link #onCreate(Bundle)} and final afterwards. |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 93 | */ |
| 94 | private static int TIMESTAMP_UPDATE_INTERVAL = 0; |
| 95 | |
Alice Yang | 0d74a66 | 2013-03-25 14:01:24 -0700 | [diff] [blame] | 96 | private static long NO_NEW_MESSAGE_DURATION = 1 * DateUtils.SECOND_IN_MILLIS; |
| 97 | |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 98 | private ControllableActivity mActivity; |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 99 | |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 100 | // Control state. |
| 101 | private ConversationListCallbacks mCallbacks; |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 102 | |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 103 | private final Handler mHandler = new Handler(); |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 104 | |
Alice Yang | 486e63e | 2013-04-05 13:01:50 -0700 | [diff] [blame] | 105 | private ConversationListView mConversationListView; |
| 106 | |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 107 | // The internal view objects. |
Mindy Pereira | f6a6b50 | 2012-03-15 15:24:26 -0700 | [diff] [blame] | 108 | private SwipeableListView mListView; |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 109 | |
| 110 | private TextView mSearchResultCountTextView; |
| 111 | private TextView mSearchStatusTextView; |
| 112 | |
| 113 | private View mSearchStatusView; |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 114 | |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 115 | /** |
| 116 | * Current Account being viewed |
| 117 | */ |
Vikram Aggarwal | d247dc9 | 2012-02-10 15:49:01 -0800 | [diff] [blame] | 118 | private Account mAccount; |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 119 | /** |
Mindy Pereira | 30fd47b | 2012-03-09 09:24:00 -0800 | [diff] [blame] | 120 | * Current folder being viewed. |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 121 | */ |
Mindy Pereira | 4f166de | 2012-02-14 13:40:58 -0800 | [diff] [blame] | 122 | private Folder mFolder; |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 123 | |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 124 | /** |
| 125 | * A simple method to update the timestamps of conversations periodically. |
| 126 | */ |
| 127 | private Runnable mUpdateTimestampsRunnable = null; |
| 128 | |
| 129 | private ConversationListContext mViewContext; |
| 130 | |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 131 | private AnimatedAdapter mListAdapter; |
Vikram Aggarwal | 7d60288 | 2012-02-07 15:01:20 -0800 | [diff] [blame] | 132 | |
Mindy Pereira | 6681f6b | 2012-03-09 13:55:54 -0800 | [diff] [blame] | 133 | private ConversationListFooterView mFooterView; |
Mindy Pereira | 6cc9553 | 2012-08-15 16:04:56 -0700 | [diff] [blame] | 134 | private View mEmptyView; |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 135 | private ErrorListener mErrorListener; |
Vikram Aggarwal | 50ff0e5 | 2013-03-14 13:58:02 -0700 | [diff] [blame] | 136 | private FolderObserver mFolderObserver; |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame] | 137 | private DataSetObserver mConversationCursorObserver; |
Vikram Aggarwal | d247dc9 | 2012-02-10 15:49:01 -0800 | [diff] [blame] | 138 | |
Vikram Aggarwal | 4f1cc0b | 2012-08-02 15:16:30 -0700 | [diff] [blame] | 139 | private ConversationSelectionSet mSelectedSet; |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 140 | private final AccountObserver mAccountObserver = new AccountObserver() { |
| 141 | @Override |
| 142 | public void onChanged(Account newAccount) { |
| 143 | mAccount = newAccount; |
| 144 | setSwipeAction(); |
| 145 | } |
| 146 | }; |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 147 | private ConversationUpdater mUpdater; |
Vikram Aggarwal | 81a4f08 | 2012-09-28 09:19:04 -0700 | [diff] [blame] | 148 | /** Hash of the Conversation Cursor we last obtained from the controller. */ |
| 149 | private int mConversationCursorHash; |
Vikram Aggarwal | 4f1cc0b | 2012-08-02 15:16:30 -0700 | [diff] [blame] | 150 | |
Vikram Aggarwal | 6c51158 | 2012-02-27 10:59:47 -0800 | [diff] [blame] | 151 | /** |
Vikram Aggarwal | 37a20ca | 2013-06-06 11:19:49 -0700 | [diff] [blame^] | 152 | * If the current list is for a folder with children, this set of loader callbacks will |
| 153 | * create a loader for all the child folders, and will return an {@link ObjectCursor} over the |
| 154 | * list. |
| 155 | */ |
| 156 | private final class ChildFolderLoads |
| 157 | implements LoaderManager.LoaderCallbacks<ObjectCursor<Folder>> { |
| 158 | /** Load all child folders for the current folder. */ |
| 159 | private static final int LOADER_CHIDREN = 0; |
| 160 | public static final String CHILD_URI = "arg-child-uri"; |
| 161 | private final String[] projection = UIProvider.FOLDERS_PROJECTION; |
| 162 | |
| 163 | @Override |
| 164 | public Loader<ObjectCursor<Folder>> onCreateLoader(int id, Bundle args) { |
| 165 | if (id != LOADER_CHIDREN) { |
| 166 | throw new IllegalStateException("ChildFolderLoads loading ID=" + id); |
| 167 | } |
| 168 | final Uri childUri = Uri.parse(args.getString(CHILD_URI)); |
| 169 | return new ObjectCursorLoader<Folder>( |
| 170 | getActivity(), childUri, projection, Folder.FACTORY); |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public void onLoadFinished(Loader<ObjectCursor<Folder>> loader, ObjectCursor<Folder> data) { |
| 175 | if (data != null && data.getCount() >= 0 && mListAdapter != null) { |
| 176 | mListAdapter.updateNestedFolders(data); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public void onLoaderReset(Loader<ObjectCursor<Folder>> loader) { |
| 182 | // Do nothing. |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | /** Callbacks to handle creating a loader and receiving child folders from it. */ |
| 187 | private final ChildFolderLoads mChildCallback = new ChildFolderLoads(); |
| 188 | |
| 189 | /** |
| 190 | * Include all the folders at the cursor provided here in the conversation list. |
| 191 | * @param cursor The cursor containing child folders for the current folder. |
| 192 | */ |
| 193 | private void showChildFolders(ObjectCursor<Folder> cursor) { |
| 194 | |
| 195 | } |
| 196 | |
| 197 | /** |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 198 | * Constructor needs to be public to handle orientation changes and activity |
| 199 | * lifecycle events. |
Vikram Aggarwal | 6c51158 | 2012-02-27 10:59:47 -0800 | [diff] [blame] | 200 | */ |
Paul Westbrook | efc9f12 | 2012-02-21 11:14:49 -0800 | [diff] [blame] | 201 | public ConversationListFragment() { |
Vikram Aggarwal | d247dc9 | 2012-02-10 15:49:01 -0800 | [diff] [blame] | 202 | super(); |
Vikram Aggarwal | d247dc9 | 2012-02-10 15:49:01 -0800 | [diff] [blame] | 203 | } |
| 204 | |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame] | 205 | private class ConversationCursorObserver extends DataSetObserver { |
Mindy Pereira | 70a70c9 | 2012-08-02 08:39:45 -0700 | [diff] [blame] | 206 | @Override |
| 207 | public void onChanged() { |
Mindy Pereira | 70a70c9 | 2012-08-02 08:39:45 -0700 | [diff] [blame] | 208 | onConversationListStatusUpdated(); |
| 209 | } |
| 210 | } |
| 211 | |
Vikram Aggarwal | 7d60288 | 2012-02-07 15:01:20 -0800 | [diff] [blame] | 212 | /** |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 213 | * Creates a new instance of {@link ConversationListFragment}, initialized |
| 214 | * to display conversation list context. |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 215 | */ |
| 216 | public static ConversationListFragment newInstance(ConversationListContext viewContext) { |
| 217 | ConversationListFragment fragment = new ConversationListFragment(); |
| 218 | Bundle args = new Bundle(); |
| 219 | args.putBundle(CONVERSATION_LIST_KEY, viewContext.toBundle()); |
| 220 | fragment.setArguments(args); |
| 221 | return fragment; |
| 222 | } |
| 223 | |
| 224 | /** |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 225 | * Show the header if the current conversation list is showing search |
| 226 | * results. |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 227 | */ |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 228 | void configureSearchResultHeader() { |
Mindy Pereira | 755fd6e | 2012-03-21 15:15:44 -0700 | [diff] [blame] | 229 | if (mActivity == null) { |
| 230 | return; |
| 231 | } |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 232 | // Only show the header if the context is for a search result |
| 233 | final Resources res = getResources(); |
Vikram Aggarwal | ae4ea99 | 2012-08-07 09:56:02 -0700 | [diff] [blame] | 234 | final boolean showHeader = ConversationListContext.isSearchResult(mViewContext); |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 235 | // TODO(viki): This code contains intimate understanding of the view. |
| 236 | // Much of this logic |
| 237 | // needs to reside in a separate class that handles the text view in |
| 238 | // isolation. Then, |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 239 | // that logic can be reused in other fragments. |
| 240 | if (showHeader) { |
| 241 | mSearchStatusTextView.setText(res.getString(R.string.search_results_searching_header)); |
| 242 | // Initially reset the count |
| 243 | mSearchResultCountTextView.setText(""); |
| 244 | } |
| 245 | mSearchStatusView.setVisibility(showHeader ? View.VISIBLE : View.GONE); |
| 246 | int marginTop = showHeader ? (int) res.getDimension(R.dimen.notification_view_height) : 0; |
| 247 | MarginLayoutParams layoutParams = (MarginLayoutParams) mListView.getLayoutParams(); |
| 248 | layoutParams.topMargin = marginTop; |
| 249 | mListView.setLayoutParams(layoutParams); |
| 250 | } |
| 251 | |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 252 | /** |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 253 | * Show the header if the current conversation list is showing search |
| 254 | * results. |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 255 | */ |
| 256 | private void updateSearchResultHeader(int count) { |
Mindy Pereira | 71a8f29 | 2012-07-26 16:30:48 -0700 | [diff] [blame] | 257 | if (mActivity == null) { |
| 258 | return; |
| 259 | } |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 260 | // Only show the header if the context is for a search result |
| 261 | final Resources res = getResources(); |
Vikram Aggarwal | ae4ea99 | 2012-08-07 09:56:02 -0700 | [diff] [blame] | 262 | final boolean showHeader = ConversationListContext.isSearchResult(mViewContext); |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 263 | if (showHeader) { |
| 264 | mSearchStatusTextView.setText(res.getString(R.string.search_results_header)); |
| 265 | mSearchResultCountTextView |
| 266 | .setText(res.getString(R.string.search_results_loaded, count)); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | /** |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 271 | * Initializes all internal state for a rendering. |
| 272 | */ |
| 273 | private void initializeUiForFirstDisplay() { |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 274 | // TODO(mindyp): find some way to make the notification container more |
| 275 | // re-usable. |
| 276 | // TODO(viki): refactor according to comment in |
| 277 | // configureSearchResultHandler() |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 278 | mSearchStatusView = mActivity.findViewById(R.id.search_status_view); |
| 279 | mSearchStatusTextView = (TextView) mActivity.findViewById(R.id.search_status_text_view); |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 280 | mSearchResultCountTextView = (TextView) mActivity |
| 281 | .findViewById(R.id.search_result_count_view); |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 282 | } |
| 283 | |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 284 | @Override |
Vikram Aggarwal | 0509bba | 2013-01-29 17:11:30 -0800 | [diff] [blame] | 285 | public void onActivityCreated(Bundle savedState) { |
| 286 | super.onActivityCreated(savedState); |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 287 | // Strictly speaking, we get back an android.app.Activity from |
| 288 | // getActivity. However, the |
| 289 | // only activity creating a ConversationListContext is a MailActivity |
| 290 | // which is of type |
| 291 | // ControllableActivity, so this cast should be safe. If this cast |
| 292 | // fails, some other |
| 293 | // activity is creating ConversationListFragments. This activity must be |
| 294 | // of type |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 295 | // ControllableActivity. |
| 296 | final Activity activity = getActivity(); |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 297 | if (!(activity instanceof ControllableActivity)) { |
| 298 | LogUtils.e(LOG_TAG, "ConversationListFragment expects only a ControllableActivity to" |
| 299 | + "create it. Cannot proceed."); |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 300 | } |
| 301 | mActivity = (ControllableActivity) activity; |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 302 | // Since we now have a controllable activity, load the account from it, |
| 303 | // and register for |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 304 | // future account changes. |
| 305 | mAccount = mAccountObserver.initialize(mActivity.getAccountController()); |
Vikram Aggarwal | 7d60288 | 2012-02-07 15:01:20 -0800 | [diff] [blame] | 306 | mCallbacks = mActivity.getListHandler(); |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 307 | mErrorListener = mActivity.getErrorListener(); |
Mindy Pereira | 278fd22 | 2012-07-26 15:23:10 -0700 | [diff] [blame] | 308 | // Start off with the current state of the folder being viewed. |
Alice Yang | 2032316 | 2013-04-17 10:37:41 -0700 | [diff] [blame] | 309 | Context activityContext = mActivity.getActivityContext(); |
Mindy Pereira | 6681f6b | 2012-03-09 13:55:54 -0800 | [diff] [blame] | 310 | mFooterView = (ConversationListFooterView) LayoutInflater.from( |
Alice Yang | 2032316 | 2013-04-17 10:37:41 -0700 | [diff] [blame] | 311 | activityContext).inflate(R.layout.conversation_list_footer_view, |
Mindy Pereira | 6681f6b | 2012-03-09 13:55:54 -0800 | [diff] [blame] | 312 | null); |
Paul Westbrook | 4969e0c | 2012-08-20 14:38:39 -0700 | [diff] [blame] | 313 | mFooterView.setClickListener(mActivity); |
Alice Yang | c36fa72 | 2013-05-05 01:23:46 -0700 | [diff] [blame] | 314 | mConversationListView.setActivity(mActivity); |
Vikram Aggarwal | 81a4f08 | 2012-09-28 09:19:04 -0700 | [diff] [blame] | 315 | final ConversationCursor conversationCursor = getConversationListCursor(); |
Vikram Aggarwal | 37a20ca | 2013-06-06 11:19:49 -0700 | [diff] [blame^] | 316 | final LoaderManager manager = getLoaderManager(); |
| 317 | |
| 318 | // If this a parent folder, load all the child folders. |
| 319 | if (mViewContext.folder.hasChildren) { |
| 320 | final Uri childUri = mViewContext.folder.childFoldersListUri; |
| 321 | final Bundle args = new Bundle(); |
| 322 | args.putString(ChildFolderLoads.CHILD_URI, childUri.toString()); |
| 323 | manager.initLoader(ChildFolderLoads.LOADER_CHIDREN, args, mChildCallback); |
| 324 | } |
Scott Kennedy | 7c8325d | 2013-02-28 10:46:10 -0800 | [diff] [blame] | 325 | |
| 326 | final ConversationListHelper helper = mActivity.getConversationListHelper(); |
Mark Wei | 2102b2c | 2013-05-02 17:15:30 -0700 | [diff] [blame] | 327 | final List<ConversationSpecialItemView> specialItemViews = helper != null ? |
| 328 | ImmutableList.copyOf(helper.makeConversationListSpecialViews( |
Vikram Aggarwal | 37a20ca | 2013-06-06 11:19:49 -0700 | [diff] [blame^] | 329 | activity, mAccount, mActivity.getFolderSelector())) |
Mark Wei | 2102b2c | 2013-05-02 17:15:30 -0700 | [diff] [blame] | 330 | : null; |
Scott Kennedy | 7c8325d | 2013-02-28 10:46:10 -0800 | [diff] [blame] | 331 | if (specialItemViews != null) { |
| 332 | // Attach to the LoaderManager |
| 333 | for (final ConversationSpecialItemView view : specialItemViews) { |
Vikram Aggarwal | 37a20ca | 2013-06-06 11:19:49 -0700 | [diff] [blame^] | 334 | view.bindLoaderManager(manager); |
Scott Kennedy | 7c8325d | 2013-02-28 10:46:10 -0800 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | |
| 338 | mListAdapter = new AnimatedAdapter(mActivity.getApplicationContext(), conversationCursor, |
Vikram Aggarwal | 37a20ca | 2013-06-06 11:19:49 -0700 | [diff] [blame^] | 339 | mActivity.getSelectedSet(), mActivity, mListView, specialItemViews, null); |
Mindy Pereira | 6681f6b | 2012-03-09 13:55:54 -0800 | [diff] [blame] | 340 | mListAdapter.addFooter(mFooterView); |
Mindy Pereira | 6f4a6af | 2012-02-29 11:48:52 -0800 | [diff] [blame] | 341 | mListView.setAdapter(mListAdapter); |
Vikram Aggarwal | 4f1cc0b | 2012-08-02 15:16:30 -0700 | [diff] [blame] | 342 | mSelectedSet = mActivity.getSelectedSet(); |
| 343 | mListView.setSelectionSet(mSelectedSet); |
Vikram Aggarwal | 37a20ca | 2013-06-06 11:19:49 -0700 | [diff] [blame^] | 344 | mListAdapter.setFooterVisibility(false); |
Vikram Aggarwal | 50ff0e5 | 2013-03-14 13:58:02 -0700 | [diff] [blame] | 345 | mFolderObserver = new FolderObserver(){ |
| 346 | @Override |
| 347 | public void onChanged(Folder newFolder) { |
| 348 | onFolderUpdated(newFolder); |
| 349 | } |
| 350 | }; |
| 351 | mFolderObserver.initialize(mActivity.getFolderController()); |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame] | 352 | mConversationCursorObserver = new ConversationCursorObserver(); |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 353 | mUpdater = mActivity.getConversationUpdater(); |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame] | 354 | mUpdater.registerConversationListObserver(mConversationCursorObserver); |
Vikram Aggarwal | bcb16b9 | 2013-01-28 18:05:03 -0800 | [diff] [blame] | 355 | mTabletDevice = Utils.useTabletUI(mActivity.getApplicationContext().getResources()); |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 356 | initializeUiForFirstDisplay(); |
Mindy Pereira | dc8963b | 2012-03-28 17:51:05 -0700 | [diff] [blame] | 357 | configureSearchResultHeader(); |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 358 | // The onViewModeChanged callback doesn't get called when the mode |
| 359 | // object is created, so |
Vikram Aggarwal | fa131a2 | 2012-02-02 13:56:22 -0800 | [diff] [blame] | 360 | // force setting the mode manually this time around. |
Mindy Pereira | f96ec32 | 2012-03-02 14:06:33 -0800 | [diff] [blame] | 361 | onViewModeChanged(mActivity.getViewMode().getMode()); |
Mindy Pereira | b5901be | 2012-08-09 17:21:53 -0700 | [diff] [blame] | 362 | mActivity.getViewMode().addListener(this); |
Paul Westbrook | 0e3fd9d | 2012-04-20 02:02:23 -0700 | [diff] [blame] | 363 | |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 364 | if (mActivity.isFinishing()) { |
| 365 | // Activity is finishing, just bail. |
| 366 | return; |
| 367 | } |
Vikram Aggarwal | 81a4f08 | 2012-09-28 09:19:04 -0700 | [diff] [blame] | 368 | mConversationCursorHash = (conversationCursor == null) ? 0 : conversationCursor.hashCode(); |
| 369 | // Belt and suspenders here; make sure we do any necessary sync of the |
| 370 | // ConversationCursor |
| 371 | if (conversationCursor != null && conversationCursor.isRefreshReady()) { |
| 372 | conversationCursor.sync(); |
| 373 | } |
| 374 | |
Vikram Aggarwal | 0509bba | 2013-01-29 17:11:30 -0800 | [diff] [blame] | 375 | // On a phone we never highlight a conversation, so the default is to select none. |
| 376 | // On a tablet, we highlight a SINGLE conversation in landscape conversation view. |
| 377 | int choice = getDefaultChoiceMode(mTabletDevice); |
| 378 | if (savedState != null) { |
| 379 | // Restore the choice mode if it was set earlier, or NONE if creating a fresh view. |
| 380 | // Choice mode here represents the current conversation only. CAB mode does not rely on |
| 381 | // the platform: checked state is a local variable {@link ConversationItemView#mChecked} |
| 382 | choice = savedState.getInt(CHOICE_MODE_KEY, choice); |
| 383 | if (savedState.containsKey(LIST_STATE_KEY)) { |
| 384 | // TODO: find a better way to unset the selected item when restoring |
| 385 | mListView.clearChoices(); |
| 386 | } |
| 387 | } |
| 388 | setChoiceMode(choice); |
| 389 | |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 390 | // Show list and start loading list. |
| 391 | showList(); |
Mindy Pereira | 4765c5c | 2012-07-19 11:58:22 -0700 | [diff] [blame] | 392 | ToastBarOperation pendingOp = mActivity.getPendingToastOperation(); |
| 393 | if (pendingOp != null) { |
| 394 | // Clear the pending operation |
| 395 | mActivity.setPendingToastOperation(null); |
| 396 | mActivity.onUndoAvailable(pendingOp); |
| 397 | } |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 398 | } |
| 399 | |
Vikram Aggarwal | 0509bba | 2013-01-29 17:11:30 -0800 | [diff] [blame] | 400 | /** |
| 401 | * Returns the default choice mode for the list based on whether the list is displayed on tablet |
| 402 | * or not. |
| 403 | * @param isTablet |
| 404 | * @return |
| 405 | */ |
| 406 | private final static int getDefaultChoiceMode(boolean isTablet) { |
| 407 | return isTablet ? ListView.CHOICE_MODE_SINGLE : ListView.CHOICE_MODE_NONE; |
| 408 | } |
| 409 | |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 410 | public AnimatedAdapter getAnimatedAdapter() { |
| 411 | return mListAdapter; |
| 412 | } |
| 413 | |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 414 | @Override |
Vikram Aggarwal | d247dc9 | 2012-02-10 15:49:01 -0800 | [diff] [blame] | 415 | public void onCreate(Bundle savedState) { |
Vikram Aggarwal | d247dc9 | 2012-02-10 15:49:01 -0800 | [diff] [blame] | 416 | super.onCreate(savedState); |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 417 | |
| 418 | // Initialize fragment constants from resources |
Vikram Aggarwal | 6c51158 | 2012-02-27 10:59:47 -0800 | [diff] [blame] | 419 | final Resources res = getResources(); |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 420 | TIMESTAMP_UPDATE_INTERVAL = res.getInteger(R.integer.timestamp_update_interval); |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 421 | mUpdateTimestampsRunnable = new Runnable() { |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 422 | @Override |
| 423 | public void run() { |
| 424 | mListView.invalidateViews(); |
| 425 | mHandler.postDelayed(mUpdateTimestampsRunnable, TIMESTAMP_UPDATE_INTERVAL); |
| 426 | } |
| 427 | }; |
| 428 | |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 429 | // Get the context from the arguments |
Vikram Aggarwal | 6c51158 | 2012-02-27 10:59:47 -0800 | [diff] [blame] | 430 | final Bundle args = getArguments(); |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 431 | mViewContext = ConversationListContext.forBundle(args.getBundle(CONVERSATION_LIST_KEY)); |
Mindy Pereira | 3982e23 | 2012-02-29 15:00:34 -0800 | [diff] [blame] | 432 | mAccount = mViewContext.account; |
Paul Westbrook | 80ecd89 | 2012-08-16 13:37:39 -0700 | [diff] [blame] | 433 | |
Vikram Aggarwal | 9a49c9b | 2012-08-31 10:49:33 -0700 | [diff] [blame] | 434 | setRetainInstance(false); |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | @Override |
Andy Huang | 9e4ca79 | 2013-02-28 14:33:43 -0800 | [diff] [blame] | 438 | public String toString() { |
| 439 | final String s = super.toString(); |
| 440 | if (mViewContext == null) { |
| 441 | return s; |
| 442 | } |
Andy Huang | c1922a9 | 2013-05-13 14:33:05 -0700 | [diff] [blame] | 443 | final StringBuilder sb = new StringBuilder(s); |
| 444 | sb.setLength(sb.length() - 1); |
| 445 | sb.append(" mListAdapter="); |
| 446 | sb.append(mListAdapter); |
| 447 | sb.append(" folder="); |
| 448 | sb.append(mViewContext.folder); |
| 449 | sb.append("}"); |
| 450 | return sb.toString(); |
Andy Huang | 9e4ca79 | 2013-02-28 14:33:43 -0800 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | @Override |
Vikram Aggarwal | 9a49c9b | 2012-08-31 10:49:33 -0700 | [diff] [blame] | 454 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState) { |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 455 | View rootView = inflater.inflate(R.layout.conversation_list, null); |
Mindy Pereira | 6cc9553 | 2012-08-15 16:04:56 -0700 | [diff] [blame] | 456 | mEmptyView = rootView.findViewById(R.id.empty_view); |
Alice Yang | 486e63e | 2013-04-05 13:01:50 -0700 | [diff] [blame] | 457 | mConversationListView = |
| 458 | (ConversationListView) rootView.findViewById(R.id.conversation_list); |
Alice Yang | 2032316 | 2013-04-17 10:37:41 -0700 | [diff] [blame] | 459 | mConversationListView.setConversationContext(mViewContext); |
Mindy Pereira | f6a6b50 | 2012-03-15 15:24:26 -0700 | [diff] [blame] | 460 | mListView = (SwipeableListView) rootView.findViewById(android.R.id.list); |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 461 | mListView.setHeaderDividersEnabled(false); |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 462 | mListView.setOnItemLongClickListener(this); |
Mindy Pereira | 4e812f4 | 2012-07-30 16:52:49 -0700 | [diff] [blame] | 463 | mListView.enableSwipe(mAccount.supportsCapability(AccountCapabilities.UNDO)); |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 464 | mListView.setSwipedListener(this); |
Marc Blank | 2af0cf3 | 2012-03-01 19:24:13 -0800 | [diff] [blame] | 465 | |
Vikram Aggarwal | 0509bba | 2013-01-29 17:11:30 -0800 | [diff] [blame] | 466 | if (savedState != null && savedState.containsKey(LIST_STATE_KEY)) { |
| 467 | mListView.onRestoreInstanceState(savedState.getParcelable(LIST_STATE_KEY)); |
Vikram Aggarwal | 9a49c9b | 2012-08-31 10:49:33 -0700 | [diff] [blame] | 468 | } |
Alice Yang | 0d74a66 | 2013-03-25 14:01:24 -0700 | [diff] [blame] | 469 | |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 470 | return rootView; |
| 471 | } |
| 472 | |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame] | 473 | /** |
| 474 | * Sets the choice mode of the list view |
| 475 | * @param choiceMode ListView# |
| 476 | */ |
| 477 | private final void setChoiceMode(int choiceMode) { |
| 478 | mListView.setChoiceMode(choiceMode); |
| 479 | } |
| 480 | |
| 481 | /** |
| 482 | * Tell the list to select nothing. |
| 483 | */ |
| 484 | public final void setChoiceNone() { |
Vikram Aggarwal | 0509bba | 2013-01-29 17:11:30 -0800 | [diff] [blame] | 485 | // On a phone, the default choice mode is already none, so nothing to do. |
| 486 | if (!mTabletDevice) { |
| 487 | return; |
| 488 | } |
Andy Huang | b0be3fc | 2013-05-02 15:48:05 -0700 | [diff] [blame] | 489 | clearChoicesAndActivated(); |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame] | 490 | setChoiceMode(ListView.CHOICE_MODE_NONE); |
| 491 | } |
| 492 | |
| 493 | /** |
| 494 | * Tell the list to get out of selecting none. |
| 495 | */ |
| 496 | public final void revertChoiceMode() { |
Vikram Aggarwal | 0509bba | 2013-01-29 17:11:30 -0800 | [diff] [blame] | 497 | // On a phone, the default choice mode is always none, so nothing to do. |
| 498 | if (!mTabletDevice) { |
| 499 | return; |
| 500 | } |
| 501 | setChoiceMode(getDefaultChoiceMode(mTabletDevice)); |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame] | 502 | } |
| 503 | |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 504 | @Override |
Andy Huang | 7517e3b | 2012-08-20 12:30:08 -0700 | [diff] [blame] | 505 | public void onDestroy() { |
Andy Huang | 7517e3b | 2012-08-20 12:30:08 -0700 | [diff] [blame] | 506 | super.onDestroy(); |
| 507 | } |
| 508 | |
| 509 | @Override |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 510 | public void onDestroyView() { |
Andy Huang | 7517e3b | 2012-08-20 12:30:08 -0700 | [diff] [blame] | 511 | |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 512 | // Clear the list's adapter |
| 513 | mListAdapter.destroy(); |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 514 | mListView.setAdapter(null); |
| 515 | |
Vikram Aggarwal | d7a12cd | 2012-02-03 09:36:20 -0800 | [diff] [blame] | 516 | mActivity.unsetViewModeListener(this); |
Mindy Pereira | 71a8f29 | 2012-07-26 16:30:48 -0700 | [diff] [blame] | 517 | if (mFolderObserver != null) { |
Vikram Aggarwal | 50ff0e5 | 2013-03-14 13:58:02 -0700 | [diff] [blame] | 518 | mFolderObserver.unregisterAndDestroy(); |
Mindy Pereira | 71a8f29 | 2012-07-26 16:30:48 -0700 | [diff] [blame] | 519 | mFolderObserver = null; |
| 520 | } |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame] | 521 | if (mConversationCursorObserver != null) { |
| 522 | mUpdater.unregisterConversationListObserver(mConversationCursorObserver); |
| 523 | mConversationCursorObserver = null; |
Mindy Pereira | 70a70c9 | 2012-08-02 08:39:45 -0700 | [diff] [blame] | 524 | } |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 525 | mAccountObserver.unregisterAndDestroy(); |
Scott Kennedy | 7c8325d | 2013-02-28 10:46:10 -0800 | [diff] [blame] | 526 | getAnimatedAdapter().cleanup(); |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 527 | super.onDestroyView(); |
| 528 | } |
| 529 | |
Vikram Aggarwal | 9730ea0 | 2012-08-02 12:46:19 -0700 | [diff] [blame] | 530 | /** |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 531 | * There are three binary variables, which determine what we do with a |
| 532 | * message. checkbEnabled: Whether check boxes are enabled or not (forced |
| 533 | * true on tablet) cabModeOn: Whether CAB mode is currently on or not. |
| 534 | * pressType: long or short tap (There is a third possibility: phone or |
| 535 | * tablet, but they have <em>identical</em> behavior) The matrix of |
| 536 | * possibilities is: |
| 537 | * <p> |
| 538 | * Long tap: Always toggle selection of conversation. If CAB mode is not |
| 539 | * started, then start it. |
Vikram Aggarwal | 9730ea0 | 2012-08-02 12:46:19 -0700 | [diff] [blame] | 540 | * <pre> |
| 541 | * | Checkboxes | No Checkboxes |
| 542 | * ----------+------------+--------------- |
| 543 | * CAB mode | Select | Select |
| 544 | * List mode | Select | Select |
| 545 | * |
Vikram Aggarwal | 9730ea0 | 2012-08-02 12:46:19 -0700 | [diff] [blame] | 546 | * </pre> |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 547 | * |
Vikram Aggarwal | 9730ea0 | 2012-08-02 12:46:19 -0700 | [diff] [blame] | 548 | * Reference: http://b/issue?id=6392199 |
| 549 | * <p> |
| 550 | * {@inheritDoc} |
| 551 | */ |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 552 | @Override |
| 553 | public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { |
Mindy Pereira | 1e231a6 | 2012-03-23 12:42:15 -0700 | [diff] [blame] | 554 | // Ignore anything that is not a conversation item. Could be a footer. |
| 555 | if (!(view instanceof ConversationItemView)) { |
Scott Kennedy | 7c8325d | 2013-02-28 10:46:10 -0800 | [diff] [blame] | 556 | return false; |
Mindy Pereira | 1e231a6 | 2012-03-23 12:42:15 -0700 | [diff] [blame] | 557 | } |
Alice Yang | 4758e98 | 2013-04-24 20:31:34 -0700 | [diff] [blame] | 558 | ((ConversationItemView) view).toggleSelectedStateOrBeginDrag(); |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 559 | return true; |
| 560 | } |
| 561 | |
Vikram Aggarwal | 4f1cc0b | 2012-08-02 15:16:30 -0700 | [diff] [blame] | 562 | /** |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 563 | * See the comment for |
| 564 | * {@link #onItemLongClick(AdapterView, View, int, long)}. |
| 565 | * <p> |
| 566 | * Short tap behavior: |
| 567 | * |
Vikram Aggarwal | 4f1cc0b | 2012-08-02 15:16:30 -0700 | [diff] [blame] | 568 | * <pre> |
| 569 | * | Checkboxes | No Checkboxes |
| 570 | * ----------+------------+--------------- |
| 571 | * CAB mode | Peek | Select |
| 572 | * List mode | Peek | Peek |
| 573 | * </pre> |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 574 | * |
Vikram Aggarwal | 4f1cc0b | 2012-08-02 15:16:30 -0700 | [diff] [blame] | 575 | * Reference: http://b/issue?id=6392199 |
| 576 | * <p> |
| 577 | * {@inheritDoc} |
| 578 | */ |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 579 | @Override |
Vikram Aggarwal | 54452ae | 2012-03-13 15:29:00 -0700 | [diff] [blame] | 580 | public void onListItemClick(ListView l, View view, int position, long id) { |
Vikram Aggarwal | 37a20ca | 2013-06-06 11:19:49 -0700 | [diff] [blame^] | 581 | if (view instanceof NestedFolderView) { |
| 582 | final FolderSelector selector = mActivity.getFolderSelector(); |
| 583 | selector.onFolderSelected(((NestedFolderView) view).getFolder()); |
| 584 | } else if (view instanceof ToggleableItem) { |
| 585 | final boolean showSenderImage = |
| 586 | (mAccount.settings.convListIcon == ConversationListIcon.SENDER_IMAGE); |
| 587 | if (!showSenderImage && !mSelectedSet.isEmpty()) { |
| 588 | ((ToggleableItem) view).toggleSelectedState(); |
| 589 | } else { |
| 590 | viewConversation(position); |
| 591 | } |
Vikram Aggarwal | 4f1cc0b | 2012-08-02 15:16:30 -0700 | [diff] [blame] | 592 | } else { |
Vikram Aggarwal | 37a20ca | 2013-06-06 11:19:49 -0700 | [diff] [blame^] | 593 | // Ignore anything that is not a conversation item. Could be a footer. |
| 594 | // If we are using a keyboard, the highlighted item is the parent; |
| 595 | // otherwise, this is a direct call from the ConverationItemView |
| 596 | return; |
Vikram Aggarwal | 4f1cc0b | 2012-08-02 15:16:30 -0700 | [diff] [blame] | 597 | } |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 598 | // When a new list item is clicked, commit any existing leave behind |
mindyp | 8694fe9 | 2012-09-25 11:07:16 -0700 | [diff] [blame] | 599 | // items. Wait until we have opened the desired conversation to cause |
| 600 | // any position changes. |
Vikram Aggarwal | bcb16b9 | 2013-01-28 18:05:03 -0800 | [diff] [blame] | 601 | commitDestructiveActions(Utils.useTabletUI(mActivity.getActivityContext().getResources())); |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | @Override |
Scott Kennedy | 08a079c | 2013-02-22 10:13:10 -0800 | [diff] [blame] | 605 | public void onResume() { |
| 606 | super.onResume(); |
| 607 | |
| 608 | final ConversationCursor conversationCursor = getConversationListCursor(); |
| 609 | if (conversationCursor != null) { |
| 610 | conversationCursor.handleNotificationActions(); |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | @Override |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 615 | public void onPause() { |
| 616 | super.onPause(); |
| 617 | } |
| 618 | |
| 619 | @Override |
| 620 | public void onSaveInstanceState(Bundle outState) { |
| 621 | super.onSaveInstanceState(outState); |
| 622 | if (mListView != null) { |
| 623 | outState.putParcelable(LIST_STATE_KEY, mListView.onSaveInstanceState()); |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame] | 624 | outState.putInt(CHOICE_MODE_KEY, mListView.getChoiceMode()); |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 625 | } |
| 626 | } |
| 627 | |
| 628 | @Override |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 629 | public void onStart() { |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 630 | super.onStart(); |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 631 | mHandler.postDelayed(mUpdateTimestampsRunnable, TIMESTAMP_UPDATE_INTERVAL); |
| 632 | } |
| 633 | |
| 634 | @Override |
| 635 | public void onStop() { |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 636 | super.onStop(); |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 637 | mHandler.removeCallbacks(mUpdateTimestampsRunnable); |
| 638 | } |
| 639 | |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 640 | @Override |
Vikram Aggarwal | fa131a2 | 2012-02-02 13:56:22 -0800 | [diff] [blame] | 641 | public void onViewModeChanged(int newMode) { |
Vikram Aggarwal | fa131a2 | 2012-02-02 13:56:22 -0800 | [diff] [blame] | 642 | if (mTabletDevice) { |
Andy Huang | b0be3fc | 2013-05-02 15:48:05 -0700 | [diff] [blame] | 643 | if (ViewMode.isListMode(newMode)) { |
Vikram Aggarwal | 0509bba | 2013-01-29 17:11:30 -0800 | [diff] [blame] | 644 | // There are no selected conversations when in conversation list mode. |
Andy Huang | b0be3fc | 2013-05-02 15:48:05 -0700 | [diff] [blame] | 645 | clearChoicesAndActivated(); |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 646 | } |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 647 | } |
Mindy Pereira | b5901be | 2012-08-09 17:21:53 -0700 | [diff] [blame] | 648 | if (mFooterView != null) { |
| 649 | mFooterView.onViewModeChanged(newMode); |
| 650 | } |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 651 | } |
Mindy Pereira | b5901be | 2012-08-09 17:21:53 -0700 | [diff] [blame] | 652 | |
Andy Huang | b0be3fc | 2013-05-02 15:48:05 -0700 | [diff] [blame] | 653 | private void clearChoicesAndActivated() { |
| 654 | final int currentSelected = mListView.getCheckedItemPosition(); |
| 655 | mListView.clearChoices(); |
| 656 | // We use the activated state to show the blue highlight on tablet. Clearing the choices |
| 657 | // removes the checked state, but doesn't do anything to the activated state. We must |
| 658 | // manually clear that. |
| 659 | if (currentSelected != ListView.INVALID_POSITION) { |
| 660 | final View v = mListView.getChildAt(currentSelected); |
| 661 | if (v != null) { |
| 662 | v.setActivated(false); |
| 663 | } |
| 664 | } |
| 665 | } |
| 666 | |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 667 | /** |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 668 | * Handles a request to show a new conversation list, either from a search |
| 669 | * query or for viewing a folder. This will initiate a data load, and hence |
| 670 | * must be called on the UI thread. |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 671 | */ |
| 672 | private void showList() { |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 673 | mListView.setEmptyView(null); |
Andy Huang | 7591d2f | 2012-07-26 16:48:06 -0700 | [diff] [blame] | 674 | onFolderUpdated(mActivity.getFolderController().getFolder()); |
mindyp | ca8ca2d | 2012-09-11 17:38:34 -0700 | [diff] [blame] | 675 | onConversationListStatusUpdated(); |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 676 | } |
| 677 | |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 678 | /** |
| 679 | * View the message at the given position. |
Scott Kennedy | 7c8325d | 2013-02-28 10:46:10 -0800 | [diff] [blame] | 680 | * |
| 681 | * @param position The position of the conversation in the list (as opposed to its position in |
| 682 | * the cursor) |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 683 | */ |
Scott Kennedy | 7c8325d | 2013-02-28 10:46:10 -0800 | [diff] [blame] | 684 | protected void viewConversation(final int position) { |
Vikram Aggarwal | c769422 | 2012-04-23 13:37:01 -0700 | [diff] [blame] | 685 | LogUtils.d(LOG_TAG, "ConversationListFragment.viewConversation(%d)", position); |
Scott Kennedy | 7c8325d | 2013-02-28 10:46:10 -0800 | [diff] [blame] | 686 | |
| 687 | final ConversationCursor cursor = |
| 688 | (ConversationCursor) getAnimatedAdapter().getItem(position); |
Andy Huang | 97b1096 | 2013-04-02 13:33:32 -0700 | [diff] [blame] | 689 | |
| 690 | if (cursor == null) { |
| 691 | LogUtils.e(LOG_TAG, |
| 692 | "unable to open conv at cursor pos=%s cursor=%s getPositionOffset=%s", |
| 693 | position, cursor, getAnimatedAdapter().getPositionOffset(position)); |
| 694 | return; |
| 695 | } |
| 696 | |
Scott Kennedy | 7c8325d | 2013-02-28 10:46:10 -0800 | [diff] [blame] | 697 | final Conversation conv = cursor.getConversation(); |
| 698 | /* |
| 699 | * The cursor position may be different than the position method parameter because of |
| 700 | * special views in the list. |
| 701 | */ |
| 702 | conv.position = cursor.getPosition(); |
| 703 | setSelected(conv.position, true); |
| 704 | mCallbacks.onConversationSelected(conv, false /* inLoaderCallbacks */); |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 705 | } |
| 706 | |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame] | 707 | /** |
| 708 | * Sets the selected conversation to the position given here. |
Alice Yang | 0d74a66 | 2013-03-25 14:01:24 -0700 | [diff] [blame] | 709 | * @param cursorPosition The position of the conversation in the cursor (as opposed to |
| 710 | * in the list) |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame] | 711 | * @param different if the currently selected conversation is different from the one provided |
| 712 | * here. This is a difference in conversations, not a difference in positions. For example, a |
| 713 | * conversation at position 2 can move to position 4 as a result of new mail. |
| 714 | */ |
Scott Kennedy | 7c8325d | 2013-02-28 10:46:10 -0800 | [diff] [blame] | 715 | public void setSelected(final int cursorPosition, boolean different) { |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame] | 716 | if (mListView.getChoiceMode() == ListView.CHOICE_MODE_NONE) { |
| 717 | return; |
| 718 | } |
Scott Kennedy | 7c8325d | 2013-02-28 10:46:10 -0800 | [diff] [blame] | 719 | |
| 720 | final int position = |
| 721 | cursorPosition + getAnimatedAdapter().getPositionOffset(cursorPosition); |
| 722 | |
mindyp | e21f886 | 2012-10-01 09:32:03 -0700 | [diff] [blame] | 723 | if (different) { |
| 724 | mListView.smoothScrollToPosition(position); |
| 725 | } |
| 726 | mListView.setItemChecked(position, true); |
| 727 | } |
| 728 | |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 729 | /** |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame] | 730 | * Returns the cursor associated with the conversation list. |
| 731 | * @return |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 732 | */ |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 733 | private ConversationCursor getConversationListCursor() { |
| 734 | return mCallbacks != null ? mCallbacks.getConversationListCursor() : null; |
Mindy Pereira | fe06bea | 2012-02-16 08:15:14 -0800 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | /** |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 738 | * Request a refresh of the list. No sync is carried out and none is |
| 739 | * promised. |
Vikram Aggarwal | 54452ae | 2012-03-13 15:29:00 -0700 | [diff] [blame] | 740 | */ |
| 741 | public void requestListRefresh() { |
| 742 | mListAdapter.notifyDataSetChanged(); |
| 743 | } |
| 744 | |
Vikram Aggarwal | 75daee5 | 2012-04-30 11:13:09 -0700 | [diff] [blame] | 745 | /** |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 746 | * Change the UI to delete the conversations provided and then call the |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 747 | * {@link DestructiveAction} provided here <b>after</b> the UI has been |
| 748 | * updated. |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 749 | * @param conversations |
Vikram Aggarwal | 75daee5 | 2012-04-30 11:13:09 -0700 | [diff] [blame] | 750 | * @param action |
| 751 | */ |
Vikram Aggarwal | a8e4318 | 2012-09-13 12:55:10 -0700 | [diff] [blame] | 752 | public void requestDelete(int actionId, final Collection<Conversation> conversations, |
Vikram Aggarwal | 669947b | 2013-01-10 17:05:56 -0800 | [diff] [blame] | 753 | final DestructiveAction action) { |
Mindy Pereira | acf6039 | 2012-04-06 09:11:00 -0700 | [diff] [blame] | 754 | for (Conversation conv : conversations) { |
| 755 | conv.localDeleteOnUpdate = true; |
| 756 | } |
Vikram Aggarwal | a8e4318 | 2012-09-13 12:55:10 -0700 | [diff] [blame] | 757 | final ListItemsRemovedListener listener = new ListItemsRemovedListener() { |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 758 | @Override |
| 759 | public void onListItemsRemoved() { |
| 760 | action.performAction(); |
| 761 | } |
Vikram Aggarwal | a8e4318 | 2012-09-13 12:55:10 -0700 | [diff] [blame] | 762 | }; |
| 763 | final SwipeableListView listView = (SwipeableListView) getListView(); |
| 764 | if (listView.getSwipeAction() == actionId) { |
Paul Westbrook | cec3e0b | 2013-02-04 22:50:22 -0800 | [diff] [blame] | 765 | if (!listView.destroyItems(conversations, listener)) { |
| 766 | // The listView failed to destroy the items, perform the action manually |
| 767 | LogUtils.e(LOG_TAG, "ConversationListFragment.requestDelete: " + |
| 768 | "listView failed to destroy items."); |
| 769 | action.performAction(); |
| 770 | } |
Vikram Aggarwal | a8e4318 | 2012-09-13 12:55:10 -0700 | [diff] [blame] | 771 | return; |
| 772 | } |
| 773 | // Delete the local delete items (all for now) and when done, |
| 774 | // update... |
| 775 | mListAdapter.delete(conversations, listener); |
Mindy Pereira | acf6039 | 2012-04-06 09:11:00 -0700 | [diff] [blame] | 776 | } |
Mindy Pereira | a3911aa | 2012-03-09 13:26:26 -0800 | [diff] [blame] | 777 | |
Mindy Pereira | 11dd5ef | 2012-03-10 15:10:18 -0800 | [diff] [blame] | 778 | public void onFolderUpdated(Folder folder) { |
Mindy Pereira | dc8963b | 2012-03-28 17:51:05 -0700 | [diff] [blame] | 779 | mFolder = folder; |
Mindy Pereira | 06642fa | 2012-07-12 16:23:27 -0700 | [diff] [blame] | 780 | setSwipeAction(); |
Mindy Pereira | 82cea48 | 2012-03-27 16:45:00 -0700 | [diff] [blame] | 781 | if (mFolder == null) { |
| 782 | return; |
| 783 | } |
Mindy Pereira | 4584a0d | 2012-03-13 14:42:14 -0700 | [diff] [blame] | 784 | mListAdapter.setFolder(mFolder); |
Mindy Pereira | 70a70c9 | 2012-08-02 08:39:45 -0700 | [diff] [blame] | 785 | mFooterView.setFolder(mFolder); |
Vikram Aggarwal | 41b9e8f | 2012-09-25 10:15:04 -0700 | [diff] [blame] | 786 | if (!mFolder.wasSyncSuccessful()) { |
Mindy Pereira | 70a70c9 | 2012-08-02 08:39:45 -0700 | [diff] [blame] | 787 | mErrorListener.onError(mFolder, false); |
| 788 | } |
Paul Westbrook | c01f5d9 | 2012-09-25 17:22:25 -0700 | [diff] [blame] | 789 | |
| 790 | // Notify of changes to the Folder. |
| 791 | onFolderStatusUpdated(); |
| 792 | |
Mindy Pereira | 12fe37a | 2012-08-15 10:02:57 -0700 | [diff] [blame] | 793 | // Blow away conversation items cache. |
| 794 | ConversationItemViewModel.onFolderUpdated(mFolder); |
Mindy Pereira | 70a70c9 | 2012-08-02 08:39:45 -0700 | [diff] [blame] | 795 | } |
| 796 | |
Vikram Aggarwal | 81a4f08 | 2012-09-28 09:19:04 -0700 | [diff] [blame] | 797 | /** |
| 798 | * Updates the footer visibility and updates the conversation cursor |
| 799 | */ |
Mindy Pereira | 70a70c9 | 2012-08-02 08:39:45 -0700 | [diff] [blame] | 800 | public void onConversationListStatusUpdated() { |
Paul Westbrook | 9a70e91 | 2012-08-17 15:53:20 -0700 | [diff] [blame] | 801 | final ConversationCursor cursor = getConversationListCursor(); |
| 802 | final boolean showFooter = cursor != null && mFooterView.updateStatus(cursor); |
Paul Westbrook | c01f5d9 | 2012-09-25 17:22:25 -0700 | [diff] [blame] | 803 | // Update the folder status, in case the cursor could affect it. |
| 804 | onFolderStatusUpdated(); |
| 805 | mListAdapter.setFooterVisibility(showFooter); |
| 806 | |
| 807 | // Also change the cursor here. |
| 808 | onCursorUpdated(); |
| 809 | } |
| 810 | |
| 811 | private void onFolderStatusUpdated() { |
Alice Yang | 0d74a66 | 2013-03-25 14:01:24 -0700 | [diff] [blame] | 812 | // Update the sync status bar with sync results if needed |
Alice Yang | 486e63e | 2013-04-05 13:01:50 -0700 | [diff] [blame] | 813 | checkSyncStatus(); |
Alice Yang | 0d74a66 | 2013-03-25 14:01:24 -0700 | [diff] [blame] | 814 | |
Paul Westbrook | c01f5d9 | 2012-09-25 17:22:25 -0700 | [diff] [blame] | 815 | final ConversationCursor cursor = getConversationListCursor(); |
Paul Westbrook | 9a70e91 | 2012-08-17 15:53:20 -0700 | [diff] [blame] | 816 | Bundle extras = cursor != null ? cursor.getExtras() : Bundle.EMPTY; |
Andrew Sapperstein | 427df9d | 2013-05-15 12:05:09 -0700 | [diff] [blame] | 817 | int errorStatus = extras.containsKey(UIProvider.CursorExtraKeys.EXTRA_ERROR) ? |
Mindy Pereira | 70a70c9 | 2012-08-02 08:39:45 -0700 | [diff] [blame] | 818 | extras.getInt(UIProvider.CursorExtraKeys.EXTRA_ERROR) |
| 819 | : UIProvider.LastSyncResult.SUCCESS; |
Andrew Sapperstein | 427df9d | 2013-05-15 12:05:09 -0700 | [diff] [blame] | 820 | int cursorStatus = extras.getInt(UIProvider.CursorExtraKeys.EXTRA_STATUS); |
Alice Yang | 0d74a66 | 2013-03-25 14:01:24 -0700 | [diff] [blame] | 821 | // We want to update the UI with this information if either we are loaded or complete, or |
Paul Westbrook | c01f5d9 | 2012-09-25 17:22:25 -0700 | [diff] [blame] | 822 | // we have a folder with a non-0 count. |
| 823 | final int folderCount = mFolder != null ? mFolder.totalCount : 0; |
Andrew Sapperstein | 427df9d | 2013-05-15 12:05:09 -0700 | [diff] [blame] | 824 | if (errorStatus == UIProvider.LastSyncResult.SUCCESS |
| 825 | && (cursorStatus == UIProvider.CursorStatus.LOADED |
| 826 | || cursorStatus == UIProvider.CursorStatus.COMPLETE) || folderCount > 0) { |
Paul Westbrook | c01f5d9 | 2012-09-25 17:22:25 -0700 | [diff] [blame] | 827 | updateSearchResultHeader(folderCount); |
| 828 | if (folderCount == 0) { |
Mindy Pereira | 6cc9553 | 2012-08-15 16:04:56 -0700 | [diff] [blame] | 829 | mListView.setEmptyView(mEmptyView); |
| 830 | } |
Mindy Pereira | 70a70c9 | 2012-08-02 08:39:45 -0700 | [diff] [blame] | 831 | } |
Mindy Pereira | a3911aa | 2012-03-09 13:26:26 -0800 | [diff] [blame] | 832 | } |
Mindy Pereira | f6a6b50 | 2012-03-15 15:24:26 -0700 | [diff] [blame] | 833 | |
Mindy Pereira | 06642fa | 2012-07-12 16:23:27 -0700 | [diff] [blame] | 834 | private void setSwipeAction() { |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 835 | int swipeSetting = Settings.getSwipeSetting(mAccount.settings); |
Mindy Pereira | e58222b | 2012-07-25 14:33:18 -0700 | [diff] [blame] | 836 | if (swipeSetting == Swipe.DISABLED |
Mindy Pereira | b807337 | 2012-08-09 14:00:10 -0700 | [diff] [blame] | 837 | || !mAccount.supportsCapability(AccountCapabilities.UNDO) |
| 838 | || (mFolder != null && mFolder.isTrash())) { |
Mindy Pereira | e58222b | 2012-07-25 14:33:18 -0700 | [diff] [blame] | 839 | mListView.enableSwipe(false); |
| 840 | } else { |
| 841 | int action; |
Rohan Shah | 7f98d0d | 2013-02-15 13:21:09 -0800 | [diff] [blame] | 842 | mListView.enableSwipe(true); |
mindyp | 77c3e1b | 2012-09-11 14:05:02 -0700 | [diff] [blame] | 843 | if (ConversationListContext.isSearchResult(mViewContext) |
Scott Kennedy | 8c1058e | 2013-03-20 13:40:20 -0700 | [diff] [blame] | 844 | || (mFolder != null && mFolder.isType(FolderType.SPAM))) { |
Mindy Pereira | e58222b | 2012-07-25 14:33:18 -0700 | [diff] [blame] | 845 | action = R.id.delete; |
Mindy Pereira | 4e812f4 | 2012-07-30 16:52:49 -0700 | [diff] [blame] | 846 | } else if (mFolder == null) { |
Mindy Pereira | 01f3050 | 2012-08-14 10:30:51 -0700 | [diff] [blame] | 847 | action = R.id.remove_folder; |
Mindy Pereira | 4e812f4 | 2012-07-30 16:52:49 -0700 | [diff] [blame] | 848 | } else { |
| 849 | // We have enough information to respect user settings. |
| 850 | switch (swipeSetting) { |
| 851 | case Swipe.ARCHIVE: |
| 852 | if (mAccount.supportsCapability(AccountCapabilities.ARCHIVE)) { |
| 853 | if (mFolder.supportsCapability(FolderCapabilities.ARCHIVE)) { |
| 854 | action = R.id.archive; |
| 855 | break; |
| 856 | } else if (mFolder.supportsCapability |
| 857 | (FolderCapabilities.CAN_ACCEPT_MOVED_MESSAGES)) { |
Mindy Pereira | 01f3050 | 2012-08-14 10:30:51 -0700 | [diff] [blame] | 858 | action = R.id.remove_folder; |
Mindy Pereira | 4e812f4 | 2012-07-30 16:52:49 -0700 | [diff] [blame] | 859 | break; |
| 860 | } |
| 861 | } |
| 862 | case Swipe.DELETE: |
| 863 | default: |
| 864 | action = R.id.delete; |
| 865 | break; |
| 866 | } |
Mindy Pereira | e58222b | 2012-07-25 14:33:18 -0700 | [diff] [blame] | 867 | } |
| 868 | mListView.setSwipeAction(action); |
| 869 | } |
Mark Wei | 6126d72 | 2013-04-24 21:09:43 -0700 | [diff] [blame] | 870 | mListView.setCurrentAccount(mAccount); |
Mindy Pereira | 06642fa | 2012-07-12 16:23:27 -0700 | [diff] [blame] | 871 | mListView.setCurrentFolder(mFolder); |
| 872 | } |
| 873 | |
Vikram Aggarwal | 17f373e | 2012-09-17 15:49:32 -0700 | [diff] [blame] | 874 | /** |
| 875 | * Changes the conversation cursor in the list and sets selected position if none is set. |
| 876 | */ |
| 877 | private void onCursorUpdated() { |
| 878 | if (mCallbacks == null || mListAdapter == null) { |
| 879 | return; |
| 880 | } |
Vikram Aggarwal | 81a4f08 | 2012-09-28 09:19:04 -0700 | [diff] [blame] | 881 | // Check against the previous cursor here and see if they are the same. If they are, then |
| 882 | // do a notifyDataSetChanged. |
| 883 | final ConversationCursor newCursor = mCallbacks.getConversationListCursor(); |
| 884 | mListAdapter.swapCursor(newCursor); |
| 885 | // When the conversation cursor is *updated*, we get back the same instance. In that |
| 886 | // situation, CursorAdapter.swapCursor() silently returns, without forcing a |
| 887 | // notifyDataSetChanged(). So let's force a call to notifyDataSetChanged, since an updated |
| 888 | // cursor means that the dataset has changed. |
| 889 | final int newCursorHash = (newCursor == null) ? 0 : newCursor.hashCode(); |
| 890 | if (mConversationCursorHash == newCursorHash && mConversationCursorHash != 0) { |
| 891 | mListAdapter.notifyDataSetChanged(); |
| 892 | } |
| 893 | mConversationCursorHash = newCursorHash; |
Scott Kennedy | b1e2148 | 2013-03-15 13:40:38 -0700 | [diff] [blame] | 894 | |
Scott Kennedy | 4b9b395 | 2013-03-20 17:20:59 -0700 | [diff] [blame] | 895 | if (newCursor != null && newCursor.getCount() > 0) { |
Scott Kennedy | b1e2148 | 2013-03-15 13:40:38 -0700 | [diff] [blame] | 896 | newCursor.markContentsSeen(); |
| 897 | } |
| 898 | |
Vikram Aggarwal | 17f373e | 2012-09-17 15:49:32 -0700 | [diff] [blame] | 899 | // If a current conversation is available, and none is selected in the list, then ask |
| 900 | // the list to select the current conversation. |
| 901 | final Conversation conv = mCallbacks.getCurrentConversation(); |
| 902 | if (conv == null) { |
| 903 | return; |
| 904 | } |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame] | 905 | if (mListView.getChoiceMode() != ListView.CHOICE_MODE_NONE |
| 906 | && mListView.getCheckedItemPosition() == -1) { |
| 907 | setSelected(conv.position, true); |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 908 | } |
Mindy Pereira | 21ab490 | 2012-03-19 18:48:03 -0700 | [diff] [blame] | 909 | } |
Mindy Pereira | 1e2573b | 2012-04-17 14:34:36 -0700 | [diff] [blame] | 910 | |
mindyp | c6adce3 | 2012-08-22 18:46:42 -0700 | [diff] [blame] | 911 | public void commitDestructiveActions(boolean animate) { |
Mindy Pereira | 8937bf1 | 2012-07-23 14:05:02 -0700 | [diff] [blame] | 912 | if (mListView != null) { |
mindyp | c6adce3 | 2012-08-22 18:46:42 -0700 | [diff] [blame] | 913 | mListView.commitDestructiveActions(animate); |
Vikram Aggarwal | 7d81600 | 2012-04-17 17:06:41 -0700 | [diff] [blame] | 914 | |
| 915 | } |
| 916 | } |
| 917 | |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 918 | @Override |
| 919 | public void onListItemSwiped(Collection<Conversation> conversations) { |
| 920 | mUpdater.showNextConversation(conversations); |
| 921 | } |
Alice Yang | 0d74a66 | 2013-03-25 14:01:24 -0700 | [diff] [blame] | 922 | |
Alice Yang | 486e63e | 2013-04-05 13:01:50 -0700 | [diff] [blame] | 923 | private void checkSyncStatus() { |
Alice Yang | 76d2065 | 2013-04-24 02:32:48 -0700 | [diff] [blame] | 924 | if (mFolder != null && mFolder.isSyncInProgress()) { |
Alice Yang | 03752f3 | 2013-05-05 15:05:16 -0700 | [diff] [blame] | 925 | LogUtils.d(LOG_TAG, "CLF.checkSyncStatus still syncing"); |
Alice Yang | 486e63e | 2013-04-05 13:01:50 -0700 | [diff] [blame] | 926 | // Still syncing, ignore |
| 927 | } else { |
| 928 | // Finished syncing: |
Alice Yang | 03752f3 | 2013-05-05 15:05:16 -0700 | [diff] [blame] | 929 | LogUtils.d(LOG_TAG, "CLF.checkSyncStatus done syncing"); |
Alice Yang | 486e63e | 2013-04-05 13:01:50 -0700 | [diff] [blame] | 930 | mConversationListView.onSyncFinished(); |
| 931 | } |
| 932 | } |
| 933 | |
Alice Yang | 0d74a66 | 2013-03-25 14:01:24 -0700 | [diff] [blame] | 934 | /** |
Alice Yang | 486e63e | 2013-04-05 13:01:50 -0700 | [diff] [blame] | 935 | * Displays the indefinite progress bar indicating a sync is in progress. This |
| 936 | * should only be called if user manually requested a sync, and not for background syncs. |
Alice Yang | 0d74a66 | 2013-03-25 14:01:24 -0700 | [diff] [blame] | 937 | */ |
| 938 | protected void showSyncStatusBar() { |
Alice Yang | 486e63e | 2013-04-05 13:01:50 -0700 | [diff] [blame] | 939 | mConversationListView.showSyncStatusBar(); |
Alice Yang | 0d74a66 | 2013-03-25 14:01:24 -0700 | [diff] [blame] | 940 | } |
Rohan Shah | d4f2287 | 2013-04-19 17:06:37 -0700 | [diff] [blame] | 941 | |
| 942 | /** |
| 943 | * Clears all items in the list. |
| 944 | */ |
| 945 | public void clear() { |
| 946 | mListView.setAdapter(null); |
| 947 | } |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 948 | } |