SweetDreamsFull.java

renamed all field variables to follow proper syntax
created isFull a protected static field boolean to change certain things between full and lite 
i set the default run time to 30 minutes

created sounds_lite.xml us be used with sweet dreams lite

created settings_lite.xml to be used for settings for the lite version

TimePickerPreference.java
i changed the method for saving time to a method that only uses one preference key\
i set the default time to 30 minutes
This commit is contained in:
2011-02-11 14:04:53 +00:00
parent f75dd9fc39
commit bb5957b7d8
5 changed files with 102 additions and 62 deletions

View File

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="sounds_lite">
<item>Rainy River</item>
<item>Air Conditioner</item>
</string-array>
</resources>

View File

@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="White Noise Options">
<ListPreference android:dialogTitle="Pick A Sound"
android:entryValues="@array/sounds_lite"
android:entries="@array/sounds_lite"
android:key="sounds"
android:title="Sounds"/>
</PreferenceCategory>
<PreferenceCategory
android:title="Timer Options">
<CheckBoxPreference
android:title="Enable"
android:summary="Enable Timer"
android:defaultValue="false"
android:key="timer_enabled"
/>
<com.TwentyCodes.android.SweetDreamsLib.TimePickerPreference
android:title="Timer"
android:enabled="false"
android:key="timer_length"
/>
<CheckBoxPreference
android:title="Exit On Finish"
android:summary="Closes Application When Timer Finishes"
android:defaultValue="false"
android:key="timer_exit_on_finish"
android:dependency="timer_enabled"
/>
</PreferenceCategory>
</PreferenceScreen>

View File

@@ -47,7 +47,7 @@ public class Settings extends PreferenceActivity implements OnPreferenceChangeLi
getPreferenceManager().setSharedPreferencesName(SETTINGS); getPreferenceManager().setSharedPreferencesName(SETTINGS);
//load preferences xml //load preferences xml
this.addPreferencesFromResource(R.xml.settings); this.addPreferencesFromResource( SweetDreamsFull.isFUll ? R.xml.settings : R.xml.settings_lite);
//get shared_prefs //get shared_prefs
SharedPreferences settings = getPreferenceManager().getSharedPreferences(); SharedPreferences settings = getPreferenceManager().getSharedPreferences();
@@ -69,9 +69,11 @@ public class Settings extends PreferenceActivity implements OnPreferenceChangeLi
Preference timerLength = findPreference(TIMER_LENGTH); Preference timerLength = findPreference(TIMER_LENGTH);
timerLength.setOnPreferenceChangeListener(this); timerLength.setOnPreferenceChangeListener(this);
try { try {
timerLength.setSummary(settings.getInt(TIMER_LENGTH, 5)); if(SweetDreamsFull.isFUll)
timerLength.setSummary(settings.getInt(TIMER_LENGTH, 5));
else
timerLength.setSummary(30);
} catch (Exception e) { } catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
} }

View File

