1 /* 2 * Copyright (C) 2024 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.internal.policy; 18 19 import android.content.ContentResolver; 20 import android.content.Context; 21 import android.database.ContentObserver; 22 import android.os.Handler; 23 import android.os.UserHandle; 24 import android.provider.Settings; 25 26 import androidx.annotation.Nullable; 27 import androidx.annotation.VisibleForTesting; 28 29 import java.util.Set; 30 31 /** 32 * A ContentObserver that listens for changes in the "Continue using apps on fold" setting. This 33 * setting determines a device's behavior when the user folds the device. 34 * @hide 35 * 36 * Keep the setting values in this class in sync with the values in 37 * {@link com.android.server.utils.FoldSettingProvider} and 38 * {@link com.android.settings.display.FoldLockBehaviorSettings} 39 */ 40 public class FoldLockSettingsObserver extends ContentObserver { 41 /** The setting for "stay awake on fold". */ 42 public static final String SETTING_VALUE_STAY_AWAKE_ON_FOLD = "stay_awake_on_fold_key"; 43 /** The setting for "swipe up to continue". */ 44 public static final String SETTING_VALUE_SELECTIVE_STAY_AWAKE = "selective_stay_awake_key"; 45 /** The setting for "always sleep on fold". */ 46 public static final String SETTING_VALUE_SLEEP_ON_FOLD = "sleep_on_fold_key"; 47 public static final String SETTING_VALUE_DEFAULT = SETTING_VALUE_SELECTIVE_STAY_AWAKE; 48 private static final Set<String> SETTING_VALUES = Set.of(SETTING_VALUE_STAY_AWAKE_ON_FOLD, 49 SETTING_VALUE_SELECTIVE_STAY_AWAKE, SETTING_VALUE_SLEEP_ON_FOLD); 50 51 private final Context mContext; 52 53 /** The cached value of the setting. */ 54 @VisibleForTesting 55 String mFoldLockSetting; 56 FoldLockSettingsObserver(Handler handler, Context context)57 public FoldLockSettingsObserver(Handler handler, Context context) { 58 super(handler); 59 mContext = context; 60 } 61 62 /** Registers the observer and updates the cache for the first time. */ register()63 public void register() { 64 final ContentResolver r = mContext.getContentResolver(); 65 r.registerContentObserver( 66 Settings.System.getUriFor(Settings.System.FOLD_LOCK_BEHAVIOR), 67 false, this, UserHandle.USER_ALL); 68 requestAndCacheFoldLockSetting(); 69 } 70 71 /** Unregisters the observer. */ unregister()72 public void unregister() { 73 mContext.getContentResolver().unregisterContentObserver(this); 74 } 75 76 /** Runs when settings changes. */ 77 @Override onChange(boolean selfChange)78 public void onChange(boolean selfChange) { 79 requestAndCacheFoldLockSetting(); 80 } 81 82 /** 83 * Requests and caches the current FOLD_LOCK_BEHAVIOR setting, which should be one of three 84 * values: SETTING_VALUE_STAY_AWAKE_ON_FOLD, SETTING_VALUE_SELECTIVE_STAY_AWAKE, 85 * SETTING_VALUE_SLEEP_ON_FOLD. If null (not set), returns the system default. 86 */ 87 @VisibleForTesting requestAndCacheFoldLockSetting()88 void requestAndCacheFoldLockSetting() { 89 String currentSetting = request(); 90 91 if (currentSetting == null || !SETTING_VALUES.contains(currentSetting)) { 92 currentSetting = SETTING_VALUE_DEFAULT; 93 } 94 95 setCurrentFoldSetting(currentSetting); 96 } 97 98 /** 99 * Makes a binder call to request the current FOLD_LOCK_BEHAVIOR setting. 100 */ 101 @VisibleForTesting 102 @Nullable request()103 String request() { 104 return Settings.System.getStringForUser(mContext.getContentResolver(), 105 Settings.System.FOLD_LOCK_BEHAVIOR, UserHandle.USER_CURRENT); 106 } 107 108 /** Caches the fold-lock behavior received from Settings. */ 109 @VisibleForTesting setCurrentFoldSetting(String newSetting)110 void setCurrentFoldSetting(String newSetting) { 111 mFoldLockSetting = newSetting; 112 } 113 114 /** Used by external requesters: checks if the current setting is "stay awake on fold". */ isStayAwakeOnFold()115 public boolean isStayAwakeOnFold() { 116 return mFoldLockSetting.equals(SETTING_VALUE_STAY_AWAKE_ON_FOLD); 117 } 118 119 /** Used by external requesters: checks if the current setting is "swipe up to continue". */ isSelectiveStayAwake()120 public boolean isSelectiveStayAwake() { 121 return mFoldLockSetting.equals(SETTING_VALUE_SELECTIVE_STAY_AWAKE); 122 } 123 124 /** Used by external requesters: checks if the current setting is "sleep on fold". */ isSleepOnFold()125 public boolean isSleepOnFold() { 126 return mFoldLockSetting.equals(SETTING_VALUE_SLEEP_ON_FOLD); 127 } 128 } 129