Fixed bug where, on app reinstall, the notification actions stopped working due to losing permission for gracecancellationpendingintent.

This commit is contained in:
Timothy Allen 2014-03-26 16:29:35 +02:00
parent d1ae1e16c9
commit c32aef7c86
9 changed files with 14 additions and 27 deletions

View File

@ -45,7 +45,6 @@ public class AlarmAlertActivity extends Activity {
setContentView(R.layout.alarm_alert);
notifyIntent = new Intent(getApplicationContext(), AlarmNotify.class);
// Disable any current notifications
stopService(notifyIntent);

View File

@ -15,7 +15,7 @@ import android.util.Log;
public class AlarmNotify extends Service {
public static final int notifyID = 1;
private volatile boolean threadRunning = false;
public volatile boolean notificationRunning = false;
@Override
public IBinder onBind(Intent intent) {
@ -25,7 +25,8 @@ public class AlarmNotify extends Service {
@Override
public void onDestroy() {
// If the notification is cancelled, stop updating.
threadRunning = false;
notificationRunning = false;
Log.d("AlarmNotify", "1: Setting notificationRunning to false");
// Remove the notification in the notification bar
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.cancel(notifyID);
@ -41,7 +42,6 @@ public class AlarmNotify extends Service {
// convert gracePeriod to milliseconds and calculate when it'll fire
final long endTime = System.currentTimeMillis() + (gracePeriod * 60 * 1000);
//Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.alarm_notification);
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_grey);
final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
final Notification.Builder notification = new Notification.Builder(this)
@ -50,13 +50,12 @@ public class AlarmNotify extends Service {
.setSmallIcon(R.drawable.alarm_notification)
.setLargeIcon(bm)
.setOnlyAlertOnce(true)
.setAutoCancel(true)
.setAutoCancel(false)
.setPriority(Notification.PRIORITY_HIGH);
//.setContentText(String.format(getString(R.string.notificationText), phoneNumber) + MainActivity.MinutesToGracePeriodStr(gracePeriod))
// Set up dismiss action
Intent cancellerIntent = new Intent(getBaseContext(), CancelGraceReceiver.class);
PendingIntent cancellerPendingIntent = PendingIntent.getBroadcast(getBaseContext(), MainActivity.CANCEL_GRACE_REQUEST, cancellerIntent, 0);
PendingIntent cancellerPendingIntent = PendingIntent.getBroadcast(getBaseContext(), MainActivity.CANCEL_GRACE_REQUEST, cancellerIntent, PendingIntent.FLAG_CANCEL_CURRENT);
// Cancel the grace period if the user clears the notification
notification.setDeleteIntent(cancellerPendingIntent);
@ -65,11 +64,14 @@ public class AlarmNotify extends Service {
// Allow the user to cancel by selecting the ContentText or ContentTitle
notification.setContentIntent(cancellerPendingIntent);
// TODO load alert activity (without sound or vibration) on select? This would allow the user to test competence
/**
* TODO load alert activity (without sound or vibration) on select?
* TODO This would allow the user to test competence
Intent alertActivityIntent = new Intent(this, AlarmAlertActivity.class);
alertActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
//notification.setContentIntent(alertActivityIntent);
notification.setContentIntent(alertActivityIntent);
*/
nm.cancel(notifyID);
nm.notify(notifyID, notification.build());
@ -77,7 +79,8 @@ public class AlarmNotify extends Service {
new Thread(new Runnable() {
@Override
public void run() {
threadRunning = true;
notificationRunning = true;
Log.d("AlarmNotify", "2: Setting notificationRunning to true");
int max = 1000;
// Convert endTime from milliseconds to seconds, and translate to time remaining
int secondsLeft = (int) ((endTime - System.currentTimeMillis())) / (1000);
@ -87,7 +90,8 @@ public class AlarmNotify extends Service {
while (progress < max) {
// Stop the thread if cancelled elsewhere
if (!threadRunning) {
Log.d("AlarmNotify", "notificationRunning is "+notificationRunning);
if (!notificationRunning) {
return;
}
int minutesLeft = secondsLeft / 60;

View File

@ -24,11 +24,6 @@ public class AlarmReceiver extends BroadcastReceiver {
Boolean alarmActive = sharedPref.getBoolean(context.getString(R.string.AlarmActivePref), true);
if (alarmActive) {
// Cancel notification if it's not already cancelled.
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.cancel(AlarmNotify.notifyID);
// Set a grace period alarm to send SMS
int gracePeriod = sharedPref.getInt(context.getString(R.string.GracePeriodPref), 60);

View File

@ -12,10 +12,6 @@ import android.widget.Toast;
public class CancelGraceReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Cancel notification if it's not already cancelled.
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.cancel(AlarmNotify.notifyID);
AlarmManager graceManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent graceIntent = new Intent(context, GraceReceiver.class);
PendingIntent gracePendingIntent = PendingIntent.getBroadcast(context, MainActivity.GRACE_REQUEST, graceIntent, 0);

View File

@ -3,7 +3,6 @@ package za.org.treehouse.hypoalarm;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.ComponentName;
@ -115,10 +114,6 @@ public class MainActivity extends ActionBarActivity {
editor.commit();
if (!b) {
// Cancel notification if it's not already cancelled.
NotificationManager nm = (NotificationManager) getActivity().getSystemService(NOTIFICATION_SERVICE);
nm.cancel(AlarmNotify.notifyID);
// Cancel any current alarm
alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
Intent alarmIntent = new Intent(getActivity(), AlarmReceiver.class);
@ -275,8 +270,6 @@ 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
//String defaultTimeStr = alarmTimeButton.getText().toString();
//String alarmTimeStr = sharedPref.getString(getString(R.string.AlarmTimePref), defaultTimeStr);
String alarmTimeStr = alarmTimeButton.getText().toString();
Calendar cal = TimeStringToCalendar(alarmTimeStr);
int hour = cal.get(Calendar.HOUR_OF_DAY);

View File

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 11 KiB

View File

Before

Width:  |  Height:  |  Size: 4.8 KiB

After

Width:  |  Height:  |  Size: 4.8 KiB

View File

Before

Width:  |  Height:  |  Size: 13 KiB

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 31 KiB