diff options
author | Demon000 <demonsingur@gmail.com> | 2017-09-15 00:18:03 +0300 |
---|---|---|
committer | Rashed Abdel-Tawab <rashed@linux.com> | 2017-10-28 13:55:56 -0700 |
commit | 9807159e6a098394561b1f1f6b65885c067ea4fa (patch) | |
tree | 37c14973c750695946a197c8d467e6c25ef1f20a | |
parent | 3b47a2095b640e3b15b2df6e058ca5c80462f7a2 (diff) | |
download | android_packages_apps_DeskClock-9807159e6a098394561b1f1f6b65885c067ea4fa.tar.gz android_packages_apps_DeskClock-9807159e6a098394561b1f1f6b65885c067ea4fa.tar.bz2 android_packages_apps_DeskClock-9807159e6a098394561b1f1f6b65885c067ea4fa.zip |
DeskClock: move to NotificationChannels
Change-Id: I39a05acb0290578c9f794896dcbe25537a2afe51
-rw-r--r-- | res/values/strings.xml | 6 | ||||
-rw-r--r-- | src/com/android/deskclock/Utils.java | 42 | ||||
-rw-r--r-- | src/com/android/deskclock/alarms/AlarmNotifications.java | 44 | ||||
-rw-r--r-- | src/com/android/deskclock/data/StopwatchNotificationBuilder.java | 8 | ||||
-rw-r--r-- | src/com/android/deskclock/data/TimerNotificationBuilder.java | 20 |
5 files changed, 95 insertions, 25 deletions
diff --git a/res/values/strings.xml b/res/values/strings.xml index deccf926b..86686f8f1 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -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> diff --git a/src/com/android/deskclock/Utils.java b/src/com/android/deskclock/Utils.java index 6f09a4398..fb31bfd4f 100644 --- a/src/com/android/deskclock/Utils.java +++ b/src/com/android/deskclock/Utils.java @@ -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)); } } -}
\ No newline at end of file + + 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); + } +} diff --git a/src/com/android/deskclock/alarms/AlarmNotifications.java b/src/com/android/deskclock/alarms/AlarmNotifications.java index df711d23a..74ad7e6bf 100644 --- a/src/com/android/deskclock/alarms/AlarmNotifications.java +++ b/src/com/android/deskclock/alarms/AlarmNotifications.java @@ -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()); diff --git a/src/com/android/deskclock/data/StopwatchNotificationBuilder.java b/src/com/android/deskclock/data/StopwatchNotificationBuilder.java index 91de2ba24..56b6b6a06 100644 --- a/src/com/android/deskclock/data/StopwatchNotificationBuilder.java +++ b/src/com/android/deskclock/data/StopwatchNotificationBuilder.java @@ -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)); diff --git a/src/com/android/deskclock/data/TimerNotificationBuilder.java b/src/com/android/deskclock/data/TimerNotificationBuilder.java index 445485702..c98fcdec1 100644 --- a/src/com/android/deskclock/data/TimerNotificationBuilder.java +++ b/src/com/android/deskclock/data/TimerNotificationBuilder.java @@ -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) |