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.Context; 22 import android.content.pm.PackageManager; 23 import android.os.Bundle; 24 import android.os.UserHandle; 25 import android.provider.Settings; 26 import android.support.annotation.Keep; 27 import android.support.v17.preference.LeanbackPreferenceFragment; 28 import android.support.v7.preference.Preference; 29 import android.support.v7.preference.TwoStatePreference; 30 import android.text.TextUtils; 31 import android.view.accessibility.AccessibilityManager; 32 33 import com.android.settingslib.accessibility.AccessibilityUtils; 34 import com.android.tv.settings.R; 35 36 import java.util.List; 37 38 /** 39 * Fragment for configuring the accessibility shortcut 40 */ 41 @Keep 42 public class AccessibilityShortcutFragment extends LeanbackPreferenceFragment { 43 private static final String KEY_ENABLE = "enable"; 44 private static final String KEY_SERVICE = "service"; 45 46 @Override 47 public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { 48 setPreferencesFromResource(R.xml.accessibility_shortcut, null); 49 50 final TwoStatePreference enablePref = (TwoStatePreference) findPreference(KEY_ENABLE); 51 enablePref.setOnPreferenceChangeListener((preference, newValue) -> { 52 setAccessibilityShortcutEnabled((Boolean) newValue); 53 return true; 54 }); 55 56 boolean shortcutEnabled = Settings.Secure.getInt(getContext().getContentResolver(), 57 Settings.Secure.ACCESSIBILITY_SHORTCUT_ENABLED, 1) == 1; 58 59 enablePref.setChecked(shortcutEnabled); 60 } 61 62 @Override 63 public void onResume() { 64 super.onResume(); 65 final Preference servicePref = findPreference(KEY_SERVICE); 66 final List<AccessibilityServiceInfo> installedServices = getContext() 67 .getSystemService(AccessibilityManager.class) 68 .getInstalledAccessibilityServiceList(); 69 final PackageManager packageManager = getContext().getPackageManager(); 70 final String currentService = getCurrentService(getContext()); 71 for (AccessibilityServiceInfo service : installedServices) { 72 final String serviceString = service.getComponentName().flattenToString(); 73 if (TextUtils.equals(currentService, serviceString)) { 74 servicePref.setSummary(service.getResolveInfo().loadLabel(packageManager)); 75 } 76 } 77 } 78 79 private void setAccessibilityShortcutEnabled(boolean enabled) { 80 Settings.Secure.putInt(getContext().getContentResolver(), 81 Settings.Secure.ACCESSIBILITY_SHORTCUT_ENABLED, enabled ? 1 : 0); 82 final Preference servicePref = findPreference(KEY_SERVICE); 83 servicePref.setEnabled(enabled); 84 } 85 86 static String getCurrentService(Context context) { 87 String shortcutServiceString = AccessibilityUtils 88 .getShortcutTargetServiceComponentNameString(context, UserHandle.myUserId()); 89 if (shortcutServiceString != null) { 90 ComponentName shortcutName = ComponentName.unflattenFromString(shortcutServiceString); 91 if (shortcutName != null) { 92 return shortcutName.flattenToString(); 93 } 94 } 95 return null; 96 } 97 } 98