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.server.utils; 18 19 import android.content.ContentResolver; 20 import android.content.Context; 21 import android.os.UserHandle; 22 import android.provider.Settings; 23 import android.util.Log; 24 25 import com.android.internal.foldables.FoldLockSettingAvailabilityProvider; 26 import com.android.internal.util.SettingsWrapper; 27 28 import java.util.Set; 29 30 /** 31 * This class provides a convenient way to access the {@link Settings.System#FOLD_LOCK_BEHAVIOR}. 32 * The {@link Settings.System#FOLD_LOCK_BEHAVIOR} setting controls the behavior of the device when 33 * it is folded, and provides the user with three different options to choose from. Those are: 34 * 1. Stay awake on fold: The device will remain unlocked when it is folded. 35 * 2. Selective stay awake: The device will remain unlocked when it is folded only if there are 36 * apps with wakelocks running. This is also the set default behavior. 37 * 3. Sleep on fold: The device will lock when it is folded, regardless of which apps are running 38 * or whether any wakelocks are held. 39 * 40 * Keep the setting values in this class in sync with the values in 41 * {@link com.android.settings.display.FoldLockBehaviorSettings} 42 */ 43 public class FoldSettingProvider { 44 45 public static final String SETTING_VALUE_STAY_AWAKE_ON_FOLD = "stay_awake_on_fold_key"; 46 public static final String SETTING_VALUE_SELECTIVE_STAY_AWAKE = "selective_stay_awake_key"; 47 public static final String SETTING_VALUE_SLEEP_ON_FOLD = "sleep_on_fold_key"; 48 private static final String SETTING_VALUE_DEFAULT = SETTING_VALUE_SELECTIVE_STAY_AWAKE; 49 private static final Set<String> SETTING_VALUES = Set.of(SETTING_VALUE_STAY_AWAKE_ON_FOLD, 50 SETTING_VALUE_SELECTIVE_STAY_AWAKE, SETTING_VALUE_SLEEP_ON_FOLD); 51 private static final String TAG = "FoldSettingProvider"; 52 53 private final ContentResolver mContentResolver; 54 private final SettingsWrapper mSettingsWrapper; 55 private final FoldLockSettingAvailabilityProvider mFoldLockSettingAvailabilityProvider; 56 FoldSettingProvider(Context context, SettingsWrapper settingsWrapper, FoldLockSettingAvailabilityProvider foldLockSettingAvailabilityProvider)57 public FoldSettingProvider(Context context, SettingsWrapper settingsWrapper, 58 FoldLockSettingAvailabilityProvider foldLockSettingAvailabilityProvider) { 59 mContentResolver = context.getContentResolver(); 60 mSettingsWrapper = settingsWrapper; 61 mFoldLockSettingAvailabilityProvider = foldLockSettingAvailabilityProvider; 62 } 63 64 /** 65 * Returns whether the device should remain awake after folding. 66 */ shouldStayAwakeOnFold()67 public boolean shouldStayAwakeOnFold() { 68 return getFoldSettingValue().equals(SETTING_VALUE_STAY_AWAKE_ON_FOLD); 69 } 70 71 /** 72 * Returns whether the device should selective remain awake after folding. 73 */ shouldSelectiveStayAwakeOnFold()74 public boolean shouldSelectiveStayAwakeOnFold() { 75 return getFoldSettingValue().equals(SETTING_VALUE_SELECTIVE_STAY_AWAKE); 76 } 77 78 /** 79 * Returns whether the device should strictly sleep after folding. 80 */ shouldSleepOnFold()81 public boolean shouldSleepOnFold() { 82 return getFoldSettingValue().equals(SETTING_VALUE_SLEEP_ON_FOLD); 83 } 84 getFoldSettingValue()85 private String getFoldSettingValue() { 86 boolean isFoldLockBehaviorAvailable = 87 mFoldLockSettingAvailabilityProvider.isFoldLockBehaviorAvailable(); 88 if (!isFoldLockBehaviorAvailable) { 89 return SETTING_VALUE_DEFAULT; 90 } 91 String foldSettingValue = mSettingsWrapper.getStringForUser( 92 mContentResolver, 93 Settings.System.FOLD_LOCK_BEHAVIOR, 94 UserHandle.USER_CURRENT); 95 foldSettingValue = (foldSettingValue != null) ? foldSettingValue : SETTING_VALUE_DEFAULT; 96 if (!SETTING_VALUES.contains(foldSettingValue)) { 97 Log.e(TAG, 98 "getFoldSettingValue: Invalid setting value, returning default setting value"); 99 foldSettingValue = SETTING_VALUE_DEFAULT; 100 } 101 102 return foldSettingValue; 103 } 104 } 105