package com.henleyb.teatimer;
import java.util.concurrent.TimeUnit;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
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;
// MediaPlayer playWhistle;
private static long userTime;
private MyCount newTimer;
private ToggleButton brewButton;
@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);
// 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 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();
} else {
// Toggle light is on
Log.d("ToggleCheck", "Toggle on");
timerInit(userTime);
}
}
});
// Fire up the timer for the first launch.
timerInit(userTime);
}
@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 static void updateView(long millisUntilFinished) {
if (millisUntilFinished == 0) {
textViewShowSeek.setText("Finished!");
shaker(500);
} else {
textViewShowSeek.setText(String
.valueOf(textViewTime(millisUntilFinished)));
seekBarTimerSet.setProgress((int) millisUntilFinished);
}
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void shaker(int amount) {
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(amount);
if (v.hasVibrator()) {
System.out.println("has vibrator");
}
}
}