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

80 lines
2.4 KiB
Java
Raw Normal View History

2024-01-24 20:17:59 +08:00
package com.xixun.xixunplayer;
2024-02-02 21:51:16 +08:00
import android.annotation.SuppressLint;
2024-01-24 20:17:59 +08:00
import android.graphics.Color;
import android.view.Choreographer;
import android.webkit.WebView;
2024-01-25 20:43:39 +08:00
import java.text.SimpleDateFormat;
2024-01-24 20:17:59 +08:00
import gnph.util.JSMap;
import gnph.util.NumFmts;
2024-02-02 21:51:16 +08:00
@SuppressLint("ViewConstructor")
2024-01-24 20:17:59 +08:00
public class SrcCountdown extends WebView implements Choreographer.FrameCallback {
long targetTime;
2024-01-25 20:43:39 +08:00
String html, lineHeight, prefix;
2024-01-24 20:17:59 +08:00
boolean hasDay, hasHour, hasMin, hasSec;
2024-02-02 21:51:16 +08:00
public SrcCountdown(Prog prog, JSMap json) {
super(prog.getContext());
2024-01-25 20:43:39 +08:00
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");
2024-01-24 20:17:59 +08:00
try {
2024-01-25 20:43:39 +08:00
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;
}
2024-01-24 20:17:59 +08:00
} catch (Exception e) {
Util.printStackTrace(e);
2024-02-02 21:51:16 +08:00
Util.makeText(prog.getContext(), Util.toStr(e)).show();
2024-01-24 20:17:59 +08:00
}
2024-02-02 21:51:16 +08:00
prog.calls.add(this);
2024-01-24 20:17:59 +08:00
}
void cal() {
2024-02-02 21:51:16 +08:00
var secs = targetTime - lastSec;
2024-01-24 20:17:59 +08:00
if(secs < 0) secs = 0;
var htm = html;
if(hasDay) {
2024-01-25 20:43:39 +08:00
htm = htm.replace("%d", Long.toString(secs/86400));
2024-01-24 20:17:59 +08:00
secs %= 86400;
}
if(hasHour) {
2024-01-25 20:43:39 +08:00
htm = htm.replace("%h", NumFmts.zz().format(secs/3600));
2024-01-24 20:17:59 +08:00
secs %= 3600;
}
if(hasMin) {
2024-01-25 20:43:39 +08:00
htm = htm.replace("%m", NumFmts.zz().format(secs/60));
2024-01-24 20:17:59 +08:00
secs %= 60;
}
2024-01-25 20:43:39 +08:00
if(hasSec) htm = htm.replace("%s", NumFmts.zz().format(secs));
loadDataWithBaseURL(null, prefix+htm+"</body>", "text/html", "UTF-8", null);
2024-01-24 20:17:59 +08:00
}
long lastSec;
@Override
2024-02-02 21:51:16 +08:00
public void doFrame(long ms) {
if(! isShown()) return;
var sec = ms / 1000;
2024-01-24 20:17:59 +08:00
if(sec != lastSec) {
lastSec = sec;
cal();
}
}
}