DRouter的底层实现浅析

前言

DRouter是滴滴乘客端自研的一套Android路由框架,基于平台化解耦的思想,为组件间通信服务。该项目以功能全面、易用为原则,支持各种路由场景,在页面路由、服务获取和过滤、跨进程及跨应用、VirtualApk插件支持等方面都能提供多样化的服务。目前已在滴滴乘客端、顺风车、单车、国际化、滴滴定制车中控、滴滴车载屏等十多个滴滴的app内使用,得到各种场景的验证。

接入方式与使用

接入

github上的项目地址:github.com/didi/DRoute…

在项目根目录下的build.gradle下添加插件依赖:

buildscript {    
	dependencies {        
		classpath "io.github.didi:drouter-plugin-proxy:1.0.1"
	}
}

在主module的build.gradle文件中应用插件:

plugins {
    id 'com.didi.drouter'
}

在主module的build.gradle文件中的dependencies中添加依赖:

dependencies {
		api "io.github.didi:drouter-api:2.1.0"
}

引入成功后,需要在Application中进行初始化:

DRouter.init(application);

使用方法

这里只做简述,详情参考官方文档:github.com/didi/DRoute…

Activity、Fragment、View

静态注册

Activity、Fragment、View只支持静态跳转。在Activity注册路由地址:

@Router(scheme = "didi", host = "router", path = "/test1")
class TestActivity1 : AppCompatActivity()
发起页面跳转
            DRouter.build("didi://router/test1")
                .putExtra("tag", this.javaClass.name)
                .start(this, object : RouterCallback.ActivityCallback() {
                    // Activity回调
                    override fun onActivityResult(resultCode: Int, data: Intent?) {
                    }
                })

Handler

Handler的典型应用场景是:

  1. 端外、Push、h5调用native代码
  2. 无需下沉接口的组件化通信,有数据返回能力
  3. 实现增强版eventbus

Handler支持静态和动态两种注册方式。

静态注册
@Router(scheme = "didi", host = "router", path = "/sendOrder",
        thread = Thread.WORKER)   //指定执行线程
public class SendOrderHandler implements IRouterHandler {
    @Override
    void handle(@NonNull Request request, @NonNull Result result);
        // result可以添加数据,会返回给调用方
        // 如果需要拦截后面的所有结点可以执行,默认不拦截
        request.getInterceptor().onInterrupt();
    }
}
动态定义和注册

在内部注册一个监听者,不会重新实例化,类似EventBus,比EventBus增加了数据返回的能力 如果注册时使用了lifecycleOwner,会自动解注册

// 动态注册
IRegister register = 
       DRouter.register(
              RouterKey.build("/dynamic/handler").setLifecycleOwner(lifecycleOwner), 
              new IRouterHandler() {
                     @Override
                     public void handle(@NonNull Request request, @NonNull Result result) {
                     }
              });
// 解注册,如果注册时使用了生命周期,则可省
register.unregister();
发起导航
DRouter.build("didi://router/sendOrder")
       .setHoldTimeout(3000)         // 设置超时时间内未返回则自动回调callback
       .start(context, new RouterCallback() {
            @Override
            public void onResult(@NonNull Result result) {
                // 只有目标被释放后才会回调,且不会阻塞该线程
            }
       });

Hold

正常情况下Activity被start后以及RouterHandler的handle方法执行完成后,RouterCallback中的Result会立刻返回,如果希望RouterCallback中的Result可以等待目标某个触发时机然后才返回Result,同时不阻塞当前线程,可以考虑暂存Result

应用场景
  • 启动登录页,等待用户真正登陆成功后回调
  • 用户端外冷启动App,需要等待MainActivity的onResume回调或某些事情准备好后再回调
  • RouterHandler有耗时任务,希望等待执行完后再返回给调用方,又不阻塞调用方
配置

Activity和RouterHandler增加hold参数,就启用了HoldResult

异步Activity
@Router(scheme = "didi", host = "router", path = "/login"
        hold = "true")
public class ActivityLogin extends Activity {

    @Override
    protected void onCreate() {
        super.onCreate();
        Request request = RouterHelper.getRequest(this);
        Result result = RouterHelper.getResult(this);

        // 根据业务,在某个时间点触发这个释放(必须有)
        RouterHelper.release(this);
    }
}
异步RouterHandler
@Router(scheme = "didi", host = "router", path = "/sendOrder",
        hold = "true")
public class SendOrderHandler implements IRouterHandler {
    @Override
    void handle(@NonNull Request request, @NonNull Result result);

        // 根据业务,在某个时间点触发这个释放(必须有)
        RouterHelper.release(request);
    }
}
发起导航
DRouter.build("didi://router/sendOrder")
       .setHoldTimeout(3000)         // 设置超时时间内未返回则自动回调callback
       .start(context, new RouterCallback() {
            @Override
            public void onResult(@NonNull Result result) {
                // 只有目标被释放后才会回调,且不会阻塞该线程
            }
       });

Interceptor

拦截器可以在打开指定页面时进行一些行为,比如:

  • 目标页面需要某种权限,比如某些场景下需要判断用户是否已登录
  • 目标页面需要先进行某项操作,比如打开定位功能
定义拦截器

全局拦截器是指任意请求都会执行

@Interceptor(name = "interceptor1",          //可选,可以使用名字替代类耦合的方式来引用到
             priority = 1,                   //可选,优先级,值越大优先级越高
             global = true)                  //可选,是否是全局拦截器
public class LoginInterceptor implements IRouterInterceptor {
    @Override
    public void handle(@NonNull Request request) {

        if (isGo?) {
            request.getInterceptor().onContinue();
        } else {
            request.getInterceptor().onInterrupt();
        }
    }
}
应用到页面
@Router(path = "/main", interceptor = LoginInterceptor.class)
public class MainActvity extends Activity {}

