1 /* 2 * Copyright (C) 2020 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.SCREEN_OFF_TIMEOUT; 20 21 import static com.android.settings.display.ScreenTimeoutSettings.FALLBACK_SCREEN_TIMEOUT_VALUE; 22 23 import android.app.admin.DevicePolicyManager; 24 import android.content.Context; 25 import android.os.UserHandle; 26 import android.os.UserManager; 27 import android.provider.Settings; 28 29 import androidx.preference.Preference; 30 31 import com.android.settings.R; 32 import com.android.settings.core.BasePreferenceController; 33 import com.android.settingslib.RestrictedLockUtils; 34 import com.android.settingslib.RestrictedLockUtilsInternal; 35 import com.android.settingslib.RestrictedPreference; 36 37 /** 38 * The controller of {@link ScreenTimeoutSettings}. 39 */ 40 public class ScreenTimeoutPreferenceController extends BasePreferenceController { 41 public static String PREF_NAME = "screen_timeout"; 42 43 private final CharSequence[] mTimeoutEntries; 44 private final CharSequence[] mTimeoutValues; 45 ScreenTimeoutPreferenceController(Context context, String key)46 public ScreenTimeoutPreferenceController(Context context, String key) { 47 super(context, key); 48 mTimeoutEntries = context.getResources().getStringArray(R.array.screen_timeout_entries); 49 mTimeoutValues = context.getResources().getStringArray(R.array.screen_timeout_values); 50 } 51 52 @Override getAvailabilityStatus()53 public int getAvailabilityStatus() { 54 return AVAILABLE; 55 } 56 57 @Override updateState(Preference preference)58 public void updateState(Preference preference) { 59 final long maxTimeout = getMaxScreenTimeout(); 60 final RestrictedLockUtils.EnforcedAdmin admin = getPreferenceDisablingAdmin(maxTimeout); 61 if (admin != null) { 62 preference.setEnabled(false); 63 preference.setSummary(mContext.getText(R.string.disabled_by_policy_title)); 64 ((RestrictedPreference) preference).setDisabledByAdmin(admin); 65 } else { 66 preference.setSummary(getTimeoutSummary(maxTimeout)); 67 } 68 } 69 getTimeoutSummary(long maxTimeout)70 private CharSequence getTimeoutSummary(long maxTimeout) { 71 final long currentTimeout = getCurrentScreenTimeout(); 72 final CharSequence description = getTimeoutDescription(currentTimeout, maxTimeout); 73 return mContext.getString(R.string.screen_timeout_summary, description); 74 } 75 getMaxScreenTimeout()76 private Long getMaxScreenTimeout() { 77 if (RestrictedLockUtilsInternal.checkIfMaximumTimeToLockIsSet(mContext) != null) { 78 final DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class); 79 if (dpm != null) { 80 return dpm.getMaximumTimeToLock(null /* admin */, UserHandle.myUserId()); 81 } 82 } 83 return Long.MAX_VALUE; 84 } 85 86 /** 87 * Returns the admin that causes the preference to be disabled completely. This could be due to 88 * either an admin that has set the {@link UserManager.DISALLOW_CONFIG_SCREEN_TIMEOUT} 89 * restriction, or an admin that has set a very small MaximumTimeToLock timeout resulting in 90 * no possible options for the user. 91 */ getPreferenceDisablingAdmin(long maxTimeout)92 private RestrictedLockUtils.EnforcedAdmin getPreferenceDisablingAdmin(long maxTimeout) { 93 final DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class); 94 RestrictedLockUtils.EnforcedAdmin admin = null; 95 if (dpm != null) { 96 admin = RestrictedLockUtilsInternal.checkIfRestrictionEnforced( 97 mContext, UserManager.DISALLOW_CONFIG_SCREEN_TIMEOUT, 98 UserHandle.myUserId()); 99 if (admin == null && getLargestTimeout(maxTimeout) == null) { 100 admin = RestrictedLockUtilsInternal.checkIfMaximumTimeToLockIsSet(mContext); 101 } 102 } 103 return admin; 104 } 105 getCurrentScreenTimeout()106 private long getCurrentScreenTimeout() { 107 return Settings.System.getLong(mContext.getContentResolver(), 108 SCREEN_OFF_TIMEOUT, FALLBACK_SCREEN_TIMEOUT_VALUE); 109 } 110 getTimeoutDescription(long currentTimeout, long maxTimeout)111 private CharSequence getTimeoutDescription(long currentTimeout, long maxTimeout) { 112 if (currentTimeout < 0 || mTimeoutEntries == null || mTimeoutValues == null 113 || mTimeoutValues.length != mTimeoutEntries.length) { 114 return null; 115 } 116 117 if (currentTimeout > maxTimeout) { 118 // The selected time out value is longer than the max timeout allowed by the admin. 119 // Select the largest value from the list by default. 120 return getLargestTimeout(maxTimeout); 121 } else { 122 return getCurrentTimeout(currentTimeout); 123 } 124 } 125 getCurrentTimeout(long currentTimeout)126 private CharSequence getCurrentTimeout(long currentTimeout) { 127 for (int i = 0; i < mTimeoutValues.length; i++) { 128 if (currentTimeout == Long.parseLong(mTimeoutValues[i].toString())) { 129 return mTimeoutEntries[i]; 130 } 131 } 132 return null; 133 } 134 getLargestTimeout(long maxTimeout)135 private CharSequence getLargestTimeout(long maxTimeout) { 136 CharSequence largestTimeout = null; 137 // The list of timeouts is sorted 138 for (int i = 0; i < mTimeoutValues.length; ++i) { 139 if (Long.parseLong(mTimeoutValues[i].toString()) <= maxTimeout) { 140 largestTimeout = mTimeoutEntries[i]; 141 } 142 } 143 return largestTimeout; 144 } 145 } 146