MainJavaScriptInterface.java
8.87 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package com.studymachine.www.js;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Looper;
import android.webkit.JavascriptInterface;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.LifecycleOwner;
import com.blankj.utilcode.util.GsonUtils;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
import com.hjq.base.BaseDialog;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.OnDownloadListener;
import com.hjq.http.model.HttpMethod;
import com.hjq.toast.ToastUtils;
import com.studymachine.www.BuildConfig;
import com.studymachine.www.app.AppPresentation;
import com.studymachine.www.app.ModeHandler;
import com.studymachine.www.event.SwfEvent;
import com.studymachine.www.http.glide.GlideApp;
import com.studymachine.www.manager.GlobalParameterManager;
import com.studymachine.www.manager.UserManager;
import com.studymachine.www.other.AppConfig;
import com.studymachine.www.other.Tool;
import com.studymachine.www.other.WebUrlConfig;
import com.studymachine.www.ui.activity.AiDetailActivity;
import com.studymachine.www.ui.activity.AiListActivity;
import com.studymachine.www.ui.activity.BrowserActivity;
import com.studymachine.www.ui.activity.SwfActivity;
import com.studymachine.www.ui.dialog.WaitDialog;
import com.studymachine.www.widget.X5WebView;
import org.greenrobot.eventbus.EventBus;
import java.io.File;
import timber.log.Timber;
/**
* webView js
*/
public class MainJavaScriptInterface {
private Activity mContext;
private X5WebView mX5WebView;
private BaseDialog mDialog;
private String singPageData;
private ModeHandler handler;
private String time;
private boolean isSwfPlay = false;
private int mode;
public MainJavaScriptInterface(Activity context, X5WebView x5WebView) {
mContext = context;
mX5WebView = x5WebView;
}
public MainJavaScriptInterface(Activity context, X5WebView x5WebView, ModeHandler handler) {
mContext = context;
mX5WebView = x5WebView;
this.handler = handler;
}
/**
* 记录ai的开始时间
*
* @param time 格式化时间
*/
@JavascriptInterface
public void setTime(String time) {
this.time = time;
}
/**
* h5端结束的时候获取开始时间
*
* @return 时间
*/
@JavascriptInterface
public String getStartTime() {
return this.time;
}
/**
* 跳转到精准强化
*
* @param id pointId
*/
@JavascriptInterface
public void goAccurateStrengthen(String id) {
BrowserActivity.start(mContext, WebUrlConfig.accurateStrengthen(id));
mContext.finish();
}
/**
* 获取config数据
*
* @return 数据
*/
@JavascriptInterface
public String getConfigData() {
Timber.d(GsonUtils.toJson(GlobalParameterManager.getInstance().getConfigData()));
return GsonUtils.toJson(GlobalParameterManager.getInstance().getConfigData());
}
/**
* 获取ai下标的信息
*
* @param index 对应下标
* @return 数据
*/
@JavascriptInterface
public String getAiIndexData(String index) {
AiDetailActivity aiDetailActivity = (AiDetailActivity) mContext;
aiDetailActivity.setAiIndex(Integer.parseInt(index));
isSwfPlay = false;
return GsonUtils.toJson(aiDetailActivity.mPointDetail.getItems().get(Integer.parseInt(index)));
}
/**
* 获取ai的信息
*
* @return 数据
*/
@JavascriptInterface
public String getAiIndexData() {
isSwfPlay = false;
AiDetailActivity aiDetailActivity = (AiDetailActivity) mContext;
return GsonUtils.toJson(aiDetailActivity.mPointDetail);
}
/**
* 获取flash的背景
*
* @return 数据
*/
@JavascriptInterface
public String isSwfPlay() {
return isSwfPlay ? "1" : "2";
}
/**
* 播放swf
*
* @param url 地址
*/
@JavascriptInterface
public void playSwf(String url) {
//下载swf 到指定目录 直接跳转到播放页面
EasyHttp.download((LifecycleOwner) mContext)
.method(HttpMethod.GET)
.file(new File("/data/data/" + BuildConfig.APPLICATION_ID + "/cache/app/null/assets/", "cached.swf"))
.url(Tool.jointOSSPath(url))
.listener(new OnDownloadListener() {
@Override
public void onStart(File file) {
if (mDialog == null) {
mDialog = new WaitDialog.Builder(mContext)
.setCancelable(false)
.create();
}
if (!mDialog.isShowing()) {
mDialog.show();
}
}
@Override
public void onProgress(File file, int progress) {
}
@Override
public void onComplete(File file) {
mDialog.hide();
SwfActivity.start(mContext);
EventBus.getDefault().post(new SwfEvent());
isSwfPlay = true;
}
@Override
public void onError(File file, Exception e) {
mDialog.hide();
ToastUtils.show("预览错误" + e.getMessage());
}
@Override
public void onEnd(File file) {
}
}).start();
}
/**
* toast
*
* @param text 文本
*/
@JavascriptInterface
public void toast(String text) {
ToastUtils.show(text);
}
/**
* 准心切换
*
* @param mode 文本
*/
@JavascriptInterface
public void modeChange(int mode) {
handler.sendEmptyMessage(mode);
}
/**
* 查询是不是air
*/
@JavascriptInterface
public int isAir() {
return 1;
}
/**
* 打开其他app
*
* @param packageName 文本
*/
@JavascriptInterface
public void gotoApp(String packageName) {
if (packageName.equals("studyMachine")) {
AiListActivity.start(mContext);
return;
}
boolean hasApp = Tool.isApkInstalled(mContext, packageName);
if (!hasApp) {
ToastUtils.show("app不存在");
return;
}
Tool.startOtherApp(mContext, packageName);
}
/**
* H5获取token
*/
@JavascriptInterface
public String getToken() {
return UserManager.getInstance().getToken();
}
/**
* H5获取api 请求地址
*/
@JavascriptInterface
public String getBaseUrl() {
return AppConfig.getHostUrl();
}
/**
* H5获取 oss api 请求地址
*/
@JavascriptInterface
public String getBaseOssUrl() {
return AppConfig.getImagerHostUrl();
}
/**
* 返回
*/
@JavascriptInterface
public void onBackPressed() {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
if (mX5WebView.canGoBack()) {
// 后退网页并且拦截该事件
mX5WebView.goBack();
} else {
mContext.finish();
}
}
});
}
/**
* 设置title标题 (弃用)
*/
@JavascriptInterface
public void setTitle(String text) {
BrowserActivity Context = (BrowserActivity) mContext;
if (text.contains("http")) {
GlideApp.with(mContext)
.load(text)
.into(new CustomTarget<Drawable>() {
@Override
public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
Context.getTitleBar().setTitleIcon(resource);
}
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
}
});
} else {
Context.getTitleBar().setTitle(text);
}
}
/**
* 获取单页内容
*/
@JavascriptInterface
public String getSingPageData() {
return singPageData;
}
/**
* 设置单页内容
*
* @param singPageData
*/
public void setSingPageData(String singPageData) {
this.singPageData = singPageData;
}
/**
* 关闭H5页面
*/
@JavascriptInterface
public void finishH5Page() {
mContext.finish();
}
}