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 import android.text.TextUtils;
22 
23 import androidx.lifecycle.Lifecycle;
24 import androidx.lifecycle.LifecycleObserver;
25 import androidx.lifecycle.OnLifecycleEvent;
26 import androidx.preference.Preference;
27 import androidx.preference.PreferenceScreen;
28 
29 import com.android.settings.R;
30 import com.android.settings.core.BasePreferenceController;
31 
32 /** Configures the behaviour of long press power footer. */
33 public class LongPressPowerFooterPreferenceController extends BasePreferenceController
34         implements PowerMenuSettingsUtils.SettingsStateCallback, LifecycleObserver {
35 
36     private Preference mPreference;
37     private final PowerMenuSettingsUtils mUtils;
38 
LongPressPowerFooterPreferenceController(Context context, String key)39     public LongPressPowerFooterPreferenceController(Context context, String key) {
40         super(context, key);
41         mUtils = new PowerMenuSettingsUtils(context);
42     }
43 
44     @Override
getAvailabilityStatus()45     public int getAvailabilityStatus() {
46         return AVAILABLE;
47     }
48 
49     @Override
displayPreference(PreferenceScreen screen)50     public void displayPreference(PreferenceScreen screen) {
51         super.displayPreference(screen);
52         mPreference = screen.findPreference(getPreferenceKey());
53     }
54 
55     @Override
updateState(Preference preference)56     public void updateState(Preference preference) {
57         super.updateState(preference);
58 
59         CharSequence footerHintText = mContext.getString(R.string.power_menu_power_volume_up_hint);
60         // If the device supports hush gesture, we need to tell the user where to find the setting.
61         if (mContext.getResources()
62                 .getBoolean(com.android.internal.R.bool.config_volumeHushGestureEnabled)) {
63             footerHintText =
64                     TextUtils.concat(
65                             footerHintText,
66                             "\n\n",
67                             mContext.getString(R.string.power_menu_power_prevent_ringing_hint));
68         }
69 
70         preference.setSummary(footerHintText);
71         preference.setVisible(PowerMenuSettingsUtils.isLongPressPowerForAssistantEnabled(mContext));
72     }
73 
74     @Override
onChange(Uri uri)75     public void onChange(Uri uri) {
76         if (mPreference != null) {
77             updateState(mPreference);
78         }
79     }
80 
81     /** @OnLifecycleEvent(Lifecycle.Event.ON_START) */
82     @OnLifecycleEvent(Lifecycle.Event.ON_START)
onStart()83     public void onStart() {
84         mUtils.registerObserver(this);
85     }
86 
87     /** @OnLifecycleEvent(Lifecycle.Event.ON_STOP) */
88     @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
onStop()89     public void onStop() {
90         mUtils.unregisterObserver();
91     }
92 }
93