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

Commit 98c20b46 authored by Sergey Nikolaienkov's avatar Sergey Nikolaienkov Committed by Android (Google) Code Review
Browse files

Merge "Move CdmService.ShellCdm into a separate class"

parents a6224093 f512ba38
Loading
Loading
Loading
Loading
+6 −74
Original line number Original line Diff line number Diff line
@@ -96,7 +96,6 @@ import android.os.RemoteException;
import android.os.ResultReceiver;
import android.os.ResultReceiver;
import android.os.ServiceManager;
import android.os.ServiceManager;
import android.os.ShellCallback;
import android.os.ShellCallback;
import android.os.ShellCommand;
import android.os.UserHandle;
import android.os.UserHandle;
import android.os.UserManager;
import android.os.UserManager;
import android.permission.PermissionControllerManager;
import android.permission.PermissionControllerManager;
@@ -104,7 +103,6 @@ import android.text.BidiFormatter;
import android.util.ArrayMap;
import android.util.ArrayMap;
import android.util.ArraySet;
import android.util.ArraySet;
import android.util.ExceptionUtils;
import android.util.ExceptionUtils;
import android.util.Log;
import android.util.PackageUtils;
import android.util.PackageUtils;
import android.util.Slog;
import android.util.Slog;
import android.util.SparseArray;
import android.util.SparseArray;
@@ -706,7 +704,10 @@ public class CompanionDeviceManagerService extends SystemService implements Bind
        public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err,
        public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err,
                String[] args, ShellCallback callback, ResultReceiver resultReceiver)
                String[] args, ShellCallback callback, ResultReceiver resultReceiver)
                throws RemoteException {
                throws RemoteException {
            new ShellCmd().exec(this, in, out, err, args, callback, resultReceiver);
            getContext().enforceCallingOrSelfPermission(
                    android.Manifest.permission.MANAGE_COMPANION_DEVICES, null);
            new CompanionDeviceShellCommand(CompanionDeviceManagerService.this)
                    .exec(this, in, out, err, args, callback, resultReceiver);
        }
        }


        @Override
        @Override
