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

295 lines
12 KiB
Java
Raw Normal View History

2023-11-09 08:37:59 +08:00
package com.xixun.xixunplayer;
2024-01-24 20:17:59 +08:00
import android.annotation.SuppressLint;
2023-11-09 08:37:59 +08:00
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;
2024-01-24 20:17:59 +08:00
import androidx.annotation.NonNull;
import java.text.SimpleDateFormat;
import java.util.Calendar;
2023-11-09 08:37:59 +08:00
import java.util.HashMap;
2024-01-24 20:17:59 +08:00
import java.util.TimeZone;
2023-11-09 08:37:59 +08:00
import gnph.android.LinearBox;
import gnph.util.Dates;
import gnph.util.JSList;
import gnph.util.JSMap;
2024-01-24 20:17:59 +08:00
@SuppressLint("ViewConstructor")
2023-11-09 08:37:59 +08:00
public class EleDigiClock extends LinearBox implements Choreographer.FrameCallback {
2024-01-24 20:17:59 +08:00
static final String weeks[] = {"SUN", "MON","TUE","WED","THU","FRI","SAT"};
2023-11-09 08:37:59 +08:00
HashMap<String, Bitmap> imgs = new HashMap<>();
2024-01-24 20:17:59 +08:00
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())};
2023-11-09 08:37:59 +08:00
ImageView weekComp = new ImageView(getContext()), ampmComp = new ImageView(getContext());
2024-01-24 20:17:59 +08:00
ImageView[] hourComps = {new ImageView(getContext()), new ImageView(getContext())};
ImageView[] minComps = {new ImageView(getContext()), new ImageView(getContext())};
ImageView[] secComps = {new ImageView(getContext()), new ImageView(getContext())};
2023-11-09 08:37:59 +08:00
2024-01-24 20:17:59 +08:00
TimeZone timeZone;
SimpleDateFormat timeptn, dataFmt = new SimpleDateFormat("yyyyMMdd");
2023-11-09 08:37:59 +08:00
boolean multiline, weekly, isSingleMonth;
2024-01-24 20:17:59 +08:00
public EleDigiClock(Context context, JSMap json) {
2023-11-09 08:37:59 +08:00
super(context);
setGravity(Gravity.CENTER);
2024-01-24 20:17:59 +08:00
var timeZoneStr = json.str("timeZone");
//if(timeZoneStr!=null) timeZone = ZoneId.of(timeZoneStr);
if(timeZoneStr!=null) TimeZone.getTimeZone(timeZoneStr);
2023-11-09 08:37:59 +08:00
var spaceWidth = json.dbl("spaceWidth");
JSList<JSMap> pics = json.jslist("arrayPics");
2024-01-24 20:17:59 +08:00
if(pics!=null && ! pics.isEmpty() && pics.get(0).containsKey("startX")) {
var big = BitmapFactory.decodeFile(Util.programDir+"/"+json.stnn("id"));
for(var pic : pics) {
var startX = pic.intg("startX");
var startY = pic.intg("startY");
imgs.put(pic.str("name"), Bitmap.createBitmap(big, startX, startY, pic.intg("endX")-startX, pic.intg("endY")-startY));
}
} else for(var pic : pics) imgs.put(pic.str("name"), BitmapFactory.decodeFile(Util.programDir+"/"+pic.stnn("id")));
2023-11-09 08:37:59 +08:00
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");
2024-01-24 20:17:59 +08:00
var AmPm = hour12 && json.bool("AmPm");
timeptn = new SimpleDateFormat(hour12 ? "hhmmssa" : "HHmmss");
2023-11-09 08:37:59 +08:00
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() {
2024-01-24 20:17:59 +08:00
//var dt = timeZone==null ? LocalDateTime.now() : LocalDateTime.now(timeZone);
var calendar = timeZone==null ? Calendar.getInstance() : Calendar.getInstance(timeZone);
var hms = timeptn.format(calendar.getTime());
ampmComp.setImageBitmap(imgs.get(calendar.get(Calendar.HOUR_OF_DAY) < 12?"AM":"PM"));
2023-11-09 08:37:59 +08:00
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)));
2024-01-24 20:17:59 +08:00
if(yearComps[0].getDrawable()==null || (calendar.get(Calendar.HOUR_OF_DAY)==0 && calendar.get(Calendar.SECOND)==0)) {
if(weekly) weekComp.setImageBitmap(imgs.get(weeks[calendar.get(Calendar.DAY_OF_WEEK) - 1]));
var ymd = dataFmt.format(calendar.getTime());
2023-11-09 08:37:59 +08:00
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
2024-01-24 20:17:59 +08:00
protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
2023-11-09 08:37:59 +08:00
super.onVisibilityChanged(changedView, visibility);
if(visibility==View.VISIBLE) {
if(lastSec==0) {
cal();
choreographer.postFrameCallback(this);
2024-01-24 20:17:59 +08:00
lastSec = 1;
2023-11-09 08:37:59 +08:00
}
}
}
Choreographer choreographer = Choreographer.getInstance();
2024-01-24 20:17:59 +08:00
long lastSec;
2023-11-09 08:37:59 +08:00
@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);
}
}