Skip to content

Commit

Permalink
update doc
Browse files Browse the repository at this point in the history
  • Loading branch information
yjfnypeu committed Aug 25, 2017
1 parent 1f8ed97 commit 3b4c8e3
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 40 deletions.
29 changes: 24 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,20 @@ compile "com.github.yjfnypeu.Router-RePlugin:plugin:${LastestVersion}"

1. 在每个插件或者宿主中。添加注册自身的编译时生成的路由表类:

```java
```
// 添加路由规则。
RouterConfiguration.get().addRouteCreator(new RouterRuleCreator());
```

2. 在宿主中进行初始化:

```java
```
HostRouterConfiguration.init(hostPackage, context);
```

3. 在插件中进行初始化:

```java
```
PluginRouterConfiguration.init(hostPackage, alias, context);
```

Expand All @@ -54,7 +54,26 @@ PluginRouterConfiguration.init(hostPackage, alias, context);

### Advance

[更多进阶用法。请参考wiki](https://github.com/yjfnypeu/Router-RePlugin/wiki)
框架提供了一些额外接口。便于更好的进行使用,所有的配置项均在宿主-插件对应的配置入口类中进行配置:

1. [IUriConverter](https://github.com/yjfnypeu/Router-RePlugin/blob/master/core/src/main/java/com/lzh/router/replugin/core/IUriConverter.java)

- 作用:当用于启动路由的uri在路由表中未找到时,使用此装换器通过uri获取到对应的插件别名
- 配置:[HostRouterConfiguration/PluginRouterConfiguration].setConverter(converter)
- 接口说明:[点击查看说明文档](https://github.com/yjfnypeu/Router-RePlugin/blob/master/core/src/main/java/com/lzh/router/replugin/core/IUriConverter.java)

2. [IPluginCallback](https://github.com/yjfnypeu/Router-RePlugin/blob/master/core/src/main/java/com/lzh/router/replugin/core/IPluginCallback.java)

- 作用:用于在对插件路由的接入过程中。出现的问题进行通知。
- 配置:[HostRouterConfiguration/PluginRouterConfiguration].setCallback(callback)
- 接口说明:[点击查看说明文档](https://github.com/yjfnypeu/Router-RePlugin/blob/master/core/src/main/java/com/lzh/router/replugin/core/IPluginCallback.java)

3. 兼容Router全局路由回调:

- 作用:

由于Router本身只提供了一个独立的全局路由回调。而Router-RePlugin框架本身内部是基于此全局回调来做的兼容配置。所以对于需要使用全局回调的需求。可以使用下方配置进行接入
- 配置:[HostRouterConfiguration/PluginRouterConfiguration].setRouteCallback(routeCallback)

### 联系作者
email: 470368500@qq.com
Expand All @@ -65,7 +84,7 @@ email: 470368500@qq.com

## License
```
Copyright 2015 Haoge
Copyright 2017 Haoge
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import android.net.Uri;

