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