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