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 * This API is equivalent to {@link DreamManager#startDream()} but with a nullable component 91 * name to be compatible with TM CTS tests. 92 * 93 * <p>This is only used for testing the dream service APIs. 94 * 95 * @see DreamManager#startDream() 96 * 97 * @hide 98 */ 99 @TestApi 100 @UserHandleAware 101 @RequiresPermission(android.Manifest.permission.WRITE_DREAM_STATE) startDream(@ullable ComponentName name)102 public void startDream(@Nullable ComponentName name) { 103 startDream(); 104 } 105 106 /** 107 * Starts dreaming. 108 * 109 * The system dream component, if set by {@link DreamManager#setSystemDreamComponent}, will be 110 * started. 111 * Otherwise, starts the active dream set by {@link DreamManager#setActiveDream}. 112 * 113 * <p>This is only used for testing the dream service APIs. 114 * 115 * @see DreamManager#setActiveDream(ComponentName) 116 * @see DreamManager#setSystemDreamComponent(ComponentName) 117 * 118 * @hide 119 */ 120 @TestApi 121 @UserHandleAware 122 @RequiresPermission(android.Manifest.permission.WRITE_DREAM_STATE) startDream()123 public void startDream() { 124 try { 125 mService.dream(); 126 } catch (RemoteException e) { 127 e.rethrowFromSystemServer(); 128 } 129 } 130 131 /** 132 * Stops the dream service on the device if one is started. 133 * 134 * <p> This is only used for testing the dream service APIs. 135 * 136 * @hide 137 */ 138 @TestApi 139 @RequiresPermission(android.Manifest.permission.WRITE_DREAM_STATE) stopDream()140 public void stopDream() { 141 try { 142 mService.awaken(); 143 } catch (RemoteException e) { 144 e.rethrowFromSystemServer(); 145 } 146 } 147 148 /** 149 * Sets the active dream on the device to be "dreamComponent". 150 * 151 * <p>This is only used for testing the dream service APIs. 152 * 153 * @hide 154 */ 155 @TestApi 156 @UserHandleAware 157 @RequiresPermission(android.Manifest.permission.WRITE_DREAM_STATE) setActiveDream(@ullable ComponentName dreamComponent)158 public void setActiveDream(@Nullable ComponentName dreamComponent) { 159 ComponentName[] dreams = {dreamComponent}; 160 161 try { 162 mService.setDreamComponentsForUser(mContext.getUserId(), 163 dreamComponent != null ? dreams : null); 164 } catch (RemoteException e) { 165 e.rethrowFromSystemServer(); 166 } 167 } 168 169 /** 170 * Sets or clears the system dream component. 171 * 172 * The system dream component, when set, will be shown instead of the user configured dream 173 * when the system starts dreaming (not dozing). If the system is dreaming at the time the 174 * system dream is set or cleared, it immediately switches dream. 175 * 176 * @hide 177 */ 178 @TestApi 179 @RequiresPermission(android.Manifest.permission.WRITE_DREAM_STATE) setSystemDreamComponent(@ullable ComponentName dreamComponent)180 public void setSystemDreamComponent(@Nullable ComponentName dreamComponent) { 181 try { 182 mService.setSystemDreamComponent(dreamComponent); 183 } catch (RemoteException e) { 184 throw e.rethrowFromSystemServer(); 185 } 186 } 187 188 /** 189 * Sets the active dream on the device to be "dreamComponent". 190 * 191 * <p>This is only used for testing the dream service APIs. 192 * 193 * @hide 194 */ 195 @TestApi 196 @UserHandleAware 197 @RequiresPermission(android.Manifest.permission.WRITE_DREAM_STATE) setDreamOverlay(@ullable ComponentName dreamOverlayComponent)198 public void setDreamOverlay(@Nullable ComponentName dreamOverlayComponent) { 199 try { 200 mService.registerDreamOverlayService(dreamOverlayComponent); 201 } catch (RemoteException e) { 202 e.rethrowFromSystemServer(); 203 } 204 } 205 206 /** 207 * Returns whether the device is Dreaming. 208 * 209 * <p> This is only used for testing the dream service APIs. 210 * 211 * @hide 212 */ 213 @TestApi 214 @RequiresPermission(android.Manifest.permission.READ_DREAM_STATE) isDreaming()215 public boolean isDreaming() { 216 try { 217 return mService.isDreaming(); 218 } catch (RemoteException e) { 219 e.rethrowFromSystemServer(); 220 } 221 return false; 222 } 223 } 224