• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.settings.accessibility;
18 
19 import static com.android.settings.accessibility.AccessibilityUtil.State.OFF;
20 import static com.android.settings.accessibility.AccessibilityUtil.State.ON;
21 
22 import android.accessibilityservice.AccessibilityServiceInfo;
23 import android.content.ComponentName;
24 import android.content.ContentResolver;
25 import android.content.Context;
26 import android.provider.Settings;
27 import android.view.accessibility.AccessibilityManager;
28 
29 import com.android.settings.core.TogglePreferenceController;
30 import com.android.settingslib.accessibility.AccessibilityUtils;
31 
32 import java.util.List;
33 import java.util.Set;
34 
35 /**
36  * PreferenceController for accessibility services to be used by Slices.
37  * Wraps the common logic which enables accessibility services and checks their availability.
38  * <p>
39  * Should not be used in a {@link com.android.settings.dashboard.DashboardFragment}.
40  */
41 public class AccessibilitySlicePreferenceController extends TogglePreferenceController {
42 
43     private final ComponentName mComponentName;
44 
45     private static final String EMPTY_STRING = "";
46 
AccessibilitySlicePreferenceController(Context context, String preferenceKey)47     public AccessibilitySlicePreferenceController(Context context, String preferenceKey) {
48         super(context, preferenceKey);
49         mComponentName = ComponentName.unflattenFromString(getPreferenceKey());
50 
51         if (mComponentName == null) {
52             throw new IllegalArgumentException(
53                     "Illegal Component Name from: " + preferenceKey);
54         }
55     }
56 
57     @Override
getSummary()58     public CharSequence getSummary() {
59         final AccessibilityServiceInfo serviceInfo = getAccessibilityServiceInfo();
60 
61         return serviceInfo == null ? EMPTY_STRING : AccessibilitySettings.getServiceSummary(
62                 mContext, serviceInfo, isChecked());
63     }
64 
65     @Override
isChecked()66     public boolean isChecked() {
67         final ContentResolver contentResolver = mContext.getContentResolver();
68         final boolean accessibilityEnabled = Settings.Secure.getInt(contentResolver,
69                 Settings.Secure.ACCESSIBILITY_ENABLED, OFF) == ON;
70 
71         if (!accessibilityEnabled) {
72             return false;
73         }
74 
75         final Set<ComponentName> componentNames =
76                 AccessibilityUtils.getEnabledServicesFromSettings(mContext);
77 
78         return componentNames.contains(mComponentName);
79     }
80 
81     @Override
setChecked(boolean isChecked)82     public boolean setChecked(boolean isChecked) {
83         if (getAccessibilityServiceInfo() == null) {
84             return false;
85         }
86         AccessibilityUtils.setAccessibilityServiceState(mContext, mComponentName, isChecked);
87         return isChecked == isChecked(); // Verify that it was probably changed.
88     }
89 
90     @Override
getAvailabilityStatus()91     public int getAvailabilityStatus() {
92         // Return unsupported when the service is disabled or not installed.
93         return getAccessibilityServiceInfo() == null ? UNSUPPORTED_ON_DEVICE : AVAILABLE;
94     }
95 
96     @Override
isPublicSlice()97     public boolean isPublicSlice() {
98         return true;
99     }
100 
getAccessibilityServiceInfo()101     private AccessibilityServiceInfo getAccessibilityServiceInfo() {
102         final AccessibilityManager accessibilityManager = mContext.getSystemService(
103                 AccessibilityManager.class);
104         final List<AccessibilityServiceInfo> serviceList =
105                 accessibilityManager.getInstalledAccessibilityServiceList();
106 
107         for (AccessibilityServiceInfo serviceInfo : serviceList) {
108             if (mComponentName.equals(serviceInfo.getComponentName())) {
109                 return serviceInfo;
110             }
111         }
112 
113         return null;
114     }
115 }
116