Skip to content

Commit

Permalink
提供接口查询跳转历史
Browse files Browse the repository at this point in the history
  • Loading branch information
beautifulSoup committed May 13, 2016
1 parent 04b62c9 commit d692fd9
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 7 deletions.
1 change: 1 addition & 0 deletions router/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ dependencies {
androidTestCompile 'com.android.support.test:runner:0.4'
// Set this dependency to use JUnit 4 rules
androidTestCompile 'com.android.support.test:rules:0.4'
compile 'org.apache.commons:commons-collections4:4.0'
// Set this dependency to build and run Espresso tests
// Optional -- UI testing with Espresso
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
Expand Down
8 changes: 8 additions & 0 deletions router/src/main/java/cn/campusapp/router/Router.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import android.content.Context;

import java.util.Queue;

import cn.campusapp.router.route.IRoute;
import cn.campusapp.router.router.ActivityRouter;
import cn.campusapp.router.router.BrowserRouter;
import cn.campusapp.router.router.HistoryItem;
import cn.campusapp.router.router.IActivityRouteTableInitializer;
import cn.campusapp.router.router.IRouter;
import timber.log.Timber;
Expand Down Expand Up @@ -90,4 +93,9 @@ public static void setBrowserRouter(BrowserRouter router){
RouterManager.getSingleton().setBrowserRouter(router);
}

public static Queue<HistoryItem> getActivityChangedHistories(){
return RouterManager.getSingleton().getActivityChangedHistories();
}


}
13 changes: 13 additions & 0 deletions router/src/main/java/cn/campusapp/router/RouterManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import cn.campusapp.router.route.IRoute;
import cn.campusapp.router.router.ActivityRouter;
import cn.campusapp.router.router.BrowserRouter;
import cn.campusapp.router.router.HistoryItem;
import cn.campusapp.router.router.IActivityRouteTableInitializer;
import cn.campusapp.router.router.IRouter;
import timber.log.Timber;
Expand Down Expand Up @@ -157,5 +159,16 @@ public void setBrowserRouter(BrowserRouter router){
}


public Queue<HistoryItem> getActivityChangedHistories(){
ActivityRouter aRouter = null;
for(IRouter router : mRouters){
if(router instanceof ActivityRouter){
aRouter = (ActivityRouter) router;
break;
}
}
return aRouter != null ? aRouter.getRouteHistories() : null;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
import android.text.TextUtils;
import android.util.Log;

import org.apache.commons.collections4.queue.CircularFifoQueue;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Queue;

import cn.campusapp.router.BuildConfig;
import cn.campusapp.router.exception.InvalidRoutePathException;
Expand All @@ -37,16 +40,18 @@ public class ActivityRouter extends BaseRouter {
static ActivityRouter mSharedActivityRouter = new ActivityRouter();
private static List<String> MATCH_SCHEMES = new ArrayList<>();
private static final String DEFAULT_SCHEME = "activity";
private static final int HISTORY_CACHE_SIZE = 20;

public static final String KEY_URL = "key_and_activity_router_url";

static {
CAN_OPEN_ROUTE = ActivityRoute.class;
MATCH_SCHEMES.add("activity");
MATCH_SCHEMES.add(DEFAULT_SCHEME);
}

Context mBaseContext;
Map<String, Class<? extends Activity>> mRouteTable = new HashMap<>();
CircularFifoQueue<HistoryItem> mHistoryCaches = new CircularFifoQueue<>(HISTORY_CACHE_SIZE);

public static ActivityRouter getSharedRouter() {
return mSharedActivityRouter;
Expand Down Expand Up @@ -205,11 +210,11 @@ public boolean open(Context context, String url) {


protected void open(ActivityRoute route, Context context) throws RouteNotFoundException {
Intent intent = match(route);
Class<?> fromClazz = context != null ? context.getClass() : mBaseContext.getClass();
Intent intent = match(fromClazz, route);
if (intent == null) {
throw new RouteNotFoundException(route.getUrl());
}

if (context == null) {
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mBaseContext.startActivity(intent);
Expand All @@ -226,7 +231,7 @@ protected void open(ActivityRoute route, Context context) throws RouteNotFoundEx
protected void openForResult(ActivityRoute route, Activity activity, int requestCode) throws RouteNotFoundException {


Intent intent = match(route);
Intent intent = match(activity.getClass(), route);
if (intent == null) {
throw new RouteNotFoundException(route.getUrl());
}
Expand All @@ -239,7 +244,7 @@ protected void openForResult(ActivityRoute route, Activity activity, int request

protected void openForResult(ActivityRoute route, Fragment fragment, int requestCode) throws RouteNotFoundException {

Intent intent = match(route);
Intent intent = match(fragment.getClass(), route);
if (intent == null) {
throw new RouteNotFoundException(route.getUrl());
}
Expand All @@ -252,7 +257,7 @@ protected void openForResult(ActivityRoute route, Fragment fragment, int request

protected void openForResult(ActivityRoute route, android.app.Fragment fragment, int requestCode) throws RouteNotFoundException {

Intent intent = match(route);
Intent intent = match(fragment.getClass(), route);
if (intent == null) {
throw new RouteNotFoundException(route.getUrl());
}
Expand Down Expand Up @@ -407,13 +412,14 @@ private Intent setExtras(Bundle bundle, Intent intent) {
}

@Nullable
private Intent match(ActivityRoute route) {
private Intent match(Class<?> from, ActivityRoute route) {
String matchedRoute = findMatchedRoute(route);
if (matchedRoute == null) {
return null;
}
Class<? extends Activity> matchedActivity = mRouteTable.get(matchedRoute);
Intent intent = new Intent(mBaseContext, matchedActivity);
mHistoryCaches.add(new HistoryItem(from, matchedActivity));
//find the key value in the path
intent = setKeyValueInThePath(matchedRoute, route.getUrl(), intent);
intent = setOptionParams(route.getUrl(), intent);
Expand All @@ -426,4 +432,9 @@ private Intent match(ActivityRoute route) {
public static String getKeyUrl(){
return KEY_URL;
}

public Queue<HistoryItem> getRouteHistories(){
return mHistoryCaches;
}

}
24 changes: 24 additions & 0 deletions router/src/main/java/cn/campusapp/router/router/HistoryItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cn.campusapp.router.router;

/**
* Created by kris on 16/5/13.
*/
public class HistoryItem {

Class<?> from;

Class<?> to;

public HistoryItem(Class<?> from , Class<?> to){
this.from = from;
this.to = to;
}

public Class<?> getFrom(){
return from;
}

public Class<?> getTo(){
return to;
}
}
1 change: 1 addition & 0 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ dependencies {
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.0'
compile 'com.android.support:design:23.2.0'
compile 'com.google.code.gson:gson:2.6.2'
compile project(':router')
apt project(':compiler')
}
11 changes: 11 additions & 0 deletions sample/src/main/java/cn/campusapp/router/sample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
import android.widget.Toast;

import java.util.Date;
import java.util.Queue;

import cn.campusapp.router.Router;
import cn.campusapp.router.route.ActivityRoute;
import cn.campusapp.router.router.HistoryItem;
import timber.log.Timber;

public class MainActivity extends Activity {

Expand Down Expand Up @@ -104,6 +107,14 @@ public void onClick(View v) {
});
}

@Override
protected void onResume() {
super.onResume();
Queue<HistoryItem> historyItems = Router.getActivityChangedHistories();
for(HistoryItem item : historyItems){
Timber.i("%s %s", item.getFrom().toString(), item.getTo().toString());
}
}

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

0 comments on commit d692fd9

Please sign in to comment.