Skip to content

Commit c055a84

Browse files
committed
Fix ADB bug where app interception wouldn't start from background
Only relevant when the app was already installed, and had been running, but had been disconnected for a while. App moved into background, and wasn't allowed to start the VPN, so just opened instead. Worked after 2nd usage, but it's much nicer if it works OOTB.
1 parent 7eae1fa commit c055a84

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/interceptors/android/adb-commands.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,12 @@ export async function injectSystemCertificate(
177177
await run(adbClient, deviceId, rootCmd.concat('sh', injectionScriptPath));
178178
}
179179

180+
export function bringToFront(
181+
adbClient: adb.AdbClient,
182+
deviceId: string,
183+
activityName: string // Of the form: com.package/com.package.YourActivity
184+
) {
185+
return adbClient.shell(deviceId, [
186+
"am", "start", "--activity-single-top", activityName
187+
]);
188+
}

src/interceptors/android/android-adb-interceptor.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ import {
1616
pushFile,
1717
injectSystemCertificate,
1818
stringAsStream,
19-
hasCertInstalled
19+
hasCertInstalled,
20+
bringToFront
2021
} from './adb-commands';
2122
import { streamLatestApk } from './fetch-apk';
2223

@@ -82,6 +83,14 @@ export class AndroidAdbInterceptor implements Interceptor {
8283
console.log("App installed successfully");
8384
}
8485

86+
// Now that the app is installed, bring it to the front (avoids issues with starting a
87+
// service for the VPN when in the foreground).
88+
await bringToFront(
89+
this.adbClient,
90+
options.deviceId,
91+
'tech.httptoolkit.android.v1/tech.httptoolkit.android.MainActivity'
92+
).catch(reportError); // Not that important, so we continue if this fails somehow
93+
8594
// Build a trigger URL to activate the proxy on the device:
8695
const setupParams = {
8796
addresses: [

0 commit comments

Comments
 (0)