Android

03dumpsys命令分析:添加dumpsys的案例

2018-11-15  本文已影响27人  泽洛灬

添加adb shell dumpsys recovery reset-factory

  1. 查询recovery对应的系统服务

通过第二节的描述,直接在Context类中找到recovery字段的定义

public static final String RECOVERY_SERVICE = "recovery";

接下来在SystemServiceRegistry中寻找对应的service

registerService(Context.RECOVERY_SERVICE, RecoverySystem.class,
                //service的封装类
        new CachedServiceFetcher<RecoverySystem>() {
    @Override
    public RecoverySystem createService(ContextImpl ctx) throws ServiceNotFoundException {
        IBinder b = ServiceManager.getServiceOrThrow(Context.RECOVERY_SERVICE);
        //继承自如下类的service将是我们需要寻找的service
        //IRecoverySystem.Stub
        IRecoverySystem service = IRecoverySystem.Stub.asInterface(b);
        return new RecoverySystem(service);
    }});

最终定位到RecoverySystemService类中,接下来尝试添加指令方法.

  1. 添加相应的dump方法实现
   /**
     * 响应recovery reset-factory指令,执行重置操作
     *
     * @param fd 管道通信的上层输出端
     * @param pw 创建的文件输出流,直接调用print方法写文件
     * @param args 命令行参数数组,对应recovery之后的部分,以空白描述符为分隔符
     */
   @Override
   protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
        if (args!=null&&args.length>0) {
            //取参数,参数错误时输出帮助信息
            if (args[0].equals("reset-factory")) {
                //输出信息表示正在执行操作
                pw.println("Reset to factory");
                try{
                    //RecoverySystem.rebootWipeUserData(mContext, false, "reset-factory", true, false);
                    synchronized (sRequestLock) {
                        String command = "--wipe_data\n--reason=reset-factory\n--locale=zh-CN";
                        if (!setupOrClearBcbInternal(true, command)) {
                            return;
                        }
                        // 创建执行重启至recovery模式的runnable
                        Runnable runnable = new Runnable() {
                            @Override
                            public void run() {
                                ShutdownThread.reboot(getUiContext(), PowerManager.REBOOT_RECOVERY, false);
                            }
                        };

                        // 需要在UI线程中进行该操作,这里获取UiThread的handler然后将执行操作的runnable发送至UiThread的消息队列
                        Message msg = Message.obtain(UiThread.getHandler(), runnable);
                        msg.setAsynchronous(true);
                        UiThread.getHandler().sendMessage(msg);
                    }
                }catch(Exception e){
                    Slog.e(TAG, "rebootWipeUserData failed", e);
                    pw.println("reset failed,"+e.getMessage());
                }
                return;
            } else {
                // 参数不正确时打印帮助信息
                pw.println("Usage: 'dumpsys recovery reset-factory'");
                pw.println("Restore the phone to factory Settings");
            }
        }
    }
上一篇 下一篇

猜你喜欢

热点阅读