0
点赞
收藏
分享

微信扫一扫

ARouter的源码梳理,flutter页面跳转卡

祈澈菇凉 2022-02-17 阅读 33

LogisticsCenter.completion(postcard);

if (!postcard.isGreenChannel()) {
interceptorService.doInterceptions(postcard, new InterceptorCallback() {
@Override
public void onContinue(Postcard postcard) {
_navigation(context, postcard, requestCode, callback);
}

@Override
public void onInterrupt(Throwable exception) {
if (null != callback) {
callback.onInterrupt(postcard);
}
}
});
} else {
return _navigation(context, postcard, requestCode, callback);
}
return null;
}

  1. LogisticsCenter的completion方法中会找到Postcard中路径对应的RouteMeta对象,该对象是在编译期根据我们添加的路由来动态生成的一个类,包含了路径和注解类相关的信息。通过对应的RouteMeta对象来补全Postcard中需要的目标类的相关信息。
  2. isGreenChannel= true表示跳过所有的拦截器,默认为false,PROVIDER和FRAGMENT类型会被默认设置greenChannel=true。这里的拦截服务interceptorService在ARouter中指的是InterceptorServiceImpl,执行对应的doInterceptions方法开启一个线程池来执行我们创建的拦截器。执行成功的话会回调到onContinue方法中,最终执行_navigation方法。

private Object _navigation(final Context context, final Postcard postcard, final int requestCode, final NavigationCallback callback) {
final Context currentContext = null == context ? mContext : context;
switch (postcard.getType()) {
case ACTIVITY:
final Intent intent = new Intent(currentContext, postcard.getDestination());
intent.putExtras(postcard.getExtras());
int flags = postcard.getFlags();
if (-1 != flags) {
intent.setFlags(flags);
} else if (!(currentContext instanceof Activity)) { // Non activity, need less one flag.
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}

// Set Actions
String action = postcard.getAction();
if (!TextUtils.isEmpty(action)) {
intent.setAction(action);
}

// Navigation in main looper.
runInMainThread(new Runnable() {
@Override
public void run() {
startActivity(requestCode, currentContext, intent, postcard, callback);
}
});

break;
case PROVIDER:
return postcard.getProvider();
case BOARDCAST:
case CONTENT_PROVIDER:
case FRAGMENT:
Class fragmentMeta = postcard.getDestination();
Object instance = fragmentMeta.getConstructor().newInstance();
if (instance instanceof Fragment) {
((Fragment) instance).setArguments(postcard.getExtras());
} else if (instance instanceof android.support.v4.app.Fragment) {
((android.support.v4.app.Fragment) instance).setArguments(postcard.getExtras());
}

return instance;
} catch (Exception ex) {
}

return null;
}

可以看到根据postcard中对应的目标Type,有不同的处理方式。例如ACTIVITY代表调转,FRAGMENT则会创建一个对应的对象并返回。

3. ARouter拦截器

public interface IProvider {
void init(Context context);
}

public interface IInterceptor extends IProvider {
void process(Postcard postcard, InterceptorCallback callback);
}

ARouter中自定义的拦截器需要继承IInterceptor,并重写initprocess方法。init方法在ARouter初始化时会被调用一次。具体的流程图为:

ARouter.init() -> _ARouter.afterInit()->InterceptorServiceImpl.init()->IInterceptor.init()

ARouter中拦截器的操作从doInterceptions开始,通过默认的线程池开启一个线程来执行拦截器的拦截操作。每一层的拦截器执行完后,需要调用onContinue交还ARouter控制权,执行下一层拦截器,或者调用onInterrupt中断路由。

public void doInterceptions(final Postcard postcard, final InterceptorCallback callback) {
LogisticsCenter.executor.execute(new Runnable() {
@Override
public void run() {
_execute(0, interceptorCounter, postcard);
if (interceptorCounter.getCount() > 0) { // Cancel the navigation this time, if it hasn’t return anythings.
callback.onInterrupt(new HandlerException(“The interceptor processing timed out.”));
} else if (null != postcard.getTag()) { // Maybe some exception in the tag.
callback.onInterrupt(new HandlerException(postcard.getTag().toString()));
} else {
callback.onContinue(postcard);
}
}
}

private static void _execute(final int index, final CancelableCountDownLatch counter, final Postcard postcard) {
if (index < Warehouse.interceptors.size()) {
IInterceptor iInterceptor = Warehouse.interceptors.get(index);
iInterceptor.process(postcard, new InterceptorCallback() {
@Override
public void onContinue(Postcard postcard) {
counter.countDown();
_execute(index + 1, counter, postcard);
}

@Override
public void onInterrupt(Throwable exception) {

}
});
}
}

拦截器执行完后会会调到InterceptorServiceImpl的doInterceptions方法中继续执行下一步,也就是我们上面讲到的ARouter的跳转的最后一步。

4. 服务管理

ARouter中通过实现IProvider接口可以创建一个服务,然后实现该接口。通过路由注解声明的服务会在编译期被添加到ARouter$$Providers$$app类中。

interface MiProvider : IProvider {
fun getName():String
}

@Route(path = “/mi/pro”,name = “这是测试”)
class IMiProviderImpl : MiProvider {
override fun getName() = “yu”
override fun init(context: Context?) {}
}

最后

Android学习是一条漫长的道路,我们要学习的东西不仅仅只有表面的 技术,还要深入底层,弄明白下面的 原理,只有这样,我们才能够提高自己的竞争力,在当今这个竞争激烈的世界里立足。

人生不可能一帆风顺,有高峰自然有低谷,要相信,那些打不倒我们的,终将使我们更强大,要做自己的摆渡人。

我把自己这段时间整理的Android最重要最热门的学习方向资料放在了我的GitHub,里面还有不同方向的自学编程路线、面试题集合/面经、及系列技术文章等。

资源持续更新中,欢迎大家一起学习和探讨。

举报

相关推荐

0 条评论