package com.henleyb.teatimer;
import java.util.concurrent.TimeUnit;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
public class TeaTimerMain extends Activity implements OnSeekBarChangeListener {
private static TextView textViewShowSeek;
private static SeekBar seekBarTimerSet;
public static MediaPlayer playWhistle;
private static long userTime;
private MyCount newTimer;
private ToggleButton brewButton;
static Vibrator mVibrate;
public static TeaReadyAlert bob;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_tea_timer);
brewButton = (ToggleButton) findViewById(R.id.butGo);
seekBarTimerSet = (SeekBar) findViewById(R.id.seekBarTimerSet);
textViewShowSeek = (TextView) findViewById(R.id.tvShowSeek);
// playWhistle = MediaPlayer.create(this, R.raw.whistle1 );
seekBarTimerSet.setOnSeekBarChangeListener(this);
// playWhistle.setDataSource(R.raw.whistle1);
// playWhistle.setOnPreparedListener(this);
// playWhistle.prepareAsync();
// Set up and get the users preference for brew time.
SharedPreferences userDetails = this.getSharedPreferences(
"userdetails", MODE_PRIVATE);
userTime = userDetails.getLong("userTime", 2000);
updateView(userTime);
// Set up the vibration
mVibrate = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
bob = new TeaReadyAlert(getApplicationContext());
// Set up Toggle Button listener and logic
brewButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
// Toggle light is off
Log.d("ToggleCheck", "Toggle off");
newTimer.cancel();
mVibrate.cancel();
} else {
// Toggle light is on
Log.d("ToggleCheck", "Toggle on");
timerInit(userTime);
}
}
});
// Fire up the timer for the first launch.
timerInit(userTime);
}
@Override
public void onPause() {
super.onPause(); // Always call the superclass method first
}
@Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater blowUp = getMenuInflater();
blowUp.inflate(R.menu.tea_settings_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.aboutUs:
Intent i = new Intent("com.henleyb.teatimer.ABOUT");
startActivity(i);
break;
case R.id.savePrefs:
TeaTimerMain.setDefaults(this);
break;
}
return false;
}
@SuppressLint("DefaultLocale")
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
if (fromUser == true) {
userTime = progress;
updateView(progress);
System.out.println("Progress Changed, userTime: " + userTime);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
if (newTimer != null) {
newTimer.cancel();
brewButton.setChecked(true);
}
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
newTimer = new MyCount(userTime, 1000);
}
public void timerInit(Long mSeekBarAmount) {
if (newTimer != null && brewButton.isChecked()) {
newTimer.cancel();
} else {
newTimer = new MyCount(mSeekBarAmount, 1000);
newTimer.start();
}
}
public static void setDefaults(Context userPrefs) {
// fired by button on the Main View
SharedPreferences userDetails = userPrefs.getSharedPreferences(
"userdetails", MODE_PRIVATE);
Editor edit = userDetails.edit();
edit.clear();
edit.putLong("userTime", userTime);
// edit.putString("password", txtPass.getText().toString().trim());
edit.commit();
Toast.makeText(
userPrefs,
"Your ideal brew time of " + textViewTime(userTime)
+ " has been saved.", Toast.LENGTH_LONG).show();
}
// converts milliseconds to time for display
@SuppressLint("DefaultLocale")
static String textViewTime(long convertVar) {
String formattedVar;
formattedVar = String.format(
"%d min %d sec",
TimeUnit.MILLISECONDS.toMinutes(convertVar),
TimeUnit.MILLISECONDS.toSeconds(convertVar)
- TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS
.toMinutes(convertVar)));
return formattedVar;
}
public void onPrepared(MediaPlayer player) {
playWhistle.start();
}
public void updateView(long millisUntilFinished) {
if (millisUntilFinished == 0) {
textViewShowSeek.setText("Finished!");
shaker();
// Pop up box to be implemented
// bob.show(this, "test");
alertbox("Hello", "hello");
} else {
if (mVibrate != null) {
mVibrate.cancel();
}
textViewShowSeek.setText(String
.valueOf(textViewTime(millisUntilFinished)));
seekBarTimerSet.setProgress((int) millisUntilFinished);
}
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static void shaker() {
if (mVibrate.hasVibrator()) {
System.out.println("has vibrator");
mVibrate.vibrate(new long[] { 0, 500, 1000, 1000, 1000 }, 0);
}
}
@SuppressWarnings("deprecation")
protected void alertbox(String title, String mymessage)
{
new AlertDialog.Builder(this)
.setMessage(mymessage)
.setTitle(title)
.setCancelable(true)
.setNeutralButton(android.R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton){}
})
.show();
}
// see http://androidsnippets.com/display-an-alert-box
}