/**
* 插件的回调。
* 插件的回调。用于在对插件路由的接入过程中。出现的问题进行通知。
* Created by haoge on 2017/8/24.
*/
public interface IPluginCallback {
Expand All @@ -16,8 +16,15 @@ public interface IPluginCallback {

/**
* 当此uri所代表的路由未找到时且此uri所对应的插件也启动了的情况下。回调通知到此。
*
* @param uri uri
* @param alias 插件别名。
*/
void notFound(Uri uri, String alias);

/**
* 当此uri对应的插件被加载成功。且成功恢复时。回调通知到此
* @param uri 恢复的路由uri
*/
void onResume(Uri uri);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@
import android.net.Uri;

/**
* 用于当指定路由为匹配到对应的路由规则时。对路由的uri进行转换。转换为对应的RePlugin所需要的包名
* 当uri所对应的路由未找到时。对uri进行转换。转换为对应的需要加载的插件别名。提供给{@link RePluginRouteCallback} 做插件接入
*
* @see IUriConverter#internal
* Created by haoge on 2017/8/24.
*/
public interface IUriConverter {

/**
* 将从uri中获取出正确的插件别名。或者返回null,代表此uri无效。
* @param uri 未找到的路由uri
* @return 根据uri转换后的插件别名。
* @return 根据uri转换后的插件别名。或者当别名为不支持时。返回null。
*/
String transform(Uri uri);

/**
* 默认的插件路由规则转换器。此转换器的规则为:路由url的scheme为各自路由的别名
* 默认的插件路由规则转换器。此转换器的规则为:使用路由uri的scheme作为各自插件的别名
*/
IUriConverter internal = new IUriConverter() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,49 @@
import com.lzh.nonview.router.route.RouteCallback;
import com.qihoo360.replugin.RePlugin;

import java.lang.ref.WeakReference;

/**
* 针对RePlugin框架所配置的路由回调。将在此进行连接Router-RePlugin配置:
*
* Created by haoge on 2017/8/24.
*/
public final class RePluginRouteCallback implements RouteCallback{
private Context context;// application context.

private static RePluginRouteCallback instance = new RePluginRouteCallback();
private RePluginRouteCallback() {}
public static RePluginRouteCallback get() {
return instance;
}

private WeakReference<Context> context;// application context.
private IUriConverter converter = IUriConverter.internal;
private IPluginCallback callback;

private RouteCallback routeCallback;

public RePluginRouteCallback(Context context) {
this.context = context.getApplicationContext();
public RePluginRouteCallback setContext(Context context) {
if (context != null) {
this.context = new WeakReference<>(context.getApplicationContext());
}
return this;
}

public void setConverter(IUriConverter converter) {
public RePluginRouteCallback setConverter(IUriConverter converter) {
if (converter != null) {
this.converter = converter;
}
return this;
}

public void setCallback(IPluginCallback callback) {
public RePluginRouteCallback setCallback(IPluginCallback callback) {
this.callback = callback;
return this;
}

public void setRouteCallback(RouteCallback routeCallback) {
public RePluginRouteCallback setRouteCallback(RouteCallback routeCallback) {
this.routeCallback = routeCallback;
return this;
}

@Override
Expand All @@ -64,12 +80,26 @@ public void notFound(Uri uri, NotFoundException e) {
return;
}

Context application = getValidContext();

// 请求加载插件并启动中间桥接页面.便于加载插件成功后恢复路由。
Intent intent = RePlugin.createIntent(alias, RouterBridgeActivity.class.getCanonicalName());
intent.putExtra("uri", uri);
intent.putExtra("extras", RouterConfiguration.get().restoreExtras(uri));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
RePlugin.startActivity(context, intent);
RePlugin.startActivity(application, intent);
}

/**
* 获取有效的context进行跳转。一般不会被触发。
* @return 保存的有效context或者Host context。
*/
private Context getValidContext() {
Context valid = context.get();
if (valid != null) {
return valid;
}
return RePlugin.getHostContext();
}

@Override
Expand All @@ -85,4 +115,8 @@ public void onOpenFailed(Uri uri, Throwable e) {
routeCallback.onOpenFailed(uri, e);
}
}

public IPluginCallback getCallback() {
return callback;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ protected void onCreate(Bundle savedInstanceState) {
// 通过传递过来的uri与其对应的extras。恢复之前插件未加载时的路由。
Router.resume(uri, extras).open(RouterBridgeActivity.this);
}
RePluginRouteCallback.get().getCallback().onResume(uri);
finish();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.content.Context;

import com.lzh.nonview.router.RouterConfiguration;
import com.lzh.nonview.router.route.RouteCallback;
import com.lzh.router.replugin.core.IPluginCallback;
import com.lzh.router.replugin.core.IUriConverter;
import com.lzh.router.replugin.core.RePluginRouteCallback;
Expand All @@ -13,7 +14,6 @@
*/
public final class HostRouterConfiguration {

private RePluginRouteCallback callback;
/**
* 初始化加载。此方法每个插件只需要被加载一次。请尽早进行初始化。
* @param hostPackage 宿主包名:用于指定启动、连接远程路由服务。为各插件提供路由索引功能
Expand All @@ -23,15 +23,19 @@ public static void init(String hostPackage, Context context) {
// 启动并连接远程路由服务。
RouterConfiguration.get().startHostService(hostPackage, context);
// 初始化callback.
RouterConfiguration.get().setCallback(get().callback = new RePluginRouteCallback(context));
RouterConfiguration.get().setCallback(RePluginRouteCallback.get().setContext(context));
// 设置路由启动器
RouterConfiguration.get().setActionLauncher(HostActionLauncher.class);
RouterConfiguration.get().setActivityLauncher(HostActivityLauncher.class);
}

public HostRouterConfiguration setCallback(IPluginCallback callback) {
check();
this.callback.setCallback(callback);
RePluginRouteCallback.get().setCallback(callback);
return this;
}

public HostRouterConfiguration setRouteCallback(RouteCallback callback) {
RePluginRouteCallback.get().setRouteCallback(callback);
return this;
}

Expand All @@ -43,19 +47,12 @@ public HostRouterConfiguration setCallback(IPluginCallback callback) {
* @return configuration
*/
public HostRouterConfiguration setConverter(IUriConverter converter) {
check();
if (converter != null) {
this.callback.setConverter(converter);
RePluginRouteCallback.get().setConverter(converter);
}
return this;
}

private void check() {
if (callback == null) {
throw new RuntimeException("Should call PluginRouterConfiguration.init() first!");
}
}

private static HostRouterConfiguration configuration = new HostRouterConfiguration();
private HostRouterConfiguration() {}
public static HostRouterConfiguration get() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.content.Context;

import com.lzh.nonview.router.RouterConfiguration;
import com.lzh.nonview.router.route.RouteCallback;
import com.lzh.router.replugin.core.IPluginCallback;
import com.lzh.router.replugin.core.IUriConverter;
import com.lzh.router.replugin.core.RePluginRouteCallback;
Expand All @@ -13,7 +14,6 @@
*/
public final class PluginRouterConfiguration {

private RePluginRouteCallback callback;
/**
* 初始化加载。此方法每个插件只需要被加载一次。请尽早进行初始化。
* @param hostPackage 宿主包名:用于指定启动、连接远程路由服务。为各插件提供路由索引功能
Expand All @@ -26,15 +26,19 @@ public static void init(String hostPackage, String alias, Context context) {
// 提供远程数据创建工厂
RouterConfiguration.get().setRemoteFactory(new PluginRemoteFactory(alias));
// 初始化callback.
RouterConfiguration.get().setCallback(get().callback = new RePluginRouteCallback(context));
RouterConfiguration.get().setCallback(RePluginRouteCallback.get().setContext(context));
// 设置路由启动器
RouterConfiguration.get().setActionLauncher(PluginActionLauncher.class);
RouterConfiguration.get().setActivityLauncher(PluginActivityLauncher.class);
}

public PluginRouterConfiguration setCallback(IPluginCallback callback) {
check();
this.callback.setCallback(callback);
RePluginRouteCallback.get().setCallback(callback);
return this;
}

public PluginRouterConfiguration setRouteCallback(RouteCallback callback) {
RePluginRouteCallback.get().setRouteCallback(callback);
return this;
}

Expand All @@ -46,19 +50,12 @@ public PluginRouterConfiguration setCallback(IPluginCallback callback) {
* @return configuration
*/
public PluginRouterConfiguration setConverter(IUriConverter converter) {
check();
if (converter != null) {
this.callback.setConverter(converter);
RePluginRouteCallback.get().setConverter(converter);
}
return this;
}

private void check() {
if (callback == null) {
throw new RuntimeException("Should call PluginRouterConfiguration.init() first!");
}
}

private static PluginRouterConfiguration configuration = new PluginRouterConfiguration();
private PluginRouterConfiguration() {}
public static PluginRouterConfiguration get() {
Expand Down

0 comments on commit 3b4c8e3

Please sign in to comment.