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 try { 75 mService.setScreensaverEnabled(enabled); 76 } catch (RemoteException e) { 77 e.rethrowFromSystemServer(); 78 } 79 } 80 81 /** 82 * Returns whether dreams are supported. 83 * 84 * @hide 85 */ 86 @TestApi areDreamsSupported()87 public boolean areDreamsSupported() { 88 return mContext.getResources().getBoolean( 89 com.android.internal.R.bool.config_dreamsSupported); 90 } 91 92 /** 93 * Starts dreaming. 94 * 95 * The system dream component, if set by {@link DreamManager#setSystemDreamComponent}, will be 96 * started. 97 * Otherwise, starts the active dream set by {@link DreamManager#setActiveDream}. 98 * 99 * <p>This is only used for testing the dream service APIs. 100 * 101 * @see DreamManager#setActiveDream(ComponentName) 102 * @see DreamManager#setSystemDreamComponent(ComponentName) 103 * 104 * @hide 105 */ 106 @TestApi 107 @UserHandleAware 108 @RequiresPermission(android.Manifest.permission.WRITE_DREAM_STATE) startDream()109 public void startDream() { 110 try { 111 mService.dream(); 112 } catch (RemoteException e) { 113 e.rethrowFromSystemServer(); 114 } 115 } 116 117 /** 118 * Stops the dream service on the device if one is started. 119 * 120 * <p> This is only used for testing the dream service APIs. 121 * 122 * @hide 123 */ 124 @TestApi 125 @RequiresPermission(android.Manifest.permission.WRITE_DREAM_STATE) stopDream()126 public void stopDream() { 127 try { 128 mService.awaken(); 129 } catch (RemoteException e) { 130 e.rethrowFromSystemServer(); 131 } 132 } 133 134 /** 135 * Sets the active dream on the device to be "dreamComponent". 136 * 137 * <p>This is only used for testing the dream service APIs. 138 * 139 * @hide 140 */ 141 @TestApi 142 @UserHandleAware 143 @RequiresPermission(android.Manifest.permission.WRITE_DREAM_STATE) setActiveDream(@ullable ComponentName dreamComponent)144 public void setActiveDream(@Nullable ComponentName dreamComponent) { 145 ComponentName[] dreams = {dreamComponent}; 146 147 try { 148 mService.setDreamComponentsForUser(mContext.getUserId(), 149 dreamComponent != null ? dreams : null); 150 } catch (RemoteException e) { 151 e.rethrowFromSystemServer(); 152 } 153 } 154 155 /** 156 * Sets or clears the system dream component. 157 * 158 * The system dream component, when set, will be shown instead of the user configured dream 159 * when the system starts dreaming (not dozing). If the system is dreaming at the time the 160 * system dream is set or cleared, it immediately switches dream. 161 * 162 * @hide 163 */ 164 @TestApi 165 @RequiresPermission(android.Manifest.permission.WRITE_DREAM_STATE) setSystemDreamComponent(@ullable ComponentName dreamComponent)166 public void setSystemDreamComponent(@Nullable ComponentName dreamComponent) { 167 try { 168 mService.setSystemDreamComponent(dreamComponent); 169 } catch (RemoteException e) { 170 throw e.rethrowFromSystemServer(); 171 } 172 } 173 174 /** 175 * Sets the active dream on the device to be "dreamComponent". 176 * 177 * <p>This is only used for testing the dream service APIs. 178 * 179 * @hide 180 */ 181 @TestApi 182 @UserHandleAware 183 @RequiresPermission(android.Manifest.permission.WRITE_DREAM_STATE) setDreamOverlay(@ullable ComponentName dreamOverlayComponent)184 public void setDreamOverlay(@Nullable ComponentName dreamOverlayComponent) { 185 try { 186 mService.registerDreamOverlayService(dreamOverlayComponent); 187 } catch (RemoteException e) { 188 e.rethrowFromSystemServer(); 189 } 190 } 191 192 /** 193 * Whether dreaming can start given user settings and the current dock/charge state. 194 * 195 * @hide 196 */ 197 @UserHandleAware 198 @RequiresPermission(android.Manifest.permission.READ_DREAM_STATE) canStartDreaming(boolean isScreenOn)199 public boolean canStartDreaming(boolean isScreenOn) { 200 try { 201 return mService.canStartDreaming(isScreenOn); 202 } catch (RemoteException e) { 203 e.rethrowFromSystemServer(); 204 } 205 return false; 206 } 207 208 /** 209 * Returns whether the device is Dreaming. 210 * 211 * <p> This is only used for testing the dream service APIs. 212 * 213 * @hide 214 */ 215 @TestApi 216 @RequiresPermission(android.Manifest.permission.READ_DREAM_STATE) isDreaming()217 public boolean isDreaming() { 218 try { 219 return mService.isDreaming(); 220 } catch (RemoteException e) { 221 e.rethrowFromSystemServer(); 222 } 223 return false; 224 } 225 226 /** 227 * Sets whether the dream is obscured by something. 228 * 229 * @hide 230 */ 231 @FlaggedApi(Flags.FLAG_DREAM_HANDLES_BEING_OBSCURED) 232 @RequiresPermission(android.Manifest.permission.WRITE_DREAM_STATE) setDreamIsObscured(boolean isObscured)233 public void setDreamIsObscured(boolean isObscured) { 234 try { 235 mService.setDreamIsObscured(isObscured); 236 } catch (RemoteException e) { 237 throw e.rethrowFromSystemServer(); 238 } 239 } 240 241 /** 242 * Notifies dream manager of device postured state, which may affect dream enablement. 243 * 244 * @hide 245 */ 246 @FlaggedApi(Flags.FLAG_ALLOW_DREAM_WHEN_POSTURED) 247 @RequiresPermission(android.Manifest.permission.WRITE_DREAM_STATE) setDevicePostured(boolean isPostured)248 public void setDevicePostured(boolean isPostured) { 249 try { 250 mService.setDevicePostured(isPostured); 251 } catch (RemoteException e) { 252 throw e.rethrowFromSystemServer(); 253 } 254 } 255 } 256