From f5be3802927ccb1f526fdc61a149325e9a5e2587 Mon Sep 17 00:00:00 2001 From: slava Date: Fri, 24 Apr 2026 21:56:20 +0700 Subject: [PATCH] Improve ADB Keyboard activation reliability: - Add retries for `ime enable` to handle delayed IME registration after installation. - Introduce flag to distinguish newly installed keyboards for adaptive retry logic. --- android/adb/AdbUtils.cpp | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/android/adb/AdbUtils.cpp b/android/adb/AdbUtils.cpp index ac2fc18..3e61311 100644 --- a/android/adb/AdbUtils.cpp +++ b/android/adb/AdbUtils.cpp @@ -343,6 +343,7 @@ bool AdbUtils::enableAdbIMEKeyboard(const QString &deviceId) { // Проверяем, установлен ли ADB Keyboard QString installedList; startProcess(adb, {"-s", deviceId, "shell", "pm", "list", "packages", "com.android.adbkeyboard"}, &installedList); + bool freshlyInstalled = false; if (!installedList.contains("com.android.adbkeyboard")) { const QString apkPath = QCoreApplication::applicationDirPath() + "/assets/ADBKeyboard.apk"; qDebug() << "[AdbUtils] ADB Keyboard not installed, installing from:" << apkPath; @@ -352,6 +353,7 @@ bool AdbUtils::enableAdbIMEKeyboard(const QString &deviceId) { return false; } qDebug() << "[AdbUtils] ADB Keyboard installed successfully"; + freshlyInstalled = true; } // Пакет мог быть отключён производителем (pm disable-user). @@ -365,14 +367,28 @@ bool AdbUtils::enableAdbIMEKeyboard(const QString &deviceId) { } } - // ime enable - { + // ime enable с retry: сразу после 'pm install' InputMethodManagerService + // может ещё не подхватить новый IME и возвращать "Unknown input method ... cannot be enabled". + // Ретраим до появления IME в списке всех зарегистрированных (ime list -a). + const int enableAttempts = freshlyInstalled ? 10 : 3; + bool imeEnabled = false; + QString lastErr; + for (int attempt = 0; attempt < enableAttempts; ++attempt) { QString out, err; - if (!startProcess(adb, {"-s", deviceId, "shell", "ime", "enable", imeId}, &out, &err)) { - qWarning() << "[AdbUtils] ime enable failed:" << err.trimmed(); - return false; + if (startProcess(adb, {"-s", deviceId, "shell", "ime", "enable", imeId}, &out, &err)) { + qDebug() << "[AdbUtils] ime enable:" << out.trimmed(); + imeEnabled = true; + break; } - qDebug() << "[AdbUtils] ime enable:" << out.trimmed(); + lastErr = err.trimmed(); + qDebug() << "[AdbUtils] ime enable attempt" << attempt + 1 << "/" << enableAttempts + << "failed:" << lastErr << "— ждём, пока IMMS перечитает IME"; + QThread::msleep(1000); + } + if (!imeEnabled) { + qWarning() << "[AdbUtils] ime enable failed after" << enableAttempts + << "attempts. Last error:" << lastErr; + return false; } // Верификация: IME должен появиться в списке разрешённых