盛康基办.apk(点击下载) / OrderCountDownHelper.java


package im.guobwnxjuc.ui.utils.timer;

import android.os.CountDownTimer;
import com.blankj.utilcode.util.TimeUtils;
import com.google.android.exoplayer2.upstream.DefaultLoadErrorHandlingPolicy;
import com.king.zxing.util.LogUtils;
import im.guobwnxjuc.messenger.LocaleController;
import im.guobwnxjuc.messenger.R;
import java.net.URL;
import java.net.URLConnection;

public class OrderCountDownHelper {
    private OrderCountDownCallBack callBack;
    private long mCompareTimeMills;
    private String mCompareTimeStr;
    private long mOrderCreateTimeMills;
    private String mOrderCreateTimeStr;
    private OrderCountDownTimer mTimer;
    private int termsMin;

    public interface OrderCountDownCallBack {
        void onTick(boolean z, String str);
    }

    public void start() {
        String str = this.mOrderCreateTimeStr;
        if (str != null) {
            this.mOrderCreateTimeMills = TimeUtils.string2Millis(str);
        }
        if (this.mOrderCreateTimeMills != 0) {
            String str2 = this.mCompareTimeStr;
            if (str2 != null) {
                this.mCompareTimeMills = TimeUtils.string2Millis(str2);
            }
            if (this.mCompareTimeStr == null && this.mCompareTimeMills == 0) {
                this.mCompareTimeMills = getNetTime();
            }
            long j = this.mCompareTimeMills;
            if (j != 0) {
                long abs = ((long) ((this.termsMin * 60) * 1000)) - Math.abs(this.mOrderCreateTimeMills - j);
                if (abs <= 0) {
                    OrderCountDownCallBack orderCountDownCallBack = this.callBack;
                    if (orderCountDownCallBack != null) {
                        orderCountDownCallBack.onTick(true, "00:00:00");
                        return;
                    }
                    return;
                }
                cancel();
                OrderCountDownTimer orderCountDownTimer = new OrderCountDownTimer(abs, 1000);
                this.mTimer = orderCountDownTimer;
                orderCountDownTimer.setCallBack(this.callBack);
                this.mTimer.start();
            }
        }
    }

    public void cancel() {
        OrderCountDownTimer orderCountDownTimer = this.mTimer;
        if (orderCountDownTimer != null && orderCountDownTimer.isRunning) {
            this.mTimer.cancel();
            this.mTimer = null;
        }
    }

    public void restart() {
        this.mCompareTimeMills = getNetTime();
        start();
    }

    public void destroy() {
        cancel();
        this.callBack = null;
    }

    public OrderCountDownTimer getTimer() {
        return this.mTimer;
    }

    public boolean isRunning() {
        OrderCountDownTimer orderCountDownTimer = this.mTimer;
        return orderCountDownTimer != null && orderCountDownTimer.isRunning;
    }

    public static long getNetTime() {
        try {
            URLConnection openConnection = new URL("http://www.ntsc.ac.cn").openConnection();
            openConnection.setReadTimeout(5000);
            openConnection.setConnectTimeout(5000);
            openConnection.connect();
            return openConnection.getDate();
        } catch (Exception unused) {
            return System.currentTimeMillis();
        }
    }

    public OrderCountDownHelper setOrderCreateTimeStr(String str) {
        this.mOrderCreateTimeStr = str;
        return this;
    }

    public OrderCountDownHelper setOrderCreateTimeMills(long j) {
        this.mOrderCreateTimeMills = j;
        return this;
    }

    public OrderCountDownHelper setTermsMin(int i) {
        this.termsMin = i;
        return this;
    }

    public OrderCountDownHelper setCompareTimeStr(String str) {
        this.mCompareTimeStr = str;
        return this;
    }

    public OrderCountDownHelper setCompareTimeMills(long j) {
        this.mCompareTimeMills = j;
        return this;
    }

    public OrderCountDownHelper setCallBack(OrderCountDownCallBack orderCountDownCallBack) {
        this.callBack = orderCountDownCallBack;
        return this;
    }

    public static class OrderCountDownTimer extends CountDownTimer {
        private OrderCountDownCallBack callBack;
        private boolean isRunning;

        public OrderCountDownTimer(long j, long j2) {
            super(j, j2);
        }

        public void onTick(long j) {
            this.isRunning = j != 0;
            if (j == 0) {
                OrderCountDownCallBack orderCountDownCallBack = this.callBack;
                if (orderCountDownCallBack != null) {
                    orderCountDownCallBack.onTick(true, "00:00:00");
                }
            } else if (this.callBack != null) {
                long j2 = j / 86400000;
                long j3 = j / 3600000;
                long j4 = j / DefaultLoadErrorHandlingPolicy.DEFAULT_TRACK_BLACKLIST_MS;
                long j5 = (j / 1000) % 60;
                StringBuilder sb = new StringBuilder();
                if (j2 > 0) {
                    sb.append(j2);
                    sb.append(LocaleController.getString("TimeUnitOfDay", R.string.TimeUnitOfDay));
                }
                if (j3 >= 0 && j3 < 10) {
                    sb.append("0");
                }
                sb.append(j3);
                sb.append(LogUtils.COLON);
                if (j4 >= 0 && j4 < 10) {
                    sb.append("0");
                }
                sb.append(j4);
                sb.append(LogUtils.COLON);
                if (j5 >= 0 && j5 < 10) {
                    sb.append("0");
                }
                sb.append(j5);
                this.callBack.onTick(false, sb.toString());
            }
        }

        public void onFinish() {
            this.isRunning = false;
            OrderCountDownCallBack orderCountDownCallBack = this.callBack;
            if (orderCountDownCallBack != null) {
                orderCountDownCallBack.onTick(true, "00:00:00");
            }
        }

        public boolean isRunning() {
            return this.isRunning;
        }

        public void setCallBack(OrderCountDownCallBack orderCountDownCallBack) {
            this.callBack = orderCountDownCallBack;
        }
    }
}