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