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.display;
18 
19 import static android.provider.Settings.System.FOLD_LOCK_BEHAVIOR;
20 
21 import static com.android.settings.display.FoldLockBehaviorSettings.SETTING_VALUES;
22 import static com.android.settings.display.FoldLockBehaviorSettings.SETTING_VALUE_SELECTIVE_STAY_AWAKE;
23 import static com.android.settings.display.FoldLockBehaviorSettings.SETTING_VALUE_SLEEP_ON_FOLD;
24 import static com.android.settings.display.FoldLockBehaviorSettings.SETTING_VALUE_STAY_AWAKE_ON_FOLD;
25 
26 import android.content.Context;
27 import android.os.UserHandle;
28 import android.provider.Settings;
29 
30 import androidx.preference.Preference;
31 
32 import com.android.internal.foldables.FoldGracePeriodProvider;
33 import com.android.internal.foldables.FoldLockSettingAvailabilityProvider;
34 import com.android.settings.R;
35 import com.android.settings.core.BasePreferenceController;
36 
37 import java.util.HashMap;
38 import java.util.Map;
39 
40 /**
41  * A preference controller for the @link android.provider.Settings.System#FOLD_LOCK_BEHAVIOR
42  * setting.
43  *
44  * This preference controller allows users to control whether or not the device
45  * stays awake when it is folded.
46  */
47 public class FoldLockBehaviorPreferenceController extends BasePreferenceController {
48 
49     private static final Map<String, String> KEY_TO_TEXT = new HashMap<>();
50     private final FoldLockSettingAvailabilityProvider mFoldLockSettingAvailabilityProvider;
51 
FoldLockBehaviorPreferenceController(Context context, String key)52     public FoldLockBehaviorPreferenceController(Context context, String key) {
53         this(context, key, new FoldLockSettingAvailabilityProvider(context.getResources()));
54     }
55 
FoldLockBehaviorPreferenceController(Context context, String key, FoldLockSettingAvailabilityProvider foldLockSettingAvailabilityProvider)56     public FoldLockBehaviorPreferenceController(Context context, String key,
57             FoldLockSettingAvailabilityProvider foldLockSettingAvailabilityProvider) {
58         super(context, key);
59         mFoldLockSettingAvailabilityProvider = foldLockSettingAvailabilityProvider;
60         KEY_TO_TEXT.put(SETTING_VALUE_STAY_AWAKE_ON_FOLD,
61                 resourceToString(R.string.stay_awake_on_fold_title));
62         if (new FoldGracePeriodProvider().isEnabled()) {
63             KEY_TO_TEXT.put(SETTING_VALUE_SELECTIVE_STAY_AWAKE,
64                     resourceToString(R.string.stay_awake_on_lockscreen_title));
65         } else {
66             KEY_TO_TEXT.put(SETTING_VALUE_SELECTIVE_STAY_AWAKE,
67                     resourceToString(R.string.selective_stay_awake_title));
68         }
69         KEY_TO_TEXT.put(SETTING_VALUE_SLEEP_ON_FOLD,
70                 resourceToString(R.string.sleep_on_fold_title));
71     }
72 
73     @Override
getAvailabilityStatus()74     public int getAvailabilityStatus() {
75         boolean isFoldLockBehaviorAvailable =
76                 mFoldLockSettingAvailabilityProvider.isFoldLockBehaviorAvailable();
77         return isFoldLockBehaviorAvailable
78                 ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
79     }
80 
81     @Override
updateState(Preference preference)82     public void updateState(Preference preference) {
83         String summary = KEY_TO_TEXT.get(getFoldSettingValue());
84         preference.setSummary(summary);
85     }
86 
getFoldSettingValue()87     private String getFoldSettingValue() {
88         String foldSettingValue = Settings.System.getStringForUser(mContext.getContentResolver(),
89                 FOLD_LOCK_BEHAVIOR, UserHandle.USER_CURRENT);
90         return (foldSettingValue != null && SETTING_VALUES.contains(foldSettingValue))
91                 ? foldSettingValue : SETTING_VALUE_SELECTIVE_STAY_AWAKE;
92     }
93 
94     @Override
getSliceHighlightMenuRes()95     public int getSliceHighlightMenuRes() {
96         return R.string.menu_key_display;
97     }
98 
resourceToString(int resource)99     private String resourceToString(int resource) {
100         return mContext.getText(resource).toString();
101     }
102 
103 }
104