WdRouterRule.java
6.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package com.wd.capability.router;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import androidx.fragment.app.Fragment;
import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.launcher.ARouter;
import com.wd.capability.router.data.ActionBean;
import com.wd.capability.router.data.RouterParameter;
/**
* 路由管理类
*
* @author shishuangxi_wd
* @date 2022/7/6 10:39
*/
public class WdRouterRule {
private static final int REQUEST_CODE = 20231112;
private static String TAG = "WdRouterRule";
private static WdRouterRule mInstance;
private Postcard postcard;
public Postcard getPostcard() {
return postcard;
}
private WdRouterRule() {
}
public static WdRouterRule getInstance() {
if (mInstance == null) {
mInstance = new WdRouterRule();
}
return mInstance;
}
/**
* 无传参时使用
*
* @param context
* @param path
*/
public void processAction(Context context, String path) {
ActionBean actionBean = new ActionBean();
actionBean.paramBean.pageID = path;
processAction(context, actionBean);
}
/**
* 有传参时 自定义 ActionBean
*
* @param context
* @param actionBean
*/
public void processAction(Context context, ActionBean actionBean) {
processAction(context, actionBean, -1);
}
/**
* 有传参时 自定义 ActionBean
*
* @param context
* @param actionBean
*/
public void processAction(Context context, ActionBean actionBean, int flag) {
processAction(context, actionBean, flag, REQUEST_CODE);
}
/**
* 有传参时 自定义 ActionBean
*
* @param context
* @param actionBean
*/
public void processAction(Context context, ActionBean actionBean, int flag, int requestCode) {
if (checkNull(actionBean)) {
Log.e(TAG, "传参 为空");
return;
}
try {
postcard = ARouter.getInstance().build(actionBean.paramBean.pageID);
if (postcard == null) {
Log.e(TAG, "postcard 为空");
return;
}
if (-1 != flag) {
postcard.withFlags(flag);
}
if (actionBean.enterAnim != -1 && actionBean.exitAnim != -1) {
postcard.withTransition(actionBean.enterAnim, actionBean.exitAnim);
}
postcard.withSerializable(RouterParameter.ACTION_KEY, actionBean);
if (actionBean.requestCode != 0) {
if (context instanceof Activity) {
postcard.navigation((Activity) context, actionBean.requestCode);
} else {
Log.e(TAG, "context is not activity");
}
} else {
if (context instanceof Activity) {
postcard.navigation((Activity) context, requestCode);
} else {
postcard.navigation();
}
}
} catch (Exception e) {
Log.e(TAG, e.toString());
}
}
/**
* 传参为bundle 类型
*/
public void processAction(Context context, String path, Bundle bundle) {
ActionBean actionBean = new ActionBean();
actionBean.paramBean.pageID = path;
processAction(context, actionBean, bundle);
}
/**
* 传参为json 和bundle 类型
*/
public void processAction(Context context, ActionBean actionBean, Bundle bundle) {
if (checkNull(actionBean)) {
Log.e(TAG, "传参 为空");
return;
}
try {
postcard = ARouter.getInstance().build(actionBean.paramBean.pageID);
if (postcard == null) {
Log.e(TAG, "postcard 为空");
return;
}
if (actionBean.enterAnim != -1 && actionBean.exitAnim != -1) {
postcard.withTransition(actionBean.enterAnim, actionBean.exitAnim);
}
postcard.withSerializable(RouterParameter.ACTION_KEY, actionBean);
if (bundle != null) {
postcard.withBundle(RouterParameter.ACTION_BUNDLE, bundle);
}
if (actionBean.requestCode != 0) {
if (context instanceof Activity) {
postcard.navigation((Activity) context, actionBean.requestCode);
} else {
Log.e(TAG, "context is not activity");
}
} else {
if (context instanceof Activity) {
postcard.navigation((Activity) context);
} else {
postcard.navigation();
}
}
} catch (Exception e) {
Log.e(TAG, e.toString());
}
}
/**
* 获取fragment实例
*/
public Fragment getFragment(String path) {
ActionBean actionBean = new ActionBean();
actionBean.paramBean.pageID = path;
return getFragment(actionBean);
}
/**
* 获取fragment实例
*/
public Fragment getFragment(ActionBean actionBean) {
Fragment navigation = null;
if (checkNull(actionBean)) {
return navigation;
}
try {
postcard = ARouter.getInstance().build(actionBean.paramBean.pageID);
if (postcard == null) {
return navigation;
}
postcard.withSerializable(RouterParameter.ACTION_KEY, actionBean);
navigation = (Fragment) postcard.navigation();
return navigation;
} catch (Exception e) {
Log.e(TAG, e.toString());
}
return navigation;
}
/**
* 获取provider实例
*/
public Object getProvider(Class clazz) {
return ARouter.getInstance().navigation(clazz);
}
/**
* 检测数据是否为空
*/
public boolean checkNull(ActionBean actionBean) {
if (actionBean == null) {
return true;
}
if (actionBean.paramBean == null) {
return true;
}
if (TextUtils.isEmpty(actionBean.paramBean.pageID)) {
return true;
}
return false;
}
}