Replace Somnambulator functionality with shell commands.

This changelist removes the ability for Somnambulator to enter dreams.
Somnambulator itself was removed from use in ag/238189, where this
functionality was moved to UiModeManager. Somnambulator was maintained
to resolve the dock intent (but not necessarily launch) and also to
be a way for launching dreams for development/test. This changelist
preserves the intent resolution, while providing the ability to launch
dreams through a shell command as root.

Fixes: 225878553
Test: verify dreams work through docking with change.
Test: adb shell cmd dreams start-dreaming
Test: adb shell cmd dreams stop-dreaming
Change-Id: Ia88de2e1548c12079690860a2bb75e4e79d10e66
diff --git a/packages/SystemUI/src/com/android/systemui/Somnambulator.java b/packages/SystemUI/src/com/android/systemui/Somnambulator.java
index 0dd6d92..25801cf 100644
--- a/packages/SystemUI/src/com/android/systemui/Somnambulator.java
+++ b/packages/SystemUI/src/com/android/systemui/Somnambulator.java
@@ -17,12 +17,15 @@
 package com.android.systemui;
 
 import android.app.Activity;
-import android.content.Intent;
 import android.service.dreams.Sandman;
 
 /**
  * A simple activity that launches a dream.
  * <p>
+ *
+ * This activity has been deprecated and no longer used. The system uses its presence to determine
+ * whether a dock app should be started on dock through intent resolution.
+ *
  * Note: This Activity is special.  If this class is moved to another package or
  * renamed, be sure to update the component name in {@link Sandman}.
  * </p>
@@ -34,27 +37,6 @@
     @Override
     public void onStart() {
         super.onStart();
-
-        final Intent launchIntent = getIntent();
-        final String action = launchIntent.getAction();
-        if (Intent.ACTION_CREATE_SHORTCUT.equals(action)) {
-            Intent shortcutIntent = new Intent(this, Somnambulator.class);
-            shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
-                    | Intent.FLAG_ACTIVITY_NEW_TASK);
-            Intent resultIntent = new Intent();
-            resultIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
-                    Intent.ShortcutIconResource.fromContext(this, R.mipmap.ic_launcher_dreams));
-            resultIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
-            resultIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.start_dreams));
-            setResult(RESULT_OK, resultIntent);
-        } else {
-            boolean docked = launchIntent.hasCategory(Intent.CATEGORY_DESK_DOCK);
-            if (docked) {
-                Sandman.startDreamWhenDockedIfAppropriate(this);
-            } else {
-                Sandman.startDreamByUserRequest(this);
-            }
-        }
         finish();
     }
 }
diff --git a/services/core/java/com/android/server/dreams/DreamManagerService.java b/services/core/java/com/android/server/dreams/DreamManagerService.java
index 63c5456..7b60345 100644
--- a/services/core/java/com/android/server/dreams/DreamManagerService.java
+++ b/services/core/java/com/android/server/dreams/DreamManagerService.java
@@ -23,6 +23,7 @@
 
 import static com.android.server.wm.ActivityInterceptorCallback.DREAM_MANAGER_ORDERED_ID;
 
+import android.annotation.NonNull;
 import android.annotation.Nullable;
 import android.app.ActivityManager;
 import android.app.TaskInfo;
@@ -45,6 +46,9 @@
 import android.os.Looper;
 import android.os.PowerManager;
 import android.os.PowerManagerInternal;
+import android.os.RemoteException;
+import android.os.ResultReceiver;
+import android.os.ShellCallback;
 import android.os.SystemClock;
 import android.os.SystemProperties;
 import android.os.UserHandle;
@@ -221,6 +225,10 @@
         }
     }
 
+    protected void requestStartDreamFromShell() {
+        requestDreamInternal();
+    }
+
     private void requestDreamInternal() {
         // Ask the power manager to nap.  It will eventually call back into
         // startDream() if/when it is appropriate to start dreaming.
@@ -275,6 +283,10 @@
         }
     }
 
+    protected void requestStopDreamFromShell() {
+        stopDreamInternal(true, "stopping dream from shell");
+    }
+
     private void stopDreamInternal(boolean immediate, String reason) {
         synchronized (mLock) {
             stopDreamLocked(immediate, reason);
@@ -593,6 +605,14 @@
             }
         }
 
+        public void onShellCommand(@Nullable FileDescriptor in, @Nullable FileDescriptor out,
+                @Nullable FileDescriptor err,
+                @NonNull String[] args, @Nullable ShellCallback callback,
+                @NonNull ResultReceiver resultReceiver) throws RemoteException {
+            new DreamShellCommand(DreamManagerService.this, mPowerManager)
+                    .exec(this, in, out, err, args, callback, resultReceiver);
+        }
+
         @Override // Binder call
         public ComponentName[] getDreamComponents() {
             return getDreamComponentsForUser(UserHandle.getCallingUserId());
diff --git a/services/core/java/com/android/server/dreams/DreamShellCommand.java b/services/core/java/com/android/server/dreams/DreamShellCommand.java
new file mode 100644
index 0000000..eae7e80
--- /dev/null
+++ b/services/core/java/com/android/server/dreams/DreamShellCommand.java
@@ -0,0 +1,92 @@
+/*
+ * Copyright (C) 2022 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 com.android.server.dreams;
+
+import android.annotation.NonNull;
+import android.os.Binder;
+import android.os.PowerManager;
+import android.os.Process;
+import android.os.ShellCommand;
+import android.os.SystemClock;
+import android.text.TextUtils;
+import android.util.Slog;
+
+import java.io.PrintWriter;
+
+/**
+ * {@link DreamShellCommand} allows accessing dream functionality, including toggling dream state.
+ */
+public class DreamShellCommand extends ShellCommand {
+    private static final boolean DEBUG = true;
+    private static final String TAG = "DreamShellCommand";
+    private final @NonNull DreamManagerService mService;
+    private final @NonNull PowerManager mPowerManager;
+
+    DreamShellCommand(@NonNull DreamManagerService service, @NonNull PowerManager powerManager) {
+        mService = service;
+        mPowerManager = powerManager;
+    }
+
+    @Override
+    public int onCommand(String cmd) {
+        final int callingUid = Binder.getCallingUid();
+        if (callingUid != Process.ROOT_UID) {
+            Slog.e(TAG, "Must be root before calling Dream shell commands");
+            return -1;
+        }
+
+        if (TextUtils.isEmpty(cmd)) {
+            return super.handleDefaultCommands(cmd);
+        }
+        if (DEBUG) {
+            Slog.d(TAG, "onCommand:" + cmd);
+        }
+
+        switch (cmd) {
+            case "start-dreaming":
+                return startDreaming();
+            case "stop-dreaming":
+                return stopDreaming();
+            default:
+                return super.handleDefaultCommands(cmd);
+        }
+    }
+
+    private int startDreaming() {
+        mPowerManager.wakeUp(SystemClock.uptimeMillis(),
+                PowerManager.WAKE_REASON_PLUGGED_IN, "shell:cmd:android.service.dreams:DREAM");
+        mService.requestStartDreamFromShell();
+        return 0;
+    }
+
+    private int stopDreaming() {
+        mService.requestStopDreamFromShell();
+        return 0;
+    }
+
+    @Override
+    public void onHelp() {
+        PrintWriter pw = getOutPrintWriter();
+        pw.println("Dream manager (dreams) commands:");
+        pw.println("  help");
+        pw.println("      Print this help text.");
+        pw.println("  start-dreaming");
+        pw.println("      Start the currently configured dream.");
+        pw.println("  stop-dreaming");
+        pw.println("      Stops any active dream");
+    }
+}
OSZAR »