@@ -46,7 +46,6 @@ import org.w3c.dom.Element
46
46
import org.w3c.dom.Node
47
47
import java.io.File
48
48
import java.io.IOException
49
- import java.net.URI
50
49
import java.util.UUID
51
50
import java.util.concurrent.CompletableFuture
52
51
import java.util.concurrent.Executors
@@ -504,85 +503,55 @@ class AndroidDriver(
504
503
}
505
504
506
505
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
+ }
511
511
512
512
if (autoVerify) {
513
- if (appId != null && ! browser) autoVerifyWithChooser(appId)
514
- autoVerifyChromeOnboarding()
513
+ autoVerifyApp(appId)
515
514
}
516
515
}
517
516
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)
531
520
}
521
+ autoVerifyChromeAgreement()
532
522
}
533
523
534
- private fun autoVerifyWithChooser (appId : String ) {
524
+ private fun autoVerifyWithAppName (appId : String ) {
535
525
val appNameResult = runCatching {
536
526
val apkFile = AndroidAppFiles .getApkFile(dadb, appId)
537
527
val appName = ApkFile (apkFile).apkMeta.name
538
528
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
557
530
}
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
+ }
563
548
}
564
-
565
- LOGGER .info(" Aborting autoVerify. Could not find app name element for $appName " )
566
549
}
567
550
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()) }
580
553
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()) }
586
555
}
587
556
588
557
private fun filterByText (textRegex : String ): UiElement ? {
@@ -603,12 +572,13 @@ class AndroidDriver(
603
572
installedPackages.contains(" com.android.chrome" ) -> {
604
573
dadb.shell(" am start -a android.intent.action.VIEW -d \" $link \" com.android.chrome" )
605
574
}
575
+
606
576
installedPackages.contains(" org.mozilla.firefox" ) -> {
607
577
dadb.shell(" am start -a android.intent.action.VIEW -d \" $link \" org.mozilla.firefox" )
608
578
}
579
+
609
580
else -> {
610
581
dadb.shell(" am start -a android.intent.action.VIEW -d \" $link \" " )
611
- autoVerifyWithChooser(" org.chromium.webview_shell" )
612
582
}
613
583
}
614
584
}
0 commit comments