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