diff options
| author | 2020-06-09 01:04:41 -0400 | |
|---|---|---|
| committer | 2020-06-11 19:08:04 -0400 | |
| commit | 6a4fbe3cb7a14e16717ba33e636f13b7e743aa69 (patch) | |
| tree | 90f763e1cba2002bf82bd689d98dca99e4b56c66 | |
| parent | 19dd14d76a486f92813274ef2720c31801aa32ee (diff) | |
Reset media controls when user changes
Fixes: 153170118
Test: manual
Test: atest com.android.systemui.media
Change-Id: I762ef0f830acdd0526f42a604c977ebf395c41ba
| -rw-r--r-- | packages/SystemUI/src/com/android/systemui/media/MediaDataManager.kt | 31 | ||||
| -rw-r--r-- | packages/SystemUI/src/com/android/systemui/media/MediaResumeListener.kt | 27 |
2 files changed, 47 insertions, 11 deletions
diff --git a/packages/SystemUI/src/com/android/systemui/media/MediaDataManager.kt b/packages/SystemUI/src/com/android/systemui/media/MediaDataManager.kt index 094c5bef3c18..6c3ad23603b5 100644 --- a/packages/SystemUI/src/com/android/systemui/media/MediaDataManager.kt +++ b/packages/SystemUI/src/com/android/systemui/media/MediaDataManager.kt @@ -18,8 +18,11 @@ package com.android.systemui.media import android.app.Notification import android.app.PendingIntent +import android.content.BroadcastReceiver import android.content.ContentResolver import android.content.Context +import android.content.Intent +import android.content.IntentFilter import android.graphics.Bitmap import android.graphics.Canvas import android.graphics.Color @@ -30,11 +33,13 @@ import android.media.MediaDescription import android.media.MediaMetadata import android.media.session.MediaSession import android.net.Uri +import android.os.UserHandle import android.service.notification.StatusBarNotification import android.text.TextUtils import android.util.Log import com.android.internal.graphics.ColorUtils import com.android.systemui.R +import com.android.systemui.broadcast.BroadcastDispatcher import com.android.systemui.dagger.qualifiers.Background import com.android.systemui.dagger.qualifiers.Main import com.android.systemui.statusbar.NotificationMediaManager @@ -87,13 +92,23 @@ class MediaDataManager @Inject constructor( private val notificationEntryManager: NotificationEntryManager, private val mediaResumeListener: MediaResumeListener, @Background private val backgroundExecutor: Executor, - @Main private val foregroundExecutor: Executor + @Main private val foregroundExecutor: Executor, + private val broadcastDispatcher: BroadcastDispatcher ) { private val listeners: MutableSet<Listener> = mutableSetOf() private val mediaEntries: LinkedHashMap<String, MediaData> = LinkedHashMap() private val useMediaResumption: Boolean = Utils.useMediaResumption(context) + private val userChangeReceiver = object : BroadcastReceiver() { + override fun onReceive(context: Context, intent: Intent) { + if (Intent.ACTION_USER_SWITCHED == intent.action) { + // Remove all controls, regardless of state + clearData() + } + } + } + init { mediaTimeoutListener.timeoutCallback = { token: String, timedOut: Boolean -> setTimedOut(token, timedOut) } @@ -111,6 +126,9 @@ class MediaDataManager @Inject constructor( } addListener(mediaResumeListener) } + + val userFilter = IntentFilter(Intent.ACTION_USER_SWITCHED) + broadcastDispatcher.registerReceiver(userChangeReceiver, userFilter, null, UserHandle.ALL) } fun onNotificationAdded(key: String, sbn: StatusBarNotification) { @@ -131,6 +149,17 @@ class MediaDataManager @Inject constructor( } } + private fun clearData() { + // Called on user change. Remove all current MediaData objects and inform listeners + val listenersCopy = listeners.toSet() + mediaEntries.forEach { + listenersCopy.forEach { listener -> + listener.onMediaDataRemoved(it.key) + } + } + mediaEntries.clear() + } + private fun addResumptionControls( desc: MediaDescription, action: Runnable, diff --git a/packages/SystemUI/src/com/android/systemui/media/MediaResumeListener.kt b/packages/SystemUI/src/com/android/systemui/media/MediaResumeListener.kt index 6bbe0d1651dd..e8a4b1e46fec 100644 --- a/packages/SystemUI/src/com/android/systemui/media/MediaResumeListener.kt +++ b/packages/SystemUI/src/com/android/systemui/media/MediaResumeListener.kt @@ -40,7 +40,7 @@ import javax.inject.Singleton private const val TAG = "MediaResumeListener" private const val MEDIA_PREFERENCES = "media_control_prefs" -private const val MEDIA_PREFERENCE_KEY = "browser_components" +private const val MEDIA_PREFERENCE_KEY = "browser_components_" @Singleton class MediaResumeListener @Inject constructor( @@ -63,11 +63,16 @@ class MediaResumeListener @Inject constructor( lateinit var resumeComponentFoundCallback: (String, Runnable?) -> Unit private var mediaBrowser: ResumeMediaBrowser? = null + private var currentUserId: Int - private val unlockReceiver = object : BroadcastReceiver() { + private val userChangeReceiver = object : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { if (Intent.ACTION_USER_UNLOCKED == intent.action) { loadMediaResumptionControls() + } else if (Intent.ACTION_USER_SWITCHED == intent.action) { + currentUserId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1) + loadSavedComponents() + loadMediaResumptionControls() } } } @@ -97,18 +102,22 @@ class MediaResumeListener @Inject constructor( } init { + currentUserId = context.userId if (useMediaResumption) { val unlockFilter = IntentFilter() unlockFilter.addAction(Intent.ACTION_USER_UNLOCKED) - broadcastDispatcher.registerReceiver(unlockReceiver, unlockFilter, null, UserHandle.ALL) + unlockFilter.addAction(Intent.ACTION_USER_SWITCHED) + broadcastDispatcher.registerReceiver(userChangeReceiver, unlockFilter, null, + UserHandle.ALL) loadSavedComponents() } } private fun loadSavedComponents() { - val userContext = context.createContextAsUser(context.getUser(), 0) - val prefs = userContext.getSharedPreferences(MEDIA_PREFERENCES, Context.MODE_PRIVATE) - val listString = prefs.getString(MEDIA_PREFERENCE_KEY, null) + // Make sure list is empty (if we switched users) + resumeComponents.clear() + val prefs = context.getSharedPreferences(MEDIA_PREFERENCES, Context.MODE_PRIVATE) + val listString = prefs.getString(MEDIA_PREFERENCE_KEY + currentUserId, null) val components = listString?.split(ResumeMediaBrowser.DELIMITER.toRegex()) ?.dropLastWhile { it.isEmpty() } components?.forEach { @@ -133,7 +142,6 @@ class MediaResumeListener @Inject constructor( val browser = ResumeMediaBrowser(context, mediaBrowserCallback, it) browser.findRecentMedia() } - broadcastDispatcher.unregisterReceiver(unlockReceiver) // only need to load once } override fun onMediaDataLoaded(key: String, oldKey: String?, data: MediaData) { @@ -212,9 +220,8 @@ class MediaResumeListener @Inject constructor( sb.append(it.flattenToString()) sb.append(ResumeMediaBrowser.DELIMITER) } - val userContext = context.createContextAsUser(context.getUser(), 0) - val prefs = userContext.getSharedPreferences(MEDIA_PREFERENCES, Context.MODE_PRIVATE) - prefs.edit().putString(MEDIA_PREFERENCE_KEY, sb.toString()).apply() + val prefs = context.getSharedPreferences(MEDIA_PREFERENCES, Context.MODE_PRIVATE) + prefs.edit().putString(MEDIA_PREFERENCE_KEY + currentUserId, sb.toString()).apply() } /** |