Also fix some test cases.
I'd prefer using Robolectric shadows rather than mocking Android
framework classes. However, I can't do that in this CL due to the
lack of multiple shadow classes support.
Test: make RunSettingsRoboTests -j56
ROBOTEST_FILTER=com.android.settings.display.darkmode.*
Bug: 215182463
Change-Id: If557d2933927a5dd0fadd3f6db6bb6f0ab7dd5ee
107 lines
3.8 KiB
Java
107 lines
3.8 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;
|
|
import java.time.format.DateTimeFormatter;
|
|
|
|
/**
|
|
* 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";
|
|
public static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("hh:mm a");
|
|
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 AVAILABLE;
|
|
}
|
|
|
|
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) {
|
|
if (mUiModeManager.getNightMode() != MODE_NIGHT_CUSTOM
|
|
|| mUiModeManager.getNightModeCustomType()
|
|
!= UiModeManager.MODE_NIGHT_CUSTOM_TYPE_SCHEDULE) {
|
|
preference.setVisible(false);
|
|
return;
|
|
}
|
|
preference.setVisible(true);
|
|
final LocalTime time;
|
|
if (TextUtils.equals(getPreferenceKey(), START_TIME_KEY)) {
|
|
time = mUiModeManager.getCustomNightModeStart();
|
|
} else {
|
|
time = mUiModeManager.getCustomNightModeEnd();
|
|
}
|
|
preference.setSummary(mFormat.of(time));
|
|
}
|
|
}
|