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

Commit 222bcd66 authored by Vania Januar's avatar Vania Januar
Browse files

Allow setting of preferred user profile to open Notes role

Bug: 278555728
Test: NoteTaskControllerTest
Change-Id: Idfff192d7874aec366478a16e238af99af1f893f
parent 56425234
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -7472,6 +7472,14 @@ public final class Settings {
        @SuppressLint("NoSettingsProvider")
        public static final String STYLUS_BUTTONS_ENABLED = "stylus_buttons_enabled";
        /**
         * Preferred default user profile to use with the notes task button shortcut.
         *
         * @hide
         */
        @SuppressLint("NoSettingsProvider")
        public static final String DEFAULT_NOTE_TASK_PROFILE = "default_note_task_profile";
        /**
         * Host name and port for global http proxy. Uses ':' seperator for
         * between host and port.
+1 −0
Original line number Diff line number Diff line
@@ -723,6 +723,7 @@ public class SettingsBackupTest {
                 Settings.Secure.CONNECTIVITY_RELEASE_PENDING_INTENT_DELAY_MS,
                 Settings.Secure.CONTENT_CAPTURE_ENABLED,
                 Settings.Secure.DEFAULT_INPUT_METHOD,
                 Settings.Secure.DEFAULT_NOTE_TASK_PROFILE,
                 Settings.Secure.DEVICE_PAIRED,
                 Settings.Secure.DIALER_DEFAULT_APPLICATION,
                 Settings.Secure.DISABLED_PRINT_SERVICES,
+14 −1
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ import android.content.pm.ShortcutManager
import android.graphics.drawable.Icon
import android.os.UserHandle
import android.os.UserManager
import android.provider.Settings
import android.widget.Toast
import androidx.annotation.VisibleForTesting
import com.android.systemui.R
@@ -45,6 +46,7 @@ import com.android.systemui.notetask.shortcut.LaunchNoteTaskManagedProfileProxyA
import com.android.systemui.settings.UserTracker
import com.android.systemui.shared.system.ActivityManagerKt.isInForeground
import com.android.systemui.util.kotlin.getOrNull
import com.android.systemui.util.settings.SecureSettings
import com.android.wm.shell.bubbles.Bubble
import com.android.wm.shell.bubbles.Bubbles
import com.android.wm.shell.bubbles.Bubbles.BubbleExpandListener
@@ -75,6 +77,7 @@ constructor(
    @NoteTaskEnabledKey private val isEnabled: Boolean,
    private val devicePolicyManager: DevicePolicyManager,
    private val userTracker: UserTracker,
    private val secureSettings: SecureSettings,
) {

    @VisibleForTesting val infoReference = AtomicReference<NoteTaskInfo?>()
@@ -145,7 +148,7 @@ constructor(
            userTracker.userProfiles.firstOrNull { userManager.isManagedProfile(it.id) }?.userHandle
                ?: userTracker.userHandle
        } else {
            userTracker.userHandle
            secureSettings.preferredUser
        }

    /**
@@ -312,6 +315,16 @@ constructor(
        }
    }

    private val SecureSettings.preferredUser: UserHandle
        get() {
            val userId =
                secureSettings.getInt(
                    Settings.Secure.DEFAULT_NOTE_TASK_PROFILE,
                    userTracker.userHandle.identifier,
                )
            return UserHandle.of(userId)
        }

    companion object {
        val TAG = NoteTaskController::class.simpleName.orEmpty()

+43 −0
Original line number Diff line number Diff line
@@ -39,6 +39,7 @@ import android.content.pm.UserInfo
import android.graphics.drawable.Icon
import android.os.UserHandle
import android.os.UserManager
import android.provider.Settings
import androidx.test.ext.truth.content.IntentSubject.assertThat
import androidx.test.filters.SmallTest
import androidx.test.runner.AndroidJUnit4
@@ -58,6 +59,7 @@ import com.android.systemui.util.mockito.argumentCaptor
import com.android.systemui.util.mockito.capture
import com.android.systemui.util.mockito.eq
import com.android.systemui.util.mockito.whenever
import com.android.systemui.util.settings.SecureSettings
import com.android.wm.shell.bubbles.Bubble
import com.android.wm.shell.bubbles.Bubbles
import com.google.common.truth.Truth.assertThat
@@ -92,6 +94,7 @@ internal class NoteTaskControllerTest : SysuiTestCase() {
    @Mock private lateinit var shortcutManager: ShortcutManager
    @Mock private lateinit var activityManager: ActivityManager
    @Mock private lateinit var devicePolicyManager: DevicePolicyManager
    @Mock private lateinit var secureSettings: SecureSettings
    private val userTracker = FakeUserTracker()

    @Before
@@ -115,6 +118,7 @@ internal class NoteTaskControllerTest : SysuiTestCase() {
        whenever(activityManager.getRunningTasks(anyInt())).thenReturn(emptyList())
        whenever(userManager.isManagedProfile(workUserInfo.id)).thenReturn(true)
        whenever(context.resources).thenReturn(getContext().resources)
        whenever(secureSettings.userTracker).thenReturn(userTracker)
    }

    private fun createNoteTaskController(
@@ -134,6 +138,7 @@ internal class NoteTaskControllerTest : SysuiTestCase() {
            roleManager = roleManager,
            shortcutManager = shortcutManager,
            activityManager = activityManager,
            secureSettings = secureSettings,
        )

    // region onBubbleExpandChanged
@@ -252,6 +257,44 @@ internal class NoteTaskControllerTest : SysuiTestCase() {
        verifyZeroInteractions(bubbles)
    }

    @Test
    fun showNoteTask_defaultUserSet_shouldStartActivityWithExpectedUserAndLogUiEvent() {
        whenever(secureSettings.getInt(eq(Settings.Secure.DEFAULT_NOTE_TASK_PROFILE), any()))
            .thenReturn(10)
        val user10 = UserHandle.of(/* userId= */ 10)

        val expectedInfo =
            NOTE_TASK_INFO.copy(
                entryPoint = NoteTaskEntryPoint.TAIL_BUTTON,
                isKeyguardLocked = true,
                user = user10,
            )
        whenever(keyguardManager.isKeyguardLocked).thenReturn(expectedInfo.isKeyguardLocked)
        whenever(resolver.resolveInfo(any(), any(), any())).thenReturn(expectedInfo)

        createNoteTaskController()
            .showNoteTask(
                entryPoint = expectedInfo.entryPoint!!,
            )

        val intentCaptor = argumentCaptor<Intent>()
        val userCaptor = argumentCaptor<UserHandle>()
        verify(context).startActivityAsUser(capture(intentCaptor), capture(userCaptor))
        intentCaptor.value.let { intent ->
            assertThat(intent.action).isEqualTo(Intent.ACTION_CREATE_NOTE)
            assertThat(intent.`package`).isEqualTo(NOTE_TASK_PACKAGE_NAME)
            assertThat(intent.flags and FLAG_ACTIVITY_NEW_TASK).isEqualTo(FLAG_ACTIVITY_NEW_TASK)
            assertThat(intent.flags and FLAG_ACTIVITY_MULTIPLE_TASK)
                .isEqualTo(FLAG_ACTIVITY_MULTIPLE_TASK)
            assertThat(intent.flags and FLAG_ACTIVITY_NEW_DOCUMENT)
                .isEqualTo(FLAG_ACTIVITY_NEW_DOCUMENT)
            assertThat(intent.getBooleanExtra(Intent.EXTRA_USE_STYLUS_MODE, false)).isTrue()
        }
        assertThat(userCaptor.value).isEqualTo(user10)
        verify(eventLogger).logNoteTaskOpened(expectedInfo)
        verifyZeroInteractions(bubbles)
    }

    @Test
    fun showNoteTaskWithUser_keyguardIsLocked_shouldStartActivityWithExpectedUserAndLogUiEvent() {
        val user10 = UserHandle.of(/* userId= */ 10)