MyChronometer.java :  » Game » guessthenumber » com » saatcioglu » android » guessthenumber » Android Open Source

Android Open Source » Game » guessthenumber 
guessthenumber » com » saatcioglu » android » guessthenumber » MyChronometer.java
/*
  Copyright 2009 Omer Saatcioglu
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

  $Author$
  $Revision$
  $LastChangedDate$
   
 */

package com.saatcioglu.android.guessthenumber;

import android.content.Context;
import android.graphics.Canvas;
import android.os.SystemClock;
import android.util.AttributeSet;

public class MyChronometer extends android.widget.Chronometer {

  boolean isTimerStarted = false;
  boolean isTimerPaused = false;
  private long lngBaseTime = 0;
  private long lngElapsedTime = 0;

  public MyChronometer(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  public MyChronometer(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
  }

  public MyChronometer(Context context) {
    super(context);
  }

  @Override
  public void start() {
    if (!isTimerStarted) {
      lngBaseTime = SystemClock.elapsedRealtime();
      _start();
      isTimerStarted = true;
    }
  }

  public void start(long lngTimePasssed) {
    if (!isTimerStarted) {
      lngBaseTime = SystemClock.elapsedRealtime() - lngTimePasssed;
      _start();
      isTimerStarted = true;
    }
  }

  private void _start() {
    setBase(lngBaseTime);
    super.start();
  }

  @Override
  public void stop() {
    if (isTimerStarted) {
      isTimerStarted = false;
      super.stop();
    }
  }

  public void pause() {
    if (!isTimerPaused) {
      lngElapsedTime = SystemClock.elapsedRealtime() - lngBaseTime;
      isTimerPaused = true;
      super.stop();
    }
  }

  public void resume() {
    if (isTimerPaused) {
      lngBaseTime = SystemClock.elapsedRealtime() - lngElapsedTime;
      lngElapsedTime = 0;
      _start();
      isTimerPaused = false;
    }
  }

  public void reset() {
    setBase(SystemClock.elapsedRealtime());
  }

  public long getElapsedTime() {
    return lngElapsedTime;
  }

  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (isTimerStarted && !isTimerPaused && SMBGuesstheNumber.nActiveMaxTime != 0) {
      long diff = (SystemClock.elapsedRealtime() - lngBaseTime) / 1000;
      if (diff >= SMBGuesstheNumber.nActiveMaxTime) {
        GfxGuesstheNumber.getInstance().gameFailed();
      }
    }
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.