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

Commit 3bb0e868 authored by Jeff Sharkey's avatar Jeff Sharkey Committed by Android (Google) Code Review
Browse files

Merge "Shell commands for roles."

parents 9a94afc6 6260c587
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -30,6 +30,8 @@ import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.ResultReceiver;
import android.os.ShellCallback;
import android.os.UserHandle;
import android.os.UserManagerInternal;
import android.text.TextUtils;
@@ -42,6 +44,7 @@ import com.android.internal.util.Preconditions;
import com.android.server.LocalServices;
import com.android.server.SystemService;

import java.io.FileDescriptor;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@@ -334,5 +337,13 @@ public class RoleManagerService extends SystemService {
            return ActivityManager.handleIncomingUser(getCallingPid(), getCallingUid(), userId,
                    false, true, name, null);
        }

        @Override
        public void onShellCommand(FileDescriptor in, FileDescriptor out,
                FileDescriptor err, String[] args, ShellCallback callback,
                ResultReceiver resultReceiver) {
            (new RoleManagerShellCommand(this)).exec(
                    this, in, out, err, args, callback, resultReceiver);
        }
    }
}
+134 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 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.role;

import android.app.role.IRoleManager;
import android.app.role.IRoleManagerCallback;
import android.os.RemoteException;
import android.os.ShellCommand;
import android.os.UserHandle;

import java.io.PrintWriter;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

class RoleManagerShellCommand extends ShellCommand {
    private final IRoleManager mRoleManager;

    RoleManagerShellCommand(IRoleManager roleManager) {
        mRoleManager = roleManager;
    }

    private class Callback extends IRoleManagerCallback.Stub {
        private final CompletableFuture<Void> mResult = new CompletableFuture<>();

        public int waitForResult() {
            try {
                mResult.get(5, TimeUnit.SECONDS);
                return 0;
            } catch (Exception e) {
                getErrPrintWriter().println("Error: " + e.toString());
                return -1;
            }
        }

        @Override
        public void onSuccess() {
            mResult.complete(null);
        }

        @Override
        public void onFailure() {
            mResult.completeExceptionally(new RuntimeException("Failed"));
        }
    }

    @Override
    public int onCommand(String cmd) {
        if (cmd == null) {
            return handleDefaultCommands(cmd);
        }

        PrintWriter pw = getOutPrintWriter();
        try {
            switch (cmd) {
                case "add-role-holder":
                    return runAddRoleHolder();
                case "remove-role-holder":
                    return runRemoveRoleHolder();
                case "clear-role-holders":
                    return runClearRoleHolders();
                default:
                    return handleDefaultCommands(cmd);
            }
        } catch (RemoteException e) {
            pw.println("Remote exception: " + e);
        }
        return -1;
    }

    private int getUserIdMaybe() {
        int userId = UserHandle.USER_SYSTEM;
        String option = getNextOption();
        if (option != null && option.equals("--user")) {
            userId = UserHandle.parseUserArg(getNextArgRequired());
        }
        return userId;
    }

    private int runAddRoleHolder() throws RemoteException {
        int userId = getUserIdMaybe();
        String roleName = getNextArgRequired();
        String packageName = getNextArgRequired();

        Callback callback = new Callback();
        mRoleManager.addRoleHolderAsUser(roleName, packageName, userId, callback);
        return callback.waitForResult();
    }

    private int runRemoveRoleHolder() throws RemoteException {
        int userId = getUserIdMaybe();
        String roleName = getNextArgRequired();
        String packageName = getNextArgRequired();

        Callback callback = new Callback();
        mRoleManager.removeRoleHolderAsUser(roleName, packageName, userId, callback);
        return callback.waitForResult();
    }

    private int runClearRoleHolders() throws RemoteException {
        int userId = getUserIdMaybe();
        String roleName = getNextArgRequired();

        Callback callback = new Callback();
        mRoleManager.clearRoleHoldersAsUser(roleName, userId, callback);
        return callback.waitForResult();
    }

    @Override
    public void onHelp() {
        PrintWriter pw = getOutPrintWriter();
        pw.println("Role manager (role) commands:");
        pw.println("  help");
        pw.println("    Print this help text.");
        pw.println();
        pw.println("  add-role-holder [--user USER_ID] ROLE PACKAGE");
        pw.println("  remove-role-holder [--user USER_ID] ROLE PACKAGE");
        pw.println("  clear-role-holders [--user USER_ID] ROLE");
        pw.println();
    }
}