Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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 | * http://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.android.systemui.media |
| 18 | |
| 19 | import android.app.Notification |
Beth Thibodeau | f55bc6a | 2020-05-20 02:01:31 -0400 | [diff] [blame] | 20 | import android.app.PendingIntent |
Beth Thibodeau | 6a4fbe3 | 2020-06-09 01:04:41 -0400 | [diff] [blame] | 21 | import android.content.BroadcastReceiver |
Selim Cinek | c543671 | 2020-04-27 15:15:44 -0700 | [diff] [blame] | 22 | import android.content.ContentResolver |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 23 | import android.content.Context |
Beth Thibodeau | 6a4fbe3 | 2020-06-09 01:04:41 -0400 | [diff] [blame] | 24 | import android.content.Intent |
| 25 | import android.content.IntentFilter |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 26 | import android.graphics.Bitmap |
Selim Cinek | c543671 | 2020-04-27 15:15:44 -0700 | [diff] [blame] | 27 | import android.graphics.Canvas |
Lucas Dupin | 96bdbee | 2020-05-14 15:51:32 -0700 | [diff] [blame] | 28 | import android.graphics.Color |
Selim Cinek | c543671 | 2020-04-27 15:15:44 -0700 | [diff] [blame] | 29 | import android.graphics.ImageDecoder |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 30 | import android.graphics.drawable.Drawable |
Selim Cinek | c543671 | 2020-04-27 15:15:44 -0700 | [diff] [blame] | 31 | import android.graphics.drawable.Icon |
Beth Thibodeau | f55bc6a | 2020-05-20 02:01:31 -0400 | [diff] [blame] | 32 | import android.media.MediaDescription |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 33 | import android.media.MediaMetadata |
| 34 | import android.media.session.MediaSession |
Selim Cinek | c543671 | 2020-04-27 15:15:44 -0700 | [diff] [blame] | 35 | import android.net.Uri |
Beth Thibodeau | 6a4fbe3 | 2020-06-09 01:04:41 -0400 | [diff] [blame] | 36 | import android.os.UserHandle |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 37 | import android.service.notification.StatusBarNotification |
Selim Cinek | c543671 | 2020-04-27 15:15:44 -0700 | [diff] [blame] | 38 | import android.text.TextUtils |
| 39 | import android.util.Log |
Lucas Dupin | 96bdbee | 2020-05-14 15:51:32 -0700 | [diff] [blame] | 40 | import com.android.internal.graphics.ColorUtils |
Beth Thibodeau | f55bc6a | 2020-05-20 02:01:31 -0400 | [diff] [blame] | 41 | import com.android.systemui.R |
Beth Thibodeau | 6a4fbe3 | 2020-06-09 01:04:41 -0400 | [diff] [blame] | 42 | import com.android.systemui.broadcast.BroadcastDispatcher |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 43 | import com.android.systemui.dagger.qualifiers.Background |
| 44 | import com.android.systemui.dagger.qualifiers.Main |
Beth Thibodeau | f55bc6a | 2020-05-20 02:01:31 -0400 | [diff] [blame] | 45 | import com.android.systemui.statusbar.NotificationMediaManager |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 46 | import com.android.systemui.statusbar.notification.MediaNotificationProcessor |
Lucas Dupin | 6f0bd31 | 2020-05-28 18:19:29 -0700 | [diff] [blame] | 47 | import com.android.systemui.statusbar.notification.NotificationEntryManager |
| 48 | import com.android.systemui.statusbar.notification.NotificationEntryManager.UNDEFINED_DISMISS_REASON |
Selim Cinek | c543671 | 2020-04-27 15:15:44 -0700 | [diff] [blame] | 49 | import com.android.systemui.statusbar.notification.row.HybridGroupManager |
Beth Thibodeau | 8a2af3b3 | 2020-05-26 19:57:42 -0400 | [diff] [blame] | 50 | import com.android.systemui.util.Assert |
Lucas Dupin | 03859d7 | 2020-05-12 12:27:47 -0700 | [diff] [blame] | 51 | import com.android.systemui.util.Utils |
Selim Cinek | c543671 | 2020-04-27 15:15:44 -0700 | [diff] [blame] | 52 | import java.io.IOException |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 53 | import java.util.concurrent.Executor |
| 54 | import javax.inject.Inject |
| 55 | import javax.inject.Singleton |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 56 | |
Selim Cinek | c543671 | 2020-04-27 15:15:44 -0700 | [diff] [blame] | 57 | // URI fields to try loading album art from |
| 58 | private val ART_URIS = arrayOf( |
| 59 | MediaMetadata.METADATA_KEY_ALBUM_ART_URI, |
| 60 | MediaMetadata.METADATA_KEY_ART_URI, |
| 61 | MediaMetadata.METADATA_KEY_DISPLAY_ICON_URI |
| 62 | ) |
| 63 | |
| 64 | private const val TAG = "MediaDataManager" |
Lucas Dupin | 96bdbee | 2020-05-14 15:51:32 -0700 | [diff] [blame] | 65 | private const val DEFAULT_LUMINOSITY = 0.25f |
| 66 | private const val LUMINOSITY_THRESHOLD = 0.05f |
| 67 | private const val SATURATION_MULTIPLIER = 0.8f |
Selim Cinek | c543671 | 2020-04-27 15:15:44 -0700 | [diff] [blame] | 68 | |
Beth Thibodeau | 4580c83 | 2020-05-15 20:11:44 -0400 | [diff] [blame] | 69 | private val LOADING = MediaData(false, 0, null, null, null, null, null, |
Lucas Dupin | 84f5a0e | 2020-06-08 19:55:33 -0700 | [diff] [blame] | 70 | emptyList(), emptyList(), "INVALID", null, null, null, true, null) |
Robert Snoeberger | 70d0d6b | 2020-05-14 16:47:02 -0400 | [diff] [blame] | 71 | |
| 72 | fun isMediaNotification(sbn: StatusBarNotification): Boolean { |
| 73 | if (!sbn.notification.hasMediaSession()) { |
| 74 | return false |
| 75 | } |
| 76 | val notificationStyle = sbn.notification.notificationStyle |
| 77 | if (Notification.DecoratedMediaCustomViewStyle::class.java.equals(notificationStyle) || |
| 78 | Notification.MediaStyle::class.java.equals(notificationStyle)) { |
| 79 | return true |
| 80 | } |
| 81 | return false |
| 82 | } |
Selim Cinek | c543671 | 2020-04-27 15:15:44 -0700 | [diff] [blame] | 83 | |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 84 | /** |
| 85 | * A class that facilitates management and loading of Media Data, ready for binding. |
| 86 | */ |
| 87 | @Singleton |
| 88 | class MediaDataManager @Inject constructor( |
| 89 | private val context: Context, |
| 90 | private val mediaControllerFactory: MediaControllerFactory, |
Lucas Dupin | 6f0bd31 | 2020-05-28 18:19:29 -0700 | [diff] [blame] | 91 | private val notificationEntryManager: NotificationEntryManager, |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 92 | @Background private val backgroundExecutor: Executor, |
Beth Thibodeau | 6a4fbe3 | 2020-06-09 01:04:41 -0400 | [diff] [blame] | 93 | @Main private val foregroundExecutor: Executor, |
Lucas Dupin | 84f5a0e | 2020-06-08 19:55:33 -0700 | [diff] [blame] | 94 | broadcastDispatcher: BroadcastDispatcher, |
| 95 | mediaTimeoutListener: MediaTimeoutListener, |
| 96 | mediaResumeListener: MediaResumeListener |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 97 | ) { |
| 98 | |
Selim Cinek | b52642b | 2020-04-17 14:30:29 -0700 | [diff] [blame] | 99 | private val listeners: MutableSet<Listener> = mutableSetOf() |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 100 | private val mediaEntries: LinkedHashMap<String, MediaData> = LinkedHashMap() |
Beth Thibodeau | f55bc6a | 2020-05-20 02:01:31 -0400 | [diff] [blame] | 101 | private val useMediaResumption: Boolean = Utils.useMediaResumption(context) |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 102 | |
Beth Thibodeau | 6a4fbe3 | 2020-06-09 01:04:41 -0400 | [diff] [blame] | 103 | private val userChangeReceiver = object : BroadcastReceiver() { |
| 104 | override fun onReceive(context: Context, intent: Intent) { |
| 105 | if (Intent.ACTION_USER_SWITCHED == intent.action) { |
| 106 | // Remove all controls, regardless of state |
| 107 | clearData() |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
Beth Thibodeau | 7fee196 | 2020-06-11 23:19:01 -0400 | [diff] [blame] | 112 | private val appChangeReceiver = object : BroadcastReceiver() { |
| 113 | override fun onReceive(context: Context, intent: Intent) { |
| 114 | when (intent.action) { |
| 115 | Intent.ACTION_PACKAGES_SUSPENDED -> { |
| 116 | val packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST) |
| 117 | packages?.forEach { |
| 118 | removeAllForPackage(it) |
| 119 | } |
| 120 | } |
| 121 | Intent.ACTION_PACKAGE_REMOVED, Intent.ACTION_PACKAGE_RESTARTED -> { |
| 122 | intent.data?.encodedSchemeSpecificPart?.let { |
| 123 | removeAllForPackage(it) |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
Lucas Dupin | 6f0bd31 | 2020-05-28 18:19:29 -0700 | [diff] [blame] | 130 | init { |
| 131 | mediaTimeoutListener.timeoutCallback = { token: String, timedOut: Boolean -> |
| 132 | setTimedOut(token, timedOut) } |
| 133 | addListener(mediaTimeoutListener) |
Beth Thibodeau | f55bc6a | 2020-05-20 02:01:31 -0400 | [diff] [blame] | 134 | if (useMediaResumption) { |
| 135 | mediaResumeListener.addTrackToResumeCallback = { desc: MediaDescription, |
| 136 | resumeAction: Runnable, token: MediaSession.Token, appName: String, |
| 137 | appIntent: PendingIntent, packageName: String -> |
| 138 | addResumptionControls(desc, resumeAction, token, appName, appIntent, packageName) |
| 139 | } |
| 140 | mediaResumeListener.resumeComponentFoundCallback = { key: String, action: Runnable? -> |
| 141 | mediaEntries.get(key)?.resumeAction = action |
| 142 | mediaEntries.get(key)?.hasCheckedForResume = true |
| 143 | } |
| 144 | addListener(mediaResumeListener) |
| 145 | } |
Beth Thibodeau | 6a4fbe3 | 2020-06-09 01:04:41 -0400 | [diff] [blame] | 146 | |
| 147 | val userFilter = IntentFilter(Intent.ACTION_USER_SWITCHED) |
| 148 | broadcastDispatcher.registerReceiver(userChangeReceiver, userFilter, null, UserHandle.ALL) |
Beth Thibodeau | 7fee196 | 2020-06-11 23:19:01 -0400 | [diff] [blame] | 149 | |
| 150 | val suspendFilter = IntentFilter(Intent.ACTION_PACKAGES_SUSPENDED) |
| 151 | broadcastDispatcher.registerReceiver(appChangeReceiver, suspendFilter, null, UserHandle.ALL) |
| 152 | |
| 153 | val uninstallFilter = IntentFilter().apply { |
| 154 | addAction(Intent.ACTION_PACKAGE_REMOVED) |
| 155 | addAction(Intent.ACTION_PACKAGE_RESTARTED) |
| 156 | addDataScheme("package") |
| 157 | } |
| 158 | // BroadcastDispatcher does not allow filters with data schemes |
| 159 | context.registerReceiver(appChangeReceiver, uninstallFilter) |
Lucas Dupin | 6f0bd31 | 2020-05-28 18:19:29 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 162 | fun onNotificationAdded(key: String, sbn: StatusBarNotification) { |
Robert Snoeberger | 70d0d6b | 2020-05-14 16:47:02 -0400 | [diff] [blame] | 163 | if (Utils.useQsMediaPlayer(context) && isMediaNotification(sbn)) { |
Beth Thibodeau | 8a2af3b3 | 2020-05-26 19:57:42 -0400 | [diff] [blame] | 164 | Assert.isMainThread() |
Beth Thibodeau | f55bc6a | 2020-05-20 02:01:31 -0400 | [diff] [blame] | 165 | val oldKey = findExistingEntry(key, sbn.packageName) |
| 166 | if (oldKey == null) { |
| 167 | val temp = LOADING.copy(packageName = sbn.packageName) |
| 168 | mediaEntries.put(key, temp) |
| 169 | } else if (oldKey != key) { |
| 170 | // Move to new key |
| 171 | val oldData = mediaEntries.remove(oldKey)!! |
| 172 | mediaEntries.put(key, oldData) |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 173 | } |
Beth Thibodeau | f55bc6a | 2020-05-20 02:01:31 -0400 | [diff] [blame] | 174 | loadMediaData(key, sbn, oldKey) |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 175 | } else { |
| 176 | onNotificationRemoved(key) |
| 177 | } |
| 178 | } |
| 179 | |
Beth Thibodeau | 6a4fbe3 | 2020-06-09 01:04:41 -0400 | [diff] [blame] | 180 | private fun clearData() { |
| 181 | // Called on user change. Remove all current MediaData objects and inform listeners |
| 182 | val listenersCopy = listeners.toSet() |
| 183 | mediaEntries.forEach { |
| 184 | listenersCopy.forEach { listener -> |
| 185 | listener.onMediaDataRemoved(it.key) |
| 186 | } |
| 187 | } |
| 188 | mediaEntries.clear() |
| 189 | } |
| 190 | |
Beth Thibodeau | 7fee196 | 2020-06-11 23:19:01 -0400 | [diff] [blame] | 191 | private fun removeAllForPackage(packageName: String) { |
| 192 | Assert.isMainThread() |
| 193 | val listenersCopy = listeners.toSet() |
| 194 | val toRemove = mediaEntries.filter { it.value.packageName == packageName } |
| 195 | toRemove.forEach { |
| 196 | mediaEntries.remove(it.key) |
| 197 | listenersCopy.forEach { listener -> |
| 198 | listener.onMediaDataRemoved(it.key) |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
Beth Thibodeau | f55bc6a | 2020-05-20 02:01:31 -0400 | [diff] [blame] | 203 | private fun addResumptionControls( |
| 204 | desc: MediaDescription, |
| 205 | action: Runnable, |
| 206 | token: MediaSession.Token, |
| 207 | appName: String, |
| 208 | appIntent: PendingIntent, |
| 209 | packageName: String |
| 210 | ) { |
| 211 | // Resume controls don't have a notification key, so store by package name instead |
| 212 | if (!mediaEntries.containsKey(packageName)) { |
| 213 | val resumeData = LOADING.copy(packageName = packageName, resumeAction = action) |
| 214 | mediaEntries.put(packageName, resumeData) |
| 215 | } |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 216 | backgroundExecutor.execute { |
Lucas Dupin | 84f5a0e | 2020-06-08 19:55:33 -0700 | [diff] [blame] | 217 | loadMediaDataInBgForResumption(desc, action, token, appName, appIntent, packageName) |
Beth Thibodeau | f55bc6a | 2020-05-20 02:01:31 -0400 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Check if there is an existing entry that matches the key or package name. |
| 223 | * Returns the key that matches, or null if not found. |
| 224 | */ |
| 225 | private fun findExistingEntry(key: String, packageName: String): String? { |
| 226 | if (mediaEntries.containsKey(key)) { |
| 227 | return key |
| 228 | } |
| 229 | // Check if we already had a resume player |
| 230 | if (mediaEntries.containsKey(packageName)) { |
| 231 | return packageName |
| 232 | } |
| 233 | return null |
| 234 | } |
| 235 | |
| 236 | private fun loadMediaData( |
| 237 | key: String, |
| 238 | sbn: StatusBarNotification, |
| 239 | oldKey: String? |
| 240 | ) { |
| 241 | backgroundExecutor.execute { |
| 242 | loadMediaDataInBg(key, sbn, oldKey) |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | |
Selim Cinek | b52642b | 2020-04-17 14:30:29 -0700 | [diff] [blame] | 246 | /** |
| 247 | * Add a listener for changes in this class |
| 248 | */ |
| 249 | fun addListener(listener: Listener) = listeners.add(listener) |
| 250 | |
| 251 | /** |
| 252 | * Remove a listener for changes in this class |
| 253 | */ |
| 254 | fun removeListener(listener: Listener) = listeners.remove(listener) |
| 255 | |
Lucas Dupin | 6f0bd31 | 2020-05-28 18:19:29 -0700 | [diff] [blame] | 256 | private fun setTimedOut(token: String, timedOut: Boolean) { |
Lucas Dupin | 6f0bd31 | 2020-05-28 18:19:29 -0700 | [diff] [blame] | 257 | mediaEntries[token]?.let { |
Lucas Dupin | 84f5a0e | 2020-06-08 19:55:33 -0700 | [diff] [blame] | 258 | if (Utils.useMediaResumption(context)) { |
| 259 | if (it.active == !timedOut) { |
| 260 | return |
| 261 | } |
| 262 | it.active = !timedOut |
| 263 | onMediaDataLoaded(token, token, it) |
Lucas Dupin | a73d43f | 2020-06-16 16:44:53 -0700 | [diff] [blame] | 264 | } else if (timedOut) { |
Lucas Dupin | 84f5a0e | 2020-06-08 19:55:33 -0700 | [diff] [blame] | 265 | notificationEntryManager.removeNotification(it.notificationKey, null /* ranking */, |
| 266 | UNDEFINED_DISMISS_REASON) |
| 267 | } |
Lucas Dupin | 6f0bd31 | 2020-05-28 18:19:29 -0700 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | |
Lucas Dupin | 84f5a0e | 2020-06-08 19:55:33 -0700 | [diff] [blame] | 271 | private fun loadMediaDataInBgForResumption( |
Beth Thibodeau | f55bc6a | 2020-05-20 02:01:31 -0400 | [diff] [blame] | 272 | desc: MediaDescription, |
| 273 | resumeAction: Runnable, |
| 274 | token: MediaSession.Token, |
| 275 | appName: String, |
| 276 | appIntent: PendingIntent, |
| 277 | packageName: String |
| 278 | ) { |
Beth Thibodeau | f55bc6a | 2020-05-20 02:01:31 -0400 | [diff] [blame] | 279 | if (TextUtils.isEmpty(desc.title)) { |
| 280 | Log.e(TAG, "Description incomplete") |
| 281 | return |
| 282 | } |
| 283 | |
| 284 | Log.d(TAG, "adding track from browser: $desc") |
| 285 | |
| 286 | // Album art |
| 287 | var artworkBitmap = desc.iconBitmap |
| 288 | if (artworkBitmap == null && desc.iconUri != null) { |
| 289 | artworkBitmap = loadBitmapFromUri(desc.iconUri!!) |
| 290 | } |
| 291 | val artworkIcon = if (artworkBitmap != null) { |
| 292 | Icon.createWithBitmap(artworkBitmap) |
| 293 | } else { |
| 294 | null |
| 295 | } |
Robert Snoeberger | c57e069 | 2020-06-18 14:54:26 -0400 | [diff] [blame^] | 296 | val bgColor = artworkBitmap?.let { computeBackgroundColor(it) } ?: Color.DKGRAY |
Beth Thibodeau | f55bc6a | 2020-05-20 02:01:31 -0400 | [diff] [blame] | 297 | |
| 298 | val mediaAction = getResumeMediaAction(resumeAction) |
| 299 | foregroundExecutor.execute { |
Robert Snoeberger | c57e069 | 2020-06-18 14:54:26 -0400 | [diff] [blame^] | 300 | onMediaDataLoaded(packageName, null, MediaData(true, bgColor, appName, |
Lucas Dupin | 84f5a0e | 2020-06-08 19:55:33 -0700 | [diff] [blame] | 301 | null, desc.subtitle, desc.title, artworkIcon, listOf(mediaAction), listOf(0), |
| 302 | packageName, token, appIntent, device = null, active = false, |
| 303 | resumeAction = resumeAction)) |
Beth Thibodeau | f55bc6a | 2020-05-20 02:01:31 -0400 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | |
| 307 | private fun loadMediaDataInBg( |
| 308 | key: String, |
| 309 | sbn: StatusBarNotification, |
| 310 | oldKey: String? |
| 311 | ) { |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 312 | val token = sbn.notification.extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) |
| 313 | as MediaSession.Token? |
| 314 | val metadata = mediaControllerFactory.create(token).metadata |
| 315 | |
| 316 | if (metadata == null) { |
| 317 | // TODO: handle this better, removing media notification |
| 318 | return |
| 319 | } |
| 320 | |
| 321 | // Foreground and Background colors computed from album art |
| 322 | val notif: Notification = sbn.notification |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 323 | var artworkBitmap = metadata.getBitmap(MediaMetadata.METADATA_KEY_ART) |
| 324 | if (artworkBitmap == null) { |
| 325 | artworkBitmap = metadata.getBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART) |
| 326 | } |
Selim Cinek | c543671 | 2020-04-27 15:15:44 -0700 | [diff] [blame] | 327 | if (artworkBitmap == null) { |
| 328 | artworkBitmap = loadBitmapFromUri(metadata) |
| 329 | } |
| 330 | val artWorkIcon = if (artworkBitmap == null) { |
| 331 | notif.getLargeIcon() |
| 332 | } else { |
| 333 | Icon.createWithBitmap(artworkBitmap) |
| 334 | } |
| 335 | if (artWorkIcon != null) { |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 336 | // If we have art, get colors from that |
Selim Cinek | c543671 | 2020-04-27 15:15:44 -0700 | [diff] [blame] | 337 | if (artworkBitmap == null) { |
Lucas Dupin | 03859d7 | 2020-05-12 12:27:47 -0700 | [diff] [blame] | 338 | if (artWorkIcon.type == Icon.TYPE_BITMAP || |
| 339 | artWorkIcon.type == Icon.TYPE_ADAPTIVE_BITMAP) { |
Selim Cinek | c543671 | 2020-04-27 15:15:44 -0700 | [diff] [blame] | 340 | artworkBitmap = artWorkIcon.bitmap |
| 341 | } else { |
| 342 | val drawable: Drawable = artWorkIcon.loadDrawable(context) |
| 343 | artworkBitmap = Bitmap.createBitmap( |
| 344 | drawable.intrinsicWidth, |
| 345 | drawable.intrinsicHeight, |
| 346 | Bitmap.Config.ARGB_8888) |
| 347 | val canvas = Canvas(artworkBitmap) |
| 348 | drawable.setBounds(0, 0, drawable.intrinsicWidth, drawable.intrinsicHeight) |
| 349 | drawable.draw(canvas) |
| 350 | } |
| 351 | } |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 352 | } |
Robert Snoeberger | c57e069 | 2020-06-18 14:54:26 -0400 | [diff] [blame^] | 353 | val bgColor = computeBackgroundColor(artworkBitmap) |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 354 | |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 355 | // App name |
| 356 | val builder = Notification.Builder.recoverBuilder(context, notif) |
| 357 | val app = builder.loadHeaderAppName() |
| 358 | |
| 359 | // App Icon |
| 360 | val smallIconDrawable: Drawable = sbn.notification.smallIcon.loadDrawable(context) |
| 361 | |
| 362 | // Song name |
Selim Cinek | c543671 | 2020-04-27 15:15:44 -0700 | [diff] [blame] | 363 | var song: CharSequence? = metadata.getString(MediaMetadata.METADATA_KEY_DISPLAY_TITLE) |
| 364 | if (song == null) { |
| 365 | song = metadata.getString(MediaMetadata.METADATA_KEY_TITLE) |
| 366 | } |
| 367 | if (song == null) { |
| 368 | song = HybridGroupManager.resolveTitle(notif) |
| 369 | } |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 370 | |
| 371 | // Artist name |
Selim Cinek | c543671 | 2020-04-27 15:15:44 -0700 | [diff] [blame] | 372 | var artist: CharSequence? = metadata.getString(MediaMetadata.METADATA_KEY_ARTIST) |
| 373 | if (artist == null) { |
| 374 | artist = HybridGroupManager.resolveText(notif) |
| 375 | } |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 376 | |
| 377 | // Control buttons |
| 378 | val actionIcons: MutableList<MediaAction> = ArrayList() |
| 379 | val actions = notif.actions |
| 380 | val actionsToShowCollapsed = notif.extras.getIntArray( |
Beth Thibodeau | f75175f | 2020-05-21 16:52:04 -0400 | [diff] [blame] | 381 | Notification.EXTRA_COMPACT_ACTIONS)?.toMutableList() ?: mutableListOf<Int>() |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 382 | // TODO: b/153736623 look into creating actions when this isn't a media style notification |
| 383 | |
| 384 | val packageContext: Context = sbn.getPackageContext(context) |
Beth Thibodeau | 4580c83 | 2020-05-15 20:11:44 -0400 | [diff] [blame] | 385 | if (actions != null) { |
Beth Thibodeau | f75175f | 2020-05-21 16:52:04 -0400 | [diff] [blame] | 386 | for ((index, action) in actions.withIndex()) { |
| 387 | if (action.getIcon() == null) { |
| 388 | Log.i(TAG, "No icon for action $index ${action.title}") |
| 389 | actionsToShowCollapsed.remove(index) |
| 390 | continue |
| 391 | } |
Beth Thibodeau | f96f4fb | 2020-06-11 19:26:54 -0400 | [diff] [blame] | 392 | val runnable = if (action.actionIntent != null) { |
| 393 | Runnable { |
| 394 | try { |
| 395 | action.actionIntent.send() |
| 396 | } catch (e: PendingIntent.CanceledException) { |
| 397 | Log.d(TAG, "Intent canceled", e) |
| 398 | } |
| 399 | } |
| 400 | } else { |
| 401 | null |
| 402 | } |
Beth Thibodeau | 4580c83 | 2020-05-15 20:11:44 -0400 | [diff] [blame] | 403 | val mediaAction = MediaAction( |
| 404 | action.getIcon().loadDrawable(packageContext), |
Beth Thibodeau | f96f4fb | 2020-06-11 19:26:54 -0400 | [diff] [blame] | 405 | runnable, |
Beth Thibodeau | 4580c83 | 2020-05-15 20:11:44 -0400 | [diff] [blame] | 406 | action.title) |
| 407 | actionIcons.add(mediaAction) |
| 408 | } |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Beth Thibodeau | f55bc6a | 2020-05-20 02:01:31 -0400 | [diff] [blame] | 411 | val resumeAction: Runnable? = mediaEntries.get(key)?.resumeAction |
Lucas Dupin | 5b27cbc | 2020-05-18 10:46:50 -0700 | [diff] [blame] | 412 | foregroundExecutor.execute { |
Beth Thibodeau | f55bc6a | 2020-05-20 02:01:31 -0400 | [diff] [blame] | 413 | onMediaDataLoaded(key, oldKey, MediaData(true, bgColor, app, smallIconDrawable, artist, |
| 414 | song, artWorkIcon, actionIcons, actionsToShowCollapsed, sbn.packageName, token, |
Lucas Dupin | 84f5a0e | 2020-06-08 19:55:33 -0700 | [diff] [blame] | 415 | notif.contentIntent, null, active = true, resumeAction = resumeAction, |
| 416 | notificationKey = key)) |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 417 | } |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Selim Cinek | c543671 | 2020-04-27 15:15:44 -0700 | [diff] [blame] | 420 | /** |
| 421 | * Load a bitmap from the various Art metadata URIs |
| 422 | */ |
| 423 | private fun loadBitmapFromUri(metadata: MediaMetadata): Bitmap? { |
| 424 | for (uri in ART_URIS) { |
| 425 | val uriString = metadata.getString(uri) |
| 426 | if (!TextUtils.isEmpty(uriString)) { |
| 427 | val albumArt = loadBitmapFromUri(Uri.parse(uriString)) |
| 428 | if (albumArt != null) { |
| 429 | Log.d(TAG, "loaded art from $uri") |
Beth Thibodeau | f55bc6a | 2020-05-20 02:01:31 -0400 | [diff] [blame] | 430 | return albumArt |
Selim Cinek | c543671 | 2020-04-27 15:15:44 -0700 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | } |
| 434 | return null |
| 435 | } |
| 436 | |
| 437 | /** |
| 438 | * Load a bitmap from a URI |
| 439 | * @param uri the uri to load |
| 440 | * @return bitmap, or null if couldn't be loaded |
| 441 | */ |
| 442 | private fun loadBitmapFromUri(uri: Uri): Bitmap? { |
| 443 | // ImageDecoder requires a scheme of the following types |
Lucas Dupin | 03859d7 | 2020-05-12 12:27:47 -0700 | [diff] [blame] | 444 | if (uri.scheme == null) { |
| 445 | return null |
Selim Cinek | c543671 | 2020-04-27 15:15:44 -0700 | [diff] [blame] | 446 | } |
| 447 | |
Lucas Dupin | 03859d7 | 2020-05-12 12:27:47 -0700 | [diff] [blame] | 448 | if (!uri.scheme.equals(ContentResolver.SCHEME_CONTENT) && |
| 449 | !uri.scheme.equals(ContentResolver.SCHEME_ANDROID_RESOURCE) && |
| 450 | !uri.scheme.equals(ContentResolver.SCHEME_FILE)) { |
| 451 | return null |
Selim Cinek | c543671 | 2020-04-27 15:15:44 -0700 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | val source = ImageDecoder.createSource(context.getContentResolver(), uri) |
| 455 | return try { |
Beth Thibodeau | f55bc6a | 2020-05-20 02:01:31 -0400 | [diff] [blame] | 456 | ImageDecoder.decodeBitmap(source) { |
| 457 | decoder, info, source -> decoder.isMutableRequired = true |
| 458 | } |
Selim Cinek | c543671 | 2020-04-27 15:15:44 -0700 | [diff] [blame] | 459 | } catch (e: IOException) { |
| 460 | e.printStackTrace() |
| 461 | null |
| 462 | } |
| 463 | } |
| 464 | |
Robert Snoeberger | c57e069 | 2020-06-18 14:54:26 -0400 | [diff] [blame^] | 465 | private fun computeBackgroundColor(artworkBitmap: Bitmap?): Int { |
| 466 | var color = Color.WHITE |
| 467 | if (artworkBitmap != null) { |
| 468 | // If we have art, get colors from that |
| 469 | val p = MediaNotificationProcessor.generateArtworkPaletteBuilder(artworkBitmap) |
| 470 | .generate() |
| 471 | val swatch = MediaNotificationProcessor.findBackgroundSwatch(p) |
| 472 | color = swatch.rgb |
| 473 | } |
| 474 | // Adapt background color, so it's always subdued and text is legible |
| 475 | val tmpHsl = floatArrayOf(0f, 0f, 0f) |
| 476 | ColorUtils.colorToHSL(color, tmpHsl) |
| 477 | |
| 478 | val l = tmpHsl[2] |
| 479 | // Colors with very low luminosity can have any saturation. This means that changing the |
| 480 | // luminosity can make a black become red. Let's remove the saturation of very light or |
| 481 | // very dark colors to avoid this issue. |
| 482 | if (l < LUMINOSITY_THRESHOLD || l > 1f - LUMINOSITY_THRESHOLD) { |
| 483 | tmpHsl[1] = 0f |
| 484 | } |
| 485 | tmpHsl[1] *= SATURATION_MULTIPLIER |
| 486 | tmpHsl[2] = DEFAULT_LUMINOSITY |
| 487 | |
| 488 | color = ColorUtils.HSLToColor(tmpHsl) |
| 489 | return color |
| 490 | } |
| 491 | |
Beth Thibodeau | f55bc6a | 2020-05-20 02:01:31 -0400 | [diff] [blame] | 492 | private fun getResumeMediaAction(action: Runnable): MediaAction { |
| 493 | return MediaAction( |
| 494 | context.getDrawable(R.drawable.lb_ic_play), |
| 495 | action, |
| 496 | context.getString(R.string.controls_media_resume) |
| 497 | ) |
| 498 | } |
| 499 | |
| 500 | fun onMediaDataLoaded(key: String, oldKey: String?, data: MediaData) { |
Beth Thibodeau | 8a2af3b3 | 2020-05-26 19:57:42 -0400 | [diff] [blame] | 501 | Assert.isMainThread() |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 502 | if (mediaEntries.containsKey(key)) { |
| 503 | // Otherwise this was removed already |
| 504 | mediaEntries.put(key, data) |
Beth Thibodeau | 8a2af3b3 | 2020-05-26 19:57:42 -0400 | [diff] [blame] | 505 | val listenersCopy = listeners.toSet() |
| 506 | listenersCopy.forEach { |
Beth Thibodeau | f55bc6a | 2020-05-20 02:01:31 -0400 | [diff] [blame] | 507 | it.onMediaDataLoaded(key, oldKey, data) |
Selim Cinek | b52642b | 2020-04-17 14:30:29 -0700 | [diff] [blame] | 508 | } |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 509 | } |
| 510 | } |
| 511 | |
| 512 | fun onNotificationRemoved(key: String) { |
Beth Thibodeau | 8a2af3b3 | 2020-05-26 19:57:42 -0400 | [diff] [blame] | 513 | Assert.isMainThread() |
Beth Thibodeau | f55bc6a | 2020-05-20 02:01:31 -0400 | [diff] [blame] | 514 | if (useMediaResumption && mediaEntries.get(key)?.resumeAction != null) { |
| 515 | Log.d(TAG, "Not removing $key because resumable") |
| 516 | // Move to resume key aka package name |
| 517 | val data = mediaEntries.remove(key)!! |
| 518 | val resumeAction = getResumeMediaAction(data.resumeAction!!) |
| 519 | val updated = data.copy(token = null, actions = listOf(resumeAction), |
| 520 | actionsToShowInCompact = listOf(0)) |
| 521 | mediaEntries.put(data.packageName, updated) |
| 522 | // Notify listeners of "new" controls |
| 523 | val listenersCopy = listeners.toSet() |
| 524 | listenersCopy.forEach { |
| 525 | it.onMediaDataLoaded(data.packageName, key, updated) |
| 526 | } |
| 527 | return |
| 528 | } |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 529 | val removed = mediaEntries.remove(key) |
| 530 | if (removed != null) { |
Beth Thibodeau | 8a2af3b3 | 2020-05-26 19:57:42 -0400 | [diff] [blame] | 531 | val listenersCopy = listeners.toSet() |
| 532 | listenersCopy.forEach { |
Selim Cinek | b52642b | 2020-04-17 14:30:29 -0700 | [diff] [blame] | 533 | it.onMediaDataRemoved(key) |
| 534 | } |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 535 | } |
| 536 | } |
| 537 | |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 538 | /** |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 539 | * Are there any media notifications active? |
| 540 | */ |
Lucas Dupin | 84f5a0e | 2020-06-08 19:55:33 -0700 | [diff] [blame] | 541 | fun hasActiveMedia() = mediaEntries.any { it.value.active } |
Selim Cinek | b52642b | 2020-04-17 14:30:29 -0700 | [diff] [blame] | 542 | |
Lucas Dupin | 84f5a0e | 2020-06-08 19:55:33 -0700 | [diff] [blame] | 543 | fun isActive(token: MediaSession.Token?): Boolean { |
| 544 | if (token == null) { |
Beth Thibodeau | f55bc6a | 2020-05-20 02:01:31 -0400 | [diff] [blame] | 545 | return false |
| 546 | } |
Lucas Dupin | 84f5a0e | 2020-06-08 19:55:33 -0700 | [diff] [blame] | 547 | val controller = mediaControllerFactory.create(token) |
Beth Thibodeau | f55bc6a | 2020-05-20 02:01:31 -0400 | [diff] [blame] | 548 | val state = controller?.playbackState?.state |
| 549 | return state != null && NotificationMediaManager.isActiveState(state) |
Selim Cinek | b52642b | 2020-04-17 14:30:29 -0700 | [diff] [blame] | 550 | } |
| 551 | |
Beth Thibodeau | f55bc6a | 2020-05-20 02:01:31 -0400 | [diff] [blame] | 552 | /** |
| 553 | * Are there any media entries, including resume controls? |
| 554 | */ |
Lucas Dupin | 84f5a0e | 2020-06-08 19:55:33 -0700 | [diff] [blame] | 555 | fun hasAnyMedia() = if (useMediaResumption) mediaEntries.isNotEmpty() else hasActiveMedia() |
Beth Thibodeau | f55bc6a | 2020-05-20 02:01:31 -0400 | [diff] [blame] | 556 | |
Selim Cinek | b52642b | 2020-04-17 14:30:29 -0700 | [diff] [blame] | 557 | interface Listener { |
| 558 | |
| 559 | /** |
Beth Thibodeau | f55bc6a | 2020-05-20 02:01:31 -0400 | [diff] [blame] | 560 | * Called whenever there's new MediaData Loaded for the consumption in views. |
| 561 | * |
| 562 | * oldKey is provided to check whether the view has changed keys, which can happen when a |
| 563 | * player has gone from resume state (key is package name) to active state (key is |
| 564 | * notification key) or vice versa. |
Selim Cinek | b52642b | 2020-04-17 14:30:29 -0700 | [diff] [blame] | 565 | */ |
Beth Thibodeau | f55bc6a | 2020-05-20 02:01:31 -0400 | [diff] [blame] | 566 | fun onMediaDataLoaded(key: String, oldKey: String?, data: MediaData) {} |
Selim Cinek | b52642b | 2020-04-17 14:30:29 -0700 | [diff] [blame] | 567 | |
| 568 | /** |
| 569 | * Called whenever a previously existing Media notification was removed |
| 570 | */ |
| 571 | fun onMediaDataRemoved(key: String) {} |
| 572 | } |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 573 | } |