MessageCompose.java revision d658fb6cac242e793df7e8269d9e96518392cb5e
1/*
2 * Copyright (C) 2008 The Android Open Source Project
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
17package com.android.email.activity;
18
19import android.app.ActionBar;
20import android.app.ActionBar.OnNavigationListener;
21import android.app.ActionBar.Tab;
22import android.app.ActionBar.TabListener;
23import android.app.Activity;
24import android.app.ActivityManager;
25import android.app.FragmentTransaction;
26import android.content.ActivityNotFoundException;
27import android.content.ContentResolver;
28import android.content.ContentUris;
29import android.content.ContentValues;
30import android.content.Context;
31import android.content.Intent;
32import android.database.Cursor;
33import android.net.Uri;
34import android.os.Bundle;
35import android.os.Parcelable;
36import android.provider.OpenableColumns;
37import android.text.InputFilter;
38import android.text.SpannableStringBuilder;
39import android.text.Spanned;
40import android.text.TextUtils;
41import android.text.TextWatcher;
42import android.text.util.Rfc822Tokenizer;
43import android.util.Log;
44import android.view.Menu;
45import android.view.MenuItem;
46import android.view.View;
47import android.view.View.OnClickListener;
48import android.view.View.OnFocusChangeListener;
49import android.view.ViewGroup;
50import android.webkit.WebView;
51import android.widget.ArrayAdapter;
52import android.widget.CheckBox;
53import android.widget.EditText;
54import android.widget.ImageView;
55import android.widget.MultiAutoCompleteTextView;
56import android.widget.TextView;
57import android.widget.Toast;
58
59import com.android.common.contacts.DataUsageStatUpdater;
60import com.android.email.Controller;
61import com.android.email.Email;
62import com.android.email.EmailAddressAdapter;
63import com.android.email.EmailAddressValidator;
64import com.android.email.R;
65import com.android.email.RecipientAdapter;
66import com.android.email.activity.setup.AccountSettings;
67import com.android.email.mail.internet.EmailHtmlUtil;
68import com.android.emailcommon.Logging;
69import com.android.emailcommon.internet.MimeUtility;
70import com.android.emailcommon.mail.Address;
71import com.android.emailcommon.provider.Account;
72import com.android.emailcommon.provider.EmailContent;
73import com.android.emailcommon.provider.EmailContent.Attachment;
74import com.android.emailcommon.provider.EmailContent.Body;
75import com.android.emailcommon.provider.EmailContent.BodyColumns;
76import com.android.emailcommon.provider.EmailContent.Message;
77import com.android.emailcommon.provider.EmailContent.MessageColumns;
78import com.android.emailcommon.provider.EmailContent.QuickResponseColumns;
79import com.android.emailcommon.provider.Mailbox;
80import com.android.emailcommon.provider.QuickResponse;
81import com.android.emailcommon.utility.AttachmentUtilities;
82import com.android.emailcommon.utility.EmailAsyncTask;
83import com.android.emailcommon.utility.Utility;
84import com.android.ex.chips.AccountSpecifier;
85import com.android.ex.chips.ChipsUtil;
86import com.android.ex.chips.RecipientEditTextView;
87import com.google.common.annotations.VisibleForTesting;
88import com.google.common.base.Objects;
89import com.google.common.collect.Lists;
90
91import java.io.File;
92import java.io.UnsupportedEncodingException;
93import java.net.URLDecoder;
94import java.util.ArrayList;
95import java.util.HashMap;
96import java.util.HashSet;
97import java.util.List;
98import java.util.concurrent.ConcurrentHashMap;
99import java.util.concurrent.ExecutionException;
100
101
102/**
103 * Activity to compose a message.
104 *
105 * TODO Revive shortcuts command for removed menu options.
106 * C: add cc/bcc
107 * N: add attachment
108 */
109public class MessageCompose extends Activity implements OnClickListener, OnFocusChangeListener,
110        DeleteMessageConfirmationDialog.Callback, InsertQuickResponseDialog.Callback {
111
112    private static final String ACTION_REPLY = "com.android.email.intent.action.REPLY";
113    private static final String ACTION_REPLY_ALL = "com.android.email.intent.action.REPLY_ALL";
114    private static final String ACTION_FORWARD = "com.android.email.intent.action.FORWARD";
115    private static final String ACTION_EDIT_DRAFT = "com.android.email.intent.action.EDIT_DRAFT";
116
117    private static final String EXTRA_ACCOUNT_ID = "account_id";
118    private static final String EXTRA_MESSAGE_ID = "message_id";
119    /** If the intent is sent from the email app itself, it should have this boolean extra. */
120    public static final String EXTRA_FROM_WITHIN_APP = "from_within_app";
121    /** If the intent is sent from thw widget. */
122    public static final String EXTRA_FROM_WIDGET = "from_widget";
123
124    private static final String STATE_KEY_CC_SHOWN =
125        "com.android.email.activity.MessageCompose.ccShown";
126    private static final String STATE_KEY_QUOTED_TEXT_SHOWN =
127        "com.android.email.activity.MessageCompose.quotedTextShown";
128    private static final String STATE_KEY_DRAFT_ID =
129        "com.android.email.activity.MessageCompose.draftId";
130    private static final String STATE_KEY_LAST_SAVE_TASK_ID =
131        "com.android.email.activity.MessageCompose.requestId";
132    private static final String STATE_KEY_ACTION =
133        "com.android.email.activity.MessageCompose.action";
134
135    private static final int ACTIVITY_REQUEST_PICK_ATTACHMENT = 1;
136
137    private static final String[] ATTACHMENT_META_SIZE_PROJECTION = {
138        OpenableColumns.SIZE
139    };
140    private static final int ATTACHMENT_META_SIZE_COLUMN_SIZE = 0;
141
142    /**
143     * A registry of the active tasks used to save messages.
144     */
145    private static final ConcurrentHashMap<Long, SendOrSaveMessageTask> sActiveSaveTasks =
146            new ConcurrentHashMap<Long, SendOrSaveMessageTask>();
147
148    private static long sNextSaveTaskId = 1;
149
150    /**
151     * The ID of the latest save or send task requested by this Activity.
152     */
153    private long mLastSaveTaskId = -1;
154
155    private Account mAccount;
156
157    /**
158     * The contents of the current message being edited. This is not always in sync with what's
159     * on the UI. {@link #updateMessage(Message, Account, boolean, boolean)} must be called to sync
160     * the UI values into this object.
161     */
162    private Message mDraft = new Message();
163
164    /**
165     * A collection of attachments the user is currently wanting to attach to this message.
166     */
167    private final ArrayList<Attachment> mAttachments = new ArrayList<Attachment>();
168
169    /**
170     * The source message for a reply, reply all, or forward. This is asynchronously loaded.
171     */
172    private Message mSource;
173
174    /**
175     * The attachments associated with the source attachments. Usually included in a forward.
176     */
177    private ArrayList<Attachment> mSourceAttachments = new ArrayList<Attachment>();
178
179    /**
180     * The action being handled by this activity. This is initially populated from the
181     * {@link Intent}, but can switch between reply/reply all/forward where appropriate.
182     * This value is nullable (a null value indicating a regular "compose").
183     */
184    private String mAction;
185
186    private TextView mFromView;
187    private MultiAutoCompleteTextView mToView;
188    private MultiAutoCompleteTextView mCcView;
189    private MultiAutoCompleteTextView mBccView;
190    private View mCcBccContainer;
191    private EditText mSubjectView;
192    private EditText mMessageContentView;
193    private View mAttachmentContainer;
194    private ViewGroup mAttachmentContentView;
195    private View mQuotedTextArea;
196    private CheckBox mIncludeQuotedTextCheckBox;
197    private WebView mQuotedText;
198    private ActionSpinnerAdapter mActionSpinnerAdapter;
199
200    private Controller mController;
201    private boolean mDraftNeedsSaving;
202    private boolean mMessageLoaded;
203    private boolean mInitiallyEmpty;
204    private boolean mPickingAttachment = false;
205    private Boolean mQuickResponsesAvailable = true;
206    private final EmailAsyncTask.Tracker mTaskTracker = new EmailAsyncTask.Tracker();
207
208    private AccountSpecifier mAddressAdapterTo;
209    private AccountSpecifier mAddressAdapterCc;
210    private AccountSpecifier mAddressAdapterBcc;
211
212    /**
213     * Watches the to, cc, bcc, subject, and message body fields.
214     */
215    private final TextWatcher mWatcher = new TextWatcher() {
216        @Override
217        public void beforeTextChanged(CharSequence s, int start,
218                                      int before, int after) { }
219
220        @Override
221        public void onTextChanged(CharSequence s, int start,
222                                      int before, int count) {
223            setMessageChanged(true);
224        }
225
226        @Override
227        public void afterTextChanged(android.text.Editable s) { }
228    };
229
230    private static Intent getBaseIntent(Context context) {
231        return new Intent(context, MessageCompose.class);
232    }
233
234    /**
235     * Create an {@link Intent} that can start the message compose activity. If accountId -1,
236     * the default account will be used; otherwise, the specified account is used.
237     */
238    public static Intent getMessageComposeIntent(Context context, long accountId) {
239        Intent i = getBaseIntent(context);
240        i.putExtra(EXTRA_ACCOUNT_ID, accountId);
241        return i;
242    }
243
244    /**
245     * Creates an {@link Intent} that can start the message compose activity from the main Email
246     * activity. This should not be used for Intents to be fired from outside of the main Email
247     * activity, such as from widgets, as the behavior of the compose screen differs subtly from
248     * those cases.
249     */
250    private static Intent getMainAppIntent(Context context, long accountId) {
251        Intent result = getMessageComposeIntent(context, accountId);
252        result.putExtra(EXTRA_FROM_WITHIN_APP, true);
253        return result;
254    }
255
256    /**
257     * Compose a new message using the given account. If account is {@link Account#NO_ACCOUNT}
258     * the default account will be used.
259     * This should only be called from the main Email application.
260     * @param context
261     * @param accountId
262     */
263    public static void actionCompose(Context context, long accountId) {
264       try {
265           Intent i = getMainAppIntent(context, accountId);
266           context.startActivity(i);
267       } catch (ActivityNotFoundException anfe) {
268           // Swallow it - this is usually a race condition, especially under automated test.
269           // (The message composer might have been disabled)
270           Email.log(anfe.toString());
271       }
272    }
273
274    /**
275     * Compose a new message using a uri (mailto:) and a given account.  If account is -1 the
276     * default account will be used.
277     * This should only be called from the main Email application.
278     * @param context
279     * @param uriString
280     * @param accountId
281     * @return true if startActivity() succeeded
282     */
283    public static boolean actionCompose(Context context, String uriString, long accountId) {
284        try {
285            Intent i = getMainAppIntent(context, accountId);
286            i.setAction(Intent.ACTION_SEND);
287            i.setData(Uri.parse(uriString));
288            context.startActivity(i);
289            return true;
290        } catch (ActivityNotFoundException anfe) {
291            // Swallow it - this is usually a race condition, especially under automated test.
292            // (The message composer might have been disabled)
293            Email.log(anfe.toString());
294            return false;
295        }
296    }
297
298    /**
299     * Compose a new message as a reply to the given message. If replyAll is true the function
300     * is reply all instead of simply reply.
301     * @param context
302     * @param messageId
303     * @param replyAll
304     */
305    public static void actionReply(Context context, long messageId, boolean replyAll) {
306        startActivityWithMessage(context, replyAll ? ACTION_REPLY_ALL : ACTION_REPLY, messageId);
307    }
308
309    /**
310     * Compose a new message as a forward of the given message.
311     * @param context
312     * @param messageId
313     */
314    public static void actionForward(Context context, long messageId) {
315        startActivityWithMessage(context, ACTION_FORWARD, messageId);
316    }
317
318    /**
319     * Continue composition of the given message. This action modifies the way this Activity
320     * handles certain actions.
321     * Save will attempt to replace the message in the given folder with the updated version.
322     * Discard will delete the message from the given folder.
323     * @param context
324     * @param messageId the message id.
325     */
326    public static void actionEditDraft(Context context, long messageId) {
327        startActivityWithMessage(context, ACTION_EDIT_DRAFT, messageId);
328    }
329
330    /**
331     * Starts a compose activity with a message as a reference message (e.g. for reply or forward).
332     */
333    private static void startActivityWithMessage(Context context, String action, long messageId) {
334        Intent i = getBaseIntent(context);
335        i.putExtra(EXTRA_MESSAGE_ID, messageId);
336        i.setAction(action);
337        context.startActivity(i);
338    }
339
340    private void setAccount(Intent intent) {
341        long accountId = intent.getLongExtra(EXTRA_ACCOUNT_ID, -1);
342        if (accountId == Account.NO_ACCOUNT) {
343            accountId = Account.getDefaultAccountId(this);
344        }
345        if (accountId == Account.NO_ACCOUNT) {
346            // There are no accounts set up. This should not have happened. Prompt the
347            // user to set up an account as an acceptable bailout.
348            Welcome.actionStart(this);
349            finish();
350        } else {
351            setAccount(Account.restoreAccountWithId(this, accountId));
352        }
353    }
354
355    private void setAccount(Account account) {
356        if (account == null) {
357            throw new IllegalArgumentException();
358        }
359        mAccount = account;
360        mFromView.setText(account.mEmailAddress);
361        mAddressAdapterTo
362                .setAccount(new android.accounts.Account(account.mEmailAddress, "unknown"));
363        mAddressAdapterCc
364                .setAccount(new android.accounts.Account(account.mEmailAddress, "unknown"));
365        mAddressAdapterBcc
366                .setAccount(new android.accounts.Account(account.mEmailAddress, "unknown"));
367
368        new QuickResponseChecker(mTaskTracker).executeParallel((Void) null);
369    }
370
371    @Override
372    public void onCreate(Bundle savedInstanceState) {
373        super.onCreate(savedInstanceState);
374        ActivityHelper.debugSetWindowFlags(this);
375        setContentView(R.layout.message_compose);
376
377        mController = Controller.getInstance(getApplication());
378        initViews();
379
380        // Show the back arrow on the action bar.
381        getActionBar().setDisplayOptions(
382                ActionBar.DISPLAY_HOME_AS_UP, ActionBar.DISPLAY_HOME_AS_UP);
383
384        if (savedInstanceState != null) {
385            long draftId = savedInstanceState.getLong(STATE_KEY_DRAFT_ID, Message.NOT_SAVED);
386            long existingSaveTaskId = savedInstanceState.getLong(STATE_KEY_LAST_SAVE_TASK_ID, -1);
387            setAction(savedInstanceState.getString(STATE_KEY_ACTION));
388            SendOrSaveMessageTask existingSaveTask = sActiveSaveTasks.get(existingSaveTaskId);
389
390            if ((draftId != Message.NOT_SAVED) || (existingSaveTask != null)) {
391                // Restoring state and there was an existing message saved or in the process of
392                // being saved.
393                resumeDraft(draftId, existingSaveTask, false /* don't restore views */);
394            } else {
395                // Restoring state but there was nothing saved - probably means the user rotated
396                // the device immediately - just use the Intent.
397                resolveIntent(getIntent());
398            }
399        } else {
400            Intent intent = getIntent();
401            setAction(intent.getAction());
402            resolveIntent(intent);
403        }
404    }
405
406    private void resolveIntent(Intent intent) {
407        if (Intent.ACTION_VIEW.equals(mAction)
408                || Intent.ACTION_SENDTO.equals(mAction)
409                || Intent.ACTION_SEND.equals(mAction)
410                || Intent.ACTION_SEND_MULTIPLE.equals(mAction)) {
411            initFromIntent(intent);
412            setMessageChanged(true);
413            setMessageLoaded(true);
414        } else if (ACTION_REPLY.equals(mAction)
415                || ACTION_REPLY_ALL.equals(mAction)
416                || ACTION_FORWARD.equals(mAction)) {
417            long sourceMessageId = getIntent().getLongExtra(EXTRA_MESSAGE_ID, Message.NOT_SAVED);
418            loadSourceMessage(sourceMessageId, true);
419
420        } else if (ACTION_EDIT_DRAFT.equals(mAction)) {
421            // Assert getIntent.hasExtra(EXTRA_MESSAGE_ID)
422            long draftId = getIntent().getLongExtra(EXTRA_MESSAGE_ID, Message.NOT_SAVED);
423            resumeDraft(draftId, null, true /* restore views */);
424
425        } else {
426            // Normal compose flow for a new message.
427            setAccount(intent);
428            setInitialComposeText(null, getAccountSignature(mAccount));
429            setMessageLoaded(true);
430        }
431    }
432
433    @Override
434    protected void onRestoreInstanceState(Bundle savedInstanceState) {
435        // Temporarily disable onTextChanged listeners while restoring the fields
436        removeListeners();
437        super.onRestoreInstanceState(savedInstanceState);
438        if (savedInstanceState.getBoolean(STATE_KEY_CC_SHOWN)) {
439            showCcBccFields();
440        }
441        mQuotedTextArea.setVisibility(savedInstanceState.getBoolean(STATE_KEY_QUOTED_TEXT_SHOWN)
442                ? View.VISIBLE : View.GONE);
443        mQuotedText.setVisibility(savedInstanceState.getBoolean(STATE_KEY_QUOTED_TEXT_SHOWN)
444                ? View.VISIBLE : View.GONE);
445        addListeners();
446    }
447
448    // needed for unit tests
449    @Override
450    public void setIntent(Intent intent) {
451        super.setIntent(intent);
452        setAction(intent.getAction());
453    }
454
455    private void setQuickResponsesAvailable(boolean quickResponsesAvailable) {
456        if (mQuickResponsesAvailable != quickResponsesAvailable) {
457            mQuickResponsesAvailable = quickResponsesAvailable;
458            invalidateOptionsMenu();
459        }
460    }
461
462    /**
463     * Given an accountId and context, finds if the database has any QuickResponse
464     * entries and returns the result to the Callback.
465     */
466    private class QuickResponseChecker extends EmailAsyncTask<Void, Void, Boolean> {
467        public QuickResponseChecker(EmailAsyncTask.Tracker tracker) {
468            super(tracker);
469        }
470
471        @Override
472        protected Boolean doInBackground(Void... params) {
473            return EmailContent.count(MessageCompose.this, QuickResponse.CONTENT_URI,
474                    QuickResponseColumns.ACCOUNT_KEY + "=?",
475                    new String[] {Long.toString(mAccount.mId)}) > 0;
476        }
477
478        @Override
479        protected void onSuccess(Boolean quickResponsesAvailable) {
480            setQuickResponsesAvailable(quickResponsesAvailable);
481        }
482    }
483
484    @Override
485    public void onResume() {
486        super.onResume();
487
488        // Exit immediately if the accounts list has changed (e.g. externally deleted)
489        if (Email.getNotifyUiAccountsChanged()) {
490            Welcome.actionStart(this);
491            finish();
492            return;
493        }
494
495        // If activity paused and quick responses are removed/added, possibly update options menu
496        if (mAccount != null) {
497            new QuickResponseChecker(mTaskTracker).executeParallel((Void) null);
498        }
499    }
500
501    @Override
502    public void onPause() {
503        super.onPause();
504        saveIfNeeded();
505    }
506
507    /**
508     * We override onDestroy to make sure that the WebView gets explicitly destroyed.
509     * Otherwise it can leak native references.
510     */
511    @Override
512    public void onDestroy() {
513        super.onDestroy();
514        mQuotedText.destroy();
515        mQuotedText = null;
516
517        mTaskTracker.cancellAllInterrupt();
518
519        if (mAddressAdapterTo != null && mAddressAdapterTo instanceof EmailAddressAdapter) {
520            ((EmailAddressAdapter) mAddressAdapterTo).close();
521        }
522        if (mAddressAdapterCc != null && mAddressAdapterCc instanceof EmailAddressAdapter) {
523            ((EmailAddressAdapter) mAddressAdapterCc).close();
524        }
525        if (mAddressAdapterBcc != null && mAddressAdapterBcc instanceof EmailAddressAdapter) {
526            ((EmailAddressAdapter) mAddressAdapterBcc).close();
527        }
528    }
529
530    /**
531     * The framework handles most of the fields, but we need to handle stuff that we
532     * dynamically show and hide:
533     * Cc field,
534     * Bcc field,
535     * Quoted text,
536     */
537    @Override
538    protected void onSaveInstanceState(Bundle outState) {
539        super.onSaveInstanceState(outState);
540
541        long draftId = mDraft.mId;
542        if (draftId != Message.NOT_SAVED) {
543            outState.putLong(STATE_KEY_DRAFT_ID, draftId);
544        }
545        outState.putBoolean(STATE_KEY_CC_SHOWN, mCcBccContainer.getVisibility() == View.VISIBLE);
546        outState.putBoolean(STATE_KEY_QUOTED_TEXT_SHOWN,
547                mQuotedTextArea.getVisibility() == View.VISIBLE);
548        outState.putString(STATE_KEY_ACTION, mAction);
549
550        // If there are any outstanding save requests, ensure that it's noted in case it hasn't
551        // finished by the time the activity is restored.
552        outState.putLong(STATE_KEY_LAST_SAVE_TASK_ID, mLastSaveTaskId);
553    }
554
555    @Override
556    public void onBackPressed() {
557        onBack(true /* systemKey */);
558    }
559
560    /**
561     * Whether or not the current message being edited has a source message (i.e. is a reply,
562     * or forward) that is loaded.
563     */
564    private boolean hasSourceMessage() {
565        return mSource != null;
566    }
567
568    /**
569     * @return true if the activity was opened by the email app itself.
570     */
571    private boolean isOpenedFromWithinApp() {
572        Intent i = getIntent();
573        return (i != null && i.getBooleanExtra(EXTRA_FROM_WITHIN_APP, false));
574    }
575
576    private boolean isOpenedFromWidget() {
577        Intent i = getIntent();
578        return (i != null && i.getBooleanExtra(EXTRA_FROM_WIDGET, false));
579    }
580
581    /**
582     * Sets message as loaded and then initializes the TextWatchers.
583     * @param isLoaded - value to which to set mMessageLoaded
584     */
585    private void setMessageLoaded(boolean isLoaded) {
586        if (mMessageLoaded != isLoaded) {
587            mMessageLoaded = isLoaded;
588            addListeners();
589            mInitiallyEmpty = areViewsEmpty();
590        }
591    }
592
593    private void setMessageChanged(boolean messageChanged) {
594        boolean needsSaving = messageChanged && !(mInitiallyEmpty && areViewsEmpty());
595
596        if (mDraftNeedsSaving != needsSaving) {
597            mDraftNeedsSaving = needsSaving;
598            invalidateOptionsMenu();
599        }
600    }
601
602    /**
603     * @return whether or not all text fields are empty (i.e. the entire compose message is empty)
604     */
605    private boolean areViewsEmpty() {
606        return (mToView.length() == 0)
607                && (mCcView.length() == 0)
608                && (mBccView.length() == 0)
609                && (mSubjectView.length() == 0)
610                && isBodyEmpty()
611                && mAttachments.isEmpty();
612    }
613
614    private boolean isBodyEmpty() {
615        return (mMessageContentView.length() == 0)
616                || mMessageContentView.getText()
617                        .toString().equals("\n" + getAccountSignature(mAccount));
618    }
619
620    public void setFocusShifter(int fromViewId, final int targetViewId) {
621        View label = findViewById(fromViewId); // xlarge only
622        if (label != null) {
623            final View target = UiUtilities.getView(this, targetViewId);
624            label.setOnClickListener(new View.OnClickListener() {
625                @Override
626                public void onClick(View v) {
627                    target.requestFocus();
628                }
629            });
630        }
631    }
632
633    /**
634     * An {@link InputFilter} that implements special address cleanup rules.
635     * The first space key entry following an "@" symbol that is followed by any combination
636     * of letters and symbols, including one+ dots and zero commas, should insert an extra
637     * comma (followed by the space).
638     */
639    @VisibleForTesting
640    static final InputFilter RECIPIENT_FILTER = new InputFilter() {
641        @Override
642        public CharSequence filter(CharSequence source, int start, int end, Spanned dest,
643                int dstart, int dend) {
644
645            // Quick check - did they enter a single space?
646            if (end-start != 1 || source.charAt(start) != ' ') {
647                return null;
648            }
649
650            // determine if the characters before the new space fit the pattern
651            // follow backwards and see if we find a comma, dot, or @
652            int scanBack = dstart;
653            boolean dotFound = false;
654            while (scanBack > 0) {
655                char c = dest.charAt(--scanBack);
656                switch (c) {
657                    case '.':
658                        dotFound = true;    // one or more dots are req'd
659                        break;
660                    case ',':
661                        return null;
662                    case '@':
663                        if (!dotFound) {
664                            return null;
665                        }
666
667                        // we have found a comma-insert case.  now just do it
668                        // in the least expensive way we can.
669                        if (source instanceof Spanned) {
670                            SpannableStringBuilder sb = new SpannableStringBuilder(",");
671                            sb.append(source);
672                            return sb;
673                        } else {
674                            return ", ";
675                        }
676                    default:
677                        // just keep going
678                }
679            }
680
681            // no termination cases were found, so don't edit the input
682            return null;
683        }
684    };
685
686    private void initViews() {
687        ViewGroup toParent = UiUtilities.getViewOrNull(this, R.id.to_content);
688        if (toParent != null) {
689            mToView = (MultiAutoCompleteTextView) toParent.findViewById(R.id.to);
690            ViewGroup ccParent, bccParent;
691            ccParent = (ViewGroup) findViewById(R.id.cc_content);
692            mCcView = (MultiAutoCompleteTextView) ccParent.findViewById(R.id.cc);
693            bccParent = (ViewGroup) findViewById(R.id.bcc_content);
694            mBccView = (MultiAutoCompleteTextView) bccParent.findViewById(R.id.bcc);
695        } else {
696            mToView = UiUtilities.getView(this, R.id.to);
697            mCcView = UiUtilities.getView(this, R.id.cc);
698            mBccView = UiUtilities.getView(this, R.id.bcc);
699        }
700
701        mFromView = UiUtilities.getView(this, R.id.from);
702        mCcBccContainer = UiUtilities.getView(this, R.id.cc_bcc_wrapper);
703        mSubjectView = UiUtilities.getView(this, R.id.subject);
704        mMessageContentView = UiUtilities.getView(this, R.id.body_text);
705        mAttachmentContentView = UiUtilities.getView(this, R.id.attachments);
706        mAttachmentContainer = UiUtilities.getView(this, R.id.attachment_container);
707        mQuotedTextArea = UiUtilities.getView(this, R.id.quoted_text_area);
708        mIncludeQuotedTextCheckBox = UiUtilities.getView(this, R.id.include_quoted_text);
709        mQuotedText = UiUtilities.getView(this, R.id.quoted_text);
710
711        InputFilter[] recipientFilters = new InputFilter[] { RECIPIENT_FILTER };
712
713        // NOTE: assumes no other filters are set
714        mToView.setFilters(recipientFilters);
715        mCcView.setFilters(recipientFilters);
716        mBccView.setFilters(recipientFilters);
717
718        /*
719         * We set this to invisible by default. Other methods will turn it back on if it's
720         * needed.
721         */
722        mQuotedTextArea.setVisibility(View.GONE);
723        setIncludeQuotedText(false, false);
724
725        mIncludeQuotedTextCheckBox.setOnClickListener(this);
726
727        EmailAddressValidator addressValidator = new EmailAddressValidator();
728
729        setupAddressAdapters();
730        mToView.setTokenizer(new Rfc822Tokenizer());
731        mToView.setValidator(addressValidator);
732
733        mCcView.setTokenizer(new Rfc822Tokenizer());
734        mCcView.setValidator(addressValidator);
735
736        mBccView.setTokenizer(new Rfc822Tokenizer());
737        mBccView.setValidator(addressValidator);
738
739        final View addCcBccView = UiUtilities.getViewOrNull(this, R.id.add_cc_bcc);
740        if (addCcBccView != null) {
741            // Tablet view.
742            addCcBccView.setOnClickListener(this);
743        }
744
745        final View addAttachmentView = UiUtilities.getViewOrNull(this, R.id.add_attachment);
746        if (addAttachmentView != null) {
747            // Tablet view.
748            addAttachmentView.setOnClickListener(this);
749        }
750
751        setFocusShifter(R.id.to_label, R.id.to);
752        setFocusShifter(R.id.cc_label, R.id.cc);
753        setFocusShifter(R.id.bcc_label, R.id.bcc);
754        setFocusShifter(R.id.composearea_tap_trap_bottom, R.id.body_text);
755
756        mMessageContentView.setOnFocusChangeListener(this);
757
758        updateAttachmentContainer();
759        mToView.requestFocus();
760    }
761
762    /**
763     * Initializes listeners. Should only be called once initializing of views is complete to
764     * avoid unnecessary draft saving.
765     */
766    private void addListeners() {
767        mToView.addTextChangedListener(mWatcher);
768        mCcView.addTextChangedListener(mWatcher);
769        mBccView.addTextChangedListener(mWatcher);
770        mSubjectView.addTextChangedListener(mWatcher);
771        mMessageContentView.addTextChangedListener(mWatcher);
772    }
773
774    /**
775     * Removes listeners from the user-editable fields. Can be used to temporarily disable them
776     * while resetting fields (such as when changing from reply to reply all) to avoid
777     * unnecessary saving.
778     */
779    private void removeListeners() {
780        mToView.removeTextChangedListener(mWatcher);
781        mCcView.removeTextChangedListener(mWatcher);
782        mBccView.removeTextChangedListener(mWatcher);
783        mSubjectView.removeTextChangedListener(mWatcher);
784        mMessageContentView.removeTextChangedListener(mWatcher);
785    }
786
787    /**
788     * Set up address auto-completion adapters.
789     */
790    private void setupAddressAdapters() {
791        boolean supportsChips = ChipsUtil.supportsChipsUi();
792
793        if (supportsChips && mToView instanceof RecipientEditTextView) {
794            mAddressAdapterTo = new RecipientAdapter(this, (RecipientEditTextView) mToView);
795            mToView.setAdapter((RecipientAdapter) mAddressAdapterTo);
796        } else {
797            mAddressAdapterTo = new EmailAddressAdapter(this);
798            mToView.setAdapter((EmailAddressAdapter) mAddressAdapterTo);
799        }
800        if (supportsChips && mCcView instanceof RecipientEditTextView) {
801            mAddressAdapterCc = new RecipientAdapter(this, (RecipientEditTextView) mCcView);
802            mCcView.setAdapter((RecipientAdapter) mAddressAdapterCc);
803        } else {
804            mAddressAdapterCc = new EmailAddressAdapter(this);
805            mCcView.setAdapter((EmailAddressAdapter) mAddressAdapterCc);
806        }
807        if (supportsChips && mBccView instanceof RecipientEditTextView) {
808            mAddressAdapterBcc = new RecipientAdapter(this, (RecipientEditTextView) mBccView);
809            mBccView.setAdapter((RecipientAdapter) mAddressAdapterBcc);
810        } else {
811            mAddressAdapterBcc = new EmailAddressAdapter(this);
812            mBccView.setAdapter((EmailAddressAdapter) mAddressAdapterBcc);
813        }
814    }
815
816    /**
817     * Asynchronously loads a draft message for editing.
818     * This may or may not restore the view contents, depending on whether or not callers want,
819     * since in the case of screen rotation, those are restored automatically.
820     */
821    private void resumeDraft(
822            long draftId,
823            SendOrSaveMessageTask existingSaveTask,
824            final boolean restoreViews) {
825        // Note - this can be Message.NOT_SAVED if there is an existing save task in progress
826        // for the draft we need to load.
827        mDraft.mId = draftId;
828
829        new LoadMessageTask(draftId, existingSaveTask, new OnMessageLoadHandler() {
830            @Override
831            public void onMessageLoaded(Message message, Body body) {
832                message.mHtml = body.mHtmlContent;
833                message.mText = body.mTextContent;
834                message.mHtmlReply = body.mHtmlReply;
835                message.mTextReply = body.mTextReply;
836                message.mIntroText = body.mIntroText;
837                message.mSourceKey = body.mSourceKey;
838
839                mDraft = message;
840                processDraftMessage(message, restoreViews);
841
842                // Load attachments related to the draft.
843                loadAttachments(message.mId, mAccount, new AttachmentLoadedCallback() {
844                    @Override
845                    public void onAttachmentLoaded(Attachment[] attachments) {
846                        for (Attachment attachment: attachments) {
847                            addAttachment(attachment);
848                        }
849                    }
850                });
851
852                // If we're resuming an edit of a reply, reply-all, or forward, re-load the
853                // source message if available so that we get more information.
854                if (message.mSourceKey != Message.NOT_SAVED) {
855                    loadSourceMessage(message.mSourceKey, false /* restore views */);
856                }
857            }
858
859            @Override
860            public void onLoadFailed() {
861                Utility.showToast(MessageCompose.this, R.string.error_loading_message_body);
862                finish();
863            }
864        }).executeSerial((Void[]) null);
865    }
866
867    @VisibleForTesting
868    void processDraftMessage(Message message, boolean restoreViews) {
869        if (restoreViews) {
870            mSubjectView.setText(message.mSubject);
871            addAddresses(mToView, Address.unpack(message.mTo));
872            Address[] cc = Address.unpack(message.mCc);
873            if (cc.length > 0) {
874                addAddresses(mCcView, cc);
875            }
876            Address[] bcc = Address.unpack(message.mBcc);
877            if (bcc.length > 0) {
878                addAddresses(mBccView, bcc);
879            }
880
881            mMessageContentView.setText(message.mText);
882
883            showCcBccFieldsIfFilled();
884            setNewMessageFocus();
885        }
886        setMessageChanged(false);
887
888        // The quoted text must always be restored.
889        displayQuotedText(message.mTextReply, message.mHtmlReply);
890        setIncludeQuotedText(
891                (mDraft.mFlags & Message.FLAG_NOT_INCLUDE_QUOTED_TEXT) == 0, false);
892    }
893
894    /**
895     * Asynchronously loads a source message (to be replied or forwarded in this current view),
896     * populating text fields and quoted text fields when the load finishes, if requested.
897     */
898    private void loadSourceMessage(long sourceMessageId, final boolean restoreViews) {
899        new LoadMessageTask(sourceMessageId, null, new OnMessageLoadHandler() {
900            @Override
901            public void onMessageLoaded(Message message, Body body) {
902                message.mHtml = body.mHtmlContent;
903                message.mText = body.mTextContent;
904                message.mHtmlReply = null;
905                message.mTextReply = null;
906                message.mIntroText = null;
907                mSource = message;
908                mSourceAttachments = new ArrayList<Attachment>();
909
910                if (restoreViews) {
911                    processSourceMessage(mSource, mAccount);
912                    setInitialComposeText(null, getAccountSignature(mAccount));
913                }
914
915                loadAttachments(message.mId, mAccount, new AttachmentLoadedCallback() {
916                    @Override
917                    public void onAttachmentLoaded(Attachment[] attachments) {
918                        final boolean supportsSmartForward =
919                            (mAccount.mFlags & Account.FLAGS_SUPPORTS_SMART_FORWARD) != 0;
920
921                        // Process the attachments to have the appropriate smart forward flags.
922                        for (Attachment attachment : attachments) {
923                            if (supportsSmartForward) {
924                                attachment.mFlags |= Attachment.FLAG_SMART_FORWARD;
925                            }
926                            mSourceAttachments.add(attachment);
927                        }
928                        if (isForward() && restoreViews) {
929                            if (processSourceMessageAttachments(
930                                    mAttachments, mSourceAttachments, true)) {
931                                updateAttachmentUi();
932                                setMessageChanged(true);
933                            }
934                        }
935                    }
936                });
937
938                if (mAction.equals(ACTION_EDIT_DRAFT)) {
939                    // Resuming a draft may in fact be resuming a reply/reply all/forward.
940                    // Use a best guess and infer the action here.
941                    String inferredAction = inferAction();
942                    if (inferredAction != null) {
943                        setAction(inferredAction);
944                        // No need to update the action selector as switching actions should do it.
945                        return;
946                    }
947                }
948
949                updateActionSelector();
950            }
951
952            @Override
953            public void onLoadFailed() {
954                // The loading of the source message is only really required if it is needed
955                // immediately to restore the view contents. In the case of resuming draft, it
956                // is only needed to gather additional information.
957                if (restoreViews) {
958                    Utility.showToast(MessageCompose.this, R.string.error_loading_message_body);
959                    finish();
960                }
961            }
962        }).executeSerial((Void[]) null);
963    }
964
965    /**
966     * Infers whether or not the current state of the message best reflects either a reply,
967     * reply-all, or forward.
968     */
969    @VisibleForTesting
970    String inferAction() {
971        String subject = mSubjectView.getText().toString();
972        if (subject == null) {
973            return null;
974        }
975        if (subject.toLowerCase().startsWith("fwd:")) {
976            return ACTION_FORWARD;
977        } else if (subject.toLowerCase().startsWith("re:")) {
978            int numRecipients = getAddresses(mToView).length
979                    + getAddresses(mCcView).length
980                    + getAddresses(mBccView).length;
981            if (numRecipients > 1) {
982                return ACTION_REPLY_ALL;
983            } else {
984                return ACTION_REPLY;
985            }
986        } else {
987            // Unsure.
988            return null;
989        }
990    }
991
992    private interface OnMessageLoadHandler {
993        /**
994         * Handles a load to a message (e.g. a draft message or a source message).
995         */
996        void onMessageLoaded(Message message, Body body);
997
998        /**
999         * Handles a failure to load a message.
1000         */
1001        void onLoadFailed();
1002    }
1003
1004    /**
1005     * Asynchronously loads a message and the account information.
1006     * This can be used to load a reference message (when replying) or when restoring a draft.
1007     */
1008    private class LoadMessageTask extends EmailAsyncTask<Void, Void, Object[]> {
1009        /**
1010         * The message ID to load, if available.
1011         */
1012        private long mMessageId;
1013
1014        /**
1015         * A future-like reference to the save task which must complete prior to this load.
1016         */
1017        private final SendOrSaveMessageTask mSaveTask;
1018
1019        /**
1020         * A callback to pass the results of the load to.
1021         */
1022        private final OnMessageLoadHandler mCallback;
1023
1024        public LoadMessageTask(
1025                long messageId, SendOrSaveMessageTask saveTask, OnMessageLoadHandler callback) {
1026            super(mTaskTracker);
1027            mMessageId = messageId;
1028            mSaveTask = saveTask;
1029            mCallback = callback;
1030        }
1031
1032        private long getIdToLoad() throws InterruptedException, ExecutionException {
1033            if (mMessageId == -1) {
1034                mMessageId = mSaveTask.get();
1035            }
1036            return mMessageId;
1037        }
1038
1039        @Override
1040        protected Object[] doInBackground(Void... params) {
1041            long messageId;
1042            try {
1043                messageId = getIdToLoad();
1044            } catch (InterruptedException e) {
1045                // Don't have a good message ID to load - bail.
1046                Log.e(Logging.LOG_TAG,
1047                        "Unable to load draft message since existing save task failed: " + e);
1048                return null;
1049            } catch (ExecutionException e) {
1050                // Don't have a good message ID to load - bail.
1051                Log.e(Logging.LOG_TAG,
1052                        "Unable to load draft message since existing save task failed: " + e);
1053                return null;
1054            }
1055            Message message = Message.restoreMessageWithId(MessageCompose.this, messageId);
1056            if (message == null) {
1057                return null;
1058            }
1059            long accountId = message.mAccountKey;
1060            Account account = Account.restoreAccountWithId(MessageCompose.this, accountId);
1061            Body body;
1062            try {
1063                body = Body.restoreBodyWithMessageId(MessageCompose.this, message.mId);
1064            } catch (RuntimeException e) {
1065                Log.d(Logging.LOG_TAG, "Exception while loading message body: " + e);
1066                return null;
1067            }
1068            return new Object[] {message, body, account};
1069        }
1070
1071        @Override
1072        protected void onSuccess(Object[] results) {
1073            if ((results == null) || (results.length != 3)) {
1074                mCallback.onLoadFailed();
1075                return;
1076            }
1077
1078            final Message message = (Message) results[0];
1079            final Body body = (Body) results[1];
1080            final Account account = (Account) results[2];
1081            if ((message == null) || (body == null) || (account == null)) {
1082                mCallback.onLoadFailed();
1083                return;
1084            }
1085
1086            setAccount(account);
1087            mCallback.onMessageLoaded(message, body);
1088            setMessageLoaded(true);
1089        }
1090    }
1091
1092    private interface AttachmentLoadedCallback {
1093        /**
1094         * Handles completion of the loading of a set of attachments.
1095         * Callback will always happen on the main thread.
1096         */
1097        void onAttachmentLoaded(Attachment[] attachment);
1098    }
1099
1100    private void loadAttachments(
1101            final long messageId,
1102            final Account account,
1103            final AttachmentLoadedCallback callback) {
1104        new EmailAsyncTask<Void, Void, Attachment[]>(mTaskTracker) {
1105            @Override
1106            protected Attachment[] doInBackground(Void... params) {
1107                return Attachment.restoreAttachmentsWithMessageId(MessageCompose.this, messageId);
1108            }
1109
1110            @Override
1111            protected void onSuccess(Attachment[] attachments) {
1112                if (attachments == null) {
1113                    attachments = new Attachment[0];
1114                }
1115                callback.onAttachmentLoaded(attachments);
1116            }
1117        }.executeSerial((Void[]) null);
1118    }
1119
1120    @Override
1121    public void onFocusChange(View view, boolean focused) {
1122        if (focused) {
1123            switch (view.getId()) {
1124                case R.id.body_text:
1125                    // When focusing on the message content via tabbing to it, or other means of
1126                    // auto focusing, move the cursor to the end of the body (before the signature).
1127                    if (mMessageContentView.getSelectionStart() == 0
1128                            && mMessageContentView.getSelectionEnd() == 0) {
1129                        // There is no way to determine if the focus change was programmatic or due
1130                        // to keyboard event, or if it was due to a tap/restore. Use a best-guess
1131                        // by using the fact that auto-focus/keyboard tabs set the selection to 0.
1132                        setMessageContentSelection(getAccountSignature(mAccount));
1133                    }
1134            }
1135        }
1136    }
1137
1138    private static void addAddresses(MultiAutoCompleteTextView view, Address[] addresses) {
1139        if (addresses == null) {
1140            return;
1141        }
1142        for (Address address : addresses) {
1143            addAddress(view, address.toString());
1144        }
1145    }
1146
1147    private static void addAddresses(MultiAutoCompleteTextView view, String[] addresses) {
1148        if (addresses == null) {
1149            return;
1150        }
1151        for (String oneAddress : addresses) {
1152            addAddress(view, oneAddress);
1153        }
1154    }
1155
1156    private static void addAddresses(MultiAutoCompleteTextView view, String addresses) {
1157        if (addresses == null) {
1158            return;
1159        }
1160        Address[] unpackedAddresses = Address.unpack(addresses);
1161        for (Address address : unpackedAddresses) {
1162            addAddress(view, address.toString());
1163        }
1164    }
1165
1166    private static void addAddress(MultiAutoCompleteTextView view, String address) {
1167        view.append(address + ", ");
1168    }
1169
1170    private static String getPackedAddresses(TextView view) {
1171        Address[] addresses = Address.parse(view.getText().toString().trim());
1172        return Address.pack(addresses);
1173    }
1174
1175    private static Address[] getAddresses(TextView view) {
1176        Address[] addresses = Address.parse(view.getText().toString().trim());
1177        return addresses;
1178    }
1179
1180    /*
1181     * Computes a short string indicating the destination of the message based on To, Cc, Bcc.
1182     * If only one address appears, returns the friendly form of that address.
1183     * Otherwise returns the friendly form of the first address appended with "and N others".
1184     */
1185    private String makeDisplayName(String packedTo, String packedCc, String packedBcc) {
1186        Address first = null;
1187        int nRecipients = 0;
1188        for (String packed: new String[] {packedTo, packedCc, packedBcc}) {
1189            Address[] addresses = Address.unpack(packed);
1190            nRecipients += addresses.length;
1191            if (first == null && addresses.length > 0) {
1192                first = addresses[0];
1193            }
1194        }
1195        if (nRecipients == 0) {
1196            return "";
1197        }
1198        String friendly = first.toFriendly();
1199        if (nRecipients == 1) {
1200            return friendly;
1201        }
1202        return this.getString(R.string.message_compose_display_name, friendly, nRecipients - 1);
1203    }
1204
1205    private ContentValues getUpdateContentValues(Message message) {
1206        ContentValues values = new ContentValues();
1207        values.put(MessageColumns.TIMESTAMP, message.mTimeStamp);
1208        values.put(MessageColumns.FROM_LIST, message.mFrom);
1209        values.put(MessageColumns.TO_LIST, message.mTo);
1210        values.put(MessageColumns.CC_LIST, message.mCc);
1211        values.put(MessageColumns.BCC_LIST, message.mBcc);
1212        values.put(MessageColumns.SUBJECT, message.mSubject);
1213        values.put(MessageColumns.DISPLAY_NAME, message.mDisplayName);
1214        values.put(MessageColumns.FLAG_READ, message.mFlagRead);
1215        values.put(MessageColumns.FLAG_LOADED, message.mFlagLoaded);
1216        values.put(MessageColumns.FLAG_ATTACHMENT, message.mFlagAttachment);
1217        values.put(MessageColumns.FLAGS, message.mFlags);
1218        return values;
1219    }
1220
1221    /**
1222     * Updates the given message using values from the compose UI.
1223     *
1224     * @param message The message to be updated.
1225     * @param account the account (used to obtain From: address).
1226     * @param hasAttachments true if it has one or more attachment.
1227     * @param sending set true if the message is about to sent, in which case we perform final
1228     *        clean up;
1229     */
1230    private void updateMessage(Message message, Account account, boolean hasAttachments,
1231            boolean sending) {
1232        if (message.mMessageId == null || message.mMessageId.length() == 0) {
1233            message.mMessageId = Utility.generateMessageId();
1234        }
1235        message.mTimeStamp = System.currentTimeMillis();
1236        message.mFrom = new Address(account.getEmailAddress(), account.getSenderName()).pack();
1237        message.mTo = getPackedAddresses(mToView);
1238        message.mCc = getPackedAddresses(mCcView);
1239        message.mBcc = getPackedAddresses(mBccView);
1240        message.mSubject = mSubjectView.getText().toString();
1241        message.mText = mMessageContentView.getText().toString();
1242        message.mAccountKey = account.mId;
1243        message.mDisplayName = makeDisplayName(message.mTo, message.mCc, message.mBcc);
1244        message.mFlagRead = true;
1245        message.mFlagLoaded = Message.FLAG_LOADED_COMPLETE;
1246        message.mFlagAttachment = hasAttachments;
1247        // Use the Intent to set flags saying this message is a reply or a forward and save the
1248        // unique id of the source message
1249        if (mSource != null && mQuotedTextArea.getVisibility() == View.VISIBLE) {
1250            message.mSourceKey = mSource.mId;
1251            // If the quote bar is visible; this must either be a reply or forward
1252            // Get the body of the source message here
1253            message.mHtmlReply = mSource.mHtml;
1254            message.mTextReply = mSource.mText;
1255            String fromAsString = Address.unpackToString(mSource.mFrom);
1256            if (isForward()) {
1257                message.mFlags |= Message.FLAG_TYPE_FORWARD;
1258                String subject = mSource.mSubject;
1259                String to = Address.unpackToString(mSource.mTo);
1260                String cc = Address.unpackToString(mSource.mCc);
1261                message.mIntroText =
1262                    getString(R.string.message_compose_fwd_header_fmt, subject, fromAsString,
1263                            to != null ? to : "", cc != null ? cc : "");
1264            } else {
1265                message.mFlags |= Message.FLAG_TYPE_REPLY;
1266                message.mIntroText =
1267                    getString(R.string.message_compose_reply_header_fmt, fromAsString);
1268            }
1269        }
1270
1271        if (includeQuotedText()) {
1272            message.mFlags &= ~Message.FLAG_NOT_INCLUDE_QUOTED_TEXT;
1273        } else {
1274            message.mFlags |= Message.FLAG_NOT_INCLUDE_QUOTED_TEXT;
1275            if (sending) {
1276                // If we are about to send a message, and not including the original message,
1277                // clear the related field.
1278                // We can't do this until the last minutes, so that the user can change their
1279                // mind later and want to include it again.
1280                mDraft.mIntroText = null;
1281                mDraft.mTextReply = null;
1282                mDraft.mHtmlReply = null;
1283
1284                // Note that mSourceKey is not cleared out as this is still considered a
1285                // reply/forward.
1286            }
1287        }
1288    }
1289
1290    private class SendOrSaveMessageTask extends EmailAsyncTask<Void, Void, Long> {
1291        private final boolean mSend;
1292        private final long mTaskId;
1293
1294        /** A context that will survive even past activity destruction. */
1295        private final Context mContext;
1296
1297        public SendOrSaveMessageTask(long taskId, boolean send) {
1298            super(null /* DO NOT cancel in onDestroy */);
1299            if (send && ActivityManager.isUserAMonkey()) {
1300                Log.d(Logging.LOG_TAG, "Inhibiting send while monkey is in charge.");
1301                send = false;
1302            }
1303            mTaskId = taskId;
1304            mSend = send;
1305            mContext = getApplicationContext();
1306
1307            sActiveSaveTasks.put(mTaskId, this);
1308        }
1309
1310        @Override
1311        protected Long doInBackground(Void... params) {
1312            synchronized (mDraft) {
1313                updateMessage(mDraft, mAccount, mAttachments.size() > 0, mSend);
1314                ContentResolver resolver = getContentResolver();
1315                if (mDraft.isSaved()) {
1316                    // Update the message
1317                    Uri draftUri =
1318                        ContentUris.withAppendedId(Message.SYNCED_CONTENT_URI, mDraft.mId);
1319                    resolver.update(draftUri, getUpdateContentValues(mDraft), null, null);
1320                    // Update the body
1321                    ContentValues values = new ContentValues();
1322                    values.put(BodyColumns.TEXT_CONTENT, mDraft.mText);
1323                    values.put(BodyColumns.TEXT_REPLY, mDraft.mTextReply);
1324                    values.put(BodyColumns.HTML_REPLY, mDraft.mHtmlReply);
1325                    values.put(BodyColumns.INTRO_TEXT, mDraft.mIntroText);
1326                    values.put(BodyColumns.SOURCE_MESSAGE_KEY, mDraft.mSourceKey);
1327                    Body.updateBodyWithMessageId(MessageCompose.this, mDraft.mId, values);
1328                } else {
1329                    // mDraft.mId is set upon return of saveToMailbox()
1330                    mController.saveToMailbox(mDraft, Mailbox.TYPE_DRAFTS);
1331                }
1332                // For any unloaded attachment, set the flag saying we need it loaded
1333                boolean hasUnloadedAttachments = false;
1334                for (Attachment attachment : mAttachments) {
1335                    if (attachment.mContentUri == null &&
1336                            ((attachment.mFlags & Attachment.FLAG_SMART_FORWARD) == 0)) {
1337                        attachment.mFlags |= Attachment.FLAG_DOWNLOAD_FORWARD;
1338                        hasUnloadedAttachments = true;
1339                        if (Email.DEBUG) {
1340                            Log.d(Logging.LOG_TAG,
1341                                    "Requesting download of attachment #" + attachment.mId);
1342                        }
1343                    }
1344                    // Make sure the UI version of the attachment has the now-correct id; we will
1345                    // use the id again when coming back from picking new attachments
1346                    if (!attachment.isSaved()) {
1347                        // this attachment is new so save it to DB.
1348                        attachment.mMessageKey = mDraft.mId;
1349                        attachment.save(MessageCompose.this);
1350                    } else if (attachment.mMessageKey != mDraft.mId) {
1351                        // We clone the attachment and save it again; otherwise, it will
1352                        // continue to point to the source message.  From this point forward,
1353                        // the attachments will be independent of the original message in the
1354                        // database; however, we still need the message on the server in order
1355                        // to retrieve unloaded attachments
1356                        attachment.mMessageKey = mDraft.mId;
1357                        ContentValues cv = attachment.toContentValues();
1358                        cv.put(Attachment.FLAGS, attachment.mFlags);
1359                        cv.put(Attachment.MESSAGE_KEY, mDraft.mId);
1360                        getContentResolver().insert(Attachment.CONTENT_URI, cv);
1361                    }
1362                }
1363
1364                if (mSend) {
1365                    // Let the user know if message sending might be delayed by background
1366                    // downlading of unloaded attachments
1367                    if (hasUnloadedAttachments) {
1368                        Utility.showToast(MessageCompose.this,
1369                                R.string.message_view_attachment_background_load);
1370                    }
1371                    mController.sendMessage(mDraft);
1372
1373                    ArrayList<CharSequence> addressTexts = new ArrayList<CharSequence>();
1374                    addressTexts.add(mToView.getText());
1375                    addressTexts.add(mCcView.getText());
1376                    addressTexts.add(mBccView.getText());
1377                    DataUsageStatUpdater updater = new DataUsageStatUpdater(mContext);
1378                    updater.updateWithRfc822Address(addressTexts);
1379                }
1380                return mDraft.mId;
1381            }
1382        }
1383
1384        private boolean shouldShowSaveToast() {
1385            // Don't show the toast when rotating, or when opening an Activity on top of this one.
1386            return !isChangingConfigurations() && !mPickingAttachment;
1387        }
1388
1389        @Override
1390        protected void onSuccess(Long draftId) {
1391            // Note that send or save tasks are always completed, even if the activity
1392            // finishes earlier.
1393            sActiveSaveTasks.remove(mTaskId);
1394            // Don't display the toast if the user is just changing the orientation
1395            if (!mSend && shouldShowSaveToast()) {
1396                Toast.makeText(mContext, R.string.message_saved_toast, Toast.LENGTH_LONG).show();
1397            }
1398        }
1399    }
1400
1401    /**
1402     * Send or save a message:
1403     * - out of the UI thread
1404     * - write to Drafts
1405     * - if send, invoke Controller.sendMessage()
1406     * - when operation is complete, display toast
1407     */
1408    private void sendOrSaveMessage(boolean send) {
1409        if (!mMessageLoaded) {
1410            Log.w(Logging.LOG_TAG,
1411                    "Attempted to save draft message prior to the state being fully loaded");
1412            return;
1413        }
1414        synchronized (sActiveSaveTasks) {
1415            mLastSaveTaskId = sNextSaveTaskId++;
1416
1417            SendOrSaveMessageTask task = new SendOrSaveMessageTask(mLastSaveTaskId, send);
1418
1419            // Ensure the tasks are executed serially so that rapid scheduling doesn't result
1420            // in inconsistent data.
1421            task.executeSerial();
1422        }
1423   }
1424
1425    private void saveIfNeeded() {
1426        if (!mDraftNeedsSaving) {
1427            return;
1428        }
1429        setMessageChanged(false);
1430        sendOrSaveMessage(false);
1431    }
1432
1433    /**
1434     * Checks whether all the email addresses listed in TO, CC, BCC are valid.
1435     */
1436    @VisibleForTesting
1437    boolean isAddressAllValid() {
1438        boolean supportsChips = ChipsUtil.supportsChipsUi();
1439        for (TextView view : new TextView[]{mToView, mCcView, mBccView}) {
1440            String addresses = view.getText().toString().trim();
1441            if (!Address.isAllValid(addresses)) {
1442                // Don't show an error message if we're using chips as the chips have
1443                // their own error state.
1444                if (!supportsChips || !(view instanceof RecipientEditTextView)) {
1445                    view.setError(getString(R.string.message_compose_error_invalid_email));
1446                }
1447                return false;
1448            }
1449        }
1450        return true;
1451    }
1452
1453    private void onSend() {
1454        if (!isAddressAllValid()) {
1455            Toast.makeText(this, getString(R.string.message_compose_error_invalid_email),
1456                           Toast.LENGTH_LONG).show();
1457        } else if (getAddresses(mToView).length == 0 &&
1458                getAddresses(mCcView).length == 0 &&
1459                getAddresses(mBccView).length == 0) {
1460            mToView.setError(getString(R.string.message_compose_error_no_recipients));
1461            Toast.makeText(this, getString(R.string.message_compose_error_no_recipients),
1462                    Toast.LENGTH_LONG).show();
1463        } else {
1464            sendOrSaveMessage(true);
1465            setMessageChanged(false);
1466            finish();
1467        }
1468    }
1469
1470    private void showQuickResponseDialog() {
1471        if (mAccount == null) {
1472            // Load not finished, bail.
1473            return;
1474        }
1475        InsertQuickResponseDialog.newInstance(null, mAccount)
1476                .show(getFragmentManager(), null);
1477    }
1478
1479    /**
1480     * Inserts the selected QuickResponse into the message body at the current cursor position.
1481     */
1482    @Override
1483    public void onQuickResponseSelected(CharSequence text) {
1484        int start = mMessageContentView.getSelectionStart();
1485        int end = mMessageContentView.getSelectionEnd();
1486        mMessageContentView.getEditableText().replace(start, end, text);
1487    }
1488
1489    private void onDiscard() {
1490        DeleteMessageConfirmationDialog.newInstance(1, null).show(getFragmentManager(), "dialog");
1491    }
1492
1493    /**
1494     * Called when ok on the "discard draft" dialog is pressed.  Actually delete the draft.
1495     */
1496    @Override
1497    public void onDeleteMessageConfirmationDialogOkPressed() {
1498        if (mDraft.mId > 0) {
1499            // By the way, we can't pass the message ID from onDiscard() to here (using a
1500            // dialog argument or whatever), because you can rotate the screen when the dialog is
1501            // shown, and during rotation we save & restore the draft.  If it's the
1502            // first save, we give it an ID at this point for the first time (and last time).
1503            // Which means it's possible for a draft to not have an ID in onDiscard(),
1504            // but here.
1505            mController.deleteMessage(mDraft.mId);
1506        }
1507        Utility.showToast(MessageCompose.this, R.string.message_discarded_toast);
1508        setMessageChanged(false);
1509        finish();
1510    }
1511
1512    /**
1513     * Handles an explicit user-initiated action to save a draft.
1514     */
1515    private void onSave() {
1516        saveIfNeeded();
1517    }
1518
1519    private void showCcBccFieldsIfFilled() {
1520        if ((mCcView.length() > 0) || (mBccView.length() > 0)) {
1521            showCcBccFields();
1522        }
1523    }
1524
1525    private void showCcBccFields() {
1526        if (mCcBccContainer.getVisibility() != View.VISIBLE) {
1527            mCcBccContainer.setVisibility(View.VISIBLE);
1528            mCcView.requestFocus();
1529            UiUtilities.setVisibilitySafe(this, R.id.add_cc_bcc, View.INVISIBLE);
1530            invalidateOptionsMenu();
1531        }
1532    }
1533
1534    /**
1535     * Kick off a picker for whatever kind of MIME types we'll accept and let Android take over.
1536     */
1537    private void onAddAttachment() {
1538        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
1539        i.addCategory(Intent.CATEGORY_OPENABLE);
1540        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
1541        i.setType(AttachmentUtilities.ACCEPTABLE_ATTACHMENT_SEND_UI_TYPES[0]);
1542        mPickingAttachment = true;
1543        startActivityForResult(
1544                Intent.createChooser(i, getString(R.string.choose_attachment_dialog_title)),
1545                ACTIVITY_REQUEST_PICK_ATTACHMENT);
1546    }
1547
1548    private Attachment loadAttachmentInfo(Uri uri) {
1549        long size = -1;
1550        ContentResolver contentResolver = getContentResolver();
1551
1552        // Load name & size independently, because not all providers support both
1553        final String name = Utility.getContentFileName(this, uri);
1554
1555        Cursor metadataCursor = contentResolver.query(uri, ATTACHMENT_META_SIZE_PROJECTION,
1556                null, null, null);
1557        if (metadataCursor != null) {
1558            try {
1559                if (metadataCursor.moveToFirst()) {
1560                    size = metadataCursor.getLong(ATTACHMENT_META_SIZE_COLUMN_SIZE);
1561                }
1562            } finally {
1563                metadataCursor.close();
1564            }
1565        }
1566
1567        // When the size is not provided, we need to determine it locally.
1568        if (size < 0) {
1569            // if the URI is a file: URI, ask file system for its size
1570            if ("file".equalsIgnoreCase(uri.getScheme())) {
1571                String path = uri.getPath();
1572                if (path != null) {
1573                    File file = new File(path);
1574                    size = file.length();  // Returns 0 for file not found
1575                }
1576            }
1577
1578            if (size <= 0) {
1579                // The size was not measurable;  This attachment is not safe to use.
1580                // Quick hack to force a relevant error into the UI
1581                // TODO: A proper announcement of the problem
1582                size = AttachmentUtilities.MAX_ATTACHMENT_UPLOAD_SIZE + 1;
1583            }
1584        }
1585
1586        Attachment attachment = new Attachment();
1587        attachment.mFileName = name;
1588        attachment.mContentUri = uri.toString();
1589        attachment.mSize = size;
1590        attachment.mMimeType = AttachmentUtilities.inferMimeTypeForUri(this, uri);
1591        return attachment;
1592    }
1593
1594    private void addAttachment(Attachment attachment) {
1595        // Before attaching the attachment, make sure it meets any other pre-attach criteria
1596        if (attachment.mSize > AttachmentUtilities.MAX_ATTACHMENT_UPLOAD_SIZE) {
1597            Toast.makeText(this, R.string.message_compose_attachment_size, Toast.LENGTH_LONG)
1598                    .show();
1599            return;
1600        }
1601
1602        mAttachments.add(attachment);
1603        updateAttachmentUi();
1604    }
1605
1606    private void updateAttachmentUi() {
1607        mAttachmentContentView.removeAllViews();
1608
1609        for (Attachment attachment : mAttachments) {
1610            // Note: allowDelete is set in two cases:
1611            // 1. First time a message (w/ attachments) is forwarded,
1612            //    where action == ACTION_FORWARD
1613            // 2. 1 -> Save -> Reopen
1614            //    but FLAG_SMART_FORWARD is already set at 1.
1615            // Even if the account supports smart-forward, attachments added
1616            // manually are still removable.
1617            final boolean allowDelete = (attachment.mFlags & Attachment.FLAG_SMART_FORWARD) == 0;
1618
1619            View view = getLayoutInflater().inflate(R.layout.attachment, mAttachmentContentView,
1620                    false);
1621            TextView nameView = UiUtilities.getView(view, R.id.attachment_name);
1622            ImageView delete = UiUtilities.getView(view, R.id.remove_attachment);
1623            TextView sizeView = UiUtilities.getView(view, R.id.attachment_size);
1624
1625            nameView.setText(attachment.mFileName);
1626            if (attachment.mSize > 0) {
1627                sizeView.setText(UiUtilities.formatSize(this, attachment.mSize));
1628            } else {
1629                sizeView.setVisibility(View.GONE);
1630            }
1631            if (allowDelete) {
1632                delete.setOnClickListener(this);
1633                delete.setTag(view);
1634            } else {
1635                delete.setVisibility(View.INVISIBLE);
1636            }
1637            view.setTag(attachment);
1638            mAttachmentContentView.addView(view);
1639        }
1640        updateAttachmentContainer();
1641    }
1642
1643    private void updateAttachmentContainer() {
1644        mAttachmentContainer.setVisibility(mAttachmentContentView.getChildCount() == 0
1645                ? View.GONE : View.VISIBLE);
1646    }
1647
1648    private void addAttachmentFromUri(Uri uri) {
1649        addAttachment(loadAttachmentInfo(uri));
1650    }
1651
1652    /**
1653     * Same as {@link #addAttachmentFromUri}, but does the mime-type check against
1654     * {@link AttachmentUtilities#ACCEPTABLE_ATTACHMENT_SEND_INTENT_TYPES}.
1655     */
1656    private void addAttachmentFromSendIntent(Uri uri) {
1657        final Attachment attachment = loadAttachmentInfo(uri);
1658        final String mimeType = attachment.mMimeType;
1659        if (!TextUtils.isEmpty(mimeType) && MimeUtility.mimeTypeMatches(mimeType,
1660                AttachmentUtilities.ACCEPTABLE_ATTACHMENT_SEND_INTENT_TYPES)) {
1661            addAttachment(attachment);
1662        }
1663    }
1664
1665    @Override
1666    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
1667        mPickingAttachment = false;
1668        if (data == null) {
1669            return;
1670        }
1671        addAttachmentFromUri(data.getData());
1672        setMessageChanged(true);
1673    }
1674
1675    private boolean includeQuotedText() {
1676        return mIncludeQuotedTextCheckBox.isChecked();
1677    }
1678
1679    public void onClick(View view) {
1680        if (handleCommand(view.getId())) {
1681            return;
1682        }
1683        switch (view.getId()) {
1684            case R.id.remove_attachment:
1685                onDeleteAttachmentIconClicked(view);
1686                break;
1687        }
1688    }
1689
1690    private void setIncludeQuotedText(boolean include, boolean updateNeedsSaving) {
1691        mIncludeQuotedTextCheckBox.setChecked(include);
1692        mQuotedText.setVisibility(mIncludeQuotedTextCheckBox.isChecked()
1693                ? View.VISIBLE : View.GONE);
1694        if (updateNeedsSaving) {
1695            setMessageChanged(true);
1696        }
1697    }
1698
1699    private void onDeleteAttachmentIconClicked(View delButtonView) {
1700        View attachmentView = (View) delButtonView.getTag();
1701        Attachment attachment = (Attachment) attachmentView.getTag();
1702        deleteAttachment(mAttachments, attachment);
1703        updateAttachmentUi();
1704        setMessageChanged(true);
1705    }
1706
1707    /**
1708     * Removes an attachment from the current message.
1709     * If the attachment has previous been saved in the db (i.e. this is a draft message which
1710     * has previously been saved), then the draft is deleted from the db.
1711     *
1712     * This does not update the UI to remove the attachment view.
1713     * @param attachments the list of attachments to delete from. Injected for tests.
1714     * @param attachment the attachment to delete
1715     */
1716    private void deleteAttachment(List<Attachment> attachments, Attachment attachment) {
1717        attachments.remove(attachment);
1718        if ((attachment.mMessageKey == mDraft.mId) && attachment.isSaved()) {
1719            final long attachmentId = attachment.mId;
1720            EmailAsyncTask.runAsyncParallel(new Runnable() {
1721                @Override
1722                public void run() {
1723                    mController.deleteAttachment(attachmentId);
1724                }
1725            });
1726        }
1727    }
1728
1729    @Override
1730    public boolean onOptionsItemSelected(MenuItem item) {
1731        if (handleCommand(item.getItemId())) {
1732            return true;
1733        }
1734        return super.onOptionsItemSelected(item);
1735    }
1736
1737    private boolean handleCommand(int viewId) {
1738        switch (viewId) {
1739        case android.R.id.home:
1740            onBack(false /* systemKey */);
1741            return true;
1742        case R.id.send:
1743            onSend();
1744            return true;
1745        case R.id.save:
1746            onSave();
1747            return true;
1748        case R.id.show_quick_text_list_dialog:
1749            showQuickResponseDialog();
1750            return true;
1751        case R.id.discard:
1752            onDiscard();
1753            return true;
1754        case R.id.include_quoted_text:
1755            // The checkbox is already toggled at this point.
1756            setIncludeQuotedText(mIncludeQuotedTextCheckBox.isChecked(), true);
1757            return true;
1758        case R.id.add_cc_bcc:
1759            showCcBccFields();
1760            return true;
1761        case R.id.add_attachment:
1762            onAddAttachment();
1763            return true;
1764        case R.id.settings:
1765            AccountSettings.actionSettings(this, mAccount.mId);
1766            return true;
1767        }
1768        return false;
1769    }
1770
1771    /**
1772     * Handle a tap to the system back key, or the "app up" button in the action bar.
1773     * @param systemKey whether or not the system key was pressed
1774     */
1775    private void onBack(boolean systemKey) {
1776        finish();
1777        if (isOpenedFromWithinApp()) {
1778            // If opened from within the app, we just close it.
1779            return;
1780        }
1781
1782        if ((isOpenedFromWidget() || !systemKey) && (mAccount != null)) {
1783            // Otherwise, need to open the main screen for the appropriate account.
1784            // Note that mAccount should always be set by the time the action bar is set up.
1785            startActivity(Welcome.createOpenAccountInboxIntent(this, mAccount.mId));
1786        }
1787    }
1788
1789    private void setAction(String action) {
1790        if (Objects.equal(action, mAction)) {
1791            return;
1792        }
1793
1794        mAction = action;
1795        onActionChanged();
1796    }
1797
1798    /**
1799     * Handles changing from reply/reply all/forward states. Note: this activity cannot transition
1800     * from a standard compose state to any of the other three states.
1801     */
1802    private void onActionChanged() {
1803        if (!hasSourceMessage()) {
1804            return;
1805        }
1806        // Temporarily remove listeners so that changing action does not invalidate and save message
1807        removeListeners();
1808
1809        processSourceMessage(mSource, mAccount);
1810
1811        // Note that the attachments might not be loaded yet, but this will safely noop
1812        // if that's the case, and the attachments will be processed when they load.
1813        if (processSourceMessageAttachments(mAttachments, mSourceAttachments, isForward())) {
1814            updateAttachmentUi();
1815            setMessageChanged(true);
1816        }
1817
1818        updateActionSelector();
1819        addListeners();
1820    }
1821
1822    /**
1823     * Updates UI components that allows the user to switch between reply/reply all/forward.
1824     */
1825    private void updateActionSelector() {
1826        ActionBar actionBar = getActionBar();
1827        if (shouldUseActionTabs()) {
1828            // Tab-based mode switching.
1829            if (actionBar.getTabCount() > 0) {
1830                selectActionTab(mAction);
1831            } else {
1832                createAndAddTab(R.string.reply_action, ACTION_REPLY);
1833                createAndAddTab(R.string.reply_all_action, ACTION_REPLY_ALL);
1834                createAndAddTab(R.string.forward_action, ACTION_FORWARD);
1835            }
1836
1837            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
1838        } else {
1839            // Spinner based mode switching.
1840            if (mActionSpinnerAdapter == null) {
1841                mActionSpinnerAdapter = new ActionSpinnerAdapter(this);
1842                actionBar.setListNavigationCallbacks(
1843                        mActionSpinnerAdapter, ACTION_SPINNER_LISTENER);
1844            }
1845            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
1846            actionBar.setSelectedNavigationItem(
1847                    ActionSpinnerAdapter.getActionPosition(mAction));
1848        }
1849        actionBar.setDisplayShowTitleEnabled(false);
1850    }
1851
1852    private final TabListener ACTION_TAB_LISTENER = new TabListener() {
1853        @Override public void onTabReselected(Tab tab, FragmentTransaction ft) {}
1854        @Override public void onTabUnselected(Tab tab, FragmentTransaction ft) {}
1855
1856        @Override
1857        public void onTabSelected(Tab tab, FragmentTransaction ft) {
1858            String action = (String) tab.getTag();
1859            setAction(action);
1860        }
1861    };
1862
1863    private final OnNavigationListener ACTION_SPINNER_LISTENER = new OnNavigationListener() {
1864        @Override
1865        public boolean onNavigationItemSelected(int itemPosition, long itemId) {
1866            setAction(ActionSpinnerAdapter.getAction(itemPosition));
1867            return true;
1868        }
1869    };
1870
1871    private static class ActionSpinnerAdapter extends ArrayAdapter<String> {
1872        public ActionSpinnerAdapter(final Context context) {
1873            super(context,
1874                    android.R.layout.simple_spinner_dropdown_item,
1875                    android.R.id.text1,
1876                    Lists.newArrayList(ACTION_REPLY, ACTION_REPLY_ALL, ACTION_FORWARD));
1877        }
1878
1879        @Override
1880        public View getDropDownView(int position, View convertView, ViewGroup parent) {
1881            View result = super.getDropDownView(position, convertView, parent);
1882            ((TextView) result.findViewById(android.R.id.text1)).setText(getDisplayValue(position));
1883            return result;
1884        }
1885
1886        @Override
1887        public View getView(int position, View convertView, ViewGroup parent) {
1888            View result = super.getView(position, convertView, parent);
1889            ((TextView) result.findViewById(android.R.id.text1)).setText(getDisplayValue(position));
1890            return result;
1891        }
1892
1893        private String getDisplayValue(int position) {
1894            switch (position) {
1895                case 0:
1896                    return getContext().getString(R.string.reply_action);
1897                case 1:
1898                    return getContext().getString(R.string.reply_all_action);
1899                case 2:
1900                    return getContext().getString(R.string.forward_action);
1901                default:
1902                    throw new IllegalArgumentException("Invalid action type for spinner");
1903            }
1904        }
1905
1906        public static String getAction(int position) {
1907            switch (position) {
1908                case 0:
1909                    return ACTION_REPLY;
1910                case 1:
1911                    return ACTION_REPLY_ALL;
1912                case 2:
1913                    return ACTION_FORWARD;
1914                default:
1915                    throw new IllegalArgumentException("Invalid action type for spinner");
1916            }
1917        }
1918
1919        public static int getActionPosition(String action) {
1920            if (ACTION_REPLY.equals(action)) {
1921                return 0;
1922            } else if (ACTION_REPLY_ALL.equals(action)) {
1923                return 1;
1924            } else if (ACTION_FORWARD.equals(action)) {
1925                return 2;
1926            }
1927            throw new IllegalArgumentException("Invalid action type for spinner");
1928        }
1929
1930    }
1931
1932    private Tab createAndAddTab(int labelResource, final String action) {
1933        ActionBar.Tab tab = getActionBar().newTab();
1934        boolean selected = mAction.equals(action);
1935        tab.setTag(action);
1936        tab.setText(getString(labelResource));
1937        tab.setTabListener(ACTION_TAB_LISTENER);
1938        getActionBar().addTab(tab, selected);
1939        return tab;
1940    }
1941
1942    private void selectActionTab(final String action) {
1943        final ActionBar actionBar = getActionBar();
1944        for (int i = 0, n = actionBar.getTabCount(); i < n; i++) {
1945            ActionBar.Tab tab = actionBar.getTabAt(i);
1946            if (action.equals(tab.getTag())) {
1947                actionBar.selectTab(tab);
1948                return;
1949            }
1950        }
1951    }
1952
1953    private boolean shouldUseActionTabs() {
1954        return getResources().getBoolean(R.bool.message_compose_action_tabs);
1955    }
1956
1957    @Override
1958    public boolean onCreateOptionsMenu(Menu menu) {
1959        super.onCreateOptionsMenu(menu);
1960        getMenuInflater().inflate(R.menu.message_compose_option, menu);
1961        return true;
1962    }
1963
1964    @Override
1965    public boolean onPrepareOptionsMenu(Menu menu) {
1966        menu.findItem(R.id.save).setEnabled(mDraftNeedsSaving);
1967        MenuItem addCcBcc = menu.findItem(R.id.add_cc_bcc);
1968        if (addCcBcc != null) {
1969            // Only available on phones.
1970            addCcBcc.setVisible(
1971                    (mCcBccContainer == null) || (mCcBccContainer.getVisibility() != View.VISIBLE));
1972        }
1973        MenuItem insertQuickResponse = menu.findItem(R.id.show_quick_text_list_dialog);
1974        insertQuickResponse.setVisible(mQuickResponsesAvailable);
1975        insertQuickResponse.setEnabled(mQuickResponsesAvailable);
1976        return true;
1977    }
1978
1979    /**
1980     * Set a message body and a signature when the Activity is launched.
1981     *
1982     * @param text the message body
1983     */
1984    @VisibleForTesting
1985    void setInitialComposeText(CharSequence text, String signature) {
1986        mMessageContentView.setText("");
1987        int textLength = 0;
1988        if (text != null) {
1989            mMessageContentView.append(text);
1990            textLength = text.length();
1991        }
1992        if (!TextUtils.isEmpty(signature)) {
1993            if (textLength == 0 || text.charAt(textLength - 1) != '\n') {
1994                mMessageContentView.append("\n");
1995            }
1996            mMessageContentView.append(signature);
1997
1998            // Reset cursor to right before the signature.
1999            mMessageContentView.setSelection(textLength);
2000        }
2001    }
2002
2003    /**
2004     * Fill all the widgets with the content found in the Intent Extra, if any.
2005     *
2006     * Note that we don't actually check the intent action  (typically VIEW, SENDTO, or SEND).
2007     * There is enough overlap in the definitions that it makes more sense to simply check for
2008     * all available data and use as much of it as possible.
2009     *
2010     * With one exception:  EXTRA_STREAM is defined as only valid for ACTION_SEND.
2011     *
2012     * @param intent the launch intent
2013     */
2014    @VisibleForTesting
2015    void initFromIntent(Intent intent) {
2016
2017        setAccount(intent);
2018
2019        // First, add values stored in top-level extras
2020        String[] extraStrings = intent.getStringArrayExtra(Intent.EXTRA_EMAIL);
2021        if (extraStrings != null) {
2022            addAddresses(mToView, extraStrings);
2023        }
2024        extraStrings = intent.getStringArrayExtra(Intent.EXTRA_CC);
2025        if (extraStrings != null) {
2026            addAddresses(mCcView, extraStrings);
2027        }
2028        extraStrings = intent.getStringArrayExtra(Intent.EXTRA_BCC);
2029        if (extraStrings != null) {
2030            addAddresses(mBccView, extraStrings);
2031        }
2032        String extraString = intent.getStringExtra(Intent.EXTRA_SUBJECT);
2033        if (extraString != null) {
2034            mSubjectView.setText(extraString);
2035        }
2036
2037        // Next, if we were invoked with a URI, try to interpret it
2038        // We'll take two courses here.  If it's mailto:, there is a specific set of rules
2039        // that define various optional fields.  However, for any other scheme, we'll simply
2040        // take the entire scheme-specific part and interpret it as a possible list of addresses.
2041        final Uri dataUri = intent.getData();
2042        if (dataUri != null) {
2043            if ("mailto".equals(dataUri.getScheme())) {
2044                initializeFromMailTo(dataUri.toString());
2045            } else {
2046                String toText = dataUri.getSchemeSpecificPart();
2047                if (toText != null) {
2048                    addAddresses(mToView, toText.split(","));
2049                }
2050            }
2051        }
2052
2053        // Next, fill in the plaintext (note, this will override mailto:?body=)
2054        CharSequence text = intent.getCharSequenceExtra(Intent.EXTRA_TEXT);
2055        setInitialComposeText(text, getAccountSignature(mAccount));
2056
2057        // Next, convert EXTRA_STREAM into an attachment
2058        if (Intent.ACTION_SEND.equals(mAction) && intent.hasExtra(Intent.EXTRA_STREAM)) {
2059            Uri uri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
2060            if (uri != null) {
2061                addAttachmentFromSendIntent(uri);
2062            }
2063        }
2064
2065        if (Intent.ACTION_SEND_MULTIPLE.equals(mAction)
2066                && intent.hasExtra(Intent.EXTRA_STREAM)) {
2067            ArrayList<Parcelable> list = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
2068            if (list != null) {
2069                for (Parcelable parcelable : list) {
2070                    Uri uri = (Uri) parcelable;
2071                    if (uri != null) {
2072                        addAttachmentFromSendIntent(uri);
2073                    }
2074                }
2075            }
2076        }
2077
2078        // Finally - expose fields that were filled in but are normally hidden, and set focus
2079        showCcBccFieldsIfFilled();
2080        setNewMessageFocus();
2081    }
2082
2083    /**
2084     * When we are launched with an intent that includes a mailto: URI, we can actually
2085     * gather quite a few of our message fields from it.
2086     *
2087     * @param mailToString the href (which must start with "mailto:").
2088     */
2089    private void initializeFromMailTo(String mailToString) {
2090
2091        // Chop up everything between mailto: and ? to find recipients
2092        int index = mailToString.indexOf("?");
2093        int length = "mailto".length() + 1;
2094        String to;
2095        try {
2096            // Extract the recipient after mailto:
2097            if (index == -1) {
2098                to = decode(mailToString.substring(length));
2099            } else {
2100                to = decode(mailToString.substring(length, index));
2101            }
2102            addAddresses(mToView, to.split(" ,"));
2103        } catch (UnsupportedEncodingException e) {
2104            Log.e(Logging.LOG_TAG, e.getMessage() + " while decoding '" + mailToString + "'");
2105        }
2106
2107        // Extract the other parameters
2108
2109        // We need to disguise this string as a URI in order to parse it
2110        Uri uri = Uri.parse("foo://" + mailToString);
2111
2112        List<String> cc = uri.getQueryParameters("cc");
2113        addAddresses(mCcView, cc.toArray(new String[cc.size()]));
2114
2115        List<String> otherTo = uri.getQueryParameters("to");
2116        addAddresses(mCcView, otherTo.toArray(new String[otherTo.size()]));
2117
2118        List<String> bcc = uri.getQueryParameters("bcc");
2119        addAddresses(mBccView, bcc.toArray(new String[bcc.size()]));
2120
2121        List<String> subject = uri.getQueryParameters("subject");
2122        if (subject.size() > 0) {
2123            mSubjectView.setText(subject.get(0));
2124        }
2125
2126        List<String> body = uri.getQueryParameters("body");
2127        if (body.size() > 0) {
2128            setInitialComposeText(body.get(0), getAccountSignature(mAccount));
2129        }
2130    }
2131
2132    private String decode(String s) throws UnsupportedEncodingException {
2133        return URLDecoder.decode(s, "UTF-8");
2134    }
2135
2136    /**
2137     * Displays quoted text from the original email
2138     */
2139    private void displayQuotedText(String textBody, String htmlBody) {
2140        // Only use plain text if there is no HTML body
2141        boolean plainTextFlag = TextUtils.isEmpty(htmlBody);
2142        String text = plainTextFlag ? textBody : htmlBody;
2143        if (text != null) {
2144            text = plainTextFlag ? EmailHtmlUtil.escapeCharacterToDisplay(text) : text;
2145            // TODO: re-enable EmailHtmlUtil.resolveInlineImage() for HTML
2146            //    EmailHtmlUtil.resolveInlineImage(getContentResolver(), mAccount,
2147            //                                     text, message, 0);
2148            mQuotedTextArea.setVisibility(View.VISIBLE);
2149            if (mQuotedText != null) {
2150                mQuotedText.loadDataWithBaseURL("email://", text, "text/html", "utf-8", null);
2151            }
2152        }
2153    }
2154
2155    /**
2156     * Given a packed address String, the address of our sending account, a view, and a list of
2157     * addressees already added to other addressing views, adds unique addressees that don't
2158     * match our address to the passed in view
2159     */
2160    private static boolean safeAddAddresses(String addrs, String ourAddress,
2161            MultiAutoCompleteTextView view, ArrayList<Address> addrList) {
2162        boolean added = false;
2163        for (Address address : Address.unpack(addrs)) {
2164            // Don't send to ourselves or already-included addresses
2165            if (!address.getAddress().equalsIgnoreCase(ourAddress) && !addrList.contains(address)) {
2166                addrList.add(address);
2167                addAddress(view, address.toString());
2168                added = true;
2169            }
2170        }
2171        return added;
2172    }
2173
2174    /**
2175     * Set up the to and cc views properly for the "reply" and "replyAll" cases.  What's important
2176     * is that we not 1) send to ourselves, and 2) duplicate addressees.
2177     * @param message the message we're replying to
2178     * @param account the account we're sending from
2179     * @param replyAll whether this is a replyAll (vs a reply)
2180     */
2181    @VisibleForTesting
2182    void setupAddressViews(Message message, Account account, boolean replyAll) {
2183        // Start clean.
2184        clearAddressViews();
2185
2186        // If Reply-to: addresses are included, use those; otherwise, use the From: address.
2187        Address[] replyToAddresses = Address.unpack(message.mReplyTo);
2188        if (replyToAddresses.length == 0) {
2189            replyToAddresses = Address.unpack(message.mFrom);
2190        }
2191
2192        // Check if ourAddress is one of the replyToAddresses to decide how to populate To: field
2193        String ourAddress = account.mEmailAddress;
2194        boolean containsOurAddress = false;
2195        for (Address address : replyToAddresses) {
2196            if (ourAddress.equalsIgnoreCase(address.getAddress())) {
2197                containsOurAddress = true;
2198                break;
2199            }
2200        }
2201
2202        if (containsOurAddress) {
2203            addAddresses(mToView, message.mTo);
2204        } else {
2205            addAddresses(mToView, replyToAddresses);
2206        }
2207
2208        if (replyAll) {
2209            // Keep a running list of addresses we're sending to
2210            ArrayList<Address> allAddresses = new ArrayList<Address>();
2211            for (Address address: replyToAddresses) {
2212                allAddresses.add(address);
2213            }
2214
2215            if (!containsOurAddress) {
2216                safeAddAddresses(message.mTo, ourAddress, mCcView, allAddresses);
2217            }
2218
2219            safeAddAddresses(message.mCc, ourAddress, mCcView, allAddresses);
2220        }
2221        showCcBccFieldsIfFilled();
2222    }
2223
2224    private void clearAddressViews() {
2225        mToView.setText("");
2226        mCcView.setText("");
2227        mBccView.setText("");
2228    }
2229
2230    /**
2231     * Pull out the parts of the now loaded source message and apply them to the new message
2232     * depending on the type of message being composed.
2233     */
2234    @VisibleForTesting
2235    void processSourceMessage(Message message, Account account) {
2236        String subject = message.mSubject;
2237        if (subject == null) {
2238            subject = "";
2239        }
2240        if (ACTION_REPLY.equals(mAction) || ACTION_REPLY_ALL.equals(mAction)) {
2241            setupAddressViews(message, account, ACTION_REPLY_ALL.equals(mAction));
2242            if (!subject.toLowerCase().startsWith("re:")) {
2243                mSubjectView.setText("Re: " + subject);
2244            } else {
2245                mSubjectView.setText(subject);
2246            }
2247            displayQuotedText(message.mText, message.mHtml);
2248            setIncludeQuotedText(true, false);
2249        } else if (ACTION_FORWARD.equals(mAction)) {
2250            clearAddressViews();
2251            mSubjectView.setText(!subject.toLowerCase().startsWith("fwd:")
2252                    ? "Fwd: " + subject : subject);
2253            displayQuotedText(message.mText, message.mHtml);
2254            setIncludeQuotedText(true, false);
2255        } else {
2256            Log.w(Logging.LOG_TAG, "Unexpected action for a call to processSourceMessage "
2257                    + mAction);
2258        }
2259        showCcBccFieldsIfFilled();
2260        setNewMessageFocus();
2261    }
2262
2263    /**
2264     * Processes the source attachments and ensures they're either included or excluded from
2265     * a list of active attachments. This can be used to add attachments for a forwarded message, or
2266     * to remove them if going from a "Forward" to a "Reply"
2267     * Uniqueness is based on filename.
2268     *
2269     * @param current the list of active attachments on the current message. Injected for tests.
2270     * @param sourceAttachments the list of attachments related with the source message. Injected
2271     *     for tests.
2272     * @param include whether or not the sourceMessages should be included or excluded from the
2273     *     current list of active attachments
2274     * @return whether or not the current attachments were modified
2275     */
2276    @VisibleForTesting
2277    boolean processSourceMessageAttachments(
2278            List<Attachment> current, List<Attachment> sourceAttachments, boolean include) {
2279
2280        // Build a map of filename to the active attachments.
2281        HashMap<String, Attachment> currentNames = new HashMap<String, Attachment>();
2282        for (Attachment attachment : current) {
2283            currentNames.put(attachment.mFileName, attachment);
2284        }
2285
2286        boolean dirty = false;
2287        if (include) {
2288            // Needs to make sure it's in the list.
2289            for (Attachment attachment : sourceAttachments) {
2290                if (!currentNames.containsKey(attachment.mFileName)) {
2291                    current.add(attachment);
2292                    dirty = true;
2293                }
2294            }
2295        } else {
2296            // Need to remove the source attachments.
2297            HashSet<String> sourceNames = new HashSet<String>();
2298            for (Attachment attachment : sourceAttachments) {
2299                if (currentNames.containsKey(attachment.mFileName)) {
2300                    deleteAttachment(current, currentNames.get(attachment.mFileName));
2301                    dirty = true;
2302                }
2303            }
2304        }
2305
2306        return dirty;
2307    }
2308
2309    /**
2310     * Set a cursor to the end of a body except a signature.
2311     */
2312    @VisibleForTesting
2313    void setMessageContentSelection(String signature) {
2314        int selection = mMessageContentView.length();
2315        if (!TextUtils.isEmpty(signature)) {
2316            int signatureLength = signature.length();
2317            int estimatedSelection = selection - signatureLength;
2318            if (estimatedSelection >= 0) {
2319                CharSequence text = mMessageContentView.getText();
2320                int i = 0;
2321                while (i < signatureLength
2322                       && text.charAt(estimatedSelection + i) == signature.charAt(i)) {
2323                    ++i;
2324                }
2325                if (i == signatureLength) {
2326                    selection = estimatedSelection;
2327                    while (selection > 0 && text.charAt(selection - 1) == '\n') {
2328                        --selection;
2329                    }
2330                }
2331            }
2332        }
2333        mMessageContentView.setSelection(selection, selection);
2334    }
2335
2336    /**
2337     * In order to accelerate typing, position the cursor in the first empty field,
2338     * or at the end of the body composition field if none are empty.  Typically, this will
2339     * play out as follows:
2340     *   Reply / Reply All - put cursor in the empty message body
2341     *   Forward - put cursor in the empty To field
2342     *   Edit Draft - put cursor in whatever field still needs entry
2343     */
2344    private void setNewMessageFocus() {
2345        if (mToView.length() == 0) {
2346            mToView.requestFocus();
2347        } else if (mSubjectView.length() == 0) {
2348            mSubjectView.requestFocus();
2349        } else {
2350            mMessageContentView.requestFocus();
2351        }
2352    }
2353
2354    private boolean isForward() {
2355        return ACTION_FORWARD.equals(mAction);
2356    }
2357
2358    /**
2359     * @return the signature for the specified account, if non-null. If the account specified is
2360     *     null or has no signature, {@code null} is returned.
2361     */
2362    private static String getAccountSignature(Account account) {
2363        return (account == null) ? null : account.mSignature;
2364    }
2365}
2366