1 /* 2 * Copyright (C) 2017 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 package com.android.settings.display; 17 18 import android.content.Context; 19 import android.hardware.display.AmbientDisplayConfiguration; 20 import android.os.UserHandle; 21 import android.provider.Settings; 22 import android.text.TextUtils; 23 24 import com.android.settings.core.TogglePreferenceController; 25 26 public class AmbientDisplayAlwaysOnPreferenceController extends TogglePreferenceController { 27 28 private final int ON = 1; 29 private final int OFF = 0; 30 31 private static final int MY_USER = UserHandle.myUserId(); 32 33 private AmbientDisplayConfiguration mConfig; 34 private OnPreferenceChangedCallback mCallback; 35 36 public interface OnPreferenceChangedCallback { onPreferenceChanged()37 void onPreferenceChanged(); 38 } 39 AmbientDisplayAlwaysOnPreferenceController(Context context, String key)40 public AmbientDisplayAlwaysOnPreferenceController(Context context, String key) { 41 super(context, key); 42 } 43 44 @Override getAvailabilityStatus()45 public int getAvailabilityStatus() { 46 return isAvailable(getConfig()) ? AVAILABLE : UNSUPPORTED_ON_DEVICE; 47 } 48 49 @Override isSliceable()50 public boolean isSliceable() { 51 return TextUtils.equals(getPreferenceKey(), "ambient_display_always_on"); 52 } 53 54 @Override isChecked()55 public boolean isChecked() { 56 return getConfig().alwaysOnEnabled(MY_USER); 57 } 58 59 @Override setChecked(boolean isChecked)60 public boolean setChecked(boolean isChecked) { 61 int enabled = isChecked ? ON : OFF; 62 Settings.Secure.putInt( 63 mContext.getContentResolver(), Settings.Secure.DOZE_ALWAYS_ON, enabled); 64 if (mCallback != null) { 65 mCallback.onPreferenceChanged(); 66 } 67 return true; 68 } 69 setConfig( AmbientDisplayConfiguration config)70 public AmbientDisplayAlwaysOnPreferenceController setConfig( 71 AmbientDisplayConfiguration config) { 72 mConfig = config; 73 return this; 74 } 75 setCallback( OnPreferenceChangedCallback callback)76 public AmbientDisplayAlwaysOnPreferenceController setCallback( 77 OnPreferenceChangedCallback callback) { 78 mCallback = callback; 79 return this; 80 } 81 isAvailable(AmbientDisplayConfiguration config)82 public static boolean isAvailable(AmbientDisplayConfiguration config) { 83 return config.alwaysOnAvailableForUser(MY_USER); 84 } 85 getConfig()86 private AmbientDisplayConfiguration getConfig() { 87 if (mConfig == null) { 88 mConfig = new AmbientDisplayConfiguration(mContext); 89 } 90 return mConfig; 91 } 92 } 93