Home | History | Annotate | Download | only in display
      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.PreferenceController;
     26 import com.android.settingslib.RestrictedLockUtils;
     27 
     28 import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT;
     29 
     30 public class TimeoutPreferenceController extends PreferenceController implements
     31         Preference.OnPreferenceChangeListener {
     32 
     33     private static final String TAG = "TimeoutPrefContr";
     34 
     35     /** If there is no setting in the provider, use this. */
     36     public static final int FALLBACK_SCREEN_TIMEOUT_VALUE = 30000;
     37 
     38     private final String mScreenTimeoutKey;
     39 
     40     public TimeoutPreferenceController(Context context, String key) {
     41         super(context);
     42         mScreenTimeoutKey = key;
     43     }
     44 
     45     @Override
     46     public boolean isAvailable() {
     47         return true;
     48     }
     49 
     50     @Override
     51     public String getPreferenceKey() {
     52         return mScreenTimeoutKey;
     53     }
     54 
     55     @Override
     56     public void updateState(Preference preference) {
     57         final TimeoutListPreference timeoutListPreference = (TimeoutListPreference) preference;
     58         final long currentTimeout = Settings.System.getLong(mContext.getContentResolver(),
     59                 SCREEN_OFF_TIMEOUT, FALLBACK_SCREEN_TIMEOUT_VALUE);
     60         timeoutListPreference.setValue(String.valueOf(currentTimeout));
     61         final DevicePolicyManager dpm = (DevicePolicyManager) mContext.getSystemService(
     62                 Context.DEVICE_POLICY_SERVICE);
     63         if (dpm != null) {
     64             final RestrictedLockUtils.EnforcedAdmin admin =
     65                     RestrictedLockUtils.checkIfMaximumTimeToLockIsSet(mContext);
     66             final long maxTimeout =
     67                     dpm.getMaximumTimeToLockForUserAndProfiles(UserHandle.myUserId());
     68             timeoutListPreference.removeUnusableTimeouts(maxTimeout, admin);
     69         }
     70         updateTimeoutPreferenceDescription(timeoutListPreference, currentTimeout);
     71     }
     72 
     73     @Override
     74     public boolean onPreferenceChange(Preference preference, Object newValue) {
     75         try {
     76             int value = Integer.parseInt((String) newValue);
     77             Settings.System.putInt(mContext.getContentResolver(), SCREEN_OFF_TIMEOUT, value);
     78             updateTimeoutPreferenceDescription((TimeoutListPreference) preference, value);
     79         } catch (NumberFormatException e) {
     80             Log.e(TAG, "could not persist screen timeout setting", e);
     81         }
     82         return true;
     83     }
     84 
     85     public static CharSequence getTimeoutDescription(
     86             long currentTimeout, CharSequence[] entries, CharSequence[] values) {
     87         if (currentTimeout < 0 || entries == null || values == null
     88                 || values.length != entries.length) {
     89             return null;
     90         }
     91 
     92         for (int i = 0; i < values.length; i++) {
     93             long timeout = Long.parseLong(values[i].toString());
     94             if (currentTimeout == timeout) {
     95                 return entries[i];
     96             }
     97         }
     98         return null;
     99     }
    100 
    101     private void updateTimeoutPreferenceDescription(TimeoutListPreference preference,
    102             long currentTimeout) {
    103         final CharSequence[] entries = preference.getEntries();
    104         final CharSequence[] values = preference.getEntryValues();
    105         final String summary;
    106         if (preference.isDisabledByAdmin()) {
    107             summary = mContext.getString(com.android.settings.R.string.disabled_by_policy_title);
    108         } else {
    109             final CharSequence timeoutDescription = getTimeoutDescription(
    110                     currentTimeout, entries, values);
    111             summary = timeoutDescription == null
    112                     ? ""
    113                     : mContext.getString(R.string.screen_timeout_summary, timeoutDescription);
    114         }
    115         preference.setSummary(summary);
    116     }
    117 
    118 }
    119