项目作者: EastWoodYang

项目描述 :
Android 接口式路由 / A routing framework to assist with Android Componentization. it's turns your route API into a Java interface.
高级语言: Java
项目地址: git://github.com/EastWoodYang/AnnoRouter.git
创建时间: 2018-07-21T01:39:07Z
项目社区:https://github.com/EastWoodYang/AnnoRouter

开源协议:Other

下载


AnnoRouter

A routing framework to assist with Android Componentization. it’s turns your route API into a Java interface.

Use annotations to define route jumps.

Get it

AnnoAdapter is now available on JCentral.

  1. implementation 'com.eastwood.common:anno-router:1.0.4'

Usage

Build Global Router

  1. Router.Builder builder = new Router.Builder()
  2. .application(this)
  3. ...
  4. Router.init(builder);

Intercept filter url

  1. new Router.Builder()
  2. .routerUrlFilter(new IRouterUrlFilter() {
  3. @Override
  4. public String filter(String url) {
  5. ...
  6. return url;
  7. }
  8. })
  9. ...

Exception error handler

  1. new Router.Builder()
  2. .exceptionHandler(new IExceptionHandler() {
  3. @Override
  4. public void handler(String url, Exception e) {
  5. }
  6. })
  7. ...

Start Activity handler

  1. new Router.Builder()
  2. .activityHandler(new IActivityHandler() {
  3. @Override
  4. public void startActivity(Context context, Intent intent, int requestCode, IActivityTransition activityTransition, OnRouterResult routerResult) {
  5. }
  6. })
  7. ...

Define Route Api

use @RouterScheme, @RouterHost, @Path and @Param to define a route url.

  1. @RouterScheme("scheme")
  2. @RouterHost("host")
  3. public interface RouterApi {
  4. @Path("path")
  5. ...
  6. void jump(@Param("paramName") int paramValue);
  7. }
  8. public interface RouterApi {
  9. @RouterScheme("scheme")
  10. @RouterHost("host")
  11. @Path("path")
  12. ...
  13. void jump(@Param("paramName") int paramValue);
  14. }

Sometimes, you may define the same scheme://host/path but different params. then, need to use @Strict to distinguish.

e.g.

  1. @RouterScheme("scheme")
  2. @RouterHost("host")
  3. public interface RouterApi {
  4. @Path("path")
  5. ...
  6. void jumpToActivity1(@Param("param1") String param1);
  7. @Strict
  8. @Path("path")
  9. ...
  10. void jumpToActivity2(@Param("param1") String param1, @Param("param2") int param2);
  11. }
  • scheme://host/path?param1=a will match method jumpToActivity1
  • scheme://host/path?param1=a¶m2=1 will match method jumpToActivity2
  • scheme://host/path?param1=a¶m2=1¶m3=1 will match method jumpToActivity1, and param2=1¶m3=1 will be ignore.

Do some verification or preparation tasks

  1. public interface RouterApi {
  2. @Task(CustomRouterTask.class)
  3. ...
  4. void jumpToActivity();
  5. }
  6. // ----------------
  7. public class CustomRouterTask implements IRouterTask {
  8. @Override
  9. public void execute(Context context, RouterInfo routerInfo, OnTaskResult onTaskResult) {
  10. // do something...
  11. onTaskResult.success();
  12. }
  13. }

Jump to a Activity or handle custom

  1. public interface RouterApi {
  2. ...
  3. @Activity(LoginActivity.class)
  4. void jumpToLogin();
  5. ...
  6. @RouterHandler(CustomRouterHandler.class)
  7. void jumpToLogin();
  8. }
  9. // ----------------
  10. public class CustomRouterHandler implements IRouterHandler {
  11. @Override
  12. public void applyRouter(Context context, RouterInfo routerInfo, OnRouterResult routerResult) {
  13. // do what you want to do.
  14. if(routerResult != null) {
  15. routerResult.onSuccess();
  16. }
  17. }
  18. }

Custom Activity transition animation

  1. public interface RouterApi {
  2. ...
  3. @Transition(CustomeTransition.class)
  4. void jumpToLogin();
  5. }
  6. // ----------------
  7. public class CustomeTransition implements IActivityTransition {
  8. @Override
  9. public int enterAnim() {
  10. return R.anim.fade_in;
  11. }
  12. @Override
  13. public int exitAnim() {
  14. return R.anim.fade_out;
  15. }
  16. }

Set Activity launchMode

  1. public interface RouterApi {
  2. ...
  3. @Flags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
  4. void jump(@Flags int flags);
  5. ...
  6. void jump(@Flags int flags);
  7. }
  8. // ----------------
  9. Router.create(RouterApi.class).jump(Intent.FLAG_ACTIVITY_CLEAR_TOP);

Add Router Api

  1. Router.addRouterIndex(RouterApi.class);

Add custom scheme handler

  1. public class HttpSchemeHandler implements ISchemeHandler {
  2. @Override
  3. public void applyRouter(Context context, String url, OnRouterResult routerResult) {
  4. Intent intent = new Intent();
  5. intent.setAction("android.intent.action.VIEW");
  6. Uri content_url = Uri.parse(url);
  7. intent.setData(content_url);
  8. context.startActivity(intent);
  9. if(routerResult != null) {
  10. routerResult.onSuccess();
  11. }
  12. }
  13. }
  14. // ----------------
  15. HttpSchemeHandler httpSchemeHandler = new HttpSchemeHandler();
  16. Router.addSchemeHandler("https", httpSchemeHandler);
  17. Router.addSchemeHandler("http", httpSchemeHandler);

Use Router Api To Jump

  1. // The Router class generates an implementation of the RouterApi interface.
  2. RouterApi routerApi = Router.create(RouterApi .class);
  3. routerApi.jump("value");
  4. // or use url instead.
  5. Router.execute("scheme://host/path?param=value");

Get Activity result

  1. @RouterScheme("app")
  2. @RouterHost("usercenter")
  3. public interface LoginRouterApi {
  4. @Path("login")
  5. @Activity(LoginActivity.class)
  6. @RequestCode(1001)
  7. void jumpToLogin(@Param("mobile") String mobile);
  8. @Activity(LoginActivity.class)
  9. @RequestCode(1001)
  10. void jumpToLogin(@Param("mobile") String mobile, OnActivityResult onActivityResult);
  11. }
  12. // ----------------
  13. OnActivityResult onActivityResult = new OnActivityResult() {
  14. @Override
  15. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  16. }
  17. @Override
  18. public void onSuccess() {
  19. }
  20. @Override
  21. public void onFailure(Exception e) {
  22. }
  23. };
  24. Router.execute("app://usercenter/login?mobile=0123456789", onActivityResult);
  25. // or
  26. Router.create(LoginRouterApi.class).jumpToLogin("0123456789", onActivityResult);

#more detail see demo.

Question or Idea

有问题或想法可以直接加我微信: EastWoodYang

License

  1. Copyright 2018 EastWood Yang
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.