Skip to content

Commit

Permalink
更改生成路由表文件策略
Browse files Browse the repository at this point in the history
  • Loading branch information
Omooo committed Mar 26, 2019
1 parent 674a9d7 commit c380640
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 22 deletions.
10 changes: 10 additions & 0 deletions app/src/main/Assets/module_info
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"modules": [
{
"packageName": "top.omooo.easyrouter"
},
{
"packageName": "top.omooo.other_module"
}
]
}
79 changes: 62 additions & 17 deletions router/src/main/java/top/omooo/router/EasyRouter.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,26 @@
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.text.TextUtils;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import top.omooo.logger.Logger;
import top.omooo.logger.StackTraceUtil;
Expand All @@ -32,7 +41,7 @@ public class EasyRouter {

private static final String PAGE_NAME = "pageName";
//路由表 key:pageName value: Activity
private HashMap<String, Object> mRouterMap;
private HashMap<String, String> mRouterMap;
private Context mContext;
private Bundle mBundle;

Expand All @@ -58,7 +67,7 @@ private static class EasyRouterHolder {
public void inject(Application application) {
mRouterMap = new HashMap<>();
if (isUseAnno) {
getRouterMapFromAnno();
getRouterMapFromAnno(application);
return;
}
//使用 Meta-Data
Expand All @@ -79,13 +88,13 @@ public void inject(Application application) {
if (TextUtils.isEmpty(bundle.getString(PAGE_NAME))) {
Logger.e(TAG, PAGE_NAME_EMPTY_DESC, StackTraceUtil.getStackTrace(), activityInfo.name);
} else {
mRouterMap.put(bundle.getString(PAGE_NAME), Class.forName(activityInfo.name));
mRouterMap.put(bundle.getString(PAGE_NAME), activityInfo.name);
Logger.d(TAG, null, "ClassName: " + activityInfo.name,
"PageName: " + bundle.getString(PAGE_NAME));
}
}
}
} catch (PackageManager.NameNotFoundException | ClassNotFoundException e) {
} catch (PackageManager.NameNotFoundException e) {
//ignore
e.printStackTrace();
}
Expand All @@ -96,34 +105,69 @@ public EasyRouter with(@NonNull Context context) {
return this;
}

public void navigate(String pageName) {
public void navigate(@NonNull String pageName) {
Log.i(TAG, "navigate: ");
if (TextUtils.isEmpty(pageName) || mRouterMap.get(pageName) == null) {
if (!TextUtils.isEmpty(pageName) && !TextUtils.isEmpty(mRouterMap.get(pageName))) {
Intent intent = new Intent();
intent.setClassName(mContext.getPackageName(), mRouterMap.get(pageName));
if (mBundle != null) {
intent.putExtras(mBundle);
}
mContext.startActivity(intent);
} else {
Logger.e(TAG, PAGE_NAME_NOT_AVAIRABLE, StackTraceUtil.getStackTrace(), "pageName: " + pageName);
return;
}
Intent intent = new Intent(mContext, (Class<?>) mRouterMap.get(pageName));
if (mBundle != null){
intent.putExtras(mBundle);
}
mContext.startActivity(intent);
}

/**
* 编译时注解扫描所有 pageName,然后返回路由表
* {@link Router}
*/
public void getRouterMapFromAnno() {
public void getRouterMapFromAnno(Application application) {
List<String> moduleList = getModuleList(application);
if (moduleList == null || moduleList.size() == 0) {
return;
}
try {
Class clazz = Class.forName("top.omooo.easyrouter.RouterFactory");
Method method = clazz.getMethod("init");
method.invoke(null);
mRouterMap = (HashMap<String, Object>) clazz.getField("sHashMap").get(clazz);
for (String moduleName : moduleList) {
Class clazz = Class.forName(moduleName + ".factory.RouterFactory");
Method method = clazz.getMethod("init");
method.invoke(null);
mRouterMap.putAll((HashMap<String, String>) clazz.getField("sHashMap").get(clazz));
}
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException | NoSuchFieldException e) {
e.printStackTrace();
}
}

/**
* 获取所有包含 {@link Router} 注解的 module
*/
private List<String> getModuleList(Application application) {
List<String> moduleList = new ArrayList<>();
StringBuilder stringBuilder = new StringBuilder();
try {
//获取assets资源管理器
AssetManager assetManager = application.getAssets();
//通过管理器打开文件并读取
BufferedReader bf = new BufferedReader(new InputStreamReader(
assetManager.open("module_info")));
String line;
while ((line = bf.readLine()) != null) {
stringBuilder.append(line);
}
JSONObject jsonObject = new JSONObject(stringBuilder.toString());
JSONArray jsonArray = jsonObject.getJSONArray("modules");
for (int i = 0; i < jsonArray.length(); i++) {
moduleList.add(((JSONObject) jsonArray.get(i)).getString("packageName"));
}
return moduleList;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

/**
* 添加 Bundle 数据
*/
Expand All @@ -137,6 +181,7 @@ public EasyRouter putString(String key, String value) {
Log.i(TAG, "putString: ");
if (mBundle == null) {
mBundle = new Bundle();
mBundle.putString(key, value);
} else {
mBundle.putString(key, value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;

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

private HashMap<String, Object> mRouterMap;
private Filer mFiler;

@Override
public synchronized void init(ProcessingEnvironment processingEnvironment) {
super.init(processingEnvironment);
mRouterMap = new HashMap<>();
mFiler = processingEnvironment.getFiler();
}

Expand Down Expand Up @@ -67,13 +66,17 @@ private void createFile(Set<? extends Element> elements) {
MethodSpec.Builder methodBuilder = MethodSpec
.methodBuilder("init")
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.addException(ClassNotFoundException.class)
.returns(void.class);
methodBuilder.addStatement("sHashMap = new HashMap()");
String packageName = "";
for (Element element : elements) {
TypeElement typeElement = (TypeElement) element;
Router metaDataAnn = typeElement.getAnnotation(Router.class);
methodBuilder.addStatement("sHashMap.put($S,$T.forName($S))", metaDataAnn.value(), Class.class, typeElement.getQualifiedName().toString());
methodBuilder.addStatement("sHashMap.put($S,$S)", metaDataAnn.value(), typeElement.getQualifiedName());
if (packageName.equals("")) {
packageName = typeElement.getQualifiedName().toString()
.replace("." + typeElement.getSimpleName(), "");
}
}

TypeSpec type = TypeSpec
Expand All @@ -83,7 +86,7 @@ private void createFile(Set<? extends Element> elements) {
.addField(fieldSpec)
.build();
JavaFile javaFile = JavaFile
.builder("top.omooo.easyrouter", type)
.builder(packageName + ".factory", type)
.build();
try {
javaFile.writeTo(mFiler);
Expand Down

0 comments on commit c380640

Please sign in to comment.