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