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

Commit 4f2e645c authored by Soonil Nagarkar's avatar Soonil Nagarkar
Browse files

Add location shell commands

Test: manual
Change-Id: I7652e863873d3d7947c407605a87bf02b51472e4
parent b66a027f
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
@@ -161,9 +161,13 @@ public class ServiceWatcher implements ServiceConnection {

        @Override
        public String toString() {
            if (component == null) {
                return "none";
            } else {
                return component.toShortString() + "@" + version + "[u" + userId + "]";
            }
        }
    }

    private final Context mContext;
    private final Handler mHandler;
+9 −0
Original line number Diff line number Diff line
@@ -70,6 +70,7 @@ import android.os.CancellationSignal;
import android.os.Handler;
import android.os.IBinder;
import android.os.ICancellationSignal;
import android.os.ParcelFileDescriptor;
import android.os.PowerManager;
import android.os.PowerManager.ServiceType;
import android.os.PowerManagerInternal;
@@ -2652,6 +2653,14 @@ public class LocationManagerService extends ILocationManager.Stub {
        return manager.getMockProviderRequests();
    }

    @Override
    public int handleShellCommand(ParcelFileDescriptor in, ParcelFileDescriptor out,
            ParcelFileDescriptor err, String[] args) {
        return new LocationShellCommand(this).exec(
                this, in.getFileDescriptor(), out.getFileDescriptor(), err.getFileDescriptor(),
                args);
    }

    @Override
    protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
        if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) {
+85 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 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.location;

import android.os.BasicShellCommandHandler;
import android.os.UserHandle;

import java.io.PrintWriter;
import java.util.Objects;

/**
 * Interprets and executes 'adb shell cmd location [args]'.
 */
class LocationShellCommand extends BasicShellCommandHandler {

    private final LocationManagerService mService;

    LocationShellCommand(LocationManagerService service) {
        mService = Objects.requireNonNull(service);
    }

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

        switch (cmd) {
            case "set-location-enabled": {
                int userId = parseUserId();
                boolean enabled = Boolean.parseBoolean(getNextArgRequired());
                mService.setLocationEnabledForUser(enabled, userId);
                return 0;
            }
            case "send-extra-command": {
                String provider = getNextArgRequired();
                String command = getNextArgRequired();
                mService.sendExtraCommand(provider, command, null);
                return 0;
            }
            default:
                return handleDefaultCommands(cmd);
        }
    }

    private int parseUserId() {
        final String option = getNextOption();
        if (option != null) {
            if (option.equals("--user")) {
                return UserHandle.parseUserArg(getNextArgRequired());
            } else {
                throw new IllegalArgumentException(
                        "Expected \"--user\" option, but got \"" + option + "\" instead");
            }
        }

        return UserHandle.USER_CURRENT_OR_SELF;
    }

    @Override
    public void onHelp() {
        PrintWriter pw = getOutPrintWriter();
        pw.println("Location service commands:");
        pw.println("  help or -h");
        pw.println("    Print this help text.");
        pw.println("  set-location-enabled [--user <USER_ID>] true|false");
        pw.println("    Sets the master location switch enabled state.");
        pw.println("  send-extra-command <PROVIDER> <COMMAND>");
        pw.println("    Sends the given extra command to the given provider.");
    }
}