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