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 Power
34  * Menu.
35  */
36 public class LongPressPowerForPowerMenuPreferenceController extends BasePreferenceController
37         implements PowerMenuSettingsUtils.SettingsStateCallback,
38                 SelectorWithWidgetPreference.OnClickListener,
39                 LifecycleObserver {
40 
41     private SelectorWithWidgetPreference mPreference;
42     private final PowerMenuSettingsUtils mUtils;
43 
LongPressPowerForPowerMenuPreferenceController(Context context, String key)44     public LongPressPowerForPowerMenuPreferenceController(Context context, String key) {
45         super(context, key);
46         mUtils = new PowerMenuSettingsUtils(context);
47     }
48 
49     @Override
getAvailabilityStatus()50     public int getAvailabilityStatus() {
51         return PowerMenuSettingsUtils.isLongPressPowerSettingAvailable(mContext)
52                 ? AVAILABLE
53                 : UNSUPPORTED_ON_DEVICE;
54     }
55 
56     @Override
displayPreference(PreferenceScreen screen)57     public void displayPreference(PreferenceScreen screen) {
58         super.displayPreference(screen);
59         mPreference = screen.findPreference(getPreferenceKey());
60         if (mPreference != null) {
61             mPreference.setOnClickListener(this);
62         }
63     }
64 
65     @Override
updateState(Preference preference)66     public void updateState(Preference preference) {
67         super.updateState(preference);
68         if (preference instanceof SelectorWithWidgetPreference) {
69             ((SelectorWithWidgetPreference) preference)
70                     .setChecked(
71                             !PowerMenuSettingsUtils.isLongPressPowerForAssistantEnabled(mContext));
72         }
73     }
74 
75     @Override
onRadioButtonClicked(SelectorWithWidgetPreference preference)76     public void onRadioButtonClicked(SelectorWithWidgetPreference preference) {
77         PowerMenuSettingsUtils.setLongPressPowerForPowerMenu(mContext);
78         if (mPreference != null) {
79             updateState(mPreference);
80         }
81     }
82 
83     @Override
onChange(Uri uri)84     public void onChange(Uri uri) {
85         if (mPreference != null) {
86             updateState(mPreference);
87         }
88     }
89 
90     /** @OnLifecycleEvent(Lifecycle.Event.ON_START) */
91     @OnLifecycleEvent(Lifecycle.Event.ON_START)
onStart()92     public void onStart() {
93         mUtils.registerObserver(this);
94     }
95 
96     /** @OnLifecycleEvent(Lifecycle.Event.ON_STOP) */
97     @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
onStop()98     public void onStop() {
99         mUtils.unregisterObserver();
100     }
101 }
102