Mindy Pereira | 11b0bbd | 2012-01-24 10:06:17 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 Google Inc. |
| 3 | * Licensed to The Android Open Source Project. |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | package com.android.mail.browse; |
| 19 | |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 20 | import android.content.Context; |
Marc Blank | a6b671d | 2012-05-25 12:52:02 -0700 | [diff] [blame] | 21 | import android.net.Uri; |
Scott Kennedy | dd2ec68 | 2013-06-03 19:16:13 -0700 | [diff] [blame] | 22 | import android.os.AsyncTask; |
Andrew Sapperstein | 52882ff | 2014-07-27 12:30:18 -0700 | [diff] [blame] | 23 | import android.support.v7.view.ActionMode; |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 24 | import android.view.Menu; |
| 25 | import android.view.MenuInflater; |
| 26 | import android.view.MenuItem; |
Marc Blank | a6b671d | 2012-05-25 12:52:02 -0700 | [diff] [blame] | 27 | import android.widget.Toast; |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 28 | |
Mindy Pereira | 11b0bbd | 2012-01-24 10:06:17 -0800 | [diff] [blame] | 29 | import com.android.mail.R; |
Andy Huang | 042a530 | 2013-08-13 12:39:08 -0700 | [diff] [blame] | 30 | import com.android.mail.analytics.Analytics; |
Mindy Pereira | 30d2d32 | 2012-02-03 08:40:49 -0800 | [diff] [blame] | 31 | import com.android.mail.providers.Account; |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 32 | import com.android.mail.providers.AccountObserver; |
Mindy Pereira | 11b0bbd | 2012-01-24 10:06:17 -0800 | [diff] [blame] | 33 | import com.android.mail.providers.Conversation; |
Mindy Pereira | 4f166de | 2012-02-14 13:40:58 -0800 | [diff] [blame] | 34 | import com.android.mail.providers.Folder; |
Marc Blank | a6b671d | 2012-05-25 12:52:02 -0700 | [diff] [blame] | 35 | import com.android.mail.providers.MailAppProvider; |
Mindy Pereira | efe3d25 | 2012-03-01 14:20:44 -0800 | [diff] [blame] | 36 | import com.android.mail.providers.Settings; |
Mindy Pereira | f98b318 | 2012-02-22 11:07:13 -0800 | [diff] [blame] | 37 | import com.android.mail.providers.UIProvider; |
Paul Westbrook | ef36254 | 2012-08-27 14:53:32 -0700 | [diff] [blame] | 38 | import com.android.mail.providers.UIProvider.AccountCapabilities; |
Marc Blank | cf164d6 | 2012-04-20 08:56:17 -0700 | [diff] [blame] | 39 | import com.android.mail.providers.UIProvider.ConversationColumns; |
Paul Westbrook | bf232c3 | 2012-04-18 03:17:41 -0700 | [diff] [blame] | 40 | import com.android.mail.providers.UIProvider.FolderCapabilities; |
Mindy Pereira | 9ba4251 | 2012-08-14 11:52:48 -0700 | [diff] [blame] | 41 | import com.android.mail.providers.UIProvider.FolderType; |
Vikram Aggarwal | 531488e | 2012-05-29 16:36:52 -0700 | [diff] [blame] | 42 | import com.android.mail.ui.ControllableActivity; |
Jin Cao | ec0fa48 | 2014-08-28 16:38:08 -0700 | [diff] [blame] | 43 | import com.android.mail.ui.ConversationCheckedSet; |
Vikram Aggarwal | a8e4318 | 2012-09-13 12:55:10 -0700 | [diff] [blame] | 44 | import com.android.mail.ui.ConversationListCallbacks; |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 45 | import com.android.mail.ui.ConversationSetObserver; |
Vikram Aggarwal | 531488e | 2012-05-29 16:36:52 -0700 | [diff] [blame] | 46 | import com.android.mail.ui.ConversationUpdater; |
Marc Blank | a6b671d | 2012-05-25 12:52:02 -0700 | [diff] [blame] | 47 | import com.android.mail.ui.DestructiveAction; |
Scott Kennedy | dd2ec68 | 2013-06-03 19:16:13 -0700 | [diff] [blame] | 48 | import com.android.mail.ui.FolderOperation; |
Mark Wei | 9eb1c9a | 2012-10-01 12:54:50 -0700 | [diff] [blame] | 49 | import com.android.mail.ui.FolderSelectionDialog; |
Paul Westbrook | b334c90 | 2012-06-25 11:42:46 -0700 | [diff] [blame] | 50 | import com.android.mail.utils.LogTag; |
Mindy Pereira | 11b0bbd | 2012-01-24 10:06:17 -0800 | [diff] [blame] | 51 | import com.android.mail.utils.LogUtils; |
Mindy Pereira | efe3d25 | 2012-03-01 14:20:44 -0800 | [diff] [blame] | 52 | import com.android.mail.utils.Utils; |
Mindy Pereira | 11b0bbd | 2012-01-24 10:06:17 -0800 | [diff] [blame] | 53 | import com.google.common.annotations.VisibleForTesting; |
Scott Kennedy | dd2ec68 | 2013-06-03 19:16:13 -0700 | [diff] [blame] | 54 | import com.google.common.collect.Lists; |
Mindy Pereira | 11b0bbd | 2012-01-24 10:06:17 -0800 | [diff] [blame] | 55 | |
| 56 | import java.util.Collection; |
Scott Kennedy | dd2ec68 | 2013-06-03 19:16:13 -0700 | [diff] [blame] | 57 | import java.util.List; |
Mindy Pereira | 11b0bbd | 2012-01-24 10:06:17 -0800 | [diff] [blame] | 58 | |
Mindy Pereira | 11b0bbd | 2012-01-24 10:06:17 -0800 | [diff] [blame] | 59 | /** |
| 60 | * A component that displays a custom view for an {@code ActionBar}'s {@code |
| 61 | * ContextMode} specific to operating on a set of conversations. |
| 62 | */ |
| 63 | public class SelectedConversationsActionMenu implements ActionMode.Callback, |
Vikram Aggarwal | d503df4 | 2012-05-11 10:13:35 -0700 | [diff] [blame] | 64 | ConversationSetObserver { |
Mindy Pereira | 11b0bbd | 2012-01-24 10:06:17 -0800 | [diff] [blame] | 65 | |
Paul Westbrook | b334c90 | 2012-06-25 11:42:46 -0700 | [diff] [blame] | 66 | private static final String LOG_TAG = LogTag.getLogTag(); |
Mindy Pereira | 11b0bbd | 2012-01-24 10:06:17 -0800 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * The set of conversations to display the menu for. |
| 70 | */ |
Jin Cao | ec0fa48 | 2014-08-28 16:38:08 -0700 | [diff] [blame] | 71 | protected final ConversationCheckedSet mCheckedSet; |
Mindy Pereira | 11b0bbd | 2012-01-24 10:06:17 -0800 | [diff] [blame] | 72 | |
Vikram Aggarwal | a8e4318 | 2012-09-13 12:55:10 -0700 | [diff] [blame] | 73 | private final ControllableActivity mActivity; |
| 74 | private final ConversationListCallbacks mListController; |
Vikram Aggarwal | cd8bf0a | 2012-02-14 13:13:18 -0800 | [diff] [blame] | 75 | /** |
| 76 | * Context of the activity. A dialog requires the context of an activity rather than the global |
| 77 | * root context of the process. So mContext = mActivity.getApplicationContext() will fail. |
| 78 | */ |
Mindy Pereira | 11b0bbd | 2012-01-24 10:06:17 -0800 | [diff] [blame] | 79 | private final Context mContext; |
| 80 | |
| 81 | @VisibleForTesting |
Vikram Aggarwal | 4f4782b | 2012-05-30 08:39:09 -0700 | [diff] [blame] | 82 | private ActionMode mActionMode; |
Mindy Pereira | 11b0bbd | 2012-01-24 10:06:17 -0800 | [diff] [blame] | 83 | |
| 84 | private boolean mActivated = false; |
| 85 | |
Vikram Aggarwal | 531488e | 2012-05-29 16:36:52 -0700 | [diff] [blame] | 86 | /** Object that can update conversation state on our behalf. */ |
| 87 | private final ConversationUpdater mUpdater; |
Mindy Pereira | 30d2d32 | 2012-02-03 08:40:49 -0800 | [diff] [blame] | 88 | |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 89 | private Account mAccount; |
Mindy Pereira | d92e9f3 | 2012-02-03 09:10:58 -0800 | [diff] [blame] | 90 | |
Vikram Aggarwal | 531488e | 2012-05-29 16:36:52 -0700 | [diff] [blame] | 91 | private final Folder mFolder; |
Mindy Pereira | 4f166de | 2012-02-14 13:40:58 -0800 | [diff] [blame] | 92 | |
Mindy Pereira | 14f64ec | 2012-08-14 17:14:02 -0700 | [diff] [blame] | 93 | private AccountObserver mAccountObserver; |
Mindy Pereira | 07118a0 | 2012-04-02 16:35:01 -0700 | [diff] [blame] | 94 | |
Jin Cao | 512821c | 2014-05-30 15:54:04 -0700 | [diff] [blame] | 95 | private MenuItem mDiscardOutboxMenuItem; |
| 96 | |
Vikram Aggarwal | 7704d79 | 2013-01-11 15:48:24 -0800 | [diff] [blame] | 97 | public SelectedConversationsActionMenu( |
Jin Cao | ec0fa48 | 2014-08-28 16:38:08 -0700 | [diff] [blame] | 98 | ControllableActivity activity, ConversationCheckedSet checkedSet, Folder folder) { |
Mindy Pereira | 11b0bbd | 2012-01-24 10:06:17 -0800 | [diff] [blame] | 99 | mActivity = activity; |
Vikram Aggarwal | a8e4318 | 2012-09-13 12:55:10 -0700 | [diff] [blame] | 100 | mListController = activity.getListHandler(); |
Jin Cao | ec0fa48 | 2014-08-28 16:38:08 -0700 | [diff] [blame] | 101 | mCheckedSet = checkedSet; |
Mindy Pereira | 14f64ec | 2012-08-14 17:14:02 -0700 | [diff] [blame] | 102 | mAccountObserver = new AccountObserver() { |
| 103 | @Override |
| 104 | public void onChanged(Account newAccount) { |
| 105 | mAccount = newAccount; |
| 106 | } |
| 107 | }; |
Vikram Aggarwal | 7c401b7 | 2012-08-13 16:43:47 -0700 | [diff] [blame] | 108 | mAccount = mAccountObserver.initialize(activity.getAccountController()); |
Mindy Pereira | 4f166de | 2012-02-14 13:40:58 -0800 | [diff] [blame] | 109 | mFolder = folder; |
Vikram Aggarwal | 04dc819 | 2012-04-09 13:07:19 -0700 | [diff] [blame] | 110 | mContext = mActivity.getActivityContext(); |
mindyp | 54f120f | 2012-08-28 13:10:33 -0700 | [diff] [blame] | 111 | mUpdater = activity.getConversationUpdater(); |
Vikram Aggarwal | e8a8532 | 2012-04-24 09:01:38 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Jin Cao | a6b4521 | 2014-09-18 13:46:54 -0700 | [diff] [blame] | 114 | public boolean onActionItemClicked(MenuItem item) { |
| 115 | return onActionItemClicked(mActionMode, item); |
| 116 | } |
| 117 | |
Mindy Pereira | 11b0bbd | 2012-01-24 10:06:17 -0800 | [diff] [blame] | 118 | @Override |
| 119 | public boolean onActionItemClicked(ActionMode mode, MenuItem item) { |
| 120 | boolean handled = true; |
Mindy Pereira | 8937bf1 | 2012-07-23 14:05:02 -0700 | [diff] [blame] | 121 | // If the user taps a new menu item, commit any existing destructive actions. |
Vikram Aggarwal | a8e4318 | 2012-09-13 12:55:10 -0700 | [diff] [blame] | 122 | mListController.commitDestructiveActions(true); |
Scott Kennedy | 2b9d80e | 2013-07-30 23:03:45 -0700 | [diff] [blame] | 123 | final int itemId = item.getItemId(); |
Andy Huang | 042a530 | 2013-08-13 12:39:08 -0700 | [diff] [blame] | 124 | |
Andy Huang | 2b55549 | 2013-08-14 21:06:21 -0700 | [diff] [blame] | 125 | Analytics.getInstance().sendMenuItemEvent(Analytics.EVENT_CATEGORY_MENU_ITEM, itemId, |
| 126 | "cab_mode", 0); |
Andy Huang | 042a530 | 2013-08-13 12:39:08 -0700 | [diff] [blame] | 127 | |
Jin Cao | f558637 | 2014-04-08 14:28:36 -0700 | [diff] [blame] | 128 | UndoCallback undoCallback = null; // not applicable here (yet) |
Scott Kennedy | 2b9d80e | 2013-07-30 23:03:45 -0700 | [diff] [blame] | 129 | if (itemId == R.id.delete) { |
| 130 | LogUtils.i(LOG_TAG, "Delete selected from CAB menu"); |
Jin Cao | f558637 | 2014-04-08 14:28:36 -0700 | [diff] [blame] | 131 | performDestructiveAction(R.id.delete, undoCallback); |
Scott Kennedy | 2b9d80e | 2013-07-30 23:03:45 -0700 | [diff] [blame] | 132 | } else if (itemId == R.id.discard_drafts) { |
Jin Cao | 512821c | 2014-05-30 15:54:04 -0700 | [diff] [blame] | 133 | LogUtils.i(LOG_TAG, "Discard drafts selected from CAB menu"); |
Jin Cao | f558637 | 2014-04-08 14:28:36 -0700 | [diff] [blame] | 134 | performDestructiveAction(R.id.discard_drafts, undoCallback); |
Jin Cao | 512821c | 2014-05-30 15:54:04 -0700 | [diff] [blame] | 135 | } else if (itemId == R.id.discard_outbox) { |
| 136 | LogUtils.i(LOG_TAG, "Discard outbox selected from CAB menu"); |
| 137 | performDestructiveAction(R.id.discard_outbox, undoCallback); |
Scott Kennedy | 2b9d80e | 2013-07-30 23:03:45 -0700 | [diff] [blame] | 138 | } else if (itemId == R.id.archive) { |
| 139 | LogUtils.i(LOG_TAG, "Archive selected from CAB menu"); |
Jin Cao | f558637 | 2014-04-08 14:28:36 -0700 | [diff] [blame] | 140 | performDestructiveAction(R.id.archive, undoCallback); |
Scott Kennedy | 2b9d80e | 2013-07-30 23:03:45 -0700 | [diff] [blame] | 141 | } else if (itemId == R.id.remove_folder) { |
Jin Cao | ec0fa48 | 2014-08-28 16:38:08 -0700 | [diff] [blame] | 142 | destroy(R.id.remove_folder, mCheckedSet.values(), |
| 143 | mUpdater.getDeferredRemoveFolder(mCheckedSet.values(), mFolder, true, |
Jin Cao | f558637 | 2014-04-08 14:28:36 -0700 | [diff] [blame] | 144 | true, true, undoCallback)); |
Scott Kennedy | 2b9d80e | 2013-07-30 23:03:45 -0700 | [diff] [blame] | 145 | } else if (itemId == R.id.mute) { |
Jin Cao | ec0fa48 | 2014-08-28 16:38:08 -0700 | [diff] [blame] | 146 | destroy(R.id.mute, mCheckedSet.values(), mUpdater.getBatchAction(R.id.mute, |
Jin Cao | f558637 | 2014-04-08 14:28:36 -0700 | [diff] [blame] | 147 | undoCallback)); |
Scott Kennedy | 2b9d80e | 2013-07-30 23:03:45 -0700 | [diff] [blame] | 148 | } else if (itemId == R.id.report_spam) { |
Jin Cao | ec0fa48 | 2014-08-28 16:38:08 -0700 | [diff] [blame] | 149 | destroy(R.id.report_spam, mCheckedSet.values(), |
Jin Cao | f558637 | 2014-04-08 14:28:36 -0700 | [diff] [blame] | 150 | mUpdater.getBatchAction(R.id.report_spam, undoCallback)); |
Scott Kennedy | 2b9d80e | 2013-07-30 23:03:45 -0700 | [diff] [blame] | 151 | } else if (itemId == R.id.mark_not_spam) { |
| 152 | // Currently, since spam messages are only shown in list with other spam messages, |
| 153 | // marking a message not as spam is a destructive action |
| 154 | destroy (R.id.mark_not_spam, |
Jin Cao | ec0fa48 | 2014-08-28 16:38:08 -0700 | [diff] [blame] | 155 | mCheckedSet.values(), mUpdater.getBatchAction(R.id.mark_not_spam, |
Jin Cao | f558637 | 2014-04-08 14:28:36 -0700 | [diff] [blame] | 156 | undoCallback)) ; |
Scott Kennedy | 2b9d80e | 2013-07-30 23:03:45 -0700 | [diff] [blame] | 157 | } else if (itemId == R.id.report_phishing) { |
| 158 | destroy(R.id.report_phishing, |
Jin Cao | ec0fa48 | 2014-08-28 16:38:08 -0700 | [diff] [blame] | 159 | mCheckedSet.values(), mUpdater.getBatchAction(R.id.report_phishing, |
Jin Cao | f558637 | 2014-04-08 14:28:36 -0700 | [diff] [blame] | 160 | undoCallback)); |
Scott Kennedy | 2b9d80e | 2013-07-30 23:03:45 -0700 | [diff] [blame] | 161 | } else if (itemId == R.id.read) { |
| 162 | markConversationsRead(true); |
| 163 | } else if (itemId == R.id.unread) { |
| 164 | markConversationsRead(false); |
| 165 | } else if (itemId == R.id.star) { |
| 166 | starConversations(true); |
Jin Cao | a6b4521 | 2014-09-18 13:46:54 -0700 | [diff] [blame] | 167 | } else if (itemId == R.id.toggle_read_unread) { |
| 168 | if (mActionMode != null) { |
| 169 | markConversationsRead(mActionMode.getMenu().findItem(R.id.read).isVisible()); |
| 170 | } |
Scott Kennedy | 2b9d80e | 2013-07-30 23:03:45 -0700 | [diff] [blame] | 171 | } else if (itemId == R.id.remove_star) { |
| 172 | if (mFolder.isType(UIProvider.FolderType.STARRED)) { |
| 173 | LogUtils.d(LOG_TAG, "We are in a starred folder, removing the star"); |
Jin Cao | f558637 | 2014-04-08 14:28:36 -0700 | [diff] [blame] | 174 | performDestructiveAction(R.id.remove_star, undoCallback); |
Scott Kennedy | 2b9d80e | 2013-07-30 23:03:45 -0700 | [diff] [blame] | 175 | } else { |
| 176 | LogUtils.d(LOG_TAG, "Not in a starred folder."); |
| 177 | starConversations(false); |
| 178 | } |
Andrew Sapperstein | 6c570db | 2013-08-06 17:21:36 -0700 | [diff] [blame] | 179 | } else if (itemId == R.id.move_to || itemId == R.id.change_folders) { |
Scott Kennedy | 2b9d80e | 2013-07-30 23:03:45 -0700 | [diff] [blame] | 180 | boolean cantMove = false; |
| 181 | Account acct = mAccount; |
| 182 | // Special handling for virtual folders |
| 183 | if (mFolder.supportsCapability(FolderCapabilities.IS_VIRTUAL)) { |
| 184 | Uri accountUri = null; |
Jin Cao | ec0fa48 | 2014-08-28 16:38:08 -0700 | [diff] [blame] | 185 | for (Conversation conv: mCheckedSet.values()) { |
Scott Kennedy | 2b9d80e | 2013-07-30 23:03:45 -0700 | [diff] [blame] | 186 | if (accountUri == null) { |
| 187 | accountUri = conv.accountUri; |
| 188 | } else if (!accountUri.equals(conv.accountUri)) { |
| 189 | // Tell the user why we can't do this |
| 190 | Toast.makeText(mContext, R.string.cant_move_or_change_labels, |
| 191 | Toast.LENGTH_LONG).show(); |
| 192 | cantMove = true; |
| 193 | return handled; |
Marc Blank | a6b671d | 2012-05-25 12:52:02 -0700 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | if (!cantMove) { |
Scott Kennedy | 2b9d80e | 2013-07-30 23:03:45 -0700 | [diff] [blame] | 197 | // Get the actual account here, so that we display its folders in the dialog |
| 198 | acct = MailAppProvider.getAccountFromAccountUri(accountUri); |
Marc Blank | a6b671d | 2012-05-25 12:52:02 -0700 | [diff] [blame] | 199 | } |
Scott Kennedy | 2b9d80e | 2013-07-30 23:03:45 -0700 | [diff] [blame] | 200 | } |
| 201 | if (!cantMove) { |
| 202 | final FolderSelectionDialog dialog = FolderSelectionDialog.getInstance( |
Jin Cao | ec0fa48 | 2014-08-28 16:38:08 -0700 | [diff] [blame] | 203 | acct, mCheckedSet.values(), true, mFolder, |
Scott Kennedy | 2b9d80e | 2013-07-30 23:03:45 -0700 | [diff] [blame] | 204 | item.getItemId() == R.id.move_to); |
| 205 | if (dialog != null) { |
Tony Mantler | 2a4be24 | 2013-12-11 15:30:53 -0800 | [diff] [blame] | 206 | dialog.show(mActivity.getFragmentManager(), null); |
Scott Kennedy | 2b9d80e | 2013-07-30 23:03:45 -0700 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | } else if (itemId == R.id.move_to_inbox) { |
| 210 | new AsyncTask<Void, Void, Folder>() { |
| 211 | @Override |
| 212 | protected Folder doInBackground(final Void... params) { |
| 213 | // Get the "move to" inbox |
| 214 | return Utils.getFolder(mContext, mAccount.settings.moveToInbox, |
| 215 | true /* allowHidden */); |
| 216 | } |
Scott Kennedy | dd2ec68 | 2013-06-03 19:16:13 -0700 | [diff] [blame] | 217 | |
Scott Kennedy | 2b9d80e | 2013-07-30 23:03:45 -0700 | [diff] [blame] | 218 | @Override |
| 219 | protected void onPostExecute(final Folder moveToInbox) { |
| 220 | final List<FolderOperation> ops = Lists.newArrayListWithCapacity(1); |
| 221 | // Add inbox |
| 222 | ops.add(new FolderOperation(moveToInbox, true)); |
Jin Cao | ec0fa48 | 2014-08-28 16:38:08 -0700 | [diff] [blame] | 223 | mUpdater.assignFolder(ops, mCheckedSet.values(), true, |
Scott Kennedy | 2b9d80e | 2013-07-30 23:03:45 -0700 | [diff] [blame] | 224 | true /* showUndo */, false /* isMoveTo */); |
Vikram Aggarwal | 04dc819 | 2012-04-09 13:07:19 -0700 | [diff] [blame] | 225 | } |
Scott Kennedy | 2b9d80e | 2013-07-30 23:03:45 -0700 | [diff] [blame] | 226 | }.execute((Void[]) null); |
| 227 | } else if (itemId == R.id.mark_important) { |
| 228 | markConversationsImportant(true); |
| 229 | } else if (itemId == R.id.mark_not_important) { |
| 230 | if (mFolder.supportsCapability(UIProvider.FolderCapabilities.ONLY_IMPORTANT)) { |
Jin Cao | f558637 | 2014-04-08 14:28:36 -0700 | [diff] [blame] | 231 | performDestructiveAction(R.id.mark_not_important, undoCallback); |
Scott Kennedy | 2b9d80e | 2013-07-30 23:03:45 -0700 | [diff] [blame] | 232 | } else { |
| 233 | markConversationsImportant(false); |
| 234 | } |
| 235 | } else { |
| 236 | handled = false; |
Mindy Pereira | 11b0bbd | 2012-01-24 10:06:17 -0800 | [diff] [blame] | 237 | } |
| 238 | return handled; |
| 239 | } |
| 240 | |
Vikram Aggarwal | ad6d628 | 2012-03-28 09:16:14 -0700 | [diff] [blame] | 241 | /** |
| 242 | * Clear the selection and perform related UI changes to keep the state consistent. |
| 243 | */ |
Jin Cao | ec0fa48 | 2014-08-28 16:38:08 -0700 | [diff] [blame] | 244 | private void clearChecked() { |
| 245 | mCheckedSet.clear(); |
Vikram Aggarwal | ad6d628 | 2012-03-28 09:16:14 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Vikram Aggarwal | 04dc819 | 2012-04-09 13:07:19 -0700 | [diff] [blame] | 248 | /** |
| 249 | * Update the underlying list adapter and redraw the menus if necessary. |
| 250 | */ |
| 251 | private void updateSelection() { |
Vikram Aggarwal | 4f4782b | 2012-05-30 08:39:09 -0700 | [diff] [blame] | 252 | mUpdater.refreshConversationList(); |
Vikram Aggarwal | 04dc819 | 2012-04-09 13:07:19 -0700 | [diff] [blame] | 253 | if (mActionMode != null) { |
| 254 | // Calling mActivity.invalidateOptionsMenu doesn't have the correct behavior, since |
| 255 | // the action mode is not refreshed when activity's options menu is invalidated. |
| 256 | // Since we need to refresh our own menu, it is easy to call onPrepareActionMode |
| 257 | // directly. |
| 258 | onPrepareActionMode(mActionMode, mActionMode.getMenu()); |
| 259 | } |
| 260 | } |
| 261 | |
Jin Cao | f558637 | 2014-04-08 14:28:36 -0700 | [diff] [blame] | 262 | private void performDestructiveAction(final int action, UndoCallback undoCallback) { |
Jin Cao | ec0fa48 | 2014-08-28 16:38:08 -0700 | [diff] [blame] | 263 | final Collection<Conversation> conversations = mCheckedSet.values(); |
Vikram Aggarwal | 6cadbfc | 2012-12-27 09:17:05 -0800 | [diff] [blame] | 264 | final Settings settings = mAccount.settings; |
Andy Huang | 121c8b8 | 2013-08-05 11:52:55 -0700 | [diff] [blame] | 265 | final boolean showDialog; |
| 266 | // no confirmation dialog by default unless user preference or common sense dictates one |
| 267 | if (action == R.id.discard_drafts) { |
| 268 | // drafts are lost forever, so always confirm |
| 269 | showDialog = true; |
| 270 | } else if (settings != null && (action == R.id.archive || action == R.id.delete)) { |
| 271 | showDialog = (action == R.id.delete) ? settings.confirmDelete : settings.confirmArchive; |
| 272 | } else { |
| 273 | showDialog = false; |
| 274 | } |
Mindy Pereira | efe3d25 | 2012-03-01 14:20:44 -0800 | [diff] [blame] | 275 | if (showDialog) { |
Jin Cao | f558637 | 2014-04-08 14:28:36 -0700 | [diff] [blame] | 276 | mUpdater.makeDialogListener(action, true /* fromSelectedSet */, null /* undoCallback */); |
Paul Westbrook | ef36254 | 2012-08-27 14:53:32 -0700 | [diff] [blame] | 277 | final int resId; |
Scott Kennedy | 2b9d80e | 2013-07-30 23:03:45 -0700 | [diff] [blame] | 278 | if (action == R.id.delete) { |
| 279 | resId = R.plurals.confirm_delete_conversation; |
| 280 | } else if (action == R.id.discard_drafts) { |
| 281 | resId = R.plurals.confirm_discard_drafts_conversation; |
| 282 | } else { |
| 283 | resId = R.plurals.confirm_archive_conversation; |
Paul Westbrook | ef36254 | 2012-08-27 14:53:32 -0700 | [diff] [blame] | 284 | } |
Vikram Aggarwal | 6cadbfc | 2012-12-27 09:17:05 -0800 | [diff] [blame] | 285 | final CharSequence message = Utils.formatPlural(mContext, resId, conversations.size()); |
| 286 | final ConfirmDialogFragment c = ConfirmDialogFragment.newInstance(message); |
Vikram Aggarwal | b8c3171 | 2013-01-03 17:03:19 -0800 | [diff] [blame] | 287 | c.displayDialog(mActivity.getFragmentManager()); |
Mindy Pereira | efe3d25 | 2012-03-01 14:20:44 -0800 | [diff] [blame] | 288 | } else { |
Vikram Aggarwal | 6cadbfc | 2012-12-27 09:17:05 -0800 | [diff] [blame] | 289 | // No need to show the dialog, just make a destructive action and destroy the |
| 290 | // selected set immediately. |
Vikram Aggarwal | b8c3171 | 2013-01-03 17:03:19 -0800 | [diff] [blame] | 291 | // TODO(viki): Stop using the deferred action here. Use the registered action. |
Jin Cao | f558637 | 2014-04-08 14:28:36 -0700 | [diff] [blame] | 292 | destroy(action, conversations, mUpdater.getDeferredBatchAction(action, undoCallback)); |
Mindy Pereira | 07118a0 | 2012-04-02 16:35:01 -0700 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
Vikram Aggarwal | 84fe994 | 2013-04-17 11:20:58 -0700 | [diff] [blame] | 296 | /** |
| 297 | * Destroy these conversations through the conversation updater |
| 298 | * @param actionId the ID of the action: R.id.archive, R.id.delete, ... |
| 299 | * @param target conversations to destroy |
| 300 | * @param action the action that performs the destruction |
| 301 | */ |
| 302 | private void destroy(int actionId, final Collection<Conversation> target, |
| 303 | final DestructiveAction action) { |
Alice Yang | 193e05a | 2013-05-05 14:12:08 -0700 | [diff] [blame] | 304 | LogUtils.i(LOG_TAG, "About to remove %d converations", target.size()); |
Scott Kennedy | caaeed3 | 2013-06-12 13:39:16 -0700 | [diff] [blame] | 305 | mUpdater.delete(actionId, target, action, true); |
Mindy Pereira | efe3d25 | 2012-03-01 14:20:44 -0800 | [diff] [blame] | 306 | } |
| 307 | |
Vikram Aggarwal | 0e091c5 | 2012-05-30 15:07:15 -0700 | [diff] [blame] | 308 | /** |
| 309 | * Marks the read state of currently selected conversations (<b>and</b> the backing storage) |
| 310 | * to the value provided here. |
| 311 | * @param read is true if the conversations are to be marked as read, false if they are to be |
| 312 | * marked unread. |
| 313 | */ |
Mindy Pereira | febeb5f | 2012-02-14 15:08:13 -0800 | [diff] [blame] | 314 | private void markConversationsRead(boolean read) { |
Jin Cao | ec0fa48 | 2014-08-28 16:38:08 -0700 | [diff] [blame] | 315 | final Collection<Conversation> targets = mCheckedSet.values(); |
Vikram Aggarwal | 66bc2aa | 2012-08-02 10:47:03 -0700 | [diff] [blame] | 316 | // The conversations are marked read but not viewed. |
| 317 | mUpdater.markConversationsRead(targets, read, false); |
Vikram Aggarwal | 04dc819 | 2012-04-09 13:07:19 -0700 | [diff] [blame] | 318 | updateSelection(); |
Mindy Pereira | 7f0a962 | 2012-02-29 15:00:34 -0800 | [diff] [blame] | 319 | } |
| 320 | |
Vikram Aggarwal | 04dc819 | 2012-04-09 13:07:19 -0700 | [diff] [blame] | 321 | /** |
Vikram Aggarwal | 0e091c5 | 2012-05-30 15:07:15 -0700 | [diff] [blame] | 322 | * Marks the important state of currently selected conversations (<b>and</b> the backing |
| 323 | * storage) to the value provided here. |
| 324 | * @param important is true if the conversations are to be marked as important, false if they |
| 325 | * are to be marked not important. |
| 326 | */ |
| 327 | private void markConversationsImportant(boolean important) { |
Jin Cao | ec0fa48 | 2014-08-28 16:38:08 -0700 | [diff] [blame] | 328 | final Collection<Conversation> target = mCheckedSet.values(); |
Vikram Aggarwal | 0e091c5 | 2012-05-30 15:07:15 -0700 | [diff] [blame] | 329 | final int priority = important ? UIProvider.ConversationPriority.HIGH |
| 330 | : UIProvider.ConversationPriority.LOW; |
| 331 | mUpdater.updateConversation(target, ConversationColumns.PRIORITY, priority); |
| 332 | // Update the conversations in the selection too. |
| 333 | for (final Conversation c : target) { |
| 334 | c.priority = priority; |
| 335 | } |
| 336 | updateSelection(); |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * Marks the selected conversations with the star setting provided here. |
Vikram Aggarwal | 04dc819 | 2012-04-09 13:07:19 -0700 | [diff] [blame] | 341 | * @param star true if you want all the conversations to have stars, false if you want to remove |
| 342 | * stars from all conversations |
| 343 | */ |
Mindy Pereira | febeb5f | 2012-02-14 15:08:13 -0800 | [diff] [blame] | 344 | private void starConversations(boolean star) { |
Jin Cao | ec0fa48 | 2014-08-28 16:38:08 -0700 | [diff] [blame] | 345 | final Collection<Conversation> target = mCheckedSet.values(); |
Vikram Aggarwal | 0e091c5 | 2012-05-30 15:07:15 -0700 | [diff] [blame] | 346 | mUpdater.updateConversation(target, ConversationColumns.STARRED, star); |
| 347 | // Update the conversations in the selection too. |
| 348 | for (final Conversation c : target) { |
| 349 | c.starred = star; |
| 350 | } |
Vikram Aggarwal | 04dc819 | 2012-04-09 13:07:19 -0700 | [diff] [blame] | 351 | updateSelection(); |
Mindy Pereira | febeb5f | 2012-02-14 15:08:13 -0800 | [diff] [blame] | 352 | } |
| 353 | |
Mindy Pereira | 11b0bbd | 2012-01-24 10:06:17 -0800 | [diff] [blame] | 354 | @Override |
| 355 | public boolean onCreateActionMode(ActionMode mode, Menu menu) { |
Jin Cao | ec0fa48 | 2014-08-28 16:38:08 -0700 | [diff] [blame] | 356 | mCheckedSet.addObserver(this); |
Vikram Aggarwal | 7f602f7 | 2012-04-30 16:04:06 -0700 | [diff] [blame] | 357 | final MenuInflater inflater = mActivity.getMenuInflater(); |
Mindy Pereira | 11b0bbd | 2012-01-24 10:06:17 -0800 | [diff] [blame] | 358 | inflater.inflate(R.menu.conversation_list_selection_actions_menu, menu); |
| 359 | mActionMode = mode; |
Andrew Sapperstein | db861e9 | 2014-09-07 17:54:30 -0700 | [diff] [blame] | 360 | updateCount(); |
Mindy Pereira | 11b0bbd | 2012-01-24 10:06:17 -0800 | [diff] [blame] | 361 | return true; |
| 362 | } |
| 363 | |
| 364 | @Override |
| 365 | public boolean onPrepareActionMode(ActionMode mode, Menu menu) { |
Vikram Aggarwal | e5e917c | 2012-09-20 16:27:41 -0700 | [diff] [blame] | 366 | // Update the actionbar to select operations available on the current conversation. |
Jin Cao | ec0fa48 | 2014-08-28 16:38:08 -0700 | [diff] [blame] | 367 | final Collection<Conversation> conversations = mCheckedSet.values(); |
Mindy Pereira | febeb5f | 2012-02-14 15:08:13 -0800 | [diff] [blame] | 368 | boolean showStar = false; |
| 369 | boolean showMarkUnread = false; |
Mindy Pereira | 7f0a962 | 2012-02-29 15:00:34 -0800 | [diff] [blame] | 370 | boolean showMarkImportant = false; |
Paul Westbrook | 77eee62 | 2012-07-10 13:41:57 -0700 | [diff] [blame] | 371 | boolean showMarkNotSpam = false; |
Paul Westbrook | 76b2062 | 2012-07-12 11:45:43 -0700 | [diff] [blame] | 372 | boolean showMarkAsPhishing = false; |
Mindy Pereira | 7f0a962 | 2012-02-29 15:00:34 -0800 | [diff] [blame] | 373 | |
Rohan Shah | 69419bb | 2014-08-15 15:53:29 -0700 | [diff] [blame] | 374 | // TODO(shahrk): Clean up these dirty calls using Utils.setMenuItemVisibility(...) or |
| 375 | // in another way |
| 376 | |
Mindy Pereira | febeb5f | 2012-02-14 15:08:13 -0800 | [diff] [blame] | 377 | for (Conversation conversation : conversations) { |
| 378 | if (!conversation.starred) { |
| 379 | showStar = true; |
| 380 | } |
| 381 | if (conversation.read) { |
| 382 | showMarkUnread = true; |
| 383 | } |
Mindy Pereira | 7f0a962 | 2012-02-29 15:00:34 -0800 | [diff] [blame] | 384 | if (!conversation.isImportant()) { |
| 385 | showMarkImportant = true; |
| 386 | } |
Paul Westbrook | 77eee62 | 2012-07-10 13:41:57 -0700 | [diff] [blame] | 387 | if (conversation.spam) { |
| 388 | showMarkNotSpam = true; |
| 389 | } |
Paul Westbrook | 76b2062 | 2012-07-12 11:45:43 -0700 | [diff] [blame] | 390 | if (!conversation.phishing) { |
| 391 | showMarkAsPhishing = true; |
| 392 | } |
| 393 | if (showStar && showMarkUnread && showMarkImportant && showMarkNotSpam && |
| 394 | showMarkAsPhishing) { |
Mindy Pereira | febeb5f | 2012-02-14 15:08:13 -0800 | [diff] [blame] | 395 | break; |
| 396 | } |
| 397 | } |
Tony Mantler | ec86c42 | 2014-09-24 14:33:20 -0700 | [diff] [blame] | 398 | final boolean canStar = mFolder != null && !mFolder.isTrash(); |
Paul Westbrook | 334e64a | 2012-02-23 13:26:35 -0800 | [diff] [blame] | 399 | final MenuItem star = menu.findItem(R.id.star); |
Tony Mantler | ec86c42 | 2014-09-24 14:33:20 -0700 | [diff] [blame] | 400 | star.setVisible(showStar && canStar); |
Paul Westbrook | 334e64a | 2012-02-23 13:26:35 -0800 | [diff] [blame] | 401 | final MenuItem unstar = menu.findItem(R.id.remove_star); |
Tony Mantler | ec86c42 | 2014-09-24 14:33:20 -0700 | [diff] [blame] | 402 | unstar.setVisible(!showStar && canStar); |
Paul Westbrook | 334e64a | 2012-02-23 13:26:35 -0800 | [diff] [blame] | 403 | final MenuItem read = menu.findItem(R.id.read); |
Mindy Pereira | febeb5f | 2012-02-14 15:08:13 -0800 | [diff] [blame] | 404 | read.setVisible(!showMarkUnread); |
Paul Westbrook | 334e64a | 2012-02-23 13:26:35 -0800 | [diff] [blame] | 405 | final MenuItem unread = menu.findItem(R.id.unread); |
Mindy Pereira | febeb5f | 2012-02-14 15:08:13 -0800 | [diff] [blame] | 406 | unread.setVisible(showMarkUnread); |
Jin Cao | a6b4521 | 2014-09-18 13:46:54 -0700 | [diff] [blame] | 407 | |
Mindy Pereira | 9ba4251 | 2012-08-14 11:52:48 -0700 | [diff] [blame] | 408 | // We only ever show one of: |
| 409 | // 1) remove folder |
| 410 | // 2) archive |
Mindy Pereira | 01f3050 | 2012-08-14 10:30:51 -0700 | [diff] [blame] | 411 | final MenuItem removeFolder = menu.findItem(R.id.remove_folder); |
Rohan Shah | aab9bc7 | 2013-02-07 14:18:05 -0800 | [diff] [blame] | 412 | final MenuItem moveTo = menu.findItem(R.id.move_to); |
Scott Kennedy | dd2ec68 | 2013-06-03 19:16:13 -0700 | [diff] [blame] | 413 | final MenuItem moveToInbox = menu.findItem(R.id.move_to_inbox); |
Scott Kennedy | 8c1058e | 2013-03-20 13:40:20 -0700 | [diff] [blame] | 414 | final boolean showRemoveFolder = mFolder != null && mFolder.isType(FolderType.DEFAULT) |
Mindy Pereira | 9ba4251 | 2012-08-14 11:52:48 -0700 | [diff] [blame] | 415 | && mFolder.supportsCapability(FolderCapabilities.CAN_ACCEPT_MOVED_MESSAGES) |
Tony Mantler | 53b3425 | 2013-09-25 16:42:25 -0700 | [diff] [blame] | 416 | && !mFolder.isProviderFolder() |
| 417 | && mAccount.supportsCapability(AccountCapabilities.ARCHIVE); |
Rohan Shah | aab9bc7 | 2013-02-07 14:18:05 -0800 | [diff] [blame] | 418 | final boolean showMoveTo = mFolder != null |
| 419 | && mFolder.supportsCapability(FolderCapabilities.ALLOWS_REMOVE_CONVERSATION); |
Scott Kennedy | dd2ec68 | 2013-06-03 19:16:13 -0700 | [diff] [blame] | 420 | final boolean showMoveToInbox = mFolder != null |
| 421 | && mFolder.supportsCapability(FolderCapabilities.ALLOWS_MOVE_TO_INBOX); |
Mindy Pereira | 9ba4251 | 2012-08-14 11:52:48 -0700 | [diff] [blame] | 422 | removeFolder.setVisible(showRemoveFolder); |
Scott Kennedy | 28f3d5c | 2013-04-08 16:53:50 -0700 | [diff] [blame] | 423 | moveTo.setVisible(showMoveTo); |
Scott Kennedy | dd2ec68 | 2013-06-03 19:16:13 -0700 | [diff] [blame] | 424 | moveToInbox.setVisible(showMoveToInbox); |
| 425 | |
Rohan Shah | 69419bb | 2014-08-15 15:53:29 -0700 | [diff] [blame] | 426 | final MenuItem changeFolders = menu.findItem(R.id.change_folders); |
| 427 | changeFolders.setVisible(mAccount.supportsCapability( |
| 428 | UIProvider.AccountCapabilities.MULTIPLE_FOLDERS_PER_CONV)); |
| 429 | |
Mindy Pereira | 9ba4251 | 2012-08-14 11:52:48 -0700 | [diff] [blame] | 430 | if (mFolder != null && showRemoveFolder) { |
Mindy Pereira | 01f3050 | 2012-08-14 10:30:51 -0700 | [diff] [blame] | 431 | removeFolder.setTitle(mActivity.getActivityContext().getString(R.string.remove_folder, |
| 432 | mFolder.name)); |
| 433 | } |
Mindy Pereira | 9ba4251 | 2012-08-14 11:52:48 -0700 | [diff] [blame] | 434 | final MenuItem archive = menu.findItem(R.id.archive); |
Andrew Sapperstein | 3047a9f | 2014-07-22 18:17:53 -0700 | [diff] [blame] | 435 | if (archive != null) { |
| 436 | archive.setVisible( |
| 437 | mAccount.supportsCapability(UIProvider.AccountCapabilities.ARCHIVE) && |
| 438 | mFolder.supportsCapability(FolderCapabilities.ARCHIVE)); |
Mindy Pereira | 50cfeda | 2012-08-14 13:54:56 -0700 | [diff] [blame] | 439 | } |
Paul Westbrook | 334e64a | 2012-02-23 13:26:35 -0800 | [diff] [blame] | 440 | final MenuItem spam = menu.findItem(R.id.report_spam); |
Mindy Pereira | 9ba4251 | 2012-08-14 11:52:48 -0700 | [diff] [blame] | 441 | spam.setVisible(!showMarkNotSpam |
| 442 | && mAccount.supportsCapability(UIProvider.AccountCapabilities.REPORT_SPAM) |
| 443 | && mFolder.supportsCapability(FolderCapabilities.REPORT_SPAM)); |
Paul Westbrook | 77eee62 | 2012-07-10 13:41:57 -0700 | [diff] [blame] | 444 | final MenuItem notSpam = menu.findItem(R.id.mark_not_spam); |
| 445 | notSpam.setVisible(showMarkNotSpam && |
| 446 | mAccount.supportsCapability(UIProvider.AccountCapabilities.REPORT_SPAM) && |
| 447 | mFolder.supportsCapability(FolderCapabilities.MARK_NOT_SPAM)); |
Paul Westbrook | 76b2062 | 2012-07-12 11:45:43 -0700 | [diff] [blame] | 448 | final MenuItem phishing = menu.findItem(R.id.report_phishing); |
| 449 | phishing.setVisible(showMarkAsPhishing && |
| 450 | mAccount.supportsCapability(UIProvider.AccountCapabilities.REPORT_PHISHING) && |
| 451 | mFolder.supportsCapability(FolderCapabilities.REPORT_PHISHING)); |
| 452 | |
Paul Westbrook | 334e64a | 2012-02-23 13:26:35 -0800 | [diff] [blame] | 453 | final MenuItem mute = menu.findItem(R.id.mute); |
Mindy Pereira | 50cfeda | 2012-08-14 13:54:56 -0700 | [diff] [blame] | 454 | if (mute != null) { |
| 455 | mute.setVisible(mAccount.supportsCapability(UIProvider.AccountCapabilities.MUTE) |
Scott Kennedy | 962a692 | 2013-03-21 17:43:49 -0700 | [diff] [blame] | 456 | && (mFolder != null && mFolder.isInbox())); |
Mindy Pereira | 50cfeda | 2012-08-14 13:54:56 -0700 | [diff] [blame] | 457 | } |
Mindy Pereira | 7f0a962 | 2012-02-29 15:00:34 -0800 | [diff] [blame] | 458 | final MenuItem markImportant = menu.findItem(R.id.mark_important); |
| 459 | markImportant.setVisible(showMarkImportant |
| 460 | && mAccount.supportsCapability(UIProvider.AccountCapabilities.MARK_IMPORTANT)); |
| 461 | final MenuItem markNotImportant = menu.findItem(R.id.mark_not_important); |
| 462 | markNotImportant.setVisible(!showMarkImportant |
| 463 | && mAccount.supportsCapability(UIProvider.AccountCapabilities.MARK_IMPORTANT)); |
Jin Cao | 512821c | 2014-05-30 15:54:04 -0700 | [diff] [blame] | 464 | |
| 465 | boolean shouldShowDiscardOutbox = mFolder != null && mFolder.isType(FolderType.OUTBOX); |
| 466 | mDiscardOutboxMenuItem = menu.findItem(R.id.discard_outbox); |
| 467 | if (mDiscardOutboxMenuItem != null) { |
| 468 | mDiscardOutboxMenuItem.setVisible(shouldShowDiscardOutbox); |
| 469 | mDiscardOutboxMenuItem.setEnabled(shouldEnableDiscardOutbox(conversations)); |
| 470 | } |
| 471 | final boolean showDelete = mFolder != null && !mFolder.isType(FolderType.OUTBOX) |
Paul Westbrook | ef36254 | 2012-08-27 14:53:32 -0700 | [diff] [blame] | 472 | && mFolder.supportsCapability(UIProvider.FolderCapabilities.DELETE); |
Mindy Pereira | b807337 | 2012-08-09 14:00:10 -0700 | [diff] [blame] | 473 | final MenuItem trash = menu.findItem(R.id.delete); |
Paul Westbrook | ef36254 | 2012-08-27 14:53:32 -0700 | [diff] [blame] | 474 | trash.setVisible(showDelete); |
| 475 | // We only want to show the discard drafts menu item if we are not showing the delete menu |
| 476 | // item, and the current folder is a draft folder and the account supports discarding |
| 477 | // drafts for a conversation |
| 478 | final boolean showDiscardDrafts = !showDelete && mFolder != null && mFolder.isDraft() && |
| 479 | mAccount.supportsCapability(AccountCapabilities.DISCARD_CONVERSATION_DRAFTS); |
| 480 | final MenuItem discardDrafts = menu.findItem(R.id.discard_drafts); |
| 481 | if (discardDrafts != null) { |
| 482 | discardDrafts.setVisible(showDiscardDrafts); |
| 483 | } |
| 484 | |
Mindy Pereira | 11b0bbd | 2012-01-24 10:06:17 -0800 | [diff] [blame] | 485 | return true; |
| 486 | } |
| 487 | |
Jin Cao | 512821c | 2014-05-30 15:54:04 -0700 | [diff] [blame] | 488 | private boolean shouldEnableDiscardOutbox(Collection<Conversation> conversations) { |
| 489 | boolean shouldEnableDiscardOutbox = true; |
| 490 | // Java should be smart enough to realize that once showDiscardOutbox becomes false it can |
| 491 | // just skip everything remaining in the for-loop.. |
| 492 | for (Conversation conv : conversations) { |
| 493 | shouldEnableDiscardOutbox &= |
| 494 | conv.sendingState != UIProvider.ConversationSendingState.SENDING && |
| 495 | conv.sendingState != UIProvider.ConversationSendingState.RETRYING; |
| 496 | } |
| 497 | return shouldEnableDiscardOutbox; |
| 498 | } |
| 499 | |
Mindy Pereira | 11b0bbd | 2012-01-24 10:06:17 -0800 | [diff] [blame] | 500 | @Override |
| 501 | public void onDestroyActionMode(ActionMode mode) { |
| 502 | mActionMode = null; |
| 503 | // The action mode may have been destroyed due to this menu being deactivated, in which |
| 504 | // case resources need not be cleaned up. However, if it was destroyed while this menu is |
| 505 | // active, that implies the user hit "Done" in the top right, and resources need cleaning. |
| 506 | if (mActivated) { |
| 507 | destroy(); |
mindyp | 54f120f | 2012-08-28 13:10:33 -0700 | [diff] [blame] | 508 | // Only commit destructive actions if the user actually pressed |
| 509 | // done; otherwise, this was handled when we toggled conversation |
| 510 | // selection state. |
Scott Kennedy | ff8553f | 2013-04-05 20:57:44 -0700 | [diff] [blame] | 511 | mActivity.getListHandler().commitDestructiveActions(true); |
Mindy Pereira | 11b0bbd | 2012-01-24 10:06:17 -0800 | [diff] [blame] | 512 | } |
Mindy Pereira | 11b0bbd | 2012-01-24 10:06:17 -0800 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | @Override |
Jin Cao | ec0fa48 | 2014-08-28 16:38:08 -0700 | [diff] [blame] | 516 | public void onSetPopulated(ConversationCheckedSet set) { |
Mindy Pereira | 11b0bbd | 2012-01-24 10:06:17 -0800 | [diff] [blame] | 517 | // Noop. This object can only exist while the set is non-empty. |
| 518 | } |
| 519 | |
| 520 | @Override |
Vikram Aggarwal | 1ddcf0f | 2012-01-13 11:45:02 -0800 | [diff] [blame] | 521 | public void onSetEmpty() { |
Vikram Aggarwal | ad6d628 | 2012-03-28 09:16:14 -0700 | [diff] [blame] | 522 | LogUtils.d(LOG_TAG, "onSetEmpty called."); |
Mindy Pereira | 11b0bbd | 2012-01-24 10:06:17 -0800 | [diff] [blame] | 523 | destroy(); |
| 524 | } |
| 525 | |
| 526 | @Override |
Jin Cao | ec0fa48 | 2014-08-28 16:38:08 -0700 | [diff] [blame] | 527 | public void onSetChanged(ConversationCheckedSet set) { |
Mindy Pereira | 11b0bbd | 2012-01-24 10:06:17 -0800 | [diff] [blame] | 528 | // If the set is empty, the menu buttons are invalid and most like the menu will be cleaned |
| 529 | // up. Avoid making any changes to stop flickering ("Add Star" -> "Remove Star") just |
| 530 | // before hiding the menu. |
| 531 | if (set.isEmpty()) { |
| 532 | return; |
| 533 | } |
Andrew Sapperstein | db861e9 | 2014-09-07 17:54:30 -0700 | [diff] [blame] | 534 | updateCount(); |
Jin Cao | 512821c | 2014-05-30 15:54:04 -0700 | [diff] [blame] | 535 | |
| 536 | if (mFolder.isType(FolderType.OUTBOX) && mDiscardOutboxMenuItem != null) { |
| 537 | mDiscardOutboxMenuItem.setEnabled(shouldEnableDiscardOutbox(set.values())); |
| 538 | } |
Mindy Pereira | 11b0bbd | 2012-01-24 10:06:17 -0800 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | /** |
Andrew Sapperstein | db861e9 | 2014-09-07 17:54:30 -0700 | [diff] [blame] | 542 | * Updates the visible count of how many conversations are selected. |
| 543 | */ |
| 544 | private void updateCount() { |
| 545 | if (mActionMode != null) { |
| 546 | mActionMode.setTitle(Integer.toString(mCheckedSet.size())); |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | /** |
Vikram Aggarwal | e128fc2 | 2012-04-04 12:33:34 -0700 | [diff] [blame] | 551 | * Activates and shows this menu (essentially starting an {@link ActionMode}) if the selected |
| 552 | * set is non-empty. |
Mindy Pereira | 11b0bbd | 2012-01-24 10:06:17 -0800 | [diff] [blame] | 553 | */ |
| 554 | public void activate() { |
Jin Cao | ec0fa48 | 2014-08-28 16:38:08 -0700 | [diff] [blame] | 555 | if (mCheckedSet.isEmpty()) { |
Vikram Aggarwal | e128fc2 | 2012-04-04 12:33:34 -0700 | [diff] [blame] | 556 | return; |
| 557 | } |
Andrew Sapperstein | 2f54287 | 2013-06-11 10:48:30 -0700 | [diff] [blame] | 558 | mListController.onCabModeEntered(); |
Mindy Pereira | 11b0bbd | 2012-01-24 10:06:17 -0800 | [diff] [blame] | 559 | mActivated = true; |
| 560 | if (mActionMode == null) { |
Andrew Sapperstein | 52882ff | 2014-07-27 12:30:18 -0700 | [diff] [blame] | 561 | mActivity.startSupportActionMode(this); |
Mindy Pereira | 11b0bbd | 2012-01-24 10:06:17 -0800 | [diff] [blame] | 562 | } |
| 563 | } |
| 564 | |
| 565 | /** |
| 566 | * De-activates and hides the menu (essentially disabling the {@link ActionMode}), but maintains |
| 567 | * the selection conversation set, and internally updates state as necessary. |
| 568 | */ |
| 569 | public void deactivate() { |
Scott Kennedy | cc13983 | 2013-08-19 18:03:54 -0700 | [diff] [blame] | 570 | mListController.onCabModeExited(); |
Andrew Sapperstein | 4c92874 | 2014-08-29 15:34:23 -0700 | [diff] [blame] | 571 | mActivated = false; |
Mindy Pereira | 11b0bbd | 2012-01-24 10:06:17 -0800 | [diff] [blame] | 572 | if (mActionMode != null) { |
Mindy Pereira | 11b0bbd | 2012-01-24 10:06:17 -0800 | [diff] [blame] | 573 | mActionMode.finish(); |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | @VisibleForTesting |
Andrew Sapperstein | 4c92874 | 2014-08-29 15:34:23 -0700 | [diff] [blame] | 578 | /** |
| 579 | * Returns true if CAB mode is active. |
| 580 | */ |
Mindy Pereira | 11b0bbd | 2012-01-24 10:06:17 -0800 | [diff] [blame] | 581 | public boolean isActivated() { |
| 582 | return mActivated; |
| 583 | } |
| 584 | |
| 585 | /** |
| 586 | * Destroys and cleans up the resources associated with this menu. |
| 587 | */ |
Vikram Aggarwal | ad6d628 | 2012-03-28 09:16:14 -0700 | [diff] [blame] | 588 | private void destroy() { |
Mindy Pereira | 11b0bbd | 2012-01-24 10:06:17 -0800 | [diff] [blame] | 589 | deactivate(); |
Jin Cao | ec0fa48 | 2014-08-28 16:38:08 -0700 | [diff] [blame] | 590 | mCheckedSet.removeObserver(this); |
| 591 | clearChecked(); |
Vikram Aggarwal | 4f4782b | 2012-05-30 08:39:09 -0700 | [diff] [blame] | 592 | mUpdater.refreshConversationList(); |
Mindy Pereira | 14f64ec | 2012-08-14 17:14:02 -0700 | [diff] [blame] | 593 | if (mAccountObserver != null) { |
| 594 | mAccountObserver.unregisterAndDestroy(); |
| 595 | mAccountObserver = null; |
| 596 | } |
Mindy Pereira | 11b0bbd | 2012-01-24 10:06:17 -0800 | [diff] [blame] | 597 | } |
Mindy Pereira | 11b0bbd | 2012-01-24 10:06:17 -0800 | [diff] [blame] | 598 | } |