Skip to content

Commit 14de349

Browse files
committed
fix(android): resolve intent ownership with effective launch arguments
1 parent 7d4d46c commit 14de349

8 files changed

Lines changed: 100 additions & 9 deletions

File tree

‎packages/contracts/src/application-lifecycle-runtime.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export type ApplicationLifecycleExecution = Readonly<{
5757
/** Semantic target resolution used before an application open. */
5858
export type OpenTargetResolutionInput = Readonly<{
5959
target?: string;
60+
launchArgs?: readonly string[];
6061
currentAppBundleId?: string;
6162
surface: SessionSurface;
6263
/** `open --foreground` asks the admitted Apple binding to inspect its exact target. */

‎packages/platform-android/src/app-lifecycle.ts‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,13 @@ function androidLaunchArgs(options: OpenAndroidAppOptions): string[] {
192192
return options.launchArgs ?? [];
193193
}
194194

195+
export function buildAndroidIntentArgs(
196+
intent: string,
197+
launchArgs: readonly string[] = [],
198+
): string[] {
199+
return ['-a', intent, ...launchArgs];
200+
}
201+
195202
export async function openAndroidApp(
196203
device: DeviceInfo,
197204
app: string,
@@ -289,7 +296,12 @@ async function openAndroidIntent(
289296
if (options.activity) {
290297
throw new AppError('INVALID_ARGS', 'Activity override requires a package name, not an intent');
291298
}
292-
await runAndroidShell(device, ['am', 'start', '-W', '-a', intent, ...androidLaunchArgs(options)]);
299+
await runAndroidShell(device, [
300+
'am',
301+
'start',
302+
'-W',
303+
...buildAndroidIntentArgs(intent, options.launchArgs),
304+
]);
293305
}
294306

295307
async function openAndroidPackageActivity(

‎packages/platform-android/src/open-target-resolution.test.ts‎

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,47 @@ test('resolveAndroidPackageForOpen retains the native requested intent package w
6666
'package',
6767
'resolve-activity',
6868
'--brief',
69-
'-a',
70-
'android.settings.SETTINGS',
7169
'-c',
7270
'android.intent.category.DEFAULT',
71+
'-a',
72+
'android.settings.SETTINGS',
7373
],
7474
{ allowFailure: true, timeoutMs: 2_000 },
7575
);
7676
expect(getAndroidAppState).not.toHaveBeenCalled();
7777
});
7878

79+
test('intent launch and ownership resolve the same overriding component arguments', async () => {
80+
const { openAndroidApp } = await import('./app-lifecycle.ts');
81+
const launchArgs = ['-n', 'com.example.app/.MainActivity'];
82+
resolveAndroidApp.mockResolvedValue({ type: 'intent', value: 'android.settings.SETTINGS' });
83+
runAndroidShell.mockImplementation(async (_device, args: string[]) => ({
84+
stdout: args.includes('com.example.app/.MainActivity')
85+
? 'com.example.app/.MainActivity'
86+
: 'com.android.settings/.Settings',
87+
stderr: '',
88+
exitCode: 0,
89+
}));
90+
91+
await openAndroidApp(androidDevice, 'settings', { launchArgs });
92+
await expect(resolveAndroidPackageForOpen(androidDevice, 'settings', launchArgs)).resolves.toBe(
93+
'com.example.app',
94+
);
95+
const commands = runAndroidShell.mock.calls.map((call) => call[1]);
96+
const intentArgs = ['-a', 'android.settings.SETTINGS', ...launchArgs];
97+
expect(commands[0]).toEqual(['am', 'start', '-W', ...intentArgs]);
98+
expect(commands[1]).toEqual([
99+
'cmd',
100+
'package',
101+
'resolve-activity',
102+
'--brief',
103+
'-c',
104+
'android.intent.category.DEFAULT',
105+
...intentArgs,
106+
]);
107+
expect(getAndroidAppState).not.toHaveBeenCalled();
108+
});
109+
79110
test.each([
80111
'No activity found',
81112
'com.android.settings/Invalid-Class',
@@ -100,7 +131,12 @@ test('intent package ownership does not adopt a component from a failed native r
100131
stderr: '',
101132
exitCode: 1,
102133
});
103-
await expect(resolveAndroidPackageForOpen(androidDevice, 'settings')).resolves.toBeUndefined();
134+
await expect(
135+
resolveAndroidPackageForOpen(androidDevice, 'settings', [
136+
'-n',
137+
'com.example.app/.MainActivity',
138+
]),
139+
).resolves.toBeUndefined();
104140
});
105141

