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