1 /* 2 * Copyright (C) 2020 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 distributed under the 11 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 12 * KIND, either express or implied. See the License for the specific language governing 13 * permissions and limitations under the License. 14 */ 15 16 package com.android.settings.display.darkmode; 17 18 import static android.app.UiModeManager.MODE_NIGHT_CUSTOM; 19 20 import android.app.TimePickerDialog; 21 import android.app.UiModeManager; 22 import android.content.Context; 23 import android.text.TextUtils; 24 25 import androidx.preference.Preference; 26 27 import com.android.settings.core.BasePreferenceController; 28 29 import java.time.LocalTime; 30 31 /** 32 * Controller for custom mode night mode time settings 33 */ 34 public class DarkModeCustomPreferenceController extends BasePreferenceController { 35 private static final String START_TIME_KEY = "dark_theme_start_time"; 36 private static final String END_TIME_KEY = "dark_theme_end_time"; 37 private final UiModeManager mUiModeManager; 38 private TimeFormatter mFormat; 39 private DarkModeSettingsFragment mFragmet; 40 DarkModeCustomPreferenceController(Context context, String key)41 public DarkModeCustomPreferenceController(Context context, String key) { 42 super(context, key); 43 mFormat = new TimeFormatter(mContext); 44 mUiModeManager = context.getSystemService(UiModeManager.class); 45 } 46 DarkModeCustomPreferenceController( Context context, String key, DarkModeSettingsFragment fragment)47 public DarkModeCustomPreferenceController( 48 Context context, String key, 49 DarkModeSettingsFragment fragment) { 50 this(context, key); 51 mFragmet = fragment; 52 } 53 DarkModeCustomPreferenceController( Context context, String key, DarkModeSettingsFragment fragment, TimeFormatter format)54 public DarkModeCustomPreferenceController( 55 Context context, String key, 56 DarkModeSettingsFragment fragment, 57 TimeFormatter format) { 58 this(context, key, fragment); 59 mFormat = format; 60 } 61 62 @Override getAvailabilityStatus()63 public int getAvailabilityStatus() { 64 return mUiModeManager.getNightMode() == MODE_NIGHT_CUSTOM 65 && mUiModeManager.getNightModeCustomType() 66 == UiModeManager.MODE_NIGHT_CUSTOM_TYPE_SCHEDULE 67 ? AVAILABLE : CONDITIONALLY_UNAVAILABLE; 68 } 69 getDialog()70 public TimePickerDialog getDialog() { 71 final LocalTime initialTime; 72 if (TextUtils.equals(getPreferenceKey(), START_TIME_KEY)) { 73 initialTime = mUiModeManager.getCustomNightModeStart(); 74 } else { 75 initialTime = mUiModeManager.getCustomNightModeEnd(); 76 } 77 return new TimePickerDialog(mContext, (view, hourOfDay, minute) -> { 78 final LocalTime time = LocalTime.of(hourOfDay, minute); 79 if (TextUtils.equals(getPreferenceKey(), START_TIME_KEY)) { 80 mUiModeManager.setCustomNightModeStart(time); 81 } else { 82 mUiModeManager.setCustomNightModeEnd(time); 83 } 84 if (mFragmet != null) { 85 mFragmet.refresh(); 86 } 87 }, initialTime.getHour(), initialTime.getMinute(), mFormat.is24HourFormat()); 88 } 89 90 @Override refreshSummary(Preference preference)91 protected void refreshSummary(Preference preference) { 92 final LocalTime time; 93 if (TextUtils.equals(getPreferenceKey(), START_TIME_KEY)) { 94 time = mUiModeManager.getCustomNightModeStart(); 95 } else { 96 time = mUiModeManager.getCustomNightModeEnd(); 97 } 98 preference.setSummary(mFormat.of(time)); 99 } 100 } 101