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 android.annotation.NonNull;
20 import android.annotation.RequiresPermission;
21 import android.annotation.SystemService;
22 import android.annotation.TestApi;
23 import android.annotation.UserHandleAware;
24 import android.content.ComponentName;
25 import android.content.Context;
26 import android.os.RemoteException;
27 import android.os.ServiceManager;
28 import android.service.dreams.DreamService;
29 import android.service.dreams.IDreamManager;
30 
31 /**
32  * @hide
33  */
34 @SystemService(Context.DREAM_SERVICE)
35 @TestApi
36 public class DreamManager {
37     private final IDreamManager mService;
38     private final Context mContext;
39 
40     /**
41      * @hide
42      */
DreamManager(Context context)43     public DreamManager(Context context) throws ServiceManager.ServiceNotFoundException {
44         mService = IDreamManager.Stub.asInterface(
45                 ServiceManager.getServiceOrThrow(DreamService.DREAM_SERVICE));
46         mContext = context;
47     }
48 
49     /**
50      * Starts dream service with name "name".
51      *
52      * <p>This is only used for testing the dream service APIs.
53      *
54      * @hide
55      */
56     @TestApi
57     @UserHandleAware
58     @RequiresPermission(android.Manifest.permission.WRITE_DREAM_STATE)
startDream(@onNull ComponentName name)59     public void startDream(@NonNull ComponentName name) {
60         try {
61             mService.dream();
62         } catch (RemoteException e) {
63             e.rethrowFromSystemServer();
64         }
65     }
66 
67     /**
68      * Stops the dream service on the device if one is started.
69      *
70      * <p> This is only used for testing the dream service APIs.
71      *
72      * @hide
73      */
74     @TestApi
75     @RequiresPermission(android.Manifest.permission.WRITE_DREAM_STATE)
stopDream()76     public void stopDream() {
77         try {
78             mService.awaken();
79         } catch (RemoteException e) {
80             e.rethrowFromSystemServer();
81         }
82     }
83 
84     /**
85      * Sets the active dream on the device to be "dreamComponent".
86      *
87      * <p>This is only used for testing the dream service APIs.
88      *
89      * @hide
90      */
91     @TestApi
92     @UserHandleAware
93     @RequiresPermission(android.Manifest.permission.WRITE_DREAM_STATE)
setActiveDream(@onNull ComponentName dreamComponent)94     public void setActiveDream(@NonNull ComponentName dreamComponent) {
95         ComponentName[] dreams = {dreamComponent};
96         try {
97             mService.setDreamComponentsForUser(mContext.getUserId(), dreams);
98         } catch (RemoteException e) {
99             e.rethrowFromSystemServer();
100         }
101     }
102 
103     /**
104      * Returns whether the device is Dreaming.
105      *
106      * <p> This is only used for testing the dream service APIs.
107      *
108      * @hide
109      */
110     @TestApi
111     @RequiresPermission(android.Manifest.permission.READ_DREAM_STATE)
isDreaming()112     public boolean isDreaming() {
113         try {
114             return mService.isDreaming();
115         } catch (RemoteException e) {
116             e.rethrowFromSystemServer();
117         }
118         return false;
119     }
120 }
121