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.settings.gestures;
18 
19 import android.content.ContentResolver;
20 import android.content.Context;
21 import android.hardware.display.AmbientDisplayConfiguration;
22 import android.provider.Settings;
23 
24 import androidx.annotation.NonNull;
25 
26 import com.android.settings.R;
27 import com.android.settings.core.BasePreferenceController;
28 import com.android.settings.overlay.FeatureFactory;
29 import com.android.settingslib.core.AbstractPreferenceController;
30 
31 import java.util.ArrayList;
32 import java.util.List;
33 
34 public class GesturesSettingPreferenceController extends BasePreferenceController {
35     private final AssistGestureFeatureProvider mFeatureProvider;
36     private List<AbstractPreferenceController> mGestureControllers;
37 
38     private static final String KEY_GESTURES_SETTINGS = "gesture_settings";
39     private static final String FAKE_PREF_KEY = "fake_key_only_for_get_available";
40 
GesturesSettingPreferenceController(Context context)41     public GesturesSettingPreferenceController(Context context) {
42         super(context, KEY_GESTURES_SETTINGS);
43         mFeatureProvider = FeatureFactory.getFactory(context).getAssistGestureFeatureProvider();
44     }
45 
46     @Override
getAvailabilityStatus()47     public int getAvailabilityStatus() {
48         if (mGestureControllers == null) {
49             mGestureControllers = buildAllPreferenceControllers(mContext);
50         }
51         boolean isAvailable = false;
52         for (AbstractPreferenceController controller : mGestureControllers) {
53             isAvailable = isAvailable || controller.isAvailable();
54         }
55         return isAvailable ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
56     }
57 
58     /**
59      * Get all controllers for their availability status when doing getAvailabilityStatus.
60      * Do not use this method to add controllers into fragment, most of below controllers already
61      * convert to TogglePreferenceController, please register them in xml.
62      * The key is fake because those controllers won't be use to control preference.
63      */
buildAllPreferenceControllers( @onNull Context context)64     private static List<AbstractPreferenceController> buildAllPreferenceControllers(
65             @NonNull Context context) {
66         final AmbientDisplayConfiguration ambientDisplayConfiguration =
67                 new AmbientDisplayConfiguration(context);
68         final List<AbstractPreferenceController> controllers = new ArrayList<>();
69 
70         controllers.add(new AssistGestureSettingsPreferenceController(context, FAKE_PREF_KEY)
71                 .setAssistOnly(false));
72         controllers.add(new SwipeToNotificationPreferenceController(context, FAKE_PREF_KEY));
73         controllers.add(new DoubleTwistPreferenceController(context, FAKE_PREF_KEY));
74         controllers.add(new DoubleTapPowerPreferenceController(context, FAKE_PREF_KEY));
75         controllers.add(new PickupGesturePreferenceController(context, FAKE_PREF_KEY)
76                 .setConfig(ambientDisplayConfiguration));
77         controllers.add(new DoubleTapScreenPreferenceController(context, FAKE_PREF_KEY)
78                 .setConfig(ambientDisplayConfiguration));
79         controllers.add(new PreventRingingParentPreferenceController(context, FAKE_PREF_KEY));
80         return controllers;
81     }
82 
83     @Override
getSummary()84     public CharSequence getSummary() {
85         if (!mFeatureProvider.isSensorAvailable(mContext)) {
86             return "";
87         }
88         final ContentResolver contentResolver = mContext.getContentResolver();
89         final boolean assistGestureEnabled = Settings.Secure.getInt(
90                 contentResolver, Settings.Secure.ASSIST_GESTURE_ENABLED, 1) != 0;
91         final boolean assistGestureSilenceEnabled = Settings.Secure.getInt(
92                 contentResolver, Settings.Secure.ASSIST_GESTURE_SILENCE_ALERTS_ENABLED, 1) != 0;
93 
94         if (mFeatureProvider.isSupported(mContext) && assistGestureEnabled) {
95             return mContext.getText(
96                     R.string.language_input_gesture_summary_on_with_assist);
97         }
98         if (assistGestureSilenceEnabled) {
99             return mContext.getText(
100                     R.string.language_input_gesture_summary_on_non_assist);
101         }
102         return mContext.getText(R.string.language_input_gesture_summary_off);
103     }
104 }
105