Android/XixunPlayer/app/src/main/java/com/xixun/xixunplayer/SrcCountdown.java
2024-02-02 21:51:16 +08:00

80 lines
2.4 KiB
Java

package com.xixun.xixunplayer;
import android.annotation.SuppressLint;
import android.graphics.Color;
import android.view.Choreographer;
import android.webkit.WebView;
import java.text.SimpleDateFormat;
import gnph.util.JSMap;
import gnph.util.NumFmts;
@SuppressLint("ViewConstructor")
public class SrcCountdown extends WebView implements Choreographer.FrameCallback {
long targetTime;
String html, lineHeight, prefix;
boolean hasDay, hasHour, hasMin, hasSec;
public SrcCountdown(Prog prog, JSMap json) {
super(prog.getContext());
setBackgroundColor(Color.TRANSPARENT);
setVerticalScrollBarEnabled(false);
setHorizontalScrollBarEnabled(false);
setInitialScale(100);
html = json.stnn("html");
lineHeight = json.str("lineHeight");
prefix = "<body style=\"color:#fff;margin:0;padding:0;";
if(lineHeight!=null) prefix += "line-height:"+lineHeight+";";
prefix += "\">";
hasDay = html.contains("%d");
hasHour = html.contains("%h");
hasMin = html.contains("%m");
hasSec = html.contains("%s");
var time = json.stnn("time");
try {
try {
targetTime = new SimpleDateFormat("y-M-d H:m:s").parse(time).getTime() / 1000;
} catch (Exception e) {
targetTime = new SimpleDateFormat("y-M-d H:m").parse(time).getTime() / 1000;
}
} catch (Exception e) {
Util.printStackTrace(e);
Util.makeText(prog.getContext(), Util.toStr(e)).show();
}
prog.calls.add(this);
}
void cal() {
var secs = targetTime - lastSec;
if(secs < 0) secs = 0;
var htm = html;
if(hasDay) {
htm = htm.replace("%d", Long.toString(secs/86400));
secs %= 86400;
}
if(hasHour) {
htm = htm.replace("%h", NumFmts.zz().format(secs/3600));
secs %= 3600;
}
if(hasMin) {
htm = htm.replace("%m", NumFmts.zz().format(secs/60));
secs %= 60;
}
if(hasSec) htm = htm.replace("%s", NumFmts.zz().format(secs));
loadDataWithBaseURL(null, prefix+htm+"</body>", "text/html", "UTF-8", null);
}
long lastSec;
@Override
public void doFrame(long ms) {
if(! isShown()) return;
var sec = ms / 1000;
if(sec != lastSec) {
lastSec = sec;
cal();
}
}
}