Back to project page improv-referee.
The source code is released under:
Copyright (C) <2011> by <Pierre-Henri Trivier> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to de...
If you think the Android project improv-referee listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package fr.pht.improv.referee; /* w ww. j a v a2s .c om*/ import java.util.ArrayList; import java.util.List; import android.os.Handler; import android.os.SystemClock; import android.util.Log; public class ProgressTimer { private Handler progressHandler = new Handler(); private long referenceTime = 0; private long elapsedTimeMillis = 0; private int durationInSeconds; private List<ProgressListener> progressListeners = new ArrayList<ProgressListener>(); private boolean running = false; private Runnable updateProgressTime = new Runnable() { public void run() { if (running) { final long start = referenceTime; final long now = SystemClock.uptimeMillis(); long millis = now - start; referenceTime = now; elapsedTimeMillis = elapsedTimeMillis + millis; int accumulatedSeconds = (int) (elapsedTimeMillis / 1000); int remainingS = (durationInSeconds - accumulatedSeconds); int proportion = (((remainingS) * 100) / durationInSeconds); int progress = 100 - proportion; for (ProgressListener listener : progressListeners) { listener.onTick(progress, elapsedTimeMillis); } // Don't schedule anything if progress is finished if (progress < 100) { progressHandler.postAtTime(this, now + 1000); } } }; }; public ProgressTimer(int durationInSeconds) { this.durationInSeconds = durationInSeconds; } public void setDurationInSeconds(int durationInSeconds) { this.durationInSeconds = durationInSeconds; } public void start() { referenceTime = SystemClock.uptimeMillis(); elapsedTimeMillis = 0; progressHandler.removeCallbacks(updateProgressTime); progressHandler.postDelayed(updateProgressTime, 100); running = true; } public void reset() { referenceTime = 0; elapsedTimeMillis = 0; progressHandler.removeCallbacks(updateProgressTime); running = false; } // TODO implement pause properly public void resume() { referenceTime = SystemClock.uptimeMillis(); progressHandler.removeCallbacks(updateProgressTime); progressHandler.postDelayed(updateProgressTime, 100); running = true; } public void stop() { running = false; } public boolean isRunning() { return running; } public void addProgressListener(ProgressListener listener) { this.progressListeners.add(listener); } public long getElapsedTimeMillis() { return elapsedTimeMillis; } public void forceElapsedTime(long elapsedTimeMillis) { referenceTime = SystemClock.uptimeMillis(); this.elapsedTimeMillis = elapsedTimeMillis; } }