Skip to content

Commit c986eb6

Browse files
Revert "Fix android open link verify (#1999)" (#2085)
This reverts commit 6ac16c2.
1 parent 9721a56 commit c986eb6

File tree

2 files changed

+38
-72
lines changed

2 files changed

+38
-72
lines changed

maestro-client/src/main/java/maestro/android/AndroidAppFiles.kt

+4-8
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,11 @@ object AndroidAppFiles {
3838
}
3939

4040
fun getApkFile(dadb: Dadb, appId: String): File {
41-
val apkPath = dadb.shell("pm path $appId").output.removePrefix("package:").trim()
41+
val apkPath = dadb.shell("pm list packages -f --user 0 | grep $appId | head -1")
42+
.output.substringAfterLast("package:").substringBefore("=$appId")
43+
apkPath.substringBefore("=$appId")
4244
val dst = File.createTempFile("tmp", ".apk")
43-
try {
44-
dadb.pull(dst, apkPath)
45-
} catch (e: IOException) {
46-
val newApkPath = "/sdcard/$appId.apk"
47-
dadb.shell("cp $apkPath $newApkPath")
48-
dadb.pull(dst, newApkPath)
49-
}
45+
dadb.pull(dst, apkPath)
5046
return dst
5147
}
5248

maestro-client/src/main/java/maestro/drivers/AndroidDriver.kt

+34-64
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ import org.w3c.dom.Element
4646
import org.w3c.dom.Node
4747
import java.io.File
4848
import java.io.IOException
49-
import java.net.URI
5049
import java.util.UUID
5150
import java.util.concurrent.CompletableFuture
5251
import java.util.concurrent.Executors
@@ -504,85 +503,55 @@ class AndroidDriver(
504503
}
505504

506505
override fun openLink(link: String, appId: String?, autoVerify: Boolean, browser: Boolean) {
507-
if (autoVerify && !browser && appId != null) autoVerifyLinkFromSettings(appId, link)
508-
509-
if (browser) openBrowser(link)
510-
else dadb.shell("am start -a android.intent.action.VIEW -d \"$link\"")
506+
if (browser) {
507+
openBrowser(link)
508+
} else {
509+
dadb.shell("am start -a android.intent.action.VIEW -d \"$link\"")
510+
}
511511

512512
if (autoVerify) {
513-
if (appId != null && !browser) autoVerifyWithChooser(appId)
514-
autoVerifyChromeOnboarding()
513+
autoVerifyApp(appId)
515514
}
516515
}
517516

518-
private fun autoVerifyLinkFromSettings(appId: String, link: String) {
519-
val apiLevel = getDeviceApiLevel()
520-
if (apiLevel <= 30) return
521-
val domain = runCatching { URI.create(link).toURL().host }.getOrNull() ?: return
522-
val packageOption = "--package $appId"
523-
val allowed = dadb.shell("pm set-app-links-allowed --user 0 $packageOption true")
524-
if (allowed.exitCode > 0) {
525-
LOGGER.debug("set-app-links-allowed failed for appId: $appId reason: ${allowed.errorOutput}")
526-
return
527-
}
528-
val selected = dadb.shell("pm set-app-links-user-selection --user 0 $packageOption true $domain")
529-
if (selected.exitCode > 0) {
530-
LOGGER.debug("set-app-links-user-selection failed for appId: $appId domain: $domain reason: ${selected.errorOutput}")
517+
private fun autoVerifyApp(appId: String?) {
518+
if (appId != null) {
519+
autoVerifyWithAppName(appId)
531520
}
521+
autoVerifyChromeAgreement()
532522
}
533523

534-
private fun autoVerifyWithChooser(appId: String) {
524+
private fun autoVerifyWithAppName(appId: String) {
535525
val appNameResult = runCatching {
536526
val apkFile = AndroidAppFiles.getApkFile(dadb, appId)
537527
val appName = ApkFile(apkFile).apkMeta.name
538528
apkFile.delete()
539-
appName ?: appId // The app chooser shows the appId if no application label attribute is set
540-
}
541-
if (appNameResult.isFailure) {
542-
LOGGER.info("Aborting autoVerify. Could not get app name from APK metadata for $appId", appNameResult.exceptionOrNull())
543-
return
544-
}
545-
546-
fun selectChooserOptionOnce(wordElement: UiElement) {
547-
tap(wordElement.bounds.center())
548-
filterById("android:id/button_once")?.let { tap(it.bounds.center()) }
549-
}
550-
551-
waitUntilScreenIsStatic(3000)
552-
553-
val appName = appNameResult.getOrThrow()
554-
filterByText(".*$appName.*")?.let {
555-
selectChooserOptionOnce(it)
556-
return
529+
appName
557530
}
558-
559-
val appNameWord = appName.split(" ").first()
560-
filterByText(".*$appNameWord.*")?.let {
561-
selectChooserOptionOnce(it)
562-
return
531+
if (appNameResult.isSuccess) {
532+
val appName = appNameResult.getOrThrow()
533+
waitUntilScreenIsStatic(3000)
534+
val appNameElement = filterByText(appName)
535+
if (appNameElement != null) {
536+
tap(appNameElement.bounds.center())
537+
filterById("android:id/button_once")?.let {
538+
tap(it.bounds.center())
539+
}
540+
} else {
541+
val openWithAppElement = filterByText(".*$appName.*")
542+
if (openWithAppElement != null) {
543+
filterById("android:id/button_once")?.let {
544+
tap(it.bounds.center())
545+
}
546+
}
547+
}
563548
}
564-
565-
LOGGER.info("Aborting autoVerify. Could not find app name element for $appName")
566549
}
567550

568-
private fun autoVerifyChromeOnboarding() {
569-
val chrome = "com.android.chrome"
570-
if (!isPackageInstalled(chrome)) return
571-
Thread.sleep(100) // Lets enough time for the transition to Chrome to start
572-
waitUntilScreenIsStatic(3000)
573-
// Welcome to Chrome screen "Accept & continue"
574-
filterById("$chrome:id/send_report_checkbox")?.let { tap(it.bounds.center()) }
575-
filterById("$chrome:id/negative_button")?.let { tap(it.bounds.center()) }
576-
filterById("$chrome:id/terms_accept")?.let { tap(it.bounds.center()) }
577-
waitForAppToSettle(null, null)
578-
// Welcome to Chrome screen "Add account to device"
579-
filterById("$chrome:id/signin_fre_dismiss_button")?.let { tap(it.bounds.center()) }
551+
private fun autoVerifyChromeAgreement() {
552+
filterById("com.android.chrome:id/terms_accept")?.let { tap(it.bounds.center()) }
580553
waitForAppToSettle(null, null)
581-
// Turn on Sync screen
582-
filterById("$chrome:id/negative_button")?.let { tap(it.bounds.center()) }
583-
waitForAppToSettle(null, null)
584-
// Chrome Notifications screen
585-
filterById("$chrome:id/negative_button")?.let { tap(it.bounds.center()) }
554+
filterById("com.android.chrome:id/negative_button")?.let { tap(it.bounds.center()) }
586555
}
587556

588557
private fun filterByText(textRegex: String): UiElement? {
@@ -603,12 +572,13 @@ class AndroidDriver(
603572
installedPackages.contains("com.android.chrome") -> {
604573
dadb.shell("am start -a android.intent.action.VIEW -d \"$link\" com.android.chrome")
605574
}
575+
606576
installedPackages.contains("org.mozilla.firefox") -> {
607577
dadb.shell("am start -a android.intent.action.VIEW -d \"$link\" org.mozilla.firefox")
608578
}
579+
609580
else -> {
610581
dadb.shell("am start -a android.intent.action.VIEW -d \"$link\"")
611-
autoVerifyWithChooser("org.chromium.webview_shell")
612582
}
613583
}
614584
}

0 commit comments

Comments
 (0)