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