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 static com.android.settings.accessibility.AccessibilityUtil.UserShortcutType;
20 
21 import android.accessibilityservice.AccessibilityServiceInfo;
22 import android.os.Bundle;
23 import android.view.View;
24 
25 import com.android.settings.R;
26 
27 import com.google.common.collect.ImmutableSet;
28 
29 /**
30  * Fragment that only allowed hardware {@link UserShortcutType} for shortcut to open.
31  *
32  * <p>The child {@link ToggleAccessibilityServicePreferenceFragment} shows the actual UI for
33  * providing basic accessibility service setup.
34  *
35  * <p>For accessibility services that target SDK <= Q.
36  */
37 public class VolumeShortcutToggleAccessibilityServicePreferenceFragment extends
38         ToggleAccessibilityServicePreferenceFragment {
39 
40     @Override
onViewCreated(View view, Bundle savedInstanceState)41     public void onViewCreated(View view, Bundle savedInstanceState) {
42         super.onViewCreated(view, savedInstanceState);
43 
44         final CharSequence hardwareTitle = getPrefContext().getText(
45                 R.string.accessibility_shortcut_edit_dialog_title_hardware);
46         mShortcutPreference.setSummary(hardwareTitle);
47         mShortcutPreference.setSettingsEditable(false);
48 
49         setAllowedPreferredShortcutType(UserShortcutType.HARDWARE);
50     }
51 
52     @Override
getUserShortcutTypes()53     int getUserShortcutTypes() {
54         int shortcutTypes = super.getUserShortcutTypes();
55         final boolean isServiceOn =
56                 getArguments().getBoolean(AccessibilitySettings.EXTRA_CHECKED);
57         final AccessibilityServiceInfo info = getAccessibilityServiceInfo();
58         final boolean hasRequestAccessibilityButtonFlag =
59                 (info.flags & AccessibilityServiceInfo.FLAG_REQUEST_ACCESSIBILITY_BUTTON) != 0;
60         if (hasRequestAccessibilityButtonFlag && isServiceOn) {
61             shortcutTypes |= UserShortcutType.SOFTWARE;
62         } else {
63             shortcutTypes &= (~UserShortcutType.SOFTWARE);
64         }
65 
66         return shortcutTypes;
67     }
68 
setAllowedPreferredShortcutType(int type)69     private void setAllowedPreferredShortcutType(int type) {
70         final AccessibilityUserShortcutType shortcut = new AccessibilityUserShortcutType(
71                 mComponentName.flattenToString(), type);
72 
73         SharedPreferenceUtils.setUserShortcutType(getPrefContext(),
74                 ImmutableSet.of(shortcut.flattenToString()));
75     }
76 }
77