ARouter解析四:發(fā)現(xiàn)服務(wù)和Fragment

本來(lái)這期應(yīng)該分享IoC思想和ARouter的自動(dòng)注入這塊內(nèi)容,但是在自動(dòng)注入這塊涉及到服務(wù)的主動(dòng)注入,而我們前面只說(shuō)到Activity的發(fā)現(xiàn),所以還是決定先做個(gè)服務(wù)和Fragment實(shí)例發(fā)現(xiàn)的分享。這也是ARouter的分享系列的第四篇,前面三篇分別是:
ARouter解析一:基本使用及頁(yè)面注冊(cè)源碼解析
ARouter解析二:頁(yè)面跳轉(zhuǎn)源碼分析
ARouter解析三:URL跳轉(zhuǎn)本地頁(yè)面源碼分析

服務(wù)和Fragment的發(fā)現(xiàn)和Activity的發(fā)現(xiàn)會(huì)有很多重疊的地方,我們不會(huì)再重復(fù)說(shuō),建議小伙伴們?cè)陂_始之前先看下解析系列的第二篇,再來(lái)看這篇會(huì)輕松很多。今天我們這次分享分成三部分。

1.服務(wù)的發(fā)現(xiàn)

2.Fragment的發(fā)現(xiàn)

3.服務(wù)發(fā)現(xiàn)的源碼分析

4.Fragment實(shí)例獲取的源碼分析

Demo還是慣例使用官方的,我們看下效果圖,點(diǎn)擊BYNAME調(diào)用服務(wù)或者BYTYPE調(diào)用服務(wù)

服務(wù).png

點(diǎn)擊獲取FRAGMENT實(shí)例可以獲取BlackFragment實(shí)例。

Fragment.png

這期涉及到的內(nèi)容Demo沒有什么可觀賞的,內(nèi)容比較有意思。好了,開始進(jìn)入正題~~~

1.發(fā)現(xiàn)服務(wù)

這里說(shuō)到的服務(wù)不是Android四大組件中的Service,這里的服務(wù)是服務(wù)端開發(fā)的概念,就是將一部分功能和組件封裝起來(lái)成為接口,以接口的形式對(duì)外提供能力,所以在這部分就可以將每個(gè)功能作為一個(gè)服務(wù),而服務(wù)的實(shí)現(xiàn)就是具體的業(yè)務(wù)功能

ARouter發(fā)現(xiàn)服務(wù)主要有兩種方式,ByName和ByType。先看看這兩種方式分別是怎么使用。ByName就是需要傳遞path路徑來(lái)進(jìn)行發(fā)現(xiàn),ByType就是通過服務(wù)class來(lái)進(jìn)行查找。

case R.id.navByName:
    ((HelloService) ARouter.getInstance().build("/service/hello").navigation()).sayHello("mike");
    break;
case R.id.navByType:
    ARouter.getInstance().navigation(HelloService.class).sayHello("mike");
    break;

那么為什么需要區(qū)分兩種類型?因?yàn)樵贘ava中接口是可以有多個(gè)實(shí)現(xiàn)的,通過ByType的方式可能難以拿到想要的多種實(shí)現(xiàn),這時(shí)候就可以通過ByName的方式獲取真實(shí)想要的服務(wù)。所以其實(shí)大多數(shù)情況是通過ByType的,如果有多實(shí)現(xiàn)的時(shí)候就需要使用ByName。

服務(wù)發(fā)現(xiàn)的使用就是以上。

2.發(fā)現(xiàn)Fragment

Fragment獲取實(shí)例的使用也是很簡(jiǎn)單,一行代碼搞定,和跳轉(zhuǎn)的寫法很基本就是一樣的。

Fragment fragment = (Fragment) ARouter.getInstance().build("/test/fragment").navigation();

Fragment發(fā)現(xiàn)的使用就是以上。

3.發(fā)現(xiàn)服務(wù)的源碼分析

接下來(lái)我們分析下發(fā)現(xiàn)服務(wù)的邏輯過程,小伙伴們有沒有疑問,在上面服務(wù)的使用時(shí),HelloService.class是一個(gè)接口,怎么去獲取到他的實(shí)現(xiàn)類的?

public interface HelloService extends IProvider {
    void sayHello(String name);
}

ByName的發(fā)現(xiàn)比較簡(jiǎn)單,很Activity的跳轉(zhuǎn)很像。我們先來(lái)看看ByType的發(fā)現(xiàn)過程。

ARouter.getInstance().navigation(HelloService.class)

先跟到_ARouter的navigation(Class<? extends T> service)中。

protected <T> T navigation(Class<? extends T> service) {
        try {
            Postcard postcard = LogisticsCenter.buildProvider(service.getName());

            // Compatible 1.0.5 compiler sdk.
            if (null == postcard) { // No service, or this service in old version.
                postcard = LogisticsCenter.buildProvider(service.getSimpleName());
            }

            LogisticsCenter.completion(postcard);
            return (T) postcard.getProvider();
        } catch (NoRouteFoundException ex) {
            logger.warning(Consts.TAG, ex.getMessage());
            return null;
        }
}

上面代碼主要就是兩步,第一構(gòu)造postcard,第二LogisticsCenter跳轉(zhuǎn)

1.構(gòu)造postcard

首先還是熟悉的先構(gòu)造postcard,只不過這里時(shí)直接在navigation中進(jìn)行構(gòu)造,之前activity或者url跳轉(zhuǎn)都是通過build構(gòu)造,意思都差不多。我們進(jìn)去LogisticsCenter.buildProvider看看??吹?code>LogisticsCenter就可以猜到這里邏輯是要和APT技術(shù)生成的Java類打交道,可以參考ARouter解析一:基本使用及頁(yè)面注冊(cè)源碼解析

public static Postcard buildProvider(String serviceName) {
        RouteMeta meta = Warehouse.providersIndex.get(serviceName);

        if (null == meta) {
            return null;
        } else {
            return new Postcard(meta.getPath(), meta.getGroup());
        }
}

我們?cè)谠创a中打個(gè)斷點(diǎn)看看meta是什么東東??梢钥吹?code>meta中有個(gè)destination屬性可以拿到具體的實(shí)現(xiàn)類HelloServiceImpl。

發(fā)現(xiàn)服務(wù).png

上面的meta是直接從倉(cāng)庫(kù)Warehouse中獲取服務(wù),那么倉(cāng)庫(kù)的providersIndex 是從哪來(lái)的?其實(shí)就是在獲取ARouter實(shí)例的時(shí)候加載進(jìn)來(lái)的,在LogisticsCenter .init(),這里為了方便分析,對(duì)代碼做了手腳。

for (String className : classFileNames) {
       if (className.startsWith(ROUTE_ROOT_PAKCAGE + DOT + SDK_NAME + SEPARATOR + SUFFIX_PROVIDERS)) {
                    // Load providerIndex
             ((IProviderGroup) (Class.forName(className).getConstructor().newInstance())).loadInto(Warehouse.providersIndex);
       }
}

Java文件名就是ARouter$$Providers$$app,這個(gè)類就是在編譯器使用APT技術(shù)自動(dòng)生成的。有木有很激動(dòng)???其實(shí)就是一個(gè)map,其中就有我們上面使用到的HelloService,對(duì)應(yīng)的注冊(cè)類就是HelloServiceImpl .class。

public class ARouter$$Providers$$app implements IProviderGroup {
  @Override
  public void loadInto(Map<String, RouteMeta> providers) {
    providers.put("com.alibaba.android.arouter.demo.testservice.HelloService", RouteMeta.build(RouteType.PROVIDER, HelloServiceImpl.class, "/service/hello", "service", null, -1, -2147483648));
    providers.put("com.alibaba.android.arouter.facade.service.SerializationService", RouteMeta.build(RouteType.PROVIDER, JsonServiceImpl.class, "/service/json", "service", null, -1, -2147483648));
    providers.put("com.alibaba.android.arouter.demo.testservice.SingleService", RouteMeta.build(RouteType.PROVIDER, SingleService.class, "/service/single", "service", null, -1, -2147483648));
  }
}

所以我們?cè)趥}(cāng)庫(kù)中就可以根據(jù)Name-HelloService發(fā)現(xiàn)服務(wù)的具體實(shí)現(xiàn)。之后就很好理解了,可以從meta中拿到path-/service/hello,group-service構(gòu)造postcard。

2.LogisticsCenter跳轉(zhuǎn)

拿到postcard之后就是跳轉(zhuǎn)到目標(biāo)服務(wù)了,這個(gè)和activity跳轉(zhuǎn)是一樣的。我們到老朋友LogisticsCenter.completion(postcard)中看下。

public synchronized static void completion(Postcard 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
            }
}

這里還是從倉(cāng)庫(kù)中去找服務(wù),一開始肯定是沒有因?yàn)檫€沒有加載。

倉(cāng)庫(kù)發(fā)現(xiàn)服務(wù).png

我們之前分享提到過ARouter是分組管理的,按需加載。所以這里到節(jié)點(diǎn)的map中找到service的分組,然后加載這個(gè)分組。


service分組.png
HelloService.png

加載service分組后,就可以拿到我們需要的HelloService了。

public class ARouter$$Group$$service implements IRouteGroup {
  @Override
  public void loadInto(Map<String, RouteMeta> atlas) {
    atlas.put("/service/hello", RouteMeta.build(RouteType.PROVIDER, HelloServiceImpl.class, "/service/hello", "service", null, -1, -2147483648));
    atlas.put("/service/json", RouteMeta.build(RouteType.PROVIDER, JsonServiceImpl.class, "/service/json", "service", null, -1, -2147483648));
    atlas.put("/service/single", RouteMeta.build(RouteType.PROVIDER, SingleService.class, "/service/single", "service", null, -1, -2147483648));
  }
}

看到這里小伙伴們有沒有感到疑惑?其實(shí)也算是框架的一個(gè)需要改進(jìn)的地方。前面在provider中已經(jīng)拿到HelloService具體實(shí)現(xiàn)類的路徑,這里又加載service分組,然后再找到具體實(shí)現(xiàn)類,做了反復(fù)沒必要的工作了。不過這并不影響框架的牛逼性哈。

再接下來(lái)其實(shí)就是通過反射構(gòu)造HelloService實(shí)例。

switch (routeMeta.getType()) {
    case PROVIDER:  // if the route is provider, should find its instance
            // Its provider, so it must be implememt 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;
}

將服務(wù)實(shí)例設(shè)置給postcard,之后可以在navigation中通過postcard的方法getProvider()得到。
發(fā)現(xiàn)服務(wù)的源碼就是以上,和activity的發(fā)現(xiàn)差別就是服務(wù)需要通過反射構(gòu)造實(shí)例返回。

4.發(fā)現(xiàn)Fragment的源碼分析

接著看下發(fā)現(xiàn)Fragment的源碼,使用上和activity是一樣的。build也是構(gòu)造postcard實(shí)例。

ARouter.getInstance().build("/test/fragment")

我們看下build的操作,也activity也是一致的,代碼比較簡(jiǎn)單,這里就不多做解釋。

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

有了postcard接著就是到LogisticsCenter.completion(postcard)中進(jìn)行具體的跳轉(zhuǎn)了。首先還是找到映射關(guān)系的類,可以看到倒數(shù)第二個(gè)就是我們需要的fragment,接著就是加載這個(gè)節(jié)點(diǎn),將信息補(bǔ)充到postcard中。

public class ARouter$$Group$$test implements IRouteGroup {
  @Override
  public void loadInto(Map<String, RouteMeta> atlas) {
    atlas.put("/test/activity1", RouteMeta.build(RouteType.ACTIVITY, Test1Activity.class, "/test/activity1", "test",
            new java.util.HashMap<String, Integer>(){{put("pac", 9); put("obj", 10); put("name", 8); put("boy", 0);
              put("age", 3); put("url", 8); }}, -1, -2147483648));
    atlas.put("/test/activity2", RouteMeta.build(RouteType.ACTIVITY, Test2Activity.class, "/test/activity2",
            "test", new java.util.HashMap<String, Integer>(){{put("key1", 8); }}, -1, -2147483648));
    atlas.put("/test/activity3", RouteMeta.build(RouteType.ACTIVITY, Test3Activity.class, "/test/activity3", "test", new java.util.HashMap<String, Integer>(){{put("name", 8); put("boy", 0); put("age", 3); }}, -1, -2147483648));
    atlas.put("/test/activity4", RouteMeta.build(RouteType.ACTIVITY, Test4Activity.class, "/test/activity4", "test", null, -1, -2147483648));
    atlas.put("/test/fragment", RouteMeta.build(RouteType.FRAGMENT, BlankFragment.class, "/test/fragment", "test", null, -1, -2147483648));
    atlas.put("/test/webview", RouteMeta.build(RouteType.ACTIVITY, TestWebview.class, "/test/webview", "test", null, -1, -2147483648));
  }
}

有了完整信息的postcard就可以拿到fragment了,跳轉(zhuǎn)邏輯在_ARouter的_navigation中,也是通過反射拿到Fragment的實(shí)例,注意Fragment實(shí)例中需要有默認(rèn)的構(gòu)造函數(shù)。通過setArguments給fragment參數(shù)。

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

從上面也可以看出,目前ARouter還沒實(shí)現(xiàn)另外三個(gè)組件 broadcast,content provider,service的路由功能,期待后續(xù)更新哈。

發(fā)現(xiàn)Fragment的源碼就是以上。

5.總結(jié)

在activity跳轉(zhuǎn)的基礎(chǔ)上我們今天分享了service(這里不是指四大組件的Service,和后端開發(fā)的接口有點(diǎn)類似),fragment路由的使用和源碼分析,大部分邏輯是類似的,主要區(qū)別就是這里需要通過反射拿到實(shí)例,activity則是拿到路徑后進(jìn)行跳轉(zhuǎn)。再有就是前面加載service時(shí)會(huì)有反復(fù)加載的過程,這應(yīng)該是沒有必要的。

今天的發(fā)現(xiàn)服務(wù)和Fragment車就開到這,小伙伴們可以下車嘍,不要忘記點(diǎn)個(gè)贊哦!

謝謝!

歡迎關(guān)注公眾號(hào):JueCode

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容