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

View File

@@ -30,17 +30,18 @@ import android.widget.TextView;
public class SweetDreamsFull extends Activity implements OnClickListener, OnSeekBarChangeListener{
private boolean isPlaying;
private ImageButton play_pause;
private ImageButton mPlayPauseButton;
private AudioManager mAudioManager;
private final int STREAM = AudioManager.STREAM_MUSIC;
protected static MediaPlayer mp;
protected static MediaPlayer mMediaPlayer;
private PostMortemReportExceptionHandler mDamageReport;
private final int SETTINGS = Menu.FIRST;
private final int QUIT = Menu.FIRST +1;
private SharedPreferences settings;
private SharedPreferences mSettings;
protected static final String TAG = "WhiteNoise";
private Timer timer;
protected static TextView timeLeft;
private Timer mTimer;
protected static TextView mTimeLeftTextView;
protected static boolean isFUll = true;
/**
* adjusts the provided Stream volume to the provided level
@@ -84,32 +85,32 @@ public class SweetDreamsFull extends Activity implements OnClickListener, OnSeek
private void loadSound(){
Log.i(TAG,"loadSound()");
try {
mp.reset();
mMediaPlayer.reset();
} catch (Exception e) {
e.printStackTrace();
}
String sound = settings.getString("sounds", Settings.SOUND_RIVER);
String sound = mSettings.getString("sounds", Settings.SOUND_RIVER);
Log.v(TAG,"sound = "+ sound);
if (sound != null){
if (sound.equals(Settings.SOUND_BEACH)){
mp = MediaPlayer.create(this, R.raw.beach);
mp.setLooping(true);
mMediaPlayer = MediaPlayer.create(this, R.raw.beach);
mMediaPlayer.setLooping(true);
}
if (sound.equals(Settings.SOUND_AC)){
mp = MediaPlayer.create(this, R.raw.ac);
mp.setLooping(true);
mMediaPlayer = MediaPlayer.create(this, R.raw.ac);
mMediaPlayer.setLooping(true);
}
if (sound.equals(Settings.SOUND_CRICKETS)){
mp = MediaPlayer.create(this, R.raw.crickets);
mp.setLooping(true);
mMediaPlayer = MediaPlayer.create(this, R.raw.crickets);
mMediaPlayer.setLooping(true);
}
if (sound.equals(Settings.SOUND_FALLS)){
mp = MediaPlayer.create(this, R.raw.falls);
mp.setLooping(true);
mMediaPlayer = MediaPlayer.create(this, R.raw.falls);
mMediaPlayer.setLooping(true);
}
if (sound.equals(Settings.SOUND_RIVER)){
mp = MediaPlayer.create(this, R.raw.river);
mp.setLooping(true);
mMediaPlayer = MediaPlayer.create(this, R.raw.river);
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
*/
if (!isPlaying){
play_pause.setBackgroundDrawable(getResources().getDrawable(R.drawable.pause_button));
mPlayPauseButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.pause_button));
isPlaying = true;
loadSound();
mp.start();
if (settings.getBoolean(Settings.TIMER_ENABLED, false)){
mMediaPlayer.start();
if (mSettings.getBoolean(Settings.TIMER_ENABLED, false)){
Log.v(TAG,"Starting timer");
long time = settings.getInt("timer_length_hour", 0) * 3600000;
time = time + (settings.getInt("timer_length_minute", 5) * 60000);
long time = + (mSettings.getInt("timer_length", 30) * 60000);
Log.v(TAG,"time = " + time);
timer = new Timer(time);
timer.start();
mTimer = new Timer(time);
mTimer.start();
}
} else {
stopPlaying();
@@ -163,8 +163,8 @@ public class SweetDreamsFull extends Activity implements OnClickListener, OnSeek
Thread.setDefaultUncaughtExceptionHandler(mDamageReport);
//initialize the play_pause button and set onClickListener
play_pause = (ImageButton) findViewById(R.id.play_pause_button);
play_pause.setOnClickListener(this);
mPlayPauseButton = (ImageButton) findViewById(R.id.play_pause_button);
mPlayPauseButton.setOnClickListener(this);
//initialize audio manager so we can control stream volumes
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
@@ -176,7 +176,7 @@ public class SweetDreamsFull extends Activity implements OnClickListener, OnSeek
sbVolume.setProgress(mAudioManager.getStreamVolume(STREAM));
//load shared_prefs
settings = getSharedPreferences(Settings.SETTINGS, 0);
mSettings = getSharedPreferences(Settings.SETTINGS, 0);
loadSound();
@@ -202,7 +202,7 @@ public class SweetDreamsFull extends Activity implements OnClickListener, OnSeek
*/
@Override
public void onDestroy(){
adjustVolume(STREAM,settings.getInt(Settings.MUSIC_VOLUME, 0));
adjustVolume(STREAM,mSettings.getInt(Settings.MUSIC_VOLUME, 0));
super.onDestroy();
}
@@ -264,7 +264,7 @@ public class SweetDreamsFull extends Activity implements OnClickListener, OnSeek
public void onResume(){
super.onResume();
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
@@ -290,7 +290,7 @@ public class SweetDreamsFull extends Activity implements OnClickListener, OnSeek
public void onStop(){
stopPlaying();
try {
mp.release();
mMediaPlayer.release();
} catch (Exception e) {
e.printStackTrace();
}
@@ -312,7 +312,7 @@ public class SweetDreamsFull extends Activity implements OnClickListener, OnSeek
* @author ricky barrette
*/
private boolean saveStreamVolume(int stream){
Editor edit = settings.edit();
Editor edit = mSettings.edit();
edit.putInt(Settings.MUSIC_VOLUME, mAudioManager.getStreamVolume(stream));
return edit.commit();
}
@@ -323,18 +323,18 @@ public class SweetDreamsFull extends Activity implements OnClickListener, OnSeek
* @author ricky barrette
*/
private void stopPlaying(){
play_pause.setBackgroundDrawable(getResources().getDrawable(R.drawable.play_button));
mPlayPauseButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.play_button));
isPlaying = false;
try {
mp.stop();
mMediaPlayer.stop();
} catch (IllegalStateException e) {
e.printStackTrace();
Log.e(TAG,"failed to stop media player");
}
if (timer != null) {
timer.cancel();
timer = null;
timeLeft.setText("");
if (mTimer != null) {
mTimer.cancel();
mTimer = null;
mTimeLeftTextView.setText("");
}
}
@@ -353,13 +353,13 @@ public class SweetDreamsFull extends Activity implements OnClickListener, OnSeek
super(millisInFuture, 1000l);
Log.i(TAG,"Timer()");
Log.v(TAG,"millisInFuture = "+ millisInFuture);
timeLeft = (TextView) findViewById(R.id.time_left);
mTimeLeftTextView = (TextView) findViewById(R.id.time_left);
int hours = (int) (millisInFuture / 3600000);
millisInFuture = millisInFuture - (hours * 3600000);
int minutes = (int) ( millisInFuture / 60000);
int seconds = (int) (millisInFuture % 60000);
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
public void onFinish() {
Log.i(TAG,"onFinish()");
timeLeft.setText("");
mTimeLeftTextView.setText("");
stopPlaying();
//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()");
finish();
}
@@ -394,7 +394,7 @@ public class SweetDreamsFull extends Activity implements OnClickListener, OnSeek
int minutes = (int) ( millisUntilFinished / 60000);
int seconds = (int) (millisUntilFinished % 60000);
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;
/**
* creates a time picker preference that saves the hour and minutes preferred as 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)
* creates a time picker preference
* @author ricky barrette
*/
public class TimePickerPreference extends Preference implements OnTimeChangedListener{
// private TimePicker timePicker;
private final String TAG = "TimePickerPreference";
private SharedPreferences settings;
/**
* creates a time picker preference that saves the hour and minutes preferred as 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)
* creates a time picker preference
* @param context
*/
public TimePickerPreference(Context 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"
* 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)
* creates a time picker preference
* @param context
* @param attrs
*/
public TimePickerPreference(Context context, AttributeSet 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"
* 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)
* creates a time picker preference
* @param context
* @param attrs
* @param defStyle
*/
public TimePickerPreference(Context context, AttributeSet attrs, int 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"
* 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)
* @see android.widget.TimePicker.OnTimeChangedListener#onTimeChanged(android.widget.TimePicker, int, int)
* @param view
@@ -76,8 +67,7 @@ public class TimePickerPreference extends Preference implements OnTimeChangedLis
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
Log.i(TAG,"onTimeChanged");
SharedPreferences.Editor editor = getEditor();
editor.putInt(getKey()+"_hour", hourOfDay);
editor.putInt(getKey()+"_minute", minute);
editor.putInt(getKey(), ((hourOfDay * 60) + minute));
editor.commit();
}
@@ -125,8 +115,11 @@ public class TimePickerPreference extends Preference implements OnTimeChangedLis
timePicker.setLayoutParams(params);
timePicker.setIs24HourView(true);
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);
/*