Alarm service bug fixes, to ensure that the service isn't prematurely cancelled. Also some commented-out code to enable a radial timepicker. This can be re-enabled once 1.5.3 of betterpickers is released, but needs testing.

This commit is contained in:
Timothy Allen 2014-04-18 16:02:17 +02:00
parent 615b81e295
commit 048d56c84d

View File

@ -65,6 +65,7 @@ public class MainActivity extends ActionBarActivity {
public static final int CANCEL_ALARM_REQUEST = 5;
public static final int PHONE_NUMBER_REQUEST = 6;
public static final int RINGTONE_REQUEST = 7;
public static final String TIMEPICKER_TAG = "alarmTimePicker";
public static final String defaultTimeStr = "09:00";
public static final int defaultGracePeriod = 60;
public static final Boolean defaultActive = true;
@ -84,6 +85,11 @@ public class MainActivity extends ActionBarActivity {
}
// Main screen
/**
* To use a radial timepicker, uncomment this line instead
public static class MainFragment extends Fragment implements RadialTimePickerDialog.OnTimeSetListener {
*/
public static class MainFragment extends Fragment {
public MainFragment() {
@ -100,23 +106,34 @@ public class MainActivity extends ActionBarActivity {
super.onStart();
/**
* To use a radial timepicker, uncomment here:
final RadialTimePickerDialog timePickerDialog = RadialTimePickerDialog.newInstance(this, 0, 0, DateFormat.is24HourFormat(getActivity()));
*/
// Set alarm time
String alarmTimeStr = verifyTimeString(sharedPref.getString(getString(R.string.AlarmTimePref), defaultTimeStr));
final Button alarmTimeButton = (Button) getActivity().findViewById(R.id.alarm_time);
// When debugging, set the alarm for one minute's time
if (HYPOALARM_DEBUG) {
Calendar c = Calendar.getInstance();
c.add(Calendar.MINUTE, 1);
alarmTimeStr = CalendarToTimeString(c);
}
final Button alarmTimeButton = (Button) getActivity().findViewById(R.id.alarm_time);
alarmTimeButton.setText(alarmTimeStr);
alarmTimeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DialogFragment alarmFragment = new TimePickerFragment();
alarmFragment.show(getActivity().getSupportFragmentManager(), "alarmTimePicker");
alarmFragment.show(getActivity().getSupportFragmentManager(), TIMEPICKER_TAG);
/**
* To use a radial time picker, uncomment here:
// Use the current set time as the default value for the picker
String alarmTimeStr = verifyTimeString(sharedPref.getString(getString(R.string.AlarmTimePref), defaultTimeStr));
timePickerDialog.setStartTime(hour, minute);
timePickerDialog.setThemeDark(true); // available from version 1.5.3...
timePickerDialog.show(getActivity().getSupportFragmentManager(), TIMEPICKER_TAG);
*/
}
});
@ -132,8 +149,13 @@ public class MainActivity extends ActionBarActivity {
editor.commit();
if (!active) {
// Stop any snoozed alerts, and cancel any alarms
getActivity().stopService(new Intent(getActivity(), AlarmService.class));
// Prevent any snoozed alarm from returning
if (AlarmService.alarmStatus.contentEquals(AlarmService.ALARM_SNOOZE_RUNNING) ||
AlarmService.alarmStatus.contentEquals(AlarmService.ALARM_SNOOZED) ||
AlarmService.alarmStatus.contentEquals(AlarmService.ALARM_RUNNING)) {
AlarmService.setAlarmStatus(AlarmService.ALARM_DISMISSED);
}
// Cancel any alarms
cancelAllAlarms(getActivity());
Toast.makeText(getActivity(), getString(R.string.alarmCancelled), Toast.LENGTH_SHORT).show();
} else {
@ -158,11 +180,13 @@ public class MainActivity extends ActionBarActivity {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Object value = parent.getItemAtPosition(pos);
if (value != null) {
SharedPreferences.Editor editor = sharedPref.edit();
int minutes = GracePeriodToMinutes(value.toString());
editor.putInt(getString(R.string.GracePeriodPref), minutes);
editor.commit();
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
@ -223,10 +247,12 @@ public class MainActivity extends ActionBarActivity {
InputType.TYPE_TEXT_FLAG_MULTI_LINE |
InputType.TYPE_TEXT_FLAG_CAP_SENTENCES |
InputType.TYPE_TEXT_FLAG_AUTO_CORRECT);
if (messageButton != null) {
if (messageButton.getText() != null) {
messageInput.setText(messageButton.getText().toString());
}
if (messageInput.getText() != null) {
messageInput.setSelection(messageInput.getText().length());
}
alert.setView(messageInput);
alert.setMessage(getString(R.string.setMessage));
@ -258,11 +284,13 @@ public class MainActivity extends ActionBarActivity {
// Set time change and boot receiver, so alarm restarts on boot
ComponentName bootReceiver = new ComponentName(getActivity(), AlarmChangeReceiver.class);
PackageManager pm = getActivity().getPackageManager();
if (pm != null) {
pm.setComponentEnabledSetting(bootReceiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
Log.d("MainActivity", "Setting boot receiver");
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
@ -285,13 +313,41 @@ public class MainActivity extends ActionBarActivity {
ringtoneButton.setText(currentRingtone.getTitle(getActivity().getApplicationContext()));
}
}
/**
* To use a radial timepicker, uncomment this function
*
@Override
public void onTimeSet(RadialPickerLayout view, int hourOfDay, int minute) {
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getActivity());
// Set time preference
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, hourOfDay);
cal.set(Calendar.MINUTE, minute);
cal.set(Calendar.SECOND, 0);
String alarmStr = CalendarToTimeString(cal);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString(getString(R.string.AlarmTimePref), alarmStr);
editor.commit();
Button alarm_time = (Button) getActivity().findViewById(R.id.alarm_time);
alarm_time.setText(alarmStr);
// Set actual alarm
setAlarm(getActivity(), cal);
// Display toast
CharSequence text = getString(R.string.alarmSetToast) + " " + CalendarToTimeString(cal);
Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT).show();
}
*/
}
/**
* Cancel main alarm, but not grace alarm.
* This should be run whenever the alarm is set.
*
* @param context
*/
public static void cancelAlarm(Context context) {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
@ -314,8 +370,6 @@ public class MainActivity extends ActionBarActivity {
/**
* Cancel grace alarm and notifications
*
* @param context
*/
public static void cancelGraceAlarm(Context context) {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
@ -328,16 +382,11 @@ public class MainActivity extends ActionBarActivity {
// Stop any notification of grace period expiry
context.stopService(new Intent(context, AlarmNotify.class));
// Stop any active/snoozed alarms
context.stopService(new Intent(context, AlarmService.class));
Log.d("MainActivity", "Grace alarm cancelled");
}
/**
* Cancels main alarm and grace alarm
* This should only be run when we're disabling the alarm entirely.
*
* @param context
*/
public static void cancelAllAlarms(Context context) {
cancelAlarm(context);
@ -396,8 +445,6 @@ public class MainActivity extends ActionBarActivity {
/**
* Set the grace alarm, to fire at the end of the grace period and send a text message.
*
* @param context
*/
public static void setGraceAlarm(Context context) {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
@ -430,8 +477,7 @@ public class MainActivity extends ActionBarActivity {
public Dialog onCreateDialog(Bundle savedInstanceState) {
sharedPref = PreferenceManager.getDefaultSharedPreferences(getActivity());
// Use the current set time as the default value for the picker
Button alarmTimeButton = (Button) getActivity().findViewById(R.id.alarm_time);
String alarmTimeStr = alarmTimeButton.getText().toString();
String alarmTimeStr = verifyTimeString(sharedPref.getString(getString(R.string.AlarmTimePref), defaultTimeStr));
// For selecting a time, the date doesn't matter, just the hour and minute
Calendar cal = TimeStringToCalendar(alarmTimeStr);
int hour = cal.get(Calendar.HOUR_OF_DAY);
@ -456,8 +502,6 @@ public class MainActivity extends ActionBarActivity {
Button alarm_time = (Button) getActivity().findViewById(R.id.alarm_time);
alarm_time.setText(alarmStr);
// Cancel any snoozed alerts
getActivity().stopService(new Intent(getActivity(), AlarmService.class));
// Set actual alarm
setAlarm(getActivity(), cal);