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