blob: 1f410c9f8da523770edf2c1e6bd257fad955e236 [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
Mindy Pereira326c6602012-01-04 15:32:42 -080019import android.app.ActionBar;
Andy Huang5c5fd572012-04-08 18:19:29 -070020import android.app.ActionBar.OnNavigationListener;
21import android.app.Activity;
Mindy Pereira82cc5662012-01-09 17:29:30 -080022import android.app.ActivityManager;
23import android.app.AlertDialog;
24import android.app.Dialog;
Mindy Pereirab199d172012-08-13 11:04:03 -070025import android.app.Fragment;
Mindy Pereirab199d172012-08-13 11:04:03 -070026import android.app.FragmentTransaction;
Mindy Pereira96a7f7a2012-07-09 16:51:06 -070027import android.app.LoaderManager;
Mindy Pereira6349a042012-01-04 11:25:01 -080028import android.content.ContentResolver;
Mindy Pereira82cc5662012-01-09 17:29:30 -080029import android.content.ContentValues;
Mindy Pereira6349a042012-01-04 11:25:01 -080030import android.content.Context;
Mindy Pereira96a7f7a2012-07-09 16:51:06 -070031import android.content.CursorLoader;
Mindy Pereira82cc5662012-01-09 17:29:30 -080032import android.content.DialogInterface;
Mindy Pereira6349a042012-01-04 11:25:01 -080033import android.content.Intent;
Mindy Pereira96a7f7a2012-07-09 16:51:06 -070034import android.content.Loader;
Mindy Pereira82cc5662012-01-09 17:29:30 -080035import android.content.pm.ActivityInfo;
Mindy Pereira7ed1c112012-01-18 10:59:25 -080036import android.database.Cursor;
Mindy Pereira6349a042012-01-04 11:25:01 -080037import android.net.Uri;
Mindy Pereira8e9305e2011-12-13 14:25:04 -080038import android.os.Bundle;
Mindy Pereira82cc5662012-01-09 17:29:30 -080039import android.os.Handler;
40import android.os.HandlerThread;
Paul Westbrookf97588b2012-03-20 11:11:37 -070041import android.os.Parcelable;
Paul Westbrook3c7f94d2012-10-23 14:13:00 -070042import android.os.ParcelFileDescriptor;
Mindy Pereira82cc5662012-01-09 17:29:30 -080043import android.provider.BaseColumns;
Mindy Pereira46ce0b12012-01-05 10:32:15 -080044import android.text.Editable;
Mindy Pereira82cc5662012-01-09 17:29:30 -080045import android.text.Html;
mindyped9c2f02012-10-12 10:02:08 -070046import android.text.SpannableString;
Mindy Pereira82cc5662012-01-09 17:29:30 -080047import android.text.Spanned;
Paul Westbrookc1827622012-01-06 11:27:12 -080048import android.text.TextUtils;
Mindy Pereira82cc5662012-01-09 17:29:30 -080049import android.text.TextWatcher;
Mindy Pereira46ce0b12012-01-05 10:32:15 -080050import android.text.util.Rfc822Token;
Mindy Pereirac17d0732011-12-29 10:46:19 -080051import android.text.util.Rfc822Tokenizer;
Mindy Pereira3cd4f402012-07-17 11:16:18 -070052import android.view.Gravity;
mindyp62d3ec72012-08-24 13:04:09 -070053import android.view.KeyEvent;
Mindy Pereira326c6602012-01-04 15:32:42 -080054import android.view.LayoutInflater;
Mindy Pereirab47f3e22011-12-13 14:25:04 -080055import android.view.Menu;
56import android.view.MenuInflater;
57import android.view.MenuItem;
Mindy Pereira8e9305e2011-12-13 14:25:04 -080058import android.view.View;
59import android.view.View.OnClickListener;
Andy Huang5c5fd572012-04-08 18:19:29 -070060import android.view.ViewGroup;
mindyp62d3ec72012-08-24 13:04:09 -070061import android.view.inputmethod.EditorInfo;
Mindy Pereira326c6602012-01-04 15:32:42 -080062import android.widget.ArrayAdapter;
Mindy Pereira8e9305e2011-12-13 14:25:04 -080063import android.widget.Button;
Mindy Pereira433b1982012-04-03 11:53:07 -070064import android.widget.EditText;
Mindy Pereira6349a042012-01-04 11:25:01 -080065import android.widget.TextView;
Mindy Pereira013194c2012-01-06 15:09:33 -080066import android.widget.Toast;
Mindy Pereira7b56a612011-12-14 12:32:28 -080067
Mindy Pereirac17d0732011-12-29 10:46:19 -080068import com.android.common.Rfc822Validator;
Andy Huang5c5fd572012-04-08 18:19:29 -070069import com.android.ex.chips.RecipientEditTextView;
70import com.android.mail.R;
mindyp40882432012-09-06 11:07:40 -070071import com.android.mail.compose.AttachmentsView.AttachmentAddedOrDeletedListener;
Mindy Pereira9932dee2012-01-10 16:09:50 -080072import com.android.mail.compose.AttachmentsView.AttachmentFailureException;
Mindy Pereira5a85e2b2012-01-11 09:53:32 -080073import com.android.mail.compose.FromAddressSpinner.OnAccountChangedListener;
Andy Huang30e2c242012-01-06 18:14:30 -080074import com.android.mail.compose.QuotedTextView.RespondInlineListener;
Mindy Pereira33fe9082012-01-09 16:24:30 -080075import com.android.mail.providers.Account;
Andy Huang30e2c242012-01-06 18:14:30 -080076import com.android.mail.providers.Address;
77import com.android.mail.providers.Attachment;
Mindy Pereira47d0e652012-07-23 09:45:07 -070078import com.android.mail.providers.MailAppProvider;
Mindy Pereira3ce64e72012-01-13 14:29:45 -080079import com.android.mail.providers.Message;
Mindy Pereira82cc5662012-01-09 17:29:30 -080080import com.android.mail.providers.MessageModification;
Mindy Pereira92551d02012-04-05 11:31:12 -070081import com.android.mail.providers.ReplyFromAccount;
Mindy Pereira181df782012-03-01 13:32:44 -080082import com.android.mail.providers.Settings;
Andy Huang30e2c242012-01-06 18:14:30 -080083import com.android.mail.providers.UIProvider;
Mindy Pereira3ca5bad2012-04-16 11:02:42 -070084import com.android.mail.providers.UIProvider.AccountCapabilities;
Mindy Pereira12575862012-03-21 16:30:54 -070085import com.android.mail.providers.UIProvider.DraftType;
Mindy Pereirafa20c1a2012-07-23 13:00:02 -070086import com.android.mail.ui.MailActivity;
Mindy Pereirab199d172012-08-13 11:04:03 -070087import com.android.mail.ui.WaitFragment;
Mark Wei62066e42012-09-13 12:07:02 -070088import com.android.mail.ui.AttachmentTile.AttachmentPreview;
Paul Westbrook92227f62012-03-20 10:32:51 -070089import com.android.mail.utils.AccountUtils;
Mark Wei434f2942012-08-24 11:54:02 -070090import com.android.mail.utils.AttachmentUtils;
mindypfebd2262012-11-13 17:45:09 -080091import com.android.mail.utils.ContentProviderTask;
Paul Westbrookb334c902012-06-25 11:42:46 -070092import com.android.mail.utils.LogTag;
Andy Huang30e2c242012-01-06 18:14:30 -080093import com.android.mail.utils.LogUtils;
Andy Huang30e2c242012-01-06 18:14:30 -080094import com.android.mail.utils.Utils;
Mindy Pereira46ce0b12012-01-05 10:32:15 -080095import com.google.common.annotations.VisibleForTesting;
Mindy Pereira82cc5662012-01-09 17:29:30 -080096import com.google.common.collect.Lists;
Mindy Pereira4a27ea92012-01-05 15:55:25 -080097import com.google.common.collect.Sets;
Mindy Pereira8e9305e2011-12-13 14:25:04 -080098
Paul Westbrook3c7f94d2012-10-23 14:13:00 -070099import java.io.FileNotFoundException;
100import java.io.IOException;
Mindy Pereira8eca57a2012-03-20 16:42:34 -0700101import java.io.UnsupportedEncodingException;
102import java.net.URLDecoder;
Mindy Pereira46ce0b12012-01-05 10:32:15 -0800103import java.util.ArrayList;
Paul Westbrookbb87b7f2012-03-20 16:20:07 -0700104import java.util.Arrays;
Mindy Pereira46ce0b12012-01-05 10:32:15 -0800105import java.util.Collection;
Mindy Pereira75f66632012-01-11 11:42:02 -0800106import java.util.HashMap;
Mindy Pereira46ce0b12012-01-05 10:32:15 -0800107import java.util.HashSet;
108import java.util.List;
Paul Westbrook1c078cf2012-03-20 16:18:51 -0700109import java.util.Map.Entry;
Paul Westbrookbb87b7f2012-03-20 16:20:07 -0700110import java.util.Set;
Mindy Pereira82cc5662012-01-09 17:29:30 -0800111import java.util.concurrent.ConcurrentHashMap;
Mindy Pereira46ce0b12012-01-05 10:32:15 -0800112
113public class ComposeActivity extends Activity implements OnClickListener, OnNavigationListener,
Mindy Pereira5a85e2b2012-01-11 09:53:32 -0800114 RespondInlineListener, DialogInterface.OnClickListener, TextWatcher,
mindyp40882432012-09-06 11:07:40 -0700115 AttachmentAddedOrDeletedListener, OnAccountChangedListener, LoaderManager.LoaderCallbacks<Cursor>,
mindyp62d3ec72012-08-24 13:04:09 -0700116 TextView.OnEditorActionListener {
Mindy Pereira6349a042012-01-04 11:25:01 -0800117 // Identifiers for which type of composition this is
Mindy Pereira36bbcae2012-04-25 09:27:04 -0700118 static final int COMPOSE = -1;
119 static final int REPLY = 0;
120 static final int REPLY_ALL = 1;
121 static final int FORWARD = 2;
122 static final int EDIT_DRAFT = 3;
Mindy Pereira6349a042012-01-04 11:25:01 -0800123
124 // Integer extra holding one of the above compose action
Mindy Pereira96a7f7a2012-07-09 16:51:06 -0700125 protected static final String EXTRA_ACTION = "action";
Mindy Pereira6349a042012-01-04 11:25:01 -0800126
Mindy Pereira326689d2012-05-17 10:14:14 -0700127 private static final String EXTRA_SHOW_CC = "showCc";
128 private static final String EXTRA_SHOW_BCC = "showBcc";
Mindy Pereiraa34c9a02012-04-17 14:10:53 -0700129
Mindy Pereira8eca57a2012-03-20 16:42:34 -0700130 private static final String UTF8_ENCODING_NAME = "UTF-8";
131
132 private static final String MAIL_TO = "mailto";
133
Mindy Pereira8eca57a2012-03-20 16:42:34 -0700134 private static final String EXTRA_SUBJECT = "subject";
135
136 private static final String EXTRA_BODY = "body";
137
mindypd27b6ea2012-10-05 09:43:49 -0700138 protected static final String EXTRA_FROM_ACCOUNT_STRING = "fromAccountString";
Mindy Pereira9a42bb42012-04-18 15:21:33 -0700139
Mark Wei62066e42012-09-13 12:07:02 -0700140 private static final String EXTRA_ATTACHMENT_PREVIEWS = "attachmentPreviews";
141
Mindy Pereira8eca57a2012-03-20 16:42:34 -0700142 // Extra that we can get passed from other activities
143 private static final String EXTRA_TO = "to";
144 private static final String EXTRA_CC = "cc";
145 private static final String EXTRA_BCC = "bcc";
146
147 // List of all the fields
148 static final String[] ALL_EXTRAS = { EXTRA_SUBJECT, EXTRA_BODY, EXTRA_TO, EXTRA_CC, EXTRA_BCC };
149
Mindy Pereira82cc5662012-01-09 17:29:30 -0800150 private static SendOrSaveCallback sTestSendOrSaveCallback = null;
151 // Map containing information about requests to create new messages, and the id of the
152 // messages that were the result of those requests.
153 //
154 // This map is used when the activity that initiated the save a of a new message, is killed
155 // before the save has completed (and when we know the id of the newly created message). When
156 // a save is completed, the service that is running in the background, will update the map
157 //
158 // When a new ComposeActivity instance is created, it will attempt to use the information in
159 // the previously instantiated map. If ComposeActivity.onCreate() is called, with a bundle
160 // (restoring data from a previous instance), and the map hasn't been created, we will attempt
161 // to populate the map with data stored in shared preferences.
Andy Huang1f8f4dd2012-10-25 21:35:35 -0700162 // FIXME: values in this map are never read.
Mindy Pereira82cc5662012-01-09 17:29:30 -0800163 private static ConcurrentHashMap<Integer, Long> sRequestMessageIdMap = null;
164 // Key used to store the above map
165 private static final String CACHED_MESSAGE_REQUEST_IDS_KEY = "cache-message-request-ids";
Mindy Pereira6349a042012-01-04 11:25:01 -0800166 /**
167 * Notifies the {@code Activity} that the caller is an Email
168 * {@code Activity}, so that the back behavior may be modified accordingly.
169 *
170 * @see #onAppUpPressed
171 */
Paul Westbrookdaecb4b2012-05-31 10:21:26 -0700172 public static final String EXTRA_FROM_EMAIL_TASK = "fromemail";
Mindy Pereira6349a042012-01-04 11:25:01 -0800173
Mindy Pereirae011b1d2012-06-18 13:45:26 -0700174 public static final String EXTRA_ATTACHMENTS = "attachments";
Paul Westbrookf97588b2012-03-20 11:11:37 -0700175
Mindy Pereira3ce64e72012-01-13 14:29:45 -0800176 // If this is a reply/forward then this extra will hold the original message
Mindy Pereira36bbcae2012-04-25 09:27:04 -0700177 private static final String EXTRA_IN_REFERENCE_TO_MESSAGE = "in-reference-to-message";
Mindy Pereirab18e5a92012-07-10 11:47:21 -0700178 // If this is a reply/forward then this extra will hold a uri we must query
179 // to get the original message.
180 protected static final String EXTRA_IN_REFERENCE_TO_MESSAGE_URI = "in-reference-to-message-uri";
Mark Wei434f2942012-08-24 11:54:02 -0700181 // If this is an action to edit an existing draft message, this extra will hold the
Paul Westbrookbb87b7f2012-03-20 16:20:07 -0700182 // draft message
183 private static final String ORIGINAL_DRAFT_MESSAGE = "original-draft-message";
Mindy Pereira4a27ea92012-01-05 15:55:25 -0800184 private static final String END_TOKEN = ", ";
Paul Westbrookb334c902012-06-25 11:42:46 -0700185 private static final String LOG_TAG = LogTag.getLogTag();
Mindy Pereira013194c2012-01-06 15:09:33 -0800186 // Request numbers for activities we start
187 private static final int RESULT_PICK_ATTACHMENT = 1;
188 private static final int RESULT_CREATE_ACCOUNT = 2;
Mindy Pereira8eca57a2012-03-20 16:42:34 -0700189 // TODO(mindyp) set mime-type for auto send?
Mindy Pereirae011b1d2012-06-18 13:45:26 -0700190 public static final String AUTO_SEND_ACTION = "com.android.mail.action.AUTO_SEND";
Mindy Pereira8eca57a2012-03-20 16:42:34 -0700191
Mindy Pereirae8f94dc2012-04-16 11:56:21 -0700192 private static final String EXTRA_SELECTED_REPLY_FROM_ACCOUNT = "replyFromAccount";
193 private static final String EXTRA_REQUEST_ID = "requestId";
194 private static final String EXTRA_FOCUS_SELECTION_START = "focusSelectionStart";
195 private static final String EXTRA_FOCUS_SELECTION_END = null;
196 private static final String EXTRA_MESSAGE = "extraMessage";
Mindy Pereira96a7f7a2012-07-09 16:51:06 -0700197 private static final int REFERENCE_MESSAGE_LOADER = 0;
Mindy Pereirab199d172012-08-13 11:04:03 -0700198 private static final int LOADER_ACCOUNT_CURSOR = 1;
Mindy Pereira47d0e652012-07-23 09:45:07 -0700199 private static final String EXTRA_SELECTED_ACCOUNT = "selectedAccount";
Mindy Pereirab199d172012-08-13 11:04:03 -0700200 private static final String TAG_WAIT = "wait-fragment";
Mindy Pereira2db7d4a2012-08-15 11:00:02 -0700201 private static final String MIME_TYPE_PHOTO = "image/*";
202 private static final String MIME_TYPE_VIDEO = "video/*";
Mindy Pereira8e9305e2011-12-13 14:25:04 -0800203
Mindy Pereira82cc5662012-01-09 17:29:30 -0800204 /**
205 * A single thread for running tasks in the background.
206 */
207 private Handler mSendSaveTaskHandler = null;
Mindy Pereirac17d0732011-12-29 10:46:19 -0800208 private RecipientEditTextView mTo;
209 private RecipientEditTextView mCc;
210 private RecipientEditTextView mBcc;
Mindy Pereira8e9305e2011-12-13 14:25:04 -0800211 private Button mCcBccButton;
212 private CcBccView mCcBccView;
Mindy Pereira7b56a612011-12-14 12:32:28 -0800213 private AttachmentsView mAttachmentsView;
Mindy Pereira33fe9082012-01-09 16:24:30 -0800214 private Account mAccount;
Mindy Pereira92551d02012-04-05 11:31:12 -0700215 private ReplyFromAccount mReplyFromAccount;
Mindy Pereira181df782012-03-01 13:32:44 -0800216 private Settings mCachedSettings;
Mindy Pereira82cc5662012-01-09 17:29:30 -0800217 private Rfc822Validator mValidator;
Mindy Pereira6349a042012-01-04 11:25:01 -0800218 private TextView mSubject;
219
Mindy Pereira326c6602012-01-04 15:32:42 -0800220 private ComposeModeAdapter mComposeModeAdapter;
221 private int mComposeMode = -1;
Mindy Pereira46ce0b12012-01-05 10:32:15 -0800222 private boolean mForward;
223 private String mRecipient;
Mindy Pereira46ce0b12012-01-05 10:32:15 -0800224 private QuotedTextView mQuotedTextView;
Mindy Pereira433b1982012-04-03 11:53:07 -0700225 private EditText mBodyView;
Mindy Pereira1a95a572012-01-05 12:21:29 -0800226 private View mFromStatic;
Mindy Pereira2eb17322012-03-07 10:07:34 -0800227 private TextView mFromStaticText;
Mindy Pereiraeaea9f12012-01-10 15:05:27 -0800228 private View mFromSpinnerWrapper;
Mindy Pereira1883b342012-06-20 08:34:56 -0700229 @VisibleForTesting
230 protected FromAddressSpinner mFromSpinner;
Mindy Pereira013194c2012-01-06 15:09:33 -0800231 private boolean mAddingAttachment;
Mindy Pereiraeaea9f12012-01-10 15:05:27 -0800232 private boolean mAttachmentsChanged;
Mindy Pereira82cc5662012-01-09 17:29:30 -0800233 private boolean mTextChanged;
234 private boolean mReplyFromChanged;
235 private MenuItem mSave;
236 private MenuItem mSend;
Mindy Pereira82cc5662012-01-09 17:29:30 -0800237 private AlertDialog mRecipientErrorDialog;
Mindy Pereiraeaea9f12012-01-10 15:05:27 -0800238 private AlertDialog mSendConfirmDialog;
Mindy Pereirab3112a22012-06-20 12:10:03 -0700239 @VisibleForTesting
240 protected Message mRefMessage;
Mindy Pereira7ed1c112012-01-18 10:59:25 -0800241 private long mDraftId = UIProvider.INVALID_MESSAGE_ID;
242 private Message mDraft;
mindyp44a63392012-11-05 12:05:16 -0800243 private ReplyFromAccount mDraftAccount;
Mindy Pereira7ed1c112012-01-18 10:59:25 -0800244 private Object mDraftLock = new Object();
mindyp93b079b2012-08-29 16:32:15 -0700245 private View mPhotoAttachmentsButton;
246 private View mVideoAttachmentsButton;
Mindy Pereira3ce64e72012-01-13 14:29:45 -0800247
Mindy Pereira326c6602012-01-04 15:32:42 -0800248 /**
Paul Westbrookdaecb4b2012-05-31 10:21:26 -0700249 * Boolean indicating whether ComposeActivity was launched from a Gmail controlled view.
250 */
251 private boolean mLaunchedFromEmail = false;
Mindy Pereiracbfb75a2012-06-25 14:52:23 -0700252 private RecipientTextWatcher mToListener;
253 private RecipientTextWatcher mCcListener;
254 private RecipientTextWatcher mBccListener;
Mindy Pereirab18e5a92012-07-10 11:47:21 -0700255 private Uri mRefMessageUri;
Mindy Pereirab199d172012-08-13 11:04:03 -0700256 private Bundle mSavedInstanceState;
Paul Westbrookdaecb4b2012-05-31 10:21:26 -0700257
258
259 /**
Mindy Pereira326c6602012-01-04 15:32:42 -0800260 * Can be called from a non-UI thread.
261 */
Mindy Pereira3ce64e72012-01-13 14:29:45 -0800262 public static void editDraft(Context launcher, Account account, Message message) {
Paul Westbrookbb87b7f2012-03-20 16:20:07 -0700263 launch(launcher, account, message, EDIT_DRAFT);
Mindy Pereira326c6602012-01-04 15:32:42 -0800264 }
265
Mindy Pereira6349a042012-01-04 11:25:01 -0800266 /**
267 * Can be called from a non-UI thread.
268 */
Mindy Pereira33fe9082012-01-09 16:24:30 -0800269 public static void compose(Context launcher, Account account) {
Mindy Pereira6349a042012-01-04 11:25:01 -0800270 launch(launcher, account, null, COMPOSE);
271 }
272
273 /**
274 * Can be called from a non-UI thread.
275 */
Mindy Pereira3ce64e72012-01-13 14:29:45 -0800276 public static void reply(Context launcher, Account account, Message message) {
277 launch(launcher, account, message, REPLY);
Mindy Pereira6349a042012-01-04 11:25:01 -0800278 }
279
280 /**
281 * Can be called from a non-UI thread.
282 */
Mindy Pereira3ce64e72012-01-13 14:29:45 -0800283 public static void replyAll(Context launcher, Account account, Message message) {
284 launch(launcher, account, message, REPLY_ALL);
Mindy Pereira6349a042012-01-04 11:25:01 -0800285 }
286
287 /**
288 * Can be called from a non-UI thread.
289 */
Mindy Pereira3ce64e72012-01-13 14:29:45 -0800290 public static void forward(Context launcher, Account account, Message message) {
291 launch(launcher, account, message, FORWARD);
Mindy Pereira6349a042012-01-04 11:25:01 -0800292 }
293
Mindy Pereira3ce64e72012-01-13 14:29:45 -0800294 private static void launch(Context launcher, Account account, Message message, int action) {
Mindy Pereira6349a042012-01-04 11:25:01 -0800295 Intent intent = new Intent(launcher, ComposeActivity.class);
296 intent.putExtra(EXTRA_FROM_EMAIL_TASK, true);
297 intent.putExtra(EXTRA_ACTION, action);
298 intent.putExtra(Utils.EXTRA_ACCOUNT, account);
Paul Westbrookbb87b7f2012-03-20 16:20:07 -0700299 if (action == EDIT_DRAFT) {
300 intent.putExtra(ORIGINAL_DRAFT_MESSAGE, message);
301 } else {
302 intent.putExtra(EXTRA_IN_REFERENCE_TO_MESSAGE, message);
303 }
Mindy Pereira6349a042012-01-04 11:25:01 -0800304 launcher.startActivity(intent);
305 }
Mindy Pereira8e9305e2011-12-13 14:25:04 -0800306
307 @Override
308 public void onCreate(Bundle savedInstanceState) {
309 super.onCreate(savedInstanceState);
Mindy Pereira3528d362012-01-05 14:39:44 -0800310 setContentView(R.layout.compose);
Mindy Pereirab199d172012-08-13 11:04:03 -0700311 mSavedInstanceState = savedInstanceState;
312 checkValidAccounts();
313 }
314
315 private void finishCreate() {
316 Bundle savedInstanceState = mSavedInstanceState;
Mindy Pereira3528d362012-01-05 14:39:44 -0800317 findViews();
Mindy Pereira818143e2012-01-11 13:59:49 -0800318 Intent intent = getIntent();
Mindy Pereirae8f94dc2012-04-16 11:56:21 -0700319 Message message;
Mark Wei62066e42012-09-13 12:07:02 -0700320 ArrayList<AttachmentPreview> previews;
Mindy Pereira71c9e562012-05-17 11:01:02 -0700321 boolean showQuotedText = false;
Mindy Pereirae8f94dc2012-04-16 11:56:21 -0700322 int action;
Mindy Pereira47d0e652012-07-23 09:45:07 -0700323 // Check for any of the possibly supplied accounts.;
324 Account account = null;
Mindy Pereiraf7fc6c32012-06-19 15:18:33 -0700325 if (hadSavedInstanceStateMessage(savedInstanceState)) {
Mindy Pereirae8f94dc2012-04-16 11:56:21 -0700326 action = savedInstanceState.getInt(EXTRA_ACTION, COMPOSE);
327 account = savedInstanceState.getParcelable(Utils.EXTRA_ACCOUNT);
328 message = (Message) savedInstanceState.getParcelable(EXTRA_MESSAGE);
Mark Wei62066e42012-09-13 12:07:02 -0700329
330 previews = savedInstanceState.getParcelableArrayList(EXTRA_ATTACHMENT_PREVIEWS);
Mindy Pereirae8f94dc2012-04-16 11:56:21 -0700331 mRefMessage = (Message) savedInstanceState.getParcelable(EXTRA_IN_REFERENCE_TO_MESSAGE);
332 } else {
Mindy Pereira47d0e652012-07-23 09:45:07 -0700333 account = obtainAccount(intent);
Mindy Pereirae8f94dc2012-04-16 11:56:21 -0700334 action = intent.getIntExtra(EXTRA_ACTION, COMPOSE);
335 // Initialize the message from the message in the intent
336 message = (Message) intent.getParcelableExtra(ORIGINAL_DRAFT_MESSAGE);
Mark Wei62066e42012-09-13 12:07:02 -0700337 previews = intent.getParcelableArrayListExtra(EXTRA_ATTACHMENT_PREVIEWS);
Mindy Pereirae8f94dc2012-04-16 11:56:21 -0700338 mRefMessage = (Message) intent.getParcelableExtra(EXTRA_IN_REFERENCE_TO_MESSAGE);
Mindy Pereirab18e5a92012-07-10 11:47:21 -0700339 mRefMessageUri = (Uri) intent.getParcelableExtra(EXTRA_IN_REFERENCE_TO_MESSAGE_URI);
Mindy Pereirae8f94dc2012-04-16 11:56:21 -0700340 }
Mark Wei62066e42012-09-13 12:07:02 -0700341 mAttachmentsView.setAttachmentPreviews(previews);
Paul Westbrook92227f62012-03-20 10:32:51 -0700342
343 setAccount(account);
Mindy Pereira818143e2012-01-11 13:59:49 -0800344 if (mAccount == null) {
345 return;
346 }
Mindy Pereirae8f94dc2012-04-16 11:56:21 -0700347
Paul Westbrookdaecb4b2012-05-31 10:21:26 -0700348 if (intent.getBooleanExtra(EXTRA_FROM_EMAIL_TASK, false)) {
349 mLaunchedFromEmail = true;
350 } else if (Intent.ACTION_SEND.equals(intent.getAction())) {
351 final Uri dataUri = intent.getData();
352 if (dataUri != null) {
353 final String dataScheme = intent.getData().getScheme();
354 final String accountScheme = mAccount.composeIntentUri.getScheme();
355 mLaunchedFromEmail = TextUtils.equals(dataScheme, accountScheme);
356 }
357 }
358
Mindy Pereira96a7f7a2012-07-09 16:51:06 -0700359 if (mRefMessageUri != null) {
360 // We have a referenced message that we must look up.
361 getLoaderManager().initLoader(REFERENCE_MESSAGE_LOADER, null, this);
362 return;
363 } else if (message != null && action != EDIT_DRAFT) {
Mindy Pereirae8f94dc2012-04-16 11:56:21 -0700364 initFromDraftMessage(message);
365 initQuotedTextFromRefMessage(mRefMessage, action);
Mindy Pereiraa34c9a02012-04-17 14:10:53 -0700366 showCcBcc(savedInstanceState);
Mindy Pereira71c9e562012-05-17 11:01:02 -0700367 showQuotedText = message.appendRefMessageContent;
Paul Westbrookbb87b7f2012-03-20 16:20:07 -0700368 } else if (action == EDIT_DRAFT) {
Mindy Pereirae8f94dc2012-04-16 11:56:21 -0700369 initFromDraftMessage(message);
Scott Kennedy8960f0a2012-11-07 15:35:50 -0800370 boolean showBcc = !TextUtils.isEmpty(message.getBcc());
371 boolean showCc = showBcc || !TextUtils.isEmpty(message.getCc());
Mindy Pereiraef388302012-06-18 19:07:44 -0700372 mCcBccView.show(false, showCc, showBcc);
Paul Westbrookbb87b7f2012-03-20 16:20:07 -0700373 // Update the action to the draft type of the previous draft
374 switch (message.draftType) {
375 case UIProvider.DraftType.REPLY:
376 action = REPLY;
377 break;
378 case UIProvider.DraftType.REPLY_ALL:
379 action = REPLY_ALL;
380 break;
381 case UIProvider.DraftType.FORWARD:
382 action = FORWARD;
383 break;
384 case UIProvider.DraftType.COMPOSE:
385 default:
386 action = COMPOSE;
387 break;
388 }
Mindy Pereirae8f94dc2012-04-16 11:56:21 -0700389 initQuotedTextFromRefMessage(mRefMessage, action);
Mindy Pereira71c9e562012-05-17 11:01:02 -0700390 showQuotedText = message.appendRefMessageContent;
Mindy Pereirae8f94dc2012-04-16 11:56:21 -0700391 } else if ((action == REPLY || action == REPLY_ALL || action == FORWARD)) {
392 if (mRefMessage != null) {
393 initFromRefMessage(action, mAccount.name);
Mindy Pereira71c9e562012-05-17 11:01:02 -0700394 showQuotedText = true;
Mindy Pereirae8f94dc2012-04-16 11:56:21 -0700395 }
Mindy Pereira8eca57a2012-03-20 16:42:34 -0700396 } else {
397 initFromExtras(intent);
Paul Westbrookbb87b7f2012-03-20 16:20:07 -0700398 }
Mindy Pereira96a7f7a2012-07-09 16:51:06 -0700399 finishSetup(action, intent, savedInstanceState, showQuotedText);
400 }
Paul Westbrookbb87b7f2012-03-20 16:20:07 -0700401
Mindy Pereirab199d172012-08-13 11:04:03 -0700402 private void checkValidAccounts() {
Paul Westbrookfaa742f2012-11-01 09:50:16 -0700403 final Account[] allAccounts = AccountUtils.getAccounts(this);
404 if (allAccounts == null || allAccounts.length == 0) {
Mindy Pereirab199d172012-08-13 11:04:03 -0700405 final Intent noAccountIntent = MailAppProvider.getNoAccountIntent(this);
406 if (noAccountIntent != null) {
Paul Westbrookfaa742f2012-11-01 09:50:16 -0700407 mAccounts = null;
Mindy Pereirab199d172012-08-13 11:04:03 -0700408 startActivityForResult(noAccountIntent, RESULT_CREATE_ACCOUNT);
409 }
410 } else {
mindyp26d4d2d2012-09-18 17:30:32 -0700411 // If none of the accounts are syncing, setup a watcher.
Mindy Pereirab199d172012-08-13 11:04:03 -0700412 boolean anySyncing = false;
Paul Westbrookfaa742f2012-11-01 09:50:16 -0700413 for (Account a : allAccounts) {
Paul Westbrookdfa1dec2012-09-26 16:27:28 -0700414 if (a.isAccountReady()) {
Mindy Pereirab199d172012-08-13 11:04:03 -0700415 anySyncing = true;
416 break;
417 }
418 }
419 if (!anySyncing) {
420 // There are accounts, but none are sync'd, which is just like having no accounts.
421 mAccounts = null;
422 getLoaderManager().initLoader(LOADER_ACCOUNT_CURSOR, null, this);
423 return;
424 }
Paul Westbrookfaa742f2012-11-01 09:50:16 -0700425 mAccounts = AccountUtils.getSyncingAccounts(this);
Mindy Pereirab199d172012-08-13 11:04:03 -0700426 finishCreate();
427 }
428 }
429
Mindy Pereira47d0e652012-07-23 09:45:07 -0700430 private Account obtainAccount(Intent intent) {
431 Account account = null;
432 Object accountExtra = null;
433 if (intent != null && intent.getExtras() != null) {
434 accountExtra = intent.getExtras().get(Utils.EXTRA_ACCOUNT);
435 if (accountExtra instanceof Account) {
436 return (Account) accountExtra;
mindyp7ae042e2012-08-27 13:27:37 -0700437 } else if (accountExtra instanceof String) {
438 // This is the Account attached to the widget compose intent.
439 account = Account.newinstance((String)accountExtra);
440 if (account != null) {
441 return account;
442 }
Mindy Pereira47d0e652012-07-23 09:45:07 -0700443 }
444 accountExtra = intent.getStringExtra(EXTRA_SELECTED_ACCOUNT);
445 }
446 if (account == null) {
mindyp06174462012-10-12 09:11:27 -0700447 MailAppProvider provider = MailAppProvider.getInstance();
448 String lastAccountUri = provider.getLastSentFromAccount();
449 if (TextUtils.isEmpty(lastAccountUri)) {
450 lastAccountUri = provider.getLastViewedAccount();
451 }
Mindy Pereira47d0e652012-07-23 09:45:07 -0700452 if (!TextUtils.isEmpty(lastAccountUri)) {
453 accountExtra = Uri.parse(lastAccountUri);
454 }
455 }
Mindy Pereirab199d172012-08-13 11:04:03 -0700456 if (mAccounts != null && mAccounts.length > 0) {
Mindy Pereira47d0e652012-07-23 09:45:07 -0700457 if (accountExtra instanceof String && !TextUtils.isEmpty((String) accountExtra)) {
458 // For backwards compatibility, we need to check account
459 // names.
Mindy Pereirab199d172012-08-13 11:04:03 -0700460 for (Account a : mAccounts) {
Mindy Pereira47d0e652012-07-23 09:45:07 -0700461 if (a.name.equals(accountExtra)) {
462 account = a;
463 }
464 }
465 } else if (accountExtra instanceof Uri) {
466 // The uri of the last viewed account is what is stored in
467 // the current code base.
Mindy Pereirab199d172012-08-13 11:04:03 -0700468 for (Account a : mAccounts) {
Mindy Pereira47d0e652012-07-23 09:45:07 -0700469 if (a.uri.equals(accountExtra)) {
470 account = a;
471 }
472 }
Mindy Pereirab199d172012-08-13 11:04:03 -0700473 }
474 if (account == null) {
475 account = mAccounts[0];
Mindy Pereira47d0e652012-07-23 09:45:07 -0700476 }
477 }
478 return account;
479 }
480
Mindy Pereira96a7f7a2012-07-09 16:51:06 -0700481 private void finishSetup(int action, Intent intent, Bundle savedInstanceState,
482 boolean showQuotedText) {
mindyp34a3c562012-11-06 15:12:15 -0800483 setFocus(action);
Paul Westbrookbb87b7f2012-03-20 16:20:07 -0700484 if (action == COMPOSE) {
Mindy Pereiraeaea9f12012-01-10 15:05:27 -0800485 mQuotedTextView.setVisibility(View.GONE);
Mindy Pereira46ce0b12012-01-05 10:32:15 -0800486 }
Mindy Pereira818143e2012-01-11 13:59:49 -0800487 initRecipients();
Mindy Pereiraf7fc6c32012-06-19 15:18:33 -0700488 // Don't bother with the intent if we have procured a message from the
489 // intent already.
490 if (!hadSavedInstanceStateMessage(savedInstanceState)) {
491 initAttachmentsFromIntent(intent);
492 }
Mindy Pereira1a95a572012-01-05 12:21:29 -0800493 initActionBar(action);
Mindy Pereirae8f94dc2012-04-16 11:56:21 -0700494 initFromSpinner(savedInstanceState != null ? savedInstanceState : intent.getExtras(),
495 action);
mindypd4a48662012-11-08 17:13:49 -0800496
497 // If this is a draft message, the draft account is whatever account was
498 // used to open the draft message in Compose.
499 if (mDraft != null) {
500 mDraftAccount = mReplyFromAccount;
501 }
502
Mindy Pereira75f66632012-01-11 11:42:02 -0800503 initChangeListeners();
Mindy Pereira326689d2012-05-17 10:14:14 -0700504 updateHideOrShowCcBcc();
Mindy Pereira71c9e562012-05-17 11:01:02 -0700505 updateHideOrShowQuotedText(showQuotedText);
506 }
507
Mindy Pereiraf7fc6c32012-06-19 15:18:33 -0700508 private boolean hadSavedInstanceStateMessage(Bundle savedInstanceState) {
509 return savedInstanceState != null && savedInstanceState.containsKey(EXTRA_MESSAGE);
510 }
511
Mindy Pereira71c9e562012-05-17 11:01:02 -0700512 private void updateHideOrShowQuotedText(boolean showQuotedText) {
513 mQuotedTextView.updateCheckedState(showQuotedText);
mindyp40882432012-09-06 11:07:40 -0700514 mQuotedTextView.setUpperDividerVisible(mAttachmentsView.getAttachments().size() > 0);
Mindy Pereira433b1982012-04-03 11:53:07 -0700515 }
516
517 private void setFocus(int action) {
518 if (action == EDIT_DRAFT) {
519 int type = mDraft.draftType;
520 switch (type) {
521 case UIProvider.DraftType.COMPOSE:
522 case UIProvider.DraftType.FORWARD:
523 action = COMPOSE;
524 break;
525 case UIProvider.DraftType.REPLY:
526 case UIProvider.DraftType.REPLY_ALL:
527 default:
528 action = REPLY;
529 break;
530 }
531 }
532 switch (action) {
533 case FORWARD:
534 case COMPOSE:
535 mTo.requestFocus();
536 break;
537 case REPLY:
538 case REPLY_ALL:
539 default:
540 focusBody();
541 break;
542 }
543 }
544
545 /**
546 * Focus the body of the message.
547 */
548 public void focusBody() {
549 mBodyView.requestFocus();
550 int length = mBodyView.getText().length();
551
552 int signatureStartPos = getSignatureStartPosition(
553 mSignature, mBodyView.getText().toString());
554 if (signatureStartPos > -1) {
555 // In case the user deleted the newlines...
556 mBodyView.setSelection(signatureStartPos);
mindyp8743cfc2012-09-18 13:29:08 -0700557 } else if (length >= 0) {
Mindy Pereira433b1982012-04-03 11:53:07 -0700558 // Move cursor to the end.
559 mBodyView.setSelection(length);
560 }
Mindy Pereira1a95a572012-01-05 12:21:29 -0800561 }
562
563 @Override
564 protected void onResume() {
565 super.onResume();
566 // Update the from spinner as other accounts
567 // may now be available.
Mindy Pereira818143e2012-01-11 13:59:49 -0800568 if (mFromSpinner != null && mAccount != null) {
Mindy Pereirab199d172012-08-13 11:04:03 -0700569 mFromSpinner.asyncInitFromSpinner(mComposeMode, mAccount, mAccounts);
Mindy Pereira818143e2012-01-11 13:59:49 -0800570 }
Mindy Pereira1a95a572012-01-05 12:21:29 -0800571 }
572
Mindy Pereiraeaea9f12012-01-10 15:05:27 -0800573 @Override
574 protected void onPause() {
575 super.onPause();
576
577 if (mSendConfirmDialog != null) {
578 mSendConfirmDialog.dismiss();
579 }
580 if (mRecipientErrorDialog != null) {
581 mRecipientErrorDialog.dismiss();
582 }
Mindy Pereiraa2148332012-07-02 13:54:14 -0700583 // When the user exits the compose view, see if this draft needs saving.
Yorke Lee3d7048e2012-09-19 14:19:25 -0700584 // Don't save unnecessary drafts if we are only changing the orientation.
585 if (!isChangingConfigurations()) {
Mindy Pereiraa2148332012-07-02 13:54:14 -0700586 saveIfNeeded();
587 }
Mindy Pereiraeaea9f12012-01-10 15:05:27 -0800588 }
589
590 @Override
591 protected final void onActivityResult(int request, int result, Intent data) {
Mindy Pereirab199d172012-08-13 11:04:03 -0700592 if (request == RESULT_PICK_ATTACHMENT && result == RESULT_OK) {
Mindy Pereiraeaea9f12012-01-10 15:05:27 -0800593 addAttachmentAndUpdateView(data);
Mindy Pereirab199d172012-08-13 11:04:03 -0700594 mAddingAttachment = false;
595 } else if (request == RESULT_CREATE_ACCOUNT) {
596 // We were waiting for the user to create an account
597 if (result != RESULT_OK) {
598 finish();
599 } else {
600 // Watch for accounts to show up!
601 // restart the loader to get the updated list of accounts
602 getLoaderManager().initLoader(LOADER_ACCOUNT_CURSOR, null, this);
603 showWaitFragment(null);
604 }
Mindy Pereiraeaea9f12012-01-10 15:05:27 -0800605 }
606 }
607
608 @Override
Mindy Pereirae8f94dc2012-04-16 11:56:21 -0700609 public final void onRestoreInstanceState(Bundle savedInstanceState) {
610 super.onRestoreInstanceState(savedInstanceState);
611 if (savedInstanceState != null) {
612 if (savedInstanceState.containsKey(EXTRA_FOCUS_SELECTION_START)) {
613 int selectionStart = savedInstanceState.getInt(EXTRA_FOCUS_SELECTION_START);
614 int selectionEnd = savedInstanceState.getInt(EXTRA_FOCUS_SELECTION_END);
615 // There should be a focus and it should be an EditText since we
616 // only save these extras if these conditions are true.
617 EditText focusEditText = (EditText) getCurrentFocus();
618 final int length = focusEditText.getText().length();
619 if (selectionStart < length && selectionEnd < length) {
620 focusEditText.setSelection(selectionStart, selectionEnd);
621 }
622 }
623 }
624 }
625
626 @Override
Mindy Pereiraeaea9f12012-01-10 15:05:27 -0800627 public final void onSaveInstanceState(Bundle state) {
628 super.onSaveInstanceState(state);
Mindy Pereirab199d172012-08-13 11:04:03 -0700629 // We have no accounts so there is nothing to compose, and therefore, nothing to save.
630 if (mAccounts == null || mAccounts.length == 0) {
631 return;
632 }
Mindy Pereirae8f94dc2012-04-16 11:56:21 -0700633 // The framework is happy to save and restore the selection but only if it also saves and
634 // restores the contents of the edit text. That's a lot of text to put in a bundle so we do
635 // this manually.
636 View focus = getCurrentFocus();
637 if (focus != null && focus instanceof EditText) {
638 EditText focusEditText = (EditText) focus;
639 state.putInt(EXTRA_FOCUS_SELECTION_START, focusEditText.getSelectionStart());
640 state.putInt(EXTRA_FOCUS_SELECTION_END, focusEditText.getSelectionEnd());
641 }
Paul Westbrook6273e962012-04-23 10:44:15 -0700642
643 final List<ReplyFromAccount> replyFromAccounts = mFromSpinner.getReplyFromAccounts();
Paul Westbrook151f1ad2012-04-24 09:13:00 -0700644 final int selectedPos = mFromSpinner.getSelectedItemPosition();
Mindy Pereirad90f7ac2012-06-27 10:31:06 -0700645 final ReplyFromAccount selectedReplyFromAccount = (replyFromAccounts != null
646 && replyFromAccounts.size() > 0 && replyFromAccounts.size() > selectedPos) ?
647 replyFromAccounts.get(selectedPos) : null;
Mindy Pereirae8f94dc2012-04-16 11:56:21 -0700648 if (selectedReplyFromAccount != null) {
649 state.putString(EXTRA_SELECTED_REPLY_FROM_ACCOUNT, selectedReplyFromAccount.serialize()
650 .toString());
651 state.putParcelable(Utils.EXTRA_ACCOUNT, selectedReplyFromAccount.account);
652 } else {
653 state.putParcelable(Utils.EXTRA_ACCOUNT, mAccount);
654 }
Mindy Pereiraeaea9f12012-01-10 15:05:27 -0800655
Mindy Pereirae8f94dc2012-04-16 11:56:21 -0700656 if (mDraftId == UIProvider.INVALID_MESSAGE_ID && mRequestId !=0) {
657 // We don't have a draft id, and we have a request id,
658 // save the request id.
659 state.putInt(EXTRA_REQUEST_ID, mRequestId);
660 }
661
662 // We want to restore the current mode after a pause
663 // or rotation.
664 int mode = getMode();
665 state.putInt(EXTRA_ACTION, mode);
666
Andy Huang1f8f4dd2012-10-25 21:35:35 -0700667 final Message message;
668 // only synthesize a message if a real one isn't already present as a draft
669 if (mDraft != null) {
670 message = mDraft;
671 updateMessage(message, selectedReplyFromAccount, mode);
672 } else {
673 message = createMessage(selectedReplyFromAccount, mode);
674 }
Mindy Pereirae8f94dc2012-04-16 11:56:21 -0700675 state.putParcelable(EXTRA_MESSAGE, message);
676
677 if (mRefMessage != null) {
678 state.putParcelable(EXTRA_IN_REFERENCE_TO_MESSAGE, mRefMessage);
679 }
Mindy Pereira326689d2012-05-17 10:14:14 -0700680 state.putBoolean(EXTRA_SHOW_CC, mCcBccView.isCcVisible());
681 state.putBoolean(EXTRA_SHOW_BCC, mCcBccView.isBccVisible());
Mark Wei62066e42012-09-13 12:07:02 -0700682
683 state.putParcelableArrayList(
684 EXTRA_ATTACHMENT_PREVIEWS, mAttachmentsView.getAttachmentPreviews());
Mindy Pereirae8f94dc2012-04-16 11:56:21 -0700685 }
686
687 private int getMode() {
688 int mode = ComposeActivity.COMPOSE;
689 ActionBar actionBar = getActionBar();
Mindy Pereirae011b1d2012-06-18 13:45:26 -0700690 if (actionBar != null
691 && actionBar.getNavigationMode() == ActionBar.NAVIGATION_MODE_LIST) {
Mindy Pereirae8f94dc2012-04-16 11:56:21 -0700692 mode = actionBar.getSelectedNavigationIndex();
693 }
694 return mode;
695 }
696
697 private Message createMessage(ReplyFromAccount selectedReplyFromAccount, int mode) {
698 Message message = new Message();
699 message.id = UIProvider.INVALID_MESSAGE_ID;
Andy Huangd47877e2012-08-09 19:31:24 -0700700 message.serverId = null;
Mindy Pereirae8f94dc2012-04-16 11:56:21 -0700701 message.uri = null;
702 message.conversationUri = null;
703 message.subject = mSubject.getText().toString();
704 message.snippet = null;
Scott Kennedy8960f0a2012-11-07 15:35:50 -0800705 message.setTo(formatSenders(mTo.getText().toString()));
706 message.setCc(formatSenders(mCc.getText().toString()));
707 message.setBcc(formatSenders(mBcc.getText().toString()));
708 message.setReplyTo(null);
Mindy Pereirae8f94dc2012-04-16 11:56:21 -0700709 message.dateReceivedMs = 0;
mindyped9c2f02012-10-12 10:02:08 -0700710 String htmlBody = Html.toHtml(new SpannableString(mBodyView.getText().toString()));
Mindy Pereirae8f94dc2012-04-16 11:56:21 -0700711 StringBuilder fullBody = new StringBuilder(htmlBody);
712 message.bodyHtml = fullBody.toString();
713 message.bodyText = mBodyView.getText().toString();
714 message.embedsExternalResources = false;
715 message.refMessageId = mRefMessage != null ? mRefMessage.uri.toString() : null;
Mindy Pereirae8f94dc2012-04-16 11:56:21 -0700716 message.appendRefMessageContent = mQuotedTextView.getQuotedTextIfIncluded() != null;
717 ArrayList<Attachment> attachments = mAttachmentsView.getAttachments();
718 message.hasAttachments = attachments != null && attachments.size() > 0;
719 message.attachmentListUri = null;
720 message.messageFlags = 0;
721 message.saveUri = null;
722 message.sendUri = null;
723 message.alwaysShowImages = false;
724 message.attachmentsJson = Attachment.toJSONArray(attachments);
725 CharSequence quotedText = mQuotedTextView.getQuotedText();
726 message.quotedTextOffset = !TextUtils.isEmpty(quotedText) ? QuotedTextView
727 .getQuotedTextOffset(quotedText.toString()) : -1;
728 message.accountUri = null;
Andy Huang1f8f4dd2012-10-25 21:35:35 -0700729 updateMessage(message, selectedReplyFromAccount, mode);
Mindy Pereirae8f94dc2012-04-16 11:56:21 -0700730 return message;
Mindy Pereiraeaea9f12012-01-10 15:05:27 -0800731 }
732
Andy Huang1f8f4dd2012-10-25 21:35:35 -0700733 private void updateMessage(Message message, ReplyFromAccount selectedReplyFromAccount,
734 int mode) {
Scott Kennedy8960f0a2012-11-07 15:35:50 -0800735 message.setFrom(selectedReplyFromAccount != null ? selectedReplyFromAccount.address
736 : mAccount != null ? mAccount.name : null);
Andy Huang1f8f4dd2012-10-25 21:35:35 -0700737 message.draftType = getDraftType(mode);
Scott Kennedy8960f0a2012-11-07 15:35:50 -0800738 message.setTo(formatSenders(mTo.getText().toString()));
739 message.setCc(formatSenders(mCc.getText().toString()));
740 message.setBcc(formatSenders(mBcc.getText().toString()));
Andy Huang1f8f4dd2012-10-25 21:35:35 -0700741 }
742
Mindy Pereira3c911582012-08-09 16:59:09 -0700743 private String formatSenders(String string) {
744 if (!TextUtils.isEmpty(string) && string.charAt(string.length() - 1) == ',') {
745 return string.substring(0, string.length() - 1);
746 }
747 return string;
748 }
749
Mindy Pereira818143e2012-01-11 13:59:49 -0800750 @VisibleForTesting
751 void setAccount(Account account) {
Mindy Pereirabb5217e2012-04-17 11:08:29 -0700752 if (account == null) {
753 return;
754 }
Mindy Pereira23e9fde2012-03-20 15:08:24 -0700755 if (!account.equals(mAccount)) {
756 mAccount = account;
Paul Westbrookb1f573c2012-04-06 11:38:28 -0700757 mCachedSettings = mAccount.settings;
758 appendSignature();
Mindy Pereira23e9fde2012-03-20 15:08:24 -0700759 }
Mindy Pereirafa20c1a2012-07-23 13:00:02 -0700760 if (mAccount != null) {
761 MailActivity.setForegroundNdef(MailActivity.getMailtoNdef(mAccount.name));
762 }
Mindy Pereira818143e2012-01-11 13:59:49 -0800763 }
764
Mindy Pereirae8f94dc2012-04-16 11:56:21 -0700765 private void initFromSpinner(Bundle bundle, int action) {
Mindy Pereira9a42bb42012-04-18 15:21:33 -0700766 String accountString = null;
Mindy Pereirae8f94dc2012-04-16 11:56:21 -0700767 if (action == EDIT_DRAFT && mDraft.draftType == UIProvider.DraftType.COMPOSE) {
Mindy Pereira62de1b12012-04-06 12:17:56 -0700768 action = COMPOSE;
769 }
Mindy Pereirab199d172012-08-13 11:04:03 -0700770 mFromSpinner.asyncInitFromSpinner(action, mAccount, mAccounts);
Mindy Pereira9a42bb42012-04-18 15:21:33 -0700771 if (bundle != null) {
772 if (bundle.containsKey(EXTRA_SELECTED_REPLY_FROM_ACCOUNT)) {
773 mReplyFromAccount = ReplyFromAccount.deserialize(mAccount,
774 bundle.getString(EXTRA_SELECTED_REPLY_FROM_ACCOUNT));
775 } else if (bundle.containsKey(EXTRA_FROM_ACCOUNT_STRING)) {
776 accountString = bundle.getString(EXTRA_FROM_ACCOUNT_STRING);
777 mReplyFromAccount = mFromSpinner.getMatchingReplyFromAccount(accountString);
778 }
Mindy Pereirae8f94dc2012-04-16 11:56:21 -0700779 }
780 if (mReplyFromAccount == null) {
781 if (mDraft != null) {
782 mReplyFromAccount = getReplyFromAccountFromDraft(mAccount, mDraft);
783 } else if (mRefMessage != null) {
784 mReplyFromAccount = getReplyFromAccountForReply(mAccount, mRefMessage);
785 }
Mindy Pereira62de1b12012-04-06 12:17:56 -0700786 }
787 if (mReplyFromAccount == null) {
Andy Huang238aa472012-10-30 17:45:17 -0700788 mReplyFromAccount = getDefaultReplyFromAccount(mAccount);
Mindy Pereira62de1b12012-04-06 12:17:56 -0700789 }
Mindy Pereira9a42bb42012-04-18 15:21:33 -0700790
Mindy Pereira62de1b12012-04-06 12:17:56 -0700791 mFromSpinner.setCurrentAccount(mReplyFromAccount);
Mindy Pereira9a42bb42012-04-18 15:21:33 -0700792
Mindy Pereira62de1b12012-04-06 12:17:56 -0700793 if (mFromSpinner.getCount() > 1) {
Mindy Pereiraa83e7082012-03-30 08:53:11 -0700794 // If there is only 1 account, just show that account.
795 // Otherwise, give the user the ability to choose which account to
Mindy Pereira62de1b12012-04-06 12:17:56 -0700796 // send mail from / save drafts to.
797 mFromStatic.setVisibility(View.GONE);
Mindy Pereiraa83e7082012-03-30 08:53:11 -0700798 mFromStaticText.setText(mAccount.name);
Mindy Pereira62de1b12012-04-06 12:17:56 -0700799 mFromSpinnerWrapper.setVisibility(View.VISIBLE);
Mindy Pereiraa83e7082012-03-30 08:53:11 -0700800 } else {
801 mFromStatic.setVisibility(View.VISIBLE);
802 mFromStaticText.setText(mAccount.name);
803 mFromSpinnerWrapper.setVisibility(View.GONE);
Mindy Pereiraa83e7082012-03-30 08:53:11 -0700804 }
Mindy Pereira46ce0b12012-01-05 10:32:15 -0800805 }
806
Mindy Pereira62de1b12012-04-06 12:17:56 -0700807 private ReplyFromAccount getReplyFromAccountForReply(Account account, Message refMessage) {
808 if (refMessage.accountUri != null) {
809 // This must be from combined inbox.
810 List<ReplyFromAccount> replyFromAccounts = mFromSpinner.getReplyFromAccounts();
811 for (ReplyFromAccount from : replyFromAccounts) {
812 if (from.account.uri.equals(refMessage.accountUri)) {
813 return from;
814 }
815 }
816 return null;
817 } else {
818 return getReplyFromAccount(account, refMessage);
819 }
820 }
821
822 /**
823 * Given an account and which email address the message was sent to,
824 * return who the message should be sent from.
825 * @param account Account in which the message arrived.
826 * @param sentTo Email address to which the message was sent.
827 * @return the address from which to reply.
828 */
829 public ReplyFromAccount getReplyFromAccount(Account account, Message refMessage) {
830 // First see if we are supposed to use the default address or
831 // the address it was sentTo.
Mindy Pereira326689d2012-05-17 10:14:14 -0700832 if (mCachedSettings.forceReplyFromDefault) {
Mindy Pereira62de1b12012-04-06 12:17:56 -0700833 return getDefaultReplyFromAccount(account);
834 } else {
Mindy Pereira89bae572012-06-18 11:34:36 -0700835 // If we aren't explicitly told which account to look for, look at
Mindy Pereira62de1b12012-04-06 12:17:56 -0700836 // all the message recipients and find one that matches
837 // a custom from or account.
838 List<String> allRecipients = new ArrayList<String>();
mindyp5ed63112012-09-17 17:31:45 -0700839 allRecipients.addAll(Arrays.asList(refMessage.getToAddresses()));
840 allRecipients.addAll(Arrays.asList(refMessage.getCcAddresses()));
Mindy Pereira62de1b12012-04-06 12:17:56 -0700841 return getMatchingRecipient(account, allRecipients);
842 }
843 }
844
845 /**
846 * Compare all the recipients of an email to the current account and all
847 * custom addresses associated with that account. Return the match if there
848 * is one, or the default account if there isn't.
849 */
850 protected ReplyFromAccount getMatchingRecipient(Account account, List<String> sentTo) {
851 // Tokenize the list and place in a hashmap.
852 ReplyFromAccount matchingReplyFrom = null;
853 Rfc822Token[] tokens;
854 HashSet<String> recipientsMap = new HashSet<String>();
855 for (String address : sentTo) {
856 tokens = Rfc822Tokenizer.tokenize(address);
857 for (int i = 0; i < tokens.length; i++) {
858 recipientsMap.add(tokens[i].getAddress());
859 }
860 }
861
862 int matchingAddressCount = 0;
863 List<ReplyFromAccount> customFroms;
Andy Huang16174812012-08-16 16:40:35 -0700864 customFroms = account.getReplyFroms();
865 if (customFroms != null) {
866 for (ReplyFromAccount entry : customFroms) {
867 if (recipientsMap.contains(entry.address)) {
868 matchingReplyFrom = entry;
869 matchingAddressCount++;
Mindy Pereira62de1b12012-04-06 12:17:56 -0700870 }
871 }
Mindy Pereira62de1b12012-04-06 12:17:56 -0700872 }
873 if (matchingAddressCount > 1) {
874 matchingReplyFrom = getDefaultReplyFromAccount(account);
875 }
876 return matchingReplyFrom;
877 }
878
879 private ReplyFromAccount getDefaultReplyFromAccount(Account account) {
Andy Huang238aa472012-10-30 17:45:17 -0700880 for (ReplyFromAccount from : account.getReplyFroms()) {
Mindy Pereira62de1b12012-04-06 12:17:56 -0700881 if (from.isDefault) {
882 return from;
883 }
884 }
Mindy Pereiracd970dd2012-05-31 10:07:47 -0700885 return new ReplyFromAccount(account, account.uri, account.name, account.name, account.name,
886 true, false);
Mindy Pereira62de1b12012-04-06 12:17:56 -0700887 }
888
Mindy Pereirae8f94dc2012-04-16 11:56:21 -0700889 private ReplyFromAccount getReplyFromAccountFromDraft(Account account, Message msg) {
Scott Kennedy8960f0a2012-11-07 15:35:50 -0800890 String sender = msg.getFrom();
Mindy Pereira62de1b12012-04-06 12:17:56 -0700891 ReplyFromAccount replyFromAccount = null;
892 List<ReplyFromAccount> replyFromAccounts = mFromSpinner.getReplyFromAccounts();
893 if (TextUtils.equals(account.name, sender)) {
894 replyFromAccount = new ReplyFromAccount(mAccount, mAccount.uri, mAccount.name,
Mindy Pereiracd970dd2012-05-31 10:07:47 -0700895 mAccount.name, mAccount.name, true, false);
Mindy Pereira62de1b12012-04-06 12:17:56 -0700896 } else {
897 for (ReplyFromAccount fromAccount : replyFromAccounts) {
898 if (TextUtils.equals(fromAccount.name, sender)) {
899 replyFromAccount = fromAccount;
900 break;
901 }
902 }
903 }
904 return replyFromAccount;
905 }
906
Mindy Pereira46ce0b12012-01-05 10:32:15 -0800907 private void findViews() {
Mindy Pereirab199d172012-08-13 11:04:03 -0700908 findViewById(R.id.compose).setVisibility(View.VISIBLE);
Mindy Pereiraa26b54e2012-01-06 12:54:33 -0800909 mCcBccButton = (Button) findViewById(R.id.add_cc_bcc);
Mindy Pereira8e9305e2011-12-13 14:25:04 -0800910 if (mCcBccButton != null) {
911 mCcBccButton.setOnClickListener(this);
912 }
913 mCcBccView = (CcBccView) findViewById(R.id.cc_bcc_wrapper);
Mindy Pereira7b56a612011-12-14 12:32:28 -0800914 mAttachmentsView = (AttachmentsView)findViewById(R.id.attachments);
mindyp93b079b2012-08-29 16:32:15 -0700915 mPhotoAttachmentsButton = findViewById(R.id.add_photo_attachment);
mindypcd0b0b92012-08-23 14:33:17 -0700916 if (mPhotoAttachmentsButton != null) {
917 mPhotoAttachmentsButton.setOnClickListener(this);
918 }
mindyp93b079b2012-08-29 16:32:15 -0700919 mVideoAttachmentsButton = findViewById(R.id.add_video_attachment);
mindypcd0b0b92012-08-23 14:33:17 -0700920 if (mVideoAttachmentsButton != null) {
921 mVideoAttachmentsButton.setOnClickListener(this);
922 }
Mindy Pereira818143e2012-01-11 13:59:49 -0800923 mTo = (RecipientEditTextView) findViewById(R.id.to);
924 mCc = (RecipientEditTextView) findViewById(R.id.cc);
925 mBcc = (RecipientEditTextView) findViewById(R.id.bcc);
Mindy Pereira82cc5662012-01-09 17:29:30 -0800926 // TODO: add special chips text change watchers before adding
927 // this as a text changed watcher to the to, cc, bcc fields.
Mindy Pereira6349a042012-01-04 11:25:01 -0800928 mSubject = (TextView) findViewById(R.id.subject);
mindyp62d3ec72012-08-24 13:04:09 -0700929 mSubject.setOnEditorActionListener(this);
Mindy Pereira46ce0b12012-01-05 10:32:15 -0800930 mQuotedTextView = (QuotedTextView) findViewById(R.id.quoted_text_view);
931 mQuotedTextView.setRespondInlineListener(this);
Mindy Pereira433b1982012-04-03 11:53:07 -0700932 mBodyView = (EditText) findViewById(R.id.body);
Mindy Pereira1a95a572012-01-05 12:21:29 -0800933 mFromStatic = findViewById(R.id.static_from_content);
Mindy Pereira2eb17322012-03-07 10:07:34 -0800934 mFromStaticText = (TextView) findViewById(R.id.from_account_name);
Mindy Pereiraeaea9f12012-01-10 15:05:27 -0800935 mFromSpinnerWrapper = findViewById(R.id.spinner_from_content);
Mindy Pereira5a85e2b2012-01-11 09:53:32 -0800936 mFromSpinner = (FromAddressSpinner) findViewById(R.id.from_picker);
Mindy Pereira6349a042012-01-04 11:25:01 -0800937 }
938
mindyp62d3ec72012-08-24 13:04:09 -0700939 @Override
940 public boolean onEditorAction(TextView view, int action, KeyEvent keyEvent) {
941 if (action == EditorInfo.IME_ACTION_DONE) {
942 focusBody();
943 return true;
944 }
945 return false;
946 }
947
Mindy Pereirae011b1d2012-06-18 13:45:26 -0700948 protected TextView getBody() {
949 return mBodyView;
950 }
951
952 @VisibleForTesting
953 public Account getFromAccount() {
954 return mReplyFromAccount != null && mReplyFromAccount.account != null ?
955 mReplyFromAccount.account : mAccount;
956 }
957
Mindy Pereiracbfb75a2012-06-25 14:52:23 -0700958 private void clearChangeListeners() {
959 mSubject.removeTextChangedListener(this);
960 mBodyView.removeTextChangedListener(this);
961 mTo.removeTextChangedListener(mToListener);
962 mCc.removeTextChangedListener(mCcListener);
963 mBcc.removeTextChangedListener(mBccListener);
964 mFromSpinner.setOnAccountChangedListener(null);
965 mAttachmentsView.setAttachmentChangesListener(null);
966 }
967
Mindy Pereira75f66632012-01-11 11:42:02 -0800968 // Now that the message has been initialized from any existing draft or
969 // ref message data, set up listeners for any changes that occur to the
970 // message.
971 private void initChangeListeners() {
972 mSubject.addTextChangedListener(this);
973 mBodyView.addTextChangedListener(this);
Mindy Pereiracbfb75a2012-06-25 14:52:23 -0700974 if (mToListener == null) {
975 mToListener = new RecipientTextWatcher(mTo, this);
976 }
977 mTo.addTextChangedListener(mToListener);
978 if (mCcListener == null) {
979 mCcListener = new RecipientTextWatcher(mCc, this);
980 }
981 mCc.addTextChangedListener(mCcListener);
982 if (mBccListener == null) {
983 mBccListener = new RecipientTextWatcher(mBcc, this);
984 }
985 mBcc.addTextChangedListener(mBccListener);
Mindy Pereira75f66632012-01-11 11:42:02 -0800986 mFromSpinner.setOnAccountChangedListener(this);
Mindy Pereira818143e2012-01-11 13:59:49 -0800987 mAttachmentsView.setAttachmentChangesListener(this);
Mindy Pereira75f66632012-01-11 11:42:02 -0800988 }
989
Mindy Pereira326c6602012-01-04 15:32:42 -0800990 private void initActionBar(int action) {
991 mComposeMode = action;
Mindy Pereiraeaea9f12012-01-10 15:05:27 -0800992 ActionBar actionBar = getActionBar();
Mindy Pereirae011b1d2012-06-18 13:45:26 -0700993 if (actionBar == null) {
994 return;
995 }
Mindy Pereira326c6602012-01-04 15:32:42 -0800996 if (action == ComposeActivity.COMPOSE) {
Mindy Pereiraeaea9f12012-01-10 15:05:27 -0800997 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
998 actionBar.setTitle(R.string.compose);
Mindy Pereira326c6602012-01-04 15:32:42 -0800999 } else {
Mindy Pereiraeaea9f12012-01-10 15:05:27 -08001000 actionBar.setTitle(null);
Mindy Pereira326c6602012-01-04 15:32:42 -08001001 if (mComposeModeAdapter == null) {
1002 mComposeModeAdapter = new ComposeModeAdapter(this);
1003 }
Mindy Pereiraeaea9f12012-01-10 15:05:27 -08001004 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
1005 actionBar.setListNavigationCallbacks(mComposeModeAdapter, this);
Mindy Pereira326c6602012-01-04 15:32:42 -08001006 switch (action) {
1007 case ComposeActivity.REPLY:
Mindy Pereiraeaea9f12012-01-10 15:05:27 -08001008 actionBar.setSelectedNavigationItem(0);
Mindy Pereira326c6602012-01-04 15:32:42 -08001009 break;
1010 case ComposeActivity.REPLY_ALL:
Mindy Pereiraeaea9f12012-01-10 15:05:27 -08001011 actionBar.setSelectedNavigationItem(1);
Mindy Pereira326c6602012-01-04 15:32:42 -08001012 break;
1013 case ComposeActivity.FORWARD:
Mindy Pereiraeaea9f12012-01-10 15:05:27 -08001014 actionBar.setSelectedNavigationItem(2);
Mindy Pereira326c6602012-01-04 15:32:42 -08001015 break;
1016 }
1017 }
Mindy Pereirafbe40192012-03-20 10:40:45 -07001018 actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME,
1019 ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME);
1020 actionBar.setHomeButtonEnabled(true);
Mindy Pereira326c6602012-01-04 15:32:42 -08001021 }
1022
Mindy Pereira46ce0b12012-01-05 10:32:15 -08001023 private void initFromRefMessage(int action, String recipientAddress) {
Mindy Pereira96a7f7a2012-07-09 16:51:06 -07001024 setFieldsFromRefMessage(action, recipientAddress);
1025 if (mRefMessage != null) {
1026 // CC field only gets populated when doing REPLY_ALL.
1027 // BCC never gets auto-populated, unless the user is editing
1028 // a draft with one.
Mindy Pereira29a717e2012-07-25 18:05:48 -07001029 if (!TextUtils.isEmpty(mCc.getText()) && action == REPLY_ALL) {
Mindy Pereira96a7f7a2012-07-09 16:51:06 -07001030 mCcBccView.show(false, true, false);
1031 }
1032 }
1033 updateHideOrShowCcBcc();
1034 }
1035
1036 private void setFieldsFromRefMessage(int action, String recipientAddress) {
Mindy Pereira8eca57a2012-03-20 16:42:34 -07001037 setSubject(mRefMessage, action);
1038 // Setup recipients
1039 if (action == FORWARD) {
1040 mForward = true;
Mindy Pereira6349a042012-01-04 11:25:01 -08001041 }
Mindy Pereira8eca57a2012-03-20 16:42:34 -07001042 initRecipientsFromRefMessage(recipientAddress, mRefMessage, action);
Mindy Pereirae8f94dc2012-04-16 11:56:21 -07001043 initQuotedTextFromRefMessage(mRefMessage, action);
Mindy Pereira8eca57a2012-03-20 16:42:34 -07001044 if (action == ComposeActivity.FORWARD || mAttachmentsChanged) {
1045 initAttachments(mRefMessage);
1046 }
Mindy Pereirac17d0732011-12-29 10:46:19 -08001047 }
1048
Mindy Pereirae8f94dc2012-04-16 11:56:21 -07001049 private void initFromDraftMessage(Message message) {
Andy Huang1f8f4dd2012-10-25 21:35:35 -07001050 LogUtils.d(LOG_TAG, "Intializing draft from previous draft message: %s", message);
Paul Westbrookbb87b7f2012-03-20 16:20:07 -07001051
1052 mDraft = message;
1053 mDraftId = message.id;
1054 mSubject.setText(message.subject);
1055 mForward = message.draftType == UIProvider.DraftType.FORWARD;
1056 final List<String> toAddresses = Arrays.asList(message.getToAddresses());
1057 addToAddresses(toAddresses);
1058 addCcAddresses(Arrays.asList(message.getCcAddresses()), toAddresses);
1059 addBccAddresses(Arrays.asList(message.getBccAddresses()));
Mindy Pereira2421dc82012-03-27 13:32:31 -07001060 if (message.hasAttachments) {
1061 List<Attachment> attachments = message.getAttachments();
1062 for (Attachment a : attachments) {
Andy Huang5c5fd572012-04-08 18:19:29 -07001063 addAttachmentAndUpdateView(a);
Mindy Pereira2421dc82012-03-27 13:32:31 -07001064 }
1065 }
Mindy Pereiracc8e7db2012-05-30 12:57:42 -07001066 int quotedTextIndex = message.appendRefMessageContent ?
Mindy Pereira002ff522012-05-30 10:31:26 -07001067 message.quotedTextOffset : -1;
Paul Westbrookbb87b7f2012-03-20 16:20:07 -07001068 // Set the body
Mindy Pereira002ff522012-05-30 10:31:26 -07001069 CharSequence quotedText = null;
Paul Westbrookbb87b7f2012-03-20 16:20:07 -07001070 if (!TextUtils.isEmpty(message.bodyHtml)) {
Mindy Pereira752222d2012-07-19 09:58:53 -07001071 CharSequence htmlText = "";
Mindy Pereira002ff522012-05-30 10:31:26 -07001072 if (quotedTextIndex > -1) {
Mindy Pereira752222d2012-07-19 09:58:53 -07001073 // Find the offset in the htmltext of the actual quoted text and strip it out.
1074 quotedTextIndex = QuotedTextView.findQuotedTextIndex(message.bodyHtml);
1075 if (quotedTextIndex > -1) {
mindypc59dd822012-11-13 10:56:21 -08001076 htmlText = Utils.convertHtmlToPlainText(message.bodyHtml.substring(0,
1077 quotedTextIndex));
Mindy Pereira752222d2012-07-19 09:58:53 -07001078 quotedText = message.bodyHtml.subSequence(quotedTextIndex,
1079 message.bodyHtml.length());
1080 }
Mindy Pereira1a6e9382012-08-14 15:51:22 -07001081 } else {
mindypc59dd822012-11-13 10:56:21 -08001082 htmlText = Utils.convertHtmlToPlainText(message.bodyHtml);
Mindy Pereira002ff522012-05-30 10:31:26 -07001083 }
1084 mBodyView.setText(htmlText);
Paul Westbrookbb87b7f2012-03-20 16:20:07 -07001085 } else {
Mindy Pereira752222d2012-07-19 09:58:53 -07001086 final String body = message.bodyText;
1087 final CharSequence bodyText = !TextUtils.isEmpty(body) ?
1088 (quotedTextIndex > -1 ?
1089 message.bodyText.substring(0, quotedTextIndex) : message.bodyText)
1090 : "";
Mindy Pereira002ff522012-05-30 10:31:26 -07001091 if (quotedTextIndex > -1) {
Mindy Pereira752222d2012-07-19 09:58:53 -07001092 quotedText = !TextUtils.isEmpty(body) ? message.bodyText.substring(quotedTextIndex)
1093 : null;
Mindy Pereira002ff522012-05-30 10:31:26 -07001094 }
1095 mBodyView.setText(bodyText);
1096 }
1097 if (quotedTextIndex > -1 && quotedText != null) {
Mindy Pereira39713232012-05-30 11:48:41 -07001098 mQuotedTextView.setQuotedTextFromDraft(quotedText, mForward);
Paul Westbrookbb87b7f2012-03-20 16:20:07 -07001099 }
Paul Westbrookbb87b7f2012-03-20 16:20:07 -07001100 }
1101
Mindy Pereira8eca57a2012-03-20 16:42:34 -07001102 /**
1103 * Fill all the widgets with the content found in the Intent Extra, if any.
1104 * Also apply the same style to all widgets. Note: if initFromExtras is
1105 * called as a result of switching between reply, reply all, and forward per
1106 * the latest revision of Gmail, and the user has already made changes to
1107 * attachments on a previous incarnation of the message (as a reply, reply
1108 * all, or forward), the original attachments from the message will not be
1109 * re-instantiated. The user's changes will be respected. This follows the
1110 * web gmail interaction.
1111 */
1112 public void initFromExtras(Intent intent) {
Mindy Pereira8eca57a2012-03-20 16:42:34 -07001113 // If we were invoked with a SENDTO intent, the value
1114 // should take precedence
1115 final Uri dataUri = intent.getData();
1116 if (dataUri != null) {
1117 if (MAIL_TO.equals(dataUri.getScheme())) {
1118 initFromMailTo(dataUri.toString());
1119 } else {
Mindy Pereira0b4f28e2012-03-28 14:12:21 -07001120 if (!mAccount.composeIntentUri.equals(dataUri)) {
Mindy Pereira8eca57a2012-03-20 16:42:34 -07001121 String toText = dataUri.getSchemeSpecificPart();
1122 if (toText != null) {
1123 mTo.setText("");
Mindy Pereiradbe89962012-04-13 09:42:38 -07001124 addToAddresses(Arrays.asList(TextUtils.split(toText, ",")));
Mindy Pereira8eca57a2012-03-20 16:42:34 -07001125 }
1126 }
1127 }
1128 }
1129
1130 String[] extraStrings = intent.getStringArrayExtra(Intent.EXTRA_EMAIL);
1131 if (extraStrings != null) {
1132 addToAddresses(Arrays.asList(extraStrings));
1133 }
1134 extraStrings = intent.getStringArrayExtra(Intent.EXTRA_CC);
1135 if (extraStrings != null) {
1136 addCcAddresses(Arrays.asList(extraStrings), null);
1137 }
1138 extraStrings = intent.getStringArrayExtra(Intent.EXTRA_BCC);
1139 if (extraStrings != null) {
1140 addBccAddresses(Arrays.asList(extraStrings));
1141 }
1142
1143 String extraString = intent.getStringExtra(Intent.EXTRA_SUBJECT);
1144 if (extraString != null) {
1145 mSubject.setText(extraString);
1146 }
1147
1148 for (String extra : ALL_EXTRAS) {
1149 if (intent.hasExtra(extra)) {
1150 String value = intent.getStringExtra(extra);
1151 if (EXTRA_TO.equals(extra)) {
Mindy Pereiradbe89962012-04-13 09:42:38 -07001152 addToAddresses(Arrays.asList(TextUtils.split(value, ",")));
Mindy Pereira8eca57a2012-03-20 16:42:34 -07001153 } else if (EXTRA_CC.equals(extra)) {
Mindy Pereiradbe89962012-04-13 09:42:38 -07001154 addCcAddresses(Arrays.asList(TextUtils.split(value, ",")), null);
Mindy Pereira8eca57a2012-03-20 16:42:34 -07001155 } else if (EXTRA_BCC.equals(extra)) {
Mindy Pereiradbe89962012-04-13 09:42:38 -07001156 addBccAddresses(Arrays.asList(TextUtils.split(value, ",")));
Mindy Pereira8eca57a2012-03-20 16:42:34 -07001157 } else if (EXTRA_SUBJECT.equals(extra)) {
1158 mSubject.setText(value);
1159 } else if (EXTRA_BODY.equals(extra)) {
1160 setBody(value, true /* with signature */);
1161 }
1162 }
1163 }
1164
1165 Bundle extras = intent.getExtras();
1166 if (extras != null) {
Mindy Pereira8eca57a2012-03-20 16:42:34 -07001167 CharSequence text = extras.getCharSequence(Intent.EXTRA_TEXT);
1168 if (text != null) {
1169 setBody(text, true /* with signature */);
1170 }
Mindy Pereira8eca57a2012-03-20 16:42:34 -07001171 }
Mindy Pereirae8f94dc2012-04-16 11:56:21 -07001172 }
1173
Mindy Pereira8eca57a2012-03-20 16:42:34 -07001174 @VisibleForTesting
1175 protected String decodeEmailInUri(String s) throws UnsupportedEncodingException {
Mindy Pereiraa4069f22012-05-30 15:31:45 -07001176 // TODO: handle the case where there are spaces in the display name as
1177 // well as the email such as "Guy with spaces <guy+with+spaces@gmail.com>"
1178 // as they could be encoded ambiguously.
Mindy Pereira8eca57a2012-03-20 16:42:34 -07001179 // Since URLDecode.decode changes + into ' ', and + is a valid
1180 // email character, we need to find/ replace these ourselves before
1181 // decoding.
1182 String replacePlus = s.replace("+", "%2B");
Mindy Pereiraa4069f22012-05-30 15:31:45 -07001183 try {
1184 return URLDecoder.decode(replacePlus, UTF8_ENCODING_NAME);
1185 } catch (IllegalArgumentException e) {
1186 if (LogUtils.isLoggable(LOG_TAG, LogUtils.VERBOSE)) {
1187 LogUtils.e(LOG_TAG, "%s while decoding '%s'", e.getMessage(), s);
1188 } else {
1189 LogUtils.e(LOG_TAG, e, "Exception while decoding mailto address");
1190 }
1191 return null;
1192 }
Mindy Pereira8eca57a2012-03-20 16:42:34 -07001193 }
1194
1195 /**
1196 * Initialize the compose view from a String representing a mailTo uri.
1197 * @param mailToString The uri as a string.
1198 */
1199 public void initFromMailTo(String mailToString) {
1200 // We need to disguise this string as a URI in order to parse it
1201 // TODO: Remove this hack when http://b/issue?id=1445295 gets fixed
1202 Uri uri = Uri.parse("foo://" + mailToString);
1203 int index = mailToString.indexOf("?");
1204 int length = "mailto".length() + 1;
1205 String to;
1206 try {
1207 // Extract the recipient after mailto:
1208 if (index == -1) {
1209 to = decodeEmailInUri(mailToString.substring(length));
1210 } else {
1211 to = decodeEmailInUri(mailToString.substring(length, index));
1212 }
Mindy Pereiraa4069f22012-05-30 15:31:45 -07001213 if (!TextUtils.isEmpty(to)) {
1214 addToAddresses(Arrays.asList(TextUtils.split(to, ",")));
1215 }
Mindy Pereira8eca57a2012-03-20 16:42:34 -07001216 } catch (UnsupportedEncodingException e) {
1217 if (LogUtils.isLoggable(LOG_TAG, LogUtils.VERBOSE)) {
1218 LogUtils.e(LOG_TAG, "%s while decoding '%s'", e.getMessage(), mailToString);
1219 } else {
1220 LogUtils.e(LOG_TAG, e, "Exception while decoding mailto address");
1221 }
1222 }
1223
1224 List<String> cc = uri.getQueryParameters("cc");
1225 addCcAddresses(Arrays.asList(cc.toArray(new String[cc.size()])), null);
1226
1227 List<String> otherTo = uri.getQueryParameters("to");
1228 addToAddresses(Arrays.asList(otherTo.toArray(new String[otherTo.size()])));
1229
1230 List<String> bcc = uri.getQueryParameters("bcc");
1231 addBccAddresses(Arrays.asList(bcc.toArray(new String[bcc.size()])));
1232
1233 List<String> subject = uri.getQueryParameters("subject");
1234 if (subject.size() > 0) {
1235 try {
1236 mSubject.setText(URLDecoder.decode(subject.get(0), UTF8_ENCODING_NAME));
1237 } catch (UnsupportedEncodingException e) {
1238 LogUtils.e(LOG_TAG, "%s while decoding subject '%s'",
1239 e.getMessage(), subject);
1240 }
1241 }
1242
1243 List<String> body = uri.getQueryParameters("body");
1244 if (body.size() > 0) {
1245 try {
1246 setBody(URLDecoder.decode(body.get(0), UTF8_ENCODING_NAME),
1247 true /* with signature */);
1248 } catch (UnsupportedEncodingException e) {
1249 LogUtils.e(LOG_TAG, "%s while decoding body '%s'", e.getMessage(), body);
1250 }
1251 }
Mindy Pereira8eca57a2012-03-20 16:42:34 -07001252 }
1253
Mindy Pereirabddd6f32012-06-20 12:10:03 -07001254 @VisibleForTesting
1255 protected void initAttachments(Message refMessage) {
Mark Wei434f2942012-08-24 11:54:02 -07001256 addAttachments(refMessage.getAttachments());
1257 }
1258
1259 public long addAttachments(List<Attachment> attachments) {
1260 long size = 0;
1261 AttachmentFailureException error = null;
1262 for (Attachment a : attachments) {
1263 try {
1264 size += mAttachmentsView.addAttachment(mAccount, a);
1265 } catch (AttachmentFailureException e) {
1266 error = e;
1267 }
Mindy Pereira3cd4f402012-07-17 11:16:18 -07001268 }
Mark Wei434f2942012-08-24 11:54:02 -07001269 if (error != null) {
1270 LogUtils.e(LOG_TAG, error, "Error adding attachment");
1271 if (attachments.size() > 1) {
1272 showAttachmentTooBigToast(R.string.too_large_to_attach_multiple);
1273 } else {
1274 showAttachmentTooBigToast(error.getErrorRes());
1275 }
1276 }
1277 return size;
Mindy Pereira3cd4f402012-07-17 11:16:18 -07001278 }
1279
1280 /**
1281 * When an attachment is too large to be added to a message, show a toast.
1282 * This method also updates the position of the toast so that it is shown
1283 * clearly above they keyboard if it happens to be open.
1284 */
Mark Wei434f2942012-08-24 11:54:02 -07001285 private void showAttachmentTooBigToast(int errorRes) {
1286 String maxSize = AttachmentUtils.convertToHumanReadableSize(
1287 getApplicationContext(), mAccount.settings.getMaxAttachmentSize());
1288 showErrorToast(getString(errorRes, maxSize));
Mindy Pereira3cd4f402012-07-17 11:16:18 -07001289 }
1290
Mark Wei434f2942012-08-24 11:54:02 -07001291 private void showErrorToast(String message) {
1292 Toast t = Toast.makeText(this, message, Toast.LENGTH_LONG);
1293 t.setText(message);
Mindy Pereira3cd4f402012-07-17 11:16:18 -07001294 t.setGravity(Gravity.CENTER_HORIZONTAL, 0,
1295 getResources().getDimensionPixelSize(R.dimen.attachment_toast_yoffset));
1296 t.show();
Mindy Pereiraeaea9f12012-01-10 15:05:27 -08001297 }
1298
Paul Westbrookf97588b2012-03-20 11:11:37 -07001299 private void initAttachmentsFromIntent(Intent intent) {
Paul Westbrook03ee9712012-04-02 09:51:51 -07001300 Bundle extras = intent.getExtras();
1301 if (extras == null) {
1302 extras = Bundle.EMPTY;
1303 }
Paul Westbrookf97588b2012-03-20 11:11:37 -07001304 final String action = intent.getAction();
1305 if (!mAttachmentsChanged) {
1306 long totalSize = 0;
1307 if (extras.containsKey(EXTRA_ATTACHMENTS)) {
1308 String[] uris = (String[]) extras.getSerializable(EXTRA_ATTACHMENTS);
1309 for (String uriString : uris) {
1310 final Uri uri = Uri.parse(uriString);
1311 long size = 0;
1312 try {
Andy Huang5c5fd572012-04-08 18:19:29 -07001313 size = mAttachmentsView.addAttachment(mAccount, uri);
Paul Westbrookf97588b2012-03-20 11:11:37 -07001314 } catch (AttachmentFailureException e) {
Paul Westbrookf97588b2012-03-20 11:11:37 -07001315 LogUtils.e(LOG_TAG, e, "Error adding attachment");
Mark Wei434f2942012-08-24 11:54:02 -07001316 showAttachmentTooBigToast(e.getErrorRes());
Paul Westbrookf97588b2012-03-20 11:11:37 -07001317 }
1318 totalSize += size;
1319 }
1320 }
mindyp9a9e8d62012-10-03 12:24:07 -07001321 if (extras.containsKey(Intent.EXTRA_STREAM)) {
1322 if (Intent.ACTION_SEND_MULTIPLE.equals(action)) {
1323 ArrayList<Parcelable> uris = extras
1324 .getParcelableArrayList(Intent.EXTRA_STREAM);
1325 ArrayList<Attachment> attachments = new ArrayList<Attachment>();
1326 for (Parcelable uri : uris) {
1327 try {
1328 attachments.add(mAttachmentsView.generateLocalAttachment((Uri) uri));
1329 } catch (AttachmentFailureException e) {
1330 LogUtils.e(LOG_TAG, e, "Error adding attachment");
1331 String maxSize = AttachmentUtils.convertToHumanReadableSize(
1332 getApplicationContext(),
1333 mAccount.settings.getMaxAttachmentSize());
1334 showErrorToast(getString
1335 (R.string.generic_attachment_problem, maxSize));
1336 }
1337 }
1338 totalSize += addAttachments(attachments);
1339 } else {
1340 final Uri uri = (Uri) extras.getParcelable(Intent.EXTRA_STREAM);
1341 long size = 0;
Paul Westbrookf97588b2012-03-20 11:11:37 -07001342 try {
mindyp9a9e8d62012-10-03 12:24:07 -07001343 size = mAttachmentsView.addAttachment(mAccount, uri);
Paul Westbrookf97588b2012-03-20 11:11:37 -07001344 } catch (AttachmentFailureException e) {
Paul Westbrookf97588b2012-03-20 11:11:37 -07001345 LogUtils.e(LOG_TAG, e, "Error adding attachment");
mindyp9a9e8d62012-10-03 12:24:07 -07001346 showAttachmentTooBigToast(e.getErrorRes());
Paul Westbrookf97588b2012-03-20 11:11:37 -07001347 }
mindyp9a9e8d62012-10-03 12:24:07 -07001348 totalSize += size;
Paul Westbrookf97588b2012-03-20 11:11:37 -07001349 }
1350 }
1351
1352 if (totalSize > 0) {
1353 mAttachmentsChanged = true;
1354 updateSaveUi();
1355 }
1356 }
1357 }
1358
1359
Mindy Pereirae8f94dc2012-04-16 11:56:21 -07001360 private void initQuotedTextFromRefMessage(Message refMessage, int action) {
1361 if (mRefMessage != null && (action == REPLY || action == REPLY_ALL || action == FORWARD)) {
Mindy Pereira9932dee2012-01-10 16:09:50 -08001362 mQuotedTextView.setQuotedText(action, refMessage, action != FORWARD);
1363 }
Mindy Pereira46ce0b12012-01-05 10:32:15 -08001364 }
1365
1366 private void updateHideOrShowCcBcc() {
Mindy Pereiraec8b0ed2012-01-06 10:35:59 -08001367 // Its possible there is a menu item OR a button.
Mindy Pereira326689d2012-05-17 10:14:14 -07001368 boolean ccVisible = mCcBccView.isCcVisible();
1369 boolean bccVisible = mCcBccView.isBccVisible();
Mindy Pereiraec8b0ed2012-01-06 10:35:59 -08001370 if (mCcBccButton != null) {
Mindy Pereira326689d2012-05-17 10:14:14 -07001371 if (!ccVisible || !bccVisible) {
Mindy Pereiraec8b0ed2012-01-06 10:35:59 -08001372 mCcBccButton.setVisibility(View.VISIBLE);
Mindy Pereira326689d2012-05-17 10:14:14 -07001373 mCcBccButton.setText(getString(!ccVisible ? R.string.add_cc_label
Mindy Pereiraec8b0ed2012-01-06 10:35:59 -08001374 : R.string.add_bcc_label));
1375 } else {
mindypcd0b0b92012-08-23 14:33:17 -07001376 mCcBccButton.setVisibility(View.INVISIBLE);
Mindy Pereiraec8b0ed2012-01-06 10:35:59 -08001377 }
1378 }
Mindy Pereira46ce0b12012-01-05 10:32:15 -08001379 }
1380
Mindy Pereiraa34c9a02012-04-17 14:10:53 -07001381 private void showCcBcc(Bundle state) {
Mindy Pereira326689d2012-05-17 10:14:14 -07001382 if (state != null && state.containsKey(EXTRA_SHOW_CC)) {
1383 boolean showCc = state.getBoolean(EXTRA_SHOW_CC);
1384 boolean showBcc = state.getBoolean(EXTRA_SHOW_BCC);
1385 if (showCc || showBcc) {
1386 mCcBccView.show(false, showCc, showBcc);
Mindy Pereira6faeedf2012-04-18 16:11:39 -07001387 }
Mindy Pereiraa34c9a02012-04-17 14:10:53 -07001388 }
1389 }
1390
Mindy Pereira013194c2012-01-06 15:09:33 -08001391 /**
1392 * Add attachment and update the compose area appropriately.
1393 * @param data
1394 */
1395 public void addAttachmentAndUpdateView(Intent data) {
Mindy Pereira2421dc82012-03-27 13:32:31 -07001396 addAttachmentAndUpdateView(data != null ? data.getData() : (Uri) null);
1397 }
1398
Andy Huang5c5fd572012-04-08 18:19:29 -07001399 public void addAttachmentAndUpdateView(Uri contentUri) {
1400 if (contentUri == null) {
Mindy Pereira2421dc82012-03-27 13:32:31 -07001401 return;
1402 }
Mindy Pereira013194c2012-01-06 15:09:33 -08001403 try {
Andy Huang5c5fd572012-04-08 18:19:29 -07001404 addAttachmentAndUpdateView(mAttachmentsView.generateLocalAttachment(contentUri));
1405 } catch (AttachmentFailureException e) {
Andy Huang5c5fd572012-04-08 18:19:29 -07001406 LogUtils.e(LOG_TAG, e, "Error adding attachment");
Mark Wei434f2942012-08-24 11:54:02 -07001407 showErrorToast(getResources().getString(
1408 e.getErrorRes(),
1409 AttachmentUtils.convertToHumanReadableSize(
1410 getApplicationContext(), mAccount.settings.getMaxAttachmentSize())));
Andy Huang5c5fd572012-04-08 18:19:29 -07001411 }
1412 }
1413
1414 public void addAttachmentAndUpdateView(Attachment attachment) {
1415 try {
Mark Wei434f2942012-08-24 11:54:02 -07001416 long size = mAttachmentsView.addAttachment(mAccount, attachment);
Mindy Pereira9932dee2012-01-10 16:09:50 -08001417 if (size > 0) {
1418 mAttachmentsChanged = true;
1419 updateSaveUi();
Mindy Pereira013194c2012-01-06 15:09:33 -08001420 }
Mindy Pereira9932dee2012-01-10 16:09:50 -08001421 } catch (AttachmentFailureException e) {
Mindy Pereira9932dee2012-01-10 16:09:50 -08001422 LogUtils.e(LOG_TAG, e, "Error adding attachment");
Mark Wei434f2942012-08-24 11:54:02 -07001423 showAttachmentTooBigToast(e.getErrorRes());
Mindy Pereira013194c2012-01-06 15:09:33 -08001424 }
1425 }
1426
Mindy Pereira3ce64e72012-01-13 14:29:45 -08001427 void initRecipientsFromRefMessage(String recipientAddress, Message refMessage,
Mindy Pereira46ce0b12012-01-05 10:32:15 -08001428 int action) {
Mindy Pereira4a27ea92012-01-05 15:55:25 -08001429 // Don't populate the address if this is a forward.
1430 if (action == ComposeActivity.FORWARD) {
1431 return;
1432 }
Mindy Pereira33fe9082012-01-09 16:24:30 -08001433 initReplyRecipients(mAccount.name, refMessage, action);
Mindy Pereira4a27ea92012-01-05 15:55:25 -08001434 }
Mindy Pereira46ce0b12012-01-05 10:32:15 -08001435
Mindy Pereira818143e2012-01-11 13:59:49 -08001436 @VisibleForTesting
Mindy Pereira3ce64e72012-01-13 14:29:45 -08001437 void initReplyRecipients(String account, Message refMessage, int action) {
Mindy Pereira4a27ea92012-01-05 15:55:25 -08001438 // This is the email address of the current user, i.e. the one composing
1439 // the reply.
Mindy Pereira4a20b702012-01-05 16:24:24 -08001440 final String accountEmail = Address.getEmailAddress(account).getAddress();
mindyp5ed63112012-09-17 17:31:45 -07001441 String[] sentToAddresses = refMessage.getToAddresses();
Scott Kennedy8960f0a2012-11-07 15:35:50 -08001442 String replytoAddress = refMessage.getReplyTo();
Mindy Pereiraa26b54e2012-01-06 12:54:33 -08001443 final Collection<String> toAddresses;
Mindy Pereira4a27ea92012-01-05 15:55:25 -08001444
1445 // If this is a reply, the Cc list is empty. If this is a reply-all, the
1446 // Cc list is the union of the To and Cc recipients of the original
1447 // message, excluding the current user's email address and any addresses
Mindy Pereiraa26b54e2012-01-06 12:54:33 -08001448 // already on the To list.
1449 if (action == ComposeActivity.REPLY) {
Scott Kennedy8960f0a2012-11-07 15:35:50 -08001450 toAddresses = initToRecipients(account, accountEmail, refMessage.getFrom(),
1451 replytoAddress, sentToAddresses);
Mindy Pereiraa26b54e2012-01-06 12:54:33 -08001452 addToAddresses(toAddresses);
1453 } else if (action == ComposeActivity.REPLY_ALL) {
Mindy Pereira4a27ea92012-01-05 15:55:25 -08001454 final Set<String> ccAddresses = Sets.newHashSet();
Scott Kennedy8960f0a2012-11-07 15:35:50 -08001455 toAddresses = initToRecipients(account, accountEmail, refMessage.getFrom(),
1456 replytoAddress, sentToAddresses);
Mindy Pereira154386a2012-01-11 13:02:33 -08001457 addToAddresses(toAddresses);
Mindy Pereira4a27ea92012-01-05 15:55:25 -08001458 addRecipients(accountEmail, ccAddresses, sentToAddresses);
mindyp5ed63112012-09-17 17:31:45 -07001459 addRecipients(accountEmail, ccAddresses, refMessage.getCcAddresses());
Mindy Pereira4a27ea92012-01-05 15:55:25 -08001460 addCcAddresses(ccAddresses, toAddresses);
1461 }
1462 }
1463
Mindy Pereira1469b4e2012-06-19 19:18:54 -07001464 private String getAddress(String toParse) {
1465 if (!TextUtils.isEmpty(toParse)) {
1466 Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(toParse);
1467 if (tokens.length > 0) {
1468 return tokens[0].getAddress();
1469 }
1470 }
1471 return "";
1472 }
1473
Mindy Pereira4a27ea92012-01-05 15:55:25 -08001474 private void addToAddresses(Collection<String> addresses) {
1475 addAddressesToList(addresses, mTo);
1476 }
1477
1478 private void addCcAddresses(Collection<String> addresses, Collection<String> toAddresses) {
Mindy Pereira8eca57a2012-03-20 16:42:34 -07001479 addCcAddressesToList(tokenizeAddressList(addresses),
1480 toAddresses != null ? tokenizeAddressList(toAddresses) : null, mCc);
Mindy Pereira4a27ea92012-01-05 15:55:25 -08001481 }
1482
Paul Westbrookbb87b7f2012-03-20 16:20:07 -07001483 private void addBccAddresses(Collection<String> addresses) {
1484 addAddressesToList(addresses, mBcc);
1485 }
1486
Mindy Pereira4a27ea92012-01-05 15:55:25 -08001487 @VisibleForTesting
1488 protected void addCcAddressesToList(List<Rfc822Token[]> addresses,
1489 List<Rfc822Token[]> compareToList, RecipientEditTextView list) {
1490 String address;
1491
Mindy Pereira8eca57a2012-03-20 16:42:34 -07001492 if (compareToList == null) {
1493 for (Rfc822Token[] tokens : addresses) {
1494 for (int i = 0; i < tokens.length; i++) {
1495 address = tokens[i].toString();
Mindy Pereira4a27ea92012-01-05 15:55:25 -08001496 list.append(address + END_TOKEN);
1497 }
1498 }
Mindy Pereira8eca57a2012-03-20 16:42:34 -07001499 } else {
1500 HashSet<String> compareTo = convertToHashSet(compareToList);
1501 for (Rfc822Token[] tokens : addresses) {
1502 for (int i = 0; i < tokens.length; i++) {
1503 address = tokens[i].toString();
1504 // Check if this is a duplicate:
1505 if (!compareTo.contains(tokens[i].getAddress())) {
1506 // Get the address here
1507 list.append(address + END_TOKEN);
1508 }
1509 }
1510 }
Mindy Pereira4a27ea92012-01-05 15:55:25 -08001511 }
1512 }
1513
Mindy Pereira4a27ea92012-01-05 15:55:25 -08001514 private HashSet<String> convertToHashSet(List<Rfc822Token[]> list) {
1515 HashSet<String> hash = new HashSet<String>();
1516 for (Rfc822Token[] tokens : list) {
1517 for (int i = 0; i < tokens.length; i++) {
1518 hash.add(tokens[i].getAddress());
1519 }
1520 }
1521 return hash;
1522 }
1523
Mindy Pereira4a27ea92012-01-05 15:55:25 -08001524 protected List<Rfc822Token[]> tokenizeAddressList(Collection<String> addresses) {
1525 @VisibleForTesting
1526 List<Rfc822Token[]> tokenized = new ArrayList<Rfc822Token[]>();
1527
1528 for (String address: addresses) {
1529 tokenized.add(Rfc822Tokenizer.tokenize(address));
1530 }
1531 return tokenized;
1532 }
1533
1534 @VisibleForTesting
1535 void addAddressesToList(Collection<String> addresses, RecipientEditTextView list) {
1536 for (String address : addresses) {
1537 addAddressToList(address, list);
1538 }
1539 }
1540
1541 private void addAddressToList(String address, RecipientEditTextView list) {
1542 if (address == null || list == null)
1543 return;
1544
1545 Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(address);
1546
1547 for (int i = 0; i < tokens.length; i++) {
1548 list.append(tokens[i] + END_TOKEN);
1549 }
1550 }
1551
1552 @VisibleForTesting
1553 protected Collection<String> initToRecipients(String account, String accountEmail,
mindyp89576472012-11-06 17:07:08 -08001554 String fullSenderAddress, String replyToAddress,
1555 String[] inToAddresses) {
Mindy Pereira4a27ea92012-01-05 15:55:25 -08001556 // The To recipient is the reply-to address specified in the original
1557 // message, unless it is:
1558 // the current user OR a custom from of the current user, in which case
1559 // it's the To recipient list of the original message.
1560 // OR missing, in which case use the sender of the original message
1561 Set<String> toAddresses = Sets.newHashSet();
mindypfe8557b2012-11-05 12:05:16 -08001562 if (!TextUtils.isEmpty(replyToAddress)
1563 && !recipientMatchesThisAccount(account, replyToAddress)) {
Mindy Pereira3ce64e72012-01-13 14:29:45 -08001564 toAddresses.add(replyToAddress);
Mindy Pereira4a27ea92012-01-05 15:55:25 -08001565 } else {
mindyp89576472012-11-06 17:07:08 -08001566 if (!recipientMatchesThisAccount(account, fullSenderAddress)) {
1567 toAddresses.add(fullSenderAddress);
Mindy Pereira1469b4e2012-06-19 19:18:54 -07001568 } else {
1569 // This happens if the user replies to a message they originally
Mindy Pereira1883b342012-06-20 08:34:56 -07001570 // wrote. In this case, "reply" really means "re-send," so we
1571 // target the original recipients. This works as expected even
1572 // if the user sent the original message to themselves.
mindypfe8557b2012-11-05 12:05:16 -08001573 for (String address : inToAddresses) {
1574 if (!recipientMatchesThisAccount(account, address)) {
1575 toAddresses.add(address);
1576 }
1577 }
Mindy Pereira1469b4e2012-06-19 19:18:54 -07001578 }
Mindy Pereira4a27ea92012-01-05 15:55:25 -08001579 }
1580 return toAddresses;
1581 }
1582
Mindy Pereiracecc54a2012-07-31 09:38:11 -07001583 private void addRecipients(String accountAddress, Set<String> recipients, String[] addresses) {
Mindy Pereira4a27ea92012-01-05 15:55:25 -08001584 for (String email : addresses) {
Mindy Pereiracecc54a2012-07-31 09:38:11 -07001585 // Do not add this account, or any of its custom from addresses, to
1586 // the list of recipients.
Mindy Pereira4a20b702012-01-05 16:24:24 -08001587 final String recipientAddress = Address.getEmailAddress(email).getAddress();
Mindy Pereiracecc54a2012-07-31 09:38:11 -07001588 if (!recipientMatchesThisAccount(accountAddress, recipientAddress)) {
Mindy Pereira4a27ea92012-01-05 15:55:25 -08001589 recipients.add(email.replace("\"\"", ""));
1590 }
1591 }
Mindy Pereira46ce0b12012-01-05 10:32:15 -08001592 }
1593
Mindy Pereiracecc54a2012-07-31 09:38:11 -07001594 /**
1595 * A recipient matches this account if it has the same address as the
1596 * currently selected account OR one of the custom from addresses associated
1597 * with the currently selected account.
1598 * @param accountAddress currently selected account
1599 * @param recipientAddress address we are comparing with the currently selected account
1600 * @return
1601 */
1602 protected boolean recipientMatchesThisAccount(String accountAddress, String recipientAddress) {
1603 return accountAddress.equalsIgnoreCase(recipientAddress)
1604 || ReplyFromAccount.isCustomFrom(recipientAddress,
mindypfe8557b2012-11-05 12:05:16 -08001605 mAccount.getReplyFroms());
Mindy Pereiracecc54a2012-07-31 09:38:11 -07001606 }
1607
Mindy Pereira3ce64e72012-01-13 14:29:45 -08001608 private void setSubject(Message refMessage, int action) {
1609 String subject = refMessage.subject;
Mindy Pereira46ce0b12012-01-05 10:32:15 -08001610 String prefix;
1611 String correctedSubject = null;
1612 if (action == ComposeActivity.COMPOSE) {
1613 prefix = "";
1614 } else if (action == ComposeActivity.FORWARD) {
1615 prefix = getString(R.string.forward_subject_label);
1616 } else {
1617 prefix = getString(R.string.reply_subject_label);
1618 }
1619
1620 // Don't duplicate the prefix
Mindy Pereirac7a36992012-07-30 14:00:37 -07001621 if (!TextUtils.isEmpty(subject)
1622 && subject.toLowerCase().startsWith(prefix.toLowerCase())) {
Mindy Pereira46ce0b12012-01-05 10:32:15 -08001623 correctedSubject = subject;
1624 } else {
1625 correctedSubject = String
1626 .format(getString(R.string.formatted_subject), prefix, subject);
1627 }
1628 mSubject.setText(correctedSubject);
1629 }
1630
Mindy Pereira818143e2012-01-11 13:59:49 -08001631 private void initRecipients() {
1632 setupRecipients(mTo);
1633 setupRecipients(mCc);
1634 setupRecipients(mBcc);
Mindy Pereiraeaea9f12012-01-10 15:05:27 -08001635 }
1636
Mindy Pereira818143e2012-01-11 13:59:49 -08001637 private void setupRecipients(RecipientEditTextView view) {
Paul Westbrook679a8cc2012-02-21 16:37:58 -08001638 view.setAdapter(new RecipientAdapter(this, mAccount));
Mindy Pereirac17d0732011-12-29 10:46:19 -08001639 view.setTokenizer(new Rfc822Tokenizer());
Mindy Pereira82cc5662012-01-09 17:29:30 -08001640 if (mValidator == null) {
Paul Westbrook679a8cc2012-02-21 16:37:58 -08001641 final String accountName = mAccount.name;
Mindy Pereira33fe9082012-01-09 16:24:30 -08001642 int offset = accountName.indexOf("@") + 1;
1643 String account = accountName;
Mindy Pereirac17d0732011-12-29 10:46:19 -08001644 if (offset > -1) {
Mindy Pereira33fe9082012-01-09 16:24:30 -08001645 account = account.substring(accountName.indexOf("@") + 1);
Mindy Pereirac17d0732011-12-29 10:46:19 -08001646 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08001647 mValidator = new Rfc822Validator(account);
Mindy Pereirac17d0732011-12-29 10:46:19 -08001648 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08001649 view.setValidator(mValidator);
Mindy Pereira8e9305e2011-12-13 14:25:04 -08001650 }
1651
1652 @Override
1653 public void onClick(View v) {
1654 int id = v.getId();
1655 switch (id) {
Mindy Pereiraa26b54e2012-01-06 12:54:33 -08001656 case R.id.add_cc_bcc:
Mindy Pereira8e9305e2011-12-13 14:25:04 -08001657 // Verify that cc/ bcc aren't showing.
1658 // Animate in cc/bcc.
Mindy Pereiraa26b54e2012-01-06 12:54:33 -08001659 showCcBccViews();
Mindy Pereira8e9305e2011-12-13 14:25:04 -08001660 break;
mindypcd0b0b92012-08-23 14:33:17 -07001661 case R.id.add_photo_attachment:
1662 doAttach(MIME_TYPE_PHOTO);
1663 break;
1664 case R.id.add_video_attachment:
1665 doAttach(MIME_TYPE_VIDEO);
1666 break;
Mindy Pereira8e9305e2011-12-13 14:25:04 -08001667 }
1668 }
Mindy Pereirab47f3e22011-12-13 14:25:04 -08001669
1670 @Override
1671 public boolean onCreateOptionsMenu(Menu menu) {
1672 super.onCreateOptionsMenu(menu);
Mindy Pereirab199d172012-08-13 11:04:03 -07001673 // Don't render any menu items when there are no accounts.
1674 if (mAccounts == null || mAccounts.length == 0) {
1675 return true;
1676 }
Mindy Pereirab47f3e22011-12-13 14:25:04 -08001677 MenuInflater inflater = getMenuInflater();
1678 inflater.inflate(R.menu.compose_menu, menu);
Mindy Pereira82cc5662012-01-09 17:29:30 -08001679 mSave = menu.findItem(R.id.save);
1680 mSend = menu.findItem(R.id.send);
Mindy Pereira3ca5bad2012-04-16 11:02:42 -07001681 MenuItem helpItem = menu.findItem(R.id.help_info_menu_item);
1682 MenuItem sendFeedbackItem = menu.findItem(R.id.feedback_menu_item);
1683 if (helpItem != null) {
1684 helpItem.setVisible(mAccount != null
1685 && mAccount.supportsCapability(AccountCapabilities.HELP_CONTENT));
1686 }
1687 if (sendFeedbackItem != null) {
1688 sendFeedbackItem.setVisible(mAccount != null
1689 && mAccount.supportsCapability(AccountCapabilities.SEND_FEEDBACK));
1690 }
Mindy Pereirab47f3e22011-12-13 14:25:04 -08001691 return true;
1692 }
1693
1694 @Override
Mindy Pereiraec8b0ed2012-01-06 10:35:59 -08001695 public boolean onPrepareOptionsMenu(Menu menu) {
1696 MenuItem ccBcc = menu.findItem(R.id.add_cc_bcc);
Mindy Pereira818143e2012-01-11 13:59:49 -08001697 if (ccBcc != null && mCc != null) {
Mindy Pereiraec8b0ed2012-01-06 10:35:59 -08001698 // Its possible there is a menu item OR a button.
1699 boolean ccFieldVisible = mCc.isShown();
1700 boolean bccFieldVisible = mBcc.isShown();
1701 if (!ccFieldVisible || !bccFieldVisible) {
1702 ccBcc.setVisible(true);
1703 ccBcc.setTitle(getString(!ccFieldVisible ? R.string.add_cc_label
1704 : R.string.add_bcc_label));
1705 } else {
1706 ccBcc.setVisible(false);
1707 }
1708 }
Mindy Pereira75f66632012-01-11 11:42:02 -08001709 if (mSave != null) {
1710 mSave.setEnabled(shouldSave());
1711 }
Mindy Pereiraec8b0ed2012-01-06 10:35:59 -08001712 return true;
1713 }
1714
1715 @Override
Mindy Pereirab47f3e22011-12-13 14:25:04 -08001716 public boolean onOptionsItemSelected(MenuItem item) {
1717 int id = item.getItemId();
Mindy Pereira75f66632012-01-11 11:42:02 -08001718 boolean handled = true;
Mindy Pereirab47f3e22011-12-13 14:25:04 -08001719 switch (id) {
Mindy Pereira2db7d4a2012-08-15 11:00:02 -07001720 case R.id.add_photo_attachment:
1721 doAttach(MIME_TYPE_PHOTO);
1722 break;
1723 case R.id.add_video_attachment:
1724 doAttach(MIME_TYPE_VIDEO);
Mindy Pereira7b56a612011-12-14 12:32:28 -08001725 break;
Mindy Pereiraec8b0ed2012-01-06 10:35:59 -08001726 case R.id.add_cc_bcc:
1727 showCcBccViews();
Mindy Pereirab47f3e22011-12-13 14:25:04 -08001728 break;
Mindy Pereira33fe9082012-01-09 16:24:30 -08001729 case R.id.save:
Mindy Pereira48e31b02012-05-30 13:12:24 -07001730 doSave(true);
Mindy Pereira33fe9082012-01-09 16:24:30 -08001731 break;
1732 case R.id.send:
1733 doSend();
Mindy Pereira75f66632012-01-11 11:42:02 -08001734 break;
Mindy Pereiraefe3d252012-03-01 14:20:44 -08001735 case R.id.discard:
1736 doDiscard();
1737 break;
Mindy Pereira1f936682012-03-02 11:30:33 -08001738 case R.id.settings:
1739 Utils.showSettings(this, mAccount);
1740 break;
Mindy Pereirafbe40192012-03-20 10:40:45 -07001741 case android.R.id.home:
Paul Westbrookdaecb4b2012-05-31 10:21:26 -07001742 onAppUpPressed();
Mindy Pereirafbe40192012-03-20 10:40:45 -07001743 break;
1744 case R.id.help_info_menu_item:
Paul Westbrook30745b62012-08-19 14:10:32 -07001745 Utils.showHelp(this, mAccount, getString(R.string.compose_help_context));
Mindy Pereirafbe40192012-03-20 10:40:45 -07001746 break;
1747 case R.id.feedback_menu_item:
Paul Westbrook17beb0b2012-08-20 13:34:37 -07001748 Utils.sendFeedback(this, mAccount, false);
Mindy Pereirafbe40192012-03-20 10:40:45 -07001749 break;
Mindy Pereira75f66632012-01-11 11:42:02 -08001750 default:
1751 handled = false;
Mindy Pereira33fe9082012-01-09 16:24:30 -08001752 break;
Mindy Pereirab47f3e22011-12-13 14:25:04 -08001753 }
1754 return !handled ? super.onOptionsItemSelected(item) : handled;
1755 }
Mindy Pereira326c6602012-01-04 15:32:42 -08001756
Mindy Pereirab199d172012-08-13 11:04:03 -07001757 @Override
1758 public void onBackPressed() {
1759 // If we are showing the wait fragment, just exit.
1760 if (getWaitFragment() != null) {
1761 finish();
1762 } else {
1763 super.onBackPressed();
1764 }
1765 }
1766
Vikram Aggarwal1672ff82012-09-21 10:15:22 -07001767 /**
1768 * Carries out the "up" action in the action bar.
1769 */
Paul Westbrookdaecb4b2012-05-31 10:21:26 -07001770 private void onAppUpPressed() {
1771 if (mLaunchedFromEmail) {
1772 // If this was started from Gmail, simply treat app up as the system back button, so
1773 // that the last view is restored.
1774 onBackPressed();
1775 return;
1776 }
1777
1778 // Fire the main activity to ensure it launches the "top" screen of mail.
1779 // Since the main Activity is singleTask, it should revive that task if it was already
1780 // started.
Vikram Aggarwal0c3c2052012-09-21 11:06:28 -07001781 final Intent mailIntent = Utils.createViewInboxIntent(mAccount);
Paul Westbrookdaecb4b2012-05-31 10:21:26 -07001782 mailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK |
1783 Intent.FLAG_ACTIVITY_TASK_ON_HOME);
1784 startActivity(mailIntent);
1785 finish();
1786 }
1787
Mindy Pereira33fe9082012-01-09 16:24:30 -08001788 private void doSend() {
Mark Weidd19b632012-10-19 13:59:28 -07001789 sendOrSaveWithSanityChecks(false, true, false, false);
Mindy Pereira33fe9082012-01-09 16:24:30 -08001790 }
1791
Mindy Pereira48e31b02012-05-30 13:12:24 -07001792 private void doSave(boolean showToast) {
Mark Weidd19b632012-10-19 13:59:28 -07001793 sendOrSaveWithSanityChecks(true, showToast, false, false);
Mindy Pereira48e31b02012-05-30 13:12:24 -07001794 }
1795
Mindy Pereirae011b1d2012-06-18 13:45:26 -07001796 @VisibleForTesting
1797 public interface SendOrSaveCallback {
Mindy Pereira82cc5662012-01-09 17:29:30 -08001798 public void initializeSendOrSave(SendOrSaveTask sendOrSaveTask);
Mindy Pereira7ed1c112012-01-18 10:59:25 -08001799 public void notifyMessageIdAllocated(SendOrSaveMessage sendOrSaveMessage, Message message);
1800 public Message getMessage();
Mindy Pereira82cc5662012-01-09 17:29:30 -08001801 public void sendOrSaveFinished(SendOrSaveTask sendOrSaveTask, boolean success);
1802 }
1803
Mindy Pereirae011b1d2012-06-18 13:45:26 -07001804 @VisibleForTesting
1805 public static class SendOrSaveTask implements Runnable {
Mindy Pereira82cc5662012-01-09 17:29:30 -08001806 private final Context mContext;
Mindy Pereira82cc5662012-01-09 17:29:30 -08001807 @VisibleForTesting
Mindy Pereirae011b1d2012-06-18 13:45:26 -07001808 public final SendOrSaveCallback mSendOrSaveCallback;
1809 @VisibleForTesting
1810 public final SendOrSaveMessage mSendOrSaveMessage;
mindyp44a63392012-11-05 12:05:16 -08001811 private ReplyFromAccount mExistingDraftAccount;
Mindy Pereira82cc5662012-01-09 17:29:30 -08001812
1813 public SendOrSaveTask(Context context, SendOrSaveMessage message,
mindyp44a63392012-11-05 12:05:16 -08001814 SendOrSaveCallback callback, ReplyFromAccount draftAccount) {
Mindy Pereira82cc5662012-01-09 17:29:30 -08001815 mContext = context;
1816 mSendOrSaveCallback = callback;
1817 mSendOrSaveMessage = message;
mindyp44a63392012-11-05 12:05:16 -08001818 mExistingDraftAccount = draftAccount;
Mindy Pereira82cc5662012-01-09 17:29:30 -08001819 }
1820
1821 @Override
1822 public void run() {
Mindy Pereira7ed1c112012-01-18 10:59:25 -08001823 final SendOrSaveMessage sendOrSaveMessage = mSendOrSaveMessage;
Mindy Pereira82cc5662012-01-09 17:29:30 -08001824
Mindy Pereira92551d02012-04-05 11:31:12 -07001825 final ReplyFromAccount selectedAccount = sendOrSaveMessage.mAccount;
Mindy Pereira7ed1c112012-01-18 10:59:25 -08001826 Message message = mSendOrSaveCallback.getMessage();
1827 long messageId = message != null ? message.id : UIProvider.INVALID_MESSAGE_ID;
Mindy Pereira82cc5662012-01-09 17:29:30 -08001828 // If a previous draft has been saved, in an account that is different
1829 // than what the user wants to send from, remove the old draft, and treat this
1830 // as a new message
mindyp44a63392012-11-05 12:05:16 -08001831 if (mExistingDraftAccount != null
1832 && !selectedAccount.account.uri.equals(mExistingDraftAccount.account.uri)) {
Mindy Pereira82cc5662012-01-09 17:29:30 -08001833 if (messageId != UIProvider.INVALID_MESSAGE_ID) {
1834 ContentResolver resolver = mContext.getContentResolver();
1835 ContentValues values = new ContentValues();
1836 values.put(BaseColumns._ID, messageId);
mindypfebd2262012-11-13 17:45:09 -08001837 if (mExistingDraftAccount.account.expungeMessageUri != null) {
1838 new ContentProviderTask.UpdateTask()
1839 .run(resolver, mExistingDraftAccount.account.expungeMessageUri,
1840 values, null, null);
Mindy Pereiracfb7f332012-02-28 10:23:43 -08001841 } else {
1842 // TODO(mindyp) delete the conversation.
Mindy Pereiraeaea9f12012-01-10 15:05:27 -08001843 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08001844 // reset messageId to 0, so a new message will be created
1845 messageId = UIProvider.INVALID_MESSAGE_ID;
1846 }
1847 }
1848
1849 final long messageIdToSave = messageId;
Paul Westbrook72e2ea82012-10-22 16:25:22 -07001850 sendOrSaveMessage(messageIdToSave, sendOrSaveMessage, selectedAccount, message);
1851
1852 if (!sendOrSaveMessage.mSave) {
1853 UIProvider.incrementRecipientsTimesContacted(mContext,
1854 (String) sendOrSaveMessage.mValues.get(UIProvider.MessageColumns.TO));
1855 UIProvider.incrementRecipientsTimesContacted(mContext,
1856 (String) sendOrSaveMessage.mValues.get(UIProvider.MessageColumns.CC));
1857 UIProvider.incrementRecipientsTimesContacted(mContext,
1858 (String) sendOrSaveMessage.mValues.get(UIProvider.MessageColumns.BCC));
1859 }
1860 mSendOrSaveCallback.sendOrSaveFinished(SendOrSaveTask.this, true);
1861 }
1862
1863 /**
1864 * Send or Save a message.
1865 */
1866 private void sendOrSaveMessage(long messageIdToSave, SendOrSaveMessage sendOrSaveMessage,
1867 ReplyFromAccount selectedAccount, Message message) {
1868 final ContentResolver resolver = mContext.getContentResolver();
1869 final boolean updateExistingMessage = messageIdToSave != UIProvider.INVALID_MESSAGE_ID;
1870
1871 final String accountMethod = sendOrSaveMessage.mSave ?
1872 UIProvider.AccountCallMethods.SAVE_MESSAGE :
1873 UIProvider.AccountCallMethods.SEND_MESSAGE;
1874
Paul Westbrook3c7f94d2012-10-23 14:13:00 -07001875 try {
1876 if (updateExistingMessage) {
1877 sendOrSaveMessage.mValues.put(BaseColumns._ID, messageIdToSave);
Paul Westbrook72e2ea82012-10-22 16:25:22 -07001878
Paul Westbrook3c7f94d2012-10-23 14:13:00 -07001879 final Bundle result = callAccountSendSaveMethod(resolver,
1880 selectedAccount.account, accountMethod, sendOrSaveMessage);
1881 if (result == null) {
1882 // TODO(pwestbro): Once Email supports the call api, remove this block
1883 // If null was returned, then the provider didn't handle the call method
1884 final Uri updateUri = Uri.parse(sendOrSaveMessage.mSave ?
1885 message.saveUri : message.sendUri);
1886 resolver.update(updateUri, sendOrSaveMessage.mValues, null, null);
1887 }
Paul Westbrook72e2ea82012-10-22 16:25:22 -07001888 } else {
Paul Westbrook3c7f94d2012-10-23 14:13:00 -07001889 final Uri messageUri;
1890 final Bundle result = callAccountSendSaveMethod(resolver,
1891 selectedAccount.account, accountMethod, sendOrSaveMessage);
1892 if (result != null) {
1893 // If a non-null value was returned, then the provider handled the call
1894 // method
1895 messageUri = result.getParcelable(UIProvider.MessageColumns.URI);
1896 } else {
1897 // TODO(pwestbro): Once Email supports the call api, remove this block
1898 messageUri = resolver.insert(
1899 sendOrSaveMessage.mSave ? selectedAccount.account.saveDraftUri
1900 : selectedAccount.account.sendMessageUri,
1901 sendOrSaveMessage.mValues);
1902 }
1903 if (sendOrSaveMessage.mSave && messageUri != null) {
1904 final Cursor messageCursor = resolver.query(messageUri,
1905 UIProvider.MESSAGE_PROJECTION, null, null, null);
1906 if (messageCursor != null) {
1907 try {
1908 if (messageCursor.moveToFirst()) {
1909 // Broadcast notification that a new message has
1910 // been allocated
1911 mSendOrSaveCallback.notifyMessageIdAllocated(sendOrSaveMessage,
1912 new Message(messageCursor));
1913 }
1914 } finally {
1915 messageCursor.close();
Paul Westbrookba558482012-03-19 11:00:24 -07001916 }
Paul Westbrook3c7f94d2012-10-23 14:13:00 -07001917 }
1918 }
1919 }
1920 } finally {
1921 // Close any opened file descriptors
1922 closeOpenedAttachmentFds(sendOrSaveMessage);
1923 }
1924 }
1925
1926 private void closeOpenedAttachmentFds(SendOrSaveMessage sendOrSaveMessage) {
1927 final Bundle openedFds = sendOrSaveMessage.attachmentFds();
1928 if (openedFds != null) {
1929 final Set<String> keys = openedFds.keySet();
1930 for (String key : keys) {
1931 final ParcelFileDescriptor fd = openedFds.getParcelable(key);
1932 if (fd != null) {
1933 try {
1934 fd.close();
1935 } catch (IOException e) {
1936 // Do nothing
Paul Westbrookba558482012-03-19 11:00:24 -07001937 }
Mindy Pereira7ed1c112012-01-18 10:59:25 -08001938 }
1939 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08001940 }
Paul Westbrook72e2ea82012-10-22 16:25:22 -07001941 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08001942
Paul Westbrook72e2ea82012-10-22 16:25:22 -07001943 /**
1944 * Use the {@link ContentResolver#call()} method to send or save the message.
1945 *
1946 * If this was successful, this method will return an non-null Bundle instance
1947 */
1948 private Bundle callAccountSendSaveMethod(ContentResolver resolver, Account account,
1949 String method, SendOrSaveMessage sendOrSaveMessage) {
1950 // Copy all of the values from the content values to the bundle
1951 final Bundle methodExtras = new Bundle(sendOrSaveMessage.mValues.size());
1952 final Set<Entry<String, Object>> valueSet = sendOrSaveMessage.mValues.valueSet();
1953
1954 for (Entry<String, Object> entry : valueSet) {
1955 final Object entryValue = entry.getValue();
1956 final String key = entry.getKey();
1957 if (entryValue instanceof String) {
1958 methodExtras.putString(key, (String)entryValue);
1959 } else if (entryValue instanceof Boolean) {
1960 methodExtras.putBoolean(key, (Boolean)entryValue);
1961 } else if (entryValue instanceof Integer) {
1962 methodExtras.putInt(key, (Integer)entryValue);
1963 } else if (entryValue instanceof Long) {
1964 methodExtras.putLong(key, (Long)entryValue);
1965 } else {
1966 LogUtils.wtf(LOG_TAG, "Unexpected object type: %s",
1967 entryValue.getClass().getName());
1968 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08001969 }
Paul Westbrook72e2ea82012-10-22 16:25:22 -07001970
Paul Westbrook3c7f94d2012-10-23 14:13:00 -07001971 // If the SendOrSaveMessage has some opened fds, add them to the bundle
1972 final Bundle fdMap = sendOrSaveMessage.attachmentFds();
1973 if (fdMap != null) {
1974 methodExtras.putParcelable(
1975 UIProvider.SendOrSaveMethodParamKeys.OPENED_FD_MAP, fdMap);
1976 }
1977
Paul Westbrook72e2ea82012-10-22 16:25:22 -07001978 return resolver.call(account.uri, method, account.uri.toString(), methodExtras);
Mindy Pereira82cc5662012-01-09 17:29:30 -08001979 }
1980 }
1981
1982 // Array of the outstanding send or save tasks. Access is synchronized
1983 // with the object itself
1984 /* package for testing */
Mindy Pereirae011b1d2012-06-18 13:45:26 -07001985 @VisibleForTesting
1986 public ArrayList<SendOrSaveTask> mActiveTasks = Lists.newArrayList();
Andy Huang1f8f4dd2012-10-25 21:35:35 -07001987 // FIXME: this variable is never read. related to sRequestMessageIdMap.
Mindy Pereira82cc5662012-01-09 17:29:30 -08001988 private int mRequestId;
Mindy Pereirabdf7a402012-03-01 15:23:26 -08001989 private String mSignature;
Mindy Pereirab199d172012-08-13 11:04:03 -07001990 private Account[] mAccounts;
Mindy Pereira82cc5662012-01-09 17:29:30 -08001991
Mindy Pereirae011b1d2012-06-18 13:45:26 -07001992 @VisibleForTesting
1993 public static class SendOrSaveMessage {
Mindy Pereira92551d02012-04-05 11:31:12 -07001994 final ReplyFromAccount mAccount;
Mindy Pereira82cc5662012-01-09 17:29:30 -08001995 final ContentValues mValues;
Mindy Pereira3ce64e72012-01-13 14:29:45 -08001996 final String mRefMessageId;
Mindy Pereirae011b1d2012-06-18 13:45:26 -07001997 @VisibleForTesting
1998 public final boolean mSave;
Mindy Pereira82cc5662012-01-09 17:29:30 -08001999 final int mRequestId;
Paul Westbrook3c7f94d2012-10-23 14:13:00 -07002000 private final Bundle mAttachmentFds;
Mindy Pereira82cc5662012-01-09 17:29:30 -08002001
Paul Westbrook3c7f94d2012-10-23 14:13:00 -07002002 public SendOrSaveMessage(Context context, ReplyFromAccount account, ContentValues values,
2003 String refMessageId, List<Attachment> attachments, boolean save) {
Mindy Pereira82cc5662012-01-09 17:29:30 -08002004 mAccount = account;
Mindy Pereira82cc5662012-01-09 17:29:30 -08002005 mValues = values;
2006 mRefMessageId = refMessageId;
2007 mSave = save;
2008 mRequestId = mValues.hashCode() ^ hashCode();
Paul Westbrook3c7f94d2012-10-23 14:13:00 -07002009
2010 mAttachmentFds = initializeAttachmentFds(context, attachments);
Mindy Pereira82cc5662012-01-09 17:29:30 -08002011 }
2012
2013 int requestId() {
2014 return mRequestId;
2015 }
Paul Westbrook3c7f94d2012-10-23 14:13:00 -07002016
2017 Bundle attachmentFds() {
2018 return mAttachmentFds;
2019 }
2020
2021 /**
2022 * Opens {@link ParcelFileDescriptor} for each of the attachments. This method must be
2023 * called before the ComposeActivity finishes.
2024 * Note: The caller is responsible for closing these file descriptors.
2025 */
2026 private Bundle initializeAttachmentFds(Context context, List<Attachment> attachments) {
2027 if (attachments == null || attachments.size() == 0) {
2028 return null;
2029 }
2030
2031 final Bundle result = new Bundle(attachments.size());
2032 final ContentResolver resolver = context.getContentResolver();
2033
2034 for (Attachment attachment : attachments) {
2035 if (attachment == null || Utils.isEmpty(attachment.contentUri)) {
2036 continue;
2037 }
2038
2039 ParcelFileDescriptor fileDescriptor;
2040 try {
2041 fileDescriptor = resolver.openFileDescriptor(attachment.contentUri, "r");
2042 } catch (FileNotFoundException e) {
2043 LogUtils.e(LOG_TAG, e, "Exception attempting to open attachment");
2044 fileDescriptor = null;
2045 }
2046
2047 if (fileDescriptor != null) {
2048 result.putParcelable(attachment.contentUri.toString(), fileDescriptor);
2049 }
2050 }
2051
2052 return result;
2053 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08002054 }
2055
2056 /**
2057 * Get the to recipients.
2058 */
2059 public String[] getToAddresses() {
2060 return getAddressesFromList(mTo);
2061 }
2062
2063 /**
2064 * Get the cc recipients.
2065 */
2066 public String[] getCcAddresses() {
2067 return getAddressesFromList(mCc);
2068 }
2069
2070 /**
2071 * Get the bcc recipients.
2072 */
2073 public String[] getBccAddresses() {
2074 return getAddressesFromList(mBcc);
2075 }
2076
2077 public String[] getAddressesFromList(RecipientEditTextView list) {
2078 if (list == null) {
2079 return new String[0];
2080 }
2081 Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(list.getText());
2082 int count = tokens.length;
2083 String[] result = new String[count];
2084 for (int i = 0; i < count; i++) {
2085 result[i] = tokens[i].toString();
2086 }
2087 return result;
2088 }
2089
2090 /**
2091 * Check for invalid email addresses.
2092 * @param to String array of email addresses to check.
2093 * @param wrongEmailsOut Emails addresses that were invalid.
2094 */
2095 public void checkInvalidEmails(String[] to, List<String> wrongEmailsOut) {
Mindy Pereirae5f20bf2012-06-25 14:20:40 -07002096 if (mValidator == null) {
2097 return;
2098 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08002099 for (String email : to) {
2100 if (!mValidator.isValid(email)) {
2101 wrongEmailsOut.add(email);
2102 }
2103 }
2104 }
2105
2106 /**
2107 * Show an error because the user has entered an invalid recipient.
2108 * @param message
2109 */
2110 public void showRecipientErrorDialog(String message) {
2111 // Only 1 invalid recipients error dialog should be allowed up at a
2112 // time.
2113 if (mRecipientErrorDialog != null) {
2114 mRecipientErrorDialog.dismiss();
2115 }
2116 mRecipientErrorDialog = new AlertDialog.Builder(this).setMessage(message).setTitle(
2117 R.string.recipient_error_dialog_title)
2118 .setIconAttribute(android.R.attr.alertDialogIcon)
Mindy Pereira82cc5662012-01-09 17:29:30 -08002119 .setPositiveButton(
2120 R.string.ok, new Dialog.OnClickListener() {
Marc Blank0bbc8582012-04-23 15:07:57 -07002121 @Override
Mindy Pereira82cc5662012-01-09 17:29:30 -08002122 public void onClick(DialogInterface dialog, int which) {
2123 // after the user dismisses the recipient error
2124 // dialog we want to make sure to refocus the
2125 // recipient to field so they can fix the issue
2126 // easily
2127 if (mTo != null) {
2128 mTo.requestFocus();
2129 }
2130 mRecipientErrorDialog = null;
2131 }
2132 }).show();
2133 }
2134
2135 /**
2136 * Update the state of the UI based on whether or not the current draft
2137 * needs to be saved and the message is not empty.
2138 */
Mindy Pereiraeaea9f12012-01-10 15:05:27 -08002139 public void updateSaveUi() {
Mindy Pereira82cc5662012-01-09 17:29:30 -08002140 if (mSave != null) {
2141 mSave.setEnabled((shouldSave() && !isBlank()));
2142 }
2143 }
2144
2145 /**
2146 * Returns true if we need to save the current draft.
2147 */
2148 private boolean shouldSave() {
Mindy Pereira7ed1c112012-01-18 10:59:25 -08002149 synchronized (mDraftLock) {
Mindy Pereira82cc5662012-01-09 17:29:30 -08002150 // The message should only be saved if:
2151 // It hasn't been sent AND
2152 // Some text has been added to the message OR
2153 // an attachment has been added or removed
Mindy Pereiraa2148332012-07-02 13:54:14 -07002154 // AND there is actually something in the draft to save.
Andy Huangd47877e2012-08-09 19:31:24 -07002155 return (mTextChanged || mAttachmentsChanged || mReplyFromChanged)
Mindy Pereiraa2148332012-07-02 13:54:14 -07002156 && !isBlank();
Mindy Pereira82cc5662012-01-09 17:29:30 -08002157 }
2158 }
2159
2160 /**
Mindy Pereirabdf7a402012-03-01 15:23:26 -08002161 * Check if all fields are blank.
Mindy Pereira82cc5662012-01-09 17:29:30 -08002162 * @return boolean
2163 */
2164 public boolean isBlank() {
2165 return mSubject.getText().length() == 0
Mindy Pereirabdf7a402012-03-01 15:23:26 -08002166 && (mBodyView.getText().length() == 0 || getSignatureStartPosition(mSignature,
2167 mBodyView.getText().toString()) == 0)
2168 && mTo.length() == 0
2169 && mCc.length() == 0 && mBcc.length() == 0
2170 && mAttachmentsView.getAttachments().size() == 0;
2171 }
2172
2173 @VisibleForTesting
2174 protected int getSignatureStartPosition(String signature, String bodyText) {
2175 int startPos = -1;
2176
2177 if (TextUtils.isEmpty(signature) || TextUtils.isEmpty(bodyText)) {
2178 return startPos;
2179 }
2180
2181 int bodyLength = bodyText.length();
2182 int signatureLength = signature.length();
2183 String printableVersion = convertToPrintableSignature(signature);
2184 int printableLength = printableVersion.length();
2185
2186 if (bodyLength >= printableLength
2187 && bodyText.substring(bodyLength - printableLength)
2188 .equals(printableVersion)) {
2189 startPos = bodyLength - printableLength;
2190 } else if (bodyLength >= signatureLength
2191 && bodyText.substring(bodyLength - signatureLength)
2192 .equals(signature)) {
2193 startPos = bodyLength - signatureLength;
2194 }
2195 return startPos;
Mindy Pereira82cc5662012-01-09 17:29:30 -08002196 }
2197
2198 /**
2199 * Allows any changes made by the user to be ignored. Called when the user
2200 * decides to discard a draft.
2201 */
2202 private void discardChanges() {
2203 mTextChanged = false;
Mindy Pereiraeaea9f12012-01-10 15:05:27 -08002204 mAttachmentsChanged = false;
Mindy Pereira82cc5662012-01-09 17:29:30 -08002205 mReplyFromChanged = false;
2206 }
2207
2208 /**
Mindy Pereira181df782012-03-01 13:32:44 -08002209 * @param body
2210 * @param save
2211 * @param showToast
2212 * @return Whether the send or save succeeded.
2213 */
2214 protected boolean sendOrSaveWithSanityChecks(final boolean save, final boolean showToast,
Mark Weidd19b632012-10-19 13:59:28 -07002215 final boolean orientationChanged, final boolean autoSend) {
Mark Wei009b3712012-10-18 18:07:50 -07002216 if (mAccounts == null || mAccount == null) {
2217 Toast.makeText(this, R.string.send_failed, Toast.LENGTH_SHORT).show();
Mark Weidd19b632012-10-19 13:59:28 -07002218 if (autoSend) {
2219 finish();
2220 }
Mark Wei009b3712012-10-18 18:07:50 -07002221 return false;
2222 }
2223
Mindy Pereira181df782012-03-01 13:32:44 -08002224 String[] to, cc, bcc;
2225 Editable body = mBodyView.getEditableText();
Mindy Pereira181df782012-03-01 13:32:44 -08002226 if (orientationChanged) {
2227 to = cc = bcc = new String[0];
2228 } else {
2229 to = getToAddresses();
2230 cc = getCcAddresses();
2231 bcc = getBccAddresses();
2232 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08002233
Mindy Pereira181df782012-03-01 13:32:44 -08002234 // Don't let the user send to nobody (but it's okay to save a message
2235 // with no recipients)
2236 if (!save && (to.length == 0 && cc.length == 0 && bcc.length == 0)) {
2237 showRecipientErrorDialog(getString(R.string.recipient_needed));
2238 return false;
2239 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08002240
Mindy Pereira181df782012-03-01 13:32:44 -08002241 List<String> wrongEmails = new ArrayList<String>();
2242 if (!save) {
2243 checkInvalidEmails(to, wrongEmails);
2244 checkInvalidEmails(cc, wrongEmails);
2245 checkInvalidEmails(bcc, wrongEmails);
2246 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08002247
Mindy Pereira181df782012-03-01 13:32:44 -08002248 // Don't let the user send an email with invalid recipients
2249 if (wrongEmails.size() > 0) {
2250 String errorText = String.format(getString(R.string.invalid_recipient),
2251 wrongEmails.get(0));
2252 showRecipientErrorDialog(errorText);
2253 return false;
2254 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08002255
Mindy Pereira181df782012-03-01 13:32:44 -08002256 DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
Marc Blank0bbc8582012-04-23 15:07:57 -07002257 @Override
Mindy Pereira181df782012-03-01 13:32:44 -08002258 public void onClick(DialogInterface dialog, int which) {
2259 sendOrSave(mBodyView.getEditableText(), save, showToast, orientationChanged);
2260 }
2261 };
Mindy Pereira82cc5662012-01-09 17:29:30 -08002262
Mindy Pereira181df782012-03-01 13:32:44 -08002263 // Show a warning before sending only if there are no attachments.
2264 if (!save) {
2265 if (mAttachmentsView.getAttachments().isEmpty() && showEmptyTextWarnings()) {
2266 boolean warnAboutEmptySubject = isSubjectEmpty();
2267 boolean emptyBody = TextUtils.getTrimmedLength(body) == 0;
Mindy Pereira82cc5662012-01-09 17:29:30 -08002268
Mindy Pereira181df782012-03-01 13:32:44 -08002269 // A warning about an empty body may not be warranted when
2270 // forwarding mails, since a common use case is to forward
2271 // quoted text and not append any more text.
2272 boolean warnAboutEmptyBody = emptyBody && (!mForward || isBodyEmpty());
Mindy Pereira82cc5662012-01-09 17:29:30 -08002273
Mindy Pereira181df782012-03-01 13:32:44 -08002274 // When we bring up a dialog warning the user about a send,
2275 // assume that they accept sending the message. If they do not,
2276 // the dialog listener is required to enable sending again.
2277 if (warnAboutEmptySubject) {
2278 showSendConfirmDialog(R.string.confirm_send_message_with_no_subject, listener);
2279 return true;
2280 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08002281
Mindy Pereira181df782012-03-01 13:32:44 -08002282 if (warnAboutEmptyBody) {
2283 showSendConfirmDialog(R.string.confirm_send_message_with_no_body, listener);
2284 return true;
2285 }
2286 }
2287 // Ask for confirmation to send (if always required)
2288 if (showSendConfirmation()) {
2289 showSendConfirmDialog(R.string.confirm_send_message, listener);
2290 return true;
2291 }
2292 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08002293
Mindy Pereira181df782012-03-01 13:32:44 -08002294 sendOrSave(body, save, showToast, false);
2295 return true;
2296 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08002297
Mindy Pereira181df782012-03-01 13:32:44 -08002298 /**
2299 * Returns a boolean indicating whether warnings should be shown for empty
2300 * subject and body fields
Andy Huang5c5fd572012-04-08 18:19:29 -07002301 *
Mindy Pereira181df782012-03-01 13:32:44 -08002302 * @return True if a warning should be shown for empty text fields
2303 */
2304 protected boolean showEmptyTextWarnings() {
2305 return mAttachmentsView.getAttachments().size() == 0;
2306 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08002307
Mindy Pereira181df782012-03-01 13:32:44 -08002308 /**
2309 * Returns a boolean indicating whether the user should confirm each send
2310 *
2311 * @return True if a warning should be on each send
2312 */
2313 protected boolean showSendConfirmation() {
2314 return mCachedSettings != null ? mCachedSettings.confirmSend : false;
2315 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08002316
Mindy Pereira181df782012-03-01 13:32:44 -08002317 private void showSendConfirmDialog(int messageId, DialogInterface.OnClickListener listener) {
2318 if (mSendConfirmDialog != null) {
2319 mSendConfirmDialog.dismiss();
2320 mSendConfirmDialog = null;
2321 }
2322 mSendConfirmDialog = new AlertDialog.Builder(this).setMessage(messageId)
2323 .setTitle(R.string.confirm_send_title)
2324 .setIconAttribute(android.R.attr.alertDialogIcon)
2325 .setPositiveButton(R.string.send, listener)
Mindy Pereira6edd5972012-06-19 10:22:36 -07002326 .setNegativeButton(R.string.cancel, this)
2327 .show();
Mindy Pereira181df782012-03-01 13:32:44 -08002328 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08002329
Mindy Pereira181df782012-03-01 13:32:44 -08002330 /**
2331 * Returns whether the ComposeArea believes there is any text in the body of
2332 * the composition. TODO: When ComposeArea controls the Body as well, add
2333 * that here.
2334 */
2335 public boolean isBodyEmpty() {
2336 return !mQuotedTextView.isTextIncluded();
2337 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08002338
Mindy Pereira181df782012-03-01 13:32:44 -08002339 /**
2340 * Test to see if the subject is empty.
2341 *
2342 * @return boolean.
2343 */
2344 // TODO: this will likely go away when composeArea.focus() is implemented
2345 // after all the widget control is moved over.
2346 public boolean isSubjectEmpty() {
2347 return TextUtils.getTrimmedLength(mSubject.getText()) == 0;
2348 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08002349
Mindy Pereira181df782012-03-01 13:32:44 -08002350 /* package */
Mindy Pereirae8f94dc2012-04-16 11:56:21 -07002351 static int sendOrSaveInternal(Context context, ReplyFromAccount replyFromAccount,
Paul Westbrook05b92b82012-04-20 13:29:37 -07002352 Message message, final Message refMessage, Spanned body, final CharSequence quotedText,
mindyp44a63392012-11-05 12:05:16 -08002353 SendOrSaveCallback callback, Handler handler, boolean save, int composeMode,
2354 ReplyFromAccount draftAccount) {
Mindy Pereira29ef1b82012-01-13 11:26:21 -08002355 ContentValues values = new ContentValues();
Mindy Pereira82cc5662012-01-09 17:29:30 -08002356
Mindy Pereirac2031972012-04-03 09:38:35 -07002357 String refMessageId = refMessage != null ? refMessage.uri.toString() : "";
2358
Mindy Pereirae8f94dc2012-04-16 11:56:21 -07002359 MessageModification.putToAddresses(values, message.getToAddresses());
2360 MessageModification.putCcAddresses(values, message.getCcAddresses());
2361 MessageModification.putBccAddresses(values, message.getBccAddresses());
Mindy Pereira82cc5662012-01-09 17:29:30 -08002362
Scott Kennedy8960f0a2012-11-07 15:35:50 -08002363 MessageModification.putCustomFromAddress(values, message.getFrom());
Mindy Pereira92551d02012-04-05 11:31:12 -07002364
Mindy Pereirae8f94dc2012-04-16 11:56:21 -07002365 MessageModification.putSubject(values, message.subject);
mindyped9c2f02012-10-12 10:02:08 -07002366 String htmlBody = Html.toHtml(new SpannableString(body.toString()));
Paul Westbrook05b92b82012-04-20 13:29:37 -07002367
Mindy Pereira29ef1b82012-01-13 11:26:21 -08002368 boolean includeQuotedText = !TextUtils.isEmpty(quotedText);
2369 StringBuilder fullBody = new StringBuilder(htmlBody);
2370 if (includeQuotedText) {
Mindy Pereirae8caf122012-03-20 15:23:31 -07002371 // HTML gets converted to text for now
2372 final String text = quotedText.toString();
2373 if (QuotedTextView.containsQuotedText(text)) {
2374 int pos = QuotedTextView.getQuotedTextOffset(text);
Paul Westbrook55271cf2012-04-20 16:25:02 -07002375 final int quoteStartPos = fullBody.length() + pos;
2376 fullBody.append(text);
2377 MessageModification.putQuoteStartPos(values, quoteStartPos);
Mindy Pereira12575862012-03-21 16:30:54 -07002378 MessageModification.putForward(values, composeMode == ComposeActivity.FORWARD);
Mindy Pereirae8caf122012-03-20 15:23:31 -07002379 MessageModification.putAppendRefMessageContent(values, includeQuotedText);
Mindy Pereira29ef1b82012-01-13 11:26:21 -08002380 } else {
Mindy Pereirae8caf122012-03-20 15:23:31 -07002381 LogUtils.w(LOG_TAG, "Couldn't find quoted text");
2382 // This shouldn't happen, but just use what we have,
2383 // and don't do server-side expansion
2384 fullBody.append(text);
Mindy Pereira29ef1b82012-01-13 11:26:21 -08002385 }
2386 }
Mindy Pereira002ff522012-05-30 10:31:26 -07002387 int draftType = getDraftType(composeMode);
Mindy Pereira12575862012-03-21 16:30:54 -07002388 MessageModification.putDraftType(values, draftType);
Mindy Pereirac6f1e2a2012-04-04 10:33:45 -07002389 if (refMessage != null) {
2390 if (!TextUtils.isEmpty(refMessage.bodyHtml)) {
2391 MessageModification.putBodyHtml(values, fullBody.toString());
2392 }
2393 if (!TextUtils.isEmpty(refMessage.bodyText)) {
mindypc59dd822012-11-13 10:56:21 -08002394 MessageModification.putBody(values,
2395 Utils.convertHtmlToPlainText(fullBody.toString()).toString());
Mindy Pereirac6f1e2a2012-04-04 10:33:45 -07002396 }
2397 } else {
Mindy Pereirac2031972012-04-03 09:38:35 -07002398 MessageModification.putBodyHtml(values, fullBody.toString());
mindypc59dd822012-11-13 10:56:21 -08002399 MessageModification.putBody(values, Utils.convertHtmlToPlainText(fullBody.toString())
2400 .toString());
Mindy Pereirac2031972012-04-03 09:38:35 -07002401 }
Mindy Pereirae8f94dc2012-04-16 11:56:21 -07002402 MessageModification.putAttachments(values, message.getAttachments());
Mindy Pereira12575862012-03-21 16:30:54 -07002403 if (!TextUtils.isEmpty(refMessageId)) {
2404 MessageModification.putRefMessageId(values, refMessageId);
2405 }
Paul Westbrook3c7f94d2012-10-23 14:13:00 -07002406 SendOrSaveMessage sendOrSaveMessage = new SendOrSaveMessage(context, replyFromAccount,
2407 values, refMessageId, message.getAttachments(), save);
mindyp44a63392012-11-05 12:05:16 -08002408 SendOrSaveTask sendOrSaveTask = new SendOrSaveTask(context, sendOrSaveMessage, callback,
2409 draftAccount);
Mindy Pereira82cc5662012-01-09 17:29:30 -08002410
Mindy Pereira181df782012-03-01 13:32:44 -08002411 callback.initializeSendOrSave(sendOrSaveTask);
Mindy Pereira181df782012-03-01 13:32:44 -08002412 // Do the send/save action on the specified handler to avoid possible
2413 // ANRs
2414 handler.post(sendOrSaveTask);
Mindy Pereira82cc5662012-01-09 17:29:30 -08002415
Mindy Pereira181df782012-03-01 13:32:44 -08002416 return sendOrSaveMessage.requestId();
2417 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08002418
Mindy Pereira002ff522012-05-30 10:31:26 -07002419 private static int getDraftType(int mode) {
2420 int draftType = -1;
2421 switch (mode) {
2422 case ComposeActivity.COMPOSE:
2423 draftType = DraftType.COMPOSE;
2424 break;
2425 case ComposeActivity.REPLY:
2426 draftType = DraftType.REPLY;
2427 break;
2428 case ComposeActivity.REPLY_ALL:
2429 draftType = DraftType.REPLY_ALL;
2430 break;
2431 case ComposeActivity.FORWARD:
2432 draftType = DraftType.FORWARD;
2433 break;
2434 }
2435 return draftType;
2436 }
2437
Mindy Pereira181df782012-03-01 13:32:44 -08002438 private void sendOrSave(Spanned body, boolean save, boolean showToast,
2439 boolean orientationChanged) {
2440 // Check if user is a monkey. Monkeys can compose and hit send
2441 // button but are not allowed to send anything off the device.
Paul Westbrook3ae824c2012-04-06 13:29:39 -07002442 if (ActivityManager.isUserAMonkey()) {
Mindy Pereira181df782012-03-01 13:32:44 -08002443 return;
2444 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08002445
Mindy Pereira181df782012-03-01 13:32:44 -08002446 String[] to, cc, bcc;
2447 if (orientationChanged) {
2448 to = cc = bcc = new String[0];
2449 } else {
2450 to = getToAddresses();
2451 cc = getCcAddresses();
2452 bcc = getBccAddresses();
2453 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08002454
Mindy Pereira181df782012-03-01 13:32:44 -08002455 SendOrSaveCallback callback = new SendOrSaveCallback() {
Andy Huang1f8f4dd2012-10-25 21:35:35 -07002456 // FIXME: unused
Mindy Pereira82cc5662012-01-09 17:29:30 -08002457 private int mRestoredRequestId;
2458
Marc Blank0bbc8582012-04-23 15:07:57 -07002459 @Override
Mindy Pereira82cc5662012-01-09 17:29:30 -08002460 public void initializeSendOrSave(SendOrSaveTask sendOrSaveTask) {
Mindy Pereira181df782012-03-01 13:32:44 -08002461 synchronized (mActiveTasks) {
2462 int numTasks = mActiveTasks.size();
2463 if (numTasks == 0) {
2464 // Start service so we won't be killed if this app is
2465 // put in the background.
2466 startService(new Intent(ComposeActivity.this, EmptyService.class));
2467 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08002468
Mindy Pereira181df782012-03-01 13:32:44 -08002469 mActiveTasks.add(sendOrSaveTask);
2470 }
2471 if (sTestSendOrSaveCallback != null) {
2472 sTestSendOrSaveCallback.initializeSendOrSave(sendOrSaveTask);
2473 }
2474 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08002475
Marc Blank0bbc8582012-04-23 15:07:57 -07002476 @Override
Mindy Pereira7ed1c112012-01-18 10:59:25 -08002477 public void notifyMessageIdAllocated(SendOrSaveMessage sendOrSaveMessage,
2478 Message message) {
Mindy Pereira181df782012-03-01 13:32:44 -08002479 synchronized (mDraftLock) {
mindyp44a63392012-11-05 12:05:16 -08002480 mDraftAccount = sendOrSaveMessage.mAccount;
Mindy Pereira181df782012-03-01 13:32:44 -08002481 mDraftId = message.id;
2482 mDraft = message;
Mindy Pereira7ed1c112012-01-18 10:59:25 -08002483 if (sRequestMessageIdMap != null) {
2484 sRequestMessageIdMap.put(sendOrSaveMessage.requestId(), mDraftId);
2485 }
Mindy Pereira181df782012-03-01 13:32:44 -08002486 // Cache request message map, in case the process is killed
2487 saveRequestMap();
2488 }
2489 if (sTestSendOrSaveCallback != null) {
Mindy Pereira7ed1c112012-01-18 10:59:25 -08002490 sTestSendOrSaveCallback.notifyMessageIdAllocated(sendOrSaveMessage, message);
Mindy Pereira181df782012-03-01 13:32:44 -08002491 }
2492 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08002493
Marc Blank0bbc8582012-04-23 15:07:57 -07002494 @Override
Mindy Pereira7ed1c112012-01-18 10:59:25 -08002495 public Message getMessage() {
2496 synchronized (mDraftLock) {
2497 return mDraft;
2498 }
2499 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08002500
Marc Blank0bbc8582012-04-23 15:07:57 -07002501 @Override
Mindy Pereira7ed1c112012-01-18 10:59:25 -08002502 public void sendOrSaveFinished(SendOrSaveTask task, boolean success) {
Mindy Pereira47d0e652012-07-23 09:45:07 -07002503 // Update the last sent from account.
2504 if (mAccount != null) {
2505 MailAppProvider.getInstance().setLastSentFromAccount(mAccount.uri.toString());
2506 }
Mindy Pereira7ed1c112012-01-18 10:59:25 -08002507 if (success) {
2508 // Successfully sent or saved so reset change markers
2509 discardChanges();
2510 } else {
2511 // A failure happened with saving/sending the draft
2512 // TODO(pwestbro): add a better string that should be used
2513 // when failing to send or save
2514 Toast.makeText(ComposeActivity.this, R.string.send_failed, Toast.LENGTH_SHORT)
2515 .show();
2516 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08002517
Mindy Pereira7ed1c112012-01-18 10:59:25 -08002518 int numTasks;
2519 synchronized (mActiveTasks) {
2520 // Remove the task from the list of active tasks
2521 mActiveTasks.remove(task);
2522 numTasks = mActiveTasks.size();
2523 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08002524
Mindy Pereira7ed1c112012-01-18 10:59:25 -08002525 if (numTasks == 0) {
2526 // Stop service so we can be killed.
2527 stopService(new Intent(ComposeActivity.this, EmptyService.class));
2528 }
2529 if (sTestSendOrSaveCallback != null) {
2530 sTestSendOrSaveCallback.sendOrSaveFinished(task, success);
2531 }
2532 }
Mindy Pereira181df782012-03-01 13:32:44 -08002533 };
Mindy Pereira82cc5662012-01-09 17:29:30 -08002534
Mindy Pereira181df782012-03-01 13:32:44 -08002535 // Get the selected account if the from spinner has been setup.
Mindy Pereira92551d02012-04-05 11:31:12 -07002536 ReplyFromAccount selectedAccount = mReplyFromAccount;
Mindy Pereira181df782012-03-01 13:32:44 -08002537 String fromAddress = selectedAccount.name;
2538 if (selectedAccount == null || fromAddress == null) {
2539 // We don't have either the selected account or from address,
2540 // use mAccount.
Mindy Pereira92551d02012-04-05 11:31:12 -07002541 selectedAccount = mReplyFromAccount;
Mindy Pereira181df782012-03-01 13:32:44 -08002542 fromAddress = mAccount.name;
2543 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08002544
Mindy Pereira181df782012-03-01 13:32:44 -08002545 if (mSendSaveTaskHandler == null) {
2546 HandlerThread handlerThread = new HandlerThread("Send Message Task Thread");
2547 handlerThread.start();
Mindy Pereira82cc5662012-01-09 17:29:30 -08002548
Mindy Pereira181df782012-03-01 13:32:44 -08002549 mSendSaveTaskHandler = new Handler(handlerThread.getLooper());
2550 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08002551
Mindy Pereirae8f94dc2012-04-16 11:56:21 -07002552 Message msg = createMessage(mReplyFromAccount, getMode());
Paul Westbrook05b92b82012-04-20 13:29:37 -07002553 mRequestId = sendOrSaveInternal(this, mReplyFromAccount, msg, mRefMessage, body,
2554 mQuotedTextView.getQuotedTextIfIncluded(), callback,
mindyp44a63392012-11-05 12:05:16 -08002555 mSendSaveTaskHandler, save, mComposeMode, mDraftAccount);
Mindy Pereira82cc5662012-01-09 17:29:30 -08002556
Mindy Pereira181df782012-03-01 13:32:44 -08002557 if (mRecipient != null && mRecipient.equals(mAccount.name)) {
2558 mRecipient = selectedAccount.name;
2559 }
Paul Westbrookb1f573c2012-04-06 11:38:28 -07002560 setAccount(selectedAccount.account);
Mindy Pereira82cc5662012-01-09 17:29:30 -08002561
Mindy Pereira181df782012-03-01 13:32:44 -08002562 // Don't display the toast if the user is just changing the orientation,
2563 // but we still need to save the draft to the cursor because this is how we restore
2564 // the attachments when the configuration change completes.
2565 if (showToast && (getChangingConfigurations() & ActivityInfo.CONFIG_ORIENTATION) == 0) {
2566 Toast.makeText(this, save ? R.string.message_saved : R.string.sending_message,
2567 Toast.LENGTH_LONG).show();
2568 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08002569
Mindy Pereira181df782012-03-01 13:32:44 -08002570 // Need to update variables here because the send or save completes
2571 // asynchronously even though the toast shows right away.
2572 discardChanges();
2573 updateSaveUi();
Mindy Pereira82cc5662012-01-09 17:29:30 -08002574
Mindy Pereira181df782012-03-01 13:32:44 -08002575 // If we are sending, finish the activity
2576 if (!save) {
2577 finish();
2578 }
2579 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08002580
Mindy Pereira181df782012-03-01 13:32:44 -08002581 /**
2582 * Save the state of the request messageid map. This allows for the Gmail
2583 * process to be killed, but and still allow for ComposeActivity instances
2584 * to be recreated correctly.
2585 */
2586 private void saveRequestMap() {
2587 // TODO: store the request map in user preferences.
2588 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08002589
Mindy Pereira2db7d4a2012-08-15 11:00:02 -07002590 private void doAttach(String type) {
Mindy Pereira013194c2012-01-06 15:09:33 -08002591 Intent i = new Intent(Intent.ACTION_GET_CONTENT);
2592 i.addCategory(Intent.CATEGORY_OPENABLE);
Paul Westbrookd6a9a3f2012-04-26 18:47:23 -07002593 i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Mindy Pereira2db7d4a2012-08-15 11:00:02 -07002594 i.setType(type);
Mindy Pereira013194c2012-01-06 15:09:33 -08002595 mAddingAttachment = true;
Mindy Pereira181df782012-03-01 13:32:44 -08002596 startActivityForResult(Intent.createChooser(i, getText(R.string.select_attachment_type)),
2597 RESULT_PICK_ATTACHMENT);
Mindy Pereira013194c2012-01-06 15:09:33 -08002598 }
2599
Mindy Pereiraec8b0ed2012-01-06 10:35:59 -08002600 private void showCcBccViews() {
Mindy Pereiraa26b54e2012-01-06 12:54:33 -08002601 mCcBccView.show(true, true, true);
Mindy Pereiraec8b0ed2012-01-06 10:35:59 -08002602 if (mCcBccButton != null) {
mindypcd0b0b92012-08-23 14:33:17 -07002603 mCcBccButton.setVisibility(View.INVISIBLE);
Mindy Pereiraec8b0ed2012-01-06 10:35:59 -08002604 }
2605 }
2606
Mindy Pereira326c6602012-01-04 15:32:42 -08002607 @Override
2608 public boolean onNavigationItemSelected(int position, long itemId) {
Mindy Pereiraa26b54e2012-01-06 12:54:33 -08002609 int initialComposeMode = mComposeMode;
Mindy Pereira326c6602012-01-04 15:32:42 -08002610 if (position == ComposeActivity.REPLY) {
2611 mComposeMode = ComposeActivity.REPLY;
2612 } else if (position == ComposeActivity.REPLY_ALL) {
2613 mComposeMode = ComposeActivity.REPLY_ALL;
2614 } else if (position == ComposeActivity.FORWARD) {
2615 mComposeMode = ComposeActivity.FORWARD;
2616 }
Mindy Pereiracbfb75a2012-06-25 14:52:23 -07002617 clearChangeListeners();
Mindy Pereiraa26b54e2012-01-06 12:54:33 -08002618 if (initialComposeMode != mComposeMode) {
Mindy Pereira154386a2012-01-11 13:02:33 -08002619 resetMessageForModeChange();
Mindy Pereiraef388302012-06-18 19:07:44 -07002620 if (mDraft == null && mRefMessage != null) {
Mindy Pereira96a7f7a2012-07-09 16:51:06 -07002621 setFieldsFromRefMessage(mComposeMode, mAccount.name);
Mindy Pereira8eca57a2012-03-20 16:42:34 -07002622 }
Mindy Pereiraef388302012-06-18 19:07:44 -07002623 boolean showCc = false;
2624 boolean showBcc = false;
2625 if (mDraft != null) {
2626 // Following desktop behavior, if the user has added a BCC
2627 // field to a draft, we show it regardless of compose mode.
Scott Kennedy8960f0a2012-11-07 15:35:50 -08002628 showBcc = !TextUtils.isEmpty(mDraft.getBcc());
Mindy Pereiraef388302012-06-18 19:07:44 -07002629 // Use the draft to determine what to populate.
2630 // If the Bcc field is showing, show the Cc field whether it is populated or not.
Scott Kennedy8960f0a2012-11-07 15:35:50 -08002631 showCc = showBcc
2632 || (!TextUtils.isEmpty(mDraft.getCc()) && mComposeMode == REPLY_ALL);
Mindy Pereiraef388302012-06-18 19:07:44 -07002633 } else if (mRefMessage != null) {
mindyp9b1ac572012-09-27 14:12:00 -07002634 showCc = !TextUtils.isEmpty(mCc.getText());
Mindy Pereiraef388302012-06-18 19:07:44 -07002635 }
2636 mCcBccView.show(false, showCc, showBcc);
Mindy Pereiraa26b54e2012-01-06 12:54:33 -08002637 }
Mindy Pereiraef388302012-06-18 19:07:44 -07002638 updateHideOrShowCcBcc();
Mindy Pereiracbfb75a2012-06-25 14:52:23 -07002639 initChangeListeners();
Mindy Pereira326c6602012-01-04 15:32:42 -08002640 return true;
2641 }
2642
Mindy Pereirab3112a22012-06-20 12:10:03 -07002643 @VisibleForTesting
2644 protected void resetMessageForModeChange() {
Mindy Pereira154386a2012-01-11 13:02:33 -08002645 // When switching between reply, reply all, forward,
2646 // follow the behavior of webview.
2647 // The contents of the following fields are cleared
2648 // so that they can be populated directly from the
2649 // ref message:
2650 // 1) Any recipient fields
2651 // 2) The subject
2652 mTo.setText("");
2653 mCc.setText("");
2654 mBcc.setText("");
2655 // Any edits to the subject are replaced with the original subject.
2656 mSubject.setText("");
2657
2658 // Any changes to the contents of the following fields are kept:
2659 // 1) Body
2660 // 2) Attachments
2661 // If the user made changes to attachments, keep their changes.
2662 if (!mAttachmentsChanged) {
2663 mAttachmentsView.deleteAllAttachments();
2664 }
2665 }
2666
Mindy Pereira326c6602012-01-04 15:32:42 -08002667 private class ComposeModeAdapter extends ArrayAdapter<String> {
2668
2669 private LayoutInflater mInflater;
2670
2671 public ComposeModeAdapter(Context context) {
2672 super(context, R.layout.compose_mode_item, R.id.mode, getResources()
2673 .getStringArray(R.array.compose_modes));
2674 }
2675
2676 private LayoutInflater getInflater() {
2677 if (mInflater == null) {
2678 mInflater = LayoutInflater.from(getContext());
2679 }
2680 return mInflater;
2681 }
2682
2683 @Override
2684 public View getView(int position, View convertView, ViewGroup parent) {
2685 if (convertView == null) {
2686 convertView = getInflater().inflate(R.layout.compose_mode_display_item, null);
2687 }
2688 ((TextView) convertView.findViewById(R.id.mode)).setText(getItem(position));
2689 return super.getView(position, convertView, parent);
2690 }
2691 }
Mindy Pereira46ce0b12012-01-05 10:32:15 -08002692
2693 @Override
2694 public void onRespondInline(String text) {
2695 appendToBody(text, false);
mindyp40882432012-09-06 11:07:40 -07002696 mQuotedTextView.setUpperDividerVisible(false);
2697 mTo.requestFocus();
Mindy Pereira46ce0b12012-01-05 10:32:15 -08002698 }
2699
2700 /**
2701 * Append text to the body of the message. If there is no existing body
2702 * text, just sets the body to text.
2703 *
2704 * @param text
2705 * @param withSignature True to append a signature.
2706 */
2707 public void appendToBody(CharSequence text, boolean withSignature) {
Mindy Pereiraeaea9f12012-01-10 15:05:27 -08002708 Editable bodyText = mBodyView.getEditableText();
Mindy Pereira46ce0b12012-01-05 10:32:15 -08002709 if (bodyText != null && bodyText.length() > 0) {
2710 bodyText.append(text);
2711 } else {
2712 setBody(text, withSignature);
2713 }
2714 }
2715
2716 /**
2717 * Set the body of the message.
Mindy Pereirabdf7a402012-03-01 15:23:26 -08002718 *
Mindy Pereira46ce0b12012-01-05 10:32:15 -08002719 * @param text
2720 * @param withSignature True to append a signature.
2721 */
2722 public void setBody(CharSequence text, boolean withSignature) {
Mindy Pereiraeaea9f12012-01-10 15:05:27 -08002723 mBodyView.setText(text);
Mindy Pereirabdf7a402012-03-01 15:23:26 -08002724 if (withSignature) {
2725 appendSignature();
2726 }
2727 }
2728
2729 private void appendSignature() {
Mindy Pereirab13917c2012-03-29 08:08:19 -07002730 String newSignature = mCachedSettings != null ? mCachedSettings.signature : null;
Mindy Pereira433b1982012-04-03 11:53:07 -07002731 boolean hasFocus = mBodyView.hasFocus();
Mindy Pereirab13917c2012-03-29 08:08:19 -07002732 if (!TextUtils.equals(newSignature, mSignature)) {
2733 mSignature = newSignature;
2734 if (!TextUtils.isEmpty(mSignature)
2735 && getSignatureStartPosition(mSignature,
2736 mBodyView.getText().toString()) < 0) {
2737 // Appending a signature does not count as changing text.
2738 mBodyView.removeTextChangedListener(this);
2739 mBodyView.append(convertToPrintableSignature(mSignature));
2740 mBodyView.addTextChangedListener(this);
2741 }
Mindy Pereira433b1982012-04-03 11:53:07 -07002742 if (hasFocus) {
2743 focusBody();
2744 }
Mindy Pereirabdf7a402012-03-01 15:23:26 -08002745 }
2746 }
2747
2748 private String convertToPrintableSignature(String signature) {
2749 String signatureResource = getResources().getString(R.string.signature);
2750 if (signature == null) {
2751 signature = "";
2752 }
2753 return String.format(signatureResource, signature);
Mindy Pereira46ce0b12012-01-05 10:32:15 -08002754 }
Mindy Pereira1a95a572012-01-05 12:21:29 -08002755
Mindy Pereira5a85e2b2012-01-11 09:53:32 -08002756 @Override
2757 public void onAccountChanged() {
Mindy Pereira92551d02012-04-05 11:31:12 -07002758 mReplyFromAccount = mFromSpinner.getCurrentAccount();
2759 if (!mAccount.equals(mReplyFromAccount.account)) {
mindypf432dbc2012-11-12 16:00:44 -08002760 // Clear a signature, if there was one.
2761 mBodyView.removeTextChangedListener(this);
2762 String oldSignature = mSignature;
2763 String bodyText = getBody().getText().toString();
2764 if (!TextUtils.isEmpty(oldSignature)) {
2765 int pos = getSignatureStartPosition(oldSignature, bodyText);
2766 if (pos > -1) {
2767 mBodyView.setText(bodyText.substring(0, pos));
2768 }
2769 }
Paul Westbrookb1f573c2012-04-06 11:38:28 -07002770 setAccount(mReplyFromAccount.account);
mindypf432dbc2012-11-12 16:00:44 -08002771 mBodyView.addTextChangedListener(this);
Mindy Pereira181df782012-03-01 13:32:44 -08002772 // TODO: handle discarding attachments when switching accounts.
2773 // Only enable save for this draft if there is any other content
2774 // in the message.
2775 if (!isBlank()) {
2776 enableSave(true);
2777 }
2778 mReplyFromChanged = true;
2779 initRecipients();
Mindy Pereira82cc5662012-01-09 17:29:30 -08002780 }
Mindy Pereira1a95a572012-01-05 12:21:29 -08002781 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08002782
2783 public void enableSave(boolean enabled) {
2784 if (mSave != null) {
2785 mSave.setEnabled(enabled);
2786 }
2787 }
2788
2789 public void enableSend(boolean enabled) {
2790 if (mSend != null) {
2791 mSend.setEnabled(enabled);
2792 }
2793 }
2794
2795 /**
2796 * Handles button clicks from any error dialogs dealing with sending
2797 * a message.
2798 */
2799 @Override
2800 public void onClick(DialogInterface dialog, int which) {
2801 switch (which) {
2802 case DialogInterface.BUTTON_POSITIVE: {
2803 doDiscardWithoutConfirmation(true /* show toast */ );
2804 break;
2805 }
2806 case DialogInterface.BUTTON_NEGATIVE: {
2807 // If the user cancels the send, re-enable the send button.
2808 enableSend(true);
2809 break;
2810 }
2811 }
2812
2813 }
2814
Mindy Pereiraefe3d252012-03-01 14:20:44 -08002815 private void doDiscard() {
2816 new AlertDialog.Builder(this).setMessage(R.string.confirm_discard_text)
2817 .setPositiveButton(R.string.ok, this)
2818 .setNegativeButton(R.string.cancel, null)
2819 .create().show();
2820 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08002821 /**
2822 * Effectively discard the current message.
2823 *
2824 * This method is either invoked from the menu or from the dialog
2825 * once the user has confirmed that they want to discard the message.
2826 * @param showToast show "Message discarded" toast if true
2827 */
2828 private void doDiscardWithoutConfirmation(boolean showToast) {
Mindy Pereira7ed1c112012-01-18 10:59:25 -08002829 synchronized (mDraftLock) {
Mindy Pereira82cc5662012-01-09 17:29:30 -08002830 if (mDraftId != UIProvider.INVALID_MESSAGE_ID) {
2831 ContentValues values = new ContentValues();
Paul Westbrookb7050e62012-03-20 12:59:44 -07002832 values.put(BaseColumns._ID, mDraftId);
Marc Blank78ea8e22012-08-04 11:14:06 -07002833 if (!mAccount.expungeMessageUri.equals(Uri.EMPTY)) {
Mindy Pereiracfb7f332012-02-28 10:23:43 -08002834 getContentResolver().update(mAccount.expungeMessageUri, values, null, null);
2835 } else {
Marc Blank0bbc8582012-04-23 15:07:57 -07002836 getContentResolver().delete(mDraft.uri, null, null);
Mindy Pereiracfb7f332012-02-28 10:23:43 -08002837 }
Mindy Pereira82cc5662012-01-09 17:29:30 -08002838 // This is not strictly necessary (since we should not try to
2839 // save the draft after calling this) but it ensures that if we
2840 // do save again for some reason we make a new draft rather than
2841 // trying to resave an expunged draft.
2842 mDraftId = UIProvider.INVALID_MESSAGE_ID;
2843 }
2844 }
2845
2846 if (showToast) {
2847 // Display a toast to let the user know
2848 Toast.makeText(this, R.string.message_discarded, Toast.LENGTH_SHORT).show();
2849 }
2850
2851 // This prevents the draft from being saved in onPause().
2852 discardChanges();
2853 finish();
2854 }
2855
Mindy Pereiraeaea9f12012-01-10 15:05:27 -08002856 private void saveIfNeeded() {
2857 if (mAccount == null) {
2858 // We have not chosen an account yet so there's no way that we can save. This is ok,
2859 // though, since we are saving our state before AccountsActivity is activated. Thus, the
2860 // user has not interacted with us yet and there is no real state to save.
2861 return;
2862 }
2863
2864 if (shouldSave()) {
Mindy Pereira48e31b02012-05-30 13:12:24 -07002865 doSave(!mAddingAttachment /* show toast */);
Mindy Pereiraeaea9f12012-01-10 15:05:27 -08002866 }
2867 }
2868
Mindy Pereiraeaea9f12012-01-10 15:05:27 -08002869 @Override
2870 public void onAttachmentDeleted() {
2871 mAttachmentsChanged = true;
mindyp40882432012-09-06 11:07:40 -07002872 // If we are showing any attachments, make sure we have an upper
2873 // divider.
2874 mQuotedTextView.setUpperDividerVisible(mAttachmentsView.getAttachments().size() > 0);
Mindy Pereiraeaea9f12012-01-10 15:05:27 -08002875 updateSaveUi();
2876 }
Mindy Pereira75f66632012-01-11 11:42:02 -08002877
mindyp40882432012-09-06 11:07:40 -07002878 @Override
2879 public void onAttachmentAdded() {
2880 mQuotedTextView.setUpperDividerVisible(mAttachmentsView.getAttachments().size() > 0);
2881 mAttachmentsView.focusLastAttachment();
2882 }
Mindy Pereira75f66632012-01-11 11:42:02 -08002883
2884 /**
2885 * This is called any time one of our text fields changes.
2886 */
Marc Blank0bbc8582012-04-23 15:07:57 -07002887 @Override
Mindy Pereira75f66632012-01-11 11:42:02 -08002888 public void afterTextChanged(Editable s) {
2889 mTextChanged = true;
2890 updateSaveUi();
2891 }
2892
2893 @Override
2894 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
2895 // Do nothing.
2896 }
2897
Marc Blank0bbc8582012-04-23 15:07:57 -07002898 @Override
Mindy Pereira75f66632012-01-11 11:42:02 -08002899 public void onTextChanged(CharSequence s, int start, int before, int count) {
2900 // Do nothing.
2901 }
2902
2903
2904 // There is a big difference between the text associated with an address changing
2905 // to add the display name or to format properly and a recipient being added or deleted.
2906 // Make sure we only notify of changes when a recipient has been added or deleted.
2907 private class RecipientTextWatcher implements TextWatcher {
2908 private HashMap<String, Integer> mContent = new HashMap<String, Integer>();
2909
2910 private RecipientEditTextView mView;
2911
2912 private TextWatcher mListener;
2913
2914 public RecipientTextWatcher(RecipientEditTextView view, TextWatcher listener) {
2915 mView = view;
2916 mListener = listener;
2917 }
2918
2919 @Override
2920 public void afterTextChanged(Editable s) {
2921 if (hasChanged()) {
2922 mListener.afterTextChanged(s);
2923 }
2924 }
2925
2926 private boolean hasChanged() {
2927 String[] currRecips = tokenizeRecips(getAddressesFromList(mView));
2928 int totalCount = currRecips.length;
2929 int totalPrevCount = 0;
2930 for (Entry<String, Integer> entry : mContent.entrySet()) {
2931 totalPrevCount += entry.getValue();
2932 }
2933 if (totalCount != totalPrevCount) {
2934 return true;
2935 }
2936
2937 for (String recip : currRecips) {
2938 if (!mContent.containsKey(recip)) {
2939 return true;
2940 } else {
2941 int count = mContent.get(recip) - 1;
2942 if (count < 0) {
2943 return true;
2944 } else {
2945 mContent.put(recip, count);
2946 }
2947 }
2948 }
2949 return false;
2950 }
2951
2952 private String[] tokenizeRecips(String[] recips) {
2953 // Tokenize them all and put them in the list.
2954 String[] recipAddresses = new String[recips.length];
2955 for (int i = 0; i < recips.length; i++) {
2956 recipAddresses[i] = Rfc822Tokenizer.tokenize(recips[i])[0].getAddress();
2957 }
2958 return recipAddresses;
2959 }
2960
2961 @Override
2962 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
2963 String[] recips = tokenizeRecips(getAddressesFromList(mView));
2964 for (String recip : recips) {
2965 if (!mContent.containsKey(recip)) {
2966 mContent.put(recip, 1);
2967 } else {
2968 mContent.put(recip, (mContent.get(recip)) + 1);
2969 }
2970 }
2971 }
2972
2973 @Override
2974 public void onTextChanged(CharSequence s, int start, int before, int count) {
2975 // Do nothing.
2976 }
2977 }
Mindy Pereirae011b1d2012-06-18 13:45:26 -07002978
2979 public static void registerTestSendOrSaveCallback(SendOrSaveCallback testCallback) {
2980 if (sTestSendOrSaveCallback != null && testCallback != null) {
2981 throw new IllegalStateException("Attempting to register more than one test callback");
2982 }
2983 sTestSendOrSaveCallback = testCallback;
2984 }
Mindy Pereirabddd6f32012-06-20 12:10:03 -07002985
2986 @VisibleForTesting
2987 protected ArrayList<Attachment> getAttachments() {
2988 return mAttachmentsView.getAttachments();
2989 }
Mindy Pereira96a7f7a2012-07-09 16:51:06 -07002990
2991 @Override
2992 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
2993 switch (id) {
2994 case REFERENCE_MESSAGE_LOADER:
2995 return new CursorLoader(this, mRefMessageUri, UIProvider.MESSAGE_PROJECTION, null,
2996 null, null);
Mindy Pereirab199d172012-08-13 11:04:03 -07002997 case LOADER_ACCOUNT_CURSOR:
2998 return new CursorLoader(this, MailAppProvider.getAccountsUri(),
2999 UIProvider.ACCOUNTS_PROJECTION, null, null, null);
Mindy Pereira96a7f7a2012-07-09 16:51:06 -07003000 }
3001 return null;
3002 }
3003
3004 @Override
3005 public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
Mindy Pereirab199d172012-08-13 11:04:03 -07003006 int id = loader.getId();
3007 switch (id) {
3008 case REFERENCE_MESSAGE_LOADER:
3009 if (data != null && data.moveToFirst()) {
3010 mRefMessage = new Message(data);
3011 // We set these based on EXTRA_TO.
Scott Kennedy8960f0a2012-11-07 15:35:50 -08003012 mRefMessage.setTo(null);
3013 mRefMessage.setFrom(null);
Mindy Pereirab199d172012-08-13 11:04:03 -07003014 Intent intent = getIntent();
3015 int action = intent.getIntExtra(EXTRA_ACTION, COMPOSE);
3016 initFromRefMessage(action, mAccount.name);
3017 finishSetup(action, intent, null, true);
3018 if (action != FORWARD) {
3019 String to = intent.getStringExtra(EXTRA_TO);
3020 if (!TextUtils.isEmpty(to)) {
3021 clearChangeListeners();
3022 mTo.append(to);
3023 initChangeListeners();
3024 }
3025 }
3026 } else {
3027 finish();
Mindy Pereira96a7f7a2012-07-09 16:51:06 -07003028 }
Mindy Pereirab199d172012-08-13 11:04:03 -07003029 break;
3030 case LOADER_ACCOUNT_CURSOR:
3031 if (data != null && data.moveToFirst()) {
3032 // there are accounts now!
3033 Account account;
Paul Westbrookfaa742f2012-11-01 09:50:16 -07003034 final ArrayList<Account> accounts = new ArrayList<Account>();
3035 final ArrayList<Account> initializedAccounts = new ArrayList<Account>();
Mindy Pereirab199d172012-08-13 11:04:03 -07003036 do {
3037 account = new Account(data);
Paul Westbrookdfa1dec2012-09-26 16:27:28 -07003038 if (account.isAccountReady()) {
Mindy Pereirab199d172012-08-13 11:04:03 -07003039 initializedAccounts.add(account);
3040 }
3041 accounts.add(account);
3042 } while (data.moveToNext());
3043 if (initializedAccounts.size() > 0) {
3044 findViewById(R.id.wait).setVisibility(View.GONE);
3045 getLoaderManager().destroyLoader(LOADER_ACCOUNT_CURSOR);
3046 findViewById(R.id.compose).setVisibility(View.VISIBLE);
Paul Westbrookfaa742f2012-11-01 09:50:16 -07003047 mAccounts = initializedAccounts.toArray(
3048 new Account[initializedAccounts.size()]);
3049
Mindy Pereirab199d172012-08-13 11:04:03 -07003050 finishCreate();
3051 invalidateOptionsMenu();
3052 } else {
3053 // Show "waiting"
3054 account = accounts.size() > 0 ? accounts.get(0) : null;
3055 showWaitFragment(account);
3056 }
3057 }
3058 break;
Mindy Pereira96a7f7a2012-07-09 16:51:06 -07003059 }
3060 }
3061
Mindy Pereirab199d172012-08-13 11:04:03 -07003062 private void showWaitFragment(Account account) {
3063 WaitFragment fragment = getWaitFragment();
3064 if (fragment != null) {
3065 fragment.updateAccount(account);
3066 } else {
3067 findViewById(R.id.wait).setVisibility(View.VISIBLE);
3068 replaceFragment(WaitFragment.newInstance(account, true),
3069 FragmentTransaction.TRANSIT_FRAGMENT_OPEN, TAG_WAIT);
3070 }
3071 }
3072
3073 private WaitFragment getWaitFragment() {
3074 return (WaitFragment) getFragmentManager().findFragmentByTag(TAG_WAIT);
3075 }
3076
3077 private int replaceFragment(Fragment fragment, int transition, String tag) {
3078 FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
3079 fragmentTransaction.addToBackStack(null);
3080 fragmentTransaction.setTransition(transition);
3081 fragmentTransaction.replace(R.id.wait, fragment, tag);
3082 final int transactionId = fragmentTransaction.commitAllowingStateLoss();
3083 return transactionId;
3084 }
3085
Mindy Pereira96a7f7a2012-07-09 16:51:06 -07003086 @Override
3087 public void onLoaderReset(Loader<Cursor> arg0) {
3088 // Do nothing.
3089 }
Andy Huang1f8f4dd2012-10-25 21:35:35 -07003090}