1 /*
2  * Copyright (C) 2020 The Android Open Source Project
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 
17 package com.android.internal.accessibility.dialog;
18 
19 import android.annotation.NonNull;
20 import android.content.Context;
21 import android.view.LayoutInflater;
22 import android.view.View;
23 import android.view.ViewGroup;
24 
25 import com.android.internal.R;
26 import com.android.internal.accessibility.common.ShortcutConstants.ShortcutMenuMode;
27 
28 import java.util.List;
29 
30 /**
31  * Extension for {@link TargetAdapter} and used for AccessibilityShortcutChooserActivity.
32  */
33 class ShortcutTargetAdapter extends TargetAdapter {
34     @ShortcutMenuMode
35     private int mShortcutMenuMode = ShortcutMenuMode.LAUNCH;
36     private final List<AccessibilityTarget> mTargets;
37 
ShortcutTargetAdapter(@onNull List<AccessibilityTarget> targets)38     ShortcutTargetAdapter(@NonNull List<AccessibilityTarget> targets) {
39         mTargets = targets;
40     }
41 
42     @Override
getCount()43     public int getCount() {
44         return mTargets.size();
45     }
46 
47     @Override
getItem(int position)48     public Object getItem(int position) {
49         return mTargets.get(position);
50     }
51 
52     @Override
getItemId(int position)53     public long getItemId(int position) {
54         return position;
55     }
56 
57     @Override
getView(int position, View convertView, ViewGroup parent)58     public View getView(int position, View convertView, ViewGroup parent) {
59         final Context context = parent.getContext();
60         ViewHolder holder;
61         if (convertView == null) {
62             convertView = LayoutInflater.from(context).inflate(
63                     R.layout.accessibility_shortcut_chooser_item, parent, /* attachToRoot= */
64                     false);
65             holder = new ViewHolder();
66             holder.mCheckBoxView = convertView.findViewById(
67                     R.id.accessibility_shortcut_target_checkbox);
68             holder.mIconView = convertView.findViewById(R.id.accessibility_shortcut_target_icon);
69             holder.mLabelView = convertView.findViewById(
70                     R.id.accessibility_shortcut_target_label);
71             holder.mStatusView = convertView.findViewById(
72                     R.id.accessibility_shortcut_target_status);
73             convertView.setTag(holder);
74         } else {
75             holder = (ViewHolder) convertView.getTag();
76         }
77 
78         final AccessibilityTarget target = mTargets.get(position);
79         target.updateActionItem(holder, mShortcutMenuMode);
80 
81         return convertView;
82     }
83 
setShortcutMenuMode(@hortcutMenuMode int shortcutMenuMode)84     void setShortcutMenuMode(@ShortcutMenuMode int shortcutMenuMode) {
85         mShortcutMenuMode = shortcutMenuMode;
86     }
87 
88     @ShortcutMenuMode
getShortcutMenuMode()89     int getShortcutMenuMode() {
90         return mShortcutMenuMode;
91     }
92 }
93