Skip to content

Commit

Permalink
Add the method open(Context, String), Add method to output logs of An…
Browse files Browse the repository at this point in the history
…drouter
  • Loading branch information
beautifulSoup committed May 12, 2016
1 parent e518d09 commit d791da3
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 11 deletions.
15 changes: 15 additions & 0 deletions router/src/main/java/cn/campusapp/router/Router.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import cn.campusapp.router.router.BrowserRouter;
import cn.campusapp.router.router.IActivityRouteTableInitializer;
import cn.campusapp.router.router.IRouter;
import timber.log.Timber;

/**
* Created by kris on 16/3/17.
Expand Down Expand Up @@ -40,6 +41,20 @@ public static boolean open(String url){
return RouterManager.getSingleton().open(url);
}

public static boolean open(Context context, String url){
return RouterManager.getSingleton().open(context, url);
}

/**
* AndRouter uses Timber to output logs. Timber needs init, so if you don't use Timber and you want to view logs of AndRouter, you may need to
* use this method, and set the debug as true
*/
public static void setDebugMode(boolean debug){
if(debug) {
Timber.plant(new Timber.DebugTree());
}
}

/**
* the route of the url, if there is not router to process the url, return null
* @param url
Expand Down
9 changes: 9 additions & 0 deletions router/src/main/java/cn/campusapp/router/RouterManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,15 @@ public IRoute getRoute(String url){
return null;
}

public boolean open(Context context, String url){
for(IRouter router : mRouters){
if(router.canOpenTheUrl(url)){
return router.open(context, url);
}
}
return false;
}


public boolean openRoute(IRoute route){
for(IRouter router : mRouters){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,22 @@ public boolean open(IRoute route) {

@Override
public boolean open(String url) {
return open(getRoute(url));
return open(null, url);
}

@Override
public boolean open(Context context, String url) {
IRoute route = getRoute(url);
if(route instanceof ActivityRoute){
ActivityRoute aRoute = (ActivityRoute) route;
try {
open(aRoute, context);
return true;
} catch (Exception e){
Timber.e(e, "Url route not specified: %s", route.getUrl());
}
}
return false;
}


Expand Down
19 changes: 14 additions & 5 deletions router/src/main/java/cn/campusapp/router/router/BrowserRouter.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ public void init(Context context){

@Override
public boolean open(IRoute route) {
Uri uri = Uri.parse(route.getUrl());
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mBaseContext.startActivity(intent);
return true;
return open(mBaseContext, route);
}

@Override
Expand All @@ -45,6 +41,19 @@ public boolean open(String url) {
return true;
}

@Override
public boolean open(Context context, String url) {
return open(context, getRoute(url));
}

protected boolean open(Context context, IRoute route){
Uri uri = Uri.parse(route.getUrl());
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
return true;
}

@Override
public IRoute getRoute(String url) {
return new BrowserRoute.Builder(this)
Expand Down
5 changes: 5 additions & 0 deletions router/src/main/java/cn/campusapp/router/router/IRouter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package cn.campusapp.router.router;

import android.content.Context;

import cn.campusapp.router.route.IRoute;

/**
Expand All @@ -22,6 +24,9 @@ public interface IRouter {
*/
boolean open(String url);


boolean open(Context context, String url);

/**
* build the route according to the url, if not match, return null
* @param url
Expand Down
6 changes: 3 additions & 3 deletions sample/src/main/java/cn/campusapp/router/sample/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import cn.campusapp.router.Router;
import cn.campusapp.router.router.IActivityRouteTableInitializer;
import timber.log.Timber;

/**
* Created by kris on 16/3/11.
Expand All @@ -23,8 +22,9 @@ public void initRouterTable(Map<String, Class<? extends Activity>> router) {
router.put("activity://second/:{name}", SecondActivity.class);
}
});
// Router.initActivityRouter(this);
// Router.initActivityRouter(this);
Router.initBrowserRouter(getApplicationContext());
Timber.plant(new Timber.DebugTree());
// to output logs of AndRouter
Router.setDebugMode(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void onClick(View v) {


private void openSecondActivity(){
Router.open("activity://second/汤二狗");
Router.open(this, "activity://second/汤二狗");
}

private void openSecondActivityWithVerticalAnim(){
Expand Down Expand Up @@ -141,7 +141,7 @@ private void openThirdActivityWithExtraValue(){

private void openThirdActivityWithExtraValueUsingAnotherRoute(){
Date date = new Date();
ActivityRoute activityRoute = (ActivityRoute) Router.getRoute("activity://third2");
ActivityRoute activityRoute = (ActivityRoute) Router.getRoute("activity://third2?text=33");
toasts("" + activityRoute
.withParams("date", date)
.open());
Expand Down

0 comments on commit d791da3

Please sign in to comment.