blob: 4be8c9ada499dfc6be4b05c766c8bee9e8125e2b [file] [log] [blame]
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -08001/*
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
18package com.android.mail.ui;
19
Mark Wei2102b2c2013-05-02 17:15:30 -070020import com.google.common.collect.ImmutableList;
21
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -080022import android.app.Activity;
23import android.app.ListFragment;
Alice Yang20323162013-04-17 10:37:41 -070024import android.content.Context;
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -080025import android.content.res.Resources;
Mindy Pereira6d8e7fe2012-07-26 16:02:49 -070026import android.database.DataSetObserver;
Alice Yang20323162013-04-17 10:37:41 -070027import android.graphics.PixelFormat;
28import android.graphics.Rect;
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -080029import android.os.Bundle;
30import android.os.Handler;
Alice Yang0d74a662013-03-25 14:01:24 -070031import android.text.format.DateUtils;
Alice Yang20323162013-04-17 10:37:41 -070032import android.util.TypedValue;
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -080033import android.view.LayoutInflater;
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -080034import android.view.View;
35import android.view.ViewGroup;
36import android.view.ViewGroup.MarginLayoutParams;
37import android.widget.AdapterView;
38import android.widget.AdapterView.OnItemLongClickListener;
39import android.widget.ListView;
40import android.widget.TextView;
41
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -080042import com.android.mail.ConversationListContext;
Marc Blankf3626952012-02-28 17:06:05 -080043import com.android.mail.R;
Vikram Aggarwal80aeac52012-02-07 15:27:20 -080044import com.android.mail.browse.ConversationCursor;
Vikram Aggarwald247dc92012-02-10 15:49:01 -080045import com.android.mail.browse.ConversationItemView;
Mindy Pereira12fe37a2012-08-15 10:02:57 -070046import com.android.mail.browse.ConversationItemViewModel;
Mindy Pereira6681f6b2012-03-09 13:55:54 -080047import com.android.mail.browse.ConversationListFooterView;
Alice Yang64273142013-04-10 18:26:56 -070048import com.android.mail.browse.ToggleableItem;
Vikram Aggarwal80aeac52012-02-07 15:27:20 -080049import com.android.mail.providers.Account;
Vikram Aggarwal7c401b72012-08-13 16:43:47 -070050import com.android.mail.providers.AccountObserver;
Vikram Aggarwald247dc92012-02-10 15:49:01 -080051import com.android.mail.providers.Conversation;
Mindy Pereira4f166de2012-02-14 13:40:58 -080052import com.android.mail.providers.Folder;
Vikram Aggarwal50ff0e52013-03-14 13:58:02 -070053import com.android.mail.providers.FolderObserver;
Vikram Aggarwal7d816002012-04-17 17:06:41 -070054import com.android.mail.providers.Settings;
Vikram Aggarwal80aeac52012-02-07 15:27:20 -080055import com.android.mail.providers.UIProvider;
Mindy Pereira4e812f42012-07-30 16:52:49 -070056import com.android.mail.providers.UIProvider.AccountCapabilities;
Alice Yang64273142013-04-10 18:26:56 -070057import com.android.mail.providers.UIProvider.ConversationListIcon;
Mindy Pereira4e812f42012-07-30 16:52:49 -070058import com.android.mail.providers.UIProvider.FolderCapabilities;
mindyp77c3e1b2012-09-11 14:05:02 -070059import com.android.mail.providers.UIProvider.FolderType;
Mindy Pereirae58222b2012-07-25 14:33:18 -070060import com.android.mail.providers.UIProvider.Swipe;
mindyp9365a822012-09-12 09:09:09 -070061import com.android.mail.ui.SwipeableListView.ListItemSwipedListener;
62import com.android.mail.ui.SwipeableListView.ListItemsRemovedListener;
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -080063import com.android.mail.ui.ViewMode.ModeChangeListener;
Paul Westbrookb334c902012-06-25 11:42:46 -070064import com.android.mail.utils.LogTag;
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -080065import com.android.mail.utils.LogUtils;
Vikram Aggarwalfa131a22012-02-02 13:56:22 -080066import com.android.mail.utils.Utils;
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -080067
Mindy Pereiraf6a6b502012-03-15 15:24:26 -070068import java.util.Collection;
Scott Kennedy7c8325d2013-02-28 10:46:10 -080069import java.util.List;
Mindy Pereirafe06bea2012-02-16 08:15:14 -080070
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -080071/**
72 * The conversation list UI component.
73 */
Vikram Aggarwald247dc92012-02-10 15:49:01 -080074public final class ConversationListFragment extends ListFragment implements
mindyp9365a822012-09-12 09:09:09 -070075 OnItemLongClickListener, ModeChangeListener, ListItemSwipedListener {
Vikram Aggarwal649b9ea2012-08-27 12:15:20 -070076 /** Key used to pass data to {@link ConversationListFragment}. */
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -080077 private static final String CONVERSATION_LIST_KEY = "conversation-list";
Vikram Aggarwal649b9ea2012-08-27 12:15:20 -070078 /** Key used to keep track of the scroll state of the list. */
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -080079 private static final String LIST_STATE_KEY = "list-state";
Vikram Aggarwal649b9ea2012-08-27 12:15:20 -070080
Paul Westbrookb334c902012-06-25 11:42:46 -070081 private static final String LOG_TAG = LogTag.getLogTag();
Vikram Aggarwala91d00b2013-01-18 12:00:37 -080082 /** Key used to save the ListView choice mode, since ListView doesn't save it automatically! */
83 private static final String CHOICE_MODE_KEY = "choice-mode-key";
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -080084
Vikram Aggarwald7a12cd2012-02-03 09:36:20 -080085 // True if we are on a tablet device
86 private static boolean mTabletDevice;
87
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -080088 /**
mindyp9365a822012-09-12 09:09:09 -070089 * Frequency of update of timestamps. Initialized in
90 * {@link #onCreate(Bundle)} and final afterwards.
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -080091 */
92 private static int TIMESTAMP_UPDATE_INTERVAL = 0;
93
Alice Yang0d74a662013-03-25 14:01:24 -070094 private static long NO_NEW_MESSAGE_DURATION = 1 * DateUtils.SECOND_IN_MILLIS;
95
Vikram Aggarwal80aeac52012-02-07 15:27:20 -080096 private ControllableActivity mActivity;
Vikram Aggarwal80aeac52012-02-07 15:27:20 -080097
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -080098 // Control state.
99 private ConversationListCallbacks mCallbacks;
Vikram Aggarwal80aeac52012-02-07 15:27:20 -0800100
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -0800101 private final Handler mHandler = new Handler();
Vikram Aggarwal80aeac52012-02-07 15:27:20 -0800102
Alice Yang486e63e2013-04-05 13:01:50 -0700103 private ConversationListView mConversationListView;
104
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -0800105 // The internal view objects.
Mindy Pereiraf6a6b502012-03-15 15:24:26 -0700106 private SwipeableListView mListView;
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -0800107
108 private TextView mSearchResultCountTextView;
109 private TextView mSearchStatusTextView;
110
111 private View mSearchStatusView;
Vikram Aggarwal80aeac52012-02-07 15:27:20 -0800112
Vikram Aggarwal80aeac52012-02-07 15:27:20 -0800113 /**
114 * Current Account being viewed
115 */
Vikram Aggarwald247dc92012-02-10 15:49:01 -0800116 private Account mAccount;
Vikram Aggarwal80aeac52012-02-07 15:27:20 -0800117 /**
Mindy Pereira30fd47b2012-03-09 09:24:00 -0800118 * Current folder being viewed.
Vikram Aggarwal80aeac52012-02-07 15:27:20 -0800119 */
Mindy Pereira4f166de2012-02-14 13:40:58 -0800120 private Folder mFolder;
Vikram Aggarwal80aeac52012-02-07 15:27:20 -0800121
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -0800122 /**
123 * A simple method to update the timestamps of conversations periodically.
124 */
125 private Runnable mUpdateTimestampsRunnable = null;
126
127 private ConversationListContext mViewContext;
128
Vikram Aggarwal80aeac52012-02-07 15:27:20 -0800129 private AnimatedAdapter mListAdapter;
Vikram Aggarwal7d602882012-02-07 15:01:20 -0800130
Mindy Pereira6681f6b2012-03-09 13:55:54 -0800131 private ConversationListFooterView mFooterView;
Mindy Pereira6cc95532012-08-15 16:04:56 -0700132 private View mEmptyView;
Andrew Sappersteinc2c9dc12012-07-02 18:17:32 -0700133 private ErrorListener mErrorListener;
Vikram Aggarwal50ff0e52013-03-14 13:58:02 -0700134 private FolderObserver mFolderObserver;
Vikram Aggarwala91d00b2013-01-18 12:00:37 -0800135 private DataSetObserver mConversationCursorObserver;
Vikram Aggarwald247dc92012-02-10 15:49:01 -0800136
Vikram Aggarwal4f1cc0b2012-08-02 15:16:30 -0700137 private ConversationSelectionSet mSelectedSet;
Vikram Aggarwal7c401b72012-08-13 16:43:47 -0700138 private final AccountObserver mAccountObserver = new AccountObserver() {
139 @Override
140 public void onChanged(Account newAccount) {
141 mAccount = newAccount;
142 setSwipeAction();
143 }
144 };
mindyp9365a822012-09-12 09:09:09 -0700145 private ConversationUpdater mUpdater;
Vikram Aggarwal81a4f082012-09-28 09:19:04 -0700146 /** Hash of the Conversation Cursor we last obtained from the controller. */
147 private int mConversationCursorHash;
Vikram Aggarwal4f1cc0b2012-08-02 15:16:30 -0700148
Vikram Aggarwal6c511582012-02-27 10:59:47 -0800149 /**
mindyp9365a822012-09-12 09:09:09 -0700150 * Constructor needs to be public to handle orientation changes and activity
151 * lifecycle events.
Vikram Aggarwal6c511582012-02-27 10:59:47 -0800152 */
Paul Westbrookefc9f122012-02-21 11:14:49 -0800153 public ConversationListFragment() {
Vikram Aggarwald247dc92012-02-10 15:49:01 -0800154 super();
Vikram Aggarwald247dc92012-02-10 15:49:01 -0800155 }
156
Vikram Aggarwala91d00b2013-01-18 12:00:37 -0800157 private class ConversationCursorObserver extends DataSetObserver {
Mindy Pereira70a70c92012-08-02 08:39:45 -0700158 @Override
159 public void onChanged() {
Mindy Pereira70a70c92012-08-02 08:39:45 -0700160 onConversationListStatusUpdated();
161 }
162 }
163
Vikram Aggarwal7d602882012-02-07 15:01:20 -0800164 /**
mindyp9365a822012-09-12 09:09:09 -0700165 * Creates a new instance of {@link ConversationListFragment}, initialized
166 * to display conversation list context.
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -0800167 */
168 public static ConversationListFragment newInstance(ConversationListContext viewContext) {
169 ConversationListFragment fragment = new ConversationListFragment();
170 Bundle args = new Bundle();
171 args.putBundle(CONVERSATION_LIST_KEY, viewContext.toBundle());
172 fragment.setArguments(args);
173 return fragment;
174 }
175
176 /**
mindyp9365a822012-09-12 09:09:09 -0700177 * Show the header if the current conversation list is showing search
178 * results.
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -0800179 */
Mindy Pereira967ede62012-03-22 09:29:09 -0700180 void configureSearchResultHeader() {
Mindy Pereira755fd6e2012-03-21 15:15:44 -0700181 if (mActivity == null) {
182 return;
183 }
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -0800184 // Only show the header if the context is for a search result
185 final Resources res = getResources();
Vikram Aggarwalae4ea992012-08-07 09:56:02 -0700186 final boolean showHeader = ConversationListContext.isSearchResult(mViewContext);
mindyp9365a822012-09-12 09:09:09 -0700187 // TODO(viki): This code contains intimate understanding of the view.
188 // Much of this logic
189 // needs to reside in a separate class that handles the text view in
190 // isolation. Then,
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -0800191 // that logic can be reused in other fragments.
192 if (showHeader) {
193 mSearchStatusTextView.setText(res.getString(R.string.search_results_searching_header));
194 // Initially reset the count
195 mSearchResultCountTextView.setText("");
196 }
197 mSearchStatusView.setVisibility(showHeader ? View.VISIBLE : View.GONE);
198 int marginTop = showHeader ? (int) res.getDimension(R.dimen.notification_view_height) : 0;
199 MarginLayoutParams layoutParams = (MarginLayoutParams) mListView.getLayoutParams();
200 layoutParams.topMargin = marginTop;
201 mListView.setLayoutParams(layoutParams);
202 }
203
Vikram Aggarwal80aeac52012-02-07 15:27:20 -0800204 /**
mindyp9365a822012-09-12 09:09:09 -0700205 * Show the header if the current conversation list is showing search
206 * results.
Mindy Pereira68f2e222012-03-07 10:36:54 -0800207 */
208 private void updateSearchResultHeader(int count) {
Mindy Pereira71a8f292012-07-26 16:30:48 -0700209 if (mActivity == null) {
210 return;
211 }
Mindy Pereira68f2e222012-03-07 10:36:54 -0800212 // Only show the header if the context is for a search result
213 final Resources res = getResources();
Vikram Aggarwalae4ea992012-08-07 09:56:02 -0700214 final boolean showHeader = ConversationListContext.isSearchResult(mViewContext);
Mindy Pereira68f2e222012-03-07 10:36:54 -0800215 if (showHeader) {
216 mSearchStatusTextView.setText(res.getString(R.string.search_results_header));
217 mSearchResultCountTextView
218 .setText(res.getString(R.string.search_results_loaded, count));
219 }
220 }
221
222 /**
Vikram Aggarwal80aeac52012-02-07 15:27:20 -0800223 * Initializes all internal state for a rendering.
224 */
225 private void initializeUiForFirstDisplay() {
mindyp9365a822012-09-12 09:09:09 -0700226 // TODO(mindyp): find some way to make the notification container more
227 // re-usable.
228 // TODO(viki): refactor according to comment in
229 // configureSearchResultHandler()
Vikram Aggarwal80aeac52012-02-07 15:27:20 -0800230 mSearchStatusView = mActivity.findViewById(R.id.search_status_view);
231 mSearchStatusTextView = (TextView) mActivity.findViewById(R.id.search_status_text_view);
mindyp9365a822012-09-12 09:09:09 -0700232 mSearchResultCountTextView = (TextView) mActivity
233 .findViewById(R.id.search_result_count_view);
Vikram Aggarwal80aeac52012-02-07 15:27:20 -0800234 }
235
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -0800236 @Override
Vikram Aggarwal0509bba2013-01-29 17:11:30 -0800237 public void onActivityCreated(Bundle savedState) {
238 super.onActivityCreated(savedState);
mindyp9365a822012-09-12 09:09:09 -0700239 // Strictly speaking, we get back an android.app.Activity from
240 // getActivity. However, the
241 // only activity creating a ConversationListContext is a MailActivity
242 // which is of type
243 // ControllableActivity, so this cast should be safe. If this cast
244 // fails, some other
245 // activity is creating ConversationListFragments. This activity must be
246 // of type
Vikram Aggarwal80aeac52012-02-07 15:27:20 -0800247 // ControllableActivity.
248 final Activity activity = getActivity();
mindyp9365a822012-09-12 09:09:09 -0700249 if (!(activity instanceof ControllableActivity)) {
250 LogUtils.e(LOG_TAG, "ConversationListFragment expects only a ControllableActivity to"
251 + "create it. Cannot proceed.");
Vikram Aggarwal80aeac52012-02-07 15:27:20 -0800252 }
253 mActivity = (ControllableActivity) activity;
mindyp9365a822012-09-12 09:09:09 -0700254 // Since we now have a controllable activity, load the account from it,
255 // and register for
Vikram Aggarwal7c401b72012-08-13 16:43:47 -0700256 // future account changes.
257 mAccount = mAccountObserver.initialize(mActivity.getAccountController());
Vikram Aggarwal7d602882012-02-07 15:01:20 -0800258 mCallbacks = mActivity.getListHandler();
Andrew Sappersteinc2c9dc12012-07-02 18:17:32 -0700259 mErrorListener = mActivity.getErrorListener();
Mindy Pereira278fd222012-07-26 15:23:10 -0700260 // Start off with the current state of the folder being viewed.
Alice Yang20323162013-04-17 10:37:41 -0700261 Context activityContext = mActivity.getActivityContext();
Mindy Pereira6681f6b2012-03-09 13:55:54 -0800262 mFooterView = (ConversationListFooterView) LayoutInflater.from(
Alice Yang20323162013-04-17 10:37:41 -0700263 activityContext).inflate(R.layout.conversation_list_footer_view,
Mindy Pereira6681f6b2012-03-09 13:55:54 -0800264 null);
Paul Westbrook4969e0c2012-08-20 14:38:39 -0700265 mFooterView.setClickListener(mActivity);
Alice Yangc36fa722013-05-05 01:23:46 -0700266 mConversationListView.setActivity(mActivity);
Vikram Aggarwal81a4f082012-09-28 09:19:04 -0700267 final ConversationCursor conversationCursor = getConversationListCursor();
Scott Kennedy7c8325d2013-02-28 10:46:10 -0800268
269 final ConversationListHelper helper = mActivity.getConversationListHelper();
Mark Wei2102b2c2013-05-02 17:15:30 -0700270 final List<ConversationSpecialItemView> specialItemViews = helper != null ?
271 ImmutableList.copyOf(helper.makeConversationListSpecialViews(
272 getActivity(), mAccount, mActivity.getFolderListSelectionListener()))
273 : null;
Scott Kennedy7c8325d2013-02-28 10:46:10 -0800274 if (specialItemViews != null) {
275 // Attach to the LoaderManager
276 for (final ConversationSpecialItemView view : specialItemViews) {
277 view.bindLoaderManager(getLoaderManager());
278 }
279 }
280
281 mListAdapter = new AnimatedAdapter(mActivity.getApplicationContext(), conversationCursor,
282 mActivity.getSelectedSet(), mActivity, mListView, specialItemViews);
Mindy Pereira6681f6b2012-03-09 13:55:54 -0800283 mListAdapter.addFooter(mFooterView);
Mindy Pereira6f4a6af2012-02-29 11:48:52 -0800284 mListView.setAdapter(mListAdapter);
Vikram Aggarwal4f1cc0b2012-08-02 15:16:30 -0700285 mSelectedSet = mActivity.getSelectedSet();
286 mListView.setSelectionSet(mSelectedSet);
Mindy Pereiradc8963b2012-03-28 17:51:05 -0700287 mListAdapter.hideFooter();
Vikram Aggarwal50ff0e52013-03-14 13:58:02 -0700288 mFolderObserver = new FolderObserver(){
289 @Override
290 public void onChanged(Folder newFolder) {
291 onFolderUpdated(newFolder);
292 }
293 };
294 mFolderObserver.initialize(mActivity.getFolderController());
Vikram Aggarwala91d00b2013-01-18 12:00:37 -0800295 mConversationCursorObserver = new ConversationCursorObserver();
mindyp9365a822012-09-12 09:09:09 -0700296 mUpdater = mActivity.getConversationUpdater();
Vikram Aggarwala91d00b2013-01-18 12:00:37 -0800297 mUpdater.registerConversationListObserver(mConversationCursorObserver);
Vikram Aggarwalbcb16b92013-01-28 18:05:03 -0800298 mTabletDevice = Utils.useTabletUI(mActivity.getApplicationContext().getResources());
Vikram Aggarwal80aeac52012-02-07 15:27:20 -0800299 initializeUiForFirstDisplay();
Mindy Pereiradc8963b2012-03-28 17:51:05 -0700300 configureSearchResultHeader();
mindyp9365a822012-09-12 09:09:09 -0700301 // The onViewModeChanged callback doesn't get called when the mode
302 // object is created, so
Vikram Aggarwalfa131a22012-02-02 13:56:22 -0800303 // force setting the mode manually this time around.
Mindy Pereiraf96ec322012-03-02 14:06:33 -0800304 onViewModeChanged(mActivity.getViewMode().getMode());
Mindy Pereirab5901be2012-08-09 17:21:53 -0700305 mActivity.getViewMode().addListener(this);
Paul Westbrook0e3fd9d2012-04-20 02:02:23 -0700306
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -0800307 if (mActivity.isFinishing()) {
308 // Activity is finishing, just bail.
309 return;
310 }
Vikram Aggarwal81a4f082012-09-28 09:19:04 -0700311 mConversationCursorHash = (conversationCursor == null) ? 0 : conversationCursor.hashCode();
312 // Belt and suspenders here; make sure we do any necessary sync of the
313 // ConversationCursor
314 if (conversationCursor != null && conversationCursor.isRefreshReady()) {
315 conversationCursor.sync();
316 }
317
Vikram Aggarwal0509bba2013-01-29 17:11:30 -0800318 // On a phone we never highlight a conversation, so the default is to select none.
319 // On a tablet, we highlight a SINGLE conversation in landscape conversation view.
320 int choice = getDefaultChoiceMode(mTabletDevice);
321 if (savedState != null) {
322 // Restore the choice mode if it was set earlier, or NONE if creating a fresh view.
323 // Choice mode here represents the current conversation only. CAB mode does not rely on
324 // the platform: checked state is a local variable {@link ConversationItemView#mChecked}
325 choice = savedState.getInt(CHOICE_MODE_KEY, choice);
326 if (savedState.containsKey(LIST_STATE_KEY)) {
327 // TODO: find a better way to unset the selected item when restoring
328 mListView.clearChoices();
329 }
330 }
331 setChoiceMode(choice);
332
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -0800333 // Show list and start loading list.
334 showList();
Mindy Pereira4765c5c2012-07-19 11:58:22 -0700335 ToastBarOperation pendingOp = mActivity.getPendingToastOperation();
336 if (pendingOp != null) {
337 // Clear the pending operation
338 mActivity.setPendingToastOperation(null);
339 mActivity.onUndoAvailable(pendingOp);
340 }
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -0800341 }
342
Vikram Aggarwal0509bba2013-01-29 17:11:30 -0800343 /**
344 * Returns the default choice mode for the list based on whether the list is displayed on tablet
345 * or not.
346 * @param isTablet
347 * @return
348 */
349 private final static int getDefaultChoiceMode(boolean isTablet) {
350 return isTablet ? ListView.CHOICE_MODE_SINGLE : ListView.CHOICE_MODE_NONE;
351 }
352
Mindy Pereira967ede62012-03-22 09:29:09 -0700353 public AnimatedAdapter getAnimatedAdapter() {
354 return mListAdapter;
355 }
356
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -0800357 @Override
Vikram Aggarwald247dc92012-02-10 15:49:01 -0800358 public void onCreate(Bundle savedState) {
Vikram Aggarwald247dc92012-02-10 15:49:01 -0800359 super.onCreate(savedState);
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -0800360
361 // Initialize fragment constants from resources
Vikram Aggarwal6c511582012-02-27 10:59:47 -0800362 final Resources res = getResources();
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -0800363 TIMESTAMP_UPDATE_INTERVAL = res.getInteger(R.integer.timestamp_update_interval);
mindyp9365a822012-09-12 09:09:09 -0700364 mUpdateTimestampsRunnable = new Runnable() {
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -0800365 @Override
366 public void run() {
367 mListView.invalidateViews();
368 mHandler.postDelayed(mUpdateTimestampsRunnable, TIMESTAMP_UPDATE_INTERVAL);
369 }
370 };
371
Vikram Aggarwal80aeac52012-02-07 15:27:20 -0800372 // Get the context from the arguments
Vikram Aggarwal6c511582012-02-27 10:59:47 -0800373 final Bundle args = getArguments();
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -0800374 mViewContext = ConversationListContext.forBundle(args.getBundle(CONVERSATION_LIST_KEY));
Mindy Pereira3982e232012-02-29 15:00:34 -0800375 mAccount = mViewContext.account;
Paul Westbrook80ecd892012-08-16 13:37:39 -0700376
Vikram Aggarwal9a49c9b2012-08-31 10:49:33 -0700377 setRetainInstance(false);
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -0800378 }
379
380 @Override
Andy Huang9e4ca792013-02-28 14:33:43 -0800381 public String toString() {
382 final String s = super.toString();
383 if (mViewContext == null) {
384 return s;
385 }
Andy Huangc1922a92013-05-13 14:33:05 -0700386 final StringBuilder sb = new StringBuilder(s);
387 sb.setLength(sb.length() - 1);
388 sb.append(" mListAdapter=");
389 sb.append(mListAdapter);
390 sb.append(" folder=");
391 sb.append(mViewContext.folder);
392 sb.append("}");
393 return sb.toString();
Andy Huang9e4ca792013-02-28 14:33:43 -0800394 }
395
396 @Override
Vikram Aggarwal9a49c9b2012-08-31 10:49:33 -0700397 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState) {
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -0800398 View rootView = inflater.inflate(R.layout.conversation_list, null);
Mindy Pereira6cc95532012-08-15 16:04:56 -0700399 mEmptyView = rootView.findViewById(R.id.empty_view);
Alice Yang486e63e2013-04-05 13:01:50 -0700400 mConversationListView =
401 (ConversationListView) rootView.findViewById(R.id.conversation_list);
Alice Yang20323162013-04-17 10:37:41 -0700402 mConversationListView.setConversationContext(mViewContext);
Mindy Pereiraf6a6b502012-03-15 15:24:26 -0700403 mListView = (SwipeableListView) rootView.findViewById(android.R.id.list);
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -0800404 mListView.setHeaderDividersEnabled(false);
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -0800405 mListView.setOnItemLongClickListener(this);
Mindy Pereira4e812f42012-07-30 16:52:49 -0700406 mListView.enableSwipe(mAccount.supportsCapability(AccountCapabilities.UNDO));
mindyp9365a822012-09-12 09:09:09 -0700407 mListView.setSwipedListener(this);
Marc Blank2af0cf32012-03-01 19:24:13 -0800408
Vikram Aggarwal0509bba2013-01-29 17:11:30 -0800409 if (savedState != null && savedState.containsKey(LIST_STATE_KEY)) {
410 mListView.onRestoreInstanceState(savedState.getParcelable(LIST_STATE_KEY));
Vikram Aggarwal9a49c9b2012-08-31 10:49:33 -0700411 }
Alice Yang0d74a662013-03-25 14:01:24 -0700412
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -0800413 return rootView;
414 }
415
Vikram Aggarwala91d00b2013-01-18 12:00:37 -0800416 /**
417 * Sets the choice mode of the list view
418 * @param choiceMode ListView#
419 */
420 private final void setChoiceMode(int choiceMode) {
421 mListView.setChoiceMode(choiceMode);
422 }
423
424 /**
425 * Tell the list to select nothing.
426 */
427 public final void setChoiceNone() {
Vikram Aggarwal0509bba2013-01-29 17:11:30 -0800428 // On a phone, the default choice mode is already none, so nothing to do.
429 if (!mTabletDevice) {
430 return;
431 }
Andy Huangb0be3fc2013-05-02 15:48:05 -0700432 clearChoicesAndActivated();
Vikram Aggarwala91d00b2013-01-18 12:00:37 -0800433 setChoiceMode(ListView.CHOICE_MODE_NONE);
434 }
435
436 /**
437 * Tell the list to get out of selecting none.
438 */
439 public final void revertChoiceMode() {
Vikram Aggarwal0509bba2013-01-29 17:11:30 -0800440 // On a phone, the default choice mode is always none, so nothing to do.
441 if (!mTabletDevice) {
442 return;
443 }
444 setChoiceMode(getDefaultChoiceMode(mTabletDevice));
Vikram Aggarwala91d00b2013-01-18 12:00:37 -0800445 }
446
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -0800447 @Override
Andy Huang7517e3b2012-08-20 12:30:08 -0700448 public void onDestroy() {
Andy Huang7517e3b2012-08-20 12:30:08 -0700449 super.onDestroy();
450 }
451
452 @Override
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -0800453 public void onDestroyView() {
Andy Huang7517e3b2012-08-20 12:30:08 -0700454
Vikram Aggarwal7c401b72012-08-13 16:43:47 -0700455 // Clear the list's adapter
456 mListAdapter.destroy();
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -0800457 mListView.setAdapter(null);
458
Vikram Aggarwald7a12cd2012-02-03 09:36:20 -0800459 mActivity.unsetViewModeListener(this);
Mindy Pereira71a8f292012-07-26 16:30:48 -0700460 if (mFolderObserver != null) {
Vikram Aggarwal50ff0e52013-03-14 13:58:02 -0700461 mFolderObserver.unregisterAndDestroy();
Mindy Pereira71a8f292012-07-26 16:30:48 -0700462 mFolderObserver = null;
463 }
Vikram Aggarwala91d00b2013-01-18 12:00:37 -0800464 if (mConversationCursorObserver != null) {
465 mUpdater.unregisterConversationListObserver(mConversationCursorObserver);
466 mConversationCursorObserver = null;
Mindy Pereira70a70c92012-08-02 08:39:45 -0700467 }
Vikram Aggarwal7c401b72012-08-13 16:43:47 -0700468 mAccountObserver.unregisterAndDestroy();
Scott Kennedy7c8325d2013-02-28 10:46:10 -0800469 getAnimatedAdapter().cleanup();
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -0800470 super.onDestroyView();
471 }
472
Vikram Aggarwal9730ea02012-08-02 12:46:19 -0700473 /**
mindyp9365a822012-09-12 09:09:09 -0700474 * There are three binary variables, which determine what we do with a
475 * message. checkbEnabled: Whether check boxes are enabled or not (forced
476 * true on tablet) cabModeOn: Whether CAB mode is currently on or not.
477 * pressType: long or short tap (There is a third possibility: phone or
478 * tablet, but they have <em>identical</em> behavior) The matrix of
479 * possibilities is:
480 * <p>
481 * Long tap: Always toggle selection of conversation. If CAB mode is not
482 * started, then start it.
Vikram Aggarwal9730ea02012-08-02 12:46:19 -0700483 * <pre>
484 * | Checkboxes | No Checkboxes
485 * ----------+------------+---------------
486 * CAB mode | Select | Select
487 * List mode | Select | Select
488 *
Vikram Aggarwal9730ea02012-08-02 12:46:19 -0700489 * </pre>
mindyp9365a822012-09-12 09:09:09 -0700490 *
Vikram Aggarwal9730ea02012-08-02 12:46:19 -0700491 * Reference: http://b/issue?id=6392199
492 * <p>
493 * {@inheritDoc}
494 */
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -0800495 @Override
496 public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Mindy Pereira1e231a62012-03-23 12:42:15 -0700497 // Ignore anything that is not a conversation item. Could be a footer.
498 if (!(view instanceof ConversationItemView)) {
Scott Kennedy7c8325d2013-02-28 10:46:10 -0800499 return false;
Mindy Pereira1e231a62012-03-23 12:42:15 -0700500 }
Alice Yang4758e982013-04-24 20:31:34 -0700501 ((ConversationItemView) view).toggleSelectedStateOrBeginDrag();
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -0800502 return true;
503 }
504
Vikram Aggarwal4f1cc0b2012-08-02 15:16:30 -0700505 /**
mindyp9365a822012-09-12 09:09:09 -0700506 * See the comment for
507 * {@link #onItemLongClick(AdapterView, View, int, long)}.
508 * <p>
509 * Short tap behavior:
510 *
Vikram Aggarwal4f1cc0b2012-08-02 15:16:30 -0700511 * <pre>
512 * | Checkboxes | No Checkboxes
513 * ----------+------------+---------------
514 * CAB mode | Peek | Select
515 * List mode | Peek | Peek
516 * </pre>
mindyp9365a822012-09-12 09:09:09 -0700517 *
Vikram Aggarwal4f1cc0b2012-08-02 15:16:30 -0700518 * Reference: http://b/issue?id=6392199
519 * <p>
520 * {@inheritDoc}
521 */
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -0800522 @Override
Vikram Aggarwal54452ae2012-03-13 15:29:00 -0700523 public void onListItemClick(ListView l, View view, int position, long id) {
Mindy Pereira1e231a62012-03-23 12:42:15 -0700524 // Ignore anything that is not a conversation item. Could be a footer.
mindyp8694fe92012-09-25 11:07:16 -0700525 // If we are using a keyboard, the highlighted item is the parent;
526 // otherwise, this is a direct call from the ConverationItemView
mindypc03b05a2012-09-28 14:23:48 -0700527 if (!(view instanceof ToggleableItem)) {
Mindy Pereira1e231a62012-03-23 12:42:15 -0700528 return;
529 }
Alice Yang4758e982013-04-24 20:31:34 -0700530 boolean showSenderImage = (mAccount.settings.convListIcon ==
531 ConversationListIcon.SENDER_IMAGE);
532 if (!showSenderImage && !mSelectedSet.isEmpty()) {
mindypc03b05a2012-09-28 14:23:48 -0700533 ToggleableItem v = (ToggleableItem) view;
Mark Wei6126d722013-04-24 21:09:43 -0700534 v.toggleSelectedState();
Vikram Aggarwal4f1cc0b2012-08-02 15:16:30 -0700535 } else {
536 viewConversation(position);
537 }
mindyp9365a822012-09-12 09:09:09 -0700538 // When a new list item is clicked, commit any existing leave behind
mindyp8694fe92012-09-25 11:07:16 -0700539 // items. Wait until we have opened the desired conversation to cause
540 // any position changes.
Vikram Aggarwalbcb16b92013-01-28 18:05:03 -0800541 commitDestructiveActions(Utils.useTabletUI(mActivity.getActivityContext().getResources()));
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -0800542 }
543
544 @Override
Scott Kennedy08a079c2013-02-22 10:13:10 -0800545 public void onResume() {
546 super.onResume();
547
548 final ConversationCursor conversationCursor = getConversationListCursor();
549 if (conversationCursor != null) {
550 conversationCursor.handleNotificationActions();
551 }
552 }
553
554 @Override
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -0800555 public void onPause() {
556 super.onPause();
557 }
558
559 @Override
560 public void onSaveInstanceState(Bundle outState) {
561 super.onSaveInstanceState(outState);
562 if (mListView != null) {
563 outState.putParcelable(LIST_STATE_KEY, mListView.onSaveInstanceState());
Vikram Aggarwala91d00b2013-01-18 12:00:37 -0800564 outState.putInt(CHOICE_MODE_KEY, mListView.getChoiceMode());
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -0800565 }
566 }
567
568 @Override
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -0800569 public void onStart() {
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -0800570 super.onStart();
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -0800571 mHandler.postDelayed(mUpdateTimestampsRunnable, TIMESTAMP_UPDATE_INTERVAL);
572 }
573
574 @Override
575 public void onStop() {
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -0800576 super.onStop();
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -0800577 mHandler.removeCallbacks(mUpdateTimestampsRunnable);
578 }
579
Vikram Aggarwal80aeac52012-02-07 15:27:20 -0800580 @Override
Vikram Aggarwalfa131a22012-02-02 13:56:22 -0800581 public void onViewModeChanged(int newMode) {
Vikram Aggarwalfa131a22012-02-02 13:56:22 -0800582 if (mTabletDevice) {
Andy Huangb0be3fc2013-05-02 15:48:05 -0700583 if (ViewMode.isListMode(newMode)) {
Vikram Aggarwal0509bba2013-01-29 17:11:30 -0800584 // There are no selected conversations when in conversation list mode.
Andy Huangb0be3fc2013-05-02 15:48:05 -0700585 clearChoicesAndActivated();
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -0800586 }
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -0800587 }
Mindy Pereirab5901be2012-08-09 17:21:53 -0700588 if (mFooterView != null) {
589 mFooterView.onViewModeChanged(newMode);
590 }
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -0800591 }
Mindy Pereirab5901be2012-08-09 17:21:53 -0700592
Andy Huangb0be3fc2013-05-02 15:48:05 -0700593 private void clearChoicesAndActivated() {
594 final int currentSelected = mListView.getCheckedItemPosition();
Andy Huangb0be3fc2013-05-02 15:48:05 -0700595 if (currentSelected != ListView.INVALID_POSITION) {
Andy Huangf0aebd32013-06-17 15:00:15 -0700596 mListView.setItemChecked(mListView.getCheckedItemPosition(), false);
Andy Huangb0be3fc2013-05-02 15:48:05 -0700597 }
598 }
599
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -0800600 /**
mindyp9365a822012-09-12 09:09:09 -0700601 * Handles a request to show a new conversation list, either from a search
602 * query or for viewing a folder. This will initiate a data load, and hence
603 * must be called on the UI thread.
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -0800604 */
605 private void showList() {
Vikram Aggarwal80aeac52012-02-07 15:27:20 -0800606 mListView.setEmptyView(null);
Andy Huang7591d2f2012-07-26 16:48:06 -0700607 onFolderUpdated(mActivity.getFolderController().getFolder());
mindypca8ca2d2012-09-11 17:38:34 -0700608 onConversationListStatusUpdated();
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -0800609 }
610
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -0800611 /**
612 * View the message at the given position.
Scott Kennedy7c8325d2013-02-28 10:46:10 -0800613 *
614 * @param position The position of the conversation in the list (as opposed to its position in
615 * the cursor)
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -0800616 */
Scott Kennedy7c8325d2013-02-28 10:46:10 -0800617 protected void viewConversation(final int position) {
Vikram Aggarwalc7694222012-04-23 13:37:01 -0700618 LogUtils.d(LOG_TAG, "ConversationListFragment.viewConversation(%d)", position);
Scott Kennedy7c8325d2013-02-28 10:46:10 -0800619
620 final ConversationCursor cursor =
621 (ConversationCursor) getAnimatedAdapter().getItem(position);
Andy Huang97b10962013-04-02 13:33:32 -0700622
623 if (cursor == null) {
624 LogUtils.e(LOG_TAG,
625 "unable to open conv at cursor pos=%s cursor=%s getPositionOffset=%s",
626 position, cursor, getAnimatedAdapter().getPositionOffset(position));
627 return;
628 }
629
Scott Kennedy7c8325d2013-02-28 10:46:10 -0800630 final Conversation conv = cursor.getConversation();
631 /*
632 * The cursor position may be different than the position method parameter because of
633 * special views in the list.
634 */
635 conv.position = cursor.getPosition();
636 setSelected(conv.position, true);
637 mCallbacks.onConversationSelected(conv, false /* inLoaderCallbacks */);
Vikram Aggarwal3d7ca9d2012-05-11 14:40:36 -0700638 }
639
Vikram Aggarwala91d00b2013-01-18 12:00:37 -0800640 /**
641 * Sets the selected conversation to the position given here.
Alice Yang0d74a662013-03-25 14:01:24 -0700642 * @param cursorPosition The position of the conversation in the cursor (as opposed to
643 * in the list)
Vikram Aggarwala91d00b2013-01-18 12:00:37 -0800644 * @param different if the currently selected conversation is different from the one provided
645 * here. This is a difference in conversations, not a difference in positions. For example, a
646 * conversation at position 2 can move to position 4 as a result of new mail.
647 */
Scott Kennedy7c8325d2013-02-28 10:46:10 -0800648 public void setSelected(final int cursorPosition, boolean different) {
Vikram Aggarwala91d00b2013-01-18 12:00:37 -0800649 if (mListView.getChoiceMode() == ListView.CHOICE_MODE_NONE) {
650 return;
651 }
Scott Kennedy7c8325d2013-02-28 10:46:10 -0800652
653 final int position =
654 cursorPosition + getAnimatedAdapter().getPositionOffset(cursorPosition);
655
mindype21f8862012-10-01 09:32:03 -0700656 if (different) {
657 mListView.smoothScrollToPosition(position);
658 }
659 mListView.setItemChecked(position, true);
660 }
661
Vikram Aggarwal3d7ca9d2012-05-11 14:40:36 -0700662 /**
Vikram Aggarwala91d00b2013-01-18 12:00:37 -0800663 * Returns the cursor associated with the conversation list.
664 * @return
Vikram Aggarwal3d7ca9d2012-05-11 14:40:36 -0700665 */
Mindy Pereira967ede62012-03-22 09:29:09 -0700666 private ConversationCursor getConversationListCursor() {
667 return mCallbacks != null ? mCallbacks.getConversationListCursor() : null;
Mindy Pereirafe06bea2012-02-16 08:15:14 -0800668 }
669
670 /**
mindyp9365a822012-09-12 09:09:09 -0700671 * Request a refresh of the list. No sync is carried out and none is
672 * promised.
Vikram Aggarwal54452ae2012-03-13 15:29:00 -0700673 */
674 public void requestListRefresh() {
675 mListAdapter.notifyDataSetChanged();
676 }
677
Vikram Aggarwal75daee52012-04-30 11:13:09 -0700678 /**
Vikram Aggarwal7f602f72012-04-30 16:04:06 -0700679 * Change the UI to delete the conversations provided and then call the
mindyp9365a822012-09-12 09:09:09 -0700680 * {@link DestructiveAction} provided here <b>after</b> the UI has been
681 * updated.
Vikram Aggarwal7f602f72012-04-30 16:04:06 -0700682 * @param conversations
Vikram Aggarwal75daee52012-04-30 11:13:09 -0700683 * @param action
684 */
Vikram Aggarwala8e43182012-09-13 12:55:10 -0700685 public void requestDelete(int actionId, final Collection<Conversation> conversations,
Vikram Aggarwal669947b2013-01-10 17:05:56 -0800686 final DestructiveAction action) {
Mindy Pereiraacf60392012-04-06 09:11:00 -0700687 for (Conversation conv : conversations) {
688 conv.localDeleteOnUpdate = true;
689 }
Vikram Aggarwala8e43182012-09-13 12:55:10 -0700690 final ListItemsRemovedListener listener = new ListItemsRemovedListener() {
mindyp9365a822012-09-12 09:09:09 -0700691 @Override
692 public void onListItemsRemoved() {
693 action.performAction();
694 }
Vikram Aggarwala8e43182012-09-13 12:55:10 -0700695 };
696 final SwipeableListView listView = (SwipeableListView) getListView();
697 if (listView.getSwipeAction() == actionId) {
Paul Westbrookcec3e0b2013-02-04 22:50:22 -0800698 if (!listView.destroyItems(conversations, listener)) {
699 // The listView failed to destroy the items, perform the action manually
700 LogUtils.e(LOG_TAG, "ConversationListFragment.requestDelete: " +
701 "listView failed to destroy items.");
702 action.performAction();
703 }
Vikram Aggarwala8e43182012-09-13 12:55:10 -0700704 return;
705 }
706 // Delete the local delete items (all for now) and when done,
707 // update...
708 mListAdapter.delete(conversations, listener);
Mindy Pereiraacf60392012-04-06 09:11:00 -0700709 }
Mindy Pereiraa3911aa2012-03-09 13:26:26 -0800710
Mindy Pereira11dd5ef2012-03-10 15:10:18 -0800711 public void onFolderUpdated(Folder folder) {
Mindy Pereiradc8963b2012-03-28 17:51:05 -0700712 mFolder = folder;
Mindy Pereira06642fa2012-07-12 16:23:27 -0700713 setSwipeAction();
Mindy Pereira82cea482012-03-27 16:45:00 -0700714 if (mFolder == null) {
715 return;
716 }
Mindy Pereira4584a0d2012-03-13 14:42:14 -0700717 mListAdapter.setFolder(mFolder);
Mindy Pereira70a70c92012-08-02 08:39:45 -0700718 mFooterView.setFolder(mFolder);
Vikram Aggarwal41b9e8f2012-09-25 10:15:04 -0700719 if (!mFolder.wasSyncSuccessful()) {
Mindy Pereira70a70c92012-08-02 08:39:45 -0700720 mErrorListener.onError(mFolder, false);
721 }
Paul Westbrookc01f5d92012-09-25 17:22:25 -0700722
723 // Notify of changes to the Folder.
724 onFolderStatusUpdated();
725
Mindy Pereira12fe37a2012-08-15 10:02:57 -0700726 // Blow away conversation items cache.
727 ConversationItemViewModel.onFolderUpdated(mFolder);
Mindy Pereira70a70c92012-08-02 08:39:45 -0700728 }
729
Vikram Aggarwal81a4f082012-09-28 09:19:04 -0700730 /**
731 * Updates the footer visibility and updates the conversation cursor
732 */
Mindy Pereira70a70c92012-08-02 08:39:45 -0700733 public void onConversationListStatusUpdated() {
Paul Westbrook9a70e912012-08-17 15:53:20 -0700734 final ConversationCursor cursor = getConversationListCursor();
735 final boolean showFooter = cursor != null && mFooterView.updateStatus(cursor);
Paul Westbrookc01f5d92012-09-25 17:22:25 -0700736 // Update the folder status, in case the cursor could affect it.
737 onFolderStatusUpdated();
738 mListAdapter.setFooterVisibility(showFooter);
739
740 // Also change the cursor here.
741 onCursorUpdated();
742 }
743
744 private void onFolderStatusUpdated() {
Alice Yang0d74a662013-03-25 14:01:24 -0700745 // Update the sync status bar with sync results if needed
Alice Yang486e63e2013-04-05 13:01:50 -0700746 checkSyncStatus();
Alice Yang0d74a662013-03-25 14:01:24 -0700747
Paul Westbrookc01f5d92012-09-25 17:22:25 -0700748 final ConversationCursor cursor = getConversationListCursor();
Paul Westbrook9a70e912012-08-17 15:53:20 -0700749 Bundle extras = cursor != null ? cursor.getExtras() : Bundle.EMPTY;
Andrew Sapperstein427df9d2013-05-15 12:05:09 -0700750 int errorStatus = extras.containsKey(UIProvider.CursorExtraKeys.EXTRA_ERROR) ?
Mindy Pereira70a70c92012-08-02 08:39:45 -0700751 extras.getInt(UIProvider.CursorExtraKeys.EXTRA_ERROR)
752 : UIProvider.LastSyncResult.SUCCESS;
Andrew Sapperstein427df9d2013-05-15 12:05:09 -0700753 int cursorStatus = extras.getInt(UIProvider.CursorExtraKeys.EXTRA_STATUS);
Alice Yang0d74a662013-03-25 14:01:24 -0700754 // We want to update the UI with this information if either we are loaded or complete, or
Paul Westbrookc01f5d92012-09-25 17:22:25 -0700755 // we have a folder with a non-0 count.
756 final int folderCount = mFolder != null ? mFolder.totalCount : 0;
Andrew Sapperstein427df9d2013-05-15 12:05:09 -0700757 if (errorStatus == UIProvider.LastSyncResult.SUCCESS
758 && (cursorStatus == UIProvider.CursorStatus.LOADED
759 || cursorStatus == UIProvider.CursorStatus.COMPLETE) || folderCount > 0) {
Paul Westbrookc01f5d92012-09-25 17:22:25 -0700760 updateSearchResultHeader(folderCount);
761 if (folderCount == 0) {
Mindy Pereira6cc95532012-08-15 16:04:56 -0700762 mListView.setEmptyView(mEmptyView);
763 }
Mindy Pereira70a70c92012-08-02 08:39:45 -0700764 }
Mindy Pereiraa3911aa2012-03-09 13:26:26 -0800765 }
Mindy Pereiraf6a6b502012-03-15 15:24:26 -0700766
Mindy Pereira06642fa2012-07-12 16:23:27 -0700767 private void setSwipeAction() {
Vikram Aggarwal7c401b72012-08-13 16:43:47 -0700768 int swipeSetting = Settings.getSwipeSetting(mAccount.settings);
Mindy Pereirae58222b2012-07-25 14:33:18 -0700769 if (swipeSetting == Swipe.DISABLED
Mindy Pereirab8073372012-08-09 14:00:10 -0700770 || !mAccount.supportsCapability(AccountCapabilities.UNDO)
771 || (mFolder != null && mFolder.isTrash())) {
Mindy Pereirae58222b2012-07-25 14:33:18 -0700772 mListView.enableSwipe(false);
773 } else {
774 int action;
Rohan Shah7f98d0d2013-02-15 13:21:09 -0800775 mListView.enableSwipe(true);
mindyp77c3e1b2012-09-11 14:05:02 -0700776 if (ConversationListContext.isSearchResult(mViewContext)
Scott Kennedy8c1058e2013-03-20 13:40:20 -0700777 || (mFolder != null && mFolder.isType(FolderType.SPAM))) {
Mindy Pereirae58222b2012-07-25 14:33:18 -0700778 action = R.id.delete;
Mindy Pereira4e812f42012-07-30 16:52:49 -0700779 } else if (mFolder == null) {
Mindy Pereira01f30502012-08-14 10:30:51 -0700780 action = R.id.remove_folder;
Mindy Pereira4e812f42012-07-30 16:52:49 -0700781 } else {
782 // We have enough information to respect user settings.
783 switch (swipeSetting) {
784 case Swipe.ARCHIVE:
785 if (mAccount.supportsCapability(AccountCapabilities.ARCHIVE)) {
786 if (mFolder.supportsCapability(FolderCapabilities.ARCHIVE)) {
787 action = R.id.archive;
788 break;
789 } else if (mFolder.supportsCapability
790 (FolderCapabilities.CAN_ACCEPT_MOVED_MESSAGES)) {
Mindy Pereira01f30502012-08-14 10:30:51 -0700791 action = R.id.remove_folder;
Mindy Pereira4e812f42012-07-30 16:52:49 -0700792 break;
793 }
794 }
795 case Swipe.DELETE:
796 default:
797 action = R.id.delete;
798 break;
799 }
Mindy Pereirae58222b2012-07-25 14:33:18 -0700800 }
801 mListView.setSwipeAction(action);
802 }
Mark Wei6126d722013-04-24 21:09:43 -0700803 mListView.setCurrentAccount(mAccount);
Mindy Pereira06642fa2012-07-12 16:23:27 -0700804 mListView.setCurrentFolder(mFolder);
805 }
806
Vikram Aggarwal17f373e2012-09-17 15:49:32 -0700807 /**
808 * Changes the conversation cursor in the list and sets selected position if none is set.
809 */
810 private void onCursorUpdated() {
811 if (mCallbacks == null || mListAdapter == null) {
812 return;
813 }
Vikram Aggarwal81a4f082012-09-28 09:19:04 -0700814 // Check against the previous cursor here and see if they are the same. If they are, then
815 // do a notifyDataSetChanged.
816 final ConversationCursor newCursor = mCallbacks.getConversationListCursor();
817 mListAdapter.swapCursor(newCursor);
818 // When the conversation cursor is *updated*, we get back the same instance. In that
819 // situation, CursorAdapter.swapCursor() silently returns, without forcing a
820 // notifyDataSetChanged(). So let's force a call to notifyDataSetChanged, since an updated
821 // cursor means that the dataset has changed.
822 final int newCursorHash = (newCursor == null) ? 0 : newCursor.hashCode();
823 if (mConversationCursorHash == newCursorHash && mConversationCursorHash != 0) {
824 mListAdapter.notifyDataSetChanged();
825 }
826 mConversationCursorHash = newCursorHash;
Scott Kennedyb1e21482013-03-15 13:40:38 -0700827
Scott Kennedy4b9b3952013-03-20 17:20:59 -0700828 if (newCursor != null && newCursor.getCount() > 0) {
Scott Kennedyb1e21482013-03-15 13:40:38 -0700829 newCursor.markContentsSeen();
830 }
831
Vikram Aggarwal17f373e2012-09-17 15:49:32 -0700832 // If a current conversation is available, and none is selected in the list, then ask
833 // the list to select the current conversation.
834 final Conversation conv = mCallbacks.getCurrentConversation();
835 if (conv == null) {
836 return;
837 }
Vikram Aggarwala91d00b2013-01-18 12:00:37 -0800838 if (mListView.getChoiceMode() != ListView.CHOICE_MODE_NONE
839 && mListView.getCheckedItemPosition() == -1) {
840 setSelected(conv.position, true);
Mindy Pereira967ede62012-03-22 09:29:09 -0700841 }
Mindy Pereira21ab4902012-03-19 18:48:03 -0700842 }
Mindy Pereira1e2573b2012-04-17 14:34:36 -0700843
mindypc6adce32012-08-22 18:46:42 -0700844 public void commitDestructiveActions(boolean animate) {
Mindy Pereira8937bf12012-07-23 14:05:02 -0700845 if (mListView != null) {
mindypc6adce32012-08-22 18:46:42 -0700846 mListView.commitDestructiveActions(animate);
Vikram Aggarwal7d816002012-04-17 17:06:41 -0700847
848 }
849 }
850
mindyp9365a822012-09-12 09:09:09 -0700851 @Override
852 public void onListItemSwiped(Collection<Conversation> conversations) {
853 mUpdater.showNextConversation(conversations);
854 }
Alice Yang0d74a662013-03-25 14:01:24 -0700855
Alice Yang486e63e2013-04-05 13:01:50 -0700856 private void checkSyncStatus() {
Alice Yang76d20652013-04-24 02:32:48 -0700857 if (mFolder != null && mFolder.isSyncInProgress()) {
Alice Yang03752f32013-05-05 15:05:16 -0700858 LogUtils.d(LOG_TAG, "CLF.checkSyncStatus still syncing");
Alice Yang486e63e2013-04-05 13:01:50 -0700859 // Still syncing, ignore
860 } else {
861 // Finished syncing:
Alice Yang03752f32013-05-05 15:05:16 -0700862 LogUtils.d(LOG_TAG, "CLF.checkSyncStatus done syncing");
Alice Yang486e63e2013-04-05 13:01:50 -0700863 mConversationListView.onSyncFinished();
864 }
865 }
866
Alice Yang0d74a662013-03-25 14:01:24 -0700867 /**
Alice Yang486e63e2013-04-05 13:01:50 -0700868 * Displays the indefinite progress bar indicating a sync is in progress. This
869 * should only be called if user manually requested a sync, and not for background syncs.
Alice Yang0d74a662013-03-25 14:01:24 -0700870 */
871 protected void showSyncStatusBar() {
Alice Yang486e63e2013-04-05 13:01:50 -0700872 mConversationListView.showSyncStatusBar();
Alice Yang0d74a662013-03-25 14:01:24 -0700873 }
Rohan Shahd4f22872013-04-19 17:06:37 -0700874
875 /**
876 * Clears all items in the list.
877 */
878 public void clear() {
879 mListView.setAdapter(null);
880 }
Vikram Aggarwalb9e1a352012-01-24 15:23:38 -0800881}