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.content.ComponentName;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.os.Bundle;
23 import android.text.TextUtils;
24 
25 import androidx.annotation.NonNull;
26 import androidx.preference.Preference;
27 import androidx.preference.PreferenceScreen;
28 import androidx.preference.SwitchPreference;
29 import androidx.preference.TwoStatePreference;
30 
31 import com.android.internal.logging.nano.MetricsProto;
32 import com.android.settingslib.accessibility.AccessibilityUtils;
33 import com.android.tv.settings.R;
34 import com.android.tv.settings.SettingsPreferenceFragment;
35 
36 import java.util.Set;
37 
38 /**
39  * Fragment for controlling accessibility service
40  */
41 public class AccessibilityServiceFragment extends SettingsPreferenceFragment implements
42         AccessibilityServiceConfirmationFragment.OnAccessibilityServiceConfirmedListener {
43     private static final String ARG_PACKAGE_NAME = "packageName";
44     private static final String ARG_SERVICE_NAME = "serviceName";
45     private static final String ARG_SETTINGS_ACTIVITY_NAME = "settingsActivityName";
46     private static final String ARG_LABEL = "label";
47 
48     private TwoStatePreference mEnablePref;
49 
50     /**
51      * Put args in bundle
52      * @param args Bundle to prepare
53      * @param packageName Package of accessibility service
54      * @param serviceName Class of accessibility service
55      * @param activityName Class of accessibility service settings activity
56      * @param label Screen title
57      */
prepareArgs(@onNull Bundle args, String packageName, String serviceName, String activityName, String label)58     public static void prepareArgs(@NonNull Bundle args, String packageName, String serviceName,
59             String activityName, String label) {
60         args.putString(ARG_PACKAGE_NAME, packageName);
61         args.putString(ARG_SERVICE_NAME, serviceName);
62         args.putString(ARG_SETTINGS_ACTIVITY_NAME, activityName);
63         args.putString(ARG_LABEL, label);
64     }
65 
66     @Override
onCreatePreferences(Bundle savedInstanceState, String rootKey)67     public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
68         final Context themedContext = getPreferenceManager().getContext();
69         final PreferenceScreen screen =
70                 getPreferenceManager().createPreferenceScreen(themedContext);
71         screen.setTitle(getArguments().getString(ARG_LABEL));
72 
73         mEnablePref = new SwitchPreference(themedContext);
74         mEnablePref.setTitle(R.string.system_accessibility_status);
75         mEnablePref.setFragment(AccessibilityServiceConfirmationFragment.class.getName());
76         screen.addPreference(mEnablePref);
77 
78         final Preference settingsPref = new Preference(themedContext);
79         settingsPref.setTitle(R.string.system_accessibility_config);
80         final String activityName = getArguments().getString(ARG_SETTINGS_ACTIVITY_NAME);
81         if (!TextUtils.isEmpty(activityName)) {
82             final String packageName = getArguments().getString(ARG_PACKAGE_NAME);
83             settingsPref.setIntent(new Intent(Intent.ACTION_MAIN)
84                     .setComponent(new ComponentName(packageName, activityName)));
85         } else {
86             settingsPref.setEnabled(false);
87         }
88         screen.addPreference(settingsPref);
89 
90         setPreferenceScreen(screen);
91     }
92 
93     @Override
onPreferenceTreeClick(Preference preference)94     public boolean onPreferenceTreeClick(Preference preference) {
95         if (preference == mEnablePref) {
96             // Prepare confirmation dialog and reverts switch until result comes back.
97             updateEnablePref();
98             // Pass to super to launch confirmation dialog.
99             super.onPreferenceTreeClick(preference);
100             return true;
101         } else {
102             return super.onPreferenceTreeClick(preference);
103         }
104     }
105 
updateEnablePref()106     private void updateEnablePref() {
107         final String packageName = getArguments().getString(ARG_PACKAGE_NAME);
108         final String serviceName = getArguments().getString(ARG_SERVICE_NAME);
109         final ComponentName serviceComponent = new ComponentName(packageName, serviceName);
110         final Set<ComponentName> enabledServices =
111                 AccessibilityUtils.getEnabledServicesFromSettings(getActivity());
112         final boolean enabled = enabledServices.contains(serviceComponent);
113         mEnablePref.setChecked(enabled);
114         AccessibilityServiceConfirmationFragment.prepareArgs(mEnablePref.getExtras(),
115                 new ComponentName(packageName, serviceName),
116                 getArguments().getString(ARG_LABEL), !enabled);
117     }
118 
119     @Override
onResume()120     public void onResume() {
121         super.onResume();
122         updateEnablePref();
123     }
124 
125     @Override
onAccessibilityServiceConfirmed(ComponentName componentName, boolean enabling)126     public void onAccessibilityServiceConfirmed(ComponentName componentName, boolean enabling) {
127         AccessibilityUtils.setAccessibilityServiceState(getActivity(),
128                 componentName, enabling);
129         if (mEnablePref != null) {
130             mEnablePref.setChecked(enabling);
131         }
132     }
133 
134     @Override
getMetricsCategory()135     public int getMetricsCategory() {
136         return MetricsProto.MetricsEvent.ACCESSIBILITY_SERVICE;
137     }
138 }
139