framework添加cmds命令

在Android的shell命令下,我们可以输入input,pm,svc等命令。

在Android源码中位于frameworks/base/cmds。拿最简单的svc进行举例,./frameworks/base/cmds/svc/src/com/android/commands/svc/Svc.java

package com.android.commands.svc;

public class Svc {

    //抽象的Command对象
    public static abstract class Command {
        private String mName;
        public Command(String name) {
            mName = name;
        }

        public String name() {
            return mName;
        }
        //显示简要的帮助信息
        public abstract String shortHelp();
        //显示详细的帮助信息
        public abstract String longHelp();
        //执行测试命令
        public abstract void run(String[] args);
    }

    public static void main(String[] args) {
        if (args.length >= 1) {
            Command c = lookupCommand(args[0]);
            if (c != null) {
                c.run(args);
                return;
            }
        }
        COMMAND_HELP.run(args);
    }

    //匹配Command对象
    private static Command lookupCommand(String name) {
        final int N = COMMANDS.length;
        for (int i=0; i<N; i++) {
            Command c = COMMANDS[i];
            if (c.name().equals(name)) {
                return c;
            }
        }
        return null;
    }

    //显示帮助信息
    public static final Command COMMAND_HELP = new Command("help") {
        public String shortHelp() {
            return "Show information about the subcommands";
        }
        public String longHelp() {
            return shortHelp();
        }
        public void run(String[] args) {
            if (args.length == 2) {
                Command c = lookupCommand(args[1]);
                if (c != null) {
                    System.err.println(c.longHelp());
                    return;
                }
            }

            System.err.println("Available commands:");
            final int N = COMMANDS.length;
            int maxlen = 0;
            for (int i=0; i<N; i++) {
                Command c = COMMANDS[i];
                int len = c.name().length();
                if (maxlen < len) {
                    maxlen = len;
                }
            }
            String format = "    %-" + maxlen + "s    %s";
            for (int i=0; i<N; i++) {
                Command c = COMMANDS[i];
                System.err.println(String.format(format, c.name(), c.shortHelp()));
            }
        }
    };

    //定义的Command对象
    public static final Command[] COMMANDS = new Command[] {
            COMMAND_HELP,
            new PowerCommand(),
            new DataCommand(),
            new WifiCommand(),
            new UsbCommand(),
            new NfcCommand(),
            new LedStatusCommand(),
    };
}

我们在来看一个简单的Command实现

package com.android.commands.svc;

import android.content.Context;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.app.ILedStatusManager;

public class LedStatusCommand extends Svc.Command {
    //设置Command名称
    public LedStatusCommand() {
        super("led");
    }
    //显示简要的帮助信息
    public String shortHelp() {
        return "Control LedStatusmanager";
    }
    //显示具体的帮助信息
    public String longHelp() {
        return shortHelp() + "\n"
                + "\n"
                + " getLedMode:\n"
                + "    get led mode";
                + "\n"
                + " getLedLevel:\n"
                + "    get led level";
    }
    //svc led getLedMode m
    //svc args[0] args[1] args[2] ...
    public void run(String[] args) {
        try {
            if (args.length >= 1) {
                ILedStatusManager manager=ILedStatusManager
                .Stub
                .asInterface(ServiceManager.getService(Context.LED_STATUS_SERVICE));
                if ("getLedMode".equals(args[1])) {
                    int curr=manager.getLedMode();
                    System.out.println(curr);
                }else if ("getLedLevel".equals(args[1])) {
                    int curr=manager.getLedLevel();
                    System.out.println(curr);
                }else if ("setLedState".equals(args[1])&&args.length>3) {
                    int mode=Integer.parseInt(args[2]);
                    int level=Integer.parseInt(args[3]);
                    int curr=manager.setLedState(mode,level);
                    System.out.println(curr);
                }
            }
        }catch (Exception e) {
                System.err.println("Faild to set setting: " + e);
        }
    }
}

此时我们将自己定义的LedStatusCommand添加到scv中的COMMANDS即可。

通过学习这个技能在修改framework源码时,可以使用这个功能在shell下快速的进行调试辅助开发。

发表回复

CAPTCHAis initialing...