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