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
|
/*
* 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.settings.localepicker;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.android.settings.R;
/**
* Add accessibility actions to the drag-and-drop locale list
*
* <p>Dragging is not supported neither by TalkBack or the accessibility
* framework at the moment. So we need custom actions to be able
* to change the order of the locales.</p>
*
* <p>Also, the remove functionality is difficult to discover and use
* with TalkBack only, so we are also adding a "remove" action.</p>
*
* <p>It only removes one locale at the time, but most users don't
* really add many locales "by mistake", so there is no real need
* to delete a lot of locales at once.</p>
*/
public class LocaleLinearLayoutManager extends LinearLayoutManager {
private final LocaleDragAndDropAdapter mAdapter;
private final Context mContext;
private final AccessibilityNodeInfoCompat.AccessibilityActionCompat mActionMoveUp;
private final AccessibilityNodeInfoCompat.AccessibilityActionCompat mActionMoveDown;
private final AccessibilityNodeInfoCompat.AccessibilityActionCompat mActionMoveTop;
private final AccessibilityNodeInfoCompat.AccessibilityActionCompat mActionMoveBottom;
private final AccessibilityNodeInfoCompat.AccessibilityActionCompat mActionRemove;
public LocaleLinearLayoutManager(Context context, LocaleDragAndDropAdapter adapter) {
super(context);
this.mContext = context;
this.mAdapter = adapter;
this.mActionMoveUp = new AccessibilityNodeInfoCompat.AccessibilityActionCompat(
R.id.action_drag_move_up,
mContext.getString(R.string.action_drag_label_move_up));
this.mActionMoveDown = new AccessibilityNodeInfoCompat.AccessibilityActionCompat(
R.id.action_drag_move_down,
mContext.getString(R.string.action_drag_label_move_down));
this.mActionMoveTop = new AccessibilityNodeInfoCompat.AccessibilityActionCompat(
R.id.action_drag_move_top,
mContext.getString(R.string.action_drag_label_move_top));
this.mActionMoveBottom = new AccessibilityNodeInfoCompat.AccessibilityActionCompat(
R.id.action_drag_move_bottom,
mContext.getString(R.string.action_drag_label_move_bottom));
this.mActionRemove = new AccessibilityNodeInfoCompat.AccessibilityActionCompat(
R.id.action_drag_remove,
mContext.getString(R.string.action_drag_label_remove));
}
@Override
public void onInitializeAccessibilityNodeInfoForItem(RecyclerView.Recycler recycler,
RecyclerView.State state, View host, AccessibilityNodeInfoCompat info) {
super.onInitializeAccessibilityNodeInfoForItem(recycler, state, host, info);
final int itemCount = this.getItemCount();
final int position = this.getPosition(host);
final LocaleDragCell dragCell = (LocaleDragCell) host;
// We want the description to be something not localizable, so that any TTS engine for
// any language can handle it. And we want the position to be part of it.
// So we use something like "2, French (France)"
final String description =
(position + 1) + ", " + dragCell.getCheckbox().getContentDescription();
info.setContentDescription(description);
if (mAdapter.isRemoveMode()) { // We don't move things around in remove mode
return;
}
// The order in which we add the actions is important for the circular selection menu.
// With the current order the "up" action will be (more or less) up, and "down" more
// or less down ("more or less" because we have 5 actions)
if (position > 0) { // it is not the first one
info.addAction(mActionMoveUp);
info.addAction(mActionMoveTop);
}
if (position + 1 < itemCount) { // it is not the last one
info.addAction(mActionMoveDown);
info.addAction(mActionMoveBottom);
}
if (itemCount > 1) {
info.addAction(mActionRemove);
}
}
@Override
public boolean performAccessibilityActionForItem(RecyclerView.Recycler recycler,
RecyclerView.State state, View host, int action, Bundle args) {
final int itemCount = this.getItemCount();
final int position = this.getPosition(host);
boolean result = false;
if (action == R.id.action_drag_move_up) {
if (position > 0) {
mAdapter.onItemMove(position, position - 1);
result = true;
}
} else if (action == R.id.action_drag_move_down) {
if (position + 1 < itemCount) {
mAdapter.onItemMove(position, position + 1);
result = true;
}
} else if (action == R.id.action_drag_move_top) {
if (position != 0) {
mAdapter.onItemMove(position, 0);
result = true;
}
} else if (action == R.id.action_drag_move_bottom) {
if (position != itemCount - 1) {
mAdapter.onItemMove(position, itemCount - 1);
result = true;
}
} else if (action == R.id.action_drag_remove) {
if (itemCount > 1) {
mAdapter.removeItem(position);
result = true;
}
} else {
return super.performAccessibilityActionForItem(recycler, state, host, action, args);
}
if (result) {
mAdapter.doTheUpdate();
}
return result;
}
}
|