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
|
/*
* Copyright (C) 2020 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.systemui.accessibility.floatingmenu;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
import androidx.core.view.ViewCompat;
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.Adapter;
import com.android.internal.accessibility.common.ShortcutConstants.AccessibilityFragmentType;
import com.android.internal.accessibility.dialog.AccessibilityTarget;
import com.android.systemui.R;
import com.android.systemui.accessibility.floatingmenu.AccessibilityTargetAdapter.ViewHolder;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.List;
/**
* An adapter which shows the set of accessibility targets that can be performed.
*/
public class AccessibilityTargetAdapter extends Adapter<ViewHolder> {
private int mIconWidthHeight;
private int mItemPadding;
private final List<AccessibilityTarget> mTargets;
@IntDef({
ItemType.FIRST_ITEM,
ItemType.REGULAR_ITEM,
ItemType.LAST_ITEM
})
@Retention(RetentionPolicy.SOURCE)
@interface ItemType {
int FIRST_ITEM = 0;
int REGULAR_ITEM = 1;
int LAST_ITEM = 2;
}
public AccessibilityTargetAdapter(List<AccessibilityTarget> targets) {
mTargets = targets;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, @ItemType int itemType) {
final View root = LayoutInflater.from(parent.getContext()).inflate(
R.layout.accessibility_floating_menu_item, parent,
/* attachToRoot= */ false);
if (itemType == ItemType.FIRST_ITEM) {
return new TopViewHolder(root);
}
if (itemType == ItemType.LAST_ITEM) {
return new BottomViewHolder(root);
}
return new ViewHolder(root);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
final AccessibilityTarget target = mTargets.get(position);
holder.mIconView.setBackground(target.getIcon());
holder.updateIconWidthHeight(mIconWidthHeight);
holder.updateItemPadding(mItemPadding, getItemCount());
holder.itemView.setOnClickListener((v) -> target.onSelected());
holder.itemView.setStateDescription(target.getStateDescription());
holder.itemView.setContentDescription(target.getLabel());
final String clickHint = target.getFragmentType() == AccessibilityFragmentType.TOGGLE
? holder.itemView.getResources().getString(
R.string.accessibility_floating_button_action_double_tap_to_toggle)
: null;
ViewCompat.replaceAccessibilityAction(holder.itemView,
AccessibilityNodeInfoCompat.AccessibilityActionCompat.ACTION_CLICK,
clickHint, /* command= */ null);
}
@ItemType
@Override
public int getItemViewType(int position) {
if (position == 0) {
return ItemType.FIRST_ITEM;
}
if (position == (getItemCount() - 1)) {
return ItemType.LAST_ITEM;
}
return ItemType.REGULAR_ITEM;
}
@Override
public int getItemCount() {
return mTargets.size();
}
public void setIconWidthHeight(int iconWidthHeight) {
mIconWidthHeight = iconWidthHeight;
}
public void setItemPadding(int itemPadding) {
mItemPadding = itemPadding;
}
static class ViewHolder extends RecyclerView.ViewHolder {
final View mIconView;
ViewHolder(View itemView) {
super(itemView);
mIconView = itemView.findViewById(R.id.icon_view);
}
void updateIconWidthHeight(int newValue) {
final ViewGroup.LayoutParams layoutParams = mIconView.getLayoutParams();
if (layoutParams.width == newValue) {
return;
}
layoutParams.width = newValue;
layoutParams.height = newValue;
mIconView.setLayoutParams(layoutParams);
}
void updateItemPadding(int padding, int size) {
itemView.setPaddingRelative(padding, padding, padding, 0);
}
}
static class TopViewHolder extends ViewHolder {
TopViewHolder(View itemView) {
super(itemView);
}
@Override
void updateItemPadding(int padding, int size) {
final int paddingBottom = size <= 1 ? padding : 0;
itemView.setPaddingRelative(padding, padding, padding, paddingBottom);
}
}
static class BottomViewHolder extends ViewHolder {
BottomViewHolder(View itemView) {
super(itemView);
}
@Override
void updateItemPadding(int padding, int size) {
itemView.setPaddingRelative(padding, padding, padding, padding);
}
}
}
|