1 /*
2  * Copyright (C) 2017 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 package com.android.internal.app;
17 
18 import static android.view.WindowManagerPolicyConstants.NAV_BAR_MODE_GESTURAL;
19 
20 import android.accessibilityservice.AccessibilityServiceInfo;
21 import android.annotation.NonNull;
22 import android.annotation.Nullable;
23 import android.app.Activity;
24 import android.content.Context;
25 import android.graphics.drawable.Drawable;
26 import android.os.Bundle;
27 import android.provider.Settings;
28 import android.text.TextUtils;
29 import android.view.LayoutInflater;
30 import android.view.View;
31 import android.view.ViewGroup;
32 import android.view.accessibility.AccessibilityManager;
33 import android.widget.BaseAdapter;
34 import android.widget.GridView;
35 import android.widget.ImageView;
36 import android.widget.TextView;
37 
38 import com.android.internal.R;
39 import com.android.internal.widget.ResolverDrawerLayout;
40 
41 import java.util.ArrayList;
42 import java.util.Collections;
43 import java.util.List;
44 
45 /**
46  * Activity used to display and persist a service or feature target for the Accessibility button.
47  */
48 public class AccessibilityButtonChooserActivity extends Activity {
49 
50     private static final String MAGNIFICATION_COMPONENT_ID =
51             "com.android.server.accessibility.MagnificationController";
52 
53     private AccessibilityButtonTarget mMagnificationTarget = null;
54 
55     private List<AccessibilityButtonTarget> mTargets = null;
56 
57     @Override
onCreate(@ullable Bundle savedInstanceState)58     protected void onCreate(@Nullable Bundle savedInstanceState) {
59         super.onCreate(savedInstanceState);
60         setContentView(R.layout.accessibility_button_chooser);
61 
62         final ResolverDrawerLayout rdl = findViewById(R.id.contentPanel);
63         if (rdl != null) {
64             rdl.setOnDismissedListener(this::finish);
65         }
66 
67         String component = Settings.Secure.getString(getContentResolver(),
68                 Settings.Secure.ACCESSIBILITY_BUTTON_TARGET_COMPONENT);
69 
70         if (isGestureNavigateEnabled()) {
71             TextView promptPrologue = findViewById(R.id.accessibility_button_prompt_prologue);
72             promptPrologue.setText(isTouchExploreOn()
73                     ? R.string.accessibility_gesture_3finger_prompt_text
74                     : R.string.accessibility_gesture_prompt_text);
75         }
76 
77         if (TextUtils.isEmpty(component)) {
78             TextView prompt = findViewById(R.id.accessibility_button_prompt);
79             if (isGestureNavigateEnabled()) {
80                 prompt.setText(isTouchExploreOn()
81                         ? R.string.accessibility_gesture_3finger_instructional_text
82                         : R.string.accessibility_gesture_instructional_text);
83             }
84             prompt.setVisibility(View.VISIBLE);
85         }
86 
87         mMagnificationTarget = new AccessibilityButtonTarget(this, MAGNIFICATION_COMPONENT_ID,
88                 R.string.accessibility_magnification_chooser_text,
89                 R.drawable.ic_accessibility_magnification);
90 
91         mTargets = getServiceAccessibilityButtonTargets(this);
92         if (Settings.Secure.getInt(getContentResolver(),
93                 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_NAVBAR_ENABLED, 0) == 1) {
94             mTargets.add(mMagnificationTarget);
95         }
96 
97         if (mTargets.size() < 2) {
98             // Why are we here?
99             finish();
100         }
101 
102         GridView gridview = findViewById(R.id.accessibility_button_chooser_grid);
103         gridview.setAdapter(new TargetAdapter());
104         gridview.setOnItemClickListener((parent, view, position, id) -> {
105             onTargetSelected(mTargets.get(position));
106         });
107     }
108 
isGestureNavigateEnabled()109     private boolean isGestureNavigateEnabled() {
110         return NAV_BAR_MODE_GESTURAL == getResources().getInteger(
111                 com.android.internal.R.integer.config_navBarInteractionMode);
112     }
113 
isTouchExploreOn()114     private boolean isTouchExploreOn() {
115         return ((AccessibilityManager) getSystemService(Context.ACCESSIBILITY_SERVICE))
116                 .isTouchExplorationEnabled();
117     }
118 
getServiceAccessibilityButtonTargets( @onNull Context context)119     private static List<AccessibilityButtonTarget> getServiceAccessibilityButtonTargets(
120             @NonNull Context context) {
121         AccessibilityManager ams = (AccessibilityManager) context.getSystemService(
122                 Context.ACCESSIBILITY_SERVICE);
123         List<AccessibilityServiceInfo> services = ams.getEnabledAccessibilityServiceList(
124                 AccessibilityServiceInfo.FEEDBACK_ALL_MASK);
125         if (services == null) {
126             return Collections.emptyList();
127         }
128 
129         ArrayList<AccessibilityButtonTarget> targets = new ArrayList<>(services.size());
130         for (AccessibilityServiceInfo info : services) {
131             if ((info.flags & AccessibilityServiceInfo.FLAG_REQUEST_ACCESSIBILITY_BUTTON) != 0) {
132                 targets.add(new AccessibilityButtonTarget(context, info));
133             }
134         }
135 
136         return targets;
137     }
138 
onTargetSelected(AccessibilityButtonTarget target)139     private void onTargetSelected(AccessibilityButtonTarget target) {
140         Settings.Secure.putString(getContentResolver(),
141                 Settings.Secure.ACCESSIBILITY_BUTTON_TARGET_COMPONENT, target.getId());
142         finish();
143     }
144 
145     private class TargetAdapter extends BaseAdapter {
146         @Override
getCount()147         public int getCount() {
148             return mTargets.size();
149         }
150 
151         @Override
getItem(int position)152         public Object getItem(int position) {
153             return null;
154         }
155 
156         @Override
getItemId(int position)157         public long getItemId(int position) {
158             return position;
159         }
160 
161         @Override
getView(int position, View convertView, ViewGroup parent)162         public View getView(int position, View convertView, ViewGroup parent) {
163             LayoutInflater inflater = AccessibilityButtonChooserActivity.this.getLayoutInflater();
164             View root = inflater.inflate(R.layout.accessibility_button_chooser_item, parent, false);
165             final AccessibilityButtonTarget target = mTargets.get(position);
166             ImageView iconView = root.findViewById(R.id.accessibility_button_target_icon);
167             TextView labelView = root.findViewById(R.id.accessibility_button_target_label);
168             iconView.setImageDrawable(target.getDrawable());
169             labelView.setText(target.getLabel());
170             return root;
171         }
172     }
173 
174     private static class AccessibilityButtonTarget {
175         public String mId;
176         public CharSequence mLabel;
177         public Drawable mDrawable;
178 
AccessibilityButtonTarget(@onNull Context context, @NonNull AccessibilityServiceInfo serviceInfo)179         public AccessibilityButtonTarget(@NonNull Context context,
180                 @NonNull AccessibilityServiceInfo serviceInfo) {
181             this.mId = serviceInfo.getComponentName().flattenToString();
182             this.mLabel = serviceInfo.getResolveInfo().loadLabel(context.getPackageManager());
183             this.mDrawable = serviceInfo.getResolveInfo().loadIcon(context.getPackageManager());
184         }
185 
AccessibilityButtonTarget(Context context, @NonNull String id, int labelResId, int iconRes)186         public AccessibilityButtonTarget(Context context, @NonNull String id, int labelResId,
187                 int iconRes) {
188             this.mId = id;
189             this.mLabel = context.getText(labelResId);
190             this.mDrawable = context.getDrawable(iconRes);
191         }
192 
getId()193         public String getId() {
194             return mId;
195         }
196 
getLabel()197         public CharSequence getLabel() {
198             return mLabel;
199         }
200 
getDrawable()201         public Drawable getDrawable() {
202             return mDrawable;
203         }
204     }
205 }
206