TimerChangeManager.java 2.71 KB
package com.sw.laryngoscope.manager;


import android.content.Context;

import com.sw.laryngoscope.utils.Logger;

public class TimerChangeManager implements Runnable {

    private static TimerChangeManager instance;

    private Thread mThread = null;
    private boolean isExitThread = false;
    Context mContext;

    private boolean isShowSet = false;

    int hour = 0;
    int min = 0;
    long time = 0;

    boolean is24HourFormat = false;

    public static TimerChangeManager getInstance() {
        if (instance == null) {
            synchronized (TimerChangeManager.class) {
                if (instance == null) {
                    instance = new TimerChangeManager();
                }
            }
        }
        return instance;
    }

    public void startThread(Context context) {
        if ( mThread == null ) {
            mThread = new Thread(this);
            mThread.setName("TimerChangeManager");
            mThread.start();
        }
        mContext = context;
        /*HandlerThread ht = new HandlerThread("Thread");
        ht.start();*/
    }

    public void exitThread() {
        isExitThread = true;
    }

    @Override
    public void run() {
        while ( !isExitThread ) {
            try {
                incTime();
                Thread.sleep(1 * 1000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public boolean isShowSet() {
        return isShowSet;
    }

    public void setShowSet(boolean showSet) {
        isShowSet = showSet;
    }

    public synchronized void setHour(int hour) {
        this.hour = hour;
        this.time = (hour * 60 + min) * 60 * 1000;
    }

    public synchronized void setMin(int min) {
        this.min = min;
        this.time = (hour * 60 + min) * 60 * 1000;
        Logger.d(" setMin time " + time + " min " + min);
    }

    public String getTimeStr() {
        if ( is24HourFormat ) {
            return String.format("%02d:%02d", hour, min);
        } else {
            if ( hour == 24 ) {
                return String.format("%02d:%02d", 0, min);
            }
            return String.format("%02d:%02d", hour >= 12 ? hour - 12 : hour, min);
        }
    }

    public synchronized void incTime() {
        time += 1000;
        //将毫秒转换为秒
        int second = Math.round(time * 1.0f / 1000);
        //计算小时
        hour = second / 3600;
        //计算分钟
        min = second % 3600 / 60;
        Logger.d(" setMin1 time " + time + " min " + min);
    }

    public void setIs24HourFormat(boolean is24HourFormat) {
        this.is24HourFormat = is24HourFormat;
    }

}