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.content.Context; 17 import android.os.Build; 18 import android.os.SystemProperties; 19 import android.provider.Settings; 20 import android.support.v14.preference.SwitchPreference; 21 import android.support.v7.preference.Preference; 22 import android.text.TextUtils; 23 24 import com.android.settings.core.PreferenceController; 25 import com.android.settings.core.instrumentation.MetricsFeatureProvider; 26 import com.android.settings.overlay.FeatureFactory; 27 28 import static android.provider.Settings.Secure.DOZE_ENABLED; 29 import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.ACTION_AMBIENT_DISPLAY; 30 31 public class DozePreferenceController extends PreferenceController implements 32 Preference.OnPreferenceChangeListener { 33 34 private static final String KEY_DOZE = "doze"; 35 36 private final MetricsFeatureProvider mMetricsFeatureProvider; 37 DozePreferenceController(Context context)38 public DozePreferenceController(Context context) { 39 super(context); 40 mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider(); 41 } 42 43 @Override getPreferenceKey()44 public String getPreferenceKey() { 45 return KEY_DOZE; 46 } 47 48 @Override handlePreferenceTreeClick(Preference preference)49 public boolean handlePreferenceTreeClick(Preference preference) { 50 if (KEY_DOZE.equals(preference.getKey())) { 51 mMetricsFeatureProvider.action(mContext, ACTION_AMBIENT_DISPLAY); 52 } 53 return false; 54 } 55 56 @Override updateState(Preference preference)57 public void updateState(Preference preference) { 58 int value = Settings.Secure.getInt(mContext.getContentResolver(), DOZE_ENABLED, 1); 59 ((SwitchPreference) preference).setChecked(value != 0); 60 } 61 62 @Override onPreferenceChange(Preference preference, Object newValue)63 public boolean onPreferenceChange(Preference preference, Object newValue) { 64 boolean value = (Boolean) newValue; 65 Settings.Secure.putInt(mContext.getContentResolver(), DOZE_ENABLED, value ? 1 : 0); 66 return true; 67 } 68 69 @Override isAvailable()70 public boolean isAvailable() { 71 String name = Build.IS_DEBUGGABLE ? SystemProperties.get("debug.doze.component") : null; 72 if (TextUtils.isEmpty(name)) { 73 name = mContext.getResources().getString( 74 com.android.internal.R.string.config_dozeComponent); 75 } 76 return !TextUtils.isEmpty(name); 77 } 78 } 79