blob: 069a83777b07cea3d601bf1c67a24b6b2036d9b8 [file] [log] [blame]
Mindy Pereira8e9305e2011-12-13 14:25:04 -08001/**
2 * Copyright (c) 2011, Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Andy Huang30e2c242012-01-06 18:14:30 -080017package com.android.mail.compose;
Mindy Pereira8e9305e2011-12-13 14:25:04 -080018
Tony Mantler581edd42014-02-18 15:41:22 -080019import android.annotation.SuppressLint;
Andrew Sapperstein1b7ad922014-05-28 11:23:33 -070020import android.annotation.TargetApi;
Andy Huang5c5fd572012-04-08 18:19:29 -070021import android.app.Activity;
Mindy Pereira82cc5662012-01-09 17:29:30 -080022import android.app.ActivityManager;
23import android.app.AlertDialog;
24import android.app.Dialog;
Tony Mantler2558b502013-07-09 10:53:34 -070025import android.app.DialogFragment;
Mindy Pereirab199d172012-08-13 11:04:03 -070026import android.app.Fragment;
Mindy Pereirab199d172012-08-13 11:04:03 -070027import android.app.FragmentTransaction;
Mindy Pereira96a7f7a2012-07-09 16:51:06 -070028import android.app.LoaderManager;
Andrew Sapperstein05089f32013-10-01 17:00:03 -070029import android.content.ClipData;
Mindy Pereira6349a042012-01-04 11:25:01 -080030import android.content.ContentResolver;
Mindy Pereira82cc5662012-01-09 17:29:30 -080031import android.content.ContentValues;
Mindy Pereira6349a042012-01-04 11:25:01 -080032import android.content.Context;
Mindy Pereira96a7f7a2012-07-09 16:51:06 -070033import android.content.CursorLoader;
Mindy Pereira82cc5662012-01-09 17:29:30 -080034import android.content.DialogInterface;
Mindy Pereira6349a042012-01-04 11:25:01 -080035import android.content.Intent;
Mindy Pereira96a7f7a2012-07-09 16:51:06 -070036import android.content.Loader;
Mindy Pereira82cc5662012-01-09 17:29:30 -080037import android.content.pm.ActivityInfo;
Andrew Sappersteinbc5e0dc2013-08-07 21:15:22 -070038import android.content.res.Resources;
Mindy Pereira7ed1c112012-01-18 10:59:25 -080039import android.database.Cursor;
Jin Cao36e23872014-07-29 13:41:12 -070040import android.graphics.Rect;
Mindy Pereira6349a042012-01-04 11:25:01 -080041import android.net.Uri;
Alan Lau15490232014-03-06 14:53:14 -080042import android.os.AsyncTask;
Andrew Sapperstein05089f32013-10-01 17:00:03 -070043import android.os.Build;
Mindy Pereira8e9305e2011-12-13 14:25:04 -080044import android.os.Bundle;
Jin Caoadea2c82014-08-28 12:25:52 -070045import android.os.Environment;
Mindy Pereira82cc5662012-01-09 17:29:30 -080046import android.os.Handler;
47import android.os.HandlerThread;
Paul Westbrook3c7f94d2012-10-23 14:13:00 -070048import android.os.ParcelFileDescriptor;
Mindy Pereira82cc5662012-01-09 17:29:30 -080049import android.provider.BaseColumns;
Alan Lau439aa5d2014-05-27 17:57:13 -070050import android.support.v4.app.RemoteInput;
Andrew Sapperstein52882ff2014-07-27 12:30:18 -070051import android.support.v7.app.ActionBar;
52import android.support.v7.app.ActionBarActivity;
Mindy Pereira46ce0b12012-01-05 10:32:15 -080053import android.text.Editable;
Mindy Pereira82cc5662012-01-09 17:29:30 -080054import android.text.Html;
Andy Huangff017272014-06-18 00:27:35 -070055import android.text.SpanWatcher;
mindyped9c2f02012-10-12 10:02:08 -070056import android.text.SpannableString;
Mindy Pereira82cc5662012-01-09 17:29:30 -080057import android.text.Spanned;
Paul Westbrookc1827622012-01-06 11:27:12 -080058import android.text.TextUtils;
Mindy Pereira82cc5662012-01-09 17:29:30 -080059import android.text.TextWatcher;
Mindy Pereira46ce0b12012-01-05 10:32:15 -080060import android.text.util.Rfc822Token;
Mindy Pereirac17d0732011-12-29 10:46:19 -080061import android.text.util.Rfc822Tokenizer;
Mindy Pereira3cd4f402012-07-17 11:16:18 -070062import android.view.Gravity;
mindyp62d3ec72012-08-24 13:04:09 -070063import android.view.KeyEvent;
Mindy Pereira326c6602012-01-04 15:32:42 -080064import android.view.LayoutInflater;
Mindy Pereirab47f3e22011-12-13 14:25:04 -080065import android.view.Menu;
66import android.view.MenuInflater;
67import android.view.MenuItem;
Mindy Pereira8e9305e2011-12-13 14:25:04 -080068import android.view.View;
69import android.view.View.OnClickListener;
Andy Huang5c5fd572012-04-08 18:19:29 -070070import android.view.ViewGroup;
Paul Westbrookb4931c62013-01-14 17:51:18 -080071import android.view.inputmethod.BaseInputConnection;
mindyp62d3ec72012-08-24 13:04:09 -070072import android.view.inputmethod.EditorInfo;
Mindy Pereira326c6602012-01-04 15:32:42 -080073import android.widget.ArrayAdapter;
Mindy Pereira433b1982012-04-03 11:53:07 -070074import android.widget.EditText;
Jin Cao36e23872014-07-29 13:41:12 -070075import android.widget.ScrollView;
Mindy Pereira6349a042012-01-04 11:25:01 -080076import android.widget.TextView;
Mindy Pereira013194c2012-01-06 15:09:33 -080077import android.widget.Toast;
Mindy Pereira7b56a612011-12-14 12:32:28 -080078
Mindy Pereirac17d0732011-12-29 10:46:19 -080079import com.android.common.Rfc822Validator;
Tony Mantler9f324232013-08-08 14:24:30 -070080import com.android.common.contacts.DataUsageStatUpdater;
Tony Mantler821e5782014-01-06 15:33:43 -080081import com.android.emailcommon.mail.Address;
Andrew Sappersteinf5ab8ac2014-05-02 15:14:54 -070082import com.android.ex.chips.BaseRecipientAdapter;
83import com.android.ex.chips.DropdownChipLayouter;
Andy Huang5c5fd572012-04-08 18:19:29 -070084import com.android.ex.chips.RecipientEditTextView;
Scott Kennedy5680ec22013-01-07 13:15:20 -080085import com.android.mail.MailIntentService;
Andy Huang5c5fd572012-04-08 18:19:29 -070086import com.android.mail.R;
Andy Huang761522c2013-08-08 13:09:11 -070087import com.android.mail.analytics.Analytics;
Alice Yang1ebc2db2013-03-14 21:21:44 -070088import com.android.mail.browse.MessageHeaderView;
mindyp40882432012-09-06 11:07:40 -070089import com.android.mail.compose.AttachmentsView.AttachmentAddedOrDeletedListener;
Mindy Pereira9932dee2012-01-10 16:09:50 -080090import com.android.mail.compose.AttachmentsView.AttachmentFailureException;
Mindy Pereira5a85e2b2012-01-11 09:53:32 -080091import com.android.mail.compose.FromAddressSpinner.OnAccountChangedListener;
Andy Huang30e2c242012-01-06 18:14:30 -080092import com.android.mail.compose.QuotedTextView.RespondInlineListener;
Mindy Pereira33fe9082012-01-09 16:24:30 -080093import com.android.mail.providers.Account;
Andy Huang30e2c242012-01-06 18:14:30 -080094import com.android.mail.providers.Attachment;
Scott Kennedy5680ec22013-01-07 13:15:20 -080095import com.android.mail.providers.Folder;
Mindy Pereira47d0e652012-07-23 09:45:07 -070096import com.android.mail.providers.MailAppProvider;
Mindy Pereira3ce64e72012-01-13 14:29:45 -080097import com.android.mail.providers.Message;
Mindy Pereira82cc5662012-01-09 17:29:30 -080098import com.android.mail.providers.MessageModification;
Mindy Pereira92551d02012-04-05 11:31:12 -070099import com.android.mail.providers.ReplyFromAccount;
Mindy Pereira181df782012-03-01 13:32:44 -0800100import com.android.mail.providers.Settings;
Andy Huang30e2c242012-01-06 18:14:30 -0800101import com.android.mail.providers.UIProvider;
Mindy Pereira3ca5bad2012-04-16 11:02:42 -0700102import com.android.mail.providers.UIProvider.AccountCapabilities;
Mindy Pereira12575862012-03-21 16:30:54 -0700103import com.android.mail.providers.UIProvider.DraftType;
Alice Yang1ebc2db2013-03-14 21:21:44 -0700104import com.android.mail.ui.AttachmentTile.AttachmentPreview;
Mindy Pereirafa20c1a2012-07-23 13:00:02 -0700105import com.android.mail.ui.MailActivity;
Mindy Pereirab199d172012-08-13 11:04:03 -0700106import com.android.mail.ui.WaitFragment;
Paul Westbrook92227f62012-03-20 10:32:51 -0700107import com.android.mail.utils.AccountUtils;
Mark Wei434f2942012-08-24 11:54:02 -0700108import com.android.mail.utils.AttachmentUtils;
mindypfebd2262012-11-13 17:45:09 -0800109import com.android.mail.utils.ContentProviderTask;
Jin Cao77b4c2c2014-05-20 13:55:53 -0700110import com.android.mail.utils.HtmlUtils;
Paul Westbrookb334c902012-06-25 11:42:46 -0700111import com.android.mail.utils.LogTag;
Andy Huang30e2c242012-01-06 18:14:30 -0800112import com.android.mail.utils.LogUtils;
Alan Lau15490232014-03-06 14:53:14 -0800113import com.android.mail.utils.NotificationActionUtils;
Andy Huang30e2c242012-01-06 18:14:30 -0800114import com.android.mail.utils.Utils;
Andy Huang9ed742c2014-06-18 02:34:50 -0700115import com.google.android.mail.common.html.parser.HtmlTree;
Mindy Pereira46ce0b12012-01-05 10:32:15 -0800116import com.google.common.annotations.VisibleForTesting;
Mindy Pereira82cc5662012-01-09 17:29:30 -0800117import com.google.common.collect.Lists;
Mindy Pereira4a27ea92012-01-05 15:55:25 -0800118import com.google.common.collect.Sets;
Mindy Pereira8e9305e2011-12-13 14:25:04 -0800119
Jin Caoadea2c82014-08-28 12:25:52 -0700120import java.io.File;
Paul Westbrook3c7f94d2012-10-23 14:13:00 -0700121import java.io.FileNotFoundException;
122import java.io.IOException;
Mindy Pereira8eca57a2012-03-20 16:42:34 -0700123import java.io.UnsupportedEncodingException;
124import java.net.URLDecoder;
Mindy Pereira46ce0b12012-01-05 10:32:15 -0800125import java.util.ArrayList;
Paul Westbrookbb87b7f2012-03-20 16:20:07 -0700126import java.util.Arrays;
Mindy Pereira46ce0b12012-01-05 10:32:15 -0800127import java.util.Collection;
Mindy Pereira75f66632012-01-11 11:42:02 -0800128import java.util.HashMap;
Mindy Pereira46ce0b12012-01-05 10:32:15 -0800129import java.util.HashSet;
130import java.util.List;
Paul Westbrook1c078cf2012-03-20 16:18:51 -0700131import java.util.Map.Entry;
Paul Westbrookbb87b7f2012-03-20 16:20:07 -0700132import java.util.Set;
Mindy Pereira82cc5662012-01-09 17:29:30 -0800133import java.util.concurrent.ConcurrentHashMap;
Mindy Pereira46ce0b12012-01-05 10:32:15 -0800134
Andrew Sapperstein52882ff2014-07-27 12:30:18 -0700135public class ComposeActivity extends ActionBarActivity
136 implements OnClickListener, ActionBar.OnNavigationListener,
Tony Mantler2558b502013-07-09 10:53:34 -0700137 RespondInlineListener, TextWatcher,
Alice Yanga990a712013-03-13 18:37:00 -0700138 AttachmentAddedOrDeletedListener, OnAccountChangedListener,
Andrew Sappersteinffd61552014-05-14 15:04:23 -0700139 LoaderManager.LoaderCallbacks<Cursor>, TextView.OnEditorActionListener,
Jin Caoc5c550a2014-07-29 11:53:17 -0700140 RecipientEditTextView.RecipientEntryItemClickedListener, View.OnFocusChangeListener {
Scott Kennedya0287a82014-04-07 14:30:13 -0700141 /**
142 * An {@link Intent} action that launches {@link ComposeActivity}, but is handled as if the
143 * {@link Activity} were launched with no special action.
144 */
145 private static final String ACTION_LAUNCH_COMPOSE =
146 "com.android.mail.intent.action.LAUNCH_COMPOSE";
147
Mindy Pereira6349a042012-01-04 11:25:01 -0800148 // Identifiers for which type of composition this is
Andrew Sappersteinbc5e0dc2013-08-07 21:15:22 -0700149 public static final int COMPOSE = -1;
150 public static final int REPLY = 0;
151 public static final int REPLY_ALL = 1;
152 public static final int FORWARD = 2;
153 public static final int EDIT_DRAFT = 3;
Mindy Pereira6349a042012-01-04 11:25:01 -0800154
155 // Integer extra holding one of the above compose action
Mindy Pereira96a7f7a2012-07-09 16:51:06 -0700156 protected static final String EXTRA_ACTION = "action";
Mindy Pereira6349a042012-01-04 11:25:01 -0800157
Mindy Pereira326689d2012-05-17 10:14:14 -0700158 private static final String EXTRA_SHOW_CC = "showCc";
159 private static final String EXTRA_SHOW_BCC = "showBcc";
mindyp1623f9b2012-11-21 12:41:16 -0800160 private static final String EXTRA_RESPONDED_INLINE = "respondedInline";
mindyp1d7e9142012-11-21 13:54:30 -0800161 private static final String EXTRA_SAVE_ENABLED = "saveEnabled";
Mindy Pereiraa34c9a02012-04-17 14:10:53 -0700162
Mindy Pereira8eca57a2012-03-20 16:42:34 -0700163 private static final String UTF8_ENCODING_NAME = "UTF-8";
164
165 private static final String MAIL_TO = "mailto";
166
Mindy Pereira8eca57a2012-03-20 16:42:34 -0700167 private static final String EXTRA_SUBJECT = "subject";
168
169 private static final String EXTRA_BODY = "body";
170
Andrew Sappersteinbc5e0dc2013-08-07 21:15:22 -0700171 /**
172 * Expected to be html formatted text.
173 */
174 private static final String EXTRA_QUOTED_TEXT = "quotedText";
175
mindypd27b6ea2012-10-05 09:43:49 -0700176 protected static final String EXTRA_FROM_ACCOUNT_STRING = "fromAccountString";
Mindy Pereira9a42bb42012-04-18 15:21:33 -0700177
Mark Wei62066e42012-09-13 12:07:02 -0700178 private static final String EXTRA_ATTACHMENT_PREVIEWS = "attachmentPreviews";
179
Mindy Pereira8eca57a2012-03-20 16:42:34 -0700180 // Extra that we can get passed from other activities
Tony Mantler184ec732013-10-24 13:13:49 -0700181 @VisibleForTesting
182 protected static final String EXTRA_TO = "to";
Mindy Pereira8eca57a2012-03-20 16:42:34 -0700183 private static final String EXTRA_CC = "cc";
184 private static final String EXTRA_BCC = "bcc";
185
Scott Kennedy60847252013-08-15 15:55:42 -0700186 /**
187 * An optional extra containing a {@link ContentValues} of values to be added to
188 * {@link SendOrSaveMessage#mValues}.
189 */
190 public static final String EXTRA_VALUES = "extra-values";
191
Mindy Pereira8eca57a2012-03-20 16:42:34 -0700192 // List of all the fields
Andrew Sappersteinbc5e0dc2013-08-07 21:15:22 -0700193 static final String[] ALL_EXTRAS = { EXTRA_SUBJECT, EXTRA_BODY, EXTRA_TO, EXTRA_CC, EXTRA_BCC,
194 EXTRA_QUOTED_TEXT };
Mindy Pereira8eca57a2012-03-20 16:42:34 -0700195
Alan Lau439aa5d2014-05-27 17:57:13 -0700196 private static final String LEGACY_WEAR_EXTRA = "com.google.android.wearable.extras";
197
Andrew Sapperstein09da9422014-05-30 09:48:08 -0700198 /**
199 * Constant value for the threshold to use for auto-complete suggestions
200 * for the to/cc/bcc fields.
201 */
202 private static final int COMPLETION_THRESHOLD = 1;
203
Mindy Pereira82cc5662012-01-09 17:29:30 -0800204 private static SendOrSaveCallback sTestSendOrSaveCallback = null;
205 // Map containing information about requests to create new messages, and the id of the
206 // messages that were the result of those requests.
207 //
208 // This map is used when the activity that initiated the save a of a new message, is killed
209 // before the save has completed (and when we know the id of the newly created message). When
210 // a save is completed, the service that is running in the background, will update the map
211 //
212 // When a new ComposeActivity instance is created, it will attempt to use the information in
213 // the previously instantiated map. If ComposeActivity.onCreate() is called, with a bundle
214 // (restoring data from a previous instance), and the map hasn't been created, we will attempt
215 // to populate the map with data stored in shared preferences.
Andy Huang1f8f4dd2012-10-25 21:35:35 -0700216 // FIXME: values in this map are never read.
Mindy Pereira82cc5662012-01-09 17:29:30 -0800217 private static ConcurrentHashMap<Integer, Long> sRequestMessageIdMap = null;
Mindy Pereira6349a042012-01-04 11:25:01 -0800218 /**
219 * Notifies the {@code Activity} that the caller is an Email
220 * {@code Activity}, so that the back behavior may be modified accordingly.
221 *
222 * @see #onAppUpPressed
223 */
Paul Westbrookdaecb4b2012-05-31 10:21:26 -0700224 public static final String EXTRA_FROM_EMAIL_TASK = "fromemail";
Mindy Pereira6349a042012-01-04 11:25:01 -0800225
Mindy Pereirae011b1d2012-06-18 13:45:26 -0700226 public static final String EXTRA_ATTACHMENTS = "attachments";
Paul Westbrookf97588b2012-03-20 11:11:37 -0700227
Scott Kennedy5680ec22013-01-07 13:15:20 -0800228 /** If set, we will clear notifications for this folder. */
229 public static final String EXTRA_NOTIFICATION_FOLDER = "extra-notification-folder";
Alan Laue806c942014-06-06 16:19:15 -0700230 public static final String EXTRA_NOTIFICATION_CONVERSATION = "extra-notification-conversation";
Scott Kennedy5680ec22013-01-07 13:15:20 -0800231
Mindy Pereira3ce64e72012-01-13 14:29:45 -0800232 // If this is a reply/forward then this extra will hold the original message
Mindy Pereira36bbcae2012-04-25 09:27:04 -0700233 private static final String EXTRA_IN_REFERENCE_TO_MESSAGE = "in-reference-to-message";
Mindy Pereirab18e5a92012-07-10 11:47:21 -0700234 // If this is a reply/forward then this extra will hold a uri we must query
235 // to get the original message.
236 protected static final String EXTRA_IN_REFERENCE_TO_MESSAGE_URI = "in-reference-to-message-uri";
Mark Wei434f2942012-08-24 11:54:02 -0700237 // If this is an action to edit an existing draft message, this extra will hold the
Paul Westbrookbb87b7f2012-03-20 16:20:07 -0700238 // draft message
239 private static final String ORIGINAL_DRAFT_MESSAGE = "original-draft-message";
Mindy Pereira4a27ea92012-01-05 15:55:25 -0800240 private static final String END_TOKEN = ", ";
Paul Westbrookb334c902012-06-25 11:42:46 -0700241 private static final String LOG_TAG = LogTag.getLogTag();
Mindy Pereira013194c2012-01-06 15:09:33 -0800242 // Request numbers for activities we start
243 private static final int RESULT_PICK_ATTACHMENT = 1;
244 private static final int RESULT_CREATE_ACCOUNT = 2;
Mindy Pereira8eca57a2012-03-20 16:42:34 -0700245 // TODO(mindyp) set mime-type for auto send?
Mindy Pereirae011b1d2012-06-18 13:45:26 -0700246 public static final String AUTO_SEND_ACTION = "com.android.mail.action.AUTO_SEND";
Mindy Pereira8eca57a2012-03-20 16:42:34 -0700247
Mindy Pereirae8f94dc2012-04-16 11:56:21 -0700248 private static final String EXTRA_SELECTED_REPLY_FROM_ACCOUNT = "replyFromAccount";
249 private static final String EXTRA_REQUEST_ID = "requestId";
250 private static final String EXTRA_FOCUS_SELECTION_START = "focusSelectionStart";
Paul Westbrook176a1992013-07-22 13:57:19 -0700251 private static final String EXTRA_FOCUS_SELECTION_END = "focusSelectionEnd";
Mindy Pereirae8f94dc2012-04-16 11:56:21 -0700252 private static final String EXTRA_MESSAGE = "extraMessage";
Mindy Pereira96a7f7a2012-07-09 16:51:06 -0700253 private static final int REFERENCE_MESSAGE_LOADER = 0;
Mindy Pereirab199d172012-08-13 11:04:03 -0700254 private static final int LOADER_ACCOUNT_CURSOR = 1;
Alice Yanga990a712013-03-13 18:37:00 -0700255 private static final int INIT_DRAFT_USING_REFERENCE_MESSAGE = 2;
Mindy Pereira47d0e652012-07-23 09:45:07 -0700256 private static final String EXTRA_SELECTED_ACCOUNT = "selectedAccount";
Mindy Pereirab199d172012-08-13 11:04:03 -0700257 private static final String TAG_WAIT = "wait-fragment";
Andrew Sapperstein5cb71802013-10-01 18:31:20 -0700258 private static final String MIME_TYPE_ALL = "*/*";
Mindy Pereira2db7d4a2012-08-15 11:00:02 -0700259 private static final String MIME_TYPE_PHOTO = "image/*";
Mindy Pereira8e9305e2011-12-13 14:25:04 -0800260
Andy Huang9f855d62013-05-30 17:15:03 -0700261 private static final String KEY_INNER_SAVED_STATE = "compose_state";
262
Jin Caoadea2c82014-08-28 12:25:52 -0700263 // A single thread for running tasks in the background.
264 private static final Handler SEND_SAVE_TASK_HANDLER;
265 // String representing the uri of the data directory (used for attachment uri checking).
266 private static final String DATA_DIRECTORY_ROOT;
267
268 // Static initializations
Jin Cao5134be52014-05-06 19:18:38 -0700269 static {
270 HandlerThread handlerThread = new HandlerThread("Send Message Task Thread");
271 handlerThread.start();
Jin Cao5134be52014-05-06 19:18:38 -0700272 SEND_SAVE_TASK_HANDLER = new Handler(handlerThread.getLooper());
Jin Caoadea2c82014-08-28 12:25:52 -0700273
274 DATA_DIRECTORY_ROOT = Environment.getDataDirectory().toString();
Jin Cao5134be52014-05-06 19:18:38 -0700275 }
276
Jin Cao36e23872014-07-29 13:41:12 -0700277 private ScrollView mScrollView;
Mindy Pereirac17d0732011-12-29 10:46:19 -0800278 private RecipientEditTextView mTo;
279 private RecipientEditTextView mCc;
280 private RecipientEditTextView mBcc;
Jin Cao9d358a12014-07-24 12:15:38 -0700281 private View mCcBccButton;
Mindy Pereira8e9305e2011-12-13 14:25:04 -0800282 private CcBccView mCcBccView;
Mindy Pereira7b56a612011-12-14 12:32:28 -0800283 private AttachmentsView mAttachmentsView;
Andrew Sapperstein746d8612013-08-26 15:56:32 -0700284 protected Account mAccount;
Tony Mantler59e69092013-08-14 11:05:00 -0700285 protected ReplyFromAccount mReplyFromAccount;
Mindy Pereira181df782012-03-01 13:32:44 -0800286 private Settings mCachedSettings;
Mindy Pereira82cc5662012-01-09 17:29:30 -0800287 private Rfc822Validator mValidator;
Mindy Pereira6349a042012-01-04 11:25:01 -0800288 private TextView mSubject;
289
Mindy Pereira326c6602012-01-04 15:32:42 -0800290 private ComposeModeAdapter mComposeModeAdapter;
Andrew Sapperstein746d8612013-08-26 15:56:32 -0700291 protected int mComposeMode = -1;
Mindy Pereira46ce0b12012-01-05 10:32:15 -0800292 private boolean mForward;
Mindy Pereira46ce0b12012-01-05 10:32:15 -0800293 private QuotedTextView mQuotedTextView;
Tony Mantler59e69092013-08-14 11:05:00 -0700294 protected EditText mBodyView;
Mindy Pereira1a95a572012-01-05 12:21:29 -0800295 private View mFromStatic;
Mindy Pereira2eb17322012-03-07 10:07:34 -0800296 private TextView mFromStaticText;
Mindy Pereiraeaea9f12012-01-10 15:05:27 -0800297 private View mFromSpinnerWrapper;
Mindy Pereira1883b342012-06-20 08:34:56 -0700298 @VisibleForTesting
299 protected FromAddressSpinner mFromSpinner;
Andy Huang5f082212014-06-11 22:19:21 -0700300 protected boolean mAddingAttachment;
Mindy Pereiraeaea9f12012-01-10 15:05:27 -0800301 private boolean mAttachmentsChanged;
Mindy Pereira82cc5662012-01-09 17:29:30 -0800302 private boolean mTextChanged;
303 private boolean mReplyFromChanged;
304 private MenuItem mSave;
Mindy Pereirab3112a22012-06-20 12:10:03 -0700305 @VisibleForTesting
306 protected Message mRefMessage;
Mindy Pereira7ed1c112012-01-18 10:59:25 -0800307 private long mDraftId = UIProvider.INVALID_MESSAGE_ID;
308 private Message mDraft;
mindyp44a63392012-11-05 12:05:16 -0800309 private ReplyFromAccount mDraftAccount;
Tony Mantler581edd42014-02-18 15:41:22 -0800310 private final Object mDraftLock = new Object();
Mindy Pereira3ce64e72012-01-13 14:29:45 -0800311
Mindy Pereira326c6602012-01-04 15:32:42 -0800312 /**
Paul Westbrookdaecb4b2012-05-31 10:21:26 -0700313 * Boolean indicating whether ComposeActivity was launched from a Gmail controlled view.
314 */
315 private boolean mLaunchedFromEmail = false;
Mindy Pereiracbfb75a2012-06-25 14:52:23 -0700316 private RecipientTextWatcher mToListener;
317 private RecipientTextWatcher mCcListener;
318 private RecipientTextWatcher mBccListener;
Mindy Pereirab18e5a92012-07-10 11:47:21 -0700319 private Uri mRefMessageUri;
Alice Yanga990a712013-03-13 18:37:00 -0700320 private boolean mShowQuotedText = false;
Andrew Sapperstein746d8612013-08-26 15:56:32 -0700321 protected Bundle mInnerSavedState;
Scott Kennedy60847252013-08-15 15:55:42 -0700322 private ContentValues mExtraValues = null;
Paul Westbrookdaecb4b2012-05-31 10:21:26 -0700323
mindyp1623f9b2012-11-21 12:41:16 -0800324 // Array of the outstanding send or save tasks. Access is synchronized
325 // with the object itself
326 /* package for testing */
327 @VisibleForTesting
Tony Mantler581edd42014-02-18 15:41:22 -0800328 public final ArrayList<SendOrSaveTask> mActiveTasks = Lists.newArrayList();
mindyp1623f9b2012-11-21 12:41:16 -0800329 // FIXME: this variable is never read. related to sRequestMessageIdMap.
330 private int mRequestId;
331 private String mSignature;
332 private Account[] mAccounts;
333 private boolean mRespondedInline;
Andy Huangdc97bf42013-08-15 16:52:45 -0700334 private boolean mPerformedSendOrDiscard = false;
mindyp1623f9b2012-11-21 12:41:16 -0800335
Andy Huang9ed742c2014-06-18 02:34:50 -0700336 private final HtmlTree.ConverterFactory mSpanConverterFactory =
337 new HtmlTree.ConverterFactory() {
338 @Override
339 public HtmlTree.Converter<Spanned> createInstance() {
340 return getSpanConverter();
341 }
342 };
343
Paul Westbrookdaecb4b2012-05-31 10:21:26 -0700344 /**
Mindy Pereira326c6602012-01-04 15:32:42 -0800345 * Can be called from a non-UI thread.
346 */
Mindy Pereira3ce64e72012-01-13 14:29:45 -0800347 public static void editDraft(Context launcher, Account account, Message message) {
Scott Kennedy60847252013-08-15 15:55:42 -0700348 launch(launcher, account, message, EDIT_DRAFT, null, null, null, null,
349 null /* extraValues */);
Mindy Pereira326c6602012-01-04 15:32:42 -0800350 }
351
Mindy Pereira6349a042012-01-04 11:25:01 -0800352 /**
353 * Can be called from a non-UI thread.
354 */
Mindy Pereira33fe9082012-01-09 16:24:30 -0800355 public static void compose(Context launcher, Account account) {
Scott Kennedy60847252013-08-15 15:55:42 -0700356 launch(launcher, account, null, COMPOSE, null, null, null, null, null /* extraValues */);
Mindy Pereira6349a042012-01-04 11:25:01 -0800357 }
358
359 /**
360 * Can be called from a non-UI thread.
361 */
Andrew Sapperstein3de76ec2013-07-16 12:08:15 -0700362 public static void composeToAddress(Context launcher, Account account, String toAddress) {
Scott Kennedy60847252013-08-15 15:55:42 -0700363 launch(launcher, account, null, COMPOSE, toAddress, null, null, null,
364 null /* extraValues */);
Andrew Sappersteinbc5e0dc2013-08-07 21:15:22 -0700365 }
366
367 /**
368 * Can be called from a non-UI thread.
369 */
Andrew Sapperstein746d8612013-08-26 15:56:32 -0700370 public static void composeWithExtraValues(Context launcher, Account account,
371 String subject, final ContentValues extraValues) {
372 launch(launcher, account, null, COMPOSE, null, null, null, subject, extraValues);
373 }
374
375 /**
376 * Can be called from a non-UI thread.
377 */
Scott Kennedy0aeaf7d2012-11-14 18:56:05 -0800378 public static Intent createReplyIntent(final Context launcher, final Account account,
379 final Uri messageUri, final boolean isReplyAll) {
380 return createActionIntent(launcher, account, messageUri, isReplyAll ? REPLY_ALL : REPLY);
381 }
382
383 /**
384 * Can be called from a non-UI thread.
385 */
386 public static Intent createForwardIntent(final Context launcher, final Account account,
387 final Uri messageUri) {
388 return createActionIntent(launcher, account, messageUri, FORWARD);
389 }
390
Scott Kennedya0287a82014-04-07 14:30:13 -0700391 private static Intent createActionIntent(final Context context, final Account account,
Scott Kennedy0aeaf7d2012-11-14 18:56:05 -0800392 final Uri messageUri, final int action) {
Scott Kennedya0287a82014-04-07 14:30:13 -0700393 final Intent intent = new Intent(ACTION_LAUNCH_COMPOSE);
394 intent.setPackage(context.getPackageName());
Scott Kennedy0aeaf7d2012-11-14 18:56:05 -0800395
Paul Westbrook6d2442b2013-07-17 17:51:51 -0700396 updateActionIntent(account, messageUri, action, intent);
397
398 return intent;
399 }
400
401 @VisibleForTesting
402 static Intent updateActionIntent(Account account, Uri messageUri, int action, Intent intent) {
Scott Kennedy0aeaf7d2012-11-14 18:56:05 -0800403 intent.putExtra(EXTRA_FROM_EMAIL_TASK, true);
404 intent.putExtra(EXTRA_ACTION, action);
405 intent.putExtra(Utils.EXTRA_ACCOUNT, account);
406 intent.putExtra(EXTRA_IN_REFERENCE_TO_MESSAGE_URI, messageUri);
407
408 return intent;
409 }
410
411 /**
412 * Can be called from a non-UI thread.
413 */
Mindy Pereira3ce64e72012-01-13 14:29:45 -0800414 public static void reply(Context launcher, Account account, Message message) {
Scott Kennedy60847252013-08-15 15:55:42 -0700415 launch(launcher, account, message, REPLY, null, null, null, null, null /* extraValues */);
Mindy Pereira6349a042012-01-04 11:25:01 -0800416 }
417
418 /**
419 * Can be called from a non-UI thread.
420 */
Mindy Pereira3ce64e72012-01-13 14:29:45 -0800421 public static void replyAll(Context launcher, Account account, Message message) {
Scott Kennedy60847252013-08-15 15:55:42 -0700422 launch(launcher, account, message, REPLY_ALL, null, null, null, null,
423 null /* extraValues */);
Mindy Pereira6349a042012-01-04 11:25:01 -0800424 }
425
426 /**
427 * Can be called from a non-UI thread.
428 */
Mindy Pereira3ce64e72012-01-13 14:29:45 -0800429 public static void forward(Context launcher, Account account, Message message) {
Scott Kennedy60847252013-08-15 15:55:42 -0700430 launch(launcher, account, message, FORWARD, null, null, null, null, null /* extraValues */);
Mindy Pereira6349a042012-01-04 11:25:01 -0800431 }
432
Alice Yang1ebc2db2013-03-14 21:21:44 -0700433 public static void reportRenderingFeedback(Context launcher, Account account, Message message,
434 String body) {
Andrew Sappersteinbc5e0dc2013-08-07 21:15:22 -0700435 launch(launcher, account, message, FORWARD,
Scott Kennedy60847252013-08-15 15:55:42 -0700436 "android-gmail-readability@google.com", body, null, null, null /* extraValues */);
Alice Yang1ebc2db2013-03-14 21:21:44 -0700437 }
438
Scott Kennedya0287a82014-04-07 14:30:13 -0700439 private static void launch(Context context, Account account, Message message, int action,
Scott Kennedy60847252013-08-15 15:55:42 -0700440 String toAddress, String body, String quotedText, String subject,
441 final ContentValues extraValues) {
Scott Kennedya0287a82014-04-07 14:30:13 -0700442 Intent intent = new Intent(ACTION_LAUNCH_COMPOSE);
443 intent.setPackage(context.getPackageName());
Mindy Pereira6349a042012-01-04 11:25:01 -0800444 intent.putExtra(EXTRA_FROM_EMAIL_TASK, true);
445 intent.putExtra(EXTRA_ACTION, action);
446 intent.putExtra(Utils.EXTRA_ACCOUNT, account);
Paul Westbrookbb87b7f2012-03-20 16:20:07 -0700447 if (action == EDIT_DRAFT) {
448 intent.putExtra(ORIGINAL_DRAFT_MESSAGE, message);
449 } else {
450 intent.putExtra(EXTRA_IN_REFERENCE_TO_MESSAGE, message);
451 }
Alice Yang1ebc2db2013-03-14 21:21:44 -0700452 if (toAddress != null) {
453 intent.putExtra(EXTRA_TO, toAddress);
454 }
455 if (body != null) {
456 intent.putExtra(EXTRA_BODY, body);
457 }
Andrew Sappersteinbc5e0dc2013-08-07 21:15:22 -0700458 if (quotedText != null) {
459 intent.putExtra(EXTRA_QUOTED_TEXT, quotedText);
460 }
461 if (subject != null) {
462 intent.putExtra(EXTRA_SUBJECT, subject);
463 }
Scott Kennedy60847252013-08-15 15:55:42 -0700464 if (extraValues != null) {
465 LogUtils.d(LOG_TAG, "Launching with extraValues: %s", extraValues.toString());
466 intent.putExtra(EXTRA_VALUES, extraValues);
467 }
Andy Huange0f03202014-06-13 17:34:49 -0700468 if (action == COMPOSE) {
469 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
470 } else if (message != null) {
James Lemieuxcb1018a2014-06-18 11:09:18 -0700471 intent.setData(Utils.normalizeUri(message.uri));
Andy Huange0f03202014-06-13 17:34:49 -0700472 }
Scott Kennedya0287a82014-04-07 14:30:13 -0700473 context.startActivity(intent);
Mindy Pereira6349a042012-01-04 11:25:01 -0800474 }
Mindy Pereira8e9305e2011-12-13 14:25:04 -0800475
Scott Kennedya0287a82014-04-07 14:30:13 -0700476 public static void composeMailto(Context context, Account account, Uri mailto) {
477 final Intent intent = new Intent(Intent.ACTION_VIEW, mailto);
478 intent.setPackage(context.getPackageName());
Andy Huang0a2a3462013-12-20 15:56:13 -0800479 intent.putExtra(EXTRA_FROM_EMAIL_TASK, true);
480 intent.putExtra(Utils.EXTRA_ACCOUNT, account);
Andy Huange0f03202014-06-13 17:34:49 -0700481 if (mailto != null) {
James Lemieuxcb1018a2014-06-18 11:09:18 -0700482 intent.setData(Utils.normalizeUri(mailto));
Andy Huange0f03202014-06-13 17:34:49 -0700483 }
Scott Kennedya0287a82014-04-07 14:30:13 -0700484 context.startActivity(intent);
Andy Huang0a2a3462013-12-20 15:56:13 -0800485 }
486
Mindy Pereira8e9305e2011-12-13 14:25:04 -0800487 @Override
Scott Kennedyd9063902013-08-02 22:14:37 -0700488 protected void onCreate(Bundle savedInstanceState) {
Mindy Pereira8e9305e2011-12-13 14:25:04 -0800489 super.onCreate(savedInstanceState);
Mindy Pereira3528d362012-01-05 14:39:44 -0800490 setContentView(R.layout.compose);
Andrew Sapperstein52882ff2014-07-27 12:30:18 -0700491 final ActionBar actionBar = getSupportActionBar();
Paul Westbrook4def3bf2014-07-01 00:38:17 -0700492 if (actionBar != null) {
493 // Hide the app icon.
Paul Westbrook5043cc22014-06-28 05:04:21 -0700494 actionBar.setIcon(null);
Paul Westbrook4def3bf2014-07-01 00:38:17 -0700495 actionBar.setDisplayUseLogoEnabled(false);
Paul Westbrook5043cc22014-06-28 05:04:21 -0700496 }
497
Andy Huang9f855d62013-05-30 17:15:03 -0700498 mInnerSavedState = (savedInstanceState != null) ?
499 savedInstanceState.getBundle(KEY_INNER_SAVED_STATE) : null;
Mindy Pereirab199d172012-08-13 11:04:03 -0700500 checkValidAccounts();
501 }
502
503 private void finishCreate() {
Andy Huang9f855d62013-05-30 17:15:03 -0700504 final Bundle savedState = mInnerSavedState;
Mindy Pereira3528d362012-01-05 14:39:44 -0800505 findViews();
Tony Mantler581edd42014-02-18 15:41:22 -0800506 final Intent intent = getIntent();
507 final Message message;
508 final ArrayList<AttachmentPreview> previews;
Alice Yanga990a712013-03-13 18:37:00 -0700509 mShowQuotedText = false;
Tony Mantler581edd42014-02-18 15:41:22 -0800510 final CharSequence quotedText;
Mindy Pereirae8f94dc2012-04-16 11:56:21 -0700511 int action;
Mindy Pereira47d0e652012-07-23 09:45:07 -0700512 // Check for any of the possibly supplied accounts.;
Tony Mantler581edd42014-02-18 15:41:22 -0800513 final Account account;
Andy Huang9f855d62013-05-30 17:15:03 -0700514 if (hadSavedInstanceStateMessage(savedState)) {
515 action = savedState.getInt(EXTRA_ACTION, COMPOSE);
516 account = savedState.getParcelable(Utils.EXTRA_ACCOUNT);
Tony Mantler581edd42014-02-18 15:41:22 -0800517 message = savedState.getParcelable(EXTRA_MESSAGE);
Mark Wei62066e42012-09-13 12:07:02 -0700518
Andy Huang9f855d62013-05-30 17:15:03 -0700519 previews = savedState.getParcelableArrayList(EXTRA_ATTACHMENT_PREVIEWS);
Tony Mantler581edd42014-02-18 15:41:22 -0800520 mRefMessage = savedState.getParcelable(EXTRA_IN_REFERENCE_TO_MESSAGE);
Andrew Sappersteinbc5e0dc2013-08-07 21:15:22 -0700521 quotedText = savedState.getCharSequence(EXTRA_QUOTED_TEXT);
Scott Kennedy44d44812013-08-19 14:18:31 -0700522
523 mExtraValues = savedState.getParcelable(EXTRA_VALUES);
Mindy Pereirae8f94dc2012-04-16 11:56:21 -0700524 } else {
Mindy Pereira47d0e652012-07-23 09:45:07 -0700525 account = obtainAccount(intent);
Mindy Pereirae8f94dc2012-04-16 11:56:21 -0700526 action = intent.getIntExtra(EXTRA_ACTION, COMPOSE);
527 // Initialize the message from the message in the intent
Tony Mantler581edd42014-02-18 15:41:22 -0800528 message = intent.getParcelableExtra(ORIGINAL_DRAFT_MESSAGE);
Mark Wei62066e42012-09-13 12:07:02 -0700529 previews = intent.getParcelableArrayListExtra(EXTRA_ATTACHMENT_PREVIEWS);
Tony Mantler581edd42014-02-18 15:41:22 -0800530 mRefMessage = intent.getParcelableExtra(EXTRA_IN_REFERENCE_TO_MESSAGE);
531 mRefMessageUri = intent.getParcelableExtra(EXTRA_IN_REFERENCE_TO_MESSAGE_URI);
532 quotedText = null;
Andy Huang4fe0af82013-08-20 17:24:51 -0700533
534 if (Analytics.isLoggable()) {
535 if (intent.getBooleanExtra(Utils.EXTRA_FROM_NOTIFICATION, false)) {
536 Analytics.getInstance().sendEvent(
537 "notification_action", "compose", getActionString(action), 0);
538 }
539 }
Mindy Pereirae8f94dc2012-04-16 11:56:21 -0700540 }
Mark Wei62066e42012-09-13 12:07:02 -0700541 mAttachmentsView.setAttachmentPreviews(previews);
Paul Westbrook92227f62012-03-20 10:32:51 -0700542
543 setAccount(account);
Mindy Pereira818143e2012-01-11 13:59:49 -0800544 if (mAccount == null) {
545 return;
546 }
Mindy Pereirae8f94dc2012-04-16 11:56:21 -0700547
Scott Kennedyfe853d32013-06-19 11:47:35 -0700548 initRecipients();
549
Scott Kennedy5680ec22013-01-07 13:15:20 -0800550 // Clear the notification and mark the conversation as seen, if necessary
551 final Folder notificationFolder =
552 intent.getParcelableExtra(EXTRA_NOTIFICATION_FOLDER);
Scott Kennedy5680ec22013-01-07 13:15:20 -0800553
Alan Laue806c942014-06-06 16:19:15 -0700554 if (notificationFolder != null) {
555 final Uri conversationUri = intent.getParcelableExtra(EXTRA_NOTIFICATION_CONVERSATION);
556 Intent actionIntent;
557 if (conversationUri != null) {
558 actionIntent = new Intent(MailIntentService.ACTION_RESEND_NOTIFICATIONS_WEAR);
559 actionIntent.putExtra(Utils.EXTRA_CONVERSATION, conversationUri);
560 } else {
561 actionIntent = new Intent(MailIntentService.ACTION_CLEAR_NEW_MAIL_NOTIFICATIONS);
562 actionIntent.setData(Utils.appendVersionQueryParameter(this,
563 notificationFolder.folderUri.fullUri));
564 }
565 actionIntent.setPackage(getPackageName());
566 actionIntent.putExtra(Utils.EXTRA_ACCOUNT, account);
567 actionIntent.putExtra(Utils.EXTRA_FOLDER, notificationFolder);
568
569 startService(actionIntent);
Scott Kennedy5680ec22013-01-07 13:15:20 -0800570 }
571
Paul Westbrookdaecb4b2012-05-31 10:21:26 -0700572 if (intent.getBooleanExtra(EXTRA_FROM_EMAIL_TASK, false)) {
573 mLaunchedFromEmail = true;
574 } else if (Intent.ACTION_SEND.equals(intent.getAction())) {
575 final Uri dataUri = intent.getData();
576 if (dataUri != null) {
577 final String dataScheme = intent.getData().getScheme();
578 final String accountScheme = mAccount.composeIntentUri.getScheme();
579 mLaunchedFromEmail = TextUtils.equals(dataScheme, accountScheme);
580 }
581 }
582
Mindy Pereira96a7f7a2012-07-09 16:51:06 -0700583 if (mRefMessageUri != null) {
Alice Yanga990a712013-03-13 18:37:00 -0700584 mShowQuotedText = true;
585 mComposeMode = action;
Alan Lau15490232014-03-06 14:53:14 -0800586
587 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
Alan Lau575255c2014-05-16 11:44:27 -0700588 Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);
Alan Lau439aa5d2014-05-27 17:57:13 -0700589 String wearReply = null;
Alan Lau575255c2014-05-16 11:44:27 -0700590 if (remoteInput != null) {
Alan Lau439aa5d2014-05-27 17:57:13 -0700591 LogUtils.d(LOG_TAG, "Got remote input from new api");
592 CharSequence input = remoteInput.getCharSequence(
Alan Lau575255c2014-05-16 11:44:27 -0700593 NotificationActionUtils.WEAR_REPLY_INPUT);
Alan Lau439aa5d2014-05-27 17:57:13 -0700594 if (input != null) {
595 wearReply = input.toString();
Alan Lau15490232014-03-06 14:53:14 -0800596 }
Alan Lau575255c2014-05-16 11:44:27 -0700597 } else {
Alan Lau439aa5d2014-05-27 17:57:13 -0700598 // TODO: remove after legacy code has been removed.
599 LogUtils.d(LOG_TAG,
600 "No remote input from new api, falling back to compatibility mode");
601 ClipData clipData = intent.getClipData();
602 if (clipData != null
603 && LEGACY_WEAR_EXTRA.equals(clipData.getDescription().getLabel())) {
604 Bundle extras = clipData.getItemAt(0).getIntent().getExtras();
605 if (extras != null) {
606 wearReply = extras.getString(NotificationActionUtils.WEAR_REPLY_INPUT);
607 }
608 }
609 }
610
611 if (!TextUtils.isEmpty(wearReply)) {
612 createWearReplyTask(this, mRefMessageUri, UIProvider.MESSAGE_PROJECTION,
613 mComposeMode, wearReply).execute();
614 finish();
615 return;
616 } else {
617 LogUtils.w(LOG_TAG, "remote input string is null");
Alan Lau15490232014-03-06 14:53:14 -0800618 }
619 }
620
Alice Yanga990a712013-03-13 18:37:00 -0700621 getLoaderManager().initLoader(INIT_DRAFT_USING_REFERENCE_MESSAGE, null, this);
Mindy Pereira96a7f7a2012-07-09 16:51:06 -0700622 return;
623 } else if (message != null && action != EDIT_DRAFT) {
Mindy Pereirae8f94dc2012-04-16 11:56:21 -0700624 initFromDraftMessage(message);
625 initQuotedTextFromRefMessage(mRefMessage, action);
Alice Yanga990a712013-03-13 18:37:00 -0700626 mShowQuotedText = message.appendRefMessageContent;
Andrew Sappersteinbc5e0dc2013-08-07 21:15:22 -0700627 // if we should be showing quoted text but mRefMessage is null
628 // and we have some quotedText, display that
Andrew Sapperstein746d8612013-08-26 15:56:32 -0700629 if (mShowQuotedText && mRefMessage == null) {
630 if (quotedText != null) {
631 initQuotedText(quotedText, false /* shouldQuoteText */);
632 } else if (mExtraValues != null) {
633 initExtraValues(mExtraValues);
634 return;
635 }
Andrew Sappersteinbc5e0dc2013-08-07 21:15:22 -0700636 }
Paul Westbrookbb87b7f2012-03-20 16:20:07 -0700637 } else if (action == EDIT_DRAFT) {
Tony Mantler581edd42014-02-18 15:41:22 -0800638 if (message == null) {
639 throw new IllegalStateException("Message must not be null to edit draft");
640 }
Mindy Pereirae8f94dc2012-04-16 11:56:21 -0700641 initFromDraftMessage(message);
Paul Westbrookbb87b7f2012-03-20 16:20:07 -0700642 // Update the action to the draft type of the previous draft
643 switch (message.draftType) {
644 case UIProvider.DraftType.REPLY:
645 action = REPLY;
646 break;
647 case UIProvider.DraftType.REPLY_ALL:
648 action = REPLY_ALL;
649 break;
650 case UIProvider.DraftType.FORWARD:
651 action = FORWARD;
652 break;
653 case UIProvider.DraftType.COMPOSE:
654 default:
655 action = COMPOSE;
656 break;
657 }
Alice Yanga990a712013-03-13 18:37:00 -0700658 LogUtils.d(LOG_TAG, "Previous draft had action type: %d", action);
659
660 mShowQuotedText = message.appendRefMessageContent;
661 if (message.refMessageUri != null) {
662 // If we're editing an existing draft that was in reference to an existing message,
663 // still need to load that original message since we might need to refer to the
664 // original sender and recipients if user switches "reply <-> reply-all".
665 mRefMessageUri = message.refMessageUri;
666 mComposeMode = action;
667 getLoaderManager().initLoader(REFERENCE_MESSAGE_LOADER, null, this);
668 return;
669 }
Mindy Pereirae8f94dc2012-04-16 11:56:21 -0700670 } else if ((action == REPLY || action == REPLY_ALL || action == FORWARD)) {
671 if (mRefMessage != null) {
Scott Kennedy0aeaf7d2012-11-14 18:56:05 -0800672 initFromRefMessage(action);
Alice Yanga990a712013-03-13 18:37:00 -0700673 mShowQuotedText = true;
Mindy Pereirae8f94dc2012-04-16 11:56:21 -0700674 }
Mindy Pereira8eca57a2012-03-20 16:42:34 -0700675 } else {
Andrew Sapperstein746d8612013-08-26 15:56:32 -0700676 if (initFromExtras(intent)) {
677 return;
678 }
Paul Westbrookbb87b7f2012-03-20 16:20:07 -0700679 }
Alice Yanga990a712013-03-13 18:37:00 -0700680
681 mComposeMode = action;
Andy Huang9f855d62013-05-30 17:15:03 -0700682 finishSetup(action, intent, savedState);
Mindy Pereira96a7f7a2012-07-09 16:51:06 -0700683 }
Paul Westbrookbb87b7f2012-03-20 16:20:07 -0700684
Andrew Sapperstein1b7ad922014-05-28 11:23:33 -0700685 @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
Alan Lau15490232014-03-06 14:53:14 -0800686 private static AsyncTask<Void, Void, Message> createWearReplyTask(
687 final ComposeActivity composeActivity,
688 final Uri refMessageUri, final String[] projection, final int action,
689 final String wearReply) {
690 return new AsyncTask<Void, Void, Message>() {
691 private Intent mEmptyServiceIntent = new Intent(composeActivity, EmptyService.class);
692
693 @Override
694 protected void onPreExecute() {
695 // Start service so we won't be killed if this app is put in the background.
696 composeActivity.startService(mEmptyServiceIntent);
697 }
698
699 @Override
700 protected Message doInBackground(Void... params) {
701 Cursor cursor = composeActivity.getContentResolver()
702 .query(refMessageUri, projection, null, null, null, null);
703 if (cursor != null) {
704 try {
705 cursor.moveToFirst();
706 return new Message(cursor);
707 } finally {
708 cursor.close();
709 }
710 }
711 return null;
712 }
713
714 @Override
715 protected void onPostExecute(Message message) {
716 composeActivity.stopService(mEmptyServiceIntent);
717
718 composeActivity.mRefMessage = message;
719 composeActivity.initFromRefMessage(action);
720 composeActivity.setBody(wearReply, false);
721 composeActivity.finishSetup(action, composeActivity.getIntent(), null);
722 composeActivity.sendOrSaveWithSanityChecks(false /* save */, true /* show toast */,
723 false /* orientationChanged */, true /* autoSend */);
724 }
725 };
726 }
727
Mindy Pereirab199d172012-08-13 11:04:03 -0700728 private void checkValidAccounts() {
Paul Westbrookfaa742f2012-11-01 09:50:16 -0700729 final Account[] allAccounts = AccountUtils.getAccounts(this);
730 if (allAccounts == null || allAccounts.length == 0) {
Mindy Pereirab199d172012-08-13 11:04:03 -0700731 final Intent noAccountIntent = MailAppProvider.getNoAccountIntent(this);
732 if (noAccountIntent != null) {
Paul Westbrookfaa742f2012-11-01 09:50:16 -0700733 mAccounts = null;
Mindy Pereirab199d172012-08-13 11:04:03 -0700734 startActivityForResult(noAccountIntent, RESULT_CREATE_ACCOUNT);
735 }
736 } else {
mindyp26d4d2d2012-09-18 17:30:32 -0700737 // If none of the accounts are syncing, setup a watcher.
Mindy Pereirab199d172012-08-13 11:04:03 -0700738 boolean anySyncing = false;
Paul Westbrookfaa742f2012-11-01 09:50:16 -0700739 for (Account a : allAccounts) {
Paul Westbrookdfa1dec2012-09-26 16:27:28 -0700740 if (a.isAccountReady()) {
Mindy Pereirab199d172012-08-13 11:04:03 -0700741 anySyncing = true;
742 break;
743 }
744 }
745 if (!anySyncing) {
746 // There are accounts, but none are sync'd, which is just like having no accounts.
747 mAccounts = null;
748 getLoaderManager().initLoader(LOADER_ACCOUNT_CURSOR, null, this);
749 return;
750 }
Paul Westbrookfaa742f2012-11-01 09:50:16 -0700751 mAccounts = AccountUtils.getSyncingAccounts(this);
Mindy Pereirab199d172012-08-13 11:04:03 -0700752 finishCreate();
753 }
754 }
755
Mindy Pereira47d0e652012-07-23 09:45:07 -0700756 private Account obtainAccount(Intent intent) {
757 Account account = null;
758 Object accountExtra = null;
759 if (intent != null && intent.getExtras() != null) {
760 accountExtra = intent.getExtras().get(Utils.EXTRA_ACCOUNT);
761 if (accountExtra instanceof Account) {
762 return (Account) accountExtra;
mindyp7ae042e2012-08-27 13:27:37 -0700763 } else if (accountExtra instanceof String) {
764 // This is the Account attached to the widget compose intent.
Tony Mantler26a20752014-02-28 16:44:24 -0800765 account = Account.newInstance((String) accountExtra);
mindyp7ae042e2012-08-27 13:27:37 -0700766 if (account != null) {
767 return account;
768 }
Mindy Pereira47d0e652012-07-23 09:45:07 -0700769 }
mindyp5ee9dc42013-01-08 09:54:54 -0800770 accountExtra = intent.hasExtra(Utils.EXTRA_ACCOUNT) ?
771 intent.getStringExtra(Utils.EXTRA_ACCOUNT) :
772 intent.getStringExtra(EXTRA_SELECTED_ACCOUNT);
Mindy Pereira47d0e652012-07-23 09:45:07 -0700773 }
Tony Mantler581edd42014-02-18 15:41:22 -0800774
775 MailAppProvider provider = MailAppProvider.getInstance();
776 String lastAccountUri = provider.getLastSentFromAccount();
777 if (TextUtils.isEmpty(lastAccountUri)) {
778 lastAccountUri = provider.getLastViewedAccount();
Mindy Pereira47d0e652012-07-23 09:45:07 -0700779 }
Tony Mantler581edd42014-02-18 15:41:22 -0800780 if (!TextUtils.isEmpty(lastAccountUri)) {
781 accountExtra = Uri.parse(lastAccountUri);
782 }
783
Mindy Pereirab199d172012-08-13 11:04:03 -0700784 if (mAccounts != null && mAccounts.length > 0) {
Mindy Pereira47d0e652012-07-23 09:45:07 -0700785 if (accountExtra instanceof String && !TextUtils.isEmpty((String) accountExtra)) {
786 // For backwards compatibility, we need to check account
787 // names.
Mindy Pereirab199d172012-08-13 11:04:03 -0700788 for (Account a : mAccounts) {
Tony Mantler79b11562013-10-09 15:31:50 -0700789 if (a.getEmailAddress().equals(accountExtra)) {
Mindy Pereira47d0e652012-07-23 09:45:07 -0700790 account = a;
791 }
792 }
793 } else if (accountExtra instanceof Uri) {
794 // The uri of the last viewed account is what is stored in
795 // the current code base.
Mindy Pereirab199d172012-08-13 11:04:03 -0700796 for (Account a : mAccounts) {
Mindy Pereira47d0e652012-07-23 09:45:07 -0700797 if (a.uri.equals(accountExtra)) {
798 account = a;
799 }
800 }
Mindy Pereirab199d172012-08-13 11:04:03 -0700801 }
802 if (account == null) {
803 account = mAccounts[0];
Mindy Pereira47d0e652012-07-23 09:45:07 -0700804 }
805 }
806 return account;
807 }
808
Andrew Sapperstein746d8612013-08-26 15:56:32 -0700809 protected void finishSetup(int action, Intent intent, Bundle savedInstanceState) {
mindyp34a3c562012-11-06 15:12:15 -0800810 setFocus(action);
Mindy Pereiraf7fc6c32012-06-19 15:18:33 -0700811 // Don't bother with the intent if we have procured a message from the
812 // intent already.
813 if (!hadSavedInstanceStateMessage(savedInstanceState)) {
814 initAttachmentsFromIntent(intent);
815 }
Alice Yanga990a712013-03-13 18:37:00 -0700816 initActionBar();
Mindy Pereirae8f94dc2012-04-16 11:56:21 -0700817 initFromSpinner(savedInstanceState != null ? savedInstanceState : intent.getExtras(),
818 action);
mindypd4a48662012-11-08 17:13:49 -0800819
820 // If this is a draft message, the draft account is whatever account was
821 // used to open the draft message in Compose.
822 if (mDraft != null) {
823 mDraftAccount = mReplyFromAccount;
824 }
825
Mindy Pereira75f66632012-01-11 11:42:02 -0800826 initChangeListeners();
Jin Cao32973b42014-05-06 16:12:11 -0700827
828 // These two should be identical since we check CC and BCC the same way
829 boolean showCc = !TextUtils.isEmpty(mCc.getText()) || (savedInstanceState != null &&
830 savedInstanceState.getBoolean(EXTRA_SHOW_CC));
831 boolean showBcc = !TextUtils.isEmpty(mBcc.getText()) || (savedInstanceState != null &&
832 savedInstanceState.getBoolean(EXTRA_SHOW_BCC));
833 mCcBccView.show(false /* animate */, showCc, showBcc);
Mindy Pereira326689d2012-05-17 10:14:14 -0700834 updateHideOrShowCcBcc();
Alice Yanga990a712013-03-13 18:37:00 -0700835 updateHideOrShowQuotedText(mShowQuotedText);
mindyp1623f9b2012-11-21 12:41:16 -0800836
Tony Mantler581edd42014-02-18 15:41:22 -0800837 mRespondedInline = mInnerSavedState != null &&
838 mInnerSavedState.getBoolean(EXTRA_RESPONDED_INLINE);
mindyp1623f9b2012-11-21 12:41:16 -0800839 if (mRespondedInline) {
840 mQuotedTextView.setVisibility(View.GONE);
841 }
Mindy Pereira71c9e562012-05-17 11:01:02 -0700842 }
843
Scott Kennedyff8553f2013-04-05 20:57:44 -0700844 private static boolean hadSavedInstanceStateMessage(final Bundle savedInstanceState) {
Mindy Pereiraf7fc6c32012-06-19 15:18:33 -0700845 return savedInstanceState != null && savedInstanceState.containsKey(EXTRA_MESSAGE);
846 }
847
Mindy Pereira71c9e562012-05-17 11:01:02 -0700848 private void updateHideOrShowQuotedText(boolean showQuotedText) {
849 mQuotedTextView.updateCheckedState(showQuotedText);
mindyp40882432012-09-06 11:07:40 -0700850 mQuotedTextView.setUpperDividerVisible(mAttachmentsView.getAttachments().size() > 0);
Mindy Pereira433b1982012-04-03 11:53:07 -0700851 }
852
853 private void setFocus(int action) {
854 if (action == EDIT_DRAFT) {
855 int type = mDraft.draftType;
856 switch (type) {
857 case UIProvider.DraftType.COMPOSE:
858 case UIProvider.DraftType.FORWARD:
859 action = COMPOSE;
860 break;
861 case UIProvider.DraftType.REPLY:
862 case UIProvider.DraftType.REPLY_ALL:
863 default:
864 action = REPLY;
865 break;
866 }
867 }
868 switch (action) {
869 case FORWARD:
870 case COMPOSE:
mindyp27083062012-11-15 09:02:01 -0800871 if (TextUtils.isEmpty(mTo.getText())) {
872 mTo.requestFocus();
873 break;
874 }
Scott Kennedyff8553f2013-04-05 20:57:44 -0700875 //$FALL-THROUGH$
Mindy Pereira433b1982012-04-03 11:53:07 -0700876 case REPLY:
877 case REPLY_ALL:
878 default:
879 focusBody();
880 break;
881 }
882 }
883
884 /**
885 * Focus the body of the message.
886 */
Tony Mantler6a7ac782014-02-19 15:22:02 -0800887 private void focusBody() {
Mindy Pereira433b1982012-04-03 11:53:07 -0700888 mBodyView.requestFocus();
Tony Mantler6a7ac782014-02-19 15:22:02 -0800889 resetBodySelection();
890 }
Mindy Pereira433b1982012-04-03 11:53:07 -0700891
Tony Mantler6a7ac782014-02-19 15:22:02 -0800892 private void resetBodySelection() {
893 int length = mBodyView.getText().length();
Mindy Pereira433b1982012-04-03 11:53:07 -0700894 int signatureStartPos = getSignatureStartPosition(
895 mSignature, mBodyView.getText().toString());
896 if (signatureStartPos > -1) {
897 // In case the user deleted the newlines...
898 mBodyView.setSelection(signatureStartPos);
mindyp8743cfc2012-09-18 13:29:08 -0700899 } else if (length >= 0) {
Mindy Pereira433b1982012-04-03 11:53:07 -0700900 // Move cursor to the end.
901 mBodyView.setSelection(length);
902 }
Mindy Pereira1a95a572012-01-05 12:21:29 -0800903 }
904
905 @Override
Andy Huang761522c2013-08-08 13:09:11 -0700906 protected void onStart() {
907 super.onStart();
908
909 Analytics.getInstance().activityStart(this);
910 }
911
912 @Override
913 protected void onStop() {
914 super.onStop();
915
916 Analytics.getInstance().activityStop(this);
917 }
918
919 @Override
Mindy Pereira1a95a572012-01-05 12:21:29 -0800920 protected void onResume() {
921 super.onResume();
922 // Update the from spinner as other accounts
923 // may now be available.
Mindy Pereira818143e2012-01-11 13:59:49 -0800924 if (mFromSpinner != null && mAccount != null) {
Andrew Sappersteina01ddca2014-03-04 10:59:56 -0800925 mFromSpinner.initialize(mComposeMode, mAccount, mAccounts, mRefMessage);
Mindy Pereira818143e2012-01-11 13:59:49 -0800926 }
Mindy Pereira1a95a572012-01-05 12:21:29 -0800927 }
928
Mindy Pereiraeaea9f12012-01-10 15:05:27 -0800929 @Override
930 protected void onPause() {
931 super.onPause();
932
Mindy Pereiraa2148332012-07-02 13:54:14 -0700933 // When the user exits the compose view, see if this draft needs saving.
Yorke Lee3d7048e2012-09-19 14:19:25 -0700934 // Don't save unnecessary drafts if we are only changing the orientation.
935 if (!isChangingConfigurations()) {
Mindy Pereiraa2148332012-07-02 13:54:14 -0700936 saveIfNeeded();
Andy Huangdc97bf42013-08-15 16:52:45 -0700937
Andy Huange003b4c2013-08-16 10:32:05 -0700938 if (isFinishing() && !mPerformedSendOrDiscard && !isBlank()) {
Andy Huangdc97bf42013-08-15 16:52:45 -0700939 // log saving upon backing out of activity. (we avoid logging every sendOrSave()
940 // because that method can be invoked many times in a single compose session.)
941 logSendOrSave(true /* save */);
942 }
Mindy Pereiraa2148332012-07-02 13:54:14 -0700943 }
Mindy Pereiraeaea9f12012-01-10 15:05:27 -0800944 }
945
946 @Override
Andrew Sappersteinf5ab8ac2014-05-02 15:14:54 -0700947 protected void onActivityResult(int request, int result, Intent data) {
Andy Huang5f082212014-06-11 22:19:21 -0700948 if (request == RESULT_PICK_ATTACHMENT) {
Mindy Pereirab199d172012-08-13 11:04:03 -0700949 mAddingAttachment = false;
Andy Huang5f082212014-06-11 22:19:21 -0700950 if (result == RESULT_OK) {
951 addAttachmentAndUpdateView(data);
952 }
Mindy Pereirab199d172012-08-13 11:04:03 -0700953 } else if (request == RESULT_CREATE_ACCOUNT) {
Alice Yanga990a712013-03-13 18:37:00 -0700954 // We were waiting for the user to create an account
Mindy Pereirab199d172012-08-13 11:04:03 -0700955 if (result != RESULT_OK) {
956 finish();
957 } else {
958 // Watch for accounts to show up!
959 // restart the loader to get the updated list of accounts
960 getLoaderManager().initLoader(LOADER_ACCOUNT_CURSOR, null, this);
961 showWaitFragment(null);
962 }
Mindy Pereiraeaea9f12012-01-10 15:05:27 -0800963 }
964 }
965
966 @Override
Scott Kennedyd9063902013-08-02 22:14:37 -0700967 protected final void onRestoreInstanceState(Bundle savedInstanceState) {
Yorke Lee7bec2b92013-04-26 08:31:42 -0700968 final boolean hasAccounts = mAccounts != null && mAccounts.length > 0;
969 if (hasAccounts) {
970 clearChangeListeners();
971 }
Mindy Pereirae8f94dc2012-04-16 11:56:21 -0700972 super.onRestoreInstanceState(savedInstanceState);
Andy Huang9f855d62013-05-30 17:15:03 -0700973 if (mInnerSavedState != null) {
974 if (mInnerSavedState.containsKey(EXTRA_FOCUS_SELECTION_START)) {
975 int selectionStart = mInnerSavedState.getInt(EXTRA_FOCUS_SELECTION_START);
976 int selectionEnd = mInnerSavedState.getInt(EXTRA_FOCUS_SELECTION_END);
Mindy Pereirae8f94dc2012-04-16 11:56:21 -0700977 // There should be a focus and it should be an EditText since we
978 // only save these extras if these conditions are true.
979 EditText focusEditText = (EditText) getCurrentFocus();
980 final int length = focusEditText.getText().length();
981 if (selectionStart < length && selectionEnd < length) {
982 focusEditText.setSelection(selectionStart, selectionEnd);
983 }
984 }
985 }
Yorke Lee7bec2b92013-04-26 08:31:42 -0700986 if (hasAccounts) {
987 initChangeListeners();
988 }
Mindy Pereirae8f94dc2012-04-16 11:56:21 -0700989 }
990
991 @Override
Andrew Sappersteinf5ab8ac2014-05-02 15:14:54 -0700992 protected void onSaveInstanceState(Bundle state) {
Mindy Pereiraeaea9f12012-01-10 15:05:27 -0800993 super.onSaveInstanceState(state);
Andy Huang9f855d62013-05-30 17:15:03 -0700994 final Bundle inner = new Bundle();
995 saveState(inner);
996 state.putBundle(KEY_INNER_SAVED_STATE, inner);
997 }
998
999 private void saveState(Bundle state) {
Mindy Pereirab199d172012-08-13 11:04:03 -07001000 // We have no accounts so there is nothing to compose, and therefore, nothing to save.
1001 if (mAccounts == null || mAccounts.length == 0) {
1002 return;
1003 }
Mindy Pereirae8f94dc2012-04-16 11:56:21 -07001004 // The framework is happy to save and restore the selection but only if it also saves and
1005 // restores the contents of the edit text. That's a lot of text to put in a bundle so we do
1006 // this manually.
1007 View focus = getCurrentFocus();
1008 if (focus != null && focus instanceof EditText) {
1009 EditText focusEditText = (EditText) focus;
1010 state.putInt(EXTRA_FOCUS_SELECTION_START, focusEditText.getSelectionStart());
1011 state.putInt(EXTRA_FOCUS_SELECTION_END, focusEditText.getSelectionEnd());
1012 }
Paul Westbrook6273e962012-04-23 10:44:15 -07001013
1014 final List<ReplyFromAccount> replyFromAccounts = mFromSpinner.getReplyFromAccounts();
Paul Westbrook151f1ad2012-04-24 09:13:00 -07001015 final int selectedPos = mFromSpinner.getSelectedItemPosition();
Mindy Pereirad90f7ac2012-06-27 10:31:06 -07001016 final ReplyFromAccount selectedReplyFromAccount = (replyFromAccounts != null
1017 && replyFromAccounts.size() > 0 && replyFromAccounts.size() > selectedPos) ?
1018 replyFromAccounts.get(selectedPos) : null;
Mindy Pereirae8f94dc2012-04-16 11:56:21 -07001019 if (selectedReplyFromAccount != null) {
1020 state.putString(EXTRA_SELECTED_REPLY_FROM_ACCOUNT, selectedReplyFromAccount.serialize()
1021 .toString());
1022 state.putParcelable(Utils.EXTRA_ACCOUNT, selectedReplyFromAccount.account);
1023 } else {
1024 state.putParcelable(Utils.EXTRA_ACCOUNT, mAccount);
1025 }
Mindy Pereiraeaea9f12012-01-10 15:05:27 -08001026
Mindy Pereirae8f94dc2012-04-16 11:56:21 -07001027 if (mDraftId == UIProvider.INVALID_MESSAGE_ID && mRequestId !=0) {
1028 // We don't have a draft id, and we have a request id,
1029 // save the request id.
1030 state.putInt(EXTRA_REQUEST_ID, mRequestId);
1031 }
1032
1033 // We want to restore the current mode after a pause
1034 // or rotation.
1035 int mode = getMode();
1036 state.putInt(EXTRA_ACTION, mode);
1037
Jin Cao77b4c2c2014-05-20 13:55:53 -07001038 final Message message = createMessage(selectedReplyFromAccount, mRefMessage, mode,
1039 removeComposingSpans(mBodyView.getText()));
Andy Huang1f8f4dd2012-10-25 21:35:35 -07001040 if (mDraft != null) {
mindype7b76aa2012-11-14 16:19:13 -08001041 message.id = mDraft.id;
1042 message.serverId = mDraft.serverId;
1043 message.uri = mDraft.uri;
Andy Huang1f8f4dd2012-10-25 21:35:35 -07001044 }
Mindy Pereirae8f94dc2012-04-16 11:56:21 -07001045 state.putParcelable(EXTRA_MESSAGE, message);
1046
1047 if (mRefMessage != null) {
1048 state.putParcelable(EXTRA_IN_REFERENCE_TO_MESSAGE, mRefMessage);
Andrew Sappersteinbc5e0dc2013-08-07 21:15:22 -07001049 } else if (message.appendRefMessageContent) {
1050 // If we have no ref message but should be appending
1051 // ref message content, we have orphaned quoted text. Save it.
1052 state.putCharSequence(EXTRA_QUOTED_TEXT, mQuotedTextView.getQuotedTextIfIncluded());
Mindy Pereirae8f94dc2012-04-16 11:56:21 -07001053 }
Mindy Pereira326689d2012-05-17 10:14:14 -07001054 state.putBoolean(EXTRA_SHOW_CC, mCcBccView.isCcVisible());
1055 state.putBoolean(EXTRA_SHOW_BCC, mCcBccView.isBccVisible());
mindyp1623f9b2012-11-21 12:41:16 -08001056 state.putBoolean(EXTRA_RESPONDED_INLINE, mRespondedInline);
mindyp816b3f02012-12-11 08:25:04 -08001057 state.putBoolean(EXTRA_SAVE_ENABLED, mSave != null && mSave.isEnabled());
Mark Wei62066e42012-09-13 12:07:02 -07001058 state.putParcelableArrayList(
1059 EXTRA_ATTACHMENT_PREVIEWS, mAttachmentsView.getAttachmentPreviews());
Scott Kennedy44d44812013-08-19 14:18:31 -07001060
1061 state.putParcelable(EXTRA_VALUES, mExtraValues);
Mindy Pereirae8f94dc2012-04-16 11:56:21 -07001062 }
1063
1064 private int getMode() {
1065 int mode = ComposeActivity.COMPOSE;
Andrew Sapperstein52882ff2014-07-27 12:30:18 -07001066 final ActionBar actionBar = getSupportActionBar();
Mindy Pereirae011b1d2012-06-18 13:45:26 -07001067 if (actionBar != null
1068 && actionBar.getNavigationMode() == ActionBar.NAVIGATION_MODE_LIST) {
Mindy Pereirae8f94dc2012-04-16 11:56:21 -07001069 mode = actionBar.getSelectedNavigationIndex();
1070 }
1071 return mode;
1072 }
1073
Jin Cao77b4c2c2014-05-20 13:55:53 -07001074 /**
1075 * This function might be called from a background thread, so be sure to move everything that
1076 * can potentially modify the UI to the main thread (e.g. removeComposingSpans for body).
1077 */
Anthony Lee2a3cc132014-04-22 14:15:25 -07001078 private Message createMessage(ReplyFromAccount selectedReplyFromAccount, Message refMessage,
Jin Cao77b4c2c2014-05-20 13:55:53 -07001079 int mode, Spanned body) {
Mindy Pereirae8f94dc2012-04-16 11:56:21 -07001080 Message message = new Message();
1081 message.id = UIProvider.INVALID_MESSAGE_ID;
Andy Huangd47877e2012-08-09 19:31:24 -07001082 message.serverId = null;
Mindy Pereirae8f94dc2012-04-16 11:56:21 -07001083 message.uri = null;
1084 message.conversationUri = null;
1085 message.subject = mSubject.getText().toString();
1086 message.snippet = null;
Scott Kennedy8960f0a2012-11-07 15:35:50 -08001087 message.setTo(formatSenders(mTo.getText().toString()));
1088 message.setCc(formatSenders(mCc.getText().toString()));
1089 message.setBcc(formatSenders(mBcc.getText().toString()));
1090 message.setReplyTo(null);
Mindy Pereirae8f94dc2012-04-16 11:56:21 -07001091 message.dateReceivedMs = 0;
Jin Cao77b4c2c2014-05-20 13:55:53 -07001092 message.bodyHtml = spannedBodyToHtml(body, true);
Jin Cao7800d292014-08-24 11:31:15 -07001093 message.bodyText = body.toString();
Mindy Pereirae8f94dc2012-04-16 11:56:21 -07001094 message.embedsExternalResources = false;
Alice Yanga990a712013-03-13 18:37:00 -07001095 message.refMessageUri = mRefMessage != null ? mRefMessage.uri : null;
Mindy Pereirae8f94dc2012-04-16 11:56:21 -07001096 message.appendRefMessageContent = mQuotedTextView.getQuotedTextIfIncluded() != null;
1097 ArrayList<Attachment> attachments = mAttachmentsView.getAttachments();
1098 message.hasAttachments = attachments != null && attachments.size() > 0;
1099 message.attachmentListUri = null;
1100 message.messageFlags = 0;
Mindy Pereirae8f94dc2012-04-16 11:56:21 -07001101 message.alwaysShowImages = false;
1102 message.attachmentsJson = Attachment.toJSONArray(attachments);
1103 CharSequence quotedText = mQuotedTextView.getQuotedText();
Anthony Lee2a3cc132014-04-22 14:15:25 -07001104 message.quotedTextOffset = -1; // Just a default value.
1105 if (refMessage != null && !TextUtils.isEmpty(quotedText)) {
1106 if (!TextUtils.isEmpty(refMessage.bodyHtml)) {
1107 // We want the index to point to just the quoted text and not the
1108 // "On December 25, 2014..." part of it.
1109 message.quotedTextOffset =
1110 QuotedTextView.getQuotedTextOffset(quotedText.toString());
1111 } else if (!TextUtils.isEmpty(refMessage.bodyText)) {
1112 // We want to point to the entire quoted text.
1113 message.quotedTextOffset = QuotedTextView.findQuotedTextIndex(quotedText);
1114 }
1115 }
Mindy Pereirae8f94dc2012-04-16 11:56:21 -07001116 message.accountUri = null;
Greg Bullock14fd3042014-08-12 09:21:15 +02001117 message.setFrom(computeFromForAccount(selectedReplyFromAccount));
1118 message.draftType = getDraftType(mode);
1119 return message;
1120 }
1121
1122 protected String computeFromForAccount(ReplyFromAccount selectedReplyFromAccount) {
Tony Mantlerbb036ff72013-10-18 14:03:43 -07001123 final String email = selectedReplyFromAccount != null ? selectedReplyFromAccount.address
1124 : mAccount != null ? mAccount.getEmailAddress() : null;
Tony Mantlerf441d142013-10-22 11:46:00 -07001125 final String senderName = selectedReplyFromAccount != null ? selectedReplyFromAccount.name
1126 : mAccount != null ? mAccount.getSenderName() : null;
Tony Mantler821e5782014-01-06 15:33:43 -08001127 final Address address = new Address(email, senderName);
Greg Bullock14fd3042014-08-12 09:21:15 +02001128 return address.toHeader();
Andy Huang1f8f4dd2012-10-25 21:35:35 -07001129 }
1130
Scott Kennedyff8553f2013-04-05 20:57:44 -07001131 private static String formatSenders(final String string) {
Mindy Pereira3c911582012-08-09 16:59:09 -07001132 if (!TextUtils.isEmpty(string) && string.charAt(string.length() - 1) == ',') {
1133 return string.substring(0, string.length() - 1);
1134 }
1135 return string;
1136 }
1137
Mindy Pereira818143e2012-01-11 13:59:49 -08001138 @VisibleForTesting
Andy Huang91ede362014-01-21 19:16:00 -08001139 protected void setAccount(Account account) {
Mindy Pereirabb5217e2012-04-17 11:08:29 -07001140 if (account == null) {
1141 return;
1142 }
Mindy Pereira23e9fde2012-03-20 15:08:24 -07001143 if (!account.equals(mAccount)) {
1144 mAccount = account;
Paul Westbrookb1f573c2012-04-06 11:38:28 -07001145 mCachedSettings = mAccount.settings;
1146 appendSignature();
Mindy Pereira23e9fde2012-03-20 15:08:24 -07001147 }
Mindy Pereirafa20c1a2012-07-23 13:00:02 -07001148 if (mAccount != null) {
Tony Mantler79b11562013-10-09 15:31:50 -07001149 MailActivity.setNfcMessage(mAccount.getEmailAddress());
Mindy Pereirafa20c1a2012-07-23 13:00:02 -07001150 }
Mindy Pereira818143e2012-01-11 13:59:49 -08001151 }
1152
Mindy Pereirae8f94dc2012-04-16 11:56:21 -07001153 private void initFromSpinner(Bundle bundle, int action) {
1154 if (action == EDIT_DRAFT && mDraft.draftType == UIProvider.DraftType.COMPOSE) {
Mindy Pereira62de1b12012-04-06 12:17:56 -07001155 action = COMPOSE;
1156 }
Andrew Sappersteina01ddca2014-03-04 10:59:56 -08001157 mFromSpinner.initialize(action, mAccount, mAccounts, mRefMessage);
Paul Westbrookc97ec3e2013-07-12 18:17:19 -07001158
Mindy Pereira9a42bb42012-04-18 15:21:33 -07001159 if (bundle != null) {
1160 if (bundle.containsKey(EXTRA_SELECTED_REPLY_FROM_ACCOUNT)) {
1161 mReplyFromAccount = ReplyFromAccount.deserialize(mAccount,
1162 bundle.getString(EXTRA_SELECTED_REPLY_FROM_ACCOUNT));
1163 } else if (bundle.containsKey(EXTRA_FROM_ACCOUNT_STRING)) {
Paul Westbrookc97ec3e2013-07-12 18:17:19 -07001164 final String accountString = bundle.getString(EXTRA_FROM_ACCOUNT_STRING);
Mindy Pereira9a42bb42012-04-18 15:21:33 -07001165 mReplyFromAccount = mFromSpinner.getMatchingReplyFromAccount(accountString);
1166 }
Mindy Pereirae8f94dc2012-04-16 11:56:21 -07001167 }
1168 if (mReplyFromAccount == null) {
1169 if (mDraft != null) {
1170 mReplyFromAccount = getReplyFromAccountFromDraft(mAccount, mDraft);
1171 } else if (mRefMessage != null) {
1172 mReplyFromAccount = getReplyFromAccountForReply(mAccount, mRefMessage);
1173 }
Mindy Pereira62de1b12012-04-06 12:17:56 -07001174 }
1175 if (mReplyFromAccount == null) {
Andy Huang238aa472012-10-30 17:45:17 -07001176 mReplyFromAccount = getDefaultReplyFromAccount(mAccount);
Mindy Pereira62de1b12012-04-06 12:17:56 -07001177 }
Mindy Pereira9a42bb42012-04-18 15:21:33 -07001178
Mindy Pereira62de1b12012-04-06 12:17:56 -07001179 mFromSpinner.setCurrentAccount(mReplyFromAccount);
Mindy Pereira9a42bb42012-04-18 15:21:33 -07001180
Mindy Pereira62de1b12012-04-06 12:17:56 -07001181 if (mFromSpinner.getCount() > 1) {
Mindy Pereiraa83e7082012-03-30 08:53:11 -07001182 // If there is only 1 account, just show that account.
1183 // Otherwise, give the user the ability to choose which account to
Mindy Pereira62de1b12012-04-06 12:17:56 -07001184 // send mail from / save drafts to.
1185 mFromStatic.setVisibility(View.GONE);
Andy Huangca4676f2014-01-16 13:22:20 -08001186 mFromStaticText.setText(mReplyFromAccount.address);
Mindy Pereira62de1b12012-04-06 12:17:56 -07001187 mFromSpinnerWrapper.setVisibility(View.VISIBLE);
Mindy Pereiraa83e7082012-03-30 08:53:11 -07001188 } else {
1189 mFromStatic.setVisibility(View.VISIBLE);
Andy Huangca4676f2014-01-16 13:22:20 -08001190 mFromStaticText.setText(mReplyFromAccount.address);
Mindy Pereiraa83e7082012-03-30 08:53:11 -07001191 mFromSpinnerWrapper.setVisibility(View.GONE);
Mindy Pereiraa83e7082012-03-30 08:53:11 -07001192 }
Mindy Pereira46ce0b12012-01-05 10:32:15 -08001193 }
1194
Mindy Pereira62de1b12012-04-06 12:17:56 -07001195 private ReplyFromAccount getReplyFromAccountForReply(Account account, Message refMessage) {
1196 if (refMessage.accountUri != null) {
1197 // This must be from combined inbox.
1198 List<ReplyFromAccount> replyFromAccounts = mFromSpinner.getReplyFromAccounts();
1199 for (ReplyFromAccount from : replyFromAccounts) {
1200 if (from.account.uri.equals(refMessage.accountUri)) {
1201 return from;
1202 }
1203 }
1204 return null;
1205 } else {
1206 return getReplyFromAccount(account, refMessage);
1207 }
1208 }
1209
1210 /**
Tony Mantler9016a5e2013-07-19 11:54:17 -07001211 * Given an account and the message we're replying to,
Mindy Pereira62de1b12012-04-06 12:17:56 -07001212 * return who the message should be sent from.
1213 * @param account Account in which the message arrived.
Tony Mantler9016a5e2013-07-19 11:54:17 -07001214 * @param refMessage Message to analyze for account selection
Mindy Pereira62de1b12012-04-06 12:17:56 -07001215 * @return the address from which to reply.
1216 */
1217 public ReplyFromAccount getReplyFromAccount(Account account, Message refMessage) {
1218 // First see if we are supposed to use the default address or
1219 // the address it was sentTo.
Mindy Pereira326689d2012-05-17 10:14:14 -07001220 if (mCachedSettings.forceReplyFromDefault) {
Mindy Pereira62de1b12012-04-06 12:17:56 -07001221 return getDefaultReplyFromAccount(account);
1222 } else {
Mindy Pereira89bae572012-06-18 11:34:36 -07001223 // If we aren't explicitly told which account to look for, look at
Mindy Pereira62de1b12012-04-06 12:17:56 -07001224 // all the message recipients and find one that matches
1225 // a custom from or account.
1226 List<String> allRecipients = new ArrayList<String>();
Tony Mantler9016a5e2013-07-19 11:54:17 -07001227 allRecipients.addAll(Arrays.asList(refMessage.getToAddressesUnescaped()));
1228 allRecipients.addAll(Arrays.asList(refMessage.getCcAddressesUnescaped()));
Mindy Pereira62de1b12012-04-06 12:17:56 -07001229 return getMatchingRecipient(account, allRecipients);
1230 }
1231 }
1232
1233 /**
1234 * Compare all the recipients of an email to the current account and all
1235 * custom addresses associated with that account. Return the match if there
1236 * is one, or the default account if there isn't.
1237 */
1238 protected ReplyFromAccount getMatchingRecipient(Account account, List<String> sentTo) {
1239 // Tokenize the list and place in a hashmap.
1240 ReplyFromAccount matchingReplyFrom = null;
1241 Rfc822Token[] tokens;
1242 HashSet<String> recipientsMap = new HashSet<String>();
1243 for (String address : sentTo) {
1244 tokens = Rfc822Tokenizer.tokenize(address);
Tony Mantler581edd42014-02-18 15:41:22 -08001245 for (final Rfc822Token token : tokens) {
1246 recipientsMap.add(token.getAddress());
Mindy Pereira62de1b12012-04-06 12:17:56 -07001247 }
1248 }
1249
1250 int matchingAddressCount = 0;
1251 List<ReplyFromAccount> customFroms;
Andy Huang16174812012-08-16 16:40:35 -07001252 customFroms = account.getReplyFroms();
1253 if (customFroms != null) {
1254 for (ReplyFromAccount entry : customFroms) {
1255 if (recipientsMap.contains(entry.address)) {
1256 matchingReplyFrom = entry;
1257 matchingAddressCount++;
Mindy Pereira62de1b12012-04-06 12:17:56 -07001258 }
1259 }
Mindy Pereira62de1b12012-04-06 12:17:56 -07001260 }
1261 if (matchingAddressCount > 1) {
1262 matchingReplyFrom = getDefaultReplyFromAccount(account);
1263 }
1264 return matchingReplyFrom;
1265 }
1266
Scott Kennedyff8553f2013-04-05 20:57:44 -07001267 private static ReplyFromAccount getDefaultReplyFromAccount(final Account account) {
1268 for (final ReplyFromAccount from : account.getReplyFroms()) {
Mindy Pereira62de1b12012-04-06 12:17:56 -07001269 if (from.isDefault) {
1270 return from;
1271 }
1272 }
Tony Mantlerf441d142013-10-22 11:46:00 -07001273 return new ReplyFromAccount(account, account.uri, account.getEmailAddress(),
1274 account.getSenderName(), account.getEmailAddress(), true, false);
Mindy Pereira62de1b12012-04-06 12:17:56 -07001275 }
1276
Tony Mantlerf441d142013-10-22 11:46:00 -07001277 private ReplyFromAccount getReplyFromAccountFromDraft(final Account account,
1278 final Message msg) {
1279 final Address[] draftFroms = Address.parse(msg.getFrom());
1280 final String sender = draftFroms.length > 0 ? draftFroms[0].getAddress() : "";
Mindy Pereira62de1b12012-04-06 12:17:56 -07001281 ReplyFromAccount replyFromAccount = null;
1282 List<ReplyFromAccount> replyFromAccounts = mFromSpinner.getReplyFromAccounts();
Tony Mantler79b11562013-10-09 15:31:50 -07001283 if (TextUtils.equals(account.getEmailAddress(), sender)) {
Tony Mantlerf441d142013-10-22 11:46:00 -07001284 replyFromAccount = getDefaultReplyFromAccount(account);
Mindy Pereira62de1b12012-04-06 12:17:56 -07001285 } else {
1286 for (ReplyFromAccount fromAccount : replyFromAccounts) {
Tony Mantler79b11562013-10-09 15:31:50 -07001287 if (TextUtils.equals(fromAccount.address, sender)) {
Mindy Pereira62de1b12012-04-06 12:17:56 -07001288 replyFromAccount = fromAccount;
1289 break;
1290 }
1291 }
1292 }
1293 return replyFromAccount;
1294 }
1295
Mindy Pereira46ce0b12012-01-05 10:32:15 -08001296 private void findViews() {
Jin Cao36e23872014-07-29 13:41:12 -07001297 mScrollView = (ScrollView) findViewById(R.id.compose);
1298 mScrollView.setVisibility(View.VISIBLE);
Jin Cao9d358a12014-07-24 12:15:38 -07001299 mCcBccButton = findViewById(R.id.add_cc_bcc);
Mindy Pereira8e9305e2011-12-13 14:25:04 -08001300 if (mCcBccButton != null) {
1301 mCcBccButton.setOnClickListener(this);
1302 }
1303 mCcBccView = (CcBccView) findViewById(R.id.cc_bcc_wrapper);
Mindy Pereira7b56a612011-12-14 12:32:28 -08001304 mAttachmentsView = (AttachmentsView)findViewById(R.id.attachments);
Mindy Pereira818143e2012-01-11 13:59:49 -08001305 mTo = (RecipientEditTextView) findViewById(R.id.to);
Andrew Sapperstein09da9422014-05-30 09:48:08 -07001306 initializeRecipientEditTextView(mTo);
Jin Cao15f09d72014-08-08 13:27:34 -07001307 mTo.setAlternatePopupAnchor(findViewById(R.id.compose_to_dropdown_anchor));
Mindy Pereira818143e2012-01-11 13:59:49 -08001308 mCc = (RecipientEditTextView) findViewById(R.id.cc);
Andrew Sapperstein09da9422014-05-30 09:48:08 -07001309 initializeRecipientEditTextView(mCc);
Mindy Pereira818143e2012-01-11 13:59:49 -08001310 mBcc = (RecipientEditTextView) findViewById(R.id.bcc);
Andrew Sapperstein09da9422014-05-30 09:48:08 -07001311 initializeRecipientEditTextView(mBcc);
Mindy Pereira82cc5662012-01-09 17:29:30 -08001312 // TODO: add special chips text change watchers before adding
1313 // this as a text changed watcher to the to, cc, bcc fields.
Mindy Pereira6349a042012-01-04 11:25:01 -08001314 mSubject = (TextView) findViewById(R.id.subject);
mindyp62d3ec72012-08-24 13:04:09 -07001315 mSubject.setOnEditorActionListener(this);
Jin Caoc5c550a2014-07-29 11:53:17 -07001316 mSubject.setOnFocusChangeListener(this);
Mindy Pereira46ce0b12012-01-05 10:32:15 -08001317 mQuotedTextView = (QuotedTextView) findViewById(R.id.quoted_text_view);
1318 mQuotedTextView.setRespondInlineListener(this);
Mindy Pereira433b1982012-04-03 11:53:07 -07001319 mBodyView = (EditText) findViewById(R.id.body);
Jin Caoc5c550a2014-07-29 11:53:17 -07001320 mBodyView.setOnFocusChangeListener(this);
Mindy Pereira1a95a572012-01-05 12:21:29 -08001321 mFromStatic = findViewById(R.id.static_from_content);
Mindy Pereira2eb17322012-03-07 10:07:34 -08001322 mFromStaticText = (TextView) findViewById(R.id.from_account_name);
Mindy Pereiraeaea9f12012-01-10 15:05:27 -08001323 mFromSpinnerWrapper = findViewById(R.id.spinner_from_content);
Mindy Pereira5a85e2b2012-01-11 09:53:32 -08001324 mFromSpinner = (FromAddressSpinner) findViewById(R.id.from_picker);
Mindy Pereira6349a042012-01-04 11:25:01 -08001325 }
1326
Andrew Sapperstein09da9422014-05-30 09:48:08 -07001327 private void initializeRecipientEditTextView(RecipientEditTextView view) {
1328 view.setTokenizer(new Rfc822Tokenizer());
1329 view.setThreshold(COMPLETION_THRESHOLD);
1330 }
1331
mindyp62d3ec72012-08-24 13:04:09 -07001332 @Override
1333 public boolean onEditorAction(TextView view, int action, KeyEvent keyEvent) {
1334 if (action == EditorInfo.IME_ACTION_DONE) {
1335 focusBody();
1336 return true;
1337 }
1338 return false;
1339 }
1340
Andy Huang91ede362014-01-21 19:16:00 -08001341 /**
1342 * Convert the body text (in {@link Spanned} form) to ready-to-send HTML format as a plain
1343 * String.
1344 *
1345 * @param body the body text including fancy style spans
Jin Cao77b4c2c2014-05-20 13:55:53 -07001346 * @param removedComposing whether the function already removed composingSpans. Necessary
1347 * because we cannot call removeComposingSpans from a background thread.
Andy Huang91ede362014-01-21 19:16:00 -08001348 * @return HTML formatted body that's suitable for sending or saving
1349 */
Jin Cao77b4c2c2014-05-20 13:55:53 -07001350 private String spannedBodyToHtml(Spanned body, boolean removedComposing) {
1351 if (!removedComposing) {
1352 body = removeComposingSpans(body);
1353 }
1354 final HtmlifyBeginResult r = onHtmlifyBegin(body);
Andy Huang91ede362014-01-21 19:16:00 -08001355 return onHtmlifyEnd(Html.toHtml(r.result), r.extras);
1356 }
1357
1358 /**
1359 * A hook for subclasses to convert custom spans in the body text prior to system HTML
1360 * conversion. That HTML conversion is lossy, so anything above and beyond its capability
1361 * has to be handled here.
1362 *
1363 * @param body
1364 * @return a copy of the body text with custom spans replaced with HTML
1365 */
1366 protected HtmlifyBeginResult onHtmlifyBegin(Spanned body) {
1367 return new HtmlifyBeginResult(body, null /* extras */);
1368 }
1369
1370 protected String onHtmlifyEnd(String html, Object extras) {
1371 return html;
1372 }
1373
Mindy Pereirae011b1d2012-06-18 13:45:26 -07001374 protected TextView getBody() {
1375 return mBodyView;
1376 }
1377
1378 @VisibleForTesting
Andy Huang0a2a3462013-12-20 15:56:13 -08001379 public String getBodyHtml() {
Jin Cao77b4c2c2014-05-20 13:55:53 -07001380 return spannedBodyToHtml(mBodyView.getText(), false);
Andy Huang0a2a3462013-12-20 15:56:13 -08001381 }
1382
1383 @VisibleForTesting
Mindy Pereirae011b1d2012-06-18 13:45:26 -07001384 public Account getFromAccount() {
1385 return mReplyFromAccount != null && mReplyFromAccount.account != null ?
1386 mReplyFromAccount.account : mAccount;
1387 }
1388
Mindy Pereiracbfb75a2012-06-25 14:52:23 -07001389 private void clearChangeListeners() {
1390 mSubject.removeTextChangedListener(this);
1391 mBodyView.removeTextChangedListener(this);
1392 mTo.removeTextChangedListener(mToListener);
1393 mCc.removeTextChangedListener(mCcListener);
1394 mBcc.removeTextChangedListener(mBccListener);
1395 mFromSpinner.setOnAccountChangedListener(null);
1396 mAttachmentsView.setAttachmentChangesListener(null);
1397 }
1398
Mindy Pereira75f66632012-01-11 11:42:02 -08001399 // Now that the message has been initialized from any existing draft or
1400 // ref message data, set up listeners for any changes that occur to the
1401 // message.
1402 private void initChangeListeners() {
mindyp1d7e9142012-11-21 13:54:30 -08001403 // Make sure we only add text changed listeners once!
1404 clearChangeListeners();
Mindy Pereira75f66632012-01-11 11:42:02 -08001405 mSubject.addTextChangedListener(this);
1406 mBodyView.addTextChangedListener(this);
Mindy Pereiracbfb75a2012-06-25 14:52:23 -07001407 if (mToListener == null) {
1408 mToListener = new RecipientTextWatcher(mTo, this);
1409 }
1410 mTo.addTextChangedListener(mToListener);
1411 if (mCcListener == null) {
1412 mCcListener = new RecipientTextWatcher(mCc, this);
1413 }
1414 mCc.addTextChangedListener(mCcListener);
1415 if (mBccListener == null) {
1416 mBccListener = new RecipientTextWatcher(mBcc, this);
1417 }
1418 mBcc.addTextChangedListener(mBccListener);
Mindy Pereira75f66632012-01-11 11:42:02 -08001419 mFromSpinner.setOnAccountChangedListener(this);
Mindy Pereira818143e2012-01-11 13:59:49 -08001420 mAttachmentsView.setAttachmentChangesListener(this);
Mindy Pereira75f66632012-01-11 11:42:02 -08001421 }
1422
Alice Yanga990a712013-03-13 18:37:00 -07001423 private void initActionBar() {
1424 LogUtils.d(LOG_TAG, "initializing action bar in ComposeActivity");
Andrew Sapperstein52882ff2014-07-27 12:30:18 -07001425 final ActionBar actionBar = getSupportActionBar();
Mindy Pereirae011b1d2012-06-18 13:45:26 -07001426 if (actionBar == null) {
1427 return;
1428 }
Alice Yanga990a712013-03-13 18:37:00 -07001429 if (mComposeMode == ComposeActivity.COMPOSE) {
Mindy Pereiraeaea9f12012-01-10 15:05:27 -08001430 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
Jin Caof0a0b4c2014-08-20 15:35:50 -07001431 actionBar.setTitle(R.string.compose_title);
Mindy Pereira326c6602012-01-04 15:32:42 -08001432 } else {
Mindy Pereiraeaea9f12012-01-10 15:05:27 -08001433 actionBar.setTitle(null);
Mindy Pereira326c6602012-01-04 15:32:42 -08001434 if (mComposeModeAdapter == null) {
Jin Caof7461632014-08-11 15:21:43 -07001435 mComposeModeAdapter = new ComposeModeAdapter(actionBar.getThemedContext());
Mindy Pereira326c6602012-01-04 15:32:42 -08001436 }
Mindy Pereiraeaea9f12012-01-10 15:05:27 -08001437 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
1438 actionBar.setListNavigationCallbacks(mComposeModeAdapter, this);
Alice Yanga990a712013-03-13 18:37:00 -07001439 switch (mComposeMode) {
Mindy Pereira326c6602012-01-04 15:32:42 -08001440 case ComposeActivity.REPLY:
Mindy Pereiraeaea9f12012-01-10 15:05:27 -08001441 actionBar.setSelectedNavigationItem(0);
Mindy Pereira326c6602012-01-04 15:32:42 -08001442 break;
1443 case ComposeActivity.REPLY_ALL:
Mindy Pereiraeaea9f12012-01-10 15:05:27 -08001444 actionBar.setSelectedNavigationItem(1);
Mindy Pereira326c6602012-01-04 15:32:42 -08001445 break;
1446 case ComposeActivity.FORWARD:
Mindy Pereiraeaea9f12012-01-10 15:05:27 -08001447 actionBar.setSelectedNavigationItem(2);
Mindy Pereira326c6602012-01-04 15:32:42 -08001448 break;
1449 }
1450 }
Paul Westbrook4def3bf2014-07-01 00:38:17 -07001451 actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP,
1452 ActionBar.DISPLAY_HOME_AS_UP);
Mindy Pereirafbe40192012-03-20 10:40:45 -07001453 actionBar.setHomeButtonEnabled(true);
Mindy Pereira326c6602012-01-04 15:32:42 -08001454 }
1455
Scott Kennedy0aeaf7d2012-11-14 18:56:05 -08001456 private void initFromRefMessage(int action) {
1457 setFieldsFromRefMessage(action);
Alice Yang1ebc2db2013-03-14 21:21:44 -07001458
1459 // Check if To: address and email body needs to be prefilled based on extras.
1460 // This is used for reporting rendering feedback.
1461 if (MessageHeaderView.ENABLE_REPORT_RENDERING_PROBLEM) {
1462 Intent intent = getIntent();
1463 if (intent.getExtras() != null) {
1464 String toAddresses = intent.getStringExtra(EXTRA_TO);
1465 if (toAddresses != null) {
1466 addToAddresses(Arrays.asList(TextUtils.split(toAddresses, ",")));
1467 }
1468 String body = intent.getStringExtra(EXTRA_BODY);
1469 if (body != null) {
1470 setBody(body, false /* withSignature */);
1471 }
1472 }
1473 }
Mindy Pereira96a7f7a2012-07-09 16:51:06 -07001474 }
1475
Scott Kennedy0aeaf7d2012-11-14 18:56:05 -08001476 private void setFieldsFromRefMessage(int action) {
Mindy Pereira8eca57a2012-03-20 16:42:34 -07001477 setSubject(mRefMessage, action);
1478 // Setup recipients
1479 if (action == FORWARD) {
1480 mForward = true;
Mindy Pereira6349a042012-01-04 11:25:01 -08001481 }
Scott Kennedy0aeaf7d2012-11-14 18:56:05 -08001482 initRecipientsFromRefMessage(mRefMessage, action);
Mindy Pereirae8f94dc2012-04-16 11:56:21 -07001483 initQuotedTextFromRefMessage(mRefMessage, action);
Mindy Pereira8eca57a2012-03-20 16:42:34 -07001484 if (action == ComposeActivity.FORWARD || mAttachmentsChanged) {
1485 initAttachments(mRefMessage);
1486 }
Mindy Pereirac17d0732011-12-29 10:46:19 -08001487 }
1488
Andy Huang9ed742c2014-06-18 02:34:50 -07001489 protected HtmlTree.Converter<Spanned> getSpanConverter() {
1490 return new HtmlUtils.SpannedConverter();
1491 }
1492
Mindy Pereirae8f94dc2012-04-16 11:56:21 -07001493 private void initFromDraftMessage(Message message) {
Andy Huang1f8f4dd2012-10-25 21:35:35 -07001494 LogUtils.d(LOG_TAG, "Intializing draft from previous draft message: %s", message);
Paul Westbrookbb87b7f2012-03-20 16:20:07 -07001495
1496 mDraft = message;
1497 mDraftId = message.id;
1498 mSubject.setText(message.subject);
1499 mForward = message.draftType == UIProvider.DraftType.FORWARD;
Tony Mantler9016a5e2013-07-19 11:54:17 -07001500 final List<String> toAddresses = Arrays.asList(message.getToAddressesUnescaped());
Paul Westbrookbb87b7f2012-03-20 16:20:07 -07001501 addToAddresses(toAddresses);
Tony Mantler9016a5e2013-07-19 11:54:17 -07001502 addCcAddresses(Arrays.asList(message.getCcAddressesUnescaped()), toAddresses);
1503 addBccAddresses(Arrays.asList(message.getBccAddressesUnescaped()));
Mindy Pereira2421dc82012-03-27 13:32:31 -07001504 if (message.hasAttachments) {
1505 List<Attachment> attachments = message.getAttachments();
1506 for (Attachment a : attachments) {
Andy Huang5c5fd572012-04-08 18:19:29 -07001507 addAttachmentAndUpdateView(a);
Mindy Pereira2421dc82012-03-27 13:32:31 -07001508 }
1509 }
Anthony Lee2a3cc132014-04-22 14:15:25 -07001510 int quotedTextIndex = message.appendRefMessageContent ? message.quotedTextOffset : -1;
Paul Westbrookbb87b7f2012-03-20 16:20:07 -07001511 // Set the body
Mindy Pereira002ff522012-05-30 10:31:26 -07001512 CharSequence quotedText = null;
Paul Westbrookbb87b7f2012-03-20 16:20:07 -07001513 if (!TextUtils.isEmpty(message.bodyHtml)) {
Jin Cao32f453b2014-07-22 14:21:15 -07001514 String body = message.bodyHtml;
Mindy Pereira002ff522012-05-30 10:31:26 -07001515 if (quotedTextIndex > -1) {
Anthony Lee2a3cc132014-04-22 14:15:25 -07001516 // Find the offset in the html text of the actual quoted text and strip it out.
1517 // Note that the actual quotedTextOffset in the message has not changed as
1518 // this different offset is used only for display purposes. They point to different
1519 // parts of the original message. Please see the comments in QuoteTextView
1520 // to see the differences.
Mindy Pereira752222d2012-07-19 09:58:53 -07001521 quotedTextIndex = QuotedTextView.findQuotedTextIndex(message.bodyHtml);
1522 if (quotedTextIndex > -1) {
Jin Cao32f453b2014-07-22 14:21:15 -07001523 body = message.bodyHtml.substring(0, quotedTextIndex);
Mindy Pereira752222d2012-07-19 09:58:53 -07001524 quotedText = message.bodyHtml.subSequence(quotedTextIndex,
1525 message.bodyHtml.length());
1526 }
Mindy Pereira002ff522012-05-30 10:31:26 -07001527 }
Jin Cao32f453b2014-07-22 14:21:15 -07001528 new HtmlToSpannedTask().execute(body);
Paul Westbrookbb87b7f2012-03-20 16:20:07 -07001529 } else {
Mindy Pereira752222d2012-07-19 09:58:53 -07001530 final String body = message.bodyText;
Anthony Lee2a3cc132014-04-22 14:15:25 -07001531 final CharSequence bodyText;
1532 if (TextUtils.isEmpty(body)) {
1533 bodyText = "";
1534 quotedText = null;
1535 } else {
1536 if (quotedTextIndex > body.length()) {
1537 // Sanity check to guarantee that we will not over index the String.
1538 // If this happens there is a bigger problem. This should never happen hence
1539 // the wtf logging.
1540 quotedTextIndex = -1;
1541 LogUtils.wtf(LOG_TAG, "quotedTextIndex (%d) > body.length() (%d)",
1542 quotedTextIndex, body.length());
1543 }
1544 bodyText = quotedTextIndex > -1 ? body.substring(0, quotedTextIndex) : body;
1545 if (quotedTextIndex > -1) {
1546 quotedText = body.substring(quotedTextIndex);
1547 }
Mindy Pereira002ff522012-05-30 10:31:26 -07001548 }
1549 mBodyView.setText(bodyText);
1550 }
1551 if (quotedTextIndex > -1 && quotedText != null) {
Mindy Pereira39713232012-05-30 11:48:41 -07001552 mQuotedTextView.setQuotedTextFromDraft(quotedText, mForward);
Paul Westbrookbb87b7f2012-03-20 16:20:07 -07001553 }
Paul Westbrookbb87b7f2012-03-20 16:20:07 -07001554 }
1555
Mindy Pereira8eca57a2012-03-20 16:42:34 -07001556 /**
1557 * Fill all the widgets with the content found in the Intent Extra, if any.
1558 * Also apply the same style to all widgets. Note: if initFromExtras is
1559 * called as a result of switching between reply, reply all, and forward per
1560 * the latest revision of Gmail, and the user has already made changes to
1561 * attachments on a previous incarnation of the message (as a reply, reply
1562 * all, or forward), the original attachments from the message will not be
1563 * re-instantiated. The user's changes will be respected. This follows the
1564 * web gmail interaction.
Andrew Sapperstein746d8612013-08-26 15:56:32 -07001565 * @return {@code true} if the activity should not call {@link #finishSetup}.
Mindy Pereira8eca57a2012-03-20 16:42:34 -07001566 */
Andrew Sapperstein746d8612013-08-26 15:56:32 -07001567 public boolean initFromExtras(Intent intent) {
Mindy Pereira8eca57a2012-03-20 16:42:34 -07001568 // If we were invoked with a SENDTO intent, the value
1569 // should take precedence
1570 final Uri dataUri = intent.getData();
1571 if (dataUri != null) {
1572 if (MAIL_TO.equals(dataUri.getScheme())) {
1573 initFromMailTo(dataUri.toString());
1574 } else {
Mindy Pereira0b4f28e2012-03-28 14:12:21 -07001575 if (!mAccount.composeIntentUri.equals(dataUri)) {
Mindy Pereira8eca57a2012-03-20 16:42:34 -07001576 String toText = dataUri.getSchemeSpecificPart();
1577 if (toText != null) {
1578 mTo.setText("");
Mindy Pereiradbe89962012-04-13 09:42:38 -07001579 addToAddresses(Arrays.asList(TextUtils.split(toText, ",")));
Mindy Pereira8eca57a2012-03-20 16:42:34 -07001580 }
1581 }
1582 }
1583 }
1584
1585 String[] extraStrings = intent.getStringArrayExtra(Intent.EXTRA_EMAIL);
1586 if (extraStrings != null) {
1587 addToAddresses(Arrays.asList(extraStrings));
1588 }
1589 extraStrings = intent.getStringArrayExtra(Intent.EXTRA_CC);
1590 if (extraStrings != null) {
1591 addCcAddresses(Arrays.asList(extraStrings), null);
1592 }
1593 extraStrings = intent.getStringArrayExtra(Intent.EXTRA_BCC);
1594 if (extraStrings != null) {
1595 addBccAddresses(Arrays.asList(extraStrings));
1596 }
1597
1598 String extraString = intent.getStringExtra(Intent.EXTRA_SUBJECT);
1599 if (extraString != null) {
1600 mSubject.setText(extraString);
1601 }
1602
1603 for (String extra : ALL_EXTRAS) {
1604 if (intent.hasExtra(extra)) {
1605 String value = intent.getStringExtra(extra);
1606 if (EXTRA_TO.equals(extra)) {
Mindy Pereiradbe89962012-04-13 09:42:38 -07001607 addToAddresses(Arrays.asList(TextUtils.split(value, ",")));
Mindy Pereira8eca57a2012-03-20 16:42:34 -07001608 } else if (EXTRA_CC.equals(extra)) {
Mindy Pereiradbe89962012-04-13 09:42:38 -07001609 addCcAddresses(Arrays.asList(TextUtils.split(value, ",")), null);
Mindy Pereira8eca57a2012-03-20 16:42:34 -07001610 } else if (EXTRA_BCC.equals(extra)) {
Mindy Pereiradbe89962012-04-13 09:42:38 -07001611 addBccAddresses(Arrays.asList(TextUtils.split(value, ",")));
Mindy Pereira8eca57a2012-03-20 16:42:34 -07001612 } else if (EXTRA_SUBJECT.equals(extra)) {
1613 mSubject.setText(value);
1614 } else if (EXTRA_BODY.equals(extra)) {
1615 setBody(value, true /* with signature */);
Andrew Sappersteinbc5e0dc2013-08-07 21:15:22 -07001616 } else if (EXTRA_QUOTED_TEXT.equals(extra)) {
1617 initQuotedText(value, true /* shouldQuoteText */);
Mindy Pereira8eca57a2012-03-20 16:42:34 -07001618 }
1619 }
1620 }
1621
1622 Bundle extras = intent.getExtras();
1623 if (extras != null) {
Mindy Pereira8eca57a2012-03-20 16:42:34 -07001624 CharSequence text = extras.getCharSequence(Intent.EXTRA_TEXT);
Jin Caoa8f34ff2014-07-24 14:43:57 -07001625 setBody((text != null) ? text : "", true /* with signature */);
Andrew Sappersteinbc5e0dc2013-08-07 21:15:22 -07001626
1627 // TODO - support EXTRA_HTML_TEXT
Mindy Pereira8eca57a2012-03-20 16:42:34 -07001628 }
Andrew Sapperstein746d8612013-08-26 15:56:32 -07001629
1630 mExtraValues = intent.getParcelableExtra(EXTRA_VALUES);
1631 if (mExtraValues != null) {
1632 LogUtils.d(LOG_TAG, "Launched with extra values: %s", mExtraValues.toString());
1633 initExtraValues(mExtraValues);
1634 return true;
1635 }
1636
1637 return false;
1638 }
1639
1640 protected void initExtraValues(ContentValues extraValues) {
1641 // DO NOTHING - Gmail will override
Mindy Pereirae8f94dc2012-04-16 11:56:21 -07001642 }
1643
Andrew Sappersteinbc5e0dc2013-08-07 21:15:22 -07001644
Mindy Pereira8eca57a2012-03-20 16:42:34 -07001645 @VisibleForTesting
1646 protected String decodeEmailInUri(String s) throws UnsupportedEncodingException {
Mindy Pereiraa4069f22012-05-30 15:31:45 -07001647 // TODO: handle the case where there are spaces in the display name as
1648 // well as the email such as "Guy with spaces <guy+with+spaces@gmail.com>"
1649 // as they could be encoded ambiguously.
Mindy Pereira8eca57a2012-03-20 16:42:34 -07001650 // Since URLDecode.decode changes + into ' ', and + is a valid
1651 // email character, we need to find/ replace these ourselves before
1652 // decoding.
Mindy Pereiraa4069f22012-05-30 15:31:45 -07001653 try {
Yorke Lee7dd05b12013-04-25 10:04:43 -07001654 return URLDecoder.decode(replacePlus(s), UTF8_ENCODING_NAME);
Mindy Pereiraa4069f22012-05-30 15:31:45 -07001655 } catch (IllegalArgumentException e) {
1656 if (LogUtils.isLoggable(LOG_TAG, LogUtils.VERBOSE)) {
1657 LogUtils.e(LOG_TAG, "%s while decoding '%s'", e.getMessage(), s);
1658 } else {
1659 LogUtils.e(LOG_TAG, e, "Exception while decoding mailto address");
1660 }
1661 return null;
1662 }
Mindy Pereira8eca57a2012-03-20 16:42:34 -07001663 }
1664
1665 /**
Yorke Lee7dd05b12013-04-25 10:04:43 -07001666 * Replaces all occurrences of '+' with "%2B", to prevent URLDecode.decode from
1667 * changing '+' into ' '
1668 *
1669 * @param toReplace Input string
1670 * @return The string with all "+" characters replaced with "%2B"
1671 */
Scott Kennedy3b965d72013-06-25 14:36:55 -07001672 private static String replacePlus(String toReplace) {
Yorke Lee7dd05b12013-04-25 10:04:43 -07001673 return toReplace.replace("+", "%2B");
1674 }
1675
1676 /**
Jin Caod67d7e32014-03-26 16:49:48 -07001677 * Replaces all occurrences of '%' with "%25", to prevent URLDecode.decode from
1678 * crashing on decoded '%' symbols
1679 *
1680 * @param toReplace Input string
1681 * @return The string with all "%" characters replaced with "%25"
1682 */
1683 private static String replacePercent(String toReplace) {
1684 return toReplace.replace("%", "%25");
1685 }
1686
1687 /**
1688 * Helper function to encapsulate encoding/decoding string from Uri.getQueryParameters
1689 * @param content Input string
1690 * @return The string that's properly escaped to be shown in mail subject/content
1691 */
1692 private static String decodeContentFromQueryParam(String content) {
1693 try {
1694 return URLDecoder.decode(replacePlus(replacePercent(content)), UTF8_ENCODING_NAME);
1695 } catch (UnsupportedEncodingException e) {
1696 LogUtils.e(LOG_TAG, "%s while decoding '%s'", e.getMessage(), content);
1697 return ""; // Default to empty string so setText/setBody has same behavior as before.
1698 }
1699 }
1700
1701 /**
Mindy Pereira8eca57a2012-03-20 16:42:34 -07001702 * Initialize the compose view from a String representing a mailTo uri.
1703 * @param mailToString The uri as a string.
1704 */
1705 public void initFromMailTo(String mailToString) {
1706 // We need to disguise this string as a URI in order to parse it
1707 // TODO: Remove this hack when http://b/issue?id=1445295 gets fixed
1708 Uri uri = Uri.parse("foo://" + mailToString);
1709 int index = mailToString.indexOf("?");
1710 int length = "mailto".length() + 1;
1711 String to;
1712 try {
1713 // Extract the recipient after mailto:
1714 if (index == -1) {
1715 to = decodeEmailInUri(mailToString.substring(length));
1716 } else {
1717 to = decodeEmailInUri(mailToString.substring(length, index));
1718 }
Mindy Pereiraa4069f22012-05-30 15:31:45 -07001719 if (!TextUtils.isEmpty(to)) {
1720 addToAddresses(Arrays.asList(TextUtils.split(to, ",")));
1721 }
Mindy Pereira8eca57a2012-03-20 16:42:34 -07001722 } catch (UnsupportedEncodingException e) {
1723 if (LogUtils.isLoggable(LOG_TAG, LogUtils.VERBOSE)) {
1724 LogUtils.e(LOG_TAG, "%s while decoding '%s'", e.getMessage(), mailToString);
1725 } else {
1726 LogUtils.e(LOG_TAG, e, "Exception while decoding mailto address");
1727 }
1728 }
1729
1730 List<String> cc = uri.getQueryParameters("cc");
1731 addCcAddresses(Arrays.asList(cc.toArray(new String[cc.size()])), null);
1732
1733 List<String> otherTo = uri.getQueryParameters("to");
1734 addToAddresses(Arrays.asList(otherTo.toArray(new String[otherTo.size()])));
1735
1736 List<String> bcc = uri.getQueryParameters("bcc");
1737 addBccAddresses(Arrays.asList(bcc.toArray(new String[bcc.size()])));
1738
Jin Caod67d7e32014-03-26 16:49:48 -07001739 // NOTE: Uri.getQueryParameters already decodes % encoded characters
Mindy Pereira8eca57a2012-03-20 16:42:34 -07001740 List<String> subject = uri.getQueryParameters("subject");
1741 if (subject.size() > 0) {
Jin Caod67d7e32014-03-26 16:49:48 -07001742 mSubject.setText(decodeContentFromQueryParam(subject.get(0)));
Mindy Pereira8eca57a2012-03-20 16:42:34 -07001743 }
1744
1745 List<String> body = uri.getQueryParameters("body");
1746 if (body.size() > 0) {
Jin Caod67d7e32014-03-26 16:49:48 -07001747 setBody(decodeContentFromQueryParam(body.get(0)), true /* with signature */);
Mindy Pereira8eca57a2012-03-20 16:42:34 -07001748 }
Mindy Pereira8eca57a2012-03-20 16:42:34 -07001749 }
1750
Mindy Pereirabddd6f32012-06-20 12:10:03 -07001751 @VisibleForTesting
1752 protected void initAttachments(Message refMessage) {
Mark Wei434f2942012-08-24 11:54:02 -07001753 addAttachments(refMessage.getAttachments());
1754 }
1755
1756 public long addAttachments(List<Attachment> attachments) {
1757 long size = 0;
1758 AttachmentFailureException error = null;
1759 for (Attachment a : attachments) {
1760 try {
1761 size += mAttachmentsView.addAttachment(mAccount, a);
1762 } catch (AttachmentFailureException e) {
1763 error = e;
1764 }
Mindy Pereira3cd4f402012-07-17 11:16:18 -07001765 }
Mark Wei434f2942012-08-24 11:54:02 -07001766 if (error != null) {
1767 LogUtils.e(LOG_TAG, error, "Error adding attachment");
1768 if (attachments.size() > 1) {
1769 showAttachmentTooBigToast(R.string.too_large_to_attach_multiple);
1770 } else {
1771 showAttachmentTooBigToast(error.getErrorRes());
1772 }
1773 }
1774 return size;
Mindy Pereira3cd4f402012-07-17 11:16:18 -07001775 }
1776
1777 /**
1778 * When an attachment is too large to be added to a message, show a toast.
1779 * This method also updates the position of the toast so that it is shown
1780 * clearly above they keyboard if it happens to be open.
1781 */
Mark Wei434f2942012-08-24 11:54:02 -07001782 private void showAttachmentTooBigToast(int errorRes) {
1783 String maxSize = AttachmentUtils.convertToHumanReadableSize(
1784 getApplicationContext(), mAccount.settings.getMaxAttachmentSize());
1785 showErrorToast(getString(errorRes, maxSize));
Mindy Pereira3cd4f402012-07-17 11:16:18 -07001786 }
1787
Mark Wei434f2942012-08-24 11:54:02 -07001788 private void showErrorToast(String message) {
1789 Toast t = Toast.makeText(this, message, Toast.LENGTH_LONG);
1790 t.setText(message);
Mindy Pereira3cd4f402012-07-17 11:16:18 -07001791 t.setGravity(Gravity.CENTER_HORIZONTAL, 0,
1792 getResources().getDimensionPixelSize(R.dimen.attachment_toast_yoffset));
1793 t.show();
Mindy Pereiraeaea9f12012-01-10 15:05:27 -08001794 }
1795
Paul Westbrookf97588b2012-03-20 11:11:37 -07001796 private void initAttachmentsFromIntent(Intent intent) {
Paul Westbrook03ee9712012-04-02 09:51:51 -07001797 Bundle extras = intent.getExtras();
1798 if (extras == null) {
1799 extras = Bundle.EMPTY;
1800 }
Paul Westbrookf97588b2012-03-20 11:11:37 -07001801 final String action = intent.getAction();
1802 if (!mAttachmentsChanged) {
1803 long totalSize = 0;
1804 if (extras.containsKey(EXTRA_ATTACHMENTS)) {
Jin Caoadea2c82014-08-28 12:25:52 -07001805 final String[] uris = (String[]) extras.getSerializable(EXTRA_ATTACHMENTS);
1806 final ArrayList<Uri> parsedUris = Lists.newArrayListWithCapacity(uris.length);
1807 for (String uri : uris) {
1808 parsedUris.add(Uri.parse(uri));
Paul Westbrookf97588b2012-03-20 11:11:37 -07001809 }
Jin Caoadea2c82014-08-28 12:25:52 -07001810 totalSize += handleAttachmentUrisFromIntent(parsedUris);
Paul Westbrookf97588b2012-03-20 11:11:37 -07001811 }
mindyp9a9e8d62012-10-03 12:24:07 -07001812 if (extras.containsKey(Intent.EXTRA_STREAM)) {
1813 if (Intent.ACTION_SEND_MULTIPLE.equals(action)) {
Andy Huang91ede362014-01-21 19:16:00 -08001814 final ArrayList<Uri> uris = extras
mindyp9a9e8d62012-10-03 12:24:07 -07001815 .getParcelableArrayList(Intent.EXTRA_STREAM);
Jin Caoadea2c82014-08-28 12:25:52 -07001816 totalSize += handleAttachmentUrisFromIntent(uris);
mindyp9a9e8d62012-10-03 12:24:07 -07001817 } else {
Tony Mantler581edd42014-02-18 15:41:22 -08001818 final Uri uri = extras.getParcelable(Intent.EXTRA_STREAM);
Jin Caoadea2c82014-08-28 12:25:52 -07001819 final ArrayList<Uri> uris = Lists.newArrayList(uri);
1820 totalSize += handleAttachmentUrisFromIntent(uris);
Paul Westbrookf97588b2012-03-20 11:11:37 -07001821 }
1822 }
1823
1824 if (totalSize > 0) {
1825 mAttachmentsChanged = true;
1826 updateSaveUi();
Andy Huange003b4c2013-08-16 10:32:05 -07001827
1828 Analytics.getInstance().sendEvent("send_intent_with_attachments",
1829 Integer.toString(getAttachments().size()), null, totalSize);
Paul Westbrookf97588b2012-03-20 11:11:37 -07001830 }
1831 }
1832 }
1833
Jin Caoadea2c82014-08-28 12:25:52 -07001834 /**
1835 * Helper function to handle a list of uris to attach.
1836 * @return the total size of all successfully attached files.
1837 */
1838 private long handleAttachmentUrisFromIntent(List<Uri> uris) {
1839 ArrayList<Attachment> attachments = Lists.newArrayList();
1840 for (Uri uri : uris) {
1841 try {
1842 if (uri != null) {
1843 if ("file".equals(uri.getScheme())) {
1844 final File f = new File(uri.getPath());
1845 // We should not be attaching any files from the data directory.
1846 if (f.getCanonicalPath().startsWith(DATA_DIRECTORY_ROOT)) {
1847 showErrorToast(getString(R.string.attachment_permission_denied));
1848 continue;
1849 }
1850 }
1851 if (!handleSpecialAttachmentUri(uri)) {
1852 final Attachment a = mAttachmentsView.generateLocalAttachment(uri);
1853 attachments.add(a);
1854
1855 Analytics.getInstance().sendEvent("send_intent_attachment",
1856 Utils.normalizeMimeType(a.getContentType()), null, a.size);
1857 }
1858 }
1859 } catch (AttachmentFailureException e) {
1860 LogUtils.e(LOG_TAG, e, "Error adding attachment");
1861 showAttachmentTooBigToast(e.getErrorRes());
1862 } catch (IOException | SecurityException e) {
1863 showErrorToast(getString(R.string.attachment_permission_denied));
1864 }
1865 }
1866 return addAttachments(attachments);
1867 }
1868
Andrew Sapperstein746d8612013-08-26 15:56:32 -07001869 protected void initQuotedText(CharSequence quotedText, boolean shouldQuoteText) {
Andrew Sappersteinbc5e0dc2013-08-07 21:15:22 -07001870 mQuotedTextView.setQuotedTextFromHtml(quotedText, shouldQuoteText);
1871 mShowQuotedText = true;
1872 }
Paul Westbrookf97588b2012-03-20 11:11:37 -07001873
Mindy Pereirae8f94dc2012-04-16 11:56:21 -07001874 private void initQuotedTextFromRefMessage(Message refMessage, int action) {
1875 if (mRefMessage != null && (action == REPLY || action == REPLY_ALL || action == FORWARD)) {
Mindy Pereira9932dee2012-01-10 16:09:50 -08001876 mQuotedTextView.setQuotedText(action, refMessage, action != FORWARD);
1877 }
Mindy Pereira46ce0b12012-01-05 10:32:15 -08001878 }
1879
1880 private void updateHideOrShowCcBcc() {
Mindy Pereiraec8b0ed2012-01-06 10:35:59 -08001881 // Its possible there is a menu item OR a button.
Mindy Pereira326689d2012-05-17 10:14:14 -07001882 boolean ccVisible = mCcBccView.isCcVisible();
1883 boolean bccVisible = mCcBccView.isBccVisible();
Mindy Pereiraec8b0ed2012-01-06 10:35:59 -08001884 if (mCcBccButton != null) {
Mindy Pereira326689d2012-05-17 10:14:14 -07001885 if (!ccVisible || !bccVisible) {
Mindy Pereiraec8b0ed2012-01-06 10:35:59 -08001886 mCcBccButton.setVisibility(View.VISIBLE);
Mindy Pereiraec8b0ed2012-01-06 10:35:59 -08001887 } else {
Jin Cao9d358a12014-07-24 12:15:38 -07001888 mCcBccButton.setVisibility(View.GONE);
Mindy Pereiraec8b0ed2012-01-06 10:35:59 -08001889 }
1890 }
Mindy Pereira46ce0b12012-01-05 10:32:15 -08001891 }
1892
Mindy Pereira013194c2012-01-06 15:09:33 -08001893 /**
1894 * Add attachment and update the compose area appropriately.
Mindy Pereira013194c2012-01-06 15:09:33 -08001895 */
Andrew Sapperstein865ae9c2014-02-10 18:23:48 -08001896 private void addAttachmentAndUpdateView(Intent data) {
Andrew Sapperstein05089f32013-10-01 17:00:03 -07001897 if (data == null) {
1898 return;
1899 }
1900
1901 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
1902 final ClipData clipData = data.getClipData();
1903 if (clipData != null) {
1904 for (int i = 0, size = clipData.getItemCount(); i < size; i++) {
1905 addAttachmentAndUpdateView(clipData.getItemAt(i).getUri());
1906 }
1907 return;
1908 }
1909 }
1910
1911 addAttachmentAndUpdateView(data.getData());
Mindy Pereira2421dc82012-03-27 13:32:31 -07001912 }
1913
Andrew Sapperstein865ae9c2014-02-10 18:23:48 -08001914 private void addAttachmentAndUpdateView(Uri contentUri) {
Andy Huang5c5fd572012-04-08 18:19:29 -07001915 if (contentUri == null) {
Mindy Pereira2421dc82012-03-27 13:32:31 -07001916 return;
1917 }
Mindy Pereira013194c2012-01-06 15:09:33 -08001918 try {
Andy Huang91ede362014-01-21 19:16:00 -08001919
1920 if (handleSpecialAttachmentUri(contentUri)) {
1921 return;
1922 }
1923
Andy Huang5c5fd572012-04-08 18:19:29 -07001924 addAttachmentAndUpdateView(mAttachmentsView.generateLocalAttachment(contentUri));
1925 } catch (AttachmentFailureException e) {
Andy Huang5c5fd572012-04-08 18:19:29 -07001926 LogUtils.e(LOG_TAG, e, "Error adding attachment");
Mark Wei434f2942012-08-24 11:54:02 -07001927 showErrorToast(getResources().getString(
1928 e.getErrorRes(),
1929 AttachmentUtils.convertToHumanReadableSize(
1930 getApplicationContext(), mAccount.settings.getMaxAttachmentSize())));
Andy Huang5c5fd572012-04-08 18:19:29 -07001931 }
1932 }
1933
Andy Huang91ede362014-01-21 19:16:00 -08001934 /**
1935 * Allow subclasses to implement custom handling of attachments.
1936 *
1937 * @param contentUri a passed-in URI from a pick intent
1938 * @return true iff handled
1939 */
1940 protected boolean handleSpecialAttachmentUri(final Uri contentUri) {
1941 return false;
1942 }
1943
Andrew Sapperstein865ae9c2014-02-10 18:23:48 -08001944 private void addAttachmentAndUpdateView(Attachment attachment) {
Andy Huang5c5fd572012-04-08 18:19:29 -07001945 try {
Mark Wei434f2942012-08-24 11:54:02 -07001946 long size = mAttachmentsView.addAttachment(mAccount, attachment);
Mindy Pereira9932dee2012-01-10 16:09:50 -08001947 if (size > 0) {
1948 mAttachmentsChanged = true;
1949 updateSaveUi();
Mindy Pereira013194c2012-01-06 15:09:33 -08001950 }
Mindy Pereira9932dee2012-01-10 16:09:50 -08001951 } catch (AttachmentFailureException e) {
Mindy Pereira9932dee2012-01-10 16:09:50 -08001952 LogUtils.e(LOG_TAG, e, "Error adding attachment");
Mark Wei434f2942012-08-24 11:54:02 -07001953 showAttachmentTooBigToast(e.getErrorRes());
Mindy Pereira013194c2012-01-06 15:09:33 -08001954 }
1955 }
1956
Scott Kennedy0aeaf7d2012-11-14 18:56:05 -08001957 void initRecipientsFromRefMessage(Message refMessage, int action) {
Mindy Pereira4a27ea92012-01-05 15:55:25 -08001958 // Don't populate the address if this is a forward.
1959 if (action == ComposeActivity.FORWARD) {
1960 return;
1961 }
Scott Kennedyff8553f2013-04-05 20:57:44 -07001962 initReplyRecipients(refMessage, action);
Mindy Pereira4a27ea92012-01-05 15:55:25 -08001963 }
Mindy Pereira46ce0b12012-01-05 10:32:15 -08001964
Paul Westbrook6d2442b2013-07-17 17:51:51 -07001965 // TODO: This should be private. This method shouldn't be used by ComposeActivityTests, as
1966 // it doesn't setup the state of the activity correctly
Mindy Pereira818143e2012-01-11 13:59:49 -08001967 @VisibleForTesting
Scott Kennedyff8553f2013-04-05 20:57:44 -07001968 void initReplyRecipients(final Message refMessage, final int action) {
Tony Mantler9016a5e2013-07-19 11:54:17 -07001969 String[] sentToAddresses = refMessage.getToAddressesUnescaped();
Mindy Pereiraa26b54e2012-01-06 12:54:33 -08001970 final Collection<String> toAddresses;
Tony Mantler89de9eb2013-07-25 11:43:58 -07001971 final String[] fromAddresses = refMessage.getFromAddressesUnescaped();
1972 final String fromAddress = fromAddresses.length > 0 ? fromAddresses[0] : null;
Andy Huange2af8872014-01-16 12:36:27 -08001973 final String[] replyToAddresses = getReplyToAddresses(
1974 refMessage.getReplyToAddressesUnescaped(), fromAddress);
Mindy Pereira4a27ea92012-01-05 15:55:25 -08001975
1976 // If this is a reply, the Cc list is empty. If this is a reply-all, the
1977 // Cc list is the union of the To and Cc recipients of the original
1978 // message, excluding the current user's email address and any addresses
Mindy Pereiraa26b54e2012-01-06 12:54:33 -08001979 // already on the To list.
1980 if (action == ComposeActivity.REPLY) {
Tony Mantler24f116f2014-01-16 10:20:50 -08001981 toAddresses = initToRecipients(fromAddress, replyToAddresses, sentToAddresses);
Mindy Pereiraa26b54e2012-01-06 12:54:33 -08001982 addToAddresses(toAddresses);
1983 } else if (action == ComposeActivity.REPLY_ALL) {
Mindy Pereira4a27ea92012-01-05 15:55:25 -08001984 final Set<String> ccAddresses = Sets.newHashSet();
Tony Mantler24f116f2014-01-16 10:20:50 -08001985 toAddresses = initToRecipients(fromAddress, replyToAddresses, sentToAddresses);
Mindy Pereira154386a2012-01-11 13:02:33 -08001986 addToAddresses(toAddresses);
Scott Kennedyff8553f2013-04-05 20:57:44 -07001987 addRecipients(ccAddresses, sentToAddresses);
Tony Mantler9016a5e2013-07-19 11:54:17 -07001988 addRecipients(ccAddresses, refMessage.getCcAddressesUnescaped());
Mindy Pereira4a27ea92012-01-05 15:55:25 -08001989 addCcAddresses(ccAddresses, toAddresses);
1990 }
1991 }
1992
Andy Huange2af8872014-01-16 12:36:27 -08001993 // If there is no reply to address, the reply to address is the sender.
1994 private static String[] getReplyToAddresses(String[] replyTo, String from) {
1995 boolean hasReplyTo = false;
1996 for (final String replyToAddress : replyTo) {
1997 if (!TextUtils.isEmpty(replyToAddress)) {
1998 hasReplyTo = true;
1999 }
2000 }
2001 return hasReplyTo ? replyTo : new String[] {from};
2002 }
2003
Mindy Pereira4a27ea92012-01-05 15:55:25 -08002004 private void addToAddresses(Collection<String> addresses) {
2005 addAddressesToList(addresses, mTo);
2006 }
2007
2008 private void addCcAddresses(Collection<String> addresses, Collection<String> toAddresses) {
Mindy Pereira8eca57a2012-03-20 16:42:34 -07002009 addCcAddressesToList(tokenizeAddressList(addresses),
2010 toAddresses != null ? tokenizeAddressList(toAddresses) : null, mCc);
Mindy Pereira4a27ea92012-01-05 15:55:25 -08002011 }
2012
Paul Westbrookbb87b7f2012-03-20 16:20:07 -07002013 private void addBccAddresses(Collection<String> addresses) {
2014 addAddressesToList(addresses, mBcc);
2015 }
2016
Mindy Pereira4a27ea92012-01-05 15:55:25 -08002017 @VisibleForTesting
2018 protected void addCcAddressesToList(List<Rfc822Token[]> addresses,
2019 List<Rfc822Token[]> compareToList, RecipientEditTextView list) {
2020 String address;
2021
Mindy Pereira8eca57a2012-03-20 16:42:34 -07002022 if (compareToList == null) {
Tony Mantler581edd42014-02-18 15:41:22 -08002023 for (final Rfc822Token[] tokens : addresses) {
2024 for (final Rfc822Token token : tokens) {
2025 address = token.toString();
Mindy Pereira4a27ea92012-01-05 15:55:25 -08002026 list.append(address + END_TOKEN);
2027 }
2028 }
Mindy Pereira8eca57a2012-03-20 16:42:34 -07002029 } else {
2030 HashSet<String> compareTo = convertToHashSet(compareToList);
Tony Mantler581edd42014-02-18 15:41:22 -08002031 for (final Rfc822Token[] tokens : addresses) {
2032 for (final Rfc822Token token : tokens) {
2033 address = token.toString();
Mindy Pereira8eca57a2012-03-20 16:42:34 -07002034 // Check if this is a duplicate:
Tony Mantler581edd42014-02-18 15:41:22 -08002035 if (!compareTo.contains(token.getAddress())) {
Mindy Pereira8eca57a2012-03-20 16:42:34 -07002036 // Get the address here
2037 list.append(address + END_TOKEN);
2038 }
2039 }
2040 }
Mindy Pereira4a27ea92012-01-05 15:55:25 -08002041 }
2042 }
2043
Scott Kennedyff8553f2013-04-05 20:57:44 -07002044 private static HashSet<String> convertToHashSet(final List<Rfc822Token[]> list) {
2045 final HashSet<String> hash = new HashSet<String>();
2046 for (final Rfc822Token[] tokens : list) {
Tony Mantler581edd42014-02-18 15:41:22 -08002047 for (final Rfc822Token token : tokens) {
2048 hash.add(token.getAddress());
Mindy Pereira4a27ea92012-01-05 15:55:25 -08002049 }
2050 }
2051 return hash;
2052 }
2053
Mindy Pereira4a27ea92012-01-05 15:55:25 -08002054 protected List<Rfc822Token[]> tokenizeAddressList(Collection<String> addresses) {
2055 @VisibleForTesting
2056 List<Rfc822Token[]> tokenized = new ArrayList<Rfc822Token[]>();
2057
2058 for (String address: addresses) {
2059 tokenized.add(Rfc822Tokenizer.tokenize(address));
2060 }
2061 return tokenized;
2062 }
2063
2064 @VisibleForTesting
2065 void addAddressesToList(Collection<String> addresses, RecipientEditTextView list) {
2066 for (String address : addresses) {
2067 addAddressToList(address, list);
2068 }
2069 }
2070
Scott Kennedyff8553f2013-04-05 20:57:44 -07002071 private static void addAddressToList(final String address, final RecipientEditTextView list) {
Mindy Pereira4a27ea92012-01-05 15:55:25 -08002072 if (address == null || list == null)
2073 return;
2074
Scott Kennedyff8553f2013-04-05 20:57:44 -07002075 final Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(address);
Mindy Pereira4a27ea92012-01-05 15:55:25 -08002076
Tony Mantler581edd42014-02-18 15:41:22 -08002077 for (final Rfc822Token token : tokens) {
2078 list.append(token + END_TOKEN);
Mindy Pereira4a27ea92012-01-05 15:55:25 -08002079 }
2080 }
2081
2082 @VisibleForTesting
Scott Kennedyff8553f2013-04-05 20:57:44 -07002083 protected Collection<String> initToRecipients(final String fullSenderAddress,
Tony Mantler24f116f2014-01-16 10:20:50 -08002084 final String[] replyToAddresses, final String[] inToAddresses) {
Mindy Pereira4a27ea92012-01-05 15:55:25 -08002085 // The To recipient is the reply-to address specified in the original
2086 // message, unless it is:
2087 // the current user OR a custom from of the current user, in which case
2088 // it's the To recipient list of the original message.
2089 // OR missing, in which case use the sender of the original message
2090 Set<String> toAddresses = Sets.newHashSet();
Tony Mantler24f116f2014-01-16 10:20:50 -08002091 for (final String replyToAddress : replyToAddresses) {
2092 if (!TextUtils.isEmpty(replyToAddress)
2093 && !recipientMatchesThisAccount(replyToAddress)) {
2094 toAddresses.add(replyToAddress);
2095 }
2096 }
2097 if (toAddresses.size() == 0) {
mindyp65b06f52012-11-21 10:35:08 -08002098 // In this case, the user is replying to a message in which their
Tony Mantler24f116f2014-01-16 10:20:50 -08002099 // current account or some of their custom from addresses are the only
2100 // recipients and they sent the original message.
mindyp65b06f52012-11-21 10:35:08 -08002101 if (inToAddresses.length == 1 && recipientMatchesThisAccount(fullSenderAddress)
2102 && recipientMatchesThisAccount(inToAddresses[0])) {
2103 toAddresses.add(inToAddresses[0]);
2104 return toAddresses;
2105 }
2106 // This happens if the user replies to a message they originally
2107 // wrote. In this case, "reply" really means "re-send," so we
2108 // target the original recipients. This works as expected even
2109 // if the user sent the original message to themselves.
2110 for (String address : inToAddresses) {
2111 if (!recipientMatchesThisAccount(address)) {
2112 toAddresses.add(address);
mindypfe8557b2012-11-05 12:05:16 -08002113 }
Mindy Pereira1469b4e2012-06-19 19:18:54 -07002114 }
Mindy Pereira4a27ea92012-01-05 15:55:25 -08002115 }
2116 return toAddresses;
2117 }
2118
Scott Kennedyff8553f2013-04-05 20:57:44 -07002119 private void addRecipients(final Set<String> recipients, final String[] addresses) {
2120 for (final String email : addresses) {
Mindy Pereiracecc54a2012-07-31 09:38:11 -07002121 // Do not add this account, or any of its custom from addresses, to
2122 // the list of recipients.
Mindy Pereira4a20b702012-01-05 16:24:24 -08002123 final String recipientAddress = Address.getEmailAddress(email).getAddress();
mindyp5ee5d692012-11-19 16:02:16 -08002124 if (!recipientMatchesThisAccount(recipientAddress)) {
Mindy Pereira4a27ea92012-01-05 15:55:25 -08002125 recipients.add(email.replace("\"\"", ""));
2126 }
2127 }
Mindy Pereira46ce0b12012-01-05 10:32:15 -08002128 }
2129
Mindy Pereiracecc54a2012-07-31 09:38:11 -07002130 /**
2131 * A recipient matches this account if it has the same address as the
2132 * currently selected account OR one of the custom from addresses associated
2133 * with the currently selected account.
Mindy Pereiracecc54a2012-07-31 09:38:11 -07002134 * @param recipientAddress address we are comparing with the currently selected account
Mindy Pereiracecc54a2012-07-31 09:38:11 -07002135 */
mindyp5ee5d692012-11-19 16:02:16 -08002136 protected boolean recipientMatchesThisAccount(String recipientAddress) {
2137 return ReplyFromAccount.matchesAccountOrCustomFrom(mAccount, recipientAddress,
mindypfe8557b2012-11-05 12:05:16 -08002138 mAccount.getReplyFroms());
Mindy Pereiracecc54a2012-07-31 09:38:11 -07002139 }
2140
Andrew Sappersteinbc5e0dc2013-08-07 21:15:22 -07002141 /**
2142 * Returns a formatted subject string with the appropriate prefix for the action type.
2143 * E.g., "FWD: " is prepended if action is {@link ComposeActivity#FORWARD}.
2144 */
Andrew Sapperstein7e04f142014-06-11 13:43:07 -07002145 public static String buildFormattedSubject(Resources res, String subject, int action) {
Tony Mantler41c3a252014-06-30 11:00:43 -07002146 final String prefix;
2147 final String correctedSubject;
Mindy Pereira46ce0b12012-01-05 10:32:15 -08002148 if (action == ComposeActivity.COMPOSE) {
2149 prefix = "";
2150 } else if (action == ComposeActivity.FORWARD) {
Andrew Sappersteinbc5e0dc2013-08-07 21:15:22 -07002151 prefix = res.getString(R.string.forward_subject_label);
Mindy Pereira46ce0b12012-01-05 10:32:15 -08002152 } else {
Andrew Sappersteinbc5e0dc2013-08-07 21:15:22 -07002153 prefix = res.getString(R.string.reply_subject_label);
Mindy Pereira46ce0b12012-01-05 10:32:15 -08002154 }
2155
Tony Mantler41c3a252014-06-30 11:00:43 -07002156 if (TextUtils.isEmpty(subject)) {
2157 correctedSubject = prefix;
Mindy Pereira46ce0b12012-01-05 10:32:15 -08002158 } else {
Tony Mantler41c3a252014-06-30 11:00:43 -07002159 // Don't duplicate the prefix
2160 if (subject.toLowerCase().startsWith(prefix.toLowerCase())) {
2161 correctedSubject = subject;
2162 } else {
2163 correctedSubject = String.format(
2164 res.getString(R.string.formatted_subject), prefix, subject);
2165 }
Mindy Pereira46ce0b12012-01-05 10:32:15 -08002166 }
Andrew Sappersteinbc5e0dc2013-08-07 21:15:22 -07002167
2168 return correctedSubject;
2169 }
2170
2171 private void setSubject(Message refMessage, int action) {
2172 mSubject.setText(buildFormattedSubject(getResources(), refMessage.subject, action));
Mindy Pereira46ce0b12012-01-05 10:32:15 -08002173 }
2174
Mindy Pereira818143e2012-01-11 13:59:49 -08002175 private void initRecipients() {
2176 setupRecipients(mTo);
2177 setupRecipients(mCc);
2178 setupRecipients(mBcc);
Mindy Pereiraeaea9f12012-01-10 15:05:27 -08002179 }
2180
Mindy Pereira818143e2012-01-11 13:59:49 -08002181 private void setupRecipients(RecipientEditTextView view) {
Andrew Sapperstein9afa8222014-06-23 16:19:23 -07002182 final DropdownChipLayouter layouter = getDropdownChipLayouter();
2183 if (layouter != null) {
2184 view.setDropdownChipLayouter(layouter);
Andrew Sappersteinf5ab8ac2014-05-02 15:14:54 -07002185 }
Andrew Sapperstein9afa8222014-06-23 16:19:23 -07002186 view.setAdapter(getRecipientAdapter());
Andrew Sappersteinffd61552014-05-14 15:04:23 -07002187 view.setRecipientEntryItemClickedListener(this);
Mindy Pereira82cc5662012-01-09 17:29:30 -08002188 if (mValidator == null) {
Tony Mantler79b11562013-10-09 15:31:50 -07002189 final String accountName = mAccount.getEmailAddress();
Mindy Pereira33fe9082012-01-09 16:24:30 -08002190 int offset = accountName.indexOf("@") + 1;
2191 String account = accountName;
Tony Mantler79b11562013-10-09 15:31:50 -07002192 if (offset > 0) {
2193 account = account.substring(offset);
Mindy Pereirac17d0732011-12-29 10:46:19 -08002194 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08002195 mValidator = new Rfc822Validator(account);
Mindy Pereirac17d0732011-12-29 10:46:19 -08002196 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08002197 view.setValidator(mValidator);
Mindy Pereira8e9305e2011-12-13 14:25:04 -08002198 }
2199
Andrew Sappersteinf5ab8ac2014-05-02 15:14:54 -07002200 /**
2201 * Derived classes should override if they wish to provide their own autocomplete behavior.
2202 */
2203 public BaseRecipientAdapter getRecipientAdapter() {
2204 return new RecipientAdapter(this, mAccount);
2205 }
2206
2207 /**
2208 * Derived classes should override this to provide their own dropdown behavior.
2209 * If the result is null, the default {@link com.android.ex.chips.DropdownChipLayouter}
2210 * is used.
2211 */
2212 public DropdownChipLayouter getDropdownChipLayouter() {
2213 return null;
2214 }
2215
Mindy Pereira8e9305e2011-12-13 14:25:04 -08002216 @Override
2217 public void onClick(View v) {
Scott Kennedy2b9d80e2013-07-30 23:03:45 -07002218 final int id = v.getId();
2219 if (id == R.id.add_cc_bcc) {
2220 // Verify that cc/ bcc aren't showing.
2221 // Animate in cc/bcc.
2222 showCcBccViews();
Mindy Pereira8e9305e2011-12-13 14:25:04 -08002223 }
2224 }
Mindy Pereirab47f3e22011-12-13 14:25:04 -08002225
2226 @Override
Jin Caoc5c550a2014-07-29 11:53:17 -07002227 public void onFocusChange (View v, boolean hasFocus) {
2228 final int id = v.getId();
2229 if (hasFocus && (id == R.id.subject || id == R.id.body)) {
2230 // Collapse cc/bcc iff both are empty
2231 final boolean showCcBccFields = !TextUtils.isEmpty(mCc.getText()) ||
2232 !TextUtils.isEmpty(mBcc.getText());
2233 mCcBccView.show(false /* animate */, showCcBccFields, showCcBccFields);
Jin Cao36e23872014-07-29 13:41:12 -07002234 mCcBccButton.setVisibility(showCcBccFields ? View.GONE : View.VISIBLE);
2235
2236 // On phones autoscroll down so that Cc aligns to the top if we are showing cc/bcc.
2237 if (getResources().getBoolean(R.bool.auto_scroll_cc) && showCcBccFields) {
2238 final int[] coords = new int[2];
2239 mCc.getLocationOnScreen(coords);
2240
2241 // Subtract status bar and action bar height from y-coord.
2242 final Rect rect = new Rect();
2243 getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
Andrew Sapperstein52882ff2014-07-27 12:30:18 -07002244 final int deltaY = coords[1] - getSupportActionBar().getHeight() - rect.top;
Jin Cao36e23872014-07-29 13:41:12 -07002245
2246 // Only scroll down
2247 if (deltaY > 0) {
2248 mScrollView.smoothScrollBy(0, deltaY);
2249 }
2250 }
Jin Caoc5c550a2014-07-29 11:53:17 -07002251 }
2252 }
2253
2254 @Override
Mindy Pereirab47f3e22011-12-13 14:25:04 -08002255 public boolean onCreateOptionsMenu(Menu menu) {
Tony Mantler5b8799a2013-10-31 10:43:03 -07002256 final boolean superCreated = super.onCreateOptionsMenu(menu);
Mindy Pereirab199d172012-08-13 11:04:03 -07002257 // Don't render any menu items when there are no accounts.
2258 if (mAccounts == null || mAccounts.length == 0) {
Tony Mantler5b8799a2013-10-31 10:43:03 -07002259 return superCreated;
Mindy Pereirab199d172012-08-13 11:04:03 -07002260 }
Mindy Pereirab47f3e22011-12-13 14:25:04 -08002261 MenuInflater inflater = getMenuInflater();
2262 inflater.inflate(R.menu.compose_menu, menu);
mindyp1d7e9142012-11-21 13:54:30 -08002263
2264 /*
2265 * Start save in the correct enabled state.
2266 * 1) If a user launches compose from within gmail, save is disabled
2267 * until they add something, at which point, save is enabled, auto save
2268 * on exit; if the user empties everything, save is disabled, exiting does not
2269 * auto-save
2270 * 2) if a user replies/ reply all/ forwards from within gmail, save is
2271 * disabled until they change something, at which point, save is
2272 * enabled, auto save on exit; if the user empties everything, save is
2273 * disabled, exiting does not auto-save.
2274 * 3) If a user launches compose from another application and something
2275 * gets populated (attachments, recipients, body, subject, etc), save is
2276 * enabled, auto save on exit; if the user empties everything, save is
2277 * disabled, exiting does not auto-save
2278 */
Mindy Pereira82cc5662012-01-09 17:29:30 -08002279 mSave = menu.findItem(R.id.save);
mindyp1d7e9142012-11-21 13:54:30 -08002280 String action = getIntent() != null ? getIntent().getAction() : null;
Andy Huang9f855d62013-05-30 17:15:03 -07002281 enableSave(mInnerSavedState != null ?
2282 mInnerSavedState.getBoolean(EXTRA_SAVE_ENABLED)
mindyp1d7e9142012-11-21 13:54:30 -08002283 : (Intent.ACTION_SEND.equals(action)
2284 || Intent.ACTION_SEND_MULTIPLE.equals(action)
2285 || Intent.ACTION_SENDTO.equals(action)
2286 || shouldSave()));
2287
Greg Bullockd47a7042014-08-13 16:02:59 +02002288 final MenuItem helpItem = menu.findItem(R.id.help_info_menu_item);
2289 final MenuItem sendFeedbackItem = menu.findItem(R.id.feedback_menu_item);
2290 final MenuItem attachFromServiceItem = menu.findItem(R.id.attach_from_service_stub1);
Mindy Pereira3ca5bad2012-04-16 11:02:42 -07002291 if (helpItem != null) {
2292 helpItem.setVisible(mAccount != null
2293 && mAccount.supportsCapability(AccountCapabilities.HELP_CONTENT));
2294 }
2295 if (sendFeedbackItem != null) {
2296 sendFeedbackItem.setVisible(mAccount != null
2297 && mAccount.supportsCapability(AccountCapabilities.SEND_FEEDBACK));
2298 }
Greg Bullockd47a7042014-08-13 16:02:59 +02002299 if (attachFromServiceItem != null) {
2300 attachFromServiceItem.setVisible(shouldEnableAttachFromServiceMenu(mAccount));
2301 }
Andrew Sapperstein5cb71802013-10-01 18:31:20 -07002302
Andrew Sapperstein8809f9f2013-10-11 16:13:35 -07002303 // Show attach picture on pre-K devices.
2304 menu.findItem(R.id.add_photo_attachment).setVisible(!Utils.isRunningKitkatOrLater());
Andrew Sapperstein5cb71802013-10-01 18:31:20 -07002305
Mindy Pereirab47f3e22011-12-13 14:25:04 -08002306 return true;
2307 }
2308
2309 @Override
2310 public boolean onOptionsItemSelected(MenuItem item) {
Scott Kennedy2b9d80e2013-07-30 23:03:45 -07002311 final int id = item.getItemId();
Andy Huangdc97bf42013-08-15 16:52:45 -07002312
Andy Huangf8c59b02014-03-19 20:00:53 -07002313 Analytics.getInstance().sendMenuItemEvent(Analytics.EVENT_CATEGORY_MENU_ITEM, id,
2314 "compose", 0);
Andy Huangdc97bf42013-08-15 16:52:45 -07002315
Mindy Pereira75f66632012-01-11 11:42:02 -08002316 boolean handled = true;
Andrew Sapperstein5cb71802013-10-01 18:31:20 -07002317 if (id == R.id.add_file_attachment) {
2318 doAttach(MIME_TYPE_ALL);
2319 } else if (id == R.id.add_photo_attachment) {
Scott Kennedy2b9d80e2013-07-30 23:03:45 -07002320 doAttach(MIME_TYPE_PHOTO);
Scott Kennedy2b9d80e2013-07-30 23:03:45 -07002321 } else if (id == R.id.save) {
2322 doSave(true);
2323 } else if (id == R.id.send) {
2324 doSend();
2325 } else if (id == R.id.discard) {
2326 doDiscard();
2327 } else if (id == R.id.settings) {
2328 Utils.showSettings(this, mAccount);
2329 } else if (id == android.R.id.home) {
2330 onAppUpPressed();
2331 } else if (id == R.id.help_info_menu_item) {
2332 Utils.showHelp(this, mAccount, getString(R.string.compose_help_context));
Scott Kennedy2b9d80e2013-07-30 23:03:45 -07002333 } else {
2334 handled = false;
Mindy Pereirab47f3e22011-12-13 14:25:04 -08002335 }
Tony Mantler581edd42014-02-18 15:41:22 -08002336 return handled || super.onOptionsItemSelected(item);
Mindy Pereirab47f3e22011-12-13 14:25:04 -08002337 }
Mindy Pereira326c6602012-01-04 15:32:42 -08002338
Mindy Pereirab199d172012-08-13 11:04:03 -07002339 @Override
2340 public void onBackPressed() {
2341 // If we are showing the wait fragment, just exit.
2342 if (getWaitFragment() != null) {
2343 finish();
2344 } else {
2345 super.onBackPressed();
2346 }
2347 }
2348
Vikram Aggarwal1672ff82012-09-21 10:15:22 -07002349 /**
2350 * Carries out the "up" action in the action bar.
2351 */
Paul Westbrookdaecb4b2012-05-31 10:21:26 -07002352 private void onAppUpPressed() {
2353 if (mLaunchedFromEmail) {
2354 // If this was started from Gmail, simply treat app up as the system back button, so
2355 // that the last view is restored.
2356 onBackPressed();
2357 return;
2358 }
2359
2360 // Fire the main activity to ensure it launches the "top" screen of mail.
2361 // Since the main Activity is singleTask, it should revive that task if it was already
2362 // started.
Vikram Aggarwal0c3c2052012-09-21 11:06:28 -07002363 final Intent mailIntent = Utils.createViewInboxIntent(mAccount);
Paul Westbrookdaecb4b2012-05-31 10:21:26 -07002364 mailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK |
2365 Intent.FLAG_ACTIVITY_TASK_ON_HOME);
2366 startActivity(mailIntent);
2367 finish();
2368 }
2369
Mindy Pereira33fe9082012-01-09 16:24:30 -08002370 private void doSend() {
Mark Weidd19b632012-10-19 13:59:28 -07002371 sendOrSaveWithSanityChecks(false, true, false, false);
Andy Huangdc97bf42013-08-15 16:52:45 -07002372 logSendOrSave(false /* save */);
2373 mPerformedSendOrDiscard = true;
Mindy Pereira33fe9082012-01-09 16:24:30 -08002374 }
2375
Mindy Pereira48e31b02012-05-30 13:12:24 -07002376 private void doSave(boolean showToast) {
Mark Weidd19b632012-10-19 13:59:28 -07002377 sendOrSaveWithSanityChecks(true, showToast, false, false);
Mindy Pereira48e31b02012-05-30 13:12:24 -07002378 }
2379
Andrew Sappersteinffd61552014-05-14 15:04:23 -07002380 @Override
2381 public void onRecipientEntryItemClicked(int charactersTyped, int position) {
2382 // Send analytics of characters typed and position in dropdown selected.
2383 Analytics.getInstance().sendEvent(
Andrew Sapperstein9afa8222014-06-23 16:19:23 -07002384 "suggest_click", Integer.toString(charactersTyped), Integer.toString(position), 0);
Andrew Sappersteinffd61552014-05-14 15:04:23 -07002385 }
2386
Mindy Pereirae011b1d2012-06-18 13:45:26 -07002387 @VisibleForTesting
2388 public interface SendOrSaveCallback {
Andrew Sappersteinf5ab8ac2014-05-02 15:14:54 -07002389 void initializeSendOrSave(SendOrSaveTask sendOrSaveTask);
2390 void notifyMessageIdAllocated(SendOrSaveMessage sendOrSaveMessage, Message message);
2391 Message getMessage();
2392 void sendOrSaveFinished(SendOrSaveTask sendOrSaveTask, boolean success);
2393 void incrementRecipientsTimesContacted(List<String> recipients);
Mindy Pereira82cc5662012-01-09 17:29:30 -08002394 }
2395
Mindy Pereirae011b1d2012-06-18 13:45:26 -07002396 @VisibleForTesting
2397 public static class SendOrSaveTask implements Runnable {
Mindy Pereira82cc5662012-01-09 17:29:30 -08002398 private final Context mContext;
Mindy Pereira82cc5662012-01-09 17:29:30 -08002399 @VisibleForTesting
Mindy Pereirae011b1d2012-06-18 13:45:26 -07002400 public final SendOrSaveCallback mSendOrSaveCallback;
2401 @VisibleForTesting
2402 public final SendOrSaveMessage mSendOrSaveMessage;
mindyp44a63392012-11-05 12:05:16 -08002403 private ReplyFromAccount mExistingDraftAccount;
Mindy Pereira82cc5662012-01-09 17:29:30 -08002404
2405 public SendOrSaveTask(Context context, SendOrSaveMessage message,
mindyp44a63392012-11-05 12:05:16 -08002406 SendOrSaveCallback callback, ReplyFromAccount draftAccount) {
Mindy Pereira82cc5662012-01-09 17:29:30 -08002407 mContext = context;
2408 mSendOrSaveCallback = callback;
2409 mSendOrSaveMessage = message;
mindyp44a63392012-11-05 12:05:16 -08002410 mExistingDraftAccount = draftAccount;
Mindy Pereira82cc5662012-01-09 17:29:30 -08002411 }
2412
2413 @Override
2414 public void run() {
Mindy Pereira7ed1c112012-01-18 10:59:25 -08002415 final SendOrSaveMessage sendOrSaveMessage = mSendOrSaveMessage;
Mindy Pereira82cc5662012-01-09 17:29:30 -08002416
Mindy Pereira92551d02012-04-05 11:31:12 -07002417 final ReplyFromAccount selectedAccount = sendOrSaveMessage.mAccount;
Mindy Pereira7ed1c112012-01-18 10:59:25 -08002418 Message message = mSendOrSaveCallback.getMessage();
2419 long messageId = message != null ? message.id : UIProvider.INVALID_MESSAGE_ID;
Mindy Pereira82cc5662012-01-09 17:29:30 -08002420 // If a previous draft has been saved, in an account that is different
2421 // than what the user wants to send from, remove the old draft, and treat this
2422 // as a new message
mindyp44a63392012-11-05 12:05:16 -08002423 if (mExistingDraftAccount != null
2424 && !selectedAccount.account.uri.equals(mExistingDraftAccount.account.uri)) {
Mindy Pereira82cc5662012-01-09 17:29:30 -08002425 if (messageId != UIProvider.INVALID_MESSAGE_ID) {
2426 ContentResolver resolver = mContext.getContentResolver();
2427 ContentValues values = new ContentValues();
2428 values.put(BaseColumns._ID, messageId);
mindypfebd2262012-11-13 17:45:09 -08002429 if (mExistingDraftAccount.account.expungeMessageUri != null) {
2430 new ContentProviderTask.UpdateTask()
2431 .run(resolver, mExistingDraftAccount.account.expungeMessageUri,
2432 values, null, null);
Mindy Pereiracfb7f332012-02-28 10:23:43 -08002433 } else {
2434 // TODO(mindyp) delete the conversation.
Mindy Pereiraeaea9f12012-01-10 15:05:27 -08002435 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08002436 // reset messageId to 0, so a new message will be created
2437 messageId = UIProvider.INVALID_MESSAGE_ID;
2438 }
2439 }
2440
2441 final long messageIdToSave = messageId;
Scott Kennedyff8553f2013-04-05 20:57:44 -07002442 sendOrSaveMessage(messageIdToSave, sendOrSaveMessage, selectedAccount);
Paul Westbrook72e2ea82012-10-22 16:25:22 -07002443
2444 if (!sendOrSaveMessage.mSave) {
Andrew Sappersteinf5ab8ac2014-05-02 15:14:54 -07002445 incrementRecipientsTimesContacted(
Andrew Sapperstein22a3a312014-06-24 18:35:49 -07002446 (String) sendOrSaveMessage.mValues.get(UIProvider.MessageColumns.TO),
2447 (String) sendOrSaveMessage.mValues.get(UIProvider.MessageColumns.CC),
Paul Westbrook72e2ea82012-10-22 16:25:22 -07002448 (String) sendOrSaveMessage.mValues.get(UIProvider.MessageColumns.BCC));
2449 }
2450 mSendOrSaveCallback.sendOrSaveFinished(SendOrSaveTask.this, true);
2451 }
2452
Andrew Sapperstein22a3a312014-06-24 18:35:49 -07002453 private void incrementRecipientsTimesContacted(
2454 final String toAddresses, final String ccAddresses, final String bccAddresses) {
2455 final List<String> recipients = Lists.newArrayList();
2456 addAddressesToRecipientList(recipients, toAddresses);
2457 addAddressesToRecipientList(recipients, ccAddresses);
2458 addAddressesToRecipientList(recipients, bccAddresses);
2459 mSendOrSaveCallback.incrementRecipientsTimesContacted(recipients);
2460 }
2461
2462 private void addAddressesToRecipientList(
2463 final List<String> recipients, final String addressString) {
2464 if (recipients == null) {
2465 throw new IllegalArgumentException("recipientList cannot be null");
2466 }
Tony Mantler9f324232013-08-08 14:24:30 -07002467 if (TextUtils.isEmpty(addressString)) {
2468 return;
2469 }
2470 final Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(addressString);
Tony Mantler581edd42014-02-18 15:41:22 -08002471 for (final Rfc822Token token : tokens) {
2472 recipients.add(token.getAddress());
Tony Mantler9f324232013-08-08 14:24:30 -07002473 }
Tony Mantler9f324232013-08-08 14:24:30 -07002474 }
2475
Paul Westbrook72e2ea82012-10-22 16:25:22 -07002476 /**
2477 * Send or Save a message.
2478 */
Scott Kennedyff8553f2013-04-05 20:57:44 -07002479 private void sendOrSaveMessage(final long messageIdToSave,
2480 final SendOrSaveMessage sendOrSaveMessage, final ReplyFromAccount selectedAccount) {
Paul Westbrook72e2ea82012-10-22 16:25:22 -07002481 final ContentResolver resolver = mContext.getContentResolver();
2482 final boolean updateExistingMessage = messageIdToSave != UIProvider.INVALID_MESSAGE_ID;
2483
2484 final String accountMethod = sendOrSaveMessage.mSave ?
2485 UIProvider.AccountCallMethods.SAVE_MESSAGE :
2486 UIProvider.AccountCallMethods.SEND_MESSAGE;
2487
Paul Westbrook3c7f94d2012-10-23 14:13:00 -07002488 try {
2489 if (updateExistingMessage) {
2490 sendOrSaveMessage.mValues.put(BaseColumns._ID, messageIdToSave);
Paul Westbrook72e2ea82012-10-22 16:25:22 -07002491
Paul Westbrook013a23c2013-02-22 10:37:41 -08002492 callAccountSendSaveMethod(resolver,
Paul Westbrook3c7f94d2012-10-23 14:13:00 -07002493 selectedAccount.account, accountMethod, sendOrSaveMessage);
Paul Westbrook72e2ea82012-10-22 16:25:22 -07002494 } else {
Paul Westbrook013a23c2013-02-22 10:37:41 -08002495 Uri messageUri = null;
Paul Westbrook3c7f94d2012-10-23 14:13:00 -07002496 final Bundle result = callAccountSendSaveMethod(resolver,
2497 selectedAccount.account, accountMethod, sendOrSaveMessage);
2498 if (result != null) {
2499 // If a non-null value was returned, then the provider handled the call
2500 // method
2501 messageUri = result.getParcelable(UIProvider.MessageColumns.URI);
Paul Westbrook3c7f94d2012-10-23 14:13:00 -07002502 }
2503 if (sendOrSaveMessage.mSave && messageUri != null) {
2504 final Cursor messageCursor = resolver.query(messageUri,
2505 UIProvider.MESSAGE_PROJECTION, null, null, null);
2506 if (messageCursor != null) {
2507 try {
2508 if (messageCursor.moveToFirst()) {
2509 // Broadcast notification that a new message has
2510 // been allocated
2511 mSendOrSaveCallback.notifyMessageIdAllocated(sendOrSaveMessage,
2512 new Message(messageCursor));
2513 }
2514 } finally {
2515 messageCursor.close();
Paul Westbrookba558482012-03-19 11:00:24 -07002516 }
Paul Westbrook3c7f94d2012-10-23 14:13:00 -07002517 }
2518 }
2519 }
2520 } finally {
2521 // Close any opened file descriptors
2522 closeOpenedAttachmentFds(sendOrSaveMessage);
2523 }
2524 }
2525
Scott Kennedyff8553f2013-04-05 20:57:44 -07002526 private static void closeOpenedAttachmentFds(final SendOrSaveMessage sendOrSaveMessage) {
Paul Westbrook3c7f94d2012-10-23 14:13:00 -07002527 final Bundle openedFds = sendOrSaveMessage.attachmentFds();
2528 if (openedFds != null) {
2529 final Set<String> keys = openedFds.keySet();
Scott Kennedyff8553f2013-04-05 20:57:44 -07002530 for (final String key : keys) {
Paul Westbrook3c7f94d2012-10-23 14:13:00 -07002531 final ParcelFileDescriptor fd = openedFds.getParcelable(key);
2532 if (fd != null) {
2533 try {
2534 fd.close();
2535 } catch (IOException e) {
2536 // Do nothing
Paul Westbrookba558482012-03-19 11:00:24 -07002537 }
Mindy Pereira7ed1c112012-01-18 10:59:25 -08002538 }
2539 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08002540 }
Paul Westbrook72e2ea82012-10-22 16:25:22 -07002541 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08002542
Paul Westbrook72e2ea82012-10-22 16:25:22 -07002543 /**
Andrew Sappersteinbc5e0dc2013-08-07 21:15:22 -07002544 * Use the {@link ContentResolver#call} method to send or save the message.
Paul Westbrook72e2ea82012-10-22 16:25:22 -07002545 *
2546 * If this was successful, this method will return an non-null Bundle instance
2547 */
Scott Kennedyff8553f2013-04-05 20:57:44 -07002548 private static Bundle callAccountSendSaveMethod(final ContentResolver resolver,
2549 final Account account, final String method,
2550 final SendOrSaveMessage sendOrSaveMessage) {
Paul Westbrook72e2ea82012-10-22 16:25:22 -07002551 // Copy all of the values from the content values to the bundle
2552 final Bundle methodExtras = new Bundle(sendOrSaveMessage.mValues.size());
2553 final Set<Entry<String, Object>> valueSet = sendOrSaveMessage.mValues.valueSet();
2554
2555 for (Entry<String, Object> entry : valueSet) {
2556 final Object entryValue = entry.getValue();
2557 final String key = entry.getKey();
2558 if (entryValue instanceof String) {
2559 methodExtras.putString(key, (String)entryValue);
2560 } else if (entryValue instanceof Boolean) {
2561 methodExtras.putBoolean(key, (Boolean)entryValue);
2562 } else if (entryValue instanceof Integer) {
2563 methodExtras.putInt(key, (Integer)entryValue);
2564 } else if (entryValue instanceof Long) {
2565 methodExtras.putLong(key, (Long)entryValue);
2566 } else {
2567 LogUtils.wtf(LOG_TAG, "Unexpected object type: %s",
2568 entryValue.getClass().getName());
2569 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08002570 }
Paul Westbrook72e2ea82012-10-22 16:25:22 -07002571
Paul Westbrook3c7f94d2012-10-23 14:13:00 -07002572 // If the SendOrSaveMessage has some opened fds, add them to the bundle
2573 final Bundle fdMap = sendOrSaveMessage.attachmentFds();
2574 if (fdMap != null) {
2575 methodExtras.putParcelable(
2576 UIProvider.SendOrSaveMethodParamKeys.OPENED_FD_MAP, fdMap);
2577 }
2578
Paul Westbrook72e2ea82012-10-22 16:25:22 -07002579 return resolver.call(account.uri, method, account.uri.toString(), methodExtras);
Mindy Pereira82cc5662012-01-09 17:29:30 -08002580 }
2581 }
2582
Andrew Sappersteinf5ab8ac2014-05-02 15:14:54 -07002583 /**
2584 * Reports recipients that have been contacted in order to improve auto-complete
2585 * suggestions. Default behavior updates usage statistics in ContactsProvider.
2586 * @param recipients addresses
2587 */
2588 protected void incrementRecipientsTimesContacted(List<String> recipients) {
2589 final DataUsageStatUpdater statsUpdater = new DataUsageStatUpdater(this);
2590 statsUpdater.updateWithAddress(recipients);
2591 }
2592
Mindy Pereirae011b1d2012-06-18 13:45:26 -07002593 @VisibleForTesting
2594 public static class SendOrSaveMessage {
Mindy Pereira92551d02012-04-05 11:31:12 -07002595 final ReplyFromAccount mAccount;
Mindy Pereira82cc5662012-01-09 17:29:30 -08002596 final ContentValues mValues;
Mindy Pereira3ce64e72012-01-13 14:29:45 -08002597 final String mRefMessageId;
Mindy Pereirae011b1d2012-06-18 13:45:26 -07002598 @VisibleForTesting
2599 public final boolean mSave;
Mindy Pereira82cc5662012-01-09 17:29:30 -08002600 final int mRequestId;
Paul Westbrook3c7f94d2012-10-23 14:13:00 -07002601 private final Bundle mAttachmentFds;
Mindy Pereira82cc5662012-01-09 17:29:30 -08002602
Paul Westbrook3c7f94d2012-10-23 14:13:00 -07002603 public SendOrSaveMessage(Context context, ReplyFromAccount account, ContentValues values,
2604 String refMessageId, List<Attachment> attachments, boolean save) {
Mindy Pereira82cc5662012-01-09 17:29:30 -08002605 mAccount = account;
Mindy Pereira82cc5662012-01-09 17:29:30 -08002606 mValues = values;
2607 mRefMessageId = refMessageId;
2608 mSave = save;
2609 mRequestId = mValues.hashCode() ^ hashCode();
Paul Westbrook3c7f94d2012-10-23 14:13:00 -07002610
2611 mAttachmentFds = initializeAttachmentFds(context, attachments);
Mindy Pereira82cc5662012-01-09 17:29:30 -08002612 }
2613
2614 int requestId() {
2615 return mRequestId;
2616 }
Paul Westbrook3c7f94d2012-10-23 14:13:00 -07002617
2618 Bundle attachmentFds() {
2619 return mAttachmentFds;
2620 }
2621
2622 /**
2623 * Opens {@link ParcelFileDescriptor} for each of the attachments. This method must be
2624 * called before the ComposeActivity finishes.
2625 * Note: The caller is responsible for closing these file descriptors.
2626 */
Scott Kennedyff8553f2013-04-05 20:57:44 -07002627 private static Bundle initializeAttachmentFds(final Context context,
2628 final List<Attachment> attachments) {
Paul Westbrook3c7f94d2012-10-23 14:13:00 -07002629 if (attachments == null || attachments.size() == 0) {
2630 return null;
2631 }
2632
2633 final Bundle result = new Bundle(attachments.size());
2634 final ContentResolver resolver = context.getContentResolver();
2635
2636 for (Attachment attachment : attachments) {
2637 if (attachment == null || Utils.isEmpty(attachment.contentUri)) {
2638 continue;
2639 }
2640
2641 ParcelFileDescriptor fileDescriptor;
2642 try {
2643 fileDescriptor = resolver.openFileDescriptor(attachment.contentUri, "r");
2644 } catch (FileNotFoundException e) {
2645 LogUtils.e(LOG_TAG, e, "Exception attempting to open attachment");
2646 fileDescriptor = null;
Paul Westbrookc537fd42013-02-20 11:10:03 -08002647 } catch (SecurityException e) {
2648 // We have encountered a security exception when attempting to open the file
2649 // specified by the content uri. If the attachment has been cached, this
2650 // isn't a problem, as even through the original permission may have been
2651 // revoked, we have cached the file. This will happen when saving/sending
2652 // a previously saved draft.
2653 // TODO(markwei): Expose whether the attachment has been cached through the
2654 // attachment object. This would allow us to limit when the log is made, as
2655 // if the attachment has been cached, this really isn't an error
2656 LogUtils.e(LOG_TAG, e, "Security Exception attempting to open attachment");
2657 // Just set the file descriptor to null, as the underlying provider needs
2658 // to handle the file descriptor not being set.
2659 fileDescriptor = null;
Paul Westbrook3c7f94d2012-10-23 14:13:00 -07002660 }
2661
2662 if (fileDescriptor != null) {
2663 result.putParcelable(attachment.contentUri.toString(), fileDescriptor);
2664 }
2665 }
2666
2667 return result;
2668 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08002669 }
2670
2671 /**
2672 * Get the to recipients.
2673 */
2674 public String[] getToAddresses() {
2675 return getAddressesFromList(mTo);
2676 }
2677
2678 /**
2679 * Get the cc recipients.
2680 */
2681 public String[] getCcAddresses() {
2682 return getAddressesFromList(mCc);
2683 }
2684
2685 /**
2686 * Get the bcc recipients.
2687 */
2688 public String[] getBccAddresses() {
2689 return getAddressesFromList(mBcc);
2690 }
2691
2692 public String[] getAddressesFromList(RecipientEditTextView list) {
2693 if (list == null) {
2694 return new String[0];
2695 }
2696 Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(list.getText());
2697 int count = tokens.length;
2698 String[] result = new String[count];
2699 for (int i = 0; i < count; i++) {
2700 result[i] = tokens[i].toString();
2701 }
2702 return result;
2703 }
2704
2705 /**
2706 * Check for invalid email addresses.
2707 * @param to String array of email addresses to check.
2708 * @param wrongEmailsOut Emails addresses that were invalid.
2709 */
Scott Kennedyff8553f2013-04-05 20:57:44 -07002710 public void checkInvalidEmails(final String[] to, final List<String> wrongEmailsOut) {
Mindy Pereirae5f20bf2012-06-25 14:20:40 -07002711 if (mValidator == null) {
2712 return;
2713 }
Scott Kennedyff8553f2013-04-05 20:57:44 -07002714 for (final String email : to) {
Mindy Pereira82cc5662012-01-09 17:29:30 -08002715 if (!mValidator.isValid(email)) {
2716 wrongEmailsOut.add(email);
2717 }
2718 }
2719 }
2720
Tony Mantler2558b502013-07-09 10:53:34 -07002721 public static class RecipientErrorDialogFragment extends DialogFragment {
Paul Westbrookf0ea4842013-08-13 16:41:18 -07002722 // Public no-args constructor needed for fragment re-instantiation
2723 public RecipientErrorDialogFragment() {}
2724
Tony Mantler2558b502013-07-09 10:53:34 -07002725 public static RecipientErrorDialogFragment newInstance(final String message) {
2726 final RecipientErrorDialogFragment frag = new RecipientErrorDialogFragment();
2727 final Bundle args = new Bundle(1);
2728 args.putString("message", message);
2729 frag.setArguments(args);
2730 return frag;
2731 }
2732
2733 @Override
2734 public Dialog onCreateDialog(Bundle savedInstanceState) {
2735 final String message = getArguments().getString("message");
Andrew Sapperstein530ac7a2013-10-29 19:12:17 -07002736 return new AlertDialog.Builder(getActivity())
2737 .setMessage(message)
Tony Mantler2558b502013-07-09 10:53:34 -07002738 .setPositiveButton(
2739 R.string.ok, new Dialog.OnClickListener() {
2740 @Override
2741 public void onClick(DialogInterface dialog, int which) {
2742 ((ComposeActivity)getActivity()).finishRecipientErrorDialog();
2743 }
2744 }).create();
2745 }
2746 }
2747
2748 private void finishRecipientErrorDialog() {
2749 // after the user dismisses the recipient error
2750 // dialog we want to make sure to refocus the
2751 // recipient to field so they can fix the issue
2752 // easily
2753 if (mTo != null) {
2754 mTo.requestFocus();
2755 }
2756 }
2757
Mindy Pereira82cc5662012-01-09 17:29:30 -08002758 /**
2759 * Show an error because the user has entered an invalid recipient.
Mindy Pereira82cc5662012-01-09 17:29:30 -08002760 */
Tony Mantler2558b502013-07-09 10:53:34 -07002761 private void showRecipientErrorDialog(final String message) {
2762 final DialogFragment frag = RecipientErrorDialogFragment.newInstance(message);
2763 frag.show(getFragmentManager(), "recipient error");
Mindy Pereira82cc5662012-01-09 17:29:30 -08002764 }
2765
2766 /**
2767 * Update the state of the UI based on whether or not the current draft
2768 * needs to be saved and the message is not empty.
2769 */
Mindy Pereiraeaea9f12012-01-10 15:05:27 -08002770 public void updateSaveUi() {
Mindy Pereira82cc5662012-01-09 17:29:30 -08002771 if (mSave != null) {
2772 mSave.setEnabled((shouldSave() && !isBlank()));
2773 }
2774 }
2775
2776 /**
2777 * Returns true if we need to save the current draft.
2778 */
2779 private boolean shouldSave() {
Mindy Pereira7ed1c112012-01-18 10:59:25 -08002780 synchronized (mDraftLock) {
Mindy Pereira82cc5662012-01-09 17:29:30 -08002781 // The message should only be saved if:
2782 // It hasn't been sent AND
2783 // Some text has been added to the message OR
2784 // an attachment has been added or removed
Mindy Pereiraa2148332012-07-02 13:54:14 -07002785 // AND there is actually something in the draft to save.
Andy Huangd47877e2012-08-09 19:31:24 -07002786 return (mTextChanged || mAttachmentsChanged || mReplyFromChanged)
Mindy Pereiraa2148332012-07-02 13:54:14 -07002787 && !isBlank();
Mindy Pereira82cc5662012-01-09 17:29:30 -08002788 }
2789 }
2790
2791 /**
Greg Bullockd47a7042014-08-13 16:02:59 +02002792 * Returns whether the "Attach from Drive" menu item should be visible.
2793 */
2794 protected boolean shouldEnableAttachFromServiceMenu(Account mAccount) {
2795 return false;
2796 }
2797
2798 /**
Mindy Pereirabdf7a402012-03-01 15:23:26 -08002799 * Check if all fields are blank.
Mindy Pereira82cc5662012-01-09 17:29:30 -08002800 * @return boolean
2801 */
2802 public boolean isBlank() {
Alice Yanga49b6842013-08-23 10:36:18 -07002803 // Need to check for null since isBlank() can be called from onPause()
2804 // before findViews() is called
2805 if (mSubject == null || mBodyView == null || mTo == null || mCc == null ||
2806 mAttachmentsView == null) {
2807 LogUtils.w(LOG_TAG, "null views in isBlank check");
2808 return true;
2809 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08002810 return mSubject.getText().length() == 0
Mindy Pereirabdf7a402012-03-01 15:23:26 -08002811 && (mBodyView.getText().length() == 0 || getSignatureStartPosition(mSignature,
2812 mBodyView.getText().toString()) == 0)
2813 && mTo.length() == 0
2814 && mCc.length() == 0 && mBcc.length() == 0
2815 && mAttachmentsView.getAttachments().size() == 0;
2816 }
2817
2818 @VisibleForTesting
2819 protected int getSignatureStartPosition(String signature, String bodyText) {
2820 int startPos = -1;
2821
2822 if (TextUtils.isEmpty(signature) || TextUtils.isEmpty(bodyText)) {
2823 return startPos;
2824 }
2825
2826 int bodyLength = bodyText.length();
2827 int signatureLength = signature.length();
2828 String printableVersion = convertToPrintableSignature(signature);
2829 int printableLength = printableVersion.length();
2830
2831 if (bodyLength >= printableLength
2832 && bodyText.substring(bodyLength - printableLength)
2833 .equals(printableVersion)) {
2834 startPos = bodyLength - printableLength;
2835 } else if (bodyLength >= signatureLength
2836 && bodyText.substring(bodyLength - signatureLength)
2837 .equals(signature)) {
2838 startPos = bodyLength - signatureLength;
2839 }
2840 return startPos;
Mindy Pereira82cc5662012-01-09 17:29:30 -08002841 }
2842
2843 /**
2844 * Allows any changes made by the user to be ignored. Called when the user
2845 * decides to discard a draft.
2846 */
2847 private void discardChanges() {
2848 mTextChanged = false;
Mindy Pereiraeaea9f12012-01-10 15:05:27 -08002849 mAttachmentsChanged = false;
Mindy Pereira82cc5662012-01-09 17:29:30 -08002850 mReplyFromChanged = false;
2851 }
2852
2853 /**
Tony Mantler581edd42014-02-18 15:41:22 -08002854 * @param save True to save, false to send
2855 * @param showToast True to show a toast once the message is sent/saved
Mindy Pereira181df782012-03-01 13:32:44 -08002856 */
Andrew Sapperstein1b7ad922014-05-28 11:23:33 -07002857 protected void sendOrSaveWithSanityChecks(final boolean save, final boolean showToast,
Mark Weidd19b632012-10-19 13:59:28 -07002858 final boolean orientationChanged, final boolean autoSend) {
Mark Wei009b3712012-10-18 18:07:50 -07002859 if (mAccounts == null || mAccount == null) {
2860 Toast.makeText(this, R.string.send_failed, Toast.LENGTH_SHORT).show();
Mark Weidd19b632012-10-19 13:59:28 -07002861 if (autoSend) {
2862 finish();
2863 }
Andrew Sapperstein1b7ad922014-05-28 11:23:33 -07002864 return;
Mark Wei009b3712012-10-18 18:07:50 -07002865 }
2866
Scott Kennedyff8553f2013-04-05 20:57:44 -07002867 final String[] to, cc, bcc;
Mindy Pereira181df782012-03-01 13:32:44 -08002868 if (orientationChanged) {
2869 to = cc = bcc = new String[0];
2870 } else {
2871 to = getToAddresses();
2872 cc = getCcAddresses();
2873 bcc = getBccAddresses();
2874 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08002875
Andrew Sapperstein1b7ad922014-05-28 11:23:33 -07002876 final ArrayList<String> recipients = buildEmailAddressList(to);
2877 recipients.addAll(buildEmailAddressList(cc));
2878 recipients.addAll(buildEmailAddressList(bcc));
2879
Mindy Pereira181df782012-03-01 13:32:44 -08002880 // Don't let the user send to nobody (but it's okay to save a message
2881 // with no recipients)
2882 if (!save && (to.length == 0 && cc.length == 0 && bcc.length == 0)) {
2883 showRecipientErrorDialog(getString(R.string.recipient_needed));
Andrew Sapperstein1b7ad922014-05-28 11:23:33 -07002884 return;
Mindy Pereira181df782012-03-01 13:32:44 -08002885 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08002886
Mindy Pereira181df782012-03-01 13:32:44 -08002887 List<String> wrongEmails = new ArrayList<String>();
2888 if (!save) {
2889 checkInvalidEmails(to, wrongEmails);
2890 checkInvalidEmails(cc, wrongEmails);
2891 checkInvalidEmails(bcc, wrongEmails);
2892 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08002893
Mindy Pereira181df782012-03-01 13:32:44 -08002894 // Don't let the user send an email with invalid recipients
2895 if (wrongEmails.size() > 0) {
2896 String errorText = String.format(getString(R.string.invalid_recipient),
2897 wrongEmails.get(0));
2898 showRecipientErrorDialog(errorText);
Andrew Sapperstein1b7ad922014-05-28 11:23:33 -07002899 return;
Mindy Pereira181df782012-03-01 13:32:44 -08002900 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08002901
Mindy Pereira181df782012-03-01 13:32:44 -08002902 if (!save) {
Alan Lau3d519042014-06-05 11:13:06 -07002903 if (autoSend) {
2904 // Skip all further checks during autosend. This flow is used by Android Wear
2905 // and Google Now.
2906 sendOrSave(save, showToast);
2907 return;
2908 }
2909
2910 // Show a warning before sending only if there are no attachments, body, or subject.
Mindy Pereira181df782012-03-01 13:32:44 -08002911 if (mAttachmentsView.getAttachments().isEmpty() && showEmptyTextWarnings()) {
2912 boolean warnAboutEmptySubject = isSubjectEmpty();
Tony Mantler2558b502013-07-09 10:53:34 -07002913 boolean emptyBody = TextUtils.getTrimmedLength(mBodyView.getEditableText()) == 0;
Mindy Pereira82cc5662012-01-09 17:29:30 -08002914
Mindy Pereira181df782012-03-01 13:32:44 -08002915 // A warning about an empty body may not be warranted when
2916 // forwarding mails, since a common use case is to forward
2917 // quoted text and not append any more text.
2918 boolean warnAboutEmptyBody = emptyBody && (!mForward || isBodyEmpty());
Mindy Pereira82cc5662012-01-09 17:29:30 -08002919
Mindy Pereira181df782012-03-01 13:32:44 -08002920 // When we bring up a dialog warning the user about a send,
2921 // assume that they accept sending the message. If they do not,
2922 // the dialog listener is required to enable sending again.
2923 if (warnAboutEmptySubject) {
Tony Mantler581edd42014-02-18 15:41:22 -08002924 showSendConfirmDialog(R.string.confirm_send_message_with_no_subject,
Andrew Sapperstein1b7ad922014-05-28 11:23:33 -07002925 showToast, recipients);
2926 return;
Mindy Pereira181df782012-03-01 13:32:44 -08002927 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08002928
Mindy Pereira181df782012-03-01 13:32:44 -08002929 if (warnAboutEmptyBody) {
Tony Mantler581edd42014-02-18 15:41:22 -08002930 showSendConfirmDialog(R.string.confirm_send_message_with_no_body,
Andrew Sapperstein1b7ad922014-05-28 11:23:33 -07002931 showToast, recipients);
2932 return;
Mindy Pereira181df782012-03-01 13:32:44 -08002933 }
2934 }
Alan Lau3d519042014-06-05 11:13:06 -07002935 // Ask for confirmation to send.
2936 if (showSendConfirmation()) {
Andrew Sapperstein1b7ad922014-05-28 11:23:33 -07002937 showSendConfirmDialog(R.string.confirm_send_message, showToast, recipients);
2938 return;
Mindy Pereira181df782012-03-01 13:32:44 -08002939 }
2940 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08002941
Andrew Sapperstein1b7ad922014-05-28 11:23:33 -07002942 performAdditionalSendOrSaveSanityChecks(save, showToast, recipients);
Mindy Pereira181df782012-03-01 13:32:44 -08002943 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08002944
Mindy Pereira181df782012-03-01 13:32:44 -08002945 /**
2946 * Returns a boolean indicating whether warnings should be shown for empty
2947 * subject and body fields
Andy Huang5c5fd572012-04-08 18:19:29 -07002948 *
Mindy Pereira181df782012-03-01 13:32:44 -08002949 * @return True if a warning should be shown for empty text fields
2950 */
2951 protected boolean showEmptyTextWarnings() {
2952 return mAttachmentsView.getAttachments().size() == 0;
2953 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08002954
Mindy Pereira181df782012-03-01 13:32:44 -08002955 /**
2956 * Returns a boolean indicating whether the user should confirm each send
2957 *
2958 * @return True if a warning should be on each send
2959 */
2960 protected boolean showSendConfirmation() {
Tony Mantler581edd42014-02-18 15:41:22 -08002961 return mCachedSettings != null && mCachedSettings.confirmSend;
Mindy Pereira181df782012-03-01 13:32:44 -08002962 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08002963
Andrew Sapperstein530ac7a2013-10-29 19:12:17 -07002964 public static class SendConfirmDialogFragment extends DialogFragment
2965 implements DialogInterface.OnClickListener {
2966
Andrew Sapperstein1b7ad922014-05-28 11:23:33 -07002967 private static final String MESSAGE_ID = "messageId";
2968 private static final String SHOW_TOAST = "showToast";
2969 private static final String RECIPIENTS = "recipients";
2970
Andrew Sapperstein530ac7a2013-10-29 19:12:17 -07002971 private boolean mShowToast;
2972
Andrew Sapperstein1b7ad922014-05-28 11:23:33 -07002973 private ArrayList<String> mRecipients;
2974
Paul Westbrookf0ea4842013-08-13 16:41:18 -07002975 // Public no-args constructor needed for fragment re-instantiation
2976 public SendConfirmDialogFragment() {}
2977
Tony Mantler2558b502013-07-09 10:53:34 -07002978 public static SendConfirmDialogFragment newInstance(final int messageId,
Andrew Sapperstein1b7ad922014-05-28 11:23:33 -07002979 final boolean showToast, final ArrayList<String> recipients) {
Tony Mantler2558b502013-07-09 10:53:34 -07002980 final SendConfirmDialogFragment frag = new SendConfirmDialogFragment();
2981 final Bundle args = new Bundle(3);
Andrew Sapperstein1b7ad922014-05-28 11:23:33 -07002982 args.putInt(MESSAGE_ID, messageId);
2983 args.putBoolean(SHOW_TOAST, showToast);
2984 args.putStringArrayList(RECIPIENTS, recipients);
Tony Mantler2558b502013-07-09 10:53:34 -07002985 frag.setArguments(args);
2986 return frag;
Mindy Pereira181df782012-03-01 13:32:44 -08002987 }
Tony Mantler2558b502013-07-09 10:53:34 -07002988
2989 @Override
2990 public Dialog onCreateDialog(Bundle savedInstanceState) {
Andrew Sapperstein1b7ad922014-05-28 11:23:33 -07002991 final int messageId = getArguments().getInt(MESSAGE_ID);
2992 mShowToast = getArguments().getBoolean(SHOW_TOAST);
2993 mRecipients = getArguments().getStringArrayList(RECIPIENTS);
Andrew Sapperstein530ac7a2013-10-29 19:12:17 -07002994
2995 final int confirmTextId = (messageId == R.string.confirm_send_message) ?
2996 R.string.ok : R.string.send;
Tony Mantler2558b502013-07-09 10:53:34 -07002997
2998 return new AlertDialog.Builder(getActivity())
2999 .setMessage(messageId)
Andrew Sapperstein530ac7a2013-10-29 19:12:17 -07003000 .setPositiveButton(confirmTextId, this)
Paul Westbrook7d1c5c42013-10-01 23:40:04 -07003001 .setNegativeButton(R.string.cancel, null)
Tony Mantler2558b502013-07-09 10:53:34 -07003002 .create();
3003 }
Andrew Sapperstein530ac7a2013-10-29 19:12:17 -07003004
3005 @Override
3006 public void onClick(DialogInterface dialog, int which) {
3007 if (which == DialogInterface.BUTTON_POSITIVE) {
Andrew Sapperstein1b7ad922014-05-28 11:23:33 -07003008 ((ComposeActivity) getActivity()).finishSendConfirmDialog(mShowToast, mRecipients);
Andrew Sapperstein530ac7a2013-10-29 19:12:17 -07003009 }
3010 }
Tony Mantler2558b502013-07-09 10:53:34 -07003011 }
3012
Andrew Sapperstein1b7ad922014-05-28 11:23:33 -07003013 private void finishSendConfirmDialog(
3014 final boolean showToast, final ArrayList<String> recipients) {
3015 performAdditionalSendOrSaveSanityChecks(false /* save */, showToast, recipients);
Tony Mantler2558b502013-07-09 10:53:34 -07003016 }
3017
Andrew Sapperstein1b7ad922014-05-28 11:23:33 -07003018 // The list of recipients are used by the additional sendOrSave checks.
3019 // However, the send confirm dialog may be shown before performing
3020 // the additional checks. As a result, we need to plumb the recipient
3021 // list through the send confirm dialog so that
3022 // performAdditionalSendOrSaveChecks can be performed properly.
Tony Mantler581edd42014-02-18 15:41:22 -08003023 private void showSendConfirmDialog(final int messageId,
Andrew Sapperstein1b7ad922014-05-28 11:23:33 -07003024 final boolean showToast, final ArrayList<String> recipients) {
3025 final DialogFragment frag = SendConfirmDialogFragment.newInstance(
3026 messageId, showToast, recipients);
Tony Mantler2558b502013-07-09 10:53:34 -07003027 frag.show(getFragmentManager(), "send confirm");
Mindy Pereira181df782012-03-01 13:32:44 -08003028 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08003029
Mindy Pereira181df782012-03-01 13:32:44 -08003030 /**
3031 * Returns whether the ComposeArea believes there is any text in the body of
3032 * the composition. TODO: When ComposeArea controls the Body as well, add
3033 * that here.
3034 */
3035 public boolean isBodyEmpty() {
3036 return !mQuotedTextView.isTextIncluded();
3037 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08003038
Mindy Pereira181df782012-03-01 13:32:44 -08003039 /**
3040 * Test to see if the subject is empty.
3041 *
3042 * @return boolean.
3043 */
3044 // TODO: this will likely go away when composeArea.focus() is implemented
3045 // after all the widget control is moved over.
3046 public boolean isSubjectEmpty() {
3047 return TextUtils.getTrimmedLength(mSubject.getText()) == 0;
3048 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08003049
Andy Huang0a2a3462013-12-20 15:56:13 -08003050 @VisibleForTesting
3051 public String getSubject() {
3052 return mSubject.getText().toString();
3053 }
3054
Andy Huang91ede362014-01-21 19:16:00 -08003055 private int sendOrSaveInternal(Context context, ReplyFromAccount replyFromAccount,
Jin Cao77b4c2c2014-05-20 13:55:53 -07003056 Message message, final Message refMessage, final CharSequence quotedText,
mindyp44a63392012-11-05 12:05:16 -08003057 SendOrSaveCallback callback, Handler handler, boolean save, int composeMode,
Scott Kennedy60847252013-08-15 15:55:42 -07003058 ReplyFromAccount draftAccount, final ContentValues extraValues) {
Paul Westbrookb4931c62013-01-14 17:51:18 -08003059 final ContentValues values = new ContentValues();
Mindy Pereira82cc5662012-01-09 17:29:30 -08003060
Paul Westbrookb4931c62013-01-14 17:51:18 -08003061 final String refMessageId = refMessage != null ? refMessage.uri.toString() : "";
Mindy Pereirac2031972012-04-03 09:38:35 -07003062
Mindy Pereirae8f94dc2012-04-16 11:56:21 -07003063 MessageModification.putToAddresses(values, message.getToAddresses());
3064 MessageModification.putCcAddresses(values, message.getCcAddresses());
3065 MessageModification.putBccAddresses(values, message.getBccAddresses());
Scott Kennedy8960f0a2012-11-07 15:35:50 -08003066 MessageModification.putCustomFromAddress(values, message.getFrom());
Mindy Pereira92551d02012-04-05 11:31:12 -07003067
Mindy Pereirae8f94dc2012-04-16 11:56:21 -07003068 MessageModification.putSubject(values, message.subject);
Anthony Lee2a3cc132014-04-22 14:15:25 -07003069
Jin Cao77b4c2c2014-05-20 13:55:53 -07003070 // bodyHtml already have the composing spans removed.
3071 final String htmlBody = message.bodyHtml;
Jin Caoa9f5a8e2014-07-22 13:48:45 -07003072 final String textBody = message.bodyText;
Anthony Lee2a3cc132014-04-22 14:15:25 -07003073 // fullbody will contain the actual body plus the quoted text.
3074 final String fullBody;
3075 final String quotedString;
3076 final boolean hasQuotedText = !TextUtils.isEmpty(quotedText);
3077 if (hasQuotedText) {
3078 // The quoted text is HTML at this point.
3079 quotedString = quotedText.toString();
3080 fullBody = htmlBody + quotedString;
3081 MessageModification.putForward(values, composeMode == ComposeActivity.FORWARD);
3082 MessageModification.putAppendRefMessageContent(values, true /* include quoted */);
3083 } else {
3084 fullBody = htmlBody;
3085 quotedString = null;
Mindy Pereira29ef1b82012-01-13 11:26:21 -08003086 }
Jin Caoa9f5a8e2014-07-22 13:48:45 -07003087 // Only take refMessage into account if either one of its html/text is not empty.
3088 if (refMessage != null && !(TextUtils.isEmpty(refMessage.bodyHtml) &&
3089 TextUtils.isEmpty(refMessage.bodyText))) {
Anthony Lee2a3cc132014-04-22 14:15:25 -07003090 // The code below might need to be revisited. The quoted text position is different
3091 // between text/html and text/plain parts and they should be stored seperately and
3092 // the right version should be used in the UI. text/html should have preference
3093 // if both exist. Issues like this made me file b/14256940 to make sure that we
3094 // properly handle the existing of both text/html and text/plain parts and to verify
3095 // that we are not making some assumptions that break if there is no text/html part.
3096 int quotedTextPos = -1;
Mindy Pereirac6f1e2a2012-04-04 10:33:45 -07003097 if (!TextUtils.isEmpty(refMessage.bodyHtml)) {
Jin Cao7800d292014-08-24 11:31:15 -07003098 MessageModification.putBodyHtml(values, fullBody);
Anthony Lee2a3cc132014-04-22 14:15:25 -07003099 if (hasQuotedText) {
3100 quotedTextPos = htmlBody.length() +
3101 QuotedTextView.getQuotedTextOffset(quotedString);
3102 }
Mindy Pereirac6f1e2a2012-04-04 10:33:45 -07003103 }
3104 if (!TextUtils.isEmpty(refMessage.bodyText)) {
mindypc59dd822012-11-13 10:56:21 -08003105 MessageModification.putBody(values,
Jin Cao7800d292014-08-24 11:31:15 -07003106 Utils.convertHtmlToPlainText(fullBody));
Anthony Lee2a3cc132014-04-22 14:15:25 -07003107 if (hasQuotedText && (quotedTextPos == -1)) {
3108 quotedTextPos = textBody.length();
3109 }
3110 }
3111 if (quotedTextPos != -1) {
3112 // The quoted text pos is the text/html version first and the text/plan version
3113 // if there is no text/html part. The reason for this is because preference
3114 // is given to text/html in the compose window if it exists. In the future, we
3115 // should calculate the index for both since the user could choose to compose
3116 // explicitly in text/plain.
3117 MessageModification.putQuoteStartPos(values, quotedTextPos);
Mindy Pereirac6f1e2a2012-04-04 10:33:45 -07003118 }
3119 } else {
Jin Cao7800d292014-08-24 11:31:15 -07003120 MessageModification.putBodyHtml(values, fullBody);
3121 MessageModification.putBody(values, Utils.convertHtmlToPlainText(fullBody));
Mindy Pereirac2031972012-04-03 09:38:35 -07003122 }
Anthony Lee2a3cc132014-04-22 14:15:25 -07003123 int draftType = getDraftType(composeMode);
3124 MessageModification.putDraftType(values, draftType);
Mindy Pereirae8f94dc2012-04-16 11:56:21 -07003125 MessageModification.putAttachments(values, message.getAttachments());
Mindy Pereira12575862012-03-21 16:30:54 -07003126 if (!TextUtils.isEmpty(refMessageId)) {
3127 MessageModification.putRefMessageId(values, refMessageId);
3128 }
Scott Kennedy60847252013-08-15 15:55:42 -07003129 if (extraValues != null) {
3130 values.putAll(extraValues);
3131 }
Paul Westbrook3c7f94d2012-10-23 14:13:00 -07003132 SendOrSaveMessage sendOrSaveMessage = new SendOrSaveMessage(context, replyFromAccount,
3133 values, refMessageId, message.getAttachments(), save);
mindyp44a63392012-11-05 12:05:16 -08003134 SendOrSaveTask sendOrSaveTask = new SendOrSaveTask(context, sendOrSaveMessage, callback,
3135 draftAccount);
Mindy Pereira82cc5662012-01-09 17:29:30 -08003136
Mindy Pereira181df782012-03-01 13:32:44 -08003137 callback.initializeSendOrSave(sendOrSaveTask);
Mindy Pereira181df782012-03-01 13:32:44 -08003138 // Do the send/save action on the specified handler to avoid possible
3139 // ANRs
3140 handler.post(sendOrSaveTask);
Jin Cao7800d292014-08-24 11:31:15 -07003141 LogUtils.i(LOG_TAG, "[compose] SendOrSaveMessage [%s] posted (isSave: %s) - " +
3142 "body length: %d, attachment count: %d",
3143 sendOrSaveMessage.requestId(), save, message.bodyText.length(),
3144 message.getAttachmentCount(true));
Mindy Pereira82cc5662012-01-09 17:29:30 -08003145
Mindy Pereira181df782012-03-01 13:32:44 -08003146 return sendOrSaveMessage.requestId();
3147 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08003148
Paul Westbrookb4931c62013-01-14 17:51:18 -08003149 /**
3150 * Removes any composing spans from the specified string. This will create a new
3151 * SpannableString instance, as to not modify the behavior of the EditText view.
3152 */
3153 private static SpannableString removeComposingSpans(Spanned body) {
3154 final SpannableString messageBody = new SpannableString(body);
3155 BaseInputConnection.removeComposingSpans(messageBody);
Andy Huangff017272014-06-18 00:27:35 -07003156
3157 // Remove watcher spans while we're at it, so any off-UI thread manipulation of these
3158 // spans doesn't trigger unexpected side-effects. This copy is essentially 100% detached
3159 // from the EditText.
3160 //
3161 // (must remove SpanWatchers first to avoid triggering them as we remove other spans)
3162 removeSpansOfType(messageBody, SpanWatcher.class);
3163 removeSpansOfType(messageBody, TextWatcher.class);
3164
Paul Westbrookb4931c62013-01-14 17:51:18 -08003165 return messageBody;
3166 }
3167
Andy Huangff017272014-06-18 00:27:35 -07003168 private static void removeSpansOfType(SpannableString str, Class<?> cls) {
3169 for (Object span : str.getSpans(0, str.length(), cls)) {
3170 str.removeSpan(span);
3171 }
3172 }
3173
Mindy Pereira002ff522012-05-30 10:31:26 -07003174 private static int getDraftType(int mode) {
3175 int draftType = -1;
3176 switch (mode) {
3177 case ComposeActivity.COMPOSE:
3178 draftType = DraftType.COMPOSE;
3179 break;
3180 case ComposeActivity.REPLY:
3181 draftType = DraftType.REPLY;
3182 break;
3183 case ComposeActivity.REPLY_ALL:
3184 draftType = DraftType.REPLY_ALL;
3185 break;
3186 case ComposeActivity.FORWARD:
3187 draftType = DraftType.FORWARD;
3188 break;
3189 }
3190 return draftType;
3191 }
3192
Andrew Sapperstein1b7ad922014-05-28 11:23:33 -07003193 /**
3194 * Derived classes should override this step to perform additional checks before
3195 * send or save. The default implementation simply calls {@link #sendOrSave(boolean, boolean)}.
3196 */
3197 protected void performAdditionalSendOrSaveSanityChecks(
3198 final boolean save, final boolean showToast, ArrayList<String> recipients) {
3199 sendOrSave(save, showToast);
3200 }
3201
3202 protected void sendOrSave(final boolean save, final boolean showToast) {
Mindy Pereira181df782012-03-01 13:32:44 -08003203 // Check if user is a monkey. Monkeys can compose and hit send
3204 // button but are not allowed to send anything off the device.
Paul Westbrook3ae824c2012-04-06 13:29:39 -07003205 if (ActivityManager.isUserAMonkey()) {
Mindy Pereira181df782012-03-01 13:32:44 -08003206 return;
3207 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08003208
Jin Cao77b4c2c2014-05-20 13:55:53 -07003209 final SendOrSaveCallback callback = new SendOrSaveCallback() {
Andy Huang1f8f4dd2012-10-25 21:35:35 -07003210 // FIXME: unused
Mindy Pereira82cc5662012-01-09 17:29:30 -08003211 private int mRestoredRequestId;
3212
Marc Blank0bbc8582012-04-23 15:07:57 -07003213 @Override
Mindy Pereira82cc5662012-01-09 17:29:30 -08003214 public void initializeSendOrSave(SendOrSaveTask sendOrSaveTask) {
Mindy Pereira181df782012-03-01 13:32:44 -08003215 synchronized (mActiveTasks) {
3216 int numTasks = mActiveTasks.size();
3217 if (numTasks == 0) {
3218 // Start service so we won't be killed if this app is
3219 // put in the background.
3220 startService(new Intent(ComposeActivity.this, EmptyService.class));
3221 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08003222
Mindy Pereira181df782012-03-01 13:32:44 -08003223 mActiveTasks.add(sendOrSaveTask);
3224 }
3225 if (sTestSendOrSaveCallback != null) {
3226 sTestSendOrSaveCallback.initializeSendOrSave(sendOrSaveTask);
3227 }
3228 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08003229
Marc Blank0bbc8582012-04-23 15:07:57 -07003230 @Override
Mindy Pereira7ed1c112012-01-18 10:59:25 -08003231 public void notifyMessageIdAllocated(SendOrSaveMessage sendOrSaveMessage,
3232 Message message) {
Mindy Pereira181df782012-03-01 13:32:44 -08003233 synchronized (mDraftLock) {
mindyp44a63392012-11-05 12:05:16 -08003234 mDraftAccount = sendOrSaveMessage.mAccount;
Mindy Pereira181df782012-03-01 13:32:44 -08003235 mDraftId = message.id;
3236 mDraft = message;
Mindy Pereira7ed1c112012-01-18 10:59:25 -08003237 if (sRequestMessageIdMap != null) {
3238 sRequestMessageIdMap.put(sendOrSaveMessage.requestId(), mDraftId);
3239 }
Mindy Pereira181df782012-03-01 13:32:44 -08003240 // Cache request message map, in case the process is killed
3241 saveRequestMap();
3242 }
3243 if (sTestSendOrSaveCallback != null) {
Mindy Pereira7ed1c112012-01-18 10:59:25 -08003244 sTestSendOrSaveCallback.notifyMessageIdAllocated(sendOrSaveMessage, message);
Mindy Pereira181df782012-03-01 13:32:44 -08003245 }
3246 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08003247
Marc Blank0bbc8582012-04-23 15:07:57 -07003248 @Override
Mindy Pereira7ed1c112012-01-18 10:59:25 -08003249 public Message getMessage() {
3250 synchronized (mDraftLock) {
3251 return mDraft;
3252 }
3253 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08003254
Marc Blank0bbc8582012-04-23 15:07:57 -07003255 @Override
Mindy Pereira7ed1c112012-01-18 10:59:25 -08003256 public void sendOrSaveFinished(SendOrSaveTask task, boolean success) {
Mindy Pereira47d0e652012-07-23 09:45:07 -07003257 // Update the last sent from account.
3258 if (mAccount != null) {
3259 MailAppProvider.getInstance().setLastSentFromAccount(mAccount.uri.toString());
3260 }
Mindy Pereira7ed1c112012-01-18 10:59:25 -08003261 if (success) {
3262 // Successfully sent or saved so reset change markers
3263 discardChanges();
3264 } else {
3265 // A failure happened with saving/sending the draft
3266 // TODO(pwestbro): add a better string that should be used
3267 // when failing to send or save
3268 Toast.makeText(ComposeActivity.this, R.string.send_failed, Toast.LENGTH_SHORT)
3269 .show();
3270 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08003271
Mindy Pereira7ed1c112012-01-18 10:59:25 -08003272 int numTasks;
3273 synchronized (mActiveTasks) {
3274 // Remove the task from the list of active tasks
3275 mActiveTasks.remove(task);
3276 numTasks = mActiveTasks.size();
3277 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08003278
Mindy Pereira7ed1c112012-01-18 10:59:25 -08003279 if (numTasks == 0) {
3280 // Stop service so we can be killed.
3281 stopService(new Intent(ComposeActivity.this, EmptyService.class));
3282 }
3283 if (sTestSendOrSaveCallback != null) {
3284 sTestSendOrSaveCallback.sendOrSaveFinished(task, success);
3285 }
3286 }
Andrew Sappersteinf5ab8ac2014-05-02 15:14:54 -07003287
3288 @Override
3289 public void incrementRecipientsTimesContacted(final List<String> recipients) {
3290 ComposeActivity.this.incrementRecipientsTimesContacted(recipients);
3291 }
Mindy Pereira181df782012-03-01 13:32:44 -08003292 };
Tony Mantler1e05a1e2013-08-12 16:44:26 -07003293 setAccount(mReplyFromAccount.account);
Mindy Pereira82cc5662012-01-09 17:29:30 -08003294
Jin Cao77b4c2c2014-05-20 13:55:53 -07003295 final Spanned body = removeComposingSpans(mBodyView.getText());
3296 SEND_SAVE_TASK_HANDLER.post(new Runnable() {
3297 @Override
3298 public void run() {
3299 final Message msg = createMessage(mReplyFromAccount, mRefMessage, getMode(), body);
3300 mRequestId = sendOrSaveInternal(ComposeActivity.this, mReplyFromAccount, msg,
3301 mRefMessage, mQuotedTextView.getQuotedTextIfIncluded(), callback,
3302 SEND_SAVE_TASK_HANDLER, save, mComposeMode, mDraftAccount, mExtraValues);
3303 }
3304 });
Mindy Pereira82cc5662012-01-09 17:29:30 -08003305
Mindy Pereira181df782012-03-01 13:32:44 -08003306 // Don't display the toast if the user is just changing the orientation,
3307 // but we still need to save the draft to the cursor because this is how we restore
3308 // the attachments when the configuration change completes.
3309 if (showToast && (getChangingConfigurations() & ActivityInfo.CONFIG_ORIENTATION) == 0) {
3310 Toast.makeText(this, save ? R.string.message_saved : R.string.sending_message,
3311 Toast.LENGTH_LONG).show();
3312 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08003313
Mindy Pereira181df782012-03-01 13:32:44 -08003314 // Need to update variables here because the send or save completes
3315 // asynchronously even though the toast shows right away.
3316 discardChanges();
3317 updateSaveUi();
Mindy Pereira82cc5662012-01-09 17:29:30 -08003318
Mindy Pereira181df782012-03-01 13:32:44 -08003319 // If we are sending, finish the activity
3320 if (!save) {
3321 finish();
3322 }
3323 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08003324
Mindy Pereira181df782012-03-01 13:32:44 -08003325 /**
3326 * Save the state of the request messageid map. This allows for the Gmail
3327 * process to be killed, but and still allow for ComposeActivity instances
3328 * to be recreated correctly.
3329 */
3330 private void saveRequestMap() {
3331 // TODO: store the request map in user preferences.
3332 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08003333
Tony Mantler581edd42014-02-18 15:41:22 -08003334 @SuppressLint("NewApi")
Mindy Pereira2db7d4a2012-08-15 11:00:02 -07003335 private void doAttach(String type) {
Mindy Pereira013194c2012-01-06 15:09:33 -08003336 Intent i = new Intent(Intent.ACTION_GET_CONTENT);
3337 i.addCategory(Intent.CATEGORY_OPENABLE);
Paul Westbrookd6a9a3f2012-04-26 18:47:23 -07003338 i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Andrew Sapperstein05089f32013-10-01 17:00:03 -07003339 i.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
Mindy Pereira2db7d4a2012-08-15 11:00:02 -07003340 i.setType(type);
Mindy Pereira013194c2012-01-06 15:09:33 -08003341 mAddingAttachment = true;
Mindy Pereira181df782012-03-01 13:32:44 -08003342 startActivityForResult(Intent.createChooser(i, getText(R.string.select_attachment_type)),
3343 RESULT_PICK_ATTACHMENT);
Mindy Pereira013194c2012-01-06 15:09:33 -08003344 }
3345
Mindy Pereiraec8b0ed2012-01-06 10:35:59 -08003346 private void showCcBccViews() {
Mindy Pereiraa26b54e2012-01-06 12:54:33 -08003347 mCcBccView.show(true, true, true);
Mindy Pereiraec8b0ed2012-01-06 10:35:59 -08003348 if (mCcBccButton != null) {
Jin Cao9d358a12014-07-24 12:15:38 -07003349 mCcBccButton.setVisibility(View.GONE);
Mindy Pereiraec8b0ed2012-01-06 10:35:59 -08003350 }
3351 }
3352
Andy Huang4fe0af82013-08-20 17:24:51 -07003353 private static String getActionString(int action) {
Andy Huangdc97bf42013-08-15 16:52:45 -07003354 final String msgType;
Andy Huang4fe0af82013-08-20 17:24:51 -07003355 switch (action) {
Andy Huangdc97bf42013-08-15 16:52:45 -07003356 case COMPOSE:
3357 msgType = "new_message";
3358 break;
3359 case REPLY:
3360 msgType = "reply";
3361 break;
3362 case REPLY_ALL:
3363 msgType = "reply_all";
3364 break;
3365 case FORWARD:
3366 msgType = "forward";
3367 break;
3368 default:
3369 msgType = "unknown";
3370 break;
3371 }
Andy Huang4fe0af82013-08-20 17:24:51 -07003372 return msgType;
3373 }
3374
3375 private void logSendOrSave(boolean save) {
3376 if (!Analytics.isLoggable() || mAttachmentsView == null) {
3377 return;
3378 }
3379
3380 final String category = (save) ? "message_save" : "message_send";
3381 final int attachmentCount = getAttachments().size();
3382 final String msgType = getActionString(mComposeMode);
Andy Huangdc97bf42013-08-15 16:52:45 -07003383 final String label;
3384 final long value;
3385 if (mComposeMode == COMPOSE) {
3386 label = Integer.toString(attachmentCount);
3387 value = attachmentCount;
3388 } else {
3389 label = null;
3390 value = 0;
3391 }
3392 Analytics.getInstance().sendEvent(category, msgType, label, value);
3393 }
3394
Mindy Pereira326c6602012-01-04 15:32:42 -08003395 @Override
3396 public boolean onNavigationItemSelected(int position, long itemId) {
Mindy Pereiraa26b54e2012-01-06 12:54:33 -08003397 int initialComposeMode = mComposeMode;
Mindy Pereira326c6602012-01-04 15:32:42 -08003398 if (position == ComposeActivity.REPLY) {
3399 mComposeMode = ComposeActivity.REPLY;
3400 } else if (position == ComposeActivity.REPLY_ALL) {
3401 mComposeMode = ComposeActivity.REPLY_ALL;
3402 } else if (position == ComposeActivity.FORWARD) {
3403 mComposeMode = ComposeActivity.FORWARD;
3404 }
Mindy Pereiracbfb75a2012-06-25 14:52:23 -07003405 clearChangeListeners();
Mindy Pereiraa26b54e2012-01-06 12:54:33 -08003406 if (initialComposeMode != mComposeMode) {
Mindy Pereira154386a2012-01-11 13:02:33 -08003407 resetMessageForModeChange();
mindyp68c0bfc2012-12-04 10:29:48 -08003408 if (mRefMessage != null) {
Scott Kennedy0aeaf7d2012-11-14 18:56:05 -08003409 setFieldsFromRefMessage(mComposeMode);
Mindy Pereira8eca57a2012-03-20 16:42:34 -07003410 }
Mindy Pereiraef388302012-06-18 19:07:44 -07003411 boolean showCc = false;
3412 boolean showBcc = false;
3413 if (mDraft != null) {
3414 // Following desktop behavior, if the user has added a BCC
3415 // field to a draft, we show it regardless of compose mode.
Scott Kennedy8960f0a2012-11-07 15:35:50 -08003416 showBcc = !TextUtils.isEmpty(mDraft.getBcc());
Mindy Pereiraef388302012-06-18 19:07:44 -07003417 // Use the draft to determine what to populate.
3418 // If the Bcc field is showing, show the Cc field whether it is populated or not.
Scott Kennedy8960f0a2012-11-07 15:35:50 -08003419 showCc = showBcc
3420 || (!TextUtils.isEmpty(mDraft.getCc()) && mComposeMode == REPLY_ALL);
mindyp68c0bfc2012-12-04 10:29:48 -08003421 }
3422 if (mRefMessage != null) {
mindyp9b1ac572012-09-27 14:12:00 -07003423 showCc = !TextUtils.isEmpty(mCc.getText());
mindyp68c0bfc2012-12-04 10:29:48 -08003424 showBcc = !TextUtils.isEmpty(mBcc.getText());
Mindy Pereiraef388302012-06-18 19:07:44 -07003425 }
Jin Caoc5c550a2014-07-29 11:53:17 -07003426 mCcBccView.show(false /* animate */, showCc, showBcc);
Mindy Pereiraa26b54e2012-01-06 12:54:33 -08003427 }
Mindy Pereiraef388302012-06-18 19:07:44 -07003428 updateHideOrShowCcBcc();
Mindy Pereiracbfb75a2012-06-25 14:52:23 -07003429 initChangeListeners();
Mindy Pereira326c6602012-01-04 15:32:42 -08003430 return true;
3431 }
3432
Mindy Pereirab3112a22012-06-20 12:10:03 -07003433 @VisibleForTesting
3434 protected void resetMessageForModeChange() {
Mindy Pereira154386a2012-01-11 13:02:33 -08003435 // When switching between reply, reply all, forward,
3436 // follow the behavior of webview.
3437 // The contents of the following fields are cleared
3438 // so that they can be populated directly from the
3439 // ref message:
3440 // 1) Any recipient fields
3441 // 2) The subject
3442 mTo.setText("");
3443 mCc.setText("");
3444 mBcc.setText("");
3445 // Any edits to the subject are replaced with the original subject.
3446 mSubject.setText("");
3447
3448 // Any changes to the contents of the following fields are kept:
3449 // 1) Body
3450 // 2) Attachments
3451 // If the user made changes to attachments, keep their changes.
3452 if (!mAttachmentsChanged) {
3453 mAttachmentsView.deleteAllAttachments();
3454 }
3455 }
3456
Mindy Pereira326c6602012-01-04 15:32:42 -08003457 private class ComposeModeAdapter extends ArrayAdapter<String> {
3458
Jin Caof7461632014-08-11 15:21:43 -07003459 private Context mContext;
Mindy Pereira326c6602012-01-04 15:32:42 -08003460 private LayoutInflater mInflater;
3461
3462 public ComposeModeAdapter(Context context) {
3463 super(context, R.layout.compose_mode_item, R.id.mode, getResources()
3464 .getStringArray(R.array.compose_modes));
Jin Caof7461632014-08-11 15:21:43 -07003465 mContext = context;
Mindy Pereira326c6602012-01-04 15:32:42 -08003466 }
3467
3468 private LayoutInflater getInflater() {
3469 if (mInflater == null) {
Jin Caof7461632014-08-11 15:21:43 -07003470 mInflater = LayoutInflater.from(mContext);
Mindy Pereira326c6602012-01-04 15:32:42 -08003471 }
3472 return mInflater;
3473 }
3474
3475 @Override
3476 public View getView(int position, View convertView, ViewGroup parent) {
3477 if (convertView == null) {
3478 convertView = getInflater().inflate(R.layout.compose_mode_display_item, null);
3479 }
3480 ((TextView) convertView.findViewById(R.id.mode)).setText(getItem(position));
3481 return super.getView(position, convertView, parent);
3482 }
3483 }
Mindy Pereira46ce0b12012-01-05 10:32:15 -08003484
3485 @Override
3486 public void onRespondInline(String text) {
3487 appendToBody(text, false);
mindyp40882432012-09-06 11:07:40 -07003488 mQuotedTextView.setUpperDividerVisible(false);
mindyp1623f9b2012-11-21 12:41:16 -08003489 mRespondedInline = true;
mindyp09dd3732012-12-17 08:37:52 -08003490 if (!mBodyView.hasFocus()) {
mindyp8654d4f2012-12-17 09:01:37 -08003491 mBodyView.requestFocus();
mindyp09dd3732012-12-17 08:37:52 -08003492 }
Mindy Pereira46ce0b12012-01-05 10:32:15 -08003493 }
3494
3495 /**
3496 * Append text to the body of the message. If there is no existing body
3497 * text, just sets the body to text.
3498 *
Tony Mantler581edd42014-02-18 15:41:22 -08003499 * @param text Text to append
Mindy Pereira46ce0b12012-01-05 10:32:15 -08003500 * @param withSignature True to append a signature.
3501 */
3502 public void appendToBody(CharSequence text, boolean withSignature) {
Mindy Pereiraeaea9f12012-01-10 15:05:27 -08003503 Editable bodyText = mBodyView.getEditableText();
Mindy Pereira46ce0b12012-01-05 10:32:15 -08003504 if (bodyText != null && bodyText.length() > 0) {
3505 bodyText.append(text);
3506 } else {
3507 setBody(text, withSignature);
3508 }
3509 }
3510
3511 /**
3512 * Set the body of the message.
Mindy Pereirabdf7a402012-03-01 15:23:26 -08003513 *
Tony Mantler581edd42014-02-18 15:41:22 -08003514 * @param text text to set
Mindy Pereira46ce0b12012-01-05 10:32:15 -08003515 * @param withSignature True to append a signature.
3516 */
3517 public void setBody(CharSequence text, boolean withSignature) {
Mindy Pereiraeaea9f12012-01-10 15:05:27 -08003518 mBodyView.setText(text);
Mindy Pereirabdf7a402012-03-01 15:23:26 -08003519 if (withSignature) {
3520 appendSignature();
3521 }
3522 }
3523
3524 private void appendSignature() {
Tony Mantler6a7ac782014-02-19 15:22:02 -08003525 final String newSignature = mCachedSettings != null ? mCachedSettings.signature : null;
3526 final int signaturePos = getSignatureStartPosition(mSignature, mBodyView.getText().toString());
mindyp27083062012-11-15 09:02:01 -08003527 if (!TextUtils.equals(newSignature, mSignature) || signaturePos < 0) {
Mindy Pereirab13917c2012-03-29 08:08:19 -07003528 mSignature = newSignature;
mindyp27083062012-11-15 09:02:01 -08003529 if (!TextUtils.isEmpty(mSignature)) {
Mindy Pereirab13917c2012-03-29 08:08:19 -07003530 // Appending a signature does not count as changing text.
3531 mBodyView.removeTextChangedListener(this);
3532 mBodyView.append(convertToPrintableSignature(mSignature));
3533 mBodyView.addTextChangedListener(this);
3534 }
Tony Mantler6a7ac782014-02-19 15:22:02 -08003535 resetBodySelection();
Mindy Pereirabdf7a402012-03-01 15:23:26 -08003536 }
3537 }
3538
3539 private String convertToPrintableSignature(String signature) {
3540 String signatureResource = getResources().getString(R.string.signature);
3541 if (signature == null) {
3542 signature = "";
3543 }
3544 return String.format(signatureResource, signature);
Mindy Pereira46ce0b12012-01-05 10:32:15 -08003545 }
Mindy Pereira1a95a572012-01-05 12:21:29 -08003546
Mindy Pereira5a85e2b2012-01-11 09:53:32 -08003547 @Override
3548 public void onAccountChanged() {
Mindy Pereira92551d02012-04-05 11:31:12 -07003549 mReplyFromAccount = mFromSpinner.getCurrentAccount();
3550 if (!mAccount.equals(mReplyFromAccount.account)) {
mindypf432dbc2012-11-12 16:00:44 -08003551 // Clear a signature, if there was one.
3552 mBodyView.removeTextChangedListener(this);
3553 String oldSignature = mSignature;
3554 String bodyText = getBody().getText().toString();
3555 if (!TextUtils.isEmpty(oldSignature)) {
3556 int pos = getSignatureStartPosition(oldSignature, bodyText);
3557 if (pos > -1) {
3558 mBodyView.setText(bodyText.substring(0, pos));
3559 }
3560 }
Paul Westbrookb1f573c2012-04-06 11:38:28 -07003561 setAccount(mReplyFromAccount.account);
mindypf432dbc2012-11-12 16:00:44 -08003562 mBodyView.addTextChangedListener(this);
Mindy Pereira181df782012-03-01 13:32:44 -08003563 // TODO: handle discarding attachments when switching accounts.
3564 // Only enable save for this draft if there is any other content
3565 // in the message.
3566 if (!isBlank()) {
3567 enableSave(true);
3568 }
3569 mReplyFromChanged = true;
3570 initRecipients();
Greg Bullockd47a7042014-08-13 16:02:59 +02003571
3572 invalidateOptionsMenu();
Mindy Pereira82cc5662012-01-09 17:29:30 -08003573 }
Mindy Pereira1a95a572012-01-05 12:21:29 -08003574 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08003575
3576 public void enableSave(boolean enabled) {
3577 if (mSave != null) {
3578 mSave.setEnabled(enabled);
3579 }
3580 }
3581
Tony Mantler2558b502013-07-09 10:53:34 -07003582 public static class DiscardConfirmDialogFragment extends DialogFragment {
Paul Westbrookf0ea4842013-08-13 16:41:18 -07003583 // Public no-args constructor needed for fragment re-instantiation
3584 public DiscardConfirmDialogFragment() {}
3585
Tony Mantler2558b502013-07-09 10:53:34 -07003586 @Override
3587 public Dialog onCreateDialog(Bundle savedInstanceState) {
3588 return new AlertDialog.Builder(getActivity())
3589 .setMessage(R.string.confirm_discard_text)
3590 .setPositiveButton(R.string.discard,
3591 new DialogInterface.OnClickListener() {
3592 @Override
3593 public void onClick(DialogInterface dialog, int which) {
3594 ((ComposeActivity)getActivity()).doDiscardWithoutConfirmation();
3595 }
3596 })
Tony Mantler2b215b72013-07-31 10:20:46 -07003597 .setNegativeButton(R.string.cancel, null)
Tony Mantler2558b502013-07-09 10:53:34 -07003598 .create();
Mindy Pereira82cc5662012-01-09 17:29:30 -08003599 }
3600 }
3601
Mindy Pereiraefe3d252012-03-01 14:20:44 -08003602 private void doDiscard() {
Tony Mantler2558b502013-07-09 10:53:34 -07003603 final DialogFragment frag = new DiscardConfirmDialogFragment();
3604 frag.show(getFragmentManager(), "discard confirm");
Mindy Pereiraefe3d252012-03-01 14:20:44 -08003605 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08003606 /**
3607 * Effectively discard the current message.
3608 *
3609 * This method is either invoked from the menu or from the dialog
3610 * once the user has confirmed that they want to discard the message.
Mindy Pereira82cc5662012-01-09 17:29:30 -08003611 */
Tony Mantler2558b502013-07-09 10:53:34 -07003612 private void doDiscardWithoutConfirmation() {
Mindy Pereira7ed1c112012-01-18 10:59:25 -08003613 synchronized (mDraftLock) {
Mindy Pereira82cc5662012-01-09 17:29:30 -08003614 if (mDraftId != UIProvider.INVALID_MESSAGE_ID) {
3615 ContentValues values = new ContentValues();
Paul Westbrookb7050e62012-03-20 12:59:44 -07003616 values.put(BaseColumns._ID, mDraftId);
Marc Blank78ea8e22012-08-04 11:14:06 -07003617 if (!mAccount.expungeMessageUri.equals(Uri.EMPTY)) {
Mindy Pereiracfb7f332012-02-28 10:23:43 -08003618 getContentResolver().update(mAccount.expungeMessageUri, values, null, null);
3619 } else {
Marc Blank0bbc8582012-04-23 15:07:57 -07003620 getContentResolver().delete(mDraft.uri, null, null);
Mindy Pereiracfb7f332012-02-28 10:23:43 -08003621 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08003622 // This is not strictly necessary (since we should not try to
3623 // save the draft after calling this) but it ensures that if we
3624 // do save again for some reason we make a new draft rather than
3625 // trying to resave an expunged draft.
3626 mDraftId = UIProvider.INVALID_MESSAGE_ID;
3627 }
3628 }
3629
Tony Mantler2558b502013-07-09 10:53:34 -07003630 // Display a toast to let the user know
3631 Toast.makeText(this, R.string.message_discarded, Toast.LENGTH_SHORT).show();
Mindy Pereira82cc5662012-01-09 17:29:30 -08003632
3633 // This prevents the draft from being saved in onPause().
3634 discardChanges();
Andy Huangdc97bf42013-08-15 16:52:45 -07003635 mPerformedSendOrDiscard = true;
Mindy Pereira82cc5662012-01-09 17:29:30 -08003636 finish();
3637 }
3638
Mindy Pereiraeaea9f12012-01-10 15:05:27 -08003639 private void saveIfNeeded() {
3640 if (mAccount == null) {
3641 // We have not chosen an account yet so there's no way that we can save. This is ok,
3642 // though, since we are saving our state before AccountsActivity is activated. Thus, the
3643 // user has not interacted with us yet and there is no real state to save.
3644 return;
3645 }
3646
3647 if (shouldSave()) {
Mindy Pereira48e31b02012-05-30 13:12:24 -07003648 doSave(!mAddingAttachment /* show toast */);
Mindy Pereiraeaea9f12012-01-10 15:05:27 -08003649 }
3650 }
3651
Mindy Pereiraeaea9f12012-01-10 15:05:27 -08003652 @Override
3653 public void onAttachmentDeleted() {
3654 mAttachmentsChanged = true;
mindyp40882432012-09-06 11:07:40 -07003655 // If we are showing any attachments, make sure we have an upper
3656 // divider.
3657 mQuotedTextView.setUpperDividerVisible(mAttachmentsView.getAttachments().size() > 0);
Mindy Pereiraeaea9f12012-01-10 15:05:27 -08003658 updateSaveUi();
3659 }
Mindy Pereira75f66632012-01-11 11:42:02 -08003660
mindyp40882432012-09-06 11:07:40 -07003661 @Override
3662 public void onAttachmentAdded() {
3663 mQuotedTextView.setUpperDividerVisible(mAttachmentsView.getAttachments().size() > 0);
3664 mAttachmentsView.focusLastAttachment();
3665 }
Mindy Pereira75f66632012-01-11 11:42:02 -08003666
3667 /**
3668 * This is called any time one of our text fields changes.
3669 */
Marc Blank0bbc8582012-04-23 15:07:57 -07003670 @Override
Mindy Pereira75f66632012-01-11 11:42:02 -08003671 public void afterTextChanged(Editable s) {
3672 mTextChanged = true;
3673 updateSaveUi();
3674 }
3675
3676 @Override
3677 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
3678 // Do nothing.
3679 }
3680
Marc Blank0bbc8582012-04-23 15:07:57 -07003681 @Override
Mindy Pereira75f66632012-01-11 11:42:02 -08003682 public void onTextChanged(CharSequence s, int start, int before, int count) {
3683 // Do nothing.
3684 }
3685
3686
3687 // There is a big difference between the text associated with an address changing
3688 // to add the display name or to format properly and a recipient being added or deleted.
3689 // Make sure we only notify of changes when a recipient has been added or deleted.
3690 private class RecipientTextWatcher implements TextWatcher {
3691 private HashMap<String, Integer> mContent = new HashMap<String, Integer>();
3692
3693 private RecipientEditTextView mView;
3694
3695 private TextWatcher mListener;
3696
3697 public RecipientTextWatcher(RecipientEditTextView view, TextWatcher listener) {
3698 mView = view;
3699 mListener = listener;
3700 }
3701
3702 @Override
3703 public void afterTextChanged(Editable s) {
3704 if (hasChanged()) {
3705 mListener.afterTextChanged(s);
3706 }
3707 }
3708
3709 private boolean hasChanged() {
Andrew Sapperstein1b7ad922014-05-28 11:23:33 -07003710 final ArrayList<String> currRecips = buildEmailAddressList(getAddressesFromList(mView));
3711 int totalCount = currRecips.size();
Mindy Pereira75f66632012-01-11 11:42:02 -08003712 int totalPrevCount = 0;
3713 for (Entry<String, Integer> entry : mContent.entrySet()) {
3714 totalPrevCount += entry.getValue();
3715 }
3716 if (totalCount != totalPrevCount) {
3717 return true;
3718 }
3719
3720 for (String recip : currRecips) {
3721 if (!mContent.containsKey(recip)) {
3722 return true;
3723 } else {
3724 int count = mContent.get(recip) - 1;
3725 if (count < 0) {
3726 return true;
3727 } else {
3728 mContent.put(recip, count);
3729 }
3730 }
3731 }
3732 return false;
3733 }
3734
Mindy Pereira75f66632012-01-11 11:42:02 -08003735 @Override
3736 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
Andrew Sapperstein1b7ad922014-05-28 11:23:33 -07003737 final ArrayList<String> recips = buildEmailAddressList(getAddressesFromList(mView));
Mindy Pereira75f66632012-01-11 11:42:02 -08003738 for (String recip : recips) {
3739 if (!mContent.containsKey(recip)) {
3740 mContent.put(recip, 1);
3741 } else {
3742 mContent.put(recip, (mContent.get(recip)) + 1);
3743 }
3744 }
3745 }
3746
3747 @Override
3748 public void onTextChanged(CharSequence s, int start, int before, int count) {
3749 // Do nothing.
3750 }
3751 }
Mindy Pereirae011b1d2012-06-18 13:45:26 -07003752
Andrew Sapperstein1b7ad922014-05-28 11:23:33 -07003753 /**
3754 * Returns a list of email addresses from the recipients. List only contains
3755 * email addresses strips additional info like the recipient's name.
3756 */
3757 private static ArrayList<String> buildEmailAddressList(String[] recips) {
3758 // Tokenize them all and put them in the list.
3759 final ArrayList<String> recipAddresses = Lists.newArrayListWithCapacity(recips.length);
3760 for (int i = 0; i < recips.length; i++) {
3761 recipAddresses.add(Rfc822Tokenizer.tokenize(recips[i])[0].getAddress());
3762 }
3763 return recipAddresses;
3764 }
3765
Mindy Pereirae011b1d2012-06-18 13:45:26 -07003766 public static void registerTestSendOrSaveCallback(SendOrSaveCallback testCallback) {
3767 if (sTestSendOrSaveCallback != null && testCallback != null) {
3768 throw new IllegalStateException("Attempting to register more than one test callback");
3769 }
3770 sTestSendOrSaveCallback = testCallback;
3771 }
Mindy Pereirabddd6f32012-06-20 12:10:03 -07003772
3773 @VisibleForTesting
3774 protected ArrayList<Attachment> getAttachments() {
3775 return mAttachmentsView.getAttachments();
3776 }
Mindy Pereira96a7f7a2012-07-09 16:51:06 -07003777
3778 @Override
3779 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
3780 switch (id) {
Alice Yanga990a712013-03-13 18:37:00 -07003781 case INIT_DRAFT_USING_REFERENCE_MESSAGE:
3782 return new CursorLoader(this, mRefMessageUri, UIProvider.MESSAGE_PROJECTION, null,
3783 null, null);
Mindy Pereira96a7f7a2012-07-09 16:51:06 -07003784 case REFERENCE_MESSAGE_LOADER:
3785 return new CursorLoader(this, mRefMessageUri, UIProvider.MESSAGE_PROJECTION, null,
3786 null, null);
Mindy Pereirab199d172012-08-13 11:04:03 -07003787 case LOADER_ACCOUNT_CURSOR:
3788 return new CursorLoader(this, MailAppProvider.getAccountsUri(),
3789 UIProvider.ACCOUNTS_PROJECTION, null, null, null);
Mindy Pereira96a7f7a2012-07-09 16:51:06 -07003790 }
3791 return null;
3792 }
3793
3794 @Override
3795 public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
Mindy Pereirab199d172012-08-13 11:04:03 -07003796 int id = loader.getId();
3797 switch (id) {
Alice Yanga990a712013-03-13 18:37:00 -07003798 case INIT_DRAFT_USING_REFERENCE_MESSAGE:
Mindy Pereirab199d172012-08-13 11:04:03 -07003799 if (data != null && data.moveToFirst()) {
3800 mRefMessage = new Message(data);
Mindy Pereirab199d172012-08-13 11:04:03 -07003801 Intent intent = getIntent();
Alice Yanga990a712013-03-13 18:37:00 -07003802 initFromRefMessage(mComposeMode);
3803 finishSetup(mComposeMode, intent, null);
3804 if (mComposeMode != FORWARD) {
Mindy Pereirab199d172012-08-13 11:04:03 -07003805 String to = intent.getStringExtra(EXTRA_TO);
3806 if (!TextUtils.isEmpty(to)) {
Scott Kennedy0aeaf7d2012-11-14 18:56:05 -08003807 mRefMessage.setTo(null);
3808 mRefMessage.setFrom(null);
Mindy Pereirab199d172012-08-13 11:04:03 -07003809 clearChangeListeners();
3810 mTo.append(to);
3811 initChangeListeners();
3812 }
3813 }
3814 } else {
3815 finish();
Mindy Pereira96a7f7a2012-07-09 16:51:06 -07003816 }
Mindy Pereirab199d172012-08-13 11:04:03 -07003817 break;
Alice Yanga990a712013-03-13 18:37:00 -07003818 case REFERENCE_MESSAGE_LOADER:
3819 // Only populate mRefMessage and leave other fields untouched.
3820 if (data != null && data.moveToFirst()) {
3821 mRefMessage = new Message(data);
3822 }
Andy Huang9f855d62013-05-30 17:15:03 -07003823 finishSetup(mComposeMode, getIntent(), mInnerSavedState);
Alice Yanga990a712013-03-13 18:37:00 -07003824 break;
Mindy Pereirab199d172012-08-13 11:04:03 -07003825 case LOADER_ACCOUNT_CURSOR:
3826 if (data != null && data.moveToFirst()) {
3827 // there are accounts now!
3828 Account account;
Paul Westbrookfaa742f2012-11-01 09:50:16 -07003829 final ArrayList<Account> accounts = new ArrayList<Account>();
3830 final ArrayList<Account> initializedAccounts = new ArrayList<Account>();
Mindy Pereirab199d172012-08-13 11:04:03 -07003831 do {
Ray Chen4b0c0122014-07-11 15:24:54 +02003832 account = Account.builder().buildFrom(data);
Paul Westbrookdfa1dec2012-09-26 16:27:28 -07003833 if (account.isAccountReady()) {
Mindy Pereirab199d172012-08-13 11:04:03 -07003834 initializedAccounts.add(account);
3835 }
3836 accounts.add(account);
3837 } while (data.moveToNext());
3838 if (initializedAccounts.size() > 0) {
3839 findViewById(R.id.wait).setVisibility(View.GONE);
3840 getLoaderManager().destroyLoader(LOADER_ACCOUNT_CURSOR);
3841 findViewById(R.id.compose).setVisibility(View.VISIBLE);
Paul Westbrookfaa742f2012-11-01 09:50:16 -07003842 mAccounts = initializedAccounts.toArray(
3843 new Account[initializedAccounts.size()]);
3844
Mindy Pereirab199d172012-08-13 11:04:03 -07003845 finishCreate();
3846 invalidateOptionsMenu();
3847 } else {
3848 // Show "waiting"
3849 account = accounts.size() > 0 ? accounts.get(0) : null;
3850 showWaitFragment(account);
3851 }
3852 }
3853 break;
Mindy Pereira96a7f7a2012-07-09 16:51:06 -07003854 }
3855 }
3856
Mindy Pereirab199d172012-08-13 11:04:03 -07003857 private void showWaitFragment(Account account) {
3858 WaitFragment fragment = getWaitFragment();
3859 if (fragment != null) {
3860 fragment.updateAccount(account);
3861 } else {
3862 findViewById(R.id.wait).setVisibility(View.VISIBLE);
Andy Huangc96efcc2014-04-09 15:30:42 -07003863 replaceFragment(WaitFragment.newInstance(account, false /* expectingMessages */),
Mindy Pereirab199d172012-08-13 11:04:03 -07003864 FragmentTransaction.TRANSIT_FRAGMENT_OPEN, TAG_WAIT);
3865 }
3866 }
3867
3868 private WaitFragment getWaitFragment() {
3869 return (WaitFragment) getFragmentManager().findFragmentByTag(TAG_WAIT);
3870 }
3871
3872 private int replaceFragment(Fragment fragment, int transition, String tag) {
3873 FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
Mindy Pereirab199d172012-08-13 11:04:03 -07003874 fragmentTransaction.setTransition(transition);
3875 fragmentTransaction.replace(R.id.wait, fragment, tag);
3876 final int transactionId = fragmentTransaction.commitAllowingStateLoss();
3877 return transactionId;
3878 }
3879
Mindy Pereira96a7f7a2012-07-09 16:51:06 -07003880 @Override
3881 public void onLoaderReset(Loader<Cursor> arg0) {
3882 // Do nothing.
3883 }
Jin Cao77b4c2c2014-05-20 13:55:53 -07003884
3885 /**
3886 * Background task to convert the message's html to Spanned.
3887 */
3888 private class HtmlToSpannedTask extends AsyncTask<String, Void, Spanned> {
3889
3890 @Override
3891 protected Spanned doInBackground(String... input) {
Andy Huang9ed742c2014-06-18 02:34:50 -07003892 return HtmlUtils.htmlToSpan(input[0], mSpanConverterFactory);
Jin Cao77b4c2c2014-05-20 13:55:53 -07003893 }
3894
3895 @Override
3896 protected void onPostExecute(Spanned spanned) {
3897 mBodyView.removeTextChangedListener(ComposeActivity.this);
3898 mBodyView.setText(spanned);
3899 mTextChanged = false;
3900 mBodyView.addTextChangedListener(ComposeActivity.this);
3901 }
3902 }
Andy Huang1f8f4dd2012-10-25 21:35:35 -07003903}