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; |
Tony Mantler | 2558b50 | 2013-07-09 10:53:34 -0700 | [diff] [blame] | 25 | import android.app.DialogFragment; |
Mindy Pereira | b199d17 | 2012-08-13 11:04:03 -0700 | [diff] [blame] | 26 | import android.app.Fragment; |
Mindy Pereira | b199d17 | 2012-08-13 11:04:03 -0700 | [diff] [blame] | 27 | import android.app.FragmentTransaction; |
Mindy Pereira | 96a7f7a | 2012-07-09 16:51:06 -0700 | [diff] [blame] | 28 | import android.app.LoaderManager; |
Andrew Sapperstein | 05089f3 | 2013-10-01 17:00:03 -0700 | [diff] [blame] | 29 | import android.content.ClipData; |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 30 | import android.content.ContentResolver; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 31 | import android.content.ContentValues; |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 32 | import android.content.Context; |
Mindy Pereira | 96a7f7a | 2012-07-09 16:51:06 -0700 | [diff] [blame] | 33 | import android.content.CursorLoader; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 34 | import android.content.DialogInterface; |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 35 | import android.content.Intent; |
Mindy Pereira | 96a7f7a | 2012-07-09 16:51:06 -0700 | [diff] [blame] | 36 | import android.content.Loader; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 37 | import android.content.pm.ActivityInfo; |
Andrew Sapperstein | bc5e0dc | 2013-08-07 21:15:22 -0700 | [diff] [blame] | 38 | import android.content.res.Resources; |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 39 | import android.database.Cursor; |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 40 | import android.net.Uri; |
Andrew Sapperstein | 05089f3 | 2013-10-01 17:00:03 -0700 | [diff] [blame] | 41 | import android.os.Build; |
Mindy Pereira | 8e9305e | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 42 | import android.os.Bundle; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 43 | import android.os.Handler; |
| 44 | import android.os.HandlerThread; |
Paul Westbrook | 3c7f94d | 2012-10-23 14:13:00 -0700 | [diff] [blame] | 45 | import android.os.ParcelFileDescriptor; |
Alice Yang | 1ebc2db | 2013-03-14 21:21:44 -0700 | [diff] [blame] | 46 | import android.os.Parcelable; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 47 | import android.provider.BaseColumns; |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 48 | import android.text.Editable; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 49 | import android.text.Html; |
mindyp | ed9c2f0 | 2012-10-12 10:02:08 -0700 | [diff] [blame] | 50 | import android.text.SpannableString; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 51 | import android.text.Spanned; |
Paul Westbrook | c182762 | 2012-01-06 11:27:12 -0800 | [diff] [blame] | 52 | import android.text.TextUtils; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 53 | import android.text.TextWatcher; |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 54 | import android.text.util.Rfc822Token; |
Mindy Pereira | c17d073 | 2011-12-29 10:46:19 -0800 | [diff] [blame] | 55 | import android.text.util.Rfc822Tokenizer; |
Mindy Pereira | 3cd4f40 | 2012-07-17 11:16:18 -0700 | [diff] [blame] | 56 | import android.view.Gravity; |
mindyp | 62d3ec7 | 2012-08-24 13:04:09 -0700 | [diff] [blame] | 57 | import android.view.KeyEvent; |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 58 | import android.view.LayoutInflater; |
Mindy Pereira | b47f3e2 | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 59 | import android.view.Menu; |
| 60 | import android.view.MenuInflater; |
| 61 | import android.view.MenuItem; |
Mindy Pereira | 8e9305e | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 62 | import android.view.View; |
| 63 | import android.view.View.OnClickListener; |
Andy Huang | 5c5fd57 | 2012-04-08 18:19:29 -0700 | [diff] [blame] | 64 | import android.view.ViewGroup; |
Paul Westbrook | b4931c6 | 2013-01-14 17:51:18 -0800 | [diff] [blame] | 65 | import android.view.inputmethod.BaseInputConnection; |
mindyp | 62d3ec7 | 2012-08-24 13:04:09 -0700 | [diff] [blame] | 66 | import android.view.inputmethod.EditorInfo; |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 67 | import android.widget.ArrayAdapter; |
Mindy Pereira | 8e9305e | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 68 | import android.widget.Button; |
Mindy Pereira | 433b198 | 2012-04-03 11:53:07 -0700 | [diff] [blame] | 69 | import android.widget.EditText; |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 70 | import android.widget.TextView; |
Mindy Pereira | 013194c | 2012-01-06 15:09:33 -0800 | [diff] [blame] | 71 | import android.widget.Toast; |
Mindy Pereira | 7b56a61 | 2011-12-14 12:32:28 -0800 | [diff] [blame] | 72 | |
Mindy Pereira | c17d073 | 2011-12-29 10:46:19 -0800 | [diff] [blame] | 73 | import com.android.common.Rfc822Validator; |
Tony Mantler | 9f32423 | 2013-08-08 14:24:30 -0700 | [diff] [blame] | 74 | import com.android.common.contacts.DataUsageStatUpdater; |
Tony Mantler | 821e578 | 2014-01-06 15:33:43 -0800 | [diff] [blame] | 75 | import com.android.emailcommon.mail.Address; |
Andy Huang | 5c5fd57 | 2012-04-08 18:19:29 -0700 | [diff] [blame] | 76 | import com.android.ex.chips.RecipientEditTextView; |
Scott Kennedy | 5680ec2 | 2013-01-07 13:15:20 -0800 | [diff] [blame] | 77 | import com.android.mail.MailIntentService; |
Andy Huang | 5c5fd57 | 2012-04-08 18:19:29 -0700 | [diff] [blame] | 78 | import com.android.mail.R; |
Andy Huang | 761522c | 2013-08-08 13:09:11 -0700 | [diff] [blame] | 79 | import com.android.mail.analytics.Analytics; |
Alice Yang | 1ebc2db | 2013-03-14 21:21:44 -0700 | [diff] [blame] | 80 | import com.android.mail.browse.MessageHeaderView; |
mindyp | 4088243 | 2012-09-06 11:07:40 -0700 | [diff] [blame] | 81 | import com.android.mail.compose.AttachmentsView.AttachmentAddedOrDeletedListener; |
Mindy Pereira | 9932dee | 2012-01-10 16:09:50 -0800 | [diff] [blame] | 82 | import com.android.mail.compose.AttachmentsView.AttachmentFailureException; |
Mindy Pereira | 5a85e2b | 2012-01-11 09:53:32 -0800 | [diff] [blame] | 83 | import com.android.mail.compose.FromAddressSpinner.OnAccountChangedListener; |
Andy Huang | 30e2c24 | 2012-01-06 18:14:30 -0800 | [diff] [blame] | 84 | import com.android.mail.compose.QuotedTextView.RespondInlineListener; |
Mindy Pereira | 33fe908 | 2012-01-09 16:24:30 -0800 | [diff] [blame] | 85 | import com.android.mail.providers.Account; |
Andy Huang | 30e2c24 | 2012-01-06 18:14:30 -0800 | [diff] [blame] | 86 | import com.android.mail.providers.Attachment; |
Scott Kennedy | 5680ec2 | 2013-01-07 13:15:20 -0800 | [diff] [blame] | 87 | import com.android.mail.providers.Folder; |
Mindy Pereira | 47d0e65 | 2012-07-23 09:45:07 -0700 | [diff] [blame] | 88 | import com.android.mail.providers.MailAppProvider; |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 89 | import com.android.mail.providers.Message; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 90 | import com.android.mail.providers.MessageModification; |
Mindy Pereira | 92551d0 | 2012-04-05 11:31:12 -0700 | [diff] [blame] | 91 | import com.android.mail.providers.ReplyFromAccount; |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 92 | import com.android.mail.providers.Settings; |
Andy Huang | 30e2c24 | 2012-01-06 18:14:30 -0800 | [diff] [blame] | 93 | import com.android.mail.providers.UIProvider; |
Mindy Pereira | 3ca5bad | 2012-04-16 11:02:42 -0700 | [diff] [blame] | 94 | import com.android.mail.providers.UIProvider.AccountCapabilities; |
Mindy Pereira | 1257586 | 2012-03-21 16:30:54 -0700 | [diff] [blame] | 95 | import com.android.mail.providers.UIProvider.DraftType; |
Alice Yang | 1ebc2db | 2013-03-14 21:21:44 -0700 | [diff] [blame] | 96 | import com.android.mail.ui.AttachmentTile.AttachmentPreview; |
Paul Westbrook | 83e6b57 | 2013-02-05 16:22:42 -0800 | [diff] [blame] | 97 | import com.android.mail.ui.FeedbackEnabledActivity; |
Mindy Pereira | fa20c1a | 2012-07-23 13:00:02 -0700 | [diff] [blame] | 98 | import com.android.mail.ui.MailActivity; |
Mindy Pereira | b199d17 | 2012-08-13 11:04:03 -0700 | [diff] [blame] | 99 | import com.android.mail.ui.WaitFragment; |
Paul Westbrook | 92227f6 | 2012-03-20 10:32:51 -0700 | [diff] [blame] | 100 | import com.android.mail.utils.AccountUtils; |
Mark Wei | 434f294 | 2012-08-24 11:54:02 -0700 | [diff] [blame] | 101 | import com.android.mail.utils.AttachmentUtils; |
mindyp | febd226 | 2012-11-13 17:45:09 -0800 | [diff] [blame] | 102 | import com.android.mail.utils.ContentProviderTask; |
Paul Westbrook | b334c90 | 2012-06-25 11:42:46 -0700 | [diff] [blame] | 103 | import com.android.mail.utils.LogTag; |
Andy Huang | 30e2c24 | 2012-01-06 18:14:30 -0800 | [diff] [blame] | 104 | import com.android.mail.utils.LogUtils; |
Andy Huang | 30e2c24 | 2012-01-06 18:14:30 -0800 | [diff] [blame] | 105 | import com.android.mail.utils.Utils; |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 106 | import com.google.common.annotations.VisibleForTesting; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 107 | import com.google.common.collect.Lists; |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 108 | import com.google.common.collect.Sets; |
Mindy Pereira | 8e9305e | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 109 | |
Paul Westbrook | 3c7f94d | 2012-10-23 14:13:00 -0700 | [diff] [blame] | 110 | import java.io.FileNotFoundException; |
| 111 | import java.io.IOException; |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 112 | import java.io.UnsupportedEncodingException; |
| 113 | import java.net.URLDecoder; |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 114 | import java.util.ArrayList; |
Paul Westbrook | bb87b7f | 2012-03-20 16:20:07 -0700 | [diff] [blame] | 115 | import java.util.Arrays; |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 116 | import java.util.Collection; |
Mindy Pereira | 75f6663 | 2012-01-11 11:42:02 -0800 | [diff] [blame] | 117 | import java.util.HashMap; |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 118 | import java.util.HashSet; |
| 119 | import java.util.List; |
Paul Westbrook | 1c078cf | 2012-03-20 16:18:51 -0700 | [diff] [blame] | 120 | import java.util.Map.Entry; |
Paul Westbrook | bb87b7f | 2012-03-20 16:20:07 -0700 | [diff] [blame] | 121 | import java.util.Set; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 122 | import java.util.concurrent.ConcurrentHashMap; |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 123 | |
| 124 | public class ComposeActivity extends Activity implements OnClickListener, OnNavigationListener, |
Tony Mantler | 2558b50 | 2013-07-09 10:53:34 -0700 | [diff] [blame] | 125 | RespondInlineListener, TextWatcher, |
Alice Yang | a990a71 | 2013-03-13 18:37:00 -0700 | [diff] [blame] | 126 | AttachmentAddedOrDeletedListener, OnAccountChangedListener, |
| 127 | LoaderManager.LoaderCallbacks<Cursor>, TextView.OnEditorActionListener, |
| 128 | FeedbackEnabledActivity { |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 129 | // Identifiers for which type of composition this is |
Andrew Sapperstein | bc5e0dc | 2013-08-07 21:15:22 -0700 | [diff] [blame] | 130 | public static final int COMPOSE = -1; |
| 131 | public static final int REPLY = 0; |
| 132 | public static final int REPLY_ALL = 1; |
| 133 | public static final int FORWARD = 2; |
| 134 | public static final int EDIT_DRAFT = 3; |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 135 | |
| 136 | // Integer extra holding one of the above compose action |
Mindy Pereira | 96a7f7a | 2012-07-09 16:51:06 -0700 | [diff] [blame] | 137 | protected static final String EXTRA_ACTION = "action"; |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 138 | |
Mindy Pereira | 326689d | 2012-05-17 10:14:14 -0700 | [diff] [blame] | 139 | private static final String EXTRA_SHOW_CC = "showCc"; |
| 140 | private static final String EXTRA_SHOW_BCC = "showBcc"; |
mindyp | 1623f9b | 2012-11-21 12:41:16 -0800 | [diff] [blame] | 141 | private static final String EXTRA_RESPONDED_INLINE = "respondedInline"; |
mindyp | 1d7e914 | 2012-11-21 13:54:30 -0800 | [diff] [blame] | 142 | private static final String EXTRA_SAVE_ENABLED = "saveEnabled"; |
Mindy Pereira | a34c9a0 | 2012-04-17 14:10:53 -0700 | [diff] [blame] | 143 | |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 144 | private static final String UTF8_ENCODING_NAME = "UTF-8"; |
| 145 | |
| 146 | private static final String MAIL_TO = "mailto"; |
| 147 | |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 148 | private static final String EXTRA_SUBJECT = "subject"; |
| 149 | |
| 150 | private static final String EXTRA_BODY = "body"; |
| 151 | |
Andrew Sapperstein | bc5e0dc | 2013-08-07 21:15:22 -0700 | [diff] [blame] | 152 | /** |
| 153 | * Expected to be html formatted text. |
| 154 | */ |
| 155 | private static final String EXTRA_QUOTED_TEXT = "quotedText"; |
| 156 | |
mindyp | d27b6ea | 2012-10-05 09:43:49 -0700 | [diff] [blame] | 157 | protected static final String EXTRA_FROM_ACCOUNT_STRING = "fromAccountString"; |
Mindy Pereira | 9a42bb4 | 2012-04-18 15:21:33 -0700 | [diff] [blame] | 158 | |
Mark Wei | 62066e4 | 2012-09-13 12:07:02 -0700 | [diff] [blame] | 159 | private static final String EXTRA_ATTACHMENT_PREVIEWS = "attachmentPreviews"; |
| 160 | |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 161 | // Extra that we can get passed from other activities |
Tony Mantler | 184ec73 | 2013-10-24 13:13:49 -0700 | [diff] [blame] | 162 | @VisibleForTesting |
| 163 | protected static final String EXTRA_TO = "to"; |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 164 | private static final String EXTRA_CC = "cc"; |
| 165 | private static final String EXTRA_BCC = "bcc"; |
| 166 | |
Scott Kennedy | 6084725 | 2013-08-15 15:55:42 -0700 | [diff] [blame] | 167 | /** |
| 168 | * An optional extra containing a {@link ContentValues} of values to be added to |
| 169 | * {@link SendOrSaveMessage#mValues}. |
| 170 | */ |
| 171 | public static final String EXTRA_VALUES = "extra-values"; |
| 172 | |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 173 | // List of all the fields |
Andrew Sapperstein | bc5e0dc | 2013-08-07 21:15:22 -0700 | [diff] [blame] | 174 | static final String[] ALL_EXTRAS = { EXTRA_SUBJECT, EXTRA_BODY, EXTRA_TO, EXTRA_CC, EXTRA_BCC, |
| 175 | EXTRA_QUOTED_TEXT }; |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 176 | |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 177 | private static SendOrSaveCallback sTestSendOrSaveCallback = null; |
| 178 | // Map containing information about requests to create new messages, and the id of the |
| 179 | // messages that were the result of those requests. |
| 180 | // |
| 181 | // This map is used when the activity that initiated the save a of a new message, is killed |
| 182 | // before the save has completed (and when we know the id of the newly created message). When |
| 183 | // a save is completed, the service that is running in the background, will update the map |
| 184 | // |
| 185 | // When a new ComposeActivity instance is created, it will attempt to use the information in |
| 186 | // the previously instantiated map. If ComposeActivity.onCreate() is called, with a bundle |
| 187 | // (restoring data from a previous instance), and the map hasn't been created, we will attempt |
| 188 | // to populate the map with data stored in shared preferences. |
Andy Huang | 1f8f4dd | 2012-10-25 21:35:35 -0700 | [diff] [blame] | 189 | // FIXME: values in this map are never read. |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 190 | private static ConcurrentHashMap<Integer, Long> sRequestMessageIdMap = null; |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 191 | /** |
| 192 | * Notifies the {@code Activity} that the caller is an Email |
| 193 | * {@code Activity}, so that the back behavior may be modified accordingly. |
| 194 | * |
| 195 | * @see #onAppUpPressed |
| 196 | */ |
Paul Westbrook | daecb4b | 2012-05-31 10:21:26 -0700 | [diff] [blame] | 197 | public static final String EXTRA_FROM_EMAIL_TASK = "fromemail"; |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 198 | |
Mindy Pereira | e011b1d | 2012-06-18 13:45:26 -0700 | [diff] [blame] | 199 | public static final String EXTRA_ATTACHMENTS = "attachments"; |
Paul Westbrook | f97588b | 2012-03-20 11:11:37 -0700 | [diff] [blame] | 200 | |
Scott Kennedy | 5680ec2 | 2013-01-07 13:15:20 -0800 | [diff] [blame] | 201 | /** If set, we will clear notifications for this folder. */ |
| 202 | public static final String EXTRA_NOTIFICATION_FOLDER = "extra-notification-folder"; |
| 203 | |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 204 | // 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] | 205 | 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] | 206 | // If this is a reply/forward then this extra will hold a uri we must query |
| 207 | // to get the original message. |
| 208 | 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] | 209 | // 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] | 210 | // draft message |
| 211 | private static final String ORIGINAL_DRAFT_MESSAGE = "original-draft-message"; |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 212 | private static final String END_TOKEN = ", "; |
Paul Westbrook | b334c90 | 2012-06-25 11:42:46 -0700 | [diff] [blame] | 213 | private static final String LOG_TAG = LogTag.getLogTag(); |
Mindy Pereira | 013194c | 2012-01-06 15:09:33 -0800 | [diff] [blame] | 214 | // Request numbers for activities we start |
| 215 | private static final int RESULT_PICK_ATTACHMENT = 1; |
| 216 | private static final int RESULT_CREATE_ACCOUNT = 2; |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 217 | // TODO(mindyp) set mime-type for auto send? |
Mindy Pereira | e011b1d | 2012-06-18 13:45:26 -0700 | [diff] [blame] | 218 | 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] | 219 | |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 220 | private static final String EXTRA_SELECTED_REPLY_FROM_ACCOUNT = "replyFromAccount"; |
| 221 | private static final String EXTRA_REQUEST_ID = "requestId"; |
| 222 | private static final String EXTRA_FOCUS_SELECTION_START = "focusSelectionStart"; |
Paul Westbrook | 176a199 | 2013-07-22 13:57:19 -0700 | [diff] [blame] | 223 | private static final String EXTRA_FOCUS_SELECTION_END = "focusSelectionEnd"; |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 224 | private static final String EXTRA_MESSAGE = "extraMessage"; |
Mindy Pereira | 96a7f7a | 2012-07-09 16:51:06 -0700 | [diff] [blame] | 225 | private static final int REFERENCE_MESSAGE_LOADER = 0; |
Mindy Pereira | b199d17 | 2012-08-13 11:04:03 -0700 | [diff] [blame] | 226 | private static final int LOADER_ACCOUNT_CURSOR = 1; |
Alice Yang | a990a71 | 2013-03-13 18:37:00 -0700 | [diff] [blame] | 227 | private static final int INIT_DRAFT_USING_REFERENCE_MESSAGE = 2; |
Mindy Pereira | 47d0e65 | 2012-07-23 09:45:07 -0700 | [diff] [blame] | 228 | private static final String EXTRA_SELECTED_ACCOUNT = "selectedAccount"; |
Mindy Pereira | b199d17 | 2012-08-13 11:04:03 -0700 | [diff] [blame] | 229 | private static final String TAG_WAIT = "wait-fragment"; |
Andrew Sapperstein | 5cb7180 | 2013-10-01 18:31:20 -0700 | [diff] [blame] | 230 | private static final String MIME_TYPE_ALL = "*/*"; |
Mindy Pereira | 2db7d4a | 2012-08-15 11:00:02 -0700 | [diff] [blame] | 231 | private static final String MIME_TYPE_PHOTO = "image/*"; |
Mindy Pereira | 8e9305e | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 232 | |
Andy Huang | 9f855d6 | 2013-05-30 17:15:03 -0700 | [diff] [blame] | 233 | private static final String KEY_INNER_SAVED_STATE = "compose_state"; |
| 234 | |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 235 | /** |
| 236 | * A single thread for running tasks in the background. |
| 237 | */ |
| 238 | private Handler mSendSaveTaskHandler = null; |
Mindy Pereira | c17d073 | 2011-12-29 10:46:19 -0800 | [diff] [blame] | 239 | private RecipientEditTextView mTo; |
| 240 | private RecipientEditTextView mCc; |
| 241 | private RecipientEditTextView mBcc; |
Mindy Pereira | 8e9305e | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 242 | private Button mCcBccButton; |
| 243 | private CcBccView mCcBccView; |
Mindy Pereira | 7b56a61 | 2011-12-14 12:32:28 -0800 | [diff] [blame] | 244 | private AttachmentsView mAttachmentsView; |
Andrew Sapperstein | 746d861 | 2013-08-26 15:56:32 -0700 | [diff] [blame] | 245 | protected Account mAccount; |
Tony Mantler | 59e6909 | 2013-08-14 11:05:00 -0700 | [diff] [blame] | 246 | protected ReplyFromAccount mReplyFromAccount; |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 247 | private Settings mCachedSettings; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 248 | private Rfc822Validator mValidator; |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 249 | private TextView mSubject; |
| 250 | |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 251 | private ComposeModeAdapter mComposeModeAdapter; |
Andrew Sapperstein | 746d861 | 2013-08-26 15:56:32 -0700 | [diff] [blame] | 252 | protected int mComposeMode = -1; |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 253 | private boolean mForward; |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 254 | private QuotedTextView mQuotedTextView; |
Tony Mantler | 59e6909 | 2013-08-14 11:05:00 -0700 | [diff] [blame] | 255 | protected EditText mBodyView; |
Mindy Pereira | 1a95a57 | 2012-01-05 12:21:29 -0800 | [diff] [blame] | 256 | private View mFromStatic; |
Mindy Pereira | 2eb1732 | 2012-03-07 10:07:34 -0800 | [diff] [blame] | 257 | private TextView mFromStaticText; |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 258 | private View mFromSpinnerWrapper; |
Mindy Pereira | 1883b34 | 2012-06-20 08:34:56 -0700 | [diff] [blame] | 259 | @VisibleForTesting |
| 260 | protected FromAddressSpinner mFromSpinner; |
Mindy Pereira | 013194c | 2012-01-06 15:09:33 -0800 | [diff] [blame] | 261 | private boolean mAddingAttachment; |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 262 | private boolean mAttachmentsChanged; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 263 | private boolean mTextChanged; |
| 264 | private boolean mReplyFromChanged; |
| 265 | private MenuItem mSave; |
Mindy Pereira | b3112a2 | 2012-06-20 12:10:03 -0700 | [diff] [blame] | 266 | @VisibleForTesting |
| 267 | protected Message mRefMessage; |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 268 | private long mDraftId = UIProvider.INVALID_MESSAGE_ID; |
| 269 | private Message mDraft; |
mindyp | 44a6339 | 2012-11-05 12:05:16 -0800 | [diff] [blame] | 270 | private ReplyFromAccount mDraftAccount; |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 271 | private Object mDraftLock = new Object(); |
Andrew Sapperstein | 6aea786 | 2013-10-24 19:59:51 -0700 | [diff] [blame] | 272 | private View mAddAttachmentsButton; |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 273 | |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 274 | /** |
Paul Westbrook | daecb4b | 2012-05-31 10:21:26 -0700 | [diff] [blame] | 275 | * Boolean indicating whether ComposeActivity was launched from a Gmail controlled view. |
| 276 | */ |
| 277 | private boolean mLaunchedFromEmail = false; |
Mindy Pereira | cbfb75a | 2012-06-25 14:52:23 -0700 | [diff] [blame] | 278 | private RecipientTextWatcher mToListener; |
| 279 | private RecipientTextWatcher mCcListener; |
| 280 | private RecipientTextWatcher mBccListener; |
Mindy Pereira | b18e5a9 | 2012-07-10 11:47:21 -0700 | [diff] [blame] | 281 | private Uri mRefMessageUri; |
Alice Yang | a990a71 | 2013-03-13 18:37:00 -0700 | [diff] [blame] | 282 | private boolean mShowQuotedText = false; |
Andrew Sapperstein | 746d861 | 2013-08-26 15:56:32 -0700 | [diff] [blame] | 283 | protected Bundle mInnerSavedState; |
Scott Kennedy | 6084725 | 2013-08-15 15:55:42 -0700 | [diff] [blame] | 284 | private ContentValues mExtraValues = null; |
Paul Westbrook | daecb4b | 2012-05-31 10:21:26 -0700 | [diff] [blame] | 285 | |
mindyp | 1623f9b | 2012-11-21 12:41:16 -0800 | [diff] [blame] | 286 | // Array of the outstanding send or save tasks. Access is synchronized |
| 287 | // with the object itself |
| 288 | /* package for testing */ |
| 289 | @VisibleForTesting |
| 290 | public ArrayList<SendOrSaveTask> mActiveTasks = Lists.newArrayList(); |
| 291 | // FIXME: this variable is never read. related to sRequestMessageIdMap. |
| 292 | private int mRequestId; |
| 293 | private String mSignature; |
| 294 | private Account[] mAccounts; |
| 295 | private boolean mRespondedInline; |
Andy Huang | dc97bf4 | 2013-08-15 16:52:45 -0700 | [diff] [blame] | 296 | private boolean mPerformedSendOrDiscard = false; |
mindyp | 1623f9b | 2012-11-21 12:41:16 -0800 | [diff] [blame] | 297 | |
Paul Westbrook | daecb4b | 2012-05-31 10:21:26 -0700 | [diff] [blame] | 298 | /** |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 299 | * Can be called from a non-UI thread. |
| 300 | */ |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 301 | public static void editDraft(Context launcher, Account account, Message message) { |
Scott Kennedy | 6084725 | 2013-08-15 15:55:42 -0700 | [diff] [blame] | 302 | launch(launcher, account, message, EDIT_DRAFT, null, null, null, null, |
| 303 | null /* extraValues */); |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 304 | } |
| 305 | |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 306 | /** |
| 307 | * Can be called from a non-UI thread. |
| 308 | */ |
Mindy Pereira | 33fe908 | 2012-01-09 16:24:30 -0800 | [diff] [blame] | 309 | public static void compose(Context launcher, Account account) { |
Scott Kennedy | 6084725 | 2013-08-15 15:55:42 -0700 | [diff] [blame] | 310 | launch(launcher, account, null, COMPOSE, null, null, null, null, null /* extraValues */); |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Can be called from a non-UI thread. |
| 315 | */ |
Andrew Sapperstein | 3de76ec | 2013-07-16 12:08:15 -0700 | [diff] [blame] | 316 | public static void composeToAddress(Context launcher, Account account, String toAddress) { |
Scott Kennedy | 6084725 | 2013-08-15 15:55:42 -0700 | [diff] [blame] | 317 | launch(launcher, account, null, COMPOSE, toAddress, null, null, null, |
| 318 | null /* extraValues */); |
Andrew Sapperstein | bc5e0dc | 2013-08-07 21:15:22 -0700 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Can be called from a non-UI thread. |
| 323 | */ |
| 324 | public static void composeWithQuotedText(Context launcher, Account account, |
Scott Kennedy | 6084725 | 2013-08-15 15:55:42 -0700 | [diff] [blame] | 325 | String quotedText, String subject, final ContentValues extraValues) { |
| 326 | launch(launcher, account, null, COMPOSE, null, null, quotedText, subject, extraValues); |
Andrew Sapperstein | 3de76ec | 2013-07-16 12:08:15 -0700 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | /** |
| 330 | * Can be called from a non-UI thread. |
| 331 | */ |
Andrew Sapperstein | 746d861 | 2013-08-26 15:56:32 -0700 | [diff] [blame] | 332 | public static void composeWithExtraValues(Context launcher, Account account, |
| 333 | String subject, final ContentValues extraValues) { |
| 334 | launch(launcher, account, null, COMPOSE, null, null, null, subject, extraValues); |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * Can be called from a non-UI thread. |
| 339 | */ |
Scott Kennedy | 0aeaf7d | 2012-11-14 18:56:05 -0800 | [diff] [blame] | 340 | public static Intent createReplyIntent(final Context launcher, final Account account, |
| 341 | final Uri messageUri, final boolean isReplyAll) { |
| 342 | return createActionIntent(launcher, account, messageUri, isReplyAll ? REPLY_ALL : REPLY); |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * Can be called from a non-UI thread. |
| 347 | */ |
| 348 | public static Intent createForwardIntent(final Context launcher, final Account account, |
| 349 | final Uri messageUri) { |
| 350 | return createActionIntent(launcher, account, messageUri, FORWARD); |
| 351 | } |
| 352 | |
| 353 | private static Intent createActionIntent(final Context launcher, final Account account, |
| 354 | final Uri messageUri, final int action) { |
| 355 | final Intent intent = new Intent(launcher, ComposeActivity.class); |
| 356 | |
Paul Westbrook | 6d2442b | 2013-07-17 17:51:51 -0700 | [diff] [blame] | 357 | updateActionIntent(account, messageUri, action, intent); |
| 358 | |
| 359 | return intent; |
| 360 | } |
| 361 | |
| 362 | @VisibleForTesting |
| 363 | static Intent updateActionIntent(Account account, Uri messageUri, int action, Intent intent) { |
Scott Kennedy | 0aeaf7d | 2012-11-14 18:56:05 -0800 | [diff] [blame] | 364 | intent.putExtra(EXTRA_FROM_EMAIL_TASK, true); |
| 365 | intent.putExtra(EXTRA_ACTION, action); |
| 366 | intent.putExtra(Utils.EXTRA_ACCOUNT, account); |
| 367 | intent.putExtra(EXTRA_IN_REFERENCE_TO_MESSAGE_URI, messageUri); |
| 368 | |
| 369 | return intent; |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * Can be called from a non-UI thread. |
| 374 | */ |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 375 | public static void reply(Context launcher, Account account, Message message) { |
Scott Kennedy | 6084725 | 2013-08-15 15:55:42 -0700 | [diff] [blame] | 376 | launch(launcher, account, message, REPLY, null, null, null, null, null /* extraValues */); |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | /** |
| 380 | * Can be called from a non-UI thread. |
| 381 | */ |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 382 | public static void replyAll(Context launcher, Account account, Message message) { |
Scott Kennedy | 6084725 | 2013-08-15 15:55:42 -0700 | [diff] [blame] | 383 | launch(launcher, account, message, REPLY_ALL, null, null, null, null, |
| 384 | null /* extraValues */); |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | /** |
| 388 | * Can be called from a non-UI thread. |
| 389 | */ |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 390 | public static void forward(Context launcher, Account account, Message message) { |
Scott Kennedy | 6084725 | 2013-08-15 15:55:42 -0700 | [diff] [blame] | 391 | launch(launcher, account, message, FORWARD, null, null, null, null, null /* extraValues */); |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 392 | } |
| 393 | |
Alice Yang | 1ebc2db | 2013-03-14 21:21:44 -0700 | [diff] [blame] | 394 | public static void reportRenderingFeedback(Context launcher, Account account, Message message, |
| 395 | String body) { |
Andrew Sapperstein | bc5e0dc | 2013-08-07 21:15:22 -0700 | [diff] [blame] | 396 | launch(launcher, account, message, FORWARD, |
Scott Kennedy | 6084725 | 2013-08-15 15:55:42 -0700 | [diff] [blame] | 397 | "android-gmail-readability@google.com", body, null, null, null /* extraValues */); |
Alice Yang | 1ebc2db | 2013-03-14 21:21:44 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | private static void launch(Context launcher, Account account, Message message, int action, |
Scott Kennedy | 6084725 | 2013-08-15 15:55:42 -0700 | [diff] [blame] | 401 | String toAddress, String body, String quotedText, String subject, |
| 402 | final ContentValues extraValues) { |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 403 | Intent intent = new Intent(launcher, ComposeActivity.class); |
| 404 | intent.putExtra(EXTRA_FROM_EMAIL_TASK, true); |
| 405 | intent.putExtra(EXTRA_ACTION, action); |
| 406 | intent.putExtra(Utils.EXTRA_ACCOUNT, account); |
Paul Westbrook | bb87b7f | 2012-03-20 16:20:07 -0700 | [diff] [blame] | 407 | if (action == EDIT_DRAFT) { |
| 408 | intent.putExtra(ORIGINAL_DRAFT_MESSAGE, message); |
| 409 | } else { |
| 410 | intent.putExtra(EXTRA_IN_REFERENCE_TO_MESSAGE, message); |
| 411 | } |
Alice Yang | 1ebc2db | 2013-03-14 21:21:44 -0700 | [diff] [blame] | 412 | if (toAddress != null) { |
| 413 | intent.putExtra(EXTRA_TO, toAddress); |
| 414 | } |
| 415 | if (body != null) { |
| 416 | intent.putExtra(EXTRA_BODY, body); |
| 417 | } |
Andrew Sapperstein | bc5e0dc | 2013-08-07 21:15:22 -0700 | [diff] [blame] | 418 | if (quotedText != null) { |
| 419 | intent.putExtra(EXTRA_QUOTED_TEXT, quotedText); |
| 420 | } |
| 421 | if (subject != null) { |
| 422 | intent.putExtra(EXTRA_SUBJECT, subject); |
| 423 | } |
Scott Kennedy | 6084725 | 2013-08-15 15:55:42 -0700 | [diff] [blame] | 424 | if (extraValues != null) { |
| 425 | LogUtils.d(LOG_TAG, "Launching with extraValues: %s", extraValues.toString()); |
| 426 | intent.putExtra(EXTRA_VALUES, extraValues); |
| 427 | } |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 428 | launcher.startActivity(intent); |
| 429 | } |
Mindy Pereira | 8e9305e | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 430 | |
Andy Huang | 0a2a346 | 2013-12-20 15:56:13 -0800 | [diff] [blame] | 431 | public static void composeMailto(Context launcher, Account account, Uri mailto) { |
| 432 | final Intent intent = new Intent(Intent.ACTION_VIEW, mailto, launcher, |
| 433 | ComposeActivity.class); |
| 434 | intent.putExtra(EXTRA_FROM_EMAIL_TASK, true); |
| 435 | intent.putExtra(Utils.EXTRA_ACCOUNT, account); |
| 436 | launcher.startActivity(intent); |
| 437 | } |
| 438 | |
Mindy Pereira | 8e9305e | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 439 | @Override |
Scott Kennedy | d906390 | 2013-08-02 22:14:37 -0700 | [diff] [blame] | 440 | protected void onCreate(Bundle savedInstanceState) { |
Mindy Pereira | 8e9305e | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 441 | super.onCreate(savedInstanceState); |
Mindy Pereira | 3528d36 | 2012-01-05 14:39:44 -0800 | [diff] [blame] | 442 | setContentView(R.layout.compose); |
Andy Huang | 9f855d6 | 2013-05-30 17:15:03 -0700 | [diff] [blame] | 443 | mInnerSavedState = (savedInstanceState != null) ? |
| 444 | savedInstanceState.getBundle(KEY_INNER_SAVED_STATE) : null; |
Mindy Pereira | b199d17 | 2012-08-13 11:04:03 -0700 | [diff] [blame] | 445 | checkValidAccounts(); |
| 446 | } |
| 447 | |
| 448 | private void finishCreate() { |
Andy Huang | 9f855d6 | 2013-05-30 17:15:03 -0700 | [diff] [blame] | 449 | final Bundle savedState = mInnerSavedState; |
Mindy Pereira | 3528d36 | 2012-01-05 14:39:44 -0800 | [diff] [blame] | 450 | findViews(); |
Mindy Pereira | 818143e | 2012-01-11 13:59:49 -0800 | [diff] [blame] | 451 | Intent intent = getIntent(); |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 452 | Message message; |
Mark Wei | 62066e4 | 2012-09-13 12:07:02 -0700 | [diff] [blame] | 453 | ArrayList<AttachmentPreview> previews; |
Alice Yang | a990a71 | 2013-03-13 18:37:00 -0700 | [diff] [blame] | 454 | mShowQuotedText = false; |
Andrew Sapperstein | bc5e0dc | 2013-08-07 21:15:22 -0700 | [diff] [blame] | 455 | CharSequence quotedText = null; |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 456 | int action; |
Mindy Pereira | 47d0e65 | 2012-07-23 09:45:07 -0700 | [diff] [blame] | 457 | // Check for any of the possibly supplied accounts.; |
| 458 | Account account = null; |
Andy Huang | 9f855d6 | 2013-05-30 17:15:03 -0700 | [diff] [blame] | 459 | if (hadSavedInstanceStateMessage(savedState)) { |
| 460 | action = savedState.getInt(EXTRA_ACTION, COMPOSE); |
| 461 | account = savedState.getParcelable(Utils.EXTRA_ACCOUNT); |
| 462 | message = (Message) savedState.getParcelable(EXTRA_MESSAGE); |
Mark Wei | 62066e4 | 2012-09-13 12:07:02 -0700 | [diff] [blame] | 463 | |
Andy Huang | 9f855d6 | 2013-05-30 17:15:03 -0700 | [diff] [blame] | 464 | previews = savedState.getParcelableArrayList(EXTRA_ATTACHMENT_PREVIEWS); |
| 465 | mRefMessage = (Message) savedState.getParcelable(EXTRA_IN_REFERENCE_TO_MESSAGE); |
Andrew Sapperstein | bc5e0dc | 2013-08-07 21:15:22 -0700 | [diff] [blame] | 466 | quotedText = savedState.getCharSequence(EXTRA_QUOTED_TEXT); |
Scott Kennedy | 44d4481 | 2013-08-19 14:18:31 -0700 | [diff] [blame] | 467 | |
| 468 | mExtraValues = savedState.getParcelable(EXTRA_VALUES); |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 469 | } else { |
Mindy Pereira | 47d0e65 | 2012-07-23 09:45:07 -0700 | [diff] [blame] | 470 | account = obtainAccount(intent); |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 471 | action = intent.getIntExtra(EXTRA_ACTION, COMPOSE); |
| 472 | // Initialize the message from the message in the intent |
| 473 | message = (Message) intent.getParcelableExtra(ORIGINAL_DRAFT_MESSAGE); |
Mark Wei | 62066e4 | 2012-09-13 12:07:02 -0700 | [diff] [blame] | 474 | previews = intent.getParcelableArrayListExtra(EXTRA_ATTACHMENT_PREVIEWS); |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 475 | mRefMessage = (Message) intent.getParcelableExtra(EXTRA_IN_REFERENCE_TO_MESSAGE); |
Mindy Pereira | b18e5a9 | 2012-07-10 11:47:21 -0700 | [diff] [blame] | 476 | mRefMessageUri = (Uri) intent.getParcelableExtra(EXTRA_IN_REFERENCE_TO_MESSAGE_URI); |
Andy Huang | 4fe0af8 | 2013-08-20 17:24:51 -0700 | [diff] [blame] | 477 | |
| 478 | if (Analytics.isLoggable()) { |
| 479 | if (intent.getBooleanExtra(Utils.EXTRA_FROM_NOTIFICATION, false)) { |
| 480 | Analytics.getInstance().sendEvent( |
| 481 | "notification_action", "compose", getActionString(action), 0); |
| 482 | } |
| 483 | } |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 484 | } |
Mark Wei | 62066e4 | 2012-09-13 12:07:02 -0700 | [diff] [blame] | 485 | mAttachmentsView.setAttachmentPreviews(previews); |
Paul Westbrook | 92227f6 | 2012-03-20 10:32:51 -0700 | [diff] [blame] | 486 | |
| 487 | setAccount(account); |
Mindy Pereira | 818143e | 2012-01-11 13:59:49 -0800 | [diff] [blame] | 488 | if (mAccount == null) { |
| 489 | return; |
| 490 | } |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 491 | |
Scott Kennedy | fe853d3 | 2013-06-19 11:47:35 -0700 | [diff] [blame] | 492 | initRecipients(); |
| 493 | |
Scott Kennedy | 5680ec2 | 2013-01-07 13:15:20 -0800 | [diff] [blame] | 494 | // Clear the notification and mark the conversation as seen, if necessary |
| 495 | final Folder notificationFolder = |
| 496 | intent.getParcelableExtra(EXTRA_NOTIFICATION_FOLDER); |
| 497 | if (notificationFolder != null) { |
| 498 | final Intent clearNotifIntent = |
| 499 | new Intent(MailIntentService.ACTION_CLEAR_NEW_MAIL_NOTIFICATIONS); |
Scott Kennedy | d5edd2d | 2012-12-05 11:11:32 -0800 | [diff] [blame] | 500 | clearNotifIntent.setPackage(getPackageName()); |
Scott Kennedy | 48cfe46 | 2013-04-10 11:32:02 -0700 | [diff] [blame] | 501 | clearNotifIntent.putExtra(Utils.EXTRA_ACCOUNT, account); |
| 502 | clearNotifIntent.putExtra(Utils.EXTRA_FOLDER, notificationFolder); |
Scott Kennedy | 5680ec2 | 2013-01-07 13:15:20 -0800 | [diff] [blame] | 503 | |
| 504 | startService(clearNotifIntent); |
| 505 | } |
| 506 | |
Paul Westbrook | daecb4b | 2012-05-31 10:21:26 -0700 | [diff] [blame] | 507 | if (intent.getBooleanExtra(EXTRA_FROM_EMAIL_TASK, false)) { |
| 508 | mLaunchedFromEmail = true; |
| 509 | } else if (Intent.ACTION_SEND.equals(intent.getAction())) { |
| 510 | final Uri dataUri = intent.getData(); |
| 511 | if (dataUri != null) { |
| 512 | final String dataScheme = intent.getData().getScheme(); |
| 513 | final String accountScheme = mAccount.composeIntentUri.getScheme(); |
| 514 | mLaunchedFromEmail = TextUtils.equals(dataScheme, accountScheme); |
| 515 | } |
| 516 | } |
| 517 | |
Mindy Pereira | 96a7f7a | 2012-07-09 16:51:06 -0700 | [diff] [blame] | 518 | if (mRefMessageUri != null) { |
Alice Yang | a990a71 | 2013-03-13 18:37:00 -0700 | [diff] [blame] | 519 | mShowQuotedText = true; |
| 520 | mComposeMode = action; |
| 521 | getLoaderManager().initLoader(INIT_DRAFT_USING_REFERENCE_MESSAGE, null, this); |
Mindy Pereira | 96a7f7a | 2012-07-09 16:51:06 -0700 | [diff] [blame] | 522 | return; |
| 523 | } else if (message != null && action != EDIT_DRAFT) { |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 524 | initFromDraftMessage(message); |
| 525 | initQuotedTextFromRefMessage(mRefMessage, action); |
Andy Huang | 9f855d6 | 2013-05-30 17:15:03 -0700 | [diff] [blame] | 526 | showCcBcc(savedState); |
Alice Yang | a990a71 | 2013-03-13 18:37:00 -0700 | [diff] [blame] | 527 | mShowQuotedText = message.appendRefMessageContent; |
Andrew Sapperstein | bc5e0dc | 2013-08-07 21:15:22 -0700 | [diff] [blame] | 528 | // if we should be showing quoted text but mRefMessage is null |
| 529 | // and we have some quotedText, display that |
Andrew Sapperstein | 746d861 | 2013-08-26 15:56:32 -0700 | [diff] [blame] | 530 | if (mShowQuotedText && mRefMessage == null) { |
| 531 | if (quotedText != null) { |
| 532 | initQuotedText(quotedText, false /* shouldQuoteText */); |
| 533 | } else if (mExtraValues != null) { |
| 534 | initExtraValues(mExtraValues); |
| 535 | return; |
| 536 | } |
Andrew Sapperstein | bc5e0dc | 2013-08-07 21:15:22 -0700 | [diff] [blame] | 537 | } |
Paul Westbrook | bb87b7f | 2012-03-20 16:20:07 -0700 | [diff] [blame] | 538 | } else if (action == EDIT_DRAFT) { |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 539 | initFromDraftMessage(message); |
Scott Kennedy | 8960f0a | 2012-11-07 15:35:50 -0800 | [diff] [blame] | 540 | boolean showBcc = !TextUtils.isEmpty(message.getBcc()); |
| 541 | boolean showCc = showBcc || !TextUtils.isEmpty(message.getCc()); |
Mindy Pereira | ef38830 | 2012-06-18 19:07:44 -0700 | [diff] [blame] | 542 | mCcBccView.show(false, showCc, showBcc); |
Paul Westbrook | bb87b7f | 2012-03-20 16:20:07 -0700 | [diff] [blame] | 543 | // Update the action to the draft type of the previous draft |
| 544 | switch (message.draftType) { |
| 545 | case UIProvider.DraftType.REPLY: |
| 546 | action = REPLY; |
| 547 | break; |
| 548 | case UIProvider.DraftType.REPLY_ALL: |
| 549 | action = REPLY_ALL; |
| 550 | break; |
| 551 | case UIProvider.DraftType.FORWARD: |
| 552 | action = FORWARD; |
| 553 | break; |
| 554 | case UIProvider.DraftType.COMPOSE: |
| 555 | default: |
| 556 | action = COMPOSE; |
| 557 | break; |
| 558 | } |
Alice Yang | a990a71 | 2013-03-13 18:37:00 -0700 | [diff] [blame] | 559 | LogUtils.d(LOG_TAG, "Previous draft had action type: %d", action); |
| 560 | |
| 561 | mShowQuotedText = message.appendRefMessageContent; |
| 562 | if (message.refMessageUri != null) { |
| 563 | // If we're editing an existing draft that was in reference to an existing message, |
| 564 | // still need to load that original message since we might need to refer to the |
| 565 | // original sender and recipients if user switches "reply <-> reply-all". |
| 566 | mRefMessageUri = message.refMessageUri; |
| 567 | mComposeMode = action; |
| 568 | getLoaderManager().initLoader(REFERENCE_MESSAGE_LOADER, null, this); |
| 569 | return; |
| 570 | } |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 571 | } else if ((action == REPLY || action == REPLY_ALL || action == FORWARD)) { |
| 572 | if (mRefMessage != null) { |
Scott Kennedy | 0aeaf7d | 2012-11-14 18:56:05 -0800 | [diff] [blame] | 573 | initFromRefMessage(action); |
Alice Yang | a990a71 | 2013-03-13 18:37:00 -0700 | [diff] [blame] | 574 | mShowQuotedText = true; |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 575 | } |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 576 | } else { |
Andrew Sapperstein | 746d861 | 2013-08-26 15:56:32 -0700 | [diff] [blame] | 577 | if (initFromExtras(intent)) { |
| 578 | return; |
| 579 | } |
Paul Westbrook | bb87b7f | 2012-03-20 16:20:07 -0700 | [diff] [blame] | 580 | } |
Alice Yang | a990a71 | 2013-03-13 18:37:00 -0700 | [diff] [blame] | 581 | |
| 582 | mComposeMode = action; |
Andy Huang | 9f855d6 | 2013-05-30 17:15:03 -0700 | [diff] [blame] | 583 | finishSetup(action, intent, savedState); |
Mindy Pereira | 96a7f7a | 2012-07-09 16:51:06 -0700 | [diff] [blame] | 584 | } |
Paul Westbrook | bb87b7f | 2012-03-20 16:20:07 -0700 | [diff] [blame] | 585 | |
Mindy Pereira | b199d17 | 2012-08-13 11:04:03 -0700 | [diff] [blame] | 586 | private void checkValidAccounts() { |
Paul Westbrook | faa742f | 2012-11-01 09:50:16 -0700 | [diff] [blame] | 587 | final Account[] allAccounts = AccountUtils.getAccounts(this); |
| 588 | if (allAccounts == null || allAccounts.length == 0) { |
Mindy Pereira | b199d17 | 2012-08-13 11:04:03 -0700 | [diff] [blame] | 589 | final Intent noAccountIntent = MailAppProvider.getNoAccountIntent(this); |
| 590 | if (noAccountIntent != null) { |
Paul Westbrook | faa742f | 2012-11-01 09:50:16 -0700 | [diff] [blame] | 591 | mAccounts = null; |
Mindy Pereira | b199d17 | 2012-08-13 11:04:03 -0700 | [diff] [blame] | 592 | startActivityForResult(noAccountIntent, RESULT_CREATE_ACCOUNT); |
| 593 | } |
| 594 | } else { |
mindyp | 26d4d2d | 2012-09-18 17:30:32 -0700 | [diff] [blame] | 595 | // If none of the accounts are syncing, setup a watcher. |
Mindy Pereira | b199d17 | 2012-08-13 11:04:03 -0700 | [diff] [blame] | 596 | boolean anySyncing = false; |
Paul Westbrook | faa742f | 2012-11-01 09:50:16 -0700 | [diff] [blame] | 597 | for (Account a : allAccounts) { |
Paul Westbrook | dfa1dec | 2012-09-26 16:27:28 -0700 | [diff] [blame] | 598 | if (a.isAccountReady()) { |
Mindy Pereira | b199d17 | 2012-08-13 11:04:03 -0700 | [diff] [blame] | 599 | anySyncing = true; |
| 600 | break; |
| 601 | } |
| 602 | } |
| 603 | if (!anySyncing) { |
| 604 | // There are accounts, but none are sync'd, which is just like having no accounts. |
| 605 | mAccounts = null; |
| 606 | getLoaderManager().initLoader(LOADER_ACCOUNT_CURSOR, null, this); |
| 607 | return; |
| 608 | } |
Paul Westbrook | faa742f | 2012-11-01 09:50:16 -0700 | [diff] [blame] | 609 | mAccounts = AccountUtils.getSyncingAccounts(this); |
Mindy Pereira | b199d17 | 2012-08-13 11:04:03 -0700 | [diff] [blame] | 610 | finishCreate(); |
| 611 | } |
| 612 | } |
| 613 | |
Mindy Pereira | 47d0e65 | 2012-07-23 09:45:07 -0700 | [diff] [blame] | 614 | private Account obtainAccount(Intent intent) { |
| 615 | Account account = null; |
| 616 | Object accountExtra = null; |
| 617 | if (intent != null && intent.getExtras() != null) { |
| 618 | accountExtra = intent.getExtras().get(Utils.EXTRA_ACCOUNT); |
| 619 | if (accountExtra instanceof Account) { |
| 620 | return (Account) accountExtra; |
mindyp | 7ae042e | 2012-08-27 13:27:37 -0700 | [diff] [blame] | 621 | } else if (accountExtra instanceof String) { |
| 622 | // This is the Account attached to the widget compose intent. |
| 623 | account = Account.newinstance((String)accountExtra); |
| 624 | if (account != null) { |
| 625 | return account; |
| 626 | } |
Mindy Pereira | 47d0e65 | 2012-07-23 09:45:07 -0700 | [diff] [blame] | 627 | } |
mindyp | 5ee9dc4 | 2013-01-08 09:54:54 -0800 | [diff] [blame] | 628 | accountExtra = intent.hasExtra(Utils.EXTRA_ACCOUNT) ? |
| 629 | intent.getStringExtra(Utils.EXTRA_ACCOUNT) : |
| 630 | intent.getStringExtra(EXTRA_SELECTED_ACCOUNT); |
Mindy Pereira | 47d0e65 | 2012-07-23 09:45:07 -0700 | [diff] [blame] | 631 | } |
| 632 | if (account == null) { |
mindyp | 0617446 | 2012-10-12 09:11:27 -0700 | [diff] [blame] | 633 | MailAppProvider provider = MailAppProvider.getInstance(); |
| 634 | String lastAccountUri = provider.getLastSentFromAccount(); |
| 635 | if (TextUtils.isEmpty(lastAccountUri)) { |
| 636 | lastAccountUri = provider.getLastViewedAccount(); |
| 637 | } |
Mindy Pereira | 47d0e65 | 2012-07-23 09:45:07 -0700 | [diff] [blame] | 638 | if (!TextUtils.isEmpty(lastAccountUri)) { |
| 639 | accountExtra = Uri.parse(lastAccountUri); |
| 640 | } |
| 641 | } |
Mindy Pereira | b199d17 | 2012-08-13 11:04:03 -0700 | [diff] [blame] | 642 | if (mAccounts != null && mAccounts.length > 0) { |
Mindy Pereira | 47d0e65 | 2012-07-23 09:45:07 -0700 | [diff] [blame] | 643 | if (accountExtra instanceof String && !TextUtils.isEmpty((String) accountExtra)) { |
| 644 | // For backwards compatibility, we need to check account |
| 645 | // names. |
Mindy Pereira | b199d17 | 2012-08-13 11:04:03 -0700 | [diff] [blame] | 646 | for (Account a : mAccounts) { |
Tony Mantler | 79b1156 | 2013-10-09 15:31:50 -0700 | [diff] [blame] | 647 | if (a.getEmailAddress().equals(accountExtra)) { |
Mindy Pereira | 47d0e65 | 2012-07-23 09:45:07 -0700 | [diff] [blame] | 648 | account = a; |
| 649 | } |
| 650 | } |
| 651 | } else if (accountExtra instanceof Uri) { |
| 652 | // The uri of the last viewed account is what is stored in |
| 653 | // the current code base. |
Mindy Pereira | b199d17 | 2012-08-13 11:04:03 -0700 | [diff] [blame] | 654 | for (Account a : mAccounts) { |
Mindy Pereira | 47d0e65 | 2012-07-23 09:45:07 -0700 | [diff] [blame] | 655 | if (a.uri.equals(accountExtra)) { |
| 656 | account = a; |
| 657 | } |
| 658 | } |
Mindy Pereira | b199d17 | 2012-08-13 11:04:03 -0700 | [diff] [blame] | 659 | } |
| 660 | if (account == null) { |
| 661 | account = mAccounts[0]; |
Mindy Pereira | 47d0e65 | 2012-07-23 09:45:07 -0700 | [diff] [blame] | 662 | } |
| 663 | } |
| 664 | return account; |
| 665 | } |
| 666 | |
Andrew Sapperstein | 746d861 | 2013-08-26 15:56:32 -0700 | [diff] [blame] | 667 | protected void finishSetup(int action, Intent intent, Bundle savedInstanceState) { |
mindyp | 34a3c56 | 2012-11-06 15:12:15 -0800 | [diff] [blame] | 668 | setFocus(action); |
Mindy Pereira | f7fc6c3 | 2012-06-19 15:18:33 -0700 | [diff] [blame] | 669 | // Don't bother with the intent if we have procured a message from the |
| 670 | // intent already. |
| 671 | if (!hadSavedInstanceStateMessage(savedInstanceState)) { |
| 672 | initAttachmentsFromIntent(intent); |
| 673 | } |
Alice Yang | a990a71 | 2013-03-13 18:37:00 -0700 | [diff] [blame] | 674 | initActionBar(); |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 675 | initFromSpinner(savedInstanceState != null ? savedInstanceState : intent.getExtras(), |
| 676 | action); |
mindyp | d4a4866 | 2012-11-08 17:13:49 -0800 | [diff] [blame] | 677 | |
| 678 | // If this is a draft message, the draft account is whatever account was |
| 679 | // used to open the draft message in Compose. |
| 680 | if (mDraft != null) { |
| 681 | mDraftAccount = mReplyFromAccount; |
| 682 | } |
| 683 | |
Mindy Pereira | 75f6663 | 2012-01-11 11:42:02 -0800 | [diff] [blame] | 684 | initChangeListeners(); |
Mindy Pereira | 326689d | 2012-05-17 10:14:14 -0700 | [diff] [blame] | 685 | updateHideOrShowCcBcc(); |
Alice Yang | a990a71 | 2013-03-13 18:37:00 -0700 | [diff] [blame] | 686 | updateHideOrShowQuotedText(mShowQuotedText); |
mindyp | 1623f9b | 2012-11-21 12:41:16 -0800 | [diff] [blame] | 687 | |
Andy Huang | 9f855d6 | 2013-05-30 17:15:03 -0700 | [diff] [blame] | 688 | mRespondedInline = mInnerSavedState != null ? |
| 689 | mInnerSavedState.getBoolean(EXTRA_RESPONDED_INLINE) : false; |
mindyp | 1623f9b | 2012-11-21 12:41:16 -0800 | [diff] [blame] | 690 | if (mRespondedInline) { |
| 691 | mQuotedTextView.setVisibility(View.GONE); |
| 692 | } |
Mindy Pereira | 71c9e56 | 2012-05-17 11:01:02 -0700 | [diff] [blame] | 693 | } |
| 694 | |
Scott Kennedy | ff8553f | 2013-04-05 20:57:44 -0700 | [diff] [blame] | 695 | private static boolean hadSavedInstanceStateMessage(final Bundle savedInstanceState) { |
Mindy Pereira | f7fc6c3 | 2012-06-19 15:18:33 -0700 | [diff] [blame] | 696 | return savedInstanceState != null && savedInstanceState.containsKey(EXTRA_MESSAGE); |
| 697 | } |
| 698 | |
Mindy Pereira | 71c9e56 | 2012-05-17 11:01:02 -0700 | [diff] [blame] | 699 | private void updateHideOrShowQuotedText(boolean showQuotedText) { |
| 700 | mQuotedTextView.updateCheckedState(showQuotedText); |
mindyp | 4088243 | 2012-09-06 11:07:40 -0700 | [diff] [blame] | 701 | mQuotedTextView.setUpperDividerVisible(mAttachmentsView.getAttachments().size() > 0); |
Mindy Pereira | 433b198 | 2012-04-03 11:53:07 -0700 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | private void setFocus(int action) { |
| 705 | if (action == EDIT_DRAFT) { |
| 706 | int type = mDraft.draftType; |
| 707 | switch (type) { |
| 708 | case UIProvider.DraftType.COMPOSE: |
| 709 | case UIProvider.DraftType.FORWARD: |
| 710 | action = COMPOSE; |
| 711 | break; |
| 712 | case UIProvider.DraftType.REPLY: |
| 713 | case UIProvider.DraftType.REPLY_ALL: |
| 714 | default: |
| 715 | action = REPLY; |
| 716 | break; |
| 717 | } |
| 718 | } |
| 719 | switch (action) { |
| 720 | case FORWARD: |
| 721 | case COMPOSE: |
mindyp | 2708306 | 2012-11-15 09:02:01 -0800 | [diff] [blame] | 722 | if (TextUtils.isEmpty(mTo.getText())) { |
| 723 | mTo.requestFocus(); |
| 724 | break; |
| 725 | } |
Scott Kennedy | ff8553f | 2013-04-05 20:57:44 -0700 | [diff] [blame] | 726 | //$FALL-THROUGH$ |
Mindy Pereira | 433b198 | 2012-04-03 11:53:07 -0700 | [diff] [blame] | 727 | case REPLY: |
| 728 | case REPLY_ALL: |
| 729 | default: |
| 730 | focusBody(); |
| 731 | break; |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | /** |
| 736 | * Focus the body of the message. |
| 737 | */ |
| 738 | public void focusBody() { |
| 739 | mBodyView.requestFocus(); |
| 740 | int length = mBodyView.getText().length(); |
| 741 | |
| 742 | int signatureStartPos = getSignatureStartPosition( |
| 743 | mSignature, mBodyView.getText().toString()); |
| 744 | if (signatureStartPos > -1) { |
| 745 | // In case the user deleted the newlines... |
| 746 | mBodyView.setSelection(signatureStartPos); |
mindyp | 8743cfc | 2012-09-18 13:29:08 -0700 | [diff] [blame] | 747 | } else if (length >= 0) { |
Mindy Pereira | 433b198 | 2012-04-03 11:53:07 -0700 | [diff] [blame] | 748 | // Move cursor to the end. |
| 749 | mBodyView.setSelection(length); |
| 750 | } |
Mindy Pereira | 1a95a57 | 2012-01-05 12:21:29 -0800 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | @Override |
Andy Huang | 761522c | 2013-08-08 13:09:11 -0700 | [diff] [blame] | 754 | protected void onStart() { |
| 755 | super.onStart(); |
| 756 | |
| 757 | Analytics.getInstance().activityStart(this); |
| 758 | } |
| 759 | |
| 760 | @Override |
| 761 | protected void onStop() { |
| 762 | super.onStop(); |
| 763 | |
| 764 | Analytics.getInstance().activityStop(this); |
| 765 | } |
| 766 | |
| 767 | @Override |
Mindy Pereira | 1a95a57 | 2012-01-05 12:21:29 -0800 | [diff] [blame] | 768 | protected void onResume() { |
| 769 | super.onResume(); |
| 770 | // Update the from spinner as other accounts |
| 771 | // may now be available. |
Mindy Pereira | 818143e | 2012-01-11 13:59:49 -0800 | [diff] [blame] | 772 | if (mFromSpinner != null && mAccount != null) { |
Paul Westbrook | c97ec3e | 2013-07-12 18:17:19 -0700 | [diff] [blame] | 773 | mFromSpinner.initialize(mComposeMode, mAccount, mAccounts, mRefMessage); |
Mindy Pereira | 818143e | 2012-01-11 13:59:49 -0800 | [diff] [blame] | 774 | } |
Mindy Pereira | 1a95a57 | 2012-01-05 12:21:29 -0800 | [diff] [blame] | 775 | } |
| 776 | |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 777 | @Override |
| 778 | protected void onPause() { |
| 779 | super.onPause(); |
| 780 | |
Mindy Pereira | a214833 | 2012-07-02 13:54:14 -0700 | [diff] [blame] | 781 | // 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] | 782 | // Don't save unnecessary drafts if we are only changing the orientation. |
| 783 | if (!isChangingConfigurations()) { |
Mindy Pereira | a214833 | 2012-07-02 13:54:14 -0700 | [diff] [blame] | 784 | saveIfNeeded(); |
Andy Huang | dc97bf4 | 2013-08-15 16:52:45 -0700 | [diff] [blame] | 785 | |
Andy Huang | e003b4c | 2013-08-16 10:32:05 -0700 | [diff] [blame] | 786 | if (isFinishing() && !mPerformedSendOrDiscard && !isBlank()) { |
Andy Huang | dc97bf4 | 2013-08-15 16:52:45 -0700 | [diff] [blame] | 787 | // log saving upon backing out of activity. (we avoid logging every sendOrSave() |
| 788 | // because that method can be invoked many times in a single compose session.) |
| 789 | logSendOrSave(true /* save */); |
| 790 | } |
Mindy Pereira | a214833 | 2012-07-02 13:54:14 -0700 | [diff] [blame] | 791 | } |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | @Override |
| 795 | protected final void onActivityResult(int request, int result, Intent data) { |
Mindy Pereira | b199d17 | 2012-08-13 11:04:03 -0700 | [diff] [blame] | 796 | if (request == RESULT_PICK_ATTACHMENT && result == RESULT_OK) { |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 797 | addAttachmentAndUpdateView(data); |
Mindy Pereira | b199d17 | 2012-08-13 11:04:03 -0700 | [diff] [blame] | 798 | mAddingAttachment = false; |
| 799 | } else if (request == RESULT_CREATE_ACCOUNT) { |
Alice Yang | a990a71 | 2013-03-13 18:37:00 -0700 | [diff] [blame] | 800 | // We were waiting for the user to create an account |
Mindy Pereira | b199d17 | 2012-08-13 11:04:03 -0700 | [diff] [blame] | 801 | if (result != RESULT_OK) { |
| 802 | finish(); |
| 803 | } else { |
| 804 | // Watch for accounts to show up! |
| 805 | // restart the loader to get the updated list of accounts |
| 806 | getLoaderManager().initLoader(LOADER_ACCOUNT_CURSOR, null, this); |
| 807 | showWaitFragment(null); |
| 808 | } |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 809 | } |
| 810 | } |
| 811 | |
| 812 | @Override |
Scott Kennedy | d906390 | 2013-08-02 22:14:37 -0700 | [diff] [blame] | 813 | protected final void onRestoreInstanceState(Bundle savedInstanceState) { |
Yorke Lee | 7bec2b9 | 2013-04-26 08:31:42 -0700 | [diff] [blame] | 814 | final boolean hasAccounts = mAccounts != null && mAccounts.length > 0; |
| 815 | if (hasAccounts) { |
| 816 | clearChangeListeners(); |
| 817 | } |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 818 | super.onRestoreInstanceState(savedInstanceState); |
Andy Huang | 9f855d6 | 2013-05-30 17:15:03 -0700 | [diff] [blame] | 819 | if (mInnerSavedState != null) { |
| 820 | if (mInnerSavedState.containsKey(EXTRA_FOCUS_SELECTION_START)) { |
| 821 | int selectionStart = mInnerSavedState.getInt(EXTRA_FOCUS_SELECTION_START); |
| 822 | int selectionEnd = mInnerSavedState.getInt(EXTRA_FOCUS_SELECTION_END); |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 823 | // There should be a focus and it should be an EditText since we |
| 824 | // only save these extras if these conditions are true. |
| 825 | EditText focusEditText = (EditText) getCurrentFocus(); |
| 826 | final int length = focusEditText.getText().length(); |
| 827 | if (selectionStart < length && selectionEnd < length) { |
| 828 | focusEditText.setSelection(selectionStart, selectionEnd); |
| 829 | } |
| 830 | } |
| 831 | } |
Yorke Lee | 7bec2b9 | 2013-04-26 08:31:42 -0700 | [diff] [blame] | 832 | if (hasAccounts) { |
| 833 | initChangeListeners(); |
| 834 | } |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | @Override |
Scott Kennedy | d906390 | 2013-08-02 22:14:37 -0700 | [diff] [blame] | 838 | protected final void onSaveInstanceState(Bundle state) { |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 839 | super.onSaveInstanceState(state); |
Andy Huang | 9f855d6 | 2013-05-30 17:15:03 -0700 | [diff] [blame] | 840 | final Bundle inner = new Bundle(); |
| 841 | saveState(inner); |
| 842 | state.putBundle(KEY_INNER_SAVED_STATE, inner); |
| 843 | } |
| 844 | |
| 845 | private void saveState(Bundle state) { |
Mindy Pereira | b199d17 | 2012-08-13 11:04:03 -0700 | [diff] [blame] | 846 | // We have no accounts so there is nothing to compose, and therefore, nothing to save. |
| 847 | if (mAccounts == null || mAccounts.length == 0) { |
| 848 | return; |
| 849 | } |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 850 | // The framework is happy to save and restore the selection but only if it also saves and |
| 851 | // restores the contents of the edit text. That's a lot of text to put in a bundle so we do |
| 852 | // this manually. |
| 853 | View focus = getCurrentFocus(); |
| 854 | if (focus != null && focus instanceof EditText) { |
| 855 | EditText focusEditText = (EditText) focus; |
| 856 | state.putInt(EXTRA_FOCUS_SELECTION_START, focusEditText.getSelectionStart()); |
| 857 | state.putInt(EXTRA_FOCUS_SELECTION_END, focusEditText.getSelectionEnd()); |
| 858 | } |
Paul Westbrook | 6273e96 | 2012-04-23 10:44:15 -0700 | [diff] [blame] | 859 | |
| 860 | final List<ReplyFromAccount> replyFromAccounts = mFromSpinner.getReplyFromAccounts(); |
Paul Westbrook | 151f1ad | 2012-04-24 09:13:00 -0700 | [diff] [blame] | 861 | final int selectedPos = mFromSpinner.getSelectedItemPosition(); |
Mindy Pereira | d90f7ac | 2012-06-27 10:31:06 -0700 | [diff] [blame] | 862 | final ReplyFromAccount selectedReplyFromAccount = (replyFromAccounts != null |
| 863 | && replyFromAccounts.size() > 0 && replyFromAccounts.size() > selectedPos) ? |
| 864 | replyFromAccounts.get(selectedPos) : null; |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 865 | if (selectedReplyFromAccount != null) { |
| 866 | state.putString(EXTRA_SELECTED_REPLY_FROM_ACCOUNT, selectedReplyFromAccount.serialize() |
| 867 | .toString()); |
| 868 | state.putParcelable(Utils.EXTRA_ACCOUNT, selectedReplyFromAccount.account); |
| 869 | } else { |
| 870 | state.putParcelable(Utils.EXTRA_ACCOUNT, mAccount); |
| 871 | } |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 872 | |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 873 | if (mDraftId == UIProvider.INVALID_MESSAGE_ID && mRequestId !=0) { |
| 874 | // We don't have a draft id, and we have a request id, |
| 875 | // save the request id. |
| 876 | state.putInt(EXTRA_REQUEST_ID, mRequestId); |
| 877 | } |
| 878 | |
| 879 | // We want to restore the current mode after a pause |
| 880 | // or rotation. |
| 881 | int mode = getMode(); |
| 882 | state.putInt(EXTRA_ACTION, mode); |
| 883 | |
mindyp | e7b76aa | 2012-11-14 16:19:13 -0800 | [diff] [blame] | 884 | final Message message = createMessage(selectedReplyFromAccount, mode); |
Andy Huang | 1f8f4dd | 2012-10-25 21:35:35 -0700 | [diff] [blame] | 885 | if (mDraft != null) { |
mindyp | e7b76aa | 2012-11-14 16:19:13 -0800 | [diff] [blame] | 886 | message.id = mDraft.id; |
| 887 | message.serverId = mDraft.serverId; |
| 888 | message.uri = mDraft.uri; |
Andy Huang | 1f8f4dd | 2012-10-25 21:35:35 -0700 | [diff] [blame] | 889 | } |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 890 | state.putParcelable(EXTRA_MESSAGE, message); |
| 891 | |
| 892 | if (mRefMessage != null) { |
| 893 | state.putParcelable(EXTRA_IN_REFERENCE_TO_MESSAGE, mRefMessage); |
Andrew Sapperstein | bc5e0dc | 2013-08-07 21:15:22 -0700 | [diff] [blame] | 894 | } else if (message.appendRefMessageContent) { |
| 895 | // If we have no ref message but should be appending |
| 896 | // ref message content, we have orphaned quoted text. Save it. |
| 897 | state.putCharSequence(EXTRA_QUOTED_TEXT, mQuotedTextView.getQuotedTextIfIncluded()); |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 898 | } |
Mindy Pereira | 326689d | 2012-05-17 10:14:14 -0700 | [diff] [blame] | 899 | state.putBoolean(EXTRA_SHOW_CC, mCcBccView.isCcVisible()); |
| 900 | state.putBoolean(EXTRA_SHOW_BCC, mCcBccView.isBccVisible()); |
mindyp | 1623f9b | 2012-11-21 12:41:16 -0800 | [diff] [blame] | 901 | state.putBoolean(EXTRA_RESPONDED_INLINE, mRespondedInline); |
mindyp | 816b3f0 | 2012-12-11 08:25:04 -0800 | [diff] [blame] | 902 | state.putBoolean(EXTRA_SAVE_ENABLED, mSave != null && mSave.isEnabled()); |
Mark Wei | 62066e4 | 2012-09-13 12:07:02 -0700 | [diff] [blame] | 903 | state.putParcelableArrayList( |
| 904 | EXTRA_ATTACHMENT_PREVIEWS, mAttachmentsView.getAttachmentPreviews()); |
Scott Kennedy | 44d4481 | 2013-08-19 14:18:31 -0700 | [diff] [blame] | 905 | |
| 906 | state.putParcelable(EXTRA_VALUES, mExtraValues); |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | private int getMode() { |
| 910 | int mode = ComposeActivity.COMPOSE; |
| 911 | ActionBar actionBar = getActionBar(); |
Mindy Pereira | e011b1d | 2012-06-18 13:45:26 -0700 | [diff] [blame] | 912 | if (actionBar != null |
| 913 | && actionBar.getNavigationMode() == ActionBar.NAVIGATION_MODE_LIST) { |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 914 | mode = actionBar.getSelectedNavigationIndex(); |
| 915 | } |
| 916 | return mode; |
| 917 | } |
| 918 | |
| 919 | private Message createMessage(ReplyFromAccount selectedReplyFromAccount, int mode) { |
| 920 | Message message = new Message(); |
| 921 | message.id = UIProvider.INVALID_MESSAGE_ID; |
Andy Huang | d47877e | 2012-08-09 19:31:24 -0700 | [diff] [blame] | 922 | message.serverId = null; |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 923 | message.uri = null; |
| 924 | message.conversationUri = null; |
| 925 | message.subject = mSubject.getText().toString(); |
| 926 | message.snippet = null; |
Scott Kennedy | 8960f0a | 2012-11-07 15:35:50 -0800 | [diff] [blame] | 927 | message.setTo(formatSenders(mTo.getText().toString())); |
| 928 | message.setCc(formatSenders(mCc.getText().toString())); |
| 929 | message.setBcc(formatSenders(mBcc.getText().toString())); |
| 930 | message.setReplyTo(null); |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 931 | message.dateReceivedMs = 0; |
Paul Westbrook | b4931c6 | 2013-01-14 17:51:18 -0800 | [diff] [blame] | 932 | final String htmlBody = Html.toHtml(removeComposingSpans(mBodyView.getText())); |
| 933 | final StringBuilder fullBody = new StringBuilder(htmlBody); |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 934 | message.bodyHtml = fullBody.toString(); |
| 935 | message.bodyText = mBodyView.getText().toString(); |
| 936 | message.embedsExternalResources = false; |
Alice Yang | a990a71 | 2013-03-13 18:37:00 -0700 | [diff] [blame] | 937 | message.refMessageUri = mRefMessage != null ? mRefMessage.uri : null; |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 938 | message.appendRefMessageContent = mQuotedTextView.getQuotedTextIfIncluded() != null; |
| 939 | ArrayList<Attachment> attachments = mAttachmentsView.getAttachments(); |
| 940 | message.hasAttachments = attachments != null && attachments.size() > 0; |
| 941 | message.attachmentListUri = null; |
| 942 | message.messageFlags = 0; |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 943 | message.alwaysShowImages = false; |
| 944 | message.attachmentsJson = Attachment.toJSONArray(attachments); |
| 945 | CharSequence quotedText = mQuotedTextView.getQuotedText(); |
| 946 | message.quotedTextOffset = !TextUtils.isEmpty(quotedText) ? QuotedTextView |
| 947 | .getQuotedTextOffset(quotedText.toString()) : -1; |
| 948 | message.accountUri = null; |
Tony Mantler | bb036ff7 | 2013-10-18 14:03:43 -0700 | [diff] [blame] | 949 | final String email = selectedReplyFromAccount != null ? selectedReplyFromAccount.address |
| 950 | : mAccount != null ? mAccount.getEmailAddress() : null; |
Tony Mantler | f441d14 | 2013-10-22 11:46:00 -0700 | [diff] [blame] | 951 | final String senderName = selectedReplyFromAccount != null ? selectedReplyFromAccount.name |
| 952 | : mAccount != null ? mAccount.getSenderName() : null; |
Tony Mantler | 821e578 | 2014-01-06 15:33:43 -0800 | [diff] [blame] | 953 | final Address address = new Address(email, senderName); |
Tony Mantler | f441d14 | 2013-10-22 11:46:00 -0700 | [diff] [blame] | 954 | message.setFrom(address.toHeader()); |
Andy Huang | 1f8f4dd | 2012-10-25 21:35:35 -0700 | [diff] [blame] | 955 | message.draftType = getDraftType(mode); |
mindyp | e7b76aa | 2012-11-14 16:19:13 -0800 | [diff] [blame] | 956 | return message; |
Andy Huang | 1f8f4dd | 2012-10-25 21:35:35 -0700 | [diff] [blame] | 957 | } |
| 958 | |
Scott Kennedy | ff8553f | 2013-04-05 20:57:44 -0700 | [diff] [blame] | 959 | private static String formatSenders(final String string) { |
Mindy Pereira | 3c91158 | 2012-08-09 16:59:09 -0700 | [diff] [blame] | 960 | if (!TextUtils.isEmpty(string) && string.charAt(string.length() - 1) == ',') { |
| 961 | return string.substring(0, string.length() - 1); |
| 962 | } |
| 963 | return string; |
| 964 | } |
| 965 | |
Mindy Pereira | 818143e | 2012-01-11 13:59:49 -0800 | [diff] [blame] | 966 | @VisibleForTesting |
| 967 | void setAccount(Account account) { |
Mindy Pereira | bb5217e | 2012-04-17 11:08:29 -0700 | [diff] [blame] | 968 | if (account == null) { |
| 969 | return; |
| 970 | } |
Mindy Pereira | 23e9fde | 2012-03-20 15:08:24 -0700 | [diff] [blame] | 971 | if (!account.equals(mAccount)) { |
| 972 | mAccount = account; |
Paul Westbrook | b1f573c | 2012-04-06 11:38:28 -0700 | [diff] [blame] | 973 | mCachedSettings = mAccount.settings; |
| 974 | appendSignature(); |
Mindy Pereira | 23e9fde | 2012-03-20 15:08:24 -0700 | [diff] [blame] | 975 | } |
Mindy Pereira | fa20c1a | 2012-07-23 13:00:02 -0700 | [diff] [blame] | 976 | if (mAccount != null) { |
Tony Mantler | 79b1156 | 2013-10-09 15:31:50 -0700 | [diff] [blame] | 977 | MailActivity.setNfcMessage(mAccount.getEmailAddress()); |
Mindy Pereira | fa20c1a | 2012-07-23 13:00:02 -0700 | [diff] [blame] | 978 | } |
Mindy Pereira | 818143e | 2012-01-11 13:59:49 -0800 | [diff] [blame] | 979 | } |
| 980 | |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 981 | private void initFromSpinner(Bundle bundle, int action) { |
| 982 | if (action == EDIT_DRAFT && mDraft.draftType == UIProvider.DraftType.COMPOSE) { |
Mindy Pereira | 62de1b1 | 2012-04-06 12:17:56 -0700 | [diff] [blame] | 983 | action = COMPOSE; |
| 984 | } |
Paul Westbrook | c97ec3e | 2013-07-12 18:17:19 -0700 | [diff] [blame] | 985 | mFromSpinner.initialize(action, mAccount, mAccounts, mRefMessage); |
| 986 | |
Mindy Pereira | 9a42bb4 | 2012-04-18 15:21:33 -0700 | [diff] [blame] | 987 | if (bundle != null) { |
| 988 | if (bundle.containsKey(EXTRA_SELECTED_REPLY_FROM_ACCOUNT)) { |
| 989 | mReplyFromAccount = ReplyFromAccount.deserialize(mAccount, |
| 990 | bundle.getString(EXTRA_SELECTED_REPLY_FROM_ACCOUNT)); |
| 991 | } else if (bundle.containsKey(EXTRA_FROM_ACCOUNT_STRING)) { |
Paul Westbrook | c97ec3e | 2013-07-12 18:17:19 -0700 | [diff] [blame] | 992 | final String accountString = bundle.getString(EXTRA_FROM_ACCOUNT_STRING); |
Mindy Pereira | 9a42bb4 | 2012-04-18 15:21:33 -0700 | [diff] [blame] | 993 | mReplyFromAccount = mFromSpinner.getMatchingReplyFromAccount(accountString); |
| 994 | } |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 995 | } |
| 996 | if (mReplyFromAccount == null) { |
| 997 | if (mDraft != null) { |
| 998 | mReplyFromAccount = getReplyFromAccountFromDraft(mAccount, mDraft); |
| 999 | } else if (mRefMessage != null) { |
| 1000 | mReplyFromAccount = getReplyFromAccountForReply(mAccount, mRefMessage); |
| 1001 | } |
Mindy Pereira | 62de1b1 | 2012-04-06 12:17:56 -0700 | [diff] [blame] | 1002 | } |
| 1003 | if (mReplyFromAccount == null) { |
Andy Huang | 238aa47 | 2012-10-30 17:45:17 -0700 | [diff] [blame] | 1004 | mReplyFromAccount = getDefaultReplyFromAccount(mAccount); |
Mindy Pereira | 62de1b1 | 2012-04-06 12:17:56 -0700 | [diff] [blame] | 1005 | } |
Mindy Pereira | 9a42bb4 | 2012-04-18 15:21:33 -0700 | [diff] [blame] | 1006 | |
Mindy Pereira | 62de1b1 | 2012-04-06 12:17:56 -0700 | [diff] [blame] | 1007 | mFromSpinner.setCurrentAccount(mReplyFromAccount); |
Mindy Pereira | 9a42bb4 | 2012-04-18 15:21:33 -0700 | [diff] [blame] | 1008 | |
Mindy Pereira | 62de1b1 | 2012-04-06 12:17:56 -0700 | [diff] [blame] | 1009 | if (mFromSpinner.getCount() > 1) { |
Mindy Pereira | a83e708 | 2012-03-30 08:53:11 -0700 | [diff] [blame] | 1010 | // If there is only 1 account, just show that account. |
| 1011 | // Otherwise, give the user the ability to choose which account to |
Mindy Pereira | 62de1b1 | 2012-04-06 12:17:56 -0700 | [diff] [blame] | 1012 | // send mail from / save drafts to. |
| 1013 | mFromStatic.setVisibility(View.GONE); |
Andy Huang | ca4676f | 2014-01-16 13:22:20 -0800 | [diff] [blame^] | 1014 | mFromStaticText.setText(mReplyFromAccount.address); |
Mindy Pereira | 62de1b1 | 2012-04-06 12:17:56 -0700 | [diff] [blame] | 1015 | mFromSpinnerWrapper.setVisibility(View.VISIBLE); |
Mindy Pereira | a83e708 | 2012-03-30 08:53:11 -0700 | [diff] [blame] | 1016 | } else { |
| 1017 | mFromStatic.setVisibility(View.VISIBLE); |
Andy Huang | ca4676f | 2014-01-16 13:22:20 -0800 | [diff] [blame^] | 1018 | mFromStaticText.setText(mReplyFromAccount.address); |
Mindy Pereira | a83e708 | 2012-03-30 08:53:11 -0700 | [diff] [blame] | 1019 | mFromSpinnerWrapper.setVisibility(View.GONE); |
Mindy Pereira | a83e708 | 2012-03-30 08:53:11 -0700 | [diff] [blame] | 1020 | } |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 1021 | } |
| 1022 | |
Mindy Pereira | 62de1b1 | 2012-04-06 12:17:56 -0700 | [diff] [blame] | 1023 | private ReplyFromAccount getReplyFromAccountForReply(Account account, Message refMessage) { |
| 1024 | if (refMessage.accountUri != null) { |
| 1025 | // This must be from combined inbox. |
| 1026 | List<ReplyFromAccount> replyFromAccounts = mFromSpinner.getReplyFromAccounts(); |
| 1027 | for (ReplyFromAccount from : replyFromAccounts) { |
| 1028 | if (from.account.uri.equals(refMessage.accountUri)) { |
| 1029 | return from; |
| 1030 | } |
| 1031 | } |
| 1032 | return null; |
| 1033 | } else { |
| 1034 | return getReplyFromAccount(account, refMessage); |
| 1035 | } |
| 1036 | } |
| 1037 | |
| 1038 | /** |
Tony Mantler | 9016a5e | 2013-07-19 11:54:17 -0700 | [diff] [blame] | 1039 | * Given an account and the message we're replying to, |
Mindy Pereira | 62de1b1 | 2012-04-06 12:17:56 -0700 | [diff] [blame] | 1040 | * return who the message should be sent from. |
| 1041 | * @param account Account in which the message arrived. |
Tony Mantler | 9016a5e | 2013-07-19 11:54:17 -0700 | [diff] [blame] | 1042 | * @param refMessage Message to analyze for account selection |
Mindy Pereira | 62de1b1 | 2012-04-06 12:17:56 -0700 | [diff] [blame] | 1043 | * @return the address from which to reply. |
| 1044 | */ |
| 1045 | public ReplyFromAccount getReplyFromAccount(Account account, Message refMessage) { |
| 1046 | // First see if we are supposed to use the default address or |
| 1047 | // the address it was sentTo. |
Mindy Pereira | 326689d | 2012-05-17 10:14:14 -0700 | [diff] [blame] | 1048 | if (mCachedSettings.forceReplyFromDefault) { |
Mindy Pereira | 62de1b1 | 2012-04-06 12:17:56 -0700 | [diff] [blame] | 1049 | return getDefaultReplyFromAccount(account); |
| 1050 | } else { |
Mindy Pereira | 89bae57 | 2012-06-18 11:34:36 -0700 | [diff] [blame] | 1051 | // 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] | 1052 | // all the message recipients and find one that matches |
| 1053 | // a custom from or account. |
| 1054 | List<String> allRecipients = new ArrayList<String>(); |
Tony Mantler | 9016a5e | 2013-07-19 11:54:17 -0700 | [diff] [blame] | 1055 | allRecipients.addAll(Arrays.asList(refMessage.getToAddressesUnescaped())); |
| 1056 | allRecipients.addAll(Arrays.asList(refMessage.getCcAddressesUnescaped())); |
Mindy Pereira | 62de1b1 | 2012-04-06 12:17:56 -0700 | [diff] [blame] | 1057 | return getMatchingRecipient(account, allRecipients); |
| 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | /** |
| 1062 | * Compare all the recipients of an email to the current account and all |
| 1063 | * custom addresses associated with that account. Return the match if there |
| 1064 | * is one, or the default account if there isn't. |
| 1065 | */ |
| 1066 | protected ReplyFromAccount getMatchingRecipient(Account account, List<String> sentTo) { |
| 1067 | // Tokenize the list and place in a hashmap. |
| 1068 | ReplyFromAccount matchingReplyFrom = null; |
| 1069 | Rfc822Token[] tokens; |
| 1070 | HashSet<String> recipientsMap = new HashSet<String>(); |
| 1071 | for (String address : sentTo) { |
| 1072 | tokens = Rfc822Tokenizer.tokenize(address); |
| 1073 | for (int i = 0; i < tokens.length; i++) { |
| 1074 | recipientsMap.add(tokens[i].getAddress()); |
| 1075 | } |
| 1076 | } |
| 1077 | |
| 1078 | int matchingAddressCount = 0; |
| 1079 | List<ReplyFromAccount> customFroms; |
Andy Huang | 1617481 | 2012-08-16 16:40:35 -0700 | [diff] [blame] | 1080 | customFroms = account.getReplyFroms(); |
| 1081 | if (customFroms != null) { |
| 1082 | for (ReplyFromAccount entry : customFroms) { |
| 1083 | if (recipientsMap.contains(entry.address)) { |
| 1084 | matchingReplyFrom = entry; |
| 1085 | matchingAddressCount++; |
Mindy Pereira | 62de1b1 | 2012-04-06 12:17:56 -0700 | [diff] [blame] | 1086 | } |
| 1087 | } |
Mindy Pereira | 62de1b1 | 2012-04-06 12:17:56 -0700 | [diff] [blame] | 1088 | } |
| 1089 | if (matchingAddressCount > 1) { |
| 1090 | matchingReplyFrom = getDefaultReplyFromAccount(account); |
| 1091 | } |
| 1092 | return matchingReplyFrom; |
| 1093 | } |
| 1094 | |
Scott Kennedy | ff8553f | 2013-04-05 20:57:44 -0700 | [diff] [blame] | 1095 | private static ReplyFromAccount getDefaultReplyFromAccount(final Account account) { |
| 1096 | for (final ReplyFromAccount from : account.getReplyFroms()) { |
Mindy Pereira | 62de1b1 | 2012-04-06 12:17:56 -0700 | [diff] [blame] | 1097 | if (from.isDefault) { |
| 1098 | return from; |
| 1099 | } |
| 1100 | } |
Tony Mantler | f441d14 | 2013-10-22 11:46:00 -0700 | [diff] [blame] | 1101 | return new ReplyFromAccount(account, account.uri, account.getEmailAddress(), |
| 1102 | account.getSenderName(), account.getEmailAddress(), true, false); |
Mindy Pereira | 62de1b1 | 2012-04-06 12:17:56 -0700 | [diff] [blame] | 1103 | } |
| 1104 | |
Tony Mantler | f441d14 | 2013-10-22 11:46:00 -0700 | [diff] [blame] | 1105 | private ReplyFromAccount getReplyFromAccountFromDraft(final Account account, |
| 1106 | final Message msg) { |
| 1107 | final Address[] draftFroms = Address.parse(msg.getFrom()); |
| 1108 | final String sender = draftFroms.length > 0 ? draftFroms[0].getAddress() : ""; |
Mindy Pereira | 62de1b1 | 2012-04-06 12:17:56 -0700 | [diff] [blame] | 1109 | ReplyFromAccount replyFromAccount = null; |
| 1110 | List<ReplyFromAccount> replyFromAccounts = mFromSpinner.getReplyFromAccounts(); |
Tony Mantler | 79b1156 | 2013-10-09 15:31:50 -0700 | [diff] [blame] | 1111 | if (TextUtils.equals(account.getEmailAddress(), sender)) { |
Tony Mantler | f441d14 | 2013-10-22 11:46:00 -0700 | [diff] [blame] | 1112 | replyFromAccount = getDefaultReplyFromAccount(account); |
Mindy Pereira | 62de1b1 | 2012-04-06 12:17:56 -0700 | [diff] [blame] | 1113 | } else { |
| 1114 | for (ReplyFromAccount fromAccount : replyFromAccounts) { |
Tony Mantler | 79b1156 | 2013-10-09 15:31:50 -0700 | [diff] [blame] | 1115 | if (TextUtils.equals(fromAccount.address, sender)) { |
Mindy Pereira | 62de1b1 | 2012-04-06 12:17:56 -0700 | [diff] [blame] | 1116 | replyFromAccount = fromAccount; |
| 1117 | break; |
| 1118 | } |
| 1119 | } |
| 1120 | } |
| 1121 | return replyFromAccount; |
| 1122 | } |
| 1123 | |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 1124 | private void findViews() { |
Mindy Pereira | b199d17 | 2012-08-13 11:04:03 -0700 | [diff] [blame] | 1125 | findViewById(R.id.compose).setVisibility(View.VISIBLE); |
Mindy Pereira | a26b54e | 2012-01-06 12:54:33 -0800 | [diff] [blame] | 1126 | mCcBccButton = (Button) findViewById(R.id.add_cc_bcc); |
Mindy Pereira | 8e9305e | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 1127 | if (mCcBccButton != null) { |
| 1128 | mCcBccButton.setOnClickListener(this); |
| 1129 | } |
| 1130 | mCcBccView = (CcBccView) findViewById(R.id.cc_bcc_wrapper); |
Mindy Pereira | 7b56a61 | 2011-12-14 12:32:28 -0800 | [diff] [blame] | 1131 | mAttachmentsView = (AttachmentsView)findViewById(R.id.attachments); |
Andrew Sapperstein | 6aea786 | 2013-10-24 19:59:51 -0700 | [diff] [blame] | 1132 | mAddAttachmentsButton = findViewById(R.id.add_attachment); |
| 1133 | if (mAddAttachmentsButton != null) { |
| 1134 | mAddAttachmentsButton.setOnClickListener(this); |
mindyp | cd0b0b9 | 2012-08-23 14:33:17 -0700 | [diff] [blame] | 1135 | } |
Mindy Pereira | 818143e | 2012-01-11 13:59:49 -0800 | [diff] [blame] | 1136 | mTo = (RecipientEditTextView) findViewById(R.id.to); |
Scott Kennedy | 4150039 | 2013-04-24 18:46:36 -0700 | [diff] [blame] | 1137 | mTo.setTokenizer(new Rfc822Tokenizer()); |
Mindy Pereira | 818143e | 2012-01-11 13:59:49 -0800 | [diff] [blame] | 1138 | mCc = (RecipientEditTextView) findViewById(R.id.cc); |
Scott Kennedy | 4150039 | 2013-04-24 18:46:36 -0700 | [diff] [blame] | 1139 | mCc.setTokenizer(new Rfc822Tokenizer()); |
Mindy Pereira | 818143e | 2012-01-11 13:59:49 -0800 | [diff] [blame] | 1140 | mBcc = (RecipientEditTextView) findViewById(R.id.bcc); |
Scott Kennedy | 4150039 | 2013-04-24 18:46:36 -0700 | [diff] [blame] | 1141 | mBcc.setTokenizer(new Rfc822Tokenizer()); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1142 | // TODO: add special chips text change watchers before adding |
| 1143 | // this as a text changed watcher to the to, cc, bcc fields. |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 1144 | mSubject = (TextView) findViewById(R.id.subject); |
mindyp | 62d3ec7 | 2012-08-24 13:04:09 -0700 | [diff] [blame] | 1145 | mSubject.setOnEditorActionListener(this); |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 1146 | mQuotedTextView = (QuotedTextView) findViewById(R.id.quoted_text_view); |
| 1147 | mQuotedTextView.setRespondInlineListener(this); |
Mindy Pereira | 433b198 | 2012-04-03 11:53:07 -0700 | [diff] [blame] | 1148 | mBodyView = (EditText) findViewById(R.id.body); |
Mindy Pereira | 1a95a57 | 2012-01-05 12:21:29 -0800 | [diff] [blame] | 1149 | mFromStatic = findViewById(R.id.static_from_content); |
Mindy Pereira | 2eb1732 | 2012-03-07 10:07:34 -0800 | [diff] [blame] | 1150 | mFromStaticText = (TextView) findViewById(R.id.from_account_name); |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 1151 | mFromSpinnerWrapper = findViewById(R.id.spinner_from_content); |
Mindy Pereira | 5a85e2b | 2012-01-11 09:53:32 -0800 | [diff] [blame] | 1152 | mFromSpinner = (FromAddressSpinner) findViewById(R.id.from_picker); |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 1153 | } |
| 1154 | |
mindyp | 62d3ec7 | 2012-08-24 13:04:09 -0700 | [diff] [blame] | 1155 | @Override |
| 1156 | public boolean onEditorAction(TextView view, int action, KeyEvent keyEvent) { |
| 1157 | if (action == EditorInfo.IME_ACTION_DONE) { |
| 1158 | focusBody(); |
| 1159 | return true; |
| 1160 | } |
| 1161 | return false; |
| 1162 | } |
| 1163 | |
Mindy Pereira | e011b1d | 2012-06-18 13:45:26 -0700 | [diff] [blame] | 1164 | protected TextView getBody() { |
| 1165 | return mBodyView; |
| 1166 | } |
| 1167 | |
| 1168 | @VisibleForTesting |
Andy Huang | 0a2a346 | 2013-12-20 15:56:13 -0800 | [diff] [blame] | 1169 | public String getBodyHtml() { |
| 1170 | return Html.toHtml(removeComposingSpans(mBodyView.getText())); |
| 1171 | } |
| 1172 | |
| 1173 | @VisibleForTesting |
Mindy Pereira | e011b1d | 2012-06-18 13:45:26 -0700 | [diff] [blame] | 1174 | public Account getFromAccount() { |
| 1175 | return mReplyFromAccount != null && mReplyFromAccount.account != null ? |
| 1176 | mReplyFromAccount.account : mAccount; |
| 1177 | } |
| 1178 | |
Mindy Pereira | cbfb75a | 2012-06-25 14:52:23 -0700 | [diff] [blame] | 1179 | private void clearChangeListeners() { |
| 1180 | mSubject.removeTextChangedListener(this); |
| 1181 | mBodyView.removeTextChangedListener(this); |
| 1182 | mTo.removeTextChangedListener(mToListener); |
| 1183 | mCc.removeTextChangedListener(mCcListener); |
| 1184 | mBcc.removeTextChangedListener(mBccListener); |
| 1185 | mFromSpinner.setOnAccountChangedListener(null); |
| 1186 | mAttachmentsView.setAttachmentChangesListener(null); |
| 1187 | } |
| 1188 | |
Mindy Pereira | 75f6663 | 2012-01-11 11:42:02 -0800 | [diff] [blame] | 1189 | // Now that the message has been initialized from any existing draft or |
| 1190 | // ref message data, set up listeners for any changes that occur to the |
| 1191 | // message. |
| 1192 | private void initChangeListeners() { |
mindyp | 1d7e914 | 2012-11-21 13:54:30 -0800 | [diff] [blame] | 1193 | // Make sure we only add text changed listeners once! |
| 1194 | clearChangeListeners(); |
Mindy Pereira | 75f6663 | 2012-01-11 11:42:02 -0800 | [diff] [blame] | 1195 | mSubject.addTextChangedListener(this); |
| 1196 | mBodyView.addTextChangedListener(this); |
Mindy Pereira | cbfb75a | 2012-06-25 14:52:23 -0700 | [diff] [blame] | 1197 | if (mToListener == null) { |
| 1198 | mToListener = new RecipientTextWatcher(mTo, this); |
| 1199 | } |
| 1200 | mTo.addTextChangedListener(mToListener); |
| 1201 | if (mCcListener == null) { |
| 1202 | mCcListener = new RecipientTextWatcher(mCc, this); |
| 1203 | } |
| 1204 | mCc.addTextChangedListener(mCcListener); |
| 1205 | if (mBccListener == null) { |
| 1206 | mBccListener = new RecipientTextWatcher(mBcc, this); |
| 1207 | } |
| 1208 | mBcc.addTextChangedListener(mBccListener); |
Mindy Pereira | 75f6663 | 2012-01-11 11:42:02 -0800 | [diff] [blame] | 1209 | mFromSpinner.setOnAccountChangedListener(this); |
Mindy Pereira | 818143e | 2012-01-11 13:59:49 -0800 | [diff] [blame] | 1210 | mAttachmentsView.setAttachmentChangesListener(this); |
Mindy Pereira | 75f6663 | 2012-01-11 11:42:02 -0800 | [diff] [blame] | 1211 | } |
| 1212 | |
Alice Yang | a990a71 | 2013-03-13 18:37:00 -0700 | [diff] [blame] | 1213 | private void initActionBar() { |
| 1214 | LogUtils.d(LOG_TAG, "initializing action bar in ComposeActivity"); |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 1215 | ActionBar actionBar = getActionBar(); |
Mindy Pereira | e011b1d | 2012-06-18 13:45:26 -0700 | [diff] [blame] | 1216 | if (actionBar == null) { |
| 1217 | return; |
| 1218 | } |
Alice Yang | a990a71 | 2013-03-13 18:37:00 -0700 | [diff] [blame] | 1219 | if (mComposeMode == ComposeActivity.COMPOSE) { |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 1220 | actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); |
| 1221 | actionBar.setTitle(R.string.compose); |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 1222 | } else { |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 1223 | actionBar.setTitle(null); |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 1224 | if (mComposeModeAdapter == null) { |
| 1225 | mComposeModeAdapter = new ComposeModeAdapter(this); |
| 1226 | } |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 1227 | actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); |
| 1228 | actionBar.setListNavigationCallbacks(mComposeModeAdapter, this); |
Alice Yang | a990a71 | 2013-03-13 18:37:00 -0700 | [diff] [blame] | 1229 | switch (mComposeMode) { |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 1230 | case ComposeActivity.REPLY: |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 1231 | actionBar.setSelectedNavigationItem(0); |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 1232 | break; |
| 1233 | case ComposeActivity.REPLY_ALL: |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 1234 | actionBar.setSelectedNavigationItem(1); |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 1235 | break; |
| 1236 | case ComposeActivity.FORWARD: |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 1237 | actionBar.setSelectedNavigationItem(2); |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 1238 | break; |
| 1239 | } |
| 1240 | } |
Mindy Pereira | fbe4019 | 2012-03-20 10:40:45 -0700 | [diff] [blame] | 1241 | actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME, |
| 1242 | ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME); |
| 1243 | actionBar.setHomeButtonEnabled(true); |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 1244 | } |
| 1245 | |
Scott Kennedy | 0aeaf7d | 2012-11-14 18:56:05 -0800 | [diff] [blame] | 1246 | private void initFromRefMessage(int action) { |
| 1247 | setFieldsFromRefMessage(action); |
Alice Yang | 1ebc2db | 2013-03-14 21:21:44 -0700 | [diff] [blame] | 1248 | |
| 1249 | // Check if To: address and email body needs to be prefilled based on extras. |
| 1250 | // This is used for reporting rendering feedback. |
| 1251 | if (MessageHeaderView.ENABLE_REPORT_RENDERING_PROBLEM) { |
| 1252 | Intent intent = getIntent(); |
| 1253 | if (intent.getExtras() != null) { |
| 1254 | String toAddresses = intent.getStringExtra(EXTRA_TO); |
| 1255 | if (toAddresses != null) { |
| 1256 | addToAddresses(Arrays.asList(TextUtils.split(toAddresses, ","))); |
| 1257 | } |
| 1258 | String body = intent.getStringExtra(EXTRA_BODY); |
| 1259 | if (body != null) { |
| 1260 | setBody(body, false /* withSignature */); |
| 1261 | } |
| 1262 | } |
| 1263 | } |
| 1264 | |
Mindy Pereira | 96a7f7a | 2012-07-09 16:51:06 -0700 | [diff] [blame] | 1265 | if (mRefMessage != null) { |
| 1266 | // CC field only gets populated when doing REPLY_ALL. |
| 1267 | // BCC never gets auto-populated, unless the user is editing |
| 1268 | // a draft with one. |
Mindy Pereira | 29a717e | 2012-07-25 18:05:48 -0700 | [diff] [blame] | 1269 | if (!TextUtils.isEmpty(mCc.getText()) && action == REPLY_ALL) { |
Mindy Pereira | 96a7f7a | 2012-07-09 16:51:06 -0700 | [diff] [blame] | 1270 | mCcBccView.show(false, true, false); |
| 1271 | } |
| 1272 | } |
| 1273 | updateHideOrShowCcBcc(); |
| 1274 | } |
| 1275 | |
Scott Kennedy | 0aeaf7d | 2012-11-14 18:56:05 -0800 | [diff] [blame] | 1276 | private void setFieldsFromRefMessage(int action) { |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 1277 | setSubject(mRefMessage, action); |
| 1278 | // Setup recipients |
| 1279 | if (action == FORWARD) { |
| 1280 | mForward = true; |
Mindy Pereira | 6349a04 | 2012-01-04 11:25:01 -0800 | [diff] [blame] | 1281 | } |
Scott Kennedy | 0aeaf7d | 2012-11-14 18:56:05 -0800 | [diff] [blame] | 1282 | initRecipientsFromRefMessage(mRefMessage, action); |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 1283 | initQuotedTextFromRefMessage(mRefMessage, action); |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 1284 | if (action == ComposeActivity.FORWARD || mAttachmentsChanged) { |
| 1285 | initAttachments(mRefMessage); |
| 1286 | } |
Mindy Pereira | c17d073 | 2011-12-29 10:46:19 -0800 | [diff] [blame] | 1287 | } |
| 1288 | |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 1289 | private void initFromDraftMessage(Message message) { |
Andy Huang | 1f8f4dd | 2012-10-25 21:35:35 -0700 | [diff] [blame] | 1290 | LogUtils.d(LOG_TAG, "Intializing draft from previous draft message: %s", message); |
Paul Westbrook | bb87b7f | 2012-03-20 16:20:07 -0700 | [diff] [blame] | 1291 | |
| 1292 | mDraft = message; |
| 1293 | mDraftId = message.id; |
| 1294 | mSubject.setText(message.subject); |
| 1295 | mForward = message.draftType == UIProvider.DraftType.FORWARD; |
Tony Mantler | 9016a5e | 2013-07-19 11:54:17 -0700 | [diff] [blame] | 1296 | final List<String> toAddresses = Arrays.asList(message.getToAddressesUnescaped()); |
Paul Westbrook | bb87b7f | 2012-03-20 16:20:07 -0700 | [diff] [blame] | 1297 | addToAddresses(toAddresses); |
Tony Mantler | 9016a5e | 2013-07-19 11:54:17 -0700 | [diff] [blame] | 1298 | addCcAddresses(Arrays.asList(message.getCcAddressesUnescaped()), toAddresses); |
| 1299 | addBccAddresses(Arrays.asList(message.getBccAddressesUnescaped())); |
Mindy Pereira | 2421dc8 | 2012-03-27 13:32:31 -0700 | [diff] [blame] | 1300 | if (message.hasAttachments) { |
| 1301 | List<Attachment> attachments = message.getAttachments(); |
| 1302 | for (Attachment a : attachments) { |
Andy Huang | 5c5fd57 | 2012-04-08 18:19:29 -0700 | [diff] [blame] | 1303 | addAttachmentAndUpdateView(a); |
Mindy Pereira | 2421dc8 | 2012-03-27 13:32:31 -0700 | [diff] [blame] | 1304 | } |
| 1305 | } |
Mindy Pereira | cc8e7db | 2012-05-30 12:57:42 -0700 | [diff] [blame] | 1306 | int quotedTextIndex = message.appendRefMessageContent ? |
Mindy Pereira | 002ff52 | 2012-05-30 10:31:26 -0700 | [diff] [blame] | 1307 | message.quotedTextOffset : -1; |
Paul Westbrook | bb87b7f | 2012-03-20 16:20:07 -0700 | [diff] [blame] | 1308 | // Set the body |
Mindy Pereira | 002ff52 | 2012-05-30 10:31:26 -0700 | [diff] [blame] | 1309 | CharSequence quotedText = null; |
Paul Westbrook | bb87b7f | 2012-03-20 16:20:07 -0700 | [diff] [blame] | 1310 | if (!TextUtils.isEmpty(message.bodyHtml)) { |
Mindy Pereira | 752222d | 2012-07-19 09:58:53 -0700 | [diff] [blame] | 1311 | CharSequence htmlText = ""; |
Mindy Pereira | 002ff52 | 2012-05-30 10:31:26 -0700 | [diff] [blame] | 1312 | if (quotedTextIndex > -1) { |
Mindy Pereira | 752222d | 2012-07-19 09:58:53 -0700 | [diff] [blame] | 1313 | // Find the offset in the htmltext of the actual quoted text and strip it out. |
| 1314 | quotedTextIndex = QuotedTextView.findQuotedTextIndex(message.bodyHtml); |
| 1315 | if (quotedTextIndex > -1) { |
mindyp | c59dd82 | 2012-11-13 10:56:21 -0800 | [diff] [blame] | 1316 | htmlText = Utils.convertHtmlToPlainText(message.bodyHtml.substring(0, |
| 1317 | quotedTextIndex)); |
Mindy Pereira | 752222d | 2012-07-19 09:58:53 -0700 | [diff] [blame] | 1318 | quotedText = message.bodyHtml.subSequence(quotedTextIndex, |
| 1319 | message.bodyHtml.length()); |
| 1320 | } |
Mindy Pereira | 1a6e938 | 2012-08-14 15:51:22 -0700 | [diff] [blame] | 1321 | } else { |
mindyp | c59dd82 | 2012-11-13 10:56:21 -0800 | [diff] [blame] | 1322 | htmlText = Utils.convertHtmlToPlainText(message.bodyHtml); |
Mindy Pereira | 002ff52 | 2012-05-30 10:31:26 -0700 | [diff] [blame] | 1323 | } |
| 1324 | mBodyView.setText(htmlText); |
Paul Westbrook | bb87b7f | 2012-03-20 16:20:07 -0700 | [diff] [blame] | 1325 | } else { |
Mindy Pereira | 752222d | 2012-07-19 09:58:53 -0700 | [diff] [blame] | 1326 | final String body = message.bodyText; |
| 1327 | final CharSequence bodyText = !TextUtils.isEmpty(body) ? |
| 1328 | (quotedTextIndex > -1 ? |
| 1329 | message.bodyText.substring(0, quotedTextIndex) : message.bodyText) |
| 1330 | : ""; |
Mindy Pereira | 002ff52 | 2012-05-30 10:31:26 -0700 | [diff] [blame] | 1331 | if (quotedTextIndex > -1) { |
Mindy Pereira | 752222d | 2012-07-19 09:58:53 -0700 | [diff] [blame] | 1332 | quotedText = !TextUtils.isEmpty(body) ? message.bodyText.substring(quotedTextIndex) |
| 1333 | : null; |
Mindy Pereira | 002ff52 | 2012-05-30 10:31:26 -0700 | [diff] [blame] | 1334 | } |
| 1335 | mBodyView.setText(bodyText); |
| 1336 | } |
| 1337 | if (quotedTextIndex > -1 && quotedText != null) { |
Mindy Pereira | 3971323 | 2012-05-30 11:48:41 -0700 | [diff] [blame] | 1338 | mQuotedTextView.setQuotedTextFromDraft(quotedText, mForward); |
Paul Westbrook | bb87b7f | 2012-03-20 16:20:07 -0700 | [diff] [blame] | 1339 | } |
Paul Westbrook | bb87b7f | 2012-03-20 16:20:07 -0700 | [diff] [blame] | 1340 | } |
| 1341 | |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 1342 | /** |
| 1343 | * Fill all the widgets with the content found in the Intent Extra, if any. |
| 1344 | * Also apply the same style to all widgets. Note: if initFromExtras is |
| 1345 | * called as a result of switching between reply, reply all, and forward per |
| 1346 | * the latest revision of Gmail, and the user has already made changes to |
| 1347 | * attachments on a previous incarnation of the message (as a reply, reply |
| 1348 | * all, or forward), the original attachments from the message will not be |
| 1349 | * re-instantiated. The user's changes will be respected. This follows the |
| 1350 | * web gmail interaction. |
Andrew Sapperstein | 746d861 | 2013-08-26 15:56:32 -0700 | [diff] [blame] | 1351 | * @return {@code true} if the activity should not call {@link #finishSetup}. |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 1352 | */ |
Andrew Sapperstein | 746d861 | 2013-08-26 15:56:32 -0700 | [diff] [blame] | 1353 | public boolean initFromExtras(Intent intent) { |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 1354 | // If we were invoked with a SENDTO intent, the value |
| 1355 | // should take precedence |
| 1356 | final Uri dataUri = intent.getData(); |
| 1357 | if (dataUri != null) { |
| 1358 | if (MAIL_TO.equals(dataUri.getScheme())) { |
| 1359 | initFromMailTo(dataUri.toString()); |
| 1360 | } else { |
Mindy Pereira | 0b4f28e | 2012-03-28 14:12:21 -0700 | [diff] [blame] | 1361 | if (!mAccount.composeIntentUri.equals(dataUri)) { |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 1362 | String toText = dataUri.getSchemeSpecificPart(); |
| 1363 | if (toText != null) { |
| 1364 | mTo.setText(""); |
Mindy Pereira | dbe8996 | 2012-04-13 09:42:38 -0700 | [diff] [blame] | 1365 | addToAddresses(Arrays.asList(TextUtils.split(toText, ","))); |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 1366 | } |
| 1367 | } |
| 1368 | } |
| 1369 | } |
| 1370 | |
| 1371 | String[] extraStrings = intent.getStringArrayExtra(Intent.EXTRA_EMAIL); |
| 1372 | if (extraStrings != null) { |
| 1373 | addToAddresses(Arrays.asList(extraStrings)); |
| 1374 | } |
| 1375 | extraStrings = intent.getStringArrayExtra(Intent.EXTRA_CC); |
| 1376 | if (extraStrings != null) { |
| 1377 | addCcAddresses(Arrays.asList(extraStrings), null); |
| 1378 | } |
| 1379 | extraStrings = intent.getStringArrayExtra(Intent.EXTRA_BCC); |
| 1380 | if (extraStrings != null) { |
| 1381 | addBccAddresses(Arrays.asList(extraStrings)); |
| 1382 | } |
| 1383 | |
| 1384 | String extraString = intent.getStringExtra(Intent.EXTRA_SUBJECT); |
| 1385 | if (extraString != null) { |
| 1386 | mSubject.setText(extraString); |
| 1387 | } |
| 1388 | |
| 1389 | for (String extra : ALL_EXTRAS) { |
| 1390 | if (intent.hasExtra(extra)) { |
| 1391 | String value = intent.getStringExtra(extra); |
| 1392 | if (EXTRA_TO.equals(extra)) { |
Mindy Pereira | dbe8996 | 2012-04-13 09:42:38 -0700 | [diff] [blame] | 1393 | addToAddresses(Arrays.asList(TextUtils.split(value, ","))); |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 1394 | } else if (EXTRA_CC.equals(extra)) { |
Mindy Pereira | dbe8996 | 2012-04-13 09:42:38 -0700 | [diff] [blame] | 1395 | addCcAddresses(Arrays.asList(TextUtils.split(value, ",")), null); |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 1396 | } else if (EXTRA_BCC.equals(extra)) { |
Mindy Pereira | dbe8996 | 2012-04-13 09:42:38 -0700 | [diff] [blame] | 1397 | addBccAddresses(Arrays.asList(TextUtils.split(value, ","))); |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 1398 | } else if (EXTRA_SUBJECT.equals(extra)) { |
| 1399 | mSubject.setText(value); |
| 1400 | } else if (EXTRA_BODY.equals(extra)) { |
| 1401 | setBody(value, true /* with signature */); |
Andrew Sapperstein | bc5e0dc | 2013-08-07 21:15:22 -0700 | [diff] [blame] | 1402 | } else if (EXTRA_QUOTED_TEXT.equals(extra)) { |
| 1403 | initQuotedText(value, true /* shouldQuoteText */); |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 1404 | } |
| 1405 | } |
| 1406 | } |
| 1407 | |
| 1408 | Bundle extras = intent.getExtras(); |
| 1409 | if (extras != null) { |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 1410 | CharSequence text = extras.getCharSequence(Intent.EXTRA_TEXT); |
| 1411 | if (text != null) { |
| 1412 | setBody(text, true /* with signature */); |
| 1413 | } |
Andrew Sapperstein | bc5e0dc | 2013-08-07 21:15:22 -0700 | [diff] [blame] | 1414 | |
| 1415 | // TODO - support EXTRA_HTML_TEXT |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 1416 | } |
Andrew Sapperstein | 746d861 | 2013-08-26 15:56:32 -0700 | [diff] [blame] | 1417 | |
| 1418 | mExtraValues = intent.getParcelableExtra(EXTRA_VALUES); |
| 1419 | if (mExtraValues != null) { |
| 1420 | LogUtils.d(LOG_TAG, "Launched with extra values: %s", mExtraValues.toString()); |
| 1421 | initExtraValues(mExtraValues); |
| 1422 | return true; |
| 1423 | } |
| 1424 | |
| 1425 | return false; |
| 1426 | } |
| 1427 | |
| 1428 | protected void initExtraValues(ContentValues extraValues) { |
| 1429 | // DO NOTHING - Gmail will override |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 1430 | } |
| 1431 | |
Andrew Sapperstein | bc5e0dc | 2013-08-07 21:15:22 -0700 | [diff] [blame] | 1432 | |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 1433 | @VisibleForTesting |
| 1434 | protected String decodeEmailInUri(String s) throws UnsupportedEncodingException { |
Mindy Pereira | a4069f2 | 2012-05-30 15:31:45 -0700 | [diff] [blame] | 1435 | // TODO: handle the case where there are spaces in the display name as |
| 1436 | // well as the email such as "Guy with spaces <guy+with+spaces@gmail.com>" |
| 1437 | // as they could be encoded ambiguously. |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 1438 | // Since URLDecode.decode changes + into ' ', and + is a valid |
| 1439 | // email character, we need to find/ replace these ourselves before |
| 1440 | // decoding. |
Mindy Pereira | a4069f2 | 2012-05-30 15:31:45 -0700 | [diff] [blame] | 1441 | try { |
Yorke Lee | 7dd05b1 | 2013-04-25 10:04:43 -0700 | [diff] [blame] | 1442 | return URLDecoder.decode(replacePlus(s), UTF8_ENCODING_NAME); |
Mindy Pereira | a4069f2 | 2012-05-30 15:31:45 -0700 | [diff] [blame] | 1443 | } catch (IllegalArgumentException e) { |
| 1444 | if (LogUtils.isLoggable(LOG_TAG, LogUtils.VERBOSE)) { |
| 1445 | LogUtils.e(LOG_TAG, "%s while decoding '%s'", e.getMessage(), s); |
| 1446 | } else { |
| 1447 | LogUtils.e(LOG_TAG, e, "Exception while decoding mailto address"); |
| 1448 | } |
| 1449 | return null; |
| 1450 | } |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 1451 | } |
| 1452 | |
| 1453 | /** |
Yorke Lee | 7dd05b1 | 2013-04-25 10:04:43 -0700 | [diff] [blame] | 1454 | * Replaces all occurrences of '+' with "%2B", to prevent URLDecode.decode from |
| 1455 | * changing '+' into ' ' |
| 1456 | * |
| 1457 | * @param toReplace Input string |
| 1458 | * @return The string with all "+" characters replaced with "%2B" |
| 1459 | */ |
Scott Kennedy | 3b965d7 | 2013-06-25 14:36:55 -0700 | [diff] [blame] | 1460 | private static String replacePlus(String toReplace) { |
Yorke Lee | 7dd05b1 | 2013-04-25 10:04:43 -0700 | [diff] [blame] | 1461 | return toReplace.replace("+", "%2B"); |
| 1462 | } |
| 1463 | |
| 1464 | /** |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 1465 | * Initialize the compose view from a String representing a mailTo uri. |
| 1466 | * @param mailToString The uri as a string. |
| 1467 | */ |
| 1468 | public void initFromMailTo(String mailToString) { |
| 1469 | // We need to disguise this string as a URI in order to parse it |
| 1470 | // TODO: Remove this hack when http://b/issue?id=1445295 gets fixed |
| 1471 | Uri uri = Uri.parse("foo://" + mailToString); |
| 1472 | int index = mailToString.indexOf("?"); |
| 1473 | int length = "mailto".length() + 1; |
| 1474 | String to; |
| 1475 | try { |
| 1476 | // Extract the recipient after mailto: |
| 1477 | if (index == -1) { |
| 1478 | to = decodeEmailInUri(mailToString.substring(length)); |
| 1479 | } else { |
| 1480 | to = decodeEmailInUri(mailToString.substring(length, index)); |
| 1481 | } |
Mindy Pereira | a4069f2 | 2012-05-30 15:31:45 -0700 | [diff] [blame] | 1482 | if (!TextUtils.isEmpty(to)) { |
| 1483 | addToAddresses(Arrays.asList(TextUtils.split(to, ","))); |
| 1484 | } |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 1485 | } catch (UnsupportedEncodingException e) { |
| 1486 | if (LogUtils.isLoggable(LOG_TAG, LogUtils.VERBOSE)) { |
| 1487 | LogUtils.e(LOG_TAG, "%s while decoding '%s'", e.getMessage(), mailToString); |
| 1488 | } else { |
| 1489 | LogUtils.e(LOG_TAG, e, "Exception while decoding mailto address"); |
| 1490 | } |
| 1491 | } |
| 1492 | |
| 1493 | List<String> cc = uri.getQueryParameters("cc"); |
| 1494 | addCcAddresses(Arrays.asList(cc.toArray(new String[cc.size()])), null); |
| 1495 | |
| 1496 | List<String> otherTo = uri.getQueryParameters("to"); |
| 1497 | addToAddresses(Arrays.asList(otherTo.toArray(new String[otherTo.size()]))); |
| 1498 | |
| 1499 | List<String> bcc = uri.getQueryParameters("bcc"); |
| 1500 | addBccAddresses(Arrays.asList(bcc.toArray(new String[bcc.size()]))); |
| 1501 | |
| 1502 | List<String> subject = uri.getQueryParameters("subject"); |
| 1503 | if (subject.size() > 0) { |
| 1504 | try { |
Yorke Lee | 7dd05b1 | 2013-04-25 10:04:43 -0700 | [diff] [blame] | 1505 | mSubject.setText(URLDecoder.decode(replacePlus(subject.get(0)), |
| 1506 | UTF8_ENCODING_NAME)); |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 1507 | } catch (UnsupportedEncodingException e) { |
| 1508 | LogUtils.e(LOG_TAG, "%s while decoding subject '%s'", |
| 1509 | e.getMessage(), subject); |
| 1510 | } |
| 1511 | } |
| 1512 | |
| 1513 | List<String> body = uri.getQueryParameters("body"); |
| 1514 | if (body.size() > 0) { |
| 1515 | try { |
Yorke Lee | 7dd05b1 | 2013-04-25 10:04:43 -0700 | [diff] [blame] | 1516 | setBody(URLDecoder.decode(replacePlus(body.get(0)), UTF8_ENCODING_NAME), |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 1517 | true /* with signature */); |
| 1518 | } catch (UnsupportedEncodingException e) { |
| 1519 | LogUtils.e(LOG_TAG, "%s while decoding body '%s'", e.getMessage(), body); |
| 1520 | } |
| 1521 | } |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 1522 | } |
| 1523 | |
Mindy Pereira | bddd6f3 | 2012-06-20 12:10:03 -0700 | [diff] [blame] | 1524 | @VisibleForTesting |
| 1525 | protected void initAttachments(Message refMessage) { |
Mark Wei | 434f294 | 2012-08-24 11:54:02 -0700 | [diff] [blame] | 1526 | addAttachments(refMessage.getAttachments()); |
| 1527 | } |
| 1528 | |
| 1529 | public long addAttachments(List<Attachment> attachments) { |
| 1530 | long size = 0; |
| 1531 | AttachmentFailureException error = null; |
| 1532 | for (Attachment a : attachments) { |
| 1533 | try { |
| 1534 | size += mAttachmentsView.addAttachment(mAccount, a); |
| 1535 | } catch (AttachmentFailureException e) { |
| 1536 | error = e; |
| 1537 | } |
Mindy Pereira | 3cd4f40 | 2012-07-17 11:16:18 -0700 | [diff] [blame] | 1538 | } |
Mark Wei | 434f294 | 2012-08-24 11:54:02 -0700 | [diff] [blame] | 1539 | if (error != null) { |
| 1540 | LogUtils.e(LOG_TAG, error, "Error adding attachment"); |
| 1541 | if (attachments.size() > 1) { |
| 1542 | showAttachmentTooBigToast(R.string.too_large_to_attach_multiple); |
| 1543 | } else { |
| 1544 | showAttachmentTooBigToast(error.getErrorRes()); |
| 1545 | } |
| 1546 | } |
| 1547 | return size; |
Mindy Pereira | 3cd4f40 | 2012-07-17 11:16:18 -0700 | [diff] [blame] | 1548 | } |
| 1549 | |
| 1550 | /** |
| 1551 | * When an attachment is too large to be added to a message, show a toast. |
| 1552 | * This method also updates the position of the toast so that it is shown |
| 1553 | * clearly above they keyboard if it happens to be open. |
| 1554 | */ |
Mark Wei | 434f294 | 2012-08-24 11:54:02 -0700 | [diff] [blame] | 1555 | private void showAttachmentTooBigToast(int errorRes) { |
| 1556 | String maxSize = AttachmentUtils.convertToHumanReadableSize( |
| 1557 | getApplicationContext(), mAccount.settings.getMaxAttachmentSize()); |
| 1558 | showErrorToast(getString(errorRes, maxSize)); |
Mindy Pereira | 3cd4f40 | 2012-07-17 11:16:18 -0700 | [diff] [blame] | 1559 | } |
| 1560 | |
Mark Wei | 434f294 | 2012-08-24 11:54:02 -0700 | [diff] [blame] | 1561 | private void showErrorToast(String message) { |
| 1562 | Toast t = Toast.makeText(this, message, Toast.LENGTH_LONG); |
| 1563 | t.setText(message); |
Mindy Pereira | 3cd4f40 | 2012-07-17 11:16:18 -0700 | [diff] [blame] | 1564 | t.setGravity(Gravity.CENTER_HORIZONTAL, 0, |
| 1565 | getResources().getDimensionPixelSize(R.dimen.attachment_toast_yoffset)); |
| 1566 | t.show(); |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 1567 | } |
| 1568 | |
Paul Westbrook | f97588b | 2012-03-20 11:11:37 -0700 | [diff] [blame] | 1569 | private void initAttachmentsFromIntent(Intent intent) { |
Paul Westbrook | 03ee971 | 2012-04-02 09:51:51 -0700 | [diff] [blame] | 1570 | Bundle extras = intent.getExtras(); |
| 1571 | if (extras == null) { |
| 1572 | extras = Bundle.EMPTY; |
| 1573 | } |
Paul Westbrook | f97588b | 2012-03-20 11:11:37 -0700 | [diff] [blame] | 1574 | final String action = intent.getAction(); |
| 1575 | if (!mAttachmentsChanged) { |
| 1576 | long totalSize = 0; |
| 1577 | if (extras.containsKey(EXTRA_ATTACHMENTS)) { |
| 1578 | String[] uris = (String[]) extras.getSerializable(EXTRA_ATTACHMENTS); |
| 1579 | for (String uriString : uris) { |
| 1580 | final Uri uri = Uri.parse(uriString); |
| 1581 | long size = 0; |
| 1582 | try { |
Andy Huang | e003b4c | 2013-08-16 10:32:05 -0700 | [diff] [blame] | 1583 | final Attachment a = mAttachmentsView.generateLocalAttachment(uri); |
| 1584 | size = mAttachmentsView.addAttachment(mAccount, a); |
| 1585 | |
| 1586 | Analytics.getInstance().sendEvent("send_intent_attachment", |
| 1587 | Utils.normalizeMimeType(a.getContentType()), null, size); |
| 1588 | |
Paul Westbrook | f97588b | 2012-03-20 11:11:37 -0700 | [diff] [blame] | 1589 | } catch (AttachmentFailureException e) { |
Paul Westbrook | f97588b | 2012-03-20 11:11:37 -0700 | [diff] [blame] | 1590 | LogUtils.e(LOG_TAG, e, "Error adding attachment"); |
Mark Wei | 434f294 | 2012-08-24 11:54:02 -0700 | [diff] [blame] | 1591 | showAttachmentTooBigToast(e.getErrorRes()); |
Paul Westbrook | f97588b | 2012-03-20 11:11:37 -0700 | [diff] [blame] | 1592 | } |
| 1593 | totalSize += size; |
| 1594 | } |
| 1595 | } |
mindyp | 9a9e8d6 | 2012-10-03 12:24:07 -0700 | [diff] [blame] | 1596 | if (extras.containsKey(Intent.EXTRA_STREAM)) { |
| 1597 | if (Intent.ACTION_SEND_MULTIPLE.equals(action)) { |
| 1598 | ArrayList<Parcelable> uris = extras |
| 1599 | .getParcelableArrayList(Intent.EXTRA_STREAM); |
| 1600 | ArrayList<Attachment> attachments = new ArrayList<Attachment>(); |
| 1601 | for (Parcelable uri : uris) { |
| 1602 | try { |
Andy Huang | e003b4c | 2013-08-16 10:32:05 -0700 | [diff] [blame] | 1603 | final Attachment a = mAttachmentsView.generateLocalAttachment( |
| 1604 | (Uri) uri); |
| 1605 | attachments.add(a); |
| 1606 | |
| 1607 | Analytics.getInstance().sendEvent("send_intent_attachment", |
| 1608 | Utils.normalizeMimeType(a.getContentType()), null, a.size); |
| 1609 | |
mindyp | 9a9e8d6 | 2012-10-03 12:24:07 -0700 | [diff] [blame] | 1610 | } catch (AttachmentFailureException e) { |
| 1611 | LogUtils.e(LOG_TAG, e, "Error adding attachment"); |
| 1612 | String maxSize = AttachmentUtils.convertToHumanReadableSize( |
| 1613 | getApplicationContext(), |
| 1614 | mAccount.settings.getMaxAttachmentSize()); |
| 1615 | showErrorToast(getString |
| 1616 | (R.string.generic_attachment_problem, maxSize)); |
| 1617 | } |
| 1618 | } |
| 1619 | totalSize += addAttachments(attachments); |
| 1620 | } else { |
| 1621 | final Uri uri = (Uri) extras.getParcelable(Intent.EXTRA_STREAM); |
| 1622 | long size = 0; |
Paul Westbrook | f97588b | 2012-03-20 11:11:37 -0700 | [diff] [blame] | 1623 | try { |
Andy Huang | e003b4c | 2013-08-16 10:32:05 -0700 | [diff] [blame] | 1624 | final Attachment a = mAttachmentsView.generateLocalAttachment(uri); |
| 1625 | size = mAttachmentsView.addAttachment(mAccount, a); |
| 1626 | |
| 1627 | Analytics.getInstance().sendEvent("send_intent_attachment", |
| 1628 | Utils.normalizeMimeType(a.getContentType()), null, size); |
| 1629 | |
Paul Westbrook | f97588b | 2012-03-20 11:11:37 -0700 | [diff] [blame] | 1630 | } catch (AttachmentFailureException e) { |
Paul Westbrook | f97588b | 2012-03-20 11:11:37 -0700 | [diff] [blame] | 1631 | LogUtils.e(LOG_TAG, e, "Error adding attachment"); |
mindyp | 9a9e8d6 | 2012-10-03 12:24:07 -0700 | [diff] [blame] | 1632 | showAttachmentTooBigToast(e.getErrorRes()); |
Paul Westbrook | f97588b | 2012-03-20 11:11:37 -0700 | [diff] [blame] | 1633 | } |
mindyp | 9a9e8d6 | 2012-10-03 12:24:07 -0700 | [diff] [blame] | 1634 | totalSize += size; |
Paul Westbrook | f97588b | 2012-03-20 11:11:37 -0700 | [diff] [blame] | 1635 | } |
| 1636 | } |
| 1637 | |
| 1638 | if (totalSize > 0) { |
| 1639 | mAttachmentsChanged = true; |
| 1640 | updateSaveUi(); |
Andy Huang | e003b4c | 2013-08-16 10:32:05 -0700 | [diff] [blame] | 1641 | |
| 1642 | Analytics.getInstance().sendEvent("send_intent_with_attachments", |
| 1643 | Integer.toString(getAttachments().size()), null, totalSize); |
Paul Westbrook | f97588b | 2012-03-20 11:11:37 -0700 | [diff] [blame] | 1644 | } |
| 1645 | } |
| 1646 | } |
| 1647 | |
Andrew Sapperstein | 746d861 | 2013-08-26 15:56:32 -0700 | [diff] [blame] | 1648 | protected void initQuotedText(CharSequence quotedText, boolean shouldQuoteText) { |
Andrew Sapperstein | bc5e0dc | 2013-08-07 21:15:22 -0700 | [diff] [blame] | 1649 | mQuotedTextView.setQuotedTextFromHtml(quotedText, shouldQuoteText); |
| 1650 | mShowQuotedText = true; |
| 1651 | } |
Paul Westbrook | f97588b | 2012-03-20 11:11:37 -0700 | [diff] [blame] | 1652 | |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 1653 | private void initQuotedTextFromRefMessage(Message refMessage, int action) { |
| 1654 | if (mRefMessage != null && (action == REPLY || action == REPLY_ALL || action == FORWARD)) { |
Mindy Pereira | 9932dee | 2012-01-10 16:09:50 -0800 | [diff] [blame] | 1655 | mQuotedTextView.setQuotedText(action, refMessage, action != FORWARD); |
| 1656 | } |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 1657 | } |
| 1658 | |
| 1659 | private void updateHideOrShowCcBcc() { |
Mindy Pereira | ec8b0ed | 2012-01-06 10:35:59 -0800 | [diff] [blame] | 1660 | // Its possible there is a menu item OR a button. |
Mindy Pereira | 326689d | 2012-05-17 10:14:14 -0700 | [diff] [blame] | 1661 | boolean ccVisible = mCcBccView.isCcVisible(); |
| 1662 | boolean bccVisible = mCcBccView.isBccVisible(); |
Mindy Pereira | ec8b0ed | 2012-01-06 10:35:59 -0800 | [diff] [blame] | 1663 | if (mCcBccButton != null) { |
Mindy Pereira | 326689d | 2012-05-17 10:14:14 -0700 | [diff] [blame] | 1664 | if (!ccVisible || !bccVisible) { |
Mindy Pereira | ec8b0ed | 2012-01-06 10:35:59 -0800 | [diff] [blame] | 1665 | mCcBccButton.setVisibility(View.VISIBLE); |
Mindy Pereira | 326689d | 2012-05-17 10:14:14 -0700 | [diff] [blame] | 1666 | mCcBccButton.setText(getString(!ccVisible ? R.string.add_cc_label |
Mindy Pereira | ec8b0ed | 2012-01-06 10:35:59 -0800 | [diff] [blame] | 1667 | : R.string.add_bcc_label)); |
| 1668 | } else { |
mindyp | cd0b0b9 | 2012-08-23 14:33:17 -0700 | [diff] [blame] | 1669 | mCcBccButton.setVisibility(View.INVISIBLE); |
Mindy Pereira | ec8b0ed | 2012-01-06 10:35:59 -0800 | [diff] [blame] | 1670 | } |
| 1671 | } |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 1672 | } |
| 1673 | |
Mindy Pereira | a34c9a0 | 2012-04-17 14:10:53 -0700 | [diff] [blame] | 1674 | private void showCcBcc(Bundle state) { |
Mindy Pereira | 326689d | 2012-05-17 10:14:14 -0700 | [diff] [blame] | 1675 | if (state != null && state.containsKey(EXTRA_SHOW_CC)) { |
| 1676 | boolean showCc = state.getBoolean(EXTRA_SHOW_CC); |
| 1677 | boolean showBcc = state.getBoolean(EXTRA_SHOW_BCC); |
| 1678 | if (showCc || showBcc) { |
| 1679 | mCcBccView.show(false, showCc, showBcc); |
Mindy Pereira | 6faeedf | 2012-04-18 16:11:39 -0700 | [diff] [blame] | 1680 | } |
Mindy Pereira | a34c9a0 | 2012-04-17 14:10:53 -0700 | [diff] [blame] | 1681 | } |
| 1682 | } |
| 1683 | |
Mindy Pereira | 013194c | 2012-01-06 15:09:33 -0800 | [diff] [blame] | 1684 | /** |
| 1685 | * Add attachment and update the compose area appropriately. |
| 1686 | * @param data |
| 1687 | */ |
| 1688 | public void addAttachmentAndUpdateView(Intent data) { |
Andrew Sapperstein | 05089f3 | 2013-10-01 17:00:03 -0700 | [diff] [blame] | 1689 | if (data == null) { |
| 1690 | return; |
| 1691 | } |
| 1692 | |
| 1693 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { |
| 1694 | final ClipData clipData = data.getClipData(); |
| 1695 | if (clipData != null) { |
| 1696 | for (int i = 0, size = clipData.getItemCount(); i < size; i++) { |
| 1697 | addAttachmentAndUpdateView(clipData.getItemAt(i).getUri()); |
| 1698 | } |
| 1699 | return; |
| 1700 | } |
| 1701 | } |
| 1702 | |
| 1703 | addAttachmentAndUpdateView(data.getData()); |
Mindy Pereira | 2421dc8 | 2012-03-27 13:32:31 -0700 | [diff] [blame] | 1704 | } |
| 1705 | |
Andy Huang | 5c5fd57 | 2012-04-08 18:19:29 -0700 | [diff] [blame] | 1706 | public void addAttachmentAndUpdateView(Uri contentUri) { |
| 1707 | if (contentUri == null) { |
Mindy Pereira | 2421dc8 | 2012-03-27 13:32:31 -0700 | [diff] [blame] | 1708 | return; |
| 1709 | } |
Mindy Pereira | 013194c | 2012-01-06 15:09:33 -0800 | [diff] [blame] | 1710 | try { |
Andy Huang | 5c5fd57 | 2012-04-08 18:19:29 -0700 | [diff] [blame] | 1711 | addAttachmentAndUpdateView(mAttachmentsView.generateLocalAttachment(contentUri)); |
| 1712 | } catch (AttachmentFailureException e) { |
Andy Huang | 5c5fd57 | 2012-04-08 18:19:29 -0700 | [diff] [blame] | 1713 | LogUtils.e(LOG_TAG, e, "Error adding attachment"); |
Mark Wei | 434f294 | 2012-08-24 11:54:02 -0700 | [diff] [blame] | 1714 | showErrorToast(getResources().getString( |
| 1715 | e.getErrorRes(), |
| 1716 | AttachmentUtils.convertToHumanReadableSize( |
| 1717 | getApplicationContext(), mAccount.settings.getMaxAttachmentSize()))); |
Andy Huang | 5c5fd57 | 2012-04-08 18:19:29 -0700 | [diff] [blame] | 1718 | } |
| 1719 | } |
| 1720 | |
| 1721 | public void addAttachmentAndUpdateView(Attachment attachment) { |
| 1722 | try { |
Mark Wei | 434f294 | 2012-08-24 11:54:02 -0700 | [diff] [blame] | 1723 | long size = mAttachmentsView.addAttachment(mAccount, attachment); |
Mindy Pereira | 9932dee | 2012-01-10 16:09:50 -0800 | [diff] [blame] | 1724 | if (size > 0) { |
| 1725 | mAttachmentsChanged = true; |
| 1726 | updateSaveUi(); |
Mindy Pereira | 013194c | 2012-01-06 15:09:33 -0800 | [diff] [blame] | 1727 | } |
Mindy Pereira | 9932dee | 2012-01-10 16:09:50 -0800 | [diff] [blame] | 1728 | } catch (AttachmentFailureException e) { |
Mindy Pereira | 9932dee | 2012-01-10 16:09:50 -0800 | [diff] [blame] | 1729 | LogUtils.e(LOG_TAG, e, "Error adding attachment"); |
Mark Wei | 434f294 | 2012-08-24 11:54:02 -0700 | [diff] [blame] | 1730 | showAttachmentTooBigToast(e.getErrorRes()); |
Mindy Pereira | 013194c | 2012-01-06 15:09:33 -0800 | [diff] [blame] | 1731 | } |
| 1732 | } |
| 1733 | |
Scott Kennedy | 0aeaf7d | 2012-11-14 18:56:05 -0800 | [diff] [blame] | 1734 | void initRecipientsFromRefMessage(Message refMessage, int action) { |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 1735 | // Don't populate the address if this is a forward. |
| 1736 | if (action == ComposeActivity.FORWARD) { |
| 1737 | return; |
| 1738 | } |
Scott Kennedy | ff8553f | 2013-04-05 20:57:44 -0700 | [diff] [blame] | 1739 | initReplyRecipients(refMessage, action); |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 1740 | } |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 1741 | |
Paul Westbrook | 6d2442b | 2013-07-17 17:51:51 -0700 | [diff] [blame] | 1742 | // TODO: This should be private. This method shouldn't be used by ComposeActivityTests, as |
| 1743 | // it doesn't setup the state of the activity correctly |
Mindy Pereira | 818143e | 2012-01-11 13:59:49 -0800 | [diff] [blame] | 1744 | @VisibleForTesting |
Scott Kennedy | ff8553f | 2013-04-05 20:57:44 -0700 | [diff] [blame] | 1745 | void initReplyRecipients(final Message refMessage, final int action) { |
Tony Mantler | 9016a5e | 2013-07-19 11:54:17 -0700 | [diff] [blame] | 1746 | String[] sentToAddresses = refMessage.getToAddressesUnescaped(); |
Mindy Pereira | a26b54e | 2012-01-06 12:54:33 -0800 | [diff] [blame] | 1747 | final Collection<String> toAddresses; |
Tony Mantler | 89de9eb | 2013-07-25 11:43:58 -0700 | [diff] [blame] | 1748 | final String[] fromAddresses = refMessage.getFromAddressesUnescaped(); |
| 1749 | final String fromAddress = fromAddresses.length > 0 ? fromAddresses[0] : null; |
Andy Huang | e2af887 | 2014-01-16 12:36:27 -0800 | [diff] [blame] | 1750 | final String[] replyToAddresses = getReplyToAddresses( |
| 1751 | refMessage.getReplyToAddressesUnescaped(), fromAddress); |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 1752 | |
| 1753 | // If this is a reply, the Cc list is empty. If this is a reply-all, the |
| 1754 | // Cc list is the union of the To and Cc recipients of the original |
| 1755 | // message, excluding the current user's email address and any addresses |
Mindy Pereira | a26b54e | 2012-01-06 12:54:33 -0800 | [diff] [blame] | 1756 | // already on the To list. |
| 1757 | if (action == ComposeActivity.REPLY) { |
Tony Mantler | 24f116f | 2014-01-16 10:20:50 -0800 | [diff] [blame] | 1758 | toAddresses = initToRecipients(fromAddress, replyToAddresses, sentToAddresses); |
Mindy Pereira | a26b54e | 2012-01-06 12:54:33 -0800 | [diff] [blame] | 1759 | addToAddresses(toAddresses); |
| 1760 | } else if (action == ComposeActivity.REPLY_ALL) { |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 1761 | final Set<String> ccAddresses = Sets.newHashSet(); |
Tony Mantler | 24f116f | 2014-01-16 10:20:50 -0800 | [diff] [blame] | 1762 | toAddresses = initToRecipients(fromAddress, replyToAddresses, sentToAddresses); |
Mindy Pereira | 154386a | 2012-01-11 13:02:33 -0800 | [diff] [blame] | 1763 | addToAddresses(toAddresses); |
Scott Kennedy | ff8553f | 2013-04-05 20:57:44 -0700 | [diff] [blame] | 1764 | addRecipients(ccAddresses, sentToAddresses); |
Tony Mantler | 9016a5e | 2013-07-19 11:54:17 -0700 | [diff] [blame] | 1765 | addRecipients(ccAddresses, refMessage.getCcAddressesUnescaped()); |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 1766 | addCcAddresses(ccAddresses, toAddresses); |
| 1767 | } |
| 1768 | } |
| 1769 | |
Andy Huang | e2af887 | 2014-01-16 12:36:27 -0800 | [diff] [blame] | 1770 | // If there is no reply to address, the reply to address is the sender. |
| 1771 | private static String[] getReplyToAddresses(String[] replyTo, String from) { |
| 1772 | boolean hasReplyTo = false; |
| 1773 | for (final String replyToAddress : replyTo) { |
| 1774 | if (!TextUtils.isEmpty(replyToAddress)) { |
| 1775 | hasReplyTo = true; |
| 1776 | } |
| 1777 | } |
| 1778 | return hasReplyTo ? replyTo : new String[] {from}; |
| 1779 | } |
| 1780 | |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 1781 | private void addToAddresses(Collection<String> addresses) { |
| 1782 | addAddressesToList(addresses, mTo); |
| 1783 | } |
| 1784 | |
| 1785 | private void addCcAddresses(Collection<String> addresses, Collection<String> toAddresses) { |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 1786 | addCcAddressesToList(tokenizeAddressList(addresses), |
| 1787 | toAddresses != null ? tokenizeAddressList(toAddresses) : null, mCc); |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 1788 | } |
| 1789 | |
Paul Westbrook | bb87b7f | 2012-03-20 16:20:07 -0700 | [diff] [blame] | 1790 | private void addBccAddresses(Collection<String> addresses) { |
| 1791 | addAddressesToList(addresses, mBcc); |
| 1792 | } |
| 1793 | |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 1794 | @VisibleForTesting |
| 1795 | protected void addCcAddressesToList(List<Rfc822Token[]> addresses, |
| 1796 | List<Rfc822Token[]> compareToList, RecipientEditTextView list) { |
| 1797 | String address; |
| 1798 | |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 1799 | if (compareToList == null) { |
| 1800 | for (Rfc822Token[] tokens : addresses) { |
| 1801 | for (int i = 0; i < tokens.length; i++) { |
| 1802 | address = tokens[i].toString(); |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 1803 | list.append(address + END_TOKEN); |
| 1804 | } |
| 1805 | } |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 1806 | } else { |
| 1807 | HashSet<String> compareTo = convertToHashSet(compareToList); |
| 1808 | for (Rfc822Token[] tokens : addresses) { |
| 1809 | for (int i = 0; i < tokens.length; i++) { |
| 1810 | address = tokens[i].toString(); |
| 1811 | // Check if this is a duplicate: |
| 1812 | if (!compareTo.contains(tokens[i].getAddress())) { |
| 1813 | // Get the address here |
| 1814 | list.append(address + END_TOKEN); |
| 1815 | } |
| 1816 | } |
| 1817 | } |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 1818 | } |
| 1819 | } |
| 1820 | |
Scott Kennedy | ff8553f | 2013-04-05 20:57:44 -0700 | [diff] [blame] | 1821 | private static HashSet<String> convertToHashSet(final List<Rfc822Token[]> list) { |
| 1822 | final HashSet<String> hash = new HashSet<String>(); |
| 1823 | for (final Rfc822Token[] tokens : list) { |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 1824 | for (int i = 0; i < tokens.length; i++) { |
| 1825 | hash.add(tokens[i].getAddress()); |
| 1826 | } |
| 1827 | } |
| 1828 | return hash; |
| 1829 | } |
| 1830 | |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 1831 | protected List<Rfc822Token[]> tokenizeAddressList(Collection<String> addresses) { |
| 1832 | @VisibleForTesting |
| 1833 | List<Rfc822Token[]> tokenized = new ArrayList<Rfc822Token[]>(); |
| 1834 | |
| 1835 | for (String address: addresses) { |
| 1836 | tokenized.add(Rfc822Tokenizer.tokenize(address)); |
| 1837 | } |
| 1838 | return tokenized; |
| 1839 | } |
| 1840 | |
| 1841 | @VisibleForTesting |
| 1842 | void addAddressesToList(Collection<String> addresses, RecipientEditTextView list) { |
| 1843 | for (String address : addresses) { |
| 1844 | addAddressToList(address, list); |
| 1845 | } |
| 1846 | } |
| 1847 | |
Scott Kennedy | ff8553f | 2013-04-05 20:57:44 -0700 | [diff] [blame] | 1848 | private static void addAddressToList(final String address, final RecipientEditTextView list) { |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 1849 | if (address == null || list == null) |
| 1850 | return; |
| 1851 | |
Scott Kennedy | ff8553f | 2013-04-05 20:57:44 -0700 | [diff] [blame] | 1852 | final Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(address); |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 1853 | |
| 1854 | for (int i = 0; i < tokens.length; i++) { |
| 1855 | list.append(tokens[i] + END_TOKEN); |
| 1856 | } |
| 1857 | } |
| 1858 | |
| 1859 | @VisibleForTesting |
Scott Kennedy | ff8553f | 2013-04-05 20:57:44 -0700 | [diff] [blame] | 1860 | protected Collection<String> initToRecipients(final String fullSenderAddress, |
Tony Mantler | 24f116f | 2014-01-16 10:20:50 -0800 | [diff] [blame] | 1861 | final String[] replyToAddresses, final String[] inToAddresses) { |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 1862 | // The To recipient is the reply-to address specified in the original |
| 1863 | // message, unless it is: |
| 1864 | // the current user OR a custom from of the current user, in which case |
| 1865 | // it's the To recipient list of the original message. |
| 1866 | // OR missing, in which case use the sender of the original message |
| 1867 | Set<String> toAddresses = Sets.newHashSet(); |
Tony Mantler | 24f116f | 2014-01-16 10:20:50 -0800 | [diff] [blame] | 1868 | for (final String replyToAddress : replyToAddresses) { |
| 1869 | if (!TextUtils.isEmpty(replyToAddress) |
| 1870 | && !recipientMatchesThisAccount(replyToAddress)) { |
| 1871 | toAddresses.add(replyToAddress); |
| 1872 | } |
| 1873 | } |
| 1874 | if (toAddresses.size() == 0) { |
mindyp | 65b06f5 | 2012-11-21 10:35:08 -0800 | [diff] [blame] | 1875 | // In this case, the user is replying to a message in which their |
Tony Mantler | 24f116f | 2014-01-16 10:20:50 -0800 | [diff] [blame] | 1876 | // current account or some of their custom from addresses are the only |
| 1877 | // recipients and they sent the original message. |
mindyp | 65b06f5 | 2012-11-21 10:35:08 -0800 | [diff] [blame] | 1878 | if (inToAddresses.length == 1 && recipientMatchesThisAccount(fullSenderAddress) |
| 1879 | && recipientMatchesThisAccount(inToAddresses[0])) { |
| 1880 | toAddresses.add(inToAddresses[0]); |
| 1881 | return toAddresses; |
| 1882 | } |
| 1883 | // This happens if the user replies to a message they originally |
| 1884 | // wrote. In this case, "reply" really means "re-send," so we |
| 1885 | // target the original recipients. This works as expected even |
| 1886 | // if the user sent the original message to themselves. |
| 1887 | for (String address : inToAddresses) { |
| 1888 | if (!recipientMatchesThisAccount(address)) { |
| 1889 | toAddresses.add(address); |
mindyp | fe8557b | 2012-11-05 12:05:16 -0800 | [diff] [blame] | 1890 | } |
Mindy Pereira | 1469b4e | 2012-06-19 19:18:54 -0700 | [diff] [blame] | 1891 | } |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 1892 | } |
| 1893 | return toAddresses; |
| 1894 | } |
| 1895 | |
Scott Kennedy | ff8553f | 2013-04-05 20:57:44 -0700 | [diff] [blame] | 1896 | private void addRecipients(final Set<String> recipients, final String[] addresses) { |
| 1897 | for (final String email : addresses) { |
Mindy Pereira | cecc54a | 2012-07-31 09:38:11 -0700 | [diff] [blame] | 1898 | // Do not add this account, or any of its custom from addresses, to |
| 1899 | // the list of recipients. |
Mindy Pereira | 4a20b70 | 2012-01-05 16:24:24 -0800 | [diff] [blame] | 1900 | final String recipientAddress = Address.getEmailAddress(email).getAddress(); |
mindyp | 5ee5d69 | 2012-11-19 16:02:16 -0800 | [diff] [blame] | 1901 | if (!recipientMatchesThisAccount(recipientAddress)) { |
Mindy Pereira | 4a27ea9 | 2012-01-05 15:55:25 -0800 | [diff] [blame] | 1902 | recipients.add(email.replace("\"\"", "")); |
| 1903 | } |
| 1904 | } |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 1905 | } |
| 1906 | |
Mindy Pereira | cecc54a | 2012-07-31 09:38:11 -0700 | [diff] [blame] | 1907 | /** |
| 1908 | * A recipient matches this account if it has the same address as the |
| 1909 | * currently selected account OR one of the custom from addresses associated |
| 1910 | * with the currently selected account. |
Mindy Pereira | cecc54a | 2012-07-31 09:38:11 -0700 | [diff] [blame] | 1911 | * @param recipientAddress address we are comparing with the currently selected account |
| 1912 | * @return |
| 1913 | */ |
mindyp | 5ee5d69 | 2012-11-19 16:02:16 -0800 | [diff] [blame] | 1914 | protected boolean recipientMatchesThisAccount(String recipientAddress) { |
| 1915 | return ReplyFromAccount.matchesAccountOrCustomFrom(mAccount, recipientAddress, |
mindyp | fe8557b | 2012-11-05 12:05:16 -0800 | [diff] [blame] | 1916 | mAccount.getReplyFroms()); |
Mindy Pereira | cecc54a | 2012-07-31 09:38:11 -0700 | [diff] [blame] | 1917 | } |
| 1918 | |
Andrew Sapperstein | bc5e0dc | 2013-08-07 21:15:22 -0700 | [diff] [blame] | 1919 | /** |
| 1920 | * Returns a formatted subject string with the appropriate prefix for the action type. |
| 1921 | * E.g., "FWD: " is prepended if action is {@link ComposeActivity#FORWARD}. |
| 1922 | */ |
Tony Mantler | a954f99 | 2013-12-03 11:22:56 -0800 | [diff] [blame] | 1923 | public static String buildFormattedSubject(final Resources res, final String subject, |
| 1924 | final int action) { |
| 1925 | final String prefix; |
| 1926 | final String correctedSubject; |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 1927 | if (action == ComposeActivity.COMPOSE) { |
| 1928 | prefix = ""; |
| 1929 | } else if (action == ComposeActivity.FORWARD) { |
Andrew Sapperstein | bc5e0dc | 2013-08-07 21:15:22 -0700 | [diff] [blame] | 1930 | prefix = res.getString(R.string.forward_subject_label); |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 1931 | } else { |
Andrew Sapperstein | bc5e0dc | 2013-08-07 21:15:22 -0700 | [diff] [blame] | 1932 | prefix = res.getString(R.string.reply_subject_label); |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 1933 | } |
| 1934 | |
| 1935 | // Don't duplicate the prefix |
Mindy Pereira | c7a3699 | 2012-07-30 14:00:37 -0700 | [diff] [blame] | 1936 | if (!TextUtils.isEmpty(subject) |
| 1937 | && subject.toLowerCase().startsWith(prefix.toLowerCase())) { |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 1938 | correctedSubject = subject; |
| 1939 | } else { |
Tony Mantler | a954f99 | 2013-12-03 11:22:56 -0800 | [diff] [blame] | 1940 | final String subjectOrNoSubject = TextUtils.isEmpty(subject) ? |
| 1941 | res.getString(R.string.no_subject) : |
| 1942 | subject; |
| 1943 | |
| 1944 | correctedSubject = |
| 1945 | res.getString(R.string.formatted_subject, prefix, subjectOrNoSubject); |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 1946 | } |
Andrew Sapperstein | bc5e0dc | 2013-08-07 21:15:22 -0700 | [diff] [blame] | 1947 | |
| 1948 | return correctedSubject; |
| 1949 | } |
| 1950 | |
| 1951 | private void setSubject(Message refMessage, int action) { |
| 1952 | mSubject.setText(buildFormattedSubject(getResources(), refMessage.subject, action)); |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 1953 | } |
| 1954 | |
Mindy Pereira | 818143e | 2012-01-11 13:59:49 -0800 | [diff] [blame] | 1955 | private void initRecipients() { |
| 1956 | setupRecipients(mTo); |
| 1957 | setupRecipients(mCc); |
| 1958 | setupRecipients(mBcc); |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 1959 | } |
| 1960 | |
Mindy Pereira | 818143e | 2012-01-11 13:59:49 -0800 | [diff] [blame] | 1961 | private void setupRecipients(RecipientEditTextView view) { |
Paul Westbrook | 679a8cc | 2012-02-21 16:37:58 -0800 | [diff] [blame] | 1962 | view.setAdapter(new RecipientAdapter(this, mAccount)); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1963 | if (mValidator == null) { |
Tony Mantler | 79b1156 | 2013-10-09 15:31:50 -0700 | [diff] [blame] | 1964 | final String accountName = mAccount.getEmailAddress(); |
Mindy Pereira | 33fe908 | 2012-01-09 16:24:30 -0800 | [diff] [blame] | 1965 | int offset = accountName.indexOf("@") + 1; |
| 1966 | String account = accountName; |
Tony Mantler | 79b1156 | 2013-10-09 15:31:50 -0700 | [diff] [blame] | 1967 | if (offset > 0) { |
| 1968 | account = account.substring(offset); |
Mindy Pereira | c17d073 | 2011-12-29 10:46:19 -0800 | [diff] [blame] | 1969 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1970 | mValidator = new Rfc822Validator(account); |
Mindy Pereira | c17d073 | 2011-12-29 10:46:19 -0800 | [diff] [blame] | 1971 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 1972 | view.setValidator(mValidator); |
Mindy Pereira | 8e9305e | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 1973 | } |
| 1974 | |
| 1975 | @Override |
| 1976 | public void onClick(View v) { |
Scott Kennedy | 2b9d80e | 2013-07-30 23:03:45 -0700 | [diff] [blame] | 1977 | final int id = v.getId(); |
| 1978 | if (id == R.id.add_cc_bcc) { |
| 1979 | // Verify that cc/ bcc aren't showing. |
| 1980 | // Animate in cc/bcc. |
| 1981 | showCcBccViews(); |
Andrew Sapperstein | 6aea786 | 2013-10-24 19:59:51 -0700 | [diff] [blame] | 1982 | } else if (id == R.id.add_attachment) { |
| 1983 | doAttach(Utils.isRunningKitkatOrLater() ? MIME_TYPE_ALL : MIME_TYPE_PHOTO); |
Mindy Pereira | 8e9305e | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 1984 | } |
| 1985 | } |
Mindy Pereira | b47f3e2 | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 1986 | |
| 1987 | @Override |
| 1988 | public boolean onCreateOptionsMenu(Menu menu) { |
Tony Mantler | 5b8799a | 2013-10-31 10:43:03 -0700 | [diff] [blame] | 1989 | final boolean superCreated = super.onCreateOptionsMenu(menu); |
Mindy Pereira | b199d17 | 2012-08-13 11:04:03 -0700 | [diff] [blame] | 1990 | // Don't render any menu items when there are no accounts. |
| 1991 | if (mAccounts == null || mAccounts.length == 0) { |
Tony Mantler | 5b8799a | 2013-10-31 10:43:03 -0700 | [diff] [blame] | 1992 | return superCreated; |
Mindy Pereira | b199d17 | 2012-08-13 11:04:03 -0700 | [diff] [blame] | 1993 | } |
Mindy Pereira | b47f3e2 | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 1994 | MenuInflater inflater = getMenuInflater(); |
| 1995 | inflater.inflate(R.menu.compose_menu, menu); |
mindyp | 1d7e914 | 2012-11-21 13:54:30 -0800 | [diff] [blame] | 1996 | |
| 1997 | /* |
| 1998 | * Start save in the correct enabled state. |
| 1999 | * 1) If a user launches compose from within gmail, save is disabled |
| 2000 | * until they add something, at which point, save is enabled, auto save |
| 2001 | * on exit; if the user empties everything, save is disabled, exiting does not |
| 2002 | * auto-save |
| 2003 | * 2) if a user replies/ reply all/ forwards from within gmail, save is |
| 2004 | * disabled until they change something, at which point, save is |
| 2005 | * enabled, auto save on exit; if the user empties everything, save is |
| 2006 | * disabled, exiting does not auto-save. |
| 2007 | * 3) If a user launches compose from another application and something |
| 2008 | * gets populated (attachments, recipients, body, subject, etc), save is |
| 2009 | * enabled, auto save on exit; if the user empties everything, save is |
| 2010 | * disabled, exiting does not auto-save |
| 2011 | */ |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2012 | mSave = menu.findItem(R.id.save); |
mindyp | 1d7e914 | 2012-11-21 13:54:30 -0800 | [diff] [blame] | 2013 | String action = getIntent() != null ? getIntent().getAction() : null; |
Andy Huang | 9f855d6 | 2013-05-30 17:15:03 -0700 | [diff] [blame] | 2014 | enableSave(mInnerSavedState != null ? |
| 2015 | mInnerSavedState.getBoolean(EXTRA_SAVE_ENABLED) |
mindyp | 1d7e914 | 2012-11-21 13:54:30 -0800 | [diff] [blame] | 2016 | : (Intent.ACTION_SEND.equals(action) |
| 2017 | || Intent.ACTION_SEND_MULTIPLE.equals(action) |
| 2018 | || Intent.ACTION_SENDTO.equals(action) |
| 2019 | || shouldSave())); |
| 2020 | |
Mindy Pereira | 3ca5bad | 2012-04-16 11:02:42 -0700 | [diff] [blame] | 2021 | MenuItem helpItem = menu.findItem(R.id.help_info_menu_item); |
| 2022 | MenuItem sendFeedbackItem = menu.findItem(R.id.feedback_menu_item); |
| 2023 | if (helpItem != null) { |
| 2024 | helpItem.setVisible(mAccount != null |
| 2025 | && mAccount.supportsCapability(AccountCapabilities.HELP_CONTENT)); |
| 2026 | } |
| 2027 | if (sendFeedbackItem != null) { |
| 2028 | sendFeedbackItem.setVisible(mAccount != null |
| 2029 | && mAccount.supportsCapability(AccountCapabilities.SEND_FEEDBACK)); |
| 2030 | } |
Andrew Sapperstein | 5cb7180 | 2013-10-01 18:31:20 -0700 | [diff] [blame] | 2031 | |
Andrew Sapperstein | 8809f9f | 2013-10-11 16:13:35 -0700 | [diff] [blame] | 2032 | // Show attach picture on pre-K devices. |
| 2033 | menu.findItem(R.id.add_photo_attachment).setVisible(!Utils.isRunningKitkatOrLater()); |
Andrew Sapperstein | 5cb7180 | 2013-10-01 18:31:20 -0700 | [diff] [blame] | 2034 | |
Mindy Pereira | b47f3e2 | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 2035 | return true; |
| 2036 | } |
| 2037 | |
| 2038 | @Override |
Mindy Pereira | ec8b0ed | 2012-01-06 10:35:59 -0800 | [diff] [blame] | 2039 | public boolean onPrepareOptionsMenu(Menu menu) { |
| 2040 | MenuItem ccBcc = menu.findItem(R.id.add_cc_bcc); |
Mindy Pereira | 818143e | 2012-01-11 13:59:49 -0800 | [diff] [blame] | 2041 | if (ccBcc != null && mCc != null) { |
Mindy Pereira | ec8b0ed | 2012-01-06 10:35:59 -0800 | [diff] [blame] | 2042 | // Its possible there is a menu item OR a button. |
| 2043 | boolean ccFieldVisible = mCc.isShown(); |
| 2044 | boolean bccFieldVisible = mBcc.isShown(); |
| 2045 | if (!ccFieldVisible || !bccFieldVisible) { |
| 2046 | ccBcc.setVisible(true); |
| 2047 | ccBcc.setTitle(getString(!ccFieldVisible ? R.string.add_cc_label |
| 2048 | : R.string.add_bcc_label)); |
| 2049 | } else { |
| 2050 | ccBcc.setVisible(false); |
| 2051 | } |
| 2052 | } |
| 2053 | return true; |
| 2054 | } |
| 2055 | |
| 2056 | @Override |
Mindy Pereira | b47f3e2 | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 2057 | public boolean onOptionsItemSelected(MenuItem item) { |
Scott Kennedy | 2b9d80e | 2013-07-30 23:03:45 -0700 | [diff] [blame] | 2058 | final int id = item.getItemId(); |
Andy Huang | dc97bf4 | 2013-08-15 16:52:45 -0700 | [diff] [blame] | 2059 | |
| 2060 | Analytics.getInstance().sendMenuItemEvent(Analytics.EVENT_CATEGORY_MENU_ITEM, id, null, 0); |
| 2061 | |
Mindy Pereira | 75f6663 | 2012-01-11 11:42:02 -0800 | [diff] [blame] | 2062 | boolean handled = true; |
Andrew Sapperstein | 5cb7180 | 2013-10-01 18:31:20 -0700 | [diff] [blame] | 2063 | if (id == R.id.add_file_attachment) { |
| 2064 | doAttach(MIME_TYPE_ALL); |
| 2065 | } else if (id == R.id.add_photo_attachment) { |
Scott Kennedy | 2b9d80e | 2013-07-30 23:03:45 -0700 | [diff] [blame] | 2066 | doAttach(MIME_TYPE_PHOTO); |
Scott Kennedy | 2b9d80e | 2013-07-30 23:03:45 -0700 | [diff] [blame] | 2067 | } else if (id == R.id.add_cc_bcc) { |
| 2068 | showCcBccViews(); |
| 2069 | } else if (id == R.id.save) { |
| 2070 | doSave(true); |
| 2071 | } else if (id == R.id.send) { |
| 2072 | doSend(); |
| 2073 | } else if (id == R.id.discard) { |
| 2074 | doDiscard(); |
| 2075 | } else if (id == R.id.settings) { |
| 2076 | Utils.showSettings(this, mAccount); |
| 2077 | } else if (id == android.R.id.home) { |
| 2078 | onAppUpPressed(); |
| 2079 | } else if (id == R.id.help_info_menu_item) { |
| 2080 | Utils.showHelp(this, mAccount, getString(R.string.compose_help_context)); |
| 2081 | } else if (id == R.id.feedback_menu_item) { |
| 2082 | Utils.sendFeedback(this, mAccount, false); |
| 2083 | } else { |
| 2084 | handled = false; |
Mindy Pereira | b47f3e2 | 2011-12-13 14:25:04 -0800 | [diff] [blame] | 2085 | } |
| 2086 | return !handled ? super.onOptionsItemSelected(item) : handled; |
| 2087 | } |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 2088 | |
Mindy Pereira | b199d17 | 2012-08-13 11:04:03 -0700 | [diff] [blame] | 2089 | @Override |
| 2090 | public void onBackPressed() { |
| 2091 | // If we are showing the wait fragment, just exit. |
| 2092 | if (getWaitFragment() != null) { |
| 2093 | finish(); |
| 2094 | } else { |
| 2095 | super.onBackPressed(); |
| 2096 | } |
| 2097 | } |
| 2098 | |
Vikram Aggarwal | 1672ff8 | 2012-09-21 10:15:22 -0700 | [diff] [blame] | 2099 | /** |
| 2100 | * Carries out the "up" action in the action bar. |
| 2101 | */ |
Paul Westbrook | daecb4b | 2012-05-31 10:21:26 -0700 | [diff] [blame] | 2102 | private void onAppUpPressed() { |
| 2103 | if (mLaunchedFromEmail) { |
| 2104 | // If this was started from Gmail, simply treat app up as the system back button, so |
| 2105 | // that the last view is restored. |
| 2106 | onBackPressed(); |
| 2107 | return; |
| 2108 | } |
| 2109 | |
| 2110 | // Fire the main activity to ensure it launches the "top" screen of mail. |
| 2111 | // Since the main Activity is singleTask, it should revive that task if it was already |
| 2112 | // started. |
Vikram Aggarwal | 0c3c205 | 2012-09-21 11:06:28 -0700 | [diff] [blame] | 2113 | final Intent mailIntent = Utils.createViewInboxIntent(mAccount); |
Paul Westbrook | daecb4b | 2012-05-31 10:21:26 -0700 | [diff] [blame] | 2114 | mailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | |
| 2115 | Intent.FLAG_ACTIVITY_TASK_ON_HOME); |
| 2116 | startActivity(mailIntent); |
| 2117 | finish(); |
| 2118 | } |
| 2119 | |
Mindy Pereira | 33fe908 | 2012-01-09 16:24:30 -0800 | [diff] [blame] | 2120 | private void doSend() { |
Mark Wei | dd19b63 | 2012-10-19 13:59:28 -0700 | [diff] [blame] | 2121 | sendOrSaveWithSanityChecks(false, true, false, false); |
Andy Huang | dc97bf4 | 2013-08-15 16:52:45 -0700 | [diff] [blame] | 2122 | logSendOrSave(false /* save */); |
| 2123 | mPerformedSendOrDiscard = true; |
Mindy Pereira | 33fe908 | 2012-01-09 16:24:30 -0800 | [diff] [blame] | 2124 | } |
| 2125 | |
Mindy Pereira | 48e31b0 | 2012-05-30 13:12:24 -0700 | [diff] [blame] | 2126 | private void doSave(boolean showToast) { |
Mark Wei | dd19b63 | 2012-10-19 13:59:28 -0700 | [diff] [blame] | 2127 | sendOrSaveWithSanityChecks(true, showToast, false, false); |
Mindy Pereira | 48e31b0 | 2012-05-30 13:12:24 -0700 | [diff] [blame] | 2128 | } |
| 2129 | |
Mindy Pereira | e011b1d | 2012-06-18 13:45:26 -0700 | [diff] [blame] | 2130 | @VisibleForTesting |
| 2131 | public interface SendOrSaveCallback { |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2132 | public void initializeSendOrSave(SendOrSaveTask sendOrSaveTask); |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 2133 | public void notifyMessageIdAllocated(SendOrSaveMessage sendOrSaveMessage, Message message); |
| 2134 | public Message getMessage(); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2135 | public void sendOrSaveFinished(SendOrSaveTask sendOrSaveTask, boolean success); |
| 2136 | } |
| 2137 | |
Mindy Pereira | e011b1d | 2012-06-18 13:45:26 -0700 | [diff] [blame] | 2138 | @VisibleForTesting |
| 2139 | public static class SendOrSaveTask implements Runnable { |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2140 | private final Context mContext; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2141 | @VisibleForTesting |
Mindy Pereira | e011b1d | 2012-06-18 13:45:26 -0700 | [diff] [blame] | 2142 | public final SendOrSaveCallback mSendOrSaveCallback; |
| 2143 | @VisibleForTesting |
| 2144 | public final SendOrSaveMessage mSendOrSaveMessage; |
mindyp | 44a6339 | 2012-11-05 12:05:16 -0800 | [diff] [blame] | 2145 | private ReplyFromAccount mExistingDraftAccount; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2146 | |
| 2147 | public SendOrSaveTask(Context context, SendOrSaveMessage message, |
mindyp | 44a6339 | 2012-11-05 12:05:16 -0800 | [diff] [blame] | 2148 | SendOrSaveCallback callback, ReplyFromAccount draftAccount) { |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2149 | mContext = context; |
| 2150 | mSendOrSaveCallback = callback; |
| 2151 | mSendOrSaveMessage = message; |
mindyp | 44a6339 | 2012-11-05 12:05:16 -0800 | [diff] [blame] | 2152 | mExistingDraftAccount = draftAccount; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2153 | } |
| 2154 | |
| 2155 | @Override |
| 2156 | public void run() { |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 2157 | final SendOrSaveMessage sendOrSaveMessage = mSendOrSaveMessage; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2158 | |
Mindy Pereira | 92551d0 | 2012-04-05 11:31:12 -0700 | [diff] [blame] | 2159 | final ReplyFromAccount selectedAccount = sendOrSaveMessage.mAccount; |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 2160 | Message message = mSendOrSaveCallback.getMessage(); |
| 2161 | long messageId = message != null ? message.id : UIProvider.INVALID_MESSAGE_ID; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2162 | // If a previous draft has been saved, in an account that is different |
| 2163 | // than what the user wants to send from, remove the old draft, and treat this |
| 2164 | // as a new message |
mindyp | 44a6339 | 2012-11-05 12:05:16 -0800 | [diff] [blame] | 2165 | if (mExistingDraftAccount != null |
| 2166 | && !selectedAccount.account.uri.equals(mExistingDraftAccount.account.uri)) { |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2167 | if (messageId != UIProvider.INVALID_MESSAGE_ID) { |
| 2168 | ContentResolver resolver = mContext.getContentResolver(); |
| 2169 | ContentValues values = new ContentValues(); |
| 2170 | values.put(BaseColumns._ID, messageId); |
mindyp | febd226 | 2012-11-13 17:45:09 -0800 | [diff] [blame] | 2171 | if (mExistingDraftAccount.account.expungeMessageUri != null) { |
| 2172 | new ContentProviderTask.UpdateTask() |
| 2173 | .run(resolver, mExistingDraftAccount.account.expungeMessageUri, |
| 2174 | values, null, null); |
Mindy Pereira | cfb7f33 | 2012-02-28 10:23:43 -0800 | [diff] [blame] | 2175 | } else { |
| 2176 | // TODO(mindyp) delete the conversation. |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 2177 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2178 | // reset messageId to 0, so a new message will be created |
| 2179 | messageId = UIProvider.INVALID_MESSAGE_ID; |
| 2180 | } |
| 2181 | } |
| 2182 | |
| 2183 | final long messageIdToSave = messageId; |
Scott Kennedy | ff8553f | 2013-04-05 20:57:44 -0700 | [diff] [blame] | 2184 | sendOrSaveMessage(messageIdToSave, sendOrSaveMessage, selectedAccount); |
Paul Westbrook | 72e2ea8 | 2012-10-22 16:25:22 -0700 | [diff] [blame] | 2185 | |
| 2186 | if (!sendOrSaveMessage.mSave) { |
Tony Mantler | 9f32423 | 2013-08-08 14:24:30 -0700 | [diff] [blame] | 2187 | incrementRecipientsTimesContacted(mContext, |
Paul Westbrook | 72e2ea8 | 2012-10-22 16:25:22 -0700 | [diff] [blame] | 2188 | (String) sendOrSaveMessage.mValues.get(UIProvider.MessageColumns.TO)); |
Tony Mantler | 9f32423 | 2013-08-08 14:24:30 -0700 | [diff] [blame] | 2189 | incrementRecipientsTimesContacted(mContext, |
Paul Westbrook | 72e2ea8 | 2012-10-22 16:25:22 -0700 | [diff] [blame] | 2190 | (String) sendOrSaveMessage.mValues.get(UIProvider.MessageColumns.CC)); |
Tony Mantler | 9f32423 | 2013-08-08 14:24:30 -0700 | [diff] [blame] | 2191 | incrementRecipientsTimesContacted(mContext, |
Paul Westbrook | 72e2ea8 | 2012-10-22 16:25:22 -0700 | [diff] [blame] | 2192 | (String) sendOrSaveMessage.mValues.get(UIProvider.MessageColumns.BCC)); |
| 2193 | } |
| 2194 | mSendOrSaveCallback.sendOrSaveFinished(SendOrSaveTask.this, true); |
| 2195 | } |
| 2196 | |
Tony Mantler | 9f32423 | 2013-08-08 14:24:30 -0700 | [diff] [blame] | 2197 | private static void incrementRecipientsTimesContacted(final Context context, |
| 2198 | final String addressString) { |
| 2199 | if (TextUtils.isEmpty(addressString)) { |
| 2200 | return; |
| 2201 | } |
| 2202 | final Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(addressString); |
| 2203 | final ArrayList<String> recipients = new ArrayList<String>(tokens.length); |
| 2204 | for (int i = 0; i < tokens.length;i++) { |
| 2205 | recipients.add(tokens[i].getAddress()); |
| 2206 | } |
| 2207 | final DataUsageStatUpdater statsUpdater = new DataUsageStatUpdater(context); |
| 2208 | statsUpdater.updateWithAddress(recipients); |
| 2209 | } |
| 2210 | |
Paul Westbrook | 72e2ea8 | 2012-10-22 16:25:22 -0700 | [diff] [blame] | 2211 | /** |
| 2212 | * Send or Save a message. |
| 2213 | */ |
Scott Kennedy | ff8553f | 2013-04-05 20:57:44 -0700 | [diff] [blame] | 2214 | private void sendOrSaveMessage(final long messageIdToSave, |
| 2215 | final SendOrSaveMessage sendOrSaveMessage, final ReplyFromAccount selectedAccount) { |
Paul Westbrook | 72e2ea8 | 2012-10-22 16:25:22 -0700 | [diff] [blame] | 2216 | final ContentResolver resolver = mContext.getContentResolver(); |
| 2217 | final boolean updateExistingMessage = messageIdToSave != UIProvider.INVALID_MESSAGE_ID; |
| 2218 | |
| 2219 | final String accountMethod = sendOrSaveMessage.mSave ? |
| 2220 | UIProvider.AccountCallMethods.SAVE_MESSAGE : |
| 2221 | UIProvider.AccountCallMethods.SEND_MESSAGE; |
| 2222 | |
Paul Westbrook | 3c7f94d | 2012-10-23 14:13:00 -0700 | [diff] [blame] | 2223 | try { |
| 2224 | if (updateExistingMessage) { |
| 2225 | sendOrSaveMessage.mValues.put(BaseColumns._ID, messageIdToSave); |
Paul Westbrook | 72e2ea8 | 2012-10-22 16:25:22 -0700 | [diff] [blame] | 2226 | |
Paul Westbrook | 013a23c | 2013-02-22 10:37:41 -0800 | [diff] [blame] | 2227 | callAccountSendSaveMethod(resolver, |
Paul Westbrook | 3c7f94d | 2012-10-23 14:13:00 -0700 | [diff] [blame] | 2228 | selectedAccount.account, accountMethod, sendOrSaveMessage); |
Paul Westbrook | 72e2ea8 | 2012-10-22 16:25:22 -0700 | [diff] [blame] | 2229 | } else { |
Paul Westbrook | 013a23c | 2013-02-22 10:37:41 -0800 | [diff] [blame] | 2230 | Uri messageUri = null; |
Paul Westbrook | 3c7f94d | 2012-10-23 14:13:00 -0700 | [diff] [blame] | 2231 | final Bundle result = callAccountSendSaveMethod(resolver, |
| 2232 | selectedAccount.account, accountMethod, sendOrSaveMessage); |
| 2233 | if (result != null) { |
| 2234 | // If a non-null value was returned, then the provider handled the call |
| 2235 | // method |
| 2236 | messageUri = result.getParcelable(UIProvider.MessageColumns.URI); |
Paul Westbrook | 3c7f94d | 2012-10-23 14:13:00 -0700 | [diff] [blame] | 2237 | } |
| 2238 | if (sendOrSaveMessage.mSave && messageUri != null) { |
| 2239 | final Cursor messageCursor = resolver.query(messageUri, |
| 2240 | UIProvider.MESSAGE_PROJECTION, null, null, null); |
| 2241 | if (messageCursor != null) { |
| 2242 | try { |
| 2243 | if (messageCursor.moveToFirst()) { |
| 2244 | // Broadcast notification that a new message has |
| 2245 | // been allocated |
| 2246 | mSendOrSaveCallback.notifyMessageIdAllocated(sendOrSaveMessage, |
| 2247 | new Message(messageCursor)); |
| 2248 | } |
| 2249 | } finally { |
| 2250 | messageCursor.close(); |
Paul Westbrook | ba55848 | 2012-03-19 11:00:24 -0700 | [diff] [blame] | 2251 | } |
Paul Westbrook | 3c7f94d | 2012-10-23 14:13:00 -0700 | [diff] [blame] | 2252 | } |
| 2253 | } |
| 2254 | } |
| 2255 | } finally { |
| 2256 | // Close any opened file descriptors |
| 2257 | closeOpenedAttachmentFds(sendOrSaveMessage); |
| 2258 | } |
| 2259 | } |
| 2260 | |
Scott Kennedy | ff8553f | 2013-04-05 20:57:44 -0700 | [diff] [blame] | 2261 | private static void closeOpenedAttachmentFds(final SendOrSaveMessage sendOrSaveMessage) { |
Paul Westbrook | 3c7f94d | 2012-10-23 14:13:00 -0700 | [diff] [blame] | 2262 | final Bundle openedFds = sendOrSaveMessage.attachmentFds(); |
| 2263 | if (openedFds != null) { |
| 2264 | final Set<String> keys = openedFds.keySet(); |
Scott Kennedy | ff8553f | 2013-04-05 20:57:44 -0700 | [diff] [blame] | 2265 | for (final String key : keys) { |
Paul Westbrook | 3c7f94d | 2012-10-23 14:13:00 -0700 | [diff] [blame] | 2266 | final ParcelFileDescriptor fd = openedFds.getParcelable(key); |
| 2267 | if (fd != null) { |
| 2268 | try { |
| 2269 | fd.close(); |
| 2270 | } catch (IOException e) { |
| 2271 | // Do nothing |
Paul Westbrook | ba55848 | 2012-03-19 11:00:24 -0700 | [diff] [blame] | 2272 | } |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 2273 | } |
| 2274 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2275 | } |
Paul Westbrook | 72e2ea8 | 2012-10-22 16:25:22 -0700 | [diff] [blame] | 2276 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2277 | |
Paul Westbrook | 72e2ea8 | 2012-10-22 16:25:22 -0700 | [diff] [blame] | 2278 | /** |
Andrew Sapperstein | bc5e0dc | 2013-08-07 21:15:22 -0700 | [diff] [blame] | 2279 | * Use the {@link ContentResolver#call} method to send or save the message. |
Paul Westbrook | 72e2ea8 | 2012-10-22 16:25:22 -0700 | [diff] [blame] | 2280 | * |
| 2281 | * If this was successful, this method will return an non-null Bundle instance |
| 2282 | */ |
Scott Kennedy | ff8553f | 2013-04-05 20:57:44 -0700 | [diff] [blame] | 2283 | private static Bundle callAccountSendSaveMethod(final ContentResolver resolver, |
| 2284 | final Account account, final String method, |
| 2285 | final SendOrSaveMessage sendOrSaveMessage) { |
Paul Westbrook | 72e2ea8 | 2012-10-22 16:25:22 -0700 | [diff] [blame] | 2286 | // Copy all of the values from the content values to the bundle |
| 2287 | final Bundle methodExtras = new Bundle(sendOrSaveMessage.mValues.size()); |
| 2288 | final Set<Entry<String, Object>> valueSet = sendOrSaveMessage.mValues.valueSet(); |
| 2289 | |
| 2290 | for (Entry<String, Object> entry : valueSet) { |
| 2291 | final Object entryValue = entry.getValue(); |
| 2292 | final String key = entry.getKey(); |
| 2293 | if (entryValue instanceof String) { |
| 2294 | methodExtras.putString(key, (String)entryValue); |
| 2295 | } else if (entryValue instanceof Boolean) { |
| 2296 | methodExtras.putBoolean(key, (Boolean)entryValue); |
| 2297 | } else if (entryValue instanceof Integer) { |
| 2298 | methodExtras.putInt(key, (Integer)entryValue); |
| 2299 | } else if (entryValue instanceof Long) { |
| 2300 | methodExtras.putLong(key, (Long)entryValue); |
| 2301 | } else { |
| 2302 | LogUtils.wtf(LOG_TAG, "Unexpected object type: %s", |
| 2303 | entryValue.getClass().getName()); |
| 2304 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2305 | } |
Paul Westbrook | 72e2ea8 | 2012-10-22 16:25:22 -0700 | [diff] [blame] | 2306 | |
Paul Westbrook | 3c7f94d | 2012-10-23 14:13:00 -0700 | [diff] [blame] | 2307 | // If the SendOrSaveMessage has some opened fds, add them to the bundle |
| 2308 | final Bundle fdMap = sendOrSaveMessage.attachmentFds(); |
| 2309 | if (fdMap != null) { |
| 2310 | methodExtras.putParcelable( |
| 2311 | UIProvider.SendOrSaveMethodParamKeys.OPENED_FD_MAP, fdMap); |
| 2312 | } |
| 2313 | |
Paul Westbrook | 72e2ea8 | 2012-10-22 16:25:22 -0700 | [diff] [blame] | 2314 | return resolver.call(account.uri, method, account.uri.toString(), methodExtras); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2315 | } |
| 2316 | } |
| 2317 | |
Mindy Pereira | e011b1d | 2012-06-18 13:45:26 -0700 | [diff] [blame] | 2318 | @VisibleForTesting |
| 2319 | public static class SendOrSaveMessage { |
Mindy Pereira | 92551d0 | 2012-04-05 11:31:12 -0700 | [diff] [blame] | 2320 | final ReplyFromAccount mAccount; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2321 | final ContentValues mValues; |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 2322 | final String mRefMessageId; |
Mindy Pereira | e011b1d | 2012-06-18 13:45:26 -0700 | [diff] [blame] | 2323 | @VisibleForTesting |
| 2324 | public final boolean mSave; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2325 | final int mRequestId; |
Paul Westbrook | 3c7f94d | 2012-10-23 14:13:00 -0700 | [diff] [blame] | 2326 | private final Bundle mAttachmentFds; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2327 | |
Paul Westbrook | 3c7f94d | 2012-10-23 14:13:00 -0700 | [diff] [blame] | 2328 | public SendOrSaveMessage(Context context, ReplyFromAccount account, ContentValues values, |
| 2329 | String refMessageId, List<Attachment> attachments, boolean save) { |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2330 | mAccount = account; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2331 | mValues = values; |
| 2332 | mRefMessageId = refMessageId; |
| 2333 | mSave = save; |
| 2334 | mRequestId = mValues.hashCode() ^ hashCode(); |
Paul Westbrook | 3c7f94d | 2012-10-23 14:13:00 -0700 | [diff] [blame] | 2335 | |
| 2336 | mAttachmentFds = initializeAttachmentFds(context, attachments); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2337 | } |
| 2338 | |
| 2339 | int requestId() { |
| 2340 | return mRequestId; |
| 2341 | } |
Paul Westbrook | 3c7f94d | 2012-10-23 14:13:00 -0700 | [diff] [blame] | 2342 | |
| 2343 | Bundle attachmentFds() { |
| 2344 | return mAttachmentFds; |
| 2345 | } |
| 2346 | |
| 2347 | /** |
| 2348 | * Opens {@link ParcelFileDescriptor} for each of the attachments. This method must be |
| 2349 | * called before the ComposeActivity finishes. |
| 2350 | * Note: The caller is responsible for closing these file descriptors. |
| 2351 | */ |
Scott Kennedy | ff8553f | 2013-04-05 20:57:44 -0700 | [diff] [blame] | 2352 | private static Bundle initializeAttachmentFds(final Context context, |
| 2353 | final List<Attachment> attachments) { |
Paul Westbrook | 3c7f94d | 2012-10-23 14:13:00 -0700 | [diff] [blame] | 2354 | if (attachments == null || attachments.size() == 0) { |
| 2355 | return null; |
| 2356 | } |
| 2357 | |
| 2358 | final Bundle result = new Bundle(attachments.size()); |
| 2359 | final ContentResolver resolver = context.getContentResolver(); |
| 2360 | |
| 2361 | for (Attachment attachment : attachments) { |
| 2362 | if (attachment == null || Utils.isEmpty(attachment.contentUri)) { |
| 2363 | continue; |
| 2364 | } |
| 2365 | |
| 2366 | ParcelFileDescriptor fileDescriptor; |
| 2367 | try { |
| 2368 | fileDescriptor = resolver.openFileDescriptor(attachment.contentUri, "r"); |
| 2369 | } catch (FileNotFoundException e) { |
| 2370 | LogUtils.e(LOG_TAG, e, "Exception attempting to open attachment"); |
| 2371 | fileDescriptor = null; |
Paul Westbrook | c537fd4 | 2013-02-20 11:10:03 -0800 | [diff] [blame] | 2372 | } catch (SecurityException e) { |
| 2373 | // We have encountered a security exception when attempting to open the file |
| 2374 | // specified by the content uri. If the attachment has been cached, this |
| 2375 | // isn't a problem, as even through the original permission may have been |
| 2376 | // revoked, we have cached the file. This will happen when saving/sending |
| 2377 | // a previously saved draft. |
| 2378 | // TODO(markwei): Expose whether the attachment has been cached through the |
| 2379 | // attachment object. This would allow us to limit when the log is made, as |
| 2380 | // if the attachment has been cached, this really isn't an error |
| 2381 | LogUtils.e(LOG_TAG, e, "Security Exception attempting to open attachment"); |
| 2382 | // Just set the file descriptor to null, as the underlying provider needs |
| 2383 | // to handle the file descriptor not being set. |
| 2384 | fileDescriptor = null; |
Paul Westbrook | 3c7f94d | 2012-10-23 14:13:00 -0700 | [diff] [blame] | 2385 | } |
| 2386 | |
| 2387 | if (fileDescriptor != null) { |
| 2388 | result.putParcelable(attachment.contentUri.toString(), fileDescriptor); |
| 2389 | } |
| 2390 | } |
| 2391 | |
| 2392 | return result; |
| 2393 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2394 | } |
| 2395 | |
| 2396 | /** |
| 2397 | * Get the to recipients. |
| 2398 | */ |
| 2399 | public String[] getToAddresses() { |
| 2400 | return getAddressesFromList(mTo); |
| 2401 | } |
| 2402 | |
| 2403 | /** |
| 2404 | * Get the cc recipients. |
| 2405 | */ |
| 2406 | public String[] getCcAddresses() { |
| 2407 | return getAddressesFromList(mCc); |
| 2408 | } |
| 2409 | |
| 2410 | /** |
| 2411 | * Get the bcc recipients. |
| 2412 | */ |
| 2413 | public String[] getBccAddresses() { |
| 2414 | return getAddressesFromList(mBcc); |
| 2415 | } |
| 2416 | |
| 2417 | public String[] getAddressesFromList(RecipientEditTextView list) { |
| 2418 | if (list == null) { |
| 2419 | return new String[0]; |
| 2420 | } |
| 2421 | Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(list.getText()); |
| 2422 | int count = tokens.length; |
| 2423 | String[] result = new String[count]; |
| 2424 | for (int i = 0; i < count; i++) { |
| 2425 | result[i] = tokens[i].toString(); |
| 2426 | } |
| 2427 | return result; |
| 2428 | } |
| 2429 | |
| 2430 | /** |
| 2431 | * Check for invalid email addresses. |
| 2432 | * @param to String array of email addresses to check. |
| 2433 | * @param wrongEmailsOut Emails addresses that were invalid. |
| 2434 | */ |
Scott Kennedy | ff8553f | 2013-04-05 20:57:44 -0700 | [diff] [blame] | 2435 | public void checkInvalidEmails(final String[] to, final List<String> wrongEmailsOut) { |
Mindy Pereira | e5f20bf | 2012-06-25 14:20:40 -0700 | [diff] [blame] | 2436 | if (mValidator == null) { |
| 2437 | return; |
| 2438 | } |
Scott Kennedy | ff8553f | 2013-04-05 20:57:44 -0700 | [diff] [blame] | 2439 | for (final String email : to) { |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2440 | if (!mValidator.isValid(email)) { |
| 2441 | wrongEmailsOut.add(email); |
| 2442 | } |
| 2443 | } |
| 2444 | } |
| 2445 | |
Tony Mantler | 2558b50 | 2013-07-09 10:53:34 -0700 | [diff] [blame] | 2446 | public static class RecipientErrorDialogFragment extends DialogFragment { |
Paul Westbrook | f0ea484 | 2013-08-13 16:41:18 -0700 | [diff] [blame] | 2447 | // Public no-args constructor needed for fragment re-instantiation |
| 2448 | public RecipientErrorDialogFragment() {} |
| 2449 | |
Tony Mantler | 2558b50 | 2013-07-09 10:53:34 -0700 | [diff] [blame] | 2450 | public static RecipientErrorDialogFragment newInstance(final String message) { |
| 2451 | final RecipientErrorDialogFragment frag = new RecipientErrorDialogFragment(); |
| 2452 | final Bundle args = new Bundle(1); |
| 2453 | args.putString("message", message); |
| 2454 | frag.setArguments(args); |
| 2455 | return frag; |
| 2456 | } |
| 2457 | |
| 2458 | @Override |
| 2459 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 2460 | final String message = getArguments().getString("message"); |
Andrew Sapperstein | 530ac7a | 2013-10-29 19:12:17 -0700 | [diff] [blame] | 2461 | return new AlertDialog.Builder(getActivity()) |
| 2462 | .setMessage(message) |
Tony Mantler | 2558b50 | 2013-07-09 10:53:34 -0700 | [diff] [blame] | 2463 | .setPositiveButton( |
| 2464 | R.string.ok, new Dialog.OnClickListener() { |
| 2465 | @Override |
| 2466 | public void onClick(DialogInterface dialog, int which) { |
| 2467 | ((ComposeActivity)getActivity()).finishRecipientErrorDialog(); |
| 2468 | } |
| 2469 | }).create(); |
| 2470 | } |
| 2471 | } |
| 2472 | |
| 2473 | private void finishRecipientErrorDialog() { |
| 2474 | // after the user dismisses the recipient error |
| 2475 | // dialog we want to make sure to refocus the |
| 2476 | // recipient to field so they can fix the issue |
| 2477 | // easily |
| 2478 | if (mTo != null) { |
| 2479 | mTo.requestFocus(); |
| 2480 | } |
| 2481 | } |
| 2482 | |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2483 | /** |
| 2484 | * Show an error because the user has entered an invalid recipient. |
| 2485 | * @param message |
| 2486 | */ |
Tony Mantler | 2558b50 | 2013-07-09 10:53:34 -0700 | [diff] [blame] | 2487 | private void showRecipientErrorDialog(final String message) { |
| 2488 | final DialogFragment frag = RecipientErrorDialogFragment.newInstance(message); |
| 2489 | frag.show(getFragmentManager(), "recipient error"); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2490 | } |
| 2491 | |
| 2492 | /** |
| 2493 | * Update the state of the UI based on whether or not the current draft |
| 2494 | * needs to be saved and the message is not empty. |
| 2495 | */ |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 2496 | public void updateSaveUi() { |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2497 | if (mSave != null) { |
| 2498 | mSave.setEnabled((shouldSave() && !isBlank())); |
| 2499 | } |
| 2500 | } |
| 2501 | |
| 2502 | /** |
| 2503 | * Returns true if we need to save the current draft. |
| 2504 | */ |
| 2505 | private boolean shouldSave() { |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 2506 | synchronized (mDraftLock) { |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2507 | // The message should only be saved if: |
| 2508 | // It hasn't been sent AND |
| 2509 | // Some text has been added to the message OR |
| 2510 | // an attachment has been added or removed |
Mindy Pereira | a214833 | 2012-07-02 13:54:14 -0700 | [diff] [blame] | 2511 | // AND there is actually something in the draft to save. |
Andy Huang | d47877e | 2012-08-09 19:31:24 -0700 | [diff] [blame] | 2512 | return (mTextChanged || mAttachmentsChanged || mReplyFromChanged) |
Mindy Pereira | a214833 | 2012-07-02 13:54:14 -0700 | [diff] [blame] | 2513 | && !isBlank(); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2514 | } |
| 2515 | } |
| 2516 | |
| 2517 | /** |
Mindy Pereira | bdf7a40 | 2012-03-01 15:23:26 -0800 | [diff] [blame] | 2518 | * Check if all fields are blank. |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2519 | * @return boolean |
| 2520 | */ |
| 2521 | public boolean isBlank() { |
Alice Yang | a49b684 | 2013-08-23 10:36:18 -0700 | [diff] [blame] | 2522 | // Need to check for null since isBlank() can be called from onPause() |
| 2523 | // before findViews() is called |
| 2524 | if (mSubject == null || mBodyView == null || mTo == null || mCc == null || |
| 2525 | mAttachmentsView == null) { |
| 2526 | LogUtils.w(LOG_TAG, "null views in isBlank check"); |
| 2527 | return true; |
| 2528 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2529 | return mSubject.getText().length() == 0 |
Mindy Pereira | bdf7a40 | 2012-03-01 15:23:26 -0800 | [diff] [blame] | 2530 | && (mBodyView.getText().length() == 0 || getSignatureStartPosition(mSignature, |
| 2531 | mBodyView.getText().toString()) == 0) |
| 2532 | && mTo.length() == 0 |
| 2533 | && mCc.length() == 0 && mBcc.length() == 0 |
| 2534 | && mAttachmentsView.getAttachments().size() == 0; |
| 2535 | } |
| 2536 | |
| 2537 | @VisibleForTesting |
| 2538 | protected int getSignatureStartPosition(String signature, String bodyText) { |
| 2539 | int startPos = -1; |
| 2540 | |
| 2541 | if (TextUtils.isEmpty(signature) || TextUtils.isEmpty(bodyText)) { |
| 2542 | return startPos; |
| 2543 | } |
| 2544 | |
| 2545 | int bodyLength = bodyText.length(); |
| 2546 | int signatureLength = signature.length(); |
| 2547 | String printableVersion = convertToPrintableSignature(signature); |
| 2548 | int printableLength = printableVersion.length(); |
| 2549 | |
| 2550 | if (bodyLength >= printableLength |
| 2551 | && bodyText.substring(bodyLength - printableLength) |
| 2552 | .equals(printableVersion)) { |
| 2553 | startPos = bodyLength - printableLength; |
| 2554 | } else if (bodyLength >= signatureLength |
| 2555 | && bodyText.substring(bodyLength - signatureLength) |
| 2556 | .equals(signature)) { |
| 2557 | startPos = bodyLength - signatureLength; |
| 2558 | } |
| 2559 | return startPos; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2560 | } |
| 2561 | |
| 2562 | /** |
| 2563 | * Allows any changes made by the user to be ignored. Called when the user |
| 2564 | * decides to discard a draft. |
| 2565 | */ |
| 2566 | private void discardChanges() { |
| 2567 | mTextChanged = false; |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 2568 | mAttachmentsChanged = false; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2569 | mReplyFromChanged = false; |
| 2570 | } |
| 2571 | |
| 2572 | /** |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2573 | * @param save |
| 2574 | * @param showToast |
| 2575 | * @return Whether the send or save succeeded. |
| 2576 | */ |
| 2577 | protected boolean sendOrSaveWithSanityChecks(final boolean save, final boolean showToast, |
Mark Wei | dd19b63 | 2012-10-19 13:59:28 -0700 | [diff] [blame] | 2578 | final boolean orientationChanged, final boolean autoSend) { |
Mark Wei | 009b371 | 2012-10-18 18:07:50 -0700 | [diff] [blame] | 2579 | if (mAccounts == null || mAccount == null) { |
| 2580 | Toast.makeText(this, R.string.send_failed, Toast.LENGTH_SHORT).show(); |
Mark Wei | dd19b63 | 2012-10-19 13:59:28 -0700 | [diff] [blame] | 2581 | if (autoSend) { |
| 2582 | finish(); |
| 2583 | } |
Mark Wei | 009b371 | 2012-10-18 18:07:50 -0700 | [diff] [blame] | 2584 | return false; |
| 2585 | } |
| 2586 | |
Scott Kennedy | ff8553f | 2013-04-05 20:57:44 -0700 | [diff] [blame] | 2587 | final String[] to, cc, bcc; |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2588 | if (orientationChanged) { |
| 2589 | to = cc = bcc = new String[0]; |
| 2590 | } else { |
| 2591 | to = getToAddresses(); |
| 2592 | cc = getCcAddresses(); |
| 2593 | bcc = getBccAddresses(); |
| 2594 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2595 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2596 | // Don't let the user send to nobody (but it's okay to save a message |
| 2597 | // with no recipients) |
| 2598 | if (!save && (to.length == 0 && cc.length == 0 && bcc.length == 0)) { |
| 2599 | showRecipientErrorDialog(getString(R.string.recipient_needed)); |
| 2600 | return false; |
| 2601 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2602 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2603 | List<String> wrongEmails = new ArrayList<String>(); |
| 2604 | if (!save) { |
| 2605 | checkInvalidEmails(to, wrongEmails); |
| 2606 | checkInvalidEmails(cc, wrongEmails); |
| 2607 | checkInvalidEmails(bcc, wrongEmails); |
| 2608 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2609 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2610 | // Don't let the user send an email with invalid recipients |
| 2611 | if (wrongEmails.size() > 0) { |
| 2612 | String errorText = String.format(getString(R.string.invalid_recipient), |
| 2613 | wrongEmails.get(0)); |
| 2614 | showRecipientErrorDialog(errorText); |
| 2615 | return false; |
| 2616 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2617 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2618 | // Show a warning before sending only if there are no attachments. |
| 2619 | if (!save) { |
| 2620 | if (mAttachmentsView.getAttachments().isEmpty() && showEmptyTextWarnings()) { |
| 2621 | boolean warnAboutEmptySubject = isSubjectEmpty(); |
Tony Mantler | 2558b50 | 2013-07-09 10:53:34 -0700 | [diff] [blame] | 2622 | boolean emptyBody = TextUtils.getTrimmedLength(mBodyView.getEditableText()) == 0; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2623 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2624 | // A warning about an empty body may not be warranted when |
| 2625 | // forwarding mails, since a common use case is to forward |
| 2626 | // quoted text and not append any more text. |
| 2627 | boolean warnAboutEmptyBody = emptyBody && (!mForward || isBodyEmpty()); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2628 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2629 | // When we bring up a dialog warning the user about a send, |
| 2630 | // assume that they accept sending the message. If they do not, |
| 2631 | // the dialog listener is required to enable sending again. |
| 2632 | if (warnAboutEmptySubject) { |
Tony Mantler | 2558b50 | 2013-07-09 10:53:34 -0700 | [diff] [blame] | 2633 | showSendConfirmDialog(R.string.confirm_send_message_with_no_subject, save, |
| 2634 | showToast); |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2635 | return true; |
| 2636 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2637 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2638 | if (warnAboutEmptyBody) { |
Tony Mantler | 2558b50 | 2013-07-09 10:53:34 -0700 | [diff] [blame] | 2639 | showSendConfirmDialog(R.string.confirm_send_message_with_no_body, save, |
| 2640 | showToast); |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2641 | return true; |
| 2642 | } |
| 2643 | } |
| 2644 | // Ask for confirmation to send (if always required) |
| 2645 | if (showSendConfirmation()) { |
Tony Mantler | 2558b50 | 2013-07-09 10:53:34 -0700 | [diff] [blame] | 2646 | showSendConfirmDialog(R.string.confirm_send_message, save, showToast); |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2647 | return true; |
| 2648 | } |
| 2649 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2650 | |
Tony Mantler | 2558b50 | 2013-07-09 10:53:34 -0700 | [diff] [blame] | 2651 | sendOrSave(save, showToast); |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2652 | return true; |
| 2653 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2654 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2655 | /** |
| 2656 | * Returns a boolean indicating whether warnings should be shown for empty |
| 2657 | * subject and body fields |
Andy Huang | 5c5fd57 | 2012-04-08 18:19:29 -0700 | [diff] [blame] | 2658 | * |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2659 | * @return True if a warning should be shown for empty text fields |
| 2660 | */ |
| 2661 | protected boolean showEmptyTextWarnings() { |
| 2662 | return mAttachmentsView.getAttachments().size() == 0; |
| 2663 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2664 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2665 | /** |
| 2666 | * Returns a boolean indicating whether the user should confirm each send |
| 2667 | * |
| 2668 | * @return True if a warning should be on each send |
| 2669 | */ |
| 2670 | protected boolean showSendConfirmation() { |
| 2671 | return mCachedSettings != null ? mCachedSettings.confirmSend : false; |
| 2672 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2673 | |
Andrew Sapperstein | 530ac7a | 2013-10-29 19:12:17 -0700 | [diff] [blame] | 2674 | public static class SendConfirmDialogFragment extends DialogFragment |
| 2675 | implements DialogInterface.OnClickListener { |
| 2676 | |
| 2677 | private boolean mSave; |
| 2678 | private boolean mShowToast; |
| 2679 | |
Paul Westbrook | f0ea484 | 2013-08-13 16:41:18 -0700 | [diff] [blame] | 2680 | // Public no-args constructor needed for fragment re-instantiation |
| 2681 | public SendConfirmDialogFragment() {} |
| 2682 | |
Tony Mantler | 2558b50 | 2013-07-09 10:53:34 -0700 | [diff] [blame] | 2683 | public static SendConfirmDialogFragment newInstance(final int messageId, |
| 2684 | final boolean save, final boolean showToast) { |
| 2685 | final SendConfirmDialogFragment frag = new SendConfirmDialogFragment(); |
| 2686 | final Bundle args = new Bundle(3); |
| 2687 | args.putInt("messageId", messageId); |
| 2688 | args.putBoolean("save", save); |
| 2689 | args.putBoolean("showToast", showToast); |
| 2690 | frag.setArguments(args); |
| 2691 | return frag; |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2692 | } |
Tony Mantler | 2558b50 | 2013-07-09 10:53:34 -0700 | [diff] [blame] | 2693 | |
| 2694 | @Override |
| 2695 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 2696 | final int messageId = getArguments().getInt("messageId"); |
Andrew Sapperstein | 530ac7a | 2013-10-29 19:12:17 -0700 | [diff] [blame] | 2697 | mSave = getArguments().getBoolean("save"); |
| 2698 | mShowToast = getArguments().getBoolean("showToast"); |
| 2699 | |
| 2700 | final int confirmTextId = (messageId == R.string.confirm_send_message) ? |
| 2701 | R.string.ok : R.string.send; |
Tony Mantler | 2558b50 | 2013-07-09 10:53:34 -0700 | [diff] [blame] | 2702 | |
| 2703 | return new AlertDialog.Builder(getActivity()) |
| 2704 | .setMessage(messageId) |
Andrew Sapperstein | 530ac7a | 2013-10-29 19:12:17 -0700 | [diff] [blame] | 2705 | .setPositiveButton(confirmTextId, this) |
Paul Westbrook | 7d1c5c4 | 2013-10-01 23:40:04 -0700 | [diff] [blame] | 2706 | .setNegativeButton(R.string.cancel, null) |
Tony Mantler | 2558b50 | 2013-07-09 10:53:34 -0700 | [diff] [blame] | 2707 | .create(); |
| 2708 | } |
Andrew Sapperstein | 530ac7a | 2013-10-29 19:12:17 -0700 | [diff] [blame] | 2709 | |
| 2710 | @Override |
| 2711 | public void onClick(DialogInterface dialog, int which) { |
| 2712 | if (which == DialogInterface.BUTTON_POSITIVE) { |
| 2713 | ((ComposeActivity) getActivity()).finishSendConfirmDialog(mSave, mShowToast); |
| 2714 | } |
| 2715 | } |
Tony Mantler | 2558b50 | 2013-07-09 10:53:34 -0700 | [diff] [blame] | 2716 | } |
| 2717 | |
| 2718 | private void finishSendConfirmDialog(final boolean save, final boolean showToast) { |
| 2719 | sendOrSave(save, showToast); |
| 2720 | } |
| 2721 | |
| 2722 | private void showSendConfirmDialog(final int messageId, final boolean save, |
| 2723 | final boolean showToast) { |
| 2724 | final DialogFragment frag = SendConfirmDialogFragment.newInstance(messageId, save, |
| 2725 | showToast); |
| 2726 | frag.show(getFragmentManager(), "send confirm"); |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2727 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2728 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2729 | /** |
| 2730 | * Returns whether the ComposeArea believes there is any text in the body of |
| 2731 | * the composition. TODO: When ComposeArea controls the Body as well, add |
| 2732 | * that here. |
| 2733 | */ |
| 2734 | public boolean isBodyEmpty() { |
| 2735 | return !mQuotedTextView.isTextIncluded(); |
| 2736 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2737 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2738 | /** |
| 2739 | * Test to see if the subject is empty. |
| 2740 | * |
| 2741 | * @return boolean. |
| 2742 | */ |
| 2743 | // TODO: this will likely go away when composeArea.focus() is implemented |
| 2744 | // after all the widget control is moved over. |
| 2745 | public boolean isSubjectEmpty() { |
| 2746 | return TextUtils.getTrimmedLength(mSubject.getText()) == 0; |
| 2747 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2748 | |
Andy Huang | 0a2a346 | 2013-12-20 15:56:13 -0800 | [diff] [blame] | 2749 | @VisibleForTesting |
| 2750 | public String getSubject() { |
| 2751 | return mSubject.getText().toString(); |
| 2752 | } |
| 2753 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2754 | /* package */ |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 2755 | static int sendOrSaveInternal(Context context, ReplyFromAccount replyFromAccount, |
Paul Westbrook | 05b92b8 | 2012-04-20 13:29:37 -0700 | [diff] [blame] | 2756 | Message message, final Message refMessage, Spanned body, final CharSequence quotedText, |
mindyp | 44a6339 | 2012-11-05 12:05:16 -0800 | [diff] [blame] | 2757 | SendOrSaveCallback callback, Handler handler, boolean save, int composeMode, |
Scott Kennedy | 6084725 | 2013-08-15 15:55:42 -0700 | [diff] [blame] | 2758 | ReplyFromAccount draftAccount, final ContentValues extraValues) { |
Paul Westbrook | b4931c6 | 2013-01-14 17:51:18 -0800 | [diff] [blame] | 2759 | final ContentValues values = new ContentValues(); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2760 | |
Paul Westbrook | b4931c6 | 2013-01-14 17:51:18 -0800 | [diff] [blame] | 2761 | final String refMessageId = refMessage != null ? refMessage.uri.toString() : ""; |
Mindy Pereira | c203197 | 2012-04-03 09:38:35 -0700 | [diff] [blame] | 2762 | |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 2763 | MessageModification.putToAddresses(values, message.getToAddresses()); |
| 2764 | MessageModification.putCcAddresses(values, message.getCcAddresses()); |
| 2765 | MessageModification.putBccAddresses(values, message.getBccAddresses()); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2766 | |
Scott Kennedy | 8960f0a | 2012-11-07 15:35:50 -0800 | [diff] [blame] | 2767 | MessageModification.putCustomFromAddress(values, message.getFrom()); |
Mindy Pereira | 92551d0 | 2012-04-05 11:31:12 -0700 | [diff] [blame] | 2768 | |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 2769 | MessageModification.putSubject(values, message.subject); |
Paul Westbrook | b4931c6 | 2013-01-14 17:51:18 -0800 | [diff] [blame] | 2770 | // Make sure to remove only the composing spans from the Spannable before saving. |
| 2771 | final String htmlBody = Html.toHtml(removeComposingSpans(body)); |
Paul Westbrook | 05b92b8 | 2012-04-20 13:29:37 -0700 | [diff] [blame] | 2772 | |
Mindy Pereira | 29ef1b8 | 2012-01-13 11:26:21 -0800 | [diff] [blame] | 2773 | boolean includeQuotedText = !TextUtils.isEmpty(quotedText); |
| 2774 | StringBuilder fullBody = new StringBuilder(htmlBody); |
| 2775 | if (includeQuotedText) { |
Mindy Pereira | e8caf12 | 2012-03-20 15:23:31 -0700 | [diff] [blame] | 2776 | // HTML gets converted to text for now |
| 2777 | final String text = quotedText.toString(); |
| 2778 | if (QuotedTextView.containsQuotedText(text)) { |
| 2779 | int pos = QuotedTextView.getQuotedTextOffset(text); |
Paul Westbrook | 55271cf | 2012-04-20 16:25:02 -0700 | [diff] [blame] | 2780 | final int quoteStartPos = fullBody.length() + pos; |
| 2781 | fullBody.append(text); |
| 2782 | MessageModification.putQuoteStartPos(values, quoteStartPos); |
Mindy Pereira | 1257586 | 2012-03-21 16:30:54 -0700 | [diff] [blame] | 2783 | MessageModification.putForward(values, composeMode == ComposeActivity.FORWARD); |
Mindy Pereira | e8caf12 | 2012-03-20 15:23:31 -0700 | [diff] [blame] | 2784 | MessageModification.putAppendRefMessageContent(values, includeQuotedText); |
Mindy Pereira | 29ef1b8 | 2012-01-13 11:26:21 -0800 | [diff] [blame] | 2785 | } else { |
Mindy Pereira | e8caf12 | 2012-03-20 15:23:31 -0700 | [diff] [blame] | 2786 | LogUtils.w(LOG_TAG, "Couldn't find quoted text"); |
| 2787 | // This shouldn't happen, but just use what we have, |
| 2788 | // and don't do server-side expansion |
| 2789 | fullBody.append(text); |
Mindy Pereira | 29ef1b8 | 2012-01-13 11:26:21 -0800 | [diff] [blame] | 2790 | } |
| 2791 | } |
Mindy Pereira | 002ff52 | 2012-05-30 10:31:26 -0700 | [diff] [blame] | 2792 | int draftType = getDraftType(composeMode); |
Mindy Pereira | 1257586 | 2012-03-21 16:30:54 -0700 | [diff] [blame] | 2793 | MessageModification.putDraftType(values, draftType); |
Mindy Pereira | c6f1e2a | 2012-04-04 10:33:45 -0700 | [diff] [blame] | 2794 | if (refMessage != null) { |
| 2795 | if (!TextUtils.isEmpty(refMessage.bodyHtml)) { |
| 2796 | MessageModification.putBodyHtml(values, fullBody.toString()); |
| 2797 | } |
| 2798 | if (!TextUtils.isEmpty(refMessage.bodyText)) { |
mindyp | c59dd82 | 2012-11-13 10:56:21 -0800 | [diff] [blame] | 2799 | MessageModification.putBody(values, |
| 2800 | Utils.convertHtmlToPlainText(fullBody.toString()).toString()); |
Mindy Pereira | c6f1e2a | 2012-04-04 10:33:45 -0700 | [diff] [blame] | 2801 | } |
| 2802 | } else { |
Mindy Pereira | c203197 | 2012-04-03 09:38:35 -0700 | [diff] [blame] | 2803 | MessageModification.putBodyHtml(values, fullBody.toString()); |
mindyp | c59dd82 | 2012-11-13 10:56:21 -0800 | [diff] [blame] | 2804 | MessageModification.putBody(values, Utils.convertHtmlToPlainText(fullBody.toString()) |
| 2805 | .toString()); |
Mindy Pereira | c203197 | 2012-04-03 09:38:35 -0700 | [diff] [blame] | 2806 | } |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 2807 | MessageModification.putAttachments(values, message.getAttachments()); |
Mindy Pereira | 1257586 | 2012-03-21 16:30:54 -0700 | [diff] [blame] | 2808 | if (!TextUtils.isEmpty(refMessageId)) { |
| 2809 | MessageModification.putRefMessageId(values, refMessageId); |
| 2810 | } |
Scott Kennedy | 6084725 | 2013-08-15 15:55:42 -0700 | [diff] [blame] | 2811 | if (extraValues != null) { |
| 2812 | values.putAll(extraValues); |
| 2813 | } |
Paul Westbrook | 3c7f94d | 2012-10-23 14:13:00 -0700 | [diff] [blame] | 2814 | SendOrSaveMessage sendOrSaveMessage = new SendOrSaveMessage(context, replyFromAccount, |
| 2815 | values, refMessageId, message.getAttachments(), save); |
mindyp | 44a6339 | 2012-11-05 12:05:16 -0800 | [diff] [blame] | 2816 | SendOrSaveTask sendOrSaveTask = new SendOrSaveTask(context, sendOrSaveMessage, callback, |
| 2817 | draftAccount); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2818 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2819 | callback.initializeSendOrSave(sendOrSaveTask); |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2820 | // Do the send/save action on the specified handler to avoid possible |
| 2821 | // ANRs |
| 2822 | handler.post(sendOrSaveTask); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2823 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2824 | return sendOrSaveMessage.requestId(); |
| 2825 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2826 | |
Paul Westbrook | b4931c6 | 2013-01-14 17:51:18 -0800 | [diff] [blame] | 2827 | /** |
| 2828 | * Removes any composing spans from the specified string. This will create a new |
| 2829 | * SpannableString instance, as to not modify the behavior of the EditText view. |
| 2830 | */ |
| 2831 | private static SpannableString removeComposingSpans(Spanned body) { |
| 2832 | final SpannableString messageBody = new SpannableString(body); |
| 2833 | BaseInputConnection.removeComposingSpans(messageBody); |
| 2834 | return messageBody; |
| 2835 | } |
| 2836 | |
Mindy Pereira | 002ff52 | 2012-05-30 10:31:26 -0700 | [diff] [blame] | 2837 | private static int getDraftType(int mode) { |
| 2838 | int draftType = -1; |
| 2839 | switch (mode) { |
| 2840 | case ComposeActivity.COMPOSE: |
| 2841 | draftType = DraftType.COMPOSE; |
| 2842 | break; |
| 2843 | case ComposeActivity.REPLY: |
| 2844 | draftType = DraftType.REPLY; |
| 2845 | break; |
| 2846 | case ComposeActivity.REPLY_ALL: |
| 2847 | draftType = DraftType.REPLY_ALL; |
| 2848 | break; |
| 2849 | case ComposeActivity.FORWARD: |
| 2850 | draftType = DraftType.FORWARD; |
| 2851 | break; |
| 2852 | } |
| 2853 | return draftType; |
| 2854 | } |
| 2855 | |
Tony Mantler | 2558b50 | 2013-07-09 10:53:34 -0700 | [diff] [blame] | 2856 | private void sendOrSave(final boolean save, final boolean showToast) { |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2857 | // Check if user is a monkey. Monkeys can compose and hit send |
| 2858 | // button but are not allowed to send anything off the device. |
Paul Westbrook | 3ae824c | 2012-04-06 13:29:39 -0700 | [diff] [blame] | 2859 | if (ActivityManager.isUserAMonkey()) { |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2860 | return; |
| 2861 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2862 | |
Tony Mantler | 2558b50 | 2013-07-09 10:53:34 -0700 | [diff] [blame] | 2863 | final Spanned body = mBodyView.getEditableText(); |
| 2864 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2865 | SendOrSaveCallback callback = new SendOrSaveCallback() { |
Andy Huang | 1f8f4dd | 2012-10-25 21:35:35 -0700 | [diff] [blame] | 2866 | // FIXME: unused |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2867 | private int mRestoredRequestId; |
| 2868 | |
Marc Blank | 0bbc858 | 2012-04-23 15:07:57 -0700 | [diff] [blame] | 2869 | @Override |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2870 | public void initializeSendOrSave(SendOrSaveTask sendOrSaveTask) { |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2871 | synchronized (mActiveTasks) { |
| 2872 | int numTasks = mActiveTasks.size(); |
| 2873 | if (numTasks == 0) { |
| 2874 | // Start service so we won't be killed if this app is |
| 2875 | // put in the background. |
| 2876 | startService(new Intent(ComposeActivity.this, EmptyService.class)); |
| 2877 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2878 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2879 | mActiveTasks.add(sendOrSaveTask); |
| 2880 | } |
| 2881 | if (sTestSendOrSaveCallback != null) { |
| 2882 | sTestSendOrSaveCallback.initializeSendOrSave(sendOrSaveTask); |
| 2883 | } |
| 2884 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2885 | |
Marc Blank | 0bbc858 | 2012-04-23 15:07:57 -0700 | [diff] [blame] | 2886 | @Override |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 2887 | public void notifyMessageIdAllocated(SendOrSaveMessage sendOrSaveMessage, |
| 2888 | Message message) { |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2889 | synchronized (mDraftLock) { |
mindyp | 44a6339 | 2012-11-05 12:05:16 -0800 | [diff] [blame] | 2890 | mDraftAccount = sendOrSaveMessage.mAccount; |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2891 | mDraftId = message.id; |
| 2892 | mDraft = message; |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 2893 | if (sRequestMessageIdMap != null) { |
| 2894 | sRequestMessageIdMap.put(sendOrSaveMessage.requestId(), mDraftId); |
| 2895 | } |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2896 | // Cache request message map, in case the process is killed |
| 2897 | saveRequestMap(); |
| 2898 | } |
| 2899 | if (sTestSendOrSaveCallback != null) { |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 2900 | sTestSendOrSaveCallback.notifyMessageIdAllocated(sendOrSaveMessage, message); |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2901 | } |
| 2902 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2903 | |
Marc Blank | 0bbc858 | 2012-04-23 15:07:57 -0700 | [diff] [blame] | 2904 | @Override |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 2905 | public Message getMessage() { |
| 2906 | synchronized (mDraftLock) { |
| 2907 | return mDraft; |
| 2908 | } |
| 2909 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2910 | |
Marc Blank | 0bbc858 | 2012-04-23 15:07:57 -0700 | [diff] [blame] | 2911 | @Override |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 2912 | public void sendOrSaveFinished(SendOrSaveTask task, boolean success) { |
Mindy Pereira | 47d0e65 | 2012-07-23 09:45:07 -0700 | [diff] [blame] | 2913 | // Update the last sent from account. |
| 2914 | if (mAccount != null) { |
| 2915 | MailAppProvider.getInstance().setLastSentFromAccount(mAccount.uri.toString()); |
| 2916 | } |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 2917 | if (success) { |
| 2918 | // Successfully sent or saved so reset change markers |
| 2919 | discardChanges(); |
| 2920 | } else { |
| 2921 | // A failure happened with saving/sending the draft |
| 2922 | // TODO(pwestbro): add a better string that should be used |
| 2923 | // when failing to send or save |
| 2924 | Toast.makeText(ComposeActivity.this, R.string.send_failed, Toast.LENGTH_SHORT) |
| 2925 | .show(); |
| 2926 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2927 | |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 2928 | int numTasks; |
| 2929 | synchronized (mActiveTasks) { |
| 2930 | // Remove the task from the list of active tasks |
| 2931 | mActiveTasks.remove(task); |
| 2932 | numTasks = mActiveTasks.size(); |
| 2933 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2934 | |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 2935 | if (numTasks == 0) { |
| 2936 | // Stop service so we can be killed. |
| 2937 | stopService(new Intent(ComposeActivity.this, EmptyService.class)); |
| 2938 | } |
| 2939 | if (sTestSendOrSaveCallback != null) { |
| 2940 | sTestSendOrSaveCallback.sendOrSaveFinished(task, success); |
| 2941 | } |
| 2942 | } |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2943 | }; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2944 | |
Tony Mantler | 1e05a1e | 2013-08-12 16:44:26 -0700 | [diff] [blame] | 2945 | setAccount(mReplyFromAccount.account); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2946 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2947 | if (mSendSaveTaskHandler == null) { |
| 2948 | HandlerThread handlerThread = new HandlerThread("Send Message Task Thread"); |
| 2949 | handlerThread.start(); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2950 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2951 | mSendSaveTaskHandler = new Handler(handlerThread.getLooper()); |
| 2952 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2953 | |
Mindy Pereira | e8f94dc | 2012-04-16 11:56:21 -0700 | [diff] [blame] | 2954 | Message msg = createMessage(mReplyFromAccount, getMode()); |
Paul Westbrook | 05b92b8 | 2012-04-20 13:29:37 -0700 | [diff] [blame] | 2955 | mRequestId = sendOrSaveInternal(this, mReplyFromAccount, msg, mRefMessage, body, |
| 2956 | mQuotedTextView.getQuotedTextIfIncluded(), callback, |
Scott Kennedy | 6084725 | 2013-08-15 15:55:42 -0700 | [diff] [blame] | 2957 | mSendSaveTaskHandler, save, mComposeMode, mDraftAccount, mExtraValues); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2958 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2959 | // Don't display the toast if the user is just changing the orientation, |
| 2960 | // but we still need to save the draft to the cursor because this is how we restore |
| 2961 | // the attachments when the configuration change completes. |
| 2962 | if (showToast && (getChangingConfigurations() & ActivityInfo.CONFIG_ORIENTATION) == 0) { |
| 2963 | Toast.makeText(this, save ? R.string.message_saved : R.string.sending_message, |
| 2964 | Toast.LENGTH_LONG).show(); |
| 2965 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2966 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2967 | // Need to update variables here because the send or save completes |
| 2968 | // asynchronously even though the toast shows right away. |
| 2969 | discardChanges(); |
| 2970 | updateSaveUi(); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2971 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2972 | // If we are sending, finish the activity |
| 2973 | if (!save) { |
| 2974 | finish(); |
| 2975 | } |
| 2976 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2977 | |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2978 | /** |
| 2979 | * Save the state of the request messageid map. This allows for the Gmail |
| 2980 | * process to be killed, but and still allow for ComposeActivity instances |
| 2981 | * to be recreated correctly. |
| 2982 | */ |
| 2983 | private void saveRequestMap() { |
| 2984 | // TODO: store the request map in user preferences. |
| 2985 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 2986 | |
Mindy Pereira | 2db7d4a | 2012-08-15 11:00:02 -0700 | [diff] [blame] | 2987 | private void doAttach(String type) { |
Mindy Pereira | 013194c | 2012-01-06 15:09:33 -0800 | [diff] [blame] | 2988 | Intent i = new Intent(Intent.ACTION_GET_CONTENT); |
| 2989 | i.addCategory(Intent.CATEGORY_OPENABLE); |
Paul Westbrook | d6a9a3f | 2012-04-26 18:47:23 -0700 | [diff] [blame] | 2990 | i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); |
Andrew Sapperstein | 05089f3 | 2013-10-01 17:00:03 -0700 | [diff] [blame] | 2991 | i.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true); |
Mindy Pereira | 2db7d4a | 2012-08-15 11:00:02 -0700 | [diff] [blame] | 2992 | i.setType(type); |
Mindy Pereira | 013194c | 2012-01-06 15:09:33 -0800 | [diff] [blame] | 2993 | mAddingAttachment = true; |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 2994 | startActivityForResult(Intent.createChooser(i, getText(R.string.select_attachment_type)), |
| 2995 | RESULT_PICK_ATTACHMENT); |
Mindy Pereira | 013194c | 2012-01-06 15:09:33 -0800 | [diff] [blame] | 2996 | } |
| 2997 | |
Mindy Pereira | ec8b0ed | 2012-01-06 10:35:59 -0800 | [diff] [blame] | 2998 | private void showCcBccViews() { |
Mindy Pereira | a26b54e | 2012-01-06 12:54:33 -0800 | [diff] [blame] | 2999 | mCcBccView.show(true, true, true); |
Mindy Pereira | ec8b0ed | 2012-01-06 10:35:59 -0800 | [diff] [blame] | 3000 | if (mCcBccButton != null) { |
mindyp | cd0b0b9 | 2012-08-23 14:33:17 -0700 | [diff] [blame] | 3001 | mCcBccButton.setVisibility(View.INVISIBLE); |
Mindy Pereira | ec8b0ed | 2012-01-06 10:35:59 -0800 | [diff] [blame] | 3002 | } |
| 3003 | } |
| 3004 | |
Andy Huang | 4fe0af8 | 2013-08-20 17:24:51 -0700 | [diff] [blame] | 3005 | private static String getActionString(int action) { |
Andy Huang | dc97bf4 | 2013-08-15 16:52:45 -0700 | [diff] [blame] | 3006 | final String msgType; |
Andy Huang | 4fe0af8 | 2013-08-20 17:24:51 -0700 | [diff] [blame] | 3007 | switch (action) { |
Andy Huang | dc97bf4 | 2013-08-15 16:52:45 -0700 | [diff] [blame] | 3008 | case COMPOSE: |
| 3009 | msgType = "new_message"; |
| 3010 | break; |
| 3011 | case REPLY: |
| 3012 | msgType = "reply"; |
| 3013 | break; |
| 3014 | case REPLY_ALL: |
| 3015 | msgType = "reply_all"; |
| 3016 | break; |
| 3017 | case FORWARD: |
| 3018 | msgType = "forward"; |
| 3019 | break; |
| 3020 | default: |
| 3021 | msgType = "unknown"; |
| 3022 | break; |
| 3023 | } |
Andy Huang | 4fe0af8 | 2013-08-20 17:24:51 -0700 | [diff] [blame] | 3024 | return msgType; |
| 3025 | } |
| 3026 | |
| 3027 | private void logSendOrSave(boolean save) { |
| 3028 | if (!Analytics.isLoggable() || mAttachmentsView == null) { |
| 3029 | return; |
| 3030 | } |
| 3031 | |
| 3032 | final String category = (save) ? "message_save" : "message_send"; |
| 3033 | final int attachmentCount = getAttachments().size(); |
| 3034 | final String msgType = getActionString(mComposeMode); |
Andy Huang | dc97bf4 | 2013-08-15 16:52:45 -0700 | [diff] [blame] | 3035 | final String label; |
| 3036 | final long value; |
| 3037 | if (mComposeMode == COMPOSE) { |
| 3038 | label = Integer.toString(attachmentCount); |
| 3039 | value = attachmentCount; |
| 3040 | } else { |
| 3041 | label = null; |
| 3042 | value = 0; |
| 3043 | } |
| 3044 | Analytics.getInstance().sendEvent(category, msgType, label, value); |
| 3045 | } |
| 3046 | |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 3047 | @Override |
| 3048 | public boolean onNavigationItemSelected(int position, long itemId) { |
Mindy Pereira | a26b54e | 2012-01-06 12:54:33 -0800 | [diff] [blame] | 3049 | int initialComposeMode = mComposeMode; |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 3050 | if (position == ComposeActivity.REPLY) { |
| 3051 | mComposeMode = ComposeActivity.REPLY; |
| 3052 | } else if (position == ComposeActivity.REPLY_ALL) { |
| 3053 | mComposeMode = ComposeActivity.REPLY_ALL; |
| 3054 | } else if (position == ComposeActivity.FORWARD) { |
| 3055 | mComposeMode = ComposeActivity.FORWARD; |
| 3056 | } |
Mindy Pereira | cbfb75a | 2012-06-25 14:52:23 -0700 | [diff] [blame] | 3057 | clearChangeListeners(); |
Mindy Pereira | a26b54e | 2012-01-06 12:54:33 -0800 | [diff] [blame] | 3058 | if (initialComposeMode != mComposeMode) { |
Mindy Pereira | 154386a | 2012-01-11 13:02:33 -0800 | [diff] [blame] | 3059 | resetMessageForModeChange(); |
mindyp | 68c0bfc | 2012-12-04 10:29:48 -0800 | [diff] [blame] | 3060 | if (mRefMessage != null) { |
Scott Kennedy | 0aeaf7d | 2012-11-14 18:56:05 -0800 | [diff] [blame] | 3061 | setFieldsFromRefMessage(mComposeMode); |
Mindy Pereira | 8eca57a | 2012-03-20 16:42:34 -0700 | [diff] [blame] | 3062 | } |
Mindy Pereira | ef38830 | 2012-06-18 19:07:44 -0700 | [diff] [blame] | 3063 | boolean showCc = false; |
| 3064 | boolean showBcc = false; |
| 3065 | if (mDraft != null) { |
| 3066 | // Following desktop behavior, if the user has added a BCC |
| 3067 | // field to a draft, we show it regardless of compose mode. |
Scott Kennedy | 8960f0a | 2012-11-07 15:35:50 -0800 | [diff] [blame] | 3068 | showBcc = !TextUtils.isEmpty(mDraft.getBcc()); |
Mindy Pereira | ef38830 | 2012-06-18 19:07:44 -0700 | [diff] [blame] | 3069 | // Use the draft to determine what to populate. |
| 3070 | // If the Bcc field is showing, show the Cc field whether it is populated or not. |
Scott Kennedy | 8960f0a | 2012-11-07 15:35:50 -0800 | [diff] [blame] | 3071 | showCc = showBcc |
| 3072 | || (!TextUtils.isEmpty(mDraft.getCc()) && mComposeMode == REPLY_ALL); |
mindyp | 68c0bfc | 2012-12-04 10:29:48 -0800 | [diff] [blame] | 3073 | } |
| 3074 | if (mRefMessage != null) { |
mindyp | 9b1ac57 | 2012-09-27 14:12:00 -0700 | [diff] [blame] | 3075 | showCc = !TextUtils.isEmpty(mCc.getText()); |
mindyp | 68c0bfc | 2012-12-04 10:29:48 -0800 | [diff] [blame] | 3076 | showBcc = !TextUtils.isEmpty(mBcc.getText()); |
Mindy Pereira | ef38830 | 2012-06-18 19:07:44 -0700 | [diff] [blame] | 3077 | } |
| 3078 | mCcBccView.show(false, showCc, showBcc); |
Mindy Pereira | a26b54e | 2012-01-06 12:54:33 -0800 | [diff] [blame] | 3079 | } |
Mindy Pereira | ef38830 | 2012-06-18 19:07:44 -0700 | [diff] [blame] | 3080 | updateHideOrShowCcBcc(); |
Mindy Pereira | cbfb75a | 2012-06-25 14:52:23 -0700 | [diff] [blame] | 3081 | initChangeListeners(); |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 3082 | return true; |
| 3083 | } |
| 3084 | |
Mindy Pereira | b3112a2 | 2012-06-20 12:10:03 -0700 | [diff] [blame] | 3085 | @VisibleForTesting |
| 3086 | protected void resetMessageForModeChange() { |
Mindy Pereira | 154386a | 2012-01-11 13:02:33 -0800 | [diff] [blame] | 3087 | // When switching between reply, reply all, forward, |
| 3088 | // follow the behavior of webview. |
| 3089 | // The contents of the following fields are cleared |
| 3090 | // so that they can be populated directly from the |
| 3091 | // ref message: |
| 3092 | // 1) Any recipient fields |
| 3093 | // 2) The subject |
| 3094 | mTo.setText(""); |
| 3095 | mCc.setText(""); |
| 3096 | mBcc.setText(""); |
| 3097 | // Any edits to the subject are replaced with the original subject. |
| 3098 | mSubject.setText(""); |
| 3099 | |
| 3100 | // Any changes to the contents of the following fields are kept: |
| 3101 | // 1) Body |
| 3102 | // 2) Attachments |
| 3103 | // If the user made changes to attachments, keep their changes. |
| 3104 | if (!mAttachmentsChanged) { |
| 3105 | mAttachmentsView.deleteAllAttachments(); |
| 3106 | } |
| 3107 | } |
| 3108 | |
Mindy Pereira | 326c660 | 2012-01-04 15:32:42 -0800 | [diff] [blame] | 3109 | private class ComposeModeAdapter extends ArrayAdapter<String> { |
| 3110 | |
| 3111 | private LayoutInflater mInflater; |
| 3112 | |
| 3113 | public ComposeModeAdapter(Context context) { |
| 3114 | super(context, R.layout.compose_mode_item, R.id.mode, getResources() |
| 3115 | .getStringArray(R.array.compose_modes)); |
| 3116 | } |
| 3117 | |
| 3118 | private LayoutInflater getInflater() { |
| 3119 | if (mInflater == null) { |
| 3120 | mInflater = LayoutInflater.from(getContext()); |
| 3121 | } |
| 3122 | return mInflater; |
| 3123 | } |
| 3124 | |
| 3125 | @Override |
| 3126 | public View getView(int position, View convertView, ViewGroup parent) { |
| 3127 | if (convertView == null) { |
| 3128 | convertView = getInflater().inflate(R.layout.compose_mode_display_item, null); |
| 3129 | } |
| 3130 | ((TextView) convertView.findViewById(R.id.mode)).setText(getItem(position)); |
| 3131 | return super.getView(position, convertView, parent); |
| 3132 | } |
| 3133 | } |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 3134 | |
| 3135 | @Override |
| 3136 | public void onRespondInline(String text) { |
| 3137 | appendToBody(text, false); |
mindyp | 4088243 | 2012-09-06 11:07:40 -0700 | [diff] [blame] | 3138 | mQuotedTextView.setUpperDividerVisible(false); |
mindyp | 1623f9b | 2012-11-21 12:41:16 -0800 | [diff] [blame] | 3139 | mRespondedInline = true; |
mindyp | 09dd373 | 2012-12-17 08:37:52 -0800 | [diff] [blame] | 3140 | if (!mBodyView.hasFocus()) { |
mindyp | 8654d4f | 2012-12-17 09:01:37 -0800 | [diff] [blame] | 3141 | mBodyView.requestFocus(); |
mindyp | 09dd373 | 2012-12-17 08:37:52 -0800 | [diff] [blame] | 3142 | } |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 3143 | } |
| 3144 | |
| 3145 | /** |
| 3146 | * Append text to the body of the message. If there is no existing body |
| 3147 | * text, just sets the body to text. |
| 3148 | * |
| 3149 | * @param text |
| 3150 | * @param withSignature True to append a signature. |
| 3151 | */ |
| 3152 | public void appendToBody(CharSequence text, boolean withSignature) { |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 3153 | Editable bodyText = mBodyView.getEditableText(); |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 3154 | if (bodyText != null && bodyText.length() > 0) { |
| 3155 | bodyText.append(text); |
| 3156 | } else { |
| 3157 | setBody(text, withSignature); |
| 3158 | } |
| 3159 | } |
| 3160 | |
| 3161 | /** |
| 3162 | * Set the body of the message. |
Mindy Pereira | bdf7a40 | 2012-03-01 15:23:26 -0800 | [diff] [blame] | 3163 | * |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 3164 | * @param text |
| 3165 | * @param withSignature True to append a signature. |
| 3166 | */ |
| 3167 | public void setBody(CharSequence text, boolean withSignature) { |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 3168 | mBodyView.setText(text); |
Mindy Pereira | bdf7a40 | 2012-03-01 15:23:26 -0800 | [diff] [blame] | 3169 | if (withSignature) { |
| 3170 | appendSignature(); |
| 3171 | } |
| 3172 | } |
| 3173 | |
| 3174 | private void appendSignature() { |
Mindy Pereira | b13917c | 2012-03-29 08:08:19 -0700 | [diff] [blame] | 3175 | String newSignature = mCachedSettings != null ? mCachedSettings.signature : null; |
Mindy Pereira | 433b198 | 2012-04-03 11:53:07 -0700 | [diff] [blame] | 3176 | boolean hasFocus = mBodyView.hasFocus(); |
mindyp | 2708306 | 2012-11-15 09:02:01 -0800 | [diff] [blame] | 3177 | int signaturePos = getSignatureStartPosition(mSignature, mBodyView.getText().toString()); |
| 3178 | if (!TextUtils.equals(newSignature, mSignature) || signaturePos < 0) { |
Mindy Pereira | b13917c | 2012-03-29 08:08:19 -0700 | [diff] [blame] | 3179 | mSignature = newSignature; |
mindyp | 2708306 | 2012-11-15 09:02:01 -0800 | [diff] [blame] | 3180 | if (!TextUtils.isEmpty(mSignature)) { |
Mindy Pereira | b13917c | 2012-03-29 08:08:19 -0700 | [diff] [blame] | 3181 | // Appending a signature does not count as changing text. |
| 3182 | mBodyView.removeTextChangedListener(this); |
| 3183 | mBodyView.append(convertToPrintableSignature(mSignature)); |
| 3184 | mBodyView.addTextChangedListener(this); |
| 3185 | } |
Mindy Pereira | 433b198 | 2012-04-03 11:53:07 -0700 | [diff] [blame] | 3186 | if (hasFocus) { |
| 3187 | focusBody(); |
| 3188 | } |
Mindy Pereira | bdf7a40 | 2012-03-01 15:23:26 -0800 | [diff] [blame] | 3189 | } |
| 3190 | } |
| 3191 | |
| 3192 | private String convertToPrintableSignature(String signature) { |
| 3193 | String signatureResource = getResources().getString(R.string.signature); |
| 3194 | if (signature == null) { |
| 3195 | signature = ""; |
| 3196 | } |
| 3197 | return String.format(signatureResource, signature); |
Mindy Pereira | 46ce0b1 | 2012-01-05 10:32:15 -0800 | [diff] [blame] | 3198 | } |
Mindy Pereira | 1a95a57 | 2012-01-05 12:21:29 -0800 | [diff] [blame] | 3199 | |
Mindy Pereira | 5a85e2b | 2012-01-11 09:53:32 -0800 | [diff] [blame] | 3200 | @Override |
| 3201 | public void onAccountChanged() { |
Mindy Pereira | 92551d0 | 2012-04-05 11:31:12 -0700 | [diff] [blame] | 3202 | mReplyFromAccount = mFromSpinner.getCurrentAccount(); |
| 3203 | if (!mAccount.equals(mReplyFromAccount.account)) { |
mindyp | f432dbc | 2012-11-12 16:00:44 -0800 | [diff] [blame] | 3204 | // Clear a signature, if there was one. |
| 3205 | mBodyView.removeTextChangedListener(this); |
| 3206 | String oldSignature = mSignature; |
| 3207 | String bodyText = getBody().getText().toString(); |
| 3208 | if (!TextUtils.isEmpty(oldSignature)) { |
| 3209 | int pos = getSignatureStartPosition(oldSignature, bodyText); |
| 3210 | if (pos > -1) { |
| 3211 | mBodyView.setText(bodyText.substring(0, pos)); |
| 3212 | } |
| 3213 | } |
Paul Westbrook | b1f573c | 2012-04-06 11:38:28 -0700 | [diff] [blame] | 3214 | setAccount(mReplyFromAccount.account); |
mindyp | f432dbc | 2012-11-12 16:00:44 -0800 | [diff] [blame] | 3215 | mBodyView.addTextChangedListener(this); |
Mindy Pereira | 181df78 | 2012-03-01 13:32:44 -0800 | [diff] [blame] | 3216 | // TODO: handle discarding attachments when switching accounts. |
| 3217 | // Only enable save for this draft if there is any other content |
| 3218 | // in the message. |
| 3219 | if (!isBlank()) { |
| 3220 | enableSave(true); |
| 3221 | } |
| 3222 | mReplyFromChanged = true; |
| 3223 | initRecipients(); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 3224 | } |
Mindy Pereira | 1a95a57 | 2012-01-05 12:21:29 -0800 | [diff] [blame] | 3225 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 3226 | |
| 3227 | public void enableSave(boolean enabled) { |
| 3228 | if (mSave != null) { |
| 3229 | mSave.setEnabled(enabled); |
| 3230 | } |
| 3231 | } |
| 3232 | |
Tony Mantler | 2558b50 | 2013-07-09 10:53:34 -0700 | [diff] [blame] | 3233 | public static class DiscardConfirmDialogFragment extends DialogFragment { |
Paul Westbrook | f0ea484 | 2013-08-13 16:41:18 -0700 | [diff] [blame] | 3234 | // Public no-args constructor needed for fragment re-instantiation |
| 3235 | public DiscardConfirmDialogFragment() {} |
| 3236 | |
Tony Mantler | 2558b50 | 2013-07-09 10:53:34 -0700 | [diff] [blame] | 3237 | @Override |
| 3238 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 3239 | return new AlertDialog.Builder(getActivity()) |
| 3240 | .setMessage(R.string.confirm_discard_text) |
| 3241 | .setPositiveButton(R.string.discard, |
| 3242 | new DialogInterface.OnClickListener() { |
| 3243 | @Override |
| 3244 | public void onClick(DialogInterface dialog, int which) { |
| 3245 | ((ComposeActivity)getActivity()).doDiscardWithoutConfirmation(); |
| 3246 | } |
| 3247 | }) |
Tony Mantler | 2b215b7 | 2013-07-31 10:20:46 -0700 | [diff] [blame] | 3248 | .setNegativeButton(R.string.cancel, null) |
Tony Mantler | 2558b50 | 2013-07-09 10:53:34 -0700 | [diff] [blame] | 3249 | .create(); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 3250 | } |
| 3251 | } |
| 3252 | |
Mindy Pereira | efe3d25 | 2012-03-01 14:20:44 -0800 | [diff] [blame] | 3253 | private void doDiscard() { |
Tony Mantler | 2558b50 | 2013-07-09 10:53:34 -0700 | [diff] [blame] | 3254 | final DialogFragment frag = new DiscardConfirmDialogFragment(); |
| 3255 | frag.show(getFragmentManager(), "discard confirm"); |
Mindy Pereira | efe3d25 | 2012-03-01 14:20:44 -0800 | [diff] [blame] | 3256 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 3257 | /** |
| 3258 | * Effectively discard the current message. |
| 3259 | * |
| 3260 | * This method is either invoked from the menu or from the dialog |
| 3261 | * once the user has confirmed that they want to discard the message. |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 3262 | */ |
Tony Mantler | 2558b50 | 2013-07-09 10:53:34 -0700 | [diff] [blame] | 3263 | private void doDiscardWithoutConfirmation() { |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 3264 | synchronized (mDraftLock) { |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 3265 | if (mDraftId != UIProvider.INVALID_MESSAGE_ID) { |
| 3266 | ContentValues values = new ContentValues(); |
Paul Westbrook | b7050e6 | 2012-03-20 12:59:44 -0700 | [diff] [blame] | 3267 | values.put(BaseColumns._ID, mDraftId); |
Marc Blank | 78ea8e2 | 2012-08-04 11:14:06 -0700 | [diff] [blame] | 3268 | if (!mAccount.expungeMessageUri.equals(Uri.EMPTY)) { |
Mindy Pereira | cfb7f33 | 2012-02-28 10:23:43 -0800 | [diff] [blame] | 3269 | getContentResolver().update(mAccount.expungeMessageUri, values, null, null); |
| 3270 | } else { |
Marc Blank | 0bbc858 | 2012-04-23 15:07:57 -0700 | [diff] [blame] | 3271 | getContentResolver().delete(mDraft.uri, null, null); |
Mindy Pereira | cfb7f33 | 2012-02-28 10:23:43 -0800 | [diff] [blame] | 3272 | } |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 3273 | // This is not strictly necessary (since we should not try to |
| 3274 | // save the draft after calling this) but it ensures that if we |
| 3275 | // do save again for some reason we make a new draft rather than |
| 3276 | // trying to resave an expunged draft. |
| 3277 | mDraftId = UIProvider.INVALID_MESSAGE_ID; |
| 3278 | } |
| 3279 | } |
| 3280 | |
Tony Mantler | 2558b50 | 2013-07-09 10:53:34 -0700 | [diff] [blame] | 3281 | // Display a toast to let the user know |
| 3282 | Toast.makeText(this, R.string.message_discarded, Toast.LENGTH_SHORT).show(); |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 3283 | |
| 3284 | // This prevents the draft from being saved in onPause(). |
| 3285 | discardChanges(); |
Andy Huang | dc97bf4 | 2013-08-15 16:52:45 -0700 | [diff] [blame] | 3286 | mPerformedSendOrDiscard = true; |
Mindy Pereira | 82cc566 | 2012-01-09 17:29:30 -0800 | [diff] [blame] | 3287 | finish(); |
| 3288 | } |
| 3289 | |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 3290 | private void saveIfNeeded() { |
| 3291 | if (mAccount == null) { |
| 3292 | // We have not chosen an account yet so there's no way that we can save. This is ok, |
| 3293 | // though, since we are saving our state before AccountsActivity is activated. Thus, the |
| 3294 | // user has not interacted with us yet and there is no real state to save. |
| 3295 | return; |
| 3296 | } |
| 3297 | |
| 3298 | if (shouldSave()) { |
Mindy Pereira | 48e31b0 | 2012-05-30 13:12:24 -0700 | [diff] [blame] | 3299 | doSave(!mAddingAttachment /* show toast */); |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 3300 | } |
| 3301 | } |
| 3302 | |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 3303 | @Override |
| 3304 | public void onAttachmentDeleted() { |
| 3305 | mAttachmentsChanged = true; |
mindyp | 4088243 | 2012-09-06 11:07:40 -0700 | [diff] [blame] | 3306 | // If we are showing any attachments, make sure we have an upper |
| 3307 | // divider. |
| 3308 | mQuotedTextView.setUpperDividerVisible(mAttachmentsView.getAttachments().size() > 0); |
Mindy Pereira | eaea9f1 | 2012-01-10 15:05:27 -0800 | [diff] [blame] | 3309 | updateSaveUi(); |
| 3310 | } |
Mindy Pereira | 75f6663 | 2012-01-11 11:42:02 -0800 | [diff] [blame] | 3311 | |
mindyp | 4088243 | 2012-09-06 11:07:40 -0700 | [diff] [blame] | 3312 | @Override |
| 3313 | public void onAttachmentAdded() { |
| 3314 | mQuotedTextView.setUpperDividerVisible(mAttachmentsView.getAttachments().size() > 0); |
| 3315 | mAttachmentsView.focusLastAttachment(); |
| 3316 | } |
Mindy Pereira | 75f6663 | 2012-01-11 11:42:02 -0800 | [diff] [blame] | 3317 | |
| 3318 | /** |
| 3319 | * This is called any time one of our text fields changes. |
| 3320 | */ |
Marc Blank | 0bbc858 | 2012-04-23 15:07:57 -0700 | [diff] [blame] | 3321 | @Override |
Mindy Pereira | 75f6663 | 2012-01-11 11:42:02 -0800 | [diff] [blame] | 3322 | public void afterTextChanged(Editable s) { |
| 3323 | mTextChanged = true; |
| 3324 | updateSaveUi(); |
| 3325 | } |
| 3326 | |
| 3327 | @Override |
| 3328 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { |
| 3329 | // Do nothing. |
| 3330 | } |
| 3331 | |
Marc Blank | 0bbc858 | 2012-04-23 15:07:57 -0700 | [diff] [blame] | 3332 | @Override |
Mindy Pereira | 75f6663 | 2012-01-11 11:42:02 -0800 | [diff] [blame] | 3333 | public void onTextChanged(CharSequence s, int start, int before, int count) { |
| 3334 | // Do nothing. |
| 3335 | } |
| 3336 | |
| 3337 | |
| 3338 | // There is a big difference between the text associated with an address changing |
| 3339 | // to add the display name or to format properly and a recipient being added or deleted. |
| 3340 | // Make sure we only notify of changes when a recipient has been added or deleted. |
| 3341 | private class RecipientTextWatcher implements TextWatcher { |
| 3342 | private HashMap<String, Integer> mContent = new HashMap<String, Integer>(); |
| 3343 | |
| 3344 | private RecipientEditTextView mView; |
| 3345 | |
| 3346 | private TextWatcher mListener; |
| 3347 | |
| 3348 | public RecipientTextWatcher(RecipientEditTextView view, TextWatcher listener) { |
| 3349 | mView = view; |
| 3350 | mListener = listener; |
| 3351 | } |
| 3352 | |
| 3353 | @Override |
| 3354 | public void afterTextChanged(Editable s) { |
| 3355 | if (hasChanged()) { |
| 3356 | mListener.afterTextChanged(s); |
| 3357 | } |
| 3358 | } |
| 3359 | |
| 3360 | private boolean hasChanged() { |
| 3361 | String[] currRecips = tokenizeRecips(getAddressesFromList(mView)); |
| 3362 | int totalCount = currRecips.length; |
| 3363 | int totalPrevCount = 0; |
| 3364 | for (Entry<String, Integer> entry : mContent.entrySet()) { |
| 3365 | totalPrevCount += entry.getValue(); |
| 3366 | } |
| 3367 | if (totalCount != totalPrevCount) { |
| 3368 | return true; |
| 3369 | } |
| 3370 | |
| 3371 | for (String recip : currRecips) { |
| 3372 | if (!mContent.containsKey(recip)) { |
| 3373 | return true; |
| 3374 | } else { |
| 3375 | int count = mContent.get(recip) - 1; |
| 3376 | if (count < 0) { |
| 3377 | return true; |
| 3378 | } else { |
| 3379 | mContent.put(recip, count); |
| 3380 | } |
| 3381 | } |
| 3382 | } |
| 3383 | return false; |
| 3384 | } |
| 3385 | |
| 3386 | private String[] tokenizeRecips(String[] recips) { |
| 3387 | // Tokenize them all and put them in the list. |
| 3388 | String[] recipAddresses = new String[recips.length]; |
| 3389 | for (int i = 0; i < recips.length; i++) { |
| 3390 | recipAddresses[i] = Rfc822Tokenizer.tokenize(recips[i])[0].getAddress(); |
| 3391 | } |
| 3392 | return recipAddresses; |
| 3393 | } |
| 3394 | |
| 3395 | @Override |
| 3396 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { |
| 3397 | String[] recips = tokenizeRecips(getAddressesFromList(mView)); |
| 3398 | for (String recip : recips) { |
| 3399 | if (!mContent.containsKey(recip)) { |
| 3400 | mContent.put(recip, 1); |
| 3401 | } else { |
| 3402 | mContent.put(recip, (mContent.get(recip)) + 1); |
| 3403 | } |
| 3404 | } |
| 3405 | } |
| 3406 | |
| 3407 | @Override |
| 3408 | public void onTextChanged(CharSequence s, int start, int before, int count) { |
| 3409 | // Do nothing. |
| 3410 | } |
| 3411 | } |
Mindy Pereira | e011b1d | 2012-06-18 13:45:26 -0700 | [diff] [blame] | 3412 | |
| 3413 | public static void registerTestSendOrSaveCallback(SendOrSaveCallback testCallback) { |
| 3414 | if (sTestSendOrSaveCallback != null && testCallback != null) { |
| 3415 | throw new IllegalStateException("Attempting to register more than one test callback"); |
| 3416 | } |
| 3417 | sTestSendOrSaveCallback = testCallback; |
| 3418 | } |
Mindy Pereira | bddd6f3 | 2012-06-20 12:10:03 -0700 | [diff] [blame] | 3419 | |
| 3420 | @VisibleForTesting |
| 3421 | protected ArrayList<Attachment> getAttachments() { |
| 3422 | return mAttachmentsView.getAttachments(); |
| 3423 | } |
Mindy Pereira | 96a7f7a | 2012-07-09 16:51:06 -0700 | [diff] [blame] | 3424 | |
| 3425 | @Override |
| 3426 | public Loader<Cursor> onCreateLoader(int id, Bundle args) { |
| 3427 | switch (id) { |
Alice Yang | a990a71 | 2013-03-13 18:37:00 -0700 | [diff] [blame] | 3428 | case INIT_DRAFT_USING_REFERENCE_MESSAGE: |
| 3429 | return new CursorLoader(this, mRefMessageUri, UIProvider.MESSAGE_PROJECTION, null, |
| 3430 | null, null); |
Mindy Pereira | 96a7f7a | 2012-07-09 16:51:06 -0700 | [diff] [blame] | 3431 | case REFERENCE_MESSAGE_LOADER: |
| 3432 | return new CursorLoader(this, mRefMessageUri, UIProvider.MESSAGE_PROJECTION, null, |
| 3433 | null, null); |
Mindy Pereira | b199d17 | 2012-08-13 11:04:03 -0700 | [diff] [blame] | 3434 | case LOADER_ACCOUNT_CURSOR: |
| 3435 | return new CursorLoader(this, MailAppProvider.getAccountsUri(), |
| 3436 | UIProvider.ACCOUNTS_PROJECTION, null, null, null); |
Mindy Pereira | 96a7f7a | 2012-07-09 16:51:06 -0700 | [diff] [blame] | 3437 | } |
| 3438 | return null; |
| 3439 | } |
| 3440 | |
| 3441 | @Override |
| 3442 | public void onLoadFinished(Loader<Cursor> loader, Cursor data) { |
Mindy Pereira | b199d17 | 2012-08-13 11:04:03 -0700 | [diff] [blame] | 3443 | int id = loader.getId(); |
| 3444 | switch (id) { |
Alice Yang | a990a71 | 2013-03-13 18:37:00 -0700 | [diff] [blame] | 3445 | case INIT_DRAFT_USING_REFERENCE_MESSAGE: |
Mindy Pereira | b199d17 | 2012-08-13 11:04:03 -0700 | [diff] [blame] | 3446 | if (data != null && data.moveToFirst()) { |
| 3447 | mRefMessage = new Message(data); |
Mindy Pereira | b199d17 | 2012-08-13 11:04:03 -0700 | [diff] [blame] | 3448 | Intent intent = getIntent(); |
Alice Yang | a990a71 | 2013-03-13 18:37:00 -0700 | [diff] [blame] | 3449 | initFromRefMessage(mComposeMode); |
| 3450 | finishSetup(mComposeMode, intent, null); |
| 3451 | if (mComposeMode != FORWARD) { |
Mindy Pereira | b199d17 | 2012-08-13 11:04:03 -0700 | [diff] [blame] | 3452 | String to = intent.getStringExtra(EXTRA_TO); |
| 3453 | if (!TextUtils.isEmpty(to)) { |
Scott Kennedy | 0aeaf7d | 2012-11-14 18:56:05 -0800 | [diff] [blame] | 3454 | mRefMessage.setTo(null); |
| 3455 | mRefMessage.setFrom(null); |
Mindy Pereira | b199d17 | 2012-08-13 11:04:03 -0700 | [diff] [blame] | 3456 | clearChangeListeners(); |
| 3457 | mTo.append(to); |
| 3458 | initChangeListeners(); |
| 3459 | } |
| 3460 | } |
| 3461 | } else { |
| 3462 | finish(); |
Mindy Pereira | 96a7f7a | 2012-07-09 16:51:06 -0700 | [diff] [blame] | 3463 | } |
Mindy Pereira | b199d17 | 2012-08-13 11:04:03 -0700 | [diff] [blame] | 3464 | break; |
Alice Yang | a990a71 | 2013-03-13 18:37:00 -0700 | [diff] [blame] | 3465 | case REFERENCE_MESSAGE_LOADER: |
| 3466 | // Only populate mRefMessage and leave other fields untouched. |
| 3467 | if (data != null && data.moveToFirst()) { |
| 3468 | mRefMessage = new Message(data); |
| 3469 | } |
Andy Huang | 9f855d6 | 2013-05-30 17:15:03 -0700 | [diff] [blame] | 3470 | finishSetup(mComposeMode, getIntent(), mInnerSavedState); |
Alice Yang | a990a71 | 2013-03-13 18:37:00 -0700 | [diff] [blame] | 3471 | break; |
Mindy Pereira | b199d17 | 2012-08-13 11:04:03 -0700 | [diff] [blame] | 3472 | case LOADER_ACCOUNT_CURSOR: |
| 3473 | if (data != null && data.moveToFirst()) { |
| 3474 | // there are accounts now! |
| 3475 | Account account; |
Paul Westbrook | faa742f | 2012-11-01 09:50:16 -0700 | [diff] [blame] | 3476 | final ArrayList<Account> accounts = new ArrayList<Account>(); |
| 3477 | final ArrayList<Account> initializedAccounts = new ArrayList<Account>(); |
Mindy Pereira | b199d17 | 2012-08-13 11:04:03 -0700 | [diff] [blame] | 3478 | do { |
| 3479 | account = new Account(data); |
Paul Westbrook | dfa1dec | 2012-09-26 16:27:28 -0700 | [diff] [blame] | 3480 | if (account.isAccountReady()) { |
Mindy Pereira | b199d17 | 2012-08-13 11:04:03 -0700 | [diff] [blame] | 3481 | initializedAccounts.add(account); |
| 3482 | } |
| 3483 | accounts.add(account); |
| 3484 | } while (data.moveToNext()); |
| 3485 | if (initializedAccounts.size() > 0) { |
| 3486 | findViewById(R.id.wait).setVisibility(View.GONE); |
| 3487 | getLoaderManager().destroyLoader(LOADER_ACCOUNT_CURSOR); |
| 3488 | findViewById(R.id.compose).setVisibility(View.VISIBLE); |
Paul Westbrook | faa742f | 2012-11-01 09:50:16 -0700 | [diff] [blame] | 3489 | mAccounts = initializedAccounts.toArray( |
| 3490 | new Account[initializedAccounts.size()]); |
| 3491 | |
Mindy Pereira | b199d17 | 2012-08-13 11:04:03 -0700 | [diff] [blame] | 3492 | finishCreate(); |
| 3493 | invalidateOptionsMenu(); |
| 3494 | } else { |
| 3495 | // Show "waiting" |
| 3496 | account = accounts.size() > 0 ? accounts.get(0) : null; |
| 3497 | showWaitFragment(account); |
| 3498 | } |
| 3499 | } |
| 3500 | break; |
Mindy Pereira | 96a7f7a | 2012-07-09 16:51:06 -0700 | [diff] [blame] | 3501 | } |
| 3502 | } |
| 3503 | |
Mindy Pereira | b199d17 | 2012-08-13 11:04:03 -0700 | [diff] [blame] | 3504 | private void showWaitFragment(Account account) { |
| 3505 | WaitFragment fragment = getWaitFragment(); |
| 3506 | if (fragment != null) { |
| 3507 | fragment.updateAccount(account); |
| 3508 | } else { |
| 3509 | findViewById(R.id.wait).setVisibility(View.VISIBLE); |
| 3510 | replaceFragment(WaitFragment.newInstance(account, true), |
| 3511 | FragmentTransaction.TRANSIT_FRAGMENT_OPEN, TAG_WAIT); |
| 3512 | } |
| 3513 | } |
| 3514 | |
| 3515 | private WaitFragment getWaitFragment() { |
| 3516 | return (WaitFragment) getFragmentManager().findFragmentByTag(TAG_WAIT); |
| 3517 | } |
| 3518 | |
| 3519 | private int replaceFragment(Fragment fragment, int transition, String tag) { |
| 3520 | FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); |
Mindy Pereira | b199d17 | 2012-08-13 11:04:03 -0700 | [diff] [blame] | 3521 | fragmentTransaction.setTransition(transition); |
| 3522 | fragmentTransaction.replace(R.id.wait, fragment, tag); |
| 3523 | final int transactionId = fragmentTransaction.commitAllowingStateLoss(); |
| 3524 | return transactionId; |
| 3525 | } |
| 3526 | |
Mindy Pereira | 96a7f7a | 2012-07-09 16:51:06 -0700 | [diff] [blame] | 3527 | @Override |
| 3528 | public void onLoaderReset(Loader<Cursor> arg0) { |
| 3529 | // Do nothing. |
| 3530 | } |
Paul Westbrook | 83e6b57 | 2013-02-05 16:22:42 -0800 | [diff] [blame] | 3531 | |
| 3532 | @Override |
| 3533 | public Context getActivityContext() { |
| 3534 | return this; |
| 3535 | } |
Andy Huang | 1f8f4dd | 2012-10-25 21:35:35 -0700 | [diff] [blame] | 3536 | } |