Files
Cognitive_Agent/N7Cockpit/scripts/start-remote-app.ps1
T

58 lines
1.9 KiB
PowerShell

[CmdletBinding()]
param(
[string]$AdbPath = 'D:\Android\Sdk\platform-tools\adb.exe',
[string]$PackageName = 'com.seeker.n7cockpit.remote',
[string]$ActivityName = 'com.seeker.n7cockpit.MainActivity',
[int]$TimeoutSeconds = 15
)
$ErrorActionPreference = 'Stop'
if (-not (Test-Path -LiteralPath $AdbPath)) {
throw "adb does not exist: $AdbPath"
}
$deviceLines = @(& $AdbPath devices) | Where-Object { $_ -match "\tdevice$" }
if ($deviceLines.Count -ne 1) {
throw "Exactly one online Android device is required; found $($deviceLines.Count)."
}
$installed = (& $AdbPath shell pm list packages $PackageName | Out-String).Trim()
if ($installed -notmatch [regex]::Escape($PackageName)) {
throw "The remote debug app is not installed: $PackageName"
}
$component = "$PackageName/$ActivityName"
& $AdbPath shell am force-stop $PackageName | Out-Null
$launchOutput = & $AdbPath shell am start -W -n $component 2>&1
if ($LASTEXITCODE -ne 0 -or ($launchOutput -join "`n") -notmatch 'Status:\s+ok') {
throw "Failed to launch app:`n$($launchOutput -join "`n")"
}
$deadline = (Get-Date).AddSeconds($TimeoutSeconds)
$resumed = ''
$pidValue = ''
do {
Start-Sleep -Milliseconds 500
$pidValue = (& $AdbPath shell pidof $PackageName 2>$null | Out-String).Trim()
$resumed = (& $AdbPath shell dumpsys activity activities |
Select-String 'topResumedActivity' |
Select-Object -First 1).Line
} while ((-not $pidValue -or $resumed -notmatch [regex]::Escape($component)) -and
(Get-Date) -lt $deadline)
if (-not $pidValue) {
throw 'The app process was not found after launch.'
}
if ($resumed -notmatch [regex]::Escape($component)) {
throw "The app process exists but its Activity is not foreground: $resumed"
}
[pscustomobject]@{
Device = (([string]$deviceLines) -split "\t")[0]
Package = $PackageName
Component = $component
Pid = $pidValue
Foreground = $true
}