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 | |
Jin Cao | 5cf73b8 | 2014-04-25 11:13:51 -0700 | [diff] [blame] | 20 | import android.animation.LayoutTransition; |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 21 | import android.app.Activity; |
| 22 | import android.app.ListFragment; |
Vikram Aggarwal | 37a20ca | 2013-06-06 11:19:49 -0700 | [diff] [blame] | 23 | import android.app.LoaderManager; |
Alice Yang | 2032316 | 2013-04-17 10:37:41 -0700 | [diff] [blame] | 24 | import android.content.Context; |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 25 | import android.content.res.Resources; |
Mindy Pereira | 6d8e7fe | 2012-07-26 16:02:49 -0700 | [diff] [blame] | 26 | import android.database.DataSetObserver; |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 27 | import android.os.Bundle; |
| 28 | import android.os.Handler; |
Scott Kennedy | f77806e | 2013-08-30 11:38:15 -0700 | [diff] [blame] | 29 | import android.os.Parcelable; |
Mindy Pereira | 4bb435c | 2013-11-13 14:21:15 -0800 | [diff] [blame] | 30 | import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener; |
Alice Yang | 0d74a66 | 2013-03-25 14:01:24 -0700 | [diff] [blame] | 31 | import android.text.format.DateUtils; |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 32 | import android.view.LayoutInflater; |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 33 | import android.view.View; |
| 34 | import android.view.ViewGroup; |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 35 | import android.widget.AdapterView; |
| 36 | import android.widget.AdapterView.OnItemLongClickListener; |
| 37 | import android.widget.ListView; |
| 38 | import android.widget.TextView; |
| 39 | |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 40 | import com.android.mail.ConversationListContext; |
Marc Blank | f362695 | 2012-02-28 17:06:05 -0800 | [diff] [blame] | 41 | import com.android.mail.R; |
Andy Huang | 761522c | 2013-08-08 13:09:11 -0700 | [diff] [blame] | 42 | import com.android.mail.analytics.Analytics; |
Jin Cao | 72953f2 | 2014-04-15 18:23:37 -0700 | [diff] [blame] | 43 | import com.android.mail.analytics.AnalyticsTimer; |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 44 | import com.android.mail.browse.ConversationCursor; |
Vikram Aggarwal | d247dc9 | 2012-02-10 15:49:01 -0800 | [diff] [blame] | 45 | import com.android.mail.browse.ConversationItemView; |
Mindy Pereira | 12fe37a | 2012-08-15 10:02:57 -0700 | [diff] [blame] | 46 | import com.android.mail.browse.ConversationItemViewModel; |
Mindy Pereira | 6681f6b | 2012-03-09 13:55:54 -0800 | [diff] [blame] | 47 | import com.android.mail.browse.ConversationListFooterView; |
Alice Yang | 6427314 | 2013-04-10 18:26:56 -0700 | [diff] [blame] | 48 | import com.android.mail.browse.ToggleableItem; |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 49 | import com.android.mail.providers.Account; |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 50 | import com.android.mail.providers.AccountObserver; |
Vikram Aggarwal | d247dc9 | 2012-02-10 15:49:01 -0800 | [diff] [blame] | 51 | import com.android.mail.providers.Conversation; |
Mindy Pereira | 4f166de | 2012-02-14 13:40:58 -0800 | [diff] [blame] | 52 | import com.android.mail.providers.Folder; |
Vikram Aggarwal | 50ff0e5 | 2013-03-14 13:58:02 -0700 | [diff] [blame] | 53 | import com.android.mail.providers.FolderObserver; |
Vikram Aggarwal | 7d81600 | 2012-04-17 17:06:41 -0700 | [diff] [blame] | 54 | import com.android.mail.providers.Settings; |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 55 | import com.android.mail.providers.UIProvider; |
Mindy Pereira | 4e812f4 | 2012-07-30 16:52:49 -0700 | [diff] [blame] | 56 | import com.android.mail.providers.UIProvider.AccountCapabilities; |
Scott Kennedy | 5dce146 | 2013-08-05 11:26:40 -0700 | [diff] [blame] | 57 | import com.android.mail.providers.UIProvider.ConversationListIcon; |
Mindy Pereira | 4e812f4 | 2012-07-30 16:52:49 -0700 | [diff] [blame] | 58 | import com.android.mail.providers.UIProvider.FolderCapabilities; |
Mindy Pereira | e58222b | 2012-07-25 14:33:18 -0700 | [diff] [blame] | 59 | import com.android.mail.providers.UIProvider.Swipe; |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 60 | import com.android.mail.ui.SwipeableListView.ListItemSwipedListener; |
| 61 | import com.android.mail.ui.SwipeableListView.ListItemsRemovedListener; |
Andrew Sapperstein | 7c411aa | 2014-02-10 12:37:21 -0800 | [diff] [blame] | 62 | import com.android.mail.ui.SwipeableListView.SwipeListener; |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 63 | import com.android.mail.ui.ViewMode.ModeChangeListener; |
Paul Westbrook | b334c90 | 2012-06-25 11:42:46 -0700 | [diff] [blame] | 64 | import com.android.mail.utils.LogTag; |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 65 | import com.android.mail.utils.LogUtils; |
Vikram Aggarwal | fa131a2 | 2012-02-02 13:56:22 -0800 | [diff] [blame] | 66 | import com.android.mail.utils.Utils; |
Andrew Sapperstein | 6c570db | 2013-08-06 17:21:36 -0700 | [diff] [blame] | 67 | import com.google.common.collect.ImmutableList; |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 68 | |
Mindy Pereira | f6a6b50 | 2012-03-15 15:24:26 -0700 | [diff] [blame] | 69 | import java.util.Collection; |
Scott Kennedy | 7c8325d | 2013-02-28 10:46:10 -0800 | [diff] [blame] | 70 | import java.util.List; |
Mindy Pereira | fe06bea | 2012-02-16 08:15:14 -0800 | [diff] [blame] | 71 | |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 72 | /** |
| 73 | * The conversation list UI component. |
| 74 | */ |
Vikram Aggarwal | d247dc9 | 2012-02-10 15:49:01 -0800 | [diff] [blame] | 75 | public final class ConversationListFragment extends ListFragment implements |
Andrew Sapperstein | 7c411aa | 2014-02-10 12:37:21 -0800 | [diff] [blame] | 76 | OnItemLongClickListener, ModeChangeListener, ListItemSwipedListener, OnRefreshListener, |
| 77 | SwipeListener { |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame] | 78 | /** Key used to pass data to {@link ConversationListFragment}. */ |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 79 | private static final String CONVERSATION_LIST_KEY = "conversation-list"; |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame] | 80 | /** Key used to keep track of the scroll state of the list. */ |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 81 | private static final String LIST_STATE_KEY = "list-state"; |
Vikram Aggarwal | 649b9ea | 2012-08-27 12:15:20 -0700 | [diff] [blame] | 82 | |
Paul Westbrook | b334c90 | 2012-06-25 11:42:46 -0700 | [diff] [blame] | 83 | private static final String LOG_TAG = LogTag.getLogTag(); |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame] | 84 | /** Key used to save the ListView choice mode, since ListView doesn't save it automatically! */ |
| 85 | private static final String CHOICE_MODE_KEY = "choice-mode-key"; |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 86 | |
Vikram Aggarwal | d7a12cd | 2012-02-03 09:36:20 -0800 | [diff] [blame] | 87 | // True if we are on a tablet device |
| 88 | private static boolean mTabletDevice; |
| 89 | |
Martin Hibdon | aaca613 | 2014-03-24 11:28:14 -0700 | [diff] [blame] | 90 | // Delay before displaying the loading view. |
Andy Huang | 0ef5cb2 | 2014-04-09 19:22:39 -0700 | [diff] [blame] | 91 | private static int LOADING_DELAY_MS; |
Martin Hibdon | 4cdc485 | 2014-04-04 15:46:12 -0700 | [diff] [blame] | 92 | // Minimum amount of time to keep the loading view displayed. |
Andy Huang | 0ef5cb2 | 2014-04-09 19:22:39 -0700 | [diff] [blame] | 93 | private static int MINIMUM_LOADING_DURATION; |
Martin Hibdon | aaca613 | 2014-03-24 11:28:14 -0700 | [diff] [blame] | 94 | |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 95 | /** |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 96 | * Frequency of update of timestamps. Initialized in |
| 97 | * {@link #onCreate(Bundle)} and final afterwards. |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 98 | */ |
| 99 | private static int TIMESTAMP_UPDATE_INTERVAL = 0; |
| 100 | |
Alice Yang | 0d74a66 | 2013-03-25 14:01:24 -0700 | [diff] [blame] | 101 | private static long NO_NEW_MESSAGE_DURATION = 1 * DateUtils.SECOND_IN_MILLIS; |
| 102 | |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 103 | private ControllableActivity mActivity; |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 104 | |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 105 | // Control state. |
| 106 | private ConversationListCallbacks mCallbacks; |
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 | private final Handler mHandler = new Handler(); |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 109 | |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 110 | // The internal view objects. |
Mindy Pereira | f6a6b50 | 2012-03-15 15:24:26 -0700 | [diff] [blame] | 111 | private SwipeableListView mListView; |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 112 | |
| 113 | private TextView mSearchResultCountTextView; |
| 114 | private TextView mSearchStatusTextView; |
| 115 | |
| 116 | private View mSearchStatusView; |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 117 | |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 118 | /** |
| 119 | * Current Account being viewed |
| 120 | */ |
Vikram Aggarwal | d247dc9 | 2012-02-10 15:49:01 -0800 | [diff] [blame] | 121 | private Account mAccount; |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 122 | /** |
Mindy Pereira | 30fd47b | 2012-03-09 09:24:00 -0800 | [diff] [blame] | 123 | * Current folder being viewed. |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 124 | */ |
Mindy Pereira | 4f166de | 2012-02-14 13:40:58 -0800 | [diff] [blame] | 125 | private Folder mFolder; |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 126 | |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 127 | /** |
| 128 | * A simple method to update the timestamps of conversations periodically. |
| 129 | */ |
| 130 | private Runnable mUpdateTimestampsRunnable = null; |
| 131 | |
| 132 | private ConversationListContext mViewContext; |
| 133 | |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 134 | private AnimatedAdapter mListAdapter; |
Vikram Aggarwal | 7d60288 | 2012-02-07 15:01:20 -0800 | [diff] [blame] | 135 | |
Mindy Pereira | 6681f6b | 2012-03-09 13:55:54 -0800 | [diff] [blame] | 136 | private ConversationListFooterView mFooterView; |
Andrew Sapperstein | a44b0ed | 2014-02-12 18:56:37 -0800 | [diff] [blame] | 137 | private ConversationListEmptyView mEmptyView; |
Martin Hibdon | aaca613 | 2014-03-24 11:28:14 -0700 | [diff] [blame] | 138 | private View mLoadingView; |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 139 | private ErrorListener mErrorListener; |
Vikram Aggarwal | 50ff0e5 | 2013-03-14 13:58:02 -0700 | [diff] [blame] | 140 | private FolderObserver mFolderObserver; |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame] | 141 | private DataSetObserver mConversationCursorObserver; |
Vikram Aggarwal | d247dc9 | 2012-02-10 15:49:01 -0800 | [diff] [blame] | 142 | |
Vikram Aggarwal | 4f1cc0b | 2012-08-02 15:16:30 -0700 | [diff] [blame] | 143 | private ConversationSelectionSet mSelectedSet; |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 144 | private final AccountObserver mAccountObserver = new AccountObserver() { |
| 145 | @Override |
| 146 | public void onChanged(Account newAccount) { |
| 147 | mAccount = newAccount; |
| 148 | setSwipeAction(); |
| 149 | } |
| 150 | }; |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 151 | private ConversationUpdater mUpdater; |
Vikram Aggarwal | 81a4f08 | 2012-09-28 09:19:04 -0700 | [diff] [blame] | 152 | /** Hash of the Conversation Cursor we last obtained from the controller. */ |
| 153 | private int mConversationCursorHash; |
Jin Cao | 343cf45 | 2014-04-18 14:05:25 -0700 | [diff] [blame] | 154 | // The number of items in the last known ConversationCursor |
| 155 | private int mConversationCursorLastCount; |
| 156 | // State variable to keep track if we just loaded a new list |
Jin Cao | 8bf9c21 | 2014-04-25 15:00:53 -0700 | [diff] [blame] | 157 | // True if NO DATA has returned, false if we either partially or fully loaded the data |
Jin Cao | faa9198 | 2014-04-24 16:52:26 -0700 | [diff] [blame] | 158 | private boolean mInitialCursorLoading; |
Vikram Aggarwal | 4f1cc0b | 2012-08-02 15:16:30 -0700 | [diff] [blame] | 159 | |
Scott Kennedy | 1fea6a3 | 2013-07-09 15:58:51 -0700 | [diff] [blame] | 160 | /** Duration, in milliseconds, of the CAB mode (peek icon) animation. */ |
| 161 | private static long sSelectionModeAnimationDuration = -1; |
Scott Kennedy | 1fea6a3 | 2013-07-09 15:58:51 -0700 | [diff] [blame] | 162 | /** The time at which we last exited CAB mode. */ |
| 163 | private long mSelectionModeExitedTimestamp = -1; |
| 164 | |
Jin Cao | 856e131 | 2014-04-09 17:36:14 -0700 | [diff] [blame] | 165 | // Let's ensure that we are only showing one out of the three views at once |
| 166 | private void showListView() { |
| 167 | mListView.setVisibility(View.VISIBLE); |
Jin Cao | 90c6167 | 2014-04-23 12:57:32 -0700 | [diff] [blame] | 168 | mEmptyView.setVisibility(View.INVISIBLE); |
| 169 | mLoadingView.setVisibility(View.INVISIBLE); |
Jin Cao | 856e131 | 2014-04-09 17:36:14 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | private void showEmptyView() { |
Jin Cao | 3f386e8 | 2014-04-18 11:29:54 -0700 | [diff] [blame] | 173 | mEmptyView.setupEmptyView( |
| 174 | mFolder, mViewContext.searchQuery, mListAdapter.getBidiFormatter()); |
Jin Cao | 90c6167 | 2014-04-23 12:57:32 -0700 | [diff] [blame] | 175 | mListView.setVisibility(View.INVISIBLE); |
Jin Cao | 856e131 | 2014-04-09 17:36:14 -0700 | [diff] [blame] | 176 | mEmptyView.setVisibility(View.VISIBLE); |
Jin Cao | 90c6167 | 2014-04-23 12:57:32 -0700 | [diff] [blame] | 177 | mLoadingView.setVisibility(View.INVISIBLE); |
Jin Cao | 856e131 | 2014-04-09 17:36:14 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | private void showLoadingView() { |
Jin Cao | 90c6167 | 2014-04-23 12:57:32 -0700 | [diff] [blame] | 181 | mListView.setVisibility(View.INVISIBLE); |
| 182 | mEmptyView.setVisibility(View.INVISIBLE); |
Jin Cao | 856e131 | 2014-04-09 17:36:14 -0700 | [diff] [blame] | 183 | mLoadingView.setVisibility(View.VISIBLE); |
| 184 | } |
| 185 | |
Martin Hibdon | 4cdc485 | 2014-04-04 15:46:12 -0700 | [diff] [blame] | 186 | private final Runnable mLoadingViewRunnable = new FragmentRunnable("LoadingRunnable", this) { |
Martin Hibdon | aaca613 | 2014-03-24 11:28:14 -0700 | [diff] [blame] | 187 | @Override |
| 188 | public void go() { |
Jin Cao | 8bf9c21 | 2014-04-25 15:00:53 -0700 | [diff] [blame] | 189 | if (mInitialCursorLoading) { |
Martin Hibdon | 4cdc485 | 2014-04-04 15:46:12 -0700 | [diff] [blame] | 190 | mCanTakeDownLoadingView = false; |
Jin Cao | 90c6167 | 2014-04-23 12:57:32 -0700 | [diff] [blame] | 191 | showLoadingView(); |
Martin Hibdon | 4cdc485 | 2014-04-04 15:46:12 -0700 | [diff] [blame] | 192 | mHandler.removeCallbacks(mHideLoadingRunnable); |
| 193 | mHandler.postDelayed(mHideLoadingRunnable, MINIMUM_LOADING_DURATION); |
Martin Hibdon | aaca613 | 2014-03-24 11:28:14 -0700 | [diff] [blame] | 194 | } |
| 195 | mLoadingViewPending = false; |
| 196 | } |
| 197 | }; |
| 198 | |
Martin Hibdon | 4cdc485 | 2014-04-04 15:46:12 -0700 | [diff] [blame] | 199 | private final Runnable mHideLoadingRunnable = new FragmentRunnable("CancelLoading", this) { |
| 200 | @Override |
| 201 | public void go() { |
| 202 | mCanTakeDownLoadingView = true; |
Jin Cao | 8bf9c21 | 2014-04-25 15:00:53 -0700 | [diff] [blame] | 203 | if (!mInitialCursorLoading) { |
Martin Hibdon | 4cdc485 | 2014-04-04 15:46:12 -0700 | [diff] [blame] | 204 | hideLoadingViewAndShowContents(); |
| 205 | } |
| 206 | } |
| 207 | }; |
| 208 | |
Jin Cao | 7f4170a | 2014-04-23 13:55:47 -0700 | [diff] [blame] | 209 | // Keep track of if we are waiting for the loading view. This variable is also used to check |
| 210 | // if the cursor corresponding to the current folder loaded (either partially or completely). |
Martin Hibdon | aaca613 | 2014-03-24 11:28:14 -0700 | [diff] [blame] | 211 | private boolean mLoadingViewPending; |
Martin Hibdon | 4cdc485 | 2014-04-04 15:46:12 -0700 | [diff] [blame] | 212 | private boolean mCanTakeDownLoadingView; |
Martin Hibdon | aaca613 | 2014-03-24 11:28:14 -0700 | [diff] [blame] | 213 | |
Vikram Aggarwal | 6c51158 | 2012-02-27 10:59:47 -0800 | [diff] [blame] | 214 | /** |
Scott Kennedy | f77806e | 2013-08-30 11:38:15 -0700 | [diff] [blame] | 215 | * If <code>true</code>, we have restored (or attempted to restore) the list's scroll position |
| 216 | * from when we were last on this conversation list. |
| 217 | */ |
| 218 | private boolean mScrollPositionRestored = false; |
Andrew Sapperstein | b6910bd | 2014-02-13 09:52:19 -0800 | [diff] [blame] | 219 | private MailSwipeRefreshLayout mSwipeRefreshWidget; |
Scott Kennedy | f77806e | 2013-08-30 11:38:15 -0700 | [diff] [blame] | 220 | |
| 221 | /** |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 222 | * Constructor needs to be public to handle orientation changes and activity |
| 223 | * lifecycle events. |
Vikram Aggarwal | 6c51158 | 2012-02-27 10:59:47 -0800 | [diff] [blame] | 224 | */ |
Paul Westbrook | efc9f12 | 2012-02-21 11:14:49 -0800 | [diff] [blame] | 225 | public ConversationListFragment() { |
Vikram Aggarwal | d247dc9 | 2012-02-10 15:49:01 -0800 | [diff] [blame] | 226 | super(); |
Vikram Aggarwal | d247dc9 | 2012-02-10 15:49:01 -0800 | [diff] [blame] | 227 | } |
| 228 | |
Andrew Sapperstein | 7c411aa | 2014-02-10 12:37:21 -0800 | [diff] [blame] | 229 | @Override |
| 230 | public void onBeginSwipe() { |
| 231 | mSwipeRefreshWidget.setEnabled(false); |
| 232 | } |
| 233 | |
| 234 | @Override |
| 235 | public void onEndSwipe() { |
| 236 | mSwipeRefreshWidget.setEnabled(true); |
| 237 | } |
| 238 | |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame] | 239 | private class ConversationCursorObserver extends DataSetObserver { |
Mindy Pereira | 70a70c9 | 2012-08-02 08:39:45 -0700 | [diff] [blame] | 240 | @Override |
| 241 | public void onChanged() { |
Mindy Pereira | 70a70c9 | 2012-08-02 08:39:45 -0700 | [diff] [blame] | 242 | onConversationListStatusUpdated(); |
| 243 | } |
| 244 | } |
| 245 | |
Vikram Aggarwal | 7d60288 | 2012-02-07 15:01:20 -0800 | [diff] [blame] | 246 | /** |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 247 | * Creates a new instance of {@link ConversationListFragment}, initialized |
| 248 | * to display conversation list context. |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 249 | */ |
| 250 | public static ConversationListFragment newInstance(ConversationListContext viewContext) { |
Paul Westbrook | e2be97a | 2013-05-21 02:12:09 -0700 | [diff] [blame] | 251 | final ConversationListFragment fragment = new ConversationListFragment(); |
| 252 | final Bundle args = new Bundle(1); |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 253 | args.putBundle(CONVERSATION_LIST_KEY, viewContext.toBundle()); |
| 254 | fragment.setArguments(args); |
| 255 | return fragment; |
| 256 | } |
| 257 | |
| 258 | /** |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 259 | * Show the header if the current conversation list is showing search |
| 260 | * results. |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 261 | */ |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 262 | void configureSearchResultHeader() { |
Mindy Pereira | 755fd6e | 2012-03-21 15:15:44 -0700 | [diff] [blame] | 263 | if (mActivity == null) { |
| 264 | return; |
| 265 | } |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 266 | // Only show the header if the context is for a search result |
| 267 | final Resources res = getResources(); |
Vikram Aggarwal | ae4ea99 | 2012-08-07 09:56:02 -0700 | [diff] [blame] | 268 | final boolean showHeader = ConversationListContext.isSearchResult(mViewContext); |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 269 | // TODO(viki): This code contains intimate understanding of the view. |
| 270 | // Much of this logic |
| 271 | // needs to reside in a separate class that handles the text view in |
| 272 | // isolation. Then, |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 273 | // that logic can be reused in other fragments. |
Jin Cao | 94bf9c9 | 2014-04-21 16:41:16 -0700 | [diff] [blame] | 274 | mSearchStatusView.setVisibility(showHeader ? View.INVISIBLE : View.GONE); |
Mindy Pereira | 4bb435c | 2013-11-13 14:21:15 -0800 | [diff] [blame] | 275 | int paddingTop = showHeader ? (int) res.getDimension(R.dimen.notification_view_height) : 0; |
| 276 | mListView.setPadding(mListView.getPaddingLeft(), paddingTop, mListView.getPaddingRight(), |
| 277 | mListView.getPaddingBottom()); |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 278 | } |
| 279 | |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 280 | /** |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 281 | * Show the header if the current conversation list is showing search |
| 282 | * results. |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 283 | */ |
| 284 | private void updateSearchResultHeader(int count) { |
Mindy Pereira | 71a8f29 | 2012-07-26 16:30:48 -0700 | [diff] [blame] | 285 | if (mActivity == null) { |
| 286 | return; |
| 287 | } |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 288 | // Only show the header if the context is for a search result |
| 289 | final Resources res = getResources(); |
Vikram Aggarwal | ae4ea99 | 2012-08-07 09:56:02 -0700 | [diff] [blame] | 290 | final boolean showHeader = ConversationListContext.isSearchResult(mViewContext); |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 291 | if (showHeader) { |
| 292 | mSearchStatusTextView.setText(res.getString(R.string.search_results_header)); |
| 293 | mSearchResultCountTextView |
| 294 | .setText(res.getString(R.string.search_results_loaded, count)); |
Jin Cao | 94bf9c9 | 2014-04-21 16:41:16 -0700 | [diff] [blame] | 295 | mSearchStatusView.setVisibility(View.VISIBLE); |
Mindy Pereira | 68f2e22 | 2012-03-07 10:36:54 -0800 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | |
| 299 | /** |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 300 | * Initializes all internal state for a rendering. |
| 301 | */ |
| 302 | private void initializeUiForFirstDisplay() { |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 303 | // TODO(mindyp): find some way to make the notification container more |
| 304 | // re-usable. |
| 305 | // TODO(viki): refactor according to comment in |
| 306 | // configureSearchResultHandler() |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 307 | mSearchStatusView = mActivity.findViewById(R.id.search_status_view); |
| 308 | mSearchStatusTextView = (TextView) mActivity.findViewById(R.id.search_status_text_view); |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 309 | mSearchResultCountTextView = (TextView) mActivity |
| 310 | .findViewById(R.id.search_result_count_view); |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 311 | } |
| 312 | |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 313 | @Override |
Vikram Aggarwal | 0509bba | 2013-01-29 17:11:30 -0800 | [diff] [blame] | 314 | public void onActivityCreated(Bundle savedState) { |
| 315 | super.onActivityCreated(savedState); |
Martin Hibdon | aaca613 | 2014-03-24 11:28:14 -0700 | [diff] [blame] | 316 | mLoadingViewPending = false; |
Martin Hibdon | 4cdc485 | 2014-04-04 15:46:12 -0700 | [diff] [blame] | 317 | mCanTakeDownLoadingView = true; |
Scott Kennedy | 1fea6a3 | 2013-07-09 15:58:51 -0700 | [diff] [blame] | 318 | if (sSelectionModeAnimationDuration < 0) { |
| 319 | sSelectionModeAnimationDuration = getResources().getInteger( |
| 320 | R.integer.conv_item_view_cab_anim_duration); |
| 321 | } |
| 322 | |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 323 | // Strictly speaking, we get back an android.app.Activity from |
| 324 | // getActivity. However, the |
| 325 | // only activity creating a ConversationListContext is a MailActivity |
| 326 | // which is of type |
| 327 | // ControllableActivity, so this cast should be safe. If this cast |
| 328 | // fails, some other |
| 329 | // activity is creating ConversationListFragments. This activity must be |
| 330 | // of type |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 331 | // ControllableActivity. |
| 332 | final Activity activity = getActivity(); |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 333 | if (!(activity instanceof ControllableActivity)) { |
| 334 | LogUtils.e(LOG_TAG, "ConversationListFragment expects only a ControllableActivity to" |
| 335 | + "create it. Cannot proceed."); |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 336 | } |
| 337 | mActivity = (ControllableActivity) activity; |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 338 | // Since we now have a controllable activity, load the account from it, |
| 339 | // and register for |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 340 | // future account changes. |
| 341 | mAccount = mAccountObserver.initialize(mActivity.getAccountController()); |
Vikram Aggarwal | 7d60288 | 2012-02-07 15:01:20 -0800 | [diff] [blame] | 342 | mCallbacks = mActivity.getListHandler(); |
Andrew Sapperstein | c2c9dc1 | 2012-07-02 18:17:32 -0700 | [diff] [blame] | 343 | mErrorListener = mActivity.getErrorListener(); |
Mindy Pereira | 278fd22 | 2012-07-26 15:23:10 -0700 | [diff] [blame] | 344 | // Start off with the current state of the folder being viewed. |
Alice Yang | 2032316 | 2013-04-17 10:37:41 -0700 | [diff] [blame] | 345 | Context activityContext = mActivity.getActivityContext(); |
Mindy Pereira | 6681f6b | 2012-03-09 13:55:54 -0800 | [diff] [blame] | 346 | mFooterView = (ConversationListFooterView) LayoutInflater.from( |
Alice Yang | 2032316 | 2013-04-17 10:37:41 -0700 | [diff] [blame] | 347 | activityContext).inflate(R.layout.conversation_list_footer_view, |
Mindy Pereira | 6681f6b | 2012-03-09 13:55:54 -0800 | [diff] [blame] | 348 | null); |
Paul Westbrook | 4969e0c | 2012-08-20 14:38:39 -0700 | [diff] [blame] | 349 | mFooterView.setClickListener(mActivity); |
Vikram Aggarwal | 81a4f08 | 2012-09-28 09:19:04 -0700 | [diff] [blame] | 350 | final ConversationCursor conversationCursor = getConversationListCursor(); |
Vikram Aggarwal | 37a20ca | 2013-06-06 11:19:49 -0700 | [diff] [blame] | 351 | final LoaderManager manager = getLoaderManager(); |
| 352 | |
Alice Yang | c556773 | 2013-07-29 18:34:51 -0700 | [diff] [blame] | 353 | // TODO: These special views are always created, doesn't matter whether they will |
| 354 | // be shown or not, as we add more views this will get more expensive. Given these are |
| 355 | // tips that are only shown once to the user, we should consider creating these on demand. |
Scott Kennedy | 7c8325d | 2013-02-28 10:46:10 -0800 | [diff] [blame] | 356 | final ConversationListHelper helper = mActivity.getConversationListHelper(); |
Mark Wei | 2102b2c | 2013-05-02 17:15:30 -0700 | [diff] [blame] | 357 | final List<ConversationSpecialItemView> specialItemViews = helper != null ? |
| 358 | ImmutableList.copyOf(helper.makeConversationListSpecialViews( |
Scott Kennedy | 103319a | 2013-07-26 13:35:35 -0700 | [diff] [blame] | 359 | activity, mActivity, mAccount)) |
Mark Wei | 2102b2c | 2013-05-02 17:15:30 -0700 | [diff] [blame] | 360 | : null; |
Scott Kennedy | 7c8325d | 2013-02-28 10:46:10 -0800 | [diff] [blame] | 361 | if (specialItemViews != null) { |
| 362 | // Attach to the LoaderManager |
| 363 | for (final ConversationSpecialItemView view : specialItemViews) { |
Scott Kennedy | 32ddb84 | 2013-08-28 17:38:22 -0700 | [diff] [blame] | 364 | view.bindFragment(manager, savedState); |
Scott Kennedy | 7c8325d | 2013-02-28 10:46:10 -0800 | [diff] [blame] | 365 | } |
| 366 | } |
| 367 | |
| 368 | mListAdapter = new AnimatedAdapter(mActivity.getApplicationContext(), conversationCursor, |
Scott Kennedy | 6af7077 | 2013-09-20 19:27:56 -0400 | [diff] [blame] | 369 | mActivity.getSelectedSet(), mActivity, mListView, specialItemViews); |
Mindy Pereira | 6681f6b | 2012-03-09 13:55:54 -0800 | [diff] [blame] | 370 | mListAdapter.addFooter(mFooterView); |
Mindy Pereira | 6f4a6af | 2012-02-29 11:48:52 -0800 | [diff] [blame] | 371 | mListView.setAdapter(mListAdapter); |
Vikram Aggarwal | 4f1cc0b | 2012-08-02 15:16:30 -0700 | [diff] [blame] | 372 | mSelectedSet = mActivity.getSelectedSet(); |
| 373 | mListView.setSelectionSet(mSelectedSet); |
Vikram Aggarwal | 37a20ca | 2013-06-06 11:19:49 -0700 | [diff] [blame] | 374 | mListAdapter.setFooterVisibility(false); |
Vikram Aggarwal | 50ff0e5 | 2013-03-14 13:58:02 -0700 | [diff] [blame] | 375 | mFolderObserver = new FolderObserver(){ |
| 376 | @Override |
| 377 | public void onChanged(Folder newFolder) { |
| 378 | onFolderUpdated(newFolder); |
| 379 | } |
| 380 | }; |
| 381 | mFolderObserver.initialize(mActivity.getFolderController()); |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame] | 382 | mConversationCursorObserver = new ConversationCursorObserver(); |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 383 | mUpdater = mActivity.getConversationUpdater(); |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame] | 384 | mUpdater.registerConversationListObserver(mConversationCursorObserver); |
Vikram Aggarwal | bcb16b9 | 2013-01-28 18:05:03 -0800 | [diff] [blame] | 385 | mTabletDevice = Utils.useTabletUI(mActivity.getApplicationContext().getResources()); |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 386 | initializeUiForFirstDisplay(); |
Mindy Pereira | dc8963b | 2012-03-28 17:51:05 -0700 | [diff] [blame] | 387 | configureSearchResultHeader(); |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 388 | // The onViewModeChanged callback doesn't get called when the mode |
| 389 | // object is created, so |
Vikram Aggarwal | fa131a2 | 2012-02-02 13:56:22 -0800 | [diff] [blame] | 390 | // force setting the mode manually this time around. |
Mindy Pereira | f96ec32 | 2012-03-02 14:06:33 -0800 | [diff] [blame] | 391 | onViewModeChanged(mActivity.getViewMode().getMode()); |
Mindy Pereira | b5901be | 2012-08-09 17:21:53 -0700 | [diff] [blame] | 392 | mActivity.getViewMode().addListener(this); |
Paul Westbrook | 0e3fd9d | 2012-04-20 02:02:23 -0700 | [diff] [blame] | 393 | |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 394 | if (mActivity.isFinishing()) { |
| 395 | // Activity is finishing, just bail. |
| 396 | return; |
| 397 | } |
Vikram Aggarwal | 81a4f08 | 2012-09-28 09:19:04 -0700 | [diff] [blame] | 398 | mConversationCursorHash = (conversationCursor == null) ? 0 : conversationCursor.hashCode(); |
| 399 | // Belt and suspenders here; make sure we do any necessary sync of the |
| 400 | // ConversationCursor |
| 401 | if (conversationCursor != null && conversationCursor.isRefreshReady()) { |
| 402 | conversationCursor.sync(); |
| 403 | } |
| 404 | |
Vikram Aggarwal | 0509bba | 2013-01-29 17:11:30 -0800 | [diff] [blame] | 405 | // On a phone we never highlight a conversation, so the default is to select none. |
| 406 | // On a tablet, we highlight a SINGLE conversation in landscape conversation view. |
| 407 | int choice = getDefaultChoiceMode(mTabletDevice); |
| 408 | if (savedState != null) { |
| 409 | // Restore the choice mode if it was set earlier, or NONE if creating a fresh view. |
| 410 | // Choice mode here represents the current conversation only. CAB mode does not rely on |
| 411 | // the platform: checked state is a local variable {@link ConversationItemView#mChecked} |
| 412 | choice = savedState.getInt(CHOICE_MODE_KEY, choice); |
| 413 | if (savedState.containsKey(LIST_STATE_KEY)) { |
| 414 | // TODO: find a better way to unset the selected item when restoring |
| 415 | mListView.clearChoices(); |
| 416 | } |
| 417 | } |
| 418 | setChoiceMode(choice); |
| 419 | |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 420 | // Show list and start loading list. |
| 421 | showList(); |
Mindy Pereira | 4765c5c | 2012-07-19 11:58:22 -0700 | [diff] [blame] | 422 | ToastBarOperation pendingOp = mActivity.getPendingToastOperation(); |
| 423 | if (pendingOp != null) { |
| 424 | // Clear the pending operation |
| 425 | mActivity.setPendingToastOperation(null); |
| 426 | mActivity.onUndoAvailable(pendingOp); |
| 427 | } |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 428 | } |
| 429 | |
Vikram Aggarwal | 0509bba | 2013-01-29 17:11:30 -0800 | [diff] [blame] | 430 | /** |
| 431 | * Returns the default choice mode for the list based on whether the list is displayed on tablet |
| 432 | * or not. |
| 433 | * @param isTablet |
| 434 | * @return |
| 435 | */ |
| 436 | private final static int getDefaultChoiceMode(boolean isTablet) { |
| 437 | return isTablet ? ListView.CHOICE_MODE_SINGLE : ListView.CHOICE_MODE_NONE; |
| 438 | } |
| 439 | |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 440 | public AnimatedAdapter getAnimatedAdapter() { |
| 441 | return mListAdapter; |
| 442 | } |
| 443 | |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 444 | @Override |
Vikram Aggarwal | d247dc9 | 2012-02-10 15:49:01 -0800 | [diff] [blame] | 445 | public void onCreate(Bundle savedState) { |
Vikram Aggarwal | d247dc9 | 2012-02-10 15:49:01 -0800 | [diff] [blame] | 446 | super.onCreate(savedState); |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 447 | |
| 448 | // Initialize fragment constants from resources |
Vikram Aggarwal | 6c51158 | 2012-02-27 10:59:47 -0800 | [diff] [blame] | 449 | final Resources res = getResources(); |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 450 | TIMESTAMP_UPDATE_INTERVAL = res.getInteger(R.integer.timestamp_update_interval); |
Andy Huang | 0ef5cb2 | 2014-04-09 19:22:39 -0700 | [diff] [blame] | 451 | LOADING_DELAY_MS = res.getInteger(R.integer.conversationview_show_loading_delay); |
| 452 | MINIMUM_LOADING_DURATION = res.getInteger(R.integer.conversationview_min_show_loading); |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 453 | mUpdateTimestampsRunnable = new Runnable() { |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 454 | @Override |
| 455 | public void run() { |
| 456 | mListView.invalidateViews(); |
| 457 | mHandler.postDelayed(mUpdateTimestampsRunnable, TIMESTAMP_UPDATE_INTERVAL); |
| 458 | } |
| 459 | }; |
| 460 | |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 461 | // Get the context from the arguments |
Vikram Aggarwal | 6c51158 | 2012-02-27 10:59:47 -0800 | [diff] [blame] | 462 | final Bundle args = getArguments(); |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 463 | mViewContext = ConversationListContext.forBundle(args.getBundle(CONVERSATION_LIST_KEY)); |
Mindy Pereira | 3982e23 | 2012-02-29 15:00:34 -0800 | [diff] [blame] | 464 | mAccount = mViewContext.account; |
Paul Westbrook | 80ecd89 | 2012-08-16 13:37:39 -0700 | [diff] [blame] | 465 | |
Vikram Aggarwal | 9a49c9b | 2012-08-31 10:49:33 -0700 | [diff] [blame] | 466 | setRetainInstance(false); |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | @Override |
Andy Huang | 9e4ca79 | 2013-02-28 14:33:43 -0800 | [diff] [blame] | 470 | public String toString() { |
| 471 | final String s = super.toString(); |
| 472 | if (mViewContext == null) { |
| 473 | return s; |
| 474 | } |
Andy Huang | c1922a9 | 2013-05-13 14:33:05 -0700 | [diff] [blame] | 475 | final StringBuilder sb = new StringBuilder(s); |
| 476 | sb.setLength(sb.length() - 1); |
| 477 | sb.append(" mListAdapter="); |
| 478 | sb.append(mListAdapter); |
| 479 | sb.append(" folder="); |
| 480 | sb.append(mViewContext.folder); |
| 481 | sb.append("}"); |
| 482 | return sb.toString(); |
Andy Huang | 9e4ca79 | 2013-02-28 14:33:43 -0800 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | @Override |
Vikram Aggarwal | 9a49c9b | 2012-08-31 10:49:33 -0700 | [diff] [blame] | 486 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState) { |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 487 | View rootView = inflater.inflate(R.layout.conversation_list, null); |
Andrew Sapperstein | a44b0ed | 2014-02-12 18:56:37 -0800 | [diff] [blame] | 488 | mEmptyView = (ConversationListEmptyView) rootView.findViewById(R.id.empty_view); |
Andy Huang | 0ef5cb2 | 2014-04-09 19:22:39 -0700 | [diff] [blame] | 489 | mLoadingView = rootView.findViewById(R.id.background_view); |
Andy Huang | c96efcc | 2014-04-09 15:30:42 -0700 | [diff] [blame] | 490 | mLoadingView.setVisibility(View.GONE); |
Andy Huang | 0ef5cb2 | 2014-04-09 19:22:39 -0700 | [diff] [blame] | 491 | mLoadingView.findViewById(R.id.loading_progress).setVisibility(View.VISIBLE); |
Mindy Pereira | f6a6b50 | 2012-03-15 15:24:26 -0700 | [diff] [blame] | 492 | mListView = (SwipeableListView) rootView.findViewById(android.R.id.list); |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 493 | mListView.setHeaderDividersEnabled(false); |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 494 | mListView.setOnItemLongClickListener(this); |
Mindy Pereira | 4e812f4 | 2012-07-30 16:52:49 -0700 | [diff] [blame] | 495 | mListView.enableSwipe(mAccount.supportsCapability(AccountCapabilities.UNDO)); |
Andrew Sapperstein | 7c411aa | 2014-02-10 12:37:21 -0800 | [diff] [blame] | 496 | mListView.setListItemSwipedListener(this); |
| 497 | mListView.setSwipeListener(this); |
Marc Blank | 2af0cf3 | 2012-03-01 19:24:13 -0800 | [diff] [blame] | 498 | |
Jin Cao | 5cf73b8 | 2014-04-25 11:13:51 -0700 | [diff] [blame] | 499 | // enable animateOnLayout (equivalent of setLayoutTransition) only for >=JB (b/14302062) |
| 500 | if (Utils.isRunningJellybeanOrLater()) { |
| 501 | ((ViewGroup) rootView.findViewById(R.id.conversation_list_parent_frame)) |
| 502 | .setLayoutTransition(new LayoutTransition()); |
| 503 | } |
| 504 | |
Jin Cao | 856e131 | 2014-04-09 17:36:14 -0700 | [diff] [blame] | 505 | // By default let's show the list view |
| 506 | showListView(); |
| 507 | |
Vikram Aggarwal | 0509bba | 2013-01-29 17:11:30 -0800 | [diff] [blame] | 508 | if (savedState != null && savedState.containsKey(LIST_STATE_KEY)) { |
| 509 | mListView.onRestoreInstanceState(savedState.getParcelable(LIST_STATE_KEY)); |
Vikram Aggarwal | 9a49c9b | 2012-08-31 10:49:33 -0700 | [diff] [blame] | 510 | } |
Andrew Sapperstein | b6910bd | 2014-02-13 09:52:19 -0800 | [diff] [blame] | 511 | mSwipeRefreshWidget = |
| 512 | (MailSwipeRefreshLayout) rootView.findViewById(R.id.swipe_refresh_widget); |
Mindy Pereira | 4bb435c | 2013-11-13 14:21:15 -0800 | [diff] [blame] | 513 | mSwipeRefreshWidget.setColorScheme(R.color.swipe_refresh_color1, |
| 514 | R.color.swipe_refresh_color2, |
| 515 | R.color.swipe_refresh_color3, R.color.swipe_refresh_color4); |
| 516 | mSwipeRefreshWidget.setOnRefreshListener(this); |
Andrew Sapperstein | b6910bd | 2014-02-13 09:52:19 -0800 | [diff] [blame] | 517 | mSwipeRefreshWidget.setScrollableChild(mListView); |
Alice Yang | 0d74a66 | 2013-03-25 14:01:24 -0700 | [diff] [blame] | 518 | |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 519 | return rootView; |
| 520 | } |
| 521 | |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame] | 522 | /** |
| 523 | * Sets the choice mode of the list view |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame] | 524 | */ |
| 525 | private final void setChoiceMode(int choiceMode) { |
| 526 | mListView.setChoiceMode(choiceMode); |
| 527 | } |
| 528 | |
| 529 | /** |
| 530 | * Tell the list to select nothing. |
| 531 | */ |
| 532 | public final void setChoiceNone() { |
Vikram Aggarwal | 0509bba | 2013-01-29 17:11:30 -0800 | [diff] [blame] | 533 | // On a phone, the default choice mode is already none, so nothing to do. |
| 534 | if (!mTabletDevice) { |
| 535 | return; |
| 536 | } |
Andy Huang | b0be3fc | 2013-05-02 15:48:05 -0700 | [diff] [blame] | 537 | clearChoicesAndActivated(); |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame] | 538 | setChoiceMode(ListView.CHOICE_MODE_NONE); |
| 539 | } |
| 540 | |
| 541 | /** |
| 542 | * Tell the list to get out of selecting none. |
| 543 | */ |
| 544 | public final void revertChoiceMode() { |
Vikram Aggarwal | 0509bba | 2013-01-29 17:11:30 -0800 | [diff] [blame] | 545 | // On a phone, the default choice mode is always none, so nothing to do. |
| 546 | if (!mTabletDevice) { |
| 547 | return; |
| 548 | } |
| 549 | setChoiceMode(getDefaultChoiceMode(mTabletDevice)); |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame] | 550 | } |
| 551 | |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 552 | @Override |
Andy Huang | 7517e3b | 2012-08-20 12:30:08 -0700 | [diff] [blame] | 553 | public void onDestroy() { |
Andy Huang | 7517e3b | 2012-08-20 12:30:08 -0700 | [diff] [blame] | 554 | super.onDestroy(); |
| 555 | } |
| 556 | |
| 557 | @Override |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 558 | public void onDestroyView() { |
Andy Huang | 7517e3b | 2012-08-20 12:30:08 -0700 | [diff] [blame] | 559 | |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 560 | // Clear the list's adapter |
| 561 | mListAdapter.destroy(); |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 562 | mListView.setAdapter(null); |
| 563 | |
Andrew Sapperstein | 6c570db | 2013-08-06 17:21:36 -0700 | [diff] [blame] | 564 | mActivity.getViewMode().removeListener(this); |
Mindy Pereira | 71a8f29 | 2012-07-26 16:30:48 -0700 | [diff] [blame] | 565 | if (mFolderObserver != null) { |
Vikram Aggarwal | 50ff0e5 | 2013-03-14 13:58:02 -0700 | [diff] [blame] | 566 | mFolderObserver.unregisterAndDestroy(); |
Mindy Pereira | 71a8f29 | 2012-07-26 16:30:48 -0700 | [diff] [blame] | 567 | mFolderObserver = null; |
| 568 | } |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame] | 569 | if (mConversationCursorObserver != null) { |
| 570 | mUpdater.unregisterConversationListObserver(mConversationCursorObserver); |
| 571 | mConversationCursorObserver = null; |
Mindy Pereira | 70a70c9 | 2012-08-02 08:39:45 -0700 | [diff] [blame] | 572 | } |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 573 | mAccountObserver.unregisterAndDestroy(); |
Scott Kennedy | 7c8325d | 2013-02-28 10:46:10 -0800 | [diff] [blame] | 574 | getAnimatedAdapter().cleanup(); |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 575 | super.onDestroyView(); |
| 576 | } |
| 577 | |
Vikram Aggarwal | 9730ea0 | 2012-08-02 12:46:19 -0700 | [diff] [blame] | 578 | /** |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 579 | * There are three binary variables, which determine what we do with a |
| 580 | * message. checkbEnabled: Whether check boxes are enabled or not (forced |
| 581 | * true on tablet) cabModeOn: Whether CAB mode is currently on or not. |
| 582 | * pressType: long or short tap (There is a third possibility: phone or |
| 583 | * tablet, but they have <em>identical</em> behavior) The matrix of |
| 584 | * possibilities is: |
| 585 | * <p> |
| 586 | * Long tap: Always toggle selection of conversation. If CAB mode is not |
| 587 | * started, then start it. |
Vikram Aggarwal | 9730ea0 | 2012-08-02 12:46:19 -0700 | [diff] [blame] | 588 | * <pre> |
| 589 | * | Checkboxes | No Checkboxes |
| 590 | * ----------+------------+--------------- |
| 591 | * CAB mode | Select | Select |
| 592 | * List mode | Select | Select |
| 593 | * |
Vikram Aggarwal | 9730ea0 | 2012-08-02 12:46:19 -0700 | [diff] [blame] | 594 | * </pre> |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 595 | * |
Vikram Aggarwal | 9730ea0 | 2012-08-02 12:46:19 -0700 | [diff] [blame] | 596 | * Reference: http://b/issue?id=6392199 |
| 597 | * <p> |
| 598 | * {@inheritDoc} |
| 599 | */ |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 600 | @Override |
| 601 | public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { |
Mindy Pereira | 1e231a6 | 2012-03-23 12:42:15 -0700 | [diff] [blame] | 602 | // Ignore anything that is not a conversation item. Could be a footer. |
| 603 | if (!(view instanceof ConversationItemView)) { |
Scott Kennedy | 7c8325d | 2013-02-28 10:46:10 -0800 | [diff] [blame] | 604 | return false; |
Mindy Pereira | 1e231a6 | 2012-03-23 12:42:15 -0700 | [diff] [blame] | 605 | } |
Scott Kennedy | 955a766 | 2013-08-06 17:12:57 -0700 | [diff] [blame] | 606 | return ((ConversationItemView) view).toggleSelectedStateOrBeginDrag(); |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 607 | } |
| 608 | |
Vikram Aggarwal | 4f1cc0b | 2012-08-02 15:16:30 -0700 | [diff] [blame] | 609 | /** |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 610 | * See the comment for |
| 611 | * {@link #onItemLongClick(AdapterView, View, int, long)}. |
| 612 | * <p> |
| 613 | * Short tap behavior: |
| 614 | * |
Vikram Aggarwal | 4f1cc0b | 2012-08-02 15:16:30 -0700 | [diff] [blame] | 615 | * <pre> |
| 616 | * | Checkboxes | No Checkboxes |
| 617 | * ----------+------------+--------------- |
Scott Kennedy | 9ba7fba | 2013-07-30 17:41:26 -0700 | [diff] [blame] | 618 | * CAB mode | Peek | Select |
Vikram Aggarwal | 4f1cc0b | 2012-08-02 15:16:30 -0700 | [diff] [blame] | 619 | * List mode | Peek | Peek |
| 620 | * </pre> |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 621 | * |
Vikram Aggarwal | 4f1cc0b | 2012-08-02 15:16:30 -0700 | [diff] [blame] | 622 | * Reference: http://b/issue?id=6392199 |
| 623 | * <p> |
| 624 | * {@inheritDoc} |
| 625 | */ |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 626 | @Override |
Vikram Aggarwal | 54452ae | 2012-03-13 15:29:00 -0700 | [diff] [blame] | 627 | public void onListItemClick(ListView l, View view, int position, long id) { |
Scott Kennedy | 0e8dc84 | 2013-09-10 11:13:53 -0700 | [diff] [blame] | 628 | if (view instanceof ToggleableItem) { |
Scott Kennedy | 5dce146 | 2013-08-05 11:26:40 -0700 | [diff] [blame] | 629 | final boolean showSenderImage = |
| 630 | (mAccount.settings.convListIcon == ConversationListIcon.SENDER_IMAGE); |
Andy Huang | 042a530 | 2013-08-13 12:39:08 -0700 | [diff] [blame] | 631 | final boolean inCabMode = !mSelectedSet.isEmpty(); |
| 632 | if (!showSenderImage && inCabMode) { |
Vikram Aggarwal | 37a20ca | 2013-06-06 11:19:49 -0700 | [diff] [blame] | 633 | ((ToggleableItem) view).toggleSelectedState(); |
| 634 | } else { |
Andy Huang | 042a530 | 2013-08-13 12:39:08 -0700 | [diff] [blame] | 635 | if (inCabMode) { |
| 636 | // this is a peek. |
| 637 | Analytics.getInstance().sendEvent("peek", null, null, mSelectedSet.size()); |
| 638 | } |
Jin Cao | 72953f2 | 2014-04-15 18:23:37 -0700 | [diff] [blame] | 639 | AnalyticsTimer.getInstance().trackStart(AnalyticsTimer.OPEN_CONV_VIEW_FROM_LIST); |
Scott Kennedy | 8afccad | 2013-07-28 19:09:11 -0700 | [diff] [blame] | 640 | viewConversation(position); |
Vikram Aggarwal | 37a20ca | 2013-06-06 11:19:49 -0700 | [diff] [blame] | 641 | } |
Vikram Aggarwal | 4f1cc0b | 2012-08-02 15:16:30 -0700 | [diff] [blame] | 642 | } else { |
Vikram Aggarwal | 37a20ca | 2013-06-06 11:19:49 -0700 | [diff] [blame] | 643 | // Ignore anything that is not a conversation item. Could be a footer. |
| 644 | // If we are using a keyboard, the highlighted item is the parent; |
| 645 | // otherwise, this is a direct call from the ConverationItemView |
| 646 | return; |
Vikram Aggarwal | 4f1cc0b | 2012-08-02 15:16:30 -0700 | [diff] [blame] | 647 | } |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 648 | // When a new list item is clicked, commit any existing leave behind |
mindyp | 8694fe9 | 2012-09-25 11:07:16 -0700 | [diff] [blame] | 649 | // items. Wait until we have opened the desired conversation to cause |
| 650 | // any position changes. |
Vikram Aggarwal | bcb16b9 | 2013-01-28 18:05:03 -0800 | [diff] [blame] | 651 | commitDestructiveActions(Utils.useTabletUI(mActivity.getActivityContext().getResources())); |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | @Override |
Scott Kennedy | 08a079c | 2013-02-22 10:13:10 -0800 | [diff] [blame] | 655 | public void onResume() { |
| 656 | super.onResume(); |
| 657 | |
| 658 | final ConversationCursor conversationCursor = getConversationListCursor(); |
Jin Cao | fa93427 | 2014-05-01 13:38:21 -0700 | [diff] [blame^] | 659 | if (!isCursorReadyToShow(conversationCursor)) { |
| 660 | mInitialCursorLoading = true; |
| 661 | |
| 662 | // Let's show the list view when we resume and are waiting for load again |
| 663 | showListView(); |
| 664 | } |
| 665 | |
Scott Kennedy | 08a079c | 2013-02-22 10:13:10 -0800 | [diff] [blame] | 666 | if (conversationCursor != null) { |
| 667 | conversationCursor.handleNotificationActions(); |
Scott Kennedy | f77806e | 2013-08-30 11:38:15 -0700 | [diff] [blame] | 668 | |
| 669 | restoreLastScrolledPosition(); |
Scott Kennedy | 08a079c | 2013-02-22 10:13:10 -0800 | [diff] [blame] | 670 | } |
Scott Kennedy | 1fea6a3 | 2013-07-09 15:58:51 -0700 | [diff] [blame] | 671 | |
| 672 | mSelectedSet.addObserver(mConversationSetObserver); |
Scott Kennedy | 08a079c | 2013-02-22 10:13:10 -0800 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | @Override |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 676 | public void onPause() { |
| 677 | super.onPause(); |
Scott Kennedy | 1fea6a3 | 2013-07-09 15:58:51 -0700 | [diff] [blame] | 678 | |
| 679 | mSelectedSet.removeObserver(mConversationSetObserver); |
Scott Kennedy | f77806e | 2013-08-30 11:38:15 -0700 | [diff] [blame] | 680 | |
| 681 | saveLastScrolledPosition(); |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | @Override |
| 685 | public void onSaveInstanceState(Bundle outState) { |
| 686 | super.onSaveInstanceState(outState); |
| 687 | if (mListView != null) { |
| 688 | outState.putParcelable(LIST_STATE_KEY, mListView.onSaveInstanceState()); |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame] | 689 | outState.putInt(CHOICE_MODE_KEY, mListView.getChoiceMode()); |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 690 | } |
Scott Kennedy | 32ddb84 | 2013-08-28 17:38:22 -0700 | [diff] [blame] | 691 | |
| 692 | if (mListAdapter != null) { |
| 693 | mListAdapter.saveSpecialItemInstanceState(outState); |
| 694 | } |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | @Override |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 698 | public void onStart() { |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 699 | super.onStart(); |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 700 | mHandler.postDelayed(mUpdateTimestampsRunnable, TIMESTAMP_UPDATE_INTERVAL); |
Andy Huang | a90c33b | 2013-11-25 17:02:05 -0800 | [diff] [blame] | 701 | Analytics.getInstance().sendView("ConversationListFragment"); |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | @Override |
| 705 | public void onStop() { |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 706 | super.onStop(); |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 707 | mHandler.removeCallbacks(mUpdateTimestampsRunnable); |
| 708 | } |
| 709 | |
Vikram Aggarwal | 80aeac5 | 2012-02-07 15:27:20 -0800 | [diff] [blame] | 710 | @Override |
Vikram Aggarwal | fa131a2 | 2012-02-02 13:56:22 -0800 | [diff] [blame] | 711 | public void onViewModeChanged(int newMode) { |
Vikram Aggarwal | fa131a2 | 2012-02-02 13:56:22 -0800 | [diff] [blame] | 712 | if (mTabletDevice) { |
Andy Huang | b0be3fc | 2013-05-02 15:48:05 -0700 | [diff] [blame] | 713 | if (ViewMode.isListMode(newMode)) { |
Vikram Aggarwal | 0509bba | 2013-01-29 17:11:30 -0800 | [diff] [blame] | 714 | // There are no selected conversations when in conversation list mode. |
Andy Huang | b0be3fc | 2013-05-02 15:48:05 -0700 | [diff] [blame] | 715 | clearChoicesAndActivated(); |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 716 | } |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 717 | } |
Mindy Pereira | b5901be | 2012-08-09 17:21:53 -0700 | [diff] [blame] | 718 | if (mFooterView != null) { |
| 719 | mFooterView.onViewModeChanged(newMode); |
| 720 | } |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 721 | } |
Mindy Pereira | b5901be | 2012-08-09 17:21:53 -0700 | [diff] [blame] | 722 | |
Andy Huang | 48ccbc5 | 2013-06-05 20:30:47 -0700 | [diff] [blame] | 723 | public boolean isAnimating() { |
| 724 | final AnimatedAdapter adapter = getAnimatedAdapter(); |
| 725 | return (adapter != null && adapter.isAnimating()) || |
| 726 | (mListView != null && mListView.isScrolling()); |
| 727 | } |
| 728 | |
Andy Huang | b0be3fc | 2013-05-02 15:48:05 -0700 | [diff] [blame] | 729 | private void clearChoicesAndActivated() { |
| 730 | final int currentSelected = mListView.getCheckedItemPosition(); |
Andy Huang | b0be3fc | 2013-05-02 15:48:05 -0700 | [diff] [blame] | 731 | if (currentSelected != ListView.INVALID_POSITION) { |
Andy Huang | f0aebd3 | 2013-06-17 15:00:15 -0700 | [diff] [blame] | 732 | mListView.setItemChecked(mListView.getCheckedItemPosition(), false); |
Andy Huang | b0be3fc | 2013-05-02 15:48:05 -0700 | [diff] [blame] | 733 | } |
| 734 | } |
| 735 | |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 736 | /** |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 737 | * Handles a request to show a new conversation list, either from a search |
| 738 | * query or for viewing a folder. This will initiate a data load, and hence |
| 739 | * must be called on the UI thread. |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 740 | */ |
| 741 | private void showList() { |
Jin Cao | faa9198 | 2014-04-24 16:52:26 -0700 | [diff] [blame] | 742 | mInitialCursorLoading = true; |
Andy Huang | 7591d2f | 2012-07-26 16:48:06 -0700 | [diff] [blame] | 743 | onFolderUpdated(mActivity.getFolderController().getFolder()); |
mindyp | ca8ca2d | 2012-09-11 17:38:34 -0700 | [diff] [blame] | 744 | onConversationListStatusUpdated(); |
Andy Huang | a90c33b | 2013-11-25 17:02:05 -0800 | [diff] [blame] | 745 | |
| 746 | // try to get an order-of-magnitude sense for message count within folders |
| 747 | // (N.B. this count currently isn't working for search folders, since their counts stream |
| 748 | // in over time in pieces.) |
| 749 | final Folder f = mViewContext.folder; |
| 750 | if (f != null) { |
| 751 | final long countLog; |
| 752 | if (f.totalCount > 0) { |
| 753 | countLog = (long) Math.log10(f.totalCount); |
| 754 | } else { |
| 755 | countLog = 0; |
| 756 | } |
| 757 | Analytics.getInstance().sendEvent("view_folder", f.getTypeDescription(), |
| 758 | Long.toString(countLog), f.totalCount); |
| 759 | } |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 760 | } |
| 761 | |
Scott Kennedy | 8afccad | 2013-07-28 19:09:11 -0700 | [diff] [blame] | 762 | /** |
| 763 | * View the message at the given position. |
| 764 | * |
| 765 | * @param position The position of the conversation in the list (as opposed to its position |
| 766 | * in the cursor) |
| 767 | */ |
| 768 | private void viewConversation(final int position) { |
| 769 | LogUtils.d(LOG_TAG, "ConversationListFragment.viewConversation(%d)", position); |
| 770 | |
| 771 | final ConversationCursor cursor = |
| 772 | (ConversationCursor) getAnimatedAdapter().getItem(position); |
| 773 | |
| 774 | if (cursor == null) { |
| 775 | LogUtils.e(LOG_TAG, |
| 776 | "unable to open conv at cursor pos=%s cursor=%s getPositionOffset=%s", |
| 777 | position, cursor, getAnimatedAdapter().getPositionOffset(position)); |
| 778 | return; |
| 779 | } |
| 780 | |
| 781 | final Conversation conv = cursor.getConversation(); |
| 782 | /* |
| 783 | * The cursor position may be different than the position method parameter because of |
| 784 | * special views in the list. |
| 785 | */ |
| 786 | conv.position = cursor.getPosition(); |
| 787 | setSelected(conv.position, true); |
| 788 | mCallbacks.onConversationSelected(conv, false /* inLoaderCallbacks */); |
| 789 | } |
| 790 | |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame] | 791 | /** |
| 792 | * Sets the selected conversation to the position given here. |
Alice Yang | 0d74a66 | 2013-03-25 14:01:24 -0700 | [diff] [blame] | 793 | * @param cursorPosition The position of the conversation in the cursor (as opposed to |
| 794 | * in the list) |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame] | 795 | * @param different if the currently selected conversation is different from the one provided |
| 796 | * here. This is a difference in conversations, not a difference in positions. For example, a |
| 797 | * conversation at position 2 can move to position 4 as a result of new mail. |
| 798 | */ |
Scott Kennedy | 7c8325d | 2013-02-28 10:46:10 -0800 | [diff] [blame] | 799 | public void setSelected(final int cursorPosition, boolean different) { |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame] | 800 | if (mListView.getChoiceMode() == ListView.CHOICE_MODE_NONE) { |
| 801 | return; |
| 802 | } |
Scott Kennedy | 7c8325d | 2013-02-28 10:46:10 -0800 | [diff] [blame] | 803 | |
| 804 | final int position = |
| 805 | cursorPosition + getAnimatedAdapter().getPositionOffset(cursorPosition); |
| 806 | |
Scott Kennedy | e0d01fb | 2013-08-19 19:07:54 -0700 | [diff] [blame] | 807 | setRawSelected(position, different); |
| 808 | } |
| 809 | |
| 810 | /** |
| 811 | * Sets the selected conversation to the position given here. |
| 812 | * @param position The position of the item in the list |
| 813 | * @param different if the currently selected conversation is different from the one provided |
| 814 | * here. This is a difference in conversations, not a difference in positions. For example, a |
| 815 | * conversation at position 2 can move to position 4 as a result of new mail. |
| 816 | */ |
| 817 | public void setRawSelected(final int position, final boolean different) { |
| 818 | if (mListView.getChoiceMode() == ListView.CHOICE_MODE_NONE) { |
| 819 | return; |
| 820 | } |
| 821 | |
mindyp | e21f886 | 2012-10-01 09:32:03 -0700 | [diff] [blame] | 822 | if (different) { |
| 823 | mListView.smoothScrollToPosition(position); |
| 824 | } |
| 825 | mListView.setItemChecked(position, true); |
| 826 | } |
| 827 | |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 828 | /** |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame] | 829 | * Returns the cursor associated with the conversation list. |
| 830 | * @return |
Vikram Aggarwal | 3d7ca9d | 2012-05-11 14:40:36 -0700 | [diff] [blame] | 831 | */ |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 832 | private ConversationCursor getConversationListCursor() { |
| 833 | return mCallbacks != null ? mCallbacks.getConversationListCursor() : null; |
Mindy Pereira | fe06bea | 2012-02-16 08:15:14 -0800 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | /** |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 837 | * Request a refresh of the list. No sync is carried out and none is |
| 838 | * promised. |
Vikram Aggarwal | 54452ae | 2012-03-13 15:29:00 -0700 | [diff] [blame] | 839 | */ |
| 840 | public void requestListRefresh() { |
| 841 | mListAdapter.notifyDataSetChanged(); |
| 842 | } |
| 843 | |
Vikram Aggarwal | 75daee5 | 2012-04-30 11:13:09 -0700 | [diff] [blame] | 844 | /** |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 845 | * Change the UI to delete the conversations provided and then call the |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 846 | * {@link DestructiveAction} provided here <b>after</b> the UI has been |
| 847 | * updated. |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 848 | * @param conversations |
Vikram Aggarwal | 75daee5 | 2012-04-30 11:13:09 -0700 | [diff] [blame] | 849 | * @param action |
| 850 | */ |
Vikram Aggarwal | a8e4318 | 2012-09-13 12:55:10 -0700 | [diff] [blame] | 851 | public void requestDelete(int actionId, final Collection<Conversation> conversations, |
Vikram Aggarwal | 669947b | 2013-01-10 17:05:56 -0800 | [diff] [blame] | 852 | final DestructiveAction action) { |
Mindy Pereira | acf6039 | 2012-04-06 09:11:00 -0700 | [diff] [blame] | 853 | for (Conversation conv : conversations) { |
| 854 | conv.localDeleteOnUpdate = true; |
| 855 | } |
Vikram Aggarwal | a8e4318 | 2012-09-13 12:55:10 -0700 | [diff] [blame] | 856 | final ListItemsRemovedListener listener = new ListItemsRemovedListener() { |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 857 | @Override |
| 858 | public void onListItemsRemoved() { |
| 859 | action.performAction(); |
| 860 | } |
Vikram Aggarwal | a8e4318 | 2012-09-13 12:55:10 -0700 | [diff] [blame] | 861 | }; |
| 862 | final SwipeableListView listView = (SwipeableListView) getListView(); |
| 863 | if (listView.getSwipeAction() == actionId) { |
Paul Westbrook | cec3e0b | 2013-02-04 22:50:22 -0800 | [diff] [blame] | 864 | if (!listView.destroyItems(conversations, listener)) { |
| 865 | // The listView failed to destroy the items, perform the action manually |
| 866 | LogUtils.e(LOG_TAG, "ConversationListFragment.requestDelete: " + |
| 867 | "listView failed to destroy items."); |
| 868 | action.performAction(); |
| 869 | } |
Vikram Aggarwal | a8e4318 | 2012-09-13 12:55:10 -0700 | [diff] [blame] | 870 | return; |
| 871 | } |
| 872 | // Delete the local delete items (all for now) and when done, |
| 873 | // update... |
| 874 | mListAdapter.delete(conversations, listener); |
Mindy Pereira | acf6039 | 2012-04-06 09:11:00 -0700 | [diff] [blame] | 875 | } |
Mindy Pereira | a3911aa | 2012-03-09 13:26:26 -0800 | [diff] [blame] | 876 | |
Mindy Pereira | 11dd5ef | 2012-03-10 15:10:18 -0800 | [diff] [blame] | 877 | public void onFolderUpdated(Folder folder) { |
Jin Cao | 8bf9c21 | 2014-04-25 15:00:53 -0700 | [diff] [blame] | 878 | if (mInitialCursorLoading) { |
Jin Cao | faa9198 | 2014-04-24 16:52:26 -0700 | [diff] [blame] | 879 | // Wait a bit before showing either the empty or loading view. If the messages are |
| 880 | // actually local, it's disorienting to see this appear on every folder transition. |
| 881 | // If they aren't, then it will likely take more than 200 milliseconds to load, and |
| 882 | // then we'll see the loading view. |
| 883 | if (!mLoadingViewPending) { |
| 884 | mHandler.postDelayed(mLoadingViewRunnable, LOADING_DELAY_MS); |
| 885 | mLoadingViewPending = true; |
| 886 | } |
| 887 | } |
| 888 | |
Mindy Pereira | dc8963b | 2012-03-28 17:51:05 -0700 | [diff] [blame] | 889 | mFolder = folder; |
Mindy Pereira | 06642fa | 2012-07-12 16:23:27 -0700 | [diff] [blame] | 890 | setSwipeAction(); |
Mindy Pereira | 4bb435c | 2013-11-13 14:21:15 -0800 | [diff] [blame] | 891 | |
| 892 | // Update enabled state of swipe to refresh. |
| 893 | mSwipeRefreshWidget.setEnabled(!ConversationListContext.isSearchResult(mViewContext)); |
| 894 | |
Mindy Pereira | 82cea48 | 2012-03-27 16:45:00 -0700 | [diff] [blame] | 895 | if (mFolder == null) { |
| 896 | return; |
| 897 | } |
Mindy Pereira | 4584a0d | 2012-03-13 14:42:14 -0700 | [diff] [blame] | 898 | mListAdapter.setFolder(mFolder); |
Mindy Pereira | 70a70c9 | 2012-08-02 08:39:45 -0700 | [diff] [blame] | 899 | mFooterView.setFolder(mFolder); |
Vikram Aggarwal | 41b9e8f | 2012-09-25 10:15:04 -0700 | [diff] [blame] | 900 | if (!mFolder.wasSyncSuccessful()) { |
Mindy Pereira | 70a70c9 | 2012-08-02 08:39:45 -0700 | [diff] [blame] | 901 | mErrorListener.onError(mFolder, false); |
| 902 | } |
Paul Westbrook | c01f5d9 | 2012-09-25 17:22:25 -0700 | [diff] [blame] | 903 | |
| 904 | // Notify of changes to the Folder. |
| 905 | onFolderStatusUpdated(); |
| 906 | |
Mindy Pereira | 12fe37a | 2012-08-15 10:02:57 -0700 | [diff] [blame] | 907 | // Blow away conversation items cache. |
| 908 | ConversationItemViewModel.onFolderUpdated(mFolder); |
Mindy Pereira | 70a70c9 | 2012-08-02 08:39:45 -0700 | [diff] [blame] | 909 | } |
| 910 | |
Vikram Aggarwal | 81a4f08 | 2012-09-28 09:19:04 -0700 | [diff] [blame] | 911 | /** |
| 912 | * Updates the footer visibility and updates the conversation cursor |
| 913 | */ |
Mindy Pereira | 70a70c9 | 2012-08-02 08:39:45 -0700 | [diff] [blame] | 914 | public void onConversationListStatusUpdated() { |
Paul Westbrook | c01f5d9 | 2012-09-25 17:22:25 -0700 | [diff] [blame] | 915 | // Also change the cursor here. |
| 916 | onCursorUpdated(); |
Jin Cao | faa9198 | 2014-04-24 16:52:26 -0700 | [diff] [blame] | 917 | |
Jin Cao | 8bf9c21 | 2014-04-25 15:00:53 -0700 | [diff] [blame] | 918 | if (!mInitialCursorLoading && mCanTakeDownLoadingView) { |
Jin Cao | faa9198 | 2014-04-24 16:52:26 -0700 | [diff] [blame] | 919 | hideLoadingViewAndShowContents(); |
| 920 | } |
Paul Westbrook | c01f5d9 | 2012-09-25 17:22:25 -0700 | [diff] [blame] | 921 | } |
| 922 | |
Martin Hibdon | 4cdc485 | 2014-04-04 15:46:12 -0700 | [diff] [blame] | 923 | private void hideLoadingViewAndShowContents() { |
Martin Hibdon | 4cdc485 | 2014-04-04 15:46:12 -0700 | [diff] [blame] | 924 | final ConversationCursor cursor = getConversationListCursor(); |
Jin Cao | 3f386e8 | 2014-04-18 11:29:54 -0700 | [diff] [blame] | 925 | if (cursor != null && cursor.getCount() == 0) { |
| 926 | showEmptyView(); |
| 927 | } else { |
| 928 | showListView(); |
| 929 | } |
Martin Hibdon | 4cdc485 | 2014-04-04 15:46:12 -0700 | [diff] [blame] | 930 | final boolean showFooter = mFooterView.updateStatus(cursor); |
| 931 | // Update the folder status, in case the cursor could affect it. |
| 932 | onFolderStatusUpdated(); |
| 933 | mListAdapter.setFooterVisibility(showFooter); |
| 934 | mLoadingViewPending = false; |
| 935 | mHandler.removeCallbacks(mLoadingViewRunnable); |
| 936 | } |
| 937 | |
Paul Westbrook | c01f5d9 | 2012-09-25 17:22:25 -0700 | [diff] [blame] | 938 | private void onFolderStatusUpdated() { |
Alice Yang | 0d74a66 | 2013-03-25 14:01:24 -0700 | [diff] [blame] | 939 | // Update the sync status bar with sync results if needed |
Alice Yang | 486e63e | 2013-04-05 13:01:50 -0700 | [diff] [blame] | 940 | checkSyncStatus(); |
Alice Yang | 0d74a66 | 2013-03-25 14:01:24 -0700 | [diff] [blame] | 941 | |
Paul Westbrook | c01f5d9 | 2012-09-25 17:22:25 -0700 | [diff] [blame] | 942 | final ConversationCursor cursor = getConversationListCursor(); |
Paul Westbrook | 9a70e91 | 2012-08-17 15:53:20 -0700 | [diff] [blame] | 943 | Bundle extras = cursor != null ? cursor.getExtras() : Bundle.EMPTY; |
Andrew Sapperstein | 427df9d | 2013-05-15 12:05:09 -0700 | [diff] [blame] | 944 | int errorStatus = extras.containsKey(UIProvider.CursorExtraKeys.EXTRA_ERROR) ? |
Mindy Pereira | 70a70c9 | 2012-08-02 08:39:45 -0700 | [diff] [blame] | 945 | extras.getInt(UIProvider.CursorExtraKeys.EXTRA_ERROR) |
| 946 | : UIProvider.LastSyncResult.SUCCESS; |
Andrew Sapperstein | 427df9d | 2013-05-15 12:05:09 -0700 | [diff] [blame] | 947 | int cursorStatus = extras.getInt(UIProvider.CursorExtraKeys.EXTRA_STATUS); |
Alice Yang | 0d74a66 | 2013-03-25 14:01:24 -0700 | [diff] [blame] | 948 | // We want to update the UI with this information if either we are loaded or complete, or |
Paul Westbrook | c01f5d9 | 2012-09-25 17:22:25 -0700 | [diff] [blame] | 949 | // we have a folder with a non-0 count. |
| 950 | final int folderCount = mFolder != null ? mFolder.totalCount : 0; |
Andrew Sapperstein | 427df9d | 2013-05-15 12:05:09 -0700 | [diff] [blame] | 951 | if (errorStatus == UIProvider.LastSyncResult.SUCCESS |
| 952 | && (cursorStatus == UIProvider.CursorStatus.LOADED |
| 953 | || cursorStatus == UIProvider.CursorStatus.COMPLETE) || folderCount > 0) { |
Paul Westbrook | c01f5d9 | 2012-09-25 17:22:25 -0700 | [diff] [blame] | 954 | updateSearchResultHeader(folderCount); |
Mindy Pereira | 70a70c9 | 2012-08-02 08:39:45 -0700 | [diff] [blame] | 955 | } |
Mindy Pereira | a3911aa | 2012-03-09 13:26:26 -0800 | [diff] [blame] | 956 | } |
Mindy Pereira | f6a6b50 | 2012-03-15 15:24:26 -0700 | [diff] [blame] | 957 | |
Mindy Pereira | 06642fa | 2012-07-12 16:23:27 -0700 | [diff] [blame] | 958 | private void setSwipeAction() { |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 959 | int swipeSetting = Settings.getSwipeSetting(mAccount.settings); |
Mindy Pereira | e58222b | 2012-07-25 14:33:18 -0700 | [diff] [blame] | 960 | if (swipeSetting == Swipe.DISABLED |
Mindy Pereira | b807337 | 2012-08-09 14:00:10 -0700 | [diff] [blame] | 961 | || !mAccount.supportsCapability(AccountCapabilities.UNDO) |
| 962 | || (mFolder != null && mFolder.isTrash())) { |
Mindy Pereira | e58222b | 2012-07-25 14:33:18 -0700 | [diff] [blame] | 963 | mListView.enableSwipe(false); |
| 964 | } else { |
Scott Kennedy | 3b965d7 | 2013-06-25 14:36:55 -0700 | [diff] [blame] | 965 | final int action; |
Rohan Shah | 7f98d0d | 2013-02-15 13:21:09 -0800 | [diff] [blame] | 966 | mListView.enableSwipe(true); |
Jin Cao | d4e02e4 | 2014-03-26 13:04:40 -0700 | [diff] [blame] | 967 | if (mFolder == null) { |
Mindy Pereira | 01f3050 | 2012-08-14 10:30:51 -0700 | [diff] [blame] | 968 | action = R.id.remove_folder; |
Mindy Pereira | 4e812f4 | 2012-07-30 16:52:49 -0700 | [diff] [blame] | 969 | } else { |
Mindy Pereira | 4e812f4 | 2012-07-30 16:52:49 -0700 | [diff] [blame] | 970 | switch (swipeSetting) { |
Jin Cao | d4e02e4 | 2014-03-26 13:04:40 -0700 | [diff] [blame] | 971 | // Try to respect user's setting as best as we can and default to doing nothing |
| 972 | case Swipe.DELETE: |
| 973 | action = R.id.delete; |
| 974 | break; |
Mindy Pereira | 4e812f4 | 2012-07-30 16:52:49 -0700 | [diff] [blame] | 975 | case Swipe.ARCHIVE: |
Jin Cao | d4e02e4 | 2014-03-26 13:04:40 -0700 | [diff] [blame] | 976 | // Special case spam since it shouldn't remove spam folder label on swipe |
| 977 | if (mAccount.supportsCapability(AccountCapabilities.ARCHIVE) |
| 978 | && !mFolder.isSpam()) { |
Mindy Pereira | 4e812f4 | 2012-07-30 16:52:49 -0700 | [diff] [blame] | 979 | if (mFolder.supportsCapability(FolderCapabilities.ARCHIVE)) { |
| 980 | action = R.id.archive; |
| 981 | break; |
| 982 | } else if (mFolder.supportsCapability |
| 983 | (FolderCapabilities.CAN_ACCEPT_MOVED_MESSAGES)) { |
Mindy Pereira | 01f3050 | 2012-08-14 10:30:51 -0700 | [diff] [blame] | 984 | action = R.id.remove_folder; |
Mindy Pereira | 4e812f4 | 2012-07-30 16:52:49 -0700 | [diff] [blame] | 985 | break; |
| 986 | } |
| 987 | } |
Scott Kennedy | 3b965d7 | 2013-06-25 14:36:55 -0700 | [diff] [blame] | 988 | |
| 989 | /* |
| 990 | * If we get here, we don't support archive, on either the account or the |
Jin Cao | 3432b63 | 2014-03-28 13:35:44 -0700 | [diff] [blame] | 991 | * folder, so we want to fall through to swipe doing nothing |
Scott Kennedy | 3b965d7 | 2013-06-25 14:36:55 -0700 | [diff] [blame] | 992 | */ |
| 993 | //$FALL-THROUGH$ |
Mindy Pereira | 4e812f4 | 2012-07-30 16:52:49 -0700 | [diff] [blame] | 994 | default: |
Jin Cao | 3432b63 | 2014-03-28 13:35:44 -0700 | [diff] [blame] | 995 | mListView.enableSwipe(false); |
| 996 | action = 0; // Use default value so setSwipeAction essentially has no effect |
Mindy Pereira | 4e812f4 | 2012-07-30 16:52:49 -0700 | [diff] [blame] | 997 | break; |
| 998 | } |
Mindy Pereira | e58222b | 2012-07-25 14:33:18 -0700 | [diff] [blame] | 999 | } |
| 1000 | mListView.setSwipeAction(action); |
| 1001 | } |
Mark Wei | 6126d72 | 2013-04-24 21:09:43 -0700 | [diff] [blame] | 1002 | mListView.setCurrentAccount(mAccount); |
Mindy Pereira | 06642fa | 2012-07-12 16:23:27 -0700 | [diff] [blame] | 1003 | mListView.setCurrentFolder(mFolder); |
| 1004 | } |
| 1005 | |
Vikram Aggarwal | 17f373e | 2012-09-17 15:49:32 -0700 | [diff] [blame] | 1006 | /** |
| 1007 | * Changes the conversation cursor in the list and sets selected position if none is set. |
| 1008 | */ |
| 1009 | private void onCursorUpdated() { |
| 1010 | if (mCallbacks == null || mListAdapter == null) { |
| 1011 | return; |
| 1012 | } |
Vikram Aggarwal | 81a4f08 | 2012-09-28 09:19:04 -0700 | [diff] [blame] | 1013 | // Check against the previous cursor here and see if they are the same. If they are, then |
| 1014 | // do a notifyDataSetChanged. |
| 1015 | final ConversationCursor newCursor = mCallbacks.getConversationListCursor(); |
Scott Kennedy | f77806e | 2013-08-30 11:38:15 -0700 | [diff] [blame] | 1016 | |
| 1017 | if (newCursor == null && mListAdapter.getCursor() != null) { |
| 1018 | // We're losing our cursor, so save our scroll position |
| 1019 | saveLastScrolledPosition(); |
| 1020 | } |
| 1021 | |
Vikram Aggarwal | 81a4f08 | 2012-09-28 09:19:04 -0700 | [diff] [blame] | 1022 | mListAdapter.swapCursor(newCursor); |
| 1023 | // When the conversation cursor is *updated*, we get back the same instance. In that |
| 1024 | // situation, CursorAdapter.swapCursor() silently returns, without forcing a |
| 1025 | // notifyDataSetChanged(). So let's force a call to notifyDataSetChanged, since an updated |
| 1026 | // cursor means that the dataset has changed. |
| 1027 | final int newCursorHash = (newCursor == null) ? 0 : newCursor.hashCode(); |
| 1028 | if (mConversationCursorHash == newCursorHash && mConversationCursorHash != 0) { |
| 1029 | mListAdapter.notifyDataSetChanged(); |
| 1030 | } |
| 1031 | mConversationCursorHash = newCursorHash; |
Scott Kennedy | b1e2148 | 2013-03-15 13:40:38 -0700 | [diff] [blame] | 1032 | |
Jin Cao | 8bf9c21 | 2014-04-25 15:00:53 -0700 | [diff] [blame] | 1033 | updateAnalyticsData(newCursor); |
Jin Cao | 779dd60 | 2014-04-22 16:16:28 -0700 | [diff] [blame] | 1034 | |
Scott Kennedy | 4b9b395 | 2013-03-20 17:20:59 -0700 | [diff] [blame] | 1035 | if (newCursor != null && newCursor.getCount() > 0) { |
Scott Kennedy | b1e2148 | 2013-03-15 13:40:38 -0700 | [diff] [blame] | 1036 | newCursor.markContentsSeen(); |
Scott Kennedy | f77806e | 2013-08-30 11:38:15 -0700 | [diff] [blame] | 1037 | restoreLastScrolledPosition(); |
Scott Kennedy | b1e2148 | 2013-03-15 13:40:38 -0700 | [diff] [blame] | 1038 | } |
| 1039 | |
Vikram Aggarwal | 17f373e | 2012-09-17 15:49:32 -0700 | [diff] [blame] | 1040 | // If a current conversation is available, and none is selected in the list, then ask |
| 1041 | // the list to select the current conversation. |
| 1042 | final Conversation conv = mCallbacks.getCurrentConversation(); |
Scott Kennedy | f77806e | 2013-08-30 11:38:15 -0700 | [diff] [blame] | 1043 | if (conv != null) { |
| 1044 | if (mListView.getChoiceMode() != ListView.CHOICE_MODE_NONE |
| 1045 | && mListView.getCheckedItemPosition() == -1) { |
| 1046 | setSelected(conv.position, true); |
| 1047 | } |
Mindy Pereira | 967ede6 | 2012-03-22 09:29:09 -0700 | [diff] [blame] | 1048 | } |
Mindy Pereira | 21ab490 | 2012-03-19 18:48:03 -0700 | [diff] [blame] | 1049 | } |
Mindy Pereira | 1e2573b | 2012-04-17 14:34:36 -0700 | [diff] [blame] | 1050 | |
mindyp | c6adce3 | 2012-08-22 18:46:42 -0700 | [diff] [blame] | 1051 | public void commitDestructiveActions(boolean animate) { |
Mindy Pereira | 8937bf1 | 2012-07-23 14:05:02 -0700 | [diff] [blame] | 1052 | if (mListView != null) { |
mindyp | c6adce3 | 2012-08-22 18:46:42 -0700 | [diff] [blame] | 1053 | mListView.commitDestructiveActions(animate); |
Vikram Aggarwal | 7d81600 | 2012-04-17 17:06:41 -0700 | [diff] [blame] | 1054 | |
| 1055 | } |
| 1056 | } |
| 1057 | |
mindyp | 9365a82 | 2012-09-12 09:09:09 -0700 | [diff] [blame] | 1058 | @Override |
| 1059 | public void onListItemSwiped(Collection<Conversation> conversations) { |
| 1060 | mUpdater.showNextConversation(conversations); |
| 1061 | } |
Alice Yang | 0d74a66 | 2013-03-25 14:01:24 -0700 | [diff] [blame] | 1062 | |
Alice Yang | 486e63e | 2013-04-05 13:01:50 -0700 | [diff] [blame] | 1063 | private void checkSyncStatus() { |
Alice Yang | 76d2065 | 2013-04-24 02:32:48 -0700 | [diff] [blame] | 1064 | if (mFolder != null && mFolder.isSyncInProgress()) { |
Alice Yang | 03752f3 | 2013-05-05 15:05:16 -0700 | [diff] [blame] | 1065 | LogUtils.d(LOG_TAG, "CLF.checkSyncStatus still syncing"); |
Alice Yang | 486e63e | 2013-04-05 13:01:50 -0700 | [diff] [blame] | 1066 | // Still syncing, ignore |
| 1067 | } else { |
| 1068 | // Finished syncing: |
Alice Yang | 03752f3 | 2013-05-05 15:05:16 -0700 | [diff] [blame] | 1069 | LogUtils.d(LOG_TAG, "CLF.checkSyncStatus done syncing"); |
Mindy Pereira | 4bb435c | 2013-11-13 14:21:15 -0800 | [diff] [blame] | 1070 | mSwipeRefreshWidget.setRefreshing(false); |
Alice Yang | 486e63e | 2013-04-05 13:01:50 -0700 | [diff] [blame] | 1071 | } |
| 1072 | } |
| 1073 | |
Alice Yang | 0d74a66 | 2013-03-25 14:01:24 -0700 | [diff] [blame] | 1074 | /** |
Alice Yang | 486e63e | 2013-04-05 13:01:50 -0700 | [diff] [blame] | 1075 | * Displays the indefinite progress bar indicating a sync is in progress. This |
| 1076 | * should only be called if user manually requested a sync, and not for background syncs. |
Alice Yang | 0d74a66 | 2013-03-25 14:01:24 -0700 | [diff] [blame] | 1077 | */ |
| 1078 | protected void showSyncStatusBar() { |
Mindy Pereira | 4bb435c | 2013-11-13 14:21:15 -0800 | [diff] [blame] | 1079 | mSwipeRefreshWidget.setRefreshing(true); |
Alice Yang | 0d74a66 | 2013-03-25 14:01:24 -0700 | [diff] [blame] | 1080 | } |
Rohan Shah | d4f2287 | 2013-04-19 17:06:37 -0700 | [diff] [blame] | 1081 | |
| 1082 | /** |
| 1083 | * Clears all items in the list. |
| 1084 | */ |
| 1085 | public void clear() { |
| 1086 | mListView.setAdapter(null); |
| 1087 | } |
Scott Kennedy | 1fea6a3 | 2013-07-09 15:58:51 -0700 | [diff] [blame] | 1088 | |
| 1089 | private final ConversationSetObserver mConversationSetObserver = new ConversationSetObserver() { |
| 1090 | @Override |
| 1091 | public void onSetPopulated(final ConversationSelectionSet set) { |
Mindy Pereira | 4bb435c | 2013-11-13 14:21:15 -0800 | [diff] [blame] | 1092 | // Disable the swipe to refresh widget. |
| 1093 | mSwipeRefreshWidget.setEnabled(false); |
Scott Kennedy | 1fea6a3 | 2013-07-09 15:58:51 -0700 | [diff] [blame] | 1094 | } |
| 1095 | |
| 1096 | @Override |
| 1097 | public void onSetEmpty() { |
| 1098 | mSelectionModeExitedTimestamp = System.currentTimeMillis(); |
Mindy Pereira | 4bb435c | 2013-11-13 14:21:15 -0800 | [diff] [blame] | 1099 | mSwipeRefreshWidget.setEnabled(true); |
Scott Kennedy | 1fea6a3 | 2013-07-09 15:58:51 -0700 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | @Override |
| 1103 | public void onSetChanged(final ConversationSelectionSet set) { |
Scott Kennedy | 8afccad | 2013-07-28 19:09:11 -0700 | [diff] [blame] | 1104 | // Do nothing |
Scott Kennedy | 1fea6a3 | 2013-07-09 15:58:51 -0700 | [diff] [blame] | 1105 | } |
| 1106 | }; |
Scott Kennedy | f77806e | 2013-08-30 11:38:15 -0700 | [diff] [blame] | 1107 | |
| 1108 | private void saveLastScrolledPosition() { |
| 1109 | if (mListAdapter.getCursor() == null) { |
| 1110 | // If you save your scroll position in an empty list, you're gonna have a bad time |
| 1111 | return; |
| 1112 | } |
| 1113 | |
| 1114 | final Parcelable savedState = mListView.onSaveInstanceState(); |
| 1115 | |
| 1116 | mActivity.getListHandler().setConversationListScrollPosition( |
| 1117 | mFolder.conversationListUri.toString(), savedState); |
| 1118 | } |
| 1119 | |
| 1120 | private void restoreLastScrolledPosition() { |
| 1121 | // Scroll to our previous position, if necessary |
Scott Kennedy | 599c3cf | 2013-09-25 15:17:19 -0700 | [diff] [blame] | 1122 | if (!mScrollPositionRestored && mFolder != null) { |
| 1123 | final String key = mFolder.conversationListUri.toString(); |
Scott Kennedy | f77806e | 2013-08-30 11:38:15 -0700 | [diff] [blame] | 1124 | final Parcelable savedState = mActivity.getListHandler() |
Scott Kennedy | 599c3cf | 2013-09-25 15:17:19 -0700 | [diff] [blame] | 1125 | .getConversationListScrollPosition(key); |
Scott Kennedy | f77806e | 2013-08-30 11:38:15 -0700 | [diff] [blame] | 1126 | if (savedState != null) { |
| 1127 | mListView.onRestoreInstanceState(savedState); |
| 1128 | } |
| 1129 | mScrollPositionRestored = true; |
| 1130 | } |
| 1131 | } |
Mindy Pereira | 4bb435c | 2013-11-13 14:21:15 -0800 | [diff] [blame] | 1132 | |
| 1133 | /* (non-Javadoc) |
| 1134 | * @see android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener#onRefresh() |
| 1135 | */ |
| 1136 | @Override |
| 1137 | public void onRefresh() { |
| 1138 | Analytics.getInstance().sendEvent(Analytics.EVENT_CATEGORY_MENU_ITEM, "swipe_refresh", null, |
| 1139 | 0); |
| 1140 | |
| 1141 | // This will call back to showSyncStatusBar(): |
| 1142 | mActivity.getFolderController().requestFolderRefresh(); |
| 1143 | } |
Jin Cao | 343cf45 | 2014-04-18 14:05:25 -0700 | [diff] [blame] | 1144 | |
| 1145 | /** |
Jin Cao | 8bf9c21 | 2014-04-25 15:00:53 -0700 | [diff] [blame] | 1146 | * Extracted function that handles Analytics state and logging updates for each new cursor |
| 1147 | * @param newCursor the new cursor pointer |
Jin Cao | 343cf45 | 2014-04-18 14:05:25 -0700 | [diff] [blame] | 1148 | */ |
| 1149 | private void updateAnalyticsData(ConversationCursor newCursor) { |
Jin Cao | 8bf9c21 | 2014-04-25 15:00:53 -0700 | [diff] [blame] | 1150 | if (newCursor != null) { |
| 1151 | // Check if the initial data returned yet |
| 1152 | if (mInitialCursorLoading) { |
| 1153 | // This marks the very first time the cursor with the data the user sees returned. |
| 1154 | // We either have a cursor in LOADING state with cursor's count > 0, OR the cursor |
| 1155 | // completed loading. |
| 1156 | // Use this point to log the appropriate timing information that depends on when |
| 1157 | // the conversation list view finishes loading |
Jin Cao | fa93427 | 2014-05-01 13:38:21 -0700 | [diff] [blame^] | 1158 | if (isCursorReadyToShow(newCursor)) { |
Jin Cao | 8bf9c21 | 2014-04-25 15:00:53 -0700 | [diff] [blame] | 1159 | if (newCursor.getCount() == 0) { |
| 1160 | Analytics.getInstance().sendEvent("empty_state", "post_label_change", |
| 1161 | mFolder.getTypeDescription(), 0); |
| 1162 | } |
| 1163 | AnalyticsTimer.getInstance().logDuration(AnalyticsTimer.COLD_START_LAUNCHER, |
| 1164 | true /* isDestructive */, "cold_start_to_list", "from_launcher", null); |
| 1165 | // Don't need null checks because the activity, controller, and folder cannot |
| 1166 | // be null in this case |
| 1167 | if (mActivity.getFolderController().getFolder().isSearch()) { |
| 1168 | AnalyticsTimer.getInstance().logDuration(AnalyticsTimer.SEARCH_TO_LIST, |
| 1169 | true /* isDestructive */, "search_to_list", null, null); |
| 1170 | } |
Jin Cao | 779dd60 | 2014-04-22 16:16:28 -0700 | [diff] [blame] | 1171 | |
Jin Cao | 8bf9c21 | 2014-04-25 15:00:53 -0700 | [diff] [blame] | 1172 | mInitialCursorLoading = false; |
| 1173 | } |
| 1174 | } else { |
| 1175 | // Log the appropriate events that happen after the initial cursor is loaded |
| 1176 | if (newCursor.getCount() == 0 && mConversationCursorLastCount > 0) { |
| 1177 | Analytics.getInstance().sendEvent("empty_state", "post_delete", |
| 1178 | mFolder.getTypeDescription(), 0); |
| 1179 | } |
Jin Cao | 779dd60 | 2014-04-22 16:16:28 -0700 | [diff] [blame] | 1180 | } |
Jin Cao | faa9198 | 2014-04-24 16:52:26 -0700 | [diff] [blame] | 1181 | |
Jin Cao | 8bf9c21 | 2014-04-25 15:00:53 -0700 | [diff] [blame] | 1182 | // We save the count here because for folders that are empty, multiple successful |
| 1183 | // cursor loads will occur with size of 0. Thus we don't want to emit any false |
| 1184 | // positive post_delete events. |
| 1185 | mConversationCursorLastCount = newCursor.getCount(); |
| 1186 | } else { |
| 1187 | mConversationCursorLastCount = 0; |
| 1188 | } |
Jin Cao | 343cf45 | 2014-04-18 14:05:25 -0700 | [diff] [blame] | 1189 | } |
Jin Cao | fa93427 | 2014-05-01 13:38:21 -0700 | [diff] [blame^] | 1190 | |
| 1191 | /** |
| 1192 | * Helper function to determine if the given cursor is ready to populate the UI |
| 1193 | * @param cursor |
| 1194 | * @return |
| 1195 | */ |
| 1196 | private boolean isCursorReadyToShow(ConversationCursor cursor) { |
| 1197 | if (cursor == null) { |
| 1198 | return false; |
| 1199 | } |
| 1200 | final int status = cursor.getExtras().getInt( |
| 1201 | UIProvider.CursorExtraKeys.EXTRA_STATUS); |
| 1202 | return (cursor.getCount() > 0 || !UIProvider.CursorStatus.isWaitingForResults(status)); |
| 1203 | } |
Vikram Aggarwal | b9e1a35 | 2012-01-24 15:23:38 -0800 | [diff] [blame] | 1204 | } |