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

Commit 1586dc6e authored by Jean Chalard's avatar Jean Chalard Committed by Android (Google) Code Review
Browse files

Merge "Fix a bug where the user dict's locale match would be wrong"

parents 14c840fb d97def59
Loading
Loading
Loading
Loading
+33 −5
Original line number Original line Diff line number Diff line
@@ -25,6 +25,7 @@ import android.database.Cursor;
import android.net.Uri;
import android.net.Uri;
import android.os.RemoteException;
import android.os.RemoteException;
import android.provider.UserDictionary.Words;
import android.provider.UserDictionary.Words;
import android.text.TextUtils;


import com.android.inputmethod.keyboard.ProximityInfo;
import com.android.inputmethod.keyboard.ProximityInfo;


@@ -42,10 +43,11 @@ public class UserDictionary extends ExpandableDictionary {
    };
    };


    private ContentObserver mObserver;
    private ContentObserver mObserver;
    private String mLocale;
    final private String mLocale;


    public UserDictionary(Context context, String locale) {
    public UserDictionary(Context context, String locale) {
        super(context, Suggest.DIC_USER);
        super(context, Suggest.DIC_USER);
        if (null == locale) throw new NullPointerException(); // Catch the error earlier
        mLocale = locale;
        mLocale = locale;
        // Perform a managed query. The Activity will handle closing and re-querying the cursor
        // Perform a managed query. The Activity will handle closing and re-querying the cursor
        // when needed.
        // when needed.
@@ -73,9 +75,35 @@ public class UserDictionary extends ExpandableDictionary {


    @Override
    @Override
    public void loadDictionaryAsync() {
    public void loadDictionaryAsync() {
        // Split the locale. For example "en" => ["en"], "de_DE" => ["de", "DE"],
        // "en_US_foo_bar_qux" => ["en", "US", "foo_bar_qux"] because of the limit of 3.
        // This is correct for locale processing.
        // For this example, we'll look at the "en_US_POSIX" case.
        final String[] localeElements =
                TextUtils.isEmpty(mLocale) ? new String[] {} : mLocale.split("_", 3);

        final StringBuilder request = new StringBuilder("(locale is NULL)");
        String localeSoFar = "";
        // At start, localeElements = ["en", "US", "POSIX"] ; localeSoFar = "" ;
        // and request = "(locale is NULL)"
        for (int i = 0; i < localeElements.length; ++i) {
            // i | localeSoFar    | localeElements
            // 0 | ""             | ["en", "US", "POSIX"]
            // 1 | "en_"          | ["en", "US", "POSIX"]
            // 2 | "en_US_"       | ["en", "en_US", "POSIX"]
            localeElements[i] = localeSoFar + localeElements[i];
            localeSoFar = localeElements[i] + "_";
            // i | request
            // 0 | "(locale is NULL)"
            // 1 | "(locale is NULL) or (locale=?)"
            // 2 | "(locale is NULL) or (locale=?) or (locale=?)"
            request.append(" or (locale=?)");
        }
        // At the end, localeElements = ["en", "en_US", "en_US_POSIX"]; localeSoFar = en_US_POSIX_"
        // and request = "(locale is NULL) or (locale=?) or (locale=?) or (locale=?)"
        Cursor cursor = getContext().getContentResolver()
        Cursor cursor = getContext().getContentResolver()
                .query(Words.CONTENT_URI, PROJECTION_QUERY, "(locale IS NULL) or (locale=?)",
                .query(Words.CONTENT_URI, PROJECTION_QUERY, request.toString(),
                        new String[] { mLocale }, null);
                        localeElements, null);
        addWords(cursor);
        addWords(cursor);
    }
    }