forked from Eanya-Tonic/CCTV_Viewer
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMainActivity.java
More file actions
1007 lines (869 loc) · 44.9 KB
/
MainActivity.java
File metadata and controls
1007 lines (869 loc) · 44.9 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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package com.eanyatonic.cctvViewer;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.webkit.JavascriptInterface;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.eanyatonic.cctvViewer.bean.EpgInfo;
import com.eanyatonic.cctvViewer.tools.FileTool;
import com.eanyatonic.cctvViewer.tools.FileUtils;
import com.eanyatonic.cctvViewer.tools.SysTool;
import com.tencent.smtt.export.external.TbsCoreSettings;
import com.tencent.smtt.export.external.interfaces.IX5WebChromeClient;
import com.tencent.smtt.export.external.interfaces.SslError;
import com.tencent.smtt.export.external.interfaces.SslErrorHandler;
import com.tencent.smtt.sdk.CookieManager;
import com.tencent.smtt.sdk.CookieSyncManager;
import com.tencent.smtt.sdk.QbSdk;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity {
public FileTool filetool;
private RecyclerView recyclerView;
private View mCustomView;
private FrameLayout mFrameLayout;
private IX5WebChromeClient.CustomViewCallback mCustomViewCallback;
private com.tencent.smtt.sdk.WebView webView; // 导入 X5 WebView
private com.tencent.smtt.sdk.WebView cctvFinishedView;
private String aach;
private ChannelAdapter channelAdapter;
private final String[] liveUrls = {
"https://tv.cctv.com/live/cctv1/",
"https://tv.cctv.com/live/cctv2/",
"https://tv.cctv.com/live/cctv3/",
"https://tv.cctv.com/live/cctv4/",
"https://tv.cctv.com/live/cctv5/",
"https://tv.cctv.com/live/cctv6/",
"https://tv.cctv.com/live/cctv7/",
"https://tv.cctv.com/live/cctv8/",
"https://tv.cctv.com/live/cctvjilu",
"https://tv.cctv.com/live/cctv10/",
"https://tv.cctv.com/live/cctv11/",
"https://tv.cctv.com/live/cctv12/",
"https://tv.cctv.com/live/cctv13/",
"https://tv.cctv.com/live/cctvchild",
"https://tv.cctv.com/live/cctv15/",
"https://tv.cctv.com/live/cctv16/",
"https://tv.cctv.com/live/cctv17/",
"https://tv.cctv.com/live/cctv5plus/",
"https://tv.cctv.com/live/cctveurope",
"https://tv.cctv.com/live/cctvamerica/",
"https://www.yangshipin.cn/tv/home?pid=600002264",
"https://www.yangshipin.cn/tv/home?pid=600001859",
"https://www.yangshipin.cn/tv/home?pid=600001800",
"https://www.yangshipin.cn/tv/home?pid=600001801",
"https://www.yangshipin.cn/tv/home?pid=600001814",
"https://www.yangshipin.cn/tv/home?pid=600001818",
"https://www.yangshipin.cn/tv/home?pid=600001817",
"https://www.yangshipin.cn/tv/home?pid=600001802",
"https://www.yangshipin.cn/tv/home?pid=600004092",
"https://www.yangshipin.cn/tv/home?pid=600001803",
"https://www.yangshipin.cn/tv/home?pid=600004078",
"https://www.yangshipin.cn/tv/home?pid=600001805",
"https://www.yangshipin.cn/tv/home?pid=600001806",
"https://www.yangshipin.cn/tv/home?pid=600001807",
"https://www.yangshipin.cn/tv/home?pid=600001811",
"https://www.yangshipin.cn/tv/home?pid=600001809",
"https://www.yangshipin.cn/tv/home?pid=600001815",
"https://www.yangshipin.cn/tv/home?pid=600098637",
"https://www.yangshipin.cn/tv/home?pid=600099502",
"https://www.yangshipin.cn/tv/home?pid=600001810",
"https://www.yangshipin.cn/tv/home?pid=600014550",
"https://www.yangshipin.cn/tv/home?pid=600084704",
"https://www.yangshipin.cn/tv/home?pid=600084758",
"https://www.yangshipin.cn/tv/home?pid=600084782",
"https://www.yangshipin.cn/tv/home?pid=600084744",
"https://www.yangshipin.cn/tv/home?pid=600084781",
"https://www.yangshipin.cn/tv/home?pid=600099658",
"https://www.yangshipin.cn/tv/home?pid=600099655",
"https://www.yangshipin.cn/tv/home?pid=600099620",
"https://www.yangshipin.cn/tv/home?pid=600099637",
"https://www.yangshipin.cn/tv/home?pid=600099660",
"https://www.yangshipin.cn/tv/home?pid=600099649",
"https://www.yangshipin.cn/tv/home?pid=600099636",
"https://www.yangshipin.cn/tv/home?pid=600099659",
"https://www.yangshipin.cn/tv/home?pid=600099650",
"https://www.yangshipin.cn/tv/home?pid=600099653",
"https://www.yangshipin.cn/tv/home?pid=600099652",
"https://www.yangshipin.cn/tv/home?pid=600099656",
"https://www.yangshipin.cn/tv/home?pid=600099651",
"https://www.yangshipin.cn/tv/home?pid=600002309",
"https://www.yangshipin.cn/tv/home?pid=600002521",
"https://www.yangshipin.cn/tv/home?pid=600002483",
"https://www.yangshipin.cn/tv/home?pid=600002520",
"https://www.yangshipin.cn/tv/home?pid=600002475",
"https://www.yangshipin.cn/tv/home?pid=600002508",
"https://www.yangshipin.cn/tv/home?pid=600002485",
"https://www.yangshipin.cn/tv/home?pid=600002509",
"https://www.yangshipin.cn/tv/home?pid=600002498",
"https://www.yangshipin.cn/tv/home?pid=600002506",
"https://www.yangshipin.cn/tv/home?pid=600002531",
"https://www.yangshipin.cn/tv/home?pid=600002481",
"https://www.yangshipin.cn/tv/home?pid=600002516",
"https://www.yangshipin.cn/tv/home?pid=600002525",
"https://www.yangshipin.cn/tv/home?pid=600002484",
"https://www.yangshipin.cn/tv/home?pid=600002490",
"https://www.yangshipin.cn/tv/home?pid=600002503",
"https://www.yangshipin.cn/tv/home?pid=600002505",
"https://www.yangshipin.cn/tv/home?pid=600002532",
"https://www.yangshipin.cn/tv/home?pid=600002493",
"https://www.yangshipin.cn/tv/home?pid=600002513",
};
/**
* for (let index = 0; index < document.querySelector(".tv-main-con-r-list-left").parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.__vue__._data.tabB.length; index++) {
* console.log(document.querySelector(".tv-main-con-r-list-left").parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.__vue__._data.tabB[index].pid)
* }
*/
private String[] channelNames = {
"CCTV-1 综合",
"CCTV-2 财经",
"CCTV-3 综艺",
"CCTV-4 中文国际",
"CCTV-5 体育",
"CCTV-6 电影",
"CCTV-7 军事农业",
"CCTV-8 电视剧",
"CCTV-9 纪录",
"CCTV-10 科教",
"CCTV-11 戏曲",
"CCTV-12 社会与法",
"CCTV-13 新闻",
"CCTV-14 少儿",
"CCTV-15 音乐",
"CCTV-16 奥林匹克",
"CCTV-17 农业农村",
"CCTV-5+ 体育赛事",
"CCTV 欧洲",
"CCTV 美国",
"CCTV4K",
"CCTV1",
"CCTV2",
"CCTV3",
"CCTV4",
"CCTV5",
"CCTV5+",
"CCTV6",
"CCTV7",
"CCTV8",
"CCTV9",
"CCTV10",
"CCTV11",
"CCTV12",
"CCTV13",
"CCTV14",
"CCTV15",
"CCTV16-HD",
"CCTV16(4K)",
"CCTV17",
"CGTN",
"CGTN法语频道",
"CGTN俄语频道",
"CGTN阿拉伯语频道",
"CGTN西班牙语频道",
"CGTN外语纪录频道",
"CCTV风云剧场频道",
"CCTV第一剧场频道",
"CCTV怀旧剧场频道",
"CCTV世界地理频道",
"CCTV风云音乐频道",
"CCTV兵器科技频道",
"CCTV风云足球频道",
"CCTV高尔夫·网球频道",
"CCTV女性时尚频道",
"CCTV央视文化精品频道",
"CCTV央视台球频道",
"CCTV电视指南频道",
"CCTV卫生健康频道",
"北京卫视",
"江苏卫视",
"东方卫视",
"浙江卫视",
"湖南卫视",
"湖北卫视",
"广东卫视",
"广西卫视",
"黑龙江卫视",
"海南卫视",
"重庆卫视",
"深圳卫视",
"四川卫视",
"河南卫视",
"福建东南卫视",
"贵州卫视",
"江西卫视",
"辽宁卫视",
"安徽卫视",
"河北卫视",
"山东卫视"
};
private int currentLiveIndex;
private static final String PREF_NAME = "MyPreferences";
private static final String PREF_KEY_LIVE_INDEX = "currentLiveIndex";
private boolean doubleBackToExitPressedOnce = false;
private final StringBuilder digitBuffer = new StringBuilder(); // 用于缓存按下的数字键
private static final long DIGIT_TIMEOUT = 3000; // 超时时间(毫秒)
private TextView inputTextView; // 用于显示正在输入的数字的 TextView
// 初始化透明的View
private View loadingOverlay;
// 频道显示view
private TextView overlayTextView;
private String info = "";
public List<EpgInfo> epgList = new ArrayList<>();
private String getYSPToken() {
// 在需要发送请求的方法中添加以下代码
OkHttpClient client = new OkHttpClient();
// 构建请求 URL
HttpUrl.Builder urlBuilder = HttpUrl.parse("https://lyrics.run/my-tv/v1/info").newBuilder();
String url = urlBuilder.build().toString();
Request request = new Request.Builder()
.url(url)
.build();
try (Response response = client.newCall(request).execute()) {
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
// 获取 JSON 字符串并解析为对象
String json = response.body().string();
JSONObject jsonObject = new JSONObject(json);
// 根据 JSON 的层级结构,逐级提取数据
return jsonObject.getJSONObject("data").getString("token");
} catch (JSONException | IOException e) {
throw new RuntimeException(e);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d("xxx",liveUrls.length+"xxxx"+channelNames.length);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mFrameLayout = findViewById(R.id.flVideoContainer);
// X5WebView初始化
initX5WebView();
Toast.makeText(MainActivity.this, "x5内核状态" + QbSdk.canLoadX5(getApplicationContext()) + "内核架构" + SysTool.showSysAach() + ",若无法播放请重启应用", Toast.LENGTH_SHORT).show();
// 初始化 WebView
webView = findViewById(R.id.webView);
// 初始化显示正在输入的数字的 TextView
inputTextView = findViewById(R.id.inputTextView);
// 初始化 loadingOverlay
loadingOverlay = findViewById(R.id.loadingOverlay);
// 初始化 overlayTextView
overlayTextView = findViewById(R.id.overlayTextView);
// 加载上次保存的位置
loadLastLiveIndex();
// 添加自动化调试
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// WebView.setWebContentsDebuggingEnabled(true);
// Log.d("remote debug", "远程调试");
// }
// 配置 WebView 设置
// filetool = new FileTool(this);
// String backwardScript =filetool.readFileContent("js/backwardScript.js");
//
// String forwardScript =filetool.readFileContent("js/forwardScript.js");
//
// String cctvOpenScript =filetool.readFileContent("js/getEpgScript.js");
webView.addJavascriptInterface(this, "bridge");
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
// 添加自动播放视频
webSettings.setMediaPlaybackRequiresUserGesture(false);
webSettings.setDatabaseEnabled(true);
webSettings.setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36");
webSettings.setUseWideViewPort(true);
// 启用 JavaScript 自动点击功能
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setMixedContentMode(WebSettings.LOAD_NORMAL);
// 设置 WebViewClient 和 WebChromeClient
webView.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedSslError(WebView webView, SslErrorHandler handler, SslError error) {
handler.proceed(); // 忽略 SSL 错误
}
// 设置 WebViewClient,监听页面加载完成事件
@Override
public void onPageFinished(WebView view, String url) {
cctvFinishedView = view;
info = "";
if (url.contains("cctv.com")){
// 获取节目预告和当前节目
view.evaluateJavascript("document.querySelector('#jiemu > li.cur.act').innerText", value -> {
// 处理获取到的元素值
if (!value.equals("null") && !value.isEmpty()) {
String elementValueNow = value.replace("\"", ""); // 去掉可能的引号
info += elementValueNow + "\n";
}
});
view.evaluateJavascript("document.querySelector('#jiemu > li:nth-child(4)').innerText", value -> {
// 处理获取到的元素值
if (!value.equals("null") && !value.isEmpty()) {
String elementValueNext = value.replace("\"", ""); // 去掉可能的引号
info += elementValueNext;
}
});
view.evaluateJavascript("""
{
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
let interval = setInterval(async function () {
console.log('页面加载完成!');
// 休眠 1000 毫秒(1秒)
await sleep(1000);
// 休眠 50 毫秒
await sleep(50);
console.log('获取节目单');
var epg_list = [
{ name: "", id: "" },
];
var epg_child = document.querySelector("#epg_player").childNodes;
for (let index = 0; index < epg_child.length; index++) {
if (index % 2 == 1) {
epg_list.push({ name: epg_child[index].innerText, id: epg_child[index].id });
}
}
let filteredArray = epg_list.map(obj => {
Object.keys(obj).forEach(key => obj[key] === '' || obj[key] === null ? delete obj[key] : '');
return obj;
}).filter(obj => Object.keys(obj).length > 0);
console.log(JSON.stringify(filteredArray));
clearInterval(interval);
bridge.setCctvEpgInfo(JSON.stringify(filteredArray));
}, 3000);
}
""", null);
view.evaluateJavascript("""
{
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
// 页面加载完成后执行 JavaScript 脚本
let interval = setInterval(async function executeScript() {
console.log('页面加载完成!');
// 休眠 1000 毫秒(1秒)
await sleep(1000);
// 休眠 50 毫秒
await sleep(50);
console.log('点击分辨率按钮');
if(document.querySelector('#resolution_item_720_player')===null){
var elem = document.querySelector("#resolution_item_480_player")
elem.click();
}else{
var elem = document.querySelector('#resolution_item_720_player');
elem.click();
}
// 休眠 50 毫秒
await sleep(50);
console.log('设置音量并点击音量按钮');
var btn = document.querySelector('#player_sound_btn_player');
btn.setAttribute('volume', 100);
btn.click();
btn.click();
btn.click();
document.querySelector("#player_sound_player").style.display = 'none'
// 休眠 50 毫秒
await sleep(50);
console.log('点击全屏按钮');
var fullscreenBtn = document.querySelector('#player_pagefullscreen_yes_player');
fullscreenBtn.click();
clearInterval(interval);
}, 3000);
}
""", null);
} else if (url.contains("yangshipin")) {
view.evaluateJavascript("""
{
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
// 页面加载完成后执行 JavaScript 脚本
let interval = setInterval(async function () {
await sleep(1050);
var cur;
if(document.querySelector('.tv-main-con-r-list-left-imgb.tvSelect')===null){
// cctv频道当前频道查询
cur = document.querySelector(".tv-main-con-r-list-left-imga.tvSelect").innerText.replace(/[\\s\\n]/g, '')
}else{
// 地方频道当前频道查询
cur = document.querySelector('.tv-main-con-r-list-left-imgb.tvSelect').innerText.replace(/[\\s\\n]/g, '')
}
console.log(cur)
clearInterval(interval)
}, 3000);
}
""",null);
view.evaluateJavascript("""
{
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
let interval = setInterval(async function () {
await sleep(1050);
console.log('设置音量并点击音量按钮');
document.querySelector(".container").__vue__.volume = 1
await sleep(50);
if(document.querySelector(".voice.on").style.display === 'none'){
await sleep(50);
document.querySelector(".container").__vue__.changeVolume()
}
console.log('点击ysp全屏按钮');
document.querySelector(".videoFull").click()
clearInterval(interval);
}, 3000);
}
""", null);
}
new Handler().postDelayed(() -> {
loadingOverlay.setVisibility(View.GONE);
showOverlay(channelNames[currentLiveIndex] + "\n" + info);
channelAdapter.notifyDataSetChanged();
}, 3000);
}
});
// 设置 WebView 客户端
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onShowCustomView(View view, IX5WebChromeClient.CustomViewCallback callback) {
Log.d("onShowCustomView", "onShowCustomView");
super.onShowCustomView(view, callback);
if (mCustomView != null) {
callback.onCustomViewHidden();
return;
}
mCustomView = view;
mFrameLayout.addView(mCustomView);
mCustomViewCallback = callback;
webView.setVisibility(View.GONE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
@Override
public void onHideCustomView() {
Log.d("onHideCustomView", "onHideCustomView");
webView.setVisibility(View.VISIBLE);
if (mCustomView == null) {
return;
}
mCustomView.setVisibility(View.GONE);
mFrameLayout.removeView(mCustomView);
mCustomViewCallback.onCustomViewHidden();
mCustomView = null;
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
super.onHideCustomView();
}
});
// 禁用缩放
webSettings.setSupportZoom(false);
webSettings.setBuiltInZoomControls(false);
webSettings.setDisplayZoomControls(false);
// 在 Android TV 上,需要禁用焦点自动导航
webView.setFocusable(false);
// 加载初始网页
webView.setBackgroundColor(Color.TRANSPARENT);
webView.setLayoutParams(new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT));
recyclerView = findViewById(R.id.recyclerView);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MainActivity.this, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setItemAnimator(null);
epgList.add(new EpgInfo("暂无节目单", "暂无节目单"));
channelAdapter = new ChannelAdapter(epgList, recyclerView, webView);
recyclerView.setAdapter(channelAdapter);
loadLiveUrl();
// new Handler().postDelayed(() -> {
// RelativeLayout mainView = findViewById(R.id.main_browse_fragment);
// if(mainView != null) {
// mainView.removeView(cctvFinishedView);
// }
// mFrameLayout.addView(cctvFinishedView);
// }, 6000);
startPeriodicTask();
}
private final Handler handler = new Handler();
// 启动自动播放定时任务
private void startPeriodicTask() {
// 使用 postDelayed 方法设置定时任务
handler.postDelayed(periodicTask, 5000); // 2000 毫秒,即 2 秒钟
}
// 定时任务具体操作
private final Runnable periodicTask = new Runnable() {
@Override
public void run() {
// 获取 div 元素的 display 属性,并执行相应的操作
getDivDisplayPropertyAndDoSimulateTouch();
// 完成后再次调度定时任务
handler.postDelayed(this, 5000); // 2000 毫秒,即 2 秒钟
}
};
// 获取 div 元素的 display 属性并执行相应的操作
private void getDivDisplayPropertyAndDoSimulateTouch() {
if (webView != null) {
if(currentLiveIndex>=getCCTVHeadOffset()&¤tLiveIndex<=getCCTVTailOffset()){
webView.evaluateJavascript("document.getElementById('play_or_pause_play_player').style.display", value -> {
// 处理获取到的 display 属性值
if (value.equals("\"block\"")) {
// 执行点击操作
simulateTouch(webView, 0.5f, 0.5f);
}
});
} else if (currentLiveIndex>=getYSPHeadOffset()&¤tLiveIndex<=getYSPTailOffset()){
String scriptPlay =
"""
if(!document.querySelector(".container").__vue__.playStatus.isPaused){
document.querySelector(".container").__vue__.togglePlay()
}
""";
webView.evaluateJavascript(scriptPlay, null);
}
}
}
private void initX5WebView() {
// 在调用TBS初始化、创建WebView之前进行如下配置2
boolean canLoadX5 = QbSdk.canLoadX5(getApplicationContext());
if (!canLoadX5) {
tbsInstall();
}
}
private void tbsInstall() {
HashMap<String, Object> map = new HashMap<>(2);
map.put(TbsCoreSettings.TBS_SETTINGS_USE_SPEEDY_CLASSLOADER, true);
map.put(TbsCoreSettings.TBS_SETTINGS_USE_DEXLOADER_SERVICE, true);
QbSdk.initTbsSettings(map);
aach = SysTool.showSysAach();
if (aach.contains("arm64")) {
FileUtils.copyAssets(getApplicationContext(), "046279_arm64v8a_x5.tbs.apk", FileUtils.getTBSFileDir(getApplicationContext()).getPath() + "/046279_arm64v8a_x5.tbs.apk");
QbSdk.reset(getApplicationContext());
QbSdk.installLocalTbsCore(getApplicationContext(), 46279, FileUtils.getTBSFileDir(getApplicationContext()).getPath() + "/046279_arm64v8a_x5.tbs.apk");
} else if (aach.contains("armeabi")) {
FileUtils.copyAssets(getApplicationContext(), "046914_armeabi_x5.tbs.apk", FileUtils.getTBSFileDir(getApplicationContext()).getPath() + "/046914_armeabi_x5.tbs.apk");
QbSdk.reset(getApplicationContext());
QbSdk.installLocalTbsCore(getApplicationContext(), 46914, FileUtils.getTBSFileDir(getApplicationContext()).getPath() + "/046914_armeabi_x5.tbs.apk");
}
Log.e("canloadX5", "canLoadX5: " + QbSdk.canLoadX5(getApplicationContext()) + "|TbsVersion:" + QbSdk.getTbsVersion(getApplicationContext()));
}
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (!recyclerView.hasFocus() && event.getAction() == KeyEvent.ACTION_DOWN) {
cctvFinishedView.evaluateJavascript(
"""
function simulate(element, eventName) {
var options = extend(defaultOptions, arguments[2] || {});
var oEvent, eventType = null;
for (var name in eventMatchers) {
if (eventMatchers[name].test(eventName)) {
eventType = name;
break;
}
}
if (!eventType) throw new SyntaxError('Only HTMLEvents and MouseEvents interfaces are supported');
if (document.createEvent) {
oEvent = document.createEvent(eventType);
if (eventType == 'HTMLEvents') {
oEvent.initEvent(eventName, options.bubbles, options.cancelable);
} else {
oEvent.initMouseEvent(eventName, options.bubbles, options.cancelable, document.defaultView, options.button, options.pointerX, options.pointerY, options.pointerX, options.pointerY, options.ctrlKey, options.altKey, options.shiftKey, options.metaKey, options.button, element);
}
element.dispatchEvent(oEvent);
} else {
options.clientX = options.pointerX;
options.clientY = options.pointerY;
var evt = document.createEventObject();
oEvent = extend(evt, options);
element.fireEvent('on' + eventName, oEvent);
}
return element;
}
function extend(destination, source) {
for (var property in source) destination[property] = source[property];
return destination;
}
var eventMatchers = {
'HTMLEvents': /^(?:load|unload|abort|error|select|change|submit|reset|focus|blur|resize|scroll)$/,
'MouseEvents': /^(?:click|dblclick|mouse(?:down|up|over|move|out))$/
}
var defaultOptions = {
pointerX: 0,
pointerY: 0,
button: 0,
ctrlKey: false,
altKey: false,
shiftKey: false,
metaKey: false,
bubbles: true,
cancelable: true
}
function triggerMouseEvent (node, eventType) {
var clickEvent = document.createEvent ('MouseEvents');
clickEvent.initEvent (eventType, true, true);
node.dispatchEvent (clickEvent);
};
async function mouseDragStart(node) {
console.log("Starting drag...");
triggerMouseEvent(node, "mousedown")
}
var progress = null;
// 200 -1000
async function mouseDragEnd(node,x,y){
console.log("Ending drag...");
await sleep(500)
simulate(node, "mousemove",{pointerX: x-30, pointerY: y})
await sleep(500)
simulate(node, "mouseup" , {pointerX: x-30, pointerY: y})
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function playback(offset){
document.querySelector('#play_or_plause_player').click();
await sleep(500);
const targetElement = document.querySelector("#timeshift_pointer_player")
const xy = document.querySelector("#timeshift_pointer_player").getClientRects()[0]
console.log(xy)
mouseDragStart(targetElement);
mouseDragEnd(targetElement,xy.x+offset,xy.y);
};
"""
, null);
if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP || event.getKeyCode() == KeyEvent.KEYCODE_DPAD_DOWN || event.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT || event.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT || event.getKeyCode() == KeyEvent.KEYCODE_ENTER || event.getKeyCode() == KeyEvent.KEYCODE_DPAD_CENTER || event.getKeyCode() == KeyEvent.KEYCODE_MENU) {
if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP) {
// 执行上一个直播地址的操作
navigateToPreviousLive();
return true; // 返回 true 表示事件已处理,不传递给 WebView
} else if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_DOWN) {
// 执行下一个直播地址的操作
navigateToNextLive();
return true; // 返回 true 表示事件已处理,不传递给 WebView
} else if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER || event.getKeyCode() == KeyEvent.KEYCODE_DPAD_CENTER) {
// 执行暂停操作
simulateTouch(webView, 0.5f, 0.5f);
return true; // 返回 true 表示事件已处理,不传递给 WebView
} else if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT) {
cctvFinishedView.evaluateJavascript(
"""
{playback(-30);}
"""
, null);
return true;
} else if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT) {
cctvFinishedView.evaluateJavascript(
"""
{playback(60);}
""", null);
return true;
} else if (event.getKeyCode() == KeyEvent.KEYCODE_MENU) {
recyclerView.setVisibility(View.VISIBLE);
recyclerView.requestFocus();
return false; // 返回 true 表示事件已处理,不传递给 WebView
}
return true; // 返回 true 表示事件已处理,不传递给 WebView
} else if (event.getKeyCode() >= KeyEvent.KEYCODE_0 && event.getKeyCode() <= KeyEvent.KEYCODE_9) {
int numericKey = event.getKeyCode() - KeyEvent.KEYCODE_0;
// 将按下的数字键追加到缓冲区
digitBuffer.append(numericKey);
// 使用 Handler 来在超时后处理输入的数字
new Handler().postDelayed(this::handleNumericInput, DIGIT_TIMEOUT);
// 更新显示正在输入的数字的 TextView
updateInputTextView();
return true; // 事件已处理,不传递给 WebView
}
}
return super.dispatchKeyEvent(event); // 如果不处理,调用父类的方法继续传递事件
}
private void handleNumericInput() {
// 将缓冲区中的数字转换为整数
if (digitBuffer.length() > 0) {
int numericValue = Integer.parseInt(digitBuffer.toString());
// 检查数字是否在有效范围内
if (numericValue > 0 && numericValue <= liveUrls.length) {
currentLiveIndex = numericValue - 1;
loadLiveUrl();
saveCurrentLiveIndex(); // 保存当前位置
}
// 重置缓冲区
digitBuffer.setLength(0);
// 取消显示正在输入的数字
inputTextView.setVisibility(View.INVISIBLE);
}
}
@SuppressLint("SetTextI18n")
private void updateInputTextView() {
// 在 TextView 中显示当前正在输入的数字
inputTextView.setVisibility(View.VISIBLE);
inputTextView.setText("换台:" + digitBuffer.toString());
}
private void loadLastLiveIndex() {
SharedPreferences preferences = getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
currentLiveIndex = preferences.getInt(PREF_KEY_LIVE_INDEX, 0); // 默认值为0
loadLiveUrl(); // 加载上次保存的位置的直播地址
}
private void saveCurrentLiveIndex() {
SharedPreferences preferences = getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt(PREF_KEY_LIVE_INDEX, currentLiveIndex);
editor.apply();
}
private void loadLiveUrl() {
if (currentLiveIndex>=getCCTVHeadOffset()&¤tLiveIndex<=getCCTVTailOffset()){
webView.setInitialScale(getMinimumScale());
webView.loadUrl(liveUrls[currentLiveIndex]);
}
if (currentLiveIndex>=getYSPHeadOffset()&¤tLiveIndex<=getYSPTailOffset()){
// 创建CookieManager对象
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
cookieManager.setAcceptThirdPartyCookies(webView, true);
Executor myExecutor = Executors.newSingleThreadExecutor();
// cookieManager.removeExpiredCookie();
// cookieManager.removeAllCookie();
// cookieManager.removeSessionCookie();
myExecutor.execute(() -> {
cookieManager.setCookie(".yangshipin.cn", "yspopenid=vu0-8lgGV2LW9QjDeuBFsX8yMnzs37Q3_HZF6XyVDpGR_I;Domain=.yangshipin.cn;Path=/");
cookieManager.setCookie(".yangshipin.cn", "vusession="+getYSPToken()+";Domain=.yangshipin.cn;Path=/");
});
// 将Cookie同步到WebView
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
cookieManager.flush();
} else {
CookieSyncManager.getInstance().sync();
}
webView.setInitialScale(getMinimumScale());
webView.loadUrl(liveUrls[currentLiveIndex]);
new Handler().postDelayed(() -> {
if(webView != null) {
webView.setInitialScale(getMinimumScale());
webView.reload();
}
}, 1000);
}
}
private int getCCTVHeadOffset(){
for (int i = 0; i < liveUrls.length; i++) {
if (liveUrls[i].contains("cctv.com")){
return i;
}
}
return 0;
}
private int getCCTVTailOffset(){
for (int i = liveUrls.length - 1; i >= 0; i--) {
if (liveUrls[i].contains("cctv.com")){
return i;
}
}
return liveUrls.length-1;
}
private int getYSPHeadOffset(){
for (int i = 0; i < liveUrls.length; i++) {
if (liveUrls[i].contains("yangshipin.cn")){
return i;
}
}
return 0;
}
private int getYSPTailOffset(){
for (int i = liveUrls.length - 1; i >= 0; i--) {
if (liveUrls[i].contains("yangshipin.cn")){
return i;
}
}
return liveUrls.length-1;
}
private void navigateToPreviousLive() {
currentLiveIndex = (currentLiveIndex - 1 + liveUrls.length) % liveUrls.length;
loadLiveUrl();
saveCurrentLiveIndex();
}
private void navigateToNextLive() {
currentLiveIndex = (currentLiveIndex + 1) % liveUrls.length;
loadLiveUrl();
saveCurrentLiveIndex();
}
private int getMinimumScale() {
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int screenWidth = displayMetrics.widthPixels;
int screenHeight = displayMetrics.heightPixels;
// 计算缩放比例,使用 double 类型进行计算
double scale = Math.min((double) screenWidth / 3840.0, (double) screenHeight / 2160.0) * 100;
// 四舍五入并转为整数
return (int) Math.round(scale);
}
// 在需要模拟触摸的地方调用该方法
public void simulateTouch(View view, float x, float y) {
long downTime = SystemClock.uptimeMillis();
long eventTime = SystemClock.uptimeMillis() + 100;
// 构造 ACTION_DOWN 事件
MotionEvent downEvent = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, x, y, 0);
view.dispatchTouchEvent(downEvent);
// 构造 ACTION_UP 事件
MotionEvent upEvent = MotionEvent.obtain(downTime, eventTime + 100, MotionEvent.ACTION_UP, x, y, 0);
view.dispatchTouchEvent(upEvent);
// 释放事件对象
downEvent.recycle();
upEvent.recycle();
}
@Override
public void onBackPressed() {
if (recyclerView.getVisibility() == View.VISIBLE) {
recyclerView.setVisibility(View.GONE);
} else {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "再按一次返回键退出应用", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(() -> doubleBackToExitPressedOnce = false, 2000);
}
// 如果两秒内再次按返回键,则退出应用
}
private void showOverlay(String channelInfo) {
// 设置覆盖层内容
overlayTextView.setText(channelInfo);
findViewById(R.id.overlayTextView).setVisibility(View.VISIBLE);
// 使用 Handler 延时隐藏覆盖层
new Handler().postDelayed(() -> {
findViewById(R.id.overlayTextView).setVisibility(View.GONE);
}, 3000);
}
@Override
protected void onDestroy() {
// 在销毁活动时,释放 WebView 资源
if (webView != null) {
webView.destroy();
}