Android Open Source - ArcTimer Interval Session






From Project

Back to project page ArcTimer.

License

The source code is released under:

GNU General Public License

If you think the Android project ArcTimer listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.taavo.arctimer;
/*from   w  ww .  j  a  v a  2s  .com*/
import java.util.Calendar;



public class IntervalSession {

  public static final int TYPE_WORK = 1;
  public static final int TYPE_REST = 2;
  
  protected int totalWorkCycles; // prefs
  protected int currentCycleType; 
  protected int currentCycle;

  protected long startTime   = 0;  // state
  protected long elapsedTime = 0;  // state 
  protected long pausedTime  = 0;  // state
  
  protected long startCycleTime   = 0;
  protected long elapsedCycleTime = 0; 
  
  protected long workCycleTime; // prefs
  protected long restCycleTime; // prefs 
  protected long currentCycleTime; // prefs
  
  protected boolean running;
  
  public IntervalSession() {
    running = false;

    currentCycle     = 0;
    currentCycleType = TYPE_WORK;
  }
    
    
  public void startSession() {
    startTime = Calendar.getInstance().getTimeInMillis();
    startCycleTime = startTime;
  }
  
  public void stopSession() {
    startTime        = 0;
    elapsedTime      = 0;
    pausedTime       = 0;
    startCycleTime   = 0;
    elapsedCycleTime = 0;    

    running = false;

    currentCycle     = 0;
    currentCycleType = TYPE_WORK;
    currentCycleTime = workCycleTime;
  }
  
  public void resumeSession() {
    long sleptTime = Calendar.getInstance().getTimeInMillis() - pausedTime;
    startTime      += sleptTime;
    startCycleTime += sleptTime;  
  }
  
  public void pauseSession() {
    pausedTime = Calendar.getInstance().getTimeInMillis();
  }
  
  public void updateSession() {
    long currentTime  = Calendar.getInstance().getTimeInMillis();
    elapsedTime = currentTime - startTime;
    elapsedCycleTime = currentTime - startCycleTime;
        
    if(elapsedCycleTime > currentCycleTime) { // check if it is time to switch type
      currentCycleType = (currentCycleType == IntervalSession.TYPE_WORK)?IntervalSession.TYPE_REST:IntervalSession.TYPE_WORK;
      startCycleTime = currentTime - (elapsedCycleTime - currentCycleTime); // reset start time, compensate for shooting over
      currentCycle++;
    }
    
    if(currentCycleType == IntervalSession.TYPE_WORK) {
      currentCycleTime = workCycleTime;      
    } else {
      currentCycleTime = restCycleTime;
    }  
    
    if (currentCycle >= (totalWorkCycles*2-1)) {
      stopSession();
    }
  }
  
  
}




Java Source Code List

com.taavo.arctimer.ArcTimerActivity.java
com.taavo.arctimer.ArcTimerView.java
com.taavo.arctimer.IntervalSession.java
com.taavo.arctimer.Prefs.java