answerPhonePrompt function

Future<String?> answerPhonePrompt(
  1. DeviceHid hid,
  2. String udid, {
  3. required RegExp question,
  4. required String button,
})

Answers a system prompt glint's own action raised on a phone: reads the screen with OCR and taps button when question is up; the question it answered, or null.

Implementation

Future<String?> answerPhonePrompt(DeviceHid hid, String udid,
    {required RegExp question, required String button}) async {
  try {
    final path = '${Directory.systemTemp.path}/glint-prompt-$udid.png';
    await hid.call({'cmd': 'screenshot', 'path': path}, timeout: const Duration(seconds: 10));
    final size = pngSize(path);
    if (size == null) return null;
    final lines = await readText(path);
    final asked = lines.where((l) => question.hasMatch(l.text)).firstOrNull;
    final target = lines.where((l) => l.text.trim().toLowerCase() == button.toLowerCase()).firstOrNull;
    if (asked == null || target == null) return null;
    final x = ((target.x + target.w / 2) / size.$1 * 65535).round();
    final y = ((target.y + target.h / 2) / size.$2 * 65535).round();
    await hid.call({
      'cmd': 'touch',
      'points': [
        [x, y, 0],
        [x, y, 50],
      ],
    });
    return asked.text.trim();
  } on Object {
    return null;
  }
}