1 /*
2  * Copyright (C) 2022 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.Context;
20 import android.net.Uri;
21 
22 import androidx.lifecycle.Lifecycle;
23 import androidx.lifecycle.LifecycleObserver;
24 import androidx.lifecycle.OnLifecycleEvent;
25 import androidx.preference.Preference;
26 import androidx.preference.PreferenceScreen;
27 
28 import com.android.settings.R;
29 import com.android.settings.core.BasePreferenceController;
30 import com.android.settingslib.widget.SelectorWithWidgetPreference;
31 
32 /**
33  * Configures the behaviour of the radio selector to configure long press power button to Assistant.
34  */
35 public class LongPressPowerForAssistantPreferenceController extends BasePreferenceController
36         implements PowerMenuSettingsUtils.SettingsStateCallback,
37                 SelectorWithWidgetPreference.OnClickListener,
38                 LifecycleObserver {
39 
40     private SelectorWithWidgetPreference mPreference;
41     private final PowerMenuSettingsUtils mUtils;
42 
LongPressPowerForAssistantPreferenceController(Context context, String key)43     public LongPressPowerForAssistantPreferenceController(Context context, String key) {
44         super(context, key);
45         mUtils = new PowerMenuSettingsUtils(context);
46     }
47 
48     @Override
getAvailabilityStatus()49     public int getAvailabilityStatus() {
50         return PowerMenuSettingsUtils.isLongPressPowerSettingAvailable(mContext)
51                 ? AVAILABLE
52                 : UNSUPPORTED_ON_DEVICE;
53     }
54 
55     @Override
displayPreference(PreferenceScreen screen)56     public void displayPreference(PreferenceScreen screen) {
57         super.displayPreference(screen);
58         mPreference = screen.findPreference(getPreferenceKey());
59         if (mPreference != null) {
60             mPreference.setOnClickListener(this);
61         }
62     }
63 
64     @Override
updateState(Preference preference)65     public void updateState(Preference preference) {
66         super.updateState(preference);
67         if (preference instanceof SelectorWithWidgetPreference) {
68             ((SelectorWithWidgetPreference) preference)
69                     .setChecked(
70                             PowerMenuSettingsUtils.isLongPressPowerForAssistantEnabled(mContext));
71         }
72     }
73 
74     @Override
onRadioButtonClicked(SelectorWithWidgetPreference preference)75     public void onRadioButtonClicked(SelectorWithWidgetPreference preference) {
76         PowerMenuSettingsUtils.setLongPressPowerForAssistant(mContext);
77         if (mPreference != null) {
78             updateState(mPreference);
79         }
80     }
81 
82     @Override
onChange(Uri uri)83     public void onChange(Uri uri) {
84         if (mPreference != null) {
85             updateState(mPreference);
86         }
87     }
88 
89     /** @OnLifecycleEvent(Lifecycle.Event.ON_START) */
90     @OnLifecycleEvent(Lifecycle.Event.ON_START)
onStart()91     public void onStart() {
92         mUtils.registerObserver(this);
93     }
94 
95     /** @OnLifecycleEvent(Lifecycle.Event.ON_STOP) */
96     @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
onStop()97     public void onStop() {
98         mUtils.unregisterObserver();
99     }
100 }
101