57 | */
58 | public static final String RAW_URI = "_ROUTER_RAW_URI_KEY_";
59 | public static boolean DEBUG = false;
60 |
61 | private Uri uri;
62 | private InternalCallback internalCallback;
63 |
64 | private Router(Uri uri) {
65 | this.uri = uri;
66 | internalCallback = new InternalCallback(this.uri);
67 | }
68 |
69 | /**
70 | * Create Router by url string
71 | * @param url The url to create Router
72 | * @return new Router
73 | */
74 | public static Router create(String url) {
75 | return new Router(Uri.parse(url == null?"":url));
76 | }
77 |
78 | /**
79 | * Create Router by uri
80 | * @param uri the uri to create Router
81 | * @return new Router
82 | */
83 | public static Router create(Uri uri) {
84 | return new Router(uri);
85 | }
86 |
87 | public static InstanceRouter createInstanceRouter(String url) {
88 | return InstanceRouter.build(url);
89 | }
90 |
91 | /**
92 | * Set a callback to notify the user when the routing were success or failure.
93 | * @param callback The callback you set.
94 | * @return Router itself
95 | */
96 | public Router setCallback (RouteCallback callback) {
97 | this.internalCallback.setCallback(callback);
98 | return this;
99 | }
100 |
101 | public Router addInterceptor(RouteInterceptor interceptor) {
102 | this.internalCallback.getExtras().addInterceptor(interceptor);
103 | return this;
104 | }
105 |
106 | public Router requestCode(int requestCode) {
107 | this.internalCallback.getExtras().setRequestCode(requestCode);
108 | return this;
109 | }
110 |
111 | public Router resultCallback(ActivityResultCallback callback) {
112 | this.internalCallback.getExtras().putValue(Constants.KEY_RESULT_CALLBACK, callback);
113 | return this;
114 | }
115 |
116 | public Router addFlags(int flag) {
117 | this.internalCallback.getExtras().addFlags(flag);
118 | return this;
119 | }
120 |
121 | public Router setAnim(int enterAnim, int exitAnim) {
122 | this.internalCallback.getExtras().setInAnimation(enterAnim);
123 | this.internalCallback.getExtras().setOutAnimation(exitAnim);
124 | return this;
125 | }
126 |
127 | public Router setOptions(Bundle options) {
128 | this.internalCallback.getExtras().putValue(Constants.KEY_ACTIVITY_OPTIONS, options);
129 | return this;
130 | }
131 |
132 | public Router addExtras(Bundle extras) {
133 | this.internalCallback.getExtras().addExtras(extras);
134 | return this;
135 | }
136 |
137 | public Router setExecutor(Executor executor) {
138 | this.internalCallback.getExtras().putValue(Constants.KEY_ACTION_EXECUTOR, executor);
139 | return this;
140 | }
141 |
142 | /**
143 | * Restore a Routing event from last uri and extras.
144 | * @param uri last uri
145 | * @param extras last extras
146 | * @return The restored routing find by {@link Router#getRoute()}
147 | */
148 | public static IRoute resume(Uri uri, RouteBundleExtras extras) {
149 | IRoute route = Router.create(uri).getRoute();
150 | if (route instanceof BaseRoute) {
151 | ((BaseRoute) route).replaceExtras(extras);
152 | }
153 | return route;
154 | }
155 |
156 | /**
157 | * launch routing task.
158 | * @param context context to launched
159 | */
160 | public void open(Context context) {
161 | getRoute().open(context);
162 | }
163 |
164 |
165 | /**
166 | * Get route by uri, you should get a route by this way and set some extras data before open
167 | * @return
168 | * An IRoute object.it will be {@link BrowserRoute}, {@link ActivityRoute} or {@link ActionRoute}.
169 | * and it also will be {@link IRoute.EmptyRoute} if it not found
170 | */
171 | public IRoute getRoute () {
172 | IRoute route = getLocalRoute();
173 | if (!(route instanceof IRoute.EmptyRoute)) {
174 | return route;
175 | }
176 | route = HostServiceWrapper.create(uri, internalCallback);
177 | if (route instanceof IRoute.EmptyRoute) {
178 | notifyNotFound(String.format("find Route by %s failed:",uri));
179 | }
180 | return route;
181 | }
182 |
183 | private IRoute getLocalRoute() {
184 | try {
185 | RouteRule rule;
186 | if (!Utils.isValidUri(uri)) {
187 | return new IRoute.EmptyRoute(internalCallback);
188 | } else if ((rule = ActionRoute.findRule(uri, Cache.TYPE_ACTION_ROUTE)) != null) {
189 | return new ActionRoute().create(uri, rule, new Bundle(), internalCallback);
190 | } else if ((rule = ActivityRoute.findRule(uri, Cache.TYPE_ACTIVITY_ROUTE)) != null) {
191 | return new ActivityRoute().create(uri, rule, new Bundle(), internalCallback);
192 | } else if (BrowserRoute.canOpenRouter(uri)) {
193 | return BrowserRoute.getInstance().setUri(uri);
194 | } else {
195 | return new IRoute.EmptyRoute(internalCallback);
196 | }
197 | } catch (Exception e) {
198 | internalCallback.onOpenFailed(e);
199 | return new IRoute.EmptyRoute(internalCallback);
200 | }
201 | }
202 |
203 | /**
204 | *
205 | * Get {@link IBaseRoute} by uri, it could be one of {@link IActivityRoute} or {@link IActionRoute}.
206 | * and you can add some {@link android.os.Bundle} data and {@link RouteInterceptor} into it.
207 | *
208 | * @return returns an {@link IBaseRoute} finds by uri or {@link IBaseRoute.EmptyBaseRoute} for not found
209 | */
210 | public IBaseRoute getBaseRoute() {
211 | IRoute route = getRoute();
212 | if (route instanceof IBaseRoute) {
213 | return (IBaseRoute) route;
214 | }
215 |
216 | notifyNotFound(String.format("find BaseRoute by %s failed, but is %s",uri, route.getClass().getSimpleName()));
217 | return new IBaseRoute.EmptyBaseRoute(internalCallback);
218 | }
219 |
220 | /**
221 | * Get {@link IActivityRoute} by uri,you should get a route by this way and set some extras data before open
222 | * @return returns an {@link IActivityRoute} finds by uri or {@link IActivityRoute.EmptyActivityRoute} for not found.
223 | */
224 | public IActivityRoute getActivityRoute() {
225 | IRoute route = getRoute();
226 | if (route instanceof IActivityRoute) {
227 | return (IActivityRoute) route;
228 | }
229 |
230 | // return an empty route to avoid NullPointException
231 | notifyNotFound(String.format("find ActivityRoute by %s failed, but is %s",uri, route.getClass().getSimpleName()));
232 | return new IActivityRoute.EmptyActivityRoute(internalCallback);
233 | }
234 |
235 | /**
236 | * Get {@link IActionRoute} by uri,you should get a route by this way and set some extras data before open
237 | * @return returns an {@link IActionRoute} finds by uri or {@link IActionRoute.EmptyActionRoute} for not found.
238 | */
239 | public IActionRoute getActionRoute() {
240 | IRoute route = getRoute();
241 | if (route instanceof IActionRoute) {
242 | return (IActionRoute) route;
243 | }
244 |
245 | notifyNotFound(String.format("find ActionRoute by %s failed, but is %s",uri, route.getClass().getSimpleName()));
246 | // return a empty route to avoid NullPointException
247 | return new IActionRoute.EmptyActionRoute(internalCallback);
248 | }
249 |
250 | private void notifyNotFound(String msg) {
251 | internalCallback.onOpenFailed(new NotFoundException(msg));
252 | }
253 | }
254 |
--------------------------------------------------------------------------------
/router-api/src/main/java/com/lzh/nonview/router/RouterConfiguration.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2017 Haoge
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.lzh.nonview.router;
17 |
18 | import android.app.Activity;
19 | import android.content.Context;
20 | import android.content.Intent;
21 | import android.net.Uri;
22 |
23 | import com.lzh.nonview.router.activityresult.ActivityResultDispatcher;
24 | import com.lzh.nonview.router.extras.RouteBundleExtras;
25 | import com.lzh.nonview.router.interceptors.RouteInterceptor;
26 | import com.lzh.nonview.router.launcher.ActionLauncher;
27 | import com.lzh.nonview.router.launcher.ActivityLauncher;
28 | import com.lzh.nonview.router.launcher.DefaultActionLauncher;
29 | import com.lzh.nonview.router.launcher.DefaultActivityLauncher;
30 | import com.lzh.nonview.router.module.RouteCreator;
31 | import com.lzh.nonview.router.protocol.HostServiceWrapper;
32 | import com.lzh.nonview.router.protocol.IRemoteFactory;
33 | import com.lzh.nonview.router.route.ActionRoute;
34 | import com.lzh.nonview.router.route.ActivityRoute;
35 | import com.lzh.nonview.router.route.InternalCallback;
36 | import com.lzh.nonview.router.route.RouteCallback;
37 | import com.lzh.nonview.router.tools.Cache;
38 |
39 | import java.util.concurrent.Executor;
40 |
41 | /**
42 | * Entrance class to store router configurations.
43 | */
44 | public final class RouterConfiguration {
45 |
46 | private RouteInterceptor interceptor;
47 | private RouteCallback callback;
48 |
49 | private IRemoteFactory remoteFactory = null;
50 | private Class extends ActivityLauncher> activityLauncher;
51 | private Class extends ActionLauncher> actionLauncher;
52 |
53 |
54 | public RouteInterceptor getInterceptor() {
55 | return interceptor;
56 | }
57 |
58 | /**
59 | * Set a default routing interceptor to used. it will be called by all the routes.
60 | * @param interceptor the default interceptor
61 | * @return config itself
62 | * @see RouteInterceptor
63 | */
64 | public RouterConfiguration setInterceptor(RouteInterceptor interceptor) {
65 | this.interceptor = interceptor;
66 | return this;
67 | }
68 |
69 | public RouteCallback getCallback() {
70 | return callback;
71 | }
72 |
73 | /**
74 | * Set a default routing callback to used. it will be called by all the routes.
75 | * @param callback The default callback
76 | * @return config itself
77 | * @see RouteCallback
78 | */
79 | public RouterConfiguration setCallback(RouteCallback callback) {
80 | this.callback = callback;
81 | return this;
82 | }
83 |
84 | public IRemoteFactory getRemoteFactory() {
85 | return remoteFactory;
86 | }
87 |
88 | /**
89 | * Set a default remote factory to used. the factory must contains a default empty construction.
90 | * @param remoteFactory The remote factory class
91 | * @return config itself
92 | * @see IRemoteFactory
93 | */
94 | public RouterConfiguration setRemoteFactory(IRemoteFactory remoteFactory) {
95 | this.remoteFactory = remoteFactory;
96 | return this;
97 | }
98 |
99 | public Class extends ActivityLauncher> getActivityLauncher() {
100 | return activityLauncher == null ? DefaultActivityLauncher.class : activityLauncher;
101 | }
102 |
103 | /**
104 | * Set a default activity launcher to used.
105 | * @param activityLauncher The launcher class for {@link ActivityRoute}
106 | * @return config itself
107 | * @see ActivityLauncher
108 | */
109 | public RouterConfiguration setActivityLauncher(Class extends ActivityLauncher> activityLauncher) {
110 | this.activityLauncher = activityLauncher;
111 | return this;
112 | }
113 |
114 | public Class extends ActionLauncher> getActionLauncher() {
115 | return actionLauncher == null ? DefaultActionLauncher.class : actionLauncher;
116 | }
117 |
118 | /**
119 | * Set a default action launcher to used.
120 | * @param actionLauncher The launcher class for {@link ActionRoute}
121 | * @return config itself
122 | * @see ActionLauncher
123 | */
124 | public RouterConfiguration setActionLauncher(Class extends ActionLauncher> actionLauncher) {
125 | this.actionLauncher = actionLauncher;
126 | return this;
127 | }
128 |
129 | /**
130 | * Add a route rule creator and register it for remote service if is launched.
131 | * @param creator Route rules creator.can't be null
132 | */
133 | public void addRouteCreator(RouteCreator creator) {
134 | Cache.addCreator(creator);
135 | HostServiceWrapper.registerRulesToHostService();
136 | }
137 |
138 | /**
139 | * To register an executor that
140 | * @param key The class of Executor
141 | * @param value The Executor instance associate with the key.
142 | * @see Cache#registerExecutors(Class, Executor)
143 | */
144 | public void registerExecutors(Class extends Executor> key, Executor value) {
145 | Cache.registerExecutors(key, value);
146 | }
147 |
148 | /**
149 | * @see RouterConfiguration#startHostService(String, Context, String)
150 | * @param hostPackage The package name of host. it to launch a remote service of host.
151 | * @param context The valid context
152 | */
153 | public void startHostService(String hostPackage, Context context) {
154 | startHostService(hostPackage, context, null);
155 | }
156 |
157 | /**
158 | * start a remote host service
159 | * @param hostPackage The package name of host. it to launch a remote service of host.
160 | * @param context The valid context
161 | * @param pluginName The unique identifier plugin name. or null to use the plugin-package name for it.
162 | */
163 | public void startHostService(String hostPackage, Context context, String pluginName) {
164 | HostServiceWrapper.startHostService(hostPackage, context, pluginName);
165 | }
166 |
167 | /**
168 | * Check if the specified plug-in names have been registered to the remote service.
169 | * @param pluginName The specified plug-in names
170 | * @return True if it has been registered.
171 | */
172 | public boolean isRegister(String pluginName) {
173 | return HostServiceWrapper.isRegister(pluginName);
174 | }
175 |
176 | /**
177 | * Restore a {@link RouteBundleExtras} by uri. this method should only be called in lifecycle of {@link RouteCallback}.
178 | * otherwise it will be null cause it is cleaned.
179 | * @param uri The uri that you open
180 | * @return The {@link RouteBundleExtras} instance that you may set before before you open the routing by uri.
181 | */
182 | public RouteBundleExtras restoreExtras(Uri uri) {
183 | return InternalCallback.findExtrasByUri(uri);
184 | }
185 |
186 | public Context restorContext(Uri uri) {
187 | return InternalCallback.findContextByUri(uri);
188 | }
189 |
190 | public boolean dispatchActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
191 | return ActivityResultDispatcher.get().dispatchActivityResult(activity, requestCode, resultCode, data);
192 | }
193 |
194 | private static RouterConfiguration config = new RouterConfiguration();
195 | private RouterConfiguration() {}
196 | public static RouterConfiguration get() {
197 | return config;
198 | }
199 | }
200 |
--------------------------------------------------------------------------------
/router-api/src/main/java/com/lzh/nonview/router/activityresult/ActivityResultCallback.java:
--------------------------------------------------------------------------------
1 | package com.lzh.nonview.router.activityresult;
2 |
3 | import android.content.Intent;
4 |
5 | public interface ActivityResultCallback {
6 | void onResult(int resultCode, Intent data);
7 | }
8 |
--------------------------------------------------------------------------------
/router-api/src/main/java/com/lzh/nonview/router/activityresult/ActivityResultDispatcher.java:
--------------------------------------------------------------------------------
1 | package com.lzh.nonview.router.activityresult;
2 |
3 | import android.app.Activity;
4 | import android.content.Intent;
5 |
6 | import com.lzh.nonview.router.tools.Utils;
7 |
8 | import java.util.ArrayList;
9 | import java.util.HashMap;
10 | import java.util.Iterator;
11 | import java.util.List;
12 | import java.util.Map;
13 | import java.util.Set;
14 |
15 | public final class ActivityResultDispatcher {
16 |
17 | private Map> container = new HashMap<>();
18 |
19 | private static ActivityResultDispatcher dispatcher = new ActivityResultDispatcher();
20 | private ActivityResultDispatcher(){ }
21 | public static ActivityResultDispatcher get() {
22 | return dispatcher;
23 | }
24 |
25 | public void bindRequestArgs(Activity activity, int requestCode, ActivityResultCallback callback) {
26 | if (!Utils.isValid(activity)
27 | || callback == null
28 | || requestCode == -1) {
29 | return;
30 | }
31 |
32 | List list = findListByKey(activity);
33 | list.add(new RequestArgs(requestCode, callback));
34 | }
35 |
36 | public boolean dispatchActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
37 | if (!container.containsKey(activity)) {
38 | return false;
39 | }
40 |
41 | boolean handle = false;
42 | List list = findListByKey(activity);
43 | for (RequestArgs args:list) {
44 | if (args.requestCode == requestCode) {
45 | args.callback.onResult(resultCode, data);
46 | list.remove(args);
47 | handle = true;
48 | break;
49 | }
50 | }
51 |
52 | releaseInvalidItems();
53 | return handle;
54 | }
55 |
56 | // 移除无效的条目:比如当前activity在后台时被回收了
57 | private void releaseInvalidItems() {
58 | Set keys = container.keySet();
59 | Iterator iterator = keys.iterator();
60 | while (iterator.hasNext()) {
61 | Activity next = iterator.next();
62 | if (!Utils.isValid(next)
63 | || container.get(next).isEmpty()) {
64 | iterator.remove();
65 | }
66 | }
67 | }
68 |
69 | private List findListByKey(Activity activity) {
70 | List list = container.get(activity);
71 | if (list == null) {
72 | list = new ArrayList<>();
73 | container.put(activity, list);
74 | }
75 | return list;
76 | }
77 |
78 | private static class RequestArgs {
79 | int requestCode;
80 | ActivityResultCallback callback;
81 |
82 | public RequestArgs(int requestCode, ActivityResultCallback callback) {
83 | this.requestCode = requestCode;
84 | this.callback = callback;
85 | }
86 | }
87 | }
--------------------------------------------------------------------------------
/router-api/src/main/java/com/lzh/nonview/router/exception/InterceptorException.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2017 Haoge
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.lzh.nonview.router.exception;
17 |
18 | import com.lzh.nonview.router.interceptors.RouteInterceptor;
19 |
20 | /**
21 | * Throw this exception when routing events were intercepted。
22 | * @author haoge
23 | */
24 | public class InterceptorException extends RuntimeException {
25 |
26 | private final RouteInterceptor interceptor;
27 | public InterceptorException(RouteInterceptor interceptor) {
28 | super("This route action has been intercepted");
29 | this.interceptor = interceptor;
30 | }
31 |
32 | /**
33 | * Provide users with access to the interceptor
34 | * @return The interceptor who intercept the event
35 | */
36 | public RouteInterceptor getInterceptor() {
37 | return interceptor;
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/router-api/src/main/java/com/lzh/nonview/router/exception/NotFoundException.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2017 Haoge
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.lzh.nonview.router.exception;
17 |
18 | /**
19 | * The custom exception represents not found
20 | * @author haoge
21 | */
22 | public class NotFoundException extends RuntimeException {
23 |
24 | public NotFoundException(String detailMessage) {
25 | super(detailMessage);
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/router-api/src/main/java/com/lzh/nonview/router/executors/MainThreadExecutor.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2017 Haoge
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.lzh.nonview.router.executors;
17 |
18 | import android.os.Handler;
19 | import android.os.Looper;
20 |
21 | import java.util.concurrent.Executor;
22 |
23 | public class MainThreadExecutor implements Executor{
24 |
25 | private Handler mainHandler = new Handler(Looper.getMainLooper());
26 |
27 | @Override
28 | public void execute(Runnable command) {
29 |
30 | if (Looper.myLooper() == Looper.getMainLooper()) {
31 | command.run();
32 | } else {
33 | mainHandler.post(command);
34 | }
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/router-api/src/main/java/com/lzh/nonview/router/extras/RouteBundleExtras.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2017 Haoge
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.lzh.nonview.router.extras;
17 |
18 | import android.os.Bundle;
19 | import android.os.Parcel;
20 | import android.os.Parcelable;
21 | import android.text.TextUtils;
22 |
23 | import com.lzh.nonview.router.interceptors.RouteInterceptor;
24 | import com.lzh.nonview.router.route.IBaseRoute;
25 | import com.lzh.nonview.router.route.RouteCallback;
26 | import com.lzh.nonview.router.tools.CacheStore;
27 |
28 | import java.util.ArrayList;
29 | import java.util.HashMap;
30 | import java.util.List;
31 | import java.util.Map;
32 |
33 | /**
34 | * An extra container contains {@link RouteBundleExtras#extras} and {@link RouteBundleExtras#interceptors}
35 | *
36 | * extras: the extra bundle data set by {@link IBaseRoute#addExtras(Bundle)}
37 | * interceptors: the extra RouteInterceptor set by {@link IBaseRoute#addInterceptor(RouteInterceptor)}
38 | *
39 | *
40 | * @author haoge
41 | * @see com.lzh.nonview.router.route.IBaseRoute
42 | */
43 | public final class RouteBundleExtras implements Parcelable{
44 |
45 | private ArrayList interceptors = new ArrayList<>();
46 | private RouteCallback callback;
47 | private Map additionalMap = new HashMap<>();
48 |
49 | private Bundle extras = new Bundle();
50 | // the extras belows is only supports for ActivityRoute.
51 | private int requestCode = -1;
52 | private int inAnimation = -1;
53 | private int outAnimation = -1;
54 | private int flags = 0;
55 |
56 | public RouteBundleExtras() {}
57 |
58 | public void addInterceptor(RouteInterceptor interceptor) {
59 | if (interceptor != null && !getInterceptors().contains(interceptor)) {
60 | getInterceptors().add(interceptor);
61 | }
62 | }
63 |
64 | public void removeInterceptor(RouteInterceptor interceptor) {
65 | if (interceptor != null) {
66 | getInterceptors().remove(interceptor);
67 | }
68 | }
69 |
70 | public void removeAllInterceptors() {
71 | getInterceptors().clear();
72 | }
73 |
74 | public List getInterceptors() {
75 | return interceptors;
76 | }
77 |
78 | public void setCallback(RouteCallback callback) {
79 | this.callback = callback;
80 | }
81 |
82 | public RouteCallback getCallback() {
83 | return callback;
84 | }
85 |
86 | public int getRequestCode() {
87 | return requestCode;
88 | }
89 |
90 | public void setRequestCode(int requestCode) {
91 | this.requestCode = requestCode;
92 | }
93 |
94 | public int getInAnimation() {
95 | return inAnimation;
96 | }
97 |
98 | public void setInAnimation(int inAnimation) {
99 | this.inAnimation = inAnimation;
100 | }
101 |
102 | public int getOutAnimation() {
103 | return outAnimation;
104 | }
105 |
106 | public void setOutAnimation(int outAnimation) {
107 | this.outAnimation = outAnimation;
108 | }
109 |
110 | public int getFlags() {
111 | return flags;
112 | }
113 |
114 | public void addFlags(int flags) {
115 | this.flags |= flags;
116 | }
117 |
118 | public T getValue(String key) {
119 | if (TextUtils.isEmpty(key)) {
120 | return null;
121 | }
122 |
123 | try {
124 | //noinspection unchecked
125 | return (T) additionalMap.get(key);
126 | } catch (ClassCastException cast) {
127 | return null;
128 | }
129 | }
130 |
131 | public void putValue(String key, Object value) {
132 | if (TextUtils.isEmpty(key) || value == null) {
133 | return;
134 | }
135 |
136 | additionalMap.put(key, value);
137 | }
138 |
139 | // ------------------------- divider for parcelable ------------------------
140 | private RouteBundleExtras(Parcel in) {
141 | requestCode = in.readInt();
142 | inAnimation = in.readInt();
143 | outAnimation = in.readInt();
144 | flags = in.readInt();
145 | extras = in.readBundle(getClass().getClassLoader());
146 |
147 | ArrayList interceptors = CacheStore.get().get(in.readInt());
148 | RouteCallback callback = CacheStore.get().get(in.readInt());
149 | Map additionalMap = CacheStore.get().get(in.readInt());
150 |
151 | if (interceptors != null) this.interceptors = interceptors;
152 | if (callback != null) this.callback = callback;
153 | if (additionalMap != null) this.additionalMap = additionalMap;
154 | }
155 |
156 | public static final Creator CREATOR = new Creator() {
157 | @Override
158 | public RouteBundleExtras createFromParcel(Parcel in) {
159 | return new RouteBundleExtras(in);
160 | }
161 |
162 | @Override
163 | public RouteBundleExtras[] newArray(int size) {
164 | return new RouteBundleExtras[size];
165 | }
166 | };
167 |
168 | @Override
169 | public int describeContents() {
170 | return 0;
171 | }
172 |
173 | @Override
174 | public void writeToParcel(Parcel dest, int flags) {
175 | dest.writeInt(requestCode);
176 | dest.writeInt(inAnimation);
177 | dest.writeInt(outAnimation);
178 | dest.writeInt(this.flags);
179 |
180 | dest.writeBundle(extras);
181 |
182 | dest.writeInt(CacheStore.get().put(interceptors));
183 | dest.writeInt(CacheStore.get().put(callback));
184 | dest.writeInt(CacheStore.get().put(additionalMap));
185 | }
186 |
187 | public Bundle getExtras() {
188 | return extras;
189 | }
190 |
191 | public void addExtras(Bundle extras) {
192 | if (extras != null) {
193 | this.extras.putAll(extras);
194 | }
195 | }
196 | }
197 |
--------------------------------------------------------------------------------
/router-api/src/main/java/com/lzh/nonview/router/interceptors/RouteInterceptor.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2017 Haoge
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.lzh.nonview.router.interceptors;
17 |
18 | import android.content.Context;
19 | import android.net.Uri;
20 |
21 | import com.lzh.nonview.router.Router;
22 | import com.lzh.nonview.router.extras.RouteBundleExtras;
23 |
24 | /**
25 | * An interceptor interface
26 | * @author haoge
27 | */
28 | public interface RouteInterceptor{
29 |
30 | /**
31 | * Whether or not be interrupted when open activity by uri
32 | * @param uri uri the uri to open
33 | * @param context context
34 | * @param extras some extras data for route,
35 | * sometimes is null when you not use
36 | * {@link Router#getBaseRoute()} to set some extras data into it
37 | * @return true if should be intercepted
38 | */
39 | boolean intercept (Uri uri, RouteBundleExtras extras, Context context);
40 |
41 | /**
42 | * This method should be invoked when you has been intercepted
43 | * @param uri uri the uri to open
44 | * @param context context
45 | * @param extras some extras data for route,
46 | * sometimes is null when you not use
47 | * {@link Router#getBaseRoute()} to set some extras data into it
48 | */
49 | void onIntercepted(Uri uri, RouteBundleExtras extras, Context context);
50 | }
51 |
--------------------------------------------------------------------------------
/router-api/src/main/java/com/lzh/nonview/router/interceptors/RouteInterceptorAction.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2017 Haoge
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.lzh.nonview.router.interceptors;
17 |
18 | import java.util.List;
19 |
20 | public interface RouteInterceptorAction {
21 |
22 | /**
23 | * Add a interceptor to container
24 | * @param interceptor interceptor instance
25 | * @return The real type
26 | */
27 | T addInterceptor (RouteInterceptor interceptor);
28 |
29 | /**
30 | * Remove a interceptor from container
31 | * @param interceptor interceptor instance
32 | * @return The real type
33 | */
34 | T removeInterceptor (RouteInterceptor interceptor);
35 |
36 | /**
37 | * remove all of interceptors you has set before
38 | * @return The real type
39 | */
40 | T removeAllInterceptors ();
41 |
42 | /**
43 | * get all interceptors you has set before
44 | * @return all of interceptors
45 | */
46 | List getInterceptors ();
47 | }
48 |
49 |
50 |
--------------------------------------------------------------------------------
/router-api/src/main/java/com/lzh/nonview/router/launcher/ActionLauncher.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2017 Haoge
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.lzh.nonview.router.launcher;
17 |
18 | import com.lzh.nonview.router.module.ActionRouteRule;
19 | import com.lzh.nonview.router.tools.Cache;
20 | import com.lzh.nonview.router.tools.Constants;
21 |
22 | import java.util.concurrent.Executor;
23 |
24 | /**
25 | * The base class of Action Launcher
26 | *
27 | *
28 | * The default impl is {@link DefaultActionLauncher}
29 | *
30 | */
31 | public abstract class ActionLauncher extends Launcher {
32 |
33 | /**
34 | * @return returns a executor instance to switching thread.
35 | */
36 | protected Executor getExecutor() {
37 | Executor executor = extras.getValue(Constants.KEY_ACTION_EXECUTOR);
38 | if (executor == null) {
39 | executor = Cache.findOrCreateExecutor(((ActionRouteRule) rule).getExecutor());
40 | }
41 | return executor;
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/router-api/src/main/java/com/lzh/nonview/router/launcher/ActivityLauncher.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2017 Haoge
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.lzh.nonview.router.launcher;
17 |
18 | import android.app.Fragment;
19 | import android.content.Context;
20 | import android.content.Intent;
21 |
22 |
23 | /**
24 | * The base class of Activity Launcher
25 | *
26 | *
27 | * The default impl is {@link DefaultActivityLauncher}
28 | *
29 | */
30 | public abstract class ActivityLauncher extends Launcher{
31 |
32 | /**
33 | * This method will be invoked when you call {@link com.lzh.nonview.router.route.ActivityRoute#createIntent(Context)}
34 | *
35 | * @param context The context instance.
36 | * @return The new intent that created by the launcher
37 | */
38 | public abstract Intent createIntent(Context context);
39 |
40 | /**
41 | * The launch method for Fragment: {@link Fragment#startActivityForResult(Intent, int)}
42 | * @param fragment The fragment instance
43 | * @throws Exception a error occurs
44 | */
45 | public abstract void open(Fragment fragment) throws Exception;
46 | }
47 |
--------------------------------------------------------------------------------
/router-api/src/main/java/com/lzh/nonview/router/launcher/DefaultActionLauncher.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2017 Haoge
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.lzh.nonview.router.launcher;
17 |
18 | import android.content.Context;
19 | import android.os.Bundle;
20 |
21 | import com.lzh.nonview.router.route.ActionSupport;
22 |
23 | /**
24 | * Default Action Launcher for {@link com.lzh.nonview.router.route.ActionRoute}
25 | */
26 | public class DefaultActionLauncher extends ActionLauncher{
27 |
28 | @Override
29 | public void open(Context context) {
30 | final ActionSupport support = newInstance(rule.getRuleClz());
31 | final Bundle data = new Bundle();
32 | data.putAll(bundle);
33 | data.putAll(extras.getExtras());
34 | getExecutor().execute(new ActionRunnable(support, context, data));
35 | }
36 |
37 | private static class ActionRunnable implements Runnable {
38 |
39 | ActionSupport support;
40 | Context context;
41 | Bundle data;
42 |
43 | ActionRunnable(ActionSupport support, Context context, Bundle data) {
44 | this.support = support;
45 | this.context = context;
46 | this.data = data;
47 | }
48 |
49 | @Override
50 | public void run() {
51 | support.onRouteTrigger(context, data);
52 | }
53 | }
54 |
55 | private ActionSupport newInstance(String name) {
56 | try {
57 | return (ActionSupport) Class.forName(name).newInstance();
58 | } catch (Exception e) {
59 | throw new RuntimeException(String.format("create instance of %s failed", name), e);
60 | }
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/router-api/src/main/java/com/lzh/nonview/router/launcher/DefaultActivityLauncher.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2017 Haoge
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.lzh.nonview.router.launcher;
17 |
18 | import android.app.Activity;
19 | import android.app.Fragment;
20 | import android.content.Context;
21 | import android.content.Intent;
22 | import android.os.Build;
23 |
24 | import com.lzh.nonview.router.activityresult.ActivityResultCallback;
25 | import com.lzh.nonview.router.activityresult.ActivityResultDispatcher;
26 | import com.lzh.nonview.router.extras.RouteBundleExtras;
27 |
28 | /**
29 | * Default Activity Launcher for {@link com.lzh.nonview.router.route.ActivityRoute}
30 | */
31 | public class DefaultActivityLauncher extends ActivityLauncher{
32 |
33 | @Override
34 | public Intent createIntent(Context context) {
35 | Intent intent = new Intent();
36 | intent.setClassName(context, rule.getRuleClz());
37 | intent.putExtras(bundle);
38 | intent.putExtras(extras.getExtras());
39 | intent.addFlags(extras.getFlags());
40 | return intent;
41 | }
42 |
43 | @Override
44 | public void open(Fragment fragment) {
45 | if (resumeContext != null) {
46 | open(resumeContext);
47 | } else if (resultCallback != null) {
48 | open(fragment.getActivity());
49 | } else {
50 | Intent intent = createIntent(fragment.getActivity());
51 | fragment.startActivityForResult(intent, extras.getRequestCode());
52 | overridePendingTransition(fragment.getActivity(), extras);
53 | }
54 | }
55 |
56 | @Override
57 | public void open(Context context) {
58 | Activity resume = resumeContext;
59 | if (resume != null) {
60 | context = resume;
61 | }
62 |
63 | ActivityResultCallback callback = resultCallback;
64 | int requestCode = extras.getRequestCode();
65 |
66 | Intent intent = createIntent(context);
67 | if (context instanceof Activity) {
68 | Activity activity = (Activity) context;
69 | if (options != null && Build.VERSION.SDK_INT >= 16) {
70 | activity.startActivityForResult(intent, requestCode, options);
71 | } else {
72 | activity.startActivityForResult(intent,requestCode);
73 | }
74 | overridePendingTransition((Activity) context, extras);
75 | ActivityResultDispatcher.get().bindRequestArgs(activity, requestCode, callback);
76 | } else {
77 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
78 | context.startActivity(intent);
79 | }
80 | }
81 |
82 | protected void overridePendingTransition(Activity activity, RouteBundleExtras extras) {
83 | if (activity == null || extras == null) {
84 | return;
85 | }
86 |
87 | int inAnimation = extras.getInAnimation();
88 | int outAnimation = extras.getOutAnimation();
89 | if (inAnimation >= 0 && outAnimation >= 0) {
90 | activity.overridePendingTransition(inAnimation,outAnimation);
91 | }
92 | }
93 |
94 | }
95 |
--------------------------------------------------------------------------------
/router-api/src/main/java/com/lzh/nonview/router/launcher/Launcher.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2017 Haoge
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.lzh.nonview.router.launcher;
17 |
18 | import android.app.Activity;
19 | import android.content.Context;
20 | import android.net.Uri;
21 | import android.os.Bundle;
22 |
23 | import com.lzh.nonview.router.Router;
24 | import com.lzh.nonview.router.activityresult.ActivityResultCallback;
25 | import com.lzh.nonview.router.extras.RouteBundleExtras;
26 | import com.lzh.nonview.router.module.RouteRule;
27 | import com.lzh.nonview.router.tools.Constants;
28 |
29 | import java.util.Random;
30 |
31 | /**
32 | * The base launcher class.
33 | */
34 | public abstract class Launcher {
35 | private static Random sCodeGenerator = new Random();
36 |
37 | protected Uri uri;
38 | protected Bundle bundle;
39 | protected RouteBundleExtras extras;
40 | protected RouteRule rule;
41 | protected Bundle remote;
42 |
43 | protected Activity resumeContext;
44 | protected ActivityResultCallback resultCallback;
45 | protected Bundle options;
46 |
47 | /**
48 | * Requires to open with this launcher.
49 | * @param context context
50 | * @throws Exception Some error occurs.
51 | */
52 | public abstract void open(Context context) throws Exception;
53 |
54 | /**
55 | * Set all of extras data to used.
56 | * @param uri The route uri.
57 | * @param bundle The bundle data that is parsed by uri params.
58 | * @param extras The extras data you set via {@link Router#getRoute()}
59 | * @param rule The rule that associate with the uri.
60 | */
61 | public final void set(Uri uri, Bundle bundle, RouteBundleExtras extras, RouteRule rule, Bundle remote) {
62 | this.uri = uri;
63 | this.bundle = bundle;
64 | this.extras = extras;
65 | this.rule = rule;
66 | this.remote = remote;
67 |
68 | resumeContext = extras.getValue(Constants.KEY_RESUME_CONTEXT);
69 | resultCallback = extras.getValue(Constants.KEY_RESULT_CALLBACK);
70 | options = extras.getValue(Constants.KEY_ACTIVITY_OPTIONS);
71 |
72 | int requestCode = extras.getRequestCode();
73 | if (resultCallback != null && requestCode == -1) {
74 | extras.setRequestCode(sCodeGenerator.nextInt(0x0000ffff));
75 | }
76 | }
77 |
78 | }
79 |
--------------------------------------------------------------------------------
/router-api/src/main/java/com/lzh/nonview/router/module/ActionRouteRule.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2017 Haoge
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.lzh.nonview.router.module;
17 |
18 | import com.lzh.nonview.router.executors.MainThreadExecutor;
19 | import com.lzh.nonview.router.launcher.ActionLauncher;
20 | import com.lzh.nonview.router.route.ActionSupport;
21 |
22 | import java.util.concurrent.Executor;
23 |
24 | public class ActionRouteRule extends RouteRule {
25 |
26 | private Class extends Executor> executor = MainThreadExecutor.class;
27 |
28 | public ActionRouteRule(Class clz) {
29 | super(clz.getCanonicalName());
30 | }
31 |
32 | public ActionRouteRule(String clzName) {
33 | super(clzName);
34 | }
35 |
36 | public ActionRouteRule setExecutorClass(Class extends Executor> executor) {
37 | if (executor != null) {
38 | this.executor = executor;
39 | }
40 | return this;
41 | }
42 |
43 | public Class extends Executor> getExecutor() {
44 | return executor;
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/router-api/src/main/java/com/lzh/nonview/router/module/ActivityRouteRule.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2017 Haoge
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.lzh.nonview.router.module;
17 |
18 |
19 | import android.app.Activity;
20 |
21 | import com.lzh.nonview.router.launcher.ActivityLauncher;
22 |
23 | public class ActivityRouteRule extends RouteRule {
24 |
25 | public ActivityRouteRule(Class clz) {
26 | super(clz.getCanonicalName());
27 | }
28 |
29 | public ActivityRouteRule(String clzName) {
30 | super(clzName);
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/router-api/src/main/java/com/lzh/nonview/router/module/CreatorRouteRule.java:
--------------------------------------------------------------------------------
1 | package com.lzh.nonview.router.module;
2 |
3 | import com.lzh.nonview.router.launcher.Launcher;
4 |
5 | /**
6 | * @author haoge on 2018/5/25
7 | */
8 | public class CreatorRouteRule extends RouteRule {
9 |
10 | private Class target;
11 |
12 | public CreatorRouteRule(Class clz) {
13 | super(clz.getCanonicalName());
14 | this.target = clz;
15 | }
16 |
17 | public Class getTarget() {
18 | return target;
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/router-api/src/main/java/com/lzh/nonview/router/module/RemoteRule.java:
--------------------------------------------------------------------------------
1 | package com.lzh.nonview.router.module;
2 |
3 | import android.os.Bundle;
4 | import android.os.Parcel;
5 | import android.os.Parcelable;
6 |
7 | import java.util.HashMap;
8 |
9 | public class RemoteRule implements Parcelable{
10 | // target class name
11 | private String name;
12 | // params in RouteRule
13 | private HashMap params;
14 | // the extra bundle created by IRemoteFactory.
15 | private Bundle extra;
16 | // type in [Action, Activity]
17 | private int type;
18 |
19 | private RouteRule rule;
20 |
21 | public RemoteRule() {
22 | }
23 |
24 | protected RemoteRule(Parcel in) {
25 | name = in.readString();
26 | params = in.readHashMap(getClass().getClassLoader());
27 | type = in.readInt();
28 | extra = in.readBundle(getClass().getClassLoader());
29 | }
30 |
31 | public static final Creator CREATOR = new Creator() {
32 | @Override
33 | public RemoteRule createFromParcel(Parcel in) {
34 | return new RemoteRule(in);
35 | }
36 |
37 | @Override
38 | public RemoteRule[] newArray(int size) {
39 | return new RemoteRule[size];
40 | }
41 | };
42 |
43 | public static RemoteRule create(RouteRule rule, Bundle extra) {
44 | RemoteRule remote = new RemoteRule();
45 | remote.name = rule.getRuleClz();
46 | remote.params = rule.getParams();
47 | remote.type = (rule instanceof ActivityRouteRule) ? 0 : 1;
48 | remote.extra = extra;
49 | return remote;
50 | }
51 |
52 | @Override
53 | public int describeContents() {
54 | return 0;
55 | }
56 |
57 | @Override
58 | public void writeToParcel(Parcel dest, int flags) {
59 | dest.writeString(name);
60 | dest.writeMap(params);
61 | dest.writeInt(type);
62 | dest.writeBundle(extra);
63 | }
64 |
65 | public Bundle getExtra() {
66 | return extra;
67 | }
68 |
69 | public RouteRule getRule() {
70 | if (rule != null) {
71 | return rule;
72 | }
73 |
74 | switch (type) {
75 | case 0:
76 | rule = new ActivityRouteRule(name).setParams(params);
77 | break;
78 | default:
79 | rule = new ActionRouteRule(name).setParams(params);
80 | }
81 | return rule;
82 | }
83 | }
84 |
--------------------------------------------------------------------------------
/router-api/src/main/java/com/lzh/nonview/router/module/RouteCreator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2017 Haoge
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.lzh.nonview.router.module;
17 |
18 | import java.util.Map;
19 |
20 | /**
21 | * An interface to define route rules to router lib
22 | * @author haoge
23 | */
24 | public interface RouteCreator {
25 |
26 | /**
27 | * create route rules for ActivityRoute
28 | * @return A map that contains of all Activity route rules.
29 | */
30 | Map createActivityRouteRules();
31 |
32 | /**
33 | * create route rules for ActionRoute
34 | * @return A map that contains of all Action route rules.
35 | */
36 | Map createActionRouteRules();
37 |
38 | Map createCreatorRouteRule();
39 |
40 | }
41 |
--------------------------------------------------------------------------------
/router-api/src/main/java/com/lzh/nonview/router/module/RouteRule.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2017 Haoge
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.lzh.nonview.router.module;
17 |
18 | import android.app.Activity;
19 |
20 | import com.lzh.nonview.router.interceptors.RouteInterceptor;
21 | import com.lzh.nonview.router.launcher.Launcher;
22 | import com.lzh.nonview.router.route.ActionSupport;
23 |
24 | import java.util.HashMap;
25 |
26 | /**
27 | * An entity to contains some data for route
28 | *
29 | * @author haoge
30 | */
31 | @SuppressWarnings("unchecked")
32 | public class RouteRule{
33 |
34 | public RouteRule(String clzName) {
35 | this.clzName = clzName;
36 | }
37 |
38 | /** The class name must be subclass of {@link Activity} or {@link ActionSupport}*/
39 | private String clzName;
40 | private HashMap params = new HashMap<>();
41 | private Class extends L> launcher;
42 | private Class extends RouteInterceptor>[] interceptors = new Class[0];
43 |
44 | public String getRuleClz() {
45 | return clzName;
46 | }
47 |
48 | public HashMap getParams() {
49 | return params;
50 | }
51 |
52 | R setParams(HashMap params) {
53 | if (params != null) {
54 | this.params = params;
55 | }
56 | return (R) this;
57 | }
58 |
59 | /**
60 | * Set a serial of {@link RouteInterceptor} to used, it means when you launch this routing, the interceptors will be triggered.
61 | * @param classes The array of {@link RouteInterceptor}
62 | * @return RouteRule
63 | */
64 | public R setInterceptors(Class extends RouteInterceptor> ... classes) {
65 | if (classes != null) {
66 | this.interceptors = classes;
67 | }
68 | return (R) this;
69 | }
70 |
71 | public Class extends RouteInterceptor>[] getInterceptors() {
72 | return interceptors;
73 | }
74 |
75 | public R setLauncher(Class extends L> launcher) {
76 | this.launcher = launcher;
77 | return (R) this;
78 | }
79 |
80 | public Class extends L> getLauncher() {
81 | return launcher;
82 | }
83 | }
84 |
--------------------------------------------------------------------------------
/router-api/src/main/java/com/lzh/nonview/router/parser/URIParser.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2017 Haoge
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.lzh.nonview.router.parser;
17 |
18 | import android.net.Uri;
19 | import android.text.TextUtils;
20 |
21 | import com.lzh.nonview.router.tools.Utils;
22 |
23 | import java.net.URLDecoder;
24 | import java.util.HashMap;
25 | import java.util.IdentityHashMap;
26 | import java.util.Map;
27 |
28 | /**
29 | * A parser to parse uri to scheme/host/params .etc
30 | * Created by lzh on 16/9/5.
31 | */
32 | public class URIParser {
33 |
34 | private Uri uri;
35 | private String route;
36 | private Map params;
37 |
38 | public URIParser(Uri uri) {
39 | this.uri = uri;
40 | parse();
41 | }
42 |
43 | private void parse() {
44 | this.route = Utils.format(uri.getScheme() + "://" + uri.getHost() + uri.getPath());
45 | String query = uri.getEncodedQuery();
46 | if (!TextUtils.isEmpty(query)) {
47 | params = parseParams(query);
48 | } else {
49 | params = new HashMap<>();
50 | }
51 | }
52 |
53 | /**
54 | * Parse params form query string
55 | *
56 | * To support parse list to bundle,use {@link IdentityHashMap} to hold key-value
57 | *
58 | * @param query query in uri
59 | * @return a map contains key-value data parsed by query in uri
60 | */
61 | static Map parseParams(String query) {
62 | Map params = new IdentityHashMap<>();
63 | String[] split = query.split("&");
64 | for (String param : split) {
65 | if (!param.contains("=")) {
66 | continue;
67 | }
68 | int index = param.indexOf("=");
69 | //noinspection RedundantStringConstructorCall
70 | params.put(new String(param.substring(0, index)), URLDecoder.decode(param.substring(index + 1)));
71 | }
72 | return params;
73 | }
74 |
75 | public Map getParams() {
76 | return params;
77 | }
78 |
79 | public String getRoute() {
80 | return route;
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/router-api/src/main/java/com/lzh/nonview/router/protocol/HostServiceWrapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2017 Haoge
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.lzh.nonview.router.protocol;
17 |
18 | import android.app.Service;
19 | import android.content.ComponentName;
20 | import android.content.Context;
21 | import android.content.Intent;
22 | import android.content.ServiceConnection;
23 | import android.net.Uri;
24 | import android.os.Bundle;
25 | import android.os.IBinder;
26 | import android.text.TextUtils;
27 |
28 | import com.lzh.nonview.router.RouterConfiguration;
29 | import com.lzh.nonview.router.module.RemoteRule;
30 | import com.lzh.nonview.router.module.RouteRule;
31 | import com.lzh.nonview.router.route.ActionRoute;
32 | import com.lzh.nonview.router.route.ActivityRoute;
33 | import com.lzh.nonview.router.route.IRoute;
34 | import com.lzh.nonview.router.route.InternalCallback;
35 | import com.lzh.nonview.router.tools.Cache;
36 |
37 | import java.util.HashMap;
38 | import java.util.Map;
39 |
40 | public class HostServiceWrapper {
41 |
42 | private static Context context;
43 | private static IService service;
44 | private static String pluginName;
45 |
46 | private static ServiceConnection connection = new ServiceConnection() {
47 | @Override
48 | public void onServiceConnected(ComponentName name, IBinder service) {
49 | HostServiceWrapper.service = IService.Stub.asInterface(service);
50 | // register rules to remote service.
51 | registerRulesToHostService();
52 | }
53 |
54 | @Override
55 | public void onServiceDisconnected(ComponentName name) {
56 | HostServiceWrapper.service = null;
57 | }
58 | };
59 |
60 | /**
61 | * Set a host package name. it will be used to bind a remote service in host app.
62 | *
63 | * @param hostPackage the package name of host.
64 | * @param context the context to start remote service.
65 | * @param pluginName The plugin name to register to remote service. to help to judge if it should be
66 | */
67 | public static void startHostService(String hostPackage, Context context, String pluginName) {
68 | if (service != null) {
69 | throw new RuntimeException("You've bind a remote service before");
70 | }
71 | if (TextUtils.isEmpty(hostPackage)) {
72 | throw new IllegalArgumentException("Please provide a valid host package name.");
73 | }
74 | HostServiceWrapper.context = context.getApplicationContext();
75 | HostServiceWrapper.pluginName = TextUtils.isEmpty(pluginName) ? context.getPackageName() : pluginName;
76 |
77 | Intent intent = new Intent();
78 | intent.setPackage(hostPackage);
79 | intent.setAction("com.lzh.router.action.HOST");
80 | context.bindService(intent, connection, Service.BIND_AUTO_CREATE);
81 | }
82 |
83 | public static IRoute create(Uri uri, InternalCallback callback) {
84 | try {
85 | return createWithThrow(uri, callback);
86 | } catch (Exception e) {
87 | return new IRoute.EmptyRoute(callback);
88 | }
89 | }
90 |
91 | private static IRoute createWithThrow(Uri uri, InternalCallback callback) throws Exception{
92 | RemoteRule rule;
93 | if ((rule = service.getActivityRule(uri)) != null) {
94 | return new ActivityRoute().create(uri, rule.getRule(), rule.getExtra(), callback);
95 | } else if ((rule = service.getActionRule(uri)) != null) {
96 | return new ActionRoute().create(uri, rule.getRule(), rule.getExtra(), callback);
97 | } else {
98 | return new IRoute.EmptyRoute(callback);
99 | }
100 | }
101 |
102 | public static boolean isRegister(String pluginName) {
103 | try {
104 | return service.isRegister(pluginName);
105 | } catch (Exception e) {
106 | return false;
107 | }
108 | }
109 |
110 | public static void registerRulesToHostService() {
111 | try {
112 | if (service == null) {
113 | return;
114 | }
115 | service.register(pluginName);
116 | service.addActionRules(transform(Cache.getActionRules()));
117 | service.addActivityRules(transform(Cache.getActivityRules()));
118 | } catch (Exception e) {
119 | // ignore
120 | }
121 | }
122 |
123 | private static Map transform(Map source){
124 | Map dest = new HashMap<>();
125 | for (String route : source.keySet()) {
126 | RouteRule rule = source.get(route);
127 | RemoteRule remote = RemoteRule.create(rule, getRemote(context, rule));
128 | dest.put(route, remote);
129 | }
130 | return dest;
131 | }
132 |
133 | private static Bundle getRemote(Context context, RouteRule rule){
134 | IRemoteFactory factory = RouterConfiguration.get().getRemoteFactory();
135 | return factory == null ? null : factory.createRemote(context, rule);
136 | }
137 |
138 | }
139 |
--------------------------------------------------------------------------------
/router-api/src/main/java/com/lzh/nonview/router/protocol/IRemoteFactory.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2017 Haoge
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.lzh.nonview.router.protocol;
17 |
18 | import android.content.Context;
19 | import android.os.Bundle;
20 |
21 | import com.lzh.nonview.router.RouterConfiguration;
22 | import com.lzh.nonview.router.module.RouteRule;
23 |
24 | /**
25 | *
26 | * This factory used to create and provide a remote bundle data.
27 | *
28 | *
29 | *
When need to register the routing rules to remote bridge service via {@link RouterConfiguration#startHostService(String, Context)},
30 | * the factory will be called to create a bundle and pass it to remote service from aidl interface.
31 | */
32 | public interface IRemoteFactory {
33 | /**
34 | * Create a extra bundle data so that others process or plugin could compat.
35 | * @param application The application context.
36 | * @param rule The routing rule
37 | * @return new extra bundle or null.
38 | */
39 | Bundle createRemote(Context application, RouteRule rule);
40 | }
41 |
--------------------------------------------------------------------------------
/router-api/src/main/java/com/lzh/nonview/router/route/ActionRoute.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2017 Haoge
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.lzh.nonview.router.route;
17 |
18 | import com.lzh.nonview.router.RouterConfiguration;
19 | import com.lzh.nonview.router.launcher.ActionLauncher;
20 | import com.lzh.nonview.router.launcher.Launcher;
21 | import com.lzh.nonview.router.module.ActionRouteRule;
22 | import com.lzh.nonview.router.tools.Constants;
23 |
24 | import java.util.concurrent.Executor;
25 |
26 | public class ActionRoute extends BaseRoute implements IActionRoute {
27 |
28 | @Override
29 | protected Launcher obtainLauncher() throws Exception{
30 | ActionRouteRule rule = (ActionRouteRule) routeRule;
31 | Class extends ActionLauncher> launcher = rule.getLauncher();
32 | if (launcher == null) {
33 | launcher = RouterConfiguration.get().getActionLauncher();
34 | }
35 | return launcher.newInstance();
36 | }
37 |
38 | @Override
39 | public void setExecutor(Executor executor) {
40 | callback.getExtras().putValue(Constants.KEY_ACTION_EXECUTOR, executor);
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/router-api/src/main/java/com/lzh/nonview/router/route/ActionSupport.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2017 Haoge
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.lzh.nonview.router.route;
17 |
18 | import android.content.Context;
19 | import android.os.Bundle;
20 |
21 | /**
22 | *
You can inherit from this class to create action routing event.
23 | *
24 | * @author haoge
25 | */
26 | public abstract class ActionSupport {
27 |
28 | /**
29 | * The callback method to received routing bundle data.
30 | * @param context The context who launch the routing event.
31 | * @param bundle The extras bundle data from {@link IBaseRoute#addExtras(Bundle)} and url parameters.
32 | */
33 | public abstract void onRouteTrigger(Context context, Bundle bundle);
34 | }
35 |
--------------------------------------------------------------------------------
/router-api/src/main/java/com/lzh/nonview/router/route/ActivityRoute.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2017 Haoge
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.lzh.nonview.router.route;
17 |
18 | import android.app.Fragment;
19 | import android.content.Context;
20 | import android.content.Intent;
21 | import android.os.Bundle;
22 |
23 | import com.lzh.nonview.router.RouterConfiguration;
24 | import com.lzh.nonview.router.activityresult.ActivityResultCallback;
25 | import com.lzh.nonview.router.launcher.ActivityLauncher;
26 | import com.lzh.nonview.router.launcher.Launcher;
27 | import com.lzh.nonview.router.module.ActivityRouteRule;
28 | import com.lzh.nonview.router.tools.Constants;
29 | import com.lzh.nonview.router.tools.Utils;
30 |
31 | /**
32 | * A route tool to check route rule by uri and launch activity
33 | * Created by lzh on 16/9/5.
34 | */
35 | public class ActivityRoute extends BaseRoute implements IActivityRoute {
36 |
37 | @Override
38 | public Intent createIntent(Context context) {
39 | ActivityLauncher activityLauncher = (ActivityLauncher) launcher;
40 | activityLauncher.set(uri, bundle, callback.getExtras(), routeRule, remote);
41 | return activityLauncher.createIntent(context);
42 | }
43 |
44 | @Override
45 | public IActivityRoute requestCode(int requestCode) {
46 | this.callback.getExtras().setRequestCode(requestCode);
47 | return this;
48 | }
49 |
50 | @Override
51 | public IActivityRoute resultCallback(ActivityResultCallback callback) {
52 | this.callback.getExtras().putValue(Constants.KEY_RESULT_CALLBACK, callback);
53 | return this;
54 | }
55 |
56 | @Override
57 | public IActivityRoute setOptions(Bundle options) {
58 | this.callback.getExtras().putValue(Constants.KEY_ACTIVITY_OPTIONS, options);
59 | return this;
60 | }
61 |
62 | @Override
63 | public IActivityRoute setAnim(int enterAnim, int exitAnim) {
64 | this.callback.getExtras().setInAnimation(enterAnim);
65 | this.callback.getExtras().setOutAnimation(exitAnim);
66 | return this;
67 | }
68 |
69 | @Override
70 | public IActivityRoute addFlags(int flag) {
71 | this.callback.getExtras().addFlags(flag);
72 | return this;
73 | }
74 |
75 | @Override
76 | public void open(Fragment fragment) {
77 | try {
78 | Utils.checkInterceptor(uri, callback.getExtras(), fragment.getActivity(), getInterceptors());
79 | ActivityLauncher activityLauncher = (ActivityLauncher) launcher;
80 | activityLauncher.set(uri, bundle, callback.getExtras(), routeRule, remote);
81 | activityLauncher.open(fragment);
82 | callback.onOpenSuccess(routeRule);
83 | } catch (Throwable e) {
84 | callback.onOpenFailed(e);
85 | }
86 |
87 | callback.invoke(fragment.getActivity());
88 | }
89 |
90 | @Override
91 | protected Launcher obtainLauncher() throws Exception{
92 | ActivityRouteRule rule = (ActivityRouteRule) routeRule;
93 | Class extends ActivityLauncher> launcher = rule.getLauncher();
94 | if (launcher == null) {
95 | launcher = RouterConfiguration.get().getActivityLauncher();
96 | }
97 | return launcher.newInstance();
98 | }
99 | }
100 |
--------------------------------------------------------------------------------
/router-api/src/main/java/com/lzh/nonview/router/route/BaseRoute.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2017 Haoge
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.lzh.nonview.router.route;
17 |
18 | import android.content.Context;
19 | import android.net.Uri;
20 | import android.os.Bundle;
21 |
22 | import com.lzh.nonview.router.Router;
23 | import com.lzh.nonview.router.RouterConfiguration;
24 | import com.lzh.nonview.router.extras.RouteBundleExtras;
25 | import com.lzh.nonview.router.interceptors.RouteInterceptor;
26 | import com.lzh.nonview.router.interceptors.RouteInterceptorAction;
27 | import com.lzh.nonview.router.launcher.Launcher;
28 | import com.lzh.nonview.router.module.RouteRule;
29 | import com.lzh.nonview.router.parser.URIParser;
30 | import com.lzh.nonview.router.tools.Cache;
31 | import com.lzh.nonview.router.tools.Utils;
32 |
33 | import java.util.ArrayList;
34 | import java.util.List;
35 |
36 | @SuppressWarnings("unchecked")
37 | public abstract class BaseRoute implements IRoute, IBaseRoute, RouteInterceptorAction {
38 | protected Bundle bundle;
39 | InternalCallback callback;
40 | protected Uri uri;
41 | protected Bundle remote;
42 | protected RouteRule routeRule = null;
43 | protected Launcher launcher;
44 |
45 | public final IRoute create(Uri uri, RouteRule rule, Bundle remote, InternalCallback callback) {
46 | try {
47 | this.uri = uri;
48 | this.remote = remote;
49 | this.callback = callback;
50 | this.routeRule = rule;
51 | this.bundle = Utils.parseToBundle(new URIParser(uri));
52 | this.bundle.putParcelable(Router.RAW_URI, uri);
53 | this.launcher = obtainLauncher();
54 | return this;
55 | } catch (Throwable e) {
56 | callback.onOpenFailed(e);
57 | return new EmptyRoute(callback);
58 | }
59 | }
60 |
61 | // =========Unify method of IBaseRoute
62 | @Override
63 | public final void open(Context context) {
64 | try {
65 | Utils.checkInterceptor(uri, callback.getExtras(), context,getInterceptors());
66 | launcher.set(uri, bundle, callback.getExtras(), routeRule, remote);
67 | launcher.open(context);
68 | // realOpen(context);
69 | callback.onOpenSuccess(routeRule);
70 | } catch (Throwable e) {
71 | callback.onOpenFailed(e);
72 | }
73 |
74 | callback.invoke(context);
75 | }
76 |
77 | @Override
78 | public T addExtras(Bundle extras) {
79 | this.callback.getExtras().addExtras(extras);
80 | return (T) this;
81 | }
82 |
83 | // =============RouteInterceptor operation===============
84 | public T addInterceptor(RouteInterceptor interceptor) {
85 | if (callback.getExtras() != null) {
86 | callback.getExtras().addInterceptor(interceptor);
87 | }
88 | return (T) this;
89 | }
90 |
91 | @Override
92 | public T removeInterceptor(RouteInterceptor interceptor) {
93 | if (callback.getExtras() != null) {
94 | callback.getExtras().removeInterceptor(interceptor);
95 | }
96 | return (T) this;
97 | }
98 |
99 | @Override
100 | public T removeAllInterceptors() {
101 | if (callback.getExtras() != null) {
102 | callback.getExtras().removeAllInterceptors();
103 | }
104 | return (T) this;
105 | }
106 |
107 | @Override
108 | public List getInterceptors() {
109 |
110 | List interceptors = new ArrayList<>();
111 | // add global interceptor
112 | if (RouterConfiguration.get().getInterceptor() != null) {
113 | interceptors.add(RouterConfiguration.get().getInterceptor());
114 | }
115 |
116 | // add extra interceptors
117 | if (callback.getExtras() != null) {
118 | interceptors.addAll(callback.getExtras().getInterceptors());
119 | }
120 |
121 | // add interceptors in rule
122 | for (Class interceptor : routeRule.getInterceptors()) {
123 | if (interceptor != null) {
124 | try {
125 | interceptors.add(interceptor.newInstance());
126 | } catch (Exception e) {
127 | throw new RuntimeException(String.format("The interceptor class [%s] should provide a default empty construction", interceptor));
128 | }
129 | }
130 | }
131 |
132 | return interceptors;
133 | }
134 |
135 | // ========getter/setter============
136 | public void replaceExtras(RouteBundleExtras extras) {
137 | this.callback.setExtras(extras);
138 | }
139 |
140 | public static RouteRule findRule(Uri uri, int type) {
141 | return Cache.getRouteMapByUri(new URIParser(uri), type);
142 | }
143 |
144 | // ============abstract methods============
145 | protected abstract Launcher obtainLauncher() throws Exception;
146 |
147 | }
148 |
--------------------------------------------------------------------------------
/router-api/src/main/java/com/lzh/nonview/router/route/BrowserRoute.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2017 Haoge
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.lzh.nonview.router.route;
17 |
18 | import android.content.Context;
19 | import android.content.Intent;
20 | import android.net.Uri;
21 |
22 | import com.lzh.nonview.router.tools.Utils;
23 |
24 | /**
25 | * A route tool to open uri by browser
26 | * Created by lzh on 16/9/5.
27 | */
28 | public class BrowserRoute implements IRoute {
29 |
30 | Uri uri;
31 |
32 | private static final BrowserRoute route = new BrowserRoute();
33 |
34 | public static BrowserRoute getInstance () {
35 | return route;
36 | }
37 |
38 | @Override
39 | public void open(Context context) {
40 | Intent intent = new Intent(Intent.ACTION_VIEW,uri);
41 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
42 | context.startActivity(intent);
43 | }
44 |
45 | public static boolean canOpenRouter(Uri uri) {
46 | return Utils.isHttp(uri.getScheme());
47 | }
48 |
49 | public IRoute setUri(Uri uri) {
50 | this.uri = uri;
51 | return this;
52 | }
53 |
54 | }
55 |
--------------------------------------------------------------------------------
/router-api/src/main/java/com/lzh/nonview/router/route/IActionRoute.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2017 Haoge
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.lzh.nonview.router.route;
17 |
18 | import java.util.concurrent.Executor;
19 |
20 | /**
21 | *
22 | * Base on the {@link IBaseRoute}
23 | *
24 | */
25 | public interface IActionRoute extends IBaseRoute{
26 |
27 | void setExecutor(Executor executor);
28 |
29 | class EmptyActionRoute extends EmptyBaseRoute implements IActionRoute {
30 |
31 | public EmptyActionRoute(InternalCallback internal) {
32 | super(internal);
33 | }
34 |
35 | @Override
36 | public void setExecutor(Executor executor) { }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/router-api/src/main/java/com/lzh/nonview/router/route/IActivityRoute.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2017 Haoge
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.lzh.nonview.router.route;
17 |
18 | import android.app.Fragment;
19 | import android.content.Context;
20 | import android.content.Intent;
21 | import android.os.Bundle;
22 |
23 | import com.lzh.nonview.router.activityresult.ActivityResultCallback;
24 | import com.lzh.nonview.router.extras.RouteBundleExtras;
25 | import com.lzh.nonview.router.tools.Constants;
26 |
27 | /**
28 | *
29 | * Base on the {@link IBaseRoute}, This interface provided some methods
30 | * to set some extras data for used by {@link android.app.Activity#startActivityForResult(Intent, int)}
31 | *
32 | */
33 | public interface IActivityRoute extends IBaseRoute {
34 |
35 | /**
36 | * Create intent by {@link RouteBundleExtras} and {@link Bundle} that parsed by uri
37 | * @param context The context to create intent
38 | * @return Intent that contains of extras data and bundle that parsed by uri
39 | */
40 | Intent createIntent (Context context);
41 |
42 | /**
43 | * Set request code for {@link android.app.Activity#startActivityForResult(Intent, int)}
44 | * @param requestCode request code
45 | * @return IActivityRoute
46 | */
47 | IActivityRoute requestCode(int requestCode);
48 |
49 | IActivityRoute resultCallback(ActivityResultCallback callback);
50 |
51 | IActivityRoute setOptions(Bundle options);
52 |
53 | /**
54 | * Set anim for {@link android.app.Activity#overridePendingTransition(int, int)}
55 | * @param enterAnim enter animation
56 | * @param exitAnim exit animation
57 | * @return IActivityRoute
58 | */
59 | IActivityRoute setAnim (int enterAnim, int exitAnim);
60 |
61 | /**
62 | * Associate with {@link Intent#addFlags(int)}
63 | * @param flag flag
64 | * @return IActivityRoute
65 | */
66 | IActivityRoute addFlags(int flag);
67 |
68 | /**
69 | * Launch routing by {@link Fragment}
70 | * @param fragment the fragment to startActivity
71 | */
72 | void open(Fragment fragment);
73 |
74 | class EmptyActivityRoute extends EmptyBaseRoute implements IActivityRoute {
75 |
76 | public EmptyActivityRoute(InternalCallback internal) {
77 | super(internal);
78 | }
79 |
80 | @Override
81 | public Intent createIntent(Context context) {
82 | internal.invoke(context);
83 | return new Intent();
84 | }
85 |
86 | @Override
87 | public IActivityRoute requestCode(int requestCode) {
88 | internal.getExtras().setRequestCode(requestCode);
89 | return this;
90 | }
91 |
92 | @Override
93 | public IActivityRoute resultCallback(ActivityResultCallback callback) {
94 | internal.getExtras().putValue(Constants.KEY_RESULT_CALLBACK, callback);
95 | return this;
96 | }
97 |
98 | @Override
99 | public IActivityRoute setOptions(Bundle options) {
100 | internal.getExtras().putValue(Constants.KEY_ACTIVITY_OPTIONS, options);
101 | return this;
102 | }
103 |
104 | @Override
105 | public IActivityRoute setAnim(int enterAnim, int exitAnim) {
106 | internal.getExtras().setInAnimation(enterAnim);
107 | internal.getExtras().setOutAnimation(exitAnim);
108 | return this;
109 | }
110 |
111 | @Override
112 | public IActivityRoute addFlags(int flag) {
113 | internal.getExtras().addFlags(flag);
114 | return this;
115 | }
116 |
117 | @Override
118 | public void open(Fragment fragment) {
119 | internal.invoke(fragment.getActivity());
120 | }
121 | }
122 | }
123 |
--------------------------------------------------------------------------------
/router-api/src/main/java/com/lzh/nonview/router/route/IBaseRoute.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2017 Haoge
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.lzh.nonview.router.route;
17 |
18 | import android.os.Bundle;
19 |
20 | import com.lzh.nonview.router.extras.RouteBundleExtras;
21 | import com.lzh.nonview.router.interceptors.RouteInterceptor;
22 | import com.lzh.nonview.router.interceptors.RouteInterceptorAction;
23 |
24 | import java.util.List;
25 |
26 | /**
27 | * Base on {@link IRoute} and {@link RouteInterceptorAction}, it subclass could be one of the
28 | * {@link IActionRoute} and {@link IActivityRoute}
29 | * @param The real type
30 | */
31 | public interface IBaseRoute extends IRoute, RouteInterceptorAction{
32 | /**
33 | * add extra bundle data to {@link RouteBundleExtras}
34 | * @param extras bundle data
35 | * @return {@link IBaseRoute}
36 | * @see IActionRoute
37 | * @see IActivityRoute
38 | */
39 | T addExtras(Bundle extras);
40 |
41 | /**
42 | * Add a interceptor to container
43 | * @param interceptor interceptor instance
44 | * @return The real type
45 | */
46 | T addInterceptor (RouteInterceptor interceptor);
47 |
48 | /**
49 | * Remove a interceptor from container
50 | * @param interceptor interceptor instance
51 | * @return The real type
52 | */
53 | T removeInterceptor (RouteInterceptor interceptor);
54 |
55 | /**
56 | * remove all of interceptors you has set before
57 | * @return The real type
58 | */
59 | T removeAllInterceptors ();
60 |
61 | /**
62 | * get all interceptors you has set before
63 | * @return all of interceptors
64 | */
65 | List getInterceptors ();
66 |
67 | @SuppressWarnings("unchecked")
68 | class EmptyBaseRoute extends EmptyRoute implements IBaseRoute {
69 |
70 | public EmptyBaseRoute(InternalCallback internal) {
71 | super(internal);
72 | }
73 |
74 | @Override
75 | public T addExtras(Bundle extras) {
76 | internal.getExtras().addExtras(extras);
77 | return (T) this;
78 | }
79 |
80 | @Override
81 | public T addInterceptor(RouteInterceptor interceptor) {
82 | internal.getExtras().addInterceptor(interceptor);
83 | return (T) this;
84 | }
85 |
86 | @Override
87 | public T removeInterceptor(RouteInterceptor interceptor) {
88 | internal.getExtras().removeInterceptor(interceptor);
89 | return (T) this;
90 | }
91 |
92 | @Override
93 | public T removeAllInterceptors() {
94 | internal.getExtras().removeAllInterceptors();
95 | return (T) this;
96 | }
97 |
98 | @Override
99 | public List getInterceptors() {
100 | return internal.getExtras().getInterceptors();
101 | }
102 | }
103 | }
104 |
--------------------------------------------------------------------------------
/router-api/src/main/java/com/lzh/nonview/router/route/ICreatorInjector.java:
--------------------------------------------------------------------------------
1 | package com.lzh.nonview.router.route;
2 |
3 | import android.os.Bundle;
4 |
5 | /**
6 | * @author haoge on 2018/5/25
7 | */
8 | public interface ICreatorInjector {
9 | void inject(Bundle bundle);
10 | }
11 |
--------------------------------------------------------------------------------
/router-api/src/main/java/com/lzh/nonview/router/route/IRoute.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2017 Haoge
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.lzh.nonview.router.route;
17 |
18 | import android.content.Context;
19 |
20 | /**
21 | * The top interface of routing operations. The subclass could be:
22 | * {@link BrowserRoute} / {@link IActionRoute} or {@link IActivityRoute}
23 | */
24 | public interface IRoute {
25 |
26 | /**
27 | * open route with uri by context
28 | * @param context The context to launch routing event
29 | */
30 | void open(Context context);
31 |
32 | class EmptyRoute implements IRoute{
33 | protected InternalCallback internal;
34 |
35 | public EmptyRoute(InternalCallback internal) {
36 | this.internal = internal;
37 | }
38 |
39 | public InternalCallback getInternal() {
40 | return internal;
41 | }
42 |
43 | @Override
44 | public void open(Context context) {
45 | internal.invoke(context);
46 | }
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/router-api/src/main/java/com/lzh/nonview/router/route/InternalCallback.java:
--------------------------------------------------------------------------------
1 | package com.lzh.nonview.router.route;
2 |
3 | import android.content.Context;
4 | import android.net.Uri;
5 | import android.util.Pair;
6 |
7 | import com.lzh.nonview.router.RouterConfiguration;
8 | import com.lzh.nonview.router.exception.NotFoundException;
9 | import com.lzh.nonview.router.extras.RouteBundleExtras;
10 | import com.lzh.nonview.router.module.RouteRule;
11 | import com.lzh.nonview.router.tools.RouterLog;
12 |
13 | import java.util.HashMap;
14 | import java.util.Map;
15 |
16 | /**
17 | *
18 | * Internal route callback.
19 | *
20 | */
21 | public final class InternalCallback {
22 |
23 | // store the map to provided find extras for uri.
24 | private static Map> cache = new HashMap<>();
25 |
26 | private Uri uri;
27 | private RouteBundleExtras extras = new RouteBundleExtras();
28 | private RouteRule rule;
29 | private Throwable error;
30 |
31 | public InternalCallback(Uri uri) {
32 | this.uri = uri;
33 | }
34 |
35 | public void setCallback(RouteCallback callback) {
36 | extras.setCallback(callback);
37 | }
38 |
39 | public RouteBundleExtras getExtras() {
40 | return extras;
41 | }
42 |
43 | public void setExtras(RouteBundleExtras extras) {
44 | if (extras != null) {
45 | this.extras = extras;
46 | }
47 | }
48 |
49 | public void onOpenSuccess(RouteRule rule) {
50 | this.rule = rule;
51 | }
52 |
53 | public void onOpenFailed(Throwable e) {
54 | if (error == null) {
55 | // 只接受第一次配置的异常
56 | this.error = e;
57 | }
58 | }
59 |
60 | void invoke(Context context) {
61 | cache.put(uri, new Pair<>(context, extras));
62 | invokeWithCallback(RouterConfiguration.get().getCallback(),
63 | extras.getCallback());
64 | cache.remove(uri);
65 | }
66 |
67 | private void invokeWithCallback(RouteCallback global, RouteCallback callback) {
68 | if (error != null && error instanceof NotFoundException) {
69 | RouterLog.d("[RouterLog] Could not found matched route for " + uri);
70 | if (global != null) {
71 | global.notFound(uri, (NotFoundException) error);
72 | }
73 | if (callback != null) {
74 | callback.notFound(uri, (NotFoundException) error);
75 | }
76 | } else if (error != null) {
77 | RouterLog.e("[RouterLog] Launch route with " + uri + " failed.", error);
78 | if (global != null) {
79 | global.onOpenFailed(uri, error);
80 | }
81 | if (callback != null) {
82 | callback.onOpenFailed(uri, error);
83 | }
84 | } else if (rule != null) {
85 | RouterLog.d("[RouterLog] Launch route with " + uri + " successful!, target class name is " + rule.getRuleClz());
86 | if (global != null) {
87 | global.onOpenSuccess(uri, rule);
88 | }
89 | if (callback != null) {
90 | callback.onOpenSuccess(uri, rule);
91 | }
92 | } else {
93 | RouterLog.e("[RouterLog] Launch route with " + uri + " failed.", null);
94 | if (global != null) {
95 | global.onOpenFailed(uri, new RuntimeException("Unknown error"));
96 | }
97 | if (callback != null) {
98 | callback.onOpenFailed(uri, new RuntimeException("Unknown error"));
99 | }
100 | }
101 | }
102 |
103 | public static RouteBundleExtras findExtrasByUri(Uri uri) {
104 | Pair pair = cache.get(uri);
105 | return pair == null? null : pair.second;
106 | }
107 |
108 | public static Context findContextByUri(Uri uri) {
109 | Pair pair = cache.get(uri);
110 | return pair == null? null : pair.first;
111 | }
112 | }
113 |
--------------------------------------------------------------------------------
/router-api/src/main/java/com/lzh/nonview/router/route/RouteCallback.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2017 Haoge
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.lzh.nonview.router.route;
17 |
18 | import android.net.Uri;
19 |
20 | import com.lzh.nonview.router.exception.NotFoundException;
21 | import com.lzh.nonview.router.module.ActionRouteRule;
22 | import com.lzh.nonview.router.module.ActivityRouteRule;
23 | import com.lzh.nonview.router.module.RouteRule;
24 |
25 | /**
26 | * The route callback to notify the status of routing event.
27 | * @author haoge
28 | */
29 | public interface RouteCallback {
30 |
31 | /**
32 | * This method will be invoked when there is no routing matched with uri.
33 | * @param uri uri the uri to open
34 | * @param e {@link NotFoundException}
35 | */
36 | void notFound(Uri uri, NotFoundException e);
37 |
38 | /**
39 | * This method will be invoked when the routing task opened successful
40 | *
41 | * @param uri the uri to open
42 | * @param rule The uri matching rule, it could be {@link ActionRouteRule} or {@link ActivityRouteRule}
43 | */
44 | void onOpenSuccess(Uri uri, RouteRule rule);
45 |
46 | /**
47 | * A callback method to notice that you occurs some exception.
48 | * exclude {@link NotFoundException}
49 | * @param uri the uri to open
50 | * @param e the exception
51 | */
52 | void onOpenFailed(Uri uri, Throwable e);
53 |
54 |
55 | }
56 |
--------------------------------------------------------------------------------
/router-api/src/main/java/com/lzh/nonview/router/tools/Cache.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2017 Haoge
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.lzh.nonview.router.tools;
17 |
18 | import com.lzh.nonview.router.executors.MainThreadExecutor;
19 | import com.lzh.nonview.router.module.ActionRouteRule;
20 | import com.lzh.nonview.router.module.ActivityRouteRule;
21 | import com.lzh.nonview.router.module.CreatorRouteRule;
22 | import com.lzh.nonview.router.module.RouteCreator;
23 | import com.lzh.nonview.router.module.RouteRule;
24 | import com.lzh.nonview.router.parser.URIParser;
25 |
26 | import java.util.ArrayList;
27 | import java.util.HashMap;
28 | import java.util.List;
29 | import java.util.Map;
30 | import java.util.concurrent.Executor;
31 |
32 | /**
33 | * The cache manager to holds all the cached instance.
34 | * @author haoge
35 | */
36 | public final class Cache {
37 |
38 | private static boolean shouldReload;// if should be reload routeMap.
39 | /** A container to contains all of route rule creator,compat with some complex scene*/
40 | private static List creatorList = new ArrayList<>();
41 | /** A map to contains all of route rule created by creatorList*/
42 | private static Map activityRouteMap = new HashMap<>();
43 | private static Map actionRouteMap = new HashMap<>();
44 | private static Map creatorRouteMap = new HashMap<>();
45 | public static final int TYPE_ACTIVITY_ROUTE = 0;
46 | public static final int TYPE_ACTION_ROUTE = 1;
47 |
48 | private final static Map, Executor> container = new HashMap<>();
49 |
50 | /**
51 | *
52 | * Add a {@link RouteCreator} who contains some route rules to be used.
53 | * this method could be invoked multiple-times. so that you can put multiple route rules from difference modules
54 | *
15 | * should not be modified by abstract,if set,should be skip
16 | * should not be modified by private,if set,should lead to crash
17 | * should be subclass of android.app.Activity,if not,should lead to crash
18 | *
19 | * @param type A element of class
20 | * @return true if it is a effective class
21 | */
22 | public static boolean checkTypeValid (TypeElement type) {
23 | Set modifiers = type.getModifiers();
24 | if (modifiers.contains(Modifier.PRIVATE)) {
25 | throw new RouterException(String.format("The class %s should not be modified by private",type.getSimpleName()),type);
26 | } else return !modifiers.contains(Modifier.ABSTRACT);
27 | }
28 |
29 | /**
30 | * Check out if the class {@code type} is a subclass of {@code superClass}
31 | * @param type the class to check
32 | * @param superClassName the super class name
33 | * @return true if is subclass
34 | */
35 | public static boolean isSuperClass (TypeElement type,String superClassName) {
36 | if (type == null) {
37 | return false;
38 | }
39 |
40 | do {
41 | type = (TypeElement) UtilMgr.getMgr().getTypeUtils().asElement(type.getSuperclass());
42 | if (type.getQualifiedName().toString().equals(superClassName)) {
43 | return true;
44 | }
45 | if ("java.lang.Object".equals(type.getQualifiedName().toString())) {
46 | return false;
47 | }
48 | } while (true);
49 | }
50 |
51 | public static boolean isEmpty (String data) {
52 | return data == null || data.length() == 0;
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/router-compiler/src/main/resources/META-INF/services/javax.annotation.processing.Processor:
--------------------------------------------------------------------------------
1 | com.lzh.nonview.router.compiler.Compiler
--------------------------------------------------------------------------------
/router-host/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/router-host/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | apply plugin: 'com.github.dcendents.android-maven'
3 |
4 | android {
5 | compileSdkVersion compile_version
6 | buildToolsVersion build_tool
7 |
8 | defaultConfig {
9 | minSdkVersion 14
10 | targetSdkVersion target_version
11 | versionCode 1
12 | versionName "1.0"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 |
21 | sourceSets {
22 | main {
23 | jni.srcDirs = [] //disable automatic ndk-build
24 | }
25 | }
26 | }
27 |
28 | dependencies {
29 | api fileTree(dir: 'libs', include: ['*.jar'])
30 | api project(':router-api')
31 | }
32 |
33 | apply from: '../javadoc.gradle'
--------------------------------------------------------------------------------
/router-host/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/haoge/Documents/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
19 | # Uncomment this to preserve the line number information for
20 | # debugging stack traces.
21 | #-keepattributes SourceFile,LineNumberTable
22 |
23 | # If you keep the line number information, uncomment this to
24 | # hide the original source file name.
25 | #-renamesourcefileattribute SourceFile
26 |
--------------------------------------------------------------------------------
/router-host/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
6 |
7 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/router-host/src/main/java/com/lzh/nonview/router/host/DefaultVerify.java:
--------------------------------------------------------------------------------
1 | package com.lzh.nonview.router.host;
2 |
3 | import android.content.Context;
4 | import android.os.Binder;
5 | import android.util.Log;
6 |
7 | /**
8 | * Default impl for {@link RemoteVerify}
9 | *
10 | *