Android Open Source - ProjectTimer My Timer






From Project

Back to project page ProjectTimer.

License

The source code is released under:

Copyright (c) 2012 Alex Wu Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software w...

If you think the Android project ProjectTimer 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.cooltofu;
//from  w w  w. j  a v  a  2 s  .com
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import android.widget.TextView;

public class MyTimer {
  private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
  private ScheduledFuture handle;
  private TextView timeTextView;
  private TextView labelTextView;
  private String label;
  private int seconds;
  private int timerId;
  private boolean timerActive;
  
  public MyTimer(int timerId, TextView labelTextView, TextView timeTextView, String label, int initialSeconds) {
    this.timerId = timerId;
    this.labelTextView = labelTextView;
    this.timeTextView = timeTextView;
    this.label = label;
    this.seconds = initialSeconds;
    init();
  }
  
  private void init() {
    timeTextView.post(new Runnable() {
      public void run() {
        timeTextView.setText(TimeUtil.formatTimeTextDisplay(seconds));
      }
    });
    
    labelTextView.post(new Runnable() {
      public void run() {
        labelTextView.setText(label);
      }
    });
  }
  
  public void startTimer() {
    final Runnable increment = new Runnable() {
      public void run() {
        timeTextView.post(new Runnable() {
          public void run() {
            timeTextView.setText(TimeUtil.formatTimeTextDisplay(seconds += 1));
          }
        });
      }
    };
    
    handle = scheduler.scheduleAtFixedRate(increment,  1, 1, TimeUnit.SECONDS);
    timerActive = true;
  }
  
  public void stopTimer() {
    if (handle == null)
      return;
    
    handle.cancel(true);
    timerActive = false;
  }
  
  
  
  public boolean isActive() {
    return timerActive;
  }
  
  public int getSeconds() {
    return seconds;
  }
  
  public void setSeconds(final int seconds) {
    this.seconds = seconds;
    timeTextView.post(new Runnable() {
      public void run() {
        timeTextView.setText(TimeUtil.formatTimeTextDisplay(seconds));
      }
    });
  }

  public int getTimerId() {
    return timerId;
  }
  
  public void setLabel(final String label) {
    this.label = label;
    
    labelTextView.post(new Runnable() {
      public void run() {
        labelTextView.setText(label);
      }
    });
  }
  
  public String getLabel() {
    return label;
  }
  
  public void reset() {
    seconds = 0;
    
    timeTextView.post(new Runnable() {
      public void run() {
        timeTextView.setText(TimeUtil.formatTimeTextDisplay(seconds));
      }
    });
  }  
}




Java Source Code List

com.cooltofu.Eula.java
com.cooltofu.MoreScreen.java
com.cooltofu.MyTimer.java
com.cooltofu.ProjectTimerActivity.java
com.cooltofu.TimeUtil.java
com.cooltofu.db.TimerDbAdapter.java
com.cooltofu.db.TimerDbHelper.java
com.cooltofu.db.TimerTable.java
com.cooltofu.util.Base64DecoderException.java
com.cooltofu.util.Base64.java
com.cooltofu.util.IabException.java
com.cooltofu.util.IabHelper.java
com.cooltofu.util.IabResult.java
com.cooltofu.util.Inventory.java
com.cooltofu.util.Purchase.java
com.cooltofu.util.Security.java
com.cooltofu.util.SkuDetails.java