File size: 4,056 Bytes
4a5ef51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import {spawnSync} from "node:child_process"

function output(command, commandArgs) {
  const result = spawnSync(command, commandArgs, {encoding: "utf8"})
  if (result.error) {
    throw result.error
  }
  if (result.status !== 0) {
    throw new Error(result.stderr || `${command} ${commandArgs.join(" ")} failed`)
  }
  return result.stdout
}

export function listAuthorizedDevices() {
  return output("adb", ["devices", "-l"])
    .split(/\r?\n/)
    .slice(1)
    .map((line) => line.trim())
    .filter(Boolean)
    .map((line) => {
      const [serial, state, ...rest] = line.split(/\s+/)
      const details = rest.join(" ")
      const model = details.match(/\bmodel:(\S+)/)?.[1]
      const product = details.match(/\bproduct:(\S+)/)?.[1]
      const device = details.match(/\bdevice:(\S+)/)?.[1]
      return {serial, state, model, product, device, line}
    })
    .filter((entry) => entry.state === "device")
}

/** Mentra Live / ASG glasses — never install the phone example app here. */
export function isGlassesDevice(device) {
  const blob = [
    device.serial,
    device.model,
    device.product,
    device.device,
    device.line,
  ]
    .filter(Boolean)
    .join(" ")
    .toLowerCase()

  return (
    blob.includes("mentralive") ||
    blob.includes("mentra_live") ||
    blob.includes("mentra live") ||
    // MentraOS mobile/scripts/android.mjs uses the same broad "live" rule on adb -l lines.
    /\bmodel:.*live\b/.test(blob) ||
    /\bproduct:.*live\b/.test(blob)
  )
}

export function isEmulator(device) {
  const blob = `${device.serial} ${device.line}`.toLowerCase()
  return blob.includes("emulator") || device.serial.startsWith("emulator-")
}

export function isPhoneTarget(device) {
  return !isEmulator(device) && !isGlassesDevice(device)
}

/**

 * Pick a USB-connected Android phone for expo run:android / android:dev.

 * Skips Mentra Live glasses and emulators unless ALLOW_MENTRA_LIVE=1.

 */
export function resolveAndroidPhoneTarget() {
  const forcedSerial = process.env.ANDROID_SERIAL?.trim()
  const allowGlasses = process.env.ALLOW_MENTRA_LIVE === "1"
  const devices = listAuthorizedDevices()

  if (devices.length === 0) {
    throw new Error("No authorized Android device found. Connect a phone and accept the USB debugging prompt.")
  }

  const skipped = devices.filter((device) => !isPhoneTarget(device))
  if (skipped.length > 0) {
    const list = skipped
      .map((device) => {
        const kind = isGlassesDevice(device) ? "Mentra Live glasses" : "emulator"
        return `  - ${device.serial} (${device.model ?? "unknown"}) — skipped (${kind})`
      })
      .join("\n")
    console.log(`Skipping non-phone targets:\n${list}`)
  }

  if (forcedSerial) {
    const match = devices.find((device) => device.serial === forcedSerial)
    if (!match) {
      throw new Error(`ANDROID_SERIAL=${forcedSerial} is not an attached, authorized Android device.`)
    }
    if (!allowGlasses && !isPhoneTarget(match)) {
      throw new Error(
        `ANDROID_SERIAL=${forcedSerial} is Mentra Live glasses or an emulator. ` +
          "Connect your phone instead, or set ALLOW_MENTRA_LIVE=1 to override.",
      )
    }
    return match
  }

  const phones = devices.filter(isPhoneTarget)

  if (phones.length === 0) {
    const all = devices.map((device) => `  - ${device.serial} (${device.model ?? "unknown"})`).join("\n")
    throw new Error(
      "No Android phone found. Connect your phone via USB and accept debugging.\n" +
        `Connected devices:\n${all}\n` +
        "Tip: ANDROID_SERIAL=<phone-serial> bun run android:dev",
    )
  }

  if (phones.length > 1) {
    const list = phones.map((device) => `  - ${device.serial}  model:${device.model ?? "?"}`).join("\n")
    throw new Error(`Multiple phones connected. Set ANDROID_SERIAL to one of:\n${list}`)
  }

  return phones[0]
}

export function expoDeviceName(device) {
  return device.model || device.serial
}