源码分析

初始化流程

上述为DRouter的初始化流程图,上述为DRouter的初始化流程图,DRouter的项目结构为:

  • drouter-api:直接供业务层调用
  • drouter-api-stub:定义了MetaLoader和它的实现类,在编译期生成路由表代码
  • drouter-plugin:真正执行DRouter构建流程的插件,执行RouterTask去创建RouterLoader的真正实现并处理注解,加载路由表。
  • drouter-plugin-proxy:是一个Gradle Task,主要任务是拉取最新版本的drouter-plugin.jar包

其中drouter-api是直接供业务层调用的module,在api module中,依赖了drouter-api-stub:

    compileOnly project(':drouter-api-stub')

只在编译时有效,不会参与打包 可以在自己的moudle中使用该方式依赖一些比如com.android.support,gson这些使用者常用的库,避免冲突。

drouter-api-stub中,定义了MetaLoader和它的实现类:RouterLoader、ServiceLoader和InterceptorLoader,而这些子类的方法都是空实现,在编译期,会通过javaassit API和Gradle Transform API来实现具体的方法。

项目中直接引用的是drouter-plugin-proxy,根目录下的build.gradle中:

classpath 'io.github.didi:drouter-plugin-proxy:1.0.1'

drouter-plugin-proxy是一个Gradle Task,主要任务是拉取最新版本的drouter-plugin.jar包

drouter-plugin内部是真正执行DRouter构建流程的插件,执行RouterTask去创建RouterLoader的真正实现并处理注解,加载路由表。

所以DRouter的初始化,可以从编译和运行两个时期去分析。

编译期

通过Transform在项目编译过程中在class编译为dex的过程中,能够获取到所有的class类,并进行处理。DRouter用到了groovy插件 + Transform API在编译期自动生成了路由表。首先,在drouter-plugin-proxy module中,创建了一个RouterPlugin:

class RouterPlugin implements Plugin<Project> {
    @Override
    void apply(Project project) {
        ProxyUtil.confirm(project)
        project.extensions.create('drouter', RouterSetting)
        project.android.registerTransform(new TransformProxy(project))
    }
}

RouterPlugin最后注册了一个TransformProxy:

class TransformProxy extends Transform {
    // ... 
  	@Override
    void transform(TransformInvocation invocation) throws TransformException, 
  			InterruptedException, IOException {
        // 插件版本
        String pluginVersion = ProxyUtil.getPluginVersion(invocation)
        if (pluginVersion != null) {
            File pluginJar = new File(project.rootDir, ".gradle/drouter/drouter-plugin-${pluginVersion}.jar")
            if (!pluginJar.exists()) { // 若不存在,尝试下载
                // ...
            }
            if (pluginJar.exists()) {
                URLClassLoader newLoader = new URLClassLoader([pluginJar.toURI().toURL()] as URL[], getClass().classLoader)
                Class<?> transformClass = newLoader.loadClass("com.didi.drouter.plugin.RouterTransform") //【1】
                ClassLoader threadLoader = Thread.currentThread().getContextClassLoader()
                Thread.currentThread().setContextClassLoader(newLoader)
                Constructor constructor = transformClass.getConstructor(Project.class)
                Transform transform = (Transform) constructor.newInstance(project)
                transform.transform(invocation)	
                Thread.currentThread().setContextClassLoader(threadLoader)
                return
            } else {
                ProxyUtil.Logger.e("Error: there is no drouter-plugin jar")
            }
        } else {
            ProxyUtil.Logger.e("Error: there is no drouter-plugin version")
        }
        copyFile(invocation)
    }
}

TransformProxy在下载最新的drouter-plugin插件,并在【1】处,创建了一个com.didi.drouter.plugin.RouterTransform对象,并执行了transform(invocation) ,查看RouterTransform的代码:

class RouterTransform extends Transform {
    @Override
    void transform(TransformInvocation invocation) throws TransformException, InterruptedException, IOException {
        long timeStart = System.currentTimeMillis()
        SystemUtil.debug = project.drouter.debug
        File cacheFile = new File(tmpDir, "cache")
        boolean configChanged = SystemUtil.configChanged(project)
        boolean useCache = !isWindow && invocation.incremental && project.drouter.cache && cacheFile.exists() && !configChanged
        if (useCache) {
            cachePathSet.addAll(cacheFile.readLines())
        }
        if (!invocation.incremental) {
            invocation.outputProvider.deleteAll()
        }
        compilePath = new ConcurrentLinkedQueue<>(project.android.bootClasspath)
        for (TransformInput transformInput : invocation.inputs) {
            handleDirectory(invocation, transformInput)
            handleJar(invocation, transformInput)
        }
        File dest = invocation.outputProvider.getContentLocation("DRouterTable", TransformManager.CONTENT_CLASS,
                ImmutableSet.of(QualifiedContent.Scope.PROJECT), Format.DIRECTORY) // 【2】
        (new RouterTask(project, compilePath, cachePathSet, useCache, dest, tmpDir, project.drouter, isWindow)).run()	// 【3】
        FileUtils.writeLines(cacheFile, cachePathSet)
        Logger.v("Link: https://github.com/didi/DRouter")
        Logger.v("DRouterTask done, time used: " + (System.currentTimeMillis() - timeStart) / 1000f  + "s")
    }
}

