Mindy Pereira | 8e9305e | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (c) 2011, Google Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Andy Huang | 30e2c24 | 2012-01-06 18:14:30 -0800 | [diff] [blame] | 17 | package com.android.mail.compose; |
Mindy Pereira | 8e9305e | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 18 | |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 19 | import android.app.ActionBar; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 20 | import android.app.ActivityManager; |
| 21 | import android.app.AlertDialog; |
| 22 | import android.app.Dialog; |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 23 | import android.app.ActionBar.OnNavigationListener; |
Mindy Pereira | 8e9305e | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 24 | import android.app.Activity; |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 25 | import android.app.LoaderManager.LoaderCallbacks; |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 26 | import android.content.ContentResolver; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 27 | import android.content.ContentValues; |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 28 | import android.content.Context; |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 29 | import android.content.CursorLoader; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 30 | import android.content.DialogInterface; |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 31 | import android.content.Intent; |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 32 | import android.content.Loader; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 33 | import android.content.pm.ActivityInfo; |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 34 | import android.database.Cursor; |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 35 | import android.net.Uri; |
Mindy Pereira | 8e9305e | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 36 | import android.os.Bundle; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 37 | import android.os.Handler; |
| 38 | import android.os.HandlerThread; |
Paul Westbrook | f97588b | 2012-03-20 11:11:37 -0700 | [diff] [blame] | 39 | import android.os.Parcelable; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 40 | import android.provider.BaseColumns; |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 41 | import android.text.Editable; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 42 | import android.text.Html; |
| 43 | import android.text.Spanned; |
Paul Westbrook | c182762 | 2012-01-06 11:27:12 -0800 | [diff] [blame] | 44 | import android.text.TextUtils; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 45 | import android.text.TextWatcher; |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 46 | import android.text.util.Rfc822Token; |
Mindy Pereira | c17d073 | 2011-12-29 10:46:19 -0800 | [diff] [blame] | 47 | import android.text.util.Rfc822Tokenizer; |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame^] | 48 | import android.view.Gravity; |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 49 | import android.view.LayoutInflater; |
Mindy Pereira | b47f3e2 | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 50 | import android.view.Menu; |
| 51 | import android.view.MenuInflater; |
| 52 | import android.view.MenuItem; |
Mindy Pereira | 8e9305e | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 53 | import android.view.View; |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 54 | import android.view.ViewGroup; |
Mindy Pereira | 8e9305e | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 55 | import android.view.View.OnClickListener; |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 56 | import android.view.inputmethod.BaseInputConnection; |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 57 | import android.widget.ArrayAdapter; |
Mindy Pereira | 8e9305e | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 58 | import android.widget.Button; |
Mindy Pereira | 1f93668 | 2012-03-02 11:30:33 -0800 | [diff] [blame] | 59 | import android.widget.ImageView; |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 60 | import android.widget.TextView; |
Mindy Pereira | 013194c | 2012-01-06 15:09:33 -0800 | [diff] [blame] | 61 | import android.widget.Toast; |
Mindy Pereira | 7b56a61 | 2011-12-14 12:32:28 -0800 | [diff] [blame] | 62 | |
Mindy Pereira | c17d073 | 2011-12-29 10:46:19 -0800 | [diff] [blame] | 63 | import com.android.common.Rfc822Validator; |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 64 | import com.android.mail.compose.AttachmentsView.AttachmentDeletedListener; |
Mindy Pereira | 9932dee | 2012-01-10 16:09:50 -0800 | [diff] [blame] | 65 | import com.android.mail.compose.AttachmentsView.AttachmentFailureException; |
Mindy Pereira | 5a85e2b | 2012-01-11 09:53:32 -0800 | [diff] [blame] | 66 | import com.android.mail.compose.FromAddressSpinner.OnAccountChangedListener; |
Andy Huang | 30e2c24 | 2012-01-06 18:14:30 -0800 | [diff] [blame] | 67 | import com.android.mail.compose.QuotedTextView.RespondInlineListener; |
Mindy Pereira | 33fe908 | 2012-01-09 16:24:30 -0800 | [diff] [blame] | 68 | import com.android.mail.providers.Account; |
Andy Huang | 30e2c24 | 2012-01-06 18:14:30 -0800 | [diff] [blame] | 69 | import com.android.mail.providers.Address; |
| 70 | import com.android.mail.providers.Attachment; |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 71 | import com.android.mail.providers.Message; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 72 | import com.android.mail.providers.MessageModification; |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 73 | import com.android.mail.providers.Settings; |
Andy Huang | 30e2c24 | 2012-01-06 18:14:30 -0800 | [diff] [blame] | 74 | import com.android.mail.providers.UIProvider; |
Andy Huang | 30e2c24 | 2012-01-06 18:14:30 -0800 | [diff] [blame] | 75 | import com.android.mail.R; |
Paul Westbrook | 92227f6 | 2012-03-20 10:32:51 -0700 | [diff] [blame] | 76 | import com.android.mail.utils.AccountUtils; |
Andy Huang | 30e2c24 | 2012-01-06 18:14:30 -0800 | [diff] [blame] | 77 | import com.android.mail.utils.LogUtils; |
Andy Huang | 30e2c24 | 2012-01-06 18:14:30 -0800 | [diff] [blame] | 78 | import com.android.mail.utils.Utils; |
Mindy Pereira | c17d073 | 2011-12-29 10:46:19 -0800 | [diff] [blame] | 79 | import com.android.ex.chips.RecipientEditTextView; |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 80 | import com.google.common.annotations.VisibleForTesting; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 81 | import com.google.common.collect.Lists; |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 82 | import com.google.common.collect.Sets; |
Mindy Pereira | 8e9305e | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 83 | |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame^] | 84 | import java.io.UnsupportedEncodingException; |
| 85 | import java.net.URLDecoder; |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 86 | import java.util.ArrayList; |
Paul Westbrook | bb87b7f | 2012-03-20 16:20:07 -0700 | [diff] [blame] | 87 | import java.util.Arrays; |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 88 | import java.util.Collection; |
Mindy Pereira | 75f6663 | 2012-01-11 11:42:02 -0800 | [diff] [blame] | 89 | import java.util.HashMap; |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 90 | import java.util.HashSet; |
| 91 | import java.util.List; |
Paul Westbrook | 1c078cf | 2012-03-20 16:18:51 -0700 | [diff] [blame] | 92 | import java.util.Map.Entry; |
Paul Westbrook | bb87b7f | 2012-03-20 16:20:07 -0700 | [diff] [blame] | 93 | import java.util.Set; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 94 | import java.util.concurrent.ConcurrentHashMap; |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 95 | |
| 96 | public class ComposeActivity extends Activity implements OnClickListener, OnNavigationListener, |
Mindy Pereira | 5a85e2b | 2012-01-11 09:53:32 -0800 | [diff] [blame] | 97 | RespondInlineListener, DialogInterface.OnClickListener, TextWatcher, |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 98 | AttachmentDeletedListener, OnAccountChangedListener, LoaderCallbacks<Cursor> { |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 99 | // Identifiers for which type of composition this is |
Paul Westbrook | bb87b7f | 2012-03-20 16:20:07 -0700 | [diff] [blame] | 100 | static final int COMPOSE = -1; |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 101 | static final int REPLY = 0; |
| 102 | static final int REPLY_ALL = 1; |
| 103 | static final int FORWARD = 2; |
Paul Westbrook | bb87b7f | 2012-03-20 16:20:07 -0700 | [diff] [blame] | 104 | static final int EDIT_DRAFT = 3; |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 105 | |
| 106 | // Integer extra holding one of the above compose action |
| 107 | private static final String EXTRA_ACTION = "action"; |
| 108 | |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame^] | 109 | private static final String UTF8_ENCODING_NAME = "UTF-8"; |
| 110 | |
| 111 | private static final String MAIL_TO = "mailto"; |
| 112 | |
| 113 | private static final String GMAIL_FROM = "gmailfrom"; |
| 114 | |
| 115 | private static final String EXTRA_SUBJECT = "subject"; |
| 116 | |
| 117 | private static final String EXTRA_BODY = "body"; |
| 118 | |
| 119 | // Extra that we can get passed from other activities |
| 120 | private static final String EXTRA_TO = "to"; |
| 121 | private static final String EXTRA_CC = "cc"; |
| 122 | private static final String EXTRA_BCC = "bcc"; |
| 123 | |
| 124 | // List of all the fields |
| 125 | static final String[] ALL_EXTRAS = { EXTRA_SUBJECT, EXTRA_BODY, EXTRA_TO, EXTRA_CC, EXTRA_BCC }; |
| 126 | |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 127 | private static SendOrSaveCallback sTestSendOrSaveCallback = null; |
| 128 | // Map containing information about requests to create new messages, and the id of the |
| 129 | // messages that were the result of those requests. |
| 130 | // |
| 131 | // This map is used when the activity that initiated the save a of a new message, is killed |
| 132 | // before the save has completed (and when we know the id of the newly created message). When |
| 133 | // a save is completed, the service that is running in the background, will update the map |
| 134 | // |
| 135 | // When a new ComposeActivity instance is created, it will attempt to use the information in |
| 136 | // the previously instantiated map. If ComposeActivity.onCreate() is called, with a bundle |
| 137 | // (restoring data from a previous instance), and the map hasn't been created, we will attempt |
| 138 | // to populate the map with data stored in shared preferences. |
| 139 | private static ConcurrentHashMap<Integer, Long> sRequestMessageIdMap = null; |
| 140 | // Key used to store the above map |
| 141 | private static final String CACHED_MESSAGE_REQUEST_IDS_KEY = "cache-message-request-ids"; |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 142 | /** |
| 143 | * Notifies the {@code Activity} that the caller is an Email |
| 144 | * {@code Activity}, so that the back behavior may be modified accordingly. |
| 145 | * |
| 146 | * @see #onAppUpPressed |
| 147 | */ |
| 148 | private static final String EXTRA_FROM_EMAIL_TASK = "fromemail"; |
| 149 | |
Paul Westbrook | f97588b | 2012-03-20 11:11:37 -0700 | [diff] [blame] | 150 | static final String EXTRA_ATTACHMENTS = "attachments"; |
| 151 | |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 152 | // If this is a reply/forward then this extra will hold the original message |
| 153 | private static final String EXTRA_IN_REFERENCE_TO_MESSAGE = "in-reference-to-message"; |
Paul Westbrook | bb87b7f | 2012-03-20 16:20:07 -0700 | [diff] [blame] | 154 | // If this is an action to edit an existing draft messagge, this extra will hold the |
| 155 | // draft message |
| 156 | private static final String ORIGINAL_DRAFT_MESSAGE = "original-draft-message"; |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 157 | private static final String END_TOKEN = ", "; |
Mindy Pereira | 013194c | 2012-01-06 15:09:33 -0800 | [diff] [blame] | 158 | private static final String LOG_TAG = new LogUtils().getLogTag(); |
| 159 | // Request numbers for activities we start |
| 160 | private static final int RESULT_PICK_ATTACHMENT = 1; |
| 161 | private static final int RESULT_CREATE_ACCOUNT = 2; |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 162 | private static final int ACCOUNT_SETTINGS_LOADER = 0; |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame^] | 163 | // TODO(mindyp) set mime-type for auto send? |
| 164 | private static final String AUTO_SEND_ACTION = "com.android.mail.action.AUTO_SEND"; |
| 165 | |
| 166 | // Max size for attachments (5 megs). Will be overridden by account settings if found. |
| 167 | // TODO(mindyp): read this from account settings? |
| 168 | private static final int DEFAULT_MAX_ATTACHMENT_SIZE = 25 * 1024 * 1024; |
Mindy Pereira | 8e9305e | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 169 | |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 170 | /** |
| 171 | * A single thread for running tasks in the background. |
| 172 | */ |
| 173 | private Handler mSendSaveTaskHandler = null; |
Mindy Pereira | c17d073 | 2011-12-29 10:46:19 -0800 | [diff] [blame] | 174 | private RecipientEditTextView mTo; |
| 175 | private RecipientEditTextView mCc; |
| 176 | private RecipientEditTextView mBcc; |
Mindy Pereira | 8e9305e | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 177 | private Button mCcBccButton; |
| 178 | private CcBccView mCcBccView; |
Mindy Pereira | 7b56a61 | 2011-12-14 12:32:28 -0800 | [diff] [blame] | 179 | private AttachmentsView mAttachmentsView; |
Mindy Pereira | 33fe908 | 2012-01-09 16:24:30 -0800 | [diff] [blame] | 180 | private Account mAccount; |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 181 | private Settings mCachedSettings; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 182 | private Rfc822Validator mValidator; |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 183 | private TextView mSubject; |
| 184 | |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 185 | private ComposeModeAdapter mComposeModeAdapter; |
| 186 | private int mComposeMode = -1; |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 187 | private boolean mForward; |
| 188 | private String mRecipient; |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 189 | private QuotedTextView mQuotedTextView; |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 190 | private TextView mBodyView; |
Mindy Pereira | 1a95a57 | 2012-01-05 12:21:29 -0800 | [diff] [blame] | 191 | private View mFromStatic; |
Mindy Pereira | 2eb1732 | 2012-03-07 10:07:34 -0800 | [diff] [blame] | 192 | private TextView mFromStaticText; |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 193 | private View mFromSpinnerWrapper; |
Mindy Pereira | 5a85e2b | 2012-01-11 09:53:32 -0800 | [diff] [blame] | 194 | private FromAddressSpinner mFromSpinner; |
Mindy Pereira | 013194c | 2012-01-06 15:09:33 -0800 | [diff] [blame] | 195 | private boolean mAddingAttachment; |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 196 | private boolean mAttachmentsChanged; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 197 | private boolean mTextChanged; |
| 198 | private boolean mReplyFromChanged; |
| 199 | private MenuItem mSave; |
| 200 | private MenuItem mSend; |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 201 | private String mRefMessageId; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 202 | private AlertDialog mRecipientErrorDialog; |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 203 | private AlertDialog mSendConfirmDialog; |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 204 | private Message mRefMessage; |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 205 | private long mDraftId = UIProvider.INVALID_MESSAGE_ID; |
| 206 | private Message mDraft; |
| 207 | private Object mDraftLock = new Object(); |
Mindy Pereira | 1f93668 | 2012-03-02 11:30:33 -0800 | [diff] [blame] | 208 | private ImageView mAttachmentsButton; |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 209 | |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 210 | /** |
| 211 | * Can be called from a non-UI thread. |
| 212 | */ |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 213 | public static void editDraft(Context launcher, Account account, Message message) { |
Paul Westbrook | bb87b7f | 2012-03-20 16:20:07 -0700 | [diff] [blame] | 214 | launch(launcher, account, message, EDIT_DRAFT); |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 215 | } |
| 216 | |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 217 | /** |
| 218 | * Can be called from a non-UI thread. |
| 219 | */ |
Mindy Pereira | 33fe908 | 2012-01-09 16:24:30 -0800 | [diff] [blame] | 220 | public static void compose(Context launcher, Account account) { |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 221 | launch(launcher, account, null, COMPOSE); |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Can be called from a non-UI thread. |
| 226 | */ |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 227 | public static void reply(Context launcher, Account account, Message message) { |
| 228 | launch(launcher, account, message, REPLY); |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Can be called from a non-UI thread. |
| 233 | */ |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 234 | public static void replyAll(Context launcher, Account account, Message message) { |
| 235 | launch(launcher, account, message, REPLY_ALL); |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Can be called from a non-UI thread. |
| 240 | */ |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 241 | public static void forward(Context launcher, Account account, Message message) { |
| 242 | launch(launcher, account, message, FORWARD); |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 243 | } |
| 244 | |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 245 | private static void launch(Context launcher, Account account, Message message, int action) { |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 246 | Intent intent = new Intent(launcher, ComposeActivity.class); |
| 247 | intent.putExtra(EXTRA_FROM_EMAIL_TASK, true); |
| 248 | intent.putExtra(EXTRA_ACTION, action); |
| 249 | intent.putExtra(Utils.EXTRA_ACCOUNT, account); |
Paul Westbrook | bb87b7f | 2012-03-20 16:20:07 -0700 | [diff] [blame] | 250 | if (action == EDIT_DRAFT) { |
| 251 | intent.putExtra(ORIGINAL_DRAFT_MESSAGE, message); |
| 252 | } else { |
| 253 | intent.putExtra(EXTRA_IN_REFERENCE_TO_MESSAGE, message); |
| 254 | } |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 255 | launcher.startActivity(intent); |
| 256 | } |
Mindy Pereira | 8e9305e | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 257 | |
| 258 | @Override |
| 259 | public void onCreate(Bundle savedInstanceState) { |
| 260 | super.onCreate(savedInstanceState); |
Mindy Pereira | 3528d36 | 2012-01-05 14:39:44 -0800 | [diff] [blame] | 261 | setContentView(R.layout.compose); |
| 262 | findViews(); |
Mindy Pereira | 818143e | 2012-01-11 13:59:49 -0800 | [diff] [blame] | 263 | Intent intent = getIntent(); |
Paul Westbrook | 92227f6 | 2012-03-20 10:32:51 -0700 | [diff] [blame] | 264 | |
| 265 | Account account = (Account)intent.getParcelableExtra(Utils.EXTRA_ACCOUNT); |
| 266 | if (account == null) { |
| 267 | final Account[] syncingAccounts = AccountUtils.getSyncingAccounts(this); |
| 268 | if (syncingAccounts.length > 0) { |
| 269 | account = syncingAccounts[0]; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | setAccount(account); |
Mindy Pereira | 818143e | 2012-01-11 13:59:49 -0800 | [diff] [blame] | 274 | if (mAccount == null) { |
| 275 | return; |
| 276 | } |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 277 | int action = intent.getIntExtra(EXTRA_ACTION, COMPOSE); |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 278 | mRefMessage = (Message) intent.getParcelableExtra(EXTRA_IN_REFERENCE_TO_MESSAGE); |
Mindy Pereira | 29ef1b8 | 2012-01-13 11:26:21 -0800 | [diff] [blame] | 279 | if ((action == REPLY || action == REPLY_ALL || action == FORWARD)) { |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame^] | 280 | if (mRefMessage != null) { |
| 281 | initFromRefMessage(action, mAccount.name); |
| 282 | } |
Paul Westbrook | bb87b7f | 2012-03-20 16:20:07 -0700 | [diff] [blame] | 283 | } else if (action == EDIT_DRAFT) { |
| 284 | // Initialize the message from the message in the intent |
| 285 | final Message message = (Message) intent.getParcelableExtra(ORIGINAL_DRAFT_MESSAGE); |
| 286 | |
| 287 | initFromMessage(message); |
| 288 | |
| 289 | // Update the action to the draft type of the previous draft |
| 290 | switch (message.draftType) { |
| 291 | case UIProvider.DraftType.REPLY: |
| 292 | action = REPLY; |
| 293 | break; |
| 294 | case UIProvider.DraftType.REPLY_ALL: |
| 295 | action = REPLY_ALL; |
| 296 | break; |
| 297 | case UIProvider.DraftType.FORWARD: |
| 298 | action = FORWARD; |
| 299 | break; |
| 300 | case UIProvider.DraftType.COMPOSE: |
| 301 | default: |
| 302 | action = COMPOSE; |
| 303 | break; |
| 304 | } |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame^] | 305 | } else { |
| 306 | initFromExtras(intent); |
Paul Westbrook | bb87b7f | 2012-03-20 16:20:07 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | if (action == COMPOSE) { |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 310 | mQuotedTextView.setVisibility(View.GONE); |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 311 | } |
Mindy Pereira | 818143e | 2012-01-11 13:59:49 -0800 | [diff] [blame] | 312 | initRecipients(); |
Paul Westbrook | f97588b | 2012-03-20 11:11:37 -0700 | [diff] [blame] | 313 | initAttachmentsFromIntent(intent); |
Mindy Pereira | 1a95a57 | 2012-01-05 12:21:29 -0800 | [diff] [blame] | 314 | initActionBar(action); |
Mindy Pereira | 5a85e2b | 2012-01-11 09:53:32 -0800 | [diff] [blame] | 315 | initFromSpinner(); |
Mindy Pereira | 75f6663 | 2012-01-11 11:42:02 -0800 | [diff] [blame] | 316 | initChangeListeners(); |
Mindy Pereira | 1a95a57 | 2012-01-05 12:21:29 -0800 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | @Override |
| 320 | protected void onResume() { |
| 321 | super.onResume(); |
| 322 | // Update the from spinner as other accounts |
| 323 | // may now be available. |
Mindy Pereira | 818143e | 2012-01-11 13:59:49 -0800 | [diff] [blame] | 324 | if (mFromSpinner != null && mAccount != null) { |
| 325 | mFromSpinner.asyncInitFromSpinner(); |
| 326 | } |
Mindy Pereira | 1a95a57 | 2012-01-05 12:21:29 -0800 | [diff] [blame] | 327 | } |
| 328 | |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 329 | @Override |
| 330 | protected void onPause() { |
| 331 | super.onPause(); |
| 332 | |
| 333 | if (mSendConfirmDialog != null) { |
| 334 | mSendConfirmDialog.dismiss(); |
| 335 | } |
| 336 | if (mRecipientErrorDialog != null) { |
| 337 | mRecipientErrorDialog.dismiss(); |
| 338 | } |
| 339 | |
| 340 | saveIfNeeded(); |
| 341 | } |
| 342 | |
| 343 | @Override |
| 344 | protected final void onActivityResult(int request, int result, Intent data) { |
| 345 | mAddingAttachment = false; |
| 346 | |
| 347 | if (result == RESULT_OK && request == RESULT_PICK_ATTACHMENT) { |
| 348 | addAttachmentAndUpdateView(data); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | @Override |
| 353 | public final void onSaveInstanceState(Bundle state) { |
| 354 | super.onSaveInstanceState(state); |
| 355 | |
| 356 | // onSaveInstanceState is only called if the user might come back to this activity so it is |
| 357 | // not an ideal location to save the draft. However, if we have never saved the draft before |
| 358 | // we have to save it here in order to have an id to save in the bundle. |
| 359 | saveIfNeededOnOrientationChanged(); |
| 360 | } |
| 361 | |
Mindy Pereira | 818143e | 2012-01-11 13:59:49 -0800 | [diff] [blame] | 362 | @VisibleForTesting |
| 363 | void setAccount(Account account) { |
Mindy Pereira | 23e9fde | 2012-03-20 15:08:24 -0700 | [diff] [blame] | 364 | assert(account != null); |
| 365 | if (!account.equals(mAccount)) { |
| 366 | mAccount = account; |
| 367 | } |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 368 | getLoaderManager().restartLoader(ACCOUNT_SETTINGS_LOADER, null, this); |
Mindy Pereira | 818143e | 2012-01-11 13:59:49 -0800 | [diff] [blame] | 369 | } |
| 370 | |
Mindy Pereira | 1a95a57 | 2012-01-05 12:21:29 -0800 | [diff] [blame] | 371 | private void initFromSpinner() { |
Mindy Pereira | 5a85e2b | 2012-01-11 09:53:32 -0800 | [diff] [blame] | 372 | mFromSpinner.setCurrentAccount(mAccount); |
| 373 | mFromSpinner.asyncInitFromSpinner(); |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 374 | boolean showSpinner = mFromSpinner.getCount() > 1; |
| 375 | // If there is only 1 account, just show that account. |
| 376 | // Otherwise, give the user the ability to choose which account to send |
| 377 | // mail from / save drafts to. |
| 378 | mFromStatic.setVisibility( |
| 379 | showSpinner ? View.GONE : View.VISIBLE); |
Mindy Pereira | 2eb1732 | 2012-03-07 10:07:34 -0800 | [diff] [blame] | 380 | mFromStaticText.setText(mAccount.name); |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 381 | mFromSpinnerWrapper.setVisibility( |
| 382 | showSpinner ? View.VISIBLE : View.GONE); |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | private void findViews() { |
Mindy Pereira | a26b54e | 2012-01-06 12:54:33 -0800 | [diff] [blame] | 386 | mCcBccButton = (Button) findViewById(R.id.add_cc_bcc); |
Mindy Pereira | 8e9305e | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 387 | if (mCcBccButton != null) { |
| 388 | mCcBccButton.setOnClickListener(this); |
| 389 | } |
| 390 | mCcBccView = (CcBccView) findViewById(R.id.cc_bcc_wrapper); |
Mindy Pereira | 7b56a61 | 2011-12-14 12:32:28 -0800 | [diff] [blame] | 391 | mAttachmentsView = (AttachmentsView)findViewById(R.id.attachments); |
Mindy Pereira | 1f93668 | 2012-03-02 11:30:33 -0800 | [diff] [blame] | 392 | mAttachmentsButton = (ImageView) findViewById(R.id.add_attachment); |
| 393 | if (mAttachmentsButton != null) { |
| 394 | mAttachmentsButton.setOnClickListener(this); |
| 395 | } |
Mindy Pereira | 818143e | 2012-01-11 13:59:49 -0800 | [diff] [blame] | 396 | mTo = (RecipientEditTextView) findViewById(R.id.to); |
| 397 | mCc = (RecipientEditTextView) findViewById(R.id.cc); |
| 398 | mBcc = (RecipientEditTextView) findViewById(R.id.bcc); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 399 | // TODO: add special chips text change watchers before adding |
| 400 | // this as a text changed watcher to the to, cc, bcc fields. |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 401 | mSubject = (TextView) findViewById(R.id.subject); |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 402 | mQuotedTextView = (QuotedTextView) findViewById(R.id.quoted_text_view); |
| 403 | mQuotedTextView.setRespondInlineListener(this); |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 404 | mBodyView = (TextView) findViewById(R.id.body); |
Mindy Pereira | 1a95a57 | 2012-01-05 12:21:29 -0800 | [diff] [blame] | 405 | mFromStatic = findViewById(R.id.static_from_content); |
Mindy Pereira | 2eb1732 | 2012-03-07 10:07:34 -0800 | [diff] [blame] | 406 | mFromStaticText = (TextView) findViewById(R.id.from_account_name); |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 407 | mFromSpinnerWrapper = findViewById(R.id.spinner_from_content); |
Mindy Pereira | 5a85e2b | 2012-01-11 09:53:32 -0800 | [diff] [blame] | 408 | mFromSpinner = (FromAddressSpinner) findViewById(R.id.from_picker); |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 409 | } |
| 410 | |
Mindy Pereira | 75f6663 | 2012-01-11 11:42:02 -0800 | [diff] [blame] | 411 | // Now that the message has been initialized from any existing draft or |
| 412 | // ref message data, set up listeners for any changes that occur to the |
| 413 | // message. |
| 414 | private void initChangeListeners() { |
| 415 | mSubject.addTextChangedListener(this); |
| 416 | mBodyView.addTextChangedListener(this); |
| 417 | mTo.addTextChangedListener(new RecipientTextWatcher(mTo, this)); |
| 418 | mCc.addTextChangedListener(new RecipientTextWatcher(mCc, this)); |
| 419 | mBcc.addTextChangedListener(new RecipientTextWatcher(mBcc, this)); |
| 420 | mFromSpinner.setOnAccountChangedListener(this); |
Mindy Pereira | 818143e | 2012-01-11 13:59:49 -0800 | [diff] [blame] | 421 | mAttachmentsView.setAttachmentChangesListener(this); |
Mindy Pereira | 75f6663 | 2012-01-11 11:42:02 -0800 | [diff] [blame] | 422 | } |
| 423 | |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 424 | private void initActionBar(int action) { |
| 425 | mComposeMode = action; |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 426 | ActionBar actionBar = getActionBar(); |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 427 | if (action == ComposeActivity.COMPOSE) { |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 428 | actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); |
| 429 | actionBar.setTitle(R.string.compose); |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 430 | } else { |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 431 | actionBar.setTitle(null); |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 432 | if (mComposeModeAdapter == null) { |
| 433 | mComposeModeAdapter = new ComposeModeAdapter(this); |
| 434 | } |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 435 | actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); |
| 436 | actionBar.setListNavigationCallbacks(mComposeModeAdapter, this); |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 437 | switch (action) { |
| 438 | case ComposeActivity.REPLY: |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 439 | actionBar.setSelectedNavigationItem(0); |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 440 | break; |
| 441 | case ComposeActivity.REPLY_ALL: |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 442 | actionBar.setSelectedNavigationItem(1); |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 443 | break; |
| 444 | case ComposeActivity.FORWARD: |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 445 | actionBar.setSelectedNavigationItem(2); |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 446 | break; |
| 447 | } |
| 448 | } |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 449 | actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME, |
| 450 | ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME); |
| 451 | actionBar.setHomeButtonEnabled(true); |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 452 | } |
| 453 | |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 454 | private void initFromRefMessage(int action, String recipientAddress) { |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame^] | 455 | mRefMessageId = mRefMessage.refMessageId; |
| 456 | setSubject(mRefMessage, action); |
| 457 | // Setup recipients |
| 458 | if (action == FORWARD) { |
| 459 | mForward = true; |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 460 | } |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame^] | 461 | initRecipientsFromRefMessage(recipientAddress, mRefMessage, action); |
| 462 | initBodyFromRefMessage(mRefMessage, action); |
| 463 | if (action == ComposeActivity.FORWARD || mAttachmentsChanged) { |
| 464 | initAttachments(mRefMessage); |
| 465 | } |
| 466 | updateHideOrShowCcBcc(); |
Mindy Pereira | c17d073 | 2011-12-29 10:46:19 -0800 | [diff] [blame] | 467 | } |
| 468 | |
Paul Westbrook | bb87b7f | 2012-03-20 16:20:07 -0700 | [diff] [blame] | 469 | private void initFromMessage(Message message) { |
| 470 | LogUtils.d(LOG_TAG, "Intializing draft from previous draft message"); |
| 471 | |
| 472 | mDraft = message; |
| 473 | mDraftId = message.id; |
| 474 | mSubject.setText(message.subject); |
| 475 | mForward = message.draftType == UIProvider.DraftType.FORWARD; |
| 476 | final List<String> toAddresses = Arrays.asList(message.getToAddresses()); |
| 477 | addToAddresses(toAddresses); |
| 478 | addCcAddresses(Arrays.asList(message.getCcAddresses()), toAddresses); |
| 479 | addBccAddresses(Arrays.asList(message.getBccAddresses())); |
| 480 | |
| 481 | // Set the body |
| 482 | if (!TextUtils.isEmpty(message.bodyHtml)) { |
| 483 | mBodyView.setText(Html.fromHtml(message.bodyHtml)); |
| 484 | } else { |
| 485 | mBodyView.setText(message.bodyText); |
| 486 | } |
| 487 | |
| 488 | // TODO: load attachments from the previous message |
| 489 | // TODO: set the from address spinner to the right from account |
| 490 | // TODO: initialize quoted text value |
| 491 | } |
| 492 | |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame^] | 493 | /** |
| 494 | * Fill all the widgets with the content found in the Intent Extra, if any. |
| 495 | * Also apply the same style to all widgets. Note: if initFromExtras is |
| 496 | * called as a result of switching between reply, reply all, and forward per |
| 497 | * the latest revision of Gmail, and the user has already made changes to |
| 498 | * attachments on a previous incarnation of the message (as a reply, reply |
| 499 | * all, or forward), the original attachments from the message will not be |
| 500 | * re-instantiated. The user's changes will be respected. This follows the |
| 501 | * web gmail interaction. |
| 502 | */ |
| 503 | public void initFromExtras(Intent intent) { |
| 504 | |
| 505 | // If we were invoked with a SENDTO intent, the value |
| 506 | // should take precedence |
| 507 | final Uri dataUri = intent.getData(); |
| 508 | if (dataUri != null) { |
| 509 | if (MAIL_TO.equals(dataUri.getScheme())) { |
| 510 | initFromMailTo(dataUri.toString()); |
| 511 | } else { |
| 512 | if (!GMAIL_FROM.equals(dataUri.getScheme())) { |
| 513 | String toText = dataUri.getSchemeSpecificPart(); |
| 514 | if (toText != null) { |
| 515 | mTo.setText(""); |
| 516 | addToAddresses(Arrays.asList(toText.split(","))); |
| 517 | } |
| 518 | } |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | String[] extraStrings = intent.getStringArrayExtra(Intent.EXTRA_EMAIL); |
| 523 | if (extraStrings != null) { |
| 524 | addToAddresses(Arrays.asList(extraStrings)); |
| 525 | } |
| 526 | extraStrings = intent.getStringArrayExtra(Intent.EXTRA_CC); |
| 527 | if (extraStrings != null) { |
| 528 | addCcAddresses(Arrays.asList(extraStrings), null); |
| 529 | } |
| 530 | extraStrings = intent.getStringArrayExtra(Intent.EXTRA_BCC); |
| 531 | if (extraStrings != null) { |
| 532 | addBccAddresses(Arrays.asList(extraStrings)); |
| 533 | } |
| 534 | |
| 535 | String extraString = intent.getStringExtra(Intent.EXTRA_SUBJECT); |
| 536 | if (extraString != null) { |
| 537 | mSubject.setText(extraString); |
| 538 | } |
| 539 | |
| 540 | for (String extra : ALL_EXTRAS) { |
| 541 | if (intent.hasExtra(extra)) { |
| 542 | String value = intent.getStringExtra(extra); |
| 543 | if (EXTRA_TO.equals(extra)) { |
| 544 | addToAddresses(Arrays.asList(value.split(","))); |
| 545 | } else if (EXTRA_CC.equals(extra)) { |
| 546 | addCcAddresses(Arrays.asList(value.split(",")), null); |
| 547 | } else if (EXTRA_BCC.equals(extra)) { |
| 548 | addBccAddresses(Arrays.asList(value.split(","))); |
| 549 | } else if (EXTRA_SUBJECT.equals(extra)) { |
| 550 | mSubject.setText(value); |
| 551 | } else if (EXTRA_BODY.equals(extra)) { |
| 552 | setBody(value, true /* with signature */); |
| 553 | } |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | Bundle extras = intent.getExtras(); |
| 558 | if (extras != null) { |
| 559 | final String action = intent.getAction(); |
| 560 | CharSequence text = extras.getCharSequence(Intent.EXTRA_TEXT); |
| 561 | if (text != null) { |
| 562 | setBody(text, true /* with signature */); |
| 563 | } |
| 564 | |
| 565 | // TODO(mindyp): read this from account settings. |
| 566 | int maxSize = DEFAULT_MAX_ATTACHMENT_SIZE; |
| 567 | int totalSize = 0; |
| 568 | |
| 569 | // Take care of attachments passed in by the extras. |
| 570 | if (!mAttachmentsChanged) { |
| 571 | if (extras.containsKey(EXTRA_ATTACHMENTS)) { |
| 572 | String[] uris = (String[]) extras.getSerializable(EXTRA_ATTACHMENTS); |
| 573 | for (String uriString : uris) { |
| 574 | final Uri uri = Uri.parse(uriString); |
| 575 | long size; |
| 576 | try { |
| 577 | size = mAttachmentsView.addAttachment(mAccount, uri, |
| 578 | false /* doSave */, true /* local file */); |
| 579 | } catch (AttachmentFailureException e) { |
| 580 | // A toast has already been shown to the user, |
| 581 | // just break out of the loop. |
| 582 | LogUtils.e(LOG_TAG, e, "Error adding attachment"); |
| 583 | finish(); |
| 584 | return; |
| 585 | } |
| 586 | } |
| 587 | mAttachmentsChanged = true; |
| 588 | } |
| 589 | if ((Intent.ACTION_SEND.equals(action) |
| 590 | || AUTO_SEND_ACTION.equals(action)) |
| 591 | && extras.containsKey(Intent.EXTRA_STREAM)) { |
| 592 | Uri uri = (Uri) extras.getParcelable(Intent.EXTRA_STREAM); |
| 593 | try { |
| 594 | mAttachmentsView.addAttachment(mAccount, uri, true /* doSave */, |
| 595 | true /* local file */); |
| 596 | } catch (AttachmentFailureException e) { |
| 597 | // A toast has already been shown to the user, so just |
| 598 | // exit. |
| 599 | LogUtils.e(LOG_TAG, e, "Error adding attachment"); |
| 600 | finish(); |
| 601 | return; |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | if (Intent.ACTION_SEND_MULTIPLE.equals(action) |
| 606 | && extras.containsKey(Intent.EXTRA_STREAM)) { |
| 607 | ArrayList<Parcelable> uris = extras.getParcelableArrayList(Intent.EXTRA_STREAM); |
| 608 | for (Parcelable uri : uris) { |
| 609 | try { |
| 610 | mAttachmentsView.addAttachment(mAccount, |
| 611 | (Uri) uri, false /* doSave */, true /* local file */); |
| 612 | } catch (AttachmentFailureException e) { |
| 613 | // A toast has already been shown to the user, |
| 614 | // just break out of the loop. |
| 615 | LogUtils.e(LOG_TAG, e, "Error adding attachment"); |
| 616 | finish(); |
| 617 | return; |
| 618 | } |
| 619 | } |
| 620 | mAttachmentsChanged = true; |
| 621 | } |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | updateHideOrShowCcBcc(); |
| 626 | } |
| 627 | |
| 628 | @VisibleForTesting |
| 629 | protected String decodeEmailInUri(String s) throws UnsupportedEncodingException { |
| 630 | // TODO: handle the case where there are spaces in the display name as well as the email |
| 631 | // such as "Guy with spaces <guy+with+spaces@gmail.com>" as they it could be encoded |
| 632 | // ambiguously. |
| 633 | |
| 634 | // Since URLDecode.decode changes + into ' ', and + is a valid |
| 635 | // email character, we need to find/ replace these ourselves before |
| 636 | // decoding. |
| 637 | String replacePlus = s.replace("+", "%2B"); |
| 638 | return URLDecoder.decode(replacePlus, UTF8_ENCODING_NAME); |
| 639 | } |
| 640 | |
| 641 | /** |
| 642 | * Initialize the compose view from a String representing a mailTo uri. |
| 643 | * @param mailToString The uri as a string. |
| 644 | */ |
| 645 | public void initFromMailTo(String mailToString) { |
| 646 | // We need to disguise this string as a URI in order to parse it |
| 647 | // TODO: Remove this hack when http://b/issue?id=1445295 gets fixed |
| 648 | Uri uri = Uri.parse("foo://" + mailToString); |
| 649 | int index = mailToString.indexOf("?"); |
| 650 | int length = "mailto".length() + 1; |
| 651 | String to; |
| 652 | try { |
| 653 | // Extract the recipient after mailto: |
| 654 | if (index == -1) { |
| 655 | to = decodeEmailInUri(mailToString.substring(length)); |
| 656 | } else { |
| 657 | to = decodeEmailInUri(mailToString.substring(length, index)); |
| 658 | } |
| 659 | addToAddresses(Arrays.asList(to.split(" ,"))); |
| 660 | } catch (UnsupportedEncodingException e) { |
| 661 | if (LogUtils.isLoggable(LOG_TAG, LogUtils.VERBOSE)) { |
| 662 | LogUtils.e(LOG_TAG, "%s while decoding '%s'", e.getMessage(), mailToString); |
| 663 | } else { |
| 664 | LogUtils.e(LOG_TAG, e, "Exception while decoding mailto address"); |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | List<String> cc = uri.getQueryParameters("cc"); |
| 669 | addCcAddresses(Arrays.asList(cc.toArray(new String[cc.size()])), null); |
| 670 | |
| 671 | List<String> otherTo = uri.getQueryParameters("to"); |
| 672 | addToAddresses(Arrays.asList(otherTo.toArray(new String[otherTo.size()]))); |
| 673 | |
| 674 | List<String> bcc = uri.getQueryParameters("bcc"); |
| 675 | addBccAddresses(Arrays.asList(bcc.toArray(new String[bcc.size()]))); |
| 676 | |
| 677 | List<String> subject = uri.getQueryParameters("subject"); |
| 678 | if (subject.size() > 0) { |
| 679 | try { |
| 680 | mSubject.setText(URLDecoder.decode(subject.get(0), UTF8_ENCODING_NAME)); |
| 681 | } catch (UnsupportedEncodingException e) { |
| 682 | LogUtils.e(LOG_TAG, "%s while decoding subject '%s'", |
| 683 | e.getMessage(), subject); |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | List<String> body = uri.getQueryParameters("body"); |
| 688 | if (body.size() > 0) { |
| 689 | try { |
| 690 | setBody(URLDecoder.decode(body.get(0), UTF8_ENCODING_NAME), |
| 691 | true /* with signature */); |
| 692 | } catch (UnsupportedEncodingException e) { |
| 693 | LogUtils.e(LOG_TAG, "%s while decoding body '%s'", e.getMessage(), body); |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | updateHideOrShowCcBcc(); |
| 698 | } |
| 699 | |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 700 | private void initAttachments(Message refMessage) { |
Mindy Pereira | 7a07fb4 | 2012-01-11 10:32:48 -0800 | [diff] [blame] | 701 | mAttachmentsView.addAttachments(mAccount, refMessage); |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 702 | } |
| 703 | |
Paul Westbrook | f97588b | 2012-03-20 11:11:37 -0700 | [diff] [blame] | 704 | private void initAttachmentsFromIntent(Intent intent) { |
| 705 | final Bundle extras = intent.getExtras(); |
| 706 | final String action = intent.getAction(); |
| 707 | if (!mAttachmentsChanged) { |
| 708 | long totalSize = 0; |
| 709 | if (extras.containsKey(EXTRA_ATTACHMENTS)) { |
| 710 | String[] uris = (String[]) extras.getSerializable(EXTRA_ATTACHMENTS); |
| 711 | for (String uriString : uris) { |
| 712 | final Uri uri = Uri.parse(uriString); |
| 713 | long size = 0; |
| 714 | try { |
| 715 | size = mAttachmentsView.addAttachment(mAccount, uri, false /* doSave */, |
| 716 | true /* local file */); |
| 717 | } catch (AttachmentFailureException e) { |
| 718 | // A toast has already been shown to the user, |
| 719 | // just break out of the loop. |
| 720 | LogUtils.e(LOG_TAG, e, "Error adding attachment"); |
| 721 | } |
| 722 | totalSize += size; |
| 723 | } |
| 724 | } |
| 725 | if (Intent.ACTION_SEND.equals(action) && extras.containsKey(Intent.EXTRA_STREAM)) { |
| 726 | final Uri uri = (Uri) extras.getParcelable(Intent.EXTRA_STREAM); |
| 727 | long size = 0; |
| 728 | try { |
| 729 | size = mAttachmentsView.addAttachment(mAccount, uri, false /* doSave */, |
| 730 | true /* local file */); |
| 731 | } catch (AttachmentFailureException e) { |
| 732 | // A toast has already been shown to the user, so just |
| 733 | // exit. |
| 734 | LogUtils.e(LOG_TAG, e, "Error adding attachment"); |
| 735 | } |
| 736 | totalSize += size; |
| 737 | } |
| 738 | |
| 739 | if (Intent.ACTION_SEND_MULTIPLE.equals(action) |
| 740 | && extras.containsKey(Intent.EXTRA_STREAM)) { |
| 741 | ArrayList<Parcelable> uris = extras.getParcelableArrayList(Intent.EXTRA_STREAM); |
| 742 | for (Parcelable uri : uris) { |
| 743 | long size = 0; |
| 744 | try { |
Paul Westbrook | bb87b7f | 2012-03-20 16:20:07 -0700 | [diff] [blame] | 745 | size = mAttachmentsView.addAttachment(mAccount, (Uri) uri, |
Paul Westbrook | f97588b | 2012-03-20 11:11:37 -0700 | [diff] [blame] | 746 | false /* doSave */, true /* local file */); |
| 747 | } catch (AttachmentFailureException e) { |
| 748 | // A toast has already been shown to the user, |
| 749 | // just break out of the loop. |
| 750 | LogUtils.e(LOG_TAG, e, "Error adding attachment"); |
| 751 | } |
| 752 | totalSize += size; |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | if (totalSize > 0) { |
| 757 | mAttachmentsChanged = true; |
| 758 | updateSaveUi(); |
| 759 | } |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 764 | private void initBodyFromRefMessage(Message refMessage, int action) { |
Mindy Pereira | 9932dee | 2012-01-10 16:09:50 -0800 | [diff] [blame] | 765 | if (action == REPLY || action == REPLY_ALL || action == FORWARD) { |
Mindy Pereira | 9932dee | 2012-01-10 16:09:50 -0800 | [diff] [blame] | 766 | mQuotedTextView.setQuotedText(action, refMessage, action != FORWARD); |
| 767 | } |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | private void updateHideOrShowCcBcc() { |
Mindy Pereira | ec8b0ed | 2012-01-06 10:35:59 -0800 | [diff] [blame] | 771 | // Its possible there is a menu item OR a button. |
Mindy Pereira | a26b54e | 2012-01-06 12:54:33 -0800 | [diff] [blame] | 772 | boolean ccVisible = !TextUtils.isEmpty(mCc.getText()); |
| 773 | boolean bccVisible = !TextUtils.isEmpty(mBcc.getText()); |
| 774 | if (ccVisible || bccVisible) { |
| 775 | mCcBccView.show(false, ccVisible, bccVisible); |
| 776 | } |
Mindy Pereira | ec8b0ed | 2012-01-06 10:35:59 -0800 | [diff] [blame] | 777 | if (mCcBccButton != null) { |
| 778 | if (!mCc.isShown() || !mBcc.isShown()) { |
| 779 | mCcBccButton.setVisibility(View.VISIBLE); |
| 780 | mCcBccButton.setText(getString(!mCc.isShown() ? R.string.add_cc_label |
| 781 | : R.string.add_bcc_label)); |
| 782 | } else { |
| 783 | mCcBccButton.setVisibility(View.GONE); |
| 784 | } |
| 785 | } |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 786 | } |
| 787 | |
Mindy Pereira | 013194c | 2012-01-06 15:09:33 -0800 | [diff] [blame] | 788 | /** |
| 789 | * Add attachment and update the compose area appropriately. |
| 790 | * @param data |
| 791 | */ |
| 792 | public void addAttachmentAndUpdateView(Intent data) { |
| 793 | Uri uri = data != null ? data.getData() : null; |
Mindy Pereira | 013194c | 2012-01-06 15:09:33 -0800 | [diff] [blame] | 794 | try { |
Mindy Pereira | f944e96 | 2012-01-17 11:43:36 -0800 | [diff] [blame] | 795 | long size = mAttachmentsView.addAttachment(mAccount, uri, false /* doSave */, |
| 796 | true /* local file */); |
Mindy Pereira | 9932dee | 2012-01-10 16:09:50 -0800 | [diff] [blame] | 797 | if (size > 0) { |
| 798 | mAttachmentsChanged = true; |
| 799 | updateSaveUi(); |
Mindy Pereira | 013194c | 2012-01-06 15:09:33 -0800 | [diff] [blame] | 800 | } |
Mindy Pereira | 9932dee | 2012-01-10 16:09:50 -0800 | [diff] [blame] | 801 | } catch (AttachmentFailureException e) { |
| 802 | // A toast has already been shown to the user, no need to do |
| 803 | // anything. |
| 804 | LogUtils.e(LOG_TAG, e, "Error adding attachment"); |
Mindy Pereira | 013194c | 2012-01-06 15:09:33 -0800 | [diff] [blame] | 805 | } |
| 806 | } |
| 807 | |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 808 | void initRecipientsFromRefMessage(String recipientAddress, Message refMessage, |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 809 | int action) { |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 810 | // Don't populate the address if this is a forward. |
| 811 | if (action == ComposeActivity.FORWARD) { |
| 812 | return; |
| 813 | } |
Mindy Pereira | 33fe908 | 2012-01-09 16:24:30 -0800 | [diff] [blame] | 814 | initReplyRecipients(mAccount.name, refMessage, action); |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 815 | } |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 816 | |
Mindy Pereira | 818143e | 2012-01-11 13:59:49 -0800 | [diff] [blame] | 817 | @VisibleForTesting |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 818 | void initReplyRecipients(String account, Message refMessage, int action) { |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 819 | // This is the email address of the current user, i.e. the one composing |
| 820 | // the reply. |
Mindy Pereira | 4a20b70 | 2012-01-05 16:24:24 -0800 | [diff] [blame] | 821 | final String accountEmail = Address.getEmailAddress(account).getAddress(); |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 822 | String fromAddress = refMessage.from; |
| 823 | String[] sentToAddresses = Utils.splitCommaSeparatedString(refMessage.to); |
| 824 | String replytoAddress = refMessage.replyTo; |
Mindy Pereira | a26b54e | 2012-01-06 12:54:33 -0800 | [diff] [blame] | 825 | final Collection<String> toAddresses; |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 826 | |
| 827 | // If this is a reply, the Cc list is empty. If this is a reply-all, the |
| 828 | // Cc list is the union of the To and Cc recipients of the original |
| 829 | // message, excluding the current user's email address and any addresses |
Mindy Pereira | a26b54e | 2012-01-06 12:54:33 -0800 | [diff] [blame] | 830 | // already on the To list. |
| 831 | if (action == ComposeActivity.REPLY) { |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 832 | toAddresses = initToRecipients(account, accountEmail, fromAddress, replytoAddress, |
| 833 | new String[0]); |
Mindy Pereira | a26b54e | 2012-01-06 12:54:33 -0800 | [diff] [blame] | 834 | addToAddresses(toAddresses); |
| 835 | } else if (action == ComposeActivity.REPLY_ALL) { |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 836 | final Set<String> ccAddresses = Sets.newHashSet(); |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 837 | toAddresses = initToRecipients(account, accountEmail, fromAddress, replytoAddress, |
| 838 | new String[0]); |
Mindy Pereira | 154386a | 2012-01-11 13:02:33 -0800 | [diff] [blame] | 839 | addToAddresses(toAddresses); |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 840 | addRecipients(accountEmail, ccAddresses, sentToAddresses); |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 841 | addRecipients(accountEmail, ccAddresses, |
| 842 | Utils.splitCommaSeparatedString(refMessage.cc)); |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 843 | addCcAddresses(ccAddresses, toAddresses); |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | private void addToAddresses(Collection<String> addresses) { |
| 848 | addAddressesToList(addresses, mTo); |
| 849 | } |
| 850 | |
| 851 | private void addCcAddresses(Collection<String> addresses, Collection<String> toAddresses) { |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame^] | 852 | addCcAddressesToList(tokenizeAddressList(addresses), |
| 853 | toAddresses != null ? tokenizeAddressList(toAddresses) : null, mCc); |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 854 | } |
| 855 | |
Paul Westbrook | bb87b7f | 2012-03-20 16:20:07 -0700 | [diff] [blame] | 856 | private void addBccAddresses(Collection<String> addresses) { |
| 857 | addAddressesToList(addresses, mBcc); |
| 858 | } |
| 859 | |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 860 | @VisibleForTesting |
| 861 | protected void addCcAddressesToList(List<Rfc822Token[]> addresses, |
| 862 | List<Rfc822Token[]> compareToList, RecipientEditTextView list) { |
| 863 | String address; |
| 864 | |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame^] | 865 | if (compareToList == null) { |
| 866 | for (Rfc822Token[] tokens : addresses) { |
| 867 | for (int i = 0; i < tokens.length; i++) { |
| 868 | address = tokens[i].toString(); |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 869 | list.append(address + END_TOKEN); |
| 870 | } |
| 871 | } |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame^] | 872 | } else { |
| 873 | HashSet<String> compareTo = convertToHashSet(compareToList); |
| 874 | for (Rfc822Token[] tokens : addresses) { |
| 875 | for (int i = 0; i < tokens.length; i++) { |
| 876 | address = tokens[i].toString(); |
| 877 | // Check if this is a duplicate: |
| 878 | if (!compareTo.contains(tokens[i].getAddress())) { |
| 879 | // Get the address here |
| 880 | list.append(address + END_TOKEN); |
| 881 | } |
| 882 | } |
| 883 | } |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 884 | } |
| 885 | } |
| 886 | |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 887 | private HashSet<String> convertToHashSet(List<Rfc822Token[]> list) { |
| 888 | HashSet<String> hash = new HashSet<String>(); |
| 889 | for (Rfc822Token[] tokens : list) { |
| 890 | for (int i = 0; i < tokens.length; i++) { |
| 891 | hash.add(tokens[i].getAddress()); |
| 892 | } |
| 893 | } |
| 894 | return hash; |
| 895 | } |
| 896 | |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 897 | protected List<Rfc822Token[]> tokenizeAddressList(Collection<String> addresses) { |
| 898 | @VisibleForTesting |
| 899 | List<Rfc822Token[]> tokenized = new ArrayList<Rfc822Token[]>(); |
| 900 | |
| 901 | for (String address: addresses) { |
| 902 | tokenized.add(Rfc822Tokenizer.tokenize(address)); |
| 903 | } |
| 904 | return tokenized; |
| 905 | } |
| 906 | |
| 907 | @VisibleForTesting |
| 908 | void addAddressesToList(Collection<String> addresses, RecipientEditTextView list) { |
| 909 | for (String address : addresses) { |
| 910 | addAddressToList(address, list); |
| 911 | } |
| 912 | } |
| 913 | |
| 914 | private void addAddressToList(String address, RecipientEditTextView list) { |
| 915 | if (address == null || list == null) |
| 916 | return; |
| 917 | |
| 918 | Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(address); |
| 919 | |
| 920 | for (int i = 0; i < tokens.length; i++) { |
| 921 | list.append(tokens[i] + END_TOKEN); |
| 922 | } |
| 923 | } |
| 924 | |
| 925 | @VisibleForTesting |
| 926 | protected Collection<String> initToRecipients(String account, String accountEmail, |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 927 | String senderAddress, String replyToAddress, String[] inToAddresses) { |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 928 | // The To recipient is the reply-to address specified in the original |
| 929 | // message, unless it is: |
| 930 | // the current user OR a custom from of the current user, in which case |
| 931 | // it's the To recipient list of the original message. |
| 932 | // OR missing, in which case use the sender of the original message |
| 933 | Set<String> toAddresses = Sets.newHashSet(); |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 934 | if (!TextUtils.isEmpty(replyToAddress)) { |
| 935 | toAddresses.add(replyToAddress); |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 936 | } else { |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 937 | toAddresses.add(senderAddress); |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 938 | } |
| 939 | return toAddresses; |
| 940 | } |
| 941 | |
| 942 | private static void addRecipients(String account, Set<String> recipients, String[] addresses) { |
| 943 | for (String email : addresses) { |
| 944 | // Do not add this account, or any of the custom froms, to the list |
| 945 | // of recipients. |
Mindy Pereira | 4a20b70 | 2012-01-05 16:24:24 -0800 | [diff] [blame] | 946 | final String recipientAddress = Address.getEmailAddress(email).getAddress(); |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 947 | if (!account.equalsIgnoreCase(recipientAddress)) { |
| 948 | recipients.add(email.replace("\"\"", "")); |
| 949 | } |
| 950 | } |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 951 | } |
| 952 | |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 953 | private void setSubject(Message refMessage, int action) { |
| 954 | String subject = refMessage.subject; |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 955 | String prefix; |
| 956 | String correctedSubject = null; |
| 957 | if (action == ComposeActivity.COMPOSE) { |
| 958 | prefix = ""; |
| 959 | } else if (action == ComposeActivity.FORWARD) { |
| 960 | prefix = getString(R.string.forward_subject_label); |
| 961 | } else { |
| 962 | prefix = getString(R.string.reply_subject_label); |
| 963 | } |
| 964 | |
| 965 | // Don't duplicate the prefix |
| 966 | if (subject.toLowerCase().startsWith(prefix.toLowerCase())) { |
| 967 | correctedSubject = subject; |
| 968 | } else { |
| 969 | correctedSubject = String |
| 970 | .format(getString(R.string.formatted_subject), prefix, subject); |
| 971 | } |
| 972 | mSubject.setText(correctedSubject); |
| 973 | } |
| 974 | |
Mindy Pereira | 818143e | 2012-01-11 13:59:49 -0800 | [diff] [blame] | 975 | private void initRecipients() { |
| 976 | setupRecipients(mTo); |
| 977 | setupRecipients(mCc); |
| 978 | setupRecipients(mBcc); |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 979 | } |
| 980 | |
Mindy Pereira | 818143e | 2012-01-11 13:59:49 -0800 | [diff] [blame] | 981 | private void setupRecipients(RecipientEditTextView view) { |
Paul Westbrook | 679a8cc | 2012-02-21 16:37:58 -0800 | [diff] [blame] | 982 | view.setAdapter(new RecipientAdapter(this, mAccount)); |
Mindy Pereira | c17d073 | 2011-12-29 10:46:19 -0800 | [diff] [blame] | 983 | view.setTokenizer(new Rfc822Tokenizer()); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 984 | if (mValidator == null) { |
Paul Westbrook | 679a8cc | 2012-02-21 16:37:58 -0800 | [diff] [blame] | 985 | final String accountName = mAccount.name; |
Mindy Pereira | 33fe908 | 2012-01-09 16:24:30 -0800 | [diff] [blame] | 986 | int offset = accountName.indexOf("@") + 1; |
| 987 | String account = accountName; |
Mindy Pereira | c17d073 | 2011-12-29 10:46:19 -0800 | [diff] [blame] | 988 | if (offset > -1) { |
Mindy Pereira | 33fe908 | 2012-01-09 16:24:30 -0800 | [diff] [blame] | 989 | account = account.substring(accountName.indexOf("@") + 1); |
Mindy Pereira | c17d073 | 2011-12-29 10:46:19 -0800 | [diff] [blame] | 990 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 991 | mValidator = new Rfc822Validator(account); |
Mindy Pereira | c17d073 | 2011-12-29 10:46:19 -0800 | [diff] [blame] | 992 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 993 | view.setValidator(mValidator); |
Mindy Pereira | 8e9305e | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | @Override |
| 997 | public void onClick(View v) { |
| 998 | int id = v.getId(); |
| 999 | switch (id) { |
Mindy Pereira | a26b54e | 2012-01-06 12:54:33 -0800 | [diff] [blame] | 1000 | case R.id.add_cc_bcc: |
Mindy Pereira | 8e9305e | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 1001 | // Verify that cc/ bcc aren't showing. |
| 1002 | // Animate in cc/bcc. |
Mindy Pereira | a26b54e | 2012-01-06 12:54:33 -0800 | [diff] [blame] | 1003 | showCcBccViews(); |
Mindy Pereira | 8e9305e | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 1004 | break; |
Mindy Pereira | 1f93668 | 2012-03-02 11:30:33 -0800 | [diff] [blame] | 1005 | case R.id.add_attachment: |
| 1006 | doAttach(); |
| 1007 | break; |
Mindy Pereira | 8e9305e | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 1008 | } |
| 1009 | } |
Mindy Pereira | b47f3e2 | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 1010 | |
| 1011 | @Override |
| 1012 | public boolean onCreateOptionsMenu(Menu menu) { |
| 1013 | super.onCreateOptionsMenu(menu); |
| 1014 | MenuInflater inflater = getMenuInflater(); |
| 1015 | inflater.inflate(R.menu.compose_menu, menu); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1016 | mSave = menu.findItem(R.id.save); |
| 1017 | mSend = menu.findItem(R.id.send); |
Mindy Pereira | b47f3e2 | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 1018 | return true; |
| 1019 | } |
| 1020 | |
| 1021 | @Override |
Mindy Pereira | ec8b0ed | 2012-01-06 10:35:59 -0800 | [diff] [blame] | 1022 | public boolean onPrepareOptionsMenu(Menu menu) { |
| 1023 | MenuItem ccBcc = menu.findItem(R.id.add_cc_bcc); |
Mindy Pereira | 818143e | 2012-01-11 13:59:49 -0800 | [diff] [blame] | 1024 | if (ccBcc != null && mCc != null) { |
Mindy Pereira | ec8b0ed | 2012-01-06 10:35:59 -0800 | [diff] [blame] | 1025 | // Its possible there is a menu item OR a button. |
| 1026 | boolean ccFieldVisible = mCc.isShown(); |
| 1027 | boolean bccFieldVisible = mBcc.isShown(); |
| 1028 | if (!ccFieldVisible || !bccFieldVisible) { |
| 1029 | ccBcc.setVisible(true); |
| 1030 | ccBcc.setTitle(getString(!ccFieldVisible ? R.string.add_cc_label |
| 1031 | : R.string.add_bcc_label)); |
| 1032 | } else { |
| 1033 | ccBcc.setVisible(false); |
| 1034 | } |
| 1035 | } |
Mindy Pereira | 75f6663 | 2012-01-11 11:42:02 -0800 | [diff] [blame] | 1036 | if (mSave != null) { |
| 1037 | mSave.setEnabled(shouldSave()); |
| 1038 | } |
Mindy Pereira | ec8b0ed | 2012-01-06 10:35:59 -0800 | [diff] [blame] | 1039 | return true; |
| 1040 | } |
| 1041 | |
| 1042 | @Override |
Mindy Pereira | b47f3e2 | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 1043 | public boolean onOptionsItemSelected(MenuItem item) { |
| 1044 | int id = item.getItemId(); |
Mindy Pereira | 75f6663 | 2012-01-11 11:42:02 -0800 | [diff] [blame] | 1045 | boolean handled = true; |
Mindy Pereira | b47f3e2 | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 1046 | switch (id) { |
Mindy Pereira | 7b56a61 | 2011-12-14 12:32:28 -0800 | [diff] [blame] | 1047 | case R.id.add_attachment: |
Mindy Pereira | 013194c | 2012-01-06 15:09:33 -0800 | [diff] [blame] | 1048 | doAttach(); |
Mindy Pereira | 7b56a61 | 2011-12-14 12:32:28 -0800 | [diff] [blame] | 1049 | break; |
Mindy Pereira | ec8b0ed | 2012-01-06 10:35:59 -0800 | [diff] [blame] | 1050 | case R.id.add_cc_bcc: |
| 1051 | showCcBccViews(); |
Mindy Pereira | b47f3e2 | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 1052 | break; |
Mindy Pereira | 33fe908 | 2012-01-09 16:24:30 -0800 | [diff] [blame] | 1053 | case R.id.save: |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 1054 | doSave(true, false); |
Mindy Pereira | 33fe908 | 2012-01-09 16:24:30 -0800 | [diff] [blame] | 1055 | break; |
| 1056 | case R.id.send: |
| 1057 | doSend(); |
Mindy Pereira | 75f6663 | 2012-01-11 11:42:02 -0800 | [diff] [blame] | 1058 | break; |
Mindy Pereira | efe3d25 | 2012-03-01 14:20:44 -0800 | [diff] [blame] | 1059 | case R.id.discard: |
| 1060 | doDiscard(); |
| 1061 | break; |
Mindy Pereira | 1f93668 | 2012-03-02 11:30:33 -0800 | [diff] [blame] | 1062 | case R.id.settings: |
| 1063 | Utils.showSettings(this, mAccount); |
| 1064 | break; |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 1065 | case android.R.id.home: |
| 1066 | finish(); |
| 1067 | break; |
| 1068 | case R.id.help_info_menu_item: |
| 1069 | // TODO: enable context sensitive help |
| 1070 | Utils.showHelp(this, mAccount.helpIntentUri, null); |
| 1071 | break; |
| 1072 | case R.id.feedback_menu_item: |
| 1073 | Utils.sendFeedback(this, mAccount); |
| 1074 | break; |
Mindy Pereira | 75f6663 | 2012-01-11 11:42:02 -0800 | [diff] [blame] | 1075 | default: |
| 1076 | handled = false; |
Mindy Pereira | 33fe908 | 2012-01-09 16:24:30 -0800 | [diff] [blame] | 1077 | break; |
Mindy Pereira | b47f3e2 | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 1078 | } |
| 1079 | return !handled ? super.onOptionsItemSelected(item) : handled; |
| 1080 | } |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 1081 | |
Mindy Pereira | 33fe908 | 2012-01-09 16:24:30 -0800 | [diff] [blame] | 1082 | private void doSend() { |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1083 | sendOrSaveWithSanityChecks(false, true, false); |
Mindy Pereira | 33fe908 | 2012-01-09 16:24:30 -0800 | [diff] [blame] | 1084 | } |
| 1085 | |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 1086 | private void doSave(boolean showToast, boolean resetIME) { |
| 1087 | sendOrSaveWithSanityChecks(true, showToast, false); |
| 1088 | if (resetIME) { |
| 1089 | // Clear the IME composing suggestions from the body. |
| 1090 | BaseInputConnection.removeComposingSpans(mBodyView.getEditableText()); |
| 1091 | } |
Mindy Pereira | 33fe908 | 2012-01-09 16:24:30 -0800 | [diff] [blame] | 1092 | } |
| 1093 | |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1094 | /*package*/ interface SendOrSaveCallback { |
| 1095 | public void initializeSendOrSave(SendOrSaveTask sendOrSaveTask); |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 1096 | public void notifyMessageIdAllocated(SendOrSaveMessage sendOrSaveMessage, Message message); |
| 1097 | public Message getMessage(); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1098 | public void sendOrSaveFinished(SendOrSaveTask sendOrSaveTask, boolean success); |
| 1099 | } |
| 1100 | |
| 1101 | /*package*/ static class SendOrSaveTask implements Runnable { |
| 1102 | private final Context mContext; |
| 1103 | private final SendOrSaveCallback mSendOrSaveCallback; |
| 1104 | @VisibleForTesting |
| 1105 | final SendOrSaveMessage mSendOrSaveMessage; |
| 1106 | |
| 1107 | public SendOrSaveTask(Context context, SendOrSaveMessage message, |
| 1108 | SendOrSaveCallback callback) { |
| 1109 | mContext = context; |
| 1110 | mSendOrSaveCallback = callback; |
| 1111 | mSendOrSaveMessage = message; |
| 1112 | } |
| 1113 | |
| 1114 | @Override |
| 1115 | public void run() { |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 1116 | final SendOrSaveMessage sendOrSaveMessage = mSendOrSaveMessage; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1117 | |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 1118 | final Account selectedAccount = sendOrSaveMessage.mSelectedAccount; |
| 1119 | Message message = mSendOrSaveCallback.getMessage(); |
| 1120 | long messageId = message != null ? message.id : UIProvider.INVALID_MESSAGE_ID; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1121 | // If a previous draft has been saved, in an account that is different |
| 1122 | // than what the user wants to send from, remove the old draft, and treat this |
| 1123 | // as a new message |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 1124 | if (!selectedAccount.equals(sendOrSaveMessage.mAccount)) { |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1125 | if (messageId != UIProvider.INVALID_MESSAGE_ID) { |
| 1126 | ContentResolver resolver = mContext.getContentResolver(); |
| 1127 | ContentValues values = new ContentValues(); |
| 1128 | values.put(BaseColumns._ID, messageId); |
Mindy Pereira | cfb7f33 | 2012-02-28 10:23:43 -0800 | [diff] [blame] | 1129 | if (selectedAccount.expungeMessageUri != null) { |
| 1130 | resolver.update(selectedAccount.expungeMessageUri, values, null, |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 1131 | null); |
Mindy Pereira | cfb7f33 | 2012-02-28 10:23:43 -0800 | [diff] [blame] | 1132 | } else { |
| 1133 | // TODO(mindyp) delete the conversation. |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 1134 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1135 | // reset messageId to 0, so a new message will be created |
| 1136 | messageId = UIProvider.INVALID_MESSAGE_ID; |
| 1137 | } |
| 1138 | } |
| 1139 | |
| 1140 | final long messageIdToSave = messageId; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1141 | if (messageIdToSave != UIProvider.INVALID_MESSAGE_ID) { |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 1142 | sendOrSaveMessage.mValues.put(BaseColumns._ID, messageIdToSave); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1143 | mContext.getContentResolver().update( |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 1144 | Uri.parse(sendOrSaveMessage.mSave ? message.saveUri : message.sendUri), |
| 1145 | sendOrSaveMessage.mValues, null, null); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1146 | } else { |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 1147 | ContentResolver resolver = mContext.getContentResolver(); |
| 1148 | Uri messageUri = resolver.insert( |
Mindy Pereira | cfb7f33 | 2012-02-28 10:23:43 -0800 | [diff] [blame] | 1149 | sendOrSaveMessage.mSave ? selectedAccount.saveDraftUri |
| 1150 | : selectedAccount.sendMessageUri, sendOrSaveMessage.mValues); |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 1151 | if (sendOrSaveMessage.mSave && messageUri != null) { |
| 1152 | Cursor messageCursor = resolver.query(messageUri, |
| 1153 | UIProvider.MESSAGE_PROJECTION, null, null, null); |
Paul Westbrook | ba55848 | 2012-03-19 11:00:24 -0700 | [diff] [blame] | 1154 | if (messageCursor != null) { |
| 1155 | try { |
| 1156 | if (messageCursor.moveToFirst()) { |
| 1157 | // Broadcast notification that a new message has |
| 1158 | // been allocated |
| 1159 | mSendOrSaveCallback.notifyMessageIdAllocated(sendOrSaveMessage, |
| 1160 | new Message(messageCursor)); |
| 1161 | } |
| 1162 | } finally { |
| 1163 | messageCursor.close(); |
| 1164 | } |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 1165 | } |
| 1166 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1167 | } |
| 1168 | |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 1169 | if (!sendOrSaveMessage.mSave) { |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1170 | UIProvider.incrementRecipientsTimesContacted(mContext, |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 1171 | (String) sendOrSaveMessage.mValues.get(UIProvider.MessageColumns.TO)); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1172 | UIProvider.incrementRecipientsTimesContacted(mContext, |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 1173 | (String) sendOrSaveMessage.mValues.get(UIProvider.MessageColumns.CC)); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1174 | UIProvider.incrementRecipientsTimesContacted(mContext, |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 1175 | (String) sendOrSaveMessage.mValues.get(UIProvider.MessageColumns.BCC)); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1176 | } |
| 1177 | mSendOrSaveCallback.sendOrSaveFinished(SendOrSaveTask.this, true); |
| 1178 | } |
| 1179 | } |
| 1180 | |
| 1181 | // Array of the outstanding send or save tasks. Access is synchronized |
| 1182 | // with the object itself |
| 1183 | /* package for testing */ |
| 1184 | ArrayList<SendOrSaveTask> mActiveTasks = Lists.newArrayList(); |
| 1185 | private int mRequestId; |
Mindy Pereira | bdf7a40 | 2012-03-01 15:23:26 -0800 | [diff] [blame] | 1186 | private String mSignature; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1187 | |
| 1188 | /*package*/ static class SendOrSaveMessage { |
| 1189 | final Account mAccount; |
| 1190 | final Account mSelectedAccount; |
| 1191 | final ContentValues mValues; |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 1192 | final String mRefMessageId; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1193 | final boolean mSave; |
| 1194 | final int mRequestId; |
| 1195 | |
| 1196 | public SendOrSaveMessage(Account account, Account selectedAccount, ContentValues values, |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 1197 | String refMessageId, boolean save) { |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1198 | mAccount = account; |
| 1199 | mSelectedAccount = selectedAccount; |
| 1200 | mValues = values; |
| 1201 | mRefMessageId = refMessageId; |
| 1202 | mSave = save; |
| 1203 | mRequestId = mValues.hashCode() ^ hashCode(); |
| 1204 | } |
| 1205 | |
| 1206 | int requestId() { |
| 1207 | return mRequestId; |
| 1208 | } |
| 1209 | } |
| 1210 | |
| 1211 | /** |
| 1212 | * Get the to recipients. |
| 1213 | */ |
| 1214 | public String[] getToAddresses() { |
| 1215 | return getAddressesFromList(mTo); |
| 1216 | } |
| 1217 | |
| 1218 | /** |
| 1219 | * Get the cc recipients. |
| 1220 | */ |
| 1221 | public String[] getCcAddresses() { |
| 1222 | return getAddressesFromList(mCc); |
| 1223 | } |
| 1224 | |
| 1225 | /** |
| 1226 | * Get the bcc recipients. |
| 1227 | */ |
| 1228 | public String[] getBccAddresses() { |
| 1229 | return getAddressesFromList(mBcc); |
| 1230 | } |
| 1231 | |
| 1232 | public String[] getAddressesFromList(RecipientEditTextView list) { |
| 1233 | if (list == null) { |
| 1234 | return new String[0]; |
| 1235 | } |
| 1236 | Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(list.getText()); |
| 1237 | int count = tokens.length; |
| 1238 | String[] result = new String[count]; |
| 1239 | for (int i = 0; i < count; i++) { |
| 1240 | result[i] = tokens[i].toString(); |
| 1241 | } |
| 1242 | return result; |
| 1243 | } |
| 1244 | |
| 1245 | /** |
| 1246 | * Check for invalid email addresses. |
| 1247 | * @param to String array of email addresses to check. |
| 1248 | * @param wrongEmailsOut Emails addresses that were invalid. |
| 1249 | */ |
| 1250 | public void checkInvalidEmails(String[] to, List<String> wrongEmailsOut) { |
| 1251 | for (String email : to) { |
| 1252 | if (!mValidator.isValid(email)) { |
| 1253 | wrongEmailsOut.add(email); |
| 1254 | } |
| 1255 | } |
| 1256 | } |
| 1257 | |
| 1258 | /** |
| 1259 | * Show an error because the user has entered an invalid recipient. |
| 1260 | * @param message |
| 1261 | */ |
| 1262 | public void showRecipientErrorDialog(String message) { |
| 1263 | // Only 1 invalid recipients error dialog should be allowed up at a |
| 1264 | // time. |
| 1265 | if (mRecipientErrorDialog != null) { |
| 1266 | mRecipientErrorDialog.dismiss(); |
| 1267 | } |
| 1268 | mRecipientErrorDialog = new AlertDialog.Builder(this).setMessage(message).setTitle( |
| 1269 | R.string.recipient_error_dialog_title) |
| 1270 | .setIconAttribute(android.R.attr.alertDialogIcon) |
| 1271 | .setCancelable(false) |
| 1272 | .setPositiveButton( |
| 1273 | R.string.ok, new Dialog.OnClickListener() { |
| 1274 | public void onClick(DialogInterface dialog, int which) { |
| 1275 | // after the user dismisses the recipient error |
| 1276 | // dialog we want to make sure to refocus the |
| 1277 | // recipient to field so they can fix the issue |
| 1278 | // easily |
| 1279 | if (mTo != null) { |
| 1280 | mTo.requestFocus(); |
| 1281 | } |
| 1282 | mRecipientErrorDialog = null; |
| 1283 | } |
| 1284 | }).show(); |
| 1285 | } |
| 1286 | |
| 1287 | /** |
| 1288 | * Update the state of the UI based on whether or not the current draft |
| 1289 | * needs to be saved and the message is not empty. |
| 1290 | */ |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 1291 | public void updateSaveUi() { |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1292 | if (mSave != null) { |
| 1293 | mSave.setEnabled((shouldSave() && !isBlank())); |
| 1294 | } |
| 1295 | } |
| 1296 | |
| 1297 | /** |
| 1298 | * Returns true if we need to save the current draft. |
| 1299 | */ |
| 1300 | private boolean shouldSave() { |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 1301 | synchronized (mDraftLock) { |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1302 | // The message should only be saved if: |
| 1303 | // It hasn't been sent AND |
| 1304 | // Some text has been added to the message OR |
| 1305 | // an attachment has been added or removed |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 1306 | return (mTextChanged || mAttachmentsChanged || |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1307 | (mReplyFromChanged && !isBlank())); |
| 1308 | } |
| 1309 | } |
| 1310 | |
| 1311 | /** |
Mindy Pereira | bdf7a40 | 2012-03-01 15:23:26 -0800 | [diff] [blame] | 1312 | * Check if all fields are blank. |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1313 | * @return boolean |
| 1314 | */ |
| 1315 | public boolean isBlank() { |
| 1316 | return mSubject.getText().length() == 0 |
Mindy Pereira | bdf7a40 | 2012-03-01 15:23:26 -0800 | [diff] [blame] | 1317 | && (mBodyView.getText().length() == 0 || getSignatureStartPosition(mSignature, |
| 1318 | mBodyView.getText().toString()) == 0) |
| 1319 | && mTo.length() == 0 |
| 1320 | && mCc.length() == 0 && mBcc.length() == 0 |
| 1321 | && mAttachmentsView.getAttachments().size() == 0; |
| 1322 | } |
| 1323 | |
| 1324 | @VisibleForTesting |
| 1325 | protected int getSignatureStartPosition(String signature, String bodyText) { |
| 1326 | int startPos = -1; |
| 1327 | |
| 1328 | if (TextUtils.isEmpty(signature) || TextUtils.isEmpty(bodyText)) { |
| 1329 | return startPos; |
| 1330 | } |
| 1331 | |
| 1332 | int bodyLength = bodyText.length(); |
| 1333 | int signatureLength = signature.length(); |
| 1334 | String printableVersion = convertToPrintableSignature(signature); |
| 1335 | int printableLength = printableVersion.length(); |
| 1336 | |
| 1337 | if (bodyLength >= printableLength |
| 1338 | && bodyText.substring(bodyLength - printableLength) |
| 1339 | .equals(printableVersion)) { |
| 1340 | startPos = bodyLength - printableLength; |
| 1341 | } else if (bodyLength >= signatureLength |
| 1342 | && bodyText.substring(bodyLength - signatureLength) |
| 1343 | .equals(signature)) { |
| 1344 | startPos = bodyLength - signatureLength; |
| 1345 | } |
| 1346 | return startPos; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1347 | } |
| 1348 | |
| 1349 | /** |
| 1350 | * Allows any changes made by the user to be ignored. Called when the user |
| 1351 | * decides to discard a draft. |
| 1352 | */ |
| 1353 | private void discardChanges() { |
| 1354 | mTextChanged = false; |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 1355 | mAttachmentsChanged = false; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1356 | mReplyFromChanged = false; |
| 1357 | } |
| 1358 | |
| 1359 | /** |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1360 | * @param body |
| 1361 | * @param save |
| 1362 | * @param showToast |
| 1363 | * @return Whether the send or save succeeded. |
| 1364 | */ |
| 1365 | protected boolean sendOrSaveWithSanityChecks(final boolean save, final boolean showToast, |
| 1366 | final boolean orientationChanged) { |
| 1367 | String[] to, cc, bcc; |
| 1368 | Editable body = mBodyView.getEditableText(); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1369 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1370 | if (orientationChanged) { |
| 1371 | to = cc = bcc = new String[0]; |
| 1372 | } else { |
| 1373 | to = getToAddresses(); |
| 1374 | cc = getCcAddresses(); |
| 1375 | bcc = getBccAddresses(); |
| 1376 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1377 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1378 | // Don't let the user send to nobody (but it's okay to save a message |
| 1379 | // with no recipients) |
| 1380 | if (!save && (to.length == 0 && cc.length == 0 && bcc.length == 0)) { |
| 1381 | showRecipientErrorDialog(getString(R.string.recipient_needed)); |
| 1382 | return false; |
| 1383 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1384 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1385 | List<String> wrongEmails = new ArrayList<String>(); |
| 1386 | if (!save) { |
| 1387 | checkInvalidEmails(to, wrongEmails); |
| 1388 | checkInvalidEmails(cc, wrongEmails); |
| 1389 | checkInvalidEmails(bcc, wrongEmails); |
| 1390 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1391 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1392 | // Don't let the user send an email with invalid recipients |
| 1393 | if (wrongEmails.size() > 0) { |
| 1394 | String errorText = String.format(getString(R.string.invalid_recipient), |
| 1395 | wrongEmails.get(0)); |
| 1396 | showRecipientErrorDialog(errorText); |
| 1397 | return false; |
| 1398 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1399 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1400 | DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() { |
| 1401 | public void onClick(DialogInterface dialog, int which) { |
| 1402 | sendOrSave(mBodyView.getEditableText(), save, showToast, orientationChanged); |
| 1403 | } |
| 1404 | }; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1405 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1406 | // Show a warning before sending only if there are no attachments. |
| 1407 | if (!save) { |
| 1408 | if (mAttachmentsView.getAttachments().isEmpty() && showEmptyTextWarnings()) { |
| 1409 | boolean warnAboutEmptySubject = isSubjectEmpty(); |
| 1410 | boolean emptyBody = TextUtils.getTrimmedLength(body) == 0; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1411 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1412 | // A warning about an empty body may not be warranted when |
| 1413 | // forwarding mails, since a common use case is to forward |
| 1414 | // quoted text and not append any more text. |
| 1415 | boolean warnAboutEmptyBody = emptyBody && (!mForward || isBodyEmpty()); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1416 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1417 | // When we bring up a dialog warning the user about a send, |
| 1418 | // assume that they accept sending the message. If they do not, |
| 1419 | // the dialog listener is required to enable sending again. |
| 1420 | if (warnAboutEmptySubject) { |
| 1421 | showSendConfirmDialog(R.string.confirm_send_message_with_no_subject, listener); |
| 1422 | return true; |
| 1423 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1424 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1425 | if (warnAboutEmptyBody) { |
| 1426 | showSendConfirmDialog(R.string.confirm_send_message_with_no_body, listener); |
| 1427 | return true; |
| 1428 | } |
| 1429 | } |
| 1430 | // Ask for confirmation to send (if always required) |
| 1431 | if (showSendConfirmation()) { |
| 1432 | showSendConfirmDialog(R.string.confirm_send_message, listener); |
| 1433 | return true; |
| 1434 | } |
| 1435 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1436 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1437 | sendOrSave(body, save, showToast, false); |
| 1438 | return true; |
| 1439 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1440 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1441 | /** |
| 1442 | * Returns a boolean indicating whether warnings should be shown for empty |
| 1443 | * subject and body fields |
| 1444 | * |
| 1445 | * @return True if a warning should be shown for empty text fields |
| 1446 | */ |
| 1447 | protected boolean showEmptyTextWarnings() { |
| 1448 | return mAttachmentsView.getAttachments().size() == 0; |
| 1449 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1450 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1451 | /** |
| 1452 | * Returns a boolean indicating whether the user should confirm each send |
| 1453 | * |
| 1454 | * @return True if a warning should be on each send |
| 1455 | */ |
| 1456 | protected boolean showSendConfirmation() { |
| 1457 | return mCachedSettings != null ? mCachedSettings.confirmSend : false; |
| 1458 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1459 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1460 | private void showSendConfirmDialog(int messageId, DialogInterface.OnClickListener listener) { |
| 1461 | if (mSendConfirmDialog != null) { |
| 1462 | mSendConfirmDialog.dismiss(); |
| 1463 | mSendConfirmDialog = null; |
| 1464 | } |
| 1465 | mSendConfirmDialog = new AlertDialog.Builder(this).setMessage(messageId) |
| 1466 | .setTitle(R.string.confirm_send_title) |
| 1467 | .setIconAttribute(android.R.attr.alertDialogIcon) |
| 1468 | .setPositiveButton(R.string.send, listener) |
| 1469 | .setNegativeButton(R.string.cancel, this).setCancelable(false).show(); |
| 1470 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1471 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1472 | /** |
| 1473 | * Returns whether the ComposeArea believes there is any text in the body of |
| 1474 | * the composition. TODO: When ComposeArea controls the Body as well, add |
| 1475 | * that here. |
| 1476 | */ |
| 1477 | public boolean isBodyEmpty() { |
| 1478 | return !mQuotedTextView.isTextIncluded(); |
| 1479 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1480 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1481 | /** |
| 1482 | * Test to see if the subject is empty. |
| 1483 | * |
| 1484 | * @return boolean. |
| 1485 | */ |
| 1486 | // TODO: this will likely go away when composeArea.focus() is implemented |
| 1487 | // after all the widget control is moved over. |
| 1488 | public boolean isSubjectEmpty() { |
| 1489 | return TextUtils.getTrimmedLength(mSubject.getText()) == 0; |
| 1490 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1491 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1492 | /* package */ |
Mindy Pereira | 29ef1b8 | 2012-01-13 11:26:21 -0800 | [diff] [blame] | 1493 | static int sendOrSaveInternal(Context context, final Account account, |
| 1494 | final Account selectedAccount, String fromAddress, final Spanned body, |
| 1495 | final String[] to, final String[] cc, final String[] bcc, final String subject, |
| 1496 | final CharSequence quotedText, final List<Attachment> attachments, |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 1497 | final String refMessageId, SendOrSaveCallback callback, Handler handler, boolean save, |
Mindy Pereira | 29ef1b8 | 2012-01-13 11:26:21 -0800 | [diff] [blame] | 1498 | boolean forward) { |
| 1499 | ContentValues values = new ContentValues(); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1500 | |
Mindy Pereira | 29ef1b8 | 2012-01-13 11:26:21 -0800 | [diff] [blame] | 1501 | MessageModification.putToAddresses(values, to); |
| 1502 | MessageModification.putCcAddresses(values, cc); |
| 1503 | MessageModification.putBccAddresses(values, bcc); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1504 | |
Mindy Pereira | 29ef1b8 | 2012-01-13 11:26:21 -0800 | [diff] [blame] | 1505 | MessageModification.putSubject(values, subject); |
| 1506 | String htmlBody = Html.toHtml(body); |
| 1507 | boolean includeQuotedText = !TextUtils.isEmpty(quotedText); |
| 1508 | StringBuilder fullBody = new StringBuilder(htmlBody); |
| 1509 | if (includeQuotedText) { |
Mindy Pereira | e8caf12 | 2012-03-20 15:23:31 -0700 | [diff] [blame] | 1510 | // HTML gets converted to text for now |
| 1511 | final String text = quotedText.toString(); |
| 1512 | if (QuotedTextView.containsQuotedText(text)) { |
| 1513 | int pos = QuotedTextView.getQuotedTextOffset(text); |
| 1514 | fullBody.append(text.substring(0, pos)); |
| 1515 | MessageModification.putIncludeQuotedText(values, true); |
| 1516 | MessageModification.putQuoteStartPos(values, pos); |
Mindy Pereira | 29ef1b8 | 2012-01-13 11:26:21 -0800 | [diff] [blame] | 1517 | MessageModification.putForward(values, forward); |
Mindy Pereira | e8caf12 | 2012-03-20 15:23:31 -0700 | [diff] [blame] | 1518 | MessageModification.putAppendRefMessageContent(values, includeQuotedText); |
Mindy Pereira | 29ef1b8 | 2012-01-13 11:26:21 -0800 | [diff] [blame] | 1519 | } else { |
Mindy Pereira | e8caf12 | 2012-03-20 15:23:31 -0700 | [diff] [blame] | 1520 | LogUtils.w(LOG_TAG, "Couldn't find quoted text"); |
| 1521 | // This shouldn't happen, but just use what we have, |
| 1522 | // and don't do server-side expansion |
| 1523 | fullBody.append(text); |
Mindy Pereira | 29ef1b8 | 2012-01-13 11:26:21 -0800 | [diff] [blame] | 1524 | } |
| 1525 | } |
| 1526 | MessageModification.putBody(values, Html.fromHtml(fullBody.toString()).toString()); |
| 1527 | MessageModification.putBodyHtml(values, fullBody.toString()); |
Mindy Pereira | f944e96 | 2012-01-17 11:43:36 -0800 | [diff] [blame] | 1528 | MessageModification.putAttachments(values, attachments); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1529 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1530 | SendOrSaveMessage sendOrSaveMessage = new SendOrSaveMessage(account, selectedAccount, |
| 1531 | values, refMessageId, save); |
| 1532 | SendOrSaveTask sendOrSaveTask = new SendOrSaveTask(context, sendOrSaveMessage, callback); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1533 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1534 | callback.initializeSendOrSave(sendOrSaveTask); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1535 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1536 | // Do the send/save action on the specified handler to avoid possible |
| 1537 | // ANRs |
| 1538 | handler.post(sendOrSaveTask); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1539 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1540 | return sendOrSaveMessage.requestId(); |
| 1541 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1542 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1543 | private void sendOrSave(Spanned body, boolean save, boolean showToast, |
| 1544 | boolean orientationChanged) { |
| 1545 | // Check if user is a monkey. Monkeys can compose and hit send |
| 1546 | // button but are not allowed to send anything off the device. |
| 1547 | if (!save && ActivityManager.isUserAMonkey()) { |
| 1548 | return; |
| 1549 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1550 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1551 | String[] to, cc, bcc; |
| 1552 | if (orientationChanged) { |
| 1553 | to = cc = bcc = new String[0]; |
| 1554 | } else { |
| 1555 | to = getToAddresses(); |
| 1556 | cc = getCcAddresses(); |
| 1557 | bcc = getBccAddresses(); |
| 1558 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1559 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1560 | SendOrSaveCallback callback = new SendOrSaveCallback() { |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1561 | private int mRestoredRequestId; |
| 1562 | |
| 1563 | public void initializeSendOrSave(SendOrSaveTask sendOrSaveTask) { |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1564 | synchronized (mActiveTasks) { |
| 1565 | int numTasks = mActiveTasks.size(); |
| 1566 | if (numTasks == 0) { |
| 1567 | // Start service so we won't be killed if this app is |
| 1568 | // put in the background. |
| 1569 | startService(new Intent(ComposeActivity.this, EmptyService.class)); |
| 1570 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1571 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1572 | mActiveTasks.add(sendOrSaveTask); |
| 1573 | } |
| 1574 | if (sTestSendOrSaveCallback != null) { |
| 1575 | sTestSendOrSaveCallback.initializeSendOrSave(sendOrSaveTask); |
| 1576 | } |
| 1577 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1578 | |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 1579 | public void notifyMessageIdAllocated(SendOrSaveMessage sendOrSaveMessage, |
| 1580 | Message message) { |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1581 | synchronized (mDraftLock) { |
| 1582 | mDraftId = message.id; |
| 1583 | mDraft = message; |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 1584 | if (sRequestMessageIdMap != null) { |
| 1585 | sRequestMessageIdMap.put(sendOrSaveMessage.requestId(), mDraftId); |
| 1586 | } |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1587 | // Cache request message map, in case the process is killed |
| 1588 | saveRequestMap(); |
| 1589 | } |
| 1590 | if (sTestSendOrSaveCallback != null) { |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 1591 | sTestSendOrSaveCallback.notifyMessageIdAllocated(sendOrSaveMessage, message); |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1592 | } |
| 1593 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1594 | |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 1595 | public Message getMessage() { |
| 1596 | synchronized (mDraftLock) { |
| 1597 | return mDraft; |
| 1598 | } |
| 1599 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1600 | |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 1601 | public void sendOrSaveFinished(SendOrSaveTask task, boolean success) { |
| 1602 | if (success) { |
| 1603 | // Successfully sent or saved so reset change markers |
| 1604 | discardChanges(); |
| 1605 | } else { |
| 1606 | // A failure happened with saving/sending the draft |
| 1607 | // TODO(pwestbro): add a better string that should be used |
| 1608 | // when failing to send or save |
| 1609 | Toast.makeText(ComposeActivity.this, R.string.send_failed, Toast.LENGTH_SHORT) |
| 1610 | .show(); |
| 1611 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1612 | |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 1613 | int numTasks; |
| 1614 | synchronized (mActiveTasks) { |
| 1615 | // Remove the task from the list of active tasks |
| 1616 | mActiveTasks.remove(task); |
| 1617 | numTasks = mActiveTasks.size(); |
| 1618 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1619 | |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 1620 | if (numTasks == 0) { |
| 1621 | // Stop service so we can be killed. |
| 1622 | stopService(new Intent(ComposeActivity.this, EmptyService.class)); |
| 1623 | } |
| 1624 | if (sTestSendOrSaveCallback != null) { |
| 1625 | sTestSendOrSaveCallback.sendOrSaveFinished(task, success); |
| 1626 | } |
| 1627 | } |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1628 | }; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1629 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1630 | // Get the selected account if the from spinner has been setup. |
| 1631 | Account selectedAccount = mAccount; |
| 1632 | String fromAddress = selectedAccount.name; |
| 1633 | if (selectedAccount == null || fromAddress == null) { |
| 1634 | // We don't have either the selected account or from address, |
| 1635 | // use mAccount. |
| 1636 | selectedAccount = mAccount; |
| 1637 | fromAddress = mAccount.name; |
| 1638 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1639 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1640 | if (mSendSaveTaskHandler == null) { |
| 1641 | HandlerThread handlerThread = new HandlerThread("Send Message Task Thread"); |
| 1642 | handlerThread.start(); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1643 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1644 | mSendSaveTaskHandler = new Handler(handlerThread.getLooper()); |
| 1645 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1646 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1647 | mRequestId = sendOrSaveInternal(this, mAccount, selectedAccount, fromAddress, body, to, cc, |
| 1648 | bcc, mSubject.getText().toString(), mQuotedTextView.getQuotedText(), |
| 1649 | mAttachmentsView.getAttachments(), mRefMessageId, callback, mSendSaveTaskHandler, |
| 1650 | save, mForward); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1651 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1652 | if (mRecipient != null && mRecipient.equals(mAccount.name)) { |
| 1653 | mRecipient = selectedAccount.name; |
| 1654 | } |
| 1655 | mAccount = selectedAccount; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1656 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1657 | // Don't display the toast if the user is just changing the orientation, |
| 1658 | // but we still need to save the draft to the cursor because this is how we restore |
| 1659 | // the attachments when the configuration change completes. |
| 1660 | if (showToast && (getChangingConfigurations() & ActivityInfo.CONFIG_ORIENTATION) == 0) { |
| 1661 | Toast.makeText(this, save ? R.string.message_saved : R.string.sending_message, |
| 1662 | Toast.LENGTH_LONG).show(); |
| 1663 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1664 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1665 | // Need to update variables here because the send or save completes |
| 1666 | // asynchronously even though the toast shows right away. |
| 1667 | discardChanges(); |
| 1668 | updateSaveUi(); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1669 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1670 | // If we are sending, finish the activity |
| 1671 | if (!save) { |
| 1672 | finish(); |
| 1673 | } |
| 1674 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1675 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1676 | /** |
| 1677 | * Save the state of the request messageid map. This allows for the Gmail |
| 1678 | * process to be killed, but and still allow for ComposeActivity instances |
| 1679 | * to be recreated correctly. |
| 1680 | */ |
| 1681 | private void saveRequestMap() { |
| 1682 | // TODO: store the request map in user preferences. |
| 1683 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1684 | |
Mindy Pereira | 013194c | 2012-01-06 15:09:33 -0800 | [diff] [blame] | 1685 | public void doAttach() { |
| 1686 | Intent i = new Intent(Intent.ACTION_GET_CONTENT); |
| 1687 | i.addCategory(Intent.CATEGORY_OPENABLE); |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1688 | if (android.provider.Settings.System.getInt(getContentResolver(), |
| 1689 | UIProvider.getAttachmentTypeSetting(), 0) != 0) { |
Mindy Pereira | 013194c | 2012-01-06 15:09:33 -0800 | [diff] [blame] | 1690 | i.setType("*/*"); |
| 1691 | } else { |
| 1692 | i.setType("image/*"); |
| 1693 | } |
| 1694 | mAddingAttachment = true; |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1695 | startActivityForResult(Intent.createChooser(i, getText(R.string.select_attachment_type)), |
| 1696 | RESULT_PICK_ATTACHMENT); |
Mindy Pereira | 013194c | 2012-01-06 15:09:33 -0800 | [diff] [blame] | 1697 | } |
| 1698 | |
Mindy Pereira | ec8b0ed | 2012-01-06 10:35:59 -0800 | [diff] [blame] | 1699 | private void showCcBccViews() { |
Mindy Pereira | a26b54e | 2012-01-06 12:54:33 -0800 | [diff] [blame] | 1700 | mCcBccView.show(true, true, true); |
Mindy Pereira | ec8b0ed | 2012-01-06 10:35:59 -0800 | [diff] [blame] | 1701 | if (mCcBccButton != null) { |
| 1702 | mCcBccButton.setVisibility(View.GONE); |
| 1703 | } |
| 1704 | } |
| 1705 | |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 1706 | @Override |
| 1707 | public boolean onNavigationItemSelected(int position, long itemId) { |
Mindy Pereira | a26b54e | 2012-01-06 12:54:33 -0800 | [diff] [blame] | 1708 | int initialComposeMode = mComposeMode; |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 1709 | if (position == ComposeActivity.REPLY) { |
| 1710 | mComposeMode = ComposeActivity.REPLY; |
| 1711 | } else if (position == ComposeActivity.REPLY_ALL) { |
| 1712 | mComposeMode = ComposeActivity.REPLY_ALL; |
| 1713 | } else if (position == ComposeActivity.FORWARD) { |
| 1714 | mComposeMode = ComposeActivity.FORWARD; |
| 1715 | } |
Mindy Pereira | a26b54e | 2012-01-06 12:54:33 -0800 | [diff] [blame] | 1716 | if (initialComposeMode != mComposeMode) { |
Mindy Pereira | 154386a | 2012-01-11 13:02:33 -0800 | [diff] [blame] | 1717 | resetMessageForModeChange(); |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame^] | 1718 | if (mRefMessage != null) { |
| 1719 | initFromRefMessage(mComposeMode, mAccount.name); |
| 1720 | } |
Mindy Pereira | a26b54e | 2012-01-06 12:54:33 -0800 | [diff] [blame] | 1721 | } |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 1722 | return true; |
| 1723 | } |
| 1724 | |
Mindy Pereira | 154386a | 2012-01-11 13:02:33 -0800 | [diff] [blame] | 1725 | private void resetMessageForModeChange() { |
| 1726 | // When switching between reply, reply all, forward, |
| 1727 | // follow the behavior of webview. |
| 1728 | // The contents of the following fields are cleared |
| 1729 | // so that they can be populated directly from the |
| 1730 | // ref message: |
| 1731 | // 1) Any recipient fields |
| 1732 | // 2) The subject |
| 1733 | mTo.setText(""); |
| 1734 | mCc.setText(""); |
| 1735 | mBcc.setText(""); |
| 1736 | // Any edits to the subject are replaced with the original subject. |
| 1737 | mSubject.setText(""); |
| 1738 | |
| 1739 | // Any changes to the contents of the following fields are kept: |
| 1740 | // 1) Body |
| 1741 | // 2) Attachments |
| 1742 | // If the user made changes to attachments, keep their changes. |
| 1743 | if (!mAttachmentsChanged) { |
| 1744 | mAttachmentsView.deleteAllAttachments(); |
| 1745 | } |
| 1746 | } |
| 1747 | |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 1748 | private class ComposeModeAdapter extends ArrayAdapter<String> { |
| 1749 | |
| 1750 | private LayoutInflater mInflater; |
| 1751 | |
| 1752 | public ComposeModeAdapter(Context context) { |
| 1753 | super(context, R.layout.compose_mode_item, R.id.mode, getResources() |
| 1754 | .getStringArray(R.array.compose_modes)); |
| 1755 | } |
| 1756 | |
| 1757 | private LayoutInflater getInflater() { |
| 1758 | if (mInflater == null) { |
| 1759 | mInflater = LayoutInflater.from(getContext()); |
| 1760 | } |
| 1761 | return mInflater; |
| 1762 | } |
| 1763 | |
| 1764 | @Override |
| 1765 | public View getView(int position, View convertView, ViewGroup parent) { |
| 1766 | if (convertView == null) { |
| 1767 | convertView = getInflater().inflate(R.layout.compose_mode_display_item, null); |
| 1768 | } |
| 1769 | ((TextView) convertView.findViewById(R.id.mode)).setText(getItem(position)); |
| 1770 | return super.getView(position, convertView, parent); |
| 1771 | } |
| 1772 | } |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 1773 | |
| 1774 | @Override |
| 1775 | public void onRespondInline(String text) { |
| 1776 | appendToBody(text, false); |
| 1777 | } |
| 1778 | |
| 1779 | /** |
| 1780 | * Append text to the body of the message. If there is no existing body |
| 1781 | * text, just sets the body to text. |
| 1782 | * |
| 1783 | * @param text |
| 1784 | * @param withSignature True to append a signature. |
| 1785 | */ |
| 1786 | public void appendToBody(CharSequence text, boolean withSignature) { |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 1787 | Editable bodyText = mBodyView.getEditableText(); |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 1788 | if (bodyText != null && bodyText.length() > 0) { |
| 1789 | bodyText.append(text); |
| 1790 | } else { |
| 1791 | setBody(text, withSignature); |
| 1792 | } |
| 1793 | } |
| 1794 | |
| 1795 | /** |
| 1796 | * Set the body of the message. |
Mindy Pereira | bdf7a40 | 2012-03-01 15:23:26 -0800 | [diff] [blame] | 1797 | * |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 1798 | * @param text |
| 1799 | * @param withSignature True to append a signature. |
| 1800 | */ |
| 1801 | public void setBody(CharSequence text, boolean withSignature) { |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 1802 | mBodyView.setText(text); |
Mindy Pereira | bdf7a40 | 2012-03-01 15:23:26 -0800 | [diff] [blame] | 1803 | if (withSignature) { |
| 1804 | appendSignature(); |
| 1805 | } |
| 1806 | } |
| 1807 | |
| 1808 | private void appendSignature() { |
| 1809 | mSignature = mCachedSettings != null ? mCachedSettings.signature : null; |
| 1810 | if (!TextUtils.isEmpty(mSignature)) { |
| 1811 | // Appending a signature does not count as changing text. |
| 1812 | mBodyView.removeTextChangedListener(this); |
| 1813 | mBodyView.append(convertToPrintableSignature(mSignature)); |
| 1814 | mBodyView.addTextChangedListener(this); |
| 1815 | } |
| 1816 | } |
| 1817 | |
| 1818 | private String convertToPrintableSignature(String signature) { |
| 1819 | String signatureResource = getResources().getString(R.string.signature); |
| 1820 | if (signature == null) { |
| 1821 | signature = ""; |
| 1822 | } |
| 1823 | return String.format(signatureResource, signature); |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 1824 | } |
Mindy Pereira | 1a95a57 | 2012-01-05 12:21:29 -0800 | [diff] [blame] | 1825 | |
Mindy Pereira | 5a85e2b | 2012-01-11 09:53:32 -0800 | [diff] [blame] | 1826 | @Override |
| 1827 | public void onAccountChanged() { |
| 1828 | Account selectedAccountInfo = mFromSpinner.getCurrentAccount(); |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1829 | if (!mAccount.equals(selectedAccountInfo)) { |
| 1830 | mAccount = selectedAccountInfo; |
| 1831 | mCachedSettings = null; |
| 1832 | getLoaderManager().restartLoader(ACCOUNT_SETTINGS_LOADER, null, this); |
| 1833 | // TODO: handle discarding attachments when switching accounts. |
| 1834 | // Only enable save for this draft if there is any other content |
| 1835 | // in the message. |
| 1836 | if (!isBlank()) { |
| 1837 | enableSave(true); |
| 1838 | } |
| 1839 | mReplyFromChanged = true; |
| 1840 | initRecipients(); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1841 | } |
Mindy Pereira | 1a95a57 | 2012-01-05 12:21:29 -0800 | [diff] [blame] | 1842 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1843 | |
| 1844 | public void enableSave(boolean enabled) { |
| 1845 | if (mSave != null) { |
| 1846 | mSave.setEnabled(enabled); |
| 1847 | } |
| 1848 | } |
| 1849 | |
| 1850 | public void enableSend(boolean enabled) { |
| 1851 | if (mSend != null) { |
| 1852 | mSend.setEnabled(enabled); |
| 1853 | } |
| 1854 | } |
| 1855 | |
| 1856 | /** |
| 1857 | * Handles button clicks from any error dialogs dealing with sending |
| 1858 | * a message. |
| 1859 | */ |
| 1860 | @Override |
| 1861 | public void onClick(DialogInterface dialog, int which) { |
| 1862 | switch (which) { |
| 1863 | case DialogInterface.BUTTON_POSITIVE: { |
| 1864 | doDiscardWithoutConfirmation(true /* show toast */ ); |
| 1865 | break; |
| 1866 | } |
| 1867 | case DialogInterface.BUTTON_NEGATIVE: { |
| 1868 | // If the user cancels the send, re-enable the send button. |
| 1869 | enableSend(true); |
| 1870 | break; |
| 1871 | } |
| 1872 | } |
| 1873 | |
| 1874 | } |
| 1875 | |
Mindy Pereira | efe3d25 | 2012-03-01 14:20:44 -0800 | [diff] [blame] | 1876 | private void doDiscard() { |
| 1877 | new AlertDialog.Builder(this).setMessage(R.string.confirm_discard_text) |
| 1878 | .setPositiveButton(R.string.ok, this) |
| 1879 | .setNegativeButton(R.string.cancel, null) |
| 1880 | .create().show(); |
| 1881 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1882 | /** |
| 1883 | * Effectively discard the current message. |
| 1884 | * |
| 1885 | * This method is either invoked from the menu or from the dialog |
| 1886 | * once the user has confirmed that they want to discard the message. |
| 1887 | * @param showToast show "Message discarded" toast if true |
| 1888 | */ |
| 1889 | private void doDiscardWithoutConfirmation(boolean showToast) { |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 1890 | synchronized (mDraftLock) { |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1891 | if (mDraftId != UIProvider.INVALID_MESSAGE_ID) { |
| 1892 | ContentValues values = new ContentValues(); |
Paul Westbrook | b7050e6 | 2012-03-20 12:59:44 -0700 | [diff] [blame] | 1893 | values.put(BaseColumns._ID, mDraftId); |
Mindy Pereira | cfb7f33 | 2012-02-28 10:23:43 -0800 | [diff] [blame] | 1894 | if (mAccount.expungeMessageUri != null) { |
| 1895 | getContentResolver().update(mAccount.expungeMessageUri, values, null, null); |
| 1896 | } else { |
| 1897 | // TODO(mindyp): call delete on this conversation instead. |
| 1898 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1899 | // This is not strictly necessary (since we should not try to |
| 1900 | // save the draft after calling this) but it ensures that if we |
| 1901 | // do save again for some reason we make a new draft rather than |
| 1902 | // trying to resave an expunged draft. |
| 1903 | mDraftId = UIProvider.INVALID_MESSAGE_ID; |
| 1904 | } |
| 1905 | } |
| 1906 | |
| 1907 | if (showToast) { |
| 1908 | // Display a toast to let the user know |
| 1909 | Toast.makeText(this, R.string.message_discarded, Toast.LENGTH_SHORT).show(); |
| 1910 | } |
| 1911 | |
| 1912 | // This prevents the draft from being saved in onPause(). |
| 1913 | discardChanges(); |
| 1914 | finish(); |
| 1915 | } |
| 1916 | |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 1917 | private void saveIfNeeded() { |
| 1918 | if (mAccount == null) { |
| 1919 | // We have not chosen an account yet so there's no way that we can save. This is ok, |
| 1920 | // though, since we are saving our state before AccountsActivity is activated. Thus, the |
| 1921 | // user has not interacted with us yet and there is no real state to save. |
| 1922 | return; |
| 1923 | } |
| 1924 | |
| 1925 | if (shouldSave()) { |
| 1926 | doSave(!mAddingAttachment /* show toast */, true /* reset IME */); |
| 1927 | } |
| 1928 | } |
| 1929 | |
| 1930 | private void saveIfNeededOnOrientationChanged() { |
| 1931 | if (mAccount == null) { |
| 1932 | // We have not chosen an account yet so there's no way that we can save. This is ok, |
| 1933 | // though, since we are saving our state before AccountsActivity is activated. Thus, the |
| 1934 | // user has not interacted with us yet and there is no real state to save. |
| 1935 | return; |
| 1936 | } |
| 1937 | |
| 1938 | if (shouldSave()) { |
| 1939 | doSaveOrientationChanged(!mAddingAttachment /* show toast */, true /* reset IME */); |
| 1940 | } |
| 1941 | } |
| 1942 | |
| 1943 | /** |
| 1944 | * Save a draft if a draft already exists or the message is not empty. |
| 1945 | */ |
| 1946 | public void doSaveOrientationChanged(boolean showToast, boolean resetIME) { |
| 1947 | saveOnOrientationChanged(); |
| 1948 | if (resetIME) { |
| 1949 | // Clear the IME composing suggestions from the body. |
| 1950 | BaseInputConnection.removeComposingSpans(mBodyView.getEditableText()); |
| 1951 | } |
| 1952 | } |
| 1953 | |
| 1954 | protected boolean saveOnOrientationChanged() { |
| 1955 | return sendOrSaveWithSanityChecks(true, false, true); |
| 1956 | } |
| 1957 | |
| 1958 | @Override |
| 1959 | public void onAttachmentDeleted() { |
| 1960 | mAttachmentsChanged = true; |
| 1961 | updateSaveUi(); |
| 1962 | } |
Mindy Pereira | 75f6663 | 2012-01-11 11:42:02 -0800 | [diff] [blame] | 1963 | |
| 1964 | |
| 1965 | /** |
| 1966 | * This is called any time one of our text fields changes. |
| 1967 | */ |
| 1968 | public void afterTextChanged(Editable s) { |
| 1969 | mTextChanged = true; |
| 1970 | updateSaveUi(); |
| 1971 | } |
| 1972 | |
| 1973 | @Override |
| 1974 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { |
| 1975 | // Do nothing. |
| 1976 | } |
| 1977 | |
| 1978 | public void onTextChanged(CharSequence s, int start, int before, int count) { |
| 1979 | // Do nothing. |
| 1980 | } |
| 1981 | |
| 1982 | |
| 1983 | // There is a big difference between the text associated with an address changing |
| 1984 | // to add the display name or to format properly and a recipient being added or deleted. |
| 1985 | // Make sure we only notify of changes when a recipient has been added or deleted. |
| 1986 | private class RecipientTextWatcher implements TextWatcher { |
| 1987 | private HashMap<String, Integer> mContent = new HashMap<String, Integer>(); |
| 1988 | |
| 1989 | private RecipientEditTextView mView; |
| 1990 | |
| 1991 | private TextWatcher mListener; |
| 1992 | |
| 1993 | public RecipientTextWatcher(RecipientEditTextView view, TextWatcher listener) { |
| 1994 | mView = view; |
| 1995 | mListener = listener; |
| 1996 | } |
| 1997 | |
| 1998 | @Override |
| 1999 | public void afterTextChanged(Editable s) { |
| 2000 | if (hasChanged()) { |
| 2001 | mListener.afterTextChanged(s); |
| 2002 | } |
| 2003 | } |
| 2004 | |
| 2005 | private boolean hasChanged() { |
| 2006 | String[] currRecips = tokenizeRecips(getAddressesFromList(mView)); |
| 2007 | int totalCount = currRecips.length; |
| 2008 | int totalPrevCount = 0; |
| 2009 | for (Entry<String, Integer> entry : mContent.entrySet()) { |
| 2010 | totalPrevCount += entry.getValue(); |
| 2011 | } |
| 2012 | if (totalCount != totalPrevCount) { |
| 2013 | return true; |
| 2014 | } |
| 2015 | |
| 2016 | for (String recip : currRecips) { |
| 2017 | if (!mContent.containsKey(recip)) { |
| 2018 | return true; |
| 2019 | } else { |
| 2020 | int count = mContent.get(recip) - 1; |
| 2021 | if (count < 0) { |
| 2022 | return true; |
| 2023 | } else { |
| 2024 | mContent.put(recip, count); |
| 2025 | } |
| 2026 | } |
| 2027 | } |
| 2028 | return false; |
| 2029 | } |
| 2030 | |
| 2031 | private String[] tokenizeRecips(String[] recips) { |
| 2032 | // Tokenize them all and put them in the list. |
| 2033 | String[] recipAddresses = new String[recips.length]; |
| 2034 | for (int i = 0; i < recips.length; i++) { |
| 2035 | recipAddresses[i] = Rfc822Tokenizer.tokenize(recips[i])[0].getAddress(); |
| 2036 | } |
| 2037 | return recipAddresses; |
| 2038 | } |
| 2039 | |
| 2040 | @Override |
| 2041 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { |
| 2042 | String[] recips = tokenizeRecips(getAddressesFromList(mView)); |
| 2043 | for (String recip : recips) { |
| 2044 | if (!mContent.containsKey(recip)) { |
| 2045 | mContent.put(recip, 1); |
| 2046 | } else { |
| 2047 | mContent.put(recip, (mContent.get(recip)) + 1); |
| 2048 | } |
| 2049 | } |
| 2050 | } |
| 2051 | |
| 2052 | @Override |
| 2053 | public void onTextChanged(CharSequence s, int start, int before, int count) { |
| 2054 | // Do nothing. |
| 2055 | } |
| 2056 | } |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2057 | |
| 2058 | @Override |
| 2059 | public Loader<Cursor> onCreateLoader(int id, Bundle args) { |
| 2060 | if (id == ACCOUNT_SETTINGS_LOADER) { |
Mindy Pereira | 23e9fde | 2012-03-20 15:08:24 -0700 | [diff] [blame] | 2061 | if (mAccount != null && mAccount.settingsQueryUri != null) { |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2062 | return new CursorLoader(this, mAccount.settingsQueryUri, |
| 2063 | UIProvider.SETTINGS_PROJECTION, null, null, null); |
| 2064 | } |
| 2065 | } |
| 2066 | return null; |
| 2067 | } |
| 2068 | |
| 2069 | @Override |
| 2070 | public void onLoadFinished(Loader<Cursor> loader, Cursor data) { |
| 2071 | if (loader.getId() == ACCOUNT_SETTINGS_LOADER) { |
| 2072 | if (data != null) { |
| 2073 | data.moveToFirst(); |
| 2074 | mCachedSettings = new Settings(data); |
Mindy Pereira | bdf7a40 | 2012-03-01 15:23:26 -0800 | [diff] [blame] | 2075 | appendSignature(); |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2076 | } |
| 2077 | } |
| 2078 | } |
| 2079 | |
| 2080 | @Override |
| 2081 | public void onLoaderReset(Loader<Cursor> loader) { |
| 2082 | // Do nothing. |
| 2083 | } |
Mindy Pereira | 8e9305e | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 2084 | } |