|
| 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