mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [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 | |
| 20 | import android.app.Activity; |
| 21 | import android.app.Fragment; |
| 22 | import android.app.LoaderManager; |
| 23 | import android.content.Context; |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 24 | import android.content.Loader; |
| 25 | import android.database.Cursor; |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 26 | import android.net.Uri; |
| 27 | import android.os.Bundle; |
mindyp | ff282d0 | 2012-09-17 10:33:02 -0700 | [diff] [blame] | 28 | import android.os.Handler; |
James Lemieux | 3f6111c | 2014-09-16 14:34:54 -0700 | [diff] [blame] | 29 | import android.support.annotation.Nullable; |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 30 | import android.view.Menu; |
| 31 | import android.view.MenuInflater; |
| 32 | import android.view.MenuItem; |
| 33 | |
Tony Mantler | 821e578 | 2014-01-06 15:33:43 -0800 | [diff] [blame] | 34 | import com.android.emailcommon.mail.Address; |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 35 | import com.android.mail.R; |
Andy Huang | 761522c | 2013-08-08 13:09:11 -0700 | [diff] [blame] | 36 | import com.android.mail.analytics.Analytics; |
Andy Huang | 8f18778 | 2012-11-06 17:49:25 -0800 | [diff] [blame] | 37 | import com.android.mail.browse.ConversationAccountController; |
Andrew Sapperstein | 8812d3c | 2013-06-04 17:06:41 -0700 | [diff] [blame] | 38 | import com.android.mail.browse.ConversationMessage; |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 39 | import com.android.mail.browse.ConversationViewHeader.ConversationViewHeaderCallbacks; |
Andy Huang | 9d3fd92 | 2012-09-26 22:23:58 -0700 | [diff] [blame] | 40 | import com.android.mail.browse.MessageCursor; |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 41 | import com.android.mail.browse.MessageCursor.ConversationController; |
Paul Westbrook | c42ad5e | 2013-05-09 16:52:15 -0700 | [diff] [blame] | 42 | import com.android.mail.content.ObjectCursor; |
| 43 | import com.android.mail.content.ObjectCursorLoader; |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 44 | import com.android.mail.providers.Account; |
| 45 | import com.android.mail.providers.AccountObserver; |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 46 | import com.android.mail.providers.Conversation; |
Andy Huang | e6c9fb6 | 2013-11-15 09:56:20 -0800 | [diff] [blame] | 47 | import com.android.mail.providers.Folder; |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 48 | import com.android.mail.providers.ListParams; |
Andrew Sapperstein | 8ec43e8 | 2013-12-17 18:27:55 -0800 | [diff] [blame] | 49 | import com.android.mail.providers.Settings; |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 50 | import com.android.mail.providers.UIProvider; |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame] | 51 | import com.android.mail.providers.UIProvider.CursorStatus; |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 52 | import com.android.mail.utils.LogTag; |
| 53 | import com.android.mail.utils.LogUtils; |
| 54 | import com.android.mail.utils.Utils; |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 55 | |
Paul Westbrook | 4d8cad5 | 2012-09-21 14:13:49 -0700 | [diff] [blame] | 56 | import java.util.Arrays; |
Andy Huang | 543e709 | 2013-04-22 11:44:56 -0700 | [diff] [blame] | 57 | import java.util.Collections; |
| 58 | import java.util.HashMap; |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 59 | import java.util.Map; |
Andrew Sapperstein | 376294b | 2013-06-06 16:04:26 -0700 | [diff] [blame] | 60 | |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 61 | public abstract class AbstractConversationViewFragment extends Fragment implements |
Andrew Sapperstein | 4ddda2f | 2013-06-10 11:15:38 -0700 | [diff] [blame] | 62 | ConversationController, ConversationAccountController, |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 63 | ConversationViewHeaderCallbacks { |
| 64 | |
Andrew Sapperstein | 606dbd7 | 2013-07-30 19:14:23 -0700 | [diff] [blame] | 65 | protected static final String ARG_ACCOUNT = "account"; |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 66 | public static final String ARG_CONVERSATION = "conversation"; |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 67 | private static final String LOG_TAG = LogTag.getLogTag(); |
| 68 | protected static final int MESSAGE_LOADER = 0; |
| 69 | protected static final int CONTACT_LOADER = 1; |
Andy Huang | 4f347e8 | 2014-02-25 17:32:28 -0800 | [diff] [blame] | 70 | public static final int ATTACHMENT_OPTION1_LOADER = 2; |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 71 | protected ControllableActivity mActivity; |
| 72 | private final MessageLoaderCallbacks mMessageLoaderCallbacks = new MessageLoaderCallbacks(); |
Andrew Sapperstein | 376294b | 2013-06-06 16:04:26 -0700 | [diff] [blame] | 73 | private ContactLoaderCallbacks mContactLoaderCallbacks; |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 74 | private MenuItem mChangeFoldersMenuItem; |
| 75 | protected Conversation mConversation; |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 76 | protected String mBaseUri; |
| 77 | protected Account mAccount; |
Andrew Sapperstein | 376294b | 2013-06-06 16:04:26 -0700 | [diff] [blame] | 78 | |
| 79 | /** |
| 80 | * Must be instantiated in a derived class's onCreate. |
| 81 | */ |
| 82 | protected AbstractConversationWebViewClient mWebViewClient; |
Andrew Sapperstein | 4ddda2f | 2013-06-10 11:15:38 -0700 | [diff] [blame] | 83 | |
Andy Huang | 543e709 | 2013-04-22 11:44:56 -0700 | [diff] [blame] | 84 | /** |
| 85 | * Cache of email address strings to parsed Address objects. |
| 86 | * <p> |
| 87 | * Remember to synchronize on the map when reading or writing to this cache, because some |
| 88 | * instances use it off the UI thread (e.g. from WebView). |
| 89 | */ |
| 90 | protected final Map<String, Address> mAddressCache = Collections.synchronizedMap( |
| 91 | new HashMap<String, Address>()); |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 92 | private MessageCursor mCursor; |
| 93 | private Context mContext; |
Andy Huang | 9d3fd92 | 2012-09-26 22:23:58 -0700 | [diff] [blame] | 94 | /** |
| 95 | * A backwards-compatible version of {{@link #getUserVisibleHint()}. Like the framework flag, |
| 96 | * this flag is saved and restored. |
| 97 | */ |
| 98 | private boolean mUserVisible; |
Andrew Sapperstein | 376294b | 2013-06-06 16:04:26 -0700 | [diff] [blame] | 99 | |
mindyp | ff282d0 | 2012-09-17 10:33:02 -0700 | [diff] [blame] | 100 | private final Handler mHandler = new Handler(); |
Vikram Aggarwal | d82a31f | 2013-02-05 15:03:00 -0800 | [diff] [blame] | 101 | /** True if we want to avoid marking the conversation as viewed and read. */ |
| 102 | private boolean mSuppressMarkingViewed; |
Paul Westbrook | 4d8cad5 | 2012-09-21 14:13:49 -0700 | [diff] [blame] | 103 | /** |
| 104 | * Parcelable state of the conversation view. Can safely be used without null checking any time |
Andy Huang | 9d3fd92 | 2012-09-26 22:23:58 -0700 | [diff] [blame] | 105 | * after {@link #onCreate(Bundle)}. |
Paul Westbrook | 4d8cad5 | 2012-09-21 14:13:49 -0700 | [diff] [blame] | 106 | */ |
| 107 | protected ConversationViewState mViewState; |
| 108 | |
Scott Kennedy | 1817678 | 2013-02-20 18:30:21 -0800 | [diff] [blame] | 109 | private boolean mIsDetached; |
| 110 | |
Andrew Sapperstein | 2fc6730 | 2013-04-29 18:24:56 -0700 | [diff] [blame] | 111 | private boolean mHasConversationBeenTransformed; |
| 112 | private boolean mHasConversationTransformBeenReverted; |
| 113 | |
Andy Huang | e6c9fb6 | 2013-11-15 09:56:20 -0800 | [diff] [blame] | 114 | protected boolean mConversationSeen = false; |
| 115 | |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 116 | private final AccountObserver mAccountObserver = new AccountObserver() { |
| 117 | @Override |
| 118 | public void onChanged(Account newAccount) { |
Andy Huang | adbf3e8 | 2012-10-13 13:30:19 -0700 | [diff] [blame] | 119 | final Account oldAccount = mAccount; |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 120 | mAccount = newAccount; |
Andrew Sapperstein | 376294b | 2013-06-06 16:04:26 -0700 | [diff] [blame] | 121 | mWebViewClient.setAccount(mAccount); |
Andy Huang | adbf3e8 | 2012-10-13 13:30:19 -0700 | [diff] [blame] | 122 | onAccountChanged(newAccount, oldAccount); |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 123 | } |
| 124 | }; |
| 125 | |
Andy Huang | 9d3fd92 | 2012-09-26 22:23:58 -0700 | [diff] [blame] | 126 | private static final String BUNDLE_VIEW_STATE = |
| 127 | AbstractConversationViewFragment.class.getName() + "viewstate"; |
Andy Huang | 9a8bc1e | 2012-10-23 19:48:25 -0700 | [diff] [blame] | 128 | /** |
| 129 | * We save the user visible flag so the various transitions that occur during rotation do not |
| 130 | * cause unnecessary visibility change. |
| 131 | */ |
Andy Huang | 9d3fd92 | 2012-09-26 22:23:58 -0700 | [diff] [blame] | 132 | private static final String BUNDLE_USER_VISIBLE = |
| 133 | AbstractConversationViewFragment.class.getName() + "uservisible"; |
| 134 | |
Scott Kennedy | 1817678 | 2013-02-20 18:30:21 -0800 | [diff] [blame] | 135 | private static final String BUNDLE_DETACHED = |
| 136 | AbstractConversationViewFragment.class.getName() + "detached"; |
| 137 | |
Andrew Sapperstein | 2fc6730 | 2013-04-29 18:24:56 -0700 | [diff] [blame] | 138 | private static final String BUNDLE_KEY_HAS_CONVERSATION_BEEN_TRANSFORMED = |
| 139 | AbstractConversationViewFragment.class.getName() + "conversationtransformed"; |
| 140 | private static final String BUNDLE_KEY_HAS_CONVERSATION_BEEN_REVERTED = |
| 141 | AbstractConversationViewFragment.class.getName() + "conversationreverted"; |
| 142 | |
Andrew Sapperstein | 606dbd7 | 2013-07-30 19:14:23 -0700 | [diff] [blame] | 143 | public static Bundle makeBasicArgs(Account account) { |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 144 | Bundle args = new Bundle(); |
| 145 | args.putParcelable(ARG_ACCOUNT, account); |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 146 | return args; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Constructor needs to be public to handle orientation changes and activity |
| 151 | * lifecycle events. |
| 152 | */ |
| 153 | public AbstractConversationViewFragment() { |
| 154 | super(); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Subclasses must override, since this depends on how many messages are |
| 159 | * shown in the conversation view. |
| 160 | */ |
Vikram Aggarwal | d82a31f | 2013-02-05 15:03:00 -0800 | [diff] [blame] | 161 | protected void markUnread() { |
| 162 | // Do not automatically mark this conversation viewed and read. |
| 163 | mSuppressMarkingViewed = true; |
| 164 | } |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 165 | |
| 166 | /** |
| 167 | * Subclasses must override this, since they may want to display a single or |
| 168 | * many messages related to this conversation. |
| 169 | */ |
Paul Westbrook | c42ad5e | 2013-05-09 16:52:15 -0700 | [diff] [blame] | 170 | protected abstract void onMessageCursorLoadFinished( |
| 171 | Loader<ObjectCursor<ConversationMessage>> loader, |
Andy Huang | 014ea4c | 2012-09-25 14:50:54 -0700 | [diff] [blame] | 172 | MessageCursor newCursor, MessageCursor oldCursor); |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 173 | |
| 174 | /** |
| 175 | * Subclasses must override this, since they may want to display a single or |
| 176 | * many messages related to this conversation. |
| 177 | */ |
| 178 | @Override |
| 179 | public abstract void onConversationViewHeaderHeightChange(int newHeight); |
| 180 | |
| 181 | public abstract void onUserVisibleHintChanged(); |
| 182 | |
| 183 | /** |
| 184 | * Subclasses must override this. |
| 185 | */ |
Andy Huang | adbf3e8 | 2012-10-13 13:30:19 -0700 | [diff] [blame] | 186 | protected abstract void onAccountChanged(Account newAccount, Account oldAccount); |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 187 | |
| 188 | @Override |
| 189 | public void onCreate(Bundle savedState) { |
| 190 | super.onCreate(savedState); |
| 191 | |
Andrew Sapperstein | 606dbd7 | 2013-07-30 19:14:23 -0700 | [diff] [blame] | 192 | parseArguments(); |
| 193 | setBaseUri(); |
Paul Westbrook | ba4cce6 | 2012-09-28 10:24:20 -0700 | [diff] [blame] | 194 | |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 195 | LogUtils.d(LOG_TAG, "onCreate in ConversationViewFragment (this=%s)", this); |
| 196 | // Not really, we just want to get a crack to store a reference to the change_folder item |
| 197 | setHasOptionsMenu(true); |
Andy Huang | 9d3fd92 | 2012-09-26 22:23:58 -0700 | [diff] [blame] | 198 | |
| 199 | if (savedState != null) { |
| 200 | mViewState = savedState.getParcelable(BUNDLE_VIEW_STATE); |
| 201 | mUserVisible = savedState.getBoolean(BUNDLE_USER_VISIBLE); |
Scott Kennedy | 1817678 | 2013-02-20 18:30:21 -0800 | [diff] [blame] | 202 | mIsDetached = savedState.getBoolean(BUNDLE_DETACHED, false); |
Andrew Sapperstein | 2fc6730 | 2013-04-29 18:24:56 -0700 | [diff] [blame] | 203 | mHasConversationBeenTransformed = |
| 204 | savedState.getBoolean(BUNDLE_KEY_HAS_CONVERSATION_BEEN_TRANSFORMED, false); |
| 205 | mHasConversationTransformBeenReverted = |
| 206 | savedState.getBoolean(BUNDLE_KEY_HAS_CONVERSATION_BEEN_REVERTED, false); |
Andy Huang | 9d3fd92 | 2012-09-26 22:23:58 -0700 | [diff] [blame] | 207 | } else { |
| 208 | mViewState = getNewViewState(); |
Andrew Sapperstein | 2fc6730 | 2013-04-29 18:24:56 -0700 | [diff] [blame] | 209 | mHasConversationBeenTransformed = false; |
| 210 | mHasConversationTransformBeenReverted = false; |
Andy Huang | 9d3fd92 | 2012-09-26 22:23:58 -0700 | [diff] [blame] | 211 | } |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Andrew Sapperstein | 606dbd7 | 2013-07-30 19:14:23 -0700 | [diff] [blame] | 214 | /** |
| 215 | * Can be overridden in case a subclass needs to get additional arguments. |
| 216 | */ |
| 217 | protected void parseArguments() { |
| 218 | final Bundle args = getArguments(); |
| 219 | mAccount = args.getParcelable(ARG_ACCOUNT); |
| 220 | mConversation = args.getParcelable(ARG_CONVERSATION); |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Can be overridden in case a subclass needs a different uri format |
| 225 | * (such as one that does not rely on account and/or conversation. |
| 226 | */ |
| 227 | protected void setBaseUri() { |
Ray Chen | ee04e66 | 2014-07-23 10:13:27 +0200 | [diff] [blame] | 228 | mBaseUri = buildBaseUri(getContext(), mAccount, mConversation); |
Andrew Sapperstein | 562c5ba | 2013-10-09 18:31:50 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Ray Chen | 4b0c012 | 2014-07-11 15:24:54 +0200 | [diff] [blame] | 231 | public static String buildBaseUri(Context context, Account account, Conversation conversation) { |
Andrew Sapperstein | 606dbd7 | 2013-07-30 19:14:23 -0700 | [diff] [blame] | 232 | // Since the uri specified in the conversation base uri may not be unique, we specify a |
| 233 | // base uri that us guaranteed to be unique for this conversation. |
Paul Westbrook | fd79237 | 2014-07-30 02:28:16 +0000 | [diff] [blame] | 234 | return "x-thread://" + account.getAccountId().hashCode() + "/" + conversation.id; |
Andrew Sapperstein | 606dbd7 | 2013-07-30 19:14:23 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Andy Huang | 9e4ca79 | 2013-02-28 14:33:43 -0800 | [diff] [blame] | 237 | @Override |
| 238 | public String toString() { |
| 239 | // log extra info at DEBUG level or finer |
| 240 | final String s = super.toString(); |
| 241 | if (!LogUtils.isLoggable(LOG_TAG, LogUtils.DEBUG) || mConversation == null) { |
| 242 | return s; |
| 243 | } |
| 244 | return "(" + s + " conv=" + mConversation + ")"; |
| 245 | } |
| 246 | |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 247 | @Override |
| 248 | public void onActivityCreated(Bundle savedInstanceState) { |
| 249 | super.onActivityCreated(savedInstanceState); |
Vikram Aggarwal | 8fe8ed4 | 2012-09-18 11:40:08 -0700 | [diff] [blame] | 250 | final Activity activity = getActivity(); |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 251 | if (!(activity instanceof ControllableActivity)) { |
| 252 | LogUtils.wtf(LOG_TAG, "ConversationViewFragment expects only a ControllableActivity to" |
| 253 | + "create it. Cannot proceed."); |
| 254 | } |
Vikram Aggarwal | 8fe8ed4 | 2012-09-18 11:40:08 -0700 | [diff] [blame] | 255 | if (activity == null || activity.isFinishing()) { |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 256 | // Activity is finishing, just bail. |
| 257 | return; |
| 258 | } |
Vikram Aggarwal | 8fe8ed4 | 2012-09-18 11:40:08 -0700 | [diff] [blame] | 259 | mActivity = (ControllableActivity) activity; |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 260 | mContext = activity.getApplicationContext(); |
Andy Huang | b622d2b | 2013-06-12 13:47:17 -0700 | [diff] [blame] | 261 | mWebViewClient.setActivity(activity); |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 262 | mAccount = mAccountObserver.initialize(mActivity.getAccountController()); |
Andrew Sapperstein | 376294b | 2013-06-06 16:04:26 -0700 | [diff] [blame] | 263 | mWebViewClient.setAccount(mAccount); |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | @Override |
| 267 | public ConversationUpdater getListController() { |
| 268 | final ControllableActivity activity = (ControllableActivity) getActivity(); |
| 269 | return activity != null ? activity.getConversationUpdater() : null; |
| 270 | } |
| 271 | |
| 272 | public Context getContext() { |
| 273 | return mContext; |
| 274 | } |
| 275 | |
Andy Huang | 02133aa | 2012-11-08 19:50:57 -0800 | [diff] [blame] | 276 | @Override |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 277 | public Conversation getConversation() { |
| 278 | return mConversation; |
| 279 | } |
| 280 | |
| 281 | @Override |
James Lemieux | 3f6111c | 2014-09-16 14:34:54 -0700 | [diff] [blame] | 282 | public @Nullable MessageCursor getMessageCursor() { |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 283 | return mCursor; |
| 284 | } |
| 285 | |
mindyp | ff282d0 | 2012-09-17 10:33:02 -0700 | [diff] [blame] | 286 | public Handler getHandler() { |
| 287 | return mHandler; |
| 288 | } |
| 289 | |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 290 | public MessageLoaderCallbacks getMessageLoaderCallbacks() { |
| 291 | return mMessageLoaderCallbacks; |
| 292 | } |
| 293 | |
| 294 | public ContactLoaderCallbacks getContactInfoSource() { |
Andrew Sapperstein | 376294b | 2013-06-06 16:04:26 -0700 | [diff] [blame] | 295 | if (mContactLoaderCallbacks == null) { |
Andrew Sapperstein | 8913ca6 | 2014-05-14 15:03:40 -0700 | [diff] [blame] | 296 | mContactLoaderCallbacks = mActivity.getContactLoaderCallbacks(); |
Andrew Sapperstein | 376294b | 2013-06-06 16:04:26 -0700 | [diff] [blame] | 297 | } |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 298 | return mContactLoaderCallbacks; |
| 299 | } |
| 300 | |
| 301 | @Override |
| 302 | public Account getAccount() { |
| 303 | return mAccount; |
| 304 | } |
| 305 | |
| 306 | @Override |
| 307 | public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { |
| 308 | super.onCreateOptionsMenu(menu, inflater); |
Andrew Sapperstein | 6c570db | 2013-08-06 17:21:36 -0700 | [diff] [blame] | 309 | mChangeFoldersMenuItem = menu.findItem(R.id.change_folders); |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | @Override |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 313 | public boolean onOptionsItemSelected(MenuItem item) { |
Andy Huang | bb9dd6b | 2013-02-28 17:13:54 -0800 | [diff] [blame] | 314 | if (!isUserVisible()) { |
| 315 | // Unclear how this is happening. Current theory is that this fragment was scheduled |
| 316 | // to be removed, but the remove transaction failed. When the Activity is later |
| 317 | // restored, the FragmentManager restores this fragment, but Fragment.mMenuVisible is |
| 318 | // stuck at its initial value (true), which makes this zombie fragment eligible for |
| 319 | // menu item clicks. |
| 320 | // |
| 321 | // Work around this by relying on the (properly restored) extra user visible hint. |
| 322 | LogUtils.e(LOG_TAG, |
| 323 | "ACVF ignoring onOptionsItemSelected b/c userVisibleHint is false. f=%s", this); |
| 324 | if (LogUtils.isLoggable(LOG_TAG, LogUtils.DEBUG)) { |
Tony Mantler | 54a9057 | 2014-03-03 16:23:08 -0800 | [diff] [blame] | 325 | LogUtils.e(LOG_TAG, "%s", Utils.dumpFragment(this)); |
Andy Huang | bb9dd6b | 2013-02-28 17:13:54 -0800 | [diff] [blame] | 326 | } |
| 327 | return false; |
| 328 | } |
| 329 | |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 330 | boolean handled = false; |
Scott Kennedy | 2b9d80e | 2013-07-30 23:03:45 -0700 | [diff] [blame] | 331 | final int itemId = item.getItemId(); |
| 332 | if (itemId == R.id.inside_conversation_unread) { |
| 333 | markUnread(); |
| 334 | handled = true; |
| 335 | } else if (itemId == R.id.show_original) { |
| 336 | showUntransformedConversation(); |
| 337 | handled = true; |
Andrew Sapperstein | 6293ef0 | 2013-10-07 18:22:10 -0700 | [diff] [blame] | 338 | } else if (itemId == R.id.print_all) { |
Andrew Sapperstein | 5c1692a | 2013-09-16 11:56:13 -0700 | [diff] [blame] | 339 | printConversation(); |
| 340 | handled = true; |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 341 | } |
| 342 | return handled; |
| 343 | } |
| 344 | |
Andrew Sapperstein | 2fc6730 | 2013-04-29 18:24:56 -0700 | [diff] [blame] | 345 | @Override |
| 346 | public void onPrepareOptionsMenu(Menu menu) { |
| 347 | // Only show option if we support message transforms and message has been transformed. |
| 348 | Utils.setMenuItemVisibility(menu, R.id.show_original, supportsMessageTransforms() && |
| 349 | mHasConversationBeenTransformed && !mHasConversationTransformBeenReverted); |
James Lemieux | 7cad280 | 2014-01-09 15:00:53 -0800 | [diff] [blame] | 350 | |
| 351 | final MenuItem printMenuItem = menu.findItem(R.id.print_all); |
| 352 | if (printMenuItem != null) { |
| 353 | // compute the visibility of the print menu item |
| 354 | printMenuItem.setVisible(Utils.isRunningKitkatOrLater() && shouldShowPrintInOverflow()); |
| 355 | |
| 356 | // compute the text displayed on the print menu item |
| 357 | if (mConversation.getNumMessages() == 1) { |
| 358 | printMenuItem.setTitle(R.string.print); |
| 359 | } else { |
| 360 | printMenuItem.setTitle(R.string.print_all); |
| 361 | } |
| 362 | } |
Andrew Sapperstein | 2fc6730 | 2013-04-29 18:24:56 -0700 | [diff] [blame] | 363 | } |
| 364 | |
Andrew Sapperstein | 4ddda2f | 2013-06-10 11:15:38 -0700 | [diff] [blame] | 365 | abstract boolean supportsMessageTransforms(); |
| 366 | |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 367 | // BEGIN conversation header callbacks |
| 368 | @Override |
| 369 | public void onFoldersClicked() { |
| 370 | if (mChangeFoldersMenuItem == null) { |
| 371 | LogUtils.e(LOG_TAG, "unable to open 'change folders' dialog for a conversation"); |
| 372 | return; |
| 373 | } |
| 374 | mActivity.onOptionsItemSelected(mChangeFoldersMenuItem); |
| 375 | } |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 376 | // END conversation header callbacks |
| 377 | |
| 378 | @Override |
Andy Huang | 761522c | 2013-08-08 13:09:11 -0700 | [diff] [blame] | 379 | public void onStart() { |
| 380 | super.onStart(); |
| 381 | |
| 382 | Analytics.getInstance().sendView(getClass().getName()); |
| 383 | } |
| 384 | |
| 385 | @Override |
Andy Huang | 9d3fd92 | 2012-09-26 22:23:58 -0700 | [diff] [blame] | 386 | public void onSaveInstanceState(Bundle outState) { |
| 387 | if (mViewState != null) { |
| 388 | outState.putParcelable(BUNDLE_VIEW_STATE, mViewState); |
| 389 | } |
| 390 | outState.putBoolean(BUNDLE_USER_VISIBLE, mUserVisible); |
Scott Kennedy | 1817678 | 2013-02-20 18:30:21 -0800 | [diff] [blame] | 391 | outState.putBoolean(BUNDLE_DETACHED, mIsDetached); |
Andrew Sapperstein | 2fc6730 | 2013-04-29 18:24:56 -0700 | [diff] [blame] | 392 | outState.putBoolean(BUNDLE_KEY_HAS_CONVERSATION_BEEN_TRANSFORMED, |
| 393 | mHasConversationBeenTransformed); |
| 394 | outState.putBoolean(BUNDLE_KEY_HAS_CONVERSATION_BEEN_REVERTED, |
| 395 | mHasConversationTransformBeenReverted); |
Andy Huang | 9d3fd92 | 2012-09-26 22:23:58 -0700 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | @Override |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 399 | public void onDestroyView() { |
| 400 | super.onDestroyView(); |
| 401 | mAccountObserver.unregisterAndDestroy(); |
| 402 | } |
| 403 | |
| 404 | /** |
| 405 | * {@link #setUserVisibleHint(boolean)} only works on API >= 15, so implement our own for |
| 406 | * reliability on older platforms. |
| 407 | */ |
| 408 | public void setExtraUserVisibleHint(boolean isVisibleToUser) { |
| 409 | LogUtils.v(LOG_TAG, "in CVF.setHint, val=%s (%s)", isVisibleToUser, this); |
| 410 | if (mUserVisible != isVisibleToUser) { |
| 411 | mUserVisible = isVisibleToUser; |
mindyp | bc4142f | 2012-09-19 09:29:49 -0700 | [diff] [blame] | 412 | MessageCursor cursor = getMessageCursor(); |
mindyp | 0b9b48c | 2012-09-19 10:00:51 -0700 | [diff] [blame] | 413 | if (mUserVisible && (cursor != null && cursor.isLoaded() && cursor.getCount() == 0)) { |
mindyp | bc4142f | 2012-09-19 09:29:49 -0700 | [diff] [blame] | 414 | // Pop back to conversation list and show error. |
| 415 | onError(); |
| 416 | return; |
| 417 | } |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 418 | onUserVisibleHintChanged(); |
| 419 | } |
| 420 | } |
| 421 | |
Andy Huang | 9d3fd92 | 2012-09-26 22:23:58 -0700 | [diff] [blame] | 422 | public boolean isUserVisible() { |
| 423 | return mUserVisible; |
| 424 | } |
| 425 | |
Andy Huang | 243c236 | 2013-03-01 17:50:35 -0800 | [diff] [blame] | 426 | protected void timerMark(String msg) { |
| 427 | if (isUserVisible()) { |
| 428 | Utils.sConvLoadTimer.mark(msg); |
| 429 | } |
| 430 | } |
| 431 | |
Paul Westbrook | c42ad5e | 2013-05-09 16:52:15 -0700 | [diff] [blame] | 432 | private class MessageLoaderCallbacks |
| 433 | implements LoaderManager.LoaderCallbacks<ObjectCursor<ConversationMessage>> { |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 434 | |
| 435 | @Override |
Paul Westbrook | c42ad5e | 2013-05-09 16:52:15 -0700 | [diff] [blame] | 436 | public Loader<ObjectCursor<ConversationMessage>> onCreateLoader(int id, Bundle args) { |
Andy Huang | 02133aa | 2012-11-08 19:50:57 -0800 | [diff] [blame] | 437 | return new MessageLoader(mActivity.getActivityContext(), mConversation.messageListUri); |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | @Override |
Paul Westbrook | c42ad5e | 2013-05-09 16:52:15 -0700 | [diff] [blame] | 441 | public void onLoadFinished(Loader<ObjectCursor<ConversationMessage>> loader, |
| 442 | ObjectCursor<ConversationMessage> data) { |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 443 | // ignore truly duplicate results |
| 444 | // this can happen when restoring after rotation |
| 445 | if (mCursor == data) { |
| 446 | return; |
| 447 | } else { |
Andy Huang | 6766b6e | 2012-09-28 12:43:52 -0700 | [diff] [blame] | 448 | final MessageCursor messageCursor = (MessageCursor) data; |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 449 | |
Andy Huang | 02133aa | 2012-11-08 19:50:57 -0800 | [diff] [blame] | 450 | // bind the cursor to this fragment so it can access to the current list controller |
| 451 | messageCursor.setController(AbstractConversationViewFragment.this); |
| 452 | |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 453 | if (LogUtils.isLoggable(LOG_TAG, LogUtils.DEBUG)) { |
| 454 | LogUtils.d(LOG_TAG, "LOADED CONVERSATION= %s", messageCursor.getDebugDump()); |
| 455 | } |
| 456 | |
Vikram Aggarwal | a91d00b | 2013-01-18 12:00:37 -0800 | [diff] [blame] | 457 | // We have no messages: exit conversation view. |
| 458 | if (messageCursor.getCount() == 0 |
Scott Kennedy | 1817678 | 2013-02-20 18:30:21 -0800 | [diff] [blame] | 459 | && (!CursorStatus.isWaitingForResults(messageCursor.getStatus()) |
| 460 | || mIsDetached)) { |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 461 | if (mUserVisible) { |
mindyp | bc4142f | 2012-09-19 09:29:49 -0700 | [diff] [blame] | 462 | onError(); |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 463 | } else { |
| 464 | // we expect that the pager adapter will remove this |
| 465 | // conversation fragment on its own due to a separate |
| 466 | // conversation cursor update (we might get here if the |
| 467 | // message list update fires first. nothing to do |
| 468 | // because we expect to be torn down soon.) |
| 469 | LogUtils.i(LOG_TAG, "CVF: offscreen conv has no messages, ignoring update" |
Paul Westbrook | c42ad5e | 2013-05-09 16:52:15 -0700 | [diff] [blame] | 470 | + " in anticipation of conv cursor update. c=%s", |
| 471 | mConversation.uri); |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 472 | } |
Andy Huang | 233d435 | 2012-10-18 14:00:24 -0700 | [diff] [blame] | 473 | // existing mCursor will imminently be closed, must stop referencing it |
| 474 | // since we expect to be kicked out soon, it doesn't matter what mCursor |
| 475 | // becomes |
| 476 | mCursor = null; |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 477 | return; |
| 478 | } |
| 479 | |
| 480 | // ignore cursors that are still loading results |
| 481 | if (!messageCursor.isLoaded()) { |
Andy Huang | 233d435 | 2012-10-18 14:00:24 -0700 | [diff] [blame] | 482 | // existing mCursor will imminently be closed, must stop referencing it |
| 483 | // in this case, the new cursor is also no good, and since don't expect to get |
| 484 | // here except in initial load situations, it's safest to just ensure the |
| 485 | // reference is null |
| 486 | mCursor = null; |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 487 | return; |
| 488 | } |
Andy Huang | 014ea4c | 2012-09-25 14:50:54 -0700 | [diff] [blame] | 489 | final MessageCursor oldCursor = mCursor; |
Andy Huang | 6766b6e | 2012-09-28 12:43:52 -0700 | [diff] [blame] | 490 | mCursor = messageCursor; |
Andy Huang | 014ea4c | 2012-09-25 14:50:54 -0700 | [diff] [blame] | 491 | onMessageCursorLoadFinished(loader, mCursor, oldCursor); |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | |
| 495 | @Override |
Paul Westbrook | c42ad5e | 2013-05-09 16:52:15 -0700 | [diff] [blame] | 496 | public void onLoaderReset(Loader<ObjectCursor<ConversationMessage>> loader) { |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 497 | mCursor = null; |
| 498 | } |
| 499 | |
| 500 | } |
| 501 | |
mindyp | bc4142f | 2012-09-19 09:29:49 -0700 | [diff] [blame] | 502 | private void onError() { |
| 503 | // need to exit this view- conversation may have been |
| 504 | // deleted, or for whatever reason is now invalid (e.g. |
| 505 | // discard single draft) |
| 506 | // |
| 507 | // N.B. this may involve a fragment transaction, which |
| 508 | // FragmentManager will refuse to execute directly |
| 509 | // within onLoadFinished. Make sure the controller knows. |
| 510 | LogUtils.i(LOG_TAG, "CVF: visible conv has no messages, exiting conv mode"); |
| 511 | // TODO(mindyp): handle ERROR status by showing an error |
| 512 | // message to the user that there are no messages in |
| 513 | // this conversation |
Scott Kennedy | 1817678 | 2013-02-20 18:30:21 -0800 | [diff] [blame] | 514 | popOut(); |
| 515 | } |
mindyp | bc4142f | 2012-09-19 09:29:49 -0700 | [diff] [blame] | 516 | |
Scott Kennedy | 1817678 | 2013-02-20 18:30:21 -0800 | [diff] [blame] | 517 | private void popOut() { |
Andrew Sapperstein | 376294b | 2013-06-06 16:04:26 -0700 | [diff] [blame] | 518 | mHandler.post(new FragmentRunnable("popOut", this) { |
mindyp | bc4142f | 2012-09-19 09:29:49 -0700 | [diff] [blame] | 519 | @Override |
Andy Huang | 9a8bc1e | 2012-10-23 19:48:25 -0700 | [diff] [blame] | 520 | public void go() { |
Alice Yang | 7729a9a | 2013-05-23 18:09:09 -0700 | [diff] [blame] | 521 | if (mActivity != null) { |
| 522 | mActivity.getListHandler() |
| 523 | .onConversationSelected(null, true /* inLoaderCallbacks */); |
| 524 | } |
mindyp | bc4142f | 2012-09-19 09:29:49 -0700 | [diff] [blame] | 525 | } |
mindyp | bc4142f | 2012-09-19 09:29:49 -0700 | [diff] [blame] | 526 | }); |
| 527 | } |
| 528 | |
Andy Huang | e6c9fb6 | 2013-11-15 09:56:20 -0800 | [diff] [blame] | 529 | /** |
| 530 | * @see Folder#getTypeDescription() |
| 531 | */ |
| 532 | protected String getCurrentFolderTypeDesc() { |
| 533 | final Folder currFolder; |
| 534 | if (mActivity != null) { |
| 535 | currFolder = mActivity.getFolderController().getFolder(); |
| 536 | } else { |
| 537 | currFolder = null; |
| 538 | } |
| 539 | final String folderStr; |
| 540 | if (currFolder != null) { |
| 541 | folderStr = currFolder.getTypeDescription(); |
| 542 | } else { |
| 543 | folderStr = "unknown_folder"; |
| 544 | } |
| 545 | return folderStr; |
| 546 | } |
| 547 | |
| 548 | private void logConversationView() { |
| 549 | final String folderStr = getCurrentFolderTypeDesc(); |
| 550 | Analytics.getInstance().sendEvent("view_conversation", folderStr, |
| 551 | mConversation.isRemote ? "unsynced" : "synced", mConversation.getNumMessages()); |
| 552 | } |
| 553 | |
Paul Westbrook | 4d8cad5 | 2012-09-21 14:13:49 -0700 | [diff] [blame] | 554 | protected void onConversationSeen() { |
Scott Kennedy | 919d01a | 2013-05-07 16:13:29 -0700 | [diff] [blame] | 555 | LogUtils.d(LOG_TAG, "AbstractConversationViewFragment#onConversationSeen()"); |
| 556 | |
Paul Westbrook | 4d8cad5 | 2012-09-21 14:13:49 -0700 | [diff] [blame] | 557 | // Ignore unsafe calls made after a fragment is detached from an activity |
| 558 | final ControllableActivity activity = (ControllableActivity) getActivity(); |
| 559 | if (activity == null) { |
| 560 | LogUtils.w(LOG_TAG, "ignoring onConversationSeen for conv=%s", mConversation.id); |
| 561 | return; |
| 562 | } |
| 563 | |
Andy Huang | e6c9fb6 | 2013-11-15 09:56:20 -0800 | [diff] [blame] | 564 | // this method is called 2x on rotation; debounce this a bit so as not to |
| 565 | // dramatically skew analytics data too much. Ideally, it should be called zero times |
| 566 | // on rotation... |
| 567 | if (!mConversationSeen) { |
| 568 | logConversationView(); |
| 569 | } |
| 570 | |
Paul Westbrook | 4d8cad5 | 2012-09-21 14:13:49 -0700 | [diff] [blame] | 571 | mViewState.setInfoForConversation(mConversation); |
| 572 | |
Scott Kennedy | 919d01a | 2013-05-07 16:13:29 -0700 | [diff] [blame] | 573 | LogUtils.d(LOG_TAG, "onConversationSeen() - mSuppressMarkingViewed = %b", |
| 574 | mSuppressMarkingViewed); |
Vikram Aggarwal | d82a31f | 2013-02-05 15:03:00 -0800 | [diff] [blame] | 575 | // In most circumstances we want to mark the conversation as viewed and read, since the |
| 576 | // user has read it. However, if the user has already marked the conversation unread, we |
| 577 | // do not want a later mark-read operation to undo this. So we check this variable which |
| 578 | // is set in #markUnread() which suppresses automatic mark-read. |
| 579 | if (!mSuppressMarkingViewed) { |
| 580 | // mark viewed/read if not previously marked viewed by this conversation view, |
| 581 | // or if unread messages still exist in the message list cursor |
| 582 | // we don't want to keep marking viewed on rotation or restore |
| 583 | // but we do want future re-renders to mark read (e.g. "New message from X" case) |
Paul Westbrook | 2145229 | 2013-04-15 18:51:07 -0700 | [diff] [blame] | 584 | final MessageCursor cursor = getMessageCursor(); |
Scott Kennedy | 919d01a | 2013-05-07 16:13:29 -0700 | [diff] [blame] | 585 | LogUtils.d(LOG_TAG, "onConversationSeen() - mConversation.isViewed() = %b, " |
| 586 | + "cursor null = %b, cursor.isConversationRead() = %b", |
| 587 | mConversation.isViewed(), cursor == null, |
| 588 | cursor != null && cursor.isConversationRead()); |
Vikram Aggarwal | d82a31f | 2013-02-05 15:03:00 -0800 | [diff] [blame] | 589 | if (!mConversation.isViewed() || (cursor != null && !cursor.isConversationRead())) { |
| 590 | // Mark the conversation viewed and read. |
| 591 | activity.getConversationUpdater() |
| 592 | .markConversationsRead(Arrays.asList(mConversation), true, true); |
Yu Ping Hu | 7c909c7 | 2013-01-18 11:58:01 -0800 | [diff] [blame] | 593 | |
Vikram Aggarwal | d82a31f | 2013-02-05 15:03:00 -0800 | [diff] [blame] | 594 | // and update the Message objects in the cursor so the next time a cursor update |
| 595 | // happens with these messages marked read, we know to ignore it |
Paul Westbrook | 2145229 | 2013-04-15 18:51:07 -0700 | [diff] [blame] | 596 | if (cursor != null && !cursor.isClosed()) { |
Vikram Aggarwal | d82a31f | 2013-02-05 15:03:00 -0800 | [diff] [blame] | 597 | cursor.markMessagesRead(); |
| 598 | } |
Paul Westbrook | 4d8cad5 | 2012-09-21 14:13:49 -0700 | [diff] [blame] | 599 | } |
| 600 | } |
Scott Kennedy | 3b965d7 | 2013-06-25 14:36:55 -0700 | [diff] [blame] | 601 | activity.getListHandler().onConversationSeen(); |
Andy Huang | e6c9fb6 | 2013-11-15 09:56:20 -0800 | [diff] [blame] | 602 | |
| 603 | mConversationSeen = true; |
Paul Westbrook | 4d8cad5 | 2012-09-21 14:13:49 -0700 | [diff] [blame] | 604 | } |
| 605 | |
Paul Westbrook | 4d8cad5 | 2012-09-21 14:13:49 -0700 | [diff] [blame] | 606 | protected ConversationViewState getNewViewState() { |
| 607 | return new ConversationViewState(); |
| 608 | } |
| 609 | |
Paul Westbrook | c42ad5e | 2013-05-09 16:52:15 -0700 | [diff] [blame] | 610 | private static class MessageLoader extends ObjectCursorLoader<ConversationMessage> { |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 611 | private boolean mDeliveredFirstResults = false; |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 612 | |
Andy Huang | 02133aa | 2012-11-08 19:50:57 -0800 | [diff] [blame] | 613 | public MessageLoader(Context c, Uri messageListUri) { |
Paul Westbrook | c42ad5e | 2013-05-09 16:52:15 -0700 | [diff] [blame] | 614 | super(c, messageListUri, UIProvider.MESSAGE_PROJECTION, ConversationMessage.FACTORY); |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | @Override |
Paul Westbrook | c42ad5e | 2013-05-09 16:52:15 -0700 | [diff] [blame] | 618 | public void deliverResult(ObjectCursor<ConversationMessage> result) { |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 619 | // We want to deliver these results, and then we want to make sure |
| 620 | // that any subsequent |
| 621 | // queries do not hit the network |
| 622 | super.deliverResult(result); |
| 623 | |
| 624 | if (!mDeliveredFirstResults) { |
| 625 | mDeliveredFirstResults = true; |
| 626 | Uri uri = getUri(); |
| 627 | |
| 628 | // Create a ListParams that tells the provider to not hit the |
| 629 | // network |
| 630 | final ListParams listParams = new ListParams(ListParams.NO_LIMIT, |
| 631 | false /* useNetwork */); |
| 632 | |
| 633 | // Build the new uri with this additional parameter |
| 634 | uri = uri |
| 635 | .buildUpon() |
| 636 | .appendQueryParameter(UIProvider.LIST_PARAMS_QUERY_PARAMETER, |
| 637 | listParams.serialize()).build(); |
| 638 | setUri(uri); |
| 639 | } |
| 640 | } |
Paul Westbrook | c42ad5e | 2013-05-09 16:52:15 -0700 | [diff] [blame] | 641 | |
| 642 | @Override |
| 643 | protected ObjectCursor<ConversationMessage> getObjectCursor(Cursor inner) { |
| 644 | return new MessageCursor(inner); |
| 645 | } |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 646 | } |
| 647 | |
mindyp | 26d4d2d | 2012-09-18 17:30:32 -0700 | [diff] [blame] | 648 | public abstract void onConversationUpdated(Conversation conversation); |
| 649 | |
Scott Kennedy | 1817678 | 2013-02-20 18:30:21 -0800 | [diff] [blame] | 650 | public void onDetachedModeEntered() { |
| 651 | // If we have no messages, then we have nothing to display, so leave this view. |
| 652 | // Otherwise, just set the detached flag. |
| 653 | final Cursor messageCursor = getMessageCursor(); |
| 654 | |
| 655 | if (messageCursor == null || messageCursor.getCount() == 0) { |
| 656 | popOut(); |
| 657 | } else { |
| 658 | mIsDetached = true; |
| 659 | } |
| 660 | } |
Andrew Sapperstein | 2fc6730 | 2013-04-29 18:24:56 -0700 | [diff] [blame] | 661 | |
| 662 | /** |
| 663 | * Called when the JavaScript reports that it transformed a message. |
| 664 | * Sets a flag to true and invalidates the options menu so it will |
| 665 | * include the "Revert auto-sizing" menu option. |
| 666 | */ |
| 667 | public void onConversationTransformed() { |
| 668 | mHasConversationBeenTransformed = true; |
Andrew Sapperstein | 376294b | 2013-06-06 16:04:26 -0700 | [diff] [blame] | 669 | mHandler.post(new FragmentRunnable("invalidateOptionsMenu", this) { |
Andrew Sapperstein | ae92e15 | 2013-05-03 13:55:18 -0700 | [diff] [blame] | 670 | @Override |
| 671 | public void go() { |
Andrew Sapperstein | 52882ff | 2014-07-27 12:30:18 -0700 | [diff] [blame] | 672 | mActivity.supportInvalidateOptionsMenu(); |
Andrew Sapperstein | ae92e15 | 2013-05-03 13:55:18 -0700 | [diff] [blame] | 673 | } |
| 674 | }); |
Andrew Sapperstein | 2fc6730 | 2013-04-29 18:24:56 -0700 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | /** |
| 678 | * Called when the "Revert auto-sizing" option is selected. Default |
| 679 | * implementation simply sets a value on whether transforms should be |
| 680 | * applied. Derived classes should override this class and force a |
| 681 | * re-render so that the conversation renders without |
| 682 | */ |
| 683 | public void showUntransformedConversation() { |
| 684 | // must set the value to true so we don't show the options menu item again |
| 685 | mHasConversationTransformBeenReverted = true; |
| 686 | } |
| 687 | |
| 688 | /** |
| 689 | * Returns {@code true} if the conversation should be transformed. {@code false}, otherwise. |
| 690 | * @return {@code true} if the conversation should be transformed. {@code false}, otherwise. |
| 691 | */ |
| 692 | public boolean shouldApplyTransforms() { |
Alice Yang | 3617b41 | 2013-05-10 00:30:07 -0700 | [diff] [blame] | 693 | return (mAccount.enableMessageTransforms > 0) && |
| 694 | !mHasConversationTransformBeenReverted; |
Andrew Sapperstein | 2fc6730 | 2013-04-29 18:24:56 -0700 | [diff] [blame] | 695 | } |
Andrew Sapperstein | 5c1692a | 2013-09-16 11:56:13 -0700 | [diff] [blame] | 696 | |
James Lemieux | 7cad280 | 2014-01-09 15:00:53 -0800 | [diff] [blame] | 697 | /** |
| 698 | * The Print item in the overflow menu of the Conversation view is shown based on the return |
| 699 | * from this method. |
| 700 | * |
| 701 | * @return {@code true} if the conversation can be printed; {@code false} otherwise. |
| 702 | */ |
| 703 | protected abstract boolean shouldShowPrintInOverflow(); |
| 704 | |
| 705 | /** |
| 706 | * Prints all messages in the conversation. |
| 707 | */ |
Andrew Sapperstein | 5c1692a | 2013-09-16 11:56:13 -0700 | [diff] [blame] | 708 | protected abstract void printConversation(); |
Andrew Sapperstein | 8ec43e8 | 2013-12-17 18:27:55 -0800 | [diff] [blame] | 709 | |
| 710 | public boolean shouldAlwaysShowImages() { |
| 711 | return (mAccount != null) && (mAccount.settings.showImages == Settings.ShowImages.ALWAYS); |
| 712 | } |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 713 | } |