Created
September 7, 2011 17:13
-
-
Save rsaunders100/1201141 to your computer and use it in GitHub Desktop.
(Android) Use to check check if a threshold has expired since a last event. e.g. 5 mins has expired since the lat time some data has been downloaded. Optional persistant storage.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package uk.co.digitaljigsaw.utils; | |
import java.util.Calendar; | |
import java.util.Date; | |
import android.R.string; | |
import android.content.Context; | |
import android.content.SharedPreferences; | |
/** | |
* | |
* Use to check check if a threshold has expired since a last event. | |
* e.g. 5 mins has expired since the last time some data has been downloaded. | |
* | |
* This has optional caching which is set up by providing a context and | |
* a reference name. | |
* | |
* @author rob | |
* | |
*/ | |
public class SimpleTimerThreshold { | |
private int _timeoutThresholdSeconds; | |
private Date _timeoutDate; | |
private SharedPreferences _preferences; | |
private String _referenceName; | |
// Private to hide no- paramerter constructor | |
private SimpleTimerThreshold() {} | |
/** | |
* Creates a new timer, | |
* call restartTimer() | |
* then later check if the amout of time has elapsed | |
* by calling hasTimerPassedThreshold() | |
* | |
* @param timeThresholdSeconds | |
* How long the timer should be. | |
*/ | |
public SimpleTimerThreshold(int timeThresholdSeconds) { | |
_timeoutThresholdSeconds = timeThresholdSeconds; | |
} | |
/** | |
* Creates a new timer, with caching enabled | |
* call restartTimer() | |
* then later check if the amout of time has elapsed | |
* by calling hasTimerPassedThreshold() | |
* | |
* @param timeThresholdSeconds | |
* How long the timer should be. | |
*/ | |
public SimpleTimerThreshold(int timeThresholdSeconds, Context context, String referenceName) { | |
_timeoutThresholdSeconds = timeThresholdSeconds; | |
_preferences = context.getSharedPreferences("SimpleTimerThreshold", 0); | |
_referenceName = referenceName; | |
Long dateLong = _preferences.getLong(_referenceName, 0); | |
if (dateLong != 0) | |
{ | |
_timeoutDate = new Date(dateLong); | |
} | |
} | |
/** | |
* Call this to start / reset the timer | |
*/ | |
public void restartTimer() { | |
// Get a refrence to the Calendar singleton | |
Calendar calendar = Calendar.getInstance(); | |
// Record the current time | |
calendar.setTime(new Date()); | |
// Add on X seconds | |
calendar.add(Calendar.SECOND, _timeoutThresholdSeconds); | |
// Save the expire time | |
_timeoutDate = calendar.getTime(); | |
// If the cache is set up save the expire date to the cache | |
if (_preferences != null && _referenceName != null) | |
{ | |
_preferences.edit().putLong(_referenceName, _timeoutDate.getTime()); | |
} | |
//Log.v(TAG, "TimeOutDate:" +_timeOutDate.toString()); | |
} | |
/** | |
* Determins if the given amout of time has expired | |
* since the timer was last started / reset. | |
*/ | |
public boolean hasTimerPassedThreshold() { | |
if (_timeoutDate == null) return false; | |
// Get the current time | |
Date now = new Date(); | |
//Log.v(TAG, "Now:" + now.toString()); | |
//Log.v(TAG, "TimeOutDate:" + _timeOutDate.toString()); | |
// Compare it to the timeout time | |
return (now.after(_timeoutDate)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment