1 /* 2 * Copyright (C) 2022 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.server.wm; 18 19 import android.app.DreamManager; 20 import android.content.ComponentName; 21 import android.content.Context; 22 23 import static org.junit.Assume.assumeTrue; 24 25 import com.android.compatibility.common.util.SystemUtil; 26 27 public class DreamCoordinator { 28 private final DreamManager mDreamManager; 29 30 private boolean mSetup; 31 private boolean mDefaultDreamServiceEnabled; 32 DreamCoordinator(Context context)33 public DreamCoordinator(Context context) { 34 mDreamManager = context.getSystemService(DreamManager.class); 35 } 36 getDreamActivityName(ComponentName dream)37 public final ComponentName getDreamActivityName(ComponentName dream) { 38 return new ComponentName(dream.getPackageName(), 39 "android.service.dreams.DreamActivity"); 40 } 41 42 /** 43 * Sets up the system to show dreams. If system doesn't support dreams, returns {@code false}. 44 */ setup()45 public void setup() { 46 assumeTrue(mDreamManager.areDreamsSupported()); 47 48 SystemUtil.runWithShellPermissionIdentity(() -> { 49 mDefaultDreamServiceEnabled = mDreamManager.isScreensaverEnabled(); 50 if (!mDefaultDreamServiceEnabled) { 51 mDreamManager.setScreensaverEnabled(true); 52 } 53 }); 54 55 mSetup = true; 56 } 57 58 /** 59 * Restores any settings changed by {@link #setup()}. 60 */ restoreDefaults()61 public void restoreDefaults() { 62 // If we have not set up the coordinator, do not do anything. 63 if (!mSetup) { 64 return; 65 } 66 67 mSetup = false; 68 69 // Nothing to restore if dreams are enabled by default. 70 if (mDefaultDreamServiceEnabled) { 71 return; 72 } 73 74 SystemUtil.runWithShellPermissionIdentity( 75 () -> mDreamManager.setScreensaverEnabled(false)); 76 } 77 startDream()78 public void startDream() { 79 SystemUtil.runWithShellPermissionIdentity(() -> mDreamManager.startDream()); 80 } 81 stopDream()82 public void stopDream() { 83 SystemUtil.runWithShellPermissionIdentity(mDreamManager::stopDream); 84 } 85 setActiveDream(ComponentName dream)86 public ComponentName setActiveDream(ComponentName dream) { 87 SystemUtil.runWithShellPermissionIdentity(() -> mDreamManager.setActiveDream(dream)); 88 return getDreamActivityName(dream); 89 } 90 setSystemDream(ComponentName dream)91 public ComponentName setSystemDream(ComponentName dream) { 92 SystemUtil.runWithShellPermissionIdentity(() -> 93 mDreamManager.setSystemDreamComponent(dream)); 94 return dream == null ? null : getDreamActivityName(dream); 95 } 96 setDreamOverlay(ComponentName overlay)97 public void setDreamOverlay(ComponentName overlay) { 98 SystemUtil.runWithShellPermissionIdentity(() -> mDreamManager.setDreamOverlay(overlay)); 99 } 100 isDreaming()101 public boolean isDreaming() { 102 return SystemUtil.runWithShellPermissionIdentity(mDreamManager::isDreaming); 103 } 104 } 105