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