1 /* 2 * Copyright (C) 2022 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 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.display.darkmode; 18 19 import static android.app.UiModeManager.MODE_NIGHT_CUSTOM_TYPE_BEDTIME; 20 21 import android.app.UiModeManager; 22 import android.content.Context; 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.settingslib.widget.FooterPreference; 30 31 /** Controller for the night mode bedtime custom mode footer. */ 32 public class DarkModeCustomBedtimePreferenceController extends BasePreferenceController { 33 private final UiModeManager mUiModeManager; 34 private FooterPreference mFooterPreference; 35 private BedtimeSettings mBedtimeSettings; 36 DarkModeCustomBedtimePreferenceController(Context context, String key)37 public DarkModeCustomBedtimePreferenceController(Context context, String key) { 38 super(context, key); 39 mUiModeManager = context.getSystemService(UiModeManager.class); 40 mBedtimeSettings = new BedtimeSettings(context); 41 } 42 43 @Override getAvailabilityStatus()44 public int getAvailabilityStatus() { 45 return mBedtimeSettings.getBedtimeSettingsIntent() == null 46 ? UNSUPPORTED_ON_DEVICE 47 : AVAILABLE_UNSEARCHABLE; 48 } 49 50 @Override displayPreference(PreferenceScreen screen)51 public void displayPreference(PreferenceScreen screen) { 52 super.displayPreference(screen); 53 mFooterPreference = screen.findPreference(getPreferenceKey()); 54 mFooterPreference.setLearnMoreAction( 55 v -> v.getContext().startActivity(mBedtimeSettings.getBedtimeSettingsIntent())); 56 mFooterPreference.setLearnMoreText( 57 mContext.getString(R.string.dark_ui_bedtime_footer_action)); 58 } 59 60 @Override updateState(Preference preference)61 public void updateState(Preference preference) { 62 if (mUiModeManager.getNightModeCustomType() != MODE_NIGHT_CUSTOM_TYPE_BEDTIME) { 63 preference.setVisible(false); 64 return; 65 } 66 preference.setVisible(true); 67 } 68 } 69