-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppLauncherTest.kt
More file actions
72 lines (57 loc) · 2.58 KB
/
AppLauncherTest.kt
File metadata and controls
72 lines (57 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import java.util.Locale
data class AppInfo(val packageName: String, val label: String, val isSystem: Boolean)
class MockPackageManager(private val apps: List<AppInfo>) {
fun getInstalledApplications(): List<AppInfo> = apps
fun getApplicationLabel(app: AppInfo): String = app.label
}
fun findPackageNameFromAppName(pm: MockPackageManager, appName: String): String? {
val packages = pm.getInstalledApplications()
// Separate user apps from system apps
val userApps = packages.filter { !it.isSystem }
val systemApps = packages.filter { it.isSystem }
println("Searching for '$appName'...")
println("User Apps: ${userApps.map { it.label }}")
println("System Apps: ${systemApps.map { it.label }}")
// 1. Exact match in USER apps
userApps.find { it.label.equals(appName, ignoreCase = true) }?.let {
println("✅ Found exact match in USER apps: ${it.label}")
return it.packageName
}
// 2. Exact match in SYSTEM apps
systemApps.find { it.label.equals(appName, ignoreCase = true) }?.let {
println("✅ Found exact match in SYSTEM apps: ${it.label}")
return it.packageName
}
// 3. Partial match in USER apps
val userMatches = userApps.filter { it.label.contains(appName, ignoreCase = true) }
if (userMatches.isNotEmpty()) {
val best = userMatches.minByOrNull { it.label.length }!!
println("✅ Found partial match in USER apps: ${best.label}")
return best.packageName
}
// 4. Partial match in SYSTEM apps
systemApps.find { it.label.contains(appName, ignoreCase = true) }?.let {
println("⚠️ Found partial match in SYSTEM apps: ${it.label}")
return it.packageName
}
println("❌ Not found")
return null
}
fun main() {
val apps = listOf(
AppInfo("com.instagram.android", "Instagram", false),
AppInfo("com.android.settings.instagram", "Instagram Settings", true),
AppInfo("com.whatsapp", "WhatsApp", false),
AppInfo("com.android.settings", "Settings", true)
)
val pm = MockPackageManager(apps)
println("--- Test 1: Search 'Instagram' ---")
val pkg1 = findPackageNameFromAppName(pm, "Instagram")
assert(pkg1 == "com.instagram.android") { "Failed Test 1" }
println("\n--- Test 2: Search 'Settings' ---")
val pkg2 = findPackageNameFromAppName(pm, "Settings")
assert(pkg2 == "com.android.settings") { "Failed Test 2" }
println("\n--- Test 3: Search 'Insta' (Partial) ---")
val pkg3 = findPackageNameFromAppName(pm, "Insta")
assert(pkg3 == "com.instagram.android") { "Failed Test 3" }
}