Android/XixunPlayer/app/src/main/java/com/xixun/xixunplayer/SrcWeb.java

121 lines
4.4 KiB
Java
Raw Normal View History

2024-08-22 17:37:46 +08:00
package com.xixun.xixunplayer;
import android.annotation.SuppressLint;
import android.graphics.Color;
2024-08-22 18:14:03 +08:00
import android.net.http.SslError;
2025-08-20 23:02:25 +08:00
import android.os.Build;
2024-08-22 17:37:46 +08:00
import android.view.Choreographer;
2024-08-22 18:14:03 +08:00
import android.webkit.SslErrorHandler;
2025-08-20 23:02:25 +08:00
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
2024-08-22 17:37:46 +08:00
import android.webkit.WebView;
import android.webkit.WebViewClient;
2025-08-20 23:02:25 +08:00
import java.util.ArrayList;
2024-08-22 17:37:46 +08:00
import gnph.util.JSMap;
@SuppressLint("ViewConstructor")
public class SrcWeb extends WebView implements Choreographer.FrameCallback {
String url;
int refresh;
2025-08-20 23:02:25 +08:00
ArrayList<Choreographer.FrameCallback> calls;
2024-08-22 17:37:46 +08:00
@SuppressLint("SetJavaScriptEnabled")
2025-08-20 23:02:25 +08:00
public SrcWeb(Prog prog, String url, JSMap json) {
2024-08-22 17:37:46 +08:00
super(prog.getContext());
2025-08-20 23:02:25 +08:00
this.url = url;
2024-08-22 17:37:46 +08:00
var settings = getSettings();
settings.setJavaScriptEnabled(true);
settings.setDomStorageEnabled(true);
settings.setLoadsImagesAutomatically(true);
setVerticalScrollBarEnabled(false);
setHorizontalScrollBarEnabled(false);
setBackgroundColor(Color.TRANSPARENT);
setInitialScale(json.intg("zoom", 100));
2025-08-20 23:02:25 +08:00
clearCache(true);
2024-08-22 17:37:46 +08:00
setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
@Override
2024-08-22 18:14:03 +08:00
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed(); //忽略SSL证书错误加载网页
}
@Override
2024-08-22 17:37:46 +08:00
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript:window.scrollTo("+json.str("offX", "0")+", "+json.str("offY", "0")+")");
}
2025-08-20 23:02:25 +08:00
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
2025-09-19 19:13:20 +08:00
if(request.isForMainFrame() && error.getErrorCode()==-2 && request.getUrl().toString().equals(url)) {
needReload = true;
Util.println(" WebView Need Reload");
}
2025-08-20 23:02:25 +08:00
Util.println(" WebView ReceivedError "+error.getErrorCode()+" "+error.getDescription()+"; isForMainFrame "+request.isForMainFrame()+" URL "+request.getUrl());
}
}
@Override
public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
Util.println(" WebView ReceivedHttpError "+errorResponse.getStatusCode()+"; isForMainFrame "+request.isForMainFrame()+" URL "+request.getUrl());
}
2024-08-22 17:37:46 +08:00
});
2025-08-20 23:02:25 +08:00
loadUrl(url);
2024-08-22 17:37:46 +08:00
refresh = json.intg("refreshSec")*1000;
2025-08-20 23:02:25 +08:00
if(refresh==0 || Util.custom!=Util.Custom.Yishi) this.calls = prog.calls;
prog.calls.add(this);
2024-08-22 17:37:46 +08:00
nextMs = System.currentTimeMillis() + refresh;
}
long nextMs;
2025-08-20 23:02:25 +08:00
int cnt, w, h;
boolean needReload;
2024-08-22 17:37:46 +08:00
@Override
public void doFrame(long ms) {
if(! isShown()) return;
2025-07-28 19:14:00 +08:00
if(Util.custom==Util.Custom.Yishi) {
2025-08-20 23:02:25 +08:00
if(refresh > 0) {
if(ms>=nextMs) {
nextMs = ms + refresh;
loadUrl(url);
}
} else if(++cnt>=600) {
cnt = 0;
if(needReload) {
needReload = false;
loadUrl(url);
Util.println(" WebView Refreshed");
}
2025-07-28 19:14:00 +08:00
}
if(Util.screenWidth!=w || Util.screenHeight!=h) {
w = Util.screenWidth;
h = Util.screenHeight;
Util.println(" w "+w+" h "+h);
setLayoutParams(new AbsLayout.LayoutParams(0, 0, w, h));
}
} else {
2025-08-20 23:02:25 +08:00
if(refresh > 0) {
if(ms>=nextMs) {
nextMs = ms + refresh;
loadUrl(url);
}
} else if(++cnt>=600) {
cnt = 0;
if(needReload) {
needReload = false;
loadUrl(url);
Util.println(" WebView Refreshed");
} else if(calls!=null) {
calls.remove(this);
calls = null;
}
2025-07-28 19:14:00 +08:00
}
2024-08-22 17:37:46 +08:00
}
}
}