-
Notifications
You must be signed in to change notification settings - Fork 176
/
DefaultNavigatorAdapter.java
201 lines (172 loc) · 6.44 KB
/
DefaultNavigatorAdapter.java
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
package com.didi.hummer.adapter.navigator.impl;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import com.didi.hummer.HummerSDK;
import com.didi.hummer.adapter.navigator.INavigatorAdapter;
import com.didi.hummer.adapter.navigator.NavCallback;
import com.didi.hummer.adapter.navigator.NavPage;
import com.didi.hummer.adapter.navigator.impl.router.ActivityLauncher;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* 默认页面导航适配器
*
* Created by XiaoFeng on 2019-12-24.
*/
public class DefaultNavigatorAdapter implements INavigatorAdapter {
public static final String EXTRA_PAGE_ID = "PAGE_ID";
public static final String EXTRA_PAGE_MODEL = "PAGE_MODEL";
public static final String SCHEME_HUMMER = "hummer";
public static final String SCHEME_HTTP = "http";
public static final String SCHEME_HTTPS = "https";
protected IntentCreator intentCreator;
public DefaultNavigatorAdapter() {
this(null);
}
public DefaultNavigatorAdapter(IntentCreator intentCreator) {
this.intentCreator = intentCreator;
if (this.intentCreator == null) {
this.intentCreator = new DefaultIntentCreator();
}
}
@Override
public void openPage(Context context, NavPage page, NavCallback callback) {
if (context == null) {
context = HummerSDK.appContext;
}
goToPage(context, page, callback);
}
@Override
public void popPage(Context context, NavPage page) {
Activity popActivity = null;
if (page != null && !TextUtils.isEmpty(page.id)) {
popActivity = ActivityStackManager.getInstance().getActivity(page.id);
} else if (context instanceof Activity) {
popActivity = (Activity) context;
}
if (popActivity == null) {
return;
}
popActivity.finish();
// 无动画
if (page != null && !page.animated) {
popActivity.overridePendingTransition(0, 0);
}
}
@Override
public void popToPage(Context context, NavPage page) {
if (page == null) {
return;
}
ActivityStackManager.getInstance().popToActivity(page.id, page.animated);
}
@Override
public void popToRootPage(Context context, NavPage page) {
boolean animated = page == null || page.animated;
ActivityStackManager.getInstance().popToRootActivity(animated);
}
@Override
public void popBack(Context context, int count, NavPage page) {
if (count == 1) {
popPage(context, page);
} else {
boolean animated = page == null || page.animated;
ActivityStackManager.getInstance().popBack(count, animated);
}
}
protected void goToPage(Context context, NavPage page, NavCallback callback) {
if (context == null || page == null || TextUtils.isEmpty(page.url)) {
return;
}
String scheme = page.getScheme();
if (!TextUtils.isEmpty(scheme)) {
scheme = scheme.toLowerCase();
switch (scheme) {
case SCHEME_HUMMER:
openHummerPage(context, page, callback);
break;
case SCHEME_HTTP:
case SCHEME_HTTPS:
if (page.isJsUrl()) {
openHummerPage(context, page, callback);
} else {
openWebPage(context, page, callback);
}
break;
default:
if (page.isJsUrl()) {
openHummerPage(context, page, callback);
} else {
openCustomPage(context, page, callback);
}
break;
}
} else if (page.isJsUrl()) {
openHummerPage(context, page, callback);
}
}
protected void openHummerPage(Context context, NavPage page, NavCallback callback) {
Intent intent = intentCreator.createHummerIntent(context, page);
openPage(context, intent, page, callback);
}
protected void openWebPage(Context context, NavPage page, NavCallback callback) {
Intent intent = intentCreator.createWebIntent(context, page);
openPage(context, intent, page, callback);
}
protected void openCustomPage(Context context, NavPage page, NavCallback callback) {
Intent intent = intentCreator.createCustomIntent(context, page);
openPage(context, intent, page, callback);
}
protected void openPage(Context context, Intent intent, NavPage page, NavCallback callback) {
if (intent == null) {
return;
}
if (context instanceof Application) {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
if (page.closeSelf) {
// 如果需要关闭自身,直接启动Activity,不接收回调
context.startActivity(intent);
} else {
// 如果不关闭自身,启动Activity时需要接收回调
ActivityLauncher.init(context).startActivityForResult(intent, (resultCode, data) -> {
Map<String, Object> result = transIntentData(data);
if (callback != null && result != null) {
callback.onResult(result);
}
});
}
// 启动Activity无动画
if (!page.animated && context instanceof Activity) {
((Activity) context).overridePendingTransition(0,0);
}
// 关闭自身页面
if (page.closeSelf && context instanceof Activity) {
((Activity) context).finish();
}
}
protected Map<String, Object> transIntentData(Intent intent) {
Map<String, Object> data = null;
try {
if (intent != null && intent.getExtras() != null) {
Bundle bundle = intent.getExtras();
data = new HashMap<>();
Set<String> keys = bundle.keySet();
for (String key : keys) {
Object value = bundle.get(key);
if (value != null) {
data.put(key, value);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return data;
}
}