1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.documentsui;
import static com.android.documentsui.base.SharedMinimal.DEBUG;
import static com.android.documentsui.util.FlagUtils.isUseMaterial3FlagEnabled;
import android.app.Activity;
import android.util.Log;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import androidx.annotation.IdRes;
import androidx.annotation.Nullable;
import androidx.recyclerview.selection.MutableSelection;
import androidx.recyclerview.selection.SelectionTracker;
import androidx.recyclerview.selection.SelectionTracker.SelectionObserver;
import com.android.documentsui.MenuManager.SelectionDetails;
import com.android.documentsui.base.EventHandler;
import com.android.documentsui.base.Menus;
import com.android.documentsui.ui.MessageBuilder;
/**
* A controller that listens to selection changes and manages life cycles of action modes.
* TODO(b/379776735): This class (and action mode in general) is no longer in use when the
* use_material3 flag is enabled. Remove the class once the flag is rolled out.
*/
public class ActionModeController extends SelectionObserver<String>
implements ActionMode.Callback, ActionModeAddons {
private static final String TAG = "ActionModeController";
private final Activity mActivity;
private final SelectionTracker<String> mSelectionMgr;
private final NavigationViewManager mNavigator;
private final MenuManager mMenuManager;
private final MessageBuilder mMessages;
private final ContentScope mScope = new ContentScope();
private final MutableSelection<String> mSelected = new MutableSelection<>();
private @Nullable ActionMode mActionMode;
private @Nullable Menu mMenu;
public ActionModeController(
Activity activity,
SelectionTracker<String> selectionMgr,
NavigationViewManager navigator,
MenuManager menuManager,
MessageBuilder messages) {
mActivity = activity;
mSelectionMgr = selectionMgr;
mNavigator = navigator;
mMenuManager = menuManager;
mMessages = messages;
}
@Override
public void onSelectionChanged() {
mSelectionMgr.copySelection(mSelected);
if (mSelected.size() > 0) {
if (mActionMode == null) {
if (DEBUG) {
Log.d(TAG, "Starting action mode.");
}
mActionMode = mActivity.startActionMode(this);
final View closeButton =
mActivity.findViewById(androidx.appcompat.R.id.action_mode_close_button);
if (closeButton != null) {
closeButton.setContentDescription(mActivity.getString(android.R.string.cancel));
}
}
updateActionMenu();
} else {
if (mActionMode != null) {
if (DEBUG) {
Log.d(TAG, "Finishing action mode.");
}
mActionMode.finish();
}
}
if (mActionMode != null) {
assert(!mSelected.isEmpty());
final String title = mMessages.getQuantityString(
R.plurals.elements_selected, mSelected.size());
mActionMode.setTitle(title);
mActivity.getWindow().setTitle(title);
}
}
@Override
public void onSelectionRestored() {
onSelectionChanged();
}
// Called when the user exits the action mode
@Override
public void onDestroyActionMode(ActionMode mode) {
if (mActionMode == null) {
if (DEBUG) {
Log.w(TAG, "Received call to destroy action mode on alien mode object.");
}
}
assert(mActionMode.equals(mode));
if (DEBUG) {
Log.d(TAG, "Handling action mode destroyed.");
}
mActionMode = null;
mMenu = null;
if (mSelected.size() > 0) {
mSelectionMgr.clearSelection();
}
// Reset window title back to activity title, i.e. Root name
mActivity.getWindow().setTitle(mActivity.getTitle());
// Re-enable TalkBack for the toolbars, as they are no longer covered by action mode.
int[] toolbarIds =
isUseMaterial3FlagEnabled()
? new int[] {R.id.toolbar}
: new int[] {R.id.toolbar, R.id.roots_toolbar};
mScope.accessibilityImportanceSetter.setAccessibilityImportance(
View.IMPORTANT_FOR_ACCESSIBILITY_AUTO, toolbarIds);
mNavigator.setActionModeActivated(false);
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
int size = mSelectionMgr.getSelection().size();
mode.getMenuInflater().inflate(R.menu.action_mode_menu, menu);
mode.setTitle(mActivity.getResources().getQuantityString(R.plurals.selected_count, size));
if (size > 0) {
mNavigator.setActionModeActivated(true);
// Hide the toolbars if action mode is enabled, so TalkBack doesn't navigate to
// these controls when using linear navigation.
int[] toolbarIds =
isUseMaterial3FlagEnabled()
? new int[] {R.id.toolbar}
: new int[] {R.id.toolbar, R.id.roots_toolbar};
mScope.accessibilityImportanceSetter.setAccessibilityImportance(
View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS,
toolbarIds);
return true;
}
return false;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
mMenu = menu;
updateActionMenu();
return true;
}
private void updateActionMenu() {
assert(mMenu != null);
mMenuManager.updateActionMenu(mMenu, mScope.selectionDetails);
Menus.disableHiddenItems(mMenu);
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return mScope.menuItemClicker.accept(item);
}
private static void setImportantForAccessibility(
Activity activity, int accessibilityImportance, @IdRes int[] viewIds) {
for (final int id : viewIds) {
final View v = activity.findViewById(id);
if (v != null) {
v.setImportantForAccessibility(accessibilityImportance);
}
}
}
@FunctionalInterface
private interface AccessibilityImportanceSetter {
void setAccessibilityImportance(int accessibilityImportance, @IdRes int... viewIds);
}
@Override
public void finishActionMode() {
if (mActionMode != null) {
mActionMode.finish();
mActionMode = null;
} else {
Log.w(TAG, "Tried to finish a null action mode.");
}
}
public ActionModeController reset(
SelectionDetails selectionDetails, EventHandler<MenuItem> menuItemClicker) {
assert(mActionMode == null);
assert(mMenu == null);
mScope.menuItemClicker = menuItemClicker;
mScope.selectionDetails = selectionDetails;
mScope.accessibilityImportanceSetter =
(int accessibilityImportance, @IdRes int[] viewIds) -> {
setImportantForAccessibility(
mActivity, accessibilityImportance, viewIds);
};
return this;
}
private static final class ContentScope {
private EventHandler<MenuItem> menuItemClicker;
private SelectionDetails selectionDetails;
private AccessibilityImportanceSetter accessibilityImportanceSetter;
}
}
|