From fdba3fd502587b52673dd94baecf07d8ff7671c3 Mon Sep 17 00:00:00 2001 From: Matt Hadden Date: Sat, 22 May 2021 19:50:26 -0400 Subject: [PATCH 1/8] Fix database close issues on cancel If nothing was found in the cursor when attempting to do a cancel it would error out and never close the database, causing further errors down the line. --- .../src/main/java/com/allyants/notifyme/NotifyMe.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/notifyme/src/main/java/com/allyants/notifyme/NotifyMe.java b/notifyme/src/main/java/com/allyants/notifyme/NotifyMe.java index 1770e0b..46afebc 100644 --- a/notifyme/src/main/java/com/allyants/notifyme/NotifyMe.java +++ b/notifyme/src/main/java/com/allyants/notifyme/NotifyMe.java @@ -124,12 +124,13 @@ public static void cancel(Context context,String key){ Notification.NotificationDBHelper mDbHelper = new Notification.NotificationDBHelper(context); SQLiteDatabase db = mDbHelper.getWritableDatabase(); Cursor cursor = db.rawQuery("SELECT * FROM " + Notification.NotificationEntry.TABLE_NAME + " WHERE custom_id = ? LIMIT 1", new String[]{key}); - cursor.moveToFirst(); - int notificationId = cursor.getInt(cursor.getColumnIndex(Notification.NotificationEntry._ID)); - db.delete(TABLE_NAME, com.allyants.notifyme.Notification.NotificationEntry._ID + " = " + notificationId, null); + if (cursor.moveToFirst()) { + int notificationId = cursor.getInt(cursor.getColumnIndex(Notification.NotificationEntry._ID)); + db.delete(TABLE_NAME, com.allyants.notifyme.Notification.NotificationEntry._ID + " = " + notificationId, null); + cursor.close(); + mNotificationManager.cancel(notificationId); + } db.close(); - cursor.close(); - mNotificationManager.cancel(notificationId); }catch (Exception e){ e.printStackTrace(); } From 56a300d348c6eb0f8aadaf66de537d9da4b16191 Mon Sep 17 00:00:00 2001 From: Matt Hadden Date: Sat, 29 May 2021 13:14:46 -0400 Subject: [PATCH 2/8] Fix a crash in NotificationPublisher.onReceive We need to check that data.moveToFirst exists before we try and fetch info from it. Otherwise the NotificaitonPublisher does not handle itself well and crashes (possibly?) causing more issues down the line. --- .../notifyme/NotificationPublisher.java | 39 ++++++++++--------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/notifyme/src/main/java/com/allyants/notifyme/NotificationPublisher.java b/notifyme/src/main/java/com/allyants/notifyme/NotificationPublisher.java index bdf05c1..bf9b9be 100644 --- a/notifyme/src/main/java/com/allyants/notifyme/NotificationPublisher.java +++ b/notifyme/src/main/java/com/allyants/notifyme/NotificationPublisher.java @@ -46,24 +46,25 @@ public void onReceive(final Context context, Intent intent) { SQLiteDatabase db = mDbHelper.getWritableDatabase(); Cursor data = db.rawQuery("SELECT * FROM "+TABLE_NAME+" WHERE "+_ID+" = "+notificationId,null); - data.moveToFirst(); - String title = data.getString(data.getColumnIndex(NOTIFICATION_TITLE_TEXT)); - String content = data.getString(data.getColumnIndex(NOTIFICATION_CONTENT_TEXT)); - String rrule = data.getString(data.getColumnIndex(NOTIFICATION_RRULE)); - long dstart = data.getLong(data.getColumnIndex(NOTIFICATION_DSTART)); - String str_actions = data.getString(data.getColumnIndex(NOTIFICATION_ACTIONS)); - String str_actions_text = data.getString(data.getColumnIndex(NOTIFICATION_ACTIONS_TEXT)); - String str_actions_dismiss = data.getString(data.getColumnIndex(NOTIFICATION_ACTIONS_DISMISS)); - String str_actions_collapse = data.getString(data.getColumnIndex(NOTIFICATION_ACTIONS_COLLAPSE)); - int led_color = data.getInt(data.getColumnIndex(NOTIFICATION_LED_COLOR)); - int small_icon = data.getInt(data.getColumnIndex(NOTIFICATION_SMALL_ICON)); - int large_icon = data.getInt(data.getColumnIndex(NOTIFICATION_LARGE_ICON)); - int color = data.getInt(data.getColumnIndex(NOTIFICATION_COLOR)); - String[] actions = NotifyMe.convertStringToArray(str_actions); - String[] actions_text = NotifyMe.convertStringToArray(str_actions_text); - String[] actions_dismiss = NotifyMe.convertStringToArray(str_actions_dismiss); - String[] actions_collapse = NotifyMe.convertStringToArray(str_actions_collapse); - data.close(); + if (data.moveToFirst()) { + String title = data.getString(data.getColumnIndex(NOTIFICATION_TITLE_TEXT)); + String content = data.getString(data.getColumnIndex(NOTIFICATION_CONTENT_TEXT)); + String rrule = data.getString(data.getColumnIndex(NOTIFICATION_RRULE)); + long dstart = data.getLong(data.getColumnIndex(NOTIFICATION_DSTART)); + String str_actions = data.getString(data.getColumnIndex(NOTIFICATION_ACTIONS)); + String str_actions_text = data.getString(data.getColumnIndex(NOTIFICATION_ACTIONS_TEXT)); + String str_actions_dismiss = data.getString(data.getColumnIndex(NOTIFICATION_ACTIONS_DISMISS)); + String str_actions_collapse = data.getString(data.getColumnIndex(NOTIFICATION_ACTIONS_COLLAPSE)); + int led_color = data.getInt(data.getColumnIndex(NOTIFICATION_LED_COLOR)); + int small_icon = data.getInt(data.getColumnIndex(NOTIFICATION_SMALL_ICON)); + int large_icon = data.getInt(data.getColumnIndex(NOTIFICATION_LARGE_ICON)); + int color = data.getInt(data.getColumnIndex(NOTIFICATION_COLOR)); + String[] actions = NotifyMe.convertStringToArray(str_actions); + String[] actions_text = NotifyMe.convertStringToArray(str_actions_text); + String[] actions_dismiss = NotifyMe.convertStringToArray(str_actions_dismiss); + String[] actions_collapse = NotifyMe.convertStringToArray(str_actions_collapse); + data.close(); + } db.close(); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context,notificationId); @@ -123,4 +124,4 @@ public void onReceive(final Context context, Intent intent) { } -} \ No newline at end of file +} From 271a1e280cf1fc13d4f98df451974c5551ed823c Mon Sep 17 00:00:00 2001 From: Matt Hadden Date: Sat, 29 May 2021 13:44:24 -0400 Subject: [PATCH 3/8] Fix build issue --- .../notifyme/NotificationPublisher.java | 106 +++++++++--------- 1 file changed, 52 insertions(+), 54 deletions(-) diff --git a/notifyme/src/main/java/com/allyants/notifyme/NotificationPublisher.java b/notifyme/src/main/java/com/allyants/notifyme/NotificationPublisher.java index bf9b9be..93c7b08 100644 --- a/notifyme/src/main/java/com/allyants/notifyme/NotificationPublisher.java +++ b/notifyme/src/main/java/com/allyants/notifyme/NotificationPublisher.java @@ -64,64 +64,62 @@ public void onReceive(final Context context, Intent intent) { String[] actions_dismiss = NotifyMe.convertStringToArray(str_actions_dismiss); String[] actions_collapse = NotifyMe.convertStringToArray(str_actions_collapse); data.close(); - } - db.close(); - NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context,notificationId); - if(small_icon != -1) { - mBuilder.setSmallIcon(small_icon); - }else{ - mBuilder.setSmallIcon(R.drawable.ic_check_circle); - } - if(large_icon != -1) { - Bitmap largeIcon = BitmapFactory.decodeResource(context.getResources(), large_icon); - mBuilder.setLargeIcon(largeIcon); - } - mBuilder.setContentTitle(title); - mBuilder.setContentText(content); - mBuilder.setColor(color); - mBuilder.setVibrate(new long[] { 1000,1000,1000 }); - for (int i = 0; i < actions.length; i++) { - try { - Intent tent = new Intent(context,ActionReceiver.class); - tent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - tent.putExtra("_id",notificationId); - tent.putExtra("rrule",rrule); - tent.putExtra("dstart",dstart); - tent.putExtra("index",i); - tent.putExtra("action",actions[i]); - tent.putExtra("collapse",Boolean.parseBoolean(actions_collapse[i])); - tent.putExtra("dismiss",Boolean.parseBoolean(actions_dismiss[i])); - PendingIntent pendingIntent = PendingIntent.getBroadcast(context,Integer.parseInt(notificationId)*3+i,tent,PendingIntent.FLAG_UPDATE_CURRENT); - mBuilder.addAction(R.drawable.ic_check_circle,actions_text[i],pendingIntent); - } catch (Exception e) { - e.printStackTrace(); + NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context,notificationId); + if(small_icon != -1) { + mBuilder.setSmallIcon(small_icon); + }else{ + mBuilder.setSmallIcon(R.drawable.ic_check_circle); } - } - Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); - mBuilder.setSound(uri); - Intent deleteIntent = new Intent(context,DeletePendingIntent.class); - deleteIntent.putExtra("_id",notificationId); - deleteIntent.putExtra("rrule",rrule); - deleteIntent.putExtra("dstart",dstart); - PendingIntent pendingIntent = PendingIntent.getBroadcast(context,Integer.parseInt(notificationId),deleteIntent,PendingIntent.FLAG_UPDATE_CURRENT); - mBuilder.setDeleteIntent(pendingIntent); - Notification notification = mBuilder.build(); + if(large_icon != -1) { + Bitmap largeIcon = BitmapFactory.decodeResource(context.getResources(), large_icon); + mBuilder.setLargeIcon(largeIcon); + } + mBuilder.setContentTitle(title); + mBuilder.setContentText(content); + mBuilder.setColor(color); + mBuilder.setVibrate(new long[] { 1000,1000,1000 }); + for (int i = 0; i < actions.length; i++) { + try { + Intent tent = new Intent(context,ActionReceiver.class); + tent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + tent.putExtra("_id",notificationId); + tent.putExtra("rrule",rrule); + tent.putExtra("dstart",dstart); + tent.putExtra("index",i); + tent.putExtra("action",actions[i]); + tent.putExtra("collapse",Boolean.parseBoolean(actions_collapse[i])); + tent.putExtra("dismiss",Boolean.parseBoolean(actions_dismiss[i])); + PendingIntent pendingIntent = PendingIntent.getBroadcast(context,Integer.parseInt(notificationId)*3+i,tent,PendingIntent.FLAG_UPDATE_CURRENT); + mBuilder.addAction(R.drawable.ic_check_circle,actions_text[i],pendingIntent); + } catch (Exception e) { + e.printStackTrace(); + } + } + Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); + mBuilder.setSound(uri); + Intent deleteIntent = new Intent(context,DeletePendingIntent.class); + deleteIntent.putExtra("_id",notificationId); + deleteIntent.putExtra("rrule",rrule); + deleteIntent.putExtra("dstart",dstart); + PendingIntent pendingIntent = PendingIntent.getBroadcast(context,Integer.parseInt(notificationId),deleteIntent,PendingIntent.FLAG_UPDATE_CURRENT); + mBuilder.setDeleteIntent(pendingIntent); + Notification notification = mBuilder.build(); - notification.ledARGB = led_color; - notification.flags = Notification.FLAG_SHOW_LIGHTS; - notification.ledOnMS = 300; - notification.ledOffMS = 1000; - NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + notification.ledARGB = led_color; + notification.flags = Notification.FLAG_SHOW_LIGHTS; + notification.ledOnMS = 300; + notification.ledOffMS = 1000; + NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - NotificationChannel nc = new NotificationChannel(notificationId,notificationId,NotificationManager.IMPORTANCE_HIGH); - nc.enableLights(true); - nc.setLightColor(led_color); - mNotificationManager.createNotificationChannel(nc); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + NotificationChannel nc = new NotificationChannel(notificationId,notificationId,NotificationManager.IMPORTANCE_HIGH); + nc.enableLights(true); + nc.setLightColor(led_color); + mNotificationManager.createNotificationChannel(nc); + } + mNotificationManager.notify(Integer.parseInt(notificationId), notification); } - mNotificationManager.notify(Integer.parseInt(notificationId), notification); + db.close(); } - - } From 956cffa09809115a5ab199ab970eb106259f1040 Mon Sep 17 00:00:00 2001 From: Matt Hadden Date: Tue, 8 Jun 2021 21:14:32 -0400 Subject: [PATCH 4/8] Attempt to cancel alarm/pendingintent --- .../src/main/java/com/allyants/notifyme/NotifyMe.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/notifyme/src/main/java/com/allyants/notifyme/NotifyMe.java b/notifyme/src/main/java/com/allyants/notifyme/NotifyMe.java index 46afebc..fa6bb96 100644 --- a/notifyme/src/main/java/com/allyants/notifyme/NotifyMe.java +++ b/notifyme/src/main/java/com/allyants/notifyme/NotifyMe.java @@ -129,6 +129,7 @@ public static void cancel(Context context,String key){ db.delete(TABLE_NAME, com.allyants.notifyme.Notification.NotificationEntry._ID + " = " + notificationId, null); cursor.close(); mNotificationManager.cancel(notificationId); + cancelNotification(context, notificationId); } db.close(); }catch (Exception e){ @@ -161,6 +162,15 @@ static void scheduleNotification(Context context, String notificationId, long ti Log.e(notificationId,String.valueOf(time)); alarmManager.set(AlarmManager.RTC_WAKEUP,time,pendingIntent); } + + static void cancelNotification(Context context, String notificationId) { + AlarmManager alarmMAnager = (AlarmManager)context.getSystemService(ALARM_SERVICE); + Intent intent = new Intent(context, NotificationPublisher.class); + PendingIntent pendingIntent = PendingIntent.getBroadcast(context, Integer.parseInt(notificationId), intent, PendingIntent.FLAG_UPDATE_CURRENT); + pendingIntent.cancel(); + Log.e(notificationId,"cancelling"); + alarmManager.cancel(pendingIntent); + } public static class Builder{ From d2a03ead8e4c5e507eb0b37ae5da07b4b0dd4719 Mon Sep 17 00:00:00 2001 From: Matt Hadden Date: Tue, 8 Jun 2021 21:19:23 -0400 Subject: [PATCH 5/8] Fix build attempt #1 --- notifyme/src/main/java/com/allyants/notifyme/NotifyMe.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/notifyme/src/main/java/com/allyants/notifyme/NotifyMe.java b/notifyme/src/main/java/com/allyants/notifyme/NotifyMe.java index fa6bb96..83fffe4 100644 --- a/notifyme/src/main/java/com/allyants/notifyme/NotifyMe.java +++ b/notifyme/src/main/java/com/allyants/notifyme/NotifyMe.java @@ -164,7 +164,7 @@ static void scheduleNotification(Context context, String notificationId, long ti } static void cancelNotification(Context context, String notificationId) { - AlarmManager alarmMAnager = (AlarmManager)context.getSystemService(ALARM_SERVICE); + AlarmManager alarmManager = (AlarmManager)context.getSystemService(ALARM_SERVICE); Intent intent = new Intent(context, NotificationPublisher.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, Integer.parseInt(notificationId), intent, PendingIntent.FLAG_UPDATE_CURRENT); pendingIntent.cancel(); From ff15d903045a303e3349194a06c3e5dc16fa30ae Mon Sep 17 00:00:00 2001 From: Matt Hadden Date: Tue, 8 Jun 2021 21:24:31 -0400 Subject: [PATCH 6/8] Fix build attempt 2 --- notifyme/src/main/java/com/allyants/notifyme/NotifyMe.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/notifyme/src/main/java/com/allyants/notifyme/NotifyMe.java b/notifyme/src/main/java/com/allyants/notifyme/NotifyMe.java index 83fffe4..6bf72e5 100644 --- a/notifyme/src/main/java/com/allyants/notifyme/NotifyMe.java +++ b/notifyme/src/main/java/com/allyants/notifyme/NotifyMe.java @@ -163,10 +163,10 @@ static void scheduleNotification(Context context, String notificationId, long ti alarmManager.set(AlarmManager.RTC_WAKEUP,time,pendingIntent); } - static void cancelNotification(Context context, String notificationId) { + static void cancelNotification(Context context, int notificationId) { AlarmManager alarmManager = (AlarmManager)context.getSystemService(ALARM_SERVICE); Intent intent = new Intent(context, NotificationPublisher.class); - PendingIntent pendingIntent = PendingIntent.getBroadcast(context, Integer.parseInt(notificationId), intent, PendingIntent.FLAG_UPDATE_CURRENT); + PendingIntent pendingIntent = PendingIntent.getBroadcast(context, notificationId, intent, PendingIntent.FLAG_UPDATE_CURRENT); pendingIntent.cancel(); Log.e(notificationId,"cancelling"); alarmManager.cancel(pendingIntent); From 04447e58891bfd1e2d5330c8766a0e1693130cbd Mon Sep 17 00:00:00 2001 From: Matt Hadden Date: Tue, 8 Jun 2021 21:28:11 -0400 Subject: [PATCH 7/8] Build fix attempt 3 --- notifyme/src/main/java/com/allyants/notifyme/NotifyMe.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/notifyme/src/main/java/com/allyants/notifyme/NotifyMe.java b/notifyme/src/main/java/com/allyants/notifyme/NotifyMe.java index 6bf72e5..7047dec 100644 --- a/notifyme/src/main/java/com/allyants/notifyme/NotifyMe.java +++ b/notifyme/src/main/java/com/allyants/notifyme/NotifyMe.java @@ -168,7 +168,7 @@ static void cancelNotification(Context context, int notificationId) { Intent intent = new Intent(context, NotificationPublisher.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, notificationId, intent, PendingIntent.FLAG_UPDATE_CURRENT); pendingIntent.cancel(); - Log.e(notificationId,"cancelling"); + Log.e(String.valueOf(notificationId),"cancelling"); alarmManager.cancel(pendingIntent); } From 8c86dabc416d84e9c2551636b8bda87beef9affb Mon Sep 17 00:00:00 2001 From: Matt Hadden Date: Fri, 11 Jun 2021 19:38:46 -0400 Subject: [PATCH 8/8] update cancelNotification to the intent has extra data --- notifyme/src/main/java/com/allyants/notifyme/NotifyMe.java | 1 + 1 file changed, 1 insertion(+) diff --git a/notifyme/src/main/java/com/allyants/notifyme/NotifyMe.java b/notifyme/src/main/java/com/allyants/notifyme/NotifyMe.java index 7047dec..bf67815 100644 --- a/notifyme/src/main/java/com/allyants/notifyme/NotifyMe.java +++ b/notifyme/src/main/java/com/allyants/notifyme/NotifyMe.java @@ -166,6 +166,7 @@ static void scheduleNotification(Context context, String notificationId, long ti static void cancelNotification(Context context, int notificationId) { AlarmManager alarmManager = (AlarmManager)context.getSystemService(ALARM_SERVICE); Intent intent = new Intent(context, NotificationPublisher.class); + intent.putExtra(NOTIFICATION_ID,String.valueOf(notificationId)); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, notificationId, intent, PendingIntent.FLAG_UPDATE_CURRENT); pendingIntent.cancel(); Log.e(String.valueOf(notificationId),"cancelling");