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