• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.app.settings.SettingsEnums;
22 import android.content.Context;
23 import android.os.UserHandle;
24 import android.provider.Settings;
25 import android.util.Log;
26 
27 import com.android.settings.R;
28 import com.android.settings.support.actionbar.HelpResourceProvider;
29 import com.android.settings.utils.CandidateInfoExtra;
30 import com.android.settings.widget.RadioButtonPickerFragment;
31 import com.android.settingslib.widget.CandidateInfo;
32 import com.android.settingslib.widget.SelectorWithWidgetPreference;
33 
34 import java.util.ArrayList;
35 import java.util.HashSet;
36 import java.util.List;
37 import java.util.Set;
38 
39 /**
40  * Fragment that is used to control fold setting.
41  *
42  * Keep the setting values in this class in sync with the values in
43  * {@link com.android.server.utils.FoldSettingProvider}
44  */
45 public class FoldLockBehaviorSettings extends RadioButtonPickerFragment implements
46         HelpResourceProvider {
47 
48     public static final String SETTING_VALUE_STAY_AWAKE_ON_FOLD = "stay_awake_on_fold_key";
49     public static final String SETTING_VALUE_SELECTIVE_STAY_AWAKE = "selective_stay_awake_key";
50     public static final String SETTING_VALUE_SLEEP_ON_FOLD = "sleep_on_fold_key";
51     private static final String SETTING_VALUE_DEFAULT = SETTING_VALUE_SELECTIVE_STAY_AWAKE;
52     public static final String TAG = "FoldLockBehaviorSetting";
53     public static final HashSet<String> SETTING_VALUES = new HashSet<>(
54             Set.of(SETTING_VALUE_STAY_AWAKE_ON_FOLD, SETTING_VALUE_SELECTIVE_STAY_AWAKE,
55                     SETTING_VALUE_SLEEP_ON_FOLD));
56 
57     private Context mContext;
58 
59     @Override
onAttach(Context context)60     public void onAttach(Context context) {
61         super.onAttach(context);
62         mContext = context;
63     }
64 
65     @Override
getCandidates()66     protected List<? extends CandidateInfo> getCandidates() {
67         List<CandidateInfoExtra> candidates = new ArrayList<>();
68         candidates.add(new CandidateInfoExtra(
69                 resourceToString(R.string.stay_awake_on_fold_title),
70                 resourceToString(R.string.stay_awake_on_fold_summary),
71                 SETTING_VALUE_STAY_AWAKE_ON_FOLD, /* enabled */ true));
72         candidates.add(new CandidateInfoExtra(
73                 resourceToString(R.string.selective_stay_awake_title),
74                 resourceToString(R.string.selective_stay_awake_summary),
75                 SETTING_VALUE_SELECTIVE_STAY_AWAKE, /* enabled */ true));
76         candidates.add(new CandidateInfoExtra(
77                 resourceToString(R.string.sleep_on_fold_title),
78                 resourceToString(R.string.sleep_on_fold_summary),
79                 SETTING_VALUE_SLEEP_ON_FOLD, /* enabled */ true));
80         return candidates;
81     }
82 
83     @Override
bindPreferenceExtra(SelectorWithWidgetPreference pref, String key, CandidateInfo info, String defaultKey, String systemDefaultKey)84     public void bindPreferenceExtra(SelectorWithWidgetPreference pref,
85             String key, CandidateInfo info, String defaultKey, String systemDefaultKey) {
86         if (!(info instanceof CandidateInfoExtra)) {
87             return;
88         }
89 
90         pref.setSummary(((CandidateInfoExtra) info).loadSummary());
91     }
92 
93     @Override
getDefaultKey()94     protected String getDefaultKey() {
95         String foldSettingValue = getCurrentFoldSettingValue();
96         foldSettingValue = (foldSettingValue != null) ? foldSettingValue : SETTING_VALUE_DEFAULT;
97         if (!SETTING_VALUES.contains(foldSettingValue)) {
98             Log.e(TAG,
99                     "getDefaultKey: Invalid setting value, returning default setting value");
100             foldSettingValue = SETTING_VALUE_DEFAULT;
101         }
102 
103         return foldSettingValue;
104     }
105 
106     @Override
setDefaultKey(String key)107     protected boolean setDefaultKey(String key) {
108         if (!SETTING_VALUES.contains(key)) {
109             Log.e(TAG, "setDefaultKey: Can not set invalid key: " + key);
110             key = SETTING_VALUE_SELECTIVE_STAY_AWAKE;
111         }
112         setCurrentFoldSettingValue(key);
113         return true;
114     }
115 
116     @Override
getMetricsCategory()117     public int getMetricsCategory() {
118         return SettingsEnums.FOLD_LOCK_BEHAVIOR;
119     }
120 
121     @Override
getPreferenceScreenResId()122     protected int getPreferenceScreenResId() {
123         return R.xml.fold_lock_behavior_settings;
124     }
125 
getCurrentFoldSettingValue()126     private String getCurrentFoldSettingValue() {
127         return Settings.System.getStringForUser(mContext.getContentResolver(),
128                 FOLD_LOCK_BEHAVIOR,
129                 UserHandle.USER_CURRENT);
130     }
131 
setCurrentFoldSettingValue(String key)132     private void setCurrentFoldSettingValue(String key) {
133         Settings.System.putStringForUser(mContext.getContentResolver(),
134                 FOLD_LOCK_BEHAVIOR,
135                 key,
136                 UserHandle.USER_CURRENT);
137     }
138 
resourceToString(int resource)139     private String resourceToString(int resource) {
140         return mContext.getText(resource).toString();
141     }
142 }
143