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