Night display setting should be toggleable
Based on Android S design, we want to let user can toggle night light in display settings page directly. 1. Use primary switch preference in xml file. 2. Add a switch toggle controler for its preference. 3. Move logic of night display preference to controler. 4. Remove the night display preference since we migrate the logic to controller. Test: Night display works as intented behavior. Bug: 174964139 Change-Id: Id920033cb22b1a9dd42c77c6fc2563ddae96bfcd
This commit is contained in:
@@ -1,83 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2016 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
|
||||
* except in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the
|
||||
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the specific language governing
|
||||
* permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.settings.display;
|
||||
|
||||
import android.content.Context;
|
||||
import android.hardware.display.ColorDisplayManager;
|
||||
import android.hardware.display.NightDisplayListener;
|
||||
import android.util.AttributeSet;
|
||||
|
||||
import androidx.preference.SwitchPreference;
|
||||
|
||||
import java.time.LocalTime;
|
||||
|
||||
public class NightDisplayPreference extends SwitchPreference
|
||||
implements NightDisplayListener.Callback {
|
||||
|
||||
private ColorDisplayManager mColorDisplayManager;
|
||||
private NightDisplayListener mNightDisplayListener;
|
||||
private NightDisplayTimeFormatter mTimeFormatter;
|
||||
|
||||
public NightDisplayPreference(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
|
||||
mColorDisplayManager = context.getSystemService(ColorDisplayManager.class);
|
||||
mNightDisplayListener = new NightDisplayListener(context);
|
||||
mTimeFormatter = new NightDisplayTimeFormatter(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAttached() {
|
||||
super.onAttached();
|
||||
|
||||
// Listen for changes only while attached.
|
||||
mNightDisplayListener.setCallback(this);
|
||||
|
||||
// Update the summary since the state may have changed while not attached.
|
||||
updateSummary();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDetached() {
|
||||
super.onDetached();
|
||||
|
||||
// Stop listening for state changes.
|
||||
mNightDisplayListener.setCallback(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivated(boolean activated) {
|
||||
updateSummary();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAutoModeChanged(int autoMode) {
|
||||
updateSummary();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCustomStartTimeChanged(LocalTime startTime) {
|
||||
updateSummary();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCustomEndTimeChanged(LocalTime endTime) {
|
||||
updateSummary();
|
||||
}
|
||||
|
||||
private void updateSummary() {
|
||||
setSummary(mTimeFormatter.getAutoModeTimeSummary(getContext(), mColorDisplayManager));
|
||||
}
|
||||
}
|
@@ -15,18 +15,30 @@ package com.android.settings.display;
|
||||
|
||||
import android.content.Context;
|
||||
import android.hardware.display.ColorDisplayManager;
|
||||
import android.hardware.display.NightDisplayListener;
|
||||
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.core.PreferenceControllerMixin;
|
||||
import com.android.settingslib.core.AbstractPreferenceController;
|
||||
import com.android.settings.core.TogglePreferenceController;
|
||||
import com.android.settings.widget.PrimarySwitchPreference;
|
||||
import com.android.settingslib.core.lifecycle.LifecycleObserver;
|
||||
import com.android.settingslib.core.lifecycle.events.OnStart;
|
||||
import com.android.settingslib.core.lifecycle.events.OnStop;
|
||||
|
||||
public class NightDisplayPreferenceController extends AbstractPreferenceController implements
|
||||
PreferenceControllerMixin {
|
||||
/** A controller can control the behavior of night display setting. */
|
||||
public class NightDisplayPreferenceController extends TogglePreferenceController
|
||||
implements NightDisplayListener.Callback, LifecycleObserver, OnStart, OnStop {
|
||||
|
||||
private static final String KEY_NIGHT_DISPLAY = "night_display";
|
||||
private final ColorDisplayManager mColorDisplayManager;
|
||||
private final NightDisplayListener mNightDisplayListener;
|
||||
private PrimarySwitchPreference mPreference;
|
||||
|
||||
public NightDisplayPreferenceController(Context context) {
|
||||
super(context);
|
||||
public NightDisplayPreferenceController(Context context, String key) {
|
||||
super(context, key);
|
||||
|
||||
mColorDisplayManager = context.getSystemService(ColorDisplayManager.class);
|
||||
mNightDisplayListener = new NightDisplayListener(context);
|
||||
}
|
||||
|
||||
public static boolean isSuggestionComplete(Context context) {
|
||||
@@ -41,12 +53,41 @@ public class NightDisplayPreferenceController extends AbstractPreferenceControll
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAvailable() {
|
||||
return ColorDisplayManager.isNightDisplayAvailable(mContext);
|
||||
public void onStart() {
|
||||
// Listen for changes only while attached.
|
||||
mNightDisplayListener.setCallback(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPreferenceKey() {
|
||||
return KEY_NIGHT_DISPLAY;
|
||||
public void onStop() {
|
||||
// Stop listening for state changes.
|
||||
mNightDisplayListener.setCallback(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void displayPreference(PreferenceScreen screen) {
|
||||
super.displayPreference(screen);
|
||||
mPreference = screen.findPreference(getPreferenceKey());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAvailabilityStatus() {
|
||||
return ColorDisplayManager.isNightDisplayAvailable(mContext)
|
||||
? AVAILABLE : UNSUPPORTED_ON_DEVICE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isChecked() {
|
||||
return mColorDisplayManager.isNightDisplayActivated();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean setChecked(boolean isChecked) {
|
||||
return mColorDisplayManager.setNightDisplayActivated(isChecked);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivated(boolean activated) {
|
||||
updateState(mPreference);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user