Currently, when schedule sets to "Turns on at bedtime", the footer will show a slid up animation when entering the page, this is because the "Start time" & "End time" preferences are hidden in onResume(). This is because these 2 preferences always return AVAILABLE in getAvailabilityStatus(), and manually update visibility in refreshSummary(), which is called each time updateState() is called. Usually the controller not set the visibility explicitly, but return CONDITIONALLY_UNAVAILABLE in getAvailabilityStatus() when they want to hide the preference. Because getAvailabilityStatus() is called in onCreate(), by using this, we can fix the flicker. Fix: 234399017 Test: visual & robo test Change-Id: I4cb7dd95d2985bd1ca4c8cb30aaebdc21a5415f8
101 lines
3.5 KiB
Java
101 lines
3.5 KiB
Java
/*
|
|
* Copyright (C) 2020 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.darkmode;
|
|
|
|
import static android.app.UiModeManager.MODE_NIGHT_CUSTOM;
|
|
|
|
import android.app.TimePickerDialog;
|
|
import android.app.UiModeManager;
|
|
import android.content.Context;
|
|
import android.text.TextUtils;
|
|
|
|
import androidx.preference.Preference;
|
|
|
|
import com.android.settings.core.BasePreferenceController;
|
|
|
|
import java.time.LocalTime;
|
|
|
|
/**
|
|
* Controller for custom mode night mode time settings
|
|
*/
|
|
public class DarkModeCustomPreferenceController extends BasePreferenceController {
|
|
private static final String START_TIME_KEY = "dark_theme_start_time";
|
|
private static final String END_TIME_KEY = "dark_theme_end_time";
|
|
private final UiModeManager mUiModeManager;
|
|
private TimeFormatter mFormat;
|
|
private DarkModeSettingsFragment mFragmet;
|
|
|
|
public DarkModeCustomPreferenceController(Context context, String key) {
|
|
super(context, key);
|
|
mFormat = new TimeFormatter(mContext);
|
|
mUiModeManager = context.getSystemService(UiModeManager.class);
|
|
}
|
|
|
|
public DarkModeCustomPreferenceController(
|
|
Context context, String key,
|
|
DarkModeSettingsFragment fragment) {
|
|
this(context, key);
|
|
mFragmet = fragment;
|
|
}
|
|
|
|
public DarkModeCustomPreferenceController(
|
|
Context context, String key,
|
|
DarkModeSettingsFragment fragment,
|
|
TimeFormatter format) {
|
|
this(context, key, fragment);
|
|
mFormat = format;
|
|
}
|
|
|
|
@Override
|
|
public int getAvailabilityStatus() {
|
|
return mUiModeManager.getNightMode() == MODE_NIGHT_CUSTOM
|
|
&& mUiModeManager.getNightModeCustomType()
|
|
== UiModeManager.MODE_NIGHT_CUSTOM_TYPE_SCHEDULE
|
|
? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
|
|
}
|
|
|
|
public TimePickerDialog getDialog() {
|
|
final LocalTime initialTime;
|
|
if (TextUtils.equals(getPreferenceKey(), START_TIME_KEY)) {
|
|
initialTime = mUiModeManager.getCustomNightModeStart();
|
|
} else {
|
|
initialTime = mUiModeManager.getCustomNightModeEnd();
|
|
}
|
|
return new TimePickerDialog(mContext, (view, hourOfDay, minute) -> {
|
|
final LocalTime time = LocalTime.of(hourOfDay, minute);
|
|
if (TextUtils.equals(getPreferenceKey(), START_TIME_KEY)) {
|
|
mUiModeManager.setCustomNightModeStart(time);
|
|
} else {
|
|
mUiModeManager.setCustomNightModeEnd(time);
|
|
}
|
|
if (mFragmet != null) {
|
|
mFragmet.refresh();
|
|
}
|
|
}, initialTime.getHour(), initialTime.getMinute(), mFormat.is24HourFormat());
|
|
}
|
|
|
|
@Override
|
|
protected void refreshSummary(Preference preference) {
|
|
final LocalTime time;
|
|
if (TextUtils.equals(getPreferenceKey(), START_TIME_KEY)) {
|
|
time = mUiModeManager.getCustomNightModeStart();
|
|
} else {
|
|
time = mUiModeManager.getCustomNightModeEnd();
|
|
}
|
|
preference.setSummary(mFormat.of(time));
|
|
}
|
|
}
|