【2】处创建了一个DRouterTable目录,供RouterTask使用,最终执行到RouterTask的run方法:

    void run() {
        StoreUtil.clear();
        JarUtils.printVersion(project, compileClassPath);
        pool = new ClassPool();
        classClassify = new ClassClassify(pool, setting);
        startExecute();
    }

    private void startExecute() {
        try {
            long timeStart = System.currentTimeMillis();
            for (File file : compileClassPath) {
                pool.appendClassPath(file.getAbsolutePath());
            }
            if (useCache) {
                loadCachePaths(cachePathSet);
            } else {
                loadFullPaths(compileClassPath);
            }
            timeStart = System.currentTimeMillis();
            classClassify.generatorRouter(routerDir); // 【4】
        } catch (Exception e) {
            JarUtils.check(e);
            String message = e.getMessage();
            if (message == null || !message.startsWith("Class:")) {
                e.printStackTrace();
            }
            throw new GradleException("DRouter: Could not generate router tablen" + e.getMessage());
        } finally {
            executor.shutdown();
            FileUtils.deleteQuietly(wTmpDir);
        }
    }

【4】处ClassClassify的generatorRouter方法:

    public void generatorRouter(File routerDir) throws Exception {
        for (int i = 0; i < classifies.size(); i++) {
            AbsRouterCollect cf = classifies.get(i);
            cf.generate(routerDir);
        }
    }

AbsRouterCollect有三个实现类:

  • RouterCollect
  • InterceptorCollect
  • ServiceCollect

ClassClassify在初始化时就添加了这三个:

public class ClassClassify {
		private List<AbsRouterCollect> classifies = new ArrayList<>();

    public ClassClassify(ClassPool pool, RouterSetting setting) {
        // 初始化时就加入了三种AbsRouterCollect
        classifies.add(new RouterCollect(pool, setting));
        classifies.add(new ServiceCollect(pool, setting));
        classifies.add(new InterceptorCollect(pool, setting));
    }
		// ...
}

