Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4e86ccd2 authored by Beth Thibodeau's avatar Beth Thibodeau Committed by Android (Google) Code Review
Browse files

Merge "Reset media controls when user changes" into rvc-dev

parents a677e1c6 6a4fbe3c
Loading
Loading
Loading
Loading
+30 −1
Original line number Diff line number Diff line
@@ -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,
+17 −10
Original line number Diff line number Diff line
@@ -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()
    }

    /**