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