1 /* 2 * Copyright (C) 2025 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.systemui.keyboard.shortcut.fakes 18 19 import android.content.ComponentName 20 import android.content.pm.LauncherActivityInfo 21 import android.content.pm.LauncherApps 22 import android.os.UserHandle 23 import org.mockito.kotlin.any 24 import org.mockito.kotlin.anyOrNull 25 import org.mockito.kotlin.mock 26 27 class FakeLauncherApps { 28 29 private val activityListPerUser: MutableMap<Int, MutableList<LauncherActivityInfo>> = 30 mutableMapOf() 31 private val callbacks: MutableList<LauncherApps.Callback> = mutableListOf() 32 <lambda>null33 val launcherApps: LauncherApps = mock { 34 on { getActivityList(anyOrNull(), any()) } 35 .then { 36 val userHandle = it.getArgument<UserHandle>(1) 37 38 activityListPerUser.getOrDefault(userHandle.identifier, emptyList()) 39 } 40 on { registerCallback(any(), any()) } 41 .then { 42 val callback = it.getArgument<LauncherApps.Callback>(0) 43 44 callbacks.add(callback) 45 } 46 on { unregisterCallback(any()) } 47 .then { 48 val callback = it.getArgument<LauncherApps.Callback>(0) 49 50 callbacks.remove(callback) 51 } 52 } 53 installPackageForUsernull54 fun installPackageForUser(packageName: String, className: String, userHandle: UserHandle) { 55 val launcherActivityInfo: LauncherActivityInfo = mock { 56 on { componentName } 57 .thenReturn(ComponentName(/* pkg= */ packageName, /* cls= */ className)) 58 } 59 60 if (!activityListPerUser.containsKey(userHandle.identifier)) { 61 activityListPerUser[userHandle.identifier] = mutableListOf() 62 } 63 64 activityListPerUser[userHandle.identifier]!!.add(launcherActivityInfo) 65 66 callbacks.forEach { it.onPackageAdded(/* pkg= */ packageName, userHandle) } 67 } 68 uninstallPackageForUsernull69 fun uninstallPackageForUser(packageName: String, className: String, userHandle: UserHandle) { 70 activityListPerUser[userHandle.identifier]?.removeIf { 71 it.componentName.packageName == packageName && it.componentName.className == className 72 } 73 74 callbacks.forEach { it.onPackageRemoved(/* pkg= */ packageName, userHandle) } 75 } 76 } 77