Skip to content

Commit c380640

Browse files
committed
更改生成路由表文件策略
1 parent 674a9d7 commit c380640

3 files changed

Lines changed: 80 additions & 22 deletions

File tree

app/src/main/Assets/module_info

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"modules": [
3+
{
4+
"packageName": "top.omooo.easyrouter"
5+
},
6+
{
7+
"packageName": "top.omooo.other_module"
8+
}
9+
]
10+
}

router/src/main/java/top/omooo/router/EasyRouter.java

Lines changed: 62 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,26 @@
66
import android.content.Intent;
77
import android.content.pm.ActivityInfo;
88
import android.content.pm.PackageManager;
9+
import android.content.res.AssetManager;
910
import android.os.Bundle;
1011
import android.os.Parcelable;
1112
import android.support.annotation.NonNull;
1213
import android.support.v4.app.ActivityCompat;
1314
import android.text.TextUtils;
1415
import android.util.Log;
1516

17+
import org.json.JSONArray;
18+
import org.json.JSONObject;
19+
20+
import java.io.BufferedReader;
21+
import java.io.IOException;
22+
import java.io.InputStreamReader;
1623
import java.io.Serializable;
1724
import java.lang.reflect.InvocationTargetException;
1825
import java.lang.reflect.Method;
26+
import java.util.ArrayList;
1927
import java.util.HashMap;
28+
import java.util.List;
2029