这里以RouterCollect为例,RouterCollect的generate方法实现为:

    @Override
    public void generate(File routerDir) throws Exception {
      	// 创建RouterLoader类 
        CtClass ctClass = pool.makeClass(getPackageName() + ".RouterLoader");
        CtClass superClass = pool.get("com.didi.drouter.store.MetaLoader");
        ctClass.setSuperclass(superClass);

        StringBuilder builder = new StringBuilder();
        builder.append("public void load(java.util.Map data) {n");
        for (CtClass routerCc : routerClass.values()) {
            try {
                StringBuilder interceptorClass = null;
                StringBuilder interceptorName = null;

                String uriValue = "";
                String schemeValue = "";
                String hostValue = "";
                String pathValue = "";
                Annotation annotation = null;
                String type;
                int thread = 0;
                int priority = 0;
                boolean hold = false;
                if (routerCc.hasAnnotation(Router.class)) {
                    uriValue = ((Router) routerCc.getAnnotation(Router.class)).uri();
                    schemeValue = ((Router) routerCc.getAnnotation(Router.class)).scheme();
                    hostValue = ((Router) routerCc.getAnnotation(Router.class)).host();
                    pathValue = ((Router) routerCc.getAnnotation(Router.class)).path();
                    thread = ((Router) routerCc.getAnnotation(Router.class)).thread();
                    priority = ((Router) routerCc.getAnnotation(Router.class)).priority();
                    hold = ((Router) routerCc.getAnnotation(Router.class)).hold();
                    annotation = getAnnotation(routerCc, Router.class);
                    if (checkSuper(routerCc, "android.app.Activity")) {
                        type = "com.didi.drouter.store.RouterMeta.ACTIVITY";
                    } else if (checkSuper(routerCc,
                            "android.support.v4.app.Fragment", "androidx.fragment.app.Fragment")) {
                        type = "com.didi.drouter.store.RouterMeta.FRAGMENT";
                    } else if (checkSuper(routerCc, "android.view.View")) {
                        type = "com.didi.drouter.store.RouterMeta.VIEW";
                    } else if (checkSuper(routerCc, "com.didi.drouter.router.IRouterHandler")) {
                        type = "com.didi.drouter.store.RouterMeta.HANDLER";
                    } else {
                        throw new Exception("@Router target class illegal, " +
                                "support only Activity/Fragment/View/IRouterHandler");
                    }
                } else {
                    pathValue = "/" + routerCc.getName().replace(".", "/");
                    type = "com.didi.drouter.store.RouterMeta.ACTIVITY";
                }
                // ... 
              	if (isAnyRegex) {
                    items.add("    put("" + uri + "", " + metaBuilder + ", data); n");
                    //builder.append("    put("").append(uri).append("", ").append(metaBuilder).append(", data); n");
                } else {
                    items.add("    data.put("" + uri + "", " + metaBuilder + "); n");
                    //builder.append("    data.put("").append(uri).append("", ").append(metaBuilder).append("); n");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        Collections.sort(items);
        for (String item : items) {
            builder.append(item);
        }
        builder.append("}");

        Logger.d("nclass RouterLoader" + "n" + builder.toString());
        generatorClass(routerDir, ctClass, builder.toString());
    }

在项目主目录(一般是app, 这里用的是官方demo)下的demo/build/intermediates/transforms/DRouter/debug/...中查看生成的class类:

public class RouterLoader extends MetaLoader {
    @Override
    public void load(Map var1) {
        var1.put("@@$$/activity/dynamic", RouterMeta.build(RouterMeta.ACTIVITY).assembleRouter("", "", "/activity/dynamic", "com.didi.demo.handler.DynamicActivity", (IRouterProxy)null, (Class[])null, (String[])null, 0, 0, false));
        var1.put("@@$$/activity/remote", RouterMeta.build(RouterMeta.ACTIVITY).assembleRouter("", "", "/activity/remote", "com.didi.demo.remote.RemoteActivity", (IRouterProxy)null, (Class[])null, (String[])null, 0, 0, false));
        var1.put("@@$$/activity/result", RouterMeta.build(RouterMeta.ACTIVITY).assembleRouter("", "", "/activity/result", "com.didi.demo.activity.ActivityResultActivity", (IRouterProxy)null, (Class[])null, (String[])null, 0, 0, false));
        var1.put("@@$$/activity/router_page_single", RouterMeta.build(RouterMeta.ACTIVITY).assembleRouter("", "", "/activity/router_page_single", "com.didi.demo.fragment.RouterPageSingleActivity", (IRouterProxy)null, (Class[])null, (String[])null, 0, 0, false));
        var1.put("@@$$/activity/router_page_stack", RouterMeta.build(RouterMeta.ACTIVITY).assembleRouter("", "", "/activity/router_page_stack", "com.didi.demo.fragment.RouterPageStackActivity", (IRouterProxy)null, (Class[])null, (String[])null, 0, 0, false));
        var1.put("@@$$/activity/router_page_viewpager", RouterMeta.build(RouterMeta.ACTIVITY).assembleRouter("", "", "/activity/router_page_viewpager", "com.didi.demo.fragment.RouterPageViewPagerActivity", (IRouterProxy)null, (Class[])null, (String[])null, 0, 0, false));
        var1.put("@@$$/activity/test3", RouterMeta.build(RouterMeta.ACTIVITY).assembleRouter("", "", "/activity/test3", "com.didi.demo.activity.ActivityTest3", (IRouterProxy)null, (Class[])null, (String[])null, 0, 0, true));
        var1.put("@@$$/activity/webview", RouterMeta.build(RouterMeta.ACTIVITY).assembleRouter("", "", "/activity/webview", "com.didi.demo.web.WebActivity", (IRouterProxy)null, (Class[])null, (String[])null, 0, 0, false));
        var1.put("@@$$/fragment/second", RouterMeta.build(RouterMeta.FRAGMENT).assembleRouter("", "", "/fragment/second", FragmentSecond.class, new com_didi_demo_fragment_FragmentSecond(), (Class[])null, (String[])null, 0, 0, false));
        var1.put("@@$$/handler/test1", RouterMeta.build(RouterMeta.HANDLER).assembleRouter("", "", "/handler/test1", HandlerTest1.class, new com_didi_demo_handler_HandlerTest1(), (Class[])null, (String[])null, 0, 1, false));
        var1.put("@@$$/handler/test3", RouterMeta.build(RouterMeta.HANDLER).assembleRouter("", "", "/handler/test3", HandlerTest3.class, new com_didi_demo_handler_HandlerTest3(), (Class[])null, (String[])null, 2, 0, true));
        var1.put("@@$$/view/bottom", RouterMeta.build(RouterMeta.VIEW).assembleRouter("", "", "/view/bottom", BottomView.class, new com_didi_demo_view_BottomView(), (Class[])null, (String[])null, 0, 0, false));
        var1.put("@@$$/view/headview", RouterMeta.build(RouterMeta.VIEW).assembleRouter("", "", "/view/headview", HeadView.class, new com_didi_demo_view_HeadView(), (Class[])null, (String[])null, 0, 0, false));
        var1.put("[email protected]@router$$/handler/test2", RouterMeta.build(RouterMeta.HANDLER).assembleRouter("didi", "router", "/handler/test2", HandlerTest2.class, new com_didi_demo_handler_HandlerTest2(), (Class[])null, (String[])null, 0, 0, false));
        this.put("@@$$/activity/Test1_<Arg1>_<Arg2>", RouterMeta.build(RouterMeta.ACTIVITY).assembleRouter("", "", "/activity/Test1_<Arg1>_<Arg2>", "com.didi.demo.activity.ActivityTest1", (IRouterProxy)null, new Class[]{InnerInterceptor.class}, new String[]{"interceptor1", "interceptor2"}, 0, 0, false), var1);
        this.put("@@$$/fragment/first/.*", RouterMeta.build(RouterMeta.FRAGMENT).assembleRouter("", "", "/fragment/first/.*", FragmentFirst.class, new com_didi_demo_fragment_FragmentFirst(), (Class[])null, (String[])null, 0, 0, false), var1);
        this.put("@@$$/handler/.*", RouterMeta.build(RouterMeta.HANDLER).assembleRouter("", "", "/handler/.*", HandlerAll.class, new com_didi_demo_handler_HandlerAll(), (Class[])null, (String[])null, 0, 2, false), var1);
        this.put("[email protected]@www\.didi\.com$$/activity/test2", RouterMeta.build(RouterMeta.ACTIVITY).assembleRouter("didi", "www\.didi\.com", "/activity/test2", "com.didi.demo.activity.ActivityTest2", (IRouterProxy)null, (Class[])null, (String[])null, 0, 0, false), var1);
    }

    public RouterLoader() {
    }
}

编译期对RouterLoader的load方法进行了修改,将带有注解的路由信息添加到了路由表中。

运行期

从DRouter的使用方法入手,我们首先看看DRouter在初始化的时候做了什么:

    public static void init(Application application) {
        SystemUtil.setApplication(application);	// 将application储存到一个静态变量中
        RouterStore.checkAndLoad(RouterStore.HOST, true); // 【5】RouterStore.HOST = "host"
    }

查看【5】部分的实现:

    public static void checkAndLoad(final String app, boolean async) {
        if (!loadRecord.contains(app)) {
            synchronized (RouterStore.class) {
                if (!loadRecord.contains(app)) {
                    loadRecord.add(app);
                    if (!async) {
                        Log.d(RouterLogger.NAME, "DRouter start load router table sync");
                        load(app);
                    } else {
                        new Thread("drouter-table-thread") {
                            @Override
                            public void run() {
                                Log.d(RouterLogger.NAME, "DRouter start load router table in drouter-table-thread");
                                load(app);
                            }
                        }.start();
                    }
                }
            }
        }

参数app是app唯一ID,若加载记录中已存在,则不会重新加载路由表;参数async用来控制是同步还是异步加载路由表。真正的路由方法在load(app)中:

    // class RouterStore
		private static void load(String app) {
        boolean load;
        if (HOST.equals(app)) {  //  HOST = ”host“ 
            load = loadHostTable(); 	// 【6】
            initialized = true;
            latch.countDown();
        } else {
          	// 【7】
            load = loadPluginTable(app,
                    Pair.create("Router", routerMetas),
                    Pair.create("Interceptor", interceptorMetas),
                    Pair.create("Service", serviceMetas));
        }
        if (!load) {
            RouterLogger.getCoreLogger().e(
                    "DRouterTable in app "%s" not found, " +
                            "please apply drouter plugin first.", app);
        }
    }

当传入的参数app等于HOST时,加载路由表的逻辑是loadHostTable(),若不相等,则是【7】处的方法。逐个分析:

    private static boolean loadHostTable() {
        try {
            new RouterLoader().load(routerMetas);
            new InterceptorLoader().load(interceptorMetas);
            new ServiceLoader().load(serviceMetas);
        }  catch (NoClassDefFoundError e) {
            return false;
        }
        return true;
    }

另一种加载路由表的逻辑:

	private static boolean loadPluginTable(String packageName, Pair... targets) {
        try {
            for (Pair<String, Map<?, ?>> target : targets) {
              	// 通过反射构造MetaLoader
                MetaLoader loader = (MetaLoader) ReflectUtil.getInstance(
                        Class.forName(String.format("com.didi.drouter.loader.%s.%sLoader",
                                packageName, target.first))
                );
                assert loader != null;
                loader.load(target.second);
            }
        } catch (ClassNotFoundException e) {
            return false;
        }
        return true;
    }

两种方式最终都是调用了loader.load()方法,load方法在编译期已经将所有带有注解的内容自动生成到方法中了,整个DRouter初始化到此结束。

动态添加路由表

Handler支持动态添加路由表,动态加载和loadHostTable中用到的三个参数也有关系:

    // key is uriKey,value is meta, with dynamic
    // key is REGEX_ROUTER,value is map<uriKey, meta>
    private static final Map<String, Object> routerMetas = new ConcurrentHashMap<>();
    // key is interceptor impl or string name
    private static Map<Object, RouterMeta> interceptorMetas = new ConcurrentHashMap<>();
    // key is interface,value is set, with dynamic
    private static final Map<Class<?>, Set<RouterMeta>> serviceMetas = new ConcurrentHashMap<>();

routerMetas的另一种添加在register方法内:

    // 注册动态handler,key是routerKey,value是handler实例
		@NonNull
    @MainThread
    public synchronized static IRegister register(final RouterKey key, final IRouterHandler handler) {
        //...
      	// 构建meta数据
        RouterMeta meta = RouterMeta.build(RouterMeta.HANDLER).assembleRouter(
                key.uri.getScheme(), key.uri.getHost(), key.uri.getPath(),
                (Class<?>) null, null, key.interceptor, key.interceptorName,
                key.thread, 0, key.hold);
        meta.setHandler(key, handler);
      	// 若包含正则表达式或占位符
        if (meta.isRegexUri()) {
            Map<String, RouterMeta> regexMap = (Map<String, RouterMeta>) routerMetas.get(REGEX_ROUTER);
            if (regexMap == null) {
                regexMap = new ConcurrentHashMap<>();
                routerMetas.put(REGEX_ROUTER, regexMap);
            }
            regexMap.put(TextUtils.getStandardRouterKey(key.uri), meta);
        } else {
            routerMetas.put(TextUtils.getStandardRouterKey(key.uri), meta);
        }
      	// 绑定具有生命周期的对象销毁时,解除注册
        if (key.lifecycleOwner != null) {
            key.lifecycleOwner.getLifecycle().addObserver(new LifecycleObserver() {
                @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
                public void onDestroy() {
                    unregister(key, handler);
                }
            });
        }
        return new RouterRegister(key, handler);
    }

serviceMetas的添加数据方法也是register,区别是支持泛型:

  	// 注册动态服务,key是serviceKey,value是服务实例  
		@NonNull
    @MainThread
    public synchronized static <T> IRegister register(final ServiceKey<T> key, final T service) {
        // ... 
        RouterMeta meta = RouterMeta.build(RouterMeta.SERVICE).assembleService(
                null, null,
                key.alias, key.feature, 0, Extend.Cache.NO);
        meta.setService(key, service);
        key.meta = meta;
        Set<RouterMeta> metas = serviceMetas.get(key.function);
        if (metas == null) {
            metas = Collections.newSetFromMap(new ConcurrentHashMap<RouterMeta, Boolean>());
            serviceMetas.put(key.function, metas);
        }
        metas.add(meta);
        if (key.lifecycleOwner != null) {
            key.lifecycleOwner.getLifecycle().addObserver(new LifecycleObserver() {
                @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
                public void onDestroy() {
                    unregister(key, service);
                }
            });
        }
        return new RouterRegister(key, service);
    }

interceptorMetas没有动态添加逻辑,因为拦截器和Activity/Fragment/View一样没有动态注册。

调用流程

调用方式为:

DRouter.build(scheme).start(context);

build方法:

    /**
     * Navigation to router
     * there will be only one activity match at most, but may be several router handler.
     * @param uri String
     * @return request
     */
    @NonNull
    public static Request build(String uri) {
        return Request.build(uri);
    }

build方法返回了一个Request对象,查看Request.build(uri);的实现逻辑:

    public static Request build(String uri) {
        return new Request(uri == null ? Uri.EMPTY : Uri.parse(uri));
    }

Request的构造方法为:

    private Request(@NonNull Uri uri) {
        this.uri = uri;
        this.serialNumber = String.valueOf(counter.getAndIncrement());
        putExtra(REQUEST_BUILD_URI, uri.toString());
    }

然后再看看Request的start方法:

    public void start(Context context) {
        start(context, null);
    }

    public void start(Context context, RouterCallback callback) {
        this.context = context == null ? DRouter.getContext() : context;
        RouterLoader.build(this, callback).start(); //【8】
    }

真正逻辑在【8】处,先看RouterLoader的build方法:

    @NonNull
    static RouterLoader build(Request request, RouterCallback callback) {
        RouterLoader loader = new RouterLoader();
        loader.primaryRequest = request; // 保存request
        loader.callback = callback;			 // 保存callback
        return loader;
    }

RouterLoader的build方法返回了一个loader对象,看看它的start方法:

    void start() {
        if (!TextUtils.isEmpty(primaryRequest.authority)) {
            startRemote();
        } else {
            startLocal();
        }
    }

根据request的authority属性是否为空来判断是远程还是本地调用,authority的值是远程进程的 ContentProvider 权限。可以在调用跳转逻辑时进行设置:

            DRouter.build("/handler/test1")
                    .putExtra("1", 1)
                    .putExtra("2", new Bundle())
                    .putAddition("3", new ParamObject())
                    .setRemoteAuthority("com.didi.drouter.remote.demo.host")
                    .start(DRouter.getContext());

两个关键方法: startRemote 和 startLocal

startLocal
    private void startLocal() {
        TextUtils.appendExtra(primaryRequest.getExtra(), TextUtils.getQuery(primaryRequest.getUri()));
        // 按顺序,优先级优先,然后完全匹配优先
        Map<Request, RouterMeta> requestMap = makeRequest(); // 【9】
        if (requestMap.isEmpty()) {
            RouterLogger.getCoreLogger().w("warning: there is no request target match");
            new Result(primaryRequest, null, callback);
            ResultAgent.release(primaryRequest, ResultAgent.STATE_NOT_FOUND);
            return;
        }
        final Result result = new Result(primaryRequest, requestMap.keySet(), callback);
        if (requestMap.size() > 1) {
            RouterLogger.getCoreLogger().w("warning: request match %s targets", requestMap.size());
        }
        final boolean[] stopByRouterTarget = {false};
      	// 遍历request
        for (final Map.Entry<Request, RouterMeta> entry : requestMap.entrySet()) {
            if (stopByRouterTarget[0]) {
                // one by one
                ResultAgent.release(entry.getKey(), ResultAgent.STATE_STOP_BY_ROUTER_TARGET);
                continue;
            }
          	// 拦截器优先处理
            InterceptorHandler.handle(entry.getKey(), entry.getValue(), new IRouterInterceptor.IInterceptor() {
                @Override
                public void onContinue() {
                    entry.getKey().interceptor = new IRouterInterceptor.IInterceptor() {
                        @Override
                        public void onContinue() {
                        }

                        @Override
                        public void onInterrupt() {
                            RouterLogger.getCoreLogger().w(
                                    "request "%s" stop all remains requests", entry.getKey().getNumber());
                            stopByRouterTarget[0] = true;
                        }
                    };
                    // 【10】it will release branch auto when no hold or no target
                    RouterDispatcher.start(entry.getKey(), entry.getValue(), result, callback);
                    entry.getKey().interceptor = null;
                }

                @Override
                public void onInterrupt() {
                    ResultAgent.release(entry.getKey(), ResultAgent.STATE_STOP_BY_INTERCEPTOR);
                }
            });
        }
    }

【9】将request按照顺序和优先级进行排序。

    private Map<Request, RouterMeta> makeRequest() {
        Map<Request, RouterMeta> requestMap = new LinkedHashMap<>();
        // 从intent中获取序列化数据
        Parcelable parcelable = primaryRequest.getParcelable(Extend.START_ACTIVITY_VIA_INTENT);
        if (parcelable instanceof Intent) { // intent
            primaryRequest.getExtra().remove(Extend.START_ACTIVITY_VIA_INTENT); // 删除额外信息
            Intent intent = (Intent) parcelable;
            PackageManager pm = primaryRequest.getContext().getPackageManager();
            List<ResolveInfo> activities = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);  // 查找匹配的activities
            if (!activities.isEmpty()) { // 存在匹配的activity
                primaryRequest.routerType = RouterType.ACTIVITY;    // request的type设置为ACTIVITY
                requestMap.put(this.primaryRequest, RouterMeta.build(RouterType.ACTIVITY).assembleRouter(intent));   // 构建一个RouterMeta存入map
            }
        } else {
            List<RouterMeta> metas = getAllRouterMetas();   // 获取所有的路由表信息
            int index = 0;
            for (RouterMeta routerMeta : metas) {   // 开始遍历
                // 将占位符键和值注入到捆绑包中
                if (!routerMeta.injectPlaceHolder(primaryRequest.getUri(), primaryRequest.extra)) {
                    continue;
                }
                // 构建request
                Request request = createBranchRequest(
                        this.primaryRequest, metas.size() > 1, routerMeta.getRouterType(), index++);
                requestMap.put(request, routerMeta);
            }
        }
        return requestMap;
    }

然后遍历request集合,去执行request的拦截器,处理完后通过【10】RouterDispatcher进行分发:

class RouterDispatcher {
		static void start(Request request, RouterMeta meta, Result result, RouterCallback callback) {
        switch (meta.getRouterType()) {
            case RouterType.ACTIVITY:
                startActivity(request, meta, result, callback);
                break;
            case RouterType.FRAGMENT:
                startFragment(request, meta, result);
                break;
            case RouterType.VIEW:
                startView(request, meta, result);
                break;
            case RouterType.HANDLER:
                startHandler(request, meta, result, callback);
                break;
            default:
                break;
        }
    }
}

这里可以看出,只有Activity和handler支持callback,看下Activity:

    private static void startActivity(Request request, RouterMeta meta, Result result, RouterCallback callback) {
       	// ...
        boolean hasRequestCode = request.getExtra().containsKey(Extend.START_ACTIVITY_REQUEST_CODE);
        int requestCode = hasRequestCode? request.getInt(Extend.START_ACTIVITY_REQUEST_CODE) : 1024;
        if (context instanceof Activity && callback instanceof RouterCallback.ActivityCallback) { // 【11】
            ActivityCompat2.startActivityForResult((Activity) context, intent,
                    requestCode, (RouterCallback.ActivityCallback) callback);
        } else if (context instanceof Activity && hasRequestCode) {		// 【12】
            ActivityCompat.startActivityForResult((Activity) context, intent,
                    requestCode, intent.getBundleExtra(Extend.START_ACTIVITY_OPTIONS));
        } else {	// 【13】
            ActivityCompat.startActivity(context, intent, intent.getBundleExtra(Extend.START_ACTIVITY_OPTIONS));
        }
      	// 处理页面动画
        int[] anim = request.getIntArray(Extend.START_ACTIVITY_ANIMATION); 
        if (context instanceof Activity && anim != null && anim.length == 2) {
            ((Activity) context).overridePendingTransition(anim[0], anim[1]);
        }
        result.isActivityStarted = true;
      	// hold处理
        if (meta.isHold() && callback != null) {
            RouterLogger.getCoreLogger().w("request "%s" will be hold", request.getNumber());
            Monitor.startMonitor(request, result); // 【14】
        } else {
            ResultAgent.release(request, ResultAgent.STATE_COMPLETE);
        }
    }

这里的【11】、【12】、【13】处,调用了不同的Activity启动方法,当callback的类型为RouterCallback.ActivityCallback时,通过自定义的工具类ActivityCompat2.startActivityForResult启动,而12、13则是调用系统的API直接startActivity:

    static void startActivityForResult(@NonNull final Activity activity,
                                       @NonNull final Intent intent, final int requestCode,
                                       RouterCallback.ActivityCallback callback) {
        final int cur = sCount.incrementAndGet();
        sCallbackMap.put(cur, new Pair<>(new WeakReference<>(activity), callback));
        final Active active;
        if (activity instanceof FragmentActivity) { // 创建一个Fragment,绑定到activity上
            active = new HolderFragmentV4();
        } else {
            active = new HolderFragment();
        }
        active.getCompat().cur = cur;
        active.attach(activity, intent, requestCode); // 【15】
    }

这里如果Activity是FragmentActivity,创建HolderFragmentV4,否则HolderFragment,两者都实现了Active接口,并调用了attach方法:

// public static class HolderFragmentV4 extends Fragment implements Active
        @Override
        public void attach(Activity activity, Intent intent, int requestCode) {
            this.intent = intent;
            this.requestCode = requestCode;
            FragmentManager fragmentManager = ((FragmentActivity)activity).getSupportFragmentManager();
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            transaction.add(this, TAG);
            transaction.commit(); // add fragment
        }
// public static class HolderFragment extends android.app.Fragment implements Active
        @Override
        public void attach(Activity activity, Intent intent, int requestCode) {
            this.intent = intent;
            this.requestCode = requestCode;
            android.app.FragmentManager fragmentManager = activity.getFragmentManager();
            android.app.FragmentTransaction transaction = fragmentManager.beginTransaction();
            transaction.add(this, TAG);
            transaction.commit();
        }

两个fragment类型的生命周期,都会调用ActivityCompat2的对应方法:

        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            activityCompat2.onCreate(savedInstanceState);
        }

        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            activityCompat2.onActivityResult(getActivity(), resultCode, data);
        }

        @Override
        public void onSaveInstanceState(@NonNull Bundle outState) {
            super.onSaveInstanceState(outState);
            activityCompat2.onSaveInstanceState(outState);
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
            activityCompat2.onDestroy();
        }

当生命周期调用到了onActivityResult时,也会调用activityCompat2的onActivityResult方法,真正对callback进行调用:

    private void onActivityResult(Activity activity, int resultCode, Intent data) {
        RouterCallback.ActivityCallback cb;
        Pair<WeakReference<Activity>, RouterCallback.ActivityCallback> pair = sCallbackMap.get(cur);
        if (pair != null && (cb = pair.second) != null) {
            RouterLogger.getCoreLogger().d("HoldFragment ActivityResult callback success");
            cb.onActivityResult(resultCode, data); // 【16】callback进行了调用
        }
        if (pair == null || pair.first == null || pair.first.get() != activity) {
	          // HoldFragment onActivityResult warn, for host activity changed, but still callback last host
        }
        sCallbackMap.remove(cur);
        active.remove();
    }

【16】处对callback进行了调用。

而【14】处的hold导致延迟执行callback,实际是通过一个handlerpostDelay消息来延时处理的:

class Monitor {

    private static Handler timeoutHandler;

    static void startMonitor(final Request request, final Result result) {
        long period = request.holdTimeout;
        if (period > 0) {
            check();
            RouterLogger.getCoreLogger().d("monitor for request "%s" start, count down "%sms"",
                    request.getNumber(), period);
            timeoutHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    RouterExecutor.submit(new Runnable() {
                        @Override
                        public void run() {
                            ResultAgent.release(request, ResultAgent.STATE_TIMEOUT);
                        }
                    });
                }
            }, period);
        }
    }

    private static void check() {
        if (timeoutHandler == null) {
            synchronized (Monitor.class) {
                if (timeoutHandler == null) {
                    HandlerThread handlerThread = new HandlerThread("timeout-monitor-thread");
                    handlerThread.start();
                    timeoutHandler = new Handler(handlerThread.getLooper());
                }
            }
        }
    }
}

