Oxygen Monitor Full Code

Note that the first line (the package name) will depend on what you named your application.

package org.spaceskills.oxygauge;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.CountDownTimer;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;


public class MainActivity extends Activity {

    private ProgressBar pgOxy;

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.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(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } // Takes a number of seconds // Returns formatted minutes and seconds private String convertTime (int seconds){ // Convert to whole minutes and seconds remaining int minutes = (int) Math.floor(seconds / 60); seconds = seconds - (minutes*60); // Format, padding single digit seconds with a zero String formatted = String.valueOf(minutes) + ":"; if (seconds < 10) formatted += "0"; formatted += String.valueOf(seconds); return formatted; // Return result } public void startUsingOxygen (View v){ // Get references to the user interface elements EditText txtVolume = (EditText) findViewById(R.id.editText); TextView txtTime = (TextView) findViewById(R.id.textView); // Read volume, and set a default value if empty String volumeText = txtVolume.getText().toString(); if ( volumeText.isEmpty() ) volumeText = "0"; // Convert text entered into a number (volume), and // calculate the time that would allow (at 0.83ltr per minute) double volume = Double.valueOf( volumeText ); if (volume < 0) volume = 0; double time = volume / 0.83; // Display the time in minutes - formatted to 2 decimal places txtTime.setText( convertTime( (int)(time*60) ) ); // Get reference to progress bar in interface pgOxy = (ProgressBar) findViewById(R.id.progressBar); // Reset everything TextView warning = (TextView) findViewById(R.id.textView2); warning.setText(""); pgOxy.getProgressDrawable().setColorFilter(Color.GREEN, PorterDuff.Mode.SRC_IN); // Convert time (currently in minutes) to seconds int s = (int) Math.ceil(time * 60.0); // Set the progress bar maximum pgOxy.setMax(s); // Create a CountDownTimer. Takes:

// number of milliseconds to count for in total (s*1000) // time interval between ticks (1000 milliseconds = 1 sec) new CountDownTimer(s * 1000, 1000) { public void onTick(long millisUntilFinished) { int secUntilFinished = (int) millisUntilFinished / 1000; // Show current progress - how long till finished? pgOxy.setProgress(secUntilFinished); TextView txtTime = (TextView) findViewById(R.id.textView); txtTime.setText( convertTime( secUntilFinished ) ); // If less than 30 seconds left, then raise alarm if (secUntilFinished < 30) { pgOxy.getProgressDrawable().setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN); TextView warning = (TextView) findViewById(R.id.textView2); // Flash warning on and off (based on whether even number of seconds) if (secUntilFinished % 2 == 0) warning.setText("OXYGEN LOW!"); else warning.setText("** OXYGEN LOW! **"); } } public void onFinish() { // Tidy up at the end pgOxy.setProgress(0); TextView warning = (TextView) findViewById(R.id.textView2); warning.setText("OXYGEN DEPLETED!"); TextView txtTime = (TextView) findViewById(R.id.textView); txtTime.setText( "0:00" ); } }.start(); } }