package com.example.Maps013;

import java.util.Calendar;

import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.text.format.DateFormat;
import android.widget.Chronometer.OnChronometerTickListener;
//import android.widget.Chronometer.OnChronometerTickListener;
import android.widget.TextView;
import android.widget.Chronometer;


public class Chrono extends Activity
{

    TextView tvData;
    Chronometer azerr;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chronometer_layout);

        tvData = (TextView) findViewById(R.id.tv_data);
        azerr = (Chronometer) findViewById(R.id.blabla);
        
     //   chrono = (Chronometer) findViewById(R.id.chronometer);

        startChronometer();
        
        
        azerr.setOnChronometerTickListener(new OnChronometerTickListener() 
        {
            public void onChronometerTick(Chronometer cArg) {
                Calendar calendar = Calendar.getInstance();
                calendar.set(Calendar.HOUR_OF_DAY, 0);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                calendar.set(Calendar.MILLISECOND, getCurrentMiliSecondsOfChronometer());
                tvData.setText(DateFormat.format("HH:mm:ss", calendar.getTime()));
            }

			
        });
        
        
        
    }

    private int getCurrentMiliSecondsOfChronometer() {
        int stoppedMilliseconds = 0;
        String chronoText = azerr.getText().toString();
        String array[] = chronoText.split(":");
        if (array.length == 2) {
            stoppedMilliseconds = Integer.parseInt(array[0]) * 60 * 1000 + Integer.parseInt(array[1]) * 1000;
        } else if (array.length == 3) {
            stoppedMilliseconds =
                    Integer.parseInt(array[0]) * 60 * 60 * 1000 + Integer.parseInt(array[1]) * 60 * 1000
                            + Integer.parseInt(array[2]) * 1000;
        }
        return stoppedMilliseconds;
    }

    private void startChronometer()
    {
        int stoppedMilliseconds = getCurrentMiliSecondsOfChronometer();
        azerr.setBase(SystemClock.elapsedRealtime() - stoppedMilliseconds);
        azerr.start();
    }
}