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