|
| 1 | +/* |
| 2 | + * Copyright 2023 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.samples.apps.nowinandroid.core.notifications |
| 18 | + |
| 19 | +import android.Manifest.permission |
| 20 | +import android.app.Notification |
| 21 | +import android.app.NotificationChannel |
| 22 | +import android.app.NotificationManager |
| 23 | +import android.app.PendingIntent |
| 24 | +import android.content.ComponentName |
| 25 | +import android.content.Context |
| 26 | +import android.content.Intent |
| 27 | +import android.content.pm.PackageManager |
| 28 | +import android.os.Build.VERSION |
| 29 | +import android.os.Build.VERSION_CODES |
| 30 | +import androidx.core.app.ActivityCompat |
| 31 | +import androidx.core.app.NotificationCompat |
| 32 | +import androidx.core.app.NotificationCompat.InboxStyle |
| 33 | +import androidx.core.app.NotificationManagerCompat |
| 34 | +import androidx.core.net.toUri |
| 35 | +import com.google.samples.apps.nowinandroid.core.model.data.NewsResource |
| 36 | +import dagger.hilt.android.qualifiers.ApplicationContext |
| 37 | +import javax.inject.Inject |
| 38 | +import javax.inject.Singleton |
| 39 | + |
| 40 | +private const val MAX_NUM_NOTIFICATIONS = 5 |
| 41 | +private const val TARGET_ACTIVITY_NAME = "com.google.samples.apps.nowinandroid.MainActivity" |
| 42 | +private const val NEWS_NOTIFICATION_REQUEST_CODE = 0 |
| 43 | +private const val NEWS_NOTIFICATION_SUMMARY_ID = 1 |
| 44 | +private const val NEWS_NOTIFICATION_CHANNEL_ID = "" |
| 45 | +private const val NEWS_NOTIFICATION_GROUP = "NEWS_NOTIFICATIONS" |
| 46 | +private const val DEEP_LINK_SCHEME_AND_HOST = "https://www.nowinandroid.apps.samples.google.com" |
| 47 | +private const val FOR_YOU_PATH = "foryou" |
| 48 | + |
| 49 | +/** |
| 50 | + * Implementation of [Notifier] that displays notifications in the system tray. |
| 51 | + */ |
| 52 | +@Singleton |
| 53 | +class SystemTrayNotifier @Inject constructor( |
| 54 | + @ApplicationContext private val context: Context, |
| 55 | +) : Notifier { |
| 56 | + |
| 57 | + override fun postNewsNotifications( |
| 58 | + newsResources: List<NewsResource>, |
| 59 | + ) = with(context) { |
| 60 | + if (ActivityCompat.checkSelfPermission( |
| 61 | + this, |
| 62 | + permission.POST_NOTIFICATIONS, |
| 63 | + ) != PackageManager.PERMISSION_GRANTED |
| 64 | + ) { |
| 65 | + return |
| 66 | + } |
| 67 | + |
| 68 | + val truncatedNewsResources = newsResources |
| 69 | + .take(MAX_NUM_NOTIFICATIONS) |
| 70 | + |
| 71 | + val newsNotifications = truncatedNewsResources |
| 72 | + .map { newsResource -> |
| 73 | + createNewsNotification { |
| 74 | + setSmallIcon( |
| 75 | + com.google.samples.apps.nowinandroid.core.common.R.drawable.ic_nia_notification, |
| 76 | + ) |
| 77 | + .setContentTitle(newsResource.title) |
| 78 | + .setContentText(newsResource.content) |
| 79 | + .setContentIntent(newsPendingIntent(newsResource)) |
| 80 | + .setGroup(NEWS_NOTIFICATION_GROUP) |
| 81 | + .setAutoCancel(true) |
| 82 | + } |
| 83 | + } |
| 84 | + val summaryNotification = createNewsNotification { |
| 85 | + val title = getString( |
| 86 | + R.string.news_notification_group_summary, |
| 87 | + truncatedNewsResources.size, |
| 88 | + ) |
| 89 | + setContentTitle(title) |
| 90 | + .setContentText(title) |
| 91 | + .setSmallIcon( |
| 92 | + com.google.samples.apps.nowinandroid.core.common.R.drawable.ic_nia_notification, |
| 93 | + ) |
| 94 | + // Build summary info into InboxStyle template. |
| 95 | + .setStyle(newsNotificationStyle(truncatedNewsResources, title)) |
| 96 | + .setGroup(NEWS_NOTIFICATION_GROUP) |
| 97 | + .setGroupSummary(true) |
| 98 | + .setAutoCancel(true) |
| 99 | + .build() |
| 100 | + } |
| 101 | + |
| 102 | + // Send the notifications |
| 103 | + val notificationManager = NotificationManagerCompat.from(this) |
| 104 | + newsNotifications.forEachIndexed { index, notification -> |
| 105 | + notificationManager.notify( |
| 106 | + truncatedNewsResources[index].id.hashCode(), |
| 107 | + notification, |
| 108 | + ) |
| 109 | + } |
| 110 | + notificationManager.notify(NEWS_NOTIFICATION_SUMMARY_ID, summaryNotification) |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Creates an inbox style summary notification for news updates |
| 115 | + */ |
| 116 | + private fun newsNotificationStyle( |
| 117 | + newsResources: List<NewsResource>, |
| 118 | + title: String, |
| 119 | + ): InboxStyle = newsResources |
| 120 | + .fold(InboxStyle()) { inboxStyle, newsResource -> |
| 121 | + inboxStyle.addLine(newsResource.title) |
| 122 | + } |
| 123 | + .setBigContentTitle(title) |
| 124 | + .setSummaryText(title) |
| 125 | +} |
| 126 | + |
| 127 | +/** |
| 128 | + * Creates a notification for configured for news updates |
| 129 | + */ |
| 130 | +private fun Context.createNewsNotification( |
| 131 | + block: NotificationCompat.Builder.() -> Unit, |
| 132 | +): Notification { |
| 133 | + ensureNotificationChannelExists() |
| 134 | + return NotificationCompat.Builder( |
| 135 | + this, |
| 136 | + NEWS_NOTIFICATION_CHANNEL_ID, |
| 137 | + ) |
| 138 | + .setPriority(NotificationCompat.PRIORITY_DEFAULT) |
| 139 | + .apply(block) |
| 140 | + .build() |
| 141 | +} |
| 142 | + |
| 143 | +/** |
| 144 | + * Ensures the a notification channel is is present if applicable |
| 145 | + */ |
| 146 | +private fun Context.ensureNotificationChannelExists() { |
| 147 | + if (VERSION.SDK_INT < VERSION_CODES.O) return |
| 148 | + |
| 149 | + val channel = NotificationChannel( |
| 150 | + NEWS_NOTIFICATION_CHANNEL_ID, |
| 151 | + getString(R.string.news_notification_channel_name), |
| 152 | + NotificationManager.IMPORTANCE_DEFAULT, |
| 153 | + ).apply { |
| 154 | + description = getString(R.string.news_notification_channel_description) |
| 155 | + } |
| 156 | + // Register the channel with the system |
| 157 | + NotificationManagerCompat.from(this).createNotificationChannel(channel) |
| 158 | +} |
| 159 | + |
| 160 | +private fun Context.newsPendingIntent( |
| 161 | + newsResource: NewsResource, |
| 162 | +): PendingIntent? = PendingIntent.getActivity( |
| 163 | + this, |
| 164 | + NEWS_NOTIFICATION_REQUEST_CODE, |
| 165 | + Intent().apply { |
| 166 | + action = Intent.ACTION_VIEW |
| 167 | + data = newsResource.newsDeepLinkUri() |
| 168 | + component = ComponentName( |
| 169 | + packageName, |
| 170 | + TARGET_ACTIVITY_NAME, |
| 171 | + ) |
| 172 | + }, |
| 173 | + PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE, |
| 174 | +) |
| 175 | + |
| 176 | +private fun NewsResource.newsDeepLinkUri() = "$DEEP_LINK_SCHEME_AND_HOST/$FOR_YOU_PATH/$id".toUri() |
0 commit comments