Arouter学习笔记--分析源码

页面跳转线分析

 ARouter.getInstance().build("/module1/mainActivity").navigation();

ARouter的方法都是通过_ARouter实现的

_ARouter.getInstance().build(path);

分析build方法 目的是构建Postcard对象 直接跟进build方法

  /**
     * Build postcard by path and default group
     */
    protected Postcard build(String path) {
        if (TextUtils.isEmpty(path)) {
            throw new HandlerException(Consts.TAG + "Parameter is invalid!");
        } else {
            PathReplaceService pService = ARouter.getInstance().navigation(PathReplaceService.class);
            if (null != pService) {
                path = pService.forString(path);
            }
            return build(path, extractGroup(path));
        }
    }

extractGroup方法 分解path 这是为什么要严格遵守路径路由的书写规则了

  /**
     * Extract the default group from path.
     */
    private String extractGroup(String path) {
        if (TextUtils.isEmpty(path) || !path.startsWith("/")) {
            throw new HandlerException(Consts.TAG + "Extract the default group failed, the path must be start with '/' and contain more than 2 '/'!");
        }

        try {
            String defaultGroup = path.substring(1, path.indexOf("/", 1));
            if (TextUtils.isEmpty(defaultGroup)) {
                throw new HandlerException(Consts.TAG + "Extract the default group failed! There's nothing between 2 '/'!");
            } else {
                return defaultGroup;
            }
        } catch (Exception e) {
            logger.warning(Consts.TAG, "Failed to extract default group! " + e.getMessage());
            return null;
        }
    }

生成postcard对象 如例子:"/module1/mainActivity"
path="/module1/mainActivity",group=“module1”

   /**
     * Build postcard by path and group
     */
    protected Postcard build(String path, String group) {
        if (TextUtils.isEmpty(path) || TextUtils.isEmpty(group)) {
            throw new HandlerException(Consts.TAG + "Parameter is invalid!");
        } else {
            PathReplaceService pService = ARouter.getInstance().navigation(PathReplaceService.class);
            if (null != pService) {
                path = pService.forString(path);
            }
            return new Postcard(path, group);
        }
    }

分析navigation() 执行逻辑
postcard.navigation()–>ARouter.getInstance().navigation()–>_ARouter.getInstance().naigation();

核心方法 责任:处理注解逻辑 跳转结果反馈 降级服务 拦截器逻辑

 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;
    }

顺着主线 调用_navigation(context, postcard, requestCode, callback);
跳转页面类型ACTIVITY: postcard.getDestination() 对应目标页面的class
目前版本 SERVICE、METHOD 还没有实现 api 版本1.5.0

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:
                // Build intent
                // postcard.getDestination() 对应目标页面的class
                final Intent intent = new Intent(currentContext, postcard.getDestination());
                intent.putExtras(postcard.getExtras());

                // Set flags.
                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();
                try {
                    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) {
                    logger.error(Consts.TAG, "Fetch fragment instance error, " + TextUtils.formatStackTrace(ex.getStackTrace()));
                }
            case METHOD:
            case SERVICE:
            default:
                return null;
        }

        return null;
    }

疑问?postcard.getDestination() 对应目标页面的class
分析物流中心
1、从路由表仓库中获取路由 第一次一定获取不到 RouteMeta routeMeta =Warehouse.routes.get(postcard.getPath());
2、获取不到 去组里获取路由组 Class groupMeta = Warehouse.groupsIndex.get(postcard.getGroup()); 路由组的由来很重要
3、通过反射 构建组 然后加载路由表
4、从新执行一遍 completion 此时能获取到路由对象 走正常分支