@@ -30,17 +30,18 @@ import android.widget.TextView;
public class SweetDreamsFull extends Activity implements OnClickListener, OnSeekBarChangeListener{ public class SweetDreamsFull extends Activity implements OnClickListener, OnSeekBarChangeListener{
private boolean isPlaying; private boolean isPlaying;
private ImageButton play_pause; private ImageButton mPlayPauseButton;
private AudioManager mAudioManager; private AudioManager mAudioManager;
private final int STREAM = AudioManager.STREAM_MUSIC; private final int STREAM = AudioManager.STREAM_MUSIC;
protected static MediaPlayer mp; protected static MediaPlayer mMediaPlayer;
private PostMortemReportExceptionHandler mDamageReport; private PostMortemReportExceptionHandler mDamageReport;
private final int SETTINGS = Menu.FIRST; private final int SETTINGS = Menu.FIRST;
private final int QUIT = Menu.FIRST +1; private final int QUIT = Menu.FIRST +1;
private SharedPreferences settings; private SharedPreferences mSettings;
protected static final String TAG = "WhiteNoise"; protected static final String TAG = "WhiteNoise";
private Timer timer; private Timer mTimer;
protected static TextView timeLeft; protected static TextView mTimeLeftTextView;
protected static boolean isFUll = true;
/** /**
* adjusts the provided Stream volume to the provided level * adjusts the provided Stream volume to the provided level
@@ -84,32 +85,32 @@ public class SweetDreamsFull extends Activity implements OnClickListener, OnSeek
private void loadSound(){ private void loadSound(){
Log.i(TAG,"loadSound()"); Log.i(TAG,"loadSound()");
try { try {
mp.reset(); mMediaPlayer.reset();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
String sound = settings.getString("sounds", Settings.SOUND_RIVER); String sound = mSettings.getString("sounds", Settings.SOUND_RIVER);
Log.v(TAG,"sound = "+ sound); Log.v(TAG,"sound = "+ sound);
if (sound != null){ if (sound != null){
if (sound.equals(Settings.SOUND_BEACH)){ if (sound.equals(Settings.SOUND_BEACH)){
mp = MediaPlayer.create(this, R.raw.beach); mMediaPlayer = MediaPlayer.create(this, R.raw.beach);
mp.setLooping(true); mMediaPlayer.setLooping(true);
} }
if (sound.equals(Settings.SOUND_AC)){ if (sound.equals(Settings.SOUND_AC)){
mp = MediaPlayer.create(this, R.raw.ac); mMediaPlayer = MediaPlayer.create(this, R.raw.ac);
mp.setLooping(true); mMediaPlayer.setLooping(true);
} }
if (sound.equals(Settings.SOUND_CRICKETS)){ if (sound.equals(Settings.SOUND_CRICKETS)){
mp = MediaPlayer.create(this, R.raw.crickets); mMediaPlayer = MediaPlayer.create(this, R.raw.crickets);
mp.setLooping(true); mMediaPlayer.setLooping(true);
} }
if (sound.equals(Settings.SOUND_FALLS)){ if (sound.equals(Settings.SOUND_FALLS)){
mp = MediaPlayer.create(this, R.raw.falls); mMediaPlayer = MediaPlayer.create(this, R.raw.falls);
mp.setLooping(true); mMediaPlayer.setLooping(true);
} }
if (sound.equals(Settings.SOUND_RIVER)){ if (sound.equals(Settings.SOUND_RIVER)){
mp = MediaPlayer.create(this, R.raw.river); mMediaPlayer = MediaPlayer.create(this, R.raw.river);
mp.setLooping(true); mMediaPlayer.setLooping(true);
} }
} }
} }
@@ -127,17 +128,16 @@ public class SweetDreamsFull extends Activity implements OnClickListener, OnSeek
* change the button background from a pause symbol to a play symbol, set isPlaying to false, and stop playing the sound track * change the button background from a pause symbol to a play symbol, set isPlaying to false, and stop playing the sound track
*/ */
if (!isPlaying){ if (!isPlaying){
play_pause.setBackgroundDrawable(getResources().getDrawable(R.drawable.pause_button)); mPlayPauseButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.pause_button));
isPlaying = true; isPlaying = true;
loadSound(); loadSound();
mp.start(); mMediaPlayer.start();
if (settings.getBoolean(Settings.TIMER_ENABLED, false)){ if (mSettings.getBoolean(Settings.TIMER_ENABLED, false)){
Log.v(TAG,"Starting timer"); Log.v(TAG,"Starting timer");
long time = settings.getInt("timer_length_hour", 0) * 3600000; long time = + (mSettings.getInt("timer_length", 30) * 60000);
time = time + (settings.getInt("timer_length_minute", 5) * 60000);
Log.v(TAG,"time = " + time); Log.v(TAG,"time = " + time);
timer = new Timer(time); mTimer = new Timer(time);
timer.start(); mTimer.start();
} }
} else { } else {
stopPlaying(); stopPlaying();
@@ -163,8 +163,8 @@ public class SweetDreamsFull extends Activity implements OnClickListener, OnSeek
Thread.setDefaultUncaughtExceptionHandler(mDamageReport); Thread.setDefaultUncaughtExceptionHandler(mDamageReport);
//initialize the play_pause button and set onClickListener //initialize the play_pause button and set onClickListener
play_pause = (ImageButton) findViewById(R.id.play_pause_button); mPlayPauseButton = (ImageButton) findViewById(R.id.play_pause_button);
play_pause.setOnClickListener(this); mPlayPauseButton.setOnClickListener(this);
//initialize audio manager so we can control stream volumes //initialize audio manager so we can control stream volumes
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
@@ -176,7 +176,7 @@ public class SweetDreamsFull extends Activity implements OnClickListener, OnSeek
sbVolume.setProgress(mAudioManager.getStreamVolume(STREAM)); sbVolume.setProgress(mAudioManager.getStreamVolume(STREAM));
//load shared_prefs //load shared_prefs
settings = getSharedPreferences(Settings.SETTINGS, 0); mSettings = getSharedPreferences(Settings.SETTINGS, 0);
loadSound(); loadSound();
@@ -202,7 +202,7 @@ public class SweetDreamsFull extends Activity implements OnClickListener, OnSeek
*/ */
@Override @Override
public void onDestroy(){ public void onDestroy(){
adjustVolume(STREAM,settings.getInt(Settings.MUSIC_VOLUME, 0)); adjustVolume(STREAM,mSettings.getInt(Settings.MUSIC_VOLUME, 0));
super.onDestroy(); super.onDestroy();
} }
@@ -264,7 +264,7 @@ public class SweetDreamsFull extends Activity implements OnClickListener, OnSeek
public void onResume(){ public void onResume(){
super.onResume(); super.onResume();
TextView trackTitle = (TextView) findViewById(R.id.track_title); TextView trackTitle = (TextView) findViewById(R.id.track_title);
trackTitle.setText(settings.getString(Settings.SOUNDS,"Rainy River")); trackTitle.setText(mSettings.getString(Settings.SOUNDS,"Rainy River"));
} }
@Override @Override
@@ -290,7 +290,7 @@ public class SweetDreamsFull extends Activity implements OnClickListener, OnSeek
public void onStop(){ public void onStop(){
stopPlaying(); stopPlaying();
try { try {
mp.release(); mMediaPlayer.release();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
@@ -312,7 +312,7 @@ public class SweetDreamsFull extends Activity implements OnClickListener, OnSeek
* @author ricky barrette * @author ricky barrette
*/ */
private boolean saveStreamVolume(int stream){ private boolean saveStreamVolume(int stream){
Editor edit = settings.edit(); Editor edit = mSettings.edit();
edit.putInt(Settings.MUSIC_VOLUME, mAudioManager.getStreamVolume(stream)); edit.putInt(Settings.MUSIC_VOLUME, mAudioManager.getStreamVolume(stream));
return edit.commit(); return edit.commit();
} }
@@ -323,18 +323,18 @@ public class SweetDreamsFull extends Activity implements OnClickListener, OnSeek
* @author ricky barrette * @author ricky barrette
*/ */
private void stopPlaying(){ private void stopPlaying(){
play_pause.setBackgroundDrawable(getResources().getDrawable(R.drawable.play_button)); mPlayPauseButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.play_button));
isPlaying = false; isPlaying = false;
try { try {
mp.stop(); mMediaPlayer.stop();
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
e.printStackTrace(); e.printStackTrace();
Log.e(TAG,"failed to stop media player"); Log.e(TAG,"failed to stop media player");
} }
if (timer != null) { if (mTimer != null) {
timer.cancel(); mTimer.cancel();
timer = null; mTimer = null;
timeLeft.setText(""); mTimeLeftTextView.setText("");
} }
} }
@@ -353,13 +353,13 @@ public class SweetDreamsFull extends Activity implements OnClickListener, OnSeek
super(millisInFuture, 1000l); super(millisInFuture, 1000l);
Log.i(TAG,"Timer()"); Log.i(TAG,"Timer()");
Log.v(TAG,"millisInFuture = "+ millisInFuture); Log.v(TAG,"millisInFuture = "+ millisInFuture);
timeLeft = (TextView) findViewById(R.id.time_left); mTimeLeftTextView = (TextView) findViewById(R.id.time_left);
int hours = (int) (millisInFuture / 3600000); int hours = (int) (millisInFuture / 3600000);
millisInFuture = millisInFuture - (hours * 3600000); millisInFuture = millisInFuture - (hours * 3600000);
int minutes = (int) ( millisInFuture / 60000); int minutes = (int) ( millisInFuture / 60000);
int seconds = (int) (millisInFuture % 60000); int seconds = (int) (millisInFuture % 60000);
seconds = seconds / 1000; seconds = seconds / 1000;
timeLeft.setText(hours +" : "+ padTime(minutes) +" : "+ padTime(seconds)); mTimeLeftTextView.setText(hours +" : "+ padTime(minutes) +" : "+ padTime(seconds));
} }
/** /**
@@ -371,10 +371,10 @@ public class SweetDreamsFull extends Activity implements OnClickListener, OnSeek
@Override @Override
public void onFinish() { public void onFinish() {
Log.i(TAG,"onFinish()"); Log.i(TAG,"onFinish()");
timeLeft.setText(""); mTimeLeftTextView.setText("");
stopPlaying(); stopPlaying();
//if the user enables exit on finish, then kill the application //if the user enables exit on finish, then kill the application
if(settings.getBoolean(Settings.TIMER_EXIT_ON_FINISH, false)){ if(mSettings.getBoolean(Settings.TIMER_EXIT_ON_FINISH, false)){
Log.v(TAG,"exit on finish enabled, calling finish()"); Log.v(TAG,"exit on finish enabled, calling finish()");
finish(); finish();
} }
@@ -394,7 +394,7 @@ public class SweetDreamsFull extends Activity implements OnClickListener, OnSeek
int minutes = (int) ( millisUntilFinished / 60000); int minutes = (int) ( millisUntilFinished / 60000);
int seconds = (int) (millisUntilFinished % 60000); int seconds = (int) (millisUntilFinished % 60000);
seconds = seconds / 1000; seconds = seconds / 1000;
timeLeft.setText(hours +" : "+ padTime(minutes) +" : "+ padTime(seconds)); mTimeLeftTextView.setText(hours +" : "+ padTime(minutes) +" : "+ padTime(seconds));
} }
/** /**

View File

@@ -19,52 +19,43 @@ import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener; import android.widget.TimePicker.OnTimeChangedListener;
/** /**
* creates a time picker preference that saves the hour and minutes preferred as getKey()+"_hour" and getKey()+"_minute" * creates a time picker preference
* NOTE: it might be better to save the combined hour and minutes saves as minutes (1 hour + 30 minutes would be saved as 90 minutes)
* @author ricky barrette * @author ricky barrette
*/ */
public class TimePickerPreference extends Preference implements OnTimeChangedListener{ public class TimePickerPreference extends Preference implements OnTimeChangedListener{
// private TimePicker timePicker;
private final String TAG = "TimePickerPreference"; private final String TAG = "TimePickerPreference";
private SharedPreferences settings; private SharedPreferences settings;
/** /**
* creates a time picker preference that saves the hour and minutes preferred as getKey()+"_hour" and getKey()+"_minute" * creates a time picker preference
* NOTE: it might be better to save the combined hour and minutes saves as minutes (1 hour + 30 minutes would be saved as 90 minutes)
* @param context * @param context
*/ */
public TimePickerPreference(Context context) { public TimePickerPreference(Context context) {
super(context); super(context);
// timePicker = new TimePicker(getContext());
} }
/** /**
* creates a time picker preference that saves the hour and minutes preferred as getKey()+"_hour" and getKey()+"_minute" * creates a time picker preference
* NOTE: it might be better to save the combined hour and minutes saves as minutes (1 hour + 30 minutes would be saved as 90 minutes)
* @param context * @param context
* @param attrs * @param attrs
*/ */
public TimePickerPreference(Context context, AttributeSet attrs) { public TimePickerPreference(Context context, AttributeSet attrs) {
super(context, attrs); super(context, attrs);
// timePicker = new TimePicker(getContext());
} }
/** /**
* creates a time picker preference that saves the hour and minutes preferred as getKey()+"_hour" and getKey()+"_minute" * creates a time picker preference
* NOTE: it might be better to save the combined hour and minutes saves as minutes (1 hour + 30 minutes would be saved as 90 minutes)
* @param context * @param context
* @param attrs * @param attrs
* @param defStyle * @param defStyle
*/ */
public TimePickerPreference(Context context, AttributeSet attrs, int defStyle) { public TimePickerPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle); super(context, attrs, defStyle);
// timePicker = new TimePicker(getContext());
} }
/** /**
* saves the current time selected by the user into the shared_prefs keys of getKey()+"_hour" and getKey()+"_minute" * saves the current time selected by the user into the shared_prefs keys of getKey()+"_hour" and getKey()+"_minute"
* NOTE: it might be better to save the combined hour and minutes saves as minutes (1 hour + 30 minutes would be saved as 90 minutes)
* (non-Javadoc) * (non-Javadoc)
* @see android.widget.TimePicker.OnTimeChangedListener#onTimeChanged(android.widget.TimePicker, int, int) * @see android.widget.TimePicker.OnTimeChangedListener#onTimeChanged(android.widget.TimePicker, int, int)
* @param view * @param view
@@ -76,8 +67,7 @@ public class TimePickerPreference extends Preference implements OnTimeChangedLis
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
Log.i(TAG,"onTimeChanged"); Log.i(TAG,"onTimeChanged");
SharedPreferences.Editor editor = getEditor(); SharedPreferences.Editor editor = getEditor();
editor.putInt(getKey()+"_hour", hourOfDay); editor.putInt(getKey(), ((hourOfDay * 60) + minute));
editor.putInt(getKey()+"_minute", minute);
editor.commit(); editor.commit();
} }
@@ -125,8 +115,11 @@ public class TimePickerPreference extends Preference implements OnTimeChangedLis
timePicker.setLayoutParams(params); timePicker.setLayoutParams(params);
timePicker.setIs24HourView(true); timePicker.setIs24HourView(true);
settings = getSharedPreferences(); settings = getSharedPreferences();
timePicker.setCurrentHour(settings.getInt(getKey() + "_hour", 0));
timePicker.setCurrentMinute(settings.getInt(getKey() + "_minute", 5)); int totalMinutes = settings.getInt(getKey(), 30);
timePicker.setCurrentHour( totalMinutes / 60);
timePicker.setCurrentMinute(totalMinutes % 60);
timePicker.setOnTimeChangedListener(this); timePicker.setOnTimeChangedListener(this);
/* /*