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.dream;
18 
19 import static androidx.lifecycle.Lifecycle.Event.ON_START;
20 import static androidx.lifecycle.Lifecycle.Event.ON_STOP;
21 
22 import android.content.Context;
23 import android.database.ContentObserver;
24 import android.os.Handler;
25 import android.os.Looper;
26 import android.provider.Settings;
27 
28 import androidx.lifecycle.OnLifecycleEvent;
29 
30 import com.android.settings.widget.SettingsMainSwitchPreferenceController;
31 import com.android.settingslib.core.lifecycle.LifecycleObserver;
32 import com.android.settingslib.dream.DreamBackend;
33 
34 /**
35  * Preference controller for switching dreams on/off.
36  */
37 public class DreamMainSwitchPreferenceController extends
38         SettingsMainSwitchPreferenceController implements LifecycleObserver {
39     static final String MAIN_SWITCH_PREF_KEY = "dream_main_settings_switch";
40     private final DreamBackend mBackend;
41 
42     private final ContentObserver mObserver = new ContentObserver(
43             new Handler(Looper.getMainLooper())) {
44         @Override
45         public void onChange(boolean selfChange) {
46             updateState(mSwitchPreference);
47         }
48     };
49 
DreamMainSwitchPreferenceController(Context context, String key)50     public DreamMainSwitchPreferenceController(Context context, String key) {
51         super(context, key);
52         mBackend = DreamBackend.getInstance(context);
53     }
54 
55     @Override
getAvailabilityStatus()56     public int getAvailabilityStatus() {
57         return AVAILABLE;
58     }
59 
60     @Override
isChecked()61     public boolean isChecked() {
62         return mBackend.isEnabled();
63     }
64 
65     @Override
setChecked(boolean isChecked)66     public boolean setChecked(boolean isChecked) {
67         mBackend.setEnabled(isChecked);
68         return true;
69     }
70 
71     @Override
isSliceable()72     public boolean isSliceable() {
73         return false;
74     }
75 
76     @Override
getSliceHighlightMenuRes()77     public int getSliceHighlightMenuRes() {
78         // not needed since it's not sliceable
79         return NO_RES;
80     }
81 
82     @OnLifecycleEvent(ON_START)
onStart()83     void onStart() {
84         mContext.getContentResolver().registerContentObserver(
85                 Settings.Secure.getUriFor(Settings.Secure.SCREENSAVER_ENABLED),
86                 /* notifyForDescendants= */ false, mObserver);
87     }
88 
89     @OnLifecycleEvent(ON_STOP)
onStop()90     void onStop() {
91         mContext.getContentResolver().unregisterContentObserver(mObserver);
92     }
93 }
94