1 /*
2  * Copyright (C) 2017 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.dream;
18 
19 import android.content.Context;
20 
21 import androidx.preference.Preference;
22 
23 import com.android.internal.annotations.VisibleForTesting;
24 import com.android.settings.R;
25 import com.android.settings.core.PreferenceControllerMixin;
26 import com.android.settings.display.AmbientDisplayAlwaysOnPreferenceController;
27 import com.android.settingslib.core.AbstractPreferenceController;
28 import com.android.settingslib.dream.DreamBackend;
29 
30 public class WhenToDreamPreferenceController extends AbstractPreferenceController implements
31         PreferenceControllerMixin {
32 
33     private static final String WHEN_TO_START = "when_to_start";
34     private final DreamBackend mBackend;
35     private final boolean mDreamsDisabledByAmbientModeSuppression;
36     private final boolean mDreamsEnabledOnBattery;
37 
WhenToDreamPreferenceController(Context context)38     WhenToDreamPreferenceController(Context context) {
39         this(context, context.getResources().getBoolean(
40                 com.android.internal.R.bool.config_dreamsDisabledByAmbientModeSuppressionConfig),
41                 context.getResources().getBoolean(
42                         com.android.internal.R.bool.config_dreamsEnabledOnBattery));
43     }
44 
45     @VisibleForTesting
WhenToDreamPreferenceController(Context context, boolean dreamsDisabledByAmbientModeSuppression, boolean dreamsEnabledOnBattery)46     WhenToDreamPreferenceController(Context context,
47             boolean dreamsDisabledByAmbientModeSuppression,
48             boolean dreamsEnabledOnBattery) {
49         super(context);
50 
51         mBackend = DreamBackend.getInstance(context);
52         mDreamsDisabledByAmbientModeSuppression = dreamsDisabledByAmbientModeSuppression;
53         mDreamsEnabledOnBattery = dreamsEnabledOnBattery;
54     }
55 
56     @Override
updateState(Preference preference)57     public void updateState(Preference preference) {
58         super.updateState(preference);
59 
60         if (mDreamsDisabledByAmbientModeSuppression
61                 && AmbientDisplayAlwaysOnPreferenceController.isAodSuppressedByBedtime(mContext)) {
62             preference.setSummary(R.string.screensaver_settings_when_to_dream_bedtime);
63         } else {
64             final int resId = DreamSettings.getDreamSettingDescriptionResId(
65                     mBackend.getWhenToDreamSetting(), mDreamsEnabledOnBattery);
66             preference.setSummary(resId);
67         }
68     }
69 
70     @Override
isAvailable()71     public boolean isAvailable() {
72         return true;
73     }
74 
75     @Override
getPreferenceKey()76     public String getPreferenceKey() {
77         return WHEN_TO_START;
78     }
79 }
80