Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a014ab43 authored by Demon000's avatar Demon000 Committed by Sam Mortimer
Browse files

DeskClock: move to NotificationChannels

Change-Id: I39a05acb0290578c9f794896dcbe25537a2afe51
parent 3424e890
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -1051,4 +1051,10 @@
    -->
    <string name="alarm_is_snoozed"><xliff:g id="alarm_time" example="14:20">%s</xliff:g> alarm snoozed for 10 minutes</string>

    <!-- The user visible name of the stopwatch channel. -->
    <string name="stopwatch_channel_name">Stopwatch</string>
    <!-- The user visible name of the timers channel. -->
    <string name="timer_channel_name">Timers</string>
    <!-- The user visible name of the alarms channel. -->
    <string name="alarm_channel_name">Alarms</string>
</resources>
+41 −1
Original line number Diff line number Diff line
@@ -20,6 +20,9 @@ import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.AlarmManager;
import android.app.AlarmManager.AlarmClockInfo;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.ContentResolver;
@@ -625,4 +628,41 @@ public class Utils {
                    AccessibilityActionCompat.ACTION_CLICK.getId(), mLabel));
        }
    }

    public static void createNotificationChannel(Context context, String id, String name, int importance) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
            return;
        }

        NotificationManager manager =
                ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE));

        NotificationChannel channel = new NotificationChannel(id, name, importance);

        manager.createNotificationChannel(channel);
    }

    static boolean areNotificationChannelsCreated = false;

    public static final String STOPWATCH_CHANNEL = "stopwatch_notification_channel";
    public static final String TIMER_CHANNEL = "timer_notification_channel";
    public static final String ALARM_CHANNEL = "alarm_notification_channel";

    public static void createNotificationChannelsIfNeeded(Context context) {
        if (areNotificationChannelsCreated) {
            return;
        }

        areNotificationChannelsCreated = true;

        createNotificationChannel(context, STOPWATCH_CHANNEL,
                context.getString(R.string.stopwatch_channel_name),
                NotificationManager.IMPORTANCE_HIGH);
        createNotificationChannel(context, TIMER_CHANNEL,
                context.getString(R.string.timer_channel_name),
                NotificationManager.IMPORTANCE_HIGH);
        createNotificationChannel(context, ALARM_CHANNEL,
                context.getString(R.string.alarm_channel_name),
                NotificationManager.IMPORTANCE_HIGH);
    }
}
+30 −14
Original line number Diff line number Diff line
@@ -86,7 +86,10 @@ final class AlarmNotifications {
            AlarmInstance instance) {
        LogUtils.v("Displaying low priority notification for alarm instance: " + instance.mId);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
        Utils.createNotificationChannelsIfNeeded(context);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context,
                    Utils.ALARM_CHANNEL)
                .setShowWhen(false)
                .setContentTitle(context.getString(
                        R.string.alarm_alert_predismiss_title))