2130
import top.omooo.logger.Logger;
2231
import top.omooo.logger.StackTraceUtil;
@@ -32,7 +41,7 @@ public class EasyRouter {
3241

3342
private static final String PAGE_NAME = "pageName";
3443
//路由表 key:pageName value: Activity
35-
private HashMap<String, Object> mRouterMap;
44+
private HashMap<String, String> mRouterMap;
3645
private Context mContext;
3746
private Bundle mBundle;
3847

@@ -58,7 +67,7 @@ private static class EasyRouterHolder {
5867
public void inject(Application application) {
5968
mRouterMap = new HashMap<>();
6069
if (isUseAnno) {
61-
getRouterMapFromAnno();
70+
getRouterMapFromAnno(application);
6271
return;
6372
}
6473
//使用 Meta-Data
@@ -79,13 +88,13 @@ public void inject(Application application) {
7988
if (TextUtils.isEmpty(bundle.getString(PAGE_NAME))) {
8089
Logger.e(TAG, PAGE_NAME_EMPTY_DESC, StackTraceUtil.getStackTrace(), activityInfo.name);
8190
} else {
82-
mRouterMap.put(bundle.getString(PAGE_NAME), Class.forName(activityInfo.name));
91+
mRouterMap.put(bundle.getString(PAGE_NAME), activityInfo.name);
8392
Logger.d(TAG, null, "ClassName: " + activityInfo.name,
8493
"PageName: " + bundle.getString(PAGE_NAME));
8594
}
8695
}
8796
}
88-
} catch (PackageManager.NameNotFoundException | ClassNotFoundException e) {
97+
} catch (PackageManager.NameNotFoundException e) {
8998
//ignore
9099
e.printStackTrace();
91100
}
@@ -96,34 +105,69 @@ public EasyRouter with(@NonNull Context context) {
96105
return this;
97106
}
98107

99-
public void navigate(String pageName) {
108+
public void navigate(@NonNull String pageName) {
100109
Log.i(TAG, "navigate: ");
101-
if (TextUtils.isEmpty(pageName) || mRouterMap.get(pageName) == null) {
110+
if (!TextUtils.isEmpty(pageName) && !TextUtils.isEmpty(mRouterMap.get(pageName))) {
111+
Intent intent = new Intent();
112+
intent.setClassName(mContext.getPackageName(), mRouterMap.get(pageName));
113+
if (mBundle != null) {
114+
intent.putExtras(mBundle);
115+
}
116+
mContext.startActivity(intent);
117+
} else {
102118
Logger.e(TAG, PAGE_NAME_NOT_AVAIRABLE, StackTraceUtil.getStackTrace(), "pageName: " + pageName);
103-
return;
104-
}
105-
Intent intent = new Intent(mContext, (Class<?>) mRouterMap.get(pageName));
106-
if (mBundle != null){
107-
intent.putExtras(mBundle);
108119
}
109-
mContext.startActivity(intent);
110120
}
111121

112122
/**
113123
* 编译时注解扫描所有 pageName,然后返回路由表
114124
* {@link Router}
115125
*/
116-
public void getRouterMapFromAnno() {
126+
public void getRouterMapFromAnno(Application application) {
127+
List<String> moduleList = getModuleList(application);
128+
if (moduleList == null || moduleList.size() == 0) {
129+
return;
130+
}
117131
try {
118-
Class clazz = Class.forName("top.omooo.easyrouter.RouterFactory");
119-
Method method = clazz.getMethod("init");
120-
method.invoke(null);
121-
mRouterMap = (HashMap<String, Object>) clazz.getField("sHashMap").get(clazz);
132+
for (String moduleName : moduleList) {
133+
Class clazz = Class.forName(moduleName + ".factory.RouterFactory");
134+
Method method = clazz.getMethod("init");
135+
method.invoke(null);
136+
mRouterMap.putAll((HashMap<String, String>) clazz.getField("sHashMap").get(clazz));
137+
}
122138
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException | NoSuchFieldException e) {
123139
e.printStackTrace();
124140
}
125141
}
126142

143+
/**
144+
* 获取所有包含 {@link Router} 注解的 module
145+
*/
146+
private List<String> getModuleList(Application application) {
147+
List<String> moduleList = new ArrayList<>();
148+
StringBuilder stringBuilder = new StringBuilder();
149+
try {
150+
//获取assets资源管理器
151+
AssetManager assetManager = application.getAssets();
152+
//通过管理器打开文件并读取
153+
BufferedReader bf = new BufferedReader(new InputStreamReader(
154+
assetManager.open("module_info")));
155+
String line;
156+
while ((line = bf.readLine()) != null) {
157+
stringBuilder.append(line);
158+
}
159+
JSONObject jsonObject = new JSONObject(stringBuilder.toString());
160+
JSONArray jsonArray = jsonObject.getJSONArray("modules");
161+
for (int i = 0; i < jsonArray.length(); i++) {
162+
moduleList.add(((JSONObject) jsonArray.get(i)).getString("packageName"));
163+
}
164+
return moduleList;
165+
} catch (Exception e) {
166+
e.printStackTrace();
167+
}
168+
return null;
169+
}
170+
127171
/**
128172
* 添加 Bundle 数据
129173
*/
@@ -137,6 +181,7 @@ public EasyRouter putString(String key, String value) {
137181
Log.i(TAG, "putString: ");
138182
if (mBundle == null) {
139183
mBundle = new Bundle();
184+
mBundle.putString(key, value);
140185
} else {
141186
mBundle.putString(key, value);
142187
}

router_processor/src/main/java/top/omooo/router_processor/BindMetaDataProcessor.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import javax.lang.model.SourceVersion;
2020
import javax.lang.model.element.Element;
2121
import javax.lang.model.element.Modifier;
22+
import javax.lang.model.element.PackageElement;
2223
import javax.lang.model.element.TypeElement;
2324

2425
import top.omooo.router_annotations.annotations.Router;
@@ -31,13 +32,11 @@
3132
@AutoService(Processor.class)
3233
public class BindMetaDataProcessor extends AbstractProcessor {
3334

34-
private HashMap<String, Object> mRouterMap;
3535
private Filer mFiler;
3636

3737
@Override
3838
public synchronized void init(ProcessingEnvironment processingEnvironment) {
3939
super.init(processingEnvironment);
40-
mRouterMap = new HashMap<>();
4140
mFiler = processingEnvironment.getFiler();
4241
}
4342

@@ -67,13 +66,17 @@ private void createFile(Set<? extends Element> elements) {
6766
MethodSpec.Builder methodBuilder = MethodSpec
6867
.methodBuilder("init")
6968
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
70-
.addException(ClassNotFoundException.class)
7169
.returns(void.class);
7270
methodBuilder.addStatement("sHashMap = new HashMap()");
71+
String packageName = "";
7372
for (Element element : elements) {
7473
TypeElement typeElement = (TypeElement) element;
7574
Router metaDataAnn = typeElement.getAnnotation(Router.class);
76-
methodBuilder.addStatement("sHashMap.put($S,$T.forName($S))", metaDataAnn.value(), Class.class, typeElement.getQualifiedName().toString());
75+
methodBuilder.addStatement("sHashMap.put($S,$S)", metaDataAnn.value(), typeElement.getQualifiedName());
76+
if (packageName.equals("")) {
77+
packageName = typeElement.getQualifiedName().toString()
78+
.replace("." + typeElement.getSimpleName(), "");
79+
}
7780
}
7881

7982
TypeSpec type = TypeSpec
@@ -83,7 +86,7 @@ private void createFile(Set<? extends Element> elements) {
8386
.addField(fieldSpec)
8487
.build();
8588
JavaFile javaFile = JavaFile
86-
.builder("top.omooo.easyrouter", type)
89+
.builder(packageName + ".factory", type)
8790
.build();
8891
try {
8992
javaFile.writeTo(mFiler);

0 commit comments

Comments
 (0)