Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bfe14724 authored by Bryce Lee's avatar Bryce Lee Committed by Automerger Merge Worker
Browse files

Merge "Replace Somnambulator functionality with shell commands." into tm-qpr-dev am: 49768370

parents 102f677a 49768370
Loading
Loading
Loading
Loading
+4 −22
Original line number Diff line number Diff line
@@ -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 @@ public class Somnambulator extends Activity {
    @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();
    }
}
+20 −0
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import static android.app.WindowConfiguration.ACTIVITY_TYPE_HOME;

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.IBinder;
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 @@ public final class DreamManagerService extends SystemService {
        }
    }

    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 @@ public final class DreamManagerService extends SystemService {
        }
    }

    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 final class DreamManagerService extends SystemService {
            }
        }

        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());
+92 −0
Original line number Diff line number Diff line
/*
 * 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");
    }
}