ARouter源码分析-降级策略

2020-07-25  本文已影响0人  dashingqi
Android_Banner.jpg

简介

降级策略的使用

单独降级 --- 接口
 mBtnDegrade.setOnClickListener(view->{
            ARouter.getInstance().build("/test/test").navigation(this, new NavigationCallback() {
                @Override
                public void onFound(Postcard postcard) {
                    Log.d(TAG, "onFound: 找到了");

                }

                /**
                 * 出现问题会回调该方法
                 * 也就是我们降级策略的处理回调
                 * @param postcard
                 */
                @Override
                public void onLost(Postcard postcard) {
                    Log.d(TAG, "onLost: 没有找到哟");

                }

                @Override
                public void onArrival(Postcard postcard) {
                    Log.d(TAG, "onArrival: 跳转完事了");

                }

                @Override
                public void onInterrupt(Postcard postcard) {
                    Log.d(TAG, "onInterrupt: 被拦截了");

                }
            });

        });
// 在本例子中,我们的路由地址 ("/test/test")是一个不存在的页面,
// 观察打印的log我们可以看出来回调到了 onLost方法
// 这就是单独降级策略达到的效果,我们可以在 onLost方法中做一些逻辑
//说完了单独降级我们看下全局降级吧
全局降级 --- 服务接口的形式(Provider)
// 声明一个降级策略服务类 实现了 DegradeService
// 使用注解Route 标识这个 Provider类型
@Route(path = "/degrade/test")
public class MyDegradeService implements DegradeService {

    private static final String TAG = "MyDegradeService";
    @Override
    public void onLost(Context context, Postcard postcard) {
        Log.d(TAG, "onLost: ");

    }

    @Override
    public void init(Context context) {
        Log.d(TAG, "init: ");

    }
}

//点击按钮进行页面跳转(提供的路由地址不存在)
 mBtnGlobalDegrade = findViewById(R.id.btnGlobalDegrade);
        mBtnGlobalDegrade.setOnClickListener(view ->{
            ARouter.getInstance().build("/test1/test1").navigation();
        });
// 参看log 显示如下
2020-07-09 00:04:21.032 4133-4133/com.dashingqi.module.arouter D/MyDegradeService: init: 
2020-07-09 00:04:21.033 4133-4133/com.dashingqi.module.arouter D/MyDegradeService: onLost: 

// 这是全局的降级策略,我们任何页面跳转出现问题 都会回调到自定义DegradeService服务类中的onLost方法中

降级策略的过程分析

单一降级(接口)
// ARouter.getInstance().build("") ----> Postcard
// Postcard # navigation()
public Object navigation(Context context, NavigationCallback callback) {
        return ARouter.getInstance().navigation(context, this, -1, callback);
    }
// -----> ARouter # navigation()
public Object navigation(Context mContext, Postcard postcard, int requestCode, NavigationCallback callback) {
        return _ARouter.getInstance().navigation(mContext, postcard, requestCode, callback);
    }
//这回跑到了 _ARouter #navigation
protected Object navigation(final Context context, final Postcard postcard, final int requestCode, final NavigationCallback callback) {
        PretreatmentService pretreatmentService = ARouter.getInstance().navigation(PretreatmentService.class);
        if (null != pretreatmentService && !pretreatmentService.onPretreatment(context, postcard)) {
            // Pretreatment failed, navigation canceled.
            return null;
        }

        try {
            LogisticsCenter.completion(postcard);
        } catch (NoRouteFoundException ex) {
            logger.warning(Consts.TAG, ex.getMessage());

            if (debuggable()) {
                // Show friendly tips for user.
                runInMainThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(mContext, "There's no route matched!\n" +
                                " Path = [" + postcard.getPath() + "]\n" +
                                " Group = [" + postcard.getGroup() + "]", Toast.LENGTH_LONG).show();
                    }
                });
            }

            if (null != callback) {
                callback.onLost(postcard);
            } else {
                // No callback for this invoke, then we use the global degrade service.
                DegradeService degradeService = ARouter.getInstance().navigation(DegradeService.class);
                if (null != degradeService) {
                    degradeService.onLost(context, postcard);
                }
            }

            return null;
        }

        if (null != callback) {
            callback.onFound(postcard);
        }

        if (!postcard.isGreenChannel()) {   // It must be run in async thread, maybe interceptor cost too mush time made ANR.
            interceptorService.doInterceptions(postcard, new InterceptorCallback() {
                /**
                 * Continue process
                 *
                 * @param postcard route meta
                 */
                @Override
                public void onContinue(Postcard postcard) {
                    _navigation(context, postcard, requestCode, callback);
                }

                /**
                 * Interrupt process, pipeline will be destory when this method called.
                 *
                 * @param exception Reson of interrupt.
                 */
                @Override
                public void onInterrupt(Throwable exception) {
                    if (null != callback) {
                        callback.onInterrupt(postcard);
                    }

                    logger.info(Consts.TAG, "Navigation failed, termination by interceptor : " + exception.getMessage());
                }
            });
        } else {
            return _navigation(context, postcard, requestCode, callback);
        }

        return null;
    }
全局的降级策略
if (null != callback) {
                callback.onLost(postcard);
            } else {
                // No callback for this invoke, then we use the global degrade service.
                DegradeService degradeService = ARouter.getInstance().navigation(DegradeService.class);
                if (null != degradeService) {
                    degradeService.onLost(context, postcard);
                }
            }
// 很好理解,可以看出如果同一个页面跳转,分别设置了单一降级策略和全局降级策略,仅仅会执行单一的降级策略不会执行全局的降级策略。
// 针对是全局降级策略,首先更具IoC中的ByType()方法拿到对应的服务实例(实则就是我们自定义的全局降级策略类)
// 当发现我们自定义的的全局降级策略类,就会回调执行它的onLost()方法

总结

上一篇下一篇

猜你喜欢

热点阅读