Update SrcCountdown.java

This commit is contained in:
Gangphon 2024-07-10 21:46:34 +08:00
parent 0d95ad356f
commit 9741d689d0

View File

@ -14,20 +14,32 @@ import gnph.util.NumFmts;
public class SrcCountdown extends WebView implements Choreographer.FrameCallback { public class SrcCountdown extends WebView implements Choreographer.FrameCallback {
long targetTime; long targetTime;
String html, lineHeight, prefix; String html, lineHeight, prefix, suffix;
boolean hasDay, hasHour, hasMin, hasSec; boolean isUp, hasDay, hasHour, hasMin, hasSec;
public SrcCountdown(Prog prog, JSMap json) { public SrcCountdown(Prog prog, JSMap json) {
super(prog.getContext()); super(prog.getContext());
setBackgroundColor(Color.TRANSPARENT); try {
var backColor = json.str("backColor");
setBackgroundColor(backColor==null ? Color.TRANSPARENT : Color.parseColor(backColor));
} catch (Exception ignored) {
setBackgroundColor(Color.TRANSPARENT);
}
setVerticalScrollBarEnabled(false); setVerticalScrollBarEnabled(false);
setHorizontalScrollBarEnabled(false); setHorizontalScrollBarEnabled(false);
setInitialScale(100); setInitialScale(100);
isUp = json.bool("isUp");
html = json.stnn("html"); html = json.stnn("html");
lineHeight = json.str("lineHeight"); lineHeight = json.str("lineHeight");
prefix = "<body style=\"color:#fff;margin:0;padding:0;"; prefix = "<body style=\"color:#fff;margin:0;padding:0;";
if(lineHeight!=null) prefix += "line-height:"+lineHeight+";"; if(lineHeight!=null) prefix += "line-height:"+lineHeight+";";
prefix += "\">"; prefix += "\">";
suffix = "</body>";
try {
var aaa = html.substring(0, 16).trim();
if(aaa.startsWith("<html") || aaa.startsWith("<body") || aaa.startsWith("<!DOCTYPE")) prefix = suffix = "";
} catch (Exception ignored) {
}
hasDay = html.contains("%d"); hasDay = html.contains("%d");
hasHour = html.contains("%h"); hasHour = html.contains("%h");
hasMin = html.contains("%m"); hasMin = html.contains("%m");
@ -47,7 +59,7 @@ public class SrcCountdown extends WebView implements Choreographer.FrameCallback
} }
void cal() { void cal() {
var secs = targetTime - lastSec; var secs = isUp ? lastSec - targetTime : targetTime - lastSec;
if(secs < 0) secs = 0; if(secs < 0) secs = 0;
var htm = html; var htm = html;
if(hasDay) { if(hasDay) {
@ -63,7 +75,7 @@ public class SrcCountdown extends WebView implements Choreographer.FrameCallback
secs %= 60; secs %= 60;
} }
if(hasSec) htm = htm.replace("%s", NumFmts.zz().format(secs)); if(hasSec) htm = htm.replace("%s", NumFmts.zz().format(secs));
loadDataWithBaseURL(null, prefix+htm+"</body>", "text/html", "UTF-8", null); loadDataWithBaseURL(null, prefix+htm+suffix, "text/html", "UTF-8", null);
} }
long lastSec; long lastSec;