Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 83a7330b authored by Guliz Tuncay's avatar Guliz Tuncay
Browse files

Update Settings properly when no Spell Checker is available.

Currently, Settings is not being updated properly when the current spell
checker service is removed or disabled and there is no other spell
checker service available. The correct behavior is that Secure.Settings
should be updated with empty string to indicate there is no selected
spell checker service.

Fixes: 64812014
Test: Manually as follows:
  0. Make sure AOSP keyboard is installed.
  1. adb shell settings put --user 0 secure selected_spell_checker com.android.inputmethod.latin/.spellcheck.AndroidSpellCheckerService
  2. tapas SampleSpellCheckerService
  3. make -j
  4. adb install --user 0  -r out/target/product/generic/system/app/SampleSpellCheckerService/SampleSpellCheckerService.apk
  5. adb shell pm disable com.android.inputmethod.latin
  6. adb shell settings get --user 0 secure selected_spell_checker
      -> com.example.android.samplespellcheckerservice/.SampleSpellCheckerService
  7. adb uninstall com.example.android.samplespellcheckerservice
  8. adb shell settings get --user 0 secure selected_spell_checker
      -> empty
  9. adb root
  10. adb shell settings get --user 0 secure selected_spell_checker
      -> empty
  11. adb shell pm enable com.android.inputmethod.latin
  12. adb shell settings get --user 0 secure selected_spell_checker
