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