From ed1ed176c990acf90800062d4ba839be5a6c0f60 Mon Sep 17 00:00:00 2001 From: sspanak Date: Wed, 11 Sep 2024 16:33:47 +0300 Subject: [PATCH] now checking if the service became a zombie every time we receive a stop request, instead of trying to guess the problematic cases (attempting to fix the Priviliged Options again) --- .../github/sspanak/tt9/ime/TraditionalT9.java | 50 +++++++++++-------- .../preferences/settings/SettingsStore.java | 3 +- .../sspanak/tt9/util/SystemSettings.java | 1 - 3 files changed, 32 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/io/github/sspanak/tt9/ime/TraditionalT9.java b/app/src/main/java/io/github/sspanak/tt9/ime/TraditionalT9.java index d25cb02b..a6cc7e8d 100644 --- a/app/src/main/java/io/github/sspanak/tt9/ime/TraditionalT9.java +++ b/app/src/main/java/io/github/sspanak/tt9/ime/TraditionalT9.java @@ -23,24 +23,19 @@ import io.github.sspanak.tt9.util.SystemSettings; public class TraditionalT9 extends MainViewHandler { @NonNull private final Handler backgroundTasks = new Handler(Looper.getMainLooper()); - private final Handler deathDetector = new Handler(Looper.getMainLooper()); + private final Handler zombieDetector = new Handler(Looper.getMainLooper()); + private int zombieChecks = 0; @Override public boolean onEvaluateInputViewShown() { super.onEvaluateInputViewShown(); + if (!SystemSettings.isTT9Active(this)) { + return false; + } + setInputField(getCurrentInputConnection(), getCurrentInputEditorInfo()); - - if (!shouldBeVisible()) { - return false; - } - - if (SystemSettings.isTT9Enabled(this)) { - return true; - } else { - onDeath(); - return false; - } + return shouldBeVisible(); } @@ -129,8 +124,6 @@ public class TraditionalT9 extends MainViewHandler { @Override protected boolean onStart(InputConnection connection, EditorInfo field) { - deathDetector.removeCallbacksAndMessages(null); - if (!super.onStart(connection, field)) { return false; } @@ -174,6 +167,10 @@ public class TraditionalT9 extends MainViewHandler { backgroundTasks.removeCallbacksAndMessages(null); backgroundTasks.postDelayed(this::runBackgroundTasks, SettingsStore.WORD_BACKGROUND_TASKS_DELAY); } + + if (zombieChecks == 0) { + startZombieCheck(); + } } @@ -181,19 +178,32 @@ public class TraditionalT9 extends MainViewHandler { * On Android 11 the IME is sometimes not killed when the user switches to a different one. * Here we attempt to detect if we are disabled, then hide and kill ourselves. */ - private void onDeath() { - if (SystemSettings.isTT9Active(this)) { - Logger.w("onDeath", "===> Still active, rescheduling"); - deathDetector.postDelayed(this::onDeath, SettingsStore.ZOMBIE_CHECK_INTERVAL); + private void startZombieCheck() { + if (!SystemSettings.isTT9Active(this)) { + onDeath(); return; + } + + if (++zombieChecks < SettingsStore.ZOMBIE_CHECK_MAX) { + zombieDetector.postDelayed(this::startZombieCheck, SettingsStore.ZOMBIE_CHECK_INTERVAL); } else { - deathDetector.removeCallbacksAndMessages(null); + Logger.d("startZombieCheck", "Not a zombie after " + zombieChecks + " checks"); + zombieChecks = 0; + } + } + + + private void onDeath() { + if (currentInputConnection == null) { + Logger.w("onDeath", "===> Already dead. Nothing to do."); + return; } Logger.w("onDeath", "===> Killing self"); requestHideSelf(0); - onStop(); + setInputField(null, null); backgroundTasks.removeCallbacksAndMessages(null); + zombieDetector.removeCallbacksAndMessages(null); stopSelf(); } diff --git a/app/src/main/java/io/github/sspanak/tt9/preferences/settings/SettingsStore.java b/app/src/main/java/io/github/sspanak/tt9/preferences/settings/SettingsStore.java index 4187dec1..44bb65ac 100644 --- a/app/src/main/java/io/github/sspanak/tt9/preferences/settings/SettingsStore.java +++ b/app/src/main/java/io/github/sspanak/tt9/preferences/settings/SettingsStore.java @@ -36,7 +36,8 @@ public class SettingsStore extends SettingsUI { public final static int WORD_FREQUENCY_NORMALIZATION_DIVIDER = 100; // normalized frequency = WORD_FREQUENCY_MAX / WORD_FREQUENCY_NORMALIZATION_DIVIDER public final static int WORD_PAIR_MAX = 1000; public final static int WORD_PAIR_MAX_WORD_LENGTH = 6; - public final static int ZOMBIE_CHECK_INTERVAL = 666; // ms + public final static int ZOMBIE_CHECK_INTERVAL = 1500; // ms + public final static int ZOMBIE_CHECK_MAX = 2; /************* hacks *************/ public final static int PREFERENCES_CLICK_DEBOUNCE_TIME = 250; // ms diff --git a/app/src/main/java/io/github/sspanak/tt9/util/SystemSettings.java b/app/src/main/java/io/github/sspanak/tt9/util/SystemSettings.java index c1ed94ee..b9c3b70b 100644 --- a/app/src/main/java/io/github/sspanak/tt9/util/SystemSettings.java +++ b/app/src/main/java/io/github/sspanak/tt9/util/SystemSettings.java @@ -33,7 +33,6 @@ public class SystemSettings { inputManager = inputManager == null ? (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE) : inputManager; packageName = packageName == null ? context.getPackageName() : packageName; - for (final InputMethodInfo imeInfo : inputManager.getEnabledInputMethodList()) { if (packageName.equals(imeInfo.getPackageName()) && imeInfo.getId().equals(defaultIME)) { return true;