Customize Max Screen Timeout

Make it possible to customize the maximum screen timeout.
Added a configurable max timeout value which, if provided, will
limit the predefined timeout values.
Leverage code from aosp.

Flag: EXEMPT bugfix
Bug: 309512299
Test: atest ScreenTimeoutSettingsTest
Change-Id: Ibb180c89489fe890f1929bdefd5ced87a9566f07
This commit is contained in:
Sunny Shao
2025-03-10 06:52:28 +00:00
parent 2342373850
commit 7f5e1f6e8b
3 changed files with 118 additions and 13 deletions

View File

@@ -127,8 +127,6 @@ public class ScreenTimeoutSettings extends RadioButtonPickerFragment
super.onAttach(context);
mContext = context;
mDevicePolicyManager = mContext.getSystemService(DevicePolicyManager.class);
mInitialEntries = getResources().getStringArray(R.array.screen_timeout_entries);
mInitialValues = getResources().getStringArray(R.array.screen_timeout_values);
mAdaptiveSleepPermissionController =
new AdaptiveSleepPermissionPreferenceController(context);
mAdaptiveSleepCameraStatePreferenceController =
@@ -153,10 +151,14 @@ public class ScreenTimeoutSettings extends RadioButtonPickerFragment
@Override
protected List<? extends CandidateInfo> getCandidates() {
mInitialEntries = getResources().getStringArray(R.array.screen_timeout_entries);
mInitialValues = getResources().getStringArray(R.array.screen_timeout_values);
final List<CandidateInfo> candidates = new ArrayList<>();
final long maxTimeout = getMaxScreenTimeout(getContext());
if (mInitialValues != null) {
for (int i = 0; i < mInitialValues.length; ++i) {
// Truncate mInitialEntries/Values so that they do not exceed maxTimeout
if (Long.parseLong(mInitialValues[i].toString()) <= maxTimeout) {
candidates.add(
new TimeoutCandidateInfo(
@@ -211,7 +213,7 @@ public class ScreenTimeoutSettings extends RadioButtonPickerFragment
for (CandidateInfo info : candidateList) {
ProtectedSelectorWithWidgetPreference pref =
new ProtectedSelectorWithWidgetPreference(
getPrefContext(), info.getKey(), this);
getContext(), info.getKey(), this);
bindPreference(pref, info.getKey(), info, defaultKey);
screen.addPreference(pref);
}
@@ -219,12 +221,17 @@ public class ScreenTimeoutSettings extends RadioButtonPickerFragment
final long selectedTimeout = getTimeoutFromKey(defaultKey);
final long maxTimeout = getMaxScreenTimeout(getContext());
if (!candidateList.isEmpty() && (selectedTimeout > maxTimeout)) {
// The selected time out value is longer than the max timeout allowed by the admin.
// Select the largest value from the list by default.
// The selected time out value is longer than the max timeout allowed by the
// admin/configuration. The list of candidates is already truncated so that
// no value exceeds the max timeout value.
// Select the largest value from the candidates list by default.
int lastIndex = candidateList.size() - 1;
final ProtectedSelectorWithWidgetPreference preferenceWithLargestTimeout =
(ProtectedSelectorWithWidgetPreference)
screen.getPreference(candidateList.size() - 1);
screen.getPreference(lastIndex);
preferenceWithLargestTimeout.setChecked(true);
// Update the system screen timeout setting to match the UI
setCurrentSystemScreenTimeout(getContext(), candidateList.get(lastIndex).getKey());
}
mPrivacyPreference = new FooterPreference(mContext);
@@ -338,7 +345,11 @@ public class ScreenTimeoutSettings extends RadioButtonPickerFragment
return R.string.help_url_adaptive_sleep;
}
// Get the maximum screen timeout as governed by admin and/or configuration.
// Returns the lowest timeout (admin/config) or Long.MAX_VALUE.
private Long getMaxScreenTimeout(Context context) {
Long adminMaxTimeout = Long.MAX_VALUE;
Long configMaxTimeout = Long.MAX_VALUE;
if (context == null) {
return Long.MAX_VALUE;
}
@@ -346,11 +357,21 @@ public class ScreenTimeoutSettings extends RadioButtonPickerFragment
if (dpm == null) {
return Long.MAX_VALUE;
}
mAdmin = RestrictedLockUtilsInternal.checkIfMaximumTimeToLockIsSet(context);
if (mAdmin != null) {
return dpm.getMaximumTimeToLock(null /* admin */, UserHandle.myUserId());
if (mAdmin == null) { // Don't overwrite mocked mAdmin
mAdmin = RestrictedLockUtilsInternal.checkIfMaximumTimeToLockIsSet(context);
}
return Long.MAX_VALUE;
if (mAdmin != null) {
// Get the admin max screen timeout
adminMaxTimeout = dpm.getMaximumTimeToLock(null /* admin */, UserHandle.myUserId());
}
try {
// Get the configurable max screen timeout
configMaxTimeout = Long.valueOf(
context.getResources().getInteger(R.integer.config_max_screen_timeout));
} catch (Resources.NotFoundException e) {
// Do nothing
}
return Math.min(adminMaxTimeout, configMaxTimeout);
}
private String getCurrentSystemScreenTimeout(Context context) {