Android/XixunPlayer/app/src/main/java/com/xixun/xixunplayer/SrcDigitalClock.java
2024-01-25 20:43:39 +08:00

113 lines
4.4 KiB
Java

package com.xixun.xixunplayer;
import android.annotation.SuppressLint;
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.util.Calendar;
import java.util.Locale;
import java.util.SimpleTimeZone;
import java.util.TimeZone;
import gnph.util.JSMap;
import gnph.util.NumFmts;
import gnph.util.Txts;
@SuppressLint("ViewConstructor")
public class SrcDigitalClock extends WebView implements Choreographer.FrameCallback {
String html, lineHeight, prefix;
Calendar calendar;
Locale locale;
boolean hasYear, hasMonthStr, hasMonth, hasDay, hasWeek, hasAP, hasHour, hasHour12, hasMin, hasSec;
public SrcDigitalClock(Context context, JSMap json) {
super(context);
setBackgroundColor(Color.TRANSPARENT);
setVerticalScrollBarEnabled(false);
setHorizontalScrollBarEnabled(false);
setInitialScale(100);
html = json.stnn("html");
lineHeight = json.str("lineHeight");
var timezone = json.dbl("timezone", 9999);
calendar = timezone==9999 ? Calendar.getInstance() : Calendar.getInstance(new SimpleTimeZone((int) (timezone*3600000), ""));
prefix = "<body style=\"color:#fff;margin:0;padding:0;";
if(lineHeight!=null) prefix += "line-height:"+lineHeight+";";
prefix += "\">";
hasYear = html.contains("%y");
var hhh = html.replace("%Mw", "");
hasMonthStr = html.length()!=hhh.length();
hasMonth = hhh.contains("%M");
hasDay = html.contains("%d");
hasWeek = html.contains("%w");
hasAP = html.contains("%am");
hasHour = html.contains("%H");
hasHour12 = html.contains("%h");
hasMin = html.contains("%m");
hasSec = html.contains("%s");
try {
var lan = Txts.split(json.stnn("language"), "-", 1);
if(lan.isEmpty()) locale = Locale.getDefault();
else if(lan.size()==1) locale = lan.get(0).equals("cn") ? new Locale("zh", "CN") : new Locale(lan.get(0));
else locale = new Locale(lan.get(0), lan.get(1));
} catch (Exception e) {
Util.printStackTrace(e);
Util.makeText(context, Util.toStr(e)).show();
}
}
void cal() {
calendar.setTimeInMillis(System.currentTimeMillis());
var htm = html;
if(hasYear) htm = htm.replace("%y", Long.toString(calendar.get(Calendar.YEAR)));
try {
if(hasMonthStr) htm = htm.replace("%Mw", calendar.getDisplayName(Calendar.MONTH, Calendar.SHORT, locale));
if(hasWeek) htm = htm.replace("%w", calendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, locale));
if(hasAP) htm = htm.replace("%am", calendar.getDisplayName(Calendar.AM_PM, Calendar.SHORT, locale));
} catch (Exception ignored) {}
if(hasMonth) htm = htm.replace("%M", NumFmts.zz().format(calendar.get(Calendar.MONTH)+1));
if(hasDay) htm = htm.replace("%d", NumFmts.zz().format(calendar.get(Calendar.DAY_OF_MONTH)));
if(hasHour) htm = htm.replace("%H", NumFmts.zz().format(calendar.get(Calendar.HOUR_OF_DAY)));
if(hasHour12) htm = htm.replace("%h", NumFmts.zz().format(caseThen(calendar.get(Calendar.HOUR), 0, 12)));
if(hasMin) htm = htm.replace("%m", NumFmts.zz().format(calendar.get(Calendar.MINUTE)));
if(hasSec) htm = htm.replace("%s", NumFmts.zz().format(calendar.get(Calendar.SECOND)));
loadDataWithBaseURL(null, prefix+htm+"</body>", "text/html", "UTF-8", null);
}
int caseThen(int val, int cas, int then) {
return val==cas ? then : val;
}
@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);
}
}