1 /* 2 * Copyright (C) 2017 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.development; 18 19 import android.content.ContentResolver; 20 import android.content.Context; 21 import android.database.ContentObserver; 22 import android.net.Uri; 23 import android.os.BatteryManager; 24 import android.os.Handler; 25 import android.provider.Settings; 26 27 import androidx.annotation.VisibleForTesting; 28 import androidx.preference.Preference; 29 import androidx.preference.PreferenceScreen; 30 31 import com.android.settings.core.PreferenceControllerMixin; 32 import com.android.settingslib.RestrictedLockUtils; 33 import com.android.settingslib.RestrictedLockUtilsInternal; 34 import com.android.settingslib.RestrictedSwitchPreference; 35 import com.android.settingslib.core.lifecycle.Lifecycle; 36 import com.android.settingslib.core.lifecycle.LifecycleObserver; 37 import com.android.settingslib.core.lifecycle.events.OnPause; 38 import com.android.settingslib.core.lifecycle.events.OnResume; 39 import com.android.settingslib.development.DeveloperOptionsPreferenceController; 40 41 42 public class StayAwakePreferenceController extends DeveloperOptionsPreferenceController 43 implements Preference.OnPreferenceChangeListener, LifecycleObserver, OnResume, OnPause, 44 PreferenceControllerMixin { 45 46 private static final String TAG = "StayAwakeCtrl"; 47 private static final String PREFERENCE_KEY = "keep_screen_on"; 48 @VisibleForTesting 49 static final int SETTING_VALUE_OFF = 0; 50 @VisibleForTesting 51 static final int SETTING_VALUE_ON = 52 BatteryManager.BATTERY_PLUGGED_AC | BatteryManager.BATTERY_PLUGGED_USB 53 | BatteryManager.BATTERY_PLUGGED_WIRELESS; 54 @VisibleForTesting 55 SettingsObserver mSettingsObserver; 56 57 private RestrictedSwitchPreference mPreference; 58 StayAwakePreferenceController(Context context, Lifecycle lifecycle)59 public StayAwakePreferenceController(Context context, Lifecycle lifecycle) { 60 super(context); 61 62 if (lifecycle != null) { 63 lifecycle.addObserver(this); 64 } 65 } 66 67 @Override getPreferenceKey()68 public String getPreferenceKey() { 69 return PREFERENCE_KEY; 70 } 71 72 @Override displayPreference(PreferenceScreen screen)73 public void displayPreference(PreferenceScreen screen) { 74 super.displayPreference(screen); 75 mPreference = screen.findPreference(getPreferenceKey()); 76 } 77 78 @Override onPreferenceChange(Preference preference, Object newValue)79 public boolean onPreferenceChange(Preference preference, Object newValue) { 80 final boolean stayAwake = (Boolean) newValue; 81 Settings.Global.putInt(mContext.getContentResolver(), 82 Settings.Global.STAY_ON_WHILE_PLUGGED_IN, 83 stayAwake ? SETTING_VALUE_ON : SETTING_VALUE_OFF); 84 return true; 85 } 86 87 @Override updateState(Preference preference)88 public void updateState(Preference preference) { 89 final RestrictedLockUtils.EnforcedAdmin admin = checkIfMaximumTimeToLockSetByAdmin(); 90 if (admin != null) { 91 mPreference.setDisabledByAdmin(admin); 92 return; 93 } 94 95 final int stayAwakeMode = Settings.Global.getInt(mContext.getContentResolver(), 96 Settings.Global.STAY_ON_WHILE_PLUGGED_IN, 97 SETTING_VALUE_OFF); 98 mPreference.setChecked(stayAwakeMode != SETTING_VALUE_OFF); 99 } 100 101 @Override onResume()102 public void onResume() { 103 if (mPreference == null) { 104 return; 105 } 106 if (mSettingsObserver == null) { 107 mSettingsObserver = new SettingsObserver(); 108 } 109 mSettingsObserver.register(true /* register */); 110 } 111 112 @Override onPause()113 public void onPause() { 114 if (mPreference == null || mSettingsObserver == null) { 115 return; 116 } 117 mSettingsObserver.register(false /* unregister */); 118 } 119 120 @Override onDeveloperOptionsSwitchDisabled()121 protected void onDeveloperOptionsSwitchDisabled() { 122 super.onDeveloperOptionsSwitchDisabled(); 123 Settings.Global.putInt(mContext.getContentResolver(), 124 Settings.Global.STAY_ON_WHILE_PLUGGED_IN, SETTING_VALUE_OFF); 125 mPreference.setChecked(false); 126 } 127 128 @VisibleForTesting checkIfMaximumTimeToLockSetByAdmin()129 RestrictedLockUtils.EnforcedAdmin checkIfMaximumTimeToLockSetByAdmin() { 130 // A DeviceAdmin has specified a maximum time until the device 131 // will lock... in this case we can't allow the user to turn 132 // on "stay awake when plugged in" because that would defeat the 133 // restriction. 134 return RestrictedLockUtilsInternal.checkIfMaximumTimeToLockIsSet(mContext); 135 } 136 137 @VisibleForTesting 138 class SettingsObserver extends ContentObserver { 139 private final Uri mStayAwakeUri = Settings.Global.getUriFor( 140 Settings.Global.STAY_ON_WHILE_PLUGGED_IN); 141 SettingsObserver()142 public SettingsObserver() { 143 super(new Handler()); 144 } 145 register(boolean register)146 public void register(boolean register) { 147 final ContentResolver cr = mContext.getContentResolver(); 148 if (register) { 149 cr.registerContentObserver( 150 mStayAwakeUri, false, this); 151 } else { 152 cr.unregisterContentObserver(this); 153 } 154 } 155 156 @Override onChange(boolean selfChange, Uri uri)157 public void onChange(boolean selfChange, Uri uri) { 158 super.onChange(selfChange, uri); 159 if (mStayAwakeUri.equals(uri)) { 160 updateState(mPreference); 161 } 162 } 163 } 164 } 165