接下来是handler的实现:

    private static void startHandler(final Request request, final RouterMeta meta,
                                     final Result result, final RouterCallback callback) {
        // dynamic
        IRouterHandler handler = meta.getHandler();
        if (handler == null) {
            handler = meta.getRouterProxy() != null ? (IRouterHandler) meta.getRouterProxy().newInstance(null) : null;
        }
        if (handler != null) {
            final IRouterHandler finalHandler = handler;
            RouterExecutor.execute(meta.getThread(), new Runnable() { 
                @Override
                public void run() {
                    if (meta.isHold()) {
                        RouterLogger.getCoreLogger().w("request "%s" will hold", request.getNumber());
                    }
                    finalHandler.handle(request, result);
                    if (meta.isHold() && callback != null) {
                        Monitor.startMonitor(request, result); // 【17】 postDelay 延时调用
                    } else {
                        ResultAgent.release(request, ResultAgent.STATE_COMPLETE); // 【18】直接调用onResult
                    }
                }
            });
        } else {
            ResultAgent.release(request, ResultAgent.STATE_ERROR);
        }
    }

RouterExecutor是一个执行器,内部有一个线程池对象可以用于指定执行的线程。在runnable中,handler调用了handle方法,callback则是通过是否hold来判断是否立刻执行。

