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

Commit 98128f97 authored by Michael Mikhail's avatar Michael Mikhail
Browse files

Filter media on user profile change

Flag: NONE
Bug: 298342949
Test: atest MediaDataFilterTest
Test: atest UserTrackerImplTest
Change-Id: Id92dce172924711e4bed8e8b0dddb80d495983e9
parent 6fafa0de
Loading
Loading
Loading
Loading
+24 −3
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package com.android.systemui.media.controls.pipeline

import android.content.Context
import android.content.pm.UserInfo
import android.os.SystemProperties
import android.util.Log
import com.android.internal.annotations.KeepForWeakReference
@@ -88,7 +89,11 @@ constructor(
    private val userTrackerCallback =
        object : UserTracker.Callback {
            override fun onUserChanged(newUser: Int, userContext: Context) {
                handleUserSwitched(newUser)
                handleUserSwitched()
            }

            override fun onProfilesChanged(profiles: List<UserInfo>) {
                handleProfileChanged()
            }
        }

@@ -109,7 +114,10 @@ constructor(
        }
        allEntries.put(key, data)

        if (!lockscreenUserManager.isCurrentProfile(data.userId)) {
        if (
            !lockscreenUserManager.isCurrentProfile(data.userId) ||
                !lockscreenUserManager.isProfileAvailable(data.userId)
        ) {
            return
        }

@@ -231,7 +239,20 @@ constructor(
    }

    @VisibleForTesting
    internal fun handleUserSwitched(id: Int) {
    internal fun handleProfileChanged() {
        // TODO(b/317221348) re-add media removed when profile is available.
        allEntries.forEach { (key, data) ->
            if (!lockscreenUserManager.isProfileAvailable(data.userId)) {
                // Only remove media when the profile is unavailable.
                if (DEBUG) Log.d(TAG, "Removing $key after profile change")
                userEntries.remove(key, data)
                listeners.forEach { listener -> listener.onMediaDataRemoved(key) }
            }
        }
    }

    @VisibleForTesting
    internal fun handleUserSwitched() {
        // If the user changes, remove all current MediaData objects and inform listeners
        val listenersCopy = listeners
        val keyCopy = userEntries.keys.toMutableList()
+9 −1
Original line number Diff line number Diff line
@@ -135,6 +135,10 @@ open class UserTrackerImpl internal constructor(
        val filter = IntentFilter().apply {
            addAction(Intent.ACTION_LOCALE_CHANGED)
            addAction(Intent.ACTION_USER_INFO_CHANGED)
            addAction(Intent.ACTION_PROFILE_ADDED)
            addAction(Intent.ACTION_PROFILE_REMOVED)
            addAction(Intent.ACTION_PROFILE_AVAILABLE)
            addAction(Intent.ACTION_PROFILE_UNAVAILABLE)
            // These get called when a managed profile goes in or out of quiet mode.
            addAction(Intent.ACTION_MANAGED_PROFILE_AVAILABLE)
            addAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE)
@@ -157,7 +161,11 @@ open class UserTrackerImpl internal constructor(
            Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE,
            Intent.ACTION_MANAGED_PROFILE_ADDED,
            Intent.ACTION_MANAGED_PROFILE_REMOVED,
            Intent.ACTION_MANAGED_PROFILE_UNLOCKED -> {
            Intent.ACTION_MANAGED_PROFILE_UNLOCKED,
            Intent.ACTION_PROFILE_ADDED,
            Intent.ACTION_PROFILE_REMOVED,
            Intent.ACTION_PROFILE_AVAILABLE,
            Intent.ACTION_PROFILE_UNAVAILABLE -> {
                handleProfilesChanged()
            }
        }
+7 −0
Original line number Diff line number Diff line
@@ -44,6 +44,13 @@ public interface NotificationLockscreenUserManager {

    boolean isCurrentProfile(int userId);

    /**
     *
     * @param userId user Id
     * @return true if user profile is running.
     */
    boolean isProfileAvailable(int userId);

    /** Adds a listener to be notified when the current user changes. */
    void addUserChangedListener(UserChangedListener listener);

+7 −0
Original line number Diff line number Diff line
@@ -461,6 +461,13 @@ public class NotificationLockscreenUserManagerImpl implements
        }
    }

    @SuppressLint("AndroidFrameworkRequiresPermission")
    public boolean isProfileAvailable(int userId) {
        synchronized (mLock) {
            return mUserManager.isUserRunning(userId);
        }
    }

    private void setShowLockscreenNotifications(boolean show) {
        mShowLockscreenNotifications = show;
    }
+29 −1
Original line number Diff line number Diff line
@@ -55,6 +55,7 @@ private const val KEY = "TEST_KEY"
private const val KEY_ALT = "TEST_KEY_2"
private const val USER_MAIN = 0
private const val USER_GUEST = 10
private const val PRIVATE_PROFILE = 12
private const val PACKAGE = "PKG"
private val INSTANCE_ID = InstanceId.fakeInstanceId(123)!!
private const val APP_UID = 99
@@ -82,6 +83,7 @@ class MediaDataFilterTest : SysuiTestCase() {
    private lateinit var mediaDataFilter: MediaDataFilter
    private lateinit var dataMain: MediaData
    private lateinit var dataGuest: MediaData
    private lateinit var dataPrivateProfile: MediaData
    private val clock = FakeSystemClock()

    @Before
@@ -115,6 +117,7 @@ class MediaDataFilterTest : SysuiTestCase() {
                appUid = APP_UID
            )
        dataGuest = dataMain.copy(userId = USER_GUEST)
        dataPrivateProfile = dataMain.copy(userId = PRIVATE_PROFILE)

        whenever(smartspaceData.targetId).thenReturn(SMARTSPACE_KEY)
        whenever(smartspaceData.isActive).thenReturn(true)
@@ -130,8 +133,19 @@ class MediaDataFilterTest : SysuiTestCase() {

    private fun setUser(id: Int) {
        whenever(lockscreenUserManager.isCurrentProfile(anyInt())).thenReturn(false)
        whenever(lockscreenUserManager.isProfileAvailable(anyInt())).thenReturn(false)
        whenever(lockscreenUserManager.isCurrentProfile(eq(id))).thenReturn(true)
        mediaDataFilter.handleUserSwitched(id)
        whenever(lockscreenUserManager.isProfileAvailable(eq(id))).thenReturn(true)
        whenever(lockscreenUserManager.isProfileAvailable(eq(PRIVATE_PROFILE))).thenReturn(true)
        mediaDataFilter.handleUserSwitched()
    }

    private fun setPrivateProfileUnavailable() {
        whenever(lockscreenUserManager.isCurrentProfile(anyInt())).thenReturn(false)
        whenever(lockscreenUserManager.isCurrentProfile(eq(USER_MAIN))).thenReturn(true)
        whenever(lockscreenUserManager.isCurrentProfile(eq(PRIVATE_PROFILE))).thenReturn(true)
        whenever(lockscreenUserManager.isProfileAvailable(eq(PRIVATE_PROFILE))).thenReturn(false)
        mediaDataFilter.handleProfileChanged()
    }

    @Test
@@ -205,6 +219,20 @@ class MediaDataFilterTest : SysuiTestCase() {
            .onMediaDataLoaded(eq(KEY), any(), eq(dataMain), anyBoolean(), anyInt(), anyBoolean())
    }

    @Test
    fun testOnProfileChanged_profileUnavailable_loadControls() {
        // GIVEN that we had some media for both profiles
        mediaDataFilter.onMediaDataLoaded(KEY, null, dataMain)
        mediaDataFilter.onMediaDataLoaded(KEY_ALT, null, dataPrivateProfile)
        reset(listener)

        // and we change profile status
        setPrivateProfileUnavailable()

        // THEN we should add the private profile media
        verify(listener).onMediaDataRemoved(eq(KEY_ALT))
    }

    @Test
    fun hasAnyMedia_noMediaSet_returnsFalse() {
        assertThat(mediaDataFilter.hasAnyMedia()).isFalse()
Loading