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 import android.content.Intent;
24 
25 import androidx.preference.Preference;
26 import androidx.preference.PreferenceScreen;
27 
28 import com.android.settings.R;
29 import com.android.settings.core.BasePreferenceController;
30 import com.android.settingslib.widget.FooterPreference;
31 
32 /** Controller for the night mode bedtime custom mode footer. */
33 public class DarkModeCustomBedtimePreferenceController extends BasePreferenceController {
34     private final UiModeManager mUiModeManager;
35     private FooterPreference mFooterPreference;
36     private BedtimeSettings mBedtimeSettings;
37 
DarkModeCustomBedtimePreferenceController(Context context, String key)38     public DarkModeCustomBedtimePreferenceController(Context context, String key) {
39         super(context, key);
40         mUiModeManager = context.getSystemService(UiModeManager.class);
41         mBedtimeSettings = new BedtimeSettings(context);
42     }
43 
44     @Override
getAvailabilityStatus()45     public int getAvailabilityStatus() {
46         return mBedtimeSettings.getBedtimeSettingsIntent() == null
47                 ? UNSUPPORTED_ON_DEVICE
48                 : AVAILABLE_UNSEARCHABLE;
49     }
50 
51     @Override
displayPreference(PreferenceScreen screen)52     public void displayPreference(PreferenceScreen screen) {
53         super.displayPreference(screen);
54         mFooterPreference = screen.findPreference(getPreferenceKey());
55         mFooterPreference.setLearnMoreAction(
56                 v -> {
57                     Intent bedtimeSettingsIntent = mBedtimeSettings.getBedtimeSettingsIntent();
58                     if (bedtimeSettingsIntent != null) {
59                         v.getContext().startActivity(bedtimeSettingsIntent);
60                     }
61                 });
62         mFooterPreference.setLearnMoreText(
63                 mContext.getString(R.string.dark_ui_bedtime_footer_action));
64     }
65 
66     @Override
updateState(Preference preference)67     public void updateState(Preference preference) {
68         if (mUiModeManager.getNightModeCustomType() != MODE_NIGHT_CUSTOM_TYPE_BEDTIME) {
69             preference.setVisible(false);
70             return;
71         }
72         preference.setVisible(true);
73     }
74 }
75