Android Open Source - SleepGuard Generic Watcher






From Project

Back to project page SleepGuard.

License

The source code is released under:

GNU General Public License

If you think the Android project SleepGuard 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.szabolcs.szijarto.sleepguard;
/* w ww. j  av a  2s  .  co  m*/
import java.text.DateFormat;

public abstract class GenericWatcher {

  protected long timeStarted, timeStopped;
  private String timeElapsedFormatted = "";
  private boolean running = false;
  private boolean wasEverStarted = false;

  protected Activity_Main myact = null;

  public GenericWatcher(Activity_Main a) {
    myact = a;
  }

  public GenericWatcher() {
    running = false;
  }

  public void start() {
    running = true;
    wasEverStarted = true;
    timeStarted = System.currentTimeMillis();
    timeElapsedFormatted = "running...";
  }

  private void calculateTimeElapsedFormatted() {
    long elapsedMillis = timeStopped - timeStarted;
    long elapsedSecs = elapsedMillis / 1000;
    long elapsedMins = elapsedSecs / 60;
    long elapsedHours = elapsedMins / 60;
    timeElapsedFormatted = elapsedHours + ":" + elapsedMins + ":"
        + elapsedSecs;
  }

  public void stop() {
    running = false;
    timeStopped = System.currentTimeMillis();
    calculateTimeElapsedFormatted();
  }

  public boolean isRunning() {
    return running;
  }

  public String getTimeStarted() throws GenericWatcherException {
    final String timeFormatted;
    if (wasEverStarted) {
      timeFormatted = DateFormat.getDateTimeInstance()
          .format(timeStarted);
    } else {
      throw new GenericWatcherException(
          "Watcher was asked for start time, but was never started.");
    }
    return timeFormatted;
  }

  private boolean wasStoppedSinceLastStart() {
    return wasEverStarted && !isRunning();
  }

  public String getTimeStopped() throws GenericWatcherException {
    final String timeFormatted;
    if (wasStoppedSinceLastStart()) {
      timeFormatted = DateFormat.getDateTimeInstance()
          .format(timeStopped);
    } else {
      throw new GenericWatcherException(
          "Watcher was asked for stop time, but was not stopped since last start.");
    }
    return timeFormatted;
  }

  public String getTimeElapsed() {
    return timeElapsedFormatted;
  }

}




Java Source Code List

com.szabolcs.szijarto.sleepguard.Activity_Main.java
com.szabolcs.szijarto.sleepguard.Activity_ShowRecording.java
com.szabolcs.szijarto.sleepguard.GenericWatcherException.java
com.szabolcs.szijarto.sleepguard.GenericWatcher.java
com.szabolcs.szijarto.sleepguard.HeartRateRec.java
com.szabolcs.szijarto.sleepguard.HeartRateWatcher.java
com.szabolcs.szijarto.sleepguard.Peak.java
com.szabolcs.szijarto.sleepguard.RecordingFile.java
com.szabolcs.szijarto.sleepguard.RecordingListView.java
com.szabolcs.szijarto.sleepguard.Recording.java
com.szabolcs.szijarto.sleepguard.SleepChart.java