From a462995dfc9b81f2e816cc856ed88f66b1bf2d15 Mon Sep 17 00:00:00 2001 From: Noah Holt Date: Tue, 8 Jan 2019 16:38:31 -0600 Subject: [PATCH 01/10] Adding new style of grouped notifications --- NotificationNotes.iml | 17 +- .../notificationnotes/NotificationMgr.java | 148 ++++++++++++++---- 2 files changed, 124 insertions(+), 41 deletions(-) diff --git a/NotificationNotes.iml b/NotificationNotes.iml index eea9428..b9c1167 100644 --- a/NotificationNotes.iml +++ b/NotificationNotes.iml @@ -1,12 +1,5 @@ - + @@ -15,14 +8,12 @@ - + - + - + \ No newline at end of file diff --git a/app/src/main/java/com/khuttun/notificationnotes/NotificationMgr.java b/app/src/main/java/com/khuttun/notificationnotes/NotificationMgr.java index bc4a931..5cfdf0a 100644 --- a/app/src/main/java/com/khuttun/notificationnotes/NotificationMgr.java +++ b/app/src/main/java/com/khuttun/notificationnotes/NotificationMgr.java @@ -23,10 +23,12 @@ public class NotificationMgr { private static final String CHANNEL_ID = "notes"; private static final int GROUP_NOTIF_ID = -1000; - + private static final int SUMMARY_NOTIF_ID = -2000; + private static final String GROUP_KEY = "com.khuttun.notificationnotes"; private Context context; private NotificationManager notificationManager; - private NotificationCompat.Builder notificationBuilder; + // private NotificationCompat.Builder notificationBuilder; + private boolean groupAPI = false; public NotificationMgr(Context context) { @@ -42,14 +44,23 @@ public NotificationMgr(Context context) NotificationManager.IMPORTANCE_LOW)); } - this.notificationBuilder = new NotificationCompat.Builder(this.context, CHANNEL_ID); - this.notificationBuilder.setSmallIcon(R.drawable.pen); - this.notificationBuilder.setOngoing(true); - this.notificationBuilder.setPriority(NotificationCompat.PRIORITY_LOW); - this.notificationBuilder.setContentIntent(PendingIntent - .getActivity(this.context, 0, new Intent(this.context, MainActivity.class), 0)); + // Grouping notifications together so they are expandable is available on Nougat and newer + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) + { + this.groupAPI = true; + } } + public NotificationCompat.Builder getNotificationBuilder() + { + NotificationCompat.Builder Builder = new NotificationCompat.Builder(this.context, CHANNEL_ID); + Builder.setSmallIcon(R.drawable.pen); + Builder.setOngoing(true); + Builder.setPriority(NotificationCompat.PRIORITY_LOW); + Builder.setContentIntent(PendingIntent + .getActivity(this.context, 0, new Intent(this.context, MainActivity.class), 0)); + return Builder; + } public void setNotification(NotificationNote note) { if (Globals.LOG) Log.d(Globals.TAG, "Notification: ID " + note.id + ", show " + note.isVisible); @@ -65,20 +76,13 @@ public void setNotification(NotificationNote note) public void setGroupNotification(ArrayList notes) { - ArrayList visibleNotes = new ArrayList<>(); - for (int i = 0; i < notes.size(); ++i) - { - NotificationNote n = notes.get(i); - if (n.isVisible) - { - visibleNotes.add(n); - } - } - + ArrayList visibleNotes = getVisibleNotes(notes); if (Globals.LOG) Log.d(Globals.TAG, "Group notification: visible notes " + visibleNotes.size()); + switch (visibleNotes.size()) { + case 0: this.notificationManager.cancel(GROUP_NOTIF_ID); break; @@ -90,43 +94,131 @@ public void setGroupNotification(ArrayList notes) break; default: - this.notificationManager.notify(GROUP_NOTIF_ID, createGroupNotification(visibleNotes)); + if (groupAPI) + { + ArrayList groupedNotes = createGroupNotifications(visibleNotes); + for (int i = 0; i < groupedNotes.size(); i++) + { + Notification note = groupedNotes.get(i); + String title = note.extras.getString("android.title"); + String text = note.extras.getString("android.text"); + int id = i == groupedNotes.size()-1 ? + SUMMARY_NOTIF_ID : findNotification(notes, title, text); + this.notificationManager.notify("note", id, note); + } + } + else + { + this.notificationManager.notify(GROUP_NOTIF_ID, + createLegacyGroupNotification(visibleNotes)); + } break; } } + private int findNotification(ArrayList notes, String title, String text) + { + for (int j = 0; j < notes.size(); j++) + { + NotificationNote note = notes.get(j); + if (note.text == text && note.title == title) + { + return note.id; + } + } + return -1; + } + + private ArrayList getVisibleNotes(ArrayList notes) { + ArrayList visibleNotes = new ArrayList<>(); + for (int i = 0; i < notes.size(); i++) + { + NotificationNote n = notes.get(i); + if (n.isVisible) + { + visibleNotes.add(n); + } + } + return visibleNotes; + } + public void clearAllNotifications() { if (Globals.LOG) Log.d(Globals.TAG, "Clearing all notifications"); this.notificationManager.cancelAll(); } + private Notification createNotification(String title, String text) { - this.notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(text)); - this.notificationBuilder.setContentTitle(title); - this.notificationBuilder.setContentText(text); - return this.notificationBuilder.build(); + NotificationCompat.Builder notificationBuilder = getNotificationBuilder(); + notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(text)); + notificationBuilder.setContentTitle(title); + notificationBuilder.setContentText(text); + return notificationBuilder.build(); } - private Notification createGroupNotification(ArrayList notes) + + // Used for Android versions below Nougat (7.0) + private Notification createLegacyGroupNotification(ArrayList notes) { + NotificationCompat.Builder notificationBuilder = getNotificationBuilder(); NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle(); for (int i = 0; i < notes.size(); ++i) { style.addLine(getGroupNotificationLine(notes.get(i))); } - this.notificationBuilder.setStyle(style); - this.notificationBuilder.setContentTitle( + notificationBuilder.setStyle(style); + notificationBuilder.setContentTitle( this.context.getString(R.string.group_notif_title) + ": " + notes.size()); - this.notificationBuilder.setContentText(getGroupNotificationLine(notes.get(0))); - return this.notificationBuilder.build(); + notificationBuilder.setContentText(getGroupNotificationLine(notes.get(0))); + return notificationBuilder.build(); } + private CharSequence getGroupNotificationLine(NotificationNote note) { Spannable sp = new SpannableString(note.title + " " + note.text); sp.setSpan(new StyleSpan(Typeface.BOLD), 0, note.title.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); return sp; } + + // Used for Android Nougat (7.0) and above + private ArrayList createGroupNotifications(ArrayList notes) { + + ArrayList groupedNotes = new ArrayList(); + for (int i = 0; i < notes.size(); ++i) + { + NotificationNote note = notes.get(i); + Notification newNote = createGroupNotification(note.title, note.text); + groupedNotes.add(newNote); + } + Notification summary = createSummaryNotification(notes.size()); + groupedNotes.add(summary); + return groupedNotes; + } + + private Notification createGroupNotification(String title, String text) + { + NotificationCompat.Builder notificationBuilder = getNotificationBuilder(); + notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(text)); + notificationBuilder.setContentTitle(title); + notificationBuilder.setContentText(text); + notificationBuilder.setGroup(this.GROUP_KEY); + notificationBuilder.setGroupSummary(false); + return notificationBuilder.build(); + } + + private Notification createSummaryNotification(int size) + { + String message = this.context.getString(R.string.group_notif_title) + ": " + size; + NotificationCompat.Builder notificationBuilder = getNotificationBuilder(); + notificationBuilder.setStyle(new NotificationCompat.InboxStyle()); + notificationBuilder.setContentTitle(message); + notificationBuilder.setContentText(message); + notificationBuilder.setGroup(this.GROUP_KEY); + notificationBuilder.setGroupSummary(true); + return notificationBuilder.build(); + } } + From ac18ce500a0790876600c40ad3ab52ca50bd8309 Mon Sep 17 00:00:00 2001 From: Noah Holt Date: Tue, 8 Jan 2019 16:56:24 -0600 Subject: [PATCH 02/10] Figured out how to remove notifications from group --- .../java/com/khuttun/notificationnotes/NotificationMgr.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/src/main/java/com/khuttun/notificationnotes/NotificationMgr.java b/app/src/main/java/com/khuttun/notificationnotes/NotificationMgr.java index 5cfdf0a..343baa0 100644 --- a/app/src/main/java/com/khuttun/notificationnotes/NotificationMgr.java +++ b/app/src/main/java/com/khuttun/notificationnotes/NotificationMgr.java @@ -61,6 +61,7 @@ public NotificationCompat.Builder getNotificationBuilder() .getActivity(this.context, 0, new Intent(this.context, MainActivity.class), 0)); return Builder; } + public void setNotification(NotificationNote note) { if (Globals.LOG) Log.d(Globals.TAG, "Notification: ID " + note.id + ", show " + note.isVisible); @@ -76,6 +77,7 @@ public void setNotification(NotificationNote note) public void setGroupNotification(ArrayList notes) { + clearAllNotifications(); ArrayList visibleNotes = getVisibleNotes(notes); if (Globals.LOG) Log.d(Globals.TAG, "Group notification: visible notes " + visibleNotes.size()); From dff13b0a8447c3bfee400346aa034ae08b2b982b Mon Sep 17 00:00:00 2001 From: Noah Holt Date: Tue, 8 Jan 2019 20:33:23 -0600 Subject: [PATCH 03/10] Added notification that tells how many notes to new API --- .../notificationnotes/NotificationMgr.java | 39 +++++++++++-------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/com/khuttun/notificationnotes/NotificationMgr.java b/app/src/main/java/com/khuttun/notificationnotes/NotificationMgr.java index 343baa0..bef43af 100644 --- a/app/src/main/java/com/khuttun/notificationnotes/NotificationMgr.java +++ b/app/src/main/java/com/khuttun/notificationnotes/NotificationMgr.java @@ -9,6 +9,7 @@ import android.graphics.Typeface; import android.os.Build; import android.support.v4.app.NotificationCompat; +import android.support.v4.app.NotificationManagerCompat; import android.text.Spannable; import android.text.SpannableString; import android.text.style.StyleSpan; @@ -26,23 +27,14 @@ public class NotificationMgr private static final int SUMMARY_NOTIF_ID = -2000; private static final String GROUP_KEY = "com.khuttun.notificationnotes"; private Context context; - private NotificationManager notificationManager; + private NotificationManagerCompat notificationManager; // private NotificationCompat.Builder notificationBuilder; private boolean groupAPI = false; public NotificationMgr(Context context) { this.context = context; - this.notificationManager = (NotificationManager) this.context.getSystemService(Context.NOTIFICATION_SERVICE); - - // NotificationChannel is required on Oreo and newer - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) - { - this.notificationManager.createNotificationChannel(new NotificationChannel( - CHANNEL_ID, - this.context.getString(R.string.channel_name), - NotificationManager.IMPORTANCE_LOW)); - } + this.notificationManager = NotificationManagerCompat.from(this.context); // Grouping notifications together so they are expandable is available on Nougat and newer if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) @@ -104,8 +96,17 @@ public void setGroupNotification(ArrayList notes) Notification note = groupedNotes.get(i); String title = note.extras.getString("android.title"); String text = note.extras.getString("android.text"); - int id = i == groupedNotes.size()-1 ? - SUMMARY_NOTIF_ID : findNotification(notes, title, text); + int id = -1; + if (i == groupedNotes.size()-1) + { + id = SUMMARY_NOTIF_ID; + } else if (i == 0) + { + id = GROUP_NOTIF_ID; + } else + { + id = findNotification(notes, title, text); + } this.notificationManager.notify("note", id, note); } } @@ -174,6 +175,8 @@ private Notification createLegacyGroupNotification(ArrayList n notificationBuilder.setContentTitle( this.context.getString(R.string.group_notif_title) + ": " + notes.size()); notificationBuilder.setContentText(getGroupNotificationLine(notes.get(0))); + notificationBuilder.setGroup(GROUP_KEY); + notificationBuilder.setGroupSummary(true); return notificationBuilder.build(); } @@ -187,15 +190,17 @@ private CharSequence getGroupNotificationLine(NotificationNote note) // Used for Android Nougat (7.0) and above private ArrayList createGroupNotifications(ArrayList notes) { - ArrayList groupedNotes = new ArrayList(); + Notification compatSummary = createLegacyGroupNotification(notes); + groupedNotes.add(compatSummary); for (int i = 0; i < notes.size(); ++i) { NotificationNote note = notes.get(i); Notification newNote = createGroupNotification(note.title, note.text); groupedNotes.add(newNote); } - Notification summary = createSummaryNotification(notes.size()); + String message = this.context.getString(R.string.group_notif_title) + ": " + notes.size(); + Notification summary = createGroupNotification(message, ""); groupedNotes.add(summary); return groupedNotes; } @@ -215,11 +220,11 @@ private Notification createSummaryNotification(int size) { String message = this.context.getString(R.string.group_notif_title) + ": " + size; NotificationCompat.Builder notificationBuilder = getNotificationBuilder(); - notificationBuilder.setStyle(new NotificationCompat.InboxStyle()); + notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(message)); notificationBuilder.setContentTitle(message); notificationBuilder.setContentText(message); notificationBuilder.setGroup(this.GROUP_KEY); - notificationBuilder.setGroupSummary(true); + notificationBuilder.setGroupSummary(false); return notificationBuilder.build(); } } From 52e6eefdcd9eeef1cbc02b37c16c0376aaa6fae7 Mon Sep 17 00:00:00 2001 From: Noah Holt Date: Tue, 8 Jan 2019 20:48:45 -0600 Subject: [PATCH 04/10] Simplified summary notification generation --- .../notificationnotes/NotificationMgr.java | 55 ++++++++----------- 1 file changed, 24 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/com/khuttun/notificationnotes/NotificationMgr.java b/app/src/main/java/com/khuttun/notificationnotes/NotificationMgr.java index bef43af..66b5abc 100644 --- a/app/src/main/java/com/khuttun/notificationnotes/NotificationMgr.java +++ b/app/src/main/java/com/khuttun/notificationnotes/NotificationMgr.java @@ -88,32 +88,24 @@ public void setGroupNotification(ArrayList notes) break; default: - if (groupAPI) + ArrayList groupedNotes = createGroupNotifications(visibleNotes); + for (int i = 0; i < groupedNotes.size(); i++) { - ArrayList groupedNotes = createGroupNotifications(visibleNotes); - for (int i = 0; i < groupedNotes.size(); i++) + Notification note = groupedNotes.get(i); + String title = note.extras.getString("android.title"); + String text = note.extras.getString("android.text"); + int id = -1; + if (i == groupedNotes.size()-1) { - Notification note = groupedNotes.get(i); - String title = note.extras.getString("android.title"); - String text = note.extras.getString("android.text"); - int id = -1; - if (i == groupedNotes.size()-1) - { - id = SUMMARY_NOTIF_ID; - } else if (i == 0) - { - id = GROUP_NOTIF_ID; - } else - { - id = findNotification(notes, title, text); - } - this.notificationManager.notify("note", id, note); + id = SUMMARY_NOTIF_ID; + } else if (i == 0) + { + id = GROUP_NOTIF_ID; + } else + { + id = findNotification(notes, title, text); } - } - else - { - this.notificationManager.notify(GROUP_NOTIF_ID, - createLegacyGroupNotification(visibleNotes)); + this.notificationManager.notify("note", id, note); } break; } @@ -191,17 +183,20 @@ private CharSequence getGroupNotificationLine(NotificationNote note) // Used for Android Nougat (7.0) and above private ArrayList createGroupNotifications(ArrayList notes) { ArrayList groupedNotes = new ArrayList(); - Notification compatSummary = createLegacyGroupNotification(notes); - groupedNotes.add(compatSummary); + Notification summary = createLegacyGroupNotification(notes); + groupedNotes.add(summary); for (int i = 0; i < notes.size(); ++i) { NotificationNote note = notes.get(i); Notification newNote = createGroupNotification(note.title, note.text); groupedNotes.add(newNote); } - String message = this.context.getString(R.string.group_notif_title) + ": " + notes.size(); - Notification summary = createGroupNotification(message, ""); - groupedNotes.add(summary); + + if (groupAPI) + { + Notification top = createTopNotification(notes.size()); + groupedNotes.add(top); + } return groupedNotes; } @@ -216,13 +211,11 @@ private Notification createGroupNotification(String title, String text) return notificationBuilder.build(); } - private Notification createSummaryNotification(int size) + private Notification createTopNotification(int size) { String message = this.context.getString(R.string.group_notif_title) + ": " + size; NotificationCompat.Builder notificationBuilder = getNotificationBuilder(); - notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(message)); notificationBuilder.setContentTitle(message); - notificationBuilder.setContentText(message); notificationBuilder.setGroup(this.GROUP_KEY); notificationBuilder.setGroupSummary(false); return notificationBuilder.build(); From 81bd4d2f920c26b5e60fd5d0b1e9cd7daef5487b Mon Sep 17 00:00:00 2001 From: Noah Holt Date: Wed, 9 Jan 2019 15:18:25 -0600 Subject: [PATCH 05/10] Cleaned up a bit & added comments --- .../notificationnotes/NotificationMgr.java | 44 ++++++++++++------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/com/khuttun/notificationnotes/NotificationMgr.java b/app/src/main/java/com/khuttun/notificationnotes/NotificationMgr.java index 66b5abc..1ad3244 100644 --- a/app/src/main/java/com/khuttun/notificationnotes/NotificationMgr.java +++ b/app/src/main/java/com/khuttun/notificationnotes/NotificationMgr.java @@ -1,8 +1,6 @@ package com.khuttun.notificationnotes; import android.app.Notification; -import android.app.NotificationChannel; -import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; @@ -24,11 +22,10 @@ public class NotificationMgr { private static final String CHANNEL_ID = "notes"; private static final int GROUP_NOTIF_ID = -1000; - private static final int SUMMARY_NOTIF_ID = -2000; + private static final int SUMMARY_NOTIF_ID = -2000; private static final String GROUP_KEY = "com.khuttun.notificationnotes"; private Context context; private NotificationManagerCompat notificationManager; - // private NotificationCompat.Builder notificationBuilder; private boolean groupAPI = false; public NotificationMgr(Context context) @@ -57,6 +54,7 @@ public NotificationCompat.Builder getNotificationBuilder() public void setNotification(NotificationNote note) { if (Globals.LOG) Log.d(Globals.TAG, "Notification: ID " + note.id + ", show " + note.isVisible); + if (note.isVisible) { this.notificationManager.notify(note.id, createNotification(note.title, note.text)); @@ -76,7 +74,6 @@ public void setGroupNotification(ArrayList notes) switch (visibleNotes.size()) { - case 0: this.notificationManager.cancel(GROUP_NOTIF_ID); break; @@ -95,15 +92,17 @@ public void setGroupNotification(ArrayList notes) String title = note.extras.getString("android.title"); String text = note.extras.getString("android.text"); int id = -1; - if (i == groupedNotes.size()-1) + if (i == groupedNotes.size() - 1) { - id = SUMMARY_NOTIF_ID; - } else if (i == 0) + id = SUMMARY_NOTIF_ID; + } + else if (i == 0) { id = GROUP_NOTIF_ID; - } else + } + else { - id = findNotification(notes, title, text); + id = findNotificationID(notes, title, text); } this.notificationManager.notify("note", id, note); } @@ -111,7 +110,7 @@ public void setGroupNotification(ArrayList notes) } } - private int findNotification(ArrayList notes, String title, String text) + private int findNotificationID(ArrayList notes, String title, String text) { for (int j = 0; j < notes.size(); j++) { @@ -124,7 +123,8 @@ private int findNotification(ArrayList notes, String title, St return -1; } - private ArrayList getVisibleNotes(ArrayList notes) { + private ArrayList getVisibleNotes(ArrayList notes) + { ArrayList visibleNotes = new ArrayList<>(); for (int i = 0; i < notes.size(); i++) { @@ -154,8 +154,12 @@ private Notification createNotification(String title, String text) } - // Used for Android versions below Nougat (7.0) - private Notification createLegacyGroupNotification(ArrayList notes) + /** + * Creates a notification with the number of notes and the first line of each note. + * Used for Android versions below Nougat (7.0). + * Is required for Android versions Nougat and beyond, but does not show. + */ + private Notification createSummaryNotification(ArrayList notes) { NotificationCompat.Builder notificationBuilder = getNotificationBuilder(); NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle(); @@ -180,10 +184,10 @@ private CharSequence getGroupNotificationLine(NotificationNote note) return sp; } - // Used for Android Nougat (7.0) and above - private ArrayList createGroupNotifications(ArrayList notes) { + private ArrayList createGroupNotifications(ArrayList notes) + { ArrayList groupedNotes = new ArrayList(); - Notification summary = createLegacyGroupNotification(notes); + Notification summary = createSummaryNotification(notes); groupedNotes.add(summary); for (int i = 0; i < notes.size(); ++i) { @@ -200,6 +204,9 @@ private ArrayList createGroupNotifications(ArrayList Date: Sun, 13 Jan 2019 14:39:16 -0600 Subject: [PATCH 06/10] Seperated code paths for Android Nougat and Pre-Nougat --- .../notificationnotes/NotificationMgr.java | 44 +++++++++++-------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/com/khuttun/notificationnotes/NotificationMgr.java b/app/src/main/java/com/khuttun/notificationnotes/NotificationMgr.java index 1ad3244..92957f0 100644 --- a/app/src/main/java/com/khuttun/notificationnotes/NotificationMgr.java +++ b/app/src/main/java/com/khuttun/notificationnotes/NotificationMgr.java @@ -85,26 +85,34 @@ public void setGroupNotification(ArrayList notes) break; default: - ArrayList groupedNotes = createGroupNotifications(visibleNotes); - for (int i = 0; i < groupedNotes.size(); i++) + if (groupAPI) { - Notification note = groupedNotes.get(i); - String title = note.extras.getString("android.title"); - String text = note.extras.getString("android.text"); - int id = -1; - if (i == groupedNotes.size() - 1) + ArrayList groupedNotes = createGroupNotifications(visibleNotes); + for (int i = 0; i < groupedNotes.size(); i++) { - id = SUMMARY_NOTIF_ID; + Notification note = groupedNotes.get(i); + String title = note.extras.getString("android.title"); + String text = note.extras.getString("android.text"); + int id = -1; + if (i == groupedNotes.size() - 1) + { + id = SUMMARY_NOTIF_ID; + } + else if (i == 0) + { + id = GROUP_NOTIF_ID; + } + else + { + id = findNotificationID(notes, title, text); + } + this.notificationManager.notify("note", id, note); } - else if (i == 0) - { - id = GROUP_NOTIF_ID; - } - else - { - id = findNotificationID(notes, title, text); - } - this.notificationManager.notify("note", id, note); + } + else + { + this.notificationManager.notify("note", SUMMARY_NOTIF_ID, + createSummaryNotification(visibleNotes)); } break; } @@ -156,7 +164,7 @@ private Notification createNotification(String title, String text) /** * Creates a notification with the number of notes and the first line of each note. - * Used for Android versions below Nougat (7.0). + * Visible for Android versions below Nougat (7.0). * Is required for Android versions Nougat and beyond, but does not show. */ private Notification createSummaryNotification(ArrayList notes) From e05343b346ced555acf0a129f701ed163edaffb0 Mon Sep 17 00:00:00 2001 From: Noah Holt Date: Thu, 24 Jan 2019 19:46:48 -0600 Subject: [PATCH 07/10] Simplified display & creation of notifications --- .../notificationnotes/NotificationMgr.java | 93 +++++++++---------- 1 file changed, 43 insertions(+), 50 deletions(-) diff --git a/app/src/main/java/com/khuttun/notificationnotes/NotificationMgr.java b/app/src/main/java/com/khuttun/notificationnotes/NotificationMgr.java index 92957f0..2e059b3 100644 --- a/app/src/main/java/com/khuttun/notificationnotes/NotificationMgr.java +++ b/app/src/main/java/com/khuttun/notificationnotes/NotificationMgr.java @@ -6,6 +6,7 @@ import android.content.Intent; import android.graphics.Typeface; import android.os.Build; +import android.os.Bundle; import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationManagerCompat; import android.text.Spannable; @@ -87,51 +88,18 @@ public void setGroupNotification(ArrayList notes) default: if (groupAPI) { - ArrayList groupedNotes = createGroupNotifications(visibleNotes); - for (int i = 0; i < groupedNotes.size(); i++) - { - Notification note = groupedNotes.get(i); - String title = note.extras.getString("android.title"); - String text = note.extras.getString("android.text"); - int id = -1; - if (i == groupedNotes.size() - 1) - { - id = SUMMARY_NOTIF_ID; - } - else if (i == 0) - { - id = GROUP_NOTIF_ID; - } - else - { - id = findNotificationID(notes, title, text); - } - this.notificationManager.notify("note", id, note); - } + displayGroupNotifications(visibleNotes); } else { - this.notificationManager.notify("note", SUMMARY_NOTIF_ID, + this.notificationManager.notify(SUMMARY_NOTIF_ID, createSummaryNotification(visibleNotes)); } break; } } - private int findNotificationID(ArrayList notes, String title, String text) - { - for (int j = 0; j < notes.size(); j++) - { - NotificationNote note = notes.get(j); - if (note.text == text && note.title == title) - { - return note.id; - } - } - return -1; - } - - private ArrayList getVisibleNotes(ArrayList notes) + private static ArrayList getVisibleNotes(ArrayList notes) { ArrayList visibleNotes = new ArrayList<>(); for (int i = 0; i < notes.size(); i++) @@ -151,7 +119,6 @@ public void clearAllNotifications() this.notificationManager.cancelAll(); } - private Notification createNotification(String title, String text) { NotificationCompat.Builder notificationBuilder = getNotificationBuilder(); @@ -161,7 +128,6 @@ private Notification createNotification(String title, String text) return notificationBuilder.build(); } - /** * Creates a notification with the number of notes and the first line of each note. * Visible for Android versions below Nougat (7.0). @@ -181,10 +147,16 @@ private Notification createSummaryNotification(ArrayList notes notificationBuilder.setContentText(getGroupNotificationLine(notes.get(0))); notificationBuilder.setGroup(GROUP_KEY); notificationBuilder.setGroupSummary(true); + // For Android Nougat and above (7.0), create a Bundle containing thek + if (groupAPI) + { + Bundle extraNotificationInfo = new Bundle(1); + extraNotificationInfo.putInt("note.id", SUMMARY_NOTIF_ID); + notificationBuilder.addExtras(extraNotificationInfo); + } return notificationBuilder.build(); } - private CharSequence getGroupNotificationLine(NotificationNote note) { Spannable sp = new SpannableString(note.title + " " + note.text); @@ -192,6 +164,25 @@ private CharSequence getGroupNotificationLine(NotificationNote note) return sp; } + private void displayGroupNotifications(ArrayList visibleNotes) + { + ArrayList groupedNotes = createGroupNotifications(visibleNotes); + for (int i = 0; i < groupedNotes.size(); i++) + { + Notification note = groupedNotes.get(i); + int id = note.extras.getInt("note.id"); + if (Globals.LOG) + { + Log.d(Globals.TAG, "Displaying notification for note with id ".concat(Integer.toString(id))); + } + this.notificationManager.notify(id, note); + } + } + + /** + * Creates the group of notifications that will be bundled together into one notification. + * Used for Android Nougat (7.0) and beyond. + */ private ArrayList createGroupNotifications(ArrayList notes) { ArrayList groupedNotes = new ArrayList(); @@ -199,30 +190,29 @@ private ArrayList createGroupNotifications(ArrayList Date: Thu, 24 Jan 2019 20:48:57 -0600 Subject: [PATCH 08/10] Reverted changes to .iml file --- NotificationNotes.iml | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/NotificationNotes.iml b/NotificationNotes.iml index b9c1167..eea9428 100644 --- a/NotificationNotes.iml +++ b/NotificationNotes.iml @@ -1,5 +1,12 @@ - + @@ -8,12 +15,14 @@ - + - + - \ No newline at end of file + From bc3f7ebb01b8d9abf9cdf2d6eca107984476eed5 Mon Sep 17 00:00:00 2001 From: Noah Holt Date: Sat, 26 Jan 2019 14:50:02 -0600 Subject: [PATCH 09/10] Reverted changes to notificationManager type --- .../notificationnotes/NotificationMgr.java | 87 ++++++++++--------- 1 file changed, 48 insertions(+), 39 deletions(-) diff --git a/app/src/main/java/com/khuttun/notificationnotes/NotificationMgr.java b/app/src/main/java/com/khuttun/notificationnotes/NotificationMgr.java index 2e059b3..cf46202 100644 --- a/app/src/main/java/com/khuttun/notificationnotes/NotificationMgr.java +++ b/app/src/main/java/com/khuttun/notificationnotes/NotificationMgr.java @@ -1,6 +1,8 @@ package com.khuttun.notificationnotes; import android.app.Notification; +import android.app.NotificationChannel; +import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; @@ -26,30 +28,39 @@ public class NotificationMgr private static final int SUMMARY_NOTIF_ID = -2000; private static final String GROUP_KEY = "com.khuttun.notificationnotes"; private Context context; - private NotificationManagerCompat notificationManager; + private NotificationManager notificationManager; private boolean groupAPI = false; + private NotificationCompat.Builder notificationBuilder; public NotificationMgr(Context context) { this.context = context; - this.notificationManager = NotificationManagerCompat.from(this.context); + // Need the NotificationManager type for a NotificationChannel, + // the official documentation saying to use NotificationManager.Compat is + // wrong + this.notificationManager = (NotificationManager) this.context.getSystemService(Context.NOTIFICATION_SERVICE); + + // A NotificationChannel is needed for Android Oreo and above + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) + { + this.notificationManager.createNotificationChannel(new NotificationChannel( + CHANNEL_ID, + this.context.getString(R.string.channel_name), + NotificationManager.IMPORTANCE_LOW)); + } // Grouping notifications together so they are expandable is available on Nougat and newer if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { this.groupAPI = true; } - } - public NotificationCompat.Builder getNotificationBuilder() - { - NotificationCompat.Builder Builder = new NotificationCompat.Builder(this.context, CHANNEL_ID); - Builder.setSmallIcon(R.drawable.pen); - Builder.setOngoing(true); - Builder.setPriority(NotificationCompat.PRIORITY_LOW); - Builder.setContentIntent(PendingIntent + notificationBuilder = new NotificationCompat.Builder(this.context, CHANNEL_ID); + notificationBuilder.setSmallIcon(R.drawable.pen); + notificationBuilder.setOngoing(true); + notificationBuilder.setPriority(NotificationCompat.PRIORITY_LOW); + notificationBuilder.setContentIntent(PendingIntent .getActivity(this.context, 0, new Intent(this.context, MainActivity.class), 0)); - return Builder; } public void setNotification(NotificationNote note) @@ -68,7 +79,6 @@ public void setNotification(NotificationNote note) public void setGroupNotification(ArrayList notes) { - clearAllNotifications(); ArrayList visibleNotes = getVisibleNotes(notes); if (Globals.LOG) Log.d(Globals.TAG, "Group notification: visible notes " + visibleNotes.size()); @@ -121,11 +131,10 @@ public void clearAllNotifications() private Notification createNotification(String title, String text) { - NotificationCompat.Builder notificationBuilder = getNotificationBuilder(); - notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(text)); - notificationBuilder.setContentTitle(title); - notificationBuilder.setContentText(text); - return notificationBuilder.build(); + this.notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(text)); + this.notificationBuilder.setContentTitle(title); + this.notificationBuilder.setContentText(text); + return this.notificationBuilder.build(); } /** @@ -135,26 +144,25 @@ private Notification createNotification(String title, String text) */ private Notification createSummaryNotification(ArrayList notes) { - NotificationCompat.Builder notificationBuilder = getNotificationBuilder(); NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle(); for (int i = 0; i < notes.size(); ++i) { style.addLine(getGroupNotificationLine(notes.get(i))); } - notificationBuilder.setStyle(style); - notificationBuilder.setContentTitle( + this.notificationBuilder.setStyle(style); + this.notificationBuilder.setContentTitle( this.context.getString(R.string.group_notif_title) + ": " + notes.size()); - notificationBuilder.setContentText(getGroupNotificationLine(notes.get(0))); - notificationBuilder.setGroup(GROUP_KEY); - notificationBuilder.setGroupSummary(true); + this.notificationBuilder.setContentText(getGroupNotificationLine(notes.get(0))); + this.notificationBuilder.setGroup(GROUP_KEY); + this.notificationBuilder.setGroupSummary(true); // For Android Nougat and above (7.0), create a Bundle containing thek if (groupAPI) { Bundle extraNotificationInfo = new Bundle(1); extraNotificationInfo.putInt("note.id", SUMMARY_NOTIF_ID); - notificationBuilder.addExtras(extraNotificationInfo); + this.notificationBuilder.addExtras(extraNotificationInfo); } - return notificationBuilder.build(); + return this.notificationBuilder.build(); } private CharSequence getGroupNotificationLine(NotificationNote note) @@ -166,6 +174,7 @@ private CharSequence getGroupNotificationLine(NotificationNote note) private void displayGroupNotifications(ArrayList visibleNotes) { + clearAllNotifications(); ArrayList groupedNotes = createGroupNotifications(visibleNotes); for (int i = 0; i < groupedNotes.size(); i++) { @@ -204,16 +213,15 @@ private ArrayList createGroupNotifications(ArrayList Date: Sun, 27 Jan 2019 16:44:55 -0600 Subject: [PATCH 10/10] Streamlined creation of group notes & fixed issue with unchecked notes showing --- .../notificationnotes/NotesListAdapter.java | 1 + .../notificationnotes/NotificationMgr.java | 76 +++++-------------- 2 files changed, 20 insertions(+), 57 deletions(-) diff --git a/app/src/main/java/com/khuttun/notificationnotes/NotesListAdapter.java b/app/src/main/java/com/khuttun/notificationnotes/NotesListAdapter.java index 12934a3..ce294ca 100644 --- a/app/src/main/java/com/khuttun/notificationnotes/NotesListAdapter.java +++ b/app/src/main/java/com/khuttun/notificationnotes/NotesListAdapter.java @@ -202,6 +202,7 @@ private void setNotification(NotificationNote n) if (PreferenceManager.getDefaultSharedPreferences(this.context).getBoolean(this.context .getString(R.string.group_notif_pref_key), false)) { + this.notificationMgr.checkNotification(n); this.notificationMgr.setGroupNotification(this.notes); } else diff --git a/app/src/main/java/com/khuttun/notificationnotes/NotificationMgr.java b/app/src/main/java/com/khuttun/notificationnotes/NotificationMgr.java index cf46202..7d57454 100644 --- a/app/src/main/java/com/khuttun/notificationnotes/NotificationMgr.java +++ b/app/src/main/java/com/khuttun/notificationnotes/NotificationMgr.java @@ -63,6 +63,17 @@ public NotificationMgr(Context context) .getActivity(this.context, 0, new Intent(this.context, MainActivity.class), 0)); } + /** + * Used for group mode to check if a notification that is + * currently being displayed needs to be canceled + */ + public void checkNotification(NotificationNote note) + { + if (!note.isVisible) + { + this.notificationManager.cancel(note.id); + } + } public void setNotification(NotificationNote note) { if (Globals.LOG) Log.d(Globals.TAG, "Notification: ID " + note.id + ", show " + note.isVisible); @@ -82,7 +93,6 @@ public void setGroupNotification(ArrayList notes) ArrayList visibleNotes = getVisibleNotes(notes); if (Globals.LOG) Log.d(Globals.TAG, "Group notification: visible notes " + visibleNotes.size()); - switch (visibleNotes.size()) { case 0: @@ -139,8 +149,8 @@ private Notification createNotification(String title, String text) /** * Creates a notification with the number of notes and the first line of each note. - * Visible for Android versions below Nougat (7.0). - * Is required for Android versions Nougat and beyond, but does not show. + * This notification is shown for Android Versions below Nougat, and is still necessary + * for Android Nougat, but is not shown. */ private Notification createSummaryNotification(ArrayList notes) { @@ -155,13 +165,6 @@ private Notification createSummaryNotification(ArrayList notes this.notificationBuilder.setContentText(getGroupNotificationLine(notes.get(0))); this.notificationBuilder.setGroup(GROUP_KEY); this.notificationBuilder.setGroupSummary(true); - // For Android Nougat and above (7.0), create a Bundle containing thek - if (groupAPI) - { - Bundle extraNotificationInfo = new Bundle(1); - extraNotificationInfo.putInt("note.id", SUMMARY_NOTIF_ID); - this.notificationBuilder.addExtras(extraNotificationInfo); - } return this.notificationBuilder.build(); } @@ -174,38 +177,17 @@ private CharSequence getGroupNotificationLine(NotificationNote note) private void displayGroupNotifications(ArrayList visibleNotes) { - clearAllNotifications(); - ArrayList groupedNotes = createGroupNotifications(visibleNotes); - for (int i = 0; i < groupedNotes.size(); i++) + int visibleSize = visibleNotes.size(); + for (int i = 0; i < visibleSize; i++) { - Notification note = groupedNotes.get(i); - int id = note.extras.getInt("note.id"); + NotificationNote note = visibleNotes.get(i); if (Globals.LOG) { - Log.d(Globals.TAG, "Displaying notification for note with id ".concat(Integer.toString(id))); + Log.d(Globals.TAG, "Displaying notification for note with id ".concat(Integer.toString(note.id))); } - this.notificationManager.notify(id, note); + this.notificationManager.notify(note.id, createGroupNotification(note)); } - } - - /** - * Creates the group of notifications that will be bundled together into one notification. - * Used for Android Nougat (7.0) and beyond. - */ - private ArrayList createGroupNotifications(ArrayList notes) - { - ArrayList groupedNotes = new ArrayList(); - Notification summary = createSummaryNotification(notes); - groupedNotes.add(summary); - for (int i = 0; i < notes.size(); ++i) - { - Notification newNote = createGroupNotification(notes.get(i)); - groupedNotes.add(newNote); - } - - Notification top = createTopNotification(notes.size()); - groupedNotes.add(top); - return groupedNotes; + this.notificationManager.notify(SUMMARY_NOTIF_ID, createSummaryNotification(visibleNotes)); } /** @@ -218,26 +200,6 @@ private Notification createGroupNotification(NotificationNote note) this.notificationBuilder.setContentText(note.text); this.notificationBuilder.setGroup(this.GROUP_KEY); this.notificationBuilder.setGroupSummary(false); - Bundle extraNotificationInfo = new Bundle(1); - extraNotificationInfo.putInt("note.id", note.id); - this.notificationBuilder.addExtras(extraNotificationInfo); - return this.notificationBuilder.build(); - } - - /** - * Creates a notification that shows how many notes there are. - */ - private Notification createTopNotification(int size) - { - String message = this.context.getString(R.string.group_notif_title) + ": " + size; - this.notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(null)); - this.notificationBuilder.setContentTitle(message); - this.notificationBuilder.setContentText(null); - this.notificationBuilder.setGroup(this.GROUP_KEY); - this.notificationBuilder.setGroupSummary(false); - Bundle extraNotificationInfo = new Bundle(1); - extraNotificationInfo.putInt("note.id", GROUP_NOTIF_ID); - this.notificationBuilder.setExtras(extraNotificationInfo); return this.notificationBuilder.build(); } }