1 /*
2  * Copyright (C) 2019 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.settings.accessibility;
18 
19 import android.accessibilityservice.AccessibilityServiceInfo;
20 import android.content.DialogInterface;
21 import android.view.View;
22 
23 import com.android.settings.R;
24 import com.android.settingslib.accessibility.AccessibilityUtils;
25 
26 /**
27  * Fragment that does not have toggle bar to turn on service to use.
28  *
29  * <p>The child {@link ToggleAccessibilityServicePreferenceFragment} shows the actual UI for
30  * providing basic accessibility service setup.
31  *
32  * <p>For accessibility services that target SDK > Q, and
33  * {@link AccessibilityServiceInfo#FLAG_REQUEST_ACCESSIBILITY_BUTTON} is set.
34  */
35 public class InvisibleToggleAccessibilityServicePreferenceFragment extends
36         ToggleAccessibilityServicePreferenceFragment implements ShortcutPreference.OnClickCallback {
37 
38     @Override
onInstallSwitchPreferenceToggleSwitch()39     protected void onInstallSwitchPreferenceToggleSwitch() {
40         super.onInstallSwitchPreferenceToggleSwitch();
41         mToggleServiceDividerSwitchPreference.setVisible(false);
42     }
43 
44     /**
45      * {@inheritDoc}
46      *
47      * Enables accessibility service only when user had allowed permission. Disables
48      * accessibility service when shortcutPreference is unchecked.
49      */
50     @Override
onToggleClicked(ShortcutPreference preference)51     public void onToggleClicked(ShortcutPreference preference) {
52         super.onToggleClicked(preference);
53         boolean enabled = getArguments().getBoolean(AccessibilitySettings.EXTRA_CHECKED)
54                 && preference.isChecked();
55 
56         AccessibilityUtils.setAccessibilityServiceState(getContext(), mComponentName, enabled);
57     }
58 
59     /**
60      * {@inheritDoc}
61      *
62      * Enables accessibility service when user clicks permission allow button.
63      */
64     @Override
onDialogButtonFromShortcutToggleClicked(View view)65     void onDialogButtonFromShortcutToggleClicked(View view) {
66         super.onDialogButtonFromShortcutToggleClicked(view);
67         if (view.getId() == R.id.permission_enable_allow_button) {
68             AccessibilityUtils.setAccessibilityServiceState(getContext(), mComponentName,
69                     true);
70         }
71     }
72 
73     /**
74      * {@inheritDoc}
75      *
76      * Enables accessibility service when shortcutPreference is checked.
77      */
78     @Override
callOnAlertDialogCheckboxClicked(DialogInterface dialog, int which)79     protected void callOnAlertDialogCheckboxClicked(DialogInterface dialog, int which) {
80         super.callOnAlertDialogCheckboxClicked(dialog, which);
81 
82         final boolean enabled = mShortcutPreference.isChecked();
83         AccessibilityUtils.setAccessibilityServiceState(getContext(), mComponentName, enabled);
84     }
85 }
86