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.hardware.display.ColorDisplayManager; 18 import android.hardware.display.NightDisplayListener; 19 20 import androidx.preference.Preference; 21 import androidx.preference.PreferenceScreen; 22 23 import com.android.settings.R; 24 import com.android.settings.core.TogglePreferenceController; 25 import com.android.settings.widget.PrimarySwitchPreference; 26 import com.android.settingslib.core.lifecycle.LifecycleObserver; 27 import com.android.settingslib.core.lifecycle.events.OnStart; 28 import com.android.settingslib.core.lifecycle.events.OnStop; 29 30 /** A controller can control the behavior of night display setting. */ 31 public class NightDisplayPreferenceController extends TogglePreferenceController 32 implements NightDisplayListener.Callback, LifecycleObserver, OnStart, OnStop { 33 34 private final ColorDisplayManager mColorDisplayManager; 35 private final NightDisplayListener mNightDisplayListener; 36 private final NightDisplayTimeFormatter mTimeFormatter; 37 private PrimarySwitchPreference mPreference; 38 NightDisplayPreferenceController(Context context, String key)39 public NightDisplayPreferenceController(Context context, String key) { 40 super(context, key); 41 42 mColorDisplayManager = context.getSystemService(ColorDisplayManager.class); 43 mNightDisplayListener = new NightDisplayListener(context); 44 mTimeFormatter = new NightDisplayTimeFormatter(context); 45 } 46 isSuggestionComplete(Context context)47 public static boolean isSuggestionComplete(Context context) { 48 final boolean isEnabled = context.getResources().getBoolean( 49 R.bool.config_night_light_suggestion_enabled); 50 // The suggestion is always complete if not enabled. 51 if (!isEnabled) { 52 return true; 53 } 54 final ColorDisplayManager manager = context.getSystemService(ColorDisplayManager.class); 55 return manager.getNightDisplayAutoMode() != ColorDisplayManager.AUTO_MODE_DISABLED; 56 } 57 58 @Override onStart()59 public void onStart() { 60 // Listen for changes only while attached. 61 mNightDisplayListener.setCallback(this); 62 } 63 64 @Override onStop()65 public void onStop() { 66 // Stop listening for state changes. 67 mNightDisplayListener.setCallback(null); 68 } 69 70 @Override displayPreference(PreferenceScreen screen)71 public void displayPreference(PreferenceScreen screen) { 72 super.displayPreference(screen); 73 mPreference = screen.findPreference(getPreferenceKey()); 74 } 75 76 @Override getAvailabilityStatus()77 public int getAvailabilityStatus() { 78 return ColorDisplayManager.isNightDisplayAvailable(mContext) 79 ? AVAILABLE : UNSUPPORTED_ON_DEVICE; 80 } 81 82 @Override isChecked()83 public boolean isChecked() { 84 return mColorDisplayManager.isNightDisplayActivated(); 85 } 86 87 @Override setChecked(boolean isChecked)88 public boolean setChecked(boolean isChecked) { 89 return mColorDisplayManager.setNightDisplayActivated(isChecked); 90 } 91 92 @Override updateState(Preference preference)93 public void updateState(Preference preference) { 94 super.updateState(preference); 95 preference.setSummary(mTimeFormatter.getAutoModeSummary(mContext, mColorDisplayManager)); 96 } 97 98 @Override onActivated(boolean activated)99 public void onActivated(boolean activated) { 100 updateState(mPreference); 101 } 102 }