106142
test('intent package ownership reads its native handler rather than assuming the Settings package', async () => {

‎packages/platform-android/src/open-target-resolution.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@ import type { DeviceInfo } from '@agent-device/kernel/device';
33
import { resolveAndroidApp } from './app-deployment-resolution.ts';
44
import { getAndroidAppState } from './window-state.ts';
55
import { runAndroidShell } from './adb.ts';
6-
import { androidActivityComponent, parseAndroidLaunchComponent } from './app-lifecycle.ts';
6+
import {
7+
androidActivityComponent,
8+
buildAndroidIntentArgs,
9+
parseAndroidLaunchComponent,
10+
} from './app-lifecycle.ts';
711

812
const INTENT_PACKAGE_RESOLUTION_TIMEOUT_MS = 2_000;
913

1014
/** Resolves an ADB app open to its package or the native handler of its requested intent. */
1115
export async function resolveAndroidPackageForOpen(
1216
device: DeviceInfo,
1317
openTarget: string | undefined,
18+
launchArgs?: readonly string[],
1419
): Promise<string | undefined> {
1520
if (device.platform !== 'android' || !openTarget || isDeepLinkTarget(openTarget))
1621
return undefined;
@@ -24,10 +29,9 @@ export async function resolveAndroidPackageForOpen(
2429
'package',
2530
'resolve-activity',
2631
'--brief',
27-
'-a',
28-
resolved.value,
2932
'-c',
3033
'android.intent.category.DEFAULT',
34+
...buildAndroidIntentArgs(resolved.value, launchArgs),
3135
],
3236
{ allowFailure: true, timeoutMs: INTENT_PACKAGE_RESOLUTION_TIMEOUT_MS },
3337
);

‎src/daemon/session-lifecycle/internal/__tests__/session-open-runtime.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,38 @@ test('open applies launch-only flags only to the direct app launch before runtim
275275
]);
276276
});
277277

278+
test('Android open forwards component launch overrides to requested app identity resolution', async () => {
279+
const sessionStore = makeSessionStore();
280+
const device = makeAndroidEmulator();
281+
const launchArgs = ['-n', 'com.example.app/.MainActivity'];
282+
mockResolveTargetDevice.mockResolvedValue(device);
283+
const response = await handleSessionCommands({
284+
createCommandSurface: testCreateCommandSurface,
285+
platformServices: daemonPlatformServicesFixture(),
286+
req: {
287+
token: 't',
288+
session: 'intent-override',
289+
command: 'open',
290+
positionals: ['settings'],
291+
flags: { platform: 'android', launchArgs },
292+
},
293+
sessionName: 'intent-override',
294+
logPath: path.join(mkdtempForTestSync('daemon'), 'daemon.log'),
295+
sessionStore,
296+
invoke: noopInvoke,
297+
});
298+
299+
expect(response?.ok).toBe(true);
300+
expect(mockResolveAndroidPackage).toHaveBeenCalledWith(device, 'settings', launchArgs);
301+
expect(mockDispatch).toHaveBeenCalledWith(
302+
device,
303+
'open',
304+
['settings'],
305+
undefined,
306+
expect.objectContaining({ launchArgs }),
307+
);
308+
});
309+
278310
test('open --metro-port alone defaults the host to 10.0.2.2 on an Android emulator', async () => {
279311
const sessionStore = makeSessionStore();
280312
const sessionName = 'runtime-open-port-only-android';

‎src/daemon/session-lifecycle/internal/session-open-prepare.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ export async function prepareOpenCommandDetails(params: {
180180
surface,
181181
openTarget,
182182
existingAppBundleId: existingSession?.appBundleId,
183+
launchArgs: req.flags?.launchArgs,
183184
foreground,
184185
});
185186
if (runtimeHintPlan.clearRemovedRuntimeHints) {
@@ -216,11 +217,13 @@ async function resolvePreparedOpenIdentity(params: {
216217
surface: SessionSurface;
217218
openTarget: string | undefined;
218219
existingAppBundleId?: string;
220+
launchArgs?: readonly string[];
219221
foreground: boolean;
220222
}): Promise<{ appBundleId?: string; appName?: string }> {
221223
const { runtime, surface, openTarget, existingAppBundleId, foreground } = params;
222224
const resolved = await runtime.operations.resolveOpenTarget({
223225
target: openTarget,
226+
launchArgs: params.launchArgs,
224227
currentAppBundleId: existingAppBundleId,
225228
surface,
226229
foreground,

‎src/platform-runtime-android-application-tools.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ async function resolveAndroidOpenTarget(
115115
device,
116116
input.target,
117117
input.currentAppBundleId,
118+
input.launchArgs,
118119
),
119120
appName: input.target,
120121
};

‎src/platform-runtime-open-target.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,25 +165,27 @@ export async function resolveSessionAppBundleIdForTarget(
165165
device: DeviceInfo,
166166
openTarget: string | undefined,
167167
currentAppBundleId: string | undefined,
168+
launchArgs?: readonly string[],
168169
): Promise<string | undefined> {
169170
if (device.platform === 'harmonyos') {
170171
return bundleIdFromOpenTarget(openTarget) ?? currentAppBundleId;
171172
}
172173
return (
173174
(await resolveIosBundleIdForOpen(device, openTarget, currentAppBundleId)) ??
174-
(await tryResolveAndroidPackageForOpen(device, openTarget)) ??
175+
(await tryResolveAndroidPackageForOpen(device, openTarget, launchArgs)) ??
175176
(shouldPreserveAndroidPackageContext(device, openTarget) ? currentAppBundleId : undefined)
176177
);
177178
}
178179

179180
async function tryResolveAndroidPackageForOpen(
180181
device: DeviceInfo,
181182
openTarget: string | undefined,
183+
launchArgs?: readonly string[],
182184
): Promise<string | undefined> {
183185
if (device.platform !== 'android' || !openTarget) return undefined;
184186
try {
185187
const { resolveAndroidPackageForOpen } = await loadAndroidMechanics();
186-
return await resolveAndroidPackageForOpen(device, openTarget);
188+
return await resolveAndroidPackageForOpen(device, openTarget, launchArgs);
187189
} catch {
188190
return undefined;
189191
}

0 commit comments

Comments
 (0)