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