Change-Id: I30902a3d94f2ddef33f4a8067799e98322ae3a03
parent b3ce96ea
Loading
Loading
Loading
Loading
+37 −30
Original line number Original line Diff line number Diff line
@@ -122,14 +122,8 @@ public class TextServicesManagerService extends ITextServicesManager.Stub {
        }
        }


        private void putSelectedSpellChecker(@Nullable String sciId) {
        private void putSelectedSpellChecker(@Nullable String sciId) {
            if (TextUtils.isEmpty(sciId)) {
                // OK to coalesce to null, since getSelectedSpellChecker() can take care of the
                // empty data scenario.
                putString(Settings.Secure.SELECTED_SPELL_CHECKER, null);
            } else {
            putString(Settings.Secure.SELECTED_SPELL_CHECKER, sciId);
            putString(Settings.Secure.SELECTED_SPELL_CHECKER, sciId);
        }
        }
        }


        private void putSelectedSpellCheckerSubtype(int hashCode) {
        private void putSelectedSpellCheckerSubtype(int hashCode) {
            putInt(Settings.Secure.SELECTED_SPELL_CHECKER_SUBTYPE, hashCode);
            putInt(Settings.Secure.SELECTED_SPELL_CHECKER_SUBTYPE, hashCode);
@@ -157,8 +151,12 @@ public class TextServicesManagerService extends ITextServicesManager.Stub {
            return mSpellCheckerMap.get(curSpellCheckerId);
            return mSpellCheckerMap.get(curSpellCheckerId);
        }
        }


        public void setCurrentSpellChecker(SpellCheckerInfo sci) {
        public void setCurrentSpellChecker(@Nullable SpellCheckerInfo sci) {
            if (sci != null) {
                putSelectedSpellChecker(sci.getId());
                putSelectedSpellChecker(sci.getId());
            } else {
                putSelectedSpellChecker("");
            }
            putSelectedSpellCheckerSubtype(SpellCheckerSubtype.SUBTYPE_ID_NONE);
            putSelectedSpellCheckerSubtype(SpellCheckerSubtype.SUBTYPE_ID_NONE);
        }
        }


@@ -331,14 +329,12 @@ public class TextServicesManagerService extends ITextServicesManager.Stub {
        SpellCheckerInfo sci = tsd.getCurrentSpellChecker();
        SpellCheckerInfo sci = tsd.getCurrentSpellChecker();
        if (sci == null) {
        if (sci == null) {
            sci = findAvailSystemSpellCheckerLocked(null, tsd);
            sci = findAvailSystemSpellCheckerLocked(null, tsd);
            if (sci != null) {
            // Set the current spell checker if there is one or more system spell checkers
                // Set the current spell checker if there is one or more spell checkers
            // available. In this case, "sci" is the first one in the available spell
            // available. In this case, "sci" is the first one in the available spell
            // checkers.
            // checkers.
            setCurrentSpellCheckerLocked(sci, tsd);
            setCurrentSpellCheckerLocked(sci, tsd);
        }
        }
    }
    }
    }


    private final class TextServicesMonitor extends PackageMonitor {
    private final class TextServicesMonitor extends PackageMonitor {
        @Override
        @Override
@@ -355,9 +351,17 @@ public class TextServicesManagerService extends ITextServicesManager.Stub {
                // TODO: Update for each locale
                // TODO: Update for each locale
                SpellCheckerInfo sci = tsd.getCurrentSpellChecker();
                SpellCheckerInfo sci = tsd.getCurrentSpellChecker();
                tsd.initializeTextServicesData();
                tsd.initializeTextServicesData();
                // If no spell checker is enabled, just return. The user should explicitly
                // If spell checker is disabled, just return. The user should explicitly
                // enable the spell checker.
                // enable the spell checker.
                if (sci == null) return;
                if (!tsd.isSpellCheckerEnabled()) return;

                if (sci == null) {
                    sci = findAvailSystemSpellCheckerLocked(null, tsd);
                    // Set the current spell checker if there is one or more system spell checkers
                    // available. In this case, "sci" is the first one in the available spell
                    // checkers.
                    setCurrentSpellCheckerLocked(sci, tsd);
                } else {
                    final String packageName = sci.getPackageName();
                    final String packageName = sci.getPackageName();
                    final int change = isPackageDisappearing(packageName);
                    final int change = isPackageDisappearing(packageName);
                    if (DBG) Slog.d(TAG, "Changing package name: " + packageName);
                    if (DBG) Slog.d(TAG, "Changing package name: " + packageName);
@@ -365,15 +369,18 @@ public class TextServicesManagerService extends ITextServicesManager.Stub {
                            change == PACKAGE_PERMANENT_CHANGE || change == PACKAGE_TEMPORARY_CHANGE
                            change == PACKAGE_PERMANENT_CHANGE || change == PACKAGE_TEMPORARY_CHANGE
                                    // Package modified
                                    // Package modified
                                    || isPackageModified(packageName)) {
                                    || isPackageModified(packageName)) {
                    SpellCheckerInfo availSci = findAvailSystemSpellCheckerLocked(packageName, tsd);
                        SpellCheckerInfo availSci =
                                findAvailSystemSpellCheckerLocked(packageName, tsd);
                        // Set the spell checker settings if different than before
                        // Set the spell checker settings if different than before
                    if (availSci != null && !availSci.getId().equals(sci.getId())) {
                        if (availSci == null
                                || (availSci != null && !availSci.getId().equals(sci.getId()))) {
                            setCurrentSpellCheckerLocked(availSci, tsd);
                            setCurrentSpellCheckerLocked(availSci, tsd);
                        }
                        }
                    }
                    }
                }
                }
            }
            }
        }
        }
    }


    private boolean bindCurrentSpellCheckerService(
    private boolean bindCurrentSpellCheckerService(
            Intent service, ServiceConnection conn, int flags, @UserIdInt int userId) {
            Intent service, ServiceConnection conn, int flags, @UserIdInt int userId) {
@@ -677,8 +684,8 @@ public class TextServicesManagerService extends ITextServicesManager.Stub {
        }
        }
    }
    }


    private void setCurrentSpellCheckerLocked(SpellCheckerInfo sci, TextServicesData tsd) {
    private void setCurrentSpellCheckerLocked(@Nullable SpellCheckerInfo sci, TextServicesData tsd) {
        final String sciId = sci.getId();
        final String sciId = (sci != null) ? sci.getId() : "";
        if (DBG) {
        if (DBG) {
            Slog.w(TAG, "setCurrentSpellChecker: " + sciId);
            Slog.w(TAG, "setCurrentSpellChecker: " + sciId);
        }
        }