Skip to content
This repository has been archived by the owner on Jan 18, 2024. It is now read-only.

Commit

Permalink
Activate android emulator window (#3345)
Browse files Browse the repository at this point in the history
* Activate android emulator window

* Update Android.ts
  • Loading branch information
EvanBacon authored Apr 1, 2021
1 parent b8f0255 commit bd05889
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion packages/xdl/src/Android.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ExpoConfig, getConfig, readExpRcAsync } from '@expo/config';
import { AndroidConfig } from '@expo/config-plugins';
import * as osascript from '@expo/osascript';
import spawnAsync from '@expo/spawn-async';
import chalk from 'chalk';
import child_process, { execFileSync } from 'child_process';
Expand Down Expand Up @@ -551,8 +552,45 @@ async function _openUrlAsync({
return openProject;
}

function getUnixPID(port: number | string) {
return execFileSync('lsof', [`-i:${port}`, '-P', '-t', '-sTCP:LISTEN'], {
encoding: 'utf8',
stdio: ['pipe', 'pipe', 'ignore'],
})
.split('\n')[0]
.trim();
}

export async function activateEmulatorWindowAsync(device: Pick<Device, 'type' | 'pid'>) {
if (
// only mac is supported for now.
process.platform !== 'darwin' ||
// can only focus emulators
device.type !== 'emulator'
) {
return;
}

// Google Emulator ID: `emulator-5554` -> `5554`
const androidPid = device.pid!.match(/-(\d+)/)?.[1];
if (!androidPid) {
return;
}
// Unix PID
const pid = getUnixPID(androidPid);

try {
await osascript.execAsync(`
tell application "System Events"
set frontmost of the first process whose unix id is ${pid} to true
end tell`);
} catch {
// noop -- this feature is very specific and subject to failure.
}
}

export async function openAppAsync(
device: Pick<Device, 'pid'>,
device: Pick<Device, 'pid' | 'type'>,
{
packageName,
mainActivity,
Expand Down Expand Up @@ -583,6 +621,8 @@ export async function openAppAsync(
throw new Error(openProject.substring(openProject.indexOf('Error: ')));
}

await activateEmulatorWindowAsync(device);

return openProject;
}

Expand Down Expand Up @@ -635,6 +675,9 @@ async function openUrlAsync({
if (!bootedDevice) {
return;
}

await activateEmulatorWindowAsync(bootedDevice);

device = bootedDevice;

let installedExpo = false;
Expand Down

0 comments on commit bd05889

Please sign in to comment.