Crossfit stopwatch: Main timer TextView controller

In my stopwatch application, when the UI is in front, the main timer will be updated very frequently. In order to update it and to avoid blocking the main thread, I created a class, which will handle this issue.

The class looks like this (only pasting the method names, whole code is on BitBucket – see below):

public class MainTimerController extends Handler {
  private int mLastHours;
  private int mLastMinutes;
  private int mLastSeconds;
  private int mLastSeconds10;
	
  private boolean mCountingDown = false;
	
  private TextView mTextView;

  public static final int MSG_HOURS     = 1;
  public static final int MSG_MINUTES   = 2;
  public static final int MSG_SECONDS   = 3;
  public static final int MSG_SECONDS10 = 4;
  public static final int MSG_ALL       = 5;
	
  MainTimerController(TextView widget) {...}

  @Override public void handleMessage(Message msg) {...}
	
  public void clearTimer() {...}
	
  public void setTimer(int hours, int minutes,
                       int seconds, int seconds10) {...}
	
  public void setCountDown(boolean countDown) {...}
	
  // ---- private helper methods ----
	
  private void updateHours(Message msg) {...}
	
  private void updateMinutes(Message msg) {...}

  private void updateSeconds(Message msg) {...}
	
  private void updateSeconds10(Message msg) {...}
	
  private void updateAll(Message msg) {...}
}

The main algorithm to update the TextView looks like this:

private void updateAll(Message msg) {
  int time = msg.arg1;

  int seconds10 = time % 10;
  int seconds   = time / 10;
  int minutes   = seconds / 60;
  seconds = seconds % 60;
  int hours     = minutes / 60;
  minutes = minutes % 60;
		
  mTextView.setText((mCountingDown ? "-" : "")
                  + (hours > 9 ? hours : "0" + hours) + ":"
                  + (minutes > 9 ? minutes : "0" + minutes) + ":"
                  + (seconds > 9 ? seconds : "0" + seconds) + "."
                  + (seconds10 % 10)
  );
		
  mLastHours     = hours;
  mLastMinutes   = minutes;
  mLastSeconds   = seconds;
  mLastSeconds10 = seconds10;
}

In the Message’s argument I send the elapsed hundreds of seconds, so in the updateAll() method is the processing and update procedure.

BitBucket: Crossfit Stopwatch

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s