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 17 package com.android.tv.settings.accessibility; 18 19 import android.accessibilityservice.AccessibilityServiceInfo; 20 import android.content.ComponentName; 21 import android.content.pm.ServiceInfo; 22 import android.os.Bundle; 23 import android.provider.Settings; 24 import android.text.TextUtils; 25 import android.view.accessibility.AccessibilityManager; 26 27 import androidx.annotation.Keep; 28 import androidx.preference.Preference; 29 import androidx.preference.PreferenceGroup; 30 import androidx.preference.SwitchPreference; 31 import androidx.preference.TwoStatePreference; 32 33 import com.android.internal.logging.nano.MetricsProto; 34 import com.android.settingslib.accessibility.AccessibilityUtils; 35 import com.android.tv.settings.R; 36 import com.android.tv.settings.SettingsPreferenceFragment; 37 38 import java.util.List; 39 import java.util.Set; 40 41 /** 42 * Fragment for Accessibility settings 43 */ 44 @Keep 45 public class AccessibilityFragment extends SettingsPreferenceFragment { 46 private static final String TOGGLE_HIGH_TEXT_CONTRAST_KEY = "toggle_high_text_contrast"; 47 private static final String ACCESSIBILITY_SERVICES_KEY = "system_accessibility_services"; 48 49 private PreferenceGroup mServicesPref; 50 51 /** 52 * Create a new instance of the fragment 53 * @return New fragment instance 54 */ newInstance()55 public static AccessibilityFragment newInstance() { 56 return new AccessibilityFragment(); 57 } 58 59 @Override onResume()60 public void onResume() { 61 super.onResume(); 62 if (mServicesPref != null) { 63 refreshServices(mServicesPref); 64 } 65 } 66 67 @Override onCreatePreferences(Bundle savedInstanceState, String rootKey)68 public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { 69 setPreferencesFromResource(R.xml.accessibility, null); 70 71 final TwoStatePreference highContrastPreference = 72 (TwoStatePreference) findPreference(TOGGLE_HIGH_TEXT_CONTRAST_KEY); 73 highContrastPreference.setChecked(Settings.Secure.getInt(getContext().getContentResolver(), 74 Settings.Secure.ACCESSIBILITY_HIGH_TEXT_CONTRAST_ENABLED, 0) == 1); 75 76 mServicesPref = (PreferenceGroup) findPreference(ACCESSIBILITY_SERVICES_KEY); 77 refreshServices(mServicesPref); 78 } 79 80 @Override onPreferenceTreeClick(Preference preference)81 public boolean onPreferenceTreeClick(Preference preference) { 82 if (TextUtils.equals(preference.getKey(), TOGGLE_HIGH_TEXT_CONTRAST_KEY)) { 83 Settings.Secure.putInt(getActivity().getContentResolver(), 84 Settings.Secure.ACCESSIBILITY_HIGH_TEXT_CONTRAST_ENABLED, 85 (((SwitchPreference) preference).isChecked() ? 1 : 0)); 86 return true; 87 } else { 88 return super.onPreferenceTreeClick(preference); 89 } 90 } 91 refreshServices(PreferenceGroup group)92 private void refreshServices(PreferenceGroup group) { 93 final List<AccessibilityServiceInfo> installedServiceInfos = 94 getActivity().getSystemService(AccessibilityManager.class) 95 .getInstalledAccessibilityServiceList(); 96 final Set<ComponentName> enabledServices = 97 AccessibilityUtils.getEnabledServicesFromSettings(getActivity()); 98 final boolean accessibilityEnabled = Settings.Secure.getInt( 99 getActivity().getContentResolver(), 100 Settings.Secure.ACCESSIBILITY_ENABLED, 0) == 1; 101 102 for (final AccessibilityServiceInfo accInfo : installedServiceInfos) { 103 final ServiceInfo serviceInfo = accInfo.getResolveInfo().serviceInfo; 104 final ComponentName componentName = new ComponentName(serviceInfo.packageName, 105 serviceInfo.name); 106 107 final boolean serviceEnabled = accessibilityEnabled 108 && enabledServices.contains(componentName); 109 110 final String title = accInfo.getResolveInfo() 111 .loadLabel(getActivity().getPackageManager()).toString(); 112 113 final String key = "ServicePref:" + componentName.flattenToString(); 114 Preference servicePref = findPreference(key); 115 if (servicePref == null) { 116 servicePref = new Preference(group.getContext()); 117 servicePref.setKey(key); 118 } 119 servicePref.setTitle(title); 120 servicePref.setSummary(serviceEnabled ? R.string.settings_on : R.string.settings_off); 121 servicePref.setFragment(AccessibilityServiceFragment.class.getName()); 122 AccessibilityServiceFragment.prepareArgs(servicePref.getExtras(), 123 serviceInfo.packageName, 124 serviceInfo.name, 125 accInfo.getSettingsActivityName(), 126 title); 127 group.addPreference(servicePref); 128 } 129 } 130 131 @Override getMetricsCategory()132 public int getMetricsCategory() { 133 return MetricsProto.MetricsEvent.ACCESSIBILITY; 134 } 135 } 136