1 /* 2 * Copyright (C) 2019 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 android.app.UiModeManager; 19 import android.content.Context; 20 import android.content.res.Configuration; 21 import android.widget.CompoundButton; 22 import android.widget.CompoundButton.OnCheckedChangeListener; 23 24 import androidx.preference.Preference; 25 import androidx.preference.PreferenceScreen; 26 27 import com.android.settings.R; 28 import com.android.settings.core.BasePreferenceController; 29 import com.android.settings.overlay.FeatureFactory; 30 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; 31 import com.android.settingslib.widget.MainSwitchPreference; 32 33 import java.time.LocalTime; 34 35 /** 36 * Controller for activate/deactivate night mode button 37 */ 38 public class DarkModeActivationPreferenceController extends BasePreferenceController implements 39 OnCheckedChangeListener { 40 41 private final UiModeManager mUiModeManager; 42 private final MetricsFeatureProvider mMetricsFeatureProvider; 43 private TimeFormatter mFormat; 44 private MainSwitchPreference mPreference; 45 DarkModeActivationPreferenceController(Context context, String preferenceKey)46 public DarkModeActivationPreferenceController(Context context, String preferenceKey) { 47 super(context, preferenceKey); 48 mUiModeManager = context.getSystemService(UiModeManager.class); 49 mFormat = new TimeFormatter(context); 50 mMetricsFeatureProvider = FeatureFactory.getFeatureFactory().getMetricsFeatureProvider(); 51 } 52 DarkModeActivationPreferenceController(Context context, String preferenceKey, TimeFormatter f)53 public DarkModeActivationPreferenceController(Context context, String preferenceKey, 54 TimeFormatter f) { 55 this(context, preferenceKey); 56 mFormat = f; 57 } 58 59 @Override updateState(Preference preference)60 public final void updateState(Preference preference) { 61 final boolean active = (mContext.getResources().getConfiguration().uiMode 62 & Configuration.UI_MODE_NIGHT_YES) != 0; 63 mPreference.updateStatus(active); 64 } 65 66 @Override getSummary()67 public CharSequence getSummary() { 68 final boolean isActivated = (mContext.getResources().getConfiguration().uiMode 69 & Configuration.UI_MODE_NIGHT_YES) != 0; 70 final int mode = mUiModeManager.getNightMode(); 71 if (mode == UiModeManager.MODE_NIGHT_AUTO) { 72 return mContext.getString(isActivated 73 ? R.string.dark_ui_summary_on_auto_mode_auto 74 : R.string.dark_ui_summary_off_auto_mode_auto); 75 } else if (mode == UiModeManager.MODE_NIGHT_CUSTOM) { 76 if (mUiModeManager.getNightModeCustomType() 77 == UiModeManager.MODE_NIGHT_CUSTOM_TYPE_BEDTIME) { 78 return mContext.getString(isActivated 79 ? R.string.dark_ui_summary_on_auto_mode_custom_bedtime 80 : R.string.dark_ui_summary_off_auto_mode_custom_bedtime); 81 } 82 83 final LocalTime time = isActivated 84 ? mUiModeManager.getCustomNightModeEnd() 85 : mUiModeManager.getCustomNightModeStart(); 86 final String timeStr = mFormat.of(time); 87 88 return mContext.getString(isActivated 89 ? R.string.dark_ui_summary_on_auto_mode_custom 90 : R.string.dark_ui_summary_off_auto_mode_custom, timeStr); 91 } else { 92 return mContext.getString(isActivated 93 ? R.string.dark_ui_summary_on_auto_mode_never 94 : R.string.dark_ui_summary_off_auto_mode_never); 95 } 96 } 97 98 @Override onCheckedChanged(CompoundButton buttonView, boolean isChecked)99 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 100 mMetricsFeatureProvider.logClickedPreference(mPreference, getMetricsCategory()); 101 final boolean active = (mContext.getResources().getConfiguration().uiMode 102 & Configuration.UI_MODE_NIGHT_YES) != 0; 103 mUiModeManager.setNightModeActivated(!active); 104 } 105 106 @Override displayPreference(PreferenceScreen screen)107 public void displayPreference(PreferenceScreen screen) { 108 super.displayPreference(screen); 109 mPreference = (MainSwitchPreference) screen.findPreference(getPreferenceKey()); 110 mPreference.addOnSwitchChangeListener(this); 111 } 112 113 @Override getAvailabilityStatus()114 public int getAvailabilityStatus() { 115 return AVAILABLE_UNSEARCHABLE; 116 } 117 } 118