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

Commit a6c3350f authored by Raymond Chiu's avatar Raymond Chiu Committed by Automerger Merge Worker
Browse files

Merge "Add shell command to game service for interventions" into sc-dev am: 0fe0a9ec

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/13874545

Change-Id: I424326867aa158d09cf6ce189d90f3e67825a3b3
parents 51a4d93f 0fe0a9ec
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -32,6 +32,8 @@ import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.Process;
import android.os.ResultReceiver;
import android.os.ShellCallback;
import android.util.ArrayMap;
import android.util.Log;
import android.util.Slog;
@@ -41,6 +43,8 @@ import com.android.internal.annotations.VisibleForTesting;
import com.android.server.ServiceThread;
import com.android.server.SystemService;

import java.io.FileDescriptor;

/**
 * Service to manage game related features.
 *
@@ -75,6 +79,12 @@ public final class GameManagerService extends IGameManagerService.Stub {
        mPackageManager = context.getPackageManager();
    }

    @Override
    public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err,
            String[] args, ShellCallback callback, ResultReceiver result) {
        new GameManagerShellCommand().exec(this, in, out, err, args, callback, result);
    }

    class SettingsHandler extends Handler {

        SettingsHandler(Looper looper) {
+136 −0
Original line number Diff line number Diff line
/*
 * 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 com.android.server.app;

import android.compat.Compatibility;
import android.content.Context;
import android.os.ServiceManager;
import android.os.ShellCommand;
import android.util.ArraySet;

import com.android.internal.compat.CompatibilityChangeConfig;
import com.android.server.compat.PlatformCompat;
import com.android.server.wm.CompatModePackages;

import java.io.PrintWriter;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * ShellCommands for GameManagerService.
 *
 * Use with {@code adb shell cmd game ...}.
 */
public class GameManagerShellCommand extends ShellCommand {

    public GameManagerShellCommand() {}

    private static final ArraySet<Long> DOWNSCALE_CHANGE_IDS = new ArraySet<>(new Long[]{
            CompatModePackages.DOWNSCALED,
            CompatModePackages.DOWNSCALE_90,
            CompatModePackages.DOWNSCALE_80,
            CompatModePackages.DOWNSCALE_70,
            CompatModePackages.DOWNSCALE_60,
            CompatModePackages.DOWNSCALE_50});

    @Override
    public int onCommand(String cmd) {
        if (cmd == null) {
            return handleDefaultCommands(cmd);
        }
        final PrintWriter pw = getOutPrintWriter();
        try {
            switch (cmd) {
                case "downscale":
                    final String ratio = getNextArgRequired();
                    final String packageName = getNextArgRequired();

                    final long changeId;
                    switch (ratio) {
                        case "0.5":
                            changeId = CompatModePackages.DOWNSCALE_50;
                            break;
                        case "0.6":
                            changeId = CompatModePackages.DOWNSCALE_60;
                            break;
                        case "0.7":
                            changeId = CompatModePackages.DOWNSCALE_70;
                            break;
                        case "0.8":
                            changeId = CompatModePackages.DOWNSCALE_80;
                            break;
                        case "0.9":
                            changeId = CompatModePackages.DOWNSCALE_90;
                            break;
                        case "disable":
                            changeId = 0;
                            break;
                        default:
                            changeId = -1;
                            pw.println("Invalid scaling ratio '" + ratio + "'");
                            break;
                    }
                    if (changeId == -1) {
                        break;
                    }

                    Set<Long> enabled = new ArraySet<>();
                    Set<Long> disabled;
                    if (changeId == 0) {
                        disabled = DOWNSCALE_CHANGE_IDS;
                    } else {
                        enabled.add(CompatModePackages.DOWNSCALED);
                        enabled.add(changeId);
                        disabled = DOWNSCALE_CHANGE_IDS.stream()
                          .filter(it -> it != CompatModePackages.DOWNSCALED && it != changeId)
                          .collect(Collectors.toSet());
                    }

                    final PlatformCompat platformCompat = (PlatformCompat)
                            ServiceManager.getService(Context.PLATFORM_COMPAT_SERVICE);
                    final CompatibilityChangeConfig overrides =
                            new CompatibilityChangeConfig(
                                new Compatibility.ChangeConfig(enabled, disabled));

                    platformCompat.setOverrides(overrides, packageName);
                    if (changeId == 0) {
                        pw.println("Disable downscaling for " + packageName + ".");
                    } else {
                        pw.println("Enable downscaling ratio for " + packageName + " to " + ratio);
                    }

                    break;
                default:
                    return handleDefaultCommands(cmd);
            }
        } catch (Exception e) {
            pw.println("Error: " + e);
        }
        return -1;
    }


    @Override
    public void onHelp() {
        PrintWriter pw = getOutPrintWriter();
        pw.println("Game manager (game) commands:");
        pw.println("  help");
        pw.println("      Print this help text.");
        pw.println("  downscale [0.5|0.6|0.7|0.8|0.9|disable] <PACKAGE_NAME>");
        pw.println("      Force app to run at the specified scaling ratio.");
    }
}