Merge "Replace the main user look up with UserManager#getMainUser" into udc-dev

This commit is contained in:
Steven Ng
2023-04-06 09:22:48 +00:00
committed by Android (Google) Code Review
2 changed files with 31 additions and 5 deletions

View File

@@ -18,9 +18,11 @@ package com.android.systemui.notetask.shortcut
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.pm.UserInfo import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.os.UserHandle
import android.os.UserManager import android.os.UserManager
import android.util.Log
import androidx.activity.ComponentActivity import androidx.activity.ComponentActivity
import com.android.systemui.notetask.NoteTaskController import com.android.systemui.notetask.NoteTaskController
import com.android.systemui.notetask.NoteTaskEntryPoint import com.android.systemui.notetask.NoteTaskEntryPoint
@@ -63,9 +65,13 @@ constructor(
// | Bubble#showOrHideAppBubble | <-------------- // | Bubble#showOrHideAppBubble | <--------------
// | (with WP user ID) | // | (with WP user ID) |
// ---------------------------- // ----------------------------
val mainUser: UserInfo? = userTracker.userProfiles.firstOrNull { it.isMain } val mainUser: UserHandle? = userManager.mainUser
if (userManager.isManagedProfile && mainUser != null) { if (userManager.isManagedProfile) {
controller.startNoteTaskProxyActivityForUser(mainUser.userHandle) if (mainUser == null) {
logDebug { "Can't find the main user. Skipping the notes app launch." }
} else {
controller.startNoteTaskProxyActivityForUser(mainUser)
}
} else { } else {
controller.showNoteTask(entryPoint = NoteTaskEntryPoint.WIDGET_PICKER_SHORTCUT) controller.showNoteTask(entryPoint = NoteTaskEntryPoint.WIDGET_PICKER_SHORTCUT)
} }
@@ -83,3 +89,8 @@ constructor(
} }
} }
} }
/** [Log.println] a [Log.DEBUG] message, only when [Build.IS_DEBUGGABLE]. */
private inline fun Any.logDebug(message: () -> String) {
if (Build.IS_DEBUGGABLE) Log.d(this::class.java.simpleName.orEmpty(), message())
}

View File

@@ -30,6 +30,7 @@ import com.android.systemui.SysuiTestCase
import com.android.systemui.notetask.NoteTaskController import com.android.systemui.notetask.NoteTaskController
import com.android.systemui.notetask.NoteTaskEntryPoint import com.android.systemui.notetask.NoteTaskEntryPoint
import com.android.systemui.settings.FakeUserTracker import com.android.systemui.settings.FakeUserTracker
import com.android.systemui.util.mockito.any
import com.android.systemui.util.mockito.eq import com.android.systemui.util.mockito.eq
import com.android.systemui.util.mockito.whenever import com.android.systemui.util.mockito.whenever
import org.junit.After import org.junit.After
@@ -38,6 +39,7 @@ import org.junit.Rule
import org.junit.Test import org.junit.Test
import org.junit.runner.RunWith import org.junit.runner.RunWith
import org.mockito.Mock import org.mockito.Mock
import org.mockito.Mockito.never
import org.mockito.MockitoAnnotations import org.mockito.MockitoAnnotations
@RunWith(AndroidTestingRunner::class) @RunWith(AndroidTestingRunner::class)
@@ -86,15 +88,28 @@ class LaunchNoteTaskActivityTest : SysuiTestCase() {
@Test @Test
fun startActivityOnWorkProfileUser_shouldLaunchProxyActivity() { fun startActivityOnWorkProfileUser_shouldLaunchProxyActivity() {
val mainUserHandle: UserHandle = mainUser.userHandle
userTracker.set(listOf(mainUser, workProfileUser), selectedUserIndex = 1) userTracker.set(listOf(mainUser, workProfileUser), selectedUserIndex = 1)
whenever(userManager.isManagedProfile).thenReturn(true) whenever(userManager.isManagedProfile).thenReturn(true)
whenever(userManager.mainUser).thenReturn(mainUserHandle)
activityRule.launchActivity(/* startIntent= */ null) activityRule.launchActivity(/* startIntent= */ null)
val mainUserHandle: UserHandle = mainUser.userHandle
verify(noteTaskController).startNoteTaskProxyActivityForUser(eq(mainUserHandle)) verify(noteTaskController).startNoteTaskProxyActivityForUser(eq(mainUserHandle))
} }
@Test
fun startActivityOnWorkProfileUser_noMainUser_shouldNotLaunch() {
userTracker.set(listOf(mainUser, workProfileUser), selectedUserIndex = 1)
whenever(userManager.isManagedProfile).thenReturn(true)
whenever(userManager.mainUser).thenReturn(null)
activityRule.launchActivity(/* startIntent= */ null)
verify(noteTaskController, never()).showNoteTask(any())
verify(noteTaskController, never()).startNoteTaskProxyActivityForUser(any())
}
private companion object { private companion object {
val mainUser = UserInfo(/* id= */ 0, /* name= */ "primary", /* flags= */ UserInfo.FLAG_MAIN) val mainUser = UserInfo(/* id= */ 0, /* name= */ "primary", /* flags= */ UserInfo.FLAG_MAIN)
val workProfileUser = val workProfileUser =