public synchronized static void completion(Postcard postcard) {
       if (null == postcard) {
           throw new NoRouteFoundException(TAG + "No postcard!");
       }

       RouteMeta routeMeta = Warehouse.routes.get(postcard.getPath());
       //首次一定获取不到
       if (null == routeMeta) {    // Maybe its does't exist, or didn't load.
          //去路由组里获取路由组对象  
           Class<? extends IRouteGroup> groupMeta = Warehouse.groupsIndex.get(postcard.getGroup());  // Load route meta.
           //不存在 抛异常
           if (null == groupMeta) {
               throw new NoRouteFoundException(TAG + "There is no route match the path [" + postcard.getPath() + "], in group [" + postcard.getGroup() + "]");
           } else {
               // Load route and cache it into memory, then delete from metas.
               try {
                   if (ARouter.debuggable()) {
                       logger.debug(TAG, String.format(Locale.getDefault(), "The group [%s] starts loading, trigger by [%s]", postcard.getGroup(), postcard.getPath()));
                   }
                  //通过反射 构建路由组对象 加载路由表
                   IRouteGroup iGroupInstance = groupMeta.getConstructor().newInstance();
                   iGroupInstance.loadInto(Warehouse.routes);
                   Warehouse.groupsIndex.remove(postcard.getGroup());

                   if (ARouter.debuggable()) {
                       logger.debug(TAG, String.format(Locale.getDefault(), "The group [%s] has already been loaded, trigger by [%s]", postcard.getGroup(), postcard.getPath()));
                   }
               } catch (Exception e) {
                   throw new HandlerException(TAG + "Fatal exception when loading group meta. [" + e.getMessage() + "]");
               }
            //重新执行一遍  
               completion(postcard);   // Reload
           }
       } else {
           //设置目标class
           postcard.setDestination(routeMeta.getDestination());
           postcard.setType(routeMeta.getType());
           postcard.setPriority(routeMeta.getPriority());
           postcard.setExtra(routeMeta.getExtra());

           Uri rawUri = postcard.getUri();
           if (null != rawUri) {   // Try to set params into bundle.
               Map<String, String> resultMap = TextUtils.splitQueryParameters(rawUri);
               Map<String, Integer> paramsType = routeMeta.getParamsType();

               if (MapUtils.isNotEmpty(paramsType)) {
                   // Set value by its type, just for params which annotation by @Param
                   for (Map.Entry<String, Integer> params : paramsType.entrySet()) {
                       setValue(postcard,
                               params.getValue(),
                               params.getKey(),
                               resultMap.get(params.getKey()));
                   }

                   // Save params name which need auto inject.
                   postcard.getExtras().putStringArray(ARouter.AUTO_INJECT, paramsType.keySet().toArray(new String[]{}));
               }

               // Save raw uri
               postcard.withString(ARouter.RAW_URI, rawUri.toString());
           }

           switch (routeMeta.getType()) {
               case PROVIDER:  // if the route is provider, should find its instance
                   // Its provider, so it must implement IProvider
                   Class<? extends IProvider> providerMeta = (Class<? extends IProvider>) routeMeta.getDestination();
                   IProvider instance = Warehouse.providers.get(providerMeta);
                   if (null == instance) { // There's no instance of this provider
                       IProvider provider;
                       try {
                           provider = providerMeta.getConstructor().newInstance();
                           provider.init(mContext);
                           Warehouse.providers.put(providerMeta, provider);
                           instance = provider;
                       } catch (Exception e) {
                           throw new HandlerException("Init provider failed! " + e.getMessage());
                       }
                   }
                   postcard.setProvider(instance);
                   postcard.greenChannel();    // Provider should skip all of interceptors
                   break;
               case FRAGMENT:
                   postcard.greenChannel();    // Fragment needn't interceptors
               default:
                   break;
           }
       }
   }

两个仓库的生成逻辑
static Map> groupsIndex = new HashMap<>();
static Map routes = new HashMap<>();

在物流中心LogisticsCenter中 routes 通过路由组 IRouteGroup 加载的

ARouter$$Group$$module1 类是通过APT 结合注解信息生成的 
IRouteGroup iGroupInstance = groupMeta.getConstructor().newInstance();
                    iGroupInstance.loadInto(Warehouse.routes);
                    Warehouse.groupsIndex.remove(postcard.getGroup());
/**
 * DO NOT EDIT THIS FILE!!! IT WAS GENERATED BY AROUTER. */
public class ARouter$$Group$$module1 implements IRouteGroup {
  @Override
  public void loadInto(Map<String, RouteMeta> atlas) {
    atlas.put("/module1/mainActivity", RouteMeta.build(RouteType.ACTIVITY, Module1_MainActivity.class, "/module1/mainactivity", "module1", new java.util.HashMap<String, Integer>(){{put("key", 8); }}, -1, -2147483648));
    atlas.put("/module1/server", RouteMeta.build(RouteType.PROVIDER, Module1Server.class, "/module1/server", "module1", null, -1, -2147483648));
  }
}

