Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split calendar notifications into channels #1571

Merged
merged 2 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions app/src/main/java/com/android/calendar/AllInOneActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -267,12 +267,12 @@ protected void onCreate(Bundle icicle) {
// This needs to be created before setContentView
mController = CalendarController.getInstance(this);

// Create notification channel
AlertService.createChannels(this);

// Check and ask for most needed permissions
checkAppPermissions();

// Create notification channels
AlertService.createChannels(this);
Wv5twkFEKh54vo4tta9yu7dHa3 marked this conversation as resolved.
Show resolved Hide resolved

// Get time from intent or icicle
long timeMillis = -1;
int viewType = -1;
Expand Down
14 changes: 6 additions & 8 deletions app/src/main/java/com/android/calendar/alerts/AlertReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

package com.android.calendar.alerts;

import static com.android.calendar.alerts.AlertService.ALERT_CHANNEL_ID;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
Expand Down Expand Up @@ -230,10 +228,10 @@ private static PendingIntent createAlertActivityIntent(Context context) {
}

public static NotificationWrapper makeBasicNotification(Context context, String title,
String summaryText, long startMillis, long endMillis, long eventId,
String summaryText, long startMillis, long endMillis, long eventId, long calendarId,
int notificationId, boolean doPopup, int priority) {
Notification n = buildBasicNotification(new Notification.Builder(context),
context, title, summaryText, startMillis, endMillis, eventId, notificationId,
context, title, summaryText, startMillis, endMillis, eventId, calendarId, notificationId,
doPopup, priority, false);
return new NotificationWrapper(n, notificationId, eventId, startMillis, endMillis, doPopup);
}
Expand All @@ -247,7 +245,7 @@ public static boolean isResolveIntent(Context context, Intent intent) {

private static Notification buildBasicNotification(Notification.Builder notificationBuilder,
Context context, String title, String summaryText, long startMillis, long endMillis,
long eventId, int notificationId, boolean doPopup, int priority,
long eventId, long calendarId, int notificationId, boolean doPopup, int priority,
boolean addActionButtons) {
Resources resources = context.getResources();
if (title == null || title.length() == 0) {
Expand All @@ -274,7 +272,7 @@ private static Notification buildBasicNotification(Notification.Builder notifica

// Add setting channel ID for Oreo or later
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationBuilder.setChannelId(ALERT_CHANNEL_ID);
notificationBuilder.setChannelId(UtilsKt.channelId(calendarId));
}

if (doPopup) {
Expand Down Expand Up @@ -346,10 +344,10 @@ private static Notification buildBasicNotification(Notification.Builder notifica
*/
public static NotificationWrapper makeExpandingNotification(Context context, String title,
String summaryText, String description, long startMillis, long endMillis, long eventId,
int notificationId, boolean doPopup, int priority) {
long calendarId, int notificationId, boolean doPopup, int priority) {
Notification.Builder basicBuilder = new Notification.Builder(context);
Notification notification = buildBasicNotification(basicBuilder, context, title,
summaryText, startMillis, endMillis, eventId, notificationId, doPopup,
summaryText, startMillis, endMillis, eventId, calendarId, notificationId, doPopup,
priority, true);

// Create a new-style expanded notification
Expand Down
32 changes: 17 additions & 15 deletions app/src/main/java/com/android/calendar/alerts/AlertService.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
*/
public class AlertService extends Service {

public static final String ALERT_CHANNEL_ID = "alert_channel_01";
public static final String ALERT_CHANNEL_GROUP_ID = "alert_channel_group_01";
public static final String FOREGROUND_CHANNEL_ID = "foreground_channel_01";

// Hard limit to the number of notifications displayed.
Expand All @@ -89,6 +89,7 @@ public class AlertService extends Service {
CalendarAlerts.BEGIN, // 9
CalendarAlerts.END, // 10
CalendarAlerts.DESCRIPTION, // 11
CalendarAlerts.CALENDAR_ID, // 12
};
private static final String TAG = "AlertService";
private static final int ALERT_INDEX_ID = 0;
Expand All @@ -103,6 +104,7 @@ public class AlertService extends Service {
private static final int ALERT_INDEX_BEGIN = 9;
private static final int ALERT_INDEX_END = 10;
private static final int ALERT_INDEX_DESCRIPTION = 11;
private static final int ALERT_INDEX_CALENDAR_ID = 12;
private static final String ACTIVE_ALERTS_SELECTION = "(" + CalendarAlerts.STATE + "=? OR "
+ CalendarAlerts.STATE + "=?) AND " + CalendarAlerts.ALARM_TIME + "<=";
private static final String[] ACTIVE_ALERTS_SELECTION_ARGS = new String[] {
Expand Down Expand Up @@ -282,7 +284,7 @@ public static boolean generateAlerts(Context context, NotificationMgr nm,
String summaryText = AlertUtils.formatTimeLocation(context, info.startMillis,
info.allDay, info.location);
notification = AlertReceiver.makeBasicNotification(context, info.eventName,
summaryText, info.startMillis, info.endMillis, info.eventId,
summaryText, info.startMillis, info.endMillis, info.eventId, info.calendarId,
AlertUtils.EXPIRED_GROUP_NOTIFICATION_ID, false,
Notification.PRIORITY_MIN);
} else {
Expand Down Expand Up @@ -473,6 +475,7 @@ static int processQuery(final Cursor alertCursor, final Context context,
while (alertCursor.moveToNext()) {
final long alertId = alertCursor.getLong(ALERT_INDEX_ID);
final long eventId = alertCursor.getLong(ALERT_INDEX_EVENT_ID);
final long calendarId = alertCursor.getLong(ALERT_INDEX_CALENDAR_ID);
final int minutes = alertCursor.getInt(ALERT_INDEX_MINUTES);
final String eventName = alertCursor.getString(ALERT_INDEX_TITLE);
final String description = alertCursor.getString(ALERT_INDEX_DESCRIPTION);
Expand Down Expand Up @@ -511,6 +514,7 @@ static int processQuery(final Cursor alertCursor, final Context context,
msgBuilder.append("alertCursor result: alarmTime:").append(alarmTime)
.append(" alertId:").append(alertId)
.append(" eventId:").append(eventId)
.append(" calendarId:").append(calendarId)
.append(" state: ").append(state)
.append(" minutes:").append(minutes)
.append(" declined:").append(declined)
Expand Down Expand Up @@ -590,7 +594,7 @@ static int processQuery(final Cursor alertCursor, final Context context,

// TODO: Prefer accepted events in case of ties.
NotificationInfo newInfo = new NotificationInfo(eventName, location,
description, beginTime, endTime, eventId, allDay, newAlert);
description, beginTime, endTime, eventId, calendarId, allDay, newAlert);

// Adjust for all day events to ensure the right bucket. Don't use the 1/4 event
// duration grace period for these.
Expand Down Expand Up @@ -707,8 +711,8 @@ private static void postNotification(NotificationInfo info, String summaryText,

String tickerText = getTickerText(info.eventName, info.location);
NotificationWrapper notification = AlertReceiver.makeExpandingNotification(context,
info.eventName, summaryText, info.description, info.startMillis,
info.endMillis, info.eventId, notificationId, prefs.getDoPopup(), priorityVal);
info.eventName, summaryText, info.description, info.startMillis, info.endMillis,
info.eventId, info.calendarId, notificationId, prefs.getDoPopup(), priorityVal);

boolean quietUpdate = true;
String ringtone = NotificationPrefs.EMPTY_RINGTONE;
Expand Down Expand Up @@ -967,24 +971,20 @@ public IBinder onBind(Intent intent) {

public static void createChannels(Context context) {
if (Utils.isOreoOrLater()) {
// Create notification channel
NotificationMgr nm = new NotificationMgrWrapper(
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE));
NotificationManager nm =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

NotificationChannel channel = new NotificationChannel(
ALERT_CHANNEL_ID,
context.getString(R.string.standalone_app_label),
NotificationManager.IMPORTANCE_HIGH);
channel.enableLights(true);
// Create a channel per calendar (so that the user can turn it off with granularity)
UtilsKt.createPerCalendarChannels(context, nm);

// Create a "Background tasks" channel to keep the app alive
NotificationChannel foregroundChannel = new NotificationChannel(
FOREGROUND_CHANNEL_ID,
context.getString(R.string.foreground_notification_channel_name),
NotificationManager.IMPORTANCE_LOW);
foregroundChannel.setDescription(
context.getString(R.string.foreground_notification_channel_description));

nm.createNotificationChannel(channel);
nm.createNotificationChannel(foregroundChannel);
}
}
Expand Down Expand Up @@ -1056,17 +1056,19 @@ static class NotificationInfo {
long startMillis;
long endMillis;
long eventId;
long calendarId;
boolean allDay;
boolean newAlert;

NotificationInfo(String eventName, String location, String description, long startMillis,
long endMillis, long eventId, boolean allDay, boolean newAlert) {
long endMillis, long eventId, long calendarId, boolean allDay, boolean newAlert) {
this.eventName = eventName;
this.location = location;
this.description = description;
this.startMillis = startMillis;
this.endMillis = endMillis;
this.eventId = eventId;
this.calendarId = calendarId;
this.newAlert = newAlert;
this.allDay = allDay;
}
Expand Down
78 changes: 78 additions & 0 deletions app/src/main/java/com/android/calendar/alerts/Utils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.android.calendar.alerts

import android.app.NotificationChannel
import android.app.NotificationChannelGroup
import android.app.NotificationManager
import android.content.Context
import android.os.Build
import android.provider.CalendarContract
import androidx.annotation.RequiresApi
import androidx.core.database.getStringOrNull
import com.android.calendar.Utils
import com.android.calendar.alerts.AlertService.ALERT_CHANNEL_GROUP_ID
import ws.xsoh.etar.R


val PROJECTION = arrayOf(
CalendarContract.Calendars._ID,
CalendarContract.Calendars.CALENDAR_DISPLAY_NAME,
)

data class CalendarChannel(val id: Long, val displayName: String?)

fun channelId(id: Long) = "calendar$id"

@RequiresApi(Build.VERSION_CODES.O)
fun createPerCalendarChannels(context: Context, nm: NotificationManager) {
val calendars: MutableList<CalendarChannel> = mutableListOf()

// Make sure we have the right permissions to access the calendar list
if (!Utils.isCalendarPermissionGranted(context, false)) return

context.contentResolver.query(
CalendarContract.Calendars.CONTENT_URI,
PROJECTION,
null,
null,
CalendarContract.Calendars.ACCOUNT_NAME
)?.use {
while (it.moveToNext()) {
val id = it.getLong(PROJECTION.indexOf(CalendarContract.Calendars._ID))
val displayName =
it.getStringOrNull(PROJECTION.indexOf(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME))

calendars.add(CalendarChannel(id, displayName))
}
}

// Make NotificationChannel group for calendars
nm.createNotificationChannelGroup(
NotificationChannelGroup(
ALERT_CHANNEL_GROUP_ID, context.getString(R.string.calendars)
)
)

// Fetch list of existing notification channels
val toDelete = nm.notificationChannels.filter { channel: NotificationChannel ->
// Only consider the channels of the calendar group
channel.group == ALERT_CHANNEL_GROUP_ID
// And only keep those that don't correspond to calendars (so those we want to delete)
&& !calendars.any { channelId(it.id) == channel.id }
}

// We want to delete these channels because they don't correspond to any calendars (anymore)
toDelete.forEach { nm.deleteNotificationChannel(it.id) }

val channels = calendars.map {
NotificationChannel(
channelId(it.id),
if (it.displayName.isNullOrBlank()) context.getString(R.string.preferences_calendar_no_display_name) else it.displayName,
NotificationManager.IMPORTANCE_HIGH
).apply {
enableLights(true)
group = ALERT_CHANNEL_GROUP_ID
}
}

channels.forEach { nm.createNotificationChannel(it) }
}
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -805,4 +805,7 @@

<string name="pref_theme_default" translatable="false">light</string>

<!-- Name of notification channel group -->
<string name="calendars">Calendars</string>

</resources>