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 | |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 20 | import android.app.Activity; |
| 21 | import android.app.ListFragment; |
| 22 | import android.content.res.Resources; |
Mindy Pereira | 6d8e7fe | 2012-07-26 16:02:49 -0700 | [diff] [blame] | 23 | import android.database.DataSetObserver; |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 24 | import android.os.Bundle; |
| 25 | import android.os.Handler; |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 26 | import android.view.LayoutInflater; |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 27 | import android.view.View; |
| 28 | import android.view.ViewGroup; |
| 29 | import android.view.ViewGroup.MarginLayoutParams; |
| 30 | import android.widget.AdapterView; |
| 31 | import android.widget.AdapterView.OnItemLongClickListener; |
| 32 | import android.widget.ListView; |
| 33 | import android.widget.TextView; |
| 34 | |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 35 | import com.android.mail.ConversationListContext; |
Marc Blank | f362695 | 2012-02-28 17:06:05 -0800 | [diff] [blame] | 36 | import com.android.mail.R; |
mindyp | c03b05a | 2012-09-28 14:23:48 -0700 | [diff] [blame] | 37 | import com.android.mail.browse.ToggleableItem; |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 38 | import com.android.mail.browse.ConversationCursor; |
Vikram Aggarwal | d247dc9 | 2012-02-10 15:49:01 -0800 | [diff] [blame] | 39 | import com.android.mail.browse.ConversationItemView; |
Mindy Pereira | 12fe37a | 2012-08-15 10:02:57 -0700 | [diff] [blame] | 40 | import com.android.mail.browse.ConversationItemViewModel; |
Mindy Pereira | 6681f6b | 2012-03-09 13:55:54 -0800 | [diff] [blame] | 41 | import com.android.mail.browse.ConversationListFooterView; |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 42 | import com.android.mail.providers.Account; |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 43 | import com.android.mail.providers.AccountObserver; |
Vikram Aggarwal | d247dc9 | 2012-02-10 15:49:01 -0800 | [diff] [blame] | 44 | import com.android.mail.providers.Conversation; |
Mindy Pereira | 4f166de | 2012-02-14 13:40:58 -0800 | [diff] [blame] | 45 | import com.android.mail.providers.Folder; |
Vikram Aggarwal | 7d81600 | 2012-04-17 17:06:41 -0700 | [diff] [blame] | 46 | import com.android.mail.providers.Settings; |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 47 | import com.android.mail.providers.UIProvider; |
Mindy Pereira | 4e812f4 | 2012-07-30 16:52:49 -0700 | [diff] [blame] | 48 | import com.android.mail.providers.UIProvider.AccountCapabilities; |
| 49 | import com.android.mail.providers.UIProvider.FolderCapabilities; |
mindyp | 77c3e1b | 2012-09-11 14:05:02 -0700 | [diff] [blame] | 50 | import com.android.mail.providers.UIProvider.FolderType; |
Mindy Pereira | e58222b | 2012-07-25 14:33:18 -0700 | [diff] [blame] | 51 | import com.android.mail.providers.UIProvider.Swipe; |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 52 | import com.android.mail.ui.SwipeableListView.ListItemSwipedListener; |
| 53 | import com.android.mail.ui.SwipeableListView.ListItemsRemovedListener; |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 54 | import com.android.mail.ui.ViewMode.ModeChangeListener; |
Paul Westbrook | b334c90 | 2012-06-25 11:42:46 -0700 | [diff] [blame] | 55 | import com.android.mail.utils.LogTag; |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 56 | import com.android.mail.utils.LogUtils; |
Vikram Aggarwal | fa131a2 | 2012-02-02 13:56:22 -0800 | [diff] [blame] | 57 | import com.android.mail.utils.Utils; |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 58 | |
Mindy Pereira | f6a6b50 | 2012-03-15 15:24:26 -0700 | [diff] [blame] | 59 | import java.util.Collection; |
Mindy Pereira | fe06bea | 2012-02-16 08:15:14 -0800 | [diff] [blame] | 60 | |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 61 | /** |
| 62 | * The conversation list UI component. |
| 63 | */ |
Vikram Aggarwal | d247dc9 | 2012-02-10 15:49:01 -0800 | [diff] [blame] | 64 | public final class ConversationListFragment extends ListFragment implements |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 65 | OnItemLongClickListener, ModeChangeListener, ListItemSwipedListener { |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame] | 66 | /** Key used to pass data to {@link ConversationListFragment}. */ |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 67 | private static final String CONVERSATION_LIST_KEY = "conversation-list"; |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame] | 68 | /** Key used to keep track of the scroll state of the list. */ |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 69 | private static final String LIST_STATE_KEY = "list-state"; |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame] | 70 | |
Paul Westbrook | b334c90 | 2012-06-25 11:42:46 -0700 | [diff] [blame] | 71 | private static final String LOG_TAG = LogTag.getLogTag(); |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame^] | 72 | /** Key used to save the ListView choice mode, since ListView doesn't save it automatically! */ |
| 73 | private static final String CHOICE_MODE_KEY = "choice-mode-key"; |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 74 | |
Vikram Aggarwal | d7a12cd | 2012-02-03 09:36:20 -0800 | [diff] [blame] | 75 | // True if we are on a tablet device |
| 76 | private static boolean mTabletDevice; |
| 77 | |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 78 | /** |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 79 | * Frequency of update of timestamps. Initialized in |
| 80 | * {@link #onCreate(Bundle)} and final afterwards. |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 81 | */ |
| 82 | private static int TIMESTAMP_UPDATE_INTERVAL = 0; |
| 83 | |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 84 | private ControllableActivity mActivity; |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 85 | |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 86 | // Control state. |
| 87 | private ConversationListCallbacks mCallbacks; |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 88 | |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 89 | private final Handler mHandler = new Handler(); |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 90 | |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 91 | // The internal view objects. |
Mindy Pereira | f6a6b50 | 2012-03-15 15:24:26 -0700 | [diff] [blame] | 92 | private SwipeableListView mListView; |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 93 | |
| 94 | private TextView mSearchResultCountTextView; |
| 95 | private TextView mSearchStatusTextView; |
| 96 | |
| 97 | private View mSearchStatusView; |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 98 | |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 99 | /** |
| 100 | * Current Account being viewed |
| 101 | */ |
Vikram Aggarwal | d247dc9 | 2012-02-10 15:49:01 -0800 | [diff] [blame] | 102 | private Account mAccount; |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 103 | /** |
Mindy Pereira | 30fd47b | 2012-03-09 09:24:00 -0800 | [diff] [blame] | 104 | * Current folder being viewed. |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 105 | */ |
Mindy Pereira | 4f166de | 2012-02-14 13:40:58 -0800 | [diff] [blame] | 106 | private Folder mFolder; |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 107 | |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 108 | /** |
| 109 | * A simple method to update the timestamps of conversations periodically. |
| 110 | */ |
| 111 | private Runnable mUpdateTimestampsRunnable = null; |
| 112 | |
| 113 | private ConversationListContext mViewContext; |
| 114 | |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 115 | private AnimatedAdapter mListAdapter; |
Vikram Aggarwal | 7d60288 | 2012-02-07 15:01:20 -0800 | [diff] [blame] | 116 | |
Mindy Pereira | 6681f6b | 2012-03-09 13:55:54 -0800 | [diff] [blame] | 117 | private ConversationListFooterView mFooterView; |
Mindy Pereira | 6cc9553 | 2012-08-15 16:04:56 -0700 | [diff] [blame] | 118 | private View mEmptyView; |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 119 | private ErrorListener mErrorListener; |
Mindy Pereira | e3c7b0b | 2012-07-26 16:28:34 -0700 | [diff] [blame] | 120 | private DataSetObserver mFolderObserver; |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame^] | 121 | private DataSetObserver mConversationCursorObserver; |
Vikram Aggarwal | d247dc9 | 2012-02-10 15:49:01 -0800 | [diff] [blame] | 122 | |
Vikram Aggarwal | 4f1cc0b | 2012-08-02 15:16:30 -0700 | [diff] [blame] | 123 | private ConversationSelectionSet mSelectedSet; |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 124 | private final AccountObserver mAccountObserver = new AccountObserver() { |
| 125 | @Override |
| 126 | public void onChanged(Account newAccount) { |
| 127 | mAccount = newAccount; |
| 128 | setSwipeAction(); |
| 129 | } |
| 130 | }; |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 131 | private ConversationUpdater mUpdater; |
Vikram Aggarwal | 81a4f08 | 2012-09-28 09:19:04 -0700 | [diff] [blame] | 132 | /** Hash of the Conversation Cursor we last obtained from the controller. */ |
| 133 | private int mConversationCursorHash; |
Vikram Aggarwal | 4f1cc0b | 2012-08-02 15:16:30 -0700 | [diff] [blame] | 134 | |
Vikram Aggarwal | 6c51158 | 2012-02-27 10:59:47 -0800 | [diff] [blame] | 135 | /** |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 136 | * Constructor needs to be public to handle orientation changes and activity |
| 137 | * lifecycle events. |
Vikram Aggarwal | 6c51158 | 2012-02-27 10:59:47 -0800 | [diff] [blame] | 138 | */ |
Paul Westbrook | efc9f12 | 2012-02-21 11:14:49 -0800 | [diff] [blame] | 139 | public ConversationListFragment() { |
Vikram Aggarwal | d247dc9 | 2012-02-10 15:49:01 -0800 | [diff] [blame] | 140 | super(); |
Vikram Aggarwal | d247dc9 | 2012-02-10 15:49:01 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Mindy Pereira | 6d8e7fe | 2012-07-26 16:02:49 -0700 | [diff] [blame] | 143 | // update the pager title strip as the Folder's conversation count changes |
| 144 | private class FolderObserver extends DataSetObserver { |
| 145 | @Override |
| 146 | public void onChanged() { |
Vikram Aggarwal | 81a4f08 | 2012-09-28 09:19:04 -0700 | [diff] [blame] | 147 | if (mActivity == null) { |
| 148 | return; |
| 149 | } |
| 150 | final FolderController controller = mActivity.getFolderController(); |
| 151 | if (controller == null) { |
| 152 | return; |
| 153 | } |
| 154 | onFolderUpdated(controller.getFolder()); |
Mindy Pereira | 6d8e7fe | 2012-07-26 16:02:49 -0700 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame^] | 158 | private class ConversationCursorObserver extends DataSetObserver { |
Mindy Pereira | 70a70c9 | 2012-08-02 08:39:45 -0700 | [diff] [blame] | 159 | @Override |
| 160 | public void onChanged() { |
Mindy Pereira | 70a70c9 | 2012-08-02 08:39:45 -0700 | [diff] [blame] | 161 | onConversationListStatusUpdated(); |
| 162 | } |
| 163 | } |
| 164 | |
Vikram Aggarwal | 7d60288 | 2012-02-07 15:01:20 -0800 | [diff] [blame] | 165 | /** |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 166 | * Creates a new instance of {@link ConversationListFragment}, initialized |
| 167 | * to display conversation list context. |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 168 | */ |
| 169 | public static ConversationListFragment newInstance(ConversationListContext viewContext) { |
| 170 | ConversationListFragment fragment = new ConversationListFragment(); |
| 171 | Bundle args = new Bundle(); |
| 172 | args.putBundle(CONVERSATION_LIST_KEY, viewContext.toBundle()); |
| 173 | fragment.setArguments(args); |
| 174 | return fragment; |
| 175 | } |
| 176 | |
| 177 | /** |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 178 | * Show the header if the current conversation list is showing search |
| 179 | * results. |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 180 | */ |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 181 | void configureSearchResultHeader() { |
Mindy Pereira | 755fd6e | 2012-03-21 15:15:44 -0700 | [diff] [blame] | 182 | if (mActivity == null) { |
| 183 | return; |
| 184 | } |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 185 | // Only show the header if the context is for a search result |
| 186 | final Resources res = getResources(); |
Vikram Aggarwal | ae4ea99 | 2012-08-07 09:56:02 -0700 | [diff] [blame] | 187 | final boolean showHeader = ConversationListContext.isSearchResult(mViewContext); |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 188 | // TODO(viki): This code contains intimate understanding of the view. |
| 189 | // Much of this logic |
| 190 | // needs to reside in a separate class that handles the text view in |
| 191 | // isolation. Then, |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 192 | // that logic can be reused in other fragments. |
| 193 | if (showHeader) { |
| 194 | mSearchStatusTextView.setText(res.getString(R.string.search_results_searching_header)); |
| 195 | // Initially reset the count |
| 196 | mSearchResultCountTextView.setText(""); |
| 197 | } |
| 198 | mSearchStatusView.setVisibility(showHeader ? View.VISIBLE : View.GONE); |
| 199 | int marginTop = showHeader ? (int) res.getDimension(R.dimen.notification_view_height) : 0; |
| 200 | MarginLayoutParams layoutParams = (MarginLayoutParams) mListView.getLayoutParams(); |
| 201 | layoutParams.topMargin = marginTop; |
| 202 | mListView.setLayoutParams(layoutParams); |
| 203 | } |
| 204 | |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 205 | /** |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 206 | * Show the header if the current conversation list is showing search |
| 207 | * results. |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 208 | */ |
| 209 | private void updateSearchResultHeader(int count) { |
Mindy Pereira | 71a8f29 | 2012-07-26 16:30:48 -0700 | [diff] [blame] | 210 | if (mActivity == null) { |
| 211 | return; |
| 212 | } |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 213 | // Only show the header if the context is for a search result |
| 214 | final Resources res = getResources(); |
Vikram Aggarwal | ae4ea99 | 2012-08-07 09:56:02 -0700 | [diff] [blame] | 215 | final boolean showHeader = ConversationListContext.isSearchResult(mViewContext); |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 216 | if (showHeader) { |
| 217 | mSearchStatusTextView.setText(res.getString(R.string.search_results_header)); |
| 218 | mSearchResultCountTextView |
| 219 | .setText(res.getString(R.string.search_results_loaded, count)); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | /** |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 224 | * Initializes all internal state for a rendering. |
| 225 | */ |
| 226 | private void initializeUiForFirstDisplay() { |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 227 | // TODO(mindyp): find some way to make the notification container more |
| 228 | // re-usable. |
| 229 | // TODO(viki): refactor according to comment in |
| 230 | // configureSearchResultHandler() |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 231 | mSearchStatusView = mActivity.findViewById(R.id.search_status_view); |
| 232 | mSearchStatusTextView = (TextView) mActivity.findViewById(R.id.search_status_text_view); |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 233 | mSearchResultCountTextView = (TextView) mActivity |
| 234 | .findViewById(R.id.search_result_count_view); |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 235 | } |
| 236 | |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 237 | @Override |
| 238 | public void onActivityCreated(Bundle savedInstanceState) { |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 239 | super.onActivityCreated(savedInstanceState); |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 240 | // Strictly speaking, we get back an android.app.Activity from |
| 241 | // getActivity. However, the |
| 242 | // only activity creating a ConversationListContext is a MailActivity |
| 243 | // which is of type |
| 244 | // ControllableActivity, so this cast should be safe. If this cast |
| 245 | // fails, some other |
| 246 | // activity is creating ConversationListFragments. This activity must be |
| 247 | // of type |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 248 | // ControllableActivity. |
| 249 | final Activity activity = getActivity(); |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 250 | if (!(activity instanceof ControllableActivity)) { |
| 251 | LogUtils.e(LOG_TAG, "ConversationListFragment expects only a ControllableActivity to" |
| 252 | + "create it. Cannot proceed."); |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 253 | } |
| 254 | mActivity = (ControllableActivity) activity; |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 255 | // Since we now have a controllable activity, load the account from it, |
| 256 | // and register for |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 257 | // future account changes. |
| 258 | mAccount = mAccountObserver.initialize(mActivity.getAccountController()); |
Vikram Aggarwal | 7d60288 | 2012-02-07 15:01:20 -0800 | [diff] [blame] | 259 | mCallbacks = mActivity.getListHandler(); |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 260 | mErrorListener = mActivity.getErrorListener(); |
Mindy Pereira | 278fd22 | 2012-07-26 15:23:10 -0700 | [diff] [blame] | 261 | // Start off with the current state of the folder being viewed. |
Mindy Pereira | 6681f6b | 2012-03-09 13:55:54 -0800 | [diff] [blame] | 262 | mFooterView = (ConversationListFooterView) LayoutInflater.from( |
| 263 | mActivity.getActivityContext()).inflate(R.layout.conversation_list_footer_view, |
| 264 | null); |
Paul Westbrook | 4969e0c | 2012-08-20 14:38:39 -0700 | [diff] [blame] | 265 | mFooterView.setClickListener(mActivity); |
Vikram Aggarwal | 81a4f08 | 2012-09-28 09:19:04 -0700 | [diff] [blame] | 266 | final ConversationCursor conversationCursor = getConversationListCursor(); |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame^] | 267 | mListAdapter = new AnimatedAdapter(mActivity.getApplicationContext(), |
Vikram Aggarwal | 81a4f08 | 2012-09-28 09:19:04 -0700 | [diff] [blame] | 268 | conversationCursor, mActivity.getSelectedSet(), mActivity, mListView); |
Mindy Pereira | 6681f6b | 2012-03-09 13:55:54 -0800 | [diff] [blame] | 269 | mListAdapter.addFooter(mFooterView); |
Mindy Pereira | 6f4a6af | 2012-02-29 11:48:52 -0800 | [diff] [blame] | 270 | mListView.setAdapter(mListAdapter); |
Vikram Aggarwal | 4f1cc0b | 2012-08-02 15:16:30 -0700 | [diff] [blame] | 271 | mSelectedSet = mActivity.getSelectedSet(); |
| 272 | mListView.setSelectionSet(mSelectedSet); |
Mindy Pereira | dc8963b | 2012-03-28 17:51:05 -0700 | [diff] [blame] | 273 | mListAdapter.hideFooter(); |
Mindy Pereira | e3c7b0b | 2012-07-26 16:28:34 -0700 | [diff] [blame] | 274 | mFolderObserver = new FolderObserver(); |
Andy Huang | 7591d2f | 2012-07-26 16:48:06 -0700 | [diff] [blame] | 275 | mActivity.getFolderController().registerFolderObserver(mFolderObserver); |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame^] | 276 | mConversationCursorObserver = new ConversationCursorObserver(); |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 277 | mUpdater = mActivity.getConversationUpdater(); |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame^] | 278 | mUpdater.registerConversationListObserver(mConversationCursorObserver); |
Vikram Aggarwal | bcb16b9 | 2013-01-28 18:05:03 -0800 | [diff] [blame] | 279 | mTabletDevice = Utils.useTabletUI(mActivity.getApplicationContext().getResources()); |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 280 | initializeUiForFirstDisplay(); |
Mindy Pereira | dc8963b | 2012-03-28 17:51:05 -0700 | [diff] [blame] | 281 | configureSearchResultHeader(); |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 282 | // The onViewModeChanged callback doesn't get called when the mode |
| 283 | // object is created, so |
Vikram Aggarwal | fa131a2 | 2012-02-02 13:56:22 -0800 | [diff] [blame] | 284 | // force setting the mode manually this time around. |
Mindy Pereira | f96ec32 | 2012-03-02 14:06:33 -0800 | [diff] [blame] | 285 | onViewModeChanged(mActivity.getViewMode().getMode()); |
Mindy Pereira | b5901be | 2012-08-09 17:21:53 -0700 | [diff] [blame] | 286 | mActivity.getViewMode().addListener(this); |
Paul Westbrook | 0e3fd9d | 2012-04-20 02:02:23 -0700 | [diff] [blame] | 287 | |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 288 | if (mActivity.isFinishing()) { |
| 289 | // Activity is finishing, just bail. |
| 290 | return; |
| 291 | } |
Vikram Aggarwal | 81a4f08 | 2012-09-28 09:19:04 -0700 | [diff] [blame] | 292 | mConversationCursorHash = (conversationCursor == null) ? 0 : conversationCursor.hashCode(); |
| 293 | // Belt and suspenders here; make sure we do any necessary sync of the |
| 294 | // ConversationCursor |
| 295 | if (conversationCursor != null && conversationCursor.isRefreshReady()) { |
| 296 | conversationCursor.sync(); |
| 297 | } |
| 298 | |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 299 | // Show list and start loading list. |
| 300 | showList(); |
Mindy Pereira | 4765c5c | 2012-07-19 11:58:22 -0700 | [diff] [blame] | 301 | ToastBarOperation pendingOp = mActivity.getPendingToastOperation(); |
| 302 | if (pendingOp != null) { |
| 303 | // Clear the pending operation |
| 304 | mActivity.setPendingToastOperation(null); |
| 305 | mActivity.onUndoAvailable(pendingOp); |
| 306 | } |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 307 | } |
| 308 | |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 309 | public AnimatedAdapter getAnimatedAdapter() { |
| 310 | return mListAdapter; |
| 311 | } |
| 312 | |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 313 | @Override |
Vikram Aggarwal | d247dc9 | 2012-02-10 15:49:01 -0800 | [diff] [blame] | 314 | public void onCreate(Bundle savedState) { |
Vikram Aggarwal | d247dc9 | 2012-02-10 15:49:01 -0800 | [diff] [blame] | 315 | super.onCreate(savedState); |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 316 | |
| 317 | // Initialize fragment constants from resources |
Vikram Aggarwal | 6c51158 | 2012-02-27 10:59:47 -0800 | [diff] [blame] | 318 | final Resources res = getResources(); |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 319 | TIMESTAMP_UPDATE_INTERVAL = res.getInteger(R.integer.timestamp_update_interval); |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 320 | mUpdateTimestampsRunnable = new Runnable() { |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 321 | @Override |
| 322 | public void run() { |
| 323 | mListView.invalidateViews(); |
| 324 | mHandler.postDelayed(mUpdateTimestampsRunnable, TIMESTAMP_UPDATE_INTERVAL); |
| 325 | } |
| 326 | }; |
| 327 | |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 328 | // Get the context from the arguments |
Vikram Aggarwal | 6c51158 | 2012-02-27 10:59:47 -0800 | [diff] [blame] | 329 | final Bundle args = getArguments(); |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 330 | mViewContext = ConversationListContext.forBundle(args.getBundle(CONVERSATION_LIST_KEY)); |
Mindy Pereira | 3982e23 | 2012-02-29 15:00:34 -0800 | [diff] [blame] | 331 | mAccount = mViewContext.account; |
Paul Westbrook | 80ecd89 | 2012-08-16 13:37:39 -0700 | [diff] [blame] | 332 | |
Vikram Aggarwal | 9a49c9b | 2012-08-31 10:49:33 -0700 | [diff] [blame] | 333 | setRetainInstance(false); |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | @Override |
Vikram Aggarwal | 9a49c9b | 2012-08-31 10:49:33 -0700 | [diff] [blame] | 337 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState) { |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 338 | View rootView = inflater.inflate(R.layout.conversation_list, null); |
Mindy Pereira | 6cc9553 | 2012-08-15 16:04:56 -0700 | [diff] [blame] | 339 | mEmptyView = rootView.findViewById(R.id.empty_view); |
Mindy Pereira | f6a6b50 | 2012-03-15 15:24:26 -0700 | [diff] [blame] | 340 | mListView = (SwipeableListView) rootView.findViewById(android.R.id.list); |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 341 | mListView.setHeaderDividersEnabled(false); |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 342 | mListView.setOnItemLongClickListener(this); |
Mindy Pereira | 4e812f4 | 2012-07-30 16:52:49 -0700 | [diff] [blame] | 343 | mListView.enableSwipe(mAccount.supportsCapability(AccountCapabilities.UNDO)); |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 344 | mListView.setSwipedListener(this); |
Marc Blank | 2af0cf3 | 2012-03-01 19:24:13 -0800 | [diff] [blame] | 345 | |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame^] | 346 | final int choiceMode; |
| 347 | if (savedState != null) { |
| 348 | // Restore the choice mode if it was set earlier, or SINGLE if creating a fresh view. |
| 349 | // Choice mode here represents the current conversation only. CAB mode does not rely on |
| 350 | // the platform: it is a local variable within conversation items. |
| 351 | choiceMode = savedState.getInt(CHOICE_MODE_KEY, ListView.CHOICE_MODE_SINGLE); |
| 352 | if (savedState.containsKey(LIST_STATE_KEY)) { |
| 353 | mListView.onRestoreInstanceState(savedState.getParcelable(LIST_STATE_KEY)); |
| 354 | // TODO: find a better way to unset the selected item when restoring |
| 355 | mListView.clearChoices(); |
| 356 | } |
| 357 | } else { |
| 358 | choiceMode = ListView.CHOICE_MODE_SINGLE; |
Vikram Aggarwal | 9a49c9b | 2012-08-31 10:49:33 -0700 | [diff] [blame] | 359 | } |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame^] | 360 | setChoiceMode(choiceMode); |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 361 | return rootView; |
| 362 | } |
| 363 | |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame^] | 364 | /** |
| 365 | * Sets the choice mode of the list view |
| 366 | * @param choiceMode ListView# |
| 367 | */ |
| 368 | private final void setChoiceMode(int choiceMode) { |
| 369 | mListView.setChoiceMode(choiceMode); |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * Tell the list to select nothing. |
| 374 | */ |
| 375 | public final void setChoiceNone() { |
| 376 | final int currentSelected = mListView.getCheckedItemPosition(); |
| 377 | mListView.clearChoices(); |
| 378 | // We use the activated state to show the blue highlight on tablet. Clearing the choices |
| 379 | // removes the checked state, but doesn't do anything to the activated state. We must |
| 380 | // manually clear that. |
| 381 | if (currentSelected != ListView.INVALID_POSITION) { |
| 382 | final View v = mListView.getChildAt(currentSelected); |
| 383 | if (v != null) { |
| 384 | v.setActivated(false); |
| 385 | } |
| 386 | } |
| 387 | setChoiceMode(ListView.CHOICE_MODE_NONE); |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * Tell the list to get out of selecting none. |
| 392 | */ |
| 393 | public final void revertChoiceMode() { |
| 394 | setChoiceMode(ListView.CHOICE_MODE_SINGLE); |
| 395 | } |
| 396 | |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 397 | @Override |
Andy Huang | 7517e3b | 2012-08-20 12:30:08 -0700 | [diff] [blame] | 398 | public void onDestroy() { |
Andy Huang | 7517e3b | 2012-08-20 12:30:08 -0700 | [diff] [blame] | 399 | super.onDestroy(); |
| 400 | } |
| 401 | |
| 402 | @Override |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 403 | public void onDestroyView() { |
Andy Huang | 7517e3b | 2012-08-20 12:30:08 -0700 | [diff] [blame] | 404 | |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 405 | // Clear the list's adapter |
| 406 | mListAdapter.destroy(); |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 407 | mListView.setAdapter(null); |
| 408 | |
Vikram Aggarwal | d7a12cd | 2012-02-03 09:36:20 -0800 | [diff] [blame] | 409 | mActivity.unsetViewModeListener(this); |
Mindy Pereira | 71a8f29 | 2012-07-26 16:30:48 -0700 | [diff] [blame] | 410 | if (mFolderObserver != null) { |
Andy Huang | 7591d2f | 2012-07-26 16:48:06 -0700 | [diff] [blame] | 411 | mActivity.getFolderController().unregisterFolderObserver(mFolderObserver); |
Mindy Pereira | 71a8f29 | 2012-07-26 16:30:48 -0700 | [diff] [blame] | 412 | mFolderObserver = null; |
| 413 | } |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame^] | 414 | if (mConversationCursorObserver != null) { |
| 415 | mUpdater.unregisterConversationListObserver(mConversationCursorObserver); |
| 416 | mConversationCursorObserver = null; |
Mindy Pereira | 70a70c9 | 2012-08-02 08:39:45 -0700 | [diff] [blame] | 417 | } |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 418 | mAccountObserver.unregisterAndDestroy(); |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 419 | super.onDestroyView(); |
| 420 | } |
| 421 | |
Vikram Aggarwal | 9730ea0 | 2012-08-02 12:46:19 -0700 | [diff] [blame] | 422 | /** |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 423 | * There are three binary variables, which determine what we do with a |
| 424 | * message. checkbEnabled: Whether check boxes are enabled or not (forced |
| 425 | * true on tablet) cabModeOn: Whether CAB mode is currently on or not. |
| 426 | * pressType: long or short tap (There is a third possibility: phone or |
| 427 | * tablet, but they have <em>identical</em> behavior) The matrix of |
| 428 | * possibilities is: |
| 429 | * <p> |
| 430 | * Long tap: Always toggle selection of conversation. If CAB mode is not |
| 431 | * started, then start it. |
Vikram Aggarwal | 9730ea0 | 2012-08-02 12:46:19 -0700 | [diff] [blame] | 432 | * <pre> |
| 433 | * | Checkboxes | No Checkboxes |
| 434 | * ----------+------------+--------------- |
| 435 | * CAB mode | Select | Select |
| 436 | * List mode | Select | Select |
| 437 | * |
Vikram Aggarwal | 9730ea0 | 2012-08-02 12:46:19 -0700 | [diff] [blame] | 438 | * </pre> |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 439 | * |
Vikram Aggarwal | 9730ea0 | 2012-08-02 12:46:19 -0700 | [diff] [blame] | 440 | * Reference: http://b/issue?id=6392199 |
| 441 | * <p> |
| 442 | * {@inheritDoc} |
| 443 | */ |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 444 | @Override |
| 445 | public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { |
Mindy Pereira | 1e231a6 | 2012-03-23 12:42:15 -0700 | [diff] [blame] | 446 | // Ignore anything that is not a conversation item. Could be a footer. |
| 447 | if (!(view instanceof ConversationItemView)) { |
| 448 | return true; |
| 449 | } |
mindyp | bad1a93 | 2012-08-23 10:25:22 -0700 | [diff] [blame] | 450 | ((ConversationItemView) view).toggleCheckMarkOrBeginDrag(); |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 451 | return true; |
| 452 | } |
| 453 | |
Vikram Aggarwal | 4f1cc0b | 2012-08-02 15:16:30 -0700 | [diff] [blame] | 454 | /** |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 455 | * See the comment for |
| 456 | * {@link #onItemLongClick(AdapterView, View, int, long)}. |
| 457 | * <p> |
| 458 | * Short tap behavior: |
| 459 | * |
Vikram Aggarwal | 4f1cc0b | 2012-08-02 15:16:30 -0700 | [diff] [blame] | 460 | * <pre> |
| 461 | * | Checkboxes | No Checkboxes |
| 462 | * ----------+------------+--------------- |
| 463 | * CAB mode | Peek | Select |
| 464 | * List mode | Peek | Peek |
| 465 | * </pre> |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 466 | * |
Vikram Aggarwal | 4f1cc0b | 2012-08-02 15:16:30 -0700 | [diff] [blame] | 467 | * Reference: http://b/issue?id=6392199 |
| 468 | * <p> |
| 469 | * {@inheritDoc} |
| 470 | */ |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 471 | @Override |
Vikram Aggarwal | 54452ae | 2012-03-13 15:29:00 -0700 | [diff] [blame] | 472 | public void onListItemClick(ListView l, View view, int position, long id) { |
Mindy Pereira | 1e231a6 | 2012-03-23 12:42:15 -0700 | [diff] [blame] | 473 | // Ignore anything that is not a conversation item. Could be a footer. |
mindyp | 8694fe9 | 2012-09-25 11:07:16 -0700 | [diff] [blame] | 474 | // If we are using a keyboard, the highlighted item is the parent; |
| 475 | // otherwise, this is a direct call from the ConverationItemView |
mindyp | c03b05a | 2012-09-28 14:23:48 -0700 | [diff] [blame] | 476 | if (!(view instanceof ToggleableItem)) { |
Mindy Pereira | 1e231a6 | 2012-03-23 12:42:15 -0700 | [diff] [blame] | 477 | return; |
| 478 | } |
Paul Westbrook | 7ed5377 | 2013-01-23 10:19:55 -0800 | [diff] [blame] | 479 | if (mAccount.settings.hideCheckboxes && !mSelectedSet.isEmpty()) { |
mindyp | c03b05a | 2012-09-28 14:23:48 -0700 | [diff] [blame] | 480 | ToggleableItem v = (ToggleableItem) view; |
| 481 | v.toggleCheckMarkOrBeginDrag(); |
Vikram Aggarwal | 4f1cc0b | 2012-08-02 15:16:30 -0700 | [diff] [blame] | 482 | } else { |
| 483 | viewConversation(position); |
| 484 | } |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 485 | // When a new list item is clicked, commit any existing leave behind |
mindyp | 8694fe9 | 2012-09-25 11:07:16 -0700 | [diff] [blame] | 486 | // items. Wait until we have opened the desired conversation to cause |
| 487 | // any position changes. |
Vikram Aggarwal | bcb16b9 | 2013-01-28 18:05:03 -0800 | [diff] [blame] | 488 | commitDestructiveActions(Utils.useTabletUI(mActivity.getActivityContext().getResources())); |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | @Override |
| 492 | public void onPause() { |
| 493 | super.onPause(); |
| 494 | } |
| 495 | |
| 496 | @Override |
| 497 | public void onSaveInstanceState(Bundle outState) { |
| 498 | super.onSaveInstanceState(outState); |
| 499 | if (mListView != null) { |
| 500 | outState.putParcelable(LIST_STATE_KEY, mListView.onSaveInstanceState()); |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame^] | 501 | outState.putInt(CHOICE_MODE_KEY, mListView.getChoiceMode()); |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 502 | } |
| 503 | } |
| 504 | |
| 505 | @Override |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 506 | public void onStart() { |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 507 | super.onStart(); |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 508 | mHandler.postDelayed(mUpdateTimestampsRunnable, TIMESTAMP_UPDATE_INTERVAL); |
| 509 | } |
| 510 | |
| 511 | @Override |
| 512 | public void onStop() { |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 513 | super.onStop(); |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 514 | mHandler.removeCallbacks(mUpdateTimestampsRunnable); |
| 515 | } |
| 516 | |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 517 | @Override |
Vikram Aggarwal | fa131a2 | 2012-02-02 13:56:22 -0800 | [diff] [blame] | 518 | public void onViewModeChanged(int newMode) { |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 519 | // Change the divider based on view mode. |
Vikram Aggarwal | fa131a2 | 2012-02-02 13:56:22 -0800 | [diff] [blame] | 520 | if (mTabletDevice) { |
| 521 | if (newMode == ViewMode.CONVERSATION) { |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 522 | mListView.setBackgroundResource(R.drawable.panel_conversation_leftstroke); |
Mindy Pereira | de1c859 | 2012-08-10 14:54:31 -0700 | [diff] [blame] | 523 | } else if (newMode == ViewMode.CONVERSATION_LIST |
| 524 | || newMode == ViewMode.SEARCH_RESULTS_LIST) { |
Mindy Pereira | b5901be | 2012-08-09 17:21:53 -0700 | [diff] [blame] | 525 | // There are no selected conversations when in conversation |
| 526 | // list mode. |
Mindy Pereira | 62c5af8 | 2012-03-05 10:08:38 -0800 | [diff] [blame] | 527 | mListView.clearChoices(); |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 528 | mListView.setBackgroundDrawable(null); |
| 529 | } |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 530 | } else { |
| 531 | mListView.setBackgroundDrawable(null); |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 532 | } |
Mindy Pereira | b5901be | 2012-08-09 17:21:53 -0700 | [diff] [blame] | 533 | if (mFooterView != null) { |
| 534 | mFooterView.onViewModeChanged(newMode); |
| 535 | } |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 536 | } |
Mindy Pereira | b5901be | 2012-08-09 17:21:53 -0700 | [diff] [blame] | 537 | |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 538 | /** |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 539 | * Handles a request to show a new conversation list, either from a search |
| 540 | * query or for viewing a folder. This will initiate a data load, and hence |
| 541 | * must be called on the UI thread. |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 542 | */ |
| 543 | private void showList() { |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 544 | mListView.setEmptyView(null); |
Andy Huang | 7591d2f | 2012-07-26 16:48:06 -0700 | [diff] [blame] | 545 | onFolderUpdated(mActivity.getFolderController().getFolder()); |
mindyp | ca8ca2d | 2012-09-11 17:38:34 -0700 | [diff] [blame] | 546 | onConversationListStatusUpdated(); |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 547 | } |
| 548 | |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 549 | /** |
| 550 | * View the message at the given position. |
| 551 | * @param position |
| 552 | */ |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 553 | protected void viewConversation(int position) { |
Vikram Aggarwal | c769422 | 2012-04-23 13:37:01 -0700 | [diff] [blame] | 554 | LogUtils.d(LOG_TAG, "ConversationListFragment.viewConversation(%d)", position); |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame^] | 555 | setSelected(position, true); |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 556 | final ConversationCursor cursor = getConversationListCursor(); |
| 557 | if (cursor != null && cursor.moveToPosition(position)) { |
| 558 | final Conversation conv = new Conversation(cursor); |
| 559 | conv.position = position; |
Andy Huang | 1ee96b2 | 2012-08-24 20:19:53 -0700 | [diff] [blame] | 560 | mCallbacks.onConversationSelected(conv, false /* inLoaderCallbacks */); |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 561 | } |
| 562 | } |
| 563 | |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame^] | 564 | /** |
| 565 | * Sets the selected conversation to the position given here. |
| 566 | * @param position |
| 567 | * @param different if the currently selected conversation is different from the one provided |
| 568 | * here. This is a difference in conversations, not a difference in positions. For example, a |
| 569 | * conversation at position 2 can move to position 4 as a result of new mail. |
| 570 | */ |
mindyp | e21f886 | 2012-10-01 09:32:03 -0700 | [diff] [blame] | 571 | public void setSelected(int position, boolean different) { |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame^] | 572 | if (mListView.getChoiceMode() == ListView.CHOICE_MODE_NONE) { |
| 573 | return; |
| 574 | } |
mindyp | e21f886 | 2012-10-01 09:32:03 -0700 | [diff] [blame] | 575 | if (different) { |
| 576 | mListView.smoothScrollToPosition(position); |
| 577 | } |
| 578 | mListView.setItemChecked(position, true); |
| 579 | } |
| 580 | |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 581 | /** |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame^] | 582 | * Returns the cursor associated with the conversation list. |
| 583 | * @return |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 584 | */ |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 585 | private ConversationCursor getConversationListCursor() { |
| 586 | return mCallbacks != null ? mCallbacks.getConversationListCursor() : null; |
Mindy Pereira | fe06bea | 2012-02-16 08:15:14 -0800 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | /** |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 590 | * Request a refresh of the list. No sync is carried out and none is |
| 591 | * promised. |
Vikram Aggarwal | 54452ae | 2012-03-13 15:29:00 -0700 | [diff] [blame] | 592 | */ |
| 593 | public void requestListRefresh() { |
| 594 | mListAdapter.notifyDataSetChanged(); |
| 595 | } |
| 596 | |
Vikram Aggarwal | 75daee5 | 2012-04-30 11:13:09 -0700 | [diff] [blame] | 597 | /** |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 598 | * Change the UI to delete the conversations provided and then call the |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 599 | * {@link DestructiveAction} provided here <b>after</b> the UI has been |
| 600 | * updated. |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 601 | * @param conversations |
Vikram Aggarwal | 75daee5 | 2012-04-30 11:13:09 -0700 | [diff] [blame] | 602 | * @param action |
| 603 | */ |
Vikram Aggarwal | a8e4318 | 2012-09-13 12:55:10 -0700 | [diff] [blame] | 604 | public void requestDelete(int actionId, final Collection<Conversation> conversations, |
Vikram Aggarwal | 669947b | 2013-01-10 17:05:56 -0800 | [diff] [blame] | 605 | final DestructiveAction action) { |
Mindy Pereira | acf6039 | 2012-04-06 09:11:00 -0700 | [diff] [blame] | 606 | for (Conversation conv : conversations) { |
| 607 | conv.localDeleteOnUpdate = true; |
| 608 | } |
Vikram Aggarwal | a8e4318 | 2012-09-13 12:55:10 -0700 | [diff] [blame] | 609 | final ListItemsRemovedListener listener = new ListItemsRemovedListener() { |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 610 | @Override |
| 611 | public void onListItemsRemoved() { |
| 612 | action.performAction(); |
| 613 | } |
Vikram Aggarwal | a8e4318 | 2012-09-13 12:55:10 -0700 | [diff] [blame] | 614 | }; |
| 615 | final SwipeableListView listView = (SwipeableListView) getListView(); |
| 616 | if (listView.getSwipeAction() == actionId) { |
mindyp | 4e77a7d | 2012-11-27 09:03:32 -0800 | [diff] [blame] | 617 | listView.destroyItems(conversations, listener); |
Vikram Aggarwal | a8e4318 | 2012-09-13 12:55:10 -0700 | [diff] [blame] | 618 | return; |
| 619 | } |
| 620 | // Delete the local delete items (all for now) and when done, |
| 621 | // update... |
| 622 | mListAdapter.delete(conversations, listener); |
Mindy Pereira | acf6039 | 2012-04-06 09:11:00 -0700 | [diff] [blame] | 623 | } |
Mindy Pereira | a3911aa | 2012-03-09 13:26:26 -0800 | [diff] [blame] | 624 | |
Mindy Pereira | 11dd5ef | 2012-03-10 15:10:18 -0800 | [diff] [blame] | 625 | public void onFolderUpdated(Folder folder) { |
Mindy Pereira | dc8963b | 2012-03-28 17:51:05 -0700 | [diff] [blame] | 626 | mFolder = folder; |
Mindy Pereira | 06642fa | 2012-07-12 16:23:27 -0700 | [diff] [blame] | 627 | setSwipeAction(); |
Mindy Pereira | 82cea48 | 2012-03-27 16:45:00 -0700 | [diff] [blame] | 628 | if (mFolder == null) { |
| 629 | return; |
| 630 | } |
Mindy Pereira | 4584a0d | 2012-03-13 14:42:14 -0700 | [diff] [blame] | 631 | mListAdapter.setFolder(mFolder); |
Mindy Pereira | 70a70c9 | 2012-08-02 08:39:45 -0700 | [diff] [blame] | 632 | mFooterView.setFolder(mFolder); |
Vikram Aggarwal | 41b9e8f | 2012-09-25 10:15:04 -0700 | [diff] [blame] | 633 | if (!mFolder.wasSyncSuccessful()) { |
Mindy Pereira | 70a70c9 | 2012-08-02 08:39:45 -0700 | [diff] [blame] | 634 | mErrorListener.onError(mFolder, false); |
| 635 | } |
Paul Westbrook | c01f5d9 | 2012-09-25 17:22:25 -0700 | [diff] [blame] | 636 | |
| 637 | // Notify of changes to the Folder. |
| 638 | onFolderStatusUpdated(); |
| 639 | |
Mindy Pereira | 12fe37a | 2012-08-15 10:02:57 -0700 | [diff] [blame] | 640 | // Blow away conversation items cache. |
| 641 | ConversationItemViewModel.onFolderUpdated(mFolder); |
Mindy Pereira | 70a70c9 | 2012-08-02 08:39:45 -0700 | [diff] [blame] | 642 | } |
| 643 | |
Vikram Aggarwal | 81a4f08 | 2012-09-28 09:19:04 -0700 | [diff] [blame] | 644 | /** |
| 645 | * Updates the footer visibility and updates the conversation cursor |
| 646 | */ |
Mindy Pereira | 70a70c9 | 2012-08-02 08:39:45 -0700 | [diff] [blame] | 647 | public void onConversationListStatusUpdated() { |
Paul Westbrook | 9a70e91 | 2012-08-17 15:53:20 -0700 | [diff] [blame] | 648 | final ConversationCursor cursor = getConversationListCursor(); |
| 649 | final boolean showFooter = cursor != null && mFooterView.updateStatus(cursor); |
Paul Westbrook | c01f5d9 | 2012-09-25 17:22:25 -0700 | [diff] [blame] | 650 | // Update the folder status, in case the cursor could affect it. |
| 651 | onFolderStatusUpdated(); |
| 652 | mListAdapter.setFooterVisibility(showFooter); |
| 653 | |
| 654 | // Also change the cursor here. |
| 655 | onCursorUpdated(); |
| 656 | } |
| 657 | |
| 658 | private void onFolderStatusUpdated() { |
| 659 | final ConversationCursor cursor = getConversationListCursor(); |
Paul Westbrook | 9a70e91 | 2012-08-17 15:53:20 -0700 | [diff] [blame] | 660 | Bundle extras = cursor != null ? cursor.getExtras() : Bundle.EMPTY; |
Mindy Pereira | 70a70c9 | 2012-08-02 08:39:45 -0700 | [diff] [blame] | 661 | int error = extras.containsKey(UIProvider.CursorExtraKeys.EXTRA_ERROR) ? |
| 662 | extras.getInt(UIProvider.CursorExtraKeys.EXTRA_ERROR) |
| 663 | : UIProvider.LastSyncResult.SUCCESS; |
Mindy Pereira | 6cc9553 | 2012-08-15 16:04:56 -0700 | [diff] [blame] | 664 | int status = extras.getInt(UIProvider.CursorExtraKeys.EXTRA_STATUS); |
Paul Westbrook | c01f5d9 | 2012-09-25 17:22:25 -0700 | [diff] [blame] | 665 | // We want to update the UI with this informaion if either we are loaded or complete, or |
| 666 | // we have a folder with a non-0 count. |
| 667 | final int folderCount = mFolder != null ? mFolder.totalCount : 0; |
Mindy Pereira | 6cc9553 | 2012-08-15 16:04:56 -0700 | [diff] [blame] | 668 | if (error == UIProvider.LastSyncResult.SUCCESS |
| 669 | && (status == UIProvider.CursorStatus.LOADED |
Paul Westbrook | c01f5d9 | 2012-09-25 17:22:25 -0700 | [diff] [blame] | 670 | || status == UIProvider.CursorStatus.COMPLETE) || folderCount > 0) { |
| 671 | updateSearchResultHeader(folderCount); |
| 672 | if (folderCount == 0) { |
Mindy Pereira | 6cc9553 | 2012-08-15 16:04:56 -0700 | [diff] [blame] | 673 | mListView.setEmptyView(mEmptyView); |
| 674 | } |
Mindy Pereira | 70a70c9 | 2012-08-02 08:39:45 -0700 | [diff] [blame] | 675 | } |
Mindy Pereira | a3911aa | 2012-03-09 13:26:26 -0800 | [diff] [blame] | 676 | } |
Mindy Pereira | f6a6b50 | 2012-03-15 15:24:26 -0700 | [diff] [blame] | 677 | |
Mindy Pereira | 06642fa | 2012-07-12 16:23:27 -0700 | [diff] [blame] | 678 | private void setSwipeAction() { |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 679 | int swipeSetting = Settings.getSwipeSetting(mAccount.settings); |
Mindy Pereira | e58222b | 2012-07-25 14:33:18 -0700 | [diff] [blame] | 680 | if (swipeSetting == Swipe.DISABLED |
Mindy Pereira | b807337 | 2012-08-09 14:00:10 -0700 | [diff] [blame] | 681 | || !mAccount.supportsCapability(AccountCapabilities.UNDO) |
| 682 | || (mFolder != null && mFolder.isTrash())) { |
Mindy Pereira | e58222b | 2012-07-25 14:33:18 -0700 | [diff] [blame] | 683 | mListView.enableSwipe(false); |
| 684 | } else { |
| 685 | int action; |
mindyp | 77c3e1b | 2012-09-11 14:05:02 -0700 | [diff] [blame] | 686 | if (ConversationListContext.isSearchResult(mViewContext) |
| 687 | || (mFolder != null && mFolder.type == FolderType.SPAM)) { |
Mindy Pereira | e58222b | 2012-07-25 14:33:18 -0700 | [diff] [blame] | 688 | action = R.id.delete; |
Mindy Pereira | 4e812f4 | 2012-07-30 16:52:49 -0700 | [diff] [blame] | 689 | } else if (mFolder == null) { |
Mindy Pereira | 01f3050 | 2012-08-14 10:30:51 -0700 | [diff] [blame] | 690 | action = R.id.remove_folder; |
Mindy Pereira | 4e812f4 | 2012-07-30 16:52:49 -0700 | [diff] [blame] | 691 | } else { |
| 692 | // We have enough information to respect user settings. |
| 693 | switch (swipeSetting) { |
| 694 | case Swipe.ARCHIVE: |
| 695 | if (mAccount.supportsCapability(AccountCapabilities.ARCHIVE)) { |
| 696 | if (mFolder.supportsCapability(FolderCapabilities.ARCHIVE)) { |
| 697 | action = R.id.archive; |
| 698 | break; |
| 699 | } else if (mFolder.supportsCapability |
| 700 | (FolderCapabilities.CAN_ACCEPT_MOVED_MESSAGES)) { |
Mindy Pereira | 01f3050 | 2012-08-14 10:30:51 -0700 | [diff] [blame] | 701 | action = R.id.remove_folder; |
Mindy Pereira | 4e812f4 | 2012-07-30 16:52:49 -0700 | [diff] [blame] | 702 | break; |
| 703 | } |
| 704 | } |
| 705 | case Swipe.DELETE: |
| 706 | default: |
| 707 | action = R.id.delete; |
| 708 | break; |
| 709 | } |
Mindy Pereira | e58222b | 2012-07-25 14:33:18 -0700 | [diff] [blame] | 710 | } |
| 711 | mListView.setSwipeAction(action); |
| 712 | } |
Mindy Pereira | 06642fa | 2012-07-12 16:23:27 -0700 | [diff] [blame] | 713 | mListView.setCurrentFolder(mFolder); |
| 714 | } |
| 715 | |
Vikram Aggarwal | 17f373e | 2012-09-17 15:49:32 -0700 | [diff] [blame] | 716 | /** |
| 717 | * Changes the conversation cursor in the list and sets selected position if none is set. |
| 718 | */ |
| 719 | private void onCursorUpdated() { |
| 720 | if (mCallbacks == null || mListAdapter == null) { |
| 721 | return; |
| 722 | } |
Vikram Aggarwal | 81a4f08 | 2012-09-28 09:19:04 -0700 | [diff] [blame] | 723 | // Check against the previous cursor here and see if they are the same. If they are, then |
| 724 | // do a notifyDataSetChanged. |
| 725 | final ConversationCursor newCursor = mCallbacks.getConversationListCursor(); |
| 726 | mListAdapter.swapCursor(newCursor); |
| 727 | // When the conversation cursor is *updated*, we get back the same instance. In that |
| 728 | // situation, CursorAdapter.swapCursor() silently returns, without forcing a |
| 729 | // notifyDataSetChanged(). So let's force a call to notifyDataSetChanged, since an updated |
| 730 | // cursor means that the dataset has changed. |
| 731 | final int newCursorHash = (newCursor == null) ? 0 : newCursor.hashCode(); |
| 732 | if (mConversationCursorHash == newCursorHash && mConversationCursorHash != 0) { |
| 733 | mListAdapter.notifyDataSetChanged(); |
| 734 | } |
| 735 | mConversationCursorHash = newCursorHash; |
Vikram Aggarwal | 17f373e | 2012-09-17 15:49:32 -0700 | [diff] [blame] | 736 | // If a current conversation is available, and none is selected in the list, then ask |
| 737 | // the list to select the current conversation. |
| 738 | final Conversation conv = mCallbacks.getCurrentConversation(); |
| 739 | if (conv == null) { |
| 740 | return; |
| 741 | } |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame^] | 742 | if (mListView.getChoiceMode() != ListView.CHOICE_MODE_NONE |
| 743 | && mListView.getCheckedItemPosition() == -1) { |
| 744 | setSelected(conv.position, true); |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 745 | } |
Mindy Pereira | 21ab490 | 2012-03-19 18:48:03 -0700 | [diff] [blame] | 746 | } |
Mindy Pereira | 1e2573b | 2012-04-17 14:34:36 -0700 | [diff] [blame] | 747 | |
mindyp | c6adce3 | 2012-08-22 18:46:42 -0700 | [diff] [blame] | 748 | public void commitDestructiveActions(boolean animate) { |
Mindy Pereira | 8937bf1 | 2012-07-23 14:05:02 -0700 | [diff] [blame] | 749 | if (mListView != null) { |
mindyp | c6adce3 | 2012-08-22 18:46:42 -0700 | [diff] [blame] | 750 | mListView.commitDestructiveActions(animate); |
Vikram Aggarwal | 7d81600 | 2012-04-17 17:06:41 -0700 | [diff] [blame] | 751 | |
| 752 | } |
| 753 | } |
| 754 | |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 755 | @Override |
| 756 | public void onListItemSwiped(Collection<Conversation> conversations) { |
| 757 | mUpdater.showNextConversation(conversations); |
| 758 | } |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 759 | } |