@@ -769,7 +770,7 @@ public class CompanionDeviceManagerService extends SystemService implements Bind
        return Binder.getCallingUid() == Process.SYSTEM_UID;
        return Binder.getCallingUid() == Process.SYSTEM_UID;
    }
    }


    private void createAssociationInternal(
    void createAssociationInternal(
            int userId, String deviceMacAddress, String packageName, String deviceProfile) {
            int userId, String deviceMacAddress, String packageName, String deviceProfile) {
        final Association association = new Association(
        final Association association = new Association(
                getNewAssociationIdForPackage(userId, packageName),
                getNewAssociationIdForPackage(userId, packageName),
@@ -1082,7 +1083,7 @@ public class CompanionDeviceManagerService extends SystemService implements Bind
        }
        }
    }
    }


    private @NonNull Set<Association> getAllAssociations(int userId) {
    @NonNull Set<Association> getAllAssociations(int userId) {
        synchronized (mLock) {
        synchronized (mLock) {
            readPersistedStateForUserIfNeededLocked(userId);
            readPersistedStateForUserIfNeededLocked(userId);
            // This returns non-null, because the readAssociationsInfoForUserIfNeededLocked() method
            // This returns non-null, because the readAssociationsInfoForUserIfNeededLocked() method
@@ -1471,75 +1472,6 @@ public class CompanionDeviceManagerService extends SystemService implements Bind
        return (userId + 1) * ASSOCIATIONS_IDS_PER_USER_RANGE;
        return (userId + 1) * ASSOCIATIONS_IDS_PER_USER_RANGE;
    }
    }


    private class ShellCmd extends ShellCommand {
        public static final String USAGE = "help\n"
                + "list USER_ID\n"
                + "associate USER_ID PACKAGE MAC_ADDRESS\n"
                + "disassociate USER_ID PACKAGE MAC_ADDRESS";

        ShellCmd() {
            getContext().enforceCallingOrSelfPermission(
                    android.Manifest.permission.MANAGE_COMPANION_DEVICES, "ShellCmd");
        }

        @Override
        public int onCommand(String cmd) {
            try {
                switch (cmd) {
                    case "list": {
                        forEach(
                                getAllAssociations(getNextArgInt()),
                                a -> getOutPrintWriter()
                                        .println(a.getPackageName() + " "
                                                + a.getDeviceMacAddress()));
                    }
                    break;

                    case "associate": {
                        int userId = getNextArgInt();
                        String packageName = getNextArgRequired();
                        String address = getNextArgRequired();
                        createAssociationInternal(userId, address, packageName, null);
                    }
                    break;

                    case "disassociate": {
                        removeAssociation(getNextArgInt(), getNextArgRequired(),
                                getNextArgRequired());
                    }
                    break;

                    case "simulate_connect": {
                        onDeviceConnected(getNextArgRequired());
                    }
                    break;

                    case "simulate_disconnect": {
                        onDeviceDisconnected(getNextArgRequired());
                    }
                    break;

                    default:
                        return handleDefaultCommands(cmd);
                }
                return 0;
            } catch (Throwable t) {
                Slog.e(LOG_TAG, "Error running a command: $ " + cmd, t);
                getErrPrintWriter().println(Log.getStackTraceString(t));
                return 1;
            }
        }

        private int getNextArgInt() {
            return Integer.parseInt(getNextArgRequired());
        }

        @Override
        public void onHelp() {
            getOutPrintWriter().println(USAGE);
        }
    }

    private class BluetoothDeviceConnectedListener
    private class BluetoothDeviceConnectedListener
            extends BluetoothAdapter.BluetoothConnectionCallback {
            extends BluetoothAdapter.BluetoothConnectionCallback {
        @Override
        @Override
+99 −0
Original line number Original line 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.companion;

import static com.android.internal.util.CollectionUtils.forEach;
import static com.android.server.companion.CompanionDeviceManagerService.LOG_TAG;

import android.util.Log;
import android.util.Slog;

import java.io.PrintWriter;

class CompanionDeviceShellCommand extends android.os.ShellCommand {
    private final CompanionDeviceManagerService mService;

    CompanionDeviceShellCommand(CompanionDeviceManagerService service) {
        mService = service;
    }

    @Override
    public int onCommand(String cmd) {
        try {
            switch (cmd) {
                case "list": {
                    forEach(
                            mService.getAllAssociations(getNextArgInt()),
                            a -> getOutPrintWriter()
                                    .println(a.getPackageName() + " "
                                            + a.getDeviceMacAddress()));
                }
                break;

                case "associate": {
                    int userId = getNextArgInt();
                    String packageName = getNextArgRequired();
                    String address = getNextArgRequired();
                    mService.createAssociationInternal(userId, address, packageName, null);
                }
                break;

                case "disassociate": {
                    mService.removeAssociation(getNextArgInt(), getNextArgRequired(),
                            getNextArgRequired());
                }
                break;

                case "simulate_connect": {
                    mService.onDeviceConnected(getNextArgRequired());
                }
                break;

                case "simulate_disconnect": {
                    mService.onDeviceDisconnected(getNextArgRequired());
                }
                break;

                default:
                    return handleDefaultCommands(cmd);
            }
            return 0;
        } catch (Throwable t) {
            Slog.e(LOG_TAG, "Error running a command: $ " + cmd, t);
            getErrPrintWriter().println(Log.getStackTraceString(t));
            return 1;
        }
    }

    private int getNextArgInt() {
        return Integer.parseInt(getNextArgRequired());
    }

    @Override
    public void onHelp() {
        PrintWriter pw = getOutPrintWriter();
        pw.println("Companion Device Manager (companiondevice) commands:");
        pw.println("  help");
        pw.println("      Print this help text.");
        pw.println("  list USER_ID");
        pw.println("      List all Associations for a user.");
        pw.println("  associate USER_ID PACKAGE MAC_ADDRESS");
        pw.println("      Create a new Association.");
        pw.println("  disassociate USER_ID PACKAGE MAC_ADDRESS");
        pw.println("      Remove an existing Association.");
    }
}