Skip to content

Commit

Permalink
It support use ActivityRouter directly, not Router. It support to set…
Browse files Browse the repository at this point in the history
…Falgs to Route
  • Loading branch information
beautifulSoup committed Aug 15, 2016
1 parent d692fd9 commit 7d430cd
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 35 deletions.
6 changes: 6 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 11 additions & 24 deletions router/src/main/java/cn/campusapp/router/RouterManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.content.Context;
import android.support.annotation.Nullable;

import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
Expand All @@ -28,21 +27,6 @@ public class RouterManager {

//注意这是个list是有顺序的,所以排在前面的优先级会比较高
static List<IRouter> mRouters = new LinkedList<>();
static ActivityRouter mActivityRouter = new ActivityRouter(); //Activity
static BrowserRouter mBrowserRouter = new BrowserRouter(); //浏览器


static {
try {
Constructor<?> constructor = Class.forName("cn.campusapp.router.router.AnnotatedRouterTableInitializer").getConstructor();
IActivityRouteTableInitializer initializer = (IActivityRouteTableInitializer) constructor.newInstance();
mActivityRouter.initActivityRouterTable(initializer);

} catch (Exception e) {
//do nothing
}
}


private RouterManager(){}

Expand Down Expand Up @@ -70,30 +54,33 @@ public synchronized void addRouter(IRouter router){
}

public synchronized void initBrowserRouter(Context context){
mBrowserRouter.init(context);
addRouter(mBrowserRouter);
BrowserRouter browserRouter = BrowserRouter.getInstance();
browserRouter.init(context);
addRouter(browserRouter);
}


public synchronized void initActivityRouter(Context context){
mActivityRouter.init(context);
addRouter(mActivityRouter);
ActivityRouter activityRouter = ActivityRouter.getInstance();
activityRouter.init(context);
addRouter(activityRouter);
}

public synchronized void initActivityRouter(Context context, String ... schemes){
initActivityRouter(context, null, schemes);
}

public synchronized void initActivityRouter(Context context, IActivityRouteTableInitializer initializer, String ... schemes){
ActivityRouter router = ActivityRouter.getInstance();
if(initializer == null) {
mActivityRouter.init(context);
router.init(context);
} else {
mActivityRouter.init(context, initializer);
router.init(context, initializer);
}
if(schemes != null && schemes.length > 0){
mActivityRouter.setMatchSchemes(schemes);
router.setMatchSchemes(schemes);
}
addRouter(mActivityRouter);
addRouter(router);
}

public List<IRouter> getRouters(){
Expand Down
17 changes: 17 additions & 0 deletions router/src/main/java/cn/campusapp/router/route/ActivityRoute.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class ActivityRoute extends BaseRoute {
private WeakReference<Fragment> mSupportFRef; //if you want to use fragment.startActivityForResutl
private int mRequestCode = 0; // request code to start activity for result
private WeakReference<android.app.Fragment> mFRef; //if you want to use android.app.fragment to startActivityForResult, you should set this
private int mFlags = 0;

private ActivityRoute(IRouter router, String url){
super(router, url);
Expand Down Expand Up @@ -90,6 +91,10 @@ public ActivityRoute setAnimation(Activity activity, int inAnimation, int outAni
return this;
}

public int getFlags(){
return mFlags;
}


/**
* 设置打开页面的方式为startActivity,这也是默认的打开方式
Expand Down Expand Up @@ -166,6 +171,11 @@ public ActivityRoute withParams(String key, long value){
return this;
}

public ActivityRoute withFlags(int flags){
mFlags = flags;
return this;
}


public static class Builder{
String mUrl;
Expand All @@ -178,6 +188,7 @@ public static class Builder{
private Fragment mSupportFra; //if you want to use fragment.startActivityForResutl
private int mRequestCode = 0; // request code to start activity for result
private android.app.Fragment mFra; //if you want to use android.app.fragment to startActivityForResult, you should set this
int mFlags = 0;


public Builder(IRouter router){
Expand Down Expand Up @@ -260,6 +271,11 @@ public Builder withOpenMethodStartForResult(android.app.Fragment fragment, int r
return this;
}

public Builder withFlags(int flags){
mFlags = flags;
return this;
}

/**
*
* @param extra
Expand Down Expand Up @@ -302,6 +318,7 @@ public ActivityRoute build(){
route.withOpenMethodStartForResult(mFra, mRequestCode);
}
route.setExtras(mBundle);
route.withFlags(mFlags);
return route;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

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

import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand All @@ -37,26 +38,36 @@
*/
public class ActivityRouter extends BaseRouter {
private static final String TAG = "Router";
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;

static ActivityRouter mActivityRouter = new ActivityRouter(); //Activity

public static final String KEY_URL = "key_and_activity_router_url";

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

try {
Constructor<?> constructor = Class.forName("cn.campusapp.router.router.AnnotatedRouterTableInitializer").getConstructor();
IActivityRouteTableInitializer initializer = (IActivityRouteTableInitializer) constructor.newInstance();
mActivityRouter.initActivityRouterTable(initializer);

} catch (Exception e) {
//do nothing
}
}

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

public static ActivityRouter getSharedRouter() {
return mSharedActivityRouter;
}

public static ActivityRouter getInstance(){
return mActivityRouter;
}

public void init(Context appContext, IActivityRouteTableInitializer initializer) {
mBaseContext = appContext;
Expand Down Expand Up @@ -91,7 +102,7 @@ public void initActivityRouterTable(IActivityRouteTableInitializer initializer)


@Override
public IRoute getRoute(String url) {
public ActivityRoute getRoute(String url) {
return new ActivityRoute.Builder(this)
.setUrl(url)
.build();
Expand Down Expand Up @@ -216,9 +227,10 @@ protected void open(ActivityRoute route, Context context) throws RouteNotFoundEx
throw new RouteNotFoundException(route.getUrl());
}
if (context == null) {
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | route.getFlags());
mBaseContext.startActivity(intent);
} else {
intent.setFlags(route.getFlags());
context.startActivity(intent);
}

Expand All @@ -235,6 +247,7 @@ protected void openForResult(ActivityRoute route, Activity activity, int request
if (intent == null) {
throw new RouteNotFoundException(route.getUrl());
}
intent.setFlags(route.getFlags());
if (route.getInAnimation() != -1 && route.getOutAnimation() != -1 && route.getActivity() != null) {
route.getActivity().overridePendingTransition(route.getInAnimation(), route.getOutAnimation());
}
Expand All @@ -248,6 +261,7 @@ protected void openForResult(ActivityRoute route, Fragment fragment, int request
if (intent == null) {
throw new RouteNotFoundException(route.getUrl());
}
intent.setFlags(route.getFlags());
if (route.getInAnimation() != -1 && route.getOutAnimation() != -1 && route.getActivity() != null) {
route.getActivity().overridePendingTransition(route.getInAnimation(), route.getOutAnimation());
}
Expand All @@ -261,6 +275,7 @@ protected void openForResult(ActivityRoute route, android.app.Fragment fragment,
if (intent == null) {
throw new RouteNotFoundException(route.getUrl());
}
intent.setFlags(route.getFlags());
if (route.getInAnimation() != -1 && route.getOutAnimation() != -1 && route.getActivity() != null) {
route.getActivity().overridePendingTransition(route.getInAnimation(), route.getOutAnimation());
}
Expand Down Expand Up @@ -436,5 +451,5 @@ public static String getKeyUrl(){
public Queue<HistoryItem> getRouteHistories(){
return mHistoryCaches;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,18 @@ public class BrowserRouter extends BaseRouter {

private Context mBaseContext;

static BrowserRouter mBrowserRouter = new BrowserRouter(); //浏览器


static {
SCHEMES_CAN_OPEN.add("https");
SCHEMES_CAN_OPEN.add("http");
}

public static BrowserRouter getInstance(){
return mBrowserRouter;
}

public void init(Context context){
mBaseContext = context;
}
Expand Down Expand Up @@ -55,7 +62,7 @@ protected boolean open(Context context, IRoute route){
}

@Override
public IRoute getRoute(String url) {
public BrowserRoute getRoute(String url) {
return new BrowserRoute.Builder(this)
.setUrl(url)
.build();
Expand Down
3 changes: 3 additions & 0 deletions router/src/main/java/cn/campusapp/router/router/IRouter.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,7 @@ public interface IRouter {
Class<? extends IRoute> getCanOpenRoute();





}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
import java.util.Queue;

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

@RouterMap("activity://main")
public class MainActivity extends Activity {


Expand Down Expand Up @@ -53,7 +54,7 @@ protected void onCreate(Bundle savedInstanceState) {
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openSecondActivity();
openSecondActivity();
}
});

Expand Down
17 changes: 17 additions & 0 deletions sample/src/main/java/cn/campusapp/router/sample/ThirdActivity.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
package cn.campusapp.router.sample;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Date;

import cn.campusapp.router.annotation.RouterMap;
import cn.campusapp.router.router.ActivityRouter;


@RouterMap({"activity2://third", "activity://third2"})
public class ThirdActivity extends Activity {

TextView vTimeTv;
Button vBtn;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -22,5 +27,17 @@ protected void onCreate(Bundle savedInstanceState) {
vTimeTv = (TextView) findViewById(R.id.time);
Date date = (Date) getIntent().getSerializableExtra("date");
vTimeTv.setText(new SimpleDateFormat("HH:mm").format(date));
vBtn = (Button) findViewById(R.id.set_flags_btn);
vBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ActivityRouter
.getInstance()
.getRoute("activity://main")
.withOpenMethodStart(ThirdActivity.this)
.withFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK)
.open();
}
});
}
}
8 changes: 8 additions & 0 deletions sample/src/main/res/layout/activity_third.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,12 @@
tools:text="11:11:11"
/>


<Button
android:id="@+id/set_flags_btn"
android:layout_width="150dp"
android:layout_height="40dp"
android:layout_below="@id/time"
android:text="With Flags"/>

</RelativeLayout>

0 comments on commit 7d430cd

Please sign in to comment.