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