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

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

Merge "Remove controls when apps are stopped or removed" into rvc-dev

parents b7b425e4 7fee1968
Loading
Loading
Loading
Loading
+41 −0
Original line number Diff line number Diff line
@@ -109,6 +109,24 @@ class MediaDataManager @Inject constructor(
        }
    }

    private val appChangeReceiver = object : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            when (intent.action) {
                Intent.ACTION_PACKAGES_SUSPENDED -> {
                    val packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST)
                    packages?.forEach {
                        removeAllForPackage(it)
                    }
                }
                Intent.ACTION_PACKAGE_REMOVED, Intent.ACTION_PACKAGE_RESTARTED -> {
                    intent.data?.encodedSchemeSpecificPart?.let {
                        removeAllForPackage(it)
                    }
                }
            }
        }
    }

    init {
        mediaTimeoutListener.timeoutCallback = { token: String, timedOut: Boolean ->
            setTimedOut(token, timedOut) }
@@ -129,6 +147,17 @@ class MediaDataManager @Inject constructor(

        val userFilter = IntentFilter(Intent.ACTION_USER_SWITCHED)
        broadcastDispatcher.registerReceiver(userChangeReceiver, userFilter, null, UserHandle.ALL)

        val suspendFilter = IntentFilter(Intent.ACTION_PACKAGES_SUSPENDED)
        broadcastDispatcher.registerReceiver(appChangeReceiver, suspendFilter, null, UserHandle.ALL)

        val uninstallFilter = IntentFilter().apply {
            addAction(Intent.ACTION_PACKAGE_REMOVED)
            addAction(Intent.ACTION_PACKAGE_RESTARTED)
            addDataScheme("package")
        }
        // BroadcastDispatcher does not allow filters with data schemes
        context.registerReceiver(appChangeReceiver, uninstallFilter)
    }

    fun onNotificationAdded(key: String, sbn: StatusBarNotification) {
@@ -160,6 +189,18 @@ class MediaDataManager @Inject constructor(
        mediaEntries.clear()
    }

    private fun removeAllForPackage(packageName: String) {
        Assert.isMainThread()
        val listenersCopy = listeners.toSet()
        val toRemove = mediaEntries.filter { it.value.packageName == packageName }
        toRemove.forEach {
            mediaEntries.remove(it.key)
            listenersCopy.forEach { listener ->
                listener.onMediaDataRemoved(it.key)
            }
        }
    }

    private fun addResumptionControls(
        desc: MediaDescription,
        action: Runnable,