2 * Copyright (C) 2017 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com.android.internal.app;
18 import android.accessibilityservice.AccessibilityServiceInfo;
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.app.Activity;
22 import android.content.Context;
23 import android.graphics.drawable.Drawable;
24 import android.os.Bundle;
25 import android.provider.Settings;
26 import android.text.TextUtils;
27 import android.view.LayoutInflater;
28 import android.view.View;
29 import android.view.ViewGroup;
30 import android.view.accessibility.AccessibilityManager;
31 import android.widget.BaseAdapter;
32 import android.widget.GridView;
33 import android.widget.ImageView;
34 import android.widget.TextView;
36 import com.android.internal.R;
38 import java.util.ArrayList;
39 import java.util.Collections;
40 import java.util.List;
43 * Activity used to display and persist a service or feature target for the Accessibility button.
45 public class AccessibilityButtonChooserActivity extends Activity {
47 private static final String MAGNIFICATION_COMPONENT_ID =
48 "com.android.server.accessibility.MagnificationController";
50 private AccessibilityButtonTarget mMagnificationTarget = null;
52 private List<AccessibilityButtonTarget> mTargets = null;
55 protected void onCreate(@Nullable Bundle savedInstanceState) {
56 super.onCreate(savedInstanceState);
57 setContentView(R.layout.accessibility_button_chooser);
59 String component = Settings.Secure.getString(getContentResolver(),
60 Settings.Secure.ACCESSIBILITY_BUTTON_TARGET_COMPONENT);
61 if (TextUtils.isEmpty(component)) {
62 TextView prompt = findViewById(R.id.accessibility_button_prompt);
63 prompt.setVisibility(View.VISIBLE);
66 mMagnificationTarget = new AccessibilityButtonTarget(this, MAGNIFICATION_COMPONENT_ID,
67 R.string.accessibility_magnification_chooser_text,
68 R.drawable.resolver_icon_placeholder);
70 mTargets = getServiceAccessibilityButtonTargets(this);
71 if (Settings.Secure.getInt(getContentResolver(),
72 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_NAVBAR_ENABLED, 0) == 1) {
73 mTargets.add(mMagnificationTarget);
76 if (mTargets.size() < 2) {
81 GridView gridview = findViewById(R.id.accessibility_button_chooser_grid);
82 gridview.setAdapter(new TargetAdapter());
83 gridview.setOnItemClickListener((parent, view, position, id) -> {
84 onTargetSelected(mTargets.get(position));
88 private static List<AccessibilityButtonTarget> getServiceAccessibilityButtonTargets(
89 @NonNull Context context) {
90 AccessibilityManager ams = (AccessibilityManager) context.getSystemService(
91 Context.ACCESSIBILITY_SERVICE);
92 List<AccessibilityServiceInfo> services = ams.getEnabledAccessibilityServiceList(
93 AccessibilityServiceInfo.FEEDBACK_ALL_MASK);
94 if (services == null) {
95 return Collections.emptyList();
98 ArrayList<AccessibilityButtonTarget> targets = new ArrayList<>(services.size());
99 for (AccessibilityServiceInfo info : services) {
100 if ((info.flags & AccessibilityServiceInfo.FLAG_REQUEST_ACCESSIBILITY_BUTTON) != 0) {
101 targets.add(new AccessibilityButtonTarget(context, info));
108 private void onTargetSelected(AccessibilityButtonTarget target) {
109 Settings.Secure.putString(getContentResolver(),
110 Settings.Secure.ACCESSIBILITY_BUTTON_TARGET_COMPONENT, target.getId());
114 private class TargetAdapter extends BaseAdapter {
116 public int getCount() {
117 return mTargets.size();
121 public Object getItem(int position) {
126 public long getItemId(int position) {
131 public View getView(int position, View convertView, ViewGroup parent) {
132 LayoutInflater inflater = AccessibilityButtonChooserActivity.this.getLayoutInflater();
133 View root = inflater.inflate(R.layout.accessibility_button_chooser_item, parent, false);
134 final AccessibilityButtonTarget target = mTargets.get(position);
135 ImageView iconView = root.findViewById(R.id.accessibility_button_target_icon);
136 TextView labelView = root.findViewById(R.id.accessibility_button_target_label);
137 iconView.setImageDrawable(target.getDrawable());
138 labelView.setText(target.getLabel());
143 private static class AccessibilityButtonTarget {
145 public CharSequence mLabel;
146 public Drawable mDrawable;
148 public AccessibilityButtonTarget(@NonNull Context context,
149 @NonNull AccessibilityServiceInfo serviceInfo) {
150 this.mId = serviceInfo.getComponentName().flattenToString();
151 this.mLabel = serviceInfo.getResolveInfo().loadLabel(context.getPackageManager());
152 this.mDrawable = serviceInfo.getResolveInfo().loadIcon(context.getPackageManager());
155 public AccessibilityButtonTarget(Context context, @NonNull String id, int labelResId,
158 this.mLabel = context.getText(labelResId);
159 this.mDrawable = context.getDrawable(iconRes);
162 public String getId() {
166 public CharSequence getLabel() {
170 public Drawable getDrawable() {