diff --git a/HypoAlarm/src/main/AndroidManifest.xml b/HypoAlarm/src/main/AndroidManifest.xml index ea8a7ac..d003b26 100644 --- a/HypoAlarm/src/main/AndroidManifest.xml +++ b/HypoAlarm/src/main/AndroidManifest.xml @@ -36,6 +36,8 @@ + @@ -46,6 +48,7 @@ + diff --git a/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmAlertActivity.java b/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmAlertActivity.java index 1eba74f..d2ee3ff 100644 --- a/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmAlertActivity.java +++ b/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmAlertActivity.java @@ -60,7 +60,7 @@ public class AlarmAlertActivity extends Activity { @Override public void onProgressChanged(SeekArc seekArc, int progress, boolean fromUser) { if (progress > 98 && !seekFinished && fromUser) { - AlarmReceiver.dismissAlarm(alertActivity); + AlarmService.dismissAlarm(alertActivity); seekFinished = true; } } @@ -87,7 +87,7 @@ public class AlarmAlertActivity extends Activity { @Override public void onTrigger(View v, int target) { // if (target == "") - AlarmReceiver.dismissAlarm(alertActivity); + AlarmService.dismissAlarm(alertActivity); } @Override @@ -107,12 +107,12 @@ public class AlarmAlertActivity extends Activity { */ @Override public void onBackPressed() { - AlarmReceiver.setAlarmStatus(AlarmReceiver.ALARM_IGNORED); - AlarmReceiver.snoozeAlarm(this); + AlarmService.setAlarmStatus(AlarmService.ALARM_IGNORED); + AlarmService.snoozeAlarm(this); } public void onUserLeaveHint() { - AlarmReceiver.setAlarmStatus(AlarmReceiver.ALARM_IGNORED); - AlarmReceiver.snoozeAlarm(this); + AlarmService.setAlarmStatus(AlarmService.ALARM_IGNORED); + AlarmService.snoozeAlarm(this); } } \ No newline at end of file diff --git a/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmKlaxon.java b/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmKlaxon.java index 18a3d8f..ff92f1b 100644 --- a/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmKlaxon.java +++ b/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmKlaxon.java @@ -5,7 +5,6 @@ import android.content.SharedPreferences; import android.content.res.AssetFileDescriptor; import android.media.AudioManager; import android.media.MediaPlayer; -import android.media.Ringtone; import android.media.RingtoneManager; import android.net.Uri; import android.os.Vibrator; @@ -17,6 +16,7 @@ import android.util.Log; import java.io.IOException; public class AlarmKlaxon { + public static Boolean klaxonActive = false; private static final long[] vPattern = {500, 500}; // Volume modification for alarms while a phone call is active, from com.android.deskclock.alarms private static final float IN_CALL_VOLUME = 0.125f; @@ -29,6 +29,11 @@ public class AlarmKlaxon { public static void start(final Context context) { SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context); + if (klaxonActive) { + stop(context); + } + klaxonActive = true; + vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); vibrator.cancel(); vibrator.vibrate(vPattern, 0); @@ -87,9 +92,12 @@ public class AlarmKlaxon { } public static void stop(final Context context) { - vibrator.cancel(); - stopAudio(context); - telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE); + if (klaxonActive) { + vibrator.cancel(); + stopAudio(context); + telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE); + klaxonActive = false; + } } private static void startAudio(final Context context) throws IOException { diff --git a/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmNotify.java b/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmNotify.java index 2dde0da..fa113b5 100644 --- a/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmNotify.java +++ b/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmNotify.java @@ -85,7 +85,7 @@ public class AlarmNotify extends Service { notificationRunning = true; int max = 1000; // Count in milliseconds for greater progress resolution - int milliSecondsLeft = (int) ((AlarmReceiver.graceEndTime - System.currentTimeMillis())); + int milliSecondsLeft = (int) ((AlarmService.graceEndTime - System.currentTimeMillis())); int gracePeriodMilliSeconds = gracePeriod * 60 * 1000; int progress = ((gracePeriodMilliSeconds - milliSecondsLeft) * max) / gracePeriodMilliSeconds; diff --git a/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmReceiver.java b/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmReceiver.java index 7c32af2..44b3fe0 100644 --- a/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmReceiver.java +++ b/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmReceiver.java @@ -1,177 +1,16 @@ package za.org.treehouse.hypoalarm; -import android.app.AlarmManager; -import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; -import android.content.SharedPreferences; -import android.os.Build; -import android.os.Handler; -import android.preference.PreferenceManager; -import android.telephony.PhoneStateListener; -import android.telephony.TelephonyManager; -import android.util.Log; -import android.widget.Toast; - -import java.io.IOException; -import java.util.Calendar; public class AlarmReceiver extends BroadcastReceiver { - private static final int SNOOZE_TIME = 1000*60*5; // Snooze for 5 minutes if need be - private static final int ALERT_LIFE = 1000*60*2; // 2 minutes - private static SharedPreferences sharedPref; - private static AlarmManager alarmManager; - private static PendingIntent alarmPendingIntent, gracePendingIntent; - private static Intent alertActivityIntent, notifyIntent; - private static TelephonyManager telephonyManager; - public static volatile String alarmStatus; // Register ALARM_DISMISSED and its brethren here - public static final String ALARM_RUNNING = "ALARM_RUNNING"; - public static final String ALARM_DISMISSED = "ALARM_DISMISSED"; - public static final String ALARM_IGNORED = "ALARM_IGNORED"; - public static final String ALARM_SNOOZED = "ALARM_SNOOZED"; - public static final String ALARM_SNOOZE_RUNNING = "ALARM_SNOOZE_RUNNING"; - public static long graceEndTime; + private static Intent alarmIntent; @Override public void onReceive(final Context context, final Intent intent) { - sharedPref = PreferenceManager.getDefaultSharedPreferences(context); - alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); - final Boolean alarmActive = sharedPref.getBoolean(context.getString(R.string.AlarmActivePref), true); - final int gracePeriod = sharedPref.getInt(context.getString(R.string.GracePeriodPref), 60); - final String alarmTimeStr = sharedPref.getString(context.getString(R.string.AlarmTimePref), null); - - if (alarmActive) { - // if nothing else happens, assume the alert was ignored. - alarmStatus = ALARM_RUNNING; - - // Cancel the pre-alarm notification, if it exists - context.stopService(new Intent(context, PreAlarmNotify.class)); - - // If dialing, active in a phone call, or on hold, don't bother with the alarm, just reset it for tomorrow - telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); - if (telephonyManager.getCallState() != TelephonyManager.CALL_STATE_OFFHOOK) { - - // Set a grace period alarm to send SMS - Calendar graceCal = Calendar.getInstance(); - graceCal.set(Calendar.SECOND, 0); - graceCal.add(Calendar.MINUTE, gracePeriod); - Intent graceIntent = new Intent(context, GraceReceiver.class); - gracePendingIntent = PendingIntent.getBroadcast(context, MainActivity.GRACE_REQUEST, graceIntent, 0); - alarmManager.cancel(gracePendingIntent); - if (Build.VERSION.SDK_INT >= 19) { - alarmManager.setExact(AlarmManager.RTC_WAKEUP, graceCal.getTimeInMillis(), gracePendingIntent); - } else { - alarmManager.set(AlarmManager.RTC_WAKEUP, graceCal.getTimeInMillis(), gracePendingIntent); - } - Log.d("AlarmReceiver", "Setting grace alarm for " + MainActivity.debugDate(graceCal)); - - // Calculate when the grace period (converted from minutes to milliseconds) ends - graceEndTime = System.currentTimeMillis() + (gracePeriod * 60 * 1000); - - // Set up intents for later use - notifyIntent = new Intent(context, AlarmNotify.class); - alertActivityIntent = new Intent(context, AlarmAlertActivity.class); - alertActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | - Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); - - // Allow user to acknowledge alarm and cancel grace alarm - startAlert(context); - } - - // Reset for tomorrow; as of API 19, setRepeating() is inexact, so we use setExact() - Calendar cal = MainActivity.TimeStringToCalendar(alarmTimeStr); - // Advance the calendar to tomorrow - cal.add(Calendar.DAY_OF_MONTH, 1); - alarmPendingIntent = PendingIntent.getBroadcast(context, MainActivity.ALARM_REQUEST, intent, 0); - alarmManager.cancel(alarmPendingIntent); - if (Build.VERSION.SDK_INT >= 19) { - alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), alarmPendingIntent); - } else { - alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), alarmPendingIntent); - } - Log.d("AlarmReceiver", "Resetting alarm for " + MainActivity.debugDate(cal)); - } + alarmIntent = new Intent(context, AlarmService.class); + context.startService(alarmIntent); } - public static void startAlert(final Context context) { - Log.d("AlarmReceiver", "Starting alarm; status is " + alarmStatus); - // Turn off any notifications first - context.stopService(notifyIntent); - - context.startActivity(alertActivityIntent); - AlarmKlaxon.start(context); - - // Turn off the alert activity after a period, and switch to a notification - new Handler().postDelayed(new Runnable() { - public void run() { - // Close the dialogue and switch to notification - // if the Activity has not been closed by the user - // (that is, snoozeAlert and dismissAlert have not been called) - if (alarmStatus.contentEquals(ALARM_DISMISSED) || - alarmStatus.contentEquals(ALARM_SNOOZED)) { - return; - } - // Stop if we're running the snooze alert, or the snooze time is less than 10 seconds - if (alarmStatus.contentEquals(ALARM_SNOOZE_RUNNING) || SNOOZE_TIME < 10000) { - stopAlert(context); - } else { - alarmStatus = ALARM_IGNORED; // This is true, although we are about to switch to ALARM_SNOOZED - snoozeAlarm(context); - } - } - }, ALERT_LIFE); - } - - public static void stopAlert(final Context context) { - Log.d("AlarmReceiver", "Stopping alarm; status is " + alarmStatus); - AlarmKlaxon.stop(context); - AlarmAlertActivity.alertActivity.finish(); - // Display a notification if the alarm hasn't been dismissed - if (!alarmStatus.contentEquals(ALARM_DISMISSED)) { - context.startService(notifyIntent); - } - } - - public static void dismissAlarm(final Context context) { - Log.d("AlarmReceiver", "Dismissing alarm"); - alarmStatus = ALARM_DISMISSED; - // Close the alert and all notifications - stopAlert(context); - - // Cancel the graceAlarm - Intent graceIntent = new Intent(context, GraceReceiver.class); - PendingIntent gracePendingIntent = PendingIntent.getBroadcast(context, MainActivity.GRACE_REQUEST, graceIntent, 0); - alarmManager.cancel(gracePendingIntent); - } - - public static void snoozeAlarm(final Context context) { - Log.d("AlarmReceiver", "Snoozing alarm"); - stopAlert(context); - // Close the alert, stop the klaxon, and start the notification, - // but only if there's time left before the gracePeriod triggers, - // and we haven't snoozed before - if (((System.currentTimeMillis() + SNOOZE_TIME) < graceEndTime) && - (!alarmStatus.contentEquals(ALARM_SNOOZED)) && - (!alarmStatus.contentEquals(ALARM_DISMISSED))) { - new Handler().postDelayed(new Runnable() { - public void run() { - Log.d("AlarmReceiver", "Resuming after snooze; status is " + alarmStatus); - // Don't run if the alarm was dismissed before the timer ran out - // (because a notification was acknowledged) - if (!alarmStatus.contentEquals(ALARM_DISMISSED)) { - alarmStatus = ALARM_SNOOZE_RUNNING; - startAlert(context); - } - } - }, SNOOZE_TIME); - // Change alarm status from ignored to snoozed - alarmStatus = ALARM_SNOOZED; - } - } - - public static void setAlarmStatus (String status) { - Log.d("AlarmReceiver", "Setting alarm status to " + status); - alarmStatus = status; - } } diff --git a/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmService.java b/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmService.java new file mode 100644 index 0000000..3e450f4 --- /dev/null +++ b/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmService.java @@ -0,0 +1,198 @@ +package za.org.treehouse.hypoalarm; + +import android.app.AlarmManager; +import android.app.PendingIntent; +import android.app.Service; +import android.content.Context; +import android.content.Intent; +import android.content.SharedPreferences; +import android.os.Build; +import android.os.Handler; +import android.os.IBinder; +import android.preference.PreferenceManager; +import android.telephony.TelephonyManager; +import android.util.Log; + +import java.util.Calendar; + +public class AlarmService extends Service { + private static final int SNOOZE_TIME = 1000*60*5; // Snooze for 5 minutes if need be + private static final int ALERT_LIFE = 1000*60*2; // 2 minutes + private static AlarmManager alarmManager; + private static Intent alertActivityIntent, notifyIntent; + public static Boolean alarmStarted = false; + public static volatile String alarmStatus; // Register ALARM_DISMISSED and its brethren here + public static final String ALARM_RUNNING = "ALARM_RUNNING"; + public static final String ALARM_DISMISSED = "ALARM_DISMISSED"; + public static final String ALARM_IGNORED = "ALARM_IGNORED"; + public static final String ALARM_SNOOZED = "ALARM_SNOOZED"; + public static final String ALARM_SNOOZE_RUNNING = "ALARM_SNOOZE_RUNNING"; + public static long graceEndTime; + + @Override + public void onCreate() { + alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); + } + @Override + public void onDestroy() { + if (alarmStarted) { + stopAlert(getApplicationContext()); + alarmStarted = false; + } + } + + @Override + public int onStartCommand(Intent intent, int flags, int startId) { + SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this); + final Boolean alarmActive = sharedPref.getBoolean(getString(R.string.AlarmActivePref), true); + final int gracePeriod = sharedPref.getInt(getString(R.string.GracePeriodPref), 60); + final String alarmTimeStr = sharedPref.getString(getString(R.string.AlarmTimePref), null); + + if (alarmActive) { + + if (alarmStarted) { + stopAlert(getApplicationContext()); + } + alarmStarted = true; + + // if nothing else happens, assume the alert was ignored. + alarmStatus = ALARM_RUNNING; + + // Cancel the pre-alarm notification, if it exists + stopService(new Intent(this, PreAlarmNotify.class)); + + // If dialing, active in a phone call, or on hold, don't bother with the alarm, just reset it for tomorrow + TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); + if (telephonyManager.getCallState() != TelephonyManager.CALL_STATE_OFFHOOK) { + + // Set a grace period alarm to send SMS + Calendar graceCal = Calendar.getInstance(); + graceCal.set(Calendar.SECOND, 0); + graceCal.add(Calendar.MINUTE, gracePeriod); + Intent graceIntent = new Intent(this, GraceReceiver.class); + PendingIntent gracePendingIntent = PendingIntent.getBroadcast(this, MainActivity.GRACE_REQUEST, graceIntent, 0); + alarmManager.cancel(gracePendingIntent); + if (Build.VERSION.SDK_INT >= 19) { + alarmManager.setExact(AlarmManager.RTC_WAKEUP, graceCal.getTimeInMillis(), gracePendingIntent); + } else { + alarmManager.set(AlarmManager.RTC_WAKEUP, graceCal.getTimeInMillis(), gracePendingIntent); + } + Log.d("AlarmService", "Setting grace alarm for " + MainActivity.debugDate(graceCal)); + + // Calculate when the grace period (converted from minutes to milliseconds) ends + graceEndTime = System.currentTimeMillis() + (gracePeriod * 60 * 1000); + + // Set up intents for later use + notifyIntent = new Intent(this, AlarmNotify.class); + alertActivityIntent = new Intent(this, AlarmAlertActivity.class); + alertActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | + Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); + + // Allow user to acknowledge alarm and cancel grace alarm + startAlert(this); + } + + // Reset for tomorrow; as of API 19, setRepeating() is inexact, so we use setExact() + Calendar cal = MainActivity.TimeStringToCalendar(alarmTimeStr); + // Advance the calendar to tomorrow + cal.add(Calendar.DAY_OF_MONTH, 1); + PendingIntent alarmPendingIntent = PendingIntent.getBroadcast(this, MainActivity.ALARM_REQUEST, intent, 0); + alarmManager.cancel(alarmPendingIntent); + if (Build.VERSION.SDK_INT >= 19) { + alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), alarmPendingIntent); + } else { + alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), alarmPendingIntent); + } + Log.d("AlarmService", "Resetting alarm for " + MainActivity.debugDate(cal)); + } + return super.onStartCommand(intent, flags, startId); + } + + public static void startAlert(final Context context) { + Log.d("AlarmService", "Starting alarm; status is " + alarmStatus); + // Turn off any notifications first + context.stopService(notifyIntent); + + context.startActivity(alertActivityIntent); + AlarmKlaxon.start(context); + + // Turn off the alert activity after a period, and switch to a notification + new Handler().postDelayed(new Runnable() { + public void run() { + // Close the dialogue and switch to notification + // if the Activity has not been closed by the user + // (that is, snoozeAlert and dismissAlert have not been called) + if (alarmStatus.contentEquals(ALARM_DISMISSED) || + alarmStatus.contentEquals(ALARM_SNOOZED)) { + return; + } + // Stop if we're running the snooze alert, or the snooze time is less than 10 seconds + if (alarmStatus.contentEquals(ALARM_SNOOZE_RUNNING) || SNOOZE_TIME < 10000) { + stopAlert(context); + } else { + alarmStatus = ALARM_IGNORED; // This is true, although we are about to switch to ALARM_SNOOZED + snoozeAlarm(context); + } + } + }, ALERT_LIFE); + } + + public static void stopAlert(final Context context) { + Log.d("AlarmService", "Stopping alarm; status is " + alarmStatus); + if (alarmStarted) { + AlarmKlaxon.stop(context); + AlarmAlertActivity.alertActivity.finish(); + // Display a notification if the alarm hasn't been dismissed + if (!alarmStatus.contentEquals(ALARM_DISMISSED)) { + context.startService(notifyIntent); + } + alarmStarted = false; + } + } + + public static void dismissAlarm(final Context context) { + Log.d("AlarmService", "Dismissing alarm"); + alarmStatus = ALARM_DISMISSED; + // Close the alert and all notifications + stopAlert(context); + + // Cancel the graceAlarm + Intent graceIntent = new Intent(context, GraceReceiver.class); + PendingIntent gracePendingIntent = PendingIntent.getBroadcast(context, MainActivity.GRACE_REQUEST, graceIntent, 0); + alarmManager.cancel(gracePendingIntent); + } + + public static void snoozeAlarm(final Context context) { + Log.d("AlarmService", "Snoozing alarm"); + stopAlert(context); + // Close the alert, stop the klaxon, and start the notification, + // but only if there's time left before the gracePeriod triggers, + // and we haven't snoozed before + if (((System.currentTimeMillis() + SNOOZE_TIME) < graceEndTime) && + (!alarmStatus.contentEquals(ALARM_SNOOZED)) && + (!alarmStatus.contentEquals(ALARM_DISMISSED))) { + new Handler().postDelayed(new Runnable() { + public void run() { + Log.d("AlarmService", "Resuming after snooze; status is " + alarmStatus); + // Don't run if the alarm was dismissed before the timer ran out + // (because a notification was acknowledged) + if (!alarmStatus.contentEquals(ALARM_DISMISSED)) { + alarmStatus = ALARM_SNOOZE_RUNNING; + startAlert(context); + } + } + }, SNOOZE_TIME); + // Change alarm status from ignored to snoozed + alarmStatus = ALARM_SNOOZED; + } + } + + public static void setAlarmStatus (String status) { + Log.d("AlarmService", "Setting alarm status to " + status); + alarmStatus = status; + } + @Override + public IBinder onBind(Intent intent) { + return null; + } +} diff --git a/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/CancelGraceReceiver.java b/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/CancelGraceReceiver.java index d2f5d60..15ddba0 100644 --- a/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/CancelGraceReceiver.java +++ b/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/CancelGraceReceiver.java @@ -19,7 +19,7 @@ public class CancelGraceReceiver extends BroadcastReceiver { Log.d("CancelGraceReceiver", "Cancelled grace alarm"); // Ensure that any snoozes that are pending never happen. - AlarmReceiver.setAlarmStatus(AlarmReceiver.ALARM_DISMISSED); + AlarmService.setAlarmStatus(AlarmService.ALARM_DISMISSED); // Display toast Toast.makeText(context, context.getString(R.string.alarmCancelToast), Toast.LENGTH_LONG).show(); diff --git a/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/MainActivity.java b/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/MainActivity.java index 2438c62..e79112c 100644 --- a/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/MainActivity.java +++ b/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/MainActivity.java @@ -66,10 +66,7 @@ public class MainActivity extends ActionBarActivity { public static int CANCEL_ALARM_REQUEST = 5; public static int PHONE_NUMBER_REQUEST = 6; public static int RINGTONE_REQUEST = 7; - private static Switch alarmActiveSwitch; private static Button alarmTimeButton; - private static Spinner gracePeriodSpinner; - private static EditText phoneNumberButton; private static EditText messageButton; public static Boolean HYPOALARM_DEBUG = true; @@ -107,7 +104,7 @@ public class MainActivity extends ActionBarActivity { // Allow alarm to activate Boolean alarmActive = sharedPref.getBoolean(getString(R.string.AlarmActivePref), true); - alarmActiveSwitch = (Switch) getActivity().findViewById(R.id.alarm_active_switch); + Switch alarmActiveSwitch = (Switch) getActivity().findViewById(R.id.alarm_active_switch); alarmActiveSwitch.setChecked(alarmActive); alarmActiveSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override @@ -177,7 +174,7 @@ public class MainActivity extends ActionBarActivity { // Set grace period int defaultGrace = 60; int gracePeriod = sharedPref.getInt(getString(R.string.GracePeriodPref), defaultGrace); - gracePeriodSpinner = (Spinner) getActivity().findViewById(R.id.grace_period); + Spinner gracePeriodSpinner = (Spinner) getActivity().findViewById(R.id.grace_period); gracePeriodSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { final SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getActivity()); @@ -203,10 +200,11 @@ public class MainActivity extends ActionBarActivity { int spinnerPosition = gracePeriodSpinnerAdapter.getPosition(MinutesToGracePeriodStr(gracePeriod)); gracePeriodSpinner.setSelection(spinnerPosition); + // Allow user to select ringtone final Button ringtoneButton = (Button) getActivity().findViewById(R.id.ringtone); String ringtoneStr = sharedPref.getString(getString(R.string.RingtonePref), RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM).toString()); - Uri ringtoneUri = Uri.parse(ringtoneStr); + final Uri ringtoneUri = Uri.parse(ringtoneStr); Ringtone currentRingtone = RingtoneManager.getRingtone(getActivity().getApplicationContext(), ringtoneUri); ringtoneButton.setText(currentRingtone.getTitle(getActivity().getApplicationContext())); ringtoneButton.setOnClickListener(new View.OnClickListener() { @@ -215,9 +213,8 @@ public class MainActivity extends ActionBarActivity { Intent ringtoneIntent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER); ringtoneIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select ringtone:"); ringtoneIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false); - //ringtoneIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true); ringtoneIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_ALARM); - //ringtoneIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, currentRingtone); + ringtoneIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, ringtoneUri); startActivityForResult(ringtoneIntent, RINGTONE_REQUEST); } @@ -226,7 +223,7 @@ public class MainActivity extends ActionBarActivity { // Set phone number String phoneNumberStr = sharedPref.getString(getString(R.string.PhoneNumberPref), null); - phoneNumberButton = (EditText) getActivity().findViewById(R.id.phone_number); + EditText phoneNumberButton = (EditText) getActivity().findViewById(R.id.phone_number); phoneNumberButton.setText(phoneNumberStr); phoneNumberButton.setOnClickListener(new View.OnClickListener() { @Override @@ -295,7 +292,6 @@ public class MainActivity extends ActionBarActivity { @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); - Log.d("MainActivity", "Request code: " + requestCode); // If we're answering a ringtone dialogue, update the correct button if (requestCode == RINGTONE_REQUEST && resultCode == RESULT_OK) {