1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14 package com.android.settings.display; 15 16 import android.app.admin.DevicePolicyManager; 17 import android.content.Context; 18 import android.os.UserHandle; 19 import android.provider.Settings; 20 import android.support.v7.preference.Preference; 21 import android.util.Log; 22 23 import com.android.settings.R; 24 import com.android.settings.TimeoutListPreference; 25 import com.android.settings.core.PreferenceControllerMixin; 26 import com.android.settingslib.RestrictedLockUtils; 27 import com.android.settingslib.core.AbstractPreferenceController; 28 29 import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT; 30 31 public class TimeoutPreferenceController extends AbstractPreferenceController implements 32 PreferenceControllerMixin, Preference.OnPreferenceChangeListener { 33 34 private static final String TAG = "TimeoutPrefContr"; 35 36 /** If there is no setting in the provider, use this. */ 37 public static final int FALLBACK_SCREEN_TIMEOUT_VALUE = 30000; 38 39 private final String mScreenTimeoutKey; 40 41 public TimeoutPreferenceController(Context context, String key) { 42 super(context); 43 mScreenTimeoutKey = key; 44 } 45 46 @Override 47 public boolean isAvailable() { 48 return true; 49 } 50 51 @Override 52 public String getPreferenceKey() { 53 return mScreenTimeoutKey; 54 } 55 56 @Override 57 public void updateState(Preference preference) { 58 final TimeoutListPreference timeoutListPreference = (TimeoutListPreference) preference; 59 final long currentTimeout = Settings.System.getLong(mContext.getContentResolver(), 60 SCREEN_OFF_TIMEOUT, FALLBACK_SCREEN_TIMEOUT_VALUE); 61 timeoutListPreference.setValue(String.valueOf(currentTimeout)); 62 final DevicePolicyManager dpm = (DevicePolicyManager) mContext.getSystemService( 63 Context.DEVICE_POLICY_SERVICE); 64 if (dpm != null) { 65 final RestrictedLockUtils.EnforcedAdmin admin = 66 RestrictedLockUtils.checkIfMaximumTimeToLockIsSet(mContext); 67 final long maxTimeout = 68 dpm.getMaximumTimeToLockForUserAndProfiles(UserHandle.myUserId()); 69 timeoutListPreference.removeUnusableTimeouts(maxTimeout, admin); 70 } 71 updateTimeoutPreferenceDescription(timeoutListPreference, currentTimeout); 72 } 73 74 @Override 75 public boolean onPreferenceChange(Preference preference, Object newValue) { 76 try { 77 int value = Integer.parseInt((String) newValue); 78 Settings.System.putInt(mContext.getContentResolver(), SCREEN_OFF_TIMEOUT, value); 79 updateTimeoutPreferenceDescription((TimeoutListPreference) preference, value); 80 } catch (NumberFormatException e) { 81 Log.e(TAG, "could not persist screen timeout setting", e); 82 } 83 return true; 84 } 85 86 public static CharSequence getTimeoutDescription( 87 long currentTimeout, CharSequence[] entries, CharSequence[] values) { 88 if (currentTimeout < 0 || entries == null || values == null 89 || values.length != entries.length) { 90 return null; 91 } 92 93 for (int i = 0; i < values.length; i++) { 94 long timeout = Long.parseLong(values[i].toString()); 95 if (currentTimeout == timeout) { 96 return entries[i]; 97 } 98 } 99 return null; 100 } 101 102 private void updateTimeoutPreferenceDescription(TimeoutListPreference preference, 103 long currentTimeout) { 104 final CharSequence[] entries = preference.getEntries(); 105 final CharSequence[] values = preference.getEntryValues(); 106 final String summary; 107 if (preference.isDisabledByAdmin()) { 108 summary = mContext.getString(com.android.settings.R.string.disabled_by_policy_title); 109 } else { 110 final CharSequence timeoutDescription = getTimeoutDescription( 111 currentTimeout, entries, values); 112 summary = timeoutDescription == null 113 ? "" 114 : mContext.getString(R.string.screen_timeout_summary, timeoutDescription); 115 } 116 preference.setSummary(summary); 117 } 118 119 } 120