@@ -95,7 +98,6 @@ final class AlarmNotifications {
                .setSmallIcon(R.drawable.stat_notify_alarm)
                .setAutoCancel(false)
                .setSortKey(createSortKey(instance))
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setCategory(NotificationCompat.CATEGORY_ALARM)
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                .setLocalOnly(true);
@@ -135,7 +137,10 @@ final class AlarmNotifications {
            AlarmInstance instance) {
        LogUtils.v("Displaying high priority notification for alarm instance: " + instance.mId);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
        Utils.createNotificationChannelsIfNeeded(context);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context,
                    Utils.ALARM_CHANNEL)
                .setShowWhen(false)
                .setContentTitle(context.getString(R.string.alarm_alert_predismiss_title))
                .setContentText(AlarmUtils.getAlarmText(context, instance, true /* includeLabel */))
@@ -143,7 +148,6 @@ final class AlarmNotifications {
                .setSmallIcon(R.drawable.stat_notify_alarm)
                .setAutoCancel(false)
                .setSortKey(createSortKey(instance))
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setCategory(NotificationCompat.CATEGORY_ALARM)
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                .setLocalOnly(true);
@@ -243,14 +247,16 @@ final class AlarmNotifications {
        Notification summary = getActiveGroupSummaryNotification(context, UPCOMING_GROUP_KEY);
        if (summary == null
                || !Objects.equals(summary.contentIntent, firstUpcoming.contentIntent)) {
            summary = new NotificationCompat.Builder(context)
            Utils.createNotificationChannelsIfNeeded(context);

            summary = new NotificationCompat.Builder(context,
                        Utils.ALARM_CHANNEL)
                    .setShowWhen(false)
                    .setContentIntent(firstUpcoming.contentIntent)
                    .setColor(ContextCompat.getColor(context, R.color.default_background))
                    .setSmallIcon(R.drawable.stat_notify_alarm)
                    .setGroup(UPCOMING_GROUP_KEY)
                    .setGroupSummary(true)
                    .setPriority(NotificationCompat.PRIORITY_HIGH)
                    .setCategory(NotificationCompat.CATEGORY_ALARM)
                    .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                    .setLocalOnly(true)
@@ -277,14 +283,16 @@ final class AlarmNotifications {
        Notification summary = getActiveGroupSummaryNotification(context, MISSED_GROUP_KEY);
        if (summary == null
                || !Objects.equals(summary.contentIntent, firstMissed.contentIntent)) {
            summary = new NotificationCompat.Builder(context)
            Utils.createNotificationChannelsIfNeeded(context);

            summary = new NotificationCompat.Builder(context,
                        Utils.ALARM_CHANNEL)
                    .setShowWhen(false)
                    .setContentIntent(firstMissed.contentIntent)
                    .setColor(ContextCompat.getColor(context, R.color.default_background))
                    .setSmallIcon(R.drawable.stat_notify_alarm)
                    .setGroup(MISSED_GROUP_KEY)
                    .setGroupSummary(true)
                    .setPriority(NotificationCompat.PRIORITY_HIGH)
                    .setCategory(NotificationCompat.CATEGORY_ALARM)
                    .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                    .setLocalOnly(true)
@@ -297,7 +305,10 @@ final class AlarmNotifications {
            AlarmInstance instance) {
        LogUtils.v("Displaying snoozed notification for alarm instance: " + instance.mId);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
        Utils.createNotificationChannelsIfNeeded(context);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context,
                    Utils.ALARM_CHANNEL)
                .setShowWhen(false)
                .setContentTitle(instance.getLabelOrDefault(context))
                .setContentText(context.getString(R.string.alarm_alert_snooze_until,
@@ -306,7 +317,6 @@ final class AlarmNotifications {
                .setSmallIcon(R.drawable.stat_notify_alarm)
                .setAutoCancel(false)
                .setSortKey(createSortKey(instance))
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .setCategory(NotificationCompat.CATEGORY_ALARM)
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                .setLocalOnly(true);
@@ -341,7 +351,11 @@ final class AlarmNotifications {

        String label = instance.mLabel;
        String alarmTime = AlarmUtils.getFormattedTime(context, instance.getAlarmTime());
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)

        Utils.createNotificationChannelsIfNeeded(context);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context,
                    Utils.ALARM_CHANNEL)
                .setShowWhen(false)
                .setContentTitle(context.getString(R.string.alarm_missed_title))
                .setContentText(instance.mLabel.isEmpty() ? alarmTime :
@@ -349,7 +363,6 @@ final class AlarmNotifications {
                .setColor(ContextCompat.getColor(context, R.color.default_background))
                .setSortKey(createSortKey(instance))
                .setSmallIcon(R.drawable.stat_notify_alarm)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setCategory(NotificationCompat.CATEGORY_ALARM)
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                .setLocalOnly(true);
@@ -384,7 +397,11 @@ final class AlarmNotifications {
        LogUtils.v("Displaying alarm notification for alarm instance: " + instance.mId);

        Resources resources = service.getResources();
        NotificationCompat.Builder notification = new NotificationCompat.Builder(service)

        Utils.createNotificationChannelsIfNeeded(service);

        NotificationCompat.Builder notification = new NotificationCompat.Builder(service,
                    Utils.ALARM_CHANNEL)
                .setContentTitle(instance.getLabelOrDefault(service))
                .setContentText(AlarmUtils.getFormattedTime(service, instance.getAlarmTime()))
                .setColor(ContextCompat.getColor(service, R.color.default_background))
@@ -432,7 +449,6 @@ final class AlarmNotifications {
        notification.setFullScreenIntent(PendingIntent.getActivity(service,
                ALARM_FIRING_NOTIFICATION_ID, fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT),
                true);
        notification.setPriority(NotificationCompat.PRIORITY_MAX);

        clearNotification(service, instance);
        service.startForeground(ALARM_FIRING_NOTIFICATION_ID, notification.build());
+5 −3
Original line number Diff line number Diff line
@@ -26,8 +26,8 @@ import android.support.annotation.DrawableRes;
import android.support.annotation.StringRes;
import android.support.v4.app.NotificationCompat.Action;
import android.support.v4.app.NotificationCompat.Builder;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.NotificationCompat;
import android.widget.RemoteViews;

import com.android.deskclock.R;
@@ -127,13 +127,15 @@ class StopwatchNotificationBuilder {
            content.setViewVisibility(R.id.state, VISIBLE);
        }

        final Builder notification = new NotificationCompat.Builder(context)
        Utils.createNotificationChannelsIfNeeded(context);

        final Builder notification = new NotificationCompat.Builder(context,
                    Utils.STOPWATCH_CHANNEL)
                .setLocalOnly(true)
                .setOngoing(running)
                .setCustomContentView(content)
                .setContentIntent(pendingShowApp)
                .setAutoCancel(stopwatch.isPaused())
                .setPriority(Notification.PRIORITY_MAX)
                .setSmallIcon(R.drawable.stat_notify_stopwatch)
                .setStyle(new NotificationCompat.DecoratedCustomViewStyle())
                .setColor(ContextCompat.getColor(context, R.color.default_background));
+13 −7
Original line number Diff line number Diff line
@@ -26,8 +26,8 @@ import android.content.res.Resources;
import android.os.Build;
import android.os.SystemClock;
import android.support.annotation.DrawableRes;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.NotificationCompat;
import android.text.TextUtils;
import android.widget.RemoteViews;

@@ -148,13 +148,15 @@ class TimerNotificationBuilder {
                PendingIntent.getService(context, REQUEST_CODE_UPCOMING, showApp,
                        PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT);

        final Builder notification = new NotificationCompat.Builder(context)
        Utils.createNotificationChannelsIfNeeded(context);

        final Builder notification = new NotificationCompat.Builder(context,
                    Utils.TIMER_CHANNEL)
                .setOngoing(true)
                .setLocalOnly(true)
                .setShowWhen(false)
                .setAutoCancel(false)
                .setContentIntent(pendingShowApp)
                .setPriority(Notification.PRIORITY_HIGH)
                .setCategory(NotificationCompat.CATEGORY_ALARM)
                .setSmallIcon(R.drawable.stat_notify_timer)
                .setSortKey(nm.getTimerNotificationSortKey())
@@ -261,13 +263,15 @@ class TimerNotificationBuilder {
                .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_USER_ACTION);
        final PendingIntent pendingFullScreen = Utils.pendingActivityIntent(context, fullScreen);

        final Builder notification = new NotificationCompat.Builder(context)
        Utils.createNotificationChannelsIfNeeded(context);

        final Builder notification = new NotificationCompat.Builder(context,
                    Utils.TIMER_CHANNEL)
                .setOngoing(true)
                .setLocalOnly(true)
                .setShowWhen(false)
                .setAutoCancel(false)
                .setContentIntent(contentIntent)
                .setPriority(Notification.PRIORITY_MAX)
                .setDefaults(Notification.DEFAULT_LIGHTS)
                .setSmallIcon(R.drawable.stat_notify_timer)
                .setFullScreenIntent(pendingFullScreen, true)
@@ -344,12 +348,14 @@ class TimerNotificationBuilder {
                PendingIntent.getService(context, REQUEST_CODE_MISSING, showApp,
                        PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT);

        final Builder notification = new NotificationCompat.Builder(context)
        Utils.createNotificationChannelsIfNeeded(context);

        final Builder notification = new NotificationCompat.Builder(context,
                    Utils.TIMER_CHANNEL)
                .setLocalOnly(true)
                .setShowWhen(false)
                .setAutoCancel(false)
                .setContentIntent(pendingShowApp)
                .setPriority(Notification.PRIORITY_HIGH)
                .setCategory(NotificationCompat.CATEGORY_ALARM)
                .setSmallIcon(R.drawable.stat_notify_timer)
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)