在【18】处,为什么会立即执行callback:

    static void release(Request request, String reason) {
        if (request != null) {
            release(request.getNumber(), reason);
        }
    }

    private synchronized static void release(String requestNumber, String reason) {
        Result result = getResult(requestNumber);
        if (result != null) {
            if (result.agent.primaryRequest.getNumber().equals(requestNumber)) {
                // all clear
                for (String number : result.agent.branchRequestMap.keySet()) {
                    if (!result.agent.branchReasonMap.containsKey(number)) {
                        completeBranch(number, reason);
                    }
                }
            } else {
                // branch only
                completeBranch(requestNumber, reason);
            }
            // check and release primary
            if (result.agent.branchReasonMap.size() == result.agent.branchRequestMap.size()) {
                completePrimary(result);
            }
        }
    }

这里的两个方法:

completeBranch:

    private synchronized static void completeBranch(String branchNumber, String reason) {
        Result result = numberToResult.get(branchNumber);
        if (result != null) {
            if (STATE_TIMEOUT.equals(reason)) {
               // 超时强制执行log
            }
            result.agent.branchReasonMap.put(branchNumber, reason);
            numberToResult.remove(branchNumber);
        }
    }

completePrimary

    private synchronized static void completePrimary(@NonNull Result result) {
        numberToResult.remove(result.agent.primaryRequest.getNumber());
        if (result.agent.callback != null) {
            result.agent.callback.onResult(result); // 【19】callback得到调用
        }
        if (!numberToResult.containsKey(result.agent.primaryRequest.getNumber())) {
            RouterLogger.getCoreLogger().d("Request finish ");
        }
    }

