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