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

283 lines
11 KiB
Java
Raw Normal View History

2023-11-09 08:37:59 +08:00
package com.xixun.xixunplayer;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Choreographer;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.HashMap;
import gnph.android.LinearBox;
import gnph.util.Dates;
import gnph.util.JSList;
import gnph.util.JSMap;
import gnph.util.Sys;
public class EleDigiClock extends LinearBox implements Choreographer.FrameCallback {
static final String weeks[] = {"MON","TUE","WED","THU","FRI","SAT","SUN"};
HashMap<String, Bitmap> imgs = new HashMap<>();
ImageView yearComps[] = {new ImageView(getContext()), new ImageView(getContext()), new ImageView(getContext()), new ImageView(getContext())};
ImageView monthComps[] = {new ImageView(getContext()), new ImageView(getContext())};
ImageView dayComps[] = {new ImageView(getContext()), new ImageView(getContext())};
ImageView weekComp = new ImageView(getContext()), ampmComp = new ImageView(getContext());
ImageView hourComps[] = {new ImageView(getContext()), new ImageView(getContext())};
ImageView minComps[] = {new ImageView(getContext()), new ImageView(getContext())};
ImageView secComps[] = {new ImageView(getContext()), new ImageView(getContext())};
ZoneId timeZone;
String timeptn;
boolean multiline, weekly, isSingleMonth;
public EleDigiClock(String prefix, JSMap json, Context context) {
super(context);
setGravity(Gravity.CENTER);
timeZone = ZoneId.of(json.stnn("timeZone"));
var spaceWidth = json.dbl("spaceWidth");
JSList<JSMap> pics = json.jslist("arrayPics");
for(var pic : pics) imgs.put(pic.stnn("name"), BitmapFactory.decodeFile(prefix+pic.stnn("id")));
int dateStyle = json.intg("dateStyle");
isSingleMonth = dateStyle==1||dateStyle==2||dateStyle==4||dateStyle==6||dateStyle==8||dateStyle==10||dateStyle==12;
var timeSep = imgs.get("maohao");
weekly = json.bool("weekly");
var hour12 = json.bool("hour12");
var AmPm = hour12 ? json.bool("AmPm") : false;
timeptn = hour12 ? "hhmmssa" : "HHmmss";
var hour = json.bool("hour");
var min = json.bool("min");
var sec = json.bool("sec");
multiline = json.bool("multiline");
addStretch();
if(multiline) {
vertical();
var hBox = new LinearBox(this).horizontal();
hBox.addStretch();
addDate(dateStyle, json, hBox);
hBox.addStretch();
if(weekly) {
hBox = new LinearBox(this).horizontal();
hBox.addStretch();
hBox.addView(weekComp);
hBox.addStretch();
}
hBox = new LinearBox(this).horizontal();
hBox.addStretch();
if(AmPm) {
hBox.addView(ampmComp);
hBox.addSpacing((int)spaceWidth);
}
if(hour) {
hBox.addView(hourComps[0]);
hBox.addView(hourComps[1]);
}
if(hour&&min) hBox.addView(newImgView(timeSep));
if(min) {
hBox.addView(minComps[0]);
hBox.addView(minComps[1]);
}
if(min&&sec) hBox.addView(newImgView(timeSep));
if(sec) {
hBox.addView(secComps[0]);
hBox.addView(secComps[1]);
}
hBox.addStretch();
} else {
setOrientation(HORIZONTAL);
addDate(dateStyle, json, this);
if(getChildCount()>1) addSpacing((int)spaceWidth*2);
if(weekly) {
addView(weekComp);
addSpacing((int)spaceWidth*2);
}
if(AmPm) {
addView(ampmComp);
addSpacing((int)spaceWidth);
}
if(hour) {
addView(hourComps[0]);
addView(hourComps[1]);
}
if(hour&&min) addView(newImgView(timeSep));
if(min) {
addView(minComps[0]);
addView(minComps[1]);
}
if(min&&sec) addView(newImgView(timeSep));
if(sec) {
addView(secComps[0]);
addView(secComps[1]);
}
}
addStretch();
}
ImageView newImgView(Bitmap img) {
var imgv = new ImageView(getContext());
imgv.setImageBitmap(img);
return imgv;
}
void addDate(int dateStyle, JSMap layer, LinearLayout tar) {
if(dateStyle==0 || dateStyle==1) {
addYear(layer, tar, imgs.get("YEAR"));
if(layer.bool("month")) {
tar.addView(monthComps[0]);
tar.addView(monthComps[1]);
tar.addView(newImgView(imgs.get("MONTH")));
}
if(layer.bool("day")) {
tar.addView(dayComps[0]);
tar.addView(dayComps[1]);
tar.addView(newImgView(imgs.get("DAY")));
}
} else if(dateStyle==2 || dateStyle==3) {
var sep = imgs.get("xiegang");
if(layer.bool("month")) {
tar.addView(monthComps[0]);
tar.addView(monthComps[1]);
tar.addView(newImgView(sep));
}
if(layer.bool("day")) {
tar.addView(dayComps[0]);
tar.addView(dayComps[1]);
tar.addView(newImgView(sep));
}
addYear(layer, tar, null);
} else if(dateStyle==4 || dateStyle==5) {
var sep = imgs.get("xiegang");
if(layer.bool("day")) {
tar.addView(dayComps[0]);
tar.addView(dayComps[1]);
tar.addView(newImgView(sep));
}
if(layer.bool("month")) {
tar.addView(monthComps[0]);
tar.addView(monthComps[1]);
tar.addView(newImgView(sep));
}
addYear(layer, tar, null);
} else if(dateStyle==6 || dateStyle==7) {
var sep = imgs.get("xiegang");
addYear(layer, tar, sep);
if(layer.bool("month")) {
tar.addView(monthComps[0]);
tar.addView(monthComps[1]);
tar.addView(newImgView(sep));
}
if(layer.bool("day")) {
tar.addView(dayComps[0]);
tar.addView(dayComps[1]);
}
} else if(dateStyle==8 || dateStyle==9) {
var sep = imgs.get("hengxian");
if(layer.bool("month")) {
tar.addView(monthComps[0]);
tar.addView(monthComps[1]);
tar.addView(newImgView(sep));
}
if(layer.bool("day")) {
tar.addView(dayComps[0]);
tar.addView(dayComps[1]);
tar.addView(newImgView(sep));
}
addYear(layer, tar, null);
} else if(dateStyle==10 || dateStyle==11) {
var sep = imgs.get("hengxian");
if(layer.bool("day")) {
tar.addView(dayComps[0]);
tar.addView(dayComps[1]);
tar.addView(newImgView(sep));
}
if(layer.bool("month")) {
tar.addView(monthComps[0]);
tar.addView(monthComps[1]);
tar.addView(newImgView(sep));
}
addYear(layer, tar, null);
} else if(dateStyle==12 || dateStyle==13) {
var sep = imgs.get("hengxian");
addYear(layer, tar, sep);
if(layer.bool("month")) {
tar.addView(monthComps[0]);
tar.addView(monthComps[1]);
tar.addView(newImgView(sep));
}
if(layer.bool("day")) {
tar.addView(dayComps[0]);
tar.addView(dayComps[1]);
}
}
}
void addYear(JSMap layer, LinearLayout tar, Bitmap sep) {
if(layer.bool("year")) {
if(layer.bool("fullYear")) {
tar.addView(yearComps[0]);
tar.addView(yearComps[1]);
}
tar.addView(yearComps[2]);
tar.addView(yearComps[3]);
if(sep != null) tar.addView(newImgView(sep));
}
}
void cal() {
var dt = LocalDateTime.now(timeZone);
var time = dt.toLocalTime();
var hms = Dates.fmt(time, timeptn);
ampmComp.setImageBitmap(imgs.get(time.getHour()<12?"AM":"PM"));
hourComps[0].setImageBitmap(imgs.get(hms.substring(0,1)));
hourComps[1].setImageBitmap(imgs.get(hms.substring(1,2)));
minComps[0].setImageBitmap(imgs.get(hms.substring(2,3)));
minComps[1].setImageBitmap(imgs.get(hms.substring(3,4)));
secComps[0].setImageBitmap(imgs.get(hms.substring(4,5)));
secComps[1].setImageBitmap(imgs.get(hms.substring(5,6)));
if(yearComps[0].getDrawable()==null || (time.getHour()==0 && time.getSecond()==0)) {
var date = dt.toLocalDate();
if(weekly) weekComp.setImageBitmap(imgs.get(weeks[date.getDayOfWeek().ordinal()]));
var ymd = Dates.fmt(date, "yyyyMMdd");
yearComps[0].setImageBitmap(imgs.get(ymd.substring(0,1)));
yearComps[1].setImageBitmap(imgs.get(ymd.substring(1,2)));
yearComps[2].setImageBitmap(imgs.get(ymd.substring(2,3)));
yearComps[3].setImageBitmap(imgs.get(ymd.substring(3,4)));
monthComps[0].setImageBitmap(isSingleMonth && ymd.charAt(4)=='0' ? null : imgs.get(ymd.substring(4,5)));
monthComps[1].setImageBitmap(imgs.get(ymd.substring(5,6)));
dayComps[0].setImageBitmap(isSingleMonth && ymd.charAt(6)=='0' ? null : imgs.get(ymd.substring(6,7)));
dayComps[1].setImageBitmap(imgs.get(ymd.substring(7,8)));
}
}
@Override
protected void onVisibilityChanged(View changedView, int visibility) {
super.onVisibilityChanged(changedView, visibility);
if(visibility==View.VISIBLE) {
if(lastSec==0) {
cal();
choreographer.postFrameCallback(this);
}
}
}
Choreographer choreographer = Choreographer.getInstance();
long lastSec = 0;
@Override
public void doFrame(long frameTimeNanos) {
if(! isShown()) {
lastSec = 0;
return;
}
var sec = System.currentTimeMillis() / 1000;
if(sec != lastSec) {
lastSec = sec;
cal();
invalidate();
}
choreographer.postFrameCallback(this);
}
}