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.settings.accessibility; 17 18 import android.accessibilityservice.AccessibilityServiceInfo; 19 import android.content.ComponentName; 20 import android.content.ContentResolver; 21 import android.content.Context; 22 import android.os.Bundle; 23 import android.os.UserHandle; 24 import android.provider.Settings; 25 import android.support.v14.preference.SwitchPreference; 26 import android.support.v7.preference.Preference; 27 import android.text.TextUtils; 28 import android.view.accessibility.AccessibilityManager; 29 import android.widget.Switch; 30 31 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 32 import com.android.settings.R; 33 import com.android.settings.search.BaseSearchIndexProvider; 34 import com.android.settings.search.Indexable; 35 import com.android.settingslib.accessibility.AccessibilityUtils; 36 37 /** 38 * Settings page for accessibility shortcut 39 */ 40 public class AccessibilityShortcutPreferenceFragment extends ToggleFeaturePreferenceFragment 41 implements Indexable { 42 43 public static final String SHORTCUT_SERVICE_KEY = "accessibility_shortcut_service"; 44 public static final String ON_LOCK_SCREEN_KEY = "accessibility_shortcut_on_lock_screen"; 45 46 private Preference mServicePreference; 47 private SwitchPreference mOnLockScreenSwitchPreference; 48 49 @Override getMetricsCategory()50 public int getMetricsCategory() { 51 return MetricsEvent.ACCESSIBILITY_TOGGLE_GLOBAL_GESTURE; 52 } 53 54 @Override onCreate(Bundle savedInstanceState)55 public void onCreate(Bundle savedInstanceState) { 56 super.onCreate(savedInstanceState); 57 58 addPreferencesFromResource(R.xml.accessibility_shortcut_settings); 59 mServicePreference = findPreference(SHORTCUT_SERVICE_KEY); 60 mOnLockScreenSwitchPreference = (SwitchPreference) findPreference(ON_LOCK_SCREEN_KEY); 61 mOnLockScreenSwitchPreference.setOnPreferenceChangeListener((Preference p, Object o) -> { 62 Settings.Secure.putInt(getContentResolver(), 63 Settings.Secure.ACCESSIBILITY_SHORTCUT_ON_LOCK_SCREEN, 64 ((Boolean) o) ? 1 : 0); 65 return true; 66 }); 67 mFooterPreferenceMixin.createFooterPreference() 68 .setTitle(R.string.accessibility_shortcut_description); 69 } 70 71 @Override onResume()72 public void onResume() { 73 super.onResume(); 74 updatePreferences(); 75 } 76 77 @Override onInstallSwitchBarToggleSwitch()78 protected void onInstallSwitchBarToggleSwitch() { 79 super.onInstallSwitchBarToggleSwitch(); 80 mSwitchBar.addOnSwitchChangeListener((Switch switchView, boolean enabled) -> { 81 onPreferenceToggled(Settings.Secure.ACCESSIBILITY_SHORTCUT_ENABLED, enabled); 82 }); 83 } 84 85 @Override onPreferenceToggled(String preferenceKey, boolean enabled)86 protected void onPreferenceToggled(String preferenceKey, boolean enabled) { 87 Settings.Secure.putInt(getContentResolver(), preferenceKey, enabled ? 1 : 0); 88 } 89 updatePreferences()90 private void updatePreferences() { 91 ContentResolver cr = getContentResolver(); 92 boolean isEnabled = Settings.Secure 93 .getInt(cr, Settings.Secure.ACCESSIBILITY_SHORTCUT_ENABLED, 1) == 1; 94 mToggleSwitch.setChecked(isEnabled); 95 CharSequence serviceName = getServiceName(getContext()); 96 mServicePreference.setSummary(serviceName); 97 mOnLockScreenSwitchPreference.setChecked(Settings.Secure.getInt( 98 cr, Settings.Secure.ACCESSIBILITY_SHORTCUT_ON_LOCK_SCREEN, 0) == 1); 99 if (TextUtils.equals(serviceName, getString(R.string.accessibility_no_service_selected))) { 100 // If there's no service configured, enabling the shortcut will have no effect 101 // It should already be disabled, but force the switch to off just in case 102 mToggleSwitch.setChecked(false); 103 mToggleSwitch.setEnabled(false); 104 mSwitchBar.setEnabled(false); 105 } else { 106 mToggleSwitch.setEnabled(true); 107 mSwitchBar.setEnabled(true); 108 } 109 } 110 111 /** 112 * Get the user-visible name of the service currently selected for the shortcut. 113 * 114 * @param context The current context 115 * @return The name of the service or a string saying that none is selected. 116 */ getServiceName(Context context)117 public static CharSequence getServiceName(Context context) { 118 ComponentName shortcutServiceName = ComponentName.unflattenFromString( 119 AccessibilityUtils.getShortcutTargetServiceComponentNameString( 120 context, UserHandle.myUserId())); 121 AccessibilityServiceInfo shortcutServiceInfo = AccessibilityManager.getInstance(context) 122 .getInstalledServiceInfoWithComponentName(shortcutServiceName); 123 if (shortcutServiceInfo != null) { 124 return shortcutServiceInfo.getResolveInfo().loadLabel(context.getPackageManager()); 125 } 126 return context.getString(R.string.accessibility_no_service_selected); 127 } 128 129 public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = 130 new BaseSearchIndexProvider() { 131 // This fragment is for details of the shortcut. Only the shortcut itself needs 132 // to be indexed. 133 protected boolean isPageSearchEnabled(Context context) { 134 return false; 135 } 136 }; 137 } 138