这里通过组加载的好处是,不用一次性加载所有模块的路由信息,在需要用到的时候在从组里加载出来,节约内存和性能

groupsIndex 组信息加载 ,路由组是在框架初始化的时候加载的
static Map> groupsIndex = new HashMap<>();

ARouter.init()–>_ARouter.init()–>LogisticsCenter.init()

Arouter学习笔记--分析源码_第1张图片

1、从dex文件中找出 com.alibaba.android.arouter.routes包下的类的类名,缓存本地
2、通过类型反射 调用loadInto 方法 加载Warehouse.groupsIndex 路由组

 public synchronized static void init(Context context, ThreadPoolExecutor tpe) throws HandlerException {
        mContext = context;
        executor = tpe;

        try {
            long startInit = System.currentTimeMillis();
            //billy.qi modified at 2017-12-06
            //load by plugin first
            loadRouterMap();
            if (registerByPlugin) {
                logger.info(TAG, "Load router map by arouter-auto-register plugin.");
            } else {
                Set<String> routerMap;

                // It will rebuild router map every times when debuggable.
                if (ARouter.debuggable() || PackageUtils.isNewVersion(context)) {
                    logger.info(TAG, "Run with debug mode or new install, rebuild router map.");
                    // These class was generated by arouter-compiler.
                    //从dex文件中找出 com.alibaba.android.arouter.routes包下的类的类名
                    routerMap = ClassUtils.getFileNameByPackageName(mContext, ROUTE_ROOT_PAKCAGE);
                    if (!routerMap.isEmpty()) {
                    //缓存本地
                        context.getSharedPreferences(AROUTER_SP_CACHE_KEY, Context.MODE_PRIVATE).edit().putStringSet(AROUTER_SP_KEY_MAP, routerMap).apply();
                    }

                    PackageUtils.updateVersion(context);    // Save new version name when router map update finishes.
                } else {
                    logger.info(TAG, "Load router map from cache.");
                    routerMap = new HashSet<>(context.getSharedPreferences(AROUTER_SP_CACHE_KEY, Context.MODE_PRIVATE).getStringSet(AROUTER_SP_KEY_MAP, new HashSet<String>()));
                }

                logger.info(TAG, "Find router map finished, map size = " + routerMap.size() + ", cost " + (System.currentTimeMillis() - startInit) + " ms.");
                startInit = System.currentTimeMillis();
               //通过反射 调用loadInto 方法 加载Warehouse.groupsIndex 路由组
                for (String className : routerMap) {
                    if (className.startsWith(ROUTE_ROOT_PAKCAGE + DOT + SDK_NAME + SEPARATOR + SUFFIX_ROOT)) {
                        // This one of root elements, load root.
                        ((IRouteRoot) (Class.forName(className).getConstructor().newInstance())).loadInto(Warehouse.groupsIndex);
                    } else if (className.startsWith(ROUTE_ROOT_PAKCAGE + DOT + SDK_NAME + SEPARATOR + SUFFIX_INTERCEPTORS)) {
                        // Load interceptorMeta
                        ((IInterceptorGroup) (Class.forName(className).getConstructor().newInstance())).loadInto(Warehouse.interceptorsIndex);
                    } else if (className.startsWith(ROUTE_ROOT_PAKCAGE + DOT + SDK_NAME + SEPARATOR + SUFFIX_PROVIDERS)) {
                        // Load providerIndex
                        ((IProviderGroup) (Class.forName(className).getConstructor().newInstance())).loadInto(Warehouse.providersIndex);
                    }
                }
            }

            logger.info(TAG, "Load root element finished, cost " + (System.currentTimeMillis() - startInit) + " ms.");

            if (Warehouse.groupsIndex.size() == 0) {
                logger.error(TAG, "No mapping files were found, check your configuration please!");
            }

            if (ARouter.debuggable()) {
                logger.debug(TAG, String.format(Locale.getDefault(), "LogisticsCenter has already been loaded, GroupIndex[%d], InterceptorIndex[%d], ProviderIndex[%d]", Warehouse.groupsIndex.size(), Warehouse.interceptorsIndex.size(), Warehouse.providersIndex.size()));
            }
        } catch (Exception e) {
            throw new HandlerException(TAG + "ARouter init logistics center exception! [" + e.getMessage() + "]");
        }
    }

一次页面跳转源码 完

你可能感兴趣的:(android,知识分享,android)