1 /*
2  * Copyright (C) 2023 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.shortcuts;
18 
19 import static com.android.internal.accessibility.AccessibilityShortcutController.MAGNIFICATION_CONTROLLER_NAME;
20 
21 import android.content.Context;
22 import android.provider.Settings;
23 import android.view.accessibility.Flags;
24 
25 import androidx.preference.Preference;
26 import androidx.preference.PreferenceScreen;
27 
28 import com.android.internal.accessibility.common.ShortcutConstants;
29 import com.android.settings.R;
30 import com.android.settings.accessibility.AccessibilityUtil;
31 import com.android.settings.accessibility.ExpandablePreference;
32 
33 import java.util.Set;
34 
35 /**
36  * A controller handles displaying the triple tap shortcut option preference and
37  * configuring the shortcut.
38  */
39 public class TripleTapShortcutOptionController extends ShortcutOptionPreferenceController
40         implements ExpandablePreference {
41 
42     private boolean mIsExpanded = false;
43 
TripleTapShortcutOptionController(Context context, String preferenceKey)44     public TripleTapShortcutOptionController(Context context, String preferenceKey) {
45         super(context, preferenceKey);
46     }
47 
48     @Override
displayPreference(PreferenceScreen screen)49     public void displayPreference(PreferenceScreen screen) {
50         super.displayPreference(screen);
51         final Preference preference = screen.findPreference(getPreferenceKey());
52         if (preference instanceof ShortcutOptionPreference shortcutOptionPreference) {
53             shortcutOptionPreference.setTitle(
54                     R.string.accessibility_shortcut_edit_screen_title_triple_tap);
55             String summary = mContext.getString(
56                     R.string.accessibility_shortcut_edit_screen_summary_triple_tap, 3);
57 
58             shortcutOptionPreference.setSummary(summary);
59             shortcutOptionPreference.setIntroImageRawResId(
60                     R.raw.accessibility_shortcut_type_tripletap);
61         }
62     }
63 
64     @Override
getAvailabilityStatus()65     public int getAvailabilityStatus() {
66         if (isExpanded() && isShortcutAvailable()) {
67             return AVAILABLE_UNSEARCHABLE;
68         }
69         return CONDITIONALLY_UNAVAILABLE;
70     }
71 
72     @ShortcutConstants.UserShortcutType
73     @Override
getShortcutType()74     protected int getShortcutType() {
75         return ShortcutConstants.UserShortcutType.TRIPLETAP;
76     }
77 
78     @Override
setExpanded(boolean expanded)79     public void setExpanded(boolean expanded) {
80         mIsExpanded = expanded;
81     }
82 
83     @Override
isExpanded()84     public boolean isExpanded() {
85         return mIsExpanded;
86     }
87 
88     @Override
isShortcutAvailable()89     protected boolean isShortcutAvailable() {
90         Set<String> shortcutTargets = getShortcutTargets();
91         return shortcutTargets.size() == 1
92                 && shortcutTargets.contains(MAGNIFICATION_CONTROLLER_NAME);
93     }
94 
95     @Override
isChecked()96     protected boolean isChecked() {
97         return Settings.Secure.getInt(
98                 mContext.getContentResolver(),
99                 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_ENABLED,
100                 AccessibilityUtil.State.OFF) == AccessibilityUtil.State.ON;
101     }
102 
103     @Override
enableShortcutForTargets(boolean enable)104     protected void enableShortcutForTargets(boolean enable) {
105         if (Flags.a11yQsShortcut()) {
106             super.enableShortcutForTargets(enable);
107             return;
108         }
109 
110         Settings.Secure.putInt(
111                 mContext.getContentResolver(),
112                 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_ENABLED,
113                 enable ? AccessibilityUtil.State.ON : AccessibilityUtil.State.OFF);
114     }
115 }
116