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

Commit 4201a5dd authored by hongzhu wang's avatar hongzhu wang
Browse files

Fix: crash when running am.jar without parameters

Issure:
When no args to run am.jar,mAm is not initialized before use
process will crash in NRE

Solution:
Initialize mAm in constructor

Bug: 202471754
Test: manual
Running follow sh on the device displays help messages correctly

base=/system
export CLASSPATH=$base/framework/am.jar
exec app_process $base/bin com.android.commands.am.Am

Change-Id: I088f4f5b4072d350c217655a291658d0bfd506e9
parent bbed6928
Loading
Loading
Loading
Loading
+18 −13
Original line number Diff line number Diff line
@@ -41,6 +41,10 @@ public class Am extends BaseCommand {
    private IActivityManager mAm;
    private IPackageManager mPm;

    Am() {
        svcInit();
    }

    /**
     * Command-line entry point.
     *
@@ -50,6 +54,20 @@ public class Am extends BaseCommand {
        (new Am()).run(args);
    }

    private void svcInit() {
        mAm = ActivityManager.getService();
        if (mAm == null) {
            System.err.println(NO_SYSTEM_ERROR_CODE);
            return;
        }

        mPm = IPackageManager.Stub.asInterface(ServiceManager.getService("package"));
        if (mPm == null) {
            System.err.println(NO_SYSTEM_ERROR_CODE);
            return;
        }
    }

    @Override
    public void onShowUsage(PrintStream out) {
        try {
@@ -61,19 +79,6 @@ public class Am extends BaseCommand {

    @Override
    public void onRun() throws Exception {

        mAm = ActivityManager.getService();
        if (mAm == null) {
            System.err.println(NO_SYSTEM_ERROR_CODE);
            throw new AndroidException("Can't connect to activity manager; is the system running?");
        }

        mPm = IPackageManager.Stub.asInterface(ServiceManager.getService("package"));
        if (mPm == null) {
            System.err.println(NO_SYSTEM_ERROR_CODE);
            throw new AndroidException("Can't connect to package manager; is the system running?");
        }

        String op = nextArgRequired();

        if (op.equals("instrument")) {