diff --git a/core/api/test-current.txt b/core/api/test-current.txt index d5ceafb6ca3d3..226deba694a5b 100644 --- a/core/api/test-current.txt +++ b/core/api/test-current.txt @@ -257,7 +257,8 @@ package android.app { public class DreamManager { method @RequiresPermission(android.Manifest.permission.READ_DREAM_STATE) public boolean isDreaming(); - method @RequiresPermission(android.Manifest.permission.WRITE_DREAM_STATE) public void setActiveDream(@NonNull android.content.ComponentName); + method @RequiresPermission(android.Manifest.permission.WRITE_DREAM_STATE) public void setActiveDream(@Nullable android.content.ComponentName); + method @RequiresPermission(android.Manifest.permission.WRITE_DREAM_STATE) public void setDreamOverlay(@Nullable android.content.ComponentName); method @RequiresPermission(android.Manifest.permission.WRITE_DREAM_STATE) public void startDream(@NonNull android.content.ComponentName); method @RequiresPermission(android.Manifest.permission.WRITE_DREAM_STATE) public void stopDream(); } @@ -2364,6 +2365,17 @@ package android.service.autofill.augmented { } +package android.service.dreams { + + public abstract class DreamOverlayService extends android.app.Service { + ctor public DreamOverlayService(); + method @Nullable public final android.os.IBinder onBind(@NonNull android.content.Intent); + method public abstract void onStartDream(@NonNull android.view.WindowManager.LayoutParams); + method public final void requestExit(); + } + +} + package android.service.notification { @Deprecated public abstract class ConditionProviderService extends android.app.Service { diff --git a/core/java/android/app/DreamManager.java b/core/java/android/app/DreamManager.java index f23681373f532..34ae08fd9b9a7 100644 --- a/core/java/android/app/DreamManager.java +++ b/core/java/android/app/DreamManager.java @@ -17,6 +17,7 @@ package android.app; import android.annotation.NonNull; +import android.annotation.Nullable; import android.annotation.RequiresPermission; import android.annotation.SystemService; import android.annotation.TestApi; @@ -91,10 +92,30 @@ public class DreamManager { @TestApi @UserHandleAware @RequiresPermission(android.Manifest.permission.WRITE_DREAM_STATE) - public void setActiveDream(@NonNull ComponentName dreamComponent) { + public void setActiveDream(@Nullable ComponentName dreamComponent) { ComponentName[] dreams = {dreamComponent}; + try { - mService.setDreamComponentsForUser(mContext.getUserId(), dreams); + mService.setDreamComponentsForUser(mContext.getUserId(), + dreamComponent != null ? dreams : null); + } catch (RemoteException e) { + e.rethrowFromSystemServer(); + } + } + + /** + * Sets the active dream on the device to be "dreamComponent". + * + *
This is only used for testing the dream service APIs. + * + * @hide + */ + @TestApi + @UserHandleAware + @RequiresPermission(android.Manifest.permission.WRITE_DREAM_STATE) + public void setDreamOverlay(@Nullable ComponentName dreamOverlayComponent) { + try { + mService.registerDreamOverlayService(dreamOverlayComponent); } catch (RemoteException e) { e.rethrowFromSystemServer(); } diff --git a/core/java/android/service/dreams/DreamOverlayService.java b/core/java/android/service/dreams/DreamOverlayService.java new file mode 100644 index 0000000000000..50f9d8ac29581 --- /dev/null +++ b/core/java/android/service/dreams/DreamOverlayService.java @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.service.dreams; + +import android.annotation.NonNull; +import android.annotation.Nullable; +import android.annotation.TestApi; +import android.app.Service; +import android.content.Intent; +import android.os.IBinder; +import android.os.RemoteException; +import android.util.Log; +import android.view.WindowManager; + + +/** + * Basic implementation of for {@link IDreamOverlay} for testing. + * @hide + */ +@TestApi +public abstract class DreamOverlayService extends Service { + private static final String TAG = "DreamOverlayService"; + private static final boolean DEBUG = false; + + private IDreamOverlay mDreamOverlay = new IDreamOverlay.Stub() { + @Override + public void startDream(WindowManager.LayoutParams layoutParams, + IDreamOverlayCallback callback) { + mDreamOverlayCallback = callback; + onStartDream(layoutParams); + } + }; + + IDreamOverlayCallback mDreamOverlayCallback; + + public DreamOverlayService() { + } + + @Nullable + @Override + public final IBinder onBind(@NonNull Intent intent) { + return mDreamOverlay.asBinder(); + } + + /** + * This method is overridden by implementations to handle when the dream has started and the + * window is ready to be interacted with. + * @param layoutParams The {@link android.view.WindowManager.LayoutParams} associated with the + * dream window. + */ + public abstract void onStartDream(@NonNull WindowManager.LayoutParams layoutParams); + + /** + * This method is invoked to request the dream exit. + */ + public final void requestExit() { + try { + mDreamOverlayCallback.onExitRequested(); + } catch (RemoteException e) { + Log.e(TAG, "Could not request exit:" + e); + } + } +}