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

Commit 42494f0d authored by Tony Mak's avatar Tony Mak
Browse files

Improve the appfunctions shell commands

1. execute-app-functions does not print the error message properly
It is because the readFromParcel flow does not invoke the supre
constructor. Also, now printing error code in toString too.

2. guard new commands with flags

Flag: EXEMPT changes in shell commands only
Test: run the command and observe the error

Change-Id: Idb6aa58e85584197e0ae1941d4c91bdd67bc7f08
parent 60fc65e4
Loading
Loading
Loading
Loading
+7 −3
Original line number Diff line number Diff line
@@ -162,9 +162,8 @@ public final class AppFunctionException extends Exception implements Parcelable
    }

    private AppFunctionException(@NonNull Parcel in) {
        mErrorCode = in.readInt();
        mErrorMessage = in.readString8();
        mExtras = Objects.requireNonNull(in.readBundle(getClass().getClassLoader()));
        this(/* errorCode= */ in.readInt(), /* errorMessage= */ in.readString8(),
                /* extras= */Objects.requireNonNull(in.readBundle(Bundle.class.getClassLoader())));
    }

    /** Returns one of the {@code ERROR} constants. */
@@ -224,6 +223,11 @@ public final class AppFunctionException extends Exception implements Parcelable
        dest.writeBundle(mExtras);
    }

    @Override
    public @NonNull String getMessage() {
        return super.getMessage() + " (code " + mErrorCode + ")";
    }

    /**
     * Error codes.
     *
+47 −31
Original line number Diff line number Diff line
@@ -108,12 +108,15 @@ public class AppFunctionManagerServiceShellCommand extends ShellCommand {
                        + ". Defaults to the current user.");

        pw.println();

        if (accessCheckFlagsEnabled()) {
            pw.println(
                    "  grant-app-function-access --agent-package <AGENT_PACKAGE_NAME> "
                            + "--target-package <TARGET_PACKAGE_NAME> [--agent-user <USER_ID>] "
                            + "[--target-user <USER_ID>]");
            pw.println("    Grants an agent package access to an app's functions.");
        pw.println("    --agent-package <AGENT_PACKAGE_NAME>: The agent package to grant access.");
            pw.println(
                    "    --agent-package <AGENT_PACKAGE_NAME>: The agent package to grant access.");
            pw.println("    --target-package <TARGET_PACKAGE_NAME>: The target package.");
            pw.println(
                    "    --agent-user <USER_ID> (optional): The user ID for the agent package. "
@@ -137,9 +140,9 @@ public class AppFunctionManagerServiceShellCommand extends ShellCommand {
            pw.println(
                    "    --target-user <USER_ID> (optional): The user ID for the target package. "
                            + "Defaults to the current user.");

            pw.println();
        }
    }

    @Override
    public int onCommand(String cmd) {
@@ -156,8 +159,14 @@ public class AppFunctionManagerServiceShellCommand extends ShellCommand {
                case "set-enabled":
                    return runSetAppFunctionEnabled();
                case "grant-app-function-access":
                    if (!accessCheckFlagsEnabled()) {
                        return -1;
                    }
                    return runGrantAppFunctionAccess();
                case "revoke-app-function-access":
                    if (!accessCheckFlagsEnabled()) {
                        return -1;
                    }
                    return runRevokeAppFunctionAccess();
                default:
                    return handleDefaultCommands(cmd);
@@ -388,7 +397,9 @@ public class AppFunctionManagerServiceShellCommand extends ShellCommand {
                    @Override
                    public void onError(AppFunctionException e) {
                        Log.d(TAG, "onError: ", e);
                        pw.println("Error executing app function: " + e.getErrorCode() + " - " + e);
                        pw.printf(
                                "Error executing app function: %s. See logcat for more details. %n",
                                e);
                        resultCode.set(-1);
                        countDownLatch.countDown();
                    }
@@ -507,4 +518,9 @@ public class AppFunctionManagerServiceShellCommand extends ShellCommand {
            default -> throw new IllegalAccessError("Only allow shell or root");
        };
    }

    private boolean accessCheckFlagsEnabled() {
        return android.permission.flags.Flags.appFunctionAccessApiEnabled()
                && android.permission.flags.Flags.appFunctionAccessServiceEnabled();
    }
}