Skip to content

Commit

Permalink
support to find route extras by uri
Browse files Browse the repository at this point in the history
  • Loading branch information
yjfnypeu committed Aug 11, 2017
1 parent 6d7b17a commit a72791d
Show file tree
Hide file tree
Showing 13 changed files with 314 additions and 250 deletions.
8 changes: 4 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ dependencies {
annotationProcessor "org.lzh.compiler.parceler:parceler-compiler:$PARCELER_VERSION"
compile "org.lzh.compiler.parceler:parceler-api:$PARCELER_VERSION"

compile "com.github.yjfnypeu.Router:router-api:$ROUTER_VERSION"
annotationProcessor "com.github.yjfnypeu.Router:router-compiler:$ROUTER_VERSION"
// compile "com.github.yjfnypeu.Router:router-api:$ROUTER_VERSION"
// annotationProcessor "com.github.yjfnypeu.Router:router-compiler:$ROUTER_VERSION"

// compile project(':routerlib')
// annotationProcessor project(':compiler')
compile project(':routerlib')
annotationProcessor project(':compiler')
// compile project(':host')
}
58 changes: 22 additions & 36 deletions routerlib/src/main/java/com/lzh/nonview/router/Router.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,20 @@
import android.os.Bundle;

import com.lzh.nonview.router.exception.NotFoundException;
import com.lzh.nonview.router.extras.RouteBundleExtras;
import com.lzh.nonview.router.interceptors.RouteInterceptor;
import com.lzh.nonview.router.module.RouteCreator;
import com.lzh.nonview.router.module.RouteRule;
import com.lzh.nonview.router.protocol.HostServiceWrapper;
import com.lzh.nonview.router.route.ActionRoute;
import com.lzh.nonview.router.route.ActivityRoute;
import com.lzh.nonview.router.route.BaseRoute;
import com.lzh.nonview.router.route.BrowserRoute;
import com.lzh.nonview.router.route.IActionRoute;
import com.lzh.nonview.router.route.IActivityRoute;
import com.lzh.nonview.router.route.IBaseRoute;
import com.lzh.nonview.router.route.IRoute;
import com.lzh.nonview.router.route.InternalCallback;
import com.lzh.nonview.router.extras.RouteBundleExtras;
import com.lzh.nonview.router.route.RouteCallback;
import com.lzh.nonview.router.tools.Cache;
import com.lzh.nonview.router.tools.Utils;
Expand All @@ -55,11 +57,11 @@ public final class Router{
public static final String RAW_URI = "_ROUTER_RAW_URI_KEY_";

private Uri uri;
private RouteCallback callback;
private RouteCallback.InternalCallback internalCallback;
private InternalCallback internalCallback;

private Router(Uri uri) {
this.uri = Utils.completeUri(uri);
internalCallback = new InternalCallback(uri);
}

/**
Expand All @@ -84,26 +86,12 @@ public static Router create(Uri uri) {
* Set a callback to notify the user when the routing were success or failure.
* @param callback The callback you set.
* @return Router itself
*
* @see Router#getCallback()
*/
public Router setCallback (RouteCallback callback) {
this.callback = callback;
this.internalCallback.setCallback(callback);
return this;
}

/**
* Obtain a callback put to use. will not be null.
* @return if you had not set yet, it will returns a global callback obtain from {@link RouterConfiguration#getCallback()}
*/
private RouteCallback.InternalCallback getCallback () {
if (internalCallback == null) {
internalCallback = new RouteCallback.InternalCallback();
internalCallback.setCallback(callback);
}
return internalCallback;
}

/**
* Restore a Routing event from last uri and extras.
* @param uri last uri
Expand All @@ -112,10 +100,8 @@ private RouteCallback.InternalCallback getCallback () {
*/
public static IRoute resume(Uri uri, RouteBundleExtras extras) {
IRoute route = Router.create(uri).getRoute();
if (route instanceof ActivityRoute) {
((ActivityRoute) route).replaceExtras(extras);
} else if (route instanceof ActionRoute) {
((ActionRoute) route).replaceExtras(extras);
if (route instanceof BaseRoute) {
((BaseRoute) route).replaceExtras(extras);
}
return route;
}
Expand All @@ -133,15 +119,15 @@ public void open(Context context) {
* Get route by uri, you should get a route by this way and set some extras data before open
* @return
* An IRoute object.it will be {@link BrowserRoute}, {@link ActivityRoute} or {@link ActionRoute}.<br>
* and it also will be {@link IRoute#EMPTY} if it not found
* and it also will be {@link IRoute.EmptyRoute} if it not found
*/
public IRoute getRoute () {
IRoute route = getLocalRoute();
if (route != IRoute.EMPTY) {
if (!(route instanceof IRoute.EmptyRoute)) {
return route;
}
route = HostServiceWrapper.create(uri, getCallback());
if (route == IRoute.EMPTY) {
route = HostServiceWrapper.create(uri, internalCallback);
if (!(route instanceof IRoute.EmptyRoute)) {
notifyNotFound(String.format("find Route by %s failed:",uri));
}
return route;
Expand All @@ -150,13 +136,13 @@ public IRoute getRoute () {
private IRoute getLocalRoute() {
RouteRule rule;
if ((rule = ActionRoute.findRule(uri, Cache.TYPE_ACTION_ROUTE)) != null) {
return new ActionRoute().create(uri, rule, new Bundle(), getCallback());
return new ActionRoute().create(uri, rule, new Bundle(), internalCallback);
} else if ((rule = ActivityRoute.findRule(uri, Cache.TYPE_ACTIVITY_ROUTE)) != null) {
return new ActivityRoute().create(uri, rule, new Bundle(), getCallback());
return new ActivityRoute().create(uri, rule, new Bundle(), internalCallback);
} else if (BrowserRoute.canOpenRouter(uri)) {
return BrowserRoute.getInstance().setUri(uri);
} else {
return IRoute.EMPTY;
return new IRoute.EmptyRoute(internalCallback);
}
}

Expand All @@ -165,7 +151,7 @@ private IRoute getLocalRoute() {
* Get {@link IBaseRoute} by uri, it could be one of {@link IActivityRoute} or {@link IActionRoute}.
* and you can add some {@link android.os.Bundle} data and {@link RouteInterceptor} into it.
* </p>
* @return returns an {@link IBaseRoute} finds by uri or {@link IBaseRoute#EMPTY} for not found
* @return returns an {@link IBaseRoute} finds by uri or {@link IBaseRoute.EmptyBaseRoute} for not found
*/
public IBaseRoute getBaseRoute() {
IRoute route = getRoute();
Expand All @@ -174,12 +160,12 @@ public IBaseRoute getBaseRoute() {
}

notifyNotFound(String.format("find BaseRoute by %s failed, but is %s",uri, route.getClass().getSimpleName()));
return IBaseRoute.EMPTY;
return new IBaseRoute.EmptyBaseRoute(internalCallback);
}

/**
* Get {@link IActivityRoute} by uri,you should get a route by this way and set some extras data before open
* @return returns an {@link IActivityRoute} finds by uri or {@link IActivityRoute#EMPTY} for not found.
* @return returns an {@link IActivityRoute} finds by uri or {@link IActivityRoute.EmptyActivityRoute} for not found.
*/
public IActivityRoute getActivityRoute() {
IRoute route = getRoute();
Expand All @@ -189,12 +175,12 @@ public IActivityRoute getActivityRoute() {

// return an empty route to avoid NullPointException
notifyNotFound(String.format("find ActivityRoute by %s failed, but is %s",uri, route.getClass().getSimpleName()));
return IActivityRoute.EMPTY;
return new IActivityRoute.EmptyActivityRoute(internalCallback);
}

/**
* Get {@link IActionRoute} by uri,you should get a route by this way and set some extras data before open
* @return returns an {@link IActionRoute} finds by uri or {@link IActionRoute#EMPTY} for not found.
* @return returns an {@link IActionRoute} finds by uri or {@link IActionRoute.EmptyActionRoute} for not found.
*/
public IActionRoute getActionRoute() {
IRoute route = getRoute();
Expand All @@ -204,11 +190,11 @@ public IActionRoute getActionRoute() {

notifyNotFound(String.format("find ActionRoute by %s failed, but is %s",uri, route.getClass().getSimpleName()));
// return a empty route to avoid NullPointException
return IActionRoute.EMPTY;
return new IActionRoute.EmptyActionRoute(internalCallback);
}

private void notifyNotFound(String msg) {
getCallback().notFound(uri, new NotFoundException(msg, NotFoundException.TYPE_SCHEMA, uri.toString()));
internalCallback.notFound(new NotFoundException(msg, NotFoundException.TYPE_SCHEMA, uri.toString()));
}

/** Consider to change entrance to {@link RouterConfiguration#setCallback(RouteCallback)}*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,85 @@ public final class RouteBundleExtras implements Parcelable, RouteInterceptorActi
private Bundle extras = new Bundle();
private ArrayList<RouteInterceptor> interceptors = new ArrayList<>();
private RouteCallback callback;

// the extras belows is only supports for ActivityRoute.
private int requestCode = -1;
private int inAnimation = -1;
private int outAnimation = -1;
private int flags = 0;

public RouteBundleExtras() {}

protected RouteBundleExtras(Parcel in) {
@SuppressWarnings("ConstantConditions")
@Override
public RouteBundleExtras addInterceptor(RouteInterceptor interceptor) {
if (interceptor != null && !interceptors.contains(interceptor)) {
interceptors.add(interceptor);
}
return this;
}

@Override
public RouteBundleExtras removeInterceptor(RouteInterceptor interceptor) {
if (interceptor != null) {
interceptors.remove(interceptor);
}
return this;
}

@Override
public RouteBundleExtras removeAllInterceptors() {
interceptors.clear();
return this;
}

@Override
public List<RouteInterceptor> getInterceptors() {
return interceptors;
}

public void setCallback(RouteCallback callback) {
this.callback = callback;
}

public RouteCallback getCallback() {
return callback;
}

public int getRequestCode() {
return requestCode;
}

public void setRequestCode(int requestCode) {
this.requestCode = requestCode;
}

public int getInAnimation() {
return inAnimation;
}

public void setInAnimation(int inAnimation) {
this.inAnimation = inAnimation;
}

public int getOutAnimation() {
return outAnimation;
}

public void setOutAnimation(int outAnimation) {
this.outAnimation = outAnimation;
}

public int getFlags() {
return flags;
}

public void addFlags(int flags) {
this.flags |= flags;
}

// ------------------------- divider for parcelable ------------------------
private RouteBundleExtras(Parcel in) {
requestCode = in.readInt();
inAnimation = in.readInt();
outAnimation = in.readInt();
Expand Down Expand Up @@ -143,72 +215,4 @@ public void addExtras(Bundle extras) {
this.extras.putAll(extras);
}
}

@SuppressWarnings("ConstantConditions")
@Override
public RouteBundleExtras addInterceptor(RouteInterceptor interceptor) {
if (interceptor != null && !interceptors.contains(interceptor)) {
interceptors.add(interceptor);
}
return this;
}

@Override
public RouteBundleExtras removeInterceptor(RouteInterceptor interceptor) {
if (interceptor != null) {
interceptors.remove(interceptor);
}
return this;
}

@Override
public RouteBundleExtras removeAllInterceptors() {
interceptors.clear();
return this;
}

@Override
public List<RouteInterceptor> getInterceptors() {
return interceptors;
}

public void setCallback(RouteCallback callback) {
this.callback = callback;
}

public RouteCallback getCallback() {
return callback;
}

public int getRequestCode() {
return requestCode;
}

public void setRequestCode(int requestCode) {
this.requestCode = requestCode;
}

public int getInAnimation() {
return inAnimation;
}

public void setInAnimation(int inAnimation) {
this.inAnimation = inAnimation;
}

public int getOutAnimation() {
return outAnimation;
}

public void setOutAnimation(int outAnimation) {
this.outAnimation = outAnimation;
}

public int getFlags() {
return flags;
}

public void addFlags(int flags) {
this.flags |= flags;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import com.lzh.nonview.router.route.ActionRoute;
import com.lzh.nonview.router.route.ActivityRoute;
import com.lzh.nonview.router.route.IRoute;
import com.lzh.nonview.router.route.RouteCallback;
import com.lzh.nonview.router.route.InternalCallback;
import com.lzh.nonview.router.tools.Cache;

import java.util.HashMap;
Expand All @@ -38,7 +38,7 @@
public class HostServiceWrapper {

private static String hostPackage;
static Context context;
private static Context context;
private static IService service;

private static ServiceConnection connection = new ServiceConnection() {
Expand Down Expand Up @@ -77,22 +77,22 @@ public static void startHostService(String hostPackage, Context context) {
context.bindService(intent, connection, Service.BIND_AUTO_CREATE);
}

public static IRoute create(Uri uri, RouteCallback.InternalCallback callback) {
public static IRoute create(Uri uri, InternalCallback callback) {
try {
return createWithThrow(uri, callback);
} catch (Exception e) {
return IRoute.EMPTY;
return new IRoute.EmptyRoute(callback);
}
}

private static IRoute createWithThrow(Uri uri, RouteCallback.InternalCallback callback) throws Exception{
private static IRoute createWithThrow(Uri uri, InternalCallback callback) throws Exception{
RemoteRule rule;
if ((rule = service.getActivityRule(uri)) != null) {
return new ActivityRoute().create(uri, rule.getRule(), rule.getExtra(), callback);
} else if ((rule = service.getActionRule(uri)) != null) {
return new ActionRoute().create(uri, rule.getRule(), rule.getExtra(), callback);
} else {
return IRoute.EMPTY;
return new IRoute.EmptyRoute(callback);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

public class ActionRoute extends BaseRoute<IActionRoute> implements IActionRoute {


@Override
protected Launcher obtainLauncher() throws Exception{
ActionRouteRule rule = (ActionRouteRule) routeRule;
Expand Down
Loading

0 comments on commit a72791d

Please sign in to comment.