1 /*
2  * Copyright (C) 2023 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 import android.provider.Settings;
21 
22 import androidx.preference.Preference;
23 
24 import com.android.settings.R;
25 import com.android.settings.core.TogglePreferenceController;
26 import com.android.settingslib.dream.DreamBackend;
27 
28 /**
29  * Controller for the {@link androidx.preference.SwitchPreference} which controls if dream
30  * overlays should be enabled.
31  */
32 public class DreamHomeControlsPreferenceController extends TogglePreferenceController {
33     private final DreamBackend mBackend;
34 
35     public static final String PREF_KEY = "dream_home_controls_toggle";
36 
DreamHomeControlsPreferenceController(Context context, DreamBackend dreamBackend)37     public DreamHomeControlsPreferenceController(Context context,
38             DreamBackend dreamBackend) {
39         super(context, PREF_KEY);
40         mBackend = dreamBackend;
41     }
42 
43     @Override
getAvailabilityStatus()44     public int getAvailabilityStatus() {
45         final boolean supported =
46                 mBackend.getSupportedComplications()
47                         .contains(DreamBackend.COMPLICATION_TYPE_HOME_CONTROLS);
48 
49         return controlsEnabledOnLockscreen() ? (supported ? AVAILABLE : CONDITIONALLY_UNAVAILABLE)
50                 : DISABLED_DEPENDENT_SETTING;
51     }
52 
53     @Override
updateState(Preference preference)54     public void updateState(Preference preference) {
55         super.updateState(preference);
56         refreshSummary(preference);
57     }
58 
59     @Override
isChecked()60     public boolean isChecked() {
61         return controlsEnabledOnLockscreen() && mBackend.getEnabledComplications().contains(
62                 DreamBackend.COMPLICATION_TYPE_HOME_CONTROLS);
63     }
64 
65     @Override
setChecked(boolean isChecked)66     public boolean setChecked(boolean isChecked) {
67         mBackend.setHomeControlsEnabled(isChecked);
68         return true;
69     }
70 
controlsEnabledOnLockscreen()71     private boolean controlsEnabledOnLockscreen() {
72         return Settings.Secure.getInt(
73                 mContext.getContentResolver(),
74                 Settings.Secure.LOCKSCREEN_SHOW_CONTROLS, 0) != 0;
75     }
76 
77     @Override
getSliceHighlightMenuRes()78     public int getSliceHighlightMenuRes() {
79         return R.string.menu_key_display;
80     }
81 }
82