From fdba3fd502587b52673dd94baecf07d8ff7671c3 Mon Sep 17 00:00:00 2001 From: Matt Hadden Date: Sat, 22 May 2021 19:50:26 -0400 Subject: [PATCH 1/3] 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/3] 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/3] 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(); } - - }