• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static android.view.accessibility.AccessibilityManager.ACCESSIBILITY_BUTTON;
20 import static android.view.accessibility.AccessibilityManager.ACCESSIBILITY_SHORTCUT_KEY;
21 
22 import static com.android.internal.accessibility.util.ShortcutUtils.convertToUserType;
23 import static com.android.internal.accessibility.util.ShortcutUtils.optInValueToSettings;
24 import static com.android.internal.accessibility.util.ShortcutUtils.optOutValueFromSettings;
25 
26 import android.annotation.NonNull;
27 import android.annotation.Nullable;
28 import android.content.Context;
29 import android.graphics.drawable.Drawable;
30 import android.view.View;
31 import android.view.accessibility.AccessibilityManager;
32 import android.view.accessibility.AccessibilityManager.ShortcutType;
33 
34 import com.android.internal.accessibility.common.ShortcutConstants;
35 import com.android.internal.accessibility.common.ShortcutConstants.AccessibilityFragmentType;
36 import com.android.internal.accessibility.dialog.TargetAdapter.ViewHolder;
37 import com.android.internal.annotations.VisibleForTesting;
38 
39 /**
40  * Abstract base class for creating various target related to accessibility service,
41  * accessibility activity, and allowlisting feature.
42  */
43 public abstract class AccessibilityTarget implements TargetOperations, OnTargetSelectedListener,
44         OnTargetCheckedChangeListener {
45     private Context mContext;
46     @ShortcutType
47     private int mShortcutType;
48     @AccessibilityFragmentType
49     private int mFragmentType;
50     private boolean mShortcutEnabled;
51     private String mId;
52     private CharSequence mLabel;
53     private Drawable mIcon;
54     private String mKey;
55 
56     @VisibleForTesting
AccessibilityTarget(Context context, @ShortcutType int shortcutType, @AccessibilityFragmentType int fragmentType, boolean isShortcutSwitched, String id, CharSequence label, Drawable icon, String key)57     public AccessibilityTarget(Context context, @ShortcutType int shortcutType,
58             @AccessibilityFragmentType int fragmentType, boolean isShortcutSwitched, String id,
59             CharSequence label, Drawable icon, String key) {
60         mContext = context;
61         mShortcutType = shortcutType;
62         mFragmentType = fragmentType;
63         mShortcutEnabled = isShortcutSwitched;
64         mId = id;
65         mLabel = label;
66         mIcon = icon;
67         mKey = key;
68     }
69 
70     @Override
updateActionItem(@onNull ViewHolder holder, @ShortcutConstants.ShortcutMenuMode int shortcutMenuMode)71     public void updateActionItem(@NonNull ViewHolder holder,
72             @ShortcutConstants.ShortcutMenuMode int shortcutMenuMode) {
73         final boolean isEditMenuMode =
74                 shortcutMenuMode == ShortcutConstants.ShortcutMenuMode.EDIT;
75 
76         holder.mCheckBoxView.setChecked(isEditMenuMode && isShortcutEnabled());
77         holder.mCheckBoxView.setVisibility(isEditMenuMode ? View.VISIBLE : View.GONE);
78         holder.mIconView.setImageDrawable(getIcon());
79         holder.mLabelView.setText(getLabel());
80         holder.mStatusView.setVisibility(View.GONE);
81     }
82 
83     @Override
onSelected()84     public void onSelected() {
85         final AccessibilityManager am =
86                 getContext().getSystemService(AccessibilityManager.class);
87         switch (getShortcutType()) {
88             case ACCESSIBILITY_BUTTON:
89                 am.notifyAccessibilityButtonClicked(getContext().getDisplayId(), getId());
90                 return;
91             case ACCESSIBILITY_SHORTCUT_KEY:
92                 am.performAccessibilityShortcut(getId());
93                 return;
94             default:
95                 throw new IllegalStateException("Unexpected shortcut type");
96         }
97     }
98 
99     @Override
onCheckedChanged(boolean isChecked)100     public void onCheckedChanged(boolean isChecked) {
101         setShortcutEnabled(isChecked);
102         if (isChecked) {
103             optInValueToSettings(getContext(), convertToUserType(getShortcutType()), getId());
104         } else {
105             optOutValueFromSettings(getContext(), convertToUserType(getShortcutType()), getId());
106         }
107     }
108 
109     /**
110      * Gets the state description of this feature target.
111      *
112      * @return the state description
113      */
114     @Nullable
getStateDescription()115     public CharSequence getStateDescription() {
116         return null;
117     }
118 
setShortcutEnabled(boolean enabled)119     public void setShortcutEnabled(boolean enabled) {
120         mShortcutEnabled = enabled;
121     }
122 
getContext()123     public Context getContext() {
124         return mContext;
125     }
126 
getShortcutType()127     public @ShortcutType int getShortcutType() {
128         return mShortcutType;
129     }
130 
getFragmentType()131     public @AccessibilityFragmentType int getFragmentType() {
132         return mFragmentType;
133     }
134 
isShortcutEnabled()135     public boolean isShortcutEnabled() {
136         return mShortcutEnabled;
137     }
138 
getId()139     public String getId() {
140         return mId;
141     }
142 
getLabel()143     public CharSequence getLabel() {
144         return mLabel;
145     }
146 
getIcon()147     public Drawable getIcon() {
148         return mIcon;
149     }
150 
getKey()151     public String getKey() {
152         return mKey;
153     }
154 }
155