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 | |
| 20 | import android.app.Activity; |
| 21 | import android.app.Fragment; |
Mindy Pereira | 8e91572 | 2012-02-16 14:42:56 -0800 | [diff] [blame] | 22 | import android.app.LoaderManager; |
Andy Huang | af5d4e0 | 2012-03-19 19:02:12 -0700 | [diff] [blame] | 23 | import android.content.ActivityNotFoundException; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 24 | import android.content.Context; |
Mindy Pereira | 465d03d | 2012-02-16 15:17:46 -0800 | [diff] [blame] | 25 | import android.content.CursorLoader; |
Andy Huang | af5d4e0 | 2012-03-19 19:02:12 -0700 | [diff] [blame] | 26 | import android.content.Intent; |
Mindy Pereira | 8e91572 | 2012-02-16 14:42:56 -0800 | [diff] [blame] | 27 | import android.content.Loader; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 28 | import android.database.Cursor; |
Mindy Pereira | 465d03d | 2012-02-16 15:17:46 -0800 | [diff] [blame] | 29 | import android.net.Uri; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 30 | import android.os.Bundle; |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 31 | import android.os.Handler; |
Andy Huang | af5d4e0 | 2012-03-19 19:02:12 -0700 | [diff] [blame] | 32 | import android.provider.Browser; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 33 | import android.view.LayoutInflater; |
Andy Huang | 5ff6374 | 2012-03-16 20:30:23 -0700 | [diff] [blame] | 34 | import android.view.Menu; |
| 35 | import android.view.MenuInflater; |
| 36 | import android.view.MenuItem; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 37 | import android.view.View; |
| 38 | import android.view.ViewGroup; |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 39 | import android.webkit.ConsoleMessage; |
| 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; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 44 | |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 45 | import com.android.mail.R; |
Andy Huang | 5ff6374 | 2012-03-16 20:30:23 -0700 | [diff] [blame] | 46 | import com.android.mail.browse.ConversationContainer; |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame^] | 47 | import com.android.mail.browse.ConversationOverlayItem; |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 48 | import com.android.mail.browse.ConversationViewAdapter; |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame^] | 49 | import com.android.mail.browse.ConversationViewAdapter.MessageFooterItem; |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 50 | import com.android.mail.browse.ConversationViewAdapter.MessageHeaderItem; |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame^] | 51 | import com.android.mail.browse.ConversationViewAdapter.SuperCollapsedBlockItem; |
Andy Huang | 5ff6374 | 2012-03-16 20:30:23 -0700 | [diff] [blame] | 52 | import com.android.mail.browse.ConversationViewHeader; |
| 53 | import com.android.mail.browse.ConversationWebView; |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 54 | import com.android.mail.browse.MessageCursor; |
Andy Huang | f0e95c3 | 2012-04-02 18:11:17 -0700 | [diff] [blame] | 55 | import com.android.mail.browse.MessageFooterView; |
Andy Huang | 3233bff | 2012-03-20 19:38:45 -0700 | [diff] [blame] | 56 | import com.android.mail.browse.MessageHeaderView.MessageHeaderViewCallbacks; |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame^] | 57 | import com.android.mail.browse.SuperCollapsedBlock; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 58 | import com.android.mail.providers.Account; |
Andy Huang | 65fe28f | 2012-04-06 18:08:53 -0700 | [diff] [blame] | 59 | import com.android.mail.providers.Address; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 60 | import com.android.mail.providers.Conversation; |
Mindy Pereira | 863e441 | 2012-03-23 12:21:38 -0700 | [diff] [blame] | 61 | import com.android.mail.providers.Folder; |
Paul Westbrook | 2d82d61 | 2012-03-07 09:21:14 -0800 | [diff] [blame] | 62 | import com.android.mail.providers.ListParams; |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 63 | import com.android.mail.providers.Message; |
Vikram Aggarwal | 7d81600 | 2012-04-17 17:06:41 -0700 | [diff] [blame] | 64 | import com.android.mail.providers.Settings; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 65 | import com.android.mail.providers.UIProvider; |
Mindy Pereira | 863e441 | 2012-03-23 12:21:38 -0700 | [diff] [blame] | 66 | import com.android.mail.providers.UIProvider.AccountCapabilities; |
| 67 | import com.android.mail.providers.UIProvider.FolderCapabilities; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 68 | import com.android.mail.utils.LogUtils; |
Andy Huang | 2e9acfe | 2012-03-15 22:39:36 -0700 | [diff] [blame] | 69 | import com.android.mail.utils.Utils; |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame^] | 70 | import com.google.common.collect.Lists; |
Andy Huang | 65fe28f | 2012-04-06 18:08:53 -0700 | [diff] [blame] | 71 | import com.google.common.collect.Maps; |
| 72 | |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame^] | 73 | import java.util.List; |
Andy Huang | 65fe28f | 2012-04-06 18:08:53 -0700 | [diff] [blame] | 74 | import java.util.Map; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 75 | |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 76 | |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 77 | /** |
| 78 | * The conversation view UI component. |
| 79 | */ |
Mindy Pereira | 8e91572 | 2012-02-16 14:42:56 -0800 | [diff] [blame] | 80 | public final class ConversationViewFragment extends Fragment implements |
Andy Huang | 5ff6374 | 2012-03-16 20:30:23 -0700 | [diff] [blame] | 81 | LoaderManager.LoaderCallbacks<Cursor>, |
Andy Huang | 3233bff | 2012-03-20 19:38:45 -0700 | [diff] [blame] | 82 | ConversationViewHeader.ConversationViewHeaderCallbacks, |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame^] | 83 | MessageHeaderViewCallbacks, |
| 84 | SuperCollapsedBlock.OnClickListener { |
Mindy Pereira | 8e91572 | 2012-02-16 14:42:56 -0800 | [diff] [blame] | 85 | |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 86 | private static final String LOG_TAG = new LogUtils().getLogTag(); |
| 87 | |
Mindy Pereira | 8e91572 | 2012-02-16 14:42:56 -0800 | [diff] [blame] | 88 | private static final int MESSAGE_LOADER_ID = 0; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 89 | |
Mindy Pereira | 8e91572 | 2012-02-16 14:42:56 -0800 | [diff] [blame] | 90 | private ControllableActivity mActivity; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 91 | |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 92 | private Context mContext; |
| 93 | |
| 94 | private Conversation mConversation; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 95 | |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 96 | private ConversationContainer mConversationContainer; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 97 | |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 98 | private Account mAccount; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 99 | |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 100 | private ConversationWebView mWebView; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 101 | |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 102 | private HtmlConversationTemplates mTemplates; |
| 103 | |
| 104 | private String mBaseUri; |
| 105 | |
| 106 | private final Handler mHandler = new Handler(); |
| 107 | |
| 108 | private final MailJsBridge mJsBridge = new MailJsBridge(); |
| 109 | |
Andy Huang | 17a9cde | 2012-03-09 18:03:16 -0800 | [diff] [blame] | 110 | private final WebViewClient mWebViewClient = new ConversationWebViewClient(); |
| 111 | |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 112 | private ConversationViewAdapter mAdapter; |
| 113 | private MessageCursor mCursor; |
Andy Huang | 5106713 | 2012-03-12 20:08:19 -0700 | [diff] [blame] | 114 | |
| 115 | private boolean mViewsCreated; |
| 116 | |
Andy Huang | 5ff6374 | 2012-03-16 20:30:23 -0700 | [diff] [blame] | 117 | private MenuItem mChangeFoldersMenuItem; |
| 118 | |
| 119 | private float mDensity; |
| 120 | |
Mindy Pereira | 863e441 | 2012-03-23 12:21:38 -0700 | [diff] [blame] | 121 | private Folder mFolder; |
| 122 | |
Andy Huang | 65fe28f | 2012-04-06 18:08:53 -0700 | [diff] [blame] | 123 | private final Map<String, Address> mAddressCache = Maps.newHashMap(); |
| 124 | |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame^] | 125 | /** |
| 126 | * Temporary string containing the message bodies of the messages within a super-collapsed |
| 127 | * block, for one-time use during block expansion. We cannot easily pass the body HTML |
| 128 | * into JS without problematic escaping, so hold onto it momentarily and signal JS to fetch it |
| 129 | * using {@link MailJsBridge}. |
| 130 | */ |
| 131 | private String mTempBodiesHtml; |
| 132 | |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 133 | private static final String ARG_ACCOUNT = "account"; |
| 134 | private static final String ARG_CONVERSATION = "conversation"; |
Mindy Pereira | 863e441 | 2012-03-23 12:21:38 -0700 | [diff] [blame] | 135 | private static final String ARG_FOLDER = "folder"; |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 136 | |
Vikram Aggarwal | 6c51158 | 2012-02-27 10:59:47 -0800 | [diff] [blame] | 137 | /** |
| 138 | * Constructor needs to be public to handle orientation changes and activity lifecycle events. |
| 139 | */ |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 140 | public ConversationViewFragment() { |
Vikram Aggarwal | 6c51158 | 2012-02-27 10:59:47 -0800 | [diff] [blame] | 141 | super(); |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Creates a new instance of {@link ConversationViewFragment}, initialized |
| 146 | * to display conversation. |
| 147 | */ |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 148 | public static ConversationViewFragment newInstance(Account account, |
Mindy Pereira | 863e441 | 2012-03-23 12:21:38 -0700 | [diff] [blame] | 149 | Conversation conversation, Folder folder) { |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 150 | ConversationViewFragment f = new ConversationViewFragment(); |
| 151 | Bundle args = new Bundle(); |
| 152 | args.putParcelable(ARG_ACCOUNT, account); |
| 153 | args.putParcelable(ARG_CONVERSATION, conversation); |
Mindy Pereira | 863e441 | 2012-03-23 12:21:38 -0700 | [diff] [blame] | 154 | args.putParcelable(ARG_FOLDER, folder); |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 155 | f.setArguments(args); |
| 156 | return f; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public void onActivityCreated(Bundle savedInstanceState) { |
| 161 | super.onActivityCreated(savedInstanceState); |
| 162 | // Strictly speaking, we get back an android.app.Activity from getActivity. However, the |
| 163 | // only activity creating a ConversationListContext is a MailActivity which is of type |
| 164 | // ControllableActivity, so this cast should be safe. If this cast fails, some other |
| 165 | // activity is creating ConversationListFragments. This activity must be of type |
| 166 | // ControllableActivity. |
| 167 | final Activity activity = getActivity(); |
Mindy Pereira | 863e441 | 2012-03-23 12:21:38 -0700 | [diff] [blame] | 168 | if (!(activity instanceof ControllableActivity)) { |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 169 | LogUtils.wtf(LOG_TAG, "ConversationViewFragment expects only a ControllableActivity to" |
| 170 | + "create it. Cannot proceed."); |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 171 | } |
| 172 | mActivity = (ControllableActivity) activity; |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 173 | mContext = mActivity.getApplicationContext(); |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 174 | if (mActivity.isFinishing()) { |
| 175 | // Activity is finishing, just bail. |
| 176 | return; |
| 177 | } |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 178 | mTemplates = new HtmlConversationTemplates(mContext); |
Andy Huang | 5106713 | 2012-03-12 20:08:19 -0700 | [diff] [blame] | 179 | |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 180 | mAdapter = new ConversationViewAdapter(mActivity.getActivityContext(), mAccount, |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame^] | 181 | getLoaderManager(), this, this, this, mAddressCache); |
Andy Huang | 5106713 | 2012-03-12 20:08:19 -0700 | [diff] [blame] | 182 | mConversationContainer.setOverlayAdapter(mAdapter); |
| 183 | |
Andy Huang | 5ff6374 | 2012-03-16 20:30:23 -0700 | [diff] [blame] | 184 | mDensity = getResources().getDisplayMetrics().density; |
| 185 | |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 186 | // Show conversation and start loading messages. |
| 187 | showConversation(); |
| 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public void onCreate(Bundle savedState) { |
| 192 | LogUtils.v(LOG_TAG, "onCreate in FolderListFragment(this=%s)", this); |
| 193 | super.onCreate(savedState); |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 194 | |
| 195 | Bundle args = getArguments(); |
| 196 | mAccount = args.getParcelable(ARG_ACCOUNT); |
| 197 | mConversation = args.getParcelable(ARG_CONVERSATION); |
Mindy Pereira | 863e441 | 2012-03-23 12:21:38 -0700 | [diff] [blame] | 198 | mFolder = args.getParcelable(ARG_FOLDER); |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 199 | mBaseUri = "x-thread://" + mAccount.name + "/" + mConversation.id; |
Andy Huang | 5ff6374 | 2012-03-16 20:30:23 -0700 | [diff] [blame] | 200 | |
| 201 | // not really, we just want to get a crack to store a reference to the change_folders item |
| 202 | setHasOptionsMenu(true); |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public View onCreateView(LayoutInflater inflater, |
| 207 | ViewGroup container, Bundle savedInstanceState) { |
| 208 | View rootView = inflater.inflate(R.layout.conversation_view, null); |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 209 | mConversationContainer = (ConversationContainer) rootView |
| 210 | .findViewById(R.id.conversation_container); |
Andy Huang | 5ff6374 | 2012-03-16 20:30:23 -0700 | [diff] [blame] | 211 | mWebView = (ConversationWebView) mConversationContainer.findViewById(R.id.webview); |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 212 | |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 213 | mWebView.addJavascriptInterface(mJsBridge, "mail"); |
Andy Huang | 17a9cde | 2012-03-09 18:03:16 -0800 | [diff] [blame] | 214 | mWebView.setWebViewClient(mWebViewClient); |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 215 | mWebView.setWebChromeClient(new WebChromeClient() { |
| 216 | @Override |
| 217 | public boolean onConsoleMessage(ConsoleMessage consoleMessage) { |
| 218 | LogUtils.i(LOG_TAG, "JS: %s (%s:%d)", consoleMessage.message(), |
| 219 | consoleMessage.sourceId(), consoleMessage.lineNumber()); |
| 220 | return true; |
| 221 | } |
| 222 | }); |
| 223 | |
Andy Huang | 3233bff | 2012-03-20 19:38:45 -0700 | [diff] [blame] | 224 | final WebSettings settings = mWebView.getSettings(); |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 225 | |
| 226 | settings.setJavaScriptEnabled(true); |
| 227 | settings.setUseWideViewPort(true); |
| 228 | |
| 229 | settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL); |
| 230 | |
| 231 | settings.setSupportZoom(true); |
| 232 | settings.setBuiltInZoomControls(true); |
| 233 | settings.setDisplayZoomControls(false); |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 234 | |
Andy Huang | 5106713 | 2012-03-12 20:08:19 -0700 | [diff] [blame] | 235 | mViewsCreated = true; |
| 236 | |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 237 | return rootView; |
| 238 | } |
| 239 | |
| 240 | @Override |
| 241 | public void onDestroyView() { |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 242 | super.onDestroyView(); |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame^] | 243 | mConversationContainer.setOverlayAdapter(null); |
| 244 | mAdapter = null; |
Andy Huang | 5106713 | 2012-03-12 20:08:19 -0700 | [diff] [blame] | 245 | mViewsCreated = false; |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 246 | } |
| 247 | |
Andy Huang | 5ff6374 | 2012-03-16 20:30:23 -0700 | [diff] [blame] | 248 | @Override |
| 249 | public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { |
| 250 | super.onCreateOptionsMenu(menu, inflater); |
| 251 | |
| 252 | mChangeFoldersMenuItem = menu.findItem(R.id.change_folders); |
| 253 | } |
| 254 | |
Andy Huang | 5a92907 | 2012-03-23 20:17:10 -0700 | [diff] [blame] | 255 | @Override |
Mindy Pereira | c9d5918 | 2012-03-22 16:06:46 -0700 | [diff] [blame] | 256 | public void onPrepareOptionsMenu(Menu menu) { |
| 257 | super.onPrepareOptionsMenu(menu); |
| 258 | boolean showMarkImportant = !mConversation.isImportant(); |
Mindy Pereira | 863e441 | 2012-03-23 12:21:38 -0700 | [diff] [blame] | 259 | Utils.setMenuItemVisibility( |
| 260 | menu, |
| 261 | R.id.mark_important, |
| 262 | showMarkImportant |
| 263 | && mAccount |
| 264 | .supportsCapability(UIProvider.AccountCapabilities.MARK_IMPORTANT)); |
| 265 | Utils.setMenuItemVisibility( |
| 266 | menu, |
| 267 | R.id.mark_not_important, |
| 268 | !showMarkImportant |
| 269 | && mAccount |
| 270 | .supportsCapability(UIProvider.AccountCapabilities.MARK_IMPORTANT)); |
| 271 | // TODO(mindyp) show/ hide spam and mute based on conversation |
| 272 | // properties to be added. |
| 273 | Utils.setMenuItemVisibility(menu, R.id.y_button, |
| 274 | mAccount.supportsCapability(AccountCapabilities.ARCHIVE) && mFolder != null |
| 275 | && mFolder.supportsCapability(FolderCapabilities.ARCHIVE)); |
| 276 | Utils.setMenuItemVisibility(menu, R.id.report_spam, |
| 277 | mAccount.supportsCapability(AccountCapabilities.REPORT_SPAM) && mFolder != null |
| 278 | && mFolder.supportsCapability(FolderCapabilities.REPORT_SPAM) |
| 279 | && !mConversation.spam); |
| 280 | Utils.setMenuItemVisibility( |
| 281 | menu, |
| 282 | R.id.mute, |
| 283 | mAccount.supportsCapability(AccountCapabilities.MUTE) && mFolder != null |
| 284 | && mFolder.supportsCapability(FolderCapabilities.DESTRUCTIVE_MUTE) |
| 285 | && !mConversation.muted); |
Mindy Pereira | c9d5918 | 2012-03-22 16:06:46 -0700 | [diff] [blame] | 286 | } |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 287 | /** |
| 288 | * Handles a request to show a new conversation list, either from a search query or for viewing |
Mindy Pereira | 30fd47b | 2012-03-09 09:24:00 -0800 | [diff] [blame] | 289 | * a folder. This will initiate a data load, and hence must be called on the UI thread. |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 290 | */ |
| 291 | private void showConversation() { |
Mindy Pereira | 8e91572 | 2012-02-16 14:42:56 -0800 | [diff] [blame] | 292 | getLoaderManager().initLoader(MESSAGE_LOADER_ID, Bundle.EMPTY, this); |
| 293 | } |
| 294 | |
| 295 | @Override |
| 296 | public Loader<Cursor> onCreateLoader(int id, Bundle args) { |
Andy Huang | 5fbda02 | 2012-02-28 18:22:03 -0800 | [diff] [blame] | 297 | return new MessageLoader(mContext, mConversation.messageListUri); |
Mindy Pereira | 8e91572 | 2012-02-16 14:42:56 -0800 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | @Override |
| 301 | public void onLoadFinished(Loader<Cursor> loader, Cursor data) { |
Andy Huang | 5fbda02 | 2012-02-28 18:22:03 -0800 | [diff] [blame] | 302 | MessageCursor messageCursor = (MessageCursor) data; |
Andy Huang | 5106713 | 2012-03-12 20:08:19 -0700 | [diff] [blame] | 303 | |
Andy Huang | 3852f87 | 2012-03-27 19:35:11 -0700 | [diff] [blame] | 304 | // TODO: handle Gmail loading states (like LOADING and ERROR) |
| 305 | if (messageCursor.getCount() == 0) { |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 306 | if (mCursor != null) { |
Andy Huang | 3852f87 | 2012-03-27 19:35:11 -0700 | [diff] [blame] | 307 | // TODO: need to exit this view- conversation may have been deleted, or for |
| 308 | // whatever reason is now invalid |
| 309 | } else { |
| 310 | // ignore zero-sized cursors during initial load |
| 311 | } |
| 312 | return; |
Andy Huang | 5106713 | 2012-03-12 20:08:19 -0700 | [diff] [blame] | 313 | } |
Andy Huang | 3852f87 | 2012-03-27 19:35:11 -0700 | [diff] [blame] | 314 | |
| 315 | renderConversation(messageCursor); |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 316 | } |
| 317 | |
Mindy Pereira | 8e91572 | 2012-02-16 14:42:56 -0800 | [diff] [blame] | 318 | @Override |
| 319 | public void onLoaderReset(Loader<Cursor> loader) { |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 320 | mCursor = null; |
| 321 | // TODO: null out all Message.mMessageCursor references |
Andy Huang | 5106713 | 2012-03-12 20:08:19 -0700 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | private void renderConversation(MessageCursor messageCursor) { |
| 325 | mWebView.loadDataWithBaseURL(mBaseUri, renderMessageBodies(messageCursor), "text/html", |
| 326 | "utf-8", null); |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 327 | mCursor = messageCursor; |
Andy Huang | 5106713 | 2012-03-12 20:08:19 -0700 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | private void updateConversation(MessageCursor messageCursor) { |
| 331 | // TODO: handle server-side conversation updates |
| 332 | // for simple things like header data changes, just re-render the affected headers |
| 333 | // if a new message is present, save off the pending cursor and show a notification to |
| 334 | // re-render |
| 335 | |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 336 | mCursor = messageCursor; |
Mindy Pereira | 8e91572 | 2012-02-16 14:42:56 -0800 | [diff] [blame] | 337 | } |
| 338 | |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 339 | /** |
| 340 | * Populate the adapter with overlay views (message headers, super-collapsed blocks, a |
| 341 | * conversation header), and return an HTML document with spacer divs inserted for all overlays. |
| 342 | * |
| 343 | */ |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 344 | private String renderMessageBodies(MessageCursor messageCursor) { |
| 345 | int pos = -1; |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 346 | boolean allowNetworkImages = false; |
| 347 | |
Andy Huang | c754357 | 2012-04-03 15:34:29 -0700 | [diff] [blame] | 348 | // TODO: re-use any existing adapter item state (expanded, details expanded, show pics) |
Vikram Aggarwal | 7d81600 | 2012-04-17 17:06:41 -0700 | [diff] [blame] | 349 | final Settings settings = mActivity.getSettings(); |
| 350 | if (settings != null) { |
| 351 | mAdapter.setDefaultReplyAll(settings.replyBehavior == |
| 352 | UIProvider.DefaultReplyBehavior.REPLY_ALL); |
| 353 | } |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 354 | // Walk through the cursor and build up an overlay adapter as you go. |
| 355 | // Each overlay has an entry in the adapter for easy scroll handling in the container. |
| 356 | // Items are not necessarily 1:1 in cursor and adapter because of super-collapsed blocks. |
| 357 | // When adding adapter items, also add their heights to help the container later determine |
| 358 | // overlay dimensions. |
| 359 | |
| 360 | mAdapter.clear(); |
| 361 | |
Andy Huang | f0e95c3 | 2012-04-02 18:11:17 -0700 | [diff] [blame] | 362 | // We don't need to kick off attachment loaders during this first measurement phase, |
| 363 | // so disable them temporarily. |
| 364 | MessageFooterView.enableAttachmentLoaders(false); |
| 365 | |
Andy Huang | 5ff6374 | 2012-03-16 20:30:23 -0700 | [diff] [blame] | 366 | // 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] | 367 | // a pixel is an mdpi pixel, unless you set device-dpi. |
Andy Huang | 5ff6374 | 2012-03-16 20:30:23 -0700 | [diff] [blame] | 368 | |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 369 | // add a single conversation header item |
| 370 | final int convHeaderPos = mAdapter.addConversationHeader(mConversation); |
| 371 | final int convHeaderDp = measureOverlayHeight(convHeaderPos); |
Andy Huang | 5ff6374 | 2012-03-16 20:30:23 -0700 | [diff] [blame] | 372 | |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 373 | mTemplates.startConversation(convHeaderDp); |
Andy Huang | 3233bff | 2012-03-20 19:38:45 -0700 | [diff] [blame] | 374 | |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame^] | 375 | int collapsedStart = -1; |
| 376 | Message prevCollapsedMsg = null; |
| 377 | boolean prevSafeForImages = false; |
| 378 | |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 379 | while (messageCursor.moveToPosition(++pos)) { |
Marc Blank | bec5115 | 2012-03-22 19:27:34 -0700 | [diff] [blame] | 380 | final Message msg = messageCursor.getMessage(); |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame^] | 381 | |
Andy Huang | 3233bff | 2012-03-20 19:38:45 -0700 | [diff] [blame] | 382 | // TODO: save/restore 'show pics' state |
| 383 | final boolean safeForImages = msg.alwaysShowImages /* || savedStateSaysSafe */; |
| 384 | allowNetworkImages |= safeForImages; |
Andy Huang | 2405528 | 2012-03-27 17:37:06 -0700 | [diff] [blame] | 385 | |
Andy Huang | c754357 | 2012-04-03 15:34:29 -0700 | [diff] [blame] | 386 | final boolean expanded = !msg.read || msg.starred || messageCursor.isLast(); |
| 387 | |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame^] | 388 | if (!expanded) { |
| 389 | // contribute to a super-collapsed block that will be emitted just before the next |
| 390 | // expanded header |
| 391 | if (collapsedStart < 0) { |
| 392 | collapsedStart = pos; |
| 393 | } |
| 394 | prevCollapsedMsg = msg; |
| 395 | prevSafeForImages = safeForImages; |
| 396 | continue; |
| 397 | } |
Andy Huang | 2405528 | 2012-03-27 17:37:06 -0700 | [diff] [blame] | 398 | |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame^] | 399 | // resolve any deferred decisions on previous collapsed items |
| 400 | if (collapsedStart >= 0) { |
| 401 | if (pos - collapsedStart == 1) { |
| 402 | // special-case for a single collapsed message: no need to super-collapse it |
| 403 | renderMessage(prevCollapsedMsg, false /* expanded */, |
| 404 | prevSafeForImages); |
| 405 | } else { |
| 406 | renderSuperCollapsedBlock(collapsedStart, pos - 1); |
| 407 | } |
| 408 | prevCollapsedMsg = null; |
| 409 | collapsedStart = -1; |
| 410 | } |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 411 | |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame^] | 412 | renderMessage(msg, expanded, safeForImages); |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 413 | } |
Andy Huang | 3233bff | 2012-03-20 19:38:45 -0700 | [diff] [blame] | 414 | |
Andy Huang | f0e95c3 | 2012-04-02 18:11:17 -0700 | [diff] [blame] | 415 | // Re-enable attachment loaders |
| 416 | MessageFooterView.enableAttachmentLoaders(true); |
| 417 | |
Andy Huang | 3233bff | 2012-03-20 19:38:45 -0700 | [diff] [blame] | 418 | mWebView.getSettings().setBlockNetworkImage(!allowNetworkImages); |
| 419 | |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 420 | return mTemplates.endConversation(mBaseUri, 320); |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 421 | } |
Mindy Pereira | 674afa4 | 2012-02-17 14:05:24 -0800 | [diff] [blame] | 422 | |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame^] | 423 | private void renderSuperCollapsedBlock(int start, int end) { |
| 424 | final int blockPos = mAdapter.addSuperCollapsedBlock(start, end); |
| 425 | final int blockDp = measureOverlayHeight(blockPos); |
| 426 | mTemplates.appendSuperCollapsedHtml(start, blockDp); |
| 427 | } |
| 428 | |
| 429 | private void renderMessage(Message msg, boolean expanded, boolean safeForImages) { |
| 430 | final int headerPos = mAdapter.addMessageHeader(msg, expanded); |
| 431 | final MessageHeaderItem headerItem = (MessageHeaderItem) mAdapter.getItem(headerPos); |
| 432 | |
| 433 | final int footerPos = mAdapter.addMessageFooter(headerItem); |
| 434 | |
| 435 | // Measure item header and footer heights to allocate spacers in HTML |
| 436 | // But since the views themselves don't exist yet, render each item temporarily into |
| 437 | // a host view for measurement. |
| 438 | final int headerDp = measureOverlayHeight(headerPos); |
| 439 | final int footerDp = measureOverlayHeight(footerPos); |
| 440 | |
| 441 | mTemplates.appendMessageHtml(msg, expanded, safeForImages, 1.0f, headerDp, |
| 442 | footerDp); |
| 443 | } |
| 444 | |
| 445 | private String renderCollapsedHeaders(MessageCursor cursor, |
| 446 | SuperCollapsedBlockItem blockToReplace) { |
| 447 | final List<ConversationOverlayItem> replacements = Lists.newArrayList(); |
| 448 | |
| 449 | mTemplates.reset(); |
| 450 | |
| 451 | for (int i = blockToReplace.getStart(), end = blockToReplace.getEnd(); i <= end; i++) { |
| 452 | cursor.moveToPosition(i); |
| 453 | final Message msg = cursor.getMessage(); |
| 454 | final MessageHeaderItem header = mAdapter.newMessageHeaderItem(msg, |
| 455 | false /* expanded */); |
| 456 | final MessageFooterItem footer = mAdapter.newMessageFooterItem(header); |
| 457 | |
| 458 | final int headerDp = measureOverlayHeight(header); |
| 459 | final int footerDp = measureOverlayHeight(footer); |
| 460 | |
| 461 | mTemplates.appendMessageHtml(msg, false /* expanded */, msg.alwaysShowImages, 1.0f, |
| 462 | headerDp, footerDp); |
| 463 | replacements.add(header); |
| 464 | replacements.add(footer); |
| 465 | } |
| 466 | |
| 467 | mAdapter.replaceSuperCollapsedBlock(blockToReplace, replacements); |
| 468 | |
| 469 | return mTemplates.emit(); |
| 470 | } |
| 471 | |
| 472 | private int measureOverlayHeight(int position) { |
| 473 | return measureOverlayHeight(mAdapter.getItem(position)); |
| 474 | } |
| 475 | |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 476 | /** |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame^] | 477 | * Measure the height of an adapter view by rendering and adapter item into a temporary |
| 478 | * 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] | 479 | * a previous adapter view from {@link ConversationContainer}'s scrap views if one was generated |
| 480 | * earlier. |
| 481 | * <p> |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame^] | 482 | * After measuring the height, this method also saves the height in the |
| 483 | * {@link ConversationOverlayItem} for later use in overlay positioning. |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 484 | * |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame^] | 485 | * @param convItem adapter item with data to render and measure |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 486 | * @return height in dp of the rendered view |
| 487 | */ |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame^] | 488 | private int measureOverlayHeight(ConversationOverlayItem convItem) { |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 489 | final int type = convItem.getType(); |
| 490 | |
| 491 | final View convertView = mConversationContainer.getScrapView(type); |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame^] | 492 | final View hostView = mAdapter.getView(convItem, convertView, mConversationContainer); |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 493 | if (convertView == null) { |
| 494 | mConversationContainer.addScrapView(type, hostView); |
| 495 | } |
| 496 | |
Andy Huang | 9875bb4 | 2012-04-04 20:36:21 -0700 | [diff] [blame] | 497 | final int heightPx = mConversationContainer.measureOverlay(hostView); |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 498 | convItem.setHeight(heightPx); |
Andy Huang | 9875bb4 | 2012-04-04 20:36:21 -0700 | [diff] [blame] | 499 | convItem.markMeasurementValid(); |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 500 | |
| 501 | return (int) (heightPx / mDensity); |
| 502 | } |
| 503 | |
Andy Huang | 3233bff | 2012-03-20 19:38:45 -0700 | [diff] [blame] | 504 | // BEGIN conversation header callbacks |
Andy Huang | 5ff6374 | 2012-03-16 20:30:23 -0700 | [diff] [blame] | 505 | @Override |
| 506 | public void onFoldersClicked() { |
| 507 | if (mChangeFoldersMenuItem == null) { |
| 508 | LogUtils.e(LOG_TAG, "unable to open 'change folders' dialog for a conversation"); |
| 509 | return; |
| 510 | } |
| 511 | mActivity.onOptionsItemSelected(mChangeFoldersMenuItem); |
| 512 | } |
| 513 | |
| 514 | @Override |
| 515 | public void onConversationViewHeaderHeightChange(int newHeight) { |
| 516 | // TODO: propagate the new height to the header's HTML spacer. This can happen when labels |
| 517 | // are added/removed |
| 518 | } |
| 519 | |
| 520 | @Override |
| 521 | public String getSubjectRemainder(String subject) { |
| 522 | // TODO: hook this up to action bar |
| 523 | return subject; |
| 524 | } |
Andy Huang | 3233bff | 2012-03-20 19:38:45 -0700 | [diff] [blame] | 525 | // END conversation header callbacks |
| 526 | |
| 527 | // START message header callbacks |
| 528 | @Override |
Andy Huang | c754357 | 2012-04-03 15:34:29 -0700 | [diff] [blame] | 529 | public void setMessageSpacerHeight(MessageHeaderItem item, int newSpacerHeightPx) { |
| 530 | mConversationContainer.invalidateSpacerGeometry(); |
| 531 | |
| 532 | // update message HTML spacer height |
| 533 | LogUtils.i(LOG_TAG, "setting HTML spacer h=%dpx", newSpacerHeightPx); |
| 534 | final int heightDp = (int) (newSpacerHeightPx / mDensity); |
| 535 | mWebView.loadUrl(String.format("javascript:setMessageHeaderSpacerHeight('%s', %d);", |
| 536 | mTemplates.getMessageDomId(item.message), heightDp)); |
Andy Huang | 3233bff | 2012-03-20 19:38:45 -0700 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | @Override |
Andy Huang | c754357 | 2012-04-03 15:34:29 -0700 | [diff] [blame] | 540 | public void setMessageExpanded(MessageHeaderItem item, int newSpacerHeightPx) { |
| 541 | mConversationContainer.invalidateSpacerGeometry(); |
| 542 | |
| 543 | // show/hide the HTML message body and update the spacer height |
| 544 | LogUtils.i(LOG_TAG, "setting HTML spacer expanded=%s h=%dpx", item.isExpanded(), |
| 545 | newSpacerHeightPx); |
| 546 | final int heightDp = (int) (newSpacerHeightPx / mDensity); |
| 547 | mWebView.loadUrl(String.format("javascript:setMessageBodyVisible('%s', %s, %d);", |
| 548 | mTemplates.getMessageDomId(item.message), item.isExpanded(), heightDp)); |
Andy Huang | 3233bff | 2012-03-20 19:38:45 -0700 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | @Override |
| 552 | public void showExternalResources(Message msg) { |
| 553 | mWebView.getSettings().setBlockNetworkImage(false); |
| 554 | mWebView.loadUrl("javascript:unblockImages('" + mTemplates.getMessageDomId(msg) + "');"); |
| 555 | } |
| 556 | // END message header callbacks |
Andy Huang | 5ff6374 | 2012-03-16 20:30:23 -0700 | [diff] [blame] | 557 | |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame^] | 558 | @Override |
| 559 | public void onSuperCollapsedClick(SuperCollapsedBlockItem item) { |
| 560 | if (mCursor == null || !mViewsCreated) { |
| 561 | return; |
| 562 | } |
| 563 | |
| 564 | mTempBodiesHtml = renderCollapsedHeaders(mCursor, item); |
| 565 | mWebView.loadUrl("javascript:replaceSuperCollapsedBlock(" + item.getStart() + ")"); |
| 566 | } |
| 567 | |
Andy Huang | 5fbda02 | 2012-02-28 18:22:03 -0800 | [diff] [blame] | 568 | private static class MessageLoader extends CursorLoader { |
Paul Westbrook | 2d82d61 | 2012-03-07 09:21:14 -0800 | [diff] [blame] | 569 | private boolean mDeliveredFirstResults = false; |
Andy Huang | 5fbda02 | 2012-02-28 18:22:03 -0800 | [diff] [blame] | 570 | |
| 571 | public MessageLoader(Context c, Uri uri) { |
| 572 | super(c, uri, UIProvider.MESSAGE_PROJECTION, null, null, null); |
| 573 | } |
| 574 | |
| 575 | @Override |
| 576 | public Cursor loadInBackground() { |
| 577 | return new MessageCursor(super.loadInBackground()); |
| 578 | |
| 579 | } |
Paul Westbrook | 2d82d61 | 2012-03-07 09:21:14 -0800 | [diff] [blame] | 580 | |
| 581 | @Override |
| 582 | public void deliverResult(Cursor result) { |
| 583 | // We want to deliver these results, and then we want to make sure that any subsequent |
| 584 | // queries do not hit the network |
| 585 | super.deliverResult(result); |
| 586 | |
| 587 | if (!mDeliveredFirstResults) { |
| 588 | mDeliveredFirstResults = true; |
| 589 | Uri uri = getUri(); |
| 590 | |
| 591 | // Create a ListParams that tells the provider to not hit the network |
| 592 | final ListParams listParams = |
| 593 | new ListParams(ListParams.NO_LIMIT, false /* useNetwork */); |
| 594 | |
| 595 | // Build the new uri with this additional parameter |
| 596 | uri = uri.buildUpon().appendQueryParameter( |
| 597 | UIProvider.LIST_PARAMS_QUERY_PARAMETER, listParams.serialize()).build(); |
| 598 | setUri(uri); |
| 599 | } |
| 600 | } |
Andy Huang | 5fbda02 | 2012-02-28 18:22:03 -0800 | [diff] [blame] | 601 | } |
| 602 | |
Andy Huang | b5078b2 | 2012-03-05 19:52:29 -0800 | [diff] [blame] | 603 | private static int[] parseInts(final String[] stringArray) { |
| 604 | final int len = stringArray.length; |
| 605 | final int[] ints = new int[len]; |
| 606 | for (int i = 0; i < len; i++) { |
| 607 | ints[i] = Integer.parseInt(stringArray[i]); |
| 608 | } |
| 609 | return ints; |
| 610 | } |
| 611 | |
Andy Huang | 17a9cde | 2012-03-09 18:03:16 -0800 | [diff] [blame] | 612 | private class ConversationWebViewClient extends WebViewClient { |
| 613 | |
| 614 | @Override |
| 615 | public void onPageFinished(WebView view, String url) { |
| 616 | super.onPageFinished(view, url); |
| 617 | |
| 618 | // TODO: save off individual message unread state (here, or in onLoadFinished?) so |
| 619 | // 'mark unread' restores the original unread state for each individual message |
| 620 | |
| 621 | // mark as read upon open |
Andy Huang | 5106713 | 2012-03-12 20:08:19 -0700 | [diff] [blame] | 622 | if (!mConversation.read) { |
| 623 | mConversation.markRead(mContext, true /* read */); |
Andy Huang | 397621b | 2012-03-14 20:52:39 -0700 | [diff] [blame] | 624 | mConversation.read = true; |
Andy Huang | 5106713 | 2012-03-12 20:08:19 -0700 | [diff] [blame] | 625 | } |
Andy Huang | 17a9cde | 2012-03-09 18:03:16 -0800 | [diff] [blame] | 626 | } |
| 627 | |
Andy Huang | af5d4e0 | 2012-03-19 19:02:12 -0700 | [diff] [blame] | 628 | @Override |
| 629 | public boolean shouldOverrideUrlLoading(WebView view, String url) { |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame^] | 630 | final Activity activity = getActivity(); |
| 631 | if (!mViewsCreated || activity == null) { |
| 632 | return false; |
| 633 | } |
| 634 | |
Andy Huang | af5d4e0 | 2012-03-19 19:02:12 -0700 | [diff] [blame] | 635 | boolean result = false; |
| 636 | final Uri uri = Uri.parse(url); |
| 637 | Intent intent = new Intent(Intent.ACTION_VIEW, uri); |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame^] | 638 | intent.putExtra(Browser.EXTRA_APPLICATION_ID, activity.getPackageName()); |
Andy Huang | af5d4e0 | 2012-03-19 19:02:12 -0700 | [diff] [blame] | 639 | intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); |
| 640 | |
| 641 | // FIXME: give provider a chance to customize url intents? |
| 642 | // Utils.addGoogleUriAccountIntentExtras(mContext, uri, mAccount, intent); |
| 643 | |
| 644 | try { |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame^] | 645 | activity.startActivity(intent); |
Andy Huang | af5d4e0 | 2012-03-19 19:02:12 -0700 | [diff] [blame] | 646 | result = true; |
| 647 | } catch (ActivityNotFoundException ex) { |
| 648 | // If no application can handle the URL, assume that the |
| 649 | // caller can handle it. |
| 650 | } |
| 651 | |
| 652 | return result; |
| 653 | } |
| 654 | |
Andy Huang | 17a9cde | 2012-03-09 18:03:16 -0800 | [diff] [blame] | 655 | } |
| 656 | |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 657 | /** |
| 658 | * NOTE: all public methods must be listed in the proguard flags so that they can be accessed |
| 659 | * via reflection and not stripped. |
| 660 | * |
| 661 | */ |
| 662 | private class MailJsBridge { |
| 663 | |
| 664 | @SuppressWarnings("unused") |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 665 | public void onWebContentGeometryChange(final String[] overlayBottomStrs) { |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame^] | 666 | try { |
| 667 | mHandler.post(new Runnable() { |
| 668 | @Override |
| 669 | public void run() { |
| 670 | if (!mViewsCreated) { |
| 671 | LogUtils.d(LOG_TAG, "ignoring webContentGeometryChange because views" + |
| 672 | " are gone, %s", ConversationViewFragment.this); |
| 673 | return; |
| 674 | } |
Andy Huang | 5106713 | 2012-03-12 20:08:19 -0700 | [diff] [blame] | 675 | |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame^] | 676 | mConversationContainer.onGeometryChange(parseInts(overlayBottomStrs)); |
| 677 | } |
| 678 | }); |
| 679 | } catch (Throwable t) { |
| 680 | LogUtils.e(LOG_TAG, t, "Error in MailJsBridge.onWebContentGeometryChange"); |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | @SuppressWarnings("unused") |
| 685 | public String getTempMessageBodies() { |
| 686 | try { |
| 687 | if (!mViewsCreated) { |
| 688 | return ""; |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 689 | } |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame^] | 690 | |
| 691 | final String s = mTempBodiesHtml; |
| 692 | mTempBodiesHtml = null; |
| 693 | return s; |
| 694 | } catch (Throwable t) { |
| 695 | LogUtils.e(LOG_TAG, t, "Error in MailJsBridge.getTempMessageBodies"); |
| 696 | return ""; |
| 697 | } |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | } |
| 701 | |
Mindy Pereira | 9b87568 | 2012-02-15 18:10:54 -0800 | [diff] [blame] | 702 | } |