Oxygen Monitor – Full Code
Note that the first line (the package name) will depend on what you named your application.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
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(); } } |