1 /*
2  * Copyright (C) 2020 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 android.app;
18 
19 import static android.Manifest.permission.WRITE_SECURE_SETTINGS;
20 
21 import android.annotation.FlaggedApi;
22 import android.annotation.Nullable;
23 import android.annotation.RequiresPermission;
24 import android.annotation.SystemService;
25 import android.annotation.TestApi;
26 import android.annotation.UserHandleAware;
27 import android.content.ComponentName;
28 import android.content.Context;
29 import android.os.RemoteException;
30 import android.os.ServiceManager;
31 import android.os.UserHandle;
32 import android.provider.Settings;
33 import android.service.dreams.DreamService;
34 import android.service.dreams.Flags;
35 import android.service.dreams.IDreamManager;
36 
37 /**
38  * @hide
39  */
40 @SystemService(Context.DREAM_SERVICE)
41 @TestApi
42 public class DreamManager {
43     private final IDreamManager mService;
44     private final Context mContext;
45 
46     /**
47      * @hide
48      */
DreamManager(Context context)49     public DreamManager(Context context) throws ServiceManager.ServiceNotFoundException {
50         mService = IDreamManager.Stub.asInterface(
51                 ServiceManager.getServiceOrThrow(DreamService.DREAM_SERVICE));
52         mContext = context;
53     }
54 
55     /**
56      * Returns whether Settings.Secure.SCREENSAVER_ENABLED is enabled.
57      *
58      * @hide
59      */
60     @TestApi
isScreensaverEnabled()61     public boolean isScreensaverEnabled() {
62         return Settings.Secure.getIntForUser(mContext.getContentResolver(),
63                 Settings.Secure.SCREENSAVER_ENABLED, 0, UserHandle.USER_CURRENT) != 0;
64     }
65 
66     /**
67      * Sets whether Settings.Secure.SCREENSAVER_ENABLED is enabled.
68      *
69      * @hide
70      */
71     @TestApi
72     @RequiresPermission(WRITE_SECURE_SETTINGS)
setScreensaverEnabled(boolean enabled)73     public void setScreensaverEnabled(boolean enabled) {
74         Settings.Secure.putIntForUser(mContext.getContentResolver(),
75                 Settings.Secure.SCREENSAVER_ENABLED, enabled ? 1 : 0, UserHandle.USER_CURRENT);
76     }
77 
78     /**
79      * Returns whether dreams are supported.
80      *
81      * @hide
82      */
83     @TestApi
areDreamsSupported()84     public boolean areDreamsSupported() {
85         return mContext.getResources().getBoolean(
86                 com.android.internal.R.bool.config_dreamsSupported);
87     }
88 
89     /**
90      * Starts dreaming.
91      *
92      * The system dream component, if set by {@link DreamManager#setSystemDreamComponent}, will be
93      * started.
94      * Otherwise, starts the active dream set by {@link DreamManager#setActiveDream}.
95      *
96      * <p>This is only used for testing the dream service APIs.
97      *
98      * @see DreamManager#setActiveDream(ComponentName)
99      * @see DreamManager#setSystemDreamComponent(ComponentName)
100      *
101      * @hide
102      */
103     @TestApi
104     @UserHandleAware
105     @RequiresPermission(android.Manifest.permission.WRITE_DREAM_STATE)
startDream()106     public void startDream() {
107         try {
108             mService.dream();
109         } catch (RemoteException e) {
110             e.rethrowFromSystemServer();
111         }
112     }
113 
114     /**
115      * Stops the dream service on the device if one is started.
116      *
117      * <p> This is only used for testing the dream service APIs.
118      *
119      * @hide
120      */
121     @TestApi
122     @RequiresPermission(android.Manifest.permission.WRITE_DREAM_STATE)
stopDream()123     public void stopDream() {
124         try {
125             mService.awaken();
126         } catch (RemoteException e) {
127             e.rethrowFromSystemServer();
128         }
129     }
130 
131     /**
132      * Sets the active dream on the device to be "dreamComponent".
133      *
134      * <p>This is only used for testing the dream service APIs.
135      *
136      * @hide
137      */
138     @TestApi
139     @UserHandleAware
140     @RequiresPermission(android.Manifest.permission.WRITE_DREAM_STATE)
setActiveDream(@ullable ComponentName dreamComponent)141     public void setActiveDream(@Nullable ComponentName dreamComponent) {
142         ComponentName[] dreams = {dreamComponent};
143 
144         try {
145             mService.setDreamComponentsForUser(mContext.getUserId(),
146                     dreamComponent != null ? dreams : null);
147         } catch (RemoteException e) {
148             e.rethrowFromSystemServer();
149         }
150     }
151 
152     /**
153      * Sets or clears the system dream component.
154      *
155      * The system dream component, when set, will be shown instead of the user configured dream
156      * when the system starts dreaming (not dozing). If the system is dreaming at the time the
157      * system dream is set or cleared, it immediately switches dream.
158      *
159      * @hide
160      */
161     @TestApi
162     @RequiresPermission(android.Manifest.permission.WRITE_DREAM_STATE)
setSystemDreamComponent(@ullable ComponentName dreamComponent)163     public void setSystemDreamComponent(@Nullable ComponentName dreamComponent) {
164         try {
165             mService.setSystemDreamComponent(dreamComponent);
166         } catch (RemoteException e) {
167             throw e.rethrowFromSystemServer();
168         }
169     }
170 
171     /**
172      * Sets the active dream on the device to be "dreamComponent".
173      *
174      * <p>This is only used for testing the dream service APIs.
175      *
176      * @hide
177      */
178     @TestApi
179     @UserHandleAware
180     @RequiresPermission(android.Manifest.permission.WRITE_DREAM_STATE)
setDreamOverlay(@ullable ComponentName dreamOverlayComponent)181     public void setDreamOverlay(@Nullable ComponentName dreamOverlayComponent) {
182         try {
183             mService.registerDreamOverlayService(dreamOverlayComponent);
184         } catch (RemoteException e) {
185             e.rethrowFromSystemServer();
186         }
187     }
188 
189     /**
190      * Whether dreaming can start given user settings and the current dock/charge state.
191      *
192      * @hide
193      */
194     @UserHandleAware
195     @RequiresPermission(android.Manifest.permission.READ_DREAM_STATE)
canStartDreaming(boolean isScreenOn)196     public boolean canStartDreaming(boolean isScreenOn) {
197         try {
198             return mService.canStartDreaming(isScreenOn);
199         } catch (RemoteException e) {
200             e.rethrowFromSystemServer();
201         }
202         return false;
203     }
204 
205     /**
206      * Returns whether the device is Dreaming.
207      *
208      * <p> This is only used for testing the dream service APIs.
209      *
210      * @hide
211      */
212     @TestApi
213     @RequiresPermission(android.Manifest.permission.READ_DREAM_STATE)
isDreaming()214     public boolean isDreaming() {
215         try {
216             return mService.isDreaming();
217         } catch (RemoteException e) {
218             e.rethrowFromSystemServer();
219         }
220         return false;
221     }
222 
223     /**
224      * Sets whether the dream is obscured by something.
225      *
226      * @hide
227      */
228     @FlaggedApi(Flags.FLAG_DREAM_HANDLES_BEING_OBSCURED)
229     @RequiresPermission(android.Manifest.permission.WRITE_DREAM_STATE)
setDreamIsObscured(boolean isObscured)230     public void setDreamIsObscured(boolean isObscured) {
231         try {
232             mService.setDreamIsObscured(isObscured);
233         } catch (RemoteException e) {
234             throw e.rethrowFromSystemServer();
235         }
236     }
237 }
238