@@ -7,6 +7,7 @@ import { isDeepLinkTarget } from '@agent-device/contracts/command';
77
88import { waitForAndroidBoot } from './emulator-lifecycle.ts' ;
99import { runAndroidShell } from './adb.ts' ;
10+ import type { AndroidAdbExecutorResult } from './adb-transport.ts' ;
1011import {
1112 androidAdbResultError ,
1213 createAndroidPortReverseManager ,
@@ -194,11 +195,20 @@ function androidLaunchArgs(options: OpenAndroidAppOptions): string[] {
194195 return options . launchArgs ?? [ ] ;
195196}
196197
198+ /** Opens the target and returns the package `am start -W` reports it launched, when reported. */
197199export async function openAndroidApp (
198200 device : DeviceInfo ,
199201 app : string ,
200202 optionsOrActivity ?: OpenAndroidAppOptions | string ,
201- ) : Promise < void > {
203+ ) : Promise < string | undefined > {
204+ return launchedAndroidPackage ( await startAndroidApp ( device , app , optionsOrActivity ) ) ;
205+ }
206+
207+ async function startAndroidApp (
208+ device : DeviceInfo ,
209+ app : string ,
210+ optionsOrActivity ?: OpenAndroidAppOptions | string ,
211+ ) : Promise < AndroidAdbExecutorResult > {
202212 const options = normalizeOpenAndroidAppOptions ( optionsOrActivity ) ;
203213 const activity = options . activity ;
204214 if ( activity !== undefined ) requireAndroidActivityComponent ( activity ) ;
@@ -207,43 +217,52 @@ export async function openAndroidApp(
207217 }
208218 const deepLinkTarget = app . trim ( ) ;
209219 if ( isDeepLinkTarget ( deepLinkTarget ) ) {
210- await openAndroidDeepLink ( device , deepLinkTarget , options ) ;
211- return ;
220+ return await openAndroidDeepLink ( device , deepLinkTarget , options ) ;
212221 }
213222 if ( options . url !== undefined ) {
214- await openAndroidAppBoundDeepLink ( device , app , options ) ;
215- return ;
223+ return await openAndroidAppBoundDeepLink ( device , app , options ) ;
216224 }
217225 const resolved = await resolveAndroidApp ( device , app ) ;
218226 const launchCategory = resolveAndroidLauncherCategory ( device ) ;
219227 if ( resolved . type === 'intent' ) {
220- await openAndroidIntent ( device , resolved . value , options ) ;
221- return ;
228+ return await openAndroidIntent ( device , resolved . value , options ) ;
222229 }
223230 if ( options . terminateRunningApp ) await stopAndroidPackage ( device , resolved . value ) ;
224231 if ( activity ) {
225- await openAndroidPackageActivity ( device , resolved . value , activity , launchCategory , options ) ;
226- return ;
232+ return await openAndroidPackageActivity (
233+ device ,
234+ resolved . value ,
235+ activity ,
236+ launchCategory ,
237+ options ,
238+ ) ;
227239 }
228- await openAndroidPackage ( device , resolved . value , launchCategory , options ) ;
240+ return await openAndroidPackage ( device , resolved . value , launchCategory , options ) ;
241+ }
242+
243+ /** `am start -W` names the activity this launch resumed on its `Activity: <package>/<class>` line. */
244+ function launchedAndroidPackage ( result : AndroidAdbExecutorResult ) : string | undefined {
245+ const line = result . stdout . split ( '\n' ) . find ( ( entry ) => entry . trim ( ) . startsWith ( 'Activity:' ) ) ;
246+ return line ?. split ( ':' ) [ 1 ] ?. trim ( ) . split ( '/' ) [ 0 ] || undefined ;
229247}
230248
231249async function openAndroidDeepLink (
232250 device : DeviceInfo ,
233251 target : string ,
234252 options : OpenAndroidAppOptions ,
235- ) : Promise < void > {
253+ ) : Promise < AndroidAdbExecutorResult > {
236254 if ( options . activity ) {
237255 throw new AppError (
238256 'INVALID_ARGS' ,
239257 'Activity override is not supported when opening a deep link URL' ,
240258 ) ;
241259 }
242260 await ensureAndroidLocalhostReverse ( device , target ) ;
243- await runAndroidShell ( device , [
261+ return await runAndroidShell ( device , [
244262 'am' ,
245263 'start' ,
246264 '-W' ,
265+ ...( options . terminateRunningApp ? [ '-S' ] : [ ] ) ,
247266 '-a' ,
248267 'android.intent.action.VIEW' ,
249268 '-d' ,
@@ -257,7 +276,7 @@ async function openAndroidAppBoundDeepLink(
257276 device : DeviceInfo ,
258277 app : string ,
259278 options : OpenAndroidAppOptions ,
260- ) : Promise < void > {
279+ ) : Promise < AndroidAdbExecutorResult > {
261280 if ( options . activity ) {
262281 throw new AppError (
263282 'INVALID_ARGS' ,
@@ -270,7 +289,8 @@ async function openAndroidAppBoundDeepLink(
270289 }
271290 await ensureAndroidLocalhostReverse ( device , deepLinkUrl ) ;
272291 const resolved = await requireAndroidPackageForOpen ( device , app , 'app-bound open' ) ;
273- await runAndroidShell ( device , [
292+ if ( options . terminateRunningApp ) await stopAndroidPackage ( device , resolved ) ;
293+ return await runAndroidShell ( device , [
274294 'am' ,
275295 'start' ,
276296 '-W' ,
@@ -288,11 +308,11 @@ async function openAndroidIntent(
288308 device : DeviceInfo ,
289309 intent : string ,
290310 options : OpenAndroidAppOptions ,
291- ) : Promise < void > {
311+ ) : Promise < AndroidAdbExecutorResult > {
292312 if ( options . activity ) {
293313 throw new AppError ( 'INVALID_ARGS' , 'Activity override requires a package name, not an intent' ) ;
294314 }
295- await runAndroidShell ( device , [
315+ return await runAndroidShell ( device , [
296316 'am' ,
297317 'start' ,
298318 '-W' ,
@@ -309,10 +329,10 @@ async function openAndroidPackageActivity(
309329 activity : string ,
310330 launchCategory : string ,
311331 options : OpenAndroidAppOptions ,
312- ) : Promise < void > {
332+ ) : Promise < AndroidAdbExecutorResult > {
313333 const component = androidActivityComponent ( packageName , activity ) ;
314334 try {
315- await runAndroidShell (
335+ return await runAndroidShell (
316336 device ,
317337 buildAndroidActivityLaunchArgs ( component , launchCategory , options ) ,
318338 ) ;
@@ -327,7 +347,7 @@ async function openAndroidPackage(
327347 packageName : string ,
328348 launchCategory : string ,
329349 options : OpenAndroidAppOptions ,
330- ) : Promise < void > {
350+ ) : Promise < AndroidAdbExecutorResult > {
331351 const primaryResult = await runAndroidShell (
332352 device ,
333353 [
@@ -347,7 +367,7 @@ async function openAndroidPackage(
347367 { allowFailure : true } ,
348368 ) ;
349369 if ( primaryResult . exitCode === 0 && ! isAmStartError ( primaryResult . stdout , primaryResult . stderr ) ) {
350- return ;
370+ return primaryResult ;
351371 }
352372 const component = await resolveAndroidLaunchComponent ( device , packageName ) ;
353373 if ( ! component ) {
@@ -356,7 +376,10 @@ async function openAndroidPackage(
356376 }
357377 throw androidAdbResultError ( `Failed to launch ${ packageName } ` , primaryResult ) ;
358378 }
359- await runAndroidShell ( device , buildAndroidActivityLaunchArgs ( component , launchCategory , options ) ) ;
379+ return await runAndroidShell (
380+ device ,
381+ buildAndroidActivityLaunchArgs ( component , launchCategory , options ) ,
382+ ) ;
360383}
361384
362385function buildAndroidActivityLaunchArgs (
0 commit comments