pm 源码分析

来源:互联网 发布:机械模块化设计软件 编辑:程序博客网 时间:2024/09/21 11:16
platform_frameworks_base/cmds/pm/Android.mk
从下面可以看出pm是一个可执行bin档。
LOCAL_PATH:= $(call my-dir)


include $(CLEAR_VARS)
LOCAL_SRC_FILES := $(call all-subdir-java-files)
LOCAL_MODULE := pmlib
LOCAL_MODULE_STEM := pm
include $(BUILD_JAVA_LIBRARY)


include $(CLEAR_VARS)
LOCAL_MODULE := pm
LOCAL_MODULE_CLASS := EXECUTABLES
LOCAL_SRC_FILES := pm
LOCAL_REQUIRED_MODULES := pmlib
include $(BUILD_PREBUILT)
具体pm 要怎么用呢?可以看
platform_frameworks_base/cmds/pm/src/com/android/commands/pm/pm.java 中的showUsage方法。
如下所示:
private static void showUsage() {
        System.err.println("usage: pm list packages [-f] [-d] [-e] [-s] [-3] [-i] [-u] [FILTER]");
        System.err.println("       pm list permission-groups");
        System.err.println("       pm list permissions [-g] [-f] [-d] [-u] [GROUP]");
        System.err.println("       pm list instrumentation [-f] [TARGET-PACKAGE]");
        System.err.println("       pm list features");



}
当执行pm list features是首先会跑pm.java的main函数.
    public static void main(String[] args) {
        new Pm().run(args);
    }
直接调用run方法。
   public void run(String[] args) {
        boolean validCommand = false;
        if (args.length < 1) {
            showUsage();
            return;
        }
        mPm = IPackageManager.Stub.asInterface(ServiceManager.getService("package"));
        if (mPm == null) {
            System.err.println(PM_NOT_RUNNING_ERR);
            return;
        }
        mArgs = args;
        String op = args[0];
        mNextArg = 1;
        if ("list".equals(op)) {
            runList();
            return;
        }
        if ("path".equals(op)) {
            runPath();
            return;
        }
        if ("install".equals(op)) {
            runInstall();
            return;
        }
pm support的所有命令都是在这个run方法中实现。我们以pm list features。会走runList();
    private void runList() {
        String type = nextArg();
        if (type == null) {
            System.err.println("Error: didn't specify type of data to list");
            showUsage();
            return;
        }
        if ("package".equals(type) || "packages".equals(type)) {
            runListPackages(false);
        } else if ("permission-groups".equals(type)) {
            runListPermissionGroups();
        } else if ("permissions".equals(type)) {
            runListPermissions();
        } else if ("features".equals(type)) {
            runListFeatures();
继续调用runListFeatures()
 private void runListFeatures() {
        try {
            List<FeatureInfo> list = new ArrayList<FeatureInfo>();
            FeatureInfo[] rawList = mPm.getSystemAvailableFeatures();
            for (int i=0; i<rawList.length; i++) {
                list.add(rawList[i]);
            }
            // Sort by name
            Collections.sort(list, new Comparator<FeatureInfo>() {
                public int compare(FeatureInfo o1, FeatureInfo o2) {
                    if (o1.name == o2.name) return 0;
                    if (o1.name == null) return -1;
                    if (o2.name == null) return 1;
                    return o1.name.compareTo(o2.name);
                }
            });
            int count = (list != null) ? list.size() : 0;
            for (int p = 0; p < count; p++) {
                FeatureInfo fi = list.get(p);
                System.out.print("feature:");
                if (fi.name != null) System.out.println(fi.name);
                else System.out.println("reqGlEsVersion=0x"
                        + Integer.toHexString(fi.reqGlEsVersion));
            }
        } catch (RemoteException e) {
            System.err.println(e.toString());
            System.err.println(PM_NOT_RUNNING_ERR);
        }
    }
这个函数调用pm的getSystemAvailableFeatures() 来返回系统所有的feature,保存在rawList 数组中,然后在rawList 中的每一项加到list列表中。通过Collections.sort来排序一下.最后通过for循环打印。


所以不用死机pm的命令,需要用的的时候,可以看源码就行了.
0 0