Skip to content

Commit 2309542

Browse files
committed
fix(android): separate intent resolution from launch controls
1 parent 14de349 commit 2309542

5 files changed

Lines changed: 407 additions & 22 deletions

File tree

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { isDeepLinkTarget } from '@agent-device/contracts/command';
77

88
import { waitForAndroidBoot } from './emulator-lifecycle.ts';
99
import { runAndroidShell } from './adb.ts';
10+
import { buildAndroidIntentArgs } from './intent-arguments.ts';
1011
import {
1112
androidAdbResultError,
1213
createAndroidPortReverseManager,
@@ -192,13 +193,6 @@ function androidLaunchArgs(options: OpenAndroidAppOptions): string[] {
192193
return options.launchArgs ?? [];
193194
}
194195

195-
export function buildAndroidIntentArgs(
196-
intent: string,
197-
launchArgs: readonly string[] = [],
198-
): string[] {
199-
return ['-a', intent, ...launchArgs];
200-
}
201-
202196
export async function openAndroidApp(
203197
device: DeviceInfo,
204198
app: string,
@@ -300,7 +294,7 @@ async function openAndroidIntent(
300294
'am',
301295
'start',
302296
'-W',
303-
...buildAndroidIntentArgs(intent, options.launchArgs),
297+
...buildAndroidIntentArgs(intent, options.launchArgs).launchArgs,
304298
]);
305299
}
306300

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import { expect, test } from 'vitest';
2+
import fc from 'fast-check';
3+
import { buildAndroidIntentArgs } from './intent-arguments.ts';
4+
5+
const ACTION_ARGS = ['-a', 'android.settings.SETTINGS'];
6+
7+
test.each([
8+
{
9+
name: 'attached short values and extras resembling launch options',
10+
args: ['-R2', '-ncom.example/.Main', '-ekey', '--display', '-P-S', '--esn', '-S', '-S'],
11+
intentArgs: ['-ncom.example/.Main', '-ekey', '--display', '--esn', '-S'],
12+
},
13+
{
14+
name: 'selector arguments',
15+
args: ['-S', '--selector', '-a', 'com.example.VIEW', '--display', '0', '-p', 'com.example'],
16+
intentArgs: ['--selector', '-a', 'com.example.VIEW', '-p', 'com.example'],
17+
},
18+
{
19+
name: 'positional component and unparsed trailing tokens',
20+
args: ['-S', 'com.example/.Main', '--display', '0'],
21+
intentArgs: ['com.example/.Main', '--display', '0'],
22+
},
23+
{
24+
name: 'explicit end of options',
25+
args: ['-S', '--', 'com.example/.Main', '-R', '2'],
26+
intentArgs: ['--', 'com.example/.Main', '-R', '2'],
27+
},
28+
])('intent arguments preserve AOSP token boundaries for $name', ({ args, intentArgs }) => {
29+
expect(buildAndroidIntentArgs('android.settings.SETTINGS', args)).toEqual({
30+
launchArgs: [...ACTION_ARGS, ...args],
31+
resolutionArgs: [...ACTION_ARGS, ...intentArgs],
32+
});
33+
});
34+
35+
test.each([
36+
['--future-option', '-p', 'com.other'],
37+
['--display=1'],
38+
['-R'],
39+
['--es', 'key'],
40+
['-Sattached'],
41+
['intent:#Intent;scheme=content;end'],
42+
])('unprojectable launch arguments retain unknown ownership: %j', (...args) => {
43+
expect(buildAndroidIntentArgs('android.settings.SETTINGS', args)).toEqual({
44+
launchArgs: [...ACTION_ARGS, ...args],
45+
resolutionArgs: undefined,
46+
});
47+
});
48+
49+
type ArgumentGroup = { role: 'intent' | 'launch'; tokens: string[] };
50+
51+
const flagLikeValueArb = fc.oneof(
52+
fc.string({ maxLength: 40 }),
53+
fc.constantFrom('-S', '-R', '--display', '--user', '--selector', '--', ''),
54+
);
55+
const argumentGroupArb: fc.Arbitrary<ArgumentGroup> = fc.oneof(
56+
fc
57+
.tuple(fc.constantFrom('-a', '-d', '-t', '-i', '-c', '-p'), flagLikeValueArb)
58+
.map<ArgumentGroup>((tokens) => ({ role: 'intent', tokens })),
59+
fc
60+
.tuple(fc.constantFrom('-e', '--es', '--esa', '--esal'), flagLikeValueArb, flagLikeValueArb)
61+
.map<ArgumentGroup>((tokens) => ({ role: 'intent', tokens })),
62+
flagLikeValueArb.map<ArgumentGroup>((key) => ({ role: 'intent', tokens: ['--esn', key] })),
63+
fc
64+
.constantFrom('-S', '-W', '-D', '--task-overlay')
65+
.map<ArgumentGroup>((flag) => ({ role: 'launch', tokens: [flag] })),
66+
fc
67+
.tuple(fc.constantFrom('-P', '--attach-agent'), flagLikeValueArb)
68+
.map<ArgumentGroup>((tokens) => ({ role: 'launch', tokens })),
69+
fc
70+
.tuple(fc.constantFrom('-R', '--display'), fc.nat({ max: 20 }))
71+
.map<ArgumentGroup>(([flag, value]) => ({ role: 'launch', tokens: [flag, String(value)] })),
72+
);
73+
74+
test('intent projection preserves whole argument groups while launch preserves the original stream', () => {
75+
fc.assert(
76+
fc.property(fc.array(argumentGroupArb, { maxLength: 25 }), (groups) => {
77+
const args = ['-t', 'application/test', ...groups.flatMap((group) => group.tokens)];
78+
const intentArgs = [
79+
'-t',
80+
'application/test',
81+
...groups.filter((group) => group.role === 'intent').flatMap((group) => group.tokens),
82+
];
83+
expect(buildAndroidIntentArgs('android.settings.SETTINGS', args)).toEqual({
84+
launchArgs: [...ACTION_ARGS, ...args],
85+
resolutionArgs: [...ACTION_ARGS, ...intentArgs],
86+
});
87+
}),
88+
);
89+
});
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
type AndroidIntentOption = readonly [audience: 'intent' | 'launch', valueCount: 0 | 1 | 2];
2+
3+
/** AOSP Intent.parseCommandArgs and ActivityManagerShellCommand.makeIntent option arities. */
4+
const ANDROID_INTENT_OPTIONS: Readonly<Record<string, AndroidIntentOption>> = {
5+
'-a': ['intent', 1],
6+
'-d': ['intent', 1],
7+
'-t': ['intent', 1],
8+
'-i': ['intent', 1],
9+
'-c': ['intent', 1],
10+
'-n': ['intent', 1],
11+
'-p': ['intent', 1],
12+
'-f': ['intent', 1],
13+
'-e': ['intent', 2],
14+
'--es': ['intent', 2],
15+
'--esn': ['intent', 1],
16+
'--ei': ['intent', 2],
17+
'--eu': ['intent', 2],
18+
'--ecn': ['intent', 2],
19+
'--eia': ['intent', 2],
20+
'--eial': ['intent', 2],
21+
'--el': ['intent', 2],
22+
'--ela': ['intent', 2],
23+
'--elal': ['intent', 2],
24+
'--ef': ['intent', 2],
25+
'--efa': ['intent', 2],
26+
'--efal': ['intent', 2],
27+
'--ed': ['intent', 2],
28+
'--eda': ['intent', 2],
29+
'--edal': ['intent', 2],
30+
'--esa': ['intent', 2],
31+
'--esal': ['intent', 2],
32+
'--ez': ['intent', 2],
33+
'--grant-read-uri-permission': ['intent', 0],
34+
'--grant-write-uri-permission': ['intent', 0],
35+
'--grant-persistable-uri-permission': ['intent', 0],
36+
'--grant-prefix-uri-permission': ['intent', 0],
37+
'--exclude-stopped-packages': ['intent', 0],
38+
'--include-stopped-packages': ['intent', 0],
39+
'--debug-log-resolution': ['intent', 0],
40+
'--activity-brought-to-front': ['intent', 0],
41+
'--activity-clear-top': ['intent', 0],
42+
'--activity-clear-when-task-reset': ['intent', 0],
43+
'--activity-exclude-from-recents': ['intent', 0],
44+
'--activity-launched-from-history': ['intent', 0],
45+
'--activity-multiple-task': ['intent', 0],
46+
'--activity-no-animation': ['intent', 0],
47+
'--activity-no-history': ['intent', 0],
48+
'--activity-no-user-action': ['intent', 0],
49+
'--activity-previous-is-top': ['intent', 0],
50+
'--activity-reorder-to-front': ['intent', 0],
51+
'--activity-reset-task-if-needed': ['intent', 0],
52+
'--activity-single-top': ['intent', 0],
53+
'--activity-clear-task': ['intent', 0],
54+
'--activity-task-on-home': ['intent', 0],
55+
'--activity-match-external': ['intent', 0],
56+
'--receiver-registered-only': ['intent', 0],
57+
'--receiver-replace-pending': ['intent', 0],
58+
'--receiver-foreground': ['intent', 0],
59+
'--receiver-no-abort': ['intent', 0],
60+
'--receiver-include-background': ['intent', 0],
61+
'--selector': ['intent', 0],
62+
'--user': ['intent', 1],
63+
'-D': ['launch', 0],
64+
'--suspend': ['launch', 0],
65+
'-N': ['launch', 0],
66+
'-W': ['launch', 0],
67+
'-P': ['launch', 1],
68+
'--start-profiler': ['launch', 1],
69+
'--sampling': ['launch', 1],
70+
'--clock-type': ['launch', 1],
71+
'--profiler-output-version': ['launch', 1],
72+
'--streaming': ['launch', 0],
73+
'--attach-agent': ['launch', 1],
74+
'--attach-agent-bind': ['launch', 1],
75+
'-R': ['launch', 1],
76+
'-S': ['launch', 0],
77+
'--track-allocation': ['launch', 0],
78+
'--receiver-permission': ['launch', 1],
79+
'--display': ['launch', 1],
80+
'--task-display-area-feature-id': ['launch', 1],
81+
'--windowingMode': ['launch', 1],
82+
'--activityType': ['launch', 1],
83+
'--task': ['launch', 1],
84+
'--task-overlay': ['launch', 0],
85+
'--lock-task': ['launch', 0],
86+
'--allow-background-activity-starts': ['launch', 0],
87+
'--async': ['launch', 0],
88+
'--splashscreen-show-icon': ['launch', 0],
89+
'--dismiss-keyguard-if-insecure': ['launch', 0],
90+
'--dismiss-keyguard': ['launch', 0],
91+
'--allow-fgs-start-reason': ['launch', 1],
92+
};
93+
94+
type ParsedAndroidIntentOption = {
95+
name: string;
96+
audience: AndroidIntentOption[0];
97+
end: number;
98+
value: string | undefined;
99+
};
100+
101+
function readAndroidIntentOption(
102+
args: readonly string[],
103+
index: number,
104+
token: string,
105+
): ParsedAndroidIntentOption | undefined {
106+
const attachedValue = token.length > 2 && token[1] !== '-';
107+
const name = attachedValue ? token.slice(0, 2) : token;
108+
const option = ANDROID_INTENT_OPTIONS[name];
109+
if (!option || (attachedValue && option[1] === 0)) return undefined;
110+
const end = index + 1 + option[1] - Number(attachedValue);
111+
if (end > args.length) return undefined;
112+
return {
113+
name,
114+
audience: option[0],
115+
end,
116+
value: attachedValue ? token.slice(2) : args[index + 1],
117+
};
118+
}
119+
120+
function canResolvePositionalIntent(token: string | undefined, type: string | undefined): boolean {
121+
return !token?.startsWith('intent:') && !(token?.startsWith('content:') && type === undefined);
122+
}
123+
124+
function needsContentType(data: string | undefined, type: string | undefined): boolean {
125+
// am start resolves content-provider MIME types; package lookup does not.
126+
return data?.startsWith('content:') === true && type === undefined;
127+
}
128+
129+
type AndroidIntentMime = { data?: string; type?: string };
130+
131+
function updateAndroidIntentMime(
132+
mime: AndroidIntentMime,
133+
name: string,
134+
value: string | undefined,
135+
): boolean {
136+
if (name === '-d') mime.data = value;
137+
if (name === '-t') mime.type = value;
138+
return name !== '--selector' || !needsContentType(mime.data, mime.type);
139+
}
140+
141+
function readAndroidIntentArguments(args: readonly string[]):
142+
| {
143+
options: Array<ParsedAndroidIntentOption & { tokens: string[] }>;
144+
positional: string[];
145+
}
146+
| undefined {
147+
const options: Array<ParsedAndroidIntentOption & { tokens: string[] }> = [];
148+
let index = 0;
149+
while (true) {
150+
const token = args[index];
151+
if (token === undefined) return { options, positional: [] };
152+
if (!token.startsWith('-') || token === '--') return { options, positional: args.slice(index) };
153+
const option = readAndroidIntentOption(args, index, token);
154+
if (!option) return undefined;
155+
options.push({ ...option, tokens: args.slice(index, option.end) });
156+
index = option.end;
157+
}
158+
}
159+
160+
function androidIntentResolutionArgs(args: readonly string[]): string[] | undefined {
161+
const parsed = readAndroidIntentArguments(args);
162+
if (!parsed) return undefined;
163+
const resolutionArgs: string[] = [];
164+
const mime: AndroidIntentMime = {};
165+
for (const option of parsed.options) {
166+
if (!updateAndroidIntentMime(mime, option.name, option.value)) return undefined;
167+
if (option.audience === 'intent') resolutionArgs.push(...option.tokens);
168+
}
169+
const positional = parsed.positional[parsed.positional[0] === '--' ? 1 : 0];
170+
if (!canResolvePositionalIntent(positional, mime.type) || needsContentType(mime.data, mime.type))
171+
return undefined;
172+
resolutionArgs.push(...parsed.positional);
173+
return resolutionArgs;
174+
}
175+
176+
/** Launch stays verbatim; resolution receives only complete, recognized intent options. */
177+
export function buildAndroidIntentArgs(
178+
intent: string,
179+
launchArgs: readonly string[] = [],
180+
): { launchArgs: string[]; resolutionArgs: string[] | undefined } {
181+
const resolutionArgs = androidIntentResolutionArgs(launchArgs);
182+
return {
183+
launchArgs: ['-a', intent, ...launchArgs],
184+
resolutionArgs: resolutionArgs && ['-a', intent, ...resolutionArgs],
185+
};
186+
}

0 commit comments

Comments
 (0)