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 | |
Andrew Sapperstein | ebc4bda | 2012-08-07 10:53:28 -0700 | [diff] [blame] | 19 | import android.animation.LayoutTransition; |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 20 | import android.app.ActionBar; |
Andy Huang | 5c5fd57 | 2012-04-08 18:19:29 -0700 | [diff] [blame] | 21 | import android.app.ActionBar.OnNavigationListener; |
| 22 | import android.app.Activity; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 23 | import android.app.ActivityManager; |
| 24 | import android.app.AlertDialog; |
| 25 | import android.app.Dialog; |
Mindy Pereira | 96a7f7a | 2012-07-09 16:51:06 -0700 | [diff] [blame] | 26 | import android.app.LoaderManager; |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 27 | import android.content.ContentResolver; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 28 | import android.content.ContentValues; |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 29 | import android.content.Context; |
Mindy Pereira | 96a7f7a | 2012-07-09 16:51:06 -0700 | [diff] [blame] | 30 | import android.content.CursorLoader; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 31 | import android.content.DialogInterface; |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 32 | import android.content.Intent; |
Mindy Pereira | 96a7f7a | 2012-07-09 16:51:06 -0700 | [diff] [blame] | 33 | import android.content.Loader; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 34 | import android.content.pm.ActivityInfo; |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 35 | import android.database.Cursor; |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 36 | import android.net.Uri; |
Mindy Pereira | 8e9305e | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 37 | import android.os.Bundle; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 38 | import android.os.Handler; |
| 39 | import android.os.HandlerThread; |
Paul Westbrook | f97588b | 2012-03-20 11:11:37 -0700 | [diff] [blame] | 40 | import android.os.Parcelable; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 41 | import android.provider.BaseColumns; |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 42 | import android.text.Editable; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 43 | import android.text.Html; |
| 44 | import android.text.Spanned; |
Paul Westbrook | c182762 | 2012-01-06 11:27:12 -0800 | [diff] [blame] | 45 | import android.text.TextUtils; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 46 | import android.text.TextWatcher; |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 47 | import android.text.util.Rfc822Token; |
Mindy Pereira | c17d073 | 2011-12-29 10:46:19 -0800 | [diff] [blame] | 48 | import android.text.util.Rfc822Tokenizer; |
Mindy Pereira | 3cd4f40 | 2012-07-17 11:16:18 -0700 | [diff] [blame] | 49 | import android.view.Gravity; |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 50 | import android.view.LayoutInflater; |
Mindy Pereira | b47f3e2 | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 51 | import android.view.Menu; |
| 52 | import android.view.MenuInflater; |
| 53 | import android.view.MenuItem; |
Mindy Pereira | 8e9305e | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 54 | import android.view.View; |
| 55 | import android.view.View.OnClickListener; |
Andy Huang | 5c5fd57 | 2012-04-08 18:19:29 -0700 | [diff] [blame] | 56 | import android.view.ViewGroup; |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 57 | import android.view.inputmethod.BaseInputConnection; |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 58 | import android.widget.ArrayAdapter; |
Mindy Pereira | 8e9305e | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 59 | import android.widget.Button; |
Mindy Pereira | 433b198 | 2012-04-03 11:53:07 -0700 | [diff] [blame] | 60 | import android.widget.EditText; |
Mindy Pereira | 1f93668 | 2012-03-02 11:30:33 -0800 | [diff] [blame] | 61 | import android.widget.ImageView; |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 62 | import android.widget.TextView; |
Mindy Pereira | 013194c | 2012-01-06 15:09:33 -0800 | [diff] [blame] | 63 | import android.widget.Toast; |
Mindy Pereira | 7b56a61 | 2011-12-14 12:32:28 -0800 | [diff] [blame] | 64 | |
Mindy Pereira | c17d073 | 2011-12-29 10:46:19 -0800 | [diff] [blame] | 65 | import com.android.common.Rfc822Validator; |
Andy Huang | 5c5fd57 | 2012-04-08 18:19:29 -0700 | [diff] [blame] | 66 | import com.android.ex.chips.RecipientEditTextView; |
| 67 | import com.android.mail.R; |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 68 | import com.android.mail.compose.AttachmentsView.AttachmentDeletedListener; |
Mindy Pereira | 9932dee | 2012-01-10 16:09:50 -0800 | [diff] [blame] | 69 | import com.android.mail.compose.AttachmentsView.AttachmentFailureException; |
Mindy Pereira | 5a85e2b | 2012-01-11 09:53:32 -0800 | [diff] [blame] | 70 | import com.android.mail.compose.FromAddressSpinner.OnAccountChangedListener; |
Andy Huang | 30e2c24 | 2012-01-06 18:14:30 -0800 | [diff] [blame] | 71 | import com.android.mail.compose.QuotedTextView.RespondInlineListener; |
Mindy Pereira | 33fe908 | 2012-01-09 16:24:30 -0800 | [diff] [blame] | 72 | import com.android.mail.providers.Account; |
Andy Huang | 30e2c24 | 2012-01-06 18:14:30 -0800 | [diff] [blame] | 73 | import com.android.mail.providers.Address; |
| 74 | import com.android.mail.providers.Attachment; |
Mindy Pereira | 5ad0291 | 2012-07-09 09:57:18 -0700 | [diff] [blame] | 75 | import com.android.mail.providers.Folder; |
Mindy Pereira | 47d0e65 | 2012-07-23 09:45:07 -0700 | [diff] [blame] | 76 | import com.android.mail.providers.MailAppProvider; |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 77 | import com.android.mail.providers.Message; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 78 | import com.android.mail.providers.MessageModification; |
Mindy Pereira | 92551d0 | 2012-04-05 11:31:12 -0700 | [diff] [blame] | 79 | import com.android.mail.providers.ReplyFromAccount; |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 80 | import com.android.mail.providers.Settings; |
Andy Huang | 30e2c24 | 2012-01-06 18:14:30 -0800 | [diff] [blame] | 81 | import com.android.mail.providers.UIProvider; |
Mindy Pereira | 3ca5bad | 2012-04-16 11:02:42 -0700 | [diff] [blame] | 82 | import com.android.mail.providers.UIProvider.AccountCapabilities; |
Mindy Pereira | 1257586 | 2012-03-21 16:30:54 -0700 | [diff] [blame] | 83 | import com.android.mail.providers.UIProvider.DraftType; |
Mindy Pereira | fa20c1a | 2012-07-23 13:00:02 -0700 | [diff] [blame] | 84 | import com.android.mail.ui.MailActivity; |
Paul Westbrook | 92227f6 | 2012-03-20 10:32:51 -0700 | [diff] [blame] | 85 | import com.android.mail.utils.AccountUtils; |
Paul Westbrook | b334c90 | 2012-06-25 11:42:46 -0700 | [diff] [blame] | 86 | import com.android.mail.utils.LogTag; |
Andy Huang | 30e2c24 | 2012-01-06 18:14:30 -0800 | [diff] [blame] | 87 | import com.android.mail.utils.LogUtils; |
Andy Huang | 30e2c24 | 2012-01-06 18:14:30 -0800 | [diff] [blame] | 88 | import com.android.mail.utils.Utils; |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 89 | import com.google.common.annotations.VisibleForTesting; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 90 | import com.google.common.collect.Lists; |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 91 | import com.google.common.collect.Sets; |
Mindy Pereira | 8e9305e | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 92 | |
Mindy Pereira | 62de1b1 | 2012-04-06 12:17:56 -0700 | [diff] [blame] | 93 | import org.json.JSONException; |
| 94 | |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 95 | import java.io.UnsupportedEncodingException; |
| 96 | import java.net.URLDecoder; |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 97 | import java.util.ArrayList; |
Paul Westbrook | bb87b7f | 2012-03-20 16:20:07 -0700 | [diff] [blame] | 98 | import java.util.Arrays; |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 99 | import java.util.Collection; |
Mindy Pereira | 75f6663 | 2012-01-11 11:42:02 -0800 | [diff] [blame] | 100 | import java.util.HashMap; |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 101 | import java.util.HashSet; |
| 102 | import java.util.List; |
Paul Westbrook | 1c078cf | 2012-03-20 16:18:51 -0700 | [diff] [blame] | 103 | import java.util.Map.Entry; |
Paul Westbrook | bb87b7f | 2012-03-20 16:20:07 -0700 | [diff] [blame] | 104 | import java.util.Set; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 105 | import java.util.concurrent.ConcurrentHashMap; |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 106 | |
| 107 | public class ComposeActivity extends Activity implements OnClickListener, OnNavigationListener, |
Mindy Pereira | 5a85e2b | 2012-01-11 09:53:32 -0800 | [diff] [blame] | 108 | RespondInlineListener, DialogInterface.OnClickListener, TextWatcher, |
Mindy Pereira | 96a7f7a | 2012-07-09 16:51:06 -0700 | [diff] [blame] | 109 | AttachmentDeletedListener, OnAccountChangedListener, LoaderManager.LoaderCallbacks<Cursor> { |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 110 | // Identifiers for which type of composition this is |
Mindy Pereira | 36bbcae | 2012-04-25 09:27:04 -0700 | [diff] [blame] | 111 | static final int COMPOSE = -1; |
| 112 | static final int REPLY = 0; |
| 113 | static final int REPLY_ALL = 1; |
| 114 | static final int FORWARD = 2; |
| 115 | static final int EDIT_DRAFT = 3; |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 116 | |
| 117 | // Integer extra holding one of the above compose action |
Mindy Pereira | 96a7f7a | 2012-07-09 16:51:06 -0700 | [diff] [blame] | 118 | protected static final String EXTRA_ACTION = "action"; |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 119 | |
Mindy Pereira | 326689d | 2012-05-17 10:14:14 -0700 | [diff] [blame] | 120 | private static final String EXTRA_SHOW_CC = "showCc"; |
| 121 | private static final String EXTRA_SHOW_BCC = "showBcc"; |
Mindy Pereira | a34c9a0 | 2012-04-17 14:10:53 -0700 | [diff] [blame] | 122 | |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 123 | private static final String UTF8_ENCODING_NAME = "UTF-8"; |
| 124 | |
| 125 | private static final String MAIL_TO = "mailto"; |
| 126 | |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 127 | private static final String EXTRA_SUBJECT = "subject"; |
| 128 | |
| 129 | private static final String EXTRA_BODY = "body"; |
| 130 | |
Mindy Pereira | 9a42bb4 | 2012-04-18 15:21:33 -0700 | [diff] [blame] | 131 | private static final String EXTRA_FROM_ACCOUNT_STRING = "fromAccountString"; |
| 132 | |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 133 | // Extra that we can get passed from other activities |
| 134 | private static final String EXTRA_TO = "to"; |
| 135 | private static final String EXTRA_CC = "cc"; |
| 136 | private static final String EXTRA_BCC = "bcc"; |
| 137 | |
| 138 | // List of all the fields |
| 139 | static final String[] ALL_EXTRAS = { EXTRA_SUBJECT, EXTRA_BODY, EXTRA_TO, EXTRA_CC, EXTRA_BCC }; |
| 140 | |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 141 | private static SendOrSaveCallback sTestSendOrSaveCallback = null; |
| 142 | // Map containing information about requests to create new messages, and the id of the |
| 143 | // messages that were the result of those requests. |
| 144 | // |
| 145 | // This map is used when the activity that initiated the save a of a new message, is killed |
| 146 | // before the save has completed (and when we know the id of the newly created message). When |
| 147 | // a save is completed, the service that is running in the background, will update the map |
| 148 | // |
| 149 | // When a new ComposeActivity instance is created, it will attempt to use the information in |
| 150 | // the previously instantiated map. If ComposeActivity.onCreate() is called, with a bundle |
| 151 | // (restoring data from a previous instance), and the map hasn't been created, we will attempt |
| 152 | // to populate the map with data stored in shared preferences. |
| 153 | private static ConcurrentHashMap<Integer, Long> sRequestMessageIdMap = null; |
| 154 | // Key used to store the above map |
| 155 | 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] | 156 | /** |
| 157 | * Notifies the {@code Activity} that the caller is an Email |
| 158 | * {@code Activity}, so that the back behavior may be modified accordingly. |
| 159 | * |
| 160 | * @see #onAppUpPressed |
| 161 | */ |
Paul Westbrook | daecb4b | 2012-05-31 10:21:26 -0700 | [diff] [blame] | 162 | public static final String EXTRA_FROM_EMAIL_TASK = "fromemail"; |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 163 | |
Mindy Pereira | e011b1d | 2012-06-18 13:45:26 -0700 | [diff] [blame] | 164 | public static final String EXTRA_ATTACHMENTS = "attachments"; |
Paul Westbrook | f97588b | 2012-03-20 11:11:37 -0700 | [diff] [blame] | 165 | |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 166 | // If this is a reply/forward then this extra will hold the original message |
Mindy Pereira | 36bbcae | 2012-04-25 09:27:04 -0700 | [diff] [blame] | 167 | private static final String EXTRA_IN_REFERENCE_TO_MESSAGE = "in-reference-to-message"; |
Mindy Pereira | b18e5a9 | 2012-07-10 11:47:21 -0700 | [diff] [blame] | 168 | // If this is a reply/forward then this extra will hold a uri we must query |
| 169 | // to get the original message. |
| 170 | protected static final String EXTRA_IN_REFERENCE_TO_MESSAGE_URI = "in-reference-to-message-uri"; |
Paul Westbrook | bb87b7f | 2012-03-20 16:20:07 -0700 | [diff] [blame] | 171 | // If this is an action to edit an existing draft messagge, this extra will hold the |
| 172 | // draft message |
| 173 | private static final String ORIGINAL_DRAFT_MESSAGE = "original-draft-message"; |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 174 | private static final String END_TOKEN = ", "; |
Paul Westbrook | b334c90 | 2012-06-25 11:42:46 -0700 | [diff] [blame] | 175 | private static final String LOG_TAG = LogTag.getLogTag(); |
Mindy Pereira | 013194c | 2012-01-06 15:09:33 -0800 | [diff] [blame] | 176 | // Request numbers for activities we start |
| 177 | private static final int RESULT_PICK_ATTACHMENT = 1; |
| 178 | private static final int RESULT_CREATE_ACCOUNT = 2; |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 179 | // TODO(mindyp) set mime-type for auto send? |
Mindy Pereira | e011b1d | 2012-06-18 13:45:26 -0700 | [diff] [blame] | 180 | public static final String AUTO_SEND_ACTION = "com.android.mail.action.AUTO_SEND"; |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 181 | |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 182 | private static final String EXTRA_SELECTED_REPLY_FROM_ACCOUNT = "replyFromAccount"; |
| 183 | private static final String EXTRA_REQUEST_ID = "requestId"; |
| 184 | private static final String EXTRA_FOCUS_SELECTION_START = "focusSelectionStart"; |
| 185 | private static final String EXTRA_FOCUS_SELECTION_END = null; |
| 186 | private static final String EXTRA_MESSAGE = "extraMessage"; |
Mindy Pereira | 96a7f7a | 2012-07-09 16:51:06 -0700 | [diff] [blame] | 187 | private static final int REFERENCE_MESSAGE_LOADER = 0; |
Mindy Pereira | 47d0e65 | 2012-07-23 09:45:07 -0700 | [diff] [blame] | 188 | private static final String EXTRA_SELECTED_ACCOUNT = "selectedAccount"; |
Mindy Pereira | 8e9305e | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 189 | |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 190 | /** |
| 191 | * A single thread for running tasks in the background. |
| 192 | */ |
| 193 | private Handler mSendSaveTaskHandler = null; |
Mindy Pereira | c17d073 | 2011-12-29 10:46:19 -0800 | [diff] [blame] | 194 | private RecipientEditTextView mTo; |
| 195 | private RecipientEditTextView mCc; |
| 196 | private RecipientEditTextView mBcc; |
Mindy Pereira | 8e9305e | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 197 | private Button mCcBccButton; |
| 198 | private CcBccView mCcBccView; |
Mindy Pereira | 7b56a61 | 2011-12-14 12:32:28 -0800 | [diff] [blame] | 199 | private AttachmentsView mAttachmentsView; |
Mindy Pereira | 33fe908 | 2012-01-09 16:24:30 -0800 | [diff] [blame] | 200 | private Account mAccount; |
Mindy Pereira | 92551d0 | 2012-04-05 11:31:12 -0700 | [diff] [blame] | 201 | private ReplyFromAccount mReplyFromAccount; |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 202 | private Settings mCachedSettings; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 203 | private Rfc822Validator mValidator; |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 204 | private TextView mSubject; |
| 205 | |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 206 | private ComposeModeAdapter mComposeModeAdapter; |
| 207 | private int mComposeMode = -1; |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 208 | private boolean mForward; |
| 209 | private String mRecipient; |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 210 | private QuotedTextView mQuotedTextView; |
Mindy Pereira | 433b198 | 2012-04-03 11:53:07 -0700 | [diff] [blame] | 211 | private EditText mBodyView; |
Mindy Pereira | 1a95a57 | 2012-01-05 12:21:29 -0800 | [diff] [blame] | 212 | private View mFromStatic; |
Mindy Pereira | 2eb1732 | 2012-03-07 10:07:34 -0800 | [diff] [blame] | 213 | private TextView mFromStaticText; |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 214 | private View mFromSpinnerWrapper; |
Mindy Pereira | 1883b34 | 2012-06-20 08:34:56 -0700 | [diff] [blame] | 215 | @VisibleForTesting |
| 216 | protected FromAddressSpinner mFromSpinner; |
Mindy Pereira | 013194c | 2012-01-06 15:09:33 -0800 | [diff] [blame] | 217 | private boolean mAddingAttachment; |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 218 | private boolean mAttachmentsChanged; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 219 | private boolean mTextChanged; |
| 220 | private boolean mReplyFromChanged; |
| 221 | private MenuItem mSave; |
| 222 | private MenuItem mSend; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 223 | private AlertDialog mRecipientErrorDialog; |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 224 | private AlertDialog mSendConfirmDialog; |
Mindy Pereira | b3112a2 | 2012-06-20 12:10:03 -0700 | [diff] [blame] | 225 | @VisibleForTesting |
| 226 | protected Message mRefMessage; |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 227 | private long mDraftId = UIProvider.INVALID_MESSAGE_ID; |
| 228 | private Message mDraft; |
| 229 | private Object mDraftLock = new Object(); |
Mindy Pereira | 1f93668 | 2012-03-02 11:30:33 -0800 | [diff] [blame] | 230 | private ImageView mAttachmentsButton; |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 231 | |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 232 | /** |
Paul Westbrook | daecb4b | 2012-05-31 10:21:26 -0700 | [diff] [blame] | 233 | * Boolean indicating whether ComposeActivity was launched from a Gmail controlled view. |
| 234 | */ |
| 235 | private boolean mLaunchedFromEmail = false; |
Mindy Pereira | cbfb75a | 2012-06-25 14:52:23 -0700 | [diff] [blame] | 236 | private RecipientTextWatcher mToListener; |
| 237 | private RecipientTextWatcher mCcListener; |
| 238 | private RecipientTextWatcher mBccListener; |
Mindy Pereira | b18e5a9 | 2012-07-10 11:47:21 -0700 | [diff] [blame] | 239 | private Uri mRefMessageUri; |
Paul Westbrook | daecb4b | 2012-05-31 10:21:26 -0700 | [diff] [blame] | 240 | |
| 241 | |
| 242 | /** |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 243 | * Can be called from a non-UI thread. |
| 244 | */ |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 245 | public static void editDraft(Context launcher, Account account, Message message) { |
Paul Westbrook | bb87b7f | 2012-03-20 16:20:07 -0700 | [diff] [blame] | 246 | launch(launcher, account, message, EDIT_DRAFT); |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 247 | } |
| 248 | |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 249 | /** |
| 250 | * Can be called from a non-UI thread. |
| 251 | */ |
Mindy Pereira | 33fe908 | 2012-01-09 16:24:30 -0800 | [diff] [blame] | 252 | public static void compose(Context launcher, Account account) { |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 253 | launch(launcher, account, null, COMPOSE); |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Can be called from a non-UI thread. |
| 258 | */ |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 259 | public static void reply(Context launcher, Account account, Message message) { |
| 260 | launch(launcher, account, message, REPLY); |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Can be called from a non-UI thread. |
| 265 | */ |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 266 | public static void replyAll(Context launcher, Account account, Message message) { |
| 267 | launch(launcher, account, message, REPLY_ALL); |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Can be called from a non-UI thread. |
| 272 | */ |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 273 | public static void forward(Context launcher, Account account, Message message) { |
| 274 | launch(launcher, account, message, FORWARD); |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 275 | } |
| 276 | |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 277 | private static void launch(Context launcher, Account account, Message message, int action) { |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 278 | Intent intent = new Intent(launcher, ComposeActivity.class); |
| 279 | intent.putExtra(EXTRA_FROM_EMAIL_TASK, true); |
| 280 | intent.putExtra(EXTRA_ACTION, action); |
| 281 | intent.putExtra(Utils.EXTRA_ACCOUNT, account); |
Paul Westbrook | bb87b7f | 2012-03-20 16:20:07 -0700 | [diff] [blame] | 282 | if (action == EDIT_DRAFT) { |
| 283 | intent.putExtra(ORIGINAL_DRAFT_MESSAGE, message); |
| 284 | } else { |
| 285 | intent.putExtra(EXTRA_IN_REFERENCE_TO_MESSAGE, message); |
| 286 | } |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 287 | launcher.startActivity(intent); |
| 288 | } |
Mindy Pereira | 8e9305e | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 289 | |
| 290 | @Override |
| 291 | public void onCreate(Bundle savedInstanceState) { |
| 292 | super.onCreate(savedInstanceState); |
Mindy Pereira | 3528d36 | 2012-01-05 14:39:44 -0800 | [diff] [blame] | 293 | setContentView(R.layout.compose); |
| 294 | findViews(); |
Mindy Pereira | 818143e | 2012-01-11 13:59:49 -0800 | [diff] [blame] | 295 | Intent intent = getIntent(); |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 296 | Message message; |
Mindy Pereira | 71c9e56 | 2012-05-17 11:01:02 -0700 | [diff] [blame] | 297 | boolean showQuotedText = false; |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 298 | int action; |
Mindy Pereira | 47d0e65 | 2012-07-23 09:45:07 -0700 | [diff] [blame] | 299 | // Check for any of the possibly supplied accounts.; |
| 300 | Account account = null; |
Mindy Pereira | f7fc6c3 | 2012-06-19 15:18:33 -0700 | [diff] [blame] | 301 | if (hadSavedInstanceStateMessage(savedInstanceState)) { |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 302 | action = savedInstanceState.getInt(EXTRA_ACTION, COMPOSE); |
| 303 | account = savedInstanceState.getParcelable(Utils.EXTRA_ACCOUNT); |
| 304 | message = (Message) savedInstanceState.getParcelable(EXTRA_MESSAGE); |
| 305 | mRefMessage = (Message) savedInstanceState.getParcelable(EXTRA_IN_REFERENCE_TO_MESSAGE); |
| 306 | } else { |
Mindy Pereira | 47d0e65 | 2012-07-23 09:45:07 -0700 | [diff] [blame] | 307 | account = obtainAccount(intent); |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 308 | action = intent.getIntExtra(EXTRA_ACTION, COMPOSE); |
| 309 | // Initialize the message from the message in the intent |
| 310 | message = (Message) intent.getParcelableExtra(ORIGINAL_DRAFT_MESSAGE); |
| 311 | mRefMessage = (Message) intent.getParcelableExtra(EXTRA_IN_REFERENCE_TO_MESSAGE); |
Mindy Pereira | b18e5a9 | 2012-07-10 11:47:21 -0700 | [diff] [blame] | 312 | mRefMessageUri = (Uri) intent.getParcelableExtra(EXTRA_IN_REFERENCE_TO_MESSAGE_URI); |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 313 | } |
Paul Westbrook | 92227f6 | 2012-03-20 10:32:51 -0700 | [diff] [blame] | 314 | |
| 315 | setAccount(account); |
Mindy Pereira | 818143e | 2012-01-11 13:59:49 -0800 | [diff] [blame] | 316 | if (mAccount == null) { |
| 317 | return; |
| 318 | } |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 319 | |
Paul Westbrook | daecb4b | 2012-05-31 10:21:26 -0700 | [diff] [blame] | 320 | if (intent.getBooleanExtra(EXTRA_FROM_EMAIL_TASK, false)) { |
| 321 | mLaunchedFromEmail = true; |
| 322 | } else if (Intent.ACTION_SEND.equals(intent.getAction())) { |
| 323 | final Uri dataUri = intent.getData(); |
| 324 | if (dataUri != null) { |
| 325 | final String dataScheme = intent.getData().getScheme(); |
| 326 | final String accountScheme = mAccount.composeIntentUri.getScheme(); |
| 327 | mLaunchedFromEmail = TextUtils.equals(dataScheme, accountScheme); |
| 328 | } |
| 329 | } |
| 330 | |
Mindy Pereira | 96a7f7a | 2012-07-09 16:51:06 -0700 | [diff] [blame] | 331 | if (mRefMessageUri != null) { |
| 332 | // We have a referenced message that we must look up. |
| 333 | getLoaderManager().initLoader(REFERENCE_MESSAGE_LOADER, null, this); |
| 334 | return; |
| 335 | } else if (message != null && action != EDIT_DRAFT) { |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 336 | initFromDraftMessage(message); |
| 337 | initQuotedTextFromRefMessage(mRefMessage, action); |
Mindy Pereira | a34c9a0 | 2012-04-17 14:10:53 -0700 | [diff] [blame] | 338 | showCcBcc(savedInstanceState); |
Mindy Pereira | 71c9e56 | 2012-05-17 11:01:02 -0700 | [diff] [blame] | 339 | showQuotedText = message.appendRefMessageContent; |
Paul Westbrook | bb87b7f | 2012-03-20 16:20:07 -0700 | [diff] [blame] | 340 | } else if (action == EDIT_DRAFT) { |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 341 | initFromDraftMessage(message); |
Mindy Pereira | ef38830 | 2012-06-18 19:07:44 -0700 | [diff] [blame] | 342 | boolean showBcc = !TextUtils.isEmpty(message.bcc); |
| 343 | boolean showCc = showBcc || !TextUtils.isEmpty(message.cc); |
| 344 | mCcBccView.show(false, showCc, showBcc); |
Paul Westbrook | bb87b7f | 2012-03-20 16:20:07 -0700 | [diff] [blame] | 345 | // Update the action to the draft type of the previous draft |
| 346 | switch (message.draftType) { |
| 347 | case UIProvider.DraftType.REPLY: |
| 348 | action = REPLY; |
| 349 | break; |
| 350 | case UIProvider.DraftType.REPLY_ALL: |
| 351 | action = REPLY_ALL; |
| 352 | break; |
| 353 | case UIProvider.DraftType.FORWARD: |
| 354 | action = FORWARD; |
| 355 | break; |
| 356 | case UIProvider.DraftType.COMPOSE: |
| 357 | default: |
| 358 | action = COMPOSE; |
| 359 | break; |
| 360 | } |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 361 | initQuotedTextFromRefMessage(mRefMessage, action); |
Mindy Pereira | 71c9e56 | 2012-05-17 11:01:02 -0700 | [diff] [blame] | 362 | showQuotedText = message.appendRefMessageContent; |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 363 | } else if ((action == REPLY || action == REPLY_ALL || action == FORWARD)) { |
| 364 | if (mRefMessage != null) { |
| 365 | initFromRefMessage(action, mAccount.name); |
Mindy Pereira | 71c9e56 | 2012-05-17 11:01:02 -0700 | [diff] [blame] | 366 | showQuotedText = true; |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 367 | } |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 368 | } else { |
| 369 | initFromExtras(intent); |
Paul Westbrook | bb87b7f | 2012-03-20 16:20:07 -0700 | [diff] [blame] | 370 | } |
Mindy Pereira | 96a7f7a | 2012-07-09 16:51:06 -0700 | [diff] [blame] | 371 | finishSetup(action, intent, savedInstanceState, showQuotedText); |
| 372 | } |
Paul Westbrook | bb87b7f | 2012-03-20 16:20:07 -0700 | [diff] [blame] | 373 | |
Mindy Pereira | 47d0e65 | 2012-07-23 09:45:07 -0700 | [diff] [blame] | 374 | private Account obtainAccount(Intent intent) { |
| 375 | Account account = null; |
| 376 | Object accountExtra = null; |
| 377 | if (intent != null && intent.getExtras() != null) { |
| 378 | accountExtra = intent.getExtras().get(Utils.EXTRA_ACCOUNT); |
| 379 | if (accountExtra instanceof Account) { |
| 380 | return (Account) accountExtra; |
| 381 | } |
| 382 | accountExtra = intent.getStringExtra(EXTRA_SELECTED_ACCOUNT); |
| 383 | } |
| 384 | if (account == null) { |
| 385 | final String lastAccountUri = MailAppProvider.getInstance().getLastSentFromAccount(); |
| 386 | if (!TextUtils.isEmpty(lastAccountUri)) { |
| 387 | accountExtra = Uri.parse(lastAccountUri); |
| 388 | } |
| 389 | } |
| 390 | final Account[] syncingAccounts = AccountUtils.getSyncingAccounts(this); |
| 391 | if (syncingAccounts.length > 0) { |
| 392 | if (accountExtra instanceof String && !TextUtils.isEmpty((String) accountExtra)) { |
| 393 | // For backwards compatibility, we need to check account |
| 394 | // names. |
| 395 | for (Account a : syncingAccounts) { |
| 396 | if (a.name.equals(accountExtra)) { |
| 397 | account = a; |
| 398 | } |
| 399 | } |
| 400 | } else if (accountExtra instanceof Uri) { |
| 401 | // The uri of the last viewed account is what is stored in |
| 402 | // the current code base. |
| 403 | for (Account a : syncingAccounts) { |
| 404 | if (a.uri.equals(accountExtra)) { |
| 405 | account = a; |
| 406 | } |
| 407 | } |
Mindy Pereira | fa20c1a | 2012-07-23 13:00:02 -0700 | [diff] [blame] | 408 | } else { |
| 409 | account = syncingAccounts[0]; |
Mindy Pereira | 47d0e65 | 2012-07-23 09:45:07 -0700 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | return account; |
| 413 | } |
| 414 | |
Mindy Pereira | 96a7f7a | 2012-07-09 16:51:06 -0700 | [diff] [blame] | 415 | private void finishSetup(int action, Intent intent, Bundle savedInstanceState, |
| 416 | boolean showQuotedText) { |
Paul Westbrook | bb87b7f | 2012-03-20 16:20:07 -0700 | [diff] [blame] | 417 | if (action == COMPOSE) { |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 418 | mQuotedTextView.setVisibility(View.GONE); |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 419 | } |
Mindy Pereira | 818143e | 2012-01-11 13:59:49 -0800 | [diff] [blame] | 420 | initRecipients(); |
Mindy Pereira | f7fc6c3 | 2012-06-19 15:18:33 -0700 | [diff] [blame] | 421 | // Don't bother with the intent if we have procured a message from the |
| 422 | // intent already. |
| 423 | if (!hadSavedInstanceStateMessage(savedInstanceState)) { |
| 424 | initAttachmentsFromIntent(intent); |
| 425 | } |
Mindy Pereira | 1a95a57 | 2012-01-05 12:21:29 -0800 | [diff] [blame] | 426 | initActionBar(action); |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 427 | initFromSpinner(savedInstanceState != null ? savedInstanceState : intent.getExtras(), |
| 428 | action); |
Mindy Pereira | 75f6663 | 2012-01-11 11:42:02 -0800 | [diff] [blame] | 429 | initChangeListeners(); |
Mindy Pereira | 433b198 | 2012-04-03 11:53:07 -0700 | [diff] [blame] | 430 | setFocus(action); |
Mindy Pereira | 326689d | 2012-05-17 10:14:14 -0700 | [diff] [blame] | 431 | updateHideOrShowCcBcc(); |
Mindy Pereira | 71c9e56 | 2012-05-17 11:01:02 -0700 | [diff] [blame] | 432 | updateHideOrShowQuotedText(showQuotedText); |
| 433 | } |
| 434 | |
Mindy Pereira | f7fc6c3 | 2012-06-19 15:18:33 -0700 | [diff] [blame] | 435 | private boolean hadSavedInstanceStateMessage(Bundle savedInstanceState) { |
| 436 | return savedInstanceState != null && savedInstanceState.containsKey(EXTRA_MESSAGE); |
| 437 | } |
| 438 | |
Mindy Pereira | 71c9e56 | 2012-05-17 11:01:02 -0700 | [diff] [blame] | 439 | private void updateHideOrShowQuotedText(boolean showQuotedText) { |
| 440 | mQuotedTextView.updateCheckedState(showQuotedText); |
Mindy Pereira | 433b198 | 2012-04-03 11:53:07 -0700 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | private void setFocus(int action) { |
| 444 | if (action == EDIT_DRAFT) { |
| 445 | int type = mDraft.draftType; |
| 446 | switch (type) { |
| 447 | case UIProvider.DraftType.COMPOSE: |
| 448 | case UIProvider.DraftType.FORWARD: |
| 449 | action = COMPOSE; |
| 450 | break; |
| 451 | case UIProvider.DraftType.REPLY: |
| 452 | case UIProvider.DraftType.REPLY_ALL: |
| 453 | default: |
| 454 | action = REPLY; |
| 455 | break; |
| 456 | } |
| 457 | } |
| 458 | switch (action) { |
| 459 | case FORWARD: |
| 460 | case COMPOSE: |
| 461 | mTo.requestFocus(); |
| 462 | break; |
| 463 | case REPLY: |
| 464 | case REPLY_ALL: |
| 465 | default: |
| 466 | focusBody(); |
| 467 | break; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | /** |
| 472 | * Focus the body of the message. |
| 473 | */ |
| 474 | public void focusBody() { |
| 475 | mBodyView.requestFocus(); |
| 476 | int length = mBodyView.getText().length(); |
| 477 | |
| 478 | int signatureStartPos = getSignatureStartPosition( |
| 479 | mSignature, mBodyView.getText().toString()); |
| 480 | if (signatureStartPos > -1) { |
| 481 | // In case the user deleted the newlines... |
| 482 | mBodyView.setSelection(signatureStartPos); |
| 483 | } else if (length > 0) { |
| 484 | // Move cursor to the end. |
| 485 | mBodyView.setSelection(length); |
| 486 | } |
Mindy Pereira | 1a95a57 | 2012-01-05 12:21:29 -0800 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | @Override |
| 490 | protected void onResume() { |
| 491 | super.onResume(); |
| 492 | // Update the from spinner as other accounts |
| 493 | // may now be available. |
Mindy Pereira | 818143e | 2012-01-11 13:59:49 -0800 | [diff] [blame] | 494 | if (mFromSpinner != null && mAccount != null) { |
Mindy Pereira | 62de1b1 | 2012-04-06 12:17:56 -0700 | [diff] [blame] | 495 | mFromSpinner.asyncInitFromSpinner(mComposeMode, mAccount); |
Mindy Pereira | 818143e | 2012-01-11 13:59:49 -0800 | [diff] [blame] | 496 | } |
Mindy Pereira | 1a95a57 | 2012-01-05 12:21:29 -0800 | [diff] [blame] | 497 | } |
| 498 | |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 499 | @Override |
| 500 | protected void onPause() { |
| 501 | super.onPause(); |
| 502 | |
| 503 | if (mSendConfirmDialog != null) { |
| 504 | mSendConfirmDialog.dismiss(); |
| 505 | } |
| 506 | if (mRecipientErrorDialog != null) { |
| 507 | mRecipientErrorDialog.dismiss(); |
| 508 | } |
Mindy Pereira | a214833 | 2012-07-02 13:54:14 -0700 | [diff] [blame] | 509 | // When the user exits the compose view, see if this draft needs saving. |
| 510 | if (isFinishing()) { |
| 511 | saveIfNeeded(); |
| 512 | } |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | @Override |
| 516 | protected final void onActivityResult(int request, int result, Intent data) { |
| 517 | mAddingAttachment = false; |
| 518 | |
| 519 | if (result == RESULT_OK && request == RESULT_PICK_ATTACHMENT) { |
| 520 | addAttachmentAndUpdateView(data); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | @Override |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 525 | public final void onRestoreInstanceState(Bundle savedInstanceState) { |
| 526 | super.onRestoreInstanceState(savedInstanceState); |
| 527 | if (savedInstanceState != null) { |
| 528 | if (savedInstanceState.containsKey(EXTRA_FOCUS_SELECTION_START)) { |
| 529 | int selectionStart = savedInstanceState.getInt(EXTRA_FOCUS_SELECTION_START); |
| 530 | int selectionEnd = savedInstanceState.getInt(EXTRA_FOCUS_SELECTION_END); |
| 531 | // There should be a focus and it should be an EditText since we |
| 532 | // only save these extras if these conditions are true. |
| 533 | EditText focusEditText = (EditText) getCurrentFocus(); |
| 534 | final int length = focusEditText.getText().length(); |
| 535 | if (selectionStart < length && selectionEnd < length) { |
| 536 | focusEditText.setSelection(selectionStart, selectionEnd); |
| 537 | } |
| 538 | } |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | @Override |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 543 | public final void onSaveInstanceState(Bundle state) { |
| 544 | super.onSaveInstanceState(state); |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 545 | // The framework is happy to save and restore the selection but only if it also saves and |
| 546 | // restores the contents of the edit text. That's a lot of text to put in a bundle so we do |
| 547 | // this manually. |
| 548 | View focus = getCurrentFocus(); |
| 549 | if (focus != null && focus instanceof EditText) { |
| 550 | EditText focusEditText = (EditText) focus; |
| 551 | state.putInt(EXTRA_FOCUS_SELECTION_START, focusEditText.getSelectionStart()); |
| 552 | state.putInt(EXTRA_FOCUS_SELECTION_END, focusEditText.getSelectionEnd()); |
| 553 | } |
Paul Westbrook | 6273e96 | 2012-04-23 10:44:15 -0700 | [diff] [blame] | 554 | |
| 555 | final List<ReplyFromAccount> replyFromAccounts = mFromSpinner.getReplyFromAccounts(); |
Paul Westbrook | 151f1ad | 2012-04-24 09:13:00 -0700 | [diff] [blame] | 556 | final int selectedPos = mFromSpinner.getSelectedItemPosition(); |
Mindy Pereira | d90f7ac | 2012-06-27 10:31:06 -0700 | [diff] [blame] | 557 | final ReplyFromAccount selectedReplyFromAccount = (replyFromAccounts != null |
| 558 | && replyFromAccounts.size() > 0 && replyFromAccounts.size() > selectedPos) ? |
| 559 | replyFromAccounts.get(selectedPos) : null; |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 560 | if (selectedReplyFromAccount != null) { |
| 561 | state.putString(EXTRA_SELECTED_REPLY_FROM_ACCOUNT, selectedReplyFromAccount.serialize() |
| 562 | .toString()); |
| 563 | state.putParcelable(Utils.EXTRA_ACCOUNT, selectedReplyFromAccount.account); |
| 564 | } else { |
| 565 | state.putParcelable(Utils.EXTRA_ACCOUNT, mAccount); |
| 566 | } |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 567 | |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 568 | if (mDraftId == UIProvider.INVALID_MESSAGE_ID && mRequestId !=0) { |
| 569 | // We don't have a draft id, and we have a request id, |
| 570 | // save the request id. |
| 571 | state.putInt(EXTRA_REQUEST_ID, mRequestId); |
| 572 | } |
| 573 | |
| 574 | // We want to restore the current mode after a pause |
| 575 | // or rotation. |
| 576 | int mode = getMode(); |
| 577 | state.putInt(EXTRA_ACTION, mode); |
| 578 | |
| 579 | Message message = createMessage(selectedReplyFromAccount, mode); |
| 580 | state.putParcelable(EXTRA_MESSAGE, message); |
| 581 | |
| 582 | if (mRefMessage != null) { |
| 583 | state.putParcelable(EXTRA_IN_REFERENCE_TO_MESSAGE, mRefMessage); |
| 584 | } |
Mindy Pereira | 326689d | 2012-05-17 10:14:14 -0700 | [diff] [blame] | 585 | state.putBoolean(EXTRA_SHOW_CC, mCcBccView.isCcVisible()); |
| 586 | state.putBoolean(EXTRA_SHOW_BCC, mCcBccView.isBccVisible()); |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | private int getMode() { |
| 590 | int mode = ComposeActivity.COMPOSE; |
| 591 | ActionBar actionBar = getActionBar(); |
Mindy Pereira | e011b1d | 2012-06-18 13:45:26 -0700 | [diff] [blame] | 592 | if (actionBar != null |
| 593 | && actionBar.getNavigationMode() == ActionBar.NAVIGATION_MODE_LIST) { |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 594 | mode = actionBar.getSelectedNavigationIndex(); |
| 595 | } |
| 596 | return mode; |
| 597 | } |
| 598 | |
| 599 | private Message createMessage(ReplyFromAccount selectedReplyFromAccount, int mode) { |
| 600 | Message message = new Message(); |
| 601 | message.id = UIProvider.INVALID_MESSAGE_ID; |
Andy Huang | d47877e | 2012-08-09 19:31:24 -0700 | [diff] [blame^] | 602 | message.serverId = null; |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 603 | message.uri = null; |
| 604 | message.conversationUri = null; |
| 605 | message.subject = mSubject.getText().toString(); |
| 606 | message.snippet = null; |
Mindy Pereira | 1eedc75 | 2012-07-16 16:34:41 -0700 | [diff] [blame] | 607 | message.from = selectedReplyFromAccount != null ? selectedReplyFromAccount.address |
Mindy Pereira | b67aa8f | 2012-07-11 15:09:06 -0700 | [diff] [blame] | 608 | : mAccount != null ? mAccount.name : null; |
Mindy Pereira | 3c91158 | 2012-08-09 16:59:09 -0700 | [diff] [blame] | 609 | message.to = formatSenders(mTo.getText().toString()); |
| 610 | message.cc = formatSenders(mCc.getText().toString()); |
| 611 | message.bcc = formatSenders(mBcc.getText().toString()); |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 612 | message.replyTo = null; |
| 613 | message.dateReceivedMs = 0; |
| 614 | String htmlBody = Html.toHtml(mBodyView.getText()); |
| 615 | StringBuilder fullBody = new StringBuilder(htmlBody); |
| 616 | message.bodyHtml = fullBody.toString(); |
| 617 | message.bodyText = mBodyView.getText().toString(); |
| 618 | message.embedsExternalResources = false; |
| 619 | message.refMessageId = mRefMessage != null ? mRefMessage.uri.toString() : null; |
Mindy Pereira | d2bef8b | 2012-05-30 12:14:52 -0700 | [diff] [blame] | 620 | message.draftType = getDraftType(mode); |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 621 | message.appendRefMessageContent = mQuotedTextView.getQuotedTextIfIncluded() != null; |
| 622 | ArrayList<Attachment> attachments = mAttachmentsView.getAttachments(); |
| 623 | message.hasAttachments = attachments != null && attachments.size() > 0; |
| 624 | message.attachmentListUri = null; |
| 625 | message.messageFlags = 0; |
| 626 | message.saveUri = null; |
| 627 | message.sendUri = null; |
| 628 | message.alwaysShowImages = false; |
| 629 | message.attachmentsJson = Attachment.toJSONArray(attachments); |
| 630 | CharSequence quotedText = mQuotedTextView.getQuotedText(); |
| 631 | message.quotedTextOffset = !TextUtils.isEmpty(quotedText) ? QuotedTextView |
| 632 | .getQuotedTextOffset(quotedText.toString()) : -1; |
| 633 | message.accountUri = null; |
| 634 | return message; |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 635 | } |
| 636 | |
Mindy Pereira | 3c91158 | 2012-08-09 16:59:09 -0700 | [diff] [blame] | 637 | private String formatSenders(String string) { |
| 638 | if (!TextUtils.isEmpty(string) && string.charAt(string.length() - 1) == ',') { |
| 639 | return string.substring(0, string.length() - 1); |
| 640 | } |
| 641 | return string; |
| 642 | } |
| 643 | |
Mindy Pereira | 818143e | 2012-01-11 13:59:49 -0800 | [diff] [blame] | 644 | @VisibleForTesting |
| 645 | void setAccount(Account account) { |
Mindy Pereira | bb5217e | 2012-04-17 11:08:29 -0700 | [diff] [blame] | 646 | if (account == null) { |
| 647 | return; |
| 648 | } |
Mindy Pereira | 23e9fde | 2012-03-20 15:08:24 -0700 | [diff] [blame] | 649 | if (!account.equals(mAccount)) { |
| 650 | mAccount = account; |
Paul Westbrook | b1f573c | 2012-04-06 11:38:28 -0700 | [diff] [blame] | 651 | mCachedSettings = mAccount.settings; |
| 652 | appendSignature(); |
Mindy Pereira | 23e9fde | 2012-03-20 15:08:24 -0700 | [diff] [blame] | 653 | } |
Mindy Pereira | fa20c1a | 2012-07-23 13:00:02 -0700 | [diff] [blame] | 654 | if (mAccount != null) { |
| 655 | MailActivity.setForegroundNdef(MailActivity.getMailtoNdef(mAccount.name)); |
| 656 | } |
Mindy Pereira | 818143e | 2012-01-11 13:59:49 -0800 | [diff] [blame] | 657 | } |
| 658 | |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 659 | private void initFromSpinner(Bundle bundle, int action) { |
Mindy Pereira | 9a42bb4 | 2012-04-18 15:21:33 -0700 | [diff] [blame] | 660 | String accountString = null; |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 661 | if (action == EDIT_DRAFT && mDraft.draftType == UIProvider.DraftType.COMPOSE) { |
Mindy Pereira | 62de1b1 | 2012-04-06 12:17:56 -0700 | [diff] [blame] | 662 | action = COMPOSE; |
| 663 | } |
| 664 | mFromSpinner.asyncInitFromSpinner(action, mAccount); |
Mindy Pereira | 9a42bb4 | 2012-04-18 15:21:33 -0700 | [diff] [blame] | 665 | if (bundle != null) { |
| 666 | if (bundle.containsKey(EXTRA_SELECTED_REPLY_FROM_ACCOUNT)) { |
| 667 | mReplyFromAccount = ReplyFromAccount.deserialize(mAccount, |
| 668 | bundle.getString(EXTRA_SELECTED_REPLY_FROM_ACCOUNT)); |
| 669 | } else if (bundle.containsKey(EXTRA_FROM_ACCOUNT_STRING)) { |
| 670 | accountString = bundle.getString(EXTRA_FROM_ACCOUNT_STRING); |
| 671 | mReplyFromAccount = mFromSpinner.getMatchingReplyFromAccount(accountString); |
| 672 | } |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 673 | } |
| 674 | if (mReplyFromAccount == null) { |
| 675 | if (mDraft != null) { |
| 676 | mReplyFromAccount = getReplyFromAccountFromDraft(mAccount, mDraft); |
| 677 | } else if (mRefMessage != null) { |
| 678 | mReplyFromAccount = getReplyFromAccountForReply(mAccount, mRefMessage); |
| 679 | } |
Mindy Pereira | 62de1b1 | 2012-04-06 12:17:56 -0700 | [diff] [blame] | 680 | } |
| 681 | if (mReplyFromAccount == null) { |
| 682 | mReplyFromAccount = new ReplyFromAccount(mAccount, mAccount.uri, mAccount.name, |
Mindy Pereira | cd970dd | 2012-05-31 10:07:47 -0700 | [diff] [blame] | 683 | mAccount.name, mAccount.name, true, false); |
Mindy Pereira | 62de1b1 | 2012-04-06 12:17:56 -0700 | [diff] [blame] | 684 | } |
Mindy Pereira | 9a42bb4 | 2012-04-18 15:21:33 -0700 | [diff] [blame] | 685 | |
Mindy Pereira | 62de1b1 | 2012-04-06 12:17:56 -0700 | [diff] [blame] | 686 | mFromSpinner.setCurrentAccount(mReplyFromAccount); |
Mindy Pereira | 9a42bb4 | 2012-04-18 15:21:33 -0700 | [diff] [blame] | 687 | |
Mindy Pereira | 62de1b1 | 2012-04-06 12:17:56 -0700 | [diff] [blame] | 688 | if (mFromSpinner.getCount() > 1) { |
Mindy Pereira | a83e708 | 2012-03-30 08:53:11 -0700 | [diff] [blame] | 689 | // If there is only 1 account, just show that account. |
| 690 | // Otherwise, give the user the ability to choose which account to |
Mindy Pereira | 62de1b1 | 2012-04-06 12:17:56 -0700 | [diff] [blame] | 691 | // send mail from / save drafts to. |
| 692 | mFromStatic.setVisibility(View.GONE); |
Mindy Pereira | a83e708 | 2012-03-30 08:53:11 -0700 | [diff] [blame] | 693 | mFromStaticText.setText(mAccount.name); |
Mindy Pereira | 62de1b1 | 2012-04-06 12:17:56 -0700 | [diff] [blame] | 694 | mFromSpinnerWrapper.setVisibility(View.VISIBLE); |
Mindy Pereira | a83e708 | 2012-03-30 08:53:11 -0700 | [diff] [blame] | 695 | } else { |
| 696 | mFromStatic.setVisibility(View.VISIBLE); |
| 697 | mFromStaticText.setText(mAccount.name); |
| 698 | mFromSpinnerWrapper.setVisibility(View.GONE); |
Mindy Pereira | a83e708 | 2012-03-30 08:53:11 -0700 | [diff] [blame] | 699 | } |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 700 | } |
| 701 | |
Mindy Pereira | 62de1b1 | 2012-04-06 12:17:56 -0700 | [diff] [blame] | 702 | private ReplyFromAccount getReplyFromAccountForReply(Account account, Message refMessage) { |
| 703 | if (refMessage.accountUri != null) { |
| 704 | // This must be from combined inbox. |
| 705 | List<ReplyFromAccount> replyFromAccounts = mFromSpinner.getReplyFromAccounts(); |
| 706 | for (ReplyFromAccount from : replyFromAccounts) { |
| 707 | if (from.account.uri.equals(refMessage.accountUri)) { |
| 708 | return from; |
| 709 | } |
| 710 | } |
| 711 | return null; |
| 712 | } else { |
| 713 | return getReplyFromAccount(account, refMessage); |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | /** |
| 718 | * Given an account and which email address the message was sent to, |
| 719 | * return who the message should be sent from. |
| 720 | * @param account Account in which the message arrived. |
| 721 | * @param sentTo Email address to which the message was sent. |
| 722 | * @return the address from which to reply. |
| 723 | */ |
| 724 | public ReplyFromAccount getReplyFromAccount(Account account, Message refMessage) { |
| 725 | // First see if we are supposed to use the default address or |
| 726 | // the address it was sentTo. |
Mindy Pereira | 326689d | 2012-05-17 10:14:14 -0700 | [diff] [blame] | 727 | if (mCachedSettings.forceReplyFromDefault) { |
Mindy Pereira | 62de1b1 | 2012-04-06 12:17:56 -0700 | [diff] [blame] | 728 | return getDefaultReplyFromAccount(account); |
| 729 | } else { |
Mindy Pereira | 89bae57 | 2012-06-18 11:34:36 -0700 | [diff] [blame] | 730 | // If we aren't explicitly told which account to look for, look at |
Mindy Pereira | 62de1b1 | 2012-04-06 12:17:56 -0700 | [diff] [blame] | 731 | // all the message recipients and find one that matches |
| 732 | // a custom from or account. |
| 733 | List<String> allRecipients = new ArrayList<String>(); |
| 734 | allRecipients.addAll(Arrays.asList(Utils.splitCommaSeparatedString(refMessage.to))); |
| 735 | allRecipients.addAll(Arrays.asList(Utils.splitCommaSeparatedString(refMessage.cc))); |
| 736 | return getMatchingRecipient(account, allRecipients); |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | /** |
| 741 | * Compare all the recipients of an email to the current account and all |
| 742 | * custom addresses associated with that account. Return the match if there |
| 743 | * is one, or the default account if there isn't. |
| 744 | */ |
| 745 | protected ReplyFromAccount getMatchingRecipient(Account account, List<String> sentTo) { |
| 746 | // Tokenize the list and place in a hashmap. |
| 747 | ReplyFromAccount matchingReplyFrom = null; |
| 748 | Rfc822Token[] tokens; |
| 749 | HashSet<String> recipientsMap = new HashSet<String>(); |
| 750 | for (String address : sentTo) { |
| 751 | tokens = Rfc822Tokenizer.tokenize(address); |
| 752 | for (int i = 0; i < tokens.length; i++) { |
| 753 | recipientsMap.add(tokens[i].getAddress()); |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | int matchingAddressCount = 0; |
| 758 | List<ReplyFromAccount> customFroms; |
| 759 | try { |
| 760 | customFroms = FromAddressSpinner.getAccountSpecificFroms(account); |
| 761 | if (customFroms != null) { |
| 762 | for (ReplyFromAccount entry : customFroms) { |
| 763 | if (recipientsMap.contains(entry.address)) { |
| 764 | matchingReplyFrom = entry; |
| 765 | matchingAddressCount++; |
| 766 | } |
| 767 | } |
| 768 | } |
| 769 | } catch (JSONException e) { |
| 770 | LogUtils.wtf(LOG_TAG, "Exception parsing from addresses for account %s", |
| 771 | account.name); |
| 772 | } |
| 773 | if (matchingAddressCount > 1) { |
| 774 | matchingReplyFrom = getDefaultReplyFromAccount(account); |
| 775 | } |
| 776 | return matchingReplyFrom; |
| 777 | } |
| 778 | |
| 779 | private ReplyFromAccount getDefaultReplyFromAccount(Account account) { |
| 780 | List<ReplyFromAccount> replyFromAccounts = mFromSpinner.getReplyFromAccounts(); |
| 781 | for (ReplyFromAccount from : replyFromAccounts) { |
| 782 | if (from.isDefault) { |
| 783 | return from; |
| 784 | } |
| 785 | } |
Mindy Pereira | cd970dd | 2012-05-31 10:07:47 -0700 | [diff] [blame] | 786 | return new ReplyFromAccount(account, account.uri, account.name, account.name, account.name, |
| 787 | true, false); |
Mindy Pereira | 62de1b1 | 2012-04-06 12:17:56 -0700 | [diff] [blame] | 788 | } |
| 789 | |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 790 | private ReplyFromAccount getReplyFromAccountFromDraft(Account account, Message msg) { |
| 791 | String sender = msg.from; |
Mindy Pereira | 62de1b1 | 2012-04-06 12:17:56 -0700 | [diff] [blame] | 792 | ReplyFromAccount replyFromAccount = null; |
| 793 | List<ReplyFromAccount> replyFromAccounts = mFromSpinner.getReplyFromAccounts(); |
| 794 | if (TextUtils.equals(account.name, sender)) { |
| 795 | replyFromAccount = new ReplyFromAccount(mAccount, mAccount.uri, mAccount.name, |
Mindy Pereira | cd970dd | 2012-05-31 10:07:47 -0700 | [diff] [blame] | 796 | mAccount.name, mAccount.name, true, false); |
Mindy Pereira | 62de1b1 | 2012-04-06 12:17:56 -0700 | [diff] [blame] | 797 | } else { |
| 798 | for (ReplyFromAccount fromAccount : replyFromAccounts) { |
| 799 | if (TextUtils.equals(fromAccount.name, sender)) { |
| 800 | replyFromAccount = fromAccount; |
| 801 | break; |
| 802 | } |
| 803 | } |
| 804 | } |
| 805 | return replyFromAccount; |
| 806 | } |
| 807 | |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 808 | private void findViews() { |
Mindy Pereira | a26b54e | 2012-01-06 12:54:33 -0800 | [diff] [blame] | 809 | mCcBccButton = (Button) findViewById(R.id.add_cc_bcc); |
Mindy Pereira | 8e9305e | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 810 | if (mCcBccButton != null) { |
| 811 | mCcBccButton.setOnClickListener(this); |
| 812 | } |
| 813 | mCcBccView = (CcBccView) findViewById(R.id.cc_bcc_wrapper); |
Mindy Pereira | 7b56a61 | 2011-12-14 12:32:28 -0800 | [diff] [blame] | 814 | mAttachmentsView = (AttachmentsView)findViewById(R.id.attachments); |
Andrew Sapperstein | ca87d6d | 2012-08-08 16:14:52 -0700 | [diff] [blame] | 815 | LayoutTransition transition = |
| 816 | ((ViewGroup) findViewById(R.id.content)).getLayoutTransition(); |
| 817 | mAttachmentsView.setComposeLayoutTransition(transition); |
Mindy Pereira | 1f93668 | 2012-03-02 11:30:33 -0800 | [diff] [blame] | 818 | mAttachmentsButton = (ImageView) findViewById(R.id.add_attachment); |
| 819 | if (mAttachmentsButton != null) { |
| 820 | mAttachmentsButton.setOnClickListener(this); |
| 821 | } |
Mindy Pereira | 818143e | 2012-01-11 13:59:49 -0800 | [diff] [blame] | 822 | mTo = (RecipientEditTextView) findViewById(R.id.to); |
| 823 | mCc = (RecipientEditTextView) findViewById(R.id.cc); |
| 824 | mBcc = (RecipientEditTextView) findViewById(R.id.bcc); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 825 | // TODO: add special chips text change watchers before adding |
| 826 | // this as a text changed watcher to the to, cc, bcc fields. |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 827 | mSubject = (TextView) findViewById(R.id.subject); |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 828 | mQuotedTextView = (QuotedTextView) findViewById(R.id.quoted_text_view); |
| 829 | mQuotedTextView.setRespondInlineListener(this); |
Mindy Pereira | 433b198 | 2012-04-03 11:53:07 -0700 | [diff] [blame] | 830 | mBodyView = (EditText) findViewById(R.id.body); |
Mindy Pereira | 1a95a57 | 2012-01-05 12:21:29 -0800 | [diff] [blame] | 831 | mFromStatic = findViewById(R.id.static_from_content); |
Mindy Pereira | 2eb1732 | 2012-03-07 10:07:34 -0800 | [diff] [blame] | 832 | mFromStaticText = (TextView) findViewById(R.id.from_account_name); |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 833 | mFromSpinnerWrapper = findViewById(R.id.spinner_from_content); |
Mindy Pereira | 5a85e2b | 2012-01-11 09:53:32 -0800 | [diff] [blame] | 834 | mFromSpinner = (FromAddressSpinner) findViewById(R.id.from_picker); |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 835 | } |
| 836 | |
Mindy Pereira | e011b1d | 2012-06-18 13:45:26 -0700 | [diff] [blame] | 837 | protected TextView getBody() { |
| 838 | return mBodyView; |
| 839 | } |
| 840 | |
| 841 | @VisibleForTesting |
| 842 | public Account getFromAccount() { |
| 843 | return mReplyFromAccount != null && mReplyFromAccount.account != null ? |
| 844 | mReplyFromAccount.account : mAccount; |
| 845 | } |
| 846 | |
Mindy Pereira | cbfb75a | 2012-06-25 14:52:23 -0700 | [diff] [blame] | 847 | private void clearChangeListeners() { |
| 848 | mSubject.removeTextChangedListener(this); |
| 849 | mBodyView.removeTextChangedListener(this); |
| 850 | mTo.removeTextChangedListener(mToListener); |
| 851 | mCc.removeTextChangedListener(mCcListener); |
| 852 | mBcc.removeTextChangedListener(mBccListener); |
| 853 | mFromSpinner.setOnAccountChangedListener(null); |
| 854 | mAttachmentsView.setAttachmentChangesListener(null); |
| 855 | } |
| 856 | |
Mindy Pereira | 75f6663 | 2012-01-11 11:42:02 -0800 | [diff] [blame] | 857 | // Now that the message has been initialized from any existing draft or |
| 858 | // ref message data, set up listeners for any changes that occur to the |
| 859 | // message. |
| 860 | private void initChangeListeners() { |
| 861 | mSubject.addTextChangedListener(this); |
| 862 | mBodyView.addTextChangedListener(this); |
Mindy Pereira | cbfb75a | 2012-06-25 14:52:23 -0700 | [diff] [blame] | 863 | if (mToListener == null) { |
| 864 | mToListener = new RecipientTextWatcher(mTo, this); |
| 865 | } |
| 866 | mTo.addTextChangedListener(mToListener); |
| 867 | if (mCcListener == null) { |
| 868 | mCcListener = new RecipientTextWatcher(mCc, this); |
| 869 | } |
| 870 | mCc.addTextChangedListener(mCcListener); |
| 871 | if (mBccListener == null) { |
| 872 | mBccListener = new RecipientTextWatcher(mBcc, this); |
| 873 | } |
| 874 | mBcc.addTextChangedListener(mBccListener); |
Mindy Pereira | 75f6663 | 2012-01-11 11:42:02 -0800 | [diff] [blame] | 875 | mFromSpinner.setOnAccountChangedListener(this); |
Mindy Pereira | 818143e | 2012-01-11 13:59:49 -0800 | [diff] [blame] | 876 | mAttachmentsView.setAttachmentChangesListener(this); |
Mindy Pereira | 75f6663 | 2012-01-11 11:42:02 -0800 | [diff] [blame] | 877 | } |
| 878 | |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 879 | private void initActionBar(int action) { |
| 880 | mComposeMode = action; |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 881 | ActionBar actionBar = getActionBar(); |
Mindy Pereira | e011b1d | 2012-06-18 13:45:26 -0700 | [diff] [blame] | 882 | if (actionBar == null) { |
| 883 | return; |
| 884 | } |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 885 | if (action == ComposeActivity.COMPOSE) { |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 886 | actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); |
| 887 | actionBar.setTitle(R.string.compose); |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 888 | } else { |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 889 | actionBar.setTitle(null); |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 890 | if (mComposeModeAdapter == null) { |
| 891 | mComposeModeAdapter = new ComposeModeAdapter(this); |
| 892 | } |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 893 | actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); |
| 894 | actionBar.setListNavigationCallbacks(mComposeModeAdapter, this); |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 895 | switch (action) { |
| 896 | case ComposeActivity.REPLY: |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 897 | actionBar.setSelectedNavigationItem(0); |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 898 | break; |
| 899 | case ComposeActivity.REPLY_ALL: |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 900 | actionBar.setSelectedNavigationItem(1); |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 901 | break; |
| 902 | case ComposeActivity.FORWARD: |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 903 | actionBar.setSelectedNavigationItem(2); |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 904 | break; |
| 905 | } |
| 906 | } |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 907 | actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME, |
| 908 | ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME); |
| 909 | actionBar.setHomeButtonEnabled(true); |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 910 | } |
| 911 | |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 912 | private void initFromRefMessage(int action, String recipientAddress) { |
Mindy Pereira | 96a7f7a | 2012-07-09 16:51:06 -0700 | [diff] [blame] | 913 | setFieldsFromRefMessage(action, recipientAddress); |
| 914 | if (mRefMessage != null) { |
| 915 | // CC field only gets populated when doing REPLY_ALL. |
| 916 | // BCC never gets auto-populated, unless the user is editing |
| 917 | // a draft with one. |
Mindy Pereira | 29a717e | 2012-07-25 18:05:48 -0700 | [diff] [blame] | 918 | if (!TextUtils.isEmpty(mCc.getText()) && action == REPLY_ALL) { |
Mindy Pereira | 96a7f7a | 2012-07-09 16:51:06 -0700 | [diff] [blame] | 919 | mCcBccView.show(false, true, false); |
| 920 | } |
| 921 | } |
| 922 | updateHideOrShowCcBcc(); |
| 923 | } |
| 924 | |
| 925 | private void setFieldsFromRefMessage(int action, String recipientAddress) { |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 926 | setSubject(mRefMessage, action); |
| 927 | // Setup recipients |
| 928 | if (action == FORWARD) { |
| 929 | mForward = true; |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 930 | } |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 931 | initRecipientsFromRefMessage(recipientAddress, mRefMessage, action); |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 932 | initQuotedTextFromRefMessage(mRefMessage, action); |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 933 | if (action == ComposeActivity.FORWARD || mAttachmentsChanged) { |
| 934 | initAttachments(mRefMessage); |
| 935 | } |
Mindy Pereira | c17d073 | 2011-12-29 10:46:19 -0800 | [diff] [blame] | 936 | } |
| 937 | |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 938 | private void initFromDraftMessage(Message message) { |
Paul Westbrook | bb87b7f | 2012-03-20 16:20:07 -0700 | [diff] [blame] | 939 | LogUtils.d(LOG_TAG, "Intializing draft from previous draft message"); |
| 940 | |
| 941 | mDraft = message; |
| 942 | mDraftId = message.id; |
| 943 | mSubject.setText(message.subject); |
| 944 | mForward = message.draftType == UIProvider.DraftType.FORWARD; |
| 945 | final List<String> toAddresses = Arrays.asList(message.getToAddresses()); |
| 946 | addToAddresses(toAddresses); |
| 947 | addCcAddresses(Arrays.asList(message.getCcAddresses()), toAddresses); |
| 948 | addBccAddresses(Arrays.asList(message.getBccAddresses())); |
Mindy Pereira | 2421dc8 | 2012-03-27 13:32:31 -0700 | [diff] [blame] | 949 | if (message.hasAttachments) { |
| 950 | List<Attachment> attachments = message.getAttachments(); |
| 951 | for (Attachment a : attachments) { |
Andy Huang | 5c5fd57 | 2012-04-08 18:19:29 -0700 | [diff] [blame] | 952 | addAttachmentAndUpdateView(a); |
Mindy Pereira | 2421dc8 | 2012-03-27 13:32:31 -0700 | [diff] [blame] | 953 | } |
| 954 | } |
Mindy Pereira | cc8e7db | 2012-05-30 12:57:42 -0700 | [diff] [blame] | 955 | int quotedTextIndex = message.appendRefMessageContent ? |
Mindy Pereira | 002ff52 | 2012-05-30 10:31:26 -0700 | [diff] [blame] | 956 | message.quotedTextOffset : -1; |
Paul Westbrook | bb87b7f | 2012-03-20 16:20:07 -0700 | [diff] [blame] | 957 | // Set the body |
Mindy Pereira | 002ff52 | 2012-05-30 10:31:26 -0700 | [diff] [blame] | 958 | CharSequence quotedText = null; |
Paul Westbrook | bb87b7f | 2012-03-20 16:20:07 -0700 | [diff] [blame] | 959 | if (!TextUtils.isEmpty(message.bodyHtml)) { |
Mindy Pereira | 752222d | 2012-07-19 09:58:53 -0700 | [diff] [blame] | 960 | CharSequence htmlText = ""; |
Mindy Pereira | 002ff52 | 2012-05-30 10:31:26 -0700 | [diff] [blame] | 961 | if (quotedTextIndex > -1) { |
Mindy Pereira | 752222d | 2012-07-19 09:58:53 -0700 | [diff] [blame] | 962 | // Find the offset in the htmltext of the actual quoted text and strip it out. |
| 963 | quotedTextIndex = QuotedTextView.findQuotedTextIndex(message.bodyHtml); |
| 964 | if (quotedTextIndex > -1) { |
| 965 | htmlText = Html.fromHtml(message.bodyHtml.substring(0, quotedTextIndex)); |
| 966 | quotedText = message.bodyHtml.subSequence(quotedTextIndex, |
| 967 | message.bodyHtml.length()); |
| 968 | } |
Mindy Pereira | 002ff52 | 2012-05-30 10:31:26 -0700 | [diff] [blame] | 969 | } |
| 970 | mBodyView.setText(htmlText); |
Paul Westbrook | bb87b7f | 2012-03-20 16:20:07 -0700 | [diff] [blame] | 971 | } else { |
Mindy Pereira | 752222d | 2012-07-19 09:58:53 -0700 | [diff] [blame] | 972 | final String body = message.bodyText; |
| 973 | final CharSequence bodyText = !TextUtils.isEmpty(body) ? |
| 974 | (quotedTextIndex > -1 ? |
| 975 | message.bodyText.substring(0, quotedTextIndex) : message.bodyText) |
| 976 | : ""; |
Mindy Pereira | 002ff52 | 2012-05-30 10:31:26 -0700 | [diff] [blame] | 977 | if (quotedTextIndex > -1) { |
Mindy Pereira | 752222d | 2012-07-19 09:58:53 -0700 | [diff] [blame] | 978 | quotedText = !TextUtils.isEmpty(body) ? message.bodyText.substring(quotedTextIndex) |
| 979 | : null; |
Mindy Pereira | 002ff52 | 2012-05-30 10:31:26 -0700 | [diff] [blame] | 980 | } |
| 981 | mBodyView.setText(bodyText); |
| 982 | } |
| 983 | if (quotedTextIndex > -1 && quotedText != null) { |
Mindy Pereira | 3971323 | 2012-05-30 11:48:41 -0700 | [diff] [blame] | 984 | mQuotedTextView.setQuotedTextFromDraft(quotedText, mForward); |
Paul Westbrook | bb87b7f | 2012-03-20 16:20:07 -0700 | [diff] [blame] | 985 | } |
Paul Westbrook | bb87b7f | 2012-03-20 16:20:07 -0700 | [diff] [blame] | 986 | } |
| 987 | |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 988 | /** |
| 989 | * Fill all the widgets with the content found in the Intent Extra, if any. |
| 990 | * Also apply the same style to all widgets. Note: if initFromExtras is |
| 991 | * called as a result of switching between reply, reply all, and forward per |
| 992 | * the latest revision of Gmail, and the user has already made changes to |
| 993 | * attachments on a previous incarnation of the message (as a reply, reply |
| 994 | * all, or forward), the original attachments from the message will not be |
| 995 | * re-instantiated. The user's changes will be respected. This follows the |
| 996 | * web gmail interaction. |
| 997 | */ |
| 998 | public void initFromExtras(Intent intent) { |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 999 | // If we were invoked with a SENDTO intent, the value |
| 1000 | // should take precedence |
| 1001 | final Uri dataUri = intent.getData(); |
| 1002 | if (dataUri != null) { |
| 1003 | if (MAIL_TO.equals(dataUri.getScheme())) { |
| 1004 | initFromMailTo(dataUri.toString()); |
| 1005 | } else { |
Mindy Pereira | 0b4f28e | 2012-03-28 14:12:21 -0700 | [diff] [blame] | 1006 | if (!mAccount.composeIntentUri.equals(dataUri)) { |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 1007 | String toText = dataUri.getSchemeSpecificPart(); |
| 1008 | if (toText != null) { |
| 1009 | mTo.setText(""); |
Mindy Pereira | dbe8996 | 2012-04-13 09:42:38 -0700 | [diff] [blame] | 1010 | addToAddresses(Arrays.asList(TextUtils.split(toText, ","))); |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 1011 | } |
| 1012 | } |
| 1013 | } |
| 1014 | } |
| 1015 | |
| 1016 | String[] extraStrings = intent.getStringArrayExtra(Intent.EXTRA_EMAIL); |
| 1017 | if (extraStrings != null) { |
| 1018 | addToAddresses(Arrays.asList(extraStrings)); |
| 1019 | } |
| 1020 | extraStrings = intent.getStringArrayExtra(Intent.EXTRA_CC); |
| 1021 | if (extraStrings != null) { |
| 1022 | addCcAddresses(Arrays.asList(extraStrings), null); |
| 1023 | } |
| 1024 | extraStrings = intent.getStringArrayExtra(Intent.EXTRA_BCC); |
| 1025 | if (extraStrings != null) { |
| 1026 | addBccAddresses(Arrays.asList(extraStrings)); |
| 1027 | } |
| 1028 | |
| 1029 | String extraString = intent.getStringExtra(Intent.EXTRA_SUBJECT); |
| 1030 | if (extraString != null) { |
| 1031 | mSubject.setText(extraString); |
| 1032 | } |
| 1033 | |
| 1034 | for (String extra : ALL_EXTRAS) { |
| 1035 | if (intent.hasExtra(extra)) { |
| 1036 | String value = intent.getStringExtra(extra); |
| 1037 | if (EXTRA_TO.equals(extra)) { |
Mindy Pereira | dbe8996 | 2012-04-13 09:42:38 -0700 | [diff] [blame] | 1038 | addToAddresses(Arrays.asList(TextUtils.split(value, ","))); |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 1039 | } else if (EXTRA_CC.equals(extra)) { |
Mindy Pereira | dbe8996 | 2012-04-13 09:42:38 -0700 | [diff] [blame] | 1040 | addCcAddresses(Arrays.asList(TextUtils.split(value, ",")), null); |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 1041 | } else if (EXTRA_BCC.equals(extra)) { |
Mindy Pereira | dbe8996 | 2012-04-13 09:42:38 -0700 | [diff] [blame] | 1042 | addBccAddresses(Arrays.asList(TextUtils.split(value, ","))); |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 1043 | } else if (EXTRA_SUBJECT.equals(extra)) { |
| 1044 | mSubject.setText(value); |
| 1045 | } else if (EXTRA_BODY.equals(extra)) { |
| 1046 | setBody(value, true /* with signature */); |
| 1047 | } |
| 1048 | } |
| 1049 | } |
| 1050 | |
| 1051 | Bundle extras = intent.getExtras(); |
| 1052 | if (extras != null) { |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 1053 | CharSequence text = extras.getCharSequence(Intent.EXTRA_TEXT); |
| 1054 | if (text != null) { |
| 1055 | setBody(text, true /* with signature */); |
| 1056 | } |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 1057 | } |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 1058 | } |
| 1059 | |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 1060 | @VisibleForTesting |
| 1061 | protected String decodeEmailInUri(String s) throws UnsupportedEncodingException { |
Mindy Pereira | a4069f2 | 2012-05-30 15:31:45 -0700 | [diff] [blame] | 1062 | // TODO: handle the case where there are spaces in the display name as |
| 1063 | // well as the email such as "Guy with spaces <guy+with+spaces@gmail.com>" |
| 1064 | // as they could be encoded ambiguously. |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 1065 | // Since URLDecode.decode changes + into ' ', and + is a valid |
| 1066 | // email character, we need to find/ replace these ourselves before |
| 1067 | // decoding. |
| 1068 | String replacePlus = s.replace("+", "%2B"); |
Mindy Pereira | a4069f2 | 2012-05-30 15:31:45 -0700 | [diff] [blame] | 1069 | try { |
| 1070 | return URLDecoder.decode(replacePlus, UTF8_ENCODING_NAME); |
| 1071 | } catch (IllegalArgumentException e) { |
| 1072 | if (LogUtils.isLoggable(LOG_TAG, LogUtils.VERBOSE)) { |
| 1073 | LogUtils.e(LOG_TAG, "%s while decoding '%s'", e.getMessage(), s); |
| 1074 | } else { |
| 1075 | LogUtils.e(LOG_TAG, e, "Exception while decoding mailto address"); |
| 1076 | } |
| 1077 | return null; |
| 1078 | } |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | /** |
| 1082 | * Initialize the compose view from a String representing a mailTo uri. |
| 1083 | * @param mailToString The uri as a string. |
| 1084 | */ |
| 1085 | public void initFromMailTo(String mailToString) { |
| 1086 | // We need to disguise this string as a URI in order to parse it |
| 1087 | // TODO: Remove this hack when http://b/issue?id=1445295 gets fixed |
| 1088 | Uri uri = Uri.parse("foo://" + mailToString); |
| 1089 | int index = mailToString.indexOf("?"); |
| 1090 | int length = "mailto".length() + 1; |
| 1091 | String to; |
| 1092 | try { |
| 1093 | // Extract the recipient after mailto: |
| 1094 | if (index == -1) { |
| 1095 | to = decodeEmailInUri(mailToString.substring(length)); |
| 1096 | } else { |
| 1097 | to = decodeEmailInUri(mailToString.substring(length, index)); |
| 1098 | } |
Mindy Pereira | a4069f2 | 2012-05-30 15:31:45 -0700 | [diff] [blame] | 1099 | if (!TextUtils.isEmpty(to)) { |
| 1100 | addToAddresses(Arrays.asList(TextUtils.split(to, ","))); |
| 1101 | } |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 1102 | } catch (UnsupportedEncodingException e) { |
| 1103 | if (LogUtils.isLoggable(LOG_TAG, LogUtils.VERBOSE)) { |
| 1104 | LogUtils.e(LOG_TAG, "%s while decoding '%s'", e.getMessage(), mailToString); |
| 1105 | } else { |
| 1106 | LogUtils.e(LOG_TAG, e, "Exception while decoding mailto address"); |
| 1107 | } |
| 1108 | } |
| 1109 | |
| 1110 | List<String> cc = uri.getQueryParameters("cc"); |
| 1111 | addCcAddresses(Arrays.asList(cc.toArray(new String[cc.size()])), null); |
| 1112 | |
| 1113 | List<String> otherTo = uri.getQueryParameters("to"); |
| 1114 | addToAddresses(Arrays.asList(otherTo.toArray(new String[otherTo.size()]))); |
| 1115 | |
| 1116 | List<String> bcc = uri.getQueryParameters("bcc"); |
| 1117 | addBccAddresses(Arrays.asList(bcc.toArray(new String[bcc.size()]))); |
| 1118 | |
| 1119 | List<String> subject = uri.getQueryParameters("subject"); |
| 1120 | if (subject.size() > 0) { |
| 1121 | try { |
| 1122 | mSubject.setText(URLDecoder.decode(subject.get(0), UTF8_ENCODING_NAME)); |
| 1123 | } catch (UnsupportedEncodingException e) { |
| 1124 | LogUtils.e(LOG_TAG, "%s while decoding subject '%s'", |
| 1125 | e.getMessage(), subject); |
| 1126 | } |
| 1127 | } |
| 1128 | |
| 1129 | List<String> body = uri.getQueryParameters("body"); |
| 1130 | if (body.size() > 0) { |
| 1131 | try { |
| 1132 | setBody(URLDecoder.decode(body.get(0), UTF8_ENCODING_NAME), |
| 1133 | true /* with signature */); |
| 1134 | } catch (UnsupportedEncodingException e) { |
| 1135 | LogUtils.e(LOG_TAG, "%s while decoding body '%s'", e.getMessage(), body); |
| 1136 | } |
| 1137 | } |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 1138 | } |
| 1139 | |
Mindy Pereira | bddd6f3 | 2012-06-20 12:10:03 -0700 | [diff] [blame] | 1140 | @VisibleForTesting |
| 1141 | protected void initAttachments(Message refMessage) { |
Mindy Pereira | 3cd4f40 | 2012-07-17 11:16:18 -0700 | [diff] [blame] | 1142 | try { |
| 1143 | mAttachmentsView.addAttachments(mAccount, refMessage); |
| 1144 | } catch (AttachmentFailureException e) { |
| 1145 | LogUtils.e(LOG_TAG, e, "Error adding attachment"); |
| 1146 | showAttachmentTooBigToast(); |
| 1147 | } |
| 1148 | } |
| 1149 | |
| 1150 | /** |
| 1151 | * When an attachment is too large to be added to a message, show a toast. |
| 1152 | * This method also updates the position of the toast so that it is shown |
| 1153 | * clearly above they keyboard if it happens to be open. |
| 1154 | */ |
| 1155 | private void showAttachmentTooBigToast() { |
| 1156 | showErrorToast(R.string.too_large_to_attach); |
| 1157 | } |
| 1158 | |
| 1159 | private void showErrorToast(int resId) { |
| 1160 | Toast t = Toast.makeText(this, resId, Toast.LENGTH_LONG); |
| 1161 | t.setText(resId); |
| 1162 | t.setGravity(Gravity.CENTER_HORIZONTAL, 0, |
| 1163 | getResources().getDimensionPixelSize(R.dimen.attachment_toast_yoffset)); |
| 1164 | t.show(); |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 1165 | } |
| 1166 | |
Paul Westbrook | f97588b | 2012-03-20 11:11:37 -0700 | [diff] [blame] | 1167 | private void initAttachmentsFromIntent(Intent intent) { |
Paul Westbrook | 03ee971 | 2012-04-02 09:51:51 -0700 | [diff] [blame] | 1168 | Bundle extras = intent.getExtras(); |
| 1169 | if (extras == null) { |
| 1170 | extras = Bundle.EMPTY; |
| 1171 | } |
Paul Westbrook | f97588b | 2012-03-20 11:11:37 -0700 | [diff] [blame] | 1172 | final String action = intent.getAction(); |
| 1173 | if (!mAttachmentsChanged) { |
| 1174 | long totalSize = 0; |
| 1175 | if (extras.containsKey(EXTRA_ATTACHMENTS)) { |
| 1176 | String[] uris = (String[]) extras.getSerializable(EXTRA_ATTACHMENTS); |
| 1177 | for (String uriString : uris) { |
| 1178 | final Uri uri = Uri.parse(uriString); |
| 1179 | long size = 0; |
| 1180 | try { |
Andy Huang | 5c5fd57 | 2012-04-08 18:19:29 -0700 | [diff] [blame] | 1181 | size = mAttachmentsView.addAttachment(mAccount, uri); |
Paul Westbrook | f97588b | 2012-03-20 11:11:37 -0700 | [diff] [blame] | 1182 | } catch (AttachmentFailureException e) { |
Paul Westbrook | f97588b | 2012-03-20 11:11:37 -0700 | [diff] [blame] | 1183 | LogUtils.e(LOG_TAG, e, "Error adding attachment"); |
Mindy Pereira | 3cd4f40 | 2012-07-17 11:16:18 -0700 | [diff] [blame] | 1184 | showAttachmentTooBigToast(); |
Paul Westbrook | f97588b | 2012-03-20 11:11:37 -0700 | [diff] [blame] | 1185 | } |
| 1186 | totalSize += size; |
| 1187 | } |
| 1188 | } |
| 1189 | if (Intent.ACTION_SEND.equals(action) && extras.containsKey(Intent.EXTRA_STREAM)) { |
| 1190 | final Uri uri = (Uri) extras.getParcelable(Intent.EXTRA_STREAM); |
| 1191 | long size = 0; |
| 1192 | try { |
Andy Huang | 5c5fd57 | 2012-04-08 18:19:29 -0700 | [diff] [blame] | 1193 | size = mAttachmentsView.addAttachment(mAccount, uri); |
Paul Westbrook | f97588b | 2012-03-20 11:11:37 -0700 | [diff] [blame] | 1194 | } catch (AttachmentFailureException e) { |
Paul Westbrook | f97588b | 2012-03-20 11:11:37 -0700 | [diff] [blame] | 1195 | LogUtils.e(LOG_TAG, e, "Error adding attachment"); |
Mindy Pereira | 3cd4f40 | 2012-07-17 11:16:18 -0700 | [diff] [blame] | 1196 | showAttachmentTooBigToast(); |
Paul Westbrook | f97588b | 2012-03-20 11:11:37 -0700 | [diff] [blame] | 1197 | } |
| 1198 | totalSize += size; |
| 1199 | } |
| 1200 | |
| 1201 | if (Intent.ACTION_SEND_MULTIPLE.equals(action) |
| 1202 | && extras.containsKey(Intent.EXTRA_STREAM)) { |
| 1203 | ArrayList<Parcelable> uris = extras.getParcelableArrayList(Intent.EXTRA_STREAM); |
| 1204 | for (Parcelable uri : uris) { |
| 1205 | long size = 0; |
| 1206 | try { |
Andy Huang | 5c5fd57 | 2012-04-08 18:19:29 -0700 | [diff] [blame] | 1207 | size = mAttachmentsView.addAttachment(mAccount, (Uri) uri); |
Paul Westbrook | f97588b | 2012-03-20 11:11:37 -0700 | [diff] [blame] | 1208 | } catch (AttachmentFailureException e) { |
Paul Westbrook | f97588b | 2012-03-20 11:11:37 -0700 | [diff] [blame] | 1209 | LogUtils.e(LOG_TAG, e, "Error adding attachment"); |
Mindy Pereira | 3cd4f40 | 2012-07-17 11:16:18 -0700 | [diff] [blame] | 1210 | showAttachmentTooBigToast(); |
Paul Westbrook | f97588b | 2012-03-20 11:11:37 -0700 | [diff] [blame] | 1211 | } |
| 1212 | totalSize += size; |
| 1213 | } |
| 1214 | } |
| 1215 | |
| 1216 | if (totalSize > 0) { |
| 1217 | mAttachmentsChanged = true; |
| 1218 | updateSaveUi(); |
| 1219 | } |
| 1220 | } |
| 1221 | } |
| 1222 | |
| 1223 | |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 1224 | private void initQuotedTextFromRefMessage(Message refMessage, int action) { |
| 1225 | if (mRefMessage != null && (action == REPLY || action == REPLY_ALL || action == FORWARD)) { |
Mindy Pereira | 9932dee | 2012-01-10 16:09:50 -0800 | [diff] [blame] | 1226 | mQuotedTextView.setQuotedText(action, refMessage, action != FORWARD); |
| 1227 | } |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 1228 | } |
| 1229 | |
| 1230 | private void updateHideOrShowCcBcc() { |
Mindy Pereira | ec8b0ed | 2012-01-06 10:35:59 -0800 | [diff] [blame] | 1231 | // Its possible there is a menu item OR a button. |
Mindy Pereira | 326689d | 2012-05-17 10:14:14 -0700 | [diff] [blame] | 1232 | boolean ccVisible = mCcBccView.isCcVisible(); |
| 1233 | boolean bccVisible = mCcBccView.isBccVisible(); |
Mindy Pereira | ec8b0ed | 2012-01-06 10:35:59 -0800 | [diff] [blame] | 1234 | if (mCcBccButton != null) { |
Mindy Pereira | 326689d | 2012-05-17 10:14:14 -0700 | [diff] [blame] | 1235 | if (!ccVisible || !bccVisible) { |
Mindy Pereira | ec8b0ed | 2012-01-06 10:35:59 -0800 | [diff] [blame] | 1236 | mCcBccButton.setVisibility(View.VISIBLE); |
Mindy Pereira | 326689d | 2012-05-17 10:14:14 -0700 | [diff] [blame] | 1237 | mCcBccButton.setText(getString(!ccVisible ? R.string.add_cc_label |
Mindy Pereira | ec8b0ed | 2012-01-06 10:35:59 -0800 | [diff] [blame] | 1238 | : R.string.add_bcc_label)); |
| 1239 | } else { |
| 1240 | mCcBccButton.setVisibility(View.GONE); |
| 1241 | } |
| 1242 | } |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 1243 | } |
| 1244 | |
Mindy Pereira | a34c9a0 | 2012-04-17 14:10:53 -0700 | [diff] [blame] | 1245 | private void showCcBcc(Bundle state) { |
Mindy Pereira | 326689d | 2012-05-17 10:14:14 -0700 | [diff] [blame] | 1246 | if (state != null && state.containsKey(EXTRA_SHOW_CC)) { |
| 1247 | boolean showCc = state.getBoolean(EXTRA_SHOW_CC); |
| 1248 | boolean showBcc = state.getBoolean(EXTRA_SHOW_BCC); |
| 1249 | if (showCc || showBcc) { |
| 1250 | mCcBccView.show(false, showCc, showBcc); |
Mindy Pereira | 6faeedf | 2012-04-18 16:11:39 -0700 | [diff] [blame] | 1251 | } |
Mindy Pereira | a34c9a0 | 2012-04-17 14:10:53 -0700 | [diff] [blame] | 1252 | } |
| 1253 | } |
| 1254 | |
Mindy Pereira | 013194c | 2012-01-06 15:09:33 -0800 | [diff] [blame] | 1255 | /** |
| 1256 | * Add attachment and update the compose area appropriately. |
| 1257 | * @param data |
| 1258 | */ |
| 1259 | public void addAttachmentAndUpdateView(Intent data) { |
Mindy Pereira | 2421dc8 | 2012-03-27 13:32:31 -0700 | [diff] [blame] | 1260 | addAttachmentAndUpdateView(data != null ? data.getData() : (Uri) null); |
| 1261 | } |
| 1262 | |
Andy Huang | 5c5fd57 | 2012-04-08 18:19:29 -0700 | [diff] [blame] | 1263 | public void addAttachmentAndUpdateView(Uri contentUri) { |
| 1264 | if (contentUri == null) { |
Mindy Pereira | 2421dc8 | 2012-03-27 13:32:31 -0700 | [diff] [blame] | 1265 | return; |
| 1266 | } |
Mindy Pereira | 013194c | 2012-01-06 15:09:33 -0800 | [diff] [blame] | 1267 | try { |
Andy Huang | 5c5fd57 | 2012-04-08 18:19:29 -0700 | [diff] [blame] | 1268 | addAttachmentAndUpdateView(mAttachmentsView.generateLocalAttachment(contentUri)); |
| 1269 | } catch (AttachmentFailureException e) { |
Andy Huang | 5c5fd57 | 2012-04-08 18:19:29 -0700 | [diff] [blame] | 1270 | LogUtils.e(LOG_TAG, e, "Error adding attachment"); |
Mindy Pereira | 3cd4f40 | 2012-07-17 11:16:18 -0700 | [diff] [blame] | 1271 | showErrorToast(R.string.generic_attachment_problem); |
Andy Huang | 5c5fd57 | 2012-04-08 18:19:29 -0700 | [diff] [blame] | 1272 | } |
| 1273 | } |
| 1274 | |
| 1275 | public void addAttachmentAndUpdateView(Attachment attachment) { |
| 1276 | try { |
| 1277 | long size = mAttachmentsView.addAttachment(mAccount, attachment); |
Mindy Pereira | 9932dee | 2012-01-10 16:09:50 -0800 | [diff] [blame] | 1278 | if (size > 0) { |
| 1279 | mAttachmentsChanged = true; |
| 1280 | updateSaveUi(); |
Mindy Pereira | 013194c | 2012-01-06 15:09:33 -0800 | [diff] [blame] | 1281 | } |
Mindy Pereira | 9932dee | 2012-01-10 16:09:50 -0800 | [diff] [blame] | 1282 | } catch (AttachmentFailureException e) { |
Mindy Pereira | 9932dee | 2012-01-10 16:09:50 -0800 | [diff] [blame] | 1283 | LogUtils.e(LOG_TAG, e, "Error adding attachment"); |
Mindy Pereira | 3cd4f40 | 2012-07-17 11:16:18 -0700 | [diff] [blame] | 1284 | showAttachmentTooBigToast(); |
Mindy Pereira | 013194c | 2012-01-06 15:09:33 -0800 | [diff] [blame] | 1285 | } |
| 1286 | } |
| 1287 | |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 1288 | void initRecipientsFromRefMessage(String recipientAddress, Message refMessage, |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 1289 | int action) { |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 1290 | // Don't populate the address if this is a forward. |
| 1291 | if (action == ComposeActivity.FORWARD) { |
| 1292 | return; |
| 1293 | } |
Mindy Pereira | 33fe908 | 2012-01-09 16:24:30 -0800 | [diff] [blame] | 1294 | initReplyRecipients(mAccount.name, refMessage, action); |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 1295 | } |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 1296 | |
Mindy Pereira | 818143e | 2012-01-11 13:59:49 -0800 | [diff] [blame] | 1297 | @VisibleForTesting |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 1298 | void initReplyRecipients(String account, Message refMessage, int action) { |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 1299 | // This is the email address of the current user, i.e. the one composing |
| 1300 | // the reply. |
Mindy Pereira | 4a20b70 | 2012-01-05 16:24:24 -0800 | [diff] [blame] | 1301 | final String accountEmail = Address.getEmailAddress(account).getAddress(); |
Mindy Pereira | 1469b4e | 2012-06-19 19:18:54 -0700 | [diff] [blame] | 1302 | String fromAddress = getAddress(refMessage.from); |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 1303 | String[] sentToAddresses = Utils.splitCommaSeparatedString(refMessage.to); |
| 1304 | String replytoAddress = refMessage.replyTo; |
Mindy Pereira | a26b54e | 2012-01-06 12:54:33 -0800 | [diff] [blame] | 1305 | final Collection<String> toAddresses; |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 1306 | |
| 1307 | // If this is a reply, the Cc list is empty. If this is a reply-all, the |
| 1308 | // Cc list is the union of the To and Cc recipients of the original |
| 1309 | // message, excluding the current user's email address and any addresses |
Mindy Pereira | a26b54e | 2012-01-06 12:54:33 -0800 | [diff] [blame] | 1310 | // already on the To list. |
| 1311 | if (action == ComposeActivity.REPLY) { |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 1312 | toAddresses = initToRecipients(account, accountEmail, fromAddress, replytoAddress, |
Mindy Pereira | 1469b4e | 2012-06-19 19:18:54 -0700 | [diff] [blame] | 1313 | sentToAddresses); |
Mindy Pereira | a26b54e | 2012-01-06 12:54:33 -0800 | [diff] [blame] | 1314 | addToAddresses(toAddresses); |
| 1315 | } else if (action == ComposeActivity.REPLY_ALL) { |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 1316 | final Set<String> ccAddresses = Sets.newHashSet(); |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 1317 | toAddresses = initToRecipients(account, accountEmail, fromAddress, replytoAddress, |
Mindy Pereira | 9b18a9a | 2012-07-25 18:13:57 -0700 | [diff] [blame] | 1318 | sentToAddresses); |
Mindy Pereira | 154386a | 2012-01-11 13:02:33 -0800 | [diff] [blame] | 1319 | addToAddresses(toAddresses); |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 1320 | addRecipients(accountEmail, ccAddresses, sentToAddresses); |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 1321 | addRecipients(accountEmail, ccAddresses, |
| 1322 | Utils.splitCommaSeparatedString(refMessage.cc)); |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 1323 | addCcAddresses(ccAddresses, toAddresses); |
| 1324 | } |
| 1325 | } |
| 1326 | |
Mindy Pereira | 1469b4e | 2012-06-19 19:18:54 -0700 | [diff] [blame] | 1327 | private String getAddress(String toParse) { |
| 1328 | if (!TextUtils.isEmpty(toParse)) { |
| 1329 | Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(toParse); |
| 1330 | if (tokens.length > 0) { |
| 1331 | return tokens[0].getAddress(); |
| 1332 | } |
| 1333 | } |
| 1334 | return ""; |
| 1335 | } |
| 1336 | |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 1337 | private void addToAddresses(Collection<String> addresses) { |
| 1338 | addAddressesToList(addresses, mTo); |
| 1339 | } |
| 1340 | |
| 1341 | private void addCcAddresses(Collection<String> addresses, Collection<String> toAddresses) { |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 1342 | addCcAddressesToList(tokenizeAddressList(addresses), |
| 1343 | toAddresses != null ? tokenizeAddressList(toAddresses) : null, mCc); |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 1344 | } |
| 1345 | |
Paul Westbrook | bb87b7f | 2012-03-20 16:20:07 -0700 | [diff] [blame] | 1346 | private void addBccAddresses(Collection<String> addresses) { |
| 1347 | addAddressesToList(addresses, mBcc); |
| 1348 | } |
| 1349 | |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 1350 | @VisibleForTesting |
| 1351 | protected void addCcAddressesToList(List<Rfc822Token[]> addresses, |
| 1352 | List<Rfc822Token[]> compareToList, RecipientEditTextView list) { |
| 1353 | String address; |
| 1354 | |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 1355 | if (compareToList == null) { |
| 1356 | for (Rfc822Token[] tokens : addresses) { |
| 1357 | for (int i = 0; i < tokens.length; i++) { |
| 1358 | address = tokens[i].toString(); |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 1359 | list.append(address + END_TOKEN); |
| 1360 | } |
| 1361 | } |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 1362 | } else { |
| 1363 | HashSet<String> compareTo = convertToHashSet(compareToList); |
| 1364 | for (Rfc822Token[] tokens : addresses) { |
| 1365 | for (int i = 0; i < tokens.length; i++) { |
| 1366 | address = tokens[i].toString(); |
| 1367 | // Check if this is a duplicate: |
| 1368 | if (!compareTo.contains(tokens[i].getAddress())) { |
| 1369 | // Get the address here |
| 1370 | list.append(address + END_TOKEN); |
| 1371 | } |
| 1372 | } |
| 1373 | } |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 1374 | } |
| 1375 | } |
| 1376 | |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 1377 | private HashSet<String> convertToHashSet(List<Rfc822Token[]> list) { |
| 1378 | HashSet<String> hash = new HashSet<String>(); |
| 1379 | for (Rfc822Token[] tokens : list) { |
| 1380 | for (int i = 0; i < tokens.length; i++) { |
| 1381 | hash.add(tokens[i].getAddress()); |
| 1382 | } |
| 1383 | } |
| 1384 | return hash; |
| 1385 | } |
| 1386 | |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 1387 | protected List<Rfc822Token[]> tokenizeAddressList(Collection<String> addresses) { |
| 1388 | @VisibleForTesting |
| 1389 | List<Rfc822Token[]> tokenized = new ArrayList<Rfc822Token[]>(); |
| 1390 | |
| 1391 | for (String address: addresses) { |
| 1392 | tokenized.add(Rfc822Tokenizer.tokenize(address)); |
| 1393 | } |
| 1394 | return tokenized; |
| 1395 | } |
| 1396 | |
| 1397 | @VisibleForTesting |
| 1398 | void addAddressesToList(Collection<String> addresses, RecipientEditTextView list) { |
| 1399 | for (String address : addresses) { |
| 1400 | addAddressToList(address, list); |
| 1401 | } |
| 1402 | } |
| 1403 | |
| 1404 | private void addAddressToList(String address, RecipientEditTextView list) { |
| 1405 | if (address == null || list == null) |
| 1406 | return; |
| 1407 | |
| 1408 | Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(address); |
| 1409 | |
| 1410 | for (int i = 0; i < tokens.length; i++) { |
| 1411 | list.append(tokens[i] + END_TOKEN); |
| 1412 | } |
| 1413 | } |
| 1414 | |
| 1415 | @VisibleForTesting |
| 1416 | protected Collection<String> initToRecipients(String account, String accountEmail, |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 1417 | String senderAddress, String replyToAddress, String[] inToAddresses) { |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 1418 | // The To recipient is the reply-to address specified in the original |
| 1419 | // message, unless it is: |
| 1420 | // the current user OR a custom from of the current user, in which case |
| 1421 | // it's the To recipient list of the original message. |
| 1422 | // OR missing, in which case use the sender of the original message |
| 1423 | Set<String> toAddresses = Sets.newHashSet(); |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 1424 | if (!TextUtils.isEmpty(replyToAddress)) { |
| 1425 | toAddresses.add(replyToAddress); |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 1426 | } else { |
Mindy Pereira | cecc54a | 2012-07-31 09:38:11 -0700 | [diff] [blame] | 1427 | if (!recipientMatchesThisAccount(account, senderAddress)) { |
Mindy Pereira | 1469b4e | 2012-06-19 19:18:54 -0700 | [diff] [blame] | 1428 | toAddresses.add(senderAddress); |
| 1429 | } else { |
| 1430 | // This happens if the user replies to a message they originally |
Mindy Pereira | 1883b34 | 2012-06-20 08:34:56 -0700 | [diff] [blame] | 1431 | // wrote. In this case, "reply" really means "re-send," so we |
| 1432 | // target the original recipients. This works as expected even |
| 1433 | // if the user sent the original message to themselves. |
Mindy Pereira | 1469b4e | 2012-06-19 19:18:54 -0700 | [diff] [blame] | 1434 | toAddresses.addAll(Arrays.asList(inToAddresses)); |
| 1435 | } |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 1436 | } |
| 1437 | return toAddresses; |
| 1438 | } |
| 1439 | |
Mindy Pereira | cecc54a | 2012-07-31 09:38:11 -0700 | [diff] [blame] | 1440 | private void addRecipients(String accountAddress, Set<String> recipients, String[] addresses) { |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 1441 | for (String email : addresses) { |
Mindy Pereira | cecc54a | 2012-07-31 09:38:11 -0700 | [diff] [blame] | 1442 | // Do not add this account, or any of its custom from addresses, to |
| 1443 | // the list of recipients. |
Mindy Pereira | 4a20b70 | 2012-01-05 16:24:24 -0800 | [diff] [blame] | 1444 | final String recipientAddress = Address.getEmailAddress(email).getAddress(); |
Mindy Pereira | cecc54a | 2012-07-31 09:38:11 -0700 | [diff] [blame] | 1445 | if (!recipientMatchesThisAccount(accountAddress, recipientAddress)) { |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 1446 | recipients.add(email.replace("\"\"", "")); |
| 1447 | } |
| 1448 | } |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 1449 | } |
| 1450 | |
Mindy Pereira | cecc54a | 2012-07-31 09:38:11 -0700 | [diff] [blame] | 1451 | /** |
| 1452 | * A recipient matches this account if it has the same address as the |
| 1453 | * currently selected account OR one of the custom from addresses associated |
| 1454 | * with the currently selected account. |
| 1455 | * @param accountAddress currently selected account |
| 1456 | * @param recipientAddress address we are comparing with the currently selected account |
| 1457 | * @return |
| 1458 | */ |
| 1459 | protected boolean recipientMatchesThisAccount(String accountAddress, String recipientAddress) { |
| 1460 | return accountAddress.equalsIgnoreCase(recipientAddress) |
| 1461 | || ReplyFromAccount.isCustomFrom(recipientAddress, |
| 1462 | mFromSpinner.getReplyFromAccounts()); |
| 1463 | } |
| 1464 | |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 1465 | private void setSubject(Message refMessage, int action) { |
| 1466 | String subject = refMessage.subject; |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 1467 | String prefix; |
| 1468 | String correctedSubject = null; |
| 1469 | if (action == ComposeActivity.COMPOSE) { |
| 1470 | prefix = ""; |
| 1471 | } else if (action == ComposeActivity.FORWARD) { |
| 1472 | prefix = getString(R.string.forward_subject_label); |
| 1473 | } else { |
| 1474 | prefix = getString(R.string.reply_subject_label); |
| 1475 | } |
| 1476 | |
| 1477 | // Don't duplicate the prefix |
Mindy Pereira | c7a3699 | 2012-07-30 14:00:37 -0700 | [diff] [blame] | 1478 | if (!TextUtils.isEmpty(subject) |
| 1479 | && subject.toLowerCase().startsWith(prefix.toLowerCase())) { |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 1480 | correctedSubject = subject; |
| 1481 | } else { |
| 1482 | correctedSubject = String |
| 1483 | .format(getString(R.string.formatted_subject), prefix, subject); |
| 1484 | } |
| 1485 | mSubject.setText(correctedSubject); |
| 1486 | } |
| 1487 | |
Mindy Pereira | 818143e | 2012-01-11 13:59:49 -0800 | [diff] [blame] | 1488 | private void initRecipients() { |
| 1489 | setupRecipients(mTo); |
| 1490 | setupRecipients(mCc); |
| 1491 | setupRecipients(mBcc); |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 1492 | } |
| 1493 | |
Mindy Pereira | 818143e | 2012-01-11 13:59:49 -0800 | [diff] [blame] | 1494 | private void setupRecipients(RecipientEditTextView view) { |
Paul Westbrook | 679a8cc | 2012-02-21 16:37:58 -0800 | [diff] [blame] | 1495 | view.setAdapter(new RecipientAdapter(this, mAccount)); |
Mindy Pereira | c17d073 | 2011-12-29 10:46:19 -0800 | [diff] [blame] | 1496 | view.setTokenizer(new Rfc822Tokenizer()); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1497 | if (mValidator == null) { |
Paul Westbrook | 679a8cc | 2012-02-21 16:37:58 -0800 | [diff] [blame] | 1498 | final String accountName = mAccount.name; |
Mindy Pereira | 33fe908 | 2012-01-09 16:24:30 -0800 | [diff] [blame] | 1499 | int offset = accountName.indexOf("@") + 1; |
| 1500 | String account = accountName; |
Mindy Pereira | c17d073 | 2011-12-29 10:46:19 -0800 | [diff] [blame] | 1501 | if (offset > -1) { |
Mindy Pereira | 33fe908 | 2012-01-09 16:24:30 -0800 | [diff] [blame] | 1502 | account = account.substring(accountName.indexOf("@") + 1); |
Mindy Pereira | c17d073 | 2011-12-29 10:46:19 -0800 | [diff] [blame] | 1503 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1504 | mValidator = new Rfc822Validator(account); |
Mindy Pereira | c17d073 | 2011-12-29 10:46:19 -0800 | [diff] [blame] | 1505 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1506 | view.setValidator(mValidator); |
Mindy Pereira | 8e9305e | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 1507 | } |
| 1508 | |
| 1509 | @Override |
| 1510 | public void onClick(View v) { |
| 1511 | int id = v.getId(); |
| 1512 | switch (id) { |
Mindy Pereira | a26b54e | 2012-01-06 12:54:33 -0800 | [diff] [blame] | 1513 | case R.id.add_cc_bcc: |
Mindy Pereira | 8e9305e | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 1514 | // Verify that cc/ bcc aren't showing. |
| 1515 | // Animate in cc/bcc. |
Mindy Pereira | a26b54e | 2012-01-06 12:54:33 -0800 | [diff] [blame] | 1516 | showCcBccViews(); |
Mindy Pereira | 8e9305e | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 1517 | break; |
Mindy Pereira | 1f93668 | 2012-03-02 11:30:33 -0800 | [diff] [blame] | 1518 | case R.id.add_attachment: |
Andrew Sapperstein | 8f1c01e | 2012-06-18 18:15:30 -0700 | [diff] [blame] | 1519 | openAttachmentTypeSelectionDialog(); |
Mindy Pereira | 1f93668 | 2012-03-02 11:30:33 -0800 | [diff] [blame] | 1520 | break; |
Mindy Pereira | 8e9305e | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 1521 | } |
| 1522 | } |
Mindy Pereira | b47f3e2 | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 1523 | |
| 1524 | @Override |
| 1525 | public boolean onCreateOptionsMenu(Menu menu) { |
| 1526 | super.onCreateOptionsMenu(menu); |
| 1527 | MenuInflater inflater = getMenuInflater(); |
| 1528 | inflater.inflate(R.menu.compose_menu, menu); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1529 | mSave = menu.findItem(R.id.save); |
| 1530 | mSend = menu.findItem(R.id.send); |
Mindy Pereira | 3ca5bad | 2012-04-16 11:02:42 -0700 | [diff] [blame] | 1531 | MenuItem helpItem = menu.findItem(R.id.help_info_menu_item); |
| 1532 | MenuItem sendFeedbackItem = menu.findItem(R.id.feedback_menu_item); |
| 1533 | if (helpItem != null) { |
| 1534 | helpItem.setVisible(mAccount != null |
| 1535 | && mAccount.supportsCapability(AccountCapabilities.HELP_CONTENT)); |
| 1536 | } |
| 1537 | if (sendFeedbackItem != null) { |
| 1538 | sendFeedbackItem.setVisible(mAccount != null |
| 1539 | && mAccount.supportsCapability(AccountCapabilities.SEND_FEEDBACK)); |
| 1540 | } |
Mindy Pereira | b47f3e2 | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 1541 | return true; |
| 1542 | } |
| 1543 | |
| 1544 | @Override |
Mindy Pereira | ec8b0ed | 2012-01-06 10:35:59 -0800 | [diff] [blame] | 1545 | public boolean onPrepareOptionsMenu(Menu menu) { |
| 1546 | MenuItem ccBcc = menu.findItem(R.id.add_cc_bcc); |
Mindy Pereira | 818143e | 2012-01-11 13:59:49 -0800 | [diff] [blame] | 1547 | if (ccBcc != null && mCc != null) { |
Mindy Pereira | ec8b0ed | 2012-01-06 10:35:59 -0800 | [diff] [blame] | 1548 | // Its possible there is a menu item OR a button. |
| 1549 | boolean ccFieldVisible = mCc.isShown(); |
| 1550 | boolean bccFieldVisible = mBcc.isShown(); |
| 1551 | if (!ccFieldVisible || !bccFieldVisible) { |
| 1552 | ccBcc.setVisible(true); |
| 1553 | ccBcc.setTitle(getString(!ccFieldVisible ? R.string.add_cc_label |
| 1554 | : R.string.add_bcc_label)); |
| 1555 | } else { |
| 1556 | ccBcc.setVisible(false); |
| 1557 | } |
| 1558 | } |
Mindy Pereira | 75f6663 | 2012-01-11 11:42:02 -0800 | [diff] [blame] | 1559 | if (mSave != null) { |
| 1560 | mSave.setEnabled(shouldSave()); |
| 1561 | } |
Mindy Pereira | ec8b0ed | 2012-01-06 10:35:59 -0800 | [diff] [blame] | 1562 | return true; |
| 1563 | } |
| 1564 | |
| 1565 | @Override |
Mindy Pereira | b47f3e2 | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 1566 | public boolean onOptionsItemSelected(MenuItem item) { |
| 1567 | int id = item.getItemId(); |
Mindy Pereira | 75f6663 | 2012-01-11 11:42:02 -0800 | [diff] [blame] | 1568 | boolean handled = true; |
Mindy Pereira | b47f3e2 | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 1569 | switch (id) { |
Mindy Pereira | 7b56a61 | 2011-12-14 12:32:28 -0800 | [diff] [blame] | 1570 | case R.id.add_attachment: |
Andrew Sapperstein | 8f1c01e | 2012-06-18 18:15:30 -0700 | [diff] [blame] | 1571 | openAttachmentTypeSelectionDialog(); |
Mindy Pereira | 7b56a61 | 2011-12-14 12:32:28 -0800 | [diff] [blame] | 1572 | break; |
Mindy Pereira | ec8b0ed | 2012-01-06 10:35:59 -0800 | [diff] [blame] | 1573 | case R.id.add_cc_bcc: |
| 1574 | showCcBccViews(); |
Mindy Pereira | b47f3e2 | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 1575 | break; |
Mindy Pereira | 33fe908 | 2012-01-09 16:24:30 -0800 | [diff] [blame] | 1576 | case R.id.save: |
Mindy Pereira | 48e31b0 | 2012-05-30 13:12:24 -0700 | [diff] [blame] | 1577 | doSave(true); |
Mindy Pereira | 33fe908 | 2012-01-09 16:24:30 -0800 | [diff] [blame] | 1578 | break; |
| 1579 | case R.id.send: |
| 1580 | doSend(); |
Mindy Pereira | 75f6663 | 2012-01-11 11:42:02 -0800 | [diff] [blame] | 1581 | break; |
Mindy Pereira | efe3d25 | 2012-03-01 14:20:44 -0800 | [diff] [blame] | 1582 | case R.id.discard: |
| 1583 | doDiscard(); |
| 1584 | break; |
Mindy Pereira | 1f93668 | 2012-03-02 11:30:33 -0800 | [diff] [blame] | 1585 | case R.id.settings: |
| 1586 | Utils.showSettings(this, mAccount); |
| 1587 | break; |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 1588 | case android.R.id.home: |
Paul Westbrook | daecb4b | 2012-05-31 10:21:26 -0700 | [diff] [blame] | 1589 | onAppUpPressed(); |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 1590 | break; |
| 1591 | case R.id.help_info_menu_item: |
| 1592 | // TODO: enable context sensitive help |
Paul Westbrook | 498e76d | 2012-04-12 16:33:02 -0700 | [diff] [blame] | 1593 | Utils.showHelp(this, mAccount, null); |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 1594 | break; |
| 1595 | case R.id.feedback_menu_item: |
| 1596 | Utils.sendFeedback(this, mAccount); |
| 1597 | break; |
Mindy Pereira | 75f6663 | 2012-01-11 11:42:02 -0800 | [diff] [blame] | 1598 | default: |
| 1599 | handled = false; |
Mindy Pereira | 33fe908 | 2012-01-09 16:24:30 -0800 | [diff] [blame] | 1600 | break; |
Mindy Pereira | b47f3e2 | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 1601 | } |
| 1602 | return !handled ? super.onOptionsItemSelected(item) : handled; |
| 1603 | } |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 1604 | |
Paul Westbrook | daecb4b | 2012-05-31 10:21:26 -0700 | [diff] [blame] | 1605 | private void onAppUpPressed() { |
| 1606 | if (mLaunchedFromEmail) { |
| 1607 | // If this was started from Gmail, simply treat app up as the system back button, so |
| 1608 | // that the last view is restored. |
| 1609 | onBackPressed(); |
| 1610 | return; |
| 1611 | } |
| 1612 | |
| 1613 | // Fire the main activity to ensure it launches the "top" screen of mail. |
| 1614 | // Since the main Activity is singleTask, it should revive that task if it was already |
| 1615 | // started. |
Mindy Pereira | 5ad0291 | 2012-07-09 09:57:18 -0700 | [diff] [blame] | 1616 | Folder defaultInbox = new Folder(); |
| 1617 | defaultInbox.uri = mAccount.settings.defaultInbox; |
Paul Westbrook | daecb4b | 2012-05-31 10:21:26 -0700 | [diff] [blame] | 1618 | final Intent mailIntent = |
Mindy Pereira | 96a7f7a | 2012-07-09 16:51:06 -0700 | [diff] [blame] | 1619 | Utils.createViewFolderIntent(defaultInbox, mAccount); |
Paul Westbrook | daecb4b | 2012-05-31 10:21:26 -0700 | [diff] [blame] | 1620 | |
| 1621 | mailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | |
| 1622 | Intent.FLAG_ACTIVITY_TASK_ON_HOME); |
| 1623 | startActivity(mailIntent); |
| 1624 | finish(); |
| 1625 | } |
| 1626 | |
Mindy Pereira | 33fe908 | 2012-01-09 16:24:30 -0800 | [diff] [blame] | 1627 | private void doSend() { |
Mindy Pereira | 8aa913b | 2012-07-17 10:58:39 -0700 | [diff] [blame] | 1628 | clearImeText(); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1629 | sendOrSaveWithSanityChecks(false, true, false); |
Mindy Pereira | 33fe908 | 2012-01-09 16:24:30 -0800 | [diff] [blame] | 1630 | } |
| 1631 | |
Mindy Pereira | 48e31b0 | 2012-05-30 13:12:24 -0700 | [diff] [blame] | 1632 | private void doSave(boolean showToast) { |
| 1633 | // Clear the IME composing suggestions from the body and subject before saving. |
Mindy Pereira | 8aa913b | 2012-07-17 10:58:39 -0700 | [diff] [blame] | 1634 | clearImeText(); |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 1635 | sendOrSaveWithSanityChecks(true, showToast, false); |
Mindy Pereira | 48e31b0 | 2012-05-30 13:12:24 -0700 | [diff] [blame] | 1636 | } |
| 1637 | |
Mindy Pereira | 8aa913b | 2012-07-17 10:58:39 -0700 | [diff] [blame] | 1638 | private void clearImeText() { |
| 1639 | mBodyView.clearComposingText(); |
| 1640 | BaseInputConnection.removeComposingSpans(mBodyView.getEditableText()); |
| 1641 | mSubject.clearComposingText(); |
| 1642 | BaseInputConnection.removeComposingSpans(mSubject.getEditableText()); |
Mindy Pereira | 33fe908 | 2012-01-09 16:24:30 -0800 | [diff] [blame] | 1643 | } |
| 1644 | |
Mindy Pereira | e011b1d | 2012-06-18 13:45:26 -0700 | [diff] [blame] | 1645 | @VisibleForTesting |
| 1646 | public interface SendOrSaveCallback { |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1647 | public void initializeSendOrSave(SendOrSaveTask sendOrSaveTask); |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 1648 | public void notifyMessageIdAllocated(SendOrSaveMessage sendOrSaveMessage, Message message); |
| 1649 | public Message getMessage(); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1650 | public void sendOrSaveFinished(SendOrSaveTask sendOrSaveTask, boolean success); |
| 1651 | } |
| 1652 | |
Mindy Pereira | e011b1d | 2012-06-18 13:45:26 -0700 | [diff] [blame] | 1653 | @VisibleForTesting |
| 1654 | public static class SendOrSaveTask implements Runnable { |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1655 | private final Context mContext; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1656 | @VisibleForTesting |
Mindy Pereira | e011b1d | 2012-06-18 13:45:26 -0700 | [diff] [blame] | 1657 | public final SendOrSaveCallback mSendOrSaveCallback; |
| 1658 | @VisibleForTesting |
| 1659 | public final SendOrSaveMessage mSendOrSaveMessage; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1660 | |
| 1661 | public SendOrSaveTask(Context context, SendOrSaveMessage message, |
| 1662 | SendOrSaveCallback callback) { |
| 1663 | mContext = context; |
| 1664 | mSendOrSaveCallback = callback; |
| 1665 | mSendOrSaveMessage = message; |
| 1666 | } |
| 1667 | |
| 1668 | @Override |
| 1669 | public void run() { |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 1670 | final SendOrSaveMessage sendOrSaveMessage = mSendOrSaveMessage; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1671 | |
Mindy Pereira | 92551d0 | 2012-04-05 11:31:12 -0700 | [diff] [blame] | 1672 | final ReplyFromAccount selectedAccount = sendOrSaveMessage.mAccount; |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 1673 | Message message = mSendOrSaveCallback.getMessage(); |
| 1674 | long messageId = message != null ? message.id : UIProvider.INVALID_MESSAGE_ID; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1675 | // If a previous draft has been saved, in an account that is different |
| 1676 | // than what the user wants to send from, remove the old draft, and treat this |
| 1677 | // as a new message |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 1678 | if (!selectedAccount.equals(sendOrSaveMessage.mAccount)) { |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1679 | if (messageId != UIProvider.INVALID_MESSAGE_ID) { |
| 1680 | ContentResolver resolver = mContext.getContentResolver(); |
| 1681 | ContentValues values = new ContentValues(); |
| 1682 | values.put(BaseColumns._ID, messageId); |
Mindy Pereira | 92551d0 | 2012-04-05 11:31:12 -0700 | [diff] [blame] | 1683 | if (selectedAccount.account.expungeMessageUri != null) { |
| 1684 | resolver.update(selectedAccount.account.expungeMessageUri, values, null, |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 1685 | null); |
Mindy Pereira | cfb7f33 | 2012-02-28 10:23:43 -0800 | [diff] [blame] | 1686 | } else { |
| 1687 | // TODO(mindyp) delete the conversation. |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 1688 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1689 | // reset messageId to 0, so a new message will be created |
| 1690 | messageId = UIProvider.INVALID_MESSAGE_ID; |
| 1691 | } |
| 1692 | } |
| 1693 | |
| 1694 | final long messageIdToSave = messageId; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1695 | if (messageIdToSave != UIProvider.INVALID_MESSAGE_ID) { |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 1696 | sendOrSaveMessage.mValues.put(BaseColumns._ID, messageIdToSave); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1697 | mContext.getContentResolver().update( |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 1698 | Uri.parse(sendOrSaveMessage.mSave ? message.saveUri : message.sendUri), |
| 1699 | sendOrSaveMessage.mValues, null, null); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1700 | } else { |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 1701 | ContentResolver resolver = mContext.getContentResolver(); |
Mindy Pereira | 92551d0 | 2012-04-05 11:31:12 -0700 | [diff] [blame] | 1702 | Uri messageUri = resolver |
| 1703 | .insert(sendOrSaveMessage.mSave ? selectedAccount.account.saveDraftUri |
| 1704 | : selectedAccount.account.sendMessageUri, |
| 1705 | sendOrSaveMessage.mValues); |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 1706 | if (sendOrSaveMessage.mSave && messageUri != null) { |
| 1707 | Cursor messageCursor = resolver.query(messageUri, |
| 1708 | UIProvider.MESSAGE_PROJECTION, null, null, null); |
Paul Westbrook | ba55848 | 2012-03-19 11:00:24 -0700 | [diff] [blame] | 1709 | if (messageCursor != null) { |
| 1710 | try { |
| 1711 | if (messageCursor.moveToFirst()) { |
| 1712 | // Broadcast notification that a new message has |
| 1713 | // been allocated |
| 1714 | mSendOrSaveCallback.notifyMessageIdAllocated(sendOrSaveMessage, |
| 1715 | new Message(messageCursor)); |
| 1716 | } |
| 1717 | } finally { |
| 1718 | messageCursor.close(); |
| 1719 | } |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 1720 | } |
| 1721 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1722 | } |
| 1723 | |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 1724 | if (!sendOrSaveMessage.mSave) { |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1725 | UIProvider.incrementRecipientsTimesContacted(mContext, |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 1726 | (String) sendOrSaveMessage.mValues.get(UIProvider.MessageColumns.TO)); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1727 | UIProvider.incrementRecipientsTimesContacted(mContext, |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 1728 | (String) sendOrSaveMessage.mValues.get(UIProvider.MessageColumns.CC)); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1729 | UIProvider.incrementRecipientsTimesContacted(mContext, |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 1730 | (String) sendOrSaveMessage.mValues.get(UIProvider.MessageColumns.BCC)); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1731 | } |
| 1732 | mSendOrSaveCallback.sendOrSaveFinished(SendOrSaveTask.this, true); |
| 1733 | } |
| 1734 | } |
| 1735 | |
| 1736 | // Array of the outstanding send or save tasks. Access is synchronized |
| 1737 | // with the object itself |
| 1738 | /* package for testing */ |
Mindy Pereira | e011b1d | 2012-06-18 13:45:26 -0700 | [diff] [blame] | 1739 | @VisibleForTesting |
| 1740 | public ArrayList<SendOrSaveTask> mActiveTasks = Lists.newArrayList(); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1741 | private int mRequestId; |
Mindy Pereira | bdf7a40 | 2012-03-01 15:23:26 -0800 | [diff] [blame] | 1742 | private String mSignature; |
Andrew Sapperstein | 8f1c01e | 2012-06-18 18:15:30 -0700 | [diff] [blame] | 1743 | private AttachmentTypeSelectorAdapter mAttachmentTypeSelectorAdapter; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1744 | |
Mindy Pereira | e011b1d | 2012-06-18 13:45:26 -0700 | [diff] [blame] | 1745 | @VisibleForTesting |
| 1746 | public static class SendOrSaveMessage { |
Mindy Pereira | 92551d0 | 2012-04-05 11:31:12 -0700 | [diff] [blame] | 1747 | final ReplyFromAccount mAccount; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1748 | final ContentValues mValues; |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 1749 | final String mRefMessageId; |
Mindy Pereira | e011b1d | 2012-06-18 13:45:26 -0700 | [diff] [blame] | 1750 | @VisibleForTesting |
| 1751 | public final boolean mSave; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1752 | final int mRequestId; |
| 1753 | |
Mindy Pereira | 92551d0 | 2012-04-05 11:31:12 -0700 | [diff] [blame] | 1754 | public SendOrSaveMessage(ReplyFromAccount account, ContentValues values, |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 1755 | String refMessageId, boolean save) { |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1756 | mAccount = account; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1757 | mValues = values; |
| 1758 | mRefMessageId = refMessageId; |
| 1759 | mSave = save; |
| 1760 | mRequestId = mValues.hashCode() ^ hashCode(); |
| 1761 | } |
| 1762 | |
| 1763 | int requestId() { |
| 1764 | return mRequestId; |
| 1765 | } |
| 1766 | } |
| 1767 | |
| 1768 | /** |
| 1769 | * Get the to recipients. |
| 1770 | */ |
| 1771 | public String[] getToAddresses() { |
| 1772 | return getAddressesFromList(mTo); |
| 1773 | } |
| 1774 | |
| 1775 | /** |
| 1776 | * Get the cc recipients. |
| 1777 | */ |
| 1778 | public String[] getCcAddresses() { |
| 1779 | return getAddressesFromList(mCc); |
| 1780 | } |
| 1781 | |
| 1782 | /** |
| 1783 | * Get the bcc recipients. |
| 1784 | */ |
| 1785 | public String[] getBccAddresses() { |
| 1786 | return getAddressesFromList(mBcc); |
| 1787 | } |
| 1788 | |
| 1789 | public String[] getAddressesFromList(RecipientEditTextView list) { |
| 1790 | if (list == null) { |
| 1791 | return new String[0]; |
| 1792 | } |
| 1793 | Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(list.getText()); |
| 1794 | int count = tokens.length; |
| 1795 | String[] result = new String[count]; |
| 1796 | for (int i = 0; i < count; i++) { |
| 1797 | result[i] = tokens[i].toString(); |
| 1798 | } |
| 1799 | return result; |
| 1800 | } |
| 1801 | |
| 1802 | /** |
| 1803 | * Check for invalid email addresses. |
| 1804 | * @param to String array of email addresses to check. |
| 1805 | * @param wrongEmailsOut Emails addresses that were invalid. |
| 1806 | */ |
| 1807 | public void checkInvalidEmails(String[] to, List<String> wrongEmailsOut) { |
Mindy Pereira | e5f20bf | 2012-06-25 14:20:40 -0700 | [diff] [blame] | 1808 | if (mValidator == null) { |
| 1809 | return; |
| 1810 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1811 | for (String email : to) { |
| 1812 | if (!mValidator.isValid(email)) { |
| 1813 | wrongEmailsOut.add(email); |
| 1814 | } |
| 1815 | } |
| 1816 | } |
| 1817 | |
| 1818 | /** |
| 1819 | * Show an error because the user has entered an invalid recipient. |
| 1820 | * @param message |
| 1821 | */ |
| 1822 | public void showRecipientErrorDialog(String message) { |
| 1823 | // Only 1 invalid recipients error dialog should be allowed up at a |
| 1824 | // time. |
| 1825 | if (mRecipientErrorDialog != null) { |
| 1826 | mRecipientErrorDialog.dismiss(); |
| 1827 | } |
| 1828 | mRecipientErrorDialog = new AlertDialog.Builder(this).setMessage(message).setTitle( |
| 1829 | R.string.recipient_error_dialog_title) |
| 1830 | .setIconAttribute(android.R.attr.alertDialogIcon) |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1831 | .setPositiveButton( |
| 1832 | R.string.ok, new Dialog.OnClickListener() { |
Marc Blank | 0bbc858 | 2012-04-23 15:07:57 -0700 | [diff] [blame] | 1833 | @Override |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1834 | public void onClick(DialogInterface dialog, int which) { |
| 1835 | // after the user dismisses the recipient error |
| 1836 | // dialog we want to make sure to refocus the |
| 1837 | // recipient to field so they can fix the issue |
| 1838 | // easily |
| 1839 | if (mTo != null) { |
| 1840 | mTo.requestFocus(); |
| 1841 | } |
| 1842 | mRecipientErrorDialog = null; |
| 1843 | } |
| 1844 | }).show(); |
| 1845 | } |
| 1846 | |
| 1847 | /** |
| 1848 | * Update the state of the UI based on whether or not the current draft |
| 1849 | * needs to be saved and the message is not empty. |
| 1850 | */ |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 1851 | public void updateSaveUi() { |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1852 | if (mSave != null) { |
| 1853 | mSave.setEnabled((shouldSave() && !isBlank())); |
| 1854 | } |
| 1855 | } |
| 1856 | |
| 1857 | /** |
| 1858 | * Returns true if we need to save the current draft. |
| 1859 | */ |
| 1860 | private boolean shouldSave() { |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 1861 | synchronized (mDraftLock) { |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1862 | // The message should only be saved if: |
| 1863 | // It hasn't been sent AND |
| 1864 | // Some text has been added to the message OR |
| 1865 | // an attachment has been added or removed |
Mindy Pereira | a214833 | 2012-07-02 13:54:14 -0700 | [diff] [blame] | 1866 | // AND there is actually something in the draft to save. |
Andy Huang | d47877e | 2012-08-09 19:31:24 -0700 | [diff] [blame^] | 1867 | return (mTextChanged || mAttachmentsChanged || mReplyFromChanged) |
Mindy Pereira | a214833 | 2012-07-02 13:54:14 -0700 | [diff] [blame] | 1868 | && !isBlank(); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1869 | } |
| 1870 | } |
| 1871 | |
| 1872 | /** |
Mindy Pereira | bdf7a40 | 2012-03-01 15:23:26 -0800 | [diff] [blame] | 1873 | * Check if all fields are blank. |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1874 | * @return boolean |
| 1875 | */ |
| 1876 | public boolean isBlank() { |
| 1877 | return mSubject.getText().length() == 0 |
Mindy Pereira | bdf7a40 | 2012-03-01 15:23:26 -0800 | [diff] [blame] | 1878 | && (mBodyView.getText().length() == 0 || getSignatureStartPosition(mSignature, |
| 1879 | mBodyView.getText().toString()) == 0) |
| 1880 | && mTo.length() == 0 |
| 1881 | && mCc.length() == 0 && mBcc.length() == 0 |
| 1882 | && mAttachmentsView.getAttachments().size() == 0; |
| 1883 | } |
| 1884 | |
| 1885 | @VisibleForTesting |
| 1886 | protected int getSignatureStartPosition(String signature, String bodyText) { |
| 1887 | int startPos = -1; |
| 1888 | |
| 1889 | if (TextUtils.isEmpty(signature) || TextUtils.isEmpty(bodyText)) { |
| 1890 | return startPos; |
| 1891 | } |
| 1892 | |
| 1893 | int bodyLength = bodyText.length(); |
| 1894 | int signatureLength = signature.length(); |
| 1895 | String printableVersion = convertToPrintableSignature(signature); |
| 1896 | int printableLength = printableVersion.length(); |
| 1897 | |
| 1898 | if (bodyLength >= printableLength |
| 1899 | && bodyText.substring(bodyLength - printableLength) |
| 1900 | .equals(printableVersion)) { |
| 1901 | startPos = bodyLength - printableLength; |
| 1902 | } else if (bodyLength >= signatureLength |
| 1903 | && bodyText.substring(bodyLength - signatureLength) |
| 1904 | .equals(signature)) { |
| 1905 | startPos = bodyLength - signatureLength; |
| 1906 | } |
| 1907 | return startPos; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1908 | } |
| 1909 | |
| 1910 | /** |
| 1911 | * Allows any changes made by the user to be ignored. Called when the user |
| 1912 | * decides to discard a draft. |
| 1913 | */ |
| 1914 | private void discardChanges() { |
| 1915 | mTextChanged = false; |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 1916 | mAttachmentsChanged = false; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1917 | mReplyFromChanged = false; |
| 1918 | } |
| 1919 | |
| 1920 | /** |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1921 | * @param body |
| 1922 | * @param save |
| 1923 | * @param showToast |
| 1924 | * @return Whether the send or save succeeded. |
| 1925 | */ |
| 1926 | protected boolean sendOrSaveWithSanityChecks(final boolean save, final boolean showToast, |
| 1927 | final boolean orientationChanged) { |
| 1928 | String[] to, cc, bcc; |
| 1929 | Editable body = mBodyView.getEditableText(); |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1930 | if (orientationChanged) { |
| 1931 | to = cc = bcc = new String[0]; |
| 1932 | } else { |
| 1933 | to = getToAddresses(); |
| 1934 | cc = getCcAddresses(); |
| 1935 | bcc = getBccAddresses(); |
| 1936 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1937 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1938 | // Don't let the user send to nobody (but it's okay to save a message |
| 1939 | // with no recipients) |
| 1940 | if (!save && (to.length == 0 && cc.length == 0 && bcc.length == 0)) { |
| 1941 | showRecipientErrorDialog(getString(R.string.recipient_needed)); |
| 1942 | return false; |
| 1943 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1944 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1945 | List<String> wrongEmails = new ArrayList<String>(); |
| 1946 | if (!save) { |
| 1947 | checkInvalidEmails(to, wrongEmails); |
| 1948 | checkInvalidEmails(cc, wrongEmails); |
| 1949 | checkInvalidEmails(bcc, wrongEmails); |
| 1950 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1951 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1952 | // Don't let the user send an email with invalid recipients |
| 1953 | if (wrongEmails.size() > 0) { |
| 1954 | String errorText = String.format(getString(R.string.invalid_recipient), |
| 1955 | wrongEmails.get(0)); |
| 1956 | showRecipientErrorDialog(errorText); |
| 1957 | return false; |
| 1958 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1959 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1960 | DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() { |
Marc Blank | 0bbc858 | 2012-04-23 15:07:57 -0700 | [diff] [blame] | 1961 | @Override |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1962 | public void onClick(DialogInterface dialog, int which) { |
| 1963 | sendOrSave(mBodyView.getEditableText(), save, showToast, orientationChanged); |
| 1964 | } |
| 1965 | }; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1966 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1967 | // Show a warning before sending only if there are no attachments. |
| 1968 | if (!save) { |
| 1969 | if (mAttachmentsView.getAttachments().isEmpty() && showEmptyTextWarnings()) { |
| 1970 | boolean warnAboutEmptySubject = isSubjectEmpty(); |
| 1971 | boolean emptyBody = TextUtils.getTrimmedLength(body) == 0; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1972 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1973 | // A warning about an empty body may not be warranted when |
| 1974 | // forwarding mails, since a common use case is to forward |
| 1975 | // quoted text and not append any more text. |
| 1976 | boolean warnAboutEmptyBody = emptyBody && (!mForward || isBodyEmpty()); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1977 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1978 | // When we bring up a dialog warning the user about a send, |
| 1979 | // assume that they accept sending the message. If they do not, |
| 1980 | // the dialog listener is required to enable sending again. |
| 1981 | if (warnAboutEmptySubject) { |
| 1982 | showSendConfirmDialog(R.string.confirm_send_message_with_no_subject, listener); |
| 1983 | return true; |
| 1984 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1985 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1986 | if (warnAboutEmptyBody) { |
| 1987 | showSendConfirmDialog(R.string.confirm_send_message_with_no_body, listener); |
| 1988 | return true; |
| 1989 | } |
| 1990 | } |
| 1991 | // Ask for confirmation to send (if always required) |
| 1992 | if (showSendConfirmation()) { |
| 1993 | showSendConfirmDialog(R.string.confirm_send_message, listener); |
| 1994 | return true; |
| 1995 | } |
| 1996 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1997 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 1998 | sendOrSave(body, save, showToast, false); |
| 1999 | return true; |
| 2000 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2001 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2002 | /** |
| 2003 | * Returns a boolean indicating whether warnings should be shown for empty |
| 2004 | * subject and body fields |
Andy Huang | 5c5fd57 | 2012-04-08 18:19:29 -0700 | [diff] [blame] | 2005 | * |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2006 | * @return True if a warning should be shown for empty text fields |
| 2007 | */ |
| 2008 | protected boolean showEmptyTextWarnings() { |
| 2009 | return mAttachmentsView.getAttachments().size() == 0; |
| 2010 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2011 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2012 | /** |
| 2013 | * Returns a boolean indicating whether the user should confirm each send |
| 2014 | * |
| 2015 | * @return True if a warning should be on each send |
| 2016 | */ |
| 2017 | protected boolean showSendConfirmation() { |
| 2018 | return mCachedSettings != null ? mCachedSettings.confirmSend : false; |
| 2019 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2020 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2021 | private void showSendConfirmDialog(int messageId, DialogInterface.OnClickListener listener) { |
| 2022 | if (mSendConfirmDialog != null) { |
| 2023 | mSendConfirmDialog.dismiss(); |
| 2024 | mSendConfirmDialog = null; |
| 2025 | } |
| 2026 | mSendConfirmDialog = new AlertDialog.Builder(this).setMessage(messageId) |
| 2027 | .setTitle(R.string.confirm_send_title) |
| 2028 | .setIconAttribute(android.R.attr.alertDialogIcon) |
| 2029 | .setPositiveButton(R.string.send, listener) |
Mindy Pereira | 6edd597 | 2012-06-19 10:22:36 -0700 | [diff] [blame] | 2030 | .setNegativeButton(R.string.cancel, this) |
| 2031 | .show(); |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2032 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2033 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2034 | /** |
| 2035 | * Returns whether the ComposeArea believes there is any text in the body of |
| 2036 | * the composition. TODO: When ComposeArea controls the Body as well, add |
| 2037 | * that here. |
| 2038 | */ |
| 2039 | public boolean isBodyEmpty() { |
| 2040 | return !mQuotedTextView.isTextIncluded(); |
| 2041 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2042 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2043 | /** |
| 2044 | * Test to see if the subject is empty. |
| 2045 | * |
| 2046 | * @return boolean. |
| 2047 | */ |
| 2048 | // TODO: this will likely go away when composeArea.focus() is implemented |
| 2049 | // after all the widget control is moved over. |
| 2050 | public boolean isSubjectEmpty() { |
| 2051 | return TextUtils.getTrimmedLength(mSubject.getText()) == 0; |
| 2052 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2053 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2054 | /* package */ |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 2055 | static int sendOrSaveInternal(Context context, ReplyFromAccount replyFromAccount, |
Paul Westbrook | 05b92b8 | 2012-04-20 13:29:37 -0700 | [diff] [blame] | 2056 | Message message, final Message refMessage, Spanned body, final CharSequence quotedText, |
| 2057 | SendOrSaveCallback callback, Handler handler, boolean save, int composeMode) { |
Mindy Pereira | 29ef1b8 | 2012-01-13 11:26:21 -0800 | [diff] [blame] | 2058 | ContentValues values = new ContentValues(); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2059 | |
Mindy Pereira | c203197 | 2012-04-03 09:38:35 -0700 | [diff] [blame] | 2060 | String refMessageId = refMessage != null ? refMessage.uri.toString() : ""; |
| 2061 | |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 2062 | MessageModification.putToAddresses(values, message.getToAddresses()); |
| 2063 | MessageModification.putCcAddresses(values, message.getCcAddresses()); |
| 2064 | MessageModification.putBccAddresses(values, message.getBccAddresses()); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2065 | |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 2066 | MessageModification.putCustomFromAddress(values, message.from); |
Mindy Pereira | 92551d0 | 2012-04-05 11:31:12 -0700 | [diff] [blame] | 2067 | |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 2068 | MessageModification.putSubject(values, message.subject); |
Mindy Pereira | 29ef1b8 | 2012-01-13 11:26:21 -0800 | [diff] [blame] | 2069 | String htmlBody = Html.toHtml(body); |
Paul Westbrook | 05b92b8 | 2012-04-20 13:29:37 -0700 | [diff] [blame] | 2070 | |
Mindy Pereira | 29ef1b8 | 2012-01-13 11:26:21 -0800 | [diff] [blame] | 2071 | boolean includeQuotedText = !TextUtils.isEmpty(quotedText); |
| 2072 | StringBuilder fullBody = new StringBuilder(htmlBody); |
| 2073 | if (includeQuotedText) { |
Mindy Pereira | e8caf12 | 2012-03-20 15:23:31 -0700 | [diff] [blame] | 2074 | // HTML gets converted to text for now |
| 2075 | final String text = quotedText.toString(); |
| 2076 | if (QuotedTextView.containsQuotedText(text)) { |
| 2077 | int pos = QuotedTextView.getQuotedTextOffset(text); |
Paul Westbrook | 55271cf | 2012-04-20 16:25:02 -0700 | [diff] [blame] | 2078 | final int quoteStartPos = fullBody.length() + pos; |
| 2079 | fullBody.append(text); |
| 2080 | MessageModification.putQuoteStartPos(values, quoteStartPos); |
Mindy Pereira | 1257586 | 2012-03-21 16:30:54 -0700 | [diff] [blame] | 2081 | MessageModification.putForward(values, composeMode == ComposeActivity.FORWARD); |
Mindy Pereira | e8caf12 | 2012-03-20 15:23:31 -0700 | [diff] [blame] | 2082 | MessageModification.putAppendRefMessageContent(values, includeQuotedText); |
Mindy Pereira | 29ef1b8 | 2012-01-13 11:26:21 -0800 | [diff] [blame] | 2083 | } else { |
Mindy Pereira | e8caf12 | 2012-03-20 15:23:31 -0700 | [diff] [blame] | 2084 | LogUtils.w(LOG_TAG, "Couldn't find quoted text"); |
| 2085 | // This shouldn't happen, but just use what we have, |
| 2086 | // and don't do server-side expansion |
| 2087 | fullBody.append(text); |
Mindy Pereira | 29ef1b8 | 2012-01-13 11:26:21 -0800 | [diff] [blame] | 2088 | } |
| 2089 | } |
Mindy Pereira | 002ff52 | 2012-05-30 10:31:26 -0700 | [diff] [blame] | 2090 | int draftType = getDraftType(composeMode); |
Mindy Pereira | 1257586 | 2012-03-21 16:30:54 -0700 | [diff] [blame] | 2091 | MessageModification.putDraftType(values, draftType); |
Mindy Pereira | c6f1e2a | 2012-04-04 10:33:45 -0700 | [diff] [blame] | 2092 | if (refMessage != null) { |
| 2093 | if (!TextUtils.isEmpty(refMessage.bodyHtml)) { |
| 2094 | MessageModification.putBodyHtml(values, fullBody.toString()); |
| 2095 | } |
| 2096 | if (!TextUtils.isEmpty(refMessage.bodyText)) { |
| 2097 | MessageModification.putBody(values, Html.fromHtml(fullBody.toString()).toString()); |
| 2098 | } |
| 2099 | } else { |
Mindy Pereira | c203197 | 2012-04-03 09:38:35 -0700 | [diff] [blame] | 2100 | MessageModification.putBodyHtml(values, fullBody.toString()); |
Mindy Pereira | c203197 | 2012-04-03 09:38:35 -0700 | [diff] [blame] | 2101 | MessageModification.putBody(values, Html.fromHtml(fullBody.toString()).toString()); |
| 2102 | } |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 2103 | MessageModification.putAttachments(values, message.getAttachments()); |
Mindy Pereira | 1257586 | 2012-03-21 16:30:54 -0700 | [diff] [blame] | 2104 | if (!TextUtils.isEmpty(refMessageId)) { |
| 2105 | MessageModification.putRefMessageId(values, refMessageId); |
| 2106 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2107 | |
Mindy Pereira | 92551d0 | 2012-04-05 11:31:12 -0700 | [diff] [blame] | 2108 | SendOrSaveMessage sendOrSaveMessage = new SendOrSaveMessage(replyFromAccount, |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2109 | values, refMessageId, save); |
| 2110 | SendOrSaveTask sendOrSaveTask = new SendOrSaveTask(context, sendOrSaveMessage, callback); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2111 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2112 | callback.initializeSendOrSave(sendOrSaveTask); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2113 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2114 | // Do the send/save action on the specified handler to avoid possible |
| 2115 | // ANRs |
| 2116 | handler.post(sendOrSaveTask); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2117 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2118 | return sendOrSaveMessage.requestId(); |
| 2119 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2120 | |
Mindy Pereira | 002ff52 | 2012-05-30 10:31:26 -0700 | [diff] [blame] | 2121 | private static int getDraftType(int mode) { |
| 2122 | int draftType = -1; |
| 2123 | switch (mode) { |
| 2124 | case ComposeActivity.COMPOSE: |
| 2125 | draftType = DraftType.COMPOSE; |
| 2126 | break; |
| 2127 | case ComposeActivity.REPLY: |
| 2128 | draftType = DraftType.REPLY; |
| 2129 | break; |
| 2130 | case ComposeActivity.REPLY_ALL: |
| 2131 | draftType = DraftType.REPLY_ALL; |
| 2132 | break; |
| 2133 | case ComposeActivity.FORWARD: |
| 2134 | draftType = DraftType.FORWARD; |
| 2135 | break; |
| 2136 | } |
| 2137 | return draftType; |
| 2138 | } |
| 2139 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2140 | private void sendOrSave(Spanned body, boolean save, boolean showToast, |
| 2141 | boolean orientationChanged) { |
| 2142 | // Check if user is a monkey. Monkeys can compose and hit send |
| 2143 | // button but are not allowed to send anything off the device. |
Paul Westbrook | 3ae824c | 2012-04-06 13:29:39 -0700 | [diff] [blame] | 2144 | if (ActivityManager.isUserAMonkey()) { |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2145 | return; |
| 2146 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2147 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2148 | String[] to, cc, bcc; |
| 2149 | if (orientationChanged) { |
| 2150 | to = cc = bcc = new String[0]; |
| 2151 | } else { |
| 2152 | to = getToAddresses(); |
| 2153 | cc = getCcAddresses(); |
| 2154 | bcc = getBccAddresses(); |
| 2155 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2156 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2157 | SendOrSaveCallback callback = new SendOrSaveCallback() { |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2158 | private int mRestoredRequestId; |
| 2159 | |
Marc Blank | 0bbc858 | 2012-04-23 15:07:57 -0700 | [diff] [blame] | 2160 | @Override |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2161 | public void initializeSendOrSave(SendOrSaveTask sendOrSaveTask) { |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2162 | synchronized (mActiveTasks) { |
| 2163 | int numTasks = mActiveTasks.size(); |
| 2164 | if (numTasks == 0) { |
| 2165 | // Start service so we won't be killed if this app is |
| 2166 | // put in the background. |
| 2167 | startService(new Intent(ComposeActivity.this, EmptyService.class)); |
| 2168 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2169 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2170 | mActiveTasks.add(sendOrSaveTask); |
| 2171 | } |
| 2172 | if (sTestSendOrSaveCallback != null) { |
| 2173 | sTestSendOrSaveCallback.initializeSendOrSave(sendOrSaveTask); |
| 2174 | } |
| 2175 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2176 | |
Marc Blank | 0bbc858 | 2012-04-23 15:07:57 -0700 | [diff] [blame] | 2177 | @Override |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 2178 | public void notifyMessageIdAllocated(SendOrSaveMessage sendOrSaveMessage, |
| 2179 | Message message) { |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2180 | synchronized (mDraftLock) { |
| 2181 | mDraftId = message.id; |
| 2182 | mDraft = message; |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 2183 | if (sRequestMessageIdMap != null) { |
| 2184 | sRequestMessageIdMap.put(sendOrSaveMessage.requestId(), mDraftId); |
| 2185 | } |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2186 | // Cache request message map, in case the process is killed |
| 2187 | saveRequestMap(); |
| 2188 | } |
| 2189 | if (sTestSendOrSaveCallback != null) { |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 2190 | sTestSendOrSaveCallback.notifyMessageIdAllocated(sendOrSaveMessage, message); |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2191 | } |
| 2192 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2193 | |
Marc Blank | 0bbc858 | 2012-04-23 15:07:57 -0700 | [diff] [blame] | 2194 | @Override |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 2195 | public Message getMessage() { |
| 2196 | synchronized (mDraftLock) { |
| 2197 | return mDraft; |
| 2198 | } |
| 2199 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2200 | |
Marc Blank | 0bbc858 | 2012-04-23 15:07:57 -0700 | [diff] [blame] | 2201 | @Override |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 2202 | public void sendOrSaveFinished(SendOrSaveTask task, boolean success) { |
Mindy Pereira | 47d0e65 | 2012-07-23 09:45:07 -0700 | [diff] [blame] | 2203 | // Update the last sent from account. |
| 2204 | if (mAccount != null) { |
| 2205 | MailAppProvider.getInstance().setLastSentFromAccount(mAccount.uri.toString()); |
| 2206 | } |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 2207 | if (success) { |
| 2208 | // Successfully sent or saved so reset change markers |
| 2209 | discardChanges(); |
| 2210 | } else { |
| 2211 | // A failure happened with saving/sending the draft |
| 2212 | // TODO(pwestbro): add a better string that should be used |
| 2213 | // when failing to send or save |
| 2214 | Toast.makeText(ComposeActivity.this, R.string.send_failed, Toast.LENGTH_SHORT) |
| 2215 | .show(); |
| 2216 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2217 | |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 2218 | int numTasks; |
| 2219 | synchronized (mActiveTasks) { |
| 2220 | // Remove the task from the list of active tasks |
| 2221 | mActiveTasks.remove(task); |
| 2222 | numTasks = mActiveTasks.size(); |
| 2223 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2224 | |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 2225 | if (numTasks == 0) { |
| 2226 | // Stop service so we can be killed. |
| 2227 | stopService(new Intent(ComposeActivity.this, EmptyService.class)); |
| 2228 | } |
| 2229 | if (sTestSendOrSaveCallback != null) { |
| 2230 | sTestSendOrSaveCallback.sendOrSaveFinished(task, success); |
| 2231 | } |
| 2232 | } |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2233 | }; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2234 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2235 | // Get the selected account if the from spinner has been setup. |
Mindy Pereira | 92551d0 | 2012-04-05 11:31:12 -0700 | [diff] [blame] | 2236 | ReplyFromAccount selectedAccount = mReplyFromAccount; |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2237 | String fromAddress = selectedAccount.name; |
| 2238 | if (selectedAccount == null || fromAddress == null) { |
| 2239 | // We don't have either the selected account or from address, |
| 2240 | // use mAccount. |
Mindy Pereira | 92551d0 | 2012-04-05 11:31:12 -0700 | [diff] [blame] | 2241 | selectedAccount = mReplyFromAccount; |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2242 | fromAddress = mAccount.name; |
| 2243 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2244 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2245 | if (mSendSaveTaskHandler == null) { |
| 2246 | HandlerThread handlerThread = new HandlerThread("Send Message Task Thread"); |
| 2247 | handlerThread.start(); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2248 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2249 | mSendSaveTaskHandler = new Handler(handlerThread.getLooper()); |
| 2250 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2251 | |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 2252 | Message msg = createMessage(mReplyFromAccount, getMode()); |
Paul Westbrook | 05b92b8 | 2012-04-20 13:29:37 -0700 | [diff] [blame] | 2253 | mRequestId = sendOrSaveInternal(this, mReplyFromAccount, msg, mRefMessage, body, |
| 2254 | mQuotedTextView.getQuotedTextIfIncluded(), callback, |
Mindy Pereira | 1257586 | 2012-03-21 16:30:54 -0700 | [diff] [blame] | 2255 | mSendSaveTaskHandler, save, mComposeMode); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2256 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2257 | if (mRecipient != null && mRecipient.equals(mAccount.name)) { |
| 2258 | mRecipient = selectedAccount.name; |
| 2259 | } |
Paul Westbrook | b1f573c | 2012-04-06 11:38:28 -0700 | [diff] [blame] | 2260 | setAccount(selectedAccount.account); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2261 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2262 | // Don't display the toast if the user is just changing the orientation, |
| 2263 | // but we still need to save the draft to the cursor because this is how we restore |
| 2264 | // the attachments when the configuration change completes. |
| 2265 | if (showToast && (getChangingConfigurations() & ActivityInfo.CONFIG_ORIENTATION) == 0) { |
| 2266 | Toast.makeText(this, save ? R.string.message_saved : R.string.sending_message, |
| 2267 | Toast.LENGTH_LONG).show(); |
| 2268 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2269 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2270 | // Need to update variables here because the send or save completes |
| 2271 | // asynchronously even though the toast shows right away. |
| 2272 | discardChanges(); |
| 2273 | updateSaveUi(); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2274 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2275 | // If we are sending, finish the activity |
| 2276 | if (!save) { |
| 2277 | finish(); |
| 2278 | } |
| 2279 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2280 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2281 | /** |
| 2282 | * Save the state of the request messageid map. This allows for the Gmail |
| 2283 | * process to be killed, but and still allow for ComposeActivity instances |
| 2284 | * to be recreated correctly. |
| 2285 | */ |
| 2286 | private void saveRequestMap() { |
| 2287 | // TODO: store the request map in user preferences. |
| 2288 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2289 | |
Andrew Sapperstein | 8f1c01e | 2012-06-18 18:15:30 -0700 | [diff] [blame] | 2290 | public void openAttachmentTypeSelectionDialog() { |
| 2291 | AlertDialog.Builder builder = new AlertDialog.Builder(this); |
| 2292 | builder.setTitle(R.string.add_file_attachment); |
| 2293 | builder.setAdapter(new AttachmentTypeSelectorAdapter(this), |
| 2294 | new DialogInterface.OnClickListener() { |
| 2295 | public void onClick(DialogInterface dialog, int position) { |
| 2296 | doAttach(position); |
| 2297 | } |
| 2298 | }); |
| 2299 | builder.show(); |
| 2300 | } |
| 2301 | |
| 2302 | private void doAttach(int position) { |
Mindy Pereira | 013194c | 2012-01-06 15:09:33 -0800 | [diff] [blame] | 2303 | Intent i = new Intent(Intent.ACTION_GET_CONTENT); |
| 2304 | i.addCategory(Intent.CATEGORY_OPENABLE); |
Paul Westbrook | d6a9a3f | 2012-04-26 18:47:23 -0700 | [diff] [blame] | 2305 | i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); |
Andrew Sapperstein | 8f1c01e | 2012-06-18 18:15:30 -0700 | [diff] [blame] | 2306 | i.setType(AttachmentTypeSelectorAdapter.ITEMS.get(position).mMimeType); |
Mindy Pereira | 013194c | 2012-01-06 15:09:33 -0800 | [diff] [blame] | 2307 | mAddingAttachment = true; |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2308 | startActivityForResult(Intent.createChooser(i, getText(R.string.select_attachment_type)), |
| 2309 | RESULT_PICK_ATTACHMENT); |
Mindy Pereira | 013194c | 2012-01-06 15:09:33 -0800 | [diff] [blame] | 2310 | } |
| 2311 | |
Mindy Pereira | ec8b0ed | 2012-01-06 10:35:59 -0800 | [diff] [blame] | 2312 | private void showCcBccViews() { |
Mindy Pereira | a26b54e | 2012-01-06 12:54:33 -0800 | [diff] [blame] | 2313 | mCcBccView.show(true, true, true); |
Mindy Pereira | ec8b0ed | 2012-01-06 10:35:59 -0800 | [diff] [blame] | 2314 | if (mCcBccButton != null) { |
| 2315 | mCcBccButton.setVisibility(View.GONE); |
| 2316 | } |
| 2317 | } |
| 2318 | |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 2319 | @Override |
| 2320 | public boolean onNavigationItemSelected(int position, long itemId) { |
Mindy Pereira | a26b54e | 2012-01-06 12:54:33 -0800 | [diff] [blame] | 2321 | int initialComposeMode = mComposeMode; |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 2322 | if (position == ComposeActivity.REPLY) { |
| 2323 | mComposeMode = ComposeActivity.REPLY; |
| 2324 | } else if (position == ComposeActivity.REPLY_ALL) { |
| 2325 | mComposeMode = ComposeActivity.REPLY_ALL; |
| 2326 | } else if (position == ComposeActivity.FORWARD) { |
| 2327 | mComposeMode = ComposeActivity.FORWARD; |
| 2328 | } |
Mindy Pereira | cbfb75a | 2012-06-25 14:52:23 -0700 | [diff] [blame] | 2329 | clearChangeListeners(); |
Mindy Pereira | a26b54e | 2012-01-06 12:54:33 -0800 | [diff] [blame] | 2330 | if (initialComposeMode != mComposeMode) { |
Mindy Pereira | 154386a | 2012-01-11 13:02:33 -0800 | [diff] [blame] | 2331 | resetMessageForModeChange(); |
Mindy Pereira | ef38830 | 2012-06-18 19:07:44 -0700 | [diff] [blame] | 2332 | if (mDraft == null && mRefMessage != null) { |
Mindy Pereira | 96a7f7a | 2012-07-09 16:51:06 -0700 | [diff] [blame] | 2333 | setFieldsFromRefMessage(mComposeMode, mAccount.name); |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 2334 | } |
Mindy Pereira | ef38830 | 2012-06-18 19:07:44 -0700 | [diff] [blame] | 2335 | boolean showCc = false; |
| 2336 | boolean showBcc = false; |
| 2337 | if (mDraft != null) { |
| 2338 | // Following desktop behavior, if the user has added a BCC |
| 2339 | // field to a draft, we show it regardless of compose mode. |
| 2340 | showBcc = !TextUtils.isEmpty(mDraft.bcc); |
| 2341 | // Use the draft to determine what to populate. |
| 2342 | // If the Bcc field is showing, show the Cc field whether it is populated or not. |
| 2343 | showCc = showBcc || (!TextUtils.isEmpty(mDraft.cc) && mComposeMode == REPLY_ALL); |
| 2344 | } else if (mRefMessage != null) { |
| 2345 | showCc = mComposeMode == REPLY_ALL && !TextUtils.isEmpty(mRefMessage.cc); |
| 2346 | } |
| 2347 | mCcBccView.show(false, showCc, showBcc); |
Mindy Pereira | a26b54e | 2012-01-06 12:54:33 -0800 | [diff] [blame] | 2348 | } |
Mindy Pereira | ef38830 | 2012-06-18 19:07:44 -0700 | [diff] [blame] | 2349 | updateHideOrShowCcBcc(); |
Mindy Pereira | cbfb75a | 2012-06-25 14:52:23 -0700 | [diff] [blame] | 2350 | initChangeListeners(); |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 2351 | return true; |
| 2352 | } |
| 2353 | |
Mindy Pereira | b3112a2 | 2012-06-20 12:10:03 -0700 | [diff] [blame] | 2354 | @VisibleForTesting |
| 2355 | protected void resetMessageForModeChange() { |
Mindy Pereira | 154386a | 2012-01-11 13:02:33 -0800 | [diff] [blame] | 2356 | // When switching between reply, reply all, forward, |
| 2357 | // follow the behavior of webview. |
| 2358 | // The contents of the following fields are cleared |
| 2359 | // so that they can be populated directly from the |
| 2360 | // ref message: |
| 2361 | // 1) Any recipient fields |
| 2362 | // 2) The subject |
| 2363 | mTo.setText(""); |
| 2364 | mCc.setText(""); |
| 2365 | mBcc.setText(""); |
| 2366 | // Any edits to the subject are replaced with the original subject. |
| 2367 | mSubject.setText(""); |
| 2368 | |
| 2369 | // Any changes to the contents of the following fields are kept: |
| 2370 | // 1) Body |
| 2371 | // 2) Attachments |
| 2372 | // If the user made changes to attachments, keep their changes. |
| 2373 | if (!mAttachmentsChanged) { |
| 2374 | mAttachmentsView.deleteAllAttachments(); |
| 2375 | } |
| 2376 | } |
| 2377 | |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 2378 | private class ComposeModeAdapter extends ArrayAdapter<String> { |
| 2379 | |
| 2380 | private LayoutInflater mInflater; |
| 2381 | |
| 2382 | public ComposeModeAdapter(Context context) { |
| 2383 | super(context, R.layout.compose_mode_item, R.id.mode, getResources() |
| 2384 | .getStringArray(R.array.compose_modes)); |
| 2385 | } |
| 2386 | |
| 2387 | private LayoutInflater getInflater() { |
| 2388 | if (mInflater == null) { |
| 2389 | mInflater = LayoutInflater.from(getContext()); |
| 2390 | } |
| 2391 | return mInflater; |
| 2392 | } |
| 2393 | |
| 2394 | @Override |
| 2395 | public View getView(int position, View convertView, ViewGroup parent) { |
| 2396 | if (convertView == null) { |
| 2397 | convertView = getInflater().inflate(R.layout.compose_mode_display_item, null); |
| 2398 | } |
| 2399 | ((TextView) convertView.findViewById(R.id.mode)).setText(getItem(position)); |
| 2400 | return super.getView(position, convertView, parent); |
| 2401 | } |
| 2402 | } |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 2403 | |
| 2404 | @Override |
| 2405 | public void onRespondInline(String text) { |
| 2406 | appendToBody(text, false); |
| 2407 | } |
| 2408 | |
| 2409 | /** |
| 2410 | * Append text to the body of the message. If there is no existing body |
| 2411 | * text, just sets the body to text. |
| 2412 | * |
| 2413 | * @param text |
| 2414 | * @param withSignature True to append a signature. |
| 2415 | */ |
| 2416 | public void appendToBody(CharSequence text, boolean withSignature) { |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 2417 | Editable bodyText = mBodyView.getEditableText(); |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 2418 | if (bodyText != null && bodyText.length() > 0) { |
| 2419 | bodyText.append(text); |
| 2420 | } else { |
| 2421 | setBody(text, withSignature); |
| 2422 | } |
| 2423 | } |
| 2424 | |
| 2425 | /** |
| 2426 | * Set the body of the message. |
Mindy Pereira | bdf7a40 | 2012-03-01 15:23:26 -0800 | [diff] [blame] | 2427 | * |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 2428 | * @param text |
| 2429 | * @param withSignature True to append a signature. |
| 2430 | */ |
| 2431 | public void setBody(CharSequence text, boolean withSignature) { |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 2432 | mBodyView.setText(text); |
Mindy Pereira | bdf7a40 | 2012-03-01 15:23:26 -0800 | [diff] [blame] | 2433 | if (withSignature) { |
| 2434 | appendSignature(); |
| 2435 | } |
| 2436 | } |
| 2437 | |
| 2438 | private void appendSignature() { |
Mindy Pereira | b13917c | 2012-03-29 08:08:19 -0700 | [diff] [blame] | 2439 | String newSignature = mCachedSettings != null ? mCachedSettings.signature : null; |
Mindy Pereira | 433b198 | 2012-04-03 11:53:07 -0700 | [diff] [blame] | 2440 | boolean hasFocus = mBodyView.hasFocus(); |
Mindy Pereira | b13917c | 2012-03-29 08:08:19 -0700 | [diff] [blame] | 2441 | if (!TextUtils.equals(newSignature, mSignature)) { |
| 2442 | mSignature = newSignature; |
| 2443 | if (!TextUtils.isEmpty(mSignature) |
| 2444 | && getSignatureStartPosition(mSignature, |
| 2445 | mBodyView.getText().toString()) < 0) { |
| 2446 | // Appending a signature does not count as changing text. |
| 2447 | mBodyView.removeTextChangedListener(this); |
| 2448 | mBodyView.append(convertToPrintableSignature(mSignature)); |
| 2449 | mBodyView.addTextChangedListener(this); |
| 2450 | } |
Mindy Pereira | 433b198 | 2012-04-03 11:53:07 -0700 | [diff] [blame] | 2451 | if (hasFocus) { |
| 2452 | focusBody(); |
| 2453 | } |
Mindy Pereira | bdf7a40 | 2012-03-01 15:23:26 -0800 | [diff] [blame] | 2454 | } |
| 2455 | } |
| 2456 | |
| 2457 | private String convertToPrintableSignature(String signature) { |
| 2458 | String signatureResource = getResources().getString(R.string.signature); |
| 2459 | if (signature == null) { |
| 2460 | signature = ""; |
| 2461 | } |
| 2462 | return String.format(signatureResource, signature); |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 2463 | } |
Mindy Pereira | 1a95a57 | 2012-01-05 12:21:29 -0800 | [diff] [blame] | 2464 | |
Mindy Pereira | 5a85e2b | 2012-01-11 09:53:32 -0800 | [diff] [blame] | 2465 | @Override |
| 2466 | public void onAccountChanged() { |
Mindy Pereira | 92551d0 | 2012-04-05 11:31:12 -0700 | [diff] [blame] | 2467 | mReplyFromAccount = mFromSpinner.getCurrentAccount(); |
| 2468 | if (!mAccount.equals(mReplyFromAccount.account)) { |
Paul Westbrook | b1f573c | 2012-04-06 11:38:28 -0700 | [diff] [blame] | 2469 | setAccount(mReplyFromAccount.account); |
| 2470 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2471 | // TODO: handle discarding attachments when switching accounts. |
| 2472 | // Only enable save for this draft if there is any other content |
| 2473 | // in the message. |
| 2474 | if (!isBlank()) { |
| 2475 | enableSave(true); |
| 2476 | } |
| 2477 | mReplyFromChanged = true; |
| 2478 | initRecipients(); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2479 | } |
Mindy Pereira | 1a95a57 | 2012-01-05 12:21:29 -0800 | [diff] [blame] | 2480 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2481 | |
| 2482 | public void enableSave(boolean enabled) { |
| 2483 | if (mSave != null) { |
| 2484 | mSave.setEnabled(enabled); |
| 2485 | } |
| 2486 | } |
| 2487 | |
| 2488 | public void enableSend(boolean enabled) { |
| 2489 | if (mSend != null) { |
| 2490 | mSend.setEnabled(enabled); |
| 2491 | } |
| 2492 | } |
| 2493 | |
| 2494 | /** |
| 2495 | * Handles button clicks from any error dialogs dealing with sending |
| 2496 | * a message. |
| 2497 | */ |
| 2498 | @Override |
| 2499 | public void onClick(DialogInterface dialog, int which) { |
| 2500 | switch (which) { |
| 2501 | case DialogInterface.BUTTON_POSITIVE: { |
| 2502 | doDiscardWithoutConfirmation(true /* show toast */ ); |
| 2503 | break; |
| 2504 | } |
| 2505 | case DialogInterface.BUTTON_NEGATIVE: { |
| 2506 | // If the user cancels the send, re-enable the send button. |
| 2507 | enableSend(true); |
| 2508 | break; |
| 2509 | } |
| 2510 | } |
| 2511 | |
| 2512 | } |
| 2513 | |
Mindy Pereira | efe3d25 | 2012-03-01 14:20:44 -0800 | [diff] [blame] | 2514 | private void doDiscard() { |
| 2515 | new AlertDialog.Builder(this).setMessage(R.string.confirm_discard_text) |
| 2516 | .setPositiveButton(R.string.ok, this) |
| 2517 | .setNegativeButton(R.string.cancel, null) |
| 2518 | .create().show(); |
| 2519 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2520 | /** |
| 2521 | * Effectively discard the current message. |
| 2522 | * |
| 2523 | * This method is either invoked from the menu or from the dialog |
| 2524 | * once the user has confirmed that they want to discard the message. |
| 2525 | * @param showToast show "Message discarded" toast if true |
| 2526 | */ |
| 2527 | private void doDiscardWithoutConfirmation(boolean showToast) { |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 2528 | synchronized (mDraftLock) { |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2529 | if (mDraftId != UIProvider.INVALID_MESSAGE_ID) { |
| 2530 | ContentValues values = new ContentValues(); |
Paul Westbrook | b7050e6 | 2012-03-20 12:59:44 -0700 | [diff] [blame] | 2531 | values.put(BaseColumns._ID, mDraftId); |
Marc Blank | 78ea8e2 | 2012-08-04 11:14:06 -0700 | [diff] [blame] | 2532 | if (!mAccount.expungeMessageUri.equals(Uri.EMPTY)) { |
Mindy Pereira | cfb7f33 | 2012-02-28 10:23:43 -0800 | [diff] [blame] | 2533 | getContentResolver().update(mAccount.expungeMessageUri, values, null, null); |
| 2534 | } else { |
Marc Blank | 0bbc858 | 2012-04-23 15:07:57 -0700 | [diff] [blame] | 2535 | getContentResolver().delete(mDraft.uri, null, null); |
Mindy Pereira | cfb7f33 | 2012-02-28 10:23:43 -0800 | [diff] [blame] | 2536 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2537 | // This is not strictly necessary (since we should not try to |
| 2538 | // save the draft after calling this) but it ensures that if we |
| 2539 | // do save again for some reason we make a new draft rather than |
| 2540 | // trying to resave an expunged draft. |
| 2541 | mDraftId = UIProvider.INVALID_MESSAGE_ID; |
| 2542 | } |
| 2543 | } |
| 2544 | |
| 2545 | if (showToast) { |
| 2546 | // Display a toast to let the user know |
| 2547 | Toast.makeText(this, R.string.message_discarded, Toast.LENGTH_SHORT).show(); |
| 2548 | } |
| 2549 | |
| 2550 | // This prevents the draft from being saved in onPause(). |
| 2551 | discardChanges(); |
| 2552 | finish(); |
| 2553 | } |
| 2554 | |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 2555 | private void saveIfNeeded() { |
| 2556 | if (mAccount == null) { |
| 2557 | // We have not chosen an account yet so there's no way that we can save. This is ok, |
| 2558 | // though, since we are saving our state before AccountsActivity is activated. Thus, the |
| 2559 | // user has not interacted with us yet and there is no real state to save. |
| 2560 | return; |
| 2561 | } |
| 2562 | |
| 2563 | if (shouldSave()) { |
Mindy Pereira | 48e31b0 | 2012-05-30 13:12:24 -0700 | [diff] [blame] | 2564 | doSave(!mAddingAttachment /* show toast */); |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 2565 | } |
| 2566 | } |
| 2567 | |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 2568 | @Override |
| 2569 | public void onAttachmentDeleted() { |
| 2570 | mAttachmentsChanged = true; |
| 2571 | updateSaveUi(); |
| 2572 | } |
Mindy Pereira | 75f6663 | 2012-01-11 11:42:02 -0800 | [diff] [blame] | 2573 | |
| 2574 | |
| 2575 | /** |
| 2576 | * This is called any time one of our text fields changes. |
| 2577 | */ |
Marc Blank | 0bbc858 | 2012-04-23 15:07:57 -0700 | [diff] [blame] | 2578 | @Override |
Mindy Pereira | 75f6663 | 2012-01-11 11:42:02 -0800 | [diff] [blame] | 2579 | public void afterTextChanged(Editable s) { |
| 2580 | mTextChanged = true; |
| 2581 | updateSaveUi(); |
| 2582 | } |
| 2583 | |
| 2584 | @Override |
| 2585 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { |
| 2586 | // Do nothing. |
| 2587 | } |
| 2588 | |
Marc Blank | 0bbc858 | 2012-04-23 15:07:57 -0700 | [diff] [blame] | 2589 | @Override |
Mindy Pereira | 75f6663 | 2012-01-11 11:42:02 -0800 | [diff] [blame] | 2590 | public void onTextChanged(CharSequence s, int start, int before, int count) { |
| 2591 | // Do nothing. |
| 2592 | } |
| 2593 | |
| 2594 | |
| 2595 | // There is a big difference between the text associated with an address changing |
| 2596 | // to add the display name or to format properly and a recipient being added or deleted. |
| 2597 | // Make sure we only notify of changes when a recipient has been added or deleted. |
| 2598 | private class RecipientTextWatcher implements TextWatcher { |
| 2599 | private HashMap<String, Integer> mContent = new HashMap<String, Integer>(); |
| 2600 | |
| 2601 | private RecipientEditTextView mView; |
| 2602 | |
| 2603 | private TextWatcher mListener; |
| 2604 | |
| 2605 | public RecipientTextWatcher(RecipientEditTextView view, TextWatcher listener) { |
| 2606 | mView = view; |
| 2607 | mListener = listener; |
| 2608 | } |
| 2609 | |
| 2610 | @Override |
| 2611 | public void afterTextChanged(Editable s) { |
| 2612 | if (hasChanged()) { |
| 2613 | mListener.afterTextChanged(s); |
| 2614 | } |
| 2615 | } |
| 2616 | |
| 2617 | private boolean hasChanged() { |
| 2618 | String[] currRecips = tokenizeRecips(getAddressesFromList(mView)); |
| 2619 | int totalCount = currRecips.length; |
| 2620 | int totalPrevCount = 0; |
| 2621 | for (Entry<String, Integer> entry : mContent.entrySet()) { |
| 2622 | totalPrevCount += entry.getValue(); |
| 2623 | } |
| 2624 | if (totalCount != totalPrevCount) { |
| 2625 | return true; |
| 2626 | } |
| 2627 | |
| 2628 | for (String recip : currRecips) { |
| 2629 | if (!mContent.containsKey(recip)) { |
| 2630 | return true; |
| 2631 | } else { |
| 2632 | int count = mContent.get(recip) - 1; |
| 2633 | if (count < 0) { |
| 2634 | return true; |
| 2635 | } else { |
| 2636 | mContent.put(recip, count); |
| 2637 | } |
| 2638 | } |
| 2639 | } |
| 2640 | return false; |
| 2641 | } |
| 2642 | |
| 2643 | private String[] tokenizeRecips(String[] recips) { |
| 2644 | // Tokenize them all and put them in the list. |
| 2645 | String[] recipAddresses = new String[recips.length]; |
| 2646 | for (int i = 0; i < recips.length; i++) { |
| 2647 | recipAddresses[i] = Rfc822Tokenizer.tokenize(recips[i])[0].getAddress(); |
| 2648 | } |
| 2649 | return recipAddresses; |
| 2650 | } |
| 2651 | |
| 2652 | @Override |
| 2653 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { |
| 2654 | String[] recips = tokenizeRecips(getAddressesFromList(mView)); |
| 2655 | for (String recip : recips) { |
| 2656 | if (!mContent.containsKey(recip)) { |
| 2657 | mContent.put(recip, 1); |
| 2658 | } else { |
| 2659 | mContent.put(recip, (mContent.get(recip)) + 1); |
| 2660 | } |
| 2661 | } |
| 2662 | } |
| 2663 | |
| 2664 | @Override |
| 2665 | public void onTextChanged(CharSequence s, int start, int before, int count) { |
| 2666 | // Do nothing. |
| 2667 | } |
| 2668 | } |
Mindy Pereira | e011b1d | 2012-06-18 13:45:26 -0700 | [diff] [blame] | 2669 | |
| 2670 | public static void registerTestSendOrSaveCallback(SendOrSaveCallback testCallback) { |
| 2671 | if (sTestSendOrSaveCallback != null && testCallback != null) { |
| 2672 | throw new IllegalStateException("Attempting to register more than one test callback"); |
| 2673 | } |
| 2674 | sTestSendOrSaveCallback = testCallback; |
| 2675 | } |
Mindy Pereira | bddd6f3 | 2012-06-20 12:10:03 -0700 | [diff] [blame] | 2676 | |
| 2677 | @VisibleForTesting |
| 2678 | protected ArrayList<Attachment> getAttachments() { |
| 2679 | return mAttachmentsView.getAttachments(); |
| 2680 | } |
Mindy Pereira | 96a7f7a | 2012-07-09 16:51:06 -0700 | [diff] [blame] | 2681 | |
| 2682 | @Override |
| 2683 | public Loader<Cursor> onCreateLoader(int id, Bundle args) { |
| 2684 | switch (id) { |
| 2685 | case REFERENCE_MESSAGE_LOADER: |
| 2686 | return new CursorLoader(this, mRefMessageUri, UIProvider.MESSAGE_PROJECTION, null, |
| 2687 | null, null); |
| 2688 | } |
| 2689 | return null; |
| 2690 | } |
| 2691 | |
| 2692 | @Override |
| 2693 | public void onLoadFinished(Loader<Cursor> loader, Cursor data) { |
| 2694 | if (data != null && data.moveToFirst()) { |
| 2695 | mRefMessage = new Message(data); |
| 2696 | // We set these based on EXTRA_TO. |
| 2697 | mRefMessage.to = null; |
| 2698 | mRefMessage.from = null; |
| 2699 | Intent intent = getIntent(); |
| 2700 | int action = intent.getIntExtra(EXTRA_ACTION, COMPOSE); |
| 2701 | initFromRefMessage(action, mAccount.name); |
| 2702 | finishSetup(action, intent, null, true); |
| 2703 | if (action != FORWARD) { |
| 2704 | String to = intent.getStringExtra(EXTRA_TO); |
| 2705 | if (!TextUtils.isEmpty(to)) { |
| 2706 | clearChangeListeners(); |
| 2707 | mTo.append(to); |
| 2708 | initChangeListeners(); |
| 2709 | } |
| 2710 | } |
| 2711 | } else { |
| 2712 | finish(); |
| 2713 | } |
| 2714 | } |
| 2715 | |
| 2716 | @Override |
| 2717 | public void onLoaderReset(Loader<Cursor> arg0) { |
| 2718 | // Do nothing. |
| 2719 | } |
| 2720 | } |