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