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 |
Selim Cinek | c543671 | 2020-04-27 15:15:44 -0700 | [diff] [blame] | 20 | import android.content.ContentResolver |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 21 | import android.content.Context |
| 22 | import android.graphics.Bitmap |
Selim Cinek | c543671 | 2020-04-27 15:15:44 -0700 | [diff] [blame] | 23 | import android.graphics.Canvas |
| 24 | import android.graphics.ImageDecoder |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 25 | import android.graphics.drawable.Drawable |
Selim Cinek | c543671 | 2020-04-27 15:15:44 -0700 | [diff] [blame] | 26 | import android.graphics.drawable.Icon |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 27 | import android.media.MediaMetadata |
| 28 | import android.media.session.MediaSession |
Selim Cinek | c543671 | 2020-04-27 15:15:44 -0700 | [diff] [blame] | 29 | import android.net.Uri |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 30 | import android.service.notification.StatusBarNotification |
Selim Cinek | c543671 | 2020-04-27 15:15:44 -0700 | [diff] [blame] | 31 | import android.text.TextUtils |
| 32 | import android.util.Log |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 33 | import com.android.internal.util.ContrastColorUtil |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 34 | import com.android.systemui.dagger.qualifiers.Background |
| 35 | import com.android.systemui.dagger.qualifiers.Main |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 36 | import com.android.systemui.statusbar.notification.MediaNotificationProcessor |
Selim Cinek | c543671 | 2020-04-27 15:15:44 -0700 | [diff] [blame] | 37 | import com.android.systemui.statusbar.notification.row.HybridGroupManager |
Lucas Dupin | 03859d7 | 2020-05-12 12:27:47 -0700 | [diff] [blame] | 38 | import com.android.systemui.util.Utils |
Selim Cinek | c543671 | 2020-04-27 15:15:44 -0700 | [diff] [blame] | 39 | import java.io.IOException |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 40 | import java.util.concurrent.Executor |
| 41 | import javax.inject.Inject |
| 42 | import javax.inject.Singleton |
| 43 | import kotlin.collections.LinkedHashMap |
| 44 | |
Selim Cinek | c543671 | 2020-04-27 15:15:44 -0700 | [diff] [blame] | 45 | // URI fields to try loading album art from |
| 46 | private val ART_URIS = arrayOf( |
| 47 | MediaMetadata.METADATA_KEY_ALBUM_ART_URI, |
| 48 | MediaMetadata.METADATA_KEY_ART_URI, |
| 49 | MediaMetadata.METADATA_KEY_DISPLAY_ICON_URI |
| 50 | ) |
| 51 | |
| 52 | private const val TAG = "MediaDataManager" |
| 53 | |
| 54 | private val LOADING = MediaData(false, 0, 0, null, null, null, null, null, |
| 55 | emptyList(), emptyList(), null, null, null) |
| 56 | |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 57 | /** |
| 58 | * A class that facilitates management and loading of Media Data, ready for binding. |
| 59 | */ |
| 60 | @Singleton |
| 61 | class MediaDataManager @Inject constructor( |
| 62 | private val context: Context, |
| 63 | private val mediaControllerFactory: MediaControllerFactory, |
| 64 | @Background private val backgroundExecutor: Executor, |
| 65 | @Main private val foregroundExcecutor: Executor |
| 66 | ) { |
| 67 | |
Selim Cinek | b52642b | 2020-04-17 14:30:29 -0700 | [diff] [blame] | 68 | private val listeners: MutableSet<Listener> = mutableSetOf() |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 69 | private val mediaEntries: LinkedHashMap<String, MediaData> = LinkedHashMap() |
| 70 | |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 71 | fun onNotificationAdded(key: String, sbn: StatusBarNotification) { |
| 72 | if (isMediaNotification(sbn)) { |
| 73 | if (!mediaEntries.containsKey(key)) { |
| 74 | mediaEntries.put(key, LOADING) |
| 75 | } |
| 76 | loadMediaData(key, sbn) |
| 77 | } else { |
| 78 | onNotificationRemoved(key) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | private fun loadMediaData(key: String, sbn: StatusBarNotification) { |
| 83 | backgroundExecutor.execute { |
| 84 | loadMediaDataInBg(key, sbn) |
| 85 | } |
| 86 | } |
| 87 | |
Selim Cinek | b52642b | 2020-04-17 14:30:29 -0700 | [diff] [blame] | 88 | /** |
| 89 | * Add a listener for changes in this class |
| 90 | */ |
| 91 | fun addListener(listener: Listener) = listeners.add(listener) |
| 92 | |
| 93 | /** |
| 94 | * Remove a listener for changes in this class |
| 95 | */ |
| 96 | fun removeListener(listener: Listener) = listeners.remove(listener) |
| 97 | |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 98 | private fun loadMediaDataInBg(key: String, sbn: StatusBarNotification) { |
| 99 | val token = sbn.notification.extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) |
| 100 | as MediaSession.Token? |
| 101 | val metadata = mediaControllerFactory.create(token).metadata |
| 102 | |
| 103 | if (metadata == null) { |
| 104 | // TODO: handle this better, removing media notification |
| 105 | return |
| 106 | } |
| 107 | |
| 108 | // Foreground and Background colors computed from album art |
| 109 | val notif: Notification = sbn.notification |
| 110 | var fgColor = notif.color |
| 111 | var bgColor = -1 |
| 112 | var artworkBitmap = metadata.getBitmap(MediaMetadata.METADATA_KEY_ART) |
| 113 | if (artworkBitmap == null) { |
| 114 | artworkBitmap = metadata.getBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART) |
| 115 | } |
Selim Cinek | c543671 | 2020-04-27 15:15:44 -0700 | [diff] [blame] | 116 | if (artworkBitmap == null) { |
| 117 | artworkBitmap = loadBitmapFromUri(metadata) |
| 118 | } |
| 119 | val artWorkIcon = if (artworkBitmap == null) { |
| 120 | notif.getLargeIcon() |
| 121 | } else { |
| 122 | Icon.createWithBitmap(artworkBitmap) |
| 123 | } |
| 124 | if (artWorkIcon != null) { |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 125 | // If we have art, get colors from that |
Selim Cinek | c543671 | 2020-04-27 15:15:44 -0700 | [diff] [blame] | 126 | if (artworkBitmap == null) { |
Lucas Dupin | 03859d7 | 2020-05-12 12:27:47 -0700 | [diff] [blame] | 127 | if (artWorkIcon.type == Icon.TYPE_BITMAP || |
| 128 | artWorkIcon.type == Icon.TYPE_ADAPTIVE_BITMAP) { |
Selim Cinek | c543671 | 2020-04-27 15:15:44 -0700 | [diff] [blame] | 129 | artworkBitmap = artWorkIcon.bitmap |
| 130 | } else { |
| 131 | val drawable: Drawable = artWorkIcon.loadDrawable(context) |
| 132 | artworkBitmap = Bitmap.createBitmap( |
| 133 | drawable.intrinsicWidth, |
| 134 | drawable.intrinsicHeight, |
| 135 | Bitmap.Config.ARGB_8888) |
| 136 | val canvas = Canvas(artworkBitmap) |
| 137 | drawable.setBounds(0, 0, drawable.intrinsicWidth, drawable.intrinsicHeight) |
| 138 | drawable.draw(canvas) |
| 139 | } |
| 140 | } |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 141 | val p = MediaNotificationProcessor.generateArtworkPaletteBuilder(artworkBitmap) |
| 142 | .generate() |
| 143 | val swatch = MediaNotificationProcessor.findBackgroundSwatch(p) |
| 144 | bgColor = swatch.rgb |
| 145 | fgColor = MediaNotificationProcessor.selectForegroundColor(bgColor, p) |
| 146 | } |
| 147 | // Make sure colors will be legible |
| 148 | val isDark = !ContrastColorUtil.isColorLight(bgColor) |
| 149 | fgColor = ContrastColorUtil.resolveContrastColor(context, fgColor, bgColor, |
| 150 | isDark) |
| 151 | fgColor = ContrastColorUtil.ensureTextContrast(fgColor, bgColor, isDark) |
| 152 | |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 153 | // App name |
| 154 | val builder = Notification.Builder.recoverBuilder(context, notif) |
| 155 | val app = builder.loadHeaderAppName() |
| 156 | |
| 157 | // App Icon |
| 158 | val smallIconDrawable: Drawable = sbn.notification.smallIcon.loadDrawable(context) |
| 159 | |
| 160 | // Song name |
Selim Cinek | c543671 | 2020-04-27 15:15:44 -0700 | [diff] [blame] | 161 | var song: CharSequence? = metadata.getString(MediaMetadata.METADATA_KEY_DISPLAY_TITLE) |
| 162 | if (song == null) { |
| 163 | song = metadata.getString(MediaMetadata.METADATA_KEY_TITLE) |
| 164 | } |
| 165 | if (song == null) { |
| 166 | song = HybridGroupManager.resolveTitle(notif) |
| 167 | } |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 168 | |
| 169 | // Artist name |
Selim Cinek | c543671 | 2020-04-27 15:15:44 -0700 | [diff] [blame] | 170 | var artist: CharSequence? = metadata.getString(MediaMetadata.METADATA_KEY_ARTIST) |
| 171 | if (artist == null) { |
| 172 | artist = HybridGroupManager.resolveText(notif) |
| 173 | } |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 174 | |
| 175 | // Control buttons |
| 176 | val actionIcons: MutableList<MediaAction> = ArrayList() |
| 177 | val actions = notif.actions |
| 178 | val actionsToShowCollapsed = notif.extras.getIntArray( |
| 179 | Notification.EXTRA_COMPACT_ACTIONS)?.toList() ?: emptyList() |
| 180 | // TODO: b/153736623 look into creating actions when this isn't a media style notification |
| 181 | |
| 182 | val packageContext: Context = sbn.getPackageContext(context) |
| 183 | for (action in actions) { |
| 184 | val mediaAction = MediaAction( |
| 185 | action.getIcon().loadDrawable(packageContext), |
| 186 | action.actionIntent, |
| 187 | action.title) |
| 188 | actionIcons.add(mediaAction) |
| 189 | } |
| 190 | |
| 191 | foregroundExcecutor.execute { |
| 192 | onMediaDataLoaded(key, MediaData(true, fgColor, bgColor, app, smallIconDrawable, artist, |
Selim Cinek | c543671 | 2020-04-27 15:15:44 -0700 | [diff] [blame] | 193 | song, artWorkIcon, actionIcons, actionsToShowCollapsed, sbn.packageName, token, |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 194 | notif.contentIntent)) |
| 195 | } |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Selim Cinek | c543671 | 2020-04-27 15:15:44 -0700 | [diff] [blame] | 198 | /** |
| 199 | * Load a bitmap from the various Art metadata URIs |
| 200 | */ |
| 201 | private fun loadBitmapFromUri(metadata: MediaMetadata): Bitmap? { |
| 202 | for (uri in ART_URIS) { |
| 203 | val uriString = metadata.getString(uri) |
| 204 | if (!TextUtils.isEmpty(uriString)) { |
| 205 | val albumArt = loadBitmapFromUri(Uri.parse(uriString)) |
| 206 | if (albumArt != null) { |
| 207 | Log.d(TAG, "loaded art from $uri") |
| 208 | break |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | return null |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Load a bitmap from a URI |
| 217 | * @param uri the uri to load |
| 218 | * @return bitmap, or null if couldn't be loaded |
| 219 | */ |
| 220 | private fun loadBitmapFromUri(uri: Uri): Bitmap? { |
| 221 | // ImageDecoder requires a scheme of the following types |
Lucas Dupin | 03859d7 | 2020-05-12 12:27:47 -0700 | [diff] [blame] | 222 | if (uri.scheme == null) { |
| 223 | return null |
Selim Cinek | c543671 | 2020-04-27 15:15:44 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Lucas Dupin | 03859d7 | 2020-05-12 12:27:47 -0700 | [diff] [blame] | 226 | if (!uri.scheme.equals(ContentResolver.SCHEME_CONTENT) && |
| 227 | !uri.scheme.equals(ContentResolver.SCHEME_ANDROID_RESOURCE) && |
| 228 | !uri.scheme.equals(ContentResolver.SCHEME_FILE)) { |
| 229 | return null |
Selim Cinek | c543671 | 2020-04-27 15:15:44 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | val source = ImageDecoder.createSource(context.getContentResolver(), uri) |
| 233 | return try { |
| 234 | ImageDecoder.decodeBitmap(source) |
| 235 | } catch (e: IOException) { |
| 236 | e.printStackTrace() |
| 237 | null |
| 238 | } |
| 239 | } |
| 240 | |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 241 | fun onMediaDataLoaded(key: String, data: MediaData) { |
| 242 | if (mediaEntries.containsKey(key)) { |
| 243 | // Otherwise this was removed already |
| 244 | mediaEntries.put(key, data) |
Selim Cinek | b52642b | 2020-04-17 14:30:29 -0700 | [diff] [blame] | 245 | listeners.forEach { |
| 246 | it.onMediaDataLoaded(key, data) |
| 247 | } |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
| 251 | fun onNotificationRemoved(key: String) { |
| 252 | val removed = mediaEntries.remove(key) |
| 253 | if (removed != null) { |
Selim Cinek | b52642b | 2020-04-17 14:30:29 -0700 | [diff] [blame] | 254 | listeners.forEach { |
| 255 | it.onMediaDataRemoved(key) |
| 256 | } |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | |
Lucas Dupin | 03859d7 | 2020-05-12 12:27:47 -0700 | [diff] [blame] | 260 | private fun isMediaNotification(sbn: StatusBarNotification): Boolean { |
| 261 | if (!Utils.useQsMediaPlayer(context)) { |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 262 | return false |
| 263 | } |
| 264 | if (!sbn.notification.hasMediaSession()) { |
| 265 | return false |
| 266 | } |
| 267 | val notificationStyle = sbn.notification.notificationStyle |
Lucas Dupin | 03859d7 | 2020-05-12 12:27:47 -0700 | [diff] [blame] | 268 | if (Notification.DecoratedMediaCustomViewStyle::class.java.equals(notificationStyle) || |
| 269 | Notification.MediaStyle::class.java.equals(notificationStyle)) { |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 270 | return true |
| 271 | } |
| 272 | return false |
| 273 | } |
| 274 | |
| 275 | /** |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 276 | * Are there any media notifications active? |
| 277 | */ |
| 278 | fun hasActiveMedia() = mediaEntries.size > 0 |
Selim Cinek | b52642b | 2020-04-17 14:30:29 -0700 | [diff] [blame] | 279 | |
| 280 | fun hasAnyMedia(): Boolean { |
| 281 | // TODO: implement this when we implemented resumption |
| 282 | return hasActiveMedia() |
| 283 | } |
| 284 | |
| 285 | interface Listener { |
| 286 | |
| 287 | /** |
| 288 | * Called whenever there's new MediaData Loaded for the consumption in views |
| 289 | */ |
| 290 | fun onMediaDataLoaded(key: String, data: MediaData) {} |
| 291 | |
| 292 | /** |
| 293 | * Called whenever a previously existing Media notification was removed |
| 294 | */ |
| 295 | fun onMediaDataRemoved(key: String) {} |
| 296 | } |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 297 | } |