Android Open Source - Intervaller Main Activity






From Project

Back to project page Intervaller.

License

The source code is released under:

GNU General Public License

If you think the Android project Intervaller 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.bbzoftware.interwaller;
/*  ww w .  j  a  va 2 s . c o  m*/
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.os.Handler;
import android.widget.Toast;

import java.util.logging.LogRecord;


public class MainActivity extends Activity {

    private Button startButton;
    private Button pauseButton;
    private Button stopButton;

    private EditText intervalBMinsValue;
    private EditText intervalAMinsValue;
    private EditText intervalBSecsValue;
    private EditText intervalASecsValue;
    private EditText intervalCounterValue;

    private TextView timerA;
    private TextView timerB;

    private long startTime = 0L;
    private long timeInMilliseconds = 0L;
    private long timeSwapBuff = 0L;
    private long updatedTime = 0L;
    private int secsA;
    private int minsA;
    private int secsB;
    private int minsB;
    private int intervalCounter;

    boolean isIntervalA = true;

    private MediaPlayer mediaPlayerBeep;
    private MediaPlayer mediaPlayerTada;

    private Handler myHandler = new Handler();

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

        startButton = (Button) findViewById(R.id.button_start);
        pauseButton = (Button) findViewById(R.id.button_pause);
        pauseButton.setEnabled(false);
        stopButton = (Button) findViewById(R.id.button_stop);
        stopButton.setEnabled(false);

        intervalAMinsValue = (EditText) findViewById(R.id.edit_interval_a_mins);
        intervalBMinsValue = (EditText) findViewById(R.id.edit_interval_b_mins);
        intervalASecsValue = (EditText) findViewById(R.id.edit_interval_a_secs);
        intervalBSecsValue = (EditText) findViewById(R.id.edit_interval_b_secs);
        intervalCounterValue = (EditText) findViewById(R.id.edit_interval_counter);

        timerA = (TextView) findViewById(R.id.time_A);
        timerB = (TextView) findViewById(R.id.time_B);

        mediaPlayerBeep = MediaPlayer.create(this, R.raw.beep);
        mediaPlayerTada = MediaPlayer.create(this, R.raw.tada);

        startButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(intervalCounterValue.getText().length() == 0 || intervalAMinsValue.getText().length() == 0 ||
                        intervalBMinsValue.getText().length() == 0 || intervalASecsValue.getText().length() == 0 ||
                        intervalBSecsValue.getText().length() == 0)
                    Toast.makeText(MainActivity.this, "This fields cannot be blank", Toast.LENGTH_SHORT).show();
                else {
                    if(Integer.valueOf(intervalAMinsValue.getText().toString()) > 59)
                        intervalAMinsValue.setText("59");
                    if(Integer.valueOf(intervalASecsValue.getText().toString()) > 59)
                        intervalASecsValue.setText("59");
                    if(Integer.valueOf(intervalBMinsValue.getText().toString()) > 59)
                        intervalBMinsValue.setText("59");
                    if(Integer.valueOf(intervalBSecsValue.getText().toString()) > 59)
                        intervalBSecsValue.setText("59");
                    intervalCounter = 0;
                    startTime = SystemClock.uptimeMillis();
                    myHandler.postDelayed(intervalMain, 0);
                    intervalAMinsValue.setEnabled(false);
                    intervalBMinsValue.setEnabled(false);
                    intervalASecsValue.setEnabled(false);
                    intervalBSecsValue.setEnabled(false);
                    intervalCounterValue.setEnabled(false);
                    stopButton.setEnabled(true);
                    pauseButton.setEnabled(true);
                    startButton.setEnabled(false);
                }
            }
        });

        pauseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                timeSwapBuff += timeInMilliseconds;
                myHandler.removeCallbacks(intervalMain);
                intervalAMinsValue.setEnabled(true);
                intervalBMinsValue.setEnabled(true);
                intervalASecsValue.setEnabled(true);
                intervalBSecsValue.setEnabled(true);
                intervalCounterValue.setEnabled(true);
                pauseButton.setEnabled(false);
                startButton.setEnabled(true);
            }
        });

        stopButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                myHandler.removeCallbacks(intervalMain);
                stopButton.setText("Stop");
                timerA.setText("00:00");
                timerB.setText("00:00");
                startTime = 0L;
                timeInMilliseconds = 0L;
                timeSwapBuff = 0L;
                updatedTime = 0L;
                intervalAMinsValue.setEnabled(true);
                intervalBMinsValue.setEnabled(true);
                intervalASecsValue.setEnabled(true);
                intervalBSecsValue.setEnabled(true);
                intervalCounterValue.setEnabled(true);
                pauseButton.setEnabled(false);
                stopButton.setEnabled(false);
                startButton.setEnabled(true);
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private Runnable intervalMain = new Runnable() {
        @Override
        public void run() {
            if (isIntervalA && Integer.valueOf(intervalCounterValue.getText().toString()) > intervalCounter) {
                timeInMilliseconds = SystemClock.uptimeMillis() - startTime;

                updatedTime = timeSwapBuff + timeInMilliseconds;

                secsA = (int) (updatedTime / 1000);
                minsA = secsA / 60;
                secsA = secsA % 60;
                if(minsA == Integer.valueOf(intervalAMinsValue.getText().toString()) &&
                        secsA == Integer.valueOf(intervalASecsValue.getText().toString())) {
                    startTime = SystemClock.uptimeMillis();
                    timeInMilliseconds = 0L;
                    timeSwapBuff = 0L;
                    updatedTime = 0L;
                    mediaPlayerBeep.start();
                    isIntervalA = false;
                }
//            int milliseconds = (int) (updatedTime % 1000);
                timerA.setText("" + String.format("%02d", minsA) + ":"
                        + String.format("%02d", secsA));
                myHandler.postDelayed(this, 0);
            } else if(!isIntervalA && Integer.valueOf(intervalCounterValue.getText().toString()) > intervalCounter) {
                timeInMilliseconds = SystemClock.uptimeMillis() - startTime;

                updatedTime = timeSwapBuff + timeInMilliseconds;

                secsB = (int) (updatedTime / 1000);
                minsB = secsB / 60;
                secsB = secsB % 60;
                if(minsB == Integer.valueOf(intervalBMinsValue.getText().toString()) &&
                        secsB == Integer.valueOf(intervalBSecsValue.getText().toString())) {
                    intervalCounter++;
                    startTime = SystemClock.uptimeMillis();
                    timeInMilliseconds = 0L;
                    timeSwapBuff = 0L;
                    updatedTime = 0L;
                    if(Integer.valueOf(intervalCounterValue.getText().toString()) == intervalCounter) {
                        mediaPlayerTada.start();
                        stopButton.setText("Reset");
                        pauseButton.setEnabled(false);
                    } else {
                        mediaPlayerBeep.start();
                    }
                    isIntervalA = true;
                }
//            int milliseconds = (int) (updatedTime % 1000);
                timerB.setText("" + String.format("%02d", minsB) + ":"
                        + String.format("%02d", secsB));
                myHandler.postDelayed(this, 0);
            }


        }
    };
}




Java Source Code List

com.bbzoftware.interwaller.ApplicationTest.java
com.bbzoftware.interwaller.MainActivity.java