最后再分析一下View和Fragment的启动:

    private static void startFragment(Request request, RouterMeta meta, Result result) {
        result.routerClass = meta.getRouterClass();
        if (request.getExtra().getBoolean(Extend.START_FRAGMENT_NEW_INSTANCE, true)) {
            Object object = meta.getRouterProxy() != null ?
                    meta.getRouterProxy().newInstance(null) : null;
            if (object instanceof Fragment) {
                result.fragment = (Fragment) object;
                result.fragment.setArguments(request.getExtra());  
            }
        }
        ResultAgent.release(request, ResultAgent.STATE_COMPLETE);
    }

    private static void startView(Request request, RouterMeta meta, Result result) {
        result.routerClass = meta.getRouterClass();
        if (request.getExtra().getBoolean(Extend.START_VIEW_NEW_INSTANCE, true)) {
            Object object = meta.getRouterProxy() != null ?
                    meta.getRouterProxy().newInstance(request.getContext()) : null;
            if (object instanceof View) {
                result.view = (View) object;
                result.view.setTag(request.getExtra());
            }
        }
        ResultAgent.release(request, ResultAgent.STATE_COMPLETE);
    }

View和Fragment的实现逻辑基本一致,创建一个对象保存到result中。

最后是远程调用startRemote:

    private void startRemote() {
        Result result = new Result(primaryRequest, Collections.singleton(primaryRequest), callback);
        RemoteBridge.load(primaryRequest.authority, primaryRequest.resendStrategy,
                primaryRequest.lifecycleOwner != null ? new WeakReference<>(primaryRequest.lifecycleOwner) : null)
                .start(primaryRequest, result, callback);
    }

RemoteBridge的load方法保存了远程调用的相关数据,start方法真正进行了调用:

    public void start(final Request request, final Result result, RouterCallback callback) {
        final RemoteCommand command = new RemoteCommand(RemoteCommand.REQUEST);
        command.bridge = RemoteBridge.this;
        command.resendStrategy = resendStrategy;
        command.lifecycle = lifecycle;
        command.uri = request.getUri().toString();
        command.extra = request.getExtra();
        command.addition = request.getAddition();
        if (callback != null) {
            command.binder = new IClientService.Stub() {
                @Override
                public RemoteResult callback(RemoteCommand resultCommand) {
                    result.extra = resultCommand.extra;
                    result.addition = resultCommand.addition;
                    // callback once, so release it
                    RouterHelper.release(request);
                    return null;
                }
            };
        } else {
            // no callback, so release immediately
            RouterHelper.release(request);
        }
        execute(command);
    }

底层原理是binder跨进程调用。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>