package com.xixun.xixunplayer; import android.content.Context; import android.graphics.Color; import android.view.Choreographer; import android.view.View; import android.webkit.WebView; import androidx.annotation.NonNull; import java.text.SimpleDateFormat; import gnph.util.JSMap; import gnph.util.NumFmts; public class SrcCountdown extends WebView implements Choreographer.FrameCallback { long targetTime; String html, lineHeight, prefix; boolean hasDay, hasHour, hasMin, hasSec; public SrcCountdown(Context context, JSMap json) { super(context); setBackgroundColor(Color.TRANSPARENT); setVerticalScrollBarEnabled(false); setHorizontalScrollBarEnabled(false); setInitialScale(100); html = json.stnn("html"); lineHeight = json.str("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(context, Util.toStr(e)).show(); } } void cal() { var cur = System.currentTimeMillis() / 1000; var secs = targetTime - cur; 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+"", "text/html", "UTF-8", null); } @Override protected void onVisibilityChanged(@NonNull View changedView, int visibility) { super.onVisibilityChanged(changedView, visibility); if(visibility==View.VISIBLE) { if(lastSec==0) { cal(); choreographer.postFrameCallback(this); lastSec = 1; } } } Choreographer choreographer = Choreographer.getInstance(); long lastSec; @Override public void doFrame(long frameTimeNanos) { if(! isShown()) { lastSec = 0; return; } var sec = System.currentTimeMillis() / 1000; if(sec != lastSec) { lastSec = sec; cal(); } choreographer.postFrameCallback(this); } }