Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -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 | |
Paul Westbrook | b8361c9 | 2012-09-27 10:57:14 -0700 | [diff] [blame] | 20 | import android.content.ContentResolver; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 21 | import android.content.Context; |
Mindy Pereira | 8e91572 | 2012-02-16 14:42:56 -0800 | [diff] [blame] | 22 | import android.content.Loader; |
mindyp | ad0c30d | 2012-09-25 12:09:13 -0700 | [diff] [blame] | 23 | import android.content.res.Resources; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 24 | import android.database.Cursor; |
Andy Huang | 9d3fd92 | 2012-09-26 22:23:58 -0700 | [diff] [blame] | 25 | import android.database.DataSetObserver; |
Paul Westbrook | b8361c9 | 2012-09-27 10:57:14 -0700 | [diff] [blame] | 26 | import android.net.Uri; |
Paul Westbrook | cebcc64 | 2012-08-08 10:06:04 -0700 | [diff] [blame] | 27 | import android.os.AsyncTask; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 28 | import android.os.Bundle; |
mindyp | 3bcf180 | 2012-09-09 11:17:00 -0700 | [diff] [blame] | 29 | import android.os.SystemClock; |
Andrew Sapperstein | 3af481c | 2013-10-30 10:29:38 -0700 | [diff] [blame] | 30 | import android.support.v4.text.BidiFormatter; |
Andrew Sapperstein | 8ec43e8 | 2013-12-17 18:27:55 -0800 | [diff] [blame^] | 31 | import android.support.v4.util.ArrayMap; |
Andy Huang | 47aa9c9 | 2012-07-31 15:37:21 -0700 | [diff] [blame] | 32 | import android.text.TextUtils; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 33 | import android.view.LayoutInflater; |
Andy Huang | c1fb9a9 | 2013-02-11 13:09:12 -0800 | [diff] [blame] | 34 | import android.view.View; |
Mark Wei | 4071c2f | 2012-09-26 14:38:38 -0700 | [diff] [blame] | 35 | import android.view.View.OnLayoutChangeListener; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 36 | import android.view.ViewGroup; |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 37 | import android.webkit.ConsoleMessage; |
Paul Westbrook | cebcc64 | 2012-08-08 10:06:04 -0700 | [diff] [blame] | 38 | import android.webkit.CookieManager; |
| 39 | import android.webkit.CookieSyncManager; |
Mindy Pereira | 974c966 | 2012-09-14 10:02:08 -0700 | [diff] [blame] | 40 | import android.webkit.JavascriptInterface; |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 41 | import android.webkit.WebChromeClient; |
| 42 | import android.webkit.WebSettings; |
Andy Huang | 17a9cde | 2012-03-09 18:03:16 -0800 | [diff] [blame] | 43 | import android.webkit.WebView; |
Andrew Sapperstein | 821fa87 | 2013-08-21 21:57:39 -0700 | [diff] [blame] | 44 | import android.widget.Button; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 45 | |
Andy Huang | 59e0b18 | 2012-08-14 14:32:23 -0700 | [diff] [blame] | 46 | import com.android.mail.FormattedDateBuilder; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 47 | import com.android.mail.R; |
Andy Huang | e6c9fb6 | 2013-11-15 09:56:20 -0800 | [diff] [blame] | 48 | import com.android.mail.analytics.Analytics; |
Andy Huang | 5ff6374 | 2012-03-16 20:30:23 -0700 | [diff] [blame] | 49 | import com.android.mail.browse.ConversationContainer; |
Andy Huang | adbf3e8 | 2012-10-13 13:30:19 -0700 | [diff] [blame] | 50 | import com.android.mail.browse.ConversationContainer.OverlayPosition; |
Andrew Sapperstein | 8812d3c | 2013-06-04 17:06:41 -0700 | [diff] [blame] | 51 | import com.android.mail.browse.ConversationMessage; |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 52 | import com.android.mail.browse.ConversationOverlayItem; |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 53 | import com.android.mail.browse.ConversationViewAdapter; |
Andrew Sapperstein | 14f9374 | 2013-07-25 14:29:56 -0700 | [diff] [blame] | 54 | import com.android.mail.browse.ConversationViewAdapter.BorderItem; |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 55 | import com.android.mail.browse.ConversationViewAdapter.MessageFooterItem; |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 56 | import com.android.mail.browse.ConversationViewAdapter.MessageHeaderItem; |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 57 | import com.android.mail.browse.ConversationViewAdapter.SuperCollapsedBlockItem; |
Andy Huang | 5ff6374 | 2012-03-16 20:30:23 -0700 | [diff] [blame] | 58 | import com.android.mail.browse.ConversationViewHeader; |
| 59 | import com.android.mail.browse.ConversationWebView; |
Andrew Sapperstein | 8ec43e8 | 2013-12-17 18:27:55 -0800 | [diff] [blame^] | 60 | import com.android.mail.browse.InlineAttachmentViewIntentBuilderCreator; |
| 61 | import com.android.mail.browse.InlineAttachmentViewIntentBuilderCreatorHolder; |
Andy Huang | c1fb9a9 | 2013-02-11 13:09:12 -0800 | [diff] [blame] | 62 | import com.android.mail.browse.MailWebView.ContentSizeChangeListener; |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 63 | import com.android.mail.browse.MessageCursor; |
Andy Huang | 59e0b18 | 2012-08-14 14:32:23 -0700 | [diff] [blame] | 64 | import com.android.mail.browse.MessageHeaderView; |
Andy Huang | adbf3e8 | 2012-10-13 13:30:19 -0700 | [diff] [blame] | 65 | import com.android.mail.browse.ScrollIndicatorsView; |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 66 | import com.android.mail.browse.SuperCollapsedBlock; |
Andy Huang | 0b7ed6f | 2012-07-25 19:23:26 -0700 | [diff] [blame] | 67 | import com.android.mail.browse.WebViewContextMenu; |
Paul Westbrook | c42ad5e | 2013-05-09 16:52:15 -0700 | [diff] [blame] | 68 | import com.android.mail.content.ObjectCursor; |
Andrew Sapperstein | 562c5ba | 2013-10-09 18:31:50 -0700 | [diff] [blame] | 69 | import com.android.mail.print.PrintUtils; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 70 | import com.android.mail.providers.Account; |
Andy Huang | 65fe28f | 2012-04-06 18:08:53 -0700 | [diff] [blame] | 71 | import com.android.mail.providers.Address; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 72 | import com.android.mail.providers.Conversation; |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 73 | import com.android.mail.providers.Message; |
Alice Yang | f323c04 | 2013-10-30 00:15:02 -0700 | [diff] [blame] | 74 | import com.android.mail.providers.Settings; |
Paul Westbrook | b8361c9 | 2012-09-27 10:57:14 -0700 | [diff] [blame] | 75 | import com.android.mail.providers.UIProvider; |
Andy Huang | cd5c5ee | 2012-08-12 19:03:51 -0700 | [diff] [blame] | 76 | import com.android.mail.ui.ConversationViewState.ExpansionState; |
Andrew Sapperstein | 376294b | 2013-06-06 16:04:26 -0700 | [diff] [blame] | 77 | import com.android.mail.utils.ConversationViewUtils; |
Paul Westbrook | b334c90 | 2012-06-25 11:42:46 -0700 | [diff] [blame] | 78 | import com.android.mail.utils.LogTag; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 79 | import com.android.mail.utils.LogUtils; |
Andy Huang | 2e9acfe | 2012-03-15 22:39:36 -0700 | [diff] [blame] | 80 | import com.android.mail.utils.Utils; |
Andy Huang | 543e709 | 2013-04-22 11:44:56 -0700 | [diff] [blame] | 81 | import com.google.common.collect.ImmutableList; |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 82 | import com.google.common.collect.Lists; |
Andy Huang | 05c70c8 | 2013-03-14 15:15:50 -0700 | [diff] [blame] | 83 | import com.google.common.collect.Maps; |
Andy Huang | b8331b4 | 2012-07-16 19:08:53 -0700 | [diff] [blame] | 84 | import com.google.common.collect.Sets; |
Andy Huang | 65fe28f | 2012-04-06 18:08:53 -0700 | [diff] [blame] | 85 | |
Scott Kennedy | eb9a4bd | 2012-11-12 10:33:04 -0800 | [diff] [blame] | 86 | import java.util.ArrayList; |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 87 | import java.util.List; |
Andy Huang | 05c70c8 | 2013-03-14 15:15:50 -0700 | [diff] [blame] | 88 | import java.util.Map; |
Andy Huang | b8331b4 | 2012-07-16 19:08:53 -0700 | [diff] [blame] | 89 | import java.util.Set; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 90 | |
| 91 | /** |
| 92 | * The conversation view UI component. |
| 93 | */ |
Andrew Sapperstein | 9f957f3 | 2013-07-19 15:18:18 -0700 | [diff] [blame] | 94 | public class ConversationViewFragment extends AbstractConversationViewFragment implements |
Andrew Sapperstein | 4ddda2f | 2013-06-10 11:15:38 -0700 | [diff] [blame] | 95 | SuperCollapsedBlock.OnClickListener, OnLayoutChangeListener, |
| 96 | MessageHeaderView.MessageHeaderViewCallbacks { |
Mindy Pereira | 8e91572 | 2012-02-16 14:42:56 -0800 | [diff] [blame] | 97 | |
Paul Westbrook | b334c90 | 2012-06-25 11:42:46 -0700 | [diff] [blame] | 98 | private static final String LOG_TAG = LogTag.getLogTag(); |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 99 | public static final String LAYOUT_TAG = "ConvLayout"; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 100 | |
Andy Huang | 9d3fd92 | 2012-09-26 22:23:58 -0700 | [diff] [blame] | 101 | /** |
mindyp | 1b3cc47 | 2012-09-27 11:32:59 -0700 | [diff] [blame] | 102 | * Difference in the height of the message header whose details have been expanded/collapsed |
| 103 | */ |
| 104 | private int mDiff = 0; |
| 105 | |
| 106 | /** |
Andy Huang | 9d3fd92 | 2012-09-26 22:23:58 -0700 | [diff] [blame] | 107 | * Default value for {@link #mLoadWaitReason}. Conversation load will happen immediately. |
| 108 | */ |
| 109 | private final int LOAD_NOW = 0; |
| 110 | /** |
| 111 | * Value for {@link #mLoadWaitReason} that means we are offscreen and waiting for the visible |
| 112 | * conversation to finish loading before beginning our load. |
| 113 | * <p> |
| 114 | * When this value is set, the fragment should register with {@link ConversationListCallbacks} |
| 115 | * to know when the visible conversation is loaded. When it is unset, it should unregister. |
| 116 | */ |
| 117 | private final int LOAD_WAIT_FOR_INITIAL_CONVERSATION = 1; |
| 118 | /** |
| 119 | * Value for {@link #mLoadWaitReason} used when a conversation is too heavyweight to load at |
| 120 | * all when not visible (e.g. requires network fetch, or too complex). Conversation load will |
| 121 | * wait until this fragment is visible. |
| 122 | */ |
| 123 | private final int LOAD_WAIT_UNTIL_VISIBLE = 2; |
mindyp | 3bcf180 | 2012-09-09 11:17:00 -0700 | [diff] [blame] | 124 | |
Andrew Sapperstein | 9f957f3 | 2013-07-19 15:18:18 -0700 | [diff] [blame] | 125 | protected ConversationContainer mConversationContainer; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 126 | |
Andrew Sapperstein | 9f957f3 | 2013-07-19 15:18:18 -0700 | [diff] [blame] | 127 | protected ConversationWebView mWebView; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 128 | |
Mark Wei | 56d8385 | 2012-09-19 14:28:50 -0700 | [diff] [blame] | 129 | private ScrollIndicatorsView mScrollIndicators; |
| 130 | |
Andrew Sapperstein | 376294b | 2013-06-06 16:04:26 -0700 | [diff] [blame] | 131 | private ConversationViewProgressController mProgressController; |
| 132 | |
Andrew Sapperstein | 821fa87 | 2013-08-21 21:57:39 -0700 | [diff] [blame] | 133 | private Button mNewMessageBar; |
Andy Huang | 47aa9c9 | 2012-07-31 15:37:21 -0700 | [diff] [blame] | 134 | |
Andrew Sapperstein | 9f957f3 | 2013-07-19 15:18:18 -0700 | [diff] [blame] | 135 | protected HtmlConversationTemplates mTemplates; |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 136 | |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 137 | private final MailJsBridge mJsBridge = new MailJsBridge(); |
| 138 | |
Andrew Sapperstein | 9f957f3 | 2013-07-19 15:18:18 -0700 | [diff] [blame] | 139 | protected ConversationViewAdapter mAdapter; |
Andy Huang | 5106713 | 2012-03-12 20:08:19 -0700 | [diff] [blame] | 140 | |
Andrew Sapperstein | b1d184d | 2013-08-09 14:14:31 -0700 | [diff] [blame] | 141 | protected boolean mViewsCreated; |
Mark Wei | 4071c2f | 2012-09-26 14:38:38 -0700 | [diff] [blame] | 142 | // True if we attempted to render before the views were laid out |
| 143 | // We will render immediately once layout is done |
| 144 | private boolean mNeedRender; |
Andy Huang | 5106713 | 2012-03-12 20:08:19 -0700 | [diff] [blame] | 145 | |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 146 | /** |
| 147 | * Temporary string containing the message bodies of the messages within a super-collapsed |
| 148 | * block, for one-time use during block expansion. We cannot easily pass the body HTML |
| 149 | * into JS without problematic escaping, so hold onto it momentarily and signal JS to fetch it |
| 150 | * using {@link MailJsBridge}. |
| 151 | */ |
| 152 | private String mTempBodiesHtml; |
| 153 | |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 154 | private int mMaxAutoLoadMessages; |
| 155 | |
Andrew Sapperstein | 9f957f3 | 2013-07-19 15:18:18 -0700 | [diff] [blame] | 156 | protected int mSideMarginPx; |
Andy Huang | 02f9d18 | 2012-11-28 22:38:02 -0800 | [diff] [blame] | 157 | |
Andy Huang | 9d3fd92 | 2012-09-26 22:23:58 -0700 | [diff] [blame] | 158 | /** |
| 159 | * If this conversation fragment is not visible, and it's inappropriate to load up front, |
| 160 | * this is the reason we are waiting. This flag should be cleared once it's okay to load |
| 161 | * the conversation. |
| 162 | */ |
| 163 | private int mLoadWaitReason = LOAD_NOW; |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 164 | |
mindyp | 3bcf180 | 2012-09-09 11:17:00 -0700 | [diff] [blame] | 165 | private boolean mEnableContentReadySignal; |
Andy Huang | 28b7aee | 2012-08-20 20:27:32 -0700 | [diff] [blame] | 166 | |
mindyp | dde3f9f | 2012-09-10 17:35:35 -0700 | [diff] [blame] | 167 | private ContentSizeChangeListener mWebViewSizeChangeListener; |
| 168 | |
Andy Huang | e964eee | 2012-10-02 19:24:58 -0700 | [diff] [blame] | 169 | private float mWebViewYPercent; |
| 170 | |
| 171 | /** |
| 172 | * Has loadData been called on the WebView yet? |
| 173 | */ |
| 174 | private boolean mWebViewLoadedData; |
| 175 | |
Andy Huang | 63b3c67 | 2012-10-05 19:27:28 -0700 | [diff] [blame] | 176 | private long mWebViewLoadStartMs; |
| 177 | |
Andy Huang | 05c70c8 | 2013-03-14 15:15:50 -0700 | [diff] [blame] | 178 | private final Map<String, String> mMessageTransforms = Maps.newHashMap(); |
| 179 | |
Andy Huang | 9d3fd92 | 2012-09-26 22:23:58 -0700 | [diff] [blame] | 180 | private final DataSetObserver mLoadedObserver = new DataSetObserver() { |
| 181 | @Override |
| 182 | public void onChanged() { |
Andrew Sapperstein | 376294b | 2013-06-06 16:04:26 -0700 | [diff] [blame] | 183 | getHandler().post(new FragmentRunnable("delayedConversationLoad", |
| 184 | ConversationViewFragment.this) { |
Andy Huang | 9d3fd92 | 2012-09-26 22:23:58 -0700 | [diff] [blame] | 185 | @Override |
| 186 | public void go() { |
| 187 | LogUtils.d(LOG_TAG, "CVF load observer fired, this=%s", |
| 188 | ConversationViewFragment.this); |
| 189 | handleDelayedConversationLoad(); |
| 190 | } |
| 191 | }); |
| 192 | } |
| 193 | }; |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 194 | |
Andrew Sapperstein | 376294b | 2013-06-06 16:04:26 -0700 | [diff] [blame] | 195 | private final Runnable mOnProgressDismiss = new FragmentRunnable("onProgressDismiss", this) { |
Andy Huang | 7d4746e | 2012-10-17 17:03:17 -0700 | [diff] [blame] | 196 | @Override |
| 197 | public void go() { |
Scott Kennedy | 58192e5 | 2013-05-08 16:35:57 -0700 | [diff] [blame] | 198 | LogUtils.d(LOG_TAG, "onProgressDismiss go() - isUserVisible() = %b", isUserVisible()); |
Andy Huang | 7d4746e | 2012-10-17 17:03:17 -0700 | [diff] [blame] | 199 | if (isUserVisible()) { |
| 200 | onConversationSeen(); |
| 201 | } |
Andy Huang | 30bcfe7 | 2012-10-18 18:09:03 -0700 | [diff] [blame] | 202 | mWebView.onRenderComplete(); |
Andy Huang | 7d4746e | 2012-10-17 17:03:17 -0700 | [diff] [blame] | 203 | } |
| 204 | }; |
| 205 | |
Andy Huang | bd544e3 | 2012-05-29 15:56:51 -0700 | [diff] [blame] | 206 | private static final boolean DEBUG_DUMP_CONVERSATION_HTML = false; |
Andy Huang | 47aa9c9 | 2012-07-31 15:37:21 -0700 | [diff] [blame] | 207 | private static final boolean DISABLE_OFFSCREEN_LOADING = false; |
Andy Huang | 06c0362 | 2012-10-22 18:59:45 -0700 | [diff] [blame] | 208 | private static final boolean DEBUG_DUMP_CURSOR_CONTENTS = false; |
Andy Huang | e964eee | 2012-10-02 19:24:58 -0700 | [diff] [blame] | 209 | |
| 210 | private static final String BUNDLE_KEY_WEBVIEW_Y_PERCENT = |
| 211 | ConversationViewFragment.class.getName() + "webview-y-percent"; |
Andy Huang | bd544e3 | 2012-05-29 15:56:51 -0700 | [diff] [blame] | 212 | |
Andrew Sapperstein | 3af481c | 2013-10-30 10:29:38 -0700 | [diff] [blame] | 213 | private BidiFormatter sBidiFormatter; |
| 214 | |
Vikram Aggarwal | 6c51158 | 2012-02-27 10:59:47 -0800 | [diff] [blame] | 215 | /** |
Andrew Sapperstein | 8ec43e8 | 2013-12-17 18:27:55 -0800 | [diff] [blame^] | 216 | * Contains a mapping between inline image attachments and their local message id. |
| 217 | */ |
| 218 | private Map<String, String> mUrlToMessageIdMap; |
| 219 | |
| 220 | /** |
Vikram Aggarwal | 6c51158 | 2012-02-27 10:59:47 -0800 | [diff] [blame] | 221 | * Constructor needs to be public to handle orientation changes and activity lifecycle events. |
| 222 | */ |
Paul Westbrook | f0ea484 | 2013-08-13 16:41:18 -0700 | [diff] [blame] | 223 | public ConversationViewFragment() {} |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 224 | |
| 225 | /** |
| 226 | * Creates a new instance of {@link ConversationViewFragment}, initialized |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 227 | * to display a conversation with other parameters inherited/copied from an existing bundle, |
| 228 | * typically one created using {@link #makeBasicArgs}. |
| 229 | */ |
| 230 | public static ConversationViewFragment newInstance(Bundle existingArgs, |
| 231 | Conversation conversation) { |
| 232 | ConversationViewFragment f = new ConversationViewFragment(); |
| 233 | Bundle args = new Bundle(existingArgs); |
| 234 | args.putParcelable(ARG_CONVERSATION, conversation); |
| 235 | f.setArguments(args); |
| 236 | return f; |
| 237 | } |
| 238 | |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 239 | @Override |
Andy Huang | adbf3e8 | 2012-10-13 13:30:19 -0700 | [diff] [blame] | 240 | public void onAccountChanged(Account newAccount, Account oldAccount) { |
| 241 | // if overview mode has changed, re-render completely (no need to also update headers) |
| 242 | if (isOverviewMode(newAccount) != isOverviewMode(oldAccount)) { |
| 243 | setupOverviewMode(); |
| 244 | final MessageCursor c = getMessageCursor(); |
| 245 | if (c != null) { |
| 246 | renderConversation(c); |
| 247 | } else { |
| 248 | // Null cursor means this fragment is either waiting to load or in the middle of |
| 249 | // loading. Either way, a future render will happen anyway, and the new setting |
| 250 | // will take effect when that happens. |
| 251 | } |
| 252 | return; |
| 253 | } |
| 254 | |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 255 | // settings may have been updated; refresh views that are known to |
| 256 | // depend on settings |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 257 | mAdapter.notifyDataSetChanged(); |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 260 | @Override |
| 261 | public void onActivityCreated(Bundle savedInstanceState) { |
Andy Huang | 9d3fd92 | 2012-09-26 22:23:58 -0700 | [diff] [blame] | 262 | LogUtils.d(LOG_TAG, "IN CVF.onActivityCreated, this=%s visible=%s", this, isUserVisible()); |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 263 | super.onActivityCreated(savedInstanceState); |
Mark Wei | 1abfcaf | 2012-09-27 11:11:07 -0700 | [diff] [blame] | 264 | |
| 265 | if (mActivity == null || mActivity.isFinishing()) { |
| 266 | // Activity is finishing, just bail. |
| 267 | return; |
| 268 | } |
| 269 | |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 270 | Context context = getContext(); |
| 271 | mTemplates = new HtmlConversationTemplates(context); |
Andy Huang | 59e0b18 | 2012-08-14 14:32:23 -0700 | [diff] [blame] | 272 | |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 273 | final FormattedDateBuilder dateBuilder = new FormattedDateBuilder(context); |
Andy Huang | 59e0b18 | 2012-08-14 14:32:23 -0700 | [diff] [blame] | 274 | |
Paul Westbrook | 8081df4 | 2012-09-10 15:43:36 -0700 | [diff] [blame] | 275 | mAdapter = new ConversationViewAdapter(mActivity, this, |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 276 | getLoaderManager(), this, getContactInfoSource(), this, |
Paul Westbrook | 8081df4 | 2012-09-10 15:43:36 -0700 | [diff] [blame] | 277 | this, mAddressCache, dateBuilder); |
Andy Huang | 5106713 | 2012-03-12 20:08:19 -0700 | [diff] [blame] | 278 | mConversationContainer.setOverlayAdapter(mAdapter); |
| 279 | |
Andy Huang | 59e0b18 | 2012-08-14 14:32:23 -0700 | [diff] [blame] | 280 | // set up snap header (the adapter usually does this with the other ones) |
Andrew Sapperstein | 85ea618 | 2013-10-14 18:21:08 -0700 | [diff] [blame] | 281 | mConversationContainer.getSnapHeader().initialize( |
| 282 | this, mAddressCache, this, getContactInfoSource(), |
| 283 | mActivity.getAccountController().getVeiledAddressMatcher()); |
Andy Huang | c1fb9a9 | 2013-02-11 13:09:12 -0800 | [diff] [blame] | 284 | |
Andrew Sapperstein | 90eccf9 | 2013-08-22 11:53:23 -0700 | [diff] [blame] | 285 | final Resources resources = getResources(); |
| 286 | mMaxAutoLoadMessages = resources.getInteger(R.integer.max_auto_load_messages); |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 287 | |
Andrew Sapperstein | 90eccf9 | 2013-08-22 11:53:23 -0700 | [diff] [blame] | 288 | mSideMarginPx = resources.getDimensionPixelOffset( |
Andy Huang | 02f9d18 | 2012-11-28 22:38:02 -0800 | [diff] [blame] | 289 | R.dimen.conversation_message_content_margin_side); |
| 290 | |
Andrew Sapperstein | 8ec43e8 | 2013-12-17 18:27:55 -0800 | [diff] [blame^] | 291 | mUrlToMessageIdMap = new ArrayMap<String, String>(); |
| 292 | final InlineAttachmentViewIntentBuilderCreator creator = |
| 293 | InlineAttachmentViewIntentBuilderCreatorHolder. |
| 294 | getInlineAttachmentViewIntentCreator(); |
| 295 | mWebView.setOnCreateContextMenuListener(new WebViewContextMenu(getActivity(), |
| 296 | creator.createInlineAttachmentViewIntentBuilder( |
| 297 | mUrlToMessageIdMap, mAccount.getEmailAddress(), |
| 298 | mConversation != null ? mConversation.id : -1))); |
Andy Huang | 0b7ed6f | 2012-07-25 19:23:26 -0700 | [diff] [blame] | 299 | |
Andy Huang | adbf3e8 | 2012-10-13 13:30:19 -0700 | [diff] [blame] | 300 | // set this up here instead of onCreateView to ensure the latest Account is loaded |
| 301 | setupOverviewMode(); |
| 302 | |
Andy Huang | 9d3fd92 | 2012-09-26 22:23:58 -0700 | [diff] [blame] | 303 | // Defer the call to initLoader with a Handler. |
| 304 | // We want to wait until we know which fragments are present and their final visibility |
| 305 | // states before going off and doing work. This prevents extraneous loading from occurring |
| 306 | // as the ViewPager shifts about before the initial position is set. |
| 307 | // |
| 308 | // e.g. click on item #10 |
| 309 | // ViewPager.setAdapter() actually first loads #0 and #1 under the assumption that #0 is |
| 310 | // the initial primary item |
| 311 | // Then CPC immediately sets the primary item to #10, which tears down #0/#1 and sets up |
| 312 | // #9/#10/#11. |
Andrew Sapperstein | 376294b | 2013-06-06 16:04:26 -0700 | [diff] [blame] | 313 | getHandler().post(new FragmentRunnable("showConversation", this) { |
Andy Huang | 9d3fd92 | 2012-09-26 22:23:58 -0700 | [diff] [blame] | 314 | @Override |
| 315 | public void go() { |
| 316 | showConversation(); |
| 317 | } |
| 318 | }); |
Paul Westbrook | cebcc64 | 2012-08-08 10:06:04 -0700 | [diff] [blame] | 319 | |
Andrew Sapperstein | 606dbd7 | 2013-07-30 19:14:23 -0700 | [diff] [blame] | 320 | if (mConversation != null && mConversation.conversationBaseUri != null && |
Paul Westbrook | b8361c9 | 2012-09-27 10:57:14 -0700 | [diff] [blame] | 321 | !Utils.isEmpty(mAccount.accoutCookieQueryUri)) { |
Paul Westbrook | cebcc64 | 2012-08-08 10:06:04 -0700 | [diff] [blame] | 322 | // Set the cookie for this base url |
Paul Westbrook | b8361c9 | 2012-09-27 10:57:14 -0700 | [diff] [blame] | 323 | new SetCookieTask(getContext(), mConversation.conversationBaseUri, |
| 324 | mAccount.accoutCookieQueryUri).execute(); |
Paul Westbrook | cebcc64 | 2012-08-08 10:06:04 -0700 | [diff] [blame] | 325 | } |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 326 | } |
| 327 | |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 328 | @Override |
Andy Huang | e964eee | 2012-10-02 19:24:58 -0700 | [diff] [blame] | 329 | public void onCreate(Bundle savedState) { |
| 330 | super.onCreate(savedState); |
| 331 | |
Andrew Sapperstein | b1d184d | 2013-08-09 14:14:31 -0700 | [diff] [blame] | 332 | mWebViewClient = createConversationWebViewClient(); |
Andrew Sapperstein | 376294b | 2013-06-06 16:04:26 -0700 | [diff] [blame] | 333 | |
Andy Huang | e964eee | 2012-10-02 19:24:58 -0700 | [diff] [blame] | 334 | if (savedState != null) { |
| 335 | mWebViewYPercent = savedState.getFloat(BUNDLE_KEY_WEBVIEW_Y_PERCENT); |
| 336 | } |
Andrew Sapperstein | 3af481c | 2013-10-30 10:29:38 -0700 | [diff] [blame] | 337 | |
| 338 | if (sBidiFormatter == null) { |
| 339 | sBidiFormatter = BidiFormatter.getInstance(); |
| 340 | } |
Andy Huang | e964eee | 2012-10-02 19:24:58 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Andrew Sapperstein | b1d184d | 2013-08-09 14:14:31 -0700 | [diff] [blame] | 343 | protected ConversationWebViewClient createConversationWebViewClient() { |
| 344 | return new ConversationWebViewClient(mAccount); |
| 345 | } |
| 346 | |
Andy Huang | e964eee | 2012-10-02 19:24:58 -0700 | [diff] [blame] | 347 | @Override |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 348 | public View onCreateView(LayoutInflater inflater, |
| 349 | ViewGroup container, Bundle savedInstanceState) { |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 350 | |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 351 | View rootView = inflater.inflate(R.layout.conversation_view, container, false); |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 352 | mConversationContainer = (ConversationContainer) rootView |
| 353 | .findViewById(R.id.conversation_container); |
Andy Huang | 8f18778 | 2012-11-06 17:49:25 -0800 | [diff] [blame] | 354 | mConversationContainer.setAccountController(this); |
Andy Huang | 47aa9c9 | 2012-07-31 15:37:21 -0700 | [diff] [blame] | 355 | |
Andrew Sapperstein | 85ea618 | 2013-10-14 18:21:08 -0700 | [diff] [blame] | 356 | final ViewGroup topmostOverlay = |
| 357 | (ViewGroup) mConversationContainer.findViewById(R.id.conversation_topmost_overlay); |
| 358 | inflateSnapHeader(topmostOverlay, inflater); |
| 359 | mConversationContainer.setupSnapHeader(); |
| 360 | |
| 361 | setupNewMessageBar(); |
Andy Huang | 47aa9c9 | 2012-07-31 15:37:21 -0700 | [diff] [blame] | 362 | |
Andrew Sapperstein | 376294b | 2013-06-06 16:04:26 -0700 | [diff] [blame] | 363 | mProgressController = new ConversationViewProgressController(this, getHandler()); |
| 364 | mProgressController.instantiateProgressIndicators(rootView); |
mindyp | 3bcf180 | 2012-09-09 11:17:00 -0700 | [diff] [blame] | 365 | |
Andy Huang | 5ff6374 | 2012-03-16 20:30:23 -0700 | [diff] [blame] | 366 | mWebView = (ConversationWebView) mConversationContainer.findViewById(R.id.webview); |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 367 | |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 368 | mWebView.addJavascriptInterface(mJsBridge, "mail"); |
mindyp | 3bcf180 | 2012-09-09 11:17:00 -0700 | [diff] [blame] | 369 | // On JB or newer, we use the 'webkitAnimationStart' DOM event to signal load complete |
| 370 | // Below JB, try to speed up initial render by having the webview do supplemental draws to |
| 371 | // custom a software canvas. |
mindyp | b941fdb | 2012-09-11 08:28:23 -0700 | [diff] [blame] | 372 | // TODO(mindyp): |
| 373 | //PAGE READINESS SIGNAL FOR JELLYBEAN AND NEWER |
| 374 | // Notify the app on 'webkitAnimationStart' of a simple dummy element with a simple no-op |
| 375 | // animation that immediately runs on page load. The app uses this as a signal that the |
| 376 | // content is loaded and ready to draw, since WebView delays firing this event until the |
| 377 | // layers are composited and everything is ready to draw. |
| 378 | // This signal does not seem to be reliable, so just use the old method for now. |
Andy Huang | f7ac83f | 2013-07-15 15:48:31 -0700 | [diff] [blame] | 379 | final boolean isJBOrLater = Utils.isRunningJellybeanOrLater(); |
| 380 | final boolean isUserVisible = isUserVisible(); |
| 381 | mWebView.setUseSoftwareLayer(!isJBOrLater); |
| 382 | mEnableContentReadySignal = isJBOrLater && isUserVisible; |
| 383 | mWebView.onUserVisibilityChanged(isUserVisible); |
Andy Huang | 17a9cde | 2012-03-09 18:03:16 -0800 | [diff] [blame] | 384 | mWebView.setWebViewClient(mWebViewClient); |
Andy Huang | c1fb9a9 | 2013-02-11 13:09:12 -0800 | [diff] [blame] | 385 | final WebChromeClient wcc = new WebChromeClient() { |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 386 | @Override |
| 387 | public boolean onConsoleMessage(ConsoleMessage consoleMessage) { |
Andrew Sapperstein | 8ec43e8 | 2013-12-17 18:27:55 -0800 | [diff] [blame^] | 388 | if (consoleMessage.messageLevel() == ConsoleMessage.MessageLevel.ERROR) { |
| 389 | LogUtils.wtf(LOG_TAG, "JS: %s (%s:%d) f=%s", consoleMessage.message(), |
| 390 | consoleMessage.sourceId(), consoleMessage.lineNumber(), |
| 391 | ConversationViewFragment.this); |
| 392 | } else { |
| 393 | LogUtils.i(LOG_TAG, "JS: %s (%s:%d) f=%s", consoleMessage.message(), |
| 394 | consoleMessage.sourceId(), consoleMessage.lineNumber(), |
| 395 | ConversationViewFragment.this); |
| 396 | } |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 397 | return true; |
| 398 | } |
Andy Huang | c1fb9a9 | 2013-02-11 13:09:12 -0800 | [diff] [blame] | 399 | }; |
| 400 | mWebView.setWebChromeClient(wcc); |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 401 | |
Andy Huang | 3233bff | 2012-03-20 19:38:45 -0700 | [diff] [blame] | 402 | final WebSettings settings = mWebView.getSettings(); |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 403 | |
Mark Wei | 56d8385 | 2012-09-19 14:28:50 -0700 | [diff] [blame] | 404 | mScrollIndicators = (ScrollIndicatorsView) rootView.findViewById(R.id.scroll_indicators); |
| 405 | mScrollIndicators.setSourceView(mWebView); |
| 406 | |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 407 | settings.setJavaScriptEnabled(true); |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 408 | |
Andrew Sapperstein | 376294b | 2013-06-06 16:04:26 -0700 | [diff] [blame] | 409 | ConversationViewUtils.setTextZoom(getResources(), settings); |
Andy Huang | c319b55 | 2012-04-25 19:53:50 -0700 | [diff] [blame] | 410 | |
Andy Huang | 5106713 | 2012-03-12 20:08:19 -0700 | [diff] [blame] | 411 | mViewsCreated = true; |
Andy Huang | e964eee | 2012-10-02 19:24:58 -0700 | [diff] [blame] | 412 | mWebViewLoadedData = false; |
Andy Huang | 5106713 | 2012-03-12 20:08:19 -0700 | [diff] [blame] | 413 | |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 414 | return rootView; |
| 415 | } |
| 416 | |
Andrew Sapperstein | 85ea618 | 2013-10-14 18:21:08 -0700 | [diff] [blame] | 417 | protected void inflateSnapHeader(ViewGroup topmostOverlay, LayoutInflater inflater) { |
| 418 | inflater.inflate(R.layout.conversation_topmost_overlay_items, topmostOverlay, true); |
| 419 | } |
| 420 | |
| 421 | protected void setupNewMessageBar() { |
| 422 | mNewMessageBar = (Button) mConversationContainer.findViewById( |
| 423 | R.id.new_message_notification_bar); |
| 424 | mNewMessageBar.setOnClickListener(new View.OnClickListener() { |
| 425 | @Override |
| 426 | public void onClick(View v) { |
| 427 | onNewMessageBarClick(); |
| 428 | } |
| 429 | }); |
| 430 | } |
| 431 | |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 432 | @Override |
Andy Huang | f7ac83f | 2013-07-15 15:48:31 -0700 | [diff] [blame] | 433 | public void onResume() { |
| 434 | super.onResume(); |
| 435 | if (mWebView != null) { |
| 436 | mWebView.onResume(); |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | @Override |
| 441 | public void onPause() { |
| 442 | super.onPause(); |
| 443 | if (mWebView != null) { |
| 444 | mWebView.onPause(); |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | @Override |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 449 | public void onDestroyView() { |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 450 | super.onDestroyView(); |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 451 | mConversationContainer.setOverlayAdapter(null); |
| 452 | mAdapter = null; |
Andy Huang | 9d3fd92 | 2012-09-26 22:23:58 -0700 | [diff] [blame] | 453 | resetLoadWaiting(); // be sure to unregister any active load observer |
Andy Huang | 5106713 | 2012-03-12 20:08:19 -0700 | [diff] [blame] | 454 | mViewsCreated = false; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 455 | } |
| 456 | |
Andy Huang | e964eee | 2012-10-02 19:24:58 -0700 | [diff] [blame] | 457 | @Override |
| 458 | public void onSaveInstanceState(Bundle outState) { |
| 459 | super.onSaveInstanceState(outState); |
| 460 | |
| 461 | outState.putFloat(BUNDLE_KEY_WEBVIEW_Y_PERCENT, calculateScrollYPercent()); |
| 462 | } |
| 463 | |
| 464 | private float calculateScrollYPercent() { |
Paul Westbrook | 1b56a67 | 2013-04-19 01:19:05 -0700 | [diff] [blame] | 465 | final float p; |
| 466 | if (mWebView == null) { |
| 467 | // onCreateView hasn't been called, return 0 as the user hasn't scrolled the view. |
| 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | final int scrollY = mWebView.getScrollY(); |
| 472 | final int viewH = mWebView.getHeight(); |
| 473 | final int webH = (int) (mWebView.getContentHeight() * mWebView.getScale()); |
Andy Huang | e964eee | 2012-10-02 19:24:58 -0700 | [diff] [blame] | 474 | |
| 475 | if (webH == 0 || webH <= viewH) { |
| 476 | p = 0; |
| 477 | } else if (scrollY + viewH >= webH) { |
| 478 | // The very bottom is a special case, it acts as a stronger anchor than the scroll top |
| 479 | // at that point. |
| 480 | p = 1.0f; |
| 481 | } else { |
| 482 | p = (float) scrollY / webH; |
| 483 | } |
| 484 | return p; |
| 485 | } |
| 486 | |
Andy Huang | 9d3fd92 | 2012-09-26 22:23:58 -0700 | [diff] [blame] | 487 | private void resetLoadWaiting() { |
| 488 | if (mLoadWaitReason == LOAD_WAIT_FOR_INITIAL_CONVERSATION) { |
| 489 | getListController().unregisterConversationLoadedObserver(mLoadedObserver); |
| 490 | } |
| 491 | mLoadWaitReason = LOAD_NOW; |
| 492 | } |
| 493 | |
Andy Huang | 5ff6374 | 2012-03-16 20:30:23 -0700 | [diff] [blame] | 494 | @Override |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 495 | protected void markUnread() { |
Vikram Aggarwal | d82a31f | 2013-02-05 15:03:00 -0800 | [diff] [blame] | 496 | super.markUnread(); |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 497 | // Ignore unsafe calls made after a fragment is detached from an activity |
| 498 | final ControllableActivity activity = (ControllableActivity) getActivity(); |
| 499 | if (activity == null) { |
| 500 | LogUtils.w(LOG_TAG, "ignoring markUnread for conv=%s", mConversation.id); |
| 501 | return; |
| 502 | } |
| 503 | |
Andy Huang | 28e31e2 | 2012-07-26 16:33:15 -0700 | [diff] [blame] | 504 | if (mViewState == null) { |
| 505 | LogUtils.i(LOG_TAG, "ignoring markUnread for conv with no view state (%d)", |
| 506 | mConversation.id); |
| 507 | return; |
| 508 | } |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 509 | activity.getConversationUpdater().markConversationMessagesUnread(mConversation, |
Vikram Aggarwal | 4a878b6 | 2012-07-31 15:09:25 -0700 | [diff] [blame] | 510 | mViewState.getUnreadMessageUris(), mViewState.getConversationInfo()); |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 511 | } |
| 512 | |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 513 | @Override |
| 514 | public void onUserVisibleHintChanged() { |
Andy Huang | 9d3fd92 | 2012-09-26 22:23:58 -0700 | [diff] [blame] | 515 | final boolean userVisible = isUserVisible(); |
Scott Kennedy | 58192e5 | 2013-05-08 16:35:57 -0700 | [diff] [blame] | 516 | LogUtils.d(LOG_TAG, "ConversationViewFragment#onUserVisibleHintChanged(), userVisible = %b", |
| 517 | userVisible); |
Andy Huang | 9d3fd92 | 2012-09-26 22:23:58 -0700 | [diff] [blame] | 518 | |
| 519 | if (!userVisible) { |
Andrew Sapperstein | 376294b | 2013-06-06 16:04:26 -0700 | [diff] [blame] | 520 | mProgressController.dismissLoadingStatus(); |
Andy Huang | 9d3fd92 | 2012-09-26 22:23:58 -0700 | [diff] [blame] | 521 | } else if (mViewsCreated) { |
Andy Huang | e6c9fb6 | 2013-11-15 09:56:20 -0800 | [diff] [blame] | 522 | String loadTag = null; |
Andy Huang | eeb4a35 | 2013-11-21 10:56:27 -0800 | [diff] [blame] | 523 | final boolean isInitialLoading; |
| 524 | if (mActivity != null) { |
| 525 | isInitialLoading = mActivity.getConversationUpdater() |
Andy Huang | e6c9fb6 | 2013-11-15 09:56:20 -0800 | [diff] [blame] | 526 | .isInitialConversationLoading(); |
Andy Huang | eeb4a35 | 2013-11-21 10:56:27 -0800 | [diff] [blame] | 527 | } else { |
| 528 | isInitialLoading = true; |
| 529 | } |
Andy Huang | e6c9fb6 | 2013-11-15 09:56:20 -0800 | [diff] [blame] | 530 | |
Andy Huang | 9d3fd92 | 2012-09-26 22:23:58 -0700 | [diff] [blame] | 531 | if (getMessageCursor() != null) { |
| 532 | LogUtils.d(LOG_TAG, "Fragment is now user-visible, onConversationSeen: %s", this); |
Andy Huang | e6c9fb6 | 2013-11-15 09:56:20 -0800 | [diff] [blame] | 533 | if (!isInitialLoading) { |
| 534 | loadTag = "preloaded"; |
| 535 | } |
Andy Huang | 9d3fd92 | 2012-09-26 22:23:58 -0700 | [diff] [blame] | 536 | onConversationSeen(); |
| 537 | } else if (isLoadWaiting()) { |
| 538 | LogUtils.d(LOG_TAG, "Fragment is now user-visible, showing conversation: %s", this); |
Andy Huang | e6c9fb6 | 2013-11-15 09:56:20 -0800 | [diff] [blame] | 539 | if (!isInitialLoading) { |
| 540 | loadTag = "load_deferred"; |
| 541 | } |
Andy Huang | 9d3fd92 | 2012-09-26 22:23:58 -0700 | [diff] [blame] | 542 | handleDelayedConversationLoad(); |
| 543 | } |
Andy Huang | e6c9fb6 | 2013-11-15 09:56:20 -0800 | [diff] [blame] | 544 | |
| 545 | if (loadTag != null) { |
| 546 | // pager swipes are visibility transitions to 'visible' except during initial |
| 547 | // pager load on A) enter conversation mode B) rotate C) 2-pane conv-mode list-tap |
| 548 | Analytics.getInstance().sendEvent("pager_swipe", loadTag, |
| 549 | getCurrentFolderTypeDesc(), 0); |
| 550 | } |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 551 | } |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 552 | |
Andy Huang | 30bcfe7 | 2012-10-18 18:09:03 -0700 | [diff] [blame] | 553 | if (mWebView != null) { |
| 554 | mWebView.onUserVisibilityChanged(userVisible); |
| 555 | } |
Andy Huang | f8cf546 | 2012-10-17 18:29:14 -0700 | [diff] [blame] | 556 | } |
| 557 | |
Andy Huang | 9d3fd92 | 2012-09-26 22:23:58 -0700 | [diff] [blame] | 558 | /** |
| 559 | * Will either call initLoader now to begin loading, or set {@link #mLoadWaitReason} and do |
| 560 | * nothing (in which case you should later call {@link #handleDelayedConversationLoad()}). |
| 561 | */ |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 562 | private void showConversation() { |
Andy Huang | 9d3fd92 | 2012-09-26 22:23:58 -0700 | [diff] [blame] | 563 | final int reason; |
| 564 | |
| 565 | if (isUserVisible()) { |
| 566 | LogUtils.i(LOG_TAG, |
| 567 | "SHOWCONV: CVF is user-visible, immediately loading conversation (%s)", this); |
| 568 | reason = LOAD_NOW; |
Andy Huang | 243c236 | 2013-03-01 17:50:35 -0800 | [diff] [blame] | 569 | timerMark("CVF.showConversation"); |
Andy Huang | 9d3fd92 | 2012-09-26 22:23:58 -0700 | [diff] [blame] | 570 | } else { |
| 571 | final boolean disableOffscreenLoading = DISABLE_OFFSCREEN_LOADING |
Alice Yang | 0b8c080 | 2013-09-16 14:35:18 -0700 | [diff] [blame] | 572 | || Utils.isLowRamDevice(getContext()) |
Andrew Sapperstein | 606dbd7 | 2013-07-30 19:14:23 -0700 | [diff] [blame] | 573 | || (mConversation != null && (mConversation.isRemote |
| 574 | || mConversation.getNumMessages() > mMaxAutoLoadMessages)); |
Andy Huang | 9d3fd92 | 2012-09-26 22:23:58 -0700 | [diff] [blame] | 575 | |
| 576 | // When not visible, we should not immediately load if either this conversation is |
| 577 | // too heavyweight, or if the main/initial conversation is busy loading. |
| 578 | if (disableOffscreenLoading) { |
| 579 | reason = LOAD_WAIT_UNTIL_VISIBLE; |
| 580 | LogUtils.i(LOG_TAG, "SHOWCONV: CVF waiting until visible to load (%s)", this); |
| 581 | } else if (getListController().isInitialConversationLoading()) { |
| 582 | reason = LOAD_WAIT_FOR_INITIAL_CONVERSATION; |
| 583 | LogUtils.i(LOG_TAG, "SHOWCONV: CVF waiting for initial to finish (%s)", this); |
| 584 | getListController().registerConversationLoadedObserver(mLoadedObserver); |
| 585 | } else { |
| 586 | LogUtils.i(LOG_TAG, |
| 587 | "SHOWCONV: CVF is not visible, but no reason to wait. loading now. (%s)", |
| 588 | this); |
| 589 | reason = LOAD_NOW; |
| 590 | } |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 591 | } |
Andy Huang | 9d3fd92 | 2012-09-26 22:23:58 -0700 | [diff] [blame] | 592 | |
| 593 | mLoadWaitReason = reason; |
| 594 | if (mLoadWaitReason == LOAD_NOW) { |
| 595 | startConversationLoad(); |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | private void handleDelayedConversationLoad() { |
| 600 | resetLoadWaiting(); |
| 601 | startConversationLoad(); |
| 602 | } |
| 603 | |
| 604 | private void startConversationLoad() { |
mindyp | 3bcf180 | 2012-09-09 11:17:00 -0700 | [diff] [blame] | 605 | mWebView.setVisibility(View.VISIBLE); |
Andrew Sapperstein | 606dbd7 | 2013-07-30 19:14:23 -0700 | [diff] [blame] | 606 | loadContent(); |
mindyp | 3bcf180 | 2012-09-09 11:17:00 -0700 | [diff] [blame] | 607 | // TODO(mindyp): don't show loading status for a previously rendered |
| 608 | // conversation. Ielieve this is better done by making sure don't show loading status |
| 609 | // until XX ms have passed without loading completed. |
Andrew Sapperstein | 376294b | 2013-06-06 16:04:26 -0700 | [diff] [blame] | 610 | mProgressController.showLoadingStatus(isUserVisible()); |
Mindy Pereira | 8e91572 | 2012-02-16 14:42:56 -0800 | [diff] [blame] | 611 | } |
| 612 | |
Andrew Sapperstein | 606dbd7 | 2013-07-30 19:14:23 -0700 | [diff] [blame] | 613 | /** |
| 614 | * Can be overridden in case a subclass needs to load something other than |
| 615 | * the messages of a conversation. |
| 616 | */ |
| 617 | protected void loadContent() { |
| 618 | getLoaderManager().initLoader(MESSAGE_LOADER, Bundle.EMPTY, getMessageLoaderCallbacks()); |
| 619 | } |
| 620 | |
Andy Huang | 7d4746e | 2012-10-17 17:03:17 -0700 | [diff] [blame] | 621 | private void revealConversation() { |
Andy Huang | 243c236 | 2013-03-01 17:50:35 -0800 | [diff] [blame] | 622 | timerMark("revealing conversation"); |
Andrew Sapperstein | 376294b | 2013-06-06 16:04:26 -0700 | [diff] [blame] | 623 | mProgressController.dismissLoadingStatus(mOnProgressDismiss); |
Andy Huang | 7d4746e | 2012-10-17 17:03:17 -0700 | [diff] [blame] | 624 | } |
| 625 | |
Andy Huang | 9d3fd92 | 2012-09-26 22:23:58 -0700 | [diff] [blame] | 626 | private boolean isLoadWaiting() { |
| 627 | return mLoadWaitReason != LOAD_NOW; |
| 628 | } |
| 629 | |
Andy Huang | 5106713 | 2012-03-12 20:08:19 -0700 | [diff] [blame] | 630 | private void renderConversation(MessageCursor messageCursor) { |
mindyp | 3bcf180 | 2012-09-09 11:17:00 -0700 | [diff] [blame] | 631 | final String convHtml = renderMessageBodies(messageCursor, mEnableContentReadySignal); |
Andy Huang | 243c236 | 2013-03-01 17:50:35 -0800 | [diff] [blame] | 632 | timerMark("rendered conversation"); |
Andy Huang | bd544e3 | 2012-05-29 15:56:51 -0700 | [diff] [blame] | 633 | |
| 634 | if (DEBUG_DUMP_CONVERSATION_HTML) { |
| 635 | java.io.FileWriter fw = null; |
| 636 | try { |
Andrew Sapperstein | f1566b1 | 2013-09-19 15:34:26 -0700 | [diff] [blame] | 637 | fw = new java.io.FileWriter(getSdCardFilePath()); |
Andy Huang | bd544e3 | 2012-05-29 15:56:51 -0700 | [diff] [blame] | 638 | fw.write(convHtml); |
| 639 | } catch (java.io.IOException e) { |
| 640 | e.printStackTrace(); |
| 641 | } finally { |
| 642 | if (fw != null) { |
| 643 | try { |
| 644 | fw.close(); |
| 645 | } catch (java.io.IOException e) { |
| 646 | e.printStackTrace(); |
| 647 | } |
| 648 | } |
| 649 | } |
| 650 | } |
| 651 | |
Andy Huang | e964eee | 2012-10-02 19:24:58 -0700 | [diff] [blame] | 652 | // save off existing scroll position before re-rendering |
| 653 | if (mWebViewLoadedData) { |
| 654 | mWebViewYPercent = calculateScrollYPercent(); |
| 655 | } |
| 656 | |
Andy Huang | bd544e3 | 2012-05-29 15:56:51 -0700 | [diff] [blame] | 657 | mWebView.loadDataWithBaseURL(mBaseUri, convHtml, "text/html", "utf-8", null); |
Andy Huang | e964eee | 2012-10-02 19:24:58 -0700 | [diff] [blame] | 658 | mWebViewLoadedData = true; |
Andy Huang | 63b3c67 | 2012-10-05 19:27:28 -0700 | [diff] [blame] | 659 | mWebViewLoadStartMs = SystemClock.uptimeMillis(); |
Andy Huang | 5106713 | 2012-03-12 20:08:19 -0700 | [diff] [blame] | 660 | } |
| 661 | |
Andrew Sapperstein | f1566b1 | 2013-09-19 15:34:26 -0700 | [diff] [blame] | 662 | protected String getSdCardFilePath() { |
| 663 | return "/sdcard/conv" + mConversation.id + ".html"; |
| 664 | } |
| 665 | |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 666 | /** |
| 667 | * Populate the adapter with overlay views (message headers, super-collapsed blocks, a |
| 668 | * conversation header), and return an HTML document with spacer divs inserted for all overlays. |
| 669 | * |
| 670 | */ |
Andrew Sapperstein | 9f957f3 | 2013-07-19 15:18:18 -0700 | [diff] [blame] | 671 | protected String renderMessageBodies(MessageCursor messageCursor, |
mindyp | 3bcf180 | 2012-09-09 11:17:00 -0700 | [diff] [blame] | 672 | boolean enableContentReadySignal) { |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 673 | int pos = -1; |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 674 | |
Andy Huang | 1ee96b2 | 2012-08-24 20:19:53 -0700 | [diff] [blame] | 675 | LogUtils.d(LOG_TAG, "IN renderMessageBodies, fragment=%s", this); |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 676 | boolean allowNetworkImages = false; |
| 677 | |
Andy Huang | c754357 | 2012-04-03 15:34:29 -0700 | [diff] [blame] | 678 | // TODO: re-use any existing adapter item state (expanded, details expanded, show pics) |
Andy Huang | 28b7aee | 2012-08-20 20:27:32 -0700 | [diff] [blame] | 679 | |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 680 | // Walk through the cursor and build up an overlay adapter as you go. |
| 681 | // Each overlay has an entry in the adapter for easy scroll handling in the container. |
| 682 | // Items are not necessarily 1:1 in cursor and adapter because of super-collapsed blocks. |
| 683 | // When adding adapter items, also add their heights to help the container later determine |
| 684 | // overlay dimensions. |
| 685 | |
Andy Huang | db620fe | 2012-08-24 15:45:28 -0700 | [diff] [blame] | 686 | // When re-rendering, prevent ConversationContainer from laying out overlays until after |
| 687 | // the new spacers are positioned by WebView. |
| 688 | mConversationContainer.invalidateSpacerGeometry(); |
| 689 | |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 690 | mAdapter.clear(); |
| 691 | |
Andy Huang | 47aa9c9 | 2012-07-31 15:37:21 -0700 | [diff] [blame] | 692 | // re-evaluate the message parts of the view state, since the messages may have changed |
| 693 | // since the previous render |
| 694 | final ConversationViewState prevState = mViewState; |
| 695 | mViewState = new ConversationViewState(prevState); |
| 696 | |
Andy Huang | 5ff6374 | 2012-03-16 20:30:23 -0700 | [diff] [blame] | 697 | // N.B. the units of height for spacers are actually dp and not px because WebView assumes |
Andy Huang | 2e9acfe | 2012-03-15 22:39:36 -0700 | [diff] [blame] | 698 | // a pixel is an mdpi pixel, unless you set device-dpi. |
Andy Huang | 5ff6374 | 2012-03-16 20:30:23 -0700 | [diff] [blame] | 699 | |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 700 | // add a single conversation header item |
| 701 | final int convHeaderPos = mAdapter.addConversationHeader(mConversation); |
Andy Huang | 2301470 | 2012-07-09 12:50:36 -0700 | [diff] [blame] | 702 | final int convHeaderPx = measureOverlayHeight(convHeaderPos); |
Andy Huang | 5ff6374 | 2012-03-16 20:30:23 -0700 | [diff] [blame] | 703 | |
Andy Huang | 02f9d18 | 2012-11-28 22:38:02 -0800 | [diff] [blame] | 704 | mTemplates.startConversation(mWebView.screenPxToWebPx(mSideMarginPx), |
Andy Huang | 256b35c | 2012-08-22 15:19:13 -0700 | [diff] [blame] | 705 | mWebView.screenPxToWebPx(convHeaderPx)); |
Andy Huang | 3233bff | 2012-03-20 19:38:45 -0700 | [diff] [blame] | 706 | |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 707 | int collapsedStart = -1; |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 708 | ConversationMessage prevCollapsedMsg = null; |
Andrew Sapperstein | e822148 | 2013-10-02 18:14:58 -0700 | [diff] [blame] | 709 | |
Andrew Sapperstein | 8ec43e8 | 2013-12-17 18:27:55 -0800 | [diff] [blame^] | 710 | final boolean alwaysShowImages = shouldAlwaysShowImages(); |
Alice Yang | f323c04 | 2013-10-30 00:15:02 -0700 | [diff] [blame] | 711 | |
Andrew Sapperstein | e822148 | 2013-10-02 18:14:58 -0700 | [diff] [blame] | 712 | boolean prevSafeForImages = alwaysShowImages; |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 713 | |
Andrew Sapperstein | cee3c90 | 2013-07-31 10:52:02 -0700 | [diff] [blame] | 714 | // Store the previous expanded state so that the border between |
| 715 | // the previous and current message can be properly initialized. |
| 716 | int previousExpandedState = ExpansionState.NONE; |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 717 | while (messageCursor.moveToPosition(++pos)) { |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 718 | final ConversationMessage msg = messageCursor.getMessage(); |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 719 | |
Andrew Sapperstein | e822148 | 2013-10-02 18:14:58 -0700 | [diff] [blame] | 720 | final boolean safeForImages = alwaysShowImages || |
Scott Kennedy | 2027384 | 2012-11-07 11:16:21 -0800 | [diff] [blame] | 721 | msg.alwaysShowImages || prevState.getShouldShowImages(msg); |
Andy Huang | 3233bff | 2012-03-20 19:38:45 -0700 | [diff] [blame] | 722 | allowNetworkImages |= safeForImages; |
Andy Huang | 2405528 | 2012-03-27 17:37:06 -0700 | [diff] [blame] | 723 | |
Paul Westbrook | 08098ec | 2012-08-12 15:30:28 -0700 | [diff] [blame] | 724 | final Integer savedExpanded = prevState.getExpansionState(msg); |
| 725 | final int expandedState; |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 726 | if (savedExpanded != null) { |
Andy Huang | 1ee96b2 | 2012-08-24 20:19:53 -0700 | [diff] [blame] | 727 | if (ExpansionState.isSuperCollapsed(savedExpanded) && messageCursor.isLast()) { |
| 728 | // override saved state when this is now the new last message |
| 729 | // this happens to the second-to-last message when you discard a draft |
| 730 | expandedState = ExpansionState.EXPANDED; |
| 731 | } else { |
| 732 | expandedState = savedExpanded; |
| 733 | } |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 734 | } else { |
Andy Huang | cd5c5ee | 2012-08-12 19:03:51 -0700 | [diff] [blame] | 735 | // new messages that are not expanded default to being eligible for super-collapse |
Paul Westbrook | 08098ec | 2012-08-12 15:30:28 -0700 | [diff] [blame] | 736 | expandedState = (!msg.read || msg.starred || messageCursor.isLast()) ? |
Andy Huang | cd5c5ee | 2012-08-12 19:03:51 -0700 | [diff] [blame] | 737 | ExpansionState.EXPANDED : ExpansionState.SUPER_COLLAPSED; |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 738 | } |
Scott Kennedy | 2027384 | 2012-11-07 11:16:21 -0800 | [diff] [blame] | 739 | mViewState.setShouldShowImages(msg, prevState.getShouldShowImages(msg)); |
Paul Westbrook | 08098ec | 2012-08-12 15:30:28 -0700 | [diff] [blame] | 740 | mViewState.setExpansionState(msg, expandedState); |
Andy Huang | c754357 | 2012-04-03 15:34:29 -0700 | [diff] [blame] | 741 | |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 742 | // save off "read" state from the cursor |
| 743 | // later, the view may not match the cursor (e.g. conversation marked read on open) |
Andy Huang | 423bea2 | 2012-08-21 12:00:49 -0700 | [diff] [blame] | 744 | // however, if a previous state indicated this message was unread, trust that instead |
| 745 | // so "mark unread" marks all originally unread messages |
| 746 | mViewState.setReadState(msg, msg.read && !prevState.isUnread(msg)); |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 747 | |
Andy Huang | cd5c5ee | 2012-08-12 19:03:51 -0700 | [diff] [blame] | 748 | // We only want to consider this for inclusion in the super collapsed block if |
| 749 | // 1) The we don't have previous state about this message (The first time that the |
| 750 | // user opens a conversation) |
| 751 | // 2) The previously saved state for this message indicates that this message is |
| 752 | // in the super collapsed block. |
| 753 | if (ExpansionState.isSuperCollapsed(expandedState)) { |
| 754 | // contribute to a super-collapsed block that will be emitted just before the |
| 755 | // next expanded header |
| 756 | if (collapsedStart < 0) { |
| 757 | collapsedStart = pos; |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 758 | } |
Andy Huang | cd5c5ee | 2012-08-12 19:03:51 -0700 | [diff] [blame] | 759 | prevCollapsedMsg = msg; |
| 760 | prevSafeForImages = safeForImages; |
Andrew Sapperstein | 7dc7fa0 | 2013-05-08 16:39:31 -0700 | [diff] [blame] | 761 | |
| 762 | // This line puts the from address in the address cache so that |
| 763 | // we get the sender image for it if it's in a super-collapsed block. |
| 764 | getAddress(msg.getFrom()); |
Andrew Sapperstein | cee3c90 | 2013-07-31 10:52:02 -0700 | [diff] [blame] | 765 | previousExpandedState = expandedState; |
Andy Huang | cd5c5ee | 2012-08-12 19:03:51 -0700 | [diff] [blame] | 766 | continue; |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 767 | } |
Andy Huang | 2405528 | 2012-03-27 17:37:06 -0700 | [diff] [blame] | 768 | |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 769 | // resolve any deferred decisions on previous collapsed items |
| 770 | if (collapsedStart >= 0) { |
| 771 | if (pos - collapsedStart == 1) { |
Andrew Sapperstein | cee3c90 | 2013-07-31 10:52:02 -0700 | [diff] [blame] | 772 | // Special-case for a single collapsed message: no need to super-collapse it. |
| 773 | // Since it is super-collapsed, there is no previous message to be |
| 774 | // collapsed and the border above it is the first border. |
| 775 | renderMessage(prevCollapsedMsg, false /* previousCollapsed */, |
| 776 | false /* expanded */, prevSafeForImages, true /* firstBorder */); |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 777 | } else { |
| 778 | renderSuperCollapsedBlock(collapsedStart, pos - 1); |
| 779 | } |
| 780 | prevCollapsedMsg = null; |
| 781 | collapsedStart = -1; |
| 782 | } |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 783 | |
Andrew Sapperstein | cee3c90 | 2013-07-31 10:52:02 -0700 | [diff] [blame] | 784 | renderMessage(msg, ExpansionState.isCollapsed(previousExpandedState), |
| 785 | ExpansionState.isExpanded(expandedState), safeForImages, |
Andrew Sapperstein | e539880 | 2013-07-29 20:55:38 -0700 | [diff] [blame] | 786 | pos == 0 /* firstBorder */); |
Andrew Sapperstein | cee3c90 | 2013-07-31 10:52:02 -0700 | [diff] [blame] | 787 | |
| 788 | previousExpandedState = expandedState; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 789 | } |
Andy Huang | 3233bff | 2012-03-20 19:38:45 -0700 | [diff] [blame] | 790 | |
| 791 | mWebView.getSettings().setBlockNetworkImage(!allowNetworkImages); |
| 792 | |
Andrew Sapperstein | 2fc6730 | 2013-04-29 18:24:56 -0700 | [diff] [blame] | 793 | final boolean applyTransforms = shouldApplyTransforms(); |
Andy Huang | 57f354c | 2013-04-11 17:23:40 -0700 | [diff] [blame] | 794 | |
Andrew Sapperstein | cee3c90 | 2013-07-31 10:52:02 -0700 | [diff] [blame] | 795 | renderBorder(true /* contiguous */, true /* expanded */, |
| 796 | false /* firstBorder */, true /* lastBorder */); |
Andrew Sapperstein | 14f9374 | 2013-07-25 14:29:56 -0700 | [diff] [blame] | 797 | |
Andy Huang | c1fb9a9 | 2013-02-11 13:09:12 -0800 | [diff] [blame] | 798 | // If the conversation has specified a base uri, use it here, otherwise use mBaseUri |
Andrew Sapperstein | 68141df | 2013-08-19 19:07:14 -0700 | [diff] [blame] | 799 | return mTemplates.endConversation(mBaseUri, mConversation.getBaseUri(mBaseUri), |
Andy Huang | 5ea5a83 | 2013-03-07 16:49:09 -0800 | [diff] [blame] | 800 | mWebView.getViewportWidth(), enableContentReadySignal, isOverviewMode(mAccount), |
Andy Huang | 57f354c | 2013-04-11 17:23:40 -0700 | [diff] [blame] | 801 | applyTransforms, applyTransforms); |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 802 | } |
Mindy Pereira | 674afa4 | 2012-02-17 14:05:24 -0800 | [diff] [blame] | 803 | |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 804 | private void renderSuperCollapsedBlock(int start, int end) { |
Andrew Sapperstein | cee3c90 | 2013-07-31 10:52:02 -0700 | [diff] [blame] | 805 | renderBorder(true /* contiguous */, true /* expanded */, |
| 806 | true /* firstBorder */, false /* lastBorder */); |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 807 | final int blockPos = mAdapter.addSuperCollapsedBlock(start, end); |
Andy Huang | 2301470 | 2012-07-09 12:50:36 -0700 | [diff] [blame] | 808 | final int blockPx = measureOverlayHeight(blockPos); |
| 809 | mTemplates.appendSuperCollapsedHtml(start, mWebView.screenPxToWebPx(blockPx)); |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 810 | } |
| 811 | |
Andrew Sapperstein | cee3c90 | 2013-07-31 10:52:02 -0700 | [diff] [blame] | 812 | protected void renderBorder( |
| 813 | boolean contiguous, boolean expanded, boolean firstBorder, boolean lastBorder) { |
| 814 | final int blockPos = mAdapter.addBorder(contiguous, expanded, firstBorder, lastBorder); |
Andrew Sapperstein | 14f9374 | 2013-07-25 14:29:56 -0700 | [diff] [blame] | 815 | final int blockPx = measureOverlayHeight(blockPos); |
| 816 | mTemplates.appendBorder(mWebView.screenPxToWebPx(blockPx)); |
| 817 | } |
| 818 | |
Andrew Sapperstein | cee3c90 | 2013-07-31 10:52:02 -0700 | [diff] [blame] | 819 | private void renderMessage(ConversationMessage msg, boolean previousCollapsed, |
| 820 | boolean expanded, boolean safeForImages, boolean firstBorder) { |
| 821 | renderMessage(msg, previousCollapsed, expanded, safeForImages, |
| 822 | true /* renderBorder */, firstBorder); |
Andrew Sapperstein | 1f08223 | 2013-07-29 11:16:07 -0700 | [diff] [blame] | 823 | } |
| 824 | |
Andrew Sapperstein | cee3c90 | 2013-07-31 10:52:02 -0700 | [diff] [blame] | 825 | private void renderMessage(ConversationMessage msg, boolean previousCollapsed, |
| 826 | boolean expanded, boolean safeForImages, boolean renderBorder, boolean firstBorder) { |
Andrew Sapperstein | 1f08223 | 2013-07-29 11:16:07 -0700 | [diff] [blame] | 827 | if (renderBorder) { |
Andrew Sapperstein | cee3c90 | 2013-07-31 10:52:02 -0700 | [diff] [blame] | 828 | // The border should be collapsed only if both the current |
| 829 | // and previous messages are collapsed. |
| 830 | renderBorder(true /* contiguous */, !previousCollapsed || expanded, |
| 831 | firstBorder, false /* lastBorder */); |
Andrew Sapperstein | 1f08223 | 2013-07-29 11:16:07 -0700 | [diff] [blame] | 832 | } |
Andrew Sapperstein | 14f9374 | 2013-07-25 14:29:56 -0700 | [diff] [blame] | 833 | |
Scott Kennedy | 2027384 | 2012-11-07 11:16:21 -0800 | [diff] [blame] | 834 | final int headerPos = mAdapter.addMessageHeader(msg, expanded, |
| 835 | mViewState.getShouldShowImages(msg)); |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 836 | final MessageHeaderItem headerItem = (MessageHeaderItem) mAdapter.getItem(headerPos); |
| 837 | |
| 838 | final int footerPos = mAdapter.addMessageFooter(headerItem); |
| 839 | |
| 840 | // Measure item header and footer heights to allocate spacers in HTML |
| 841 | // But since the views themselves don't exist yet, render each item temporarily into |
| 842 | // a host view for measurement. |
Andy Huang | 2301470 | 2012-07-09 12:50:36 -0700 | [diff] [blame] | 843 | final int headerPx = measureOverlayHeight(headerPos); |
| 844 | final int footerPx = measureOverlayHeight(footerPos); |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 845 | |
Andy Huang | 256b35c | 2012-08-22 15:19:13 -0700 | [diff] [blame] | 846 | mTemplates.appendMessageHtml(msg, expanded, safeForImages, |
Andy Huang | 2301470 | 2012-07-09 12:50:36 -0700 | [diff] [blame] | 847 | mWebView.screenPxToWebPx(headerPx), mWebView.screenPxToWebPx(footerPx)); |
Andy Huang | 243c236 | 2013-03-01 17:50:35 -0800 | [diff] [blame] | 848 | timerMark("rendered message"); |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | private String renderCollapsedHeaders(MessageCursor cursor, |
| 852 | SuperCollapsedBlockItem blockToReplace) { |
| 853 | final List<ConversationOverlayItem> replacements = Lists.newArrayList(); |
| 854 | |
| 855 | mTemplates.reset(); |
| 856 | |
Alice Yang | f323c04 | 2013-10-30 00:15:02 -0700 | [diff] [blame] | 857 | final boolean alwaysShowImages = (mAccount != null) && |
| 858 | (mAccount.settings.showImages == Settings.ShowImages.ALWAYS); |
Andrew Sapperstein | e822148 | 2013-10-02 18:14:58 -0700 | [diff] [blame] | 859 | |
Mark Wei | 2b24e99 | 2012-09-10 16:40:07 -0700 | [diff] [blame] | 860 | // In devices with non-integral density multiplier, screen pixels translate to non-integral |
| 861 | // web pixels. Keep track of the error that occurs when we cast all heights to int |
| 862 | float error = 0f; |
Andrew Sapperstein | 14f9374 | 2013-07-25 14:29:56 -0700 | [diff] [blame] | 863 | boolean first = true; |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 864 | for (int i = blockToReplace.getStart(), end = blockToReplace.getEnd(); i <= end; i++) { |
| 865 | cursor.moveToPosition(i); |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 866 | final ConversationMessage msg = cursor.getMessage(); |
Andrew Sapperstein | 14f9374 | 2013-07-25 14:29:56 -0700 | [diff] [blame] | 867 | |
| 868 | final int borderPx; |
| 869 | if (first) { |
| 870 | borderPx = 0; |
| 871 | first = false; |
| 872 | } else { |
Andrew Sapperstein | cee3c90 | 2013-07-31 10:52:02 -0700 | [diff] [blame] | 873 | // When replacing the super-collapsed block, |
| 874 | // the border is always collapsed between messages. |
| 875 | final BorderItem border = mAdapter.newBorderItem( |
| 876 | true /* contiguous */, false /* expanded */); |
Andrew Sapperstein | 14f9374 | 2013-07-25 14:29:56 -0700 | [diff] [blame] | 877 | borderPx = measureOverlayHeight(border); |
| 878 | replacements.add(border); |
| 879 | mTemplates.appendBorder(mWebView.screenPxToWebPx(borderPx)); |
| 880 | } |
| 881 | |
Andrew Sapperstein | 4ddda2f | 2013-06-10 11:15:38 -0700 | [diff] [blame] | 882 | final MessageHeaderItem header = ConversationViewAdapter.newMessageHeaderItem( |
Andrew Sapperstein | 14f9374 | 2013-07-25 14:29:56 -0700 | [diff] [blame] | 883 | mAdapter, mAdapter.getDateBuilder(), msg, false /* expanded */, |
Andrew Sapperstein | e822148 | 2013-10-02 18:14:58 -0700 | [diff] [blame] | 884 | alwaysShowImages || mViewState.getShouldShowImages(msg)); |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 885 | final MessageFooterItem footer = mAdapter.newMessageFooterItem(header); |
| 886 | |
Andy Huang | 2301470 | 2012-07-09 12:50:36 -0700 | [diff] [blame] | 887 | final int headerPx = measureOverlayHeight(header); |
| 888 | final int footerPx = measureOverlayHeight(footer); |
Mark Wei | 2b24e99 | 2012-09-10 16:40:07 -0700 | [diff] [blame] | 889 | error += mWebView.screenPxToWebPxError(headerPx) |
Andrew Sapperstein | 14f9374 | 2013-07-25 14:29:56 -0700 | [diff] [blame] | 890 | + mWebView.screenPxToWebPxError(footerPx) |
| 891 | + mWebView.screenPxToWebPxError(borderPx); |
Mark Wei | 2b24e99 | 2012-09-10 16:40:07 -0700 | [diff] [blame] | 892 | |
| 893 | // When the error becomes greater than 1 pixel, make the next header 1 pixel taller |
| 894 | int correction = 0; |
| 895 | if (error >= 1) { |
| 896 | correction = 1; |
| 897 | error -= 1; |
| 898 | } |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 899 | |
Andrew Sapperstein | e822148 | 2013-10-02 18:14:58 -0700 | [diff] [blame] | 900 | mTemplates.appendMessageHtml(msg, false /* expanded */, |
| 901 | alwaysShowImages || msg.alwaysShowImages, |
Mark Wei | 2b24e99 | 2012-09-10 16:40:07 -0700 | [diff] [blame] | 902 | mWebView.screenPxToWebPx(headerPx) + correction, |
| 903 | mWebView.screenPxToWebPx(footerPx)); |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 904 | replacements.add(header); |
| 905 | replacements.add(footer); |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 906 | |
Paul Westbrook | 08098ec | 2012-08-12 15:30:28 -0700 | [diff] [blame] | 907 | mViewState.setExpansionState(msg, ExpansionState.COLLAPSED); |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | mAdapter.replaceSuperCollapsedBlock(blockToReplace, replacements); |
Andy Huang | 06c0362 | 2012-10-22 18:59:45 -0700 | [diff] [blame] | 911 | mAdapter.notifyDataSetChanged(); |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 912 | |
| 913 | return mTemplates.emit(); |
| 914 | } |
| 915 | |
Andrew Sapperstein | 9f957f3 | 2013-07-19 15:18:18 -0700 | [diff] [blame] | 916 | protected int measureOverlayHeight(int position) { |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 917 | return measureOverlayHeight(mAdapter.getItem(position)); |
| 918 | } |
| 919 | |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 920 | /** |
Andy Huang | b8331b4 | 2012-07-16 19:08:53 -0700 | [diff] [blame] | 921 | * Measure the height of an adapter view by rendering an adapter item into a temporary |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 922 | * host view, and asking the view to immediately measure itself. This method will reuse |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 923 | * a previous adapter view from {@link ConversationContainer}'s scrap views if one was generated |
| 924 | * earlier. |
| 925 | * <p> |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 926 | * After measuring the height, this method also saves the height in the |
| 927 | * {@link ConversationOverlayItem} for later use in overlay positioning. |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 928 | * |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 929 | * @param convItem adapter item with data to render and measure |
Andy Huang | 2301470 | 2012-07-09 12:50:36 -0700 | [diff] [blame] | 930 | * @return height of the rendered view in screen px |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 931 | */ |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 932 | private int measureOverlayHeight(ConversationOverlayItem convItem) { |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 933 | final int type = convItem.getType(); |
| 934 | |
| 935 | final View convertView = mConversationContainer.getScrapView(type); |
Andy Huang | b8331b4 | 2012-07-16 19:08:53 -0700 | [diff] [blame] | 936 | final View hostView = mAdapter.getView(convItem, convertView, mConversationContainer, |
| 937 | true /* measureOnly */); |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 938 | if (convertView == null) { |
| 939 | mConversationContainer.addScrapView(type, hostView); |
| 940 | } |
| 941 | |
Andy Huang | 9875bb4 | 2012-04-04 20:36:21 -0700 | [diff] [blame] | 942 | final int heightPx = mConversationContainer.measureOverlay(hostView); |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 943 | convItem.setHeight(heightPx); |
Andy Huang | 9875bb4 | 2012-04-04 20:36:21 -0700 | [diff] [blame] | 944 | convItem.markMeasurementValid(); |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 945 | |
Andy Huang | 2301470 | 2012-07-09 12:50:36 -0700 | [diff] [blame] | 946 | return heightPx; |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 947 | } |
| 948 | |
Andy Huang | 5ff6374 | 2012-03-16 20:30:23 -0700 | [diff] [blame] | 949 | @Override |
| 950 | public void onConversationViewHeaderHeightChange(int newHeight) { |
Mark Wei | ab2d998 | 2012-09-25 13:06:17 -0700 | [diff] [blame] | 951 | final int h = mWebView.screenPxToWebPx(newHeight); |
| 952 | |
| 953 | mWebView.loadUrl(String.format("javascript:setConversationHeaderSpacerHeight(%s);", h)); |
Andy Huang | 5ff6374 | 2012-03-16 20:30:23 -0700 | [diff] [blame] | 954 | } |
| 955 | |
Andy Huang | 3233bff | 2012-03-20 19:38:45 -0700 | [diff] [blame] | 956 | // END conversation header callbacks |
| 957 | |
| 958 | // START message header callbacks |
| 959 | @Override |
Andy Huang | c754357 | 2012-04-03 15:34:29 -0700 | [diff] [blame] | 960 | public void setMessageSpacerHeight(MessageHeaderItem item, int newSpacerHeightPx) { |
| 961 | mConversationContainer.invalidateSpacerGeometry(); |
| 962 | |
| 963 | // update message HTML spacer height |
Andy Huang | 2301470 | 2012-07-09 12:50:36 -0700 | [diff] [blame] | 964 | final int h = mWebView.screenPxToWebPx(newSpacerHeightPx); |
| 965 | LogUtils.i(LAYOUT_TAG, "setting HTML spacer h=%dwebPx (%dscreenPx)", h, |
| 966 | newSpacerHeightPx); |
Vikram Aggarwal | 5349ce1 | 2012-09-24 14:12:40 -0700 | [diff] [blame] | 967 | mWebView.loadUrl(String.format("javascript:setMessageHeaderSpacerHeight('%s', %s);", |
Andy Huang | 014ea4c | 2012-09-25 14:50:54 -0700 | [diff] [blame] | 968 | mTemplates.getMessageDomId(item.getMessage()), h)); |
Andy Huang | 3233bff | 2012-03-20 19:38:45 -0700 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | @Override |
Andrew Sapperstein | cee3c90 | 2013-07-31 10:52:02 -0700 | [diff] [blame] | 972 | public void setMessageExpanded(MessageHeaderItem item, int newSpacerHeightPx, |
| 973 | int topBorderHeight, int bottomBorderHeight) { |
Andy Huang | c754357 | 2012-04-03 15:34:29 -0700 | [diff] [blame] | 974 | mConversationContainer.invalidateSpacerGeometry(); |
| 975 | |
| 976 | // show/hide the HTML message body and update the spacer height |
Andy Huang | 2301470 | 2012-07-09 12:50:36 -0700 | [diff] [blame] | 977 | final int h = mWebView.screenPxToWebPx(newSpacerHeightPx); |
Andrew Sapperstein | cee3c90 | 2013-07-31 10:52:02 -0700 | [diff] [blame] | 978 | final int topHeight = mWebView.screenPxToWebPx(topBorderHeight); |
| 979 | final int bottomHeight = mWebView.screenPxToWebPx(bottomBorderHeight); |
Andy Huang | 2301470 | 2012-07-09 12:50:36 -0700 | [diff] [blame] | 980 | LogUtils.i(LAYOUT_TAG, "setting HTML spacer expanded=%s h=%dwebPx (%dscreenPx)", |
| 981 | item.isExpanded(), h, newSpacerHeightPx); |
Andrew Sapperstein | cee3c90 | 2013-07-31 10:52:02 -0700 | [diff] [blame] | 982 | mWebView.loadUrl(String.format("javascript:setMessageBodyVisible('%s', %s, %s, %s, %s);", |
| 983 | mTemplates.getMessageDomId(item.getMessage()), item.isExpanded(), |
| 984 | h, topHeight, bottomHeight)); |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 985 | |
Andy Huang | 014ea4c | 2012-09-25 14:50:54 -0700 | [diff] [blame] | 986 | mViewState.setExpansionState(item.getMessage(), |
Paul Westbrook | 08098ec | 2012-08-12 15:30:28 -0700 | [diff] [blame] | 987 | item.isExpanded() ? ExpansionState.EXPANDED : ExpansionState.COLLAPSED); |
Andy Huang | 3233bff | 2012-03-20 19:38:45 -0700 | [diff] [blame] | 988 | } |
| 989 | |
| 990 | @Override |
Scott Kennedy | eb9a4bd | 2012-11-12 10:33:04 -0800 | [diff] [blame] | 991 | public void showExternalResources(final Message msg) { |
Scott Kennedy | 2027384 | 2012-11-07 11:16:21 -0800 | [diff] [blame] | 992 | mViewState.setShouldShowImages(msg, true); |
Andy Huang | 3233bff | 2012-03-20 19:38:45 -0700 | [diff] [blame] | 993 | mWebView.getSettings().setBlockNetworkImage(false); |
Scott Kennedy | eb9a4bd | 2012-11-12 10:33:04 -0800 | [diff] [blame] | 994 | mWebView.loadUrl("javascript:unblockImages(['" + mTemplates.getMessageDomId(msg) + "']);"); |
| 995 | } |
| 996 | |
| 997 | @Override |
| 998 | public void showExternalResources(final String senderRawAddress) { |
| 999 | mWebView.getSettings().setBlockNetworkImage(false); |
| 1000 | |
| 1001 | final Address sender = getAddress(senderRawAddress); |
| 1002 | final MessageCursor cursor = getMessageCursor(); |
| 1003 | |
| 1004 | final List<String> messageDomIds = new ArrayList<String>(); |
| 1005 | |
| 1006 | int pos = -1; |
| 1007 | while (cursor.moveToPosition(++pos)) { |
| 1008 | final ConversationMessage message = cursor.getMessage(); |
| 1009 | if (sender.equals(getAddress(message.getFrom()))) { |
| 1010 | message.alwaysShowImages = true; |
| 1011 | |
| 1012 | mViewState.setShouldShowImages(message, true); |
| 1013 | messageDomIds.add(mTemplates.getMessageDomId(message)); |
| 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | final String url = String.format( |
| 1018 | "javascript:unblockImages(['%s']);", TextUtils.join("','", messageDomIds)); |
| 1019 | mWebView.loadUrl(url); |
Andy Huang | 3233bff | 2012-03-20 19:38:45 -0700 | [diff] [blame] | 1020 | } |
Alice Yang | 1ebc2db | 2013-03-14 21:21:44 -0700 | [diff] [blame] | 1021 | |
| 1022 | @Override |
Andy Huang | 75b52a5 | 2013-03-15 15:40:24 -0700 | [diff] [blame] | 1023 | public boolean supportsMessageTransforms() { |
| 1024 | return true; |
| 1025 | } |
| 1026 | |
| 1027 | @Override |
Alice Yang | 1ebc2db | 2013-03-14 21:21:44 -0700 | [diff] [blame] | 1028 | public String getMessageTransforms(final Message msg) { |
| 1029 | final String domId = mTemplates.getMessageDomId(msg); |
| 1030 | return (domId == null) ? null : mMessageTransforms.get(domId); |
| 1031 | } |
| 1032 | |
Andy Huang | 3233bff | 2012-03-20 19:38:45 -0700 | [diff] [blame] | 1033 | // END message header callbacks |
Andy Huang | 5ff6374 | 2012-03-16 20:30:23 -0700 | [diff] [blame] | 1034 | |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 1035 | @Override |
Andrew Sapperstein | 2fc6730 | 2013-04-29 18:24:56 -0700 | [diff] [blame] | 1036 | public void showUntransformedConversation() { |
| 1037 | super.showUntransformedConversation(); |
| 1038 | renderConversation(getMessageCursor()); |
| 1039 | } |
| 1040 | |
| 1041 | @Override |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 1042 | public void onSuperCollapsedClick(SuperCollapsedBlockItem item) { |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 1043 | MessageCursor cursor = getMessageCursor(); |
| 1044 | if (cursor == null || !mViewsCreated) { |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 1045 | return; |
| 1046 | } |
| 1047 | |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 1048 | mTempBodiesHtml = renderCollapsedHeaders(cursor, item); |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 1049 | mWebView.loadUrl("javascript:replaceSuperCollapsedBlock(" + item.getStart() + ")"); |
| 1050 | } |
| 1051 | |
Andy Huang | 47aa9c9 | 2012-07-31 15:37:21 -0700 | [diff] [blame] | 1052 | private void showNewMessageNotification(NewMessagesInfo info) { |
Andrew Sapperstein | 821fa87 | 2013-08-21 21:57:39 -0700 | [diff] [blame] | 1053 | mNewMessageBar.setText(info.getNotificationText()); |
Andy Huang | 47aa9c9 | 2012-07-31 15:37:21 -0700 | [diff] [blame] | 1054 | mNewMessageBar.setVisibility(View.VISIBLE); |
| 1055 | } |
| 1056 | |
| 1057 | private void onNewMessageBarClick() { |
| 1058 | mNewMessageBar.setVisibility(View.GONE); |
| 1059 | |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 1060 | renderConversation(getMessageCursor()); // mCursor is already up-to-date |
| 1061 | // per onLoadFinished() |
Andy Huang | 5fbda02 | 2012-02-28 18:22:03 -0800 | [diff] [blame] | 1062 | } |
| 1063 | |
Andy Huang | adbf3e8 | 2012-10-13 13:30:19 -0700 | [diff] [blame] | 1064 | private static OverlayPosition[] parsePositions(final String[] topArray, |
| 1065 | final String[] bottomArray) { |
| 1066 | final int len = topArray.length; |
| 1067 | final OverlayPosition[] positions = new OverlayPosition[len]; |
Andy Huang | b5078b2 | 2012-03-05 19:52:29 -0800 | [diff] [blame] | 1068 | for (int i = 0; i < len; i++) { |
Andy Huang | adbf3e8 | 2012-10-13 13:30:19 -0700 | [diff] [blame] | 1069 | positions[i] = new OverlayPosition( |
| 1070 | Integer.parseInt(topArray[i]), Integer.parseInt(bottomArray[i])); |
Andy Huang | b5078b2 | 2012-03-05 19:52:29 -0800 | [diff] [blame] | 1071 | } |
Andy Huang | adbf3e8 | 2012-10-13 13:30:19 -0700 | [diff] [blame] | 1072 | return positions; |
Andy Huang | b5078b2 | 2012-03-05 19:52:29 -0800 | [diff] [blame] | 1073 | } |
| 1074 | |
Andrew Sapperstein | 9f957f3 | 2013-07-19 15:18:18 -0700 | [diff] [blame] | 1075 | protected Address getAddress(String rawFrom) { |
Paul Westbrook | 0dfae69 | 2013-10-02 00:51:29 -0700 | [diff] [blame] | 1076 | return Utils.getAddress(mAddressCache, rawFrom); |
Andy Huang | 1617481 | 2012-08-16 16:40:35 -0700 | [diff] [blame] | 1077 | } |
| 1078 | |
Andy Huang | 9d3fd92 | 2012-09-26 22:23:58 -0700 | [diff] [blame] | 1079 | private void ensureContentSizeChangeListener() { |
| 1080 | if (mWebViewSizeChangeListener == null) { |
Andy Huang | c1fb9a9 | 2013-02-11 13:09:12 -0800 | [diff] [blame] | 1081 | mWebViewSizeChangeListener = new ContentSizeChangeListener() { |
Andy Huang | 9d3fd92 | 2012-09-26 22:23:58 -0700 | [diff] [blame] | 1082 | @Override |
| 1083 | public void onHeightChange(int h) { |
| 1084 | // When WebKit says the DOM height has changed, re-measure |
| 1085 | // bodies and re-position their headers. |
| 1086 | // This is separate from the typical JavaScript DOM change |
| 1087 | // listeners because cases like NARROW_COLUMNS text reflow do not trigger DOM |
| 1088 | // events. |
| 1089 | mWebView.loadUrl("javascript:measurePositions();"); |
| 1090 | } |
| 1091 | }; |
| 1092 | } |
| 1093 | mWebView.setContentSizeChangeListener(mWebViewSizeChangeListener); |
| 1094 | } |
| 1095 | |
Andrew Sapperstein | 9f957f3 | 2013-07-19 15:18:18 -0700 | [diff] [blame] | 1096 | public static boolean isOverviewMode(Account acct) { |
Andy Huang | ccf6780 | 2013-03-15 14:31:57 -0700 | [diff] [blame] | 1097 | return acct.settings.isOverviewMode(); |
Andy Huang | adbf3e8 | 2012-10-13 13:30:19 -0700 | [diff] [blame] | 1098 | } |
| 1099 | |
| 1100 | private void setupOverviewMode() { |
Andy Huang | 02f9d18 | 2012-11-28 22:38:02 -0800 | [diff] [blame] | 1101 | // for now, overview mode means use the built-in WebView zoom and disable custom scale |
| 1102 | // gesture handling |
Andy Huang | adbf3e8 | 2012-10-13 13:30:19 -0700 | [diff] [blame] | 1103 | final boolean overviewMode = isOverviewMode(mAccount); |
| 1104 | final WebSettings settings = mWebView.getSettings(); |
Andy Huang | 06def56 | 2012-10-14 00:19:11 -0700 | [diff] [blame] | 1105 | settings.setUseWideViewPort(overviewMode); |
Andy Huang | 57f354c | 2013-04-11 17:23:40 -0700 | [diff] [blame] | 1106 | settings.setSupportZoom(overviewMode); |
| 1107 | settings.setBuiltInZoomControls(overviewMode); |
| 1108 | if (overviewMode) { |
| 1109 | settings.setDisplayZoomControls(false); |
Andy Huang | adbf3e8 | 2012-10-13 13:30:19 -0700 | [diff] [blame] | 1110 | } |
| 1111 | } |
| 1112 | |
Andrew Sapperstein | b1d184d | 2013-08-09 14:14:31 -0700 | [diff] [blame] | 1113 | public class ConversationWebViewClient extends AbstractConversationWebViewClient { |
Andrew Sapperstein | 4ddda2f | 2013-06-10 11:15:38 -0700 | [diff] [blame] | 1114 | public ConversationWebViewClient(Account account) { |
| 1115 | super(account); |
Andrew Sapperstein | 376294b | 2013-06-06 16:04:26 -0700 | [diff] [blame] | 1116 | } |
| 1117 | |
Andy Huang | 17a9cde | 2012-03-09 18:03:16 -0800 | [diff] [blame] | 1118 | @Override |
| 1119 | public void onPageFinished(WebView view, String url) { |
Andy Huang | 9a8bc1e | 2012-10-23 19:48:25 -0700 | [diff] [blame] | 1120 | // Ignore unsafe calls made after a fragment is detached from an activity. |
| 1121 | // This method needs to, for example, get at the loader manager, which needs |
| 1122 | // the fragment to be added. |
Andy Huang | 9a8bc1e | 2012-10-23 19:48:25 -0700 | [diff] [blame] | 1123 | if (!isAdded() || !mViewsCreated) { |
Paul Westbrook | 006e13c | 2013-07-24 18:40:20 -0700 | [diff] [blame] | 1124 | LogUtils.d(LOG_TAG, "ignoring CVF.onPageFinished, url=%s fragment=%s", url, |
Andy Huang | b95da85 | 2012-07-18 14:16:58 -0700 | [diff] [blame] | 1125 | ConversationViewFragment.this); |
| 1126 | return; |
| 1127 | } |
| 1128 | |
Paul Westbrook | 006e13c | 2013-07-24 18:40:20 -0700 | [diff] [blame] | 1129 | LogUtils.d(LOG_TAG, "IN CVF.onPageFinished, url=%s fragment=%s wv=%s t=%sms", url, |
Andy Huang | 30bcfe7 | 2012-10-18 18:09:03 -0700 | [diff] [blame] | 1130 | ConversationViewFragment.this, view, |
Andy Huang | 63b3c67 | 2012-10-05 19:27:28 -0700 | [diff] [blame] | 1131 | (SystemClock.uptimeMillis() - mWebViewLoadStartMs)); |
Andy Huang | 632721e | 2012-04-11 16:57:26 -0700 | [diff] [blame] | 1132 | |
Andy Huang | 9d3fd92 | 2012-09-26 22:23:58 -0700 | [diff] [blame] | 1133 | ensureContentSizeChangeListener(); |
| 1134 | |
mindyp | 3bcf180 | 2012-09-09 11:17:00 -0700 | [diff] [blame] | 1135 | if (!mEnableContentReadySignal) { |
Andy Huang | 7d4746e | 2012-10-17 17:03:17 -0700 | [diff] [blame] | 1136 | revealConversation(); |
mindyp | 3bcf180 | 2012-09-09 11:17:00 -0700 | [diff] [blame] | 1137 | } |
Andy Huang | 9d3fd92 | 2012-09-26 22:23:58 -0700 | [diff] [blame] | 1138 | |
Andy Huang | 9a8bc1e | 2012-10-23 19:48:25 -0700 | [diff] [blame] | 1139 | final Set<String> emailAddresses = Sets.newHashSet(); |
Andy Huang | 543e709 | 2013-04-22 11:44:56 -0700 | [diff] [blame] | 1140 | final List<Address> cacheCopy; |
| 1141 | synchronized (mAddressCache) { |
| 1142 | cacheCopy = ImmutableList.copyOf(mAddressCache.values()); |
| 1143 | } |
| 1144 | for (Address addr : cacheCopy) { |
Andy Huang | 9a8bc1e | 2012-10-23 19:48:25 -0700 | [diff] [blame] | 1145 | emailAddresses.add(addr.getAddress()); |
Andy Huang | b8331b4 | 2012-07-16 19:08:53 -0700 | [diff] [blame] | 1146 | } |
Andrew Sapperstein | 4ddda2f | 2013-06-10 11:15:38 -0700 | [diff] [blame] | 1147 | final ContactLoaderCallbacks callbacks = getContactInfoSource(); |
| 1148 | callbacks.setSenders(emailAddresses); |
Andy Huang | 9a8bc1e | 2012-10-23 19:48:25 -0700 | [diff] [blame] | 1149 | getLoaderManager().restartLoader(CONTACT_LOADER, Bundle.EMPTY, callbacks); |
Andy Huang | 17a9cde | 2012-03-09 18:03:16 -0800 | [diff] [blame] | 1150 | } |
| 1151 | |
Andy Huang | af5d4e0 | 2012-03-19 19:02:12 -0700 | [diff] [blame] | 1152 | @Override |
| 1153 | public boolean shouldOverrideUrlLoading(WebView view, String url) { |
Paul Westbrook | 542fec9 | 2012-09-18 14:47:51 -0700 | [diff] [blame] | 1154 | return mViewsCreated && super.shouldOverrideUrlLoading(view, url); |
Andy Huang | af5d4e0 | 2012-03-19 19:02:12 -0700 | [diff] [blame] | 1155 | } |
Andy Huang | 17a9cde | 2012-03-09 18:03:16 -0800 | [diff] [blame] | 1156 | } |
| 1157 | |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 1158 | /** |
| 1159 | * NOTE: all public methods must be listed in the proguard flags so that they can be accessed |
| 1160 | * via reflection and not stripped. |
| 1161 | * |
| 1162 | */ |
| 1163 | private class MailJsBridge { |
Mindy Pereira | 974c966 | 2012-09-14 10:02:08 -0700 | [diff] [blame] | 1164 | @JavascriptInterface |
Andy Huang | adbf3e8 | 2012-10-13 13:30:19 -0700 | [diff] [blame] | 1165 | public void onWebContentGeometryChange(final String[] overlayTopStrs, |
| 1166 | final String[] overlayBottomStrs) { |
Andrew Sapperstein | 8ec43e8 | 2013-12-17 18:27:55 -0800 | [diff] [blame^] | 1167 | try { |
| 1168 | getHandler().post(new FragmentRunnable("onWebContentGeometryChange", |
| 1169 | ConversationViewFragment.this) { |
| 1170 | @Override |
| 1171 | public void go() { |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 1172 | if (!mViewsCreated) { |
mindyp | 1b3cc47 | 2012-09-27 11:32:59 -0700 | [diff] [blame] | 1173 | LogUtils.d(LOG_TAG, "ignoring webContentGeometryChange because views" |
| 1174 | + " are gone, %s", ConversationViewFragment.this); |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 1175 | return; |
| 1176 | } |
Andy Huang | adbf3e8 | 2012-10-13 13:30:19 -0700 | [diff] [blame] | 1177 | mConversationContainer.onGeometryChange( |
| 1178 | parsePositions(overlayTopStrs, overlayBottomStrs)); |
mindyp | 1b3cc47 | 2012-09-27 11:32:59 -0700 | [diff] [blame] | 1179 | if (mDiff != 0) { |
| 1180 | // SCROLL! |
| 1181 | int scale = (int) (mWebView.getScale() / mWebView.getInitialScale()); |
| 1182 | if (scale > 1) { |
| 1183 | mWebView.scrollBy(0, (mDiff * (scale - 1))); |
| 1184 | } |
| 1185 | mDiff = 0; |
| 1186 | } |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 1187 | } |
Andrew Sapperstein | 8ec43e8 | 2013-12-17 18:27:55 -0800 | [diff] [blame^] | 1188 | }); |
| 1189 | } catch (Throwable t) { |
| 1190 | LogUtils.e(LOG_TAG, t, "Error in MailJsBridge.onWebContentGeometryChange"); |
| 1191 | } |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 1192 | } |
| 1193 | |
Mindy Pereira | 974c966 | 2012-09-14 10:02:08 -0700 | [diff] [blame] | 1194 | @JavascriptInterface |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 1195 | public String getTempMessageBodies() { |
| 1196 | try { |
| 1197 | if (!mViewsCreated) { |
| 1198 | return ""; |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 1199 | } |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 1200 | |
| 1201 | final String s = mTempBodiesHtml; |
| 1202 | mTempBodiesHtml = null; |
| 1203 | return s; |
| 1204 | } catch (Throwable t) { |
| 1205 | LogUtils.e(LOG_TAG, t, "Error in MailJsBridge.getTempMessageBodies"); |
| 1206 | return ""; |
| 1207 | } |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 1208 | } |
| 1209 | |
Andy Huang | 014ea4c | 2012-09-25 14:50:54 -0700 | [diff] [blame] | 1210 | @JavascriptInterface |
| 1211 | public String getMessageBody(String domId) { |
| 1212 | try { |
| 1213 | final MessageCursor cursor = getMessageCursor(); |
| 1214 | if (!mViewsCreated || cursor == null) { |
| 1215 | return ""; |
| 1216 | } |
| 1217 | |
| 1218 | int pos = -1; |
| 1219 | while (cursor.moveToPosition(++pos)) { |
| 1220 | final ConversationMessage msg = cursor.getMessage(); |
| 1221 | if (TextUtils.equals(domId, mTemplates.getMessageDomId(msg))) { |
| 1222 | return msg.getBodyAsHtml(); |
| 1223 | } |
| 1224 | } |
| 1225 | |
| 1226 | return ""; |
| 1227 | |
| 1228 | } catch (Throwable t) { |
| 1229 | LogUtils.e(LOG_TAG, t, "Error in MailJsBridge.getMessageBody"); |
| 1230 | return ""; |
| 1231 | } |
| 1232 | } |
| 1233 | |
Mindy Pereira | 974c966 | 2012-09-14 10:02:08 -0700 | [diff] [blame] | 1234 | @JavascriptInterface |
Andy Huang | 543e709 | 2013-04-22 11:44:56 -0700 | [diff] [blame] | 1235 | public String getMessageSender(String domId) { |
| 1236 | try { |
| 1237 | final MessageCursor cursor = getMessageCursor(); |
| 1238 | if (!mViewsCreated || cursor == null) { |
| 1239 | return ""; |
| 1240 | } |
| 1241 | |
| 1242 | int pos = -1; |
| 1243 | while (cursor.moveToPosition(++pos)) { |
| 1244 | final ConversationMessage msg = cursor.getMessage(); |
| 1245 | if (TextUtils.equals(domId, mTemplates.getMessageDomId(msg))) { |
| 1246 | return getAddress(msg.getFrom()).getAddress(); |
| 1247 | } |
| 1248 | } |
| 1249 | |
| 1250 | return ""; |
| 1251 | |
| 1252 | } catch (Throwable t) { |
| 1253 | LogUtils.e(LOG_TAG, t, "Error in MailJsBridge.getMessageSender"); |
| 1254 | return ""; |
| 1255 | } |
| 1256 | } |
| 1257 | |
Andy Huang | 543e709 | 2013-04-22 11:44:56 -0700 | [diff] [blame] | 1258 | @JavascriptInterface |
mindyp | 3bcf180 | 2012-09-09 11:17:00 -0700 | [diff] [blame] | 1259 | public void onContentReady() { |
Andrew Sapperstein | 8ec43e8 | 2013-12-17 18:27:55 -0800 | [diff] [blame^] | 1260 | try { |
| 1261 | getHandler().post(new FragmentRunnable("onContentReady", |
| 1262 | ConversationViewFragment.this) { |
| 1263 | @Override |
| 1264 | public void go() { |
| 1265 | try { |
| 1266 | if (mWebViewLoadStartMs != 0) { |
| 1267 | LogUtils.i(LOG_TAG, "IN CVF.onContentReady, f=%s vis=%s t=%sms", |
| 1268 | ConversationViewFragment.this, |
| 1269 | isUserVisible(), |
| 1270 | (SystemClock.uptimeMillis() - mWebViewLoadStartMs)); |
| 1271 | } |
| 1272 | revealConversation(); |
| 1273 | } catch (Throwable t) { |
| 1274 | LogUtils.e(LOG_TAG, t, "Error in MailJsBridge.onContentReady"); |
| 1275 | // Still try to show the conversation. |
| 1276 | revealConversation(); |
Andy Huang | 63b3c67 | 2012-10-05 19:27:28 -0700 | [diff] [blame] | 1277 | } |
mindyp | 3bcf180 | 2012-09-09 11:17:00 -0700 | [diff] [blame] | 1278 | } |
Andrew Sapperstein | 8ec43e8 | 2013-12-17 18:27:55 -0800 | [diff] [blame^] | 1279 | }); |
| 1280 | } catch (Throwable t) { |
| 1281 | LogUtils.e(LOG_TAG, t, "Error in MailJsBridge.onContentReady"); |
| 1282 | } |
mindyp | 3bcf180 | 2012-09-09 11:17:00 -0700 | [diff] [blame] | 1283 | } |
Andy Huang | e964eee | 2012-10-02 19:24:58 -0700 | [diff] [blame] | 1284 | |
Andy Huang | e964eee | 2012-10-02 19:24:58 -0700 | [diff] [blame] | 1285 | @JavascriptInterface |
| 1286 | public float getScrollYPercent() { |
| 1287 | try { |
| 1288 | return mWebViewYPercent; |
| 1289 | } catch (Throwable t) { |
| 1290 | LogUtils.e(LOG_TAG, t, "Error in MailJsBridge.getScrollYPercent"); |
| 1291 | return 0f; |
| 1292 | } |
| 1293 | } |
Andy Huang | 05c70c8 | 2013-03-14 15:15:50 -0700 | [diff] [blame] | 1294 | |
Andy Huang | 05c70c8 | 2013-03-14 15:15:50 -0700 | [diff] [blame] | 1295 | @JavascriptInterface |
| 1296 | public void onMessageTransform(String messageDomId, String transformText) { |
Andrew Sapperstein | ae92e15 | 2013-05-03 13:55:18 -0700 | [diff] [blame] | 1297 | try { |
| 1298 | LogUtils.i(LOG_TAG, "TRANSFORM: (%s) %s", messageDomId, transformText); |
| 1299 | mMessageTransforms.put(messageDomId, transformText); |
| 1300 | onConversationTransformed(); |
| 1301 | } catch (Throwable t) { |
| 1302 | LogUtils.e(LOG_TAG, t, "Error in MailJsBridge.onMessageTransform"); |
Andrew Sapperstein | 8ec43e8 | 2013-12-17 18:27:55 -0800 | [diff] [blame^] | 1303 | } |
| 1304 | } |
| 1305 | |
| 1306 | @JavascriptInterface |
| 1307 | public void onInlineAttachmentsParsed(final String[] urls, final String[] messageIds) { |
| 1308 | try { |
| 1309 | getHandler().post(new FragmentRunnable("onInlineAttachmentsParsed", |
| 1310 | ConversationViewFragment.this) { |
| 1311 | @Override |
| 1312 | public void go() { |
| 1313 | try { |
| 1314 | for (int i = 0, size = urls.length; i < size; i++) { |
| 1315 | mUrlToMessageIdMap.put(urls[i], messageIds[i]); |
| 1316 | } |
| 1317 | } catch (ArrayIndexOutOfBoundsException e) { |
| 1318 | LogUtils.e(LOG_TAG, e, |
| 1319 | "Number of urls does not match number of message ids - %s:%s", |
| 1320 | urls.length, messageIds.length); |
| 1321 | } |
| 1322 | } |
| 1323 | }); |
| 1324 | } catch (Throwable t) { |
| 1325 | LogUtils.e(LOG_TAG, t, "Error in MailJsBridge.onInlineAttachmentsParsed"); |
Andrew Sapperstein | ae92e15 | 2013-05-03 13:55:18 -0700 | [diff] [blame] | 1326 | } |
Andy Huang | 05c70c8 | 2013-03-14 15:15:50 -0700 | [diff] [blame] | 1327 | } |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 1328 | } |
| 1329 | |
Andy Huang | 47aa9c9 | 2012-07-31 15:37:21 -0700 | [diff] [blame] | 1330 | private class NewMessagesInfo { |
| 1331 | int count; |
Andy Huang | 06c0362 | 2012-10-22 18:59:45 -0700 | [diff] [blame] | 1332 | int countFromSelf; |
Andy Huang | 47aa9c9 | 2012-07-31 15:37:21 -0700 | [diff] [blame] | 1333 | String senderAddress; |
| 1334 | |
| 1335 | /** |
| 1336 | * Return the display text for the new message notification overlay. It will be formatted |
| 1337 | * appropriately for a single new message vs. multiple new messages. |
| 1338 | * |
| 1339 | * @return display text |
| 1340 | */ |
| 1341 | public String getNotificationText() { |
mindyp | ad0c30d | 2012-09-25 12:09:13 -0700 | [diff] [blame] | 1342 | Resources res = getResources(); |
Andy Huang | 47aa9c9 | 2012-07-31 15:37:21 -0700 | [diff] [blame] | 1343 | if (count > 1) { |
Andrew Sapperstein | 66d6911 | 2013-08-23 12:24:44 -0700 | [diff] [blame] | 1344 | return res.getQuantityString(R.plurals.new_incoming_messages_many, count, count); |
Andy Huang | 47aa9c9 | 2012-07-31 15:37:21 -0700 | [diff] [blame] | 1345 | } else { |
Andy Huang | 1617481 | 2012-08-16 16:40:35 -0700 | [diff] [blame] | 1346 | final Address addr = getAddress(senderAddress); |
mindyp | ad0c30d | 2012-09-25 12:09:13 -0700 | [diff] [blame] | 1347 | return res.getString(R.string.new_incoming_messages_one, |
Andrew Sapperstein | 3af481c | 2013-10-30 10:29:38 -0700 | [diff] [blame] | 1348 | sBidiFormatter.unicodeWrap(TextUtils.isEmpty(addr.getName()) |
| 1349 | ? addr.getAddress() : addr.getName())); |
Andy Huang | 47aa9c9 | 2012-07-31 15:37:21 -0700 | [diff] [blame] | 1350 | } |
Andy Huang | 47aa9c9 | 2012-07-31 15:37:21 -0700 | [diff] [blame] | 1351 | } |
| 1352 | } |
| 1353 | |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 1354 | @Override |
Paul Westbrook | c42ad5e | 2013-05-09 16:52:15 -0700 | [diff] [blame] | 1355 | public void onMessageCursorLoadFinished(Loader<ObjectCursor<ConversationMessage>> loader, |
| 1356 | MessageCursor newCursor, MessageCursor oldCursor) { |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 1357 | /* |
| 1358 | * what kind of changes affect the MessageCursor? 1. new message(s) 2. |
| 1359 | * read/unread state change 3. deleted message, either regular or draft |
| 1360 | * 4. updated message, either from self or from others, updated in |
| 1361 | * content or state or sender 5. star/unstar of message (technically |
| 1362 | * similar to #1) 6. other label change Use MessageCursor.hashCode() to |
| 1363 | * sort out interesting vs. no-op cursor updates. |
| 1364 | */ |
Andy Huang | b8331b4 | 2012-07-16 19:08:53 -0700 | [diff] [blame] | 1365 | |
Andy Huang | 233d435 | 2012-10-18 14:00:24 -0700 | [diff] [blame] | 1366 | if (oldCursor != null && !oldCursor.isClosed()) { |
Andy Huang | 014ea4c | 2012-09-25 14:50:54 -0700 | [diff] [blame] | 1367 | final NewMessagesInfo info = getNewIncomingMessagesInfo(newCursor); |
Andy Huang | b8331b4 | 2012-07-16 19:08:53 -0700 | [diff] [blame] | 1368 | |
Andy Huang | 014ea4c | 2012-09-25 14:50:54 -0700 | [diff] [blame] | 1369 | if (info.count > 0) { |
| 1370 | // don't immediately render new incoming messages from other |
| 1371 | // senders |
| 1372 | // (to avoid a new message from losing the user's focus) |
| 1373 | LogUtils.i(LOG_TAG, "CONV RENDER: conversation updated" |
Andy Huang | 9d3fd92 | 2012-09-26 22:23:58 -0700 | [diff] [blame] | 1374 | + ", holding cursor for new incoming message (%s)", this); |
Andy Huang | 014ea4c | 2012-09-25 14:50:54 -0700 | [diff] [blame] | 1375 | showNewMessageNotification(info); |
| 1376 | return; |
| 1377 | } |
| 1378 | |
Andy Huang | 06c0362 | 2012-10-22 18:59:45 -0700 | [diff] [blame] | 1379 | final int oldState = oldCursor.getStateHashCode(); |
| 1380 | final boolean changed = newCursor.getStateHashCode() != oldState; |
Andy Huang | 233d435 | 2012-10-18 14:00:24 -0700 | [diff] [blame] | 1381 | |
Andy Huang | 014ea4c | 2012-09-25 14:50:54 -0700 | [diff] [blame] | 1382 | if (!changed) { |
| 1383 | final boolean processedInPlace = processInPlaceUpdates(newCursor, oldCursor); |
| 1384 | if (processedInPlace) { |
Andy Huang | 9d3fd92 | 2012-09-26 22:23:58 -0700 | [diff] [blame] | 1385 | LogUtils.i(LOG_TAG, "CONV RENDER: processed update(s) in place (%s)", this); |
Andy Huang | 1ee96b2 | 2012-08-24 20:19:53 -0700 | [diff] [blame] | 1386 | } else { |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 1387 | LogUtils.i(LOG_TAG, "CONV RENDER: uninteresting update" |
Andy Huang | 9d3fd92 | 2012-09-26 22:23:58 -0700 | [diff] [blame] | 1388 | + ", ignoring this conversation update (%s)", this); |
Andy Huang | 1ee96b2 | 2012-08-24 20:19:53 -0700 | [diff] [blame] | 1389 | } |
Andy Huang | b8331b4 | 2012-07-16 19:08:53 -0700 | [diff] [blame] | 1390 | return; |
Andy Huang | 06c0362 | 2012-10-22 18:59:45 -0700 | [diff] [blame] | 1391 | } else if (info.countFromSelf == 1) { |
| 1392 | // Special-case the very common case of a new cursor that is the same as the old |
| 1393 | // one, except that there is a new message from yourself. This happens upon send. |
| 1394 | final boolean sameExceptNewLast = newCursor.getStateHashCode(1) == oldState; |
| 1395 | if (sameExceptNewLast) { |
| 1396 | LogUtils.i(LOG_TAG, "CONV RENDER: update is a single new message from self" |
| 1397 | + " (%s)", this); |
| 1398 | newCursor.moveToLast(); |
| 1399 | processNewOutgoingMessage(newCursor.getMessage()); |
| 1400 | return; |
| 1401 | } |
Andy Huang | b8331b4 | 2012-07-16 19:08:53 -0700 | [diff] [blame] | 1402 | } |
Andy Huang | 6766b6e | 2012-09-28 12:43:52 -0700 | [diff] [blame] | 1403 | // cursors are different, and not due to an incoming message. fall |
| 1404 | // through and render. |
| 1405 | LogUtils.i(LOG_TAG, "CONV RENDER: conversation updated" |
| 1406 | + ", but not due to incoming message. rendering. (%s)", this); |
Andy Huang | 06c0362 | 2012-10-22 18:59:45 -0700 | [diff] [blame] | 1407 | |
| 1408 | if (DEBUG_DUMP_CURSOR_CONTENTS) { |
| 1409 | LogUtils.i(LOG_TAG, "old cursor: %s", oldCursor.getDebugDump()); |
| 1410 | LogUtils.i(LOG_TAG, "new cursor: %s", newCursor.getDebugDump()); |
| 1411 | } |
Andy Huang | 6766b6e | 2012-09-28 12:43:52 -0700 | [diff] [blame] | 1412 | } else { |
| 1413 | LogUtils.i(LOG_TAG, "CONV RENDER: initial render. (%s)", this); |
Andy Huang | 243c236 | 2013-03-01 17:50:35 -0800 | [diff] [blame] | 1414 | timerMark("message cursor load finished"); |
Andy Huang | b8331b4 | 2012-07-16 19:08:53 -0700 | [diff] [blame] | 1415 | } |
| 1416 | |
Andrew Sapperstein | 606dbd7 | 2013-07-30 19:14:23 -0700 | [diff] [blame] | 1417 | renderContent(newCursor); |
| 1418 | } |
| 1419 | |
| 1420 | protected void renderContent(MessageCursor messageCursor) { |
Mark Wei | 4071c2f | 2012-09-26 14:38:38 -0700 | [diff] [blame] | 1421 | // if layout hasn't happened, delay render |
| 1422 | // This is needed in addition to the showConversation() delay to speed |
| 1423 | // up rotation and restoration. |
| 1424 | if (mConversationContainer.getWidth() == 0) { |
| 1425 | mNeedRender = true; |
| 1426 | mConversationContainer.addOnLayoutChangeListener(this); |
| 1427 | } else { |
Andrew Sapperstein | 606dbd7 | 2013-07-30 19:14:23 -0700 | [diff] [blame] | 1428 | renderConversation(messageCursor); |
Mark Wei | 4071c2f | 2012-09-26 14:38:38 -0700 | [diff] [blame] | 1429 | } |
Andy Huang | b8331b4 | 2012-07-16 19:08:53 -0700 | [diff] [blame] | 1430 | } |
| 1431 | |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 1432 | private NewMessagesInfo getNewIncomingMessagesInfo(MessageCursor newCursor) { |
| 1433 | final NewMessagesInfo info = new NewMessagesInfo(); |
Andy Huang | b8331b4 | 2012-07-16 19:08:53 -0700 | [diff] [blame] | 1434 | |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 1435 | int pos = -1; |
| 1436 | while (newCursor.moveToPosition(++pos)) { |
| 1437 | final Message m = newCursor.getMessage(); |
| 1438 | if (!mViewState.contains(m)) { |
| 1439 | LogUtils.i(LOG_TAG, "conversation diff: found new msg: %s", m.uri); |
Andy Huang | b8331b4 | 2012-07-16 19:08:53 -0700 | [diff] [blame] | 1440 | |
Scott Kennedy | 8960f0a | 2012-11-07 15:35:50 -0800 | [diff] [blame] | 1441 | final Address from = getAddress(m.getFrom()); |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 1442 | // distinguish ours from theirs |
| 1443 | // new messages from the account owner should not trigger a |
| 1444 | // notification |
| 1445 | if (mAccount.ownsFromAddress(from.getAddress())) { |
| 1446 | LogUtils.i(LOG_TAG, "found message from self: %s", m.uri); |
Andy Huang | 06c0362 | 2012-10-22 18:59:45 -0700 | [diff] [blame] | 1447 | info.countFromSelf++; |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 1448 | continue; |
| 1449 | } |
Andy Huang | b8331b4 | 2012-07-16 19:08:53 -0700 | [diff] [blame] | 1450 | |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 1451 | info.count++; |
Scott Kennedy | 8960f0a | 2012-11-07 15:35:50 -0800 | [diff] [blame] | 1452 | info.senderAddress = m.getFrom(); |
Andy Huang | b8331b4 | 2012-07-16 19:08:53 -0700 | [diff] [blame] | 1453 | } |
Andy Huang | b8331b4 | 2012-07-16 19:08:53 -0700 | [diff] [blame] | 1454 | } |
mindyp | f4fce12 | 2012-09-14 15:55:33 -0700 | [diff] [blame] | 1455 | return info; |
Andy Huang | b8331b4 | 2012-07-16 19:08:53 -0700 | [diff] [blame] | 1456 | } |
| 1457 | |
Andy Huang | 014ea4c | 2012-09-25 14:50:54 -0700 | [diff] [blame] | 1458 | private boolean processInPlaceUpdates(MessageCursor newCursor, MessageCursor oldCursor) { |
| 1459 | final Set<String> idsOfChangedBodies = Sets.newHashSet(); |
Andy Huang | 6b3d0d9 | 2012-10-30 15:46:48 -0700 | [diff] [blame] | 1460 | final List<Integer> changedOverlayPositions = Lists.newArrayList(); |
| 1461 | |
Andy Huang | 014ea4c | 2012-09-25 14:50:54 -0700 | [diff] [blame] | 1462 | boolean changed = false; |
| 1463 | |
| 1464 | int pos = 0; |
| 1465 | while (true) { |
| 1466 | if (!newCursor.moveToPosition(pos) || !oldCursor.moveToPosition(pos)) { |
| 1467 | break; |
| 1468 | } |
| 1469 | |
| 1470 | final ConversationMessage newMsg = newCursor.getMessage(); |
| 1471 | final ConversationMessage oldMsg = oldCursor.getMessage(); |
| 1472 | |
Scott Kennedy | 8960f0a | 2012-11-07 15:35:50 -0800 | [diff] [blame] | 1473 | if (!TextUtils.equals(newMsg.getFrom(), oldMsg.getFrom()) || |
Andy Huang | 2a1e8e3 | 2012-10-23 18:54:57 -0700 | [diff] [blame] | 1474 | newMsg.isSending != oldMsg.isSending) { |
Andy Huang | 6b3d0d9 | 2012-10-30 15:46:48 -0700 | [diff] [blame] | 1475 | mAdapter.updateItemsForMessage(newMsg, changedOverlayPositions); |
Andy Huang | 2a1e8e3 | 2012-10-23 18:54:57 -0700 | [diff] [blame] | 1476 | LogUtils.i(LOG_TAG, "msg #%d (%d): detected from/sending change. isSending=%s", |
| 1477 | pos, newMsg.id, newMsg.isSending); |
Andy Huang | 014ea4c | 2012-09-25 14:50:54 -0700 | [diff] [blame] | 1478 | } |
| 1479 | |
| 1480 | // update changed message bodies in-place |
| 1481 | if (!TextUtils.equals(newMsg.bodyHtml, oldMsg.bodyHtml) || |
| 1482 | !TextUtils.equals(newMsg.bodyText, oldMsg.bodyText)) { |
| 1483 | // maybe just set a flag to notify JS to re-request changed bodies |
| 1484 | idsOfChangedBodies.add('"' + mTemplates.getMessageDomId(newMsg) + '"'); |
| 1485 | LogUtils.i(LOG_TAG, "msg #%d (%d): detected body change", pos, newMsg.id); |
| 1486 | } |
| 1487 | |
| 1488 | pos++; |
| 1489 | } |
| 1490 | |
Andy Huang | 6b3d0d9 | 2012-10-30 15:46:48 -0700 | [diff] [blame] | 1491 | |
| 1492 | if (!changedOverlayPositions.isEmpty()) { |
Andy Huang | 06c0362 | 2012-10-22 18:59:45 -0700 | [diff] [blame] | 1493 | // notify once after the entire adapter is updated |
Andy Huang | 6b3d0d9 | 2012-10-30 15:46:48 -0700 | [diff] [blame] | 1494 | mConversationContainer.onOverlayModelUpdate(changedOverlayPositions); |
| 1495 | changed = true; |
Andy Huang | 06c0362 | 2012-10-22 18:59:45 -0700 | [diff] [blame] | 1496 | } |
| 1497 | |
Andy Huang | 014ea4c | 2012-09-25 14:50:54 -0700 | [diff] [blame] | 1498 | if (!idsOfChangedBodies.isEmpty()) { |
| 1499 | mWebView.loadUrl(String.format("javascript:replaceMessageBodies([%s]);", |
| 1500 | TextUtils.join(",", idsOfChangedBodies))); |
| 1501 | changed = true; |
| 1502 | } |
| 1503 | |
| 1504 | return changed; |
| 1505 | } |
| 1506 | |
Andy Huang | 06c0362 | 2012-10-22 18:59:45 -0700 | [diff] [blame] | 1507 | private void processNewOutgoingMessage(ConversationMessage msg) { |
Andrew Sapperstein | 99ee456 | 2013-08-22 16:19:42 -0700 | [diff] [blame] | 1508 | // if there are items in the adapter and the last item is a border, |
| 1509 | // make the last border no longer be the last border |
| 1510 | if (mAdapter.getCount() > 0) { |
| 1511 | final ConversationOverlayItem item = mAdapter.getItem(mAdapter.getCount() - 1); |
| 1512 | if (item.getType() == ConversationViewAdapter.VIEW_TYPE_BORDER) { |
| 1513 | ((BorderItem) item).setIsLastBorder(false); |
| 1514 | } |
| 1515 | } |
Andrew Sapperstein | cee3c90 | 2013-07-31 10:52:02 -0700 | [diff] [blame] | 1516 | |
Andy Huang | 06c0362 | 2012-10-22 18:59:45 -0700 | [diff] [blame] | 1517 | mTemplates.reset(); |
| 1518 | // this method will add some items to mAdapter, but we deliberately want to avoid notifying |
| 1519 | // adapter listeners (i.e. ConversationContainer) until onWebContentGeometryChange is next |
| 1520 | // called, to prevent N+1 headers rendering with N message bodies. |
Andrew Sapperstein | cee3c90 | 2013-07-31 10:52:02 -0700 | [diff] [blame] | 1521 | |
| 1522 | // We can just call previousCollapsed false here since the border |
| 1523 | // above the message we're about to render should always show |
| 1524 | // (which it also will since the message being render is expanded). |
| 1525 | renderMessage(msg, false /* previousCollapsed */, true /* expanded */, |
| 1526 | msg.alwaysShowImages, false /* renderBorder */, false /* firstBorder */); |
| 1527 | renderBorder(true /* contiguous */, true /* expanded */, |
| 1528 | false /* firstBorder */, true /* lastBorder */); |
Andy Huang | 06c0362 | 2012-10-22 18:59:45 -0700 | [diff] [blame] | 1529 | mTempBodiesHtml = mTemplates.emit(); |
| 1530 | |
| 1531 | mViewState.setExpansionState(msg, ExpansionState.EXPANDED); |
| 1532 | // FIXME: should the provider set this as initial state? |
| 1533 | mViewState.setReadState(msg, false /* read */); |
| 1534 | |
Andy Huang | 91d782a | 2012-10-25 12:37:29 -0700 | [diff] [blame] | 1535 | // From now until the updated spacer geometry is returned, the adapter items are mismatched |
| 1536 | // with the existing spacers. Do not let them layout. |
| 1537 | mConversationContainer.invalidateSpacerGeometry(); |
| 1538 | |
Andy Huang | 06c0362 | 2012-10-22 18:59:45 -0700 | [diff] [blame] | 1539 | mWebView.loadUrl("javascript:appendMessageHtml();"); |
| 1540 | } |
| 1541 | |
Paul Westbrook | cebcc64 | 2012-08-08 10:06:04 -0700 | [diff] [blame] | 1542 | private class SetCookieTask extends AsyncTask<Void, Void, Void> { |
| 1543 | final String mUri; |
Paul Westbrook | b8361c9 | 2012-09-27 10:57:14 -0700 | [diff] [blame] | 1544 | final Uri mAccountCookieQueryUri; |
| 1545 | final ContentResolver mResolver; |
Paul Westbrook | cebcc64 | 2012-08-08 10:06:04 -0700 | [diff] [blame] | 1546 | |
Paul Westbrook | b8361c9 | 2012-09-27 10:57:14 -0700 | [diff] [blame] | 1547 | SetCookieTask(Context context, Uri baseUri, Uri accountCookieQueryUri) { |
| 1548 | mUri = baseUri.toString(); |
| 1549 | mAccountCookieQueryUri = accountCookieQueryUri; |
| 1550 | mResolver = context.getContentResolver(); |
Paul Westbrook | cebcc64 | 2012-08-08 10:06:04 -0700 | [diff] [blame] | 1551 | } |
| 1552 | |
| 1553 | @Override |
| 1554 | public Void doInBackground(Void... args) { |
Paul Westbrook | b8361c9 | 2012-09-27 10:57:14 -0700 | [diff] [blame] | 1555 | // First query for the coookie string from the UI provider |
| 1556 | final Cursor cookieCursor = mResolver.query(mAccountCookieQueryUri, |
| 1557 | UIProvider.ACCOUNT_COOKIE_PROJECTION, null, null, null); |
| 1558 | if (cookieCursor == null) { |
| 1559 | return null; |
| 1560 | } |
| 1561 | |
| 1562 | try { |
| 1563 | if (cookieCursor.moveToFirst()) { |
| 1564 | final String cookie = cookieCursor.getString( |
| 1565 | cookieCursor.getColumnIndex(UIProvider.AccountCookieColumns.COOKIE)); |
| 1566 | |
| 1567 | if (cookie != null) { |
| 1568 | final CookieSyncManager csm = |
| 1569 | CookieSyncManager.createInstance(getContext()); |
| 1570 | CookieManager.getInstance().setCookie(mUri, cookie); |
| 1571 | csm.sync(); |
| 1572 | } |
| 1573 | } |
| 1574 | |
| 1575 | } finally { |
| 1576 | cookieCursor.close(); |
| 1577 | } |
| 1578 | |
| 1579 | |
Paul Westbrook | cebcc64 | 2012-08-08 10:06:04 -0700 | [diff] [blame] | 1580 | return null; |
| 1581 | } |
| 1582 | } |
mindyp | 36280f3 | 2012-09-09 16:11:23 -0700 | [diff] [blame] | 1583 | |
mindyp | 26d4d2d | 2012-09-18 17:30:32 -0700 | [diff] [blame] | 1584 | @Override |
mindyp | 36280f3 | 2012-09-09 16:11:23 -0700 | [diff] [blame] | 1585 | public void onConversationUpdated(Conversation conv) { |
| 1586 | final ConversationViewHeader headerView = (ConversationViewHeader) mConversationContainer |
| 1587 | .findViewById(R.id.conversation_header); |
mindyp | b2b98ba | 2012-09-24 14:13:58 -0700 | [diff] [blame] | 1588 | mConversation = conv; |
mindyp | 9e0b236 | 2012-09-09 16:31:21 -0700 | [diff] [blame] | 1589 | if (headerView != null) { |
| 1590 | headerView.onConversationUpdated(conv); |
Vikram Aggarwal | 51ad904 | 2013-01-17 13:18:09 -0800 | [diff] [blame] | 1591 | headerView.setSubject(conv.subject); |
mindyp | 9e0b236 | 2012-09-09 16:31:21 -0700 | [diff] [blame] | 1592 | } |
mindyp | 36280f3 | 2012-09-09 16:11:23 -0700 | [diff] [blame] | 1593 | } |
Mark Wei | 4071c2f | 2012-09-26 14:38:38 -0700 | [diff] [blame] | 1594 | |
| 1595 | @Override |
| 1596 | public void onLayoutChange(View v, int left, int top, int right, |
| 1597 | int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { |
| 1598 | boolean sizeChanged = mNeedRender |
| 1599 | && mConversationContainer.getWidth() != 0; |
| 1600 | if (sizeChanged) { |
| 1601 | mNeedRender = false; |
| 1602 | mConversationContainer.removeOnLayoutChangeListener(this); |
| 1603 | renderConversation(getMessageCursor()); |
| 1604 | } |
| 1605 | } |
mindyp | 1b3cc47 | 2012-09-27 11:32:59 -0700 | [diff] [blame] | 1606 | |
| 1607 | @Override |
| 1608 | public void setMessageDetailsExpanded(MessageHeaderItem i, boolean expanded, |
| 1609 | int heightBefore) { |
| 1610 | mDiff = (expanded ? 1 : -1) * Math.abs(i.getHeight() - heightBefore); |
| 1611 | } |
Andy Huang | 02f9d18 | 2012-11-28 22:38:02 -0800 | [diff] [blame] | 1612 | |
Andrew Sapperstein | 5c1692a | 2013-09-16 11:56:13 -0700 | [diff] [blame] | 1613 | protected void printConversation() { |
Andrew Sapperstein | 234d353 | 2013-10-29 14:54:04 -0700 | [diff] [blame] | 1614 | PrintUtils.printConversation(mActivity.getActivityContext(), getMessageCursor(), |
| 1615 | mAddressCache, mConversation.getBaseUri(mBaseUri), true /* useJavascript */); |
Andrew Sapperstein | 5c1692a | 2013-09-16 11:56:13 -0700 | [diff] [blame] | 1616 | } |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 1617 | } |