Skip to content

Commit

Permalink
Feature : Support register route meta dynamic.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhi1ong committed Oct 21, 2020
1 parent 75c38c0 commit ee67a3d
Show file tree
Hide file tree
Showing 11 changed files with 189 additions and 3 deletions.
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ version|[![Download](https://api.bintray.com/packages/zhi1ong/maven/arouter-api/

#### Demo

##### [Demo apk](https://github.com/alibaba/ARouter/blob/develop/demo/arouter-demo.apk)[Demo Gif](https://raw.githubusercontent.com/alibaba/ARouter/master/demo/arouter-demo.gif)
##### [Demo apk](https://github.com/alibaba/ARouter/blob/develop/demo/arouter-demo-1.5.2.apk)[Demo Gif](https://raw.githubusercontent.com/alibaba/ARouter/master/demo/arouter-demo.gif)

#### I. Feature
1. **Supports direct parsing of standard URLs for jumps and automatic injection of parameters into target pages**
Expand All @@ -34,6 +34,7 @@ version|[![Download](https://api.bintray.com/packages/zhi1ong/maven/arouter-api/
13. **Generate route doc support**
14. **Provide IDE plugin for quick navigation to target class**
15. Support Incremental annotation processing
16. Support register route meta dynamic.

#### II. Classic Case
1. Forward from external URLs to internal pages, and parsing parameters
Expand Down Expand Up @@ -338,6 +339,29 @@ version|[![Download](https://api.bintray.com/packages/zhi1ong/maven/arouter-api/
}
```

9. Dynamic register route meta
Applicable to apps with plug-in architectures or some scenarios where routing information
needs to be dynamically registered,Dynamic registration can be achieved through the
interface provided by ARouter, The target page and service need not be marked with @Route
annotation,**Only the routing information of the same group can be registered in the same batch**
``` java
ARouter.getInstance().addRouteGroup(new IRouteGroup() {
@Override
public void loadInto(Map<String, RouteMeta> atlas) {
atlas.put("/dynamic/activity", // path
RouteMeta.build(
RouteType.ACTIVITY, // Route type
TestDynamicActivity.class, // Target class
"/dynamic/activity", // Path
"dynamic", // Group
0, // not need
0 // Extra tag, Used to mark page feature
)
);
}
});
```

#### V. More features

1. Other settings in initialization
Expand Down
24 changes: 23 additions & 1 deletion README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

#### Demo展示

##### [Demo apk下载](https://github.com/alibaba/ARouter/blob/develop/demo/arouter-demo.apk)[Demo Gif](https://raw.githubusercontent.com/alibaba/ARouter/master/demo/arouter-demo.gif)
##### [Demo apk下载](https://github.com/alibaba/ARouter/blob/develop/demo/arouter-demo-1.5.2.apk)[Demo Gif](https://raw.githubusercontent.com/alibaba/ARouter/master/demo/arouter-demo.gif)

#### 一、功能介绍
1. **支持直接解析标准URL进行跳转,并自动注入参数到目标页面中**
Expand All @@ -35,6 +35,7 @@
14. **支持生成路由文档**
15. **提供 IDE 插件便捷的关联路径和目标类**
16. 支持增量编译(开启文档生成后无法增量编译)
17. 支持动态注册路由信息

#### 二、典型应用
1. 从外部URL映射到内部页面,以及参数传递与解析
Expand Down Expand Up @@ -364,6 +365,27 @@
}
}
```

10. 动态注册路由信息
适用于部分插件化架构的App以及需要动态注册路由信息的场景,可以通过 ARouter 提供的接口实现动态注册
路由信息,目标页面和服务可以不标注 @Route 注解,**注意:同一批次仅允许相同 group 的路由信息注册**
``` java
ARouter.getInstance().addRouteGroup(new IRouteGroup() {
@Override
public void loadInto(Map<String, RouteMeta> atlas) {
atlas.put("/dynamic/activity", // path
RouteMeta.build(
RouteType.ACTIVITY, // 路由信息
TestDynamicActivity.class, // 目标的 Class
"/dynamic/activity", // Path
"dynamic", // Group, 尽量保持和 path 的第一段相同
0, // 优先级,暂未使用
0 // Extra,用于给页面打标
)
);
}
});
```

#### 五、更多功能

Expand Down
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
</activity>
<activity android:name=".testactivity.Test4Activity">
</activity>
<activity android:name=".testactivity.TestDynamicActivity">
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@
import android.view.View;
import android.widget.Toast;

import com.alibaba.android.arouter.demo.testactivity.TestDynamicActivity;
import com.alibaba.android.arouter.demo.testinject.TestObj;
import com.alibaba.android.arouter.demo.testinject.TestParcelable;
import com.alibaba.android.arouter.demo.testinject.TestSerializable;
import com.alibaba.android.arouter.demo.testservice.HelloService;
import com.alibaba.android.arouter.demo.testservice.SingleService;
import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.callback.NavCallback;
import com.alibaba.android.arouter.facade.enums.RouteType;
import com.alibaba.android.arouter.facade.model.RouteMeta;
import com.alibaba.android.arouter.facade.template.IRouteGroup;
import com.alibaba.android.arouter.launcher.ARouter;

import java.util.ArrayList;
Expand Down Expand Up @@ -210,6 +214,32 @@ public void onInterrupt(Postcard postcard) {
.withObject("map", map).navigation();
Toast.makeText(this, "找到Fragment:" + fragment.toString(), Toast.LENGTH_SHORT).show();
break;
case R.id.addGroup:
ARouter.getInstance().addRouteGroup(new IRouteGroup() {
@Override
public void loadInto(Map<String, RouteMeta> atlas) {
atlas.put("/dynamic/activity", RouteMeta.build(
RouteType.ACTIVITY,
TestDynamicActivity.class,
"/dynamic/activity",
"dynamic", 0, 0));
}
});
break;
case R.id.dynamicNavigation:
// 该页面未配置 Route 注解,动态注册到 ARouter
ARouter.getInstance().build("/dynamic/activity")
.withString("name", "老王")
.withInt("age", 18)
.withBoolean("boy", true)
.withLong("high", 180)
.withString("url", "https://a.b.c")
.withSerializable("ser", testSerializable)
.withParcelable("pac", testParcelable)
.withObject("obj", testObj)
.withObject("objList", objList)
.withObject("map", map).navigation(this);
break;
default:
break;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.alibaba.android.arouter.demo.testactivity;

import android.os.Bundle;

// 用于测试不标注 Route 的情况下,动态增加路由
//@Route(path="/dynamic/activity")
public class TestDynamicActivity extends Test1Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
28 changes: 28 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -261,5 +261,33 @@
android:text="服务调用失败" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/between_cell"
android:background="@drawable/bg_test_area"
android:orientation="vertical"
android:padding="@dimen/test_area_padding">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="动态增加路由测试" />

<Button
android:id="@+id/addGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="动态增加路由" />

<Button
android:id="@+id/dynamicNavigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="动态路由测试" />
</LinearLayout>
</LinearLayout>
</ScrollView>
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.alibaba.android.arouter.utils.PackageUtils;
import com.alibaba.android.arouter.utils.TextUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -367,4 +368,15 @@ private static void setValue(Postcard postcard, Integer typeDef, String key, Str
public static void suspend() {
Warehouse.clear();
}

public synchronized static void addRouteGroupDynamic(String groupName, IRouteGroup group) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
if (Warehouse.groupsIndex.containsKey(groupName)){
// If this group is included, but it has not been loaded
// load this group first, because dynamic route has high priority.
Warehouse.groupsIndex.get(groupName).getConstructor().newInstance().loadInto(Warehouse.routes);
}

// cover old group.
group.loadInto(Warehouse.routes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import com.alibaba.android.arouter.exception.InitException;
import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.callback.NavigationCallback;
import com.alibaba.android.arouter.facade.model.RouteMeta;
import com.alibaba.android.arouter.facade.template.ILogger;
import com.alibaba.android.arouter.facade.template.IRouteGroup;
import com.alibaba.android.arouter.utils.Consts;

import java.util.concurrent.ThreadPoolExecutor;
Expand Down Expand Up @@ -183,4 +185,13 @@ public <T> T navigation(Class<? extends T> service) {
public Object navigation(Context mContext, Postcard postcard, int requestCode, NavigationCallback callback) {
return _ARouter.getInstance().navigation(mContext, postcard, requestCode, callback);
}

/**
* Add route group dynamic.
* @param group route group.
* @return add result.
*/
public boolean addRouteGroup(IRouteGroup group) {
return _ARouter.getInstance().addRouteGroup(group);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@
import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.callback.InterceptorCallback;
import com.alibaba.android.arouter.facade.callback.NavigationCallback;
import com.alibaba.android.arouter.facade.model.RouteMeta;
import com.alibaba.android.arouter.facade.service.*;
import com.alibaba.android.arouter.facade.template.ILogger;
import com.alibaba.android.arouter.facade.template.IRouteGroup;
import com.alibaba.android.arouter.thread.DefaultPoolExecutor;
import com.alibaba.android.arouter.utils.Consts;
import com.alibaba.android.arouter.utils.DefaultLogger;
import com.alibaba.android.arouter.utils.TextUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ThreadPoolExecutor;

/**
Expand Down Expand Up @@ -453,4 +457,44 @@ private void startActivity(int requestCode, Context currentContext, Intent inten
callback.onArrival(postcard);
}
}

boolean addRouteGroup(IRouteGroup group) {
if (null == group) {
return false;
}

String groupName = null;

try {
// Extract route meta.
Map<String, RouteMeta> dynamicRoute = new HashMap<>();
group.loadInto(dynamicRoute);

// Check route meta.
for (Map.Entry<String, RouteMeta> route : dynamicRoute.entrySet()) {
String path = route.getKey();
String groupByExtract = extractGroup(path);
RouteMeta meta = route.getValue();

if (null == groupName) {
groupName = groupByExtract;
}

if (null == groupName || !groupName.equals(groupByExtract) || !groupName.equals(meta.getGroup())) {
// Group name not consistent
return false;
}
}

LogisticsCenter.addRouteGroupDynamic(groupName, group);

logger.info(Consts.TAG, "Add route group [" + groupName + "] finish, " + dynamicRoute.size() + " new route meta.");

return true;
} catch (Exception exception) {
logger.error(Consts.TAG, "Add route group dynamic exception! " + exception.getMessage());
}

return false;
}
}
Binary file added demo/arouter-demo-1.5.2.apk
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ SUPPORT_LIB_VERSION=28.0.0
MIN_SDK_VERSION=14
TARGET_SDK_VERSION=28

arouter_main_version=1.5.1
arouter_main_version=1.5.2
arouter_annotation_version=1.0.6
arouter_register_version=1.0.2
arouter_idea_plugin_version=1.0.0
Expand Down

0 comments on commit ee67a3d

Please sign in to comment.