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

Commit 9d95a996 authored by Tadashi G. Takaoka's avatar Tadashi G. Takaoka Committed by Android (Google) Code Review
Browse files

Merge "Add language suffix to main dictionary"

parents ab0d0d8a 78ab8084
Loading
Loading
Loading
Loading
+0 −3
Original line number Diff line number Diff line
@@ -17,11 +17,8 @@
package com.android.inputmethod.latin;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.content.res.Resources;

import com.android.inputmethod.keyboard.ProximityInfo;
import com.android.inputmethod.latin.LocaleUtils.RunInLocale;

import java.util.Arrays;
import java.util.Locale;
+3 −13
Original line number Diff line number Diff line
@@ -20,11 +20,8 @@ import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.AssetFileDescriptor;
import android.content.res.Resources;
import android.util.Log;

import com.android.inputmethod.latin.LocaleUtils.RunInLocale;

import java.io.File;
import java.util.ArrayList;
import java.util.Locale;
@@ -155,14 +152,8 @@ class BinaryDictionaryGetter {
     * Returns a file address from a resource, or null if it cannot be opened.
     */
    private static AssetFileAddress loadFallbackResource(final Context context,
            final int fallbackResId, final Locale locale) {
        final RunInLocale<AssetFileDescriptor> job = new RunInLocale<AssetFileDescriptor>() {
            @Override
            protected AssetFileDescriptor job(Resources res) {
                return res.openRawResourceFd(fallbackResId);
            }
        };
        final AssetFileDescriptor afd = job.runInLocale(context.getResources(), locale);
            final int fallbackResId) {
        final AssetFileDescriptor afd = context.getResources().openRawResourceFd(fallbackResId);
        if (afd == null) {
            Log.e(TAG, "Found the resource but cannot read it. Is it compressed? resId="
                    + fallbackResId);
@@ -299,8 +290,7 @@ class BinaryDictionaryGetter {
        }

        if (!foundMainDict && dictPackSettings.isWordListActive(mainDictId)) {
            final AssetFileAddress fallbackAsset = loadFallbackResource(context, fallbackResId,
                    locale);
            final AssetFileAddress fallbackAsset = loadFallbackResource(context, fallbackResId);
            if (null != fallbackAsset) {
                fileList.add(fallbackAsset);
            }
+45 −44
Original line number Diff line number Diff line
@@ -21,8 +21,6 @@ import android.content.res.AssetFileDescriptor;
import android.content.res.Resources;
import android.util.Log;

import com.android.inputmethod.latin.LocaleUtils.RunInLocale;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedList;
@@ -101,13 +99,7 @@ public class DictionaryFactory {
            final int resId, final Locale locale) {
        AssetFileDescriptor afd = null;
        try {
            final RunInLocale<AssetFileDescriptor> job = new RunInLocale<AssetFileDescriptor>() {
                @Override
                protected AssetFileDescriptor job(Resources res) {
                    return res.openRawResourceFd(resId);
                }
            };
            afd = job.runInLocale(context.getResources(), locale);
            afd = context.getResources().openRawResourceFd(resId);
            if (afd == null) {
                Log.e(TAG, "Found the resource but it is compressed. resId=" + resId);
                return null;
@@ -163,10 +155,8 @@ public class DictionaryFactory {
     * @return whether a (non-placeholder) dictionary is available or not.
     */
    public static boolean isDictionaryAvailable(Context context, Locale locale) {
        final RunInLocale<Boolean> job = new RunInLocale<Boolean>() {
            @Override
            protected Boolean job(Resources res) {
                final int resourceId = getMainDictionaryResourceId(res);
        final Resources res = context.getResources();
        final int resourceId = getMainDictionaryResourceId(res, locale);
        final AssetFileDescriptor afd = res.openRawResourceFd(resourceId);
        final boolean hasDictionary = isFullDictionary(afd);
        try {
@@ -176,16 +166,11 @@ public class DictionaryFactory {
        }
        return hasDictionary;
    }
        };
        return job.runInLocale(context.getResources(), locale);
    }

    // TODO: Do not use the size of the dictionary as an unique dictionary ID.
    public static Long getDictionaryId(final Context context, final Locale locale) {
        final RunInLocale<Long> job = new RunInLocale<Long>() {
            @Override
            protected Long job(Resources res) {
                final int resourceId = getMainDictionaryResourceId(res);
        final Resources res = context.getResources();
        final int resourceId = getMainDictionaryResourceId(res, locale);
        final AssetFileDescriptor afd = res.openRawResourceFd(resourceId);
        final Long size = (afd != null && afd.getLength() > PLACEHOLDER_LENGTH)
                ? afd.getLength()
@@ -196,9 +181,6 @@ public class DictionaryFactory {
        }
        return size;
    }
        };
        return job.runInLocale(context.getResources(), locale);
    }

    // TODO: Find the Right Way to find out whether the resource is a placeholder or not.
    // Suggestion : strip the locale, open the placeholder file and store its offset.
@@ -214,13 +196,32 @@ public class DictionaryFactory {
        return (afd != null && afd.getLength() > PLACEHOLDER_LENGTH);
    }

    private static final String DEFAULT_MAIN_DICT = "main";
    private static final String MAIN_DICT_PREFIX = "main_";

    /**
     * Returns a main dictionary resource id
     * @param locale dictionary locale
     * @return main dictionary resource id
     */
    public static int getMainDictionaryResourceId(Resources res) {
        final String MAIN_DIC_NAME = "main";
        String packageName = LatinIME.class.getPackage().getName();
        return res.getIdentifier(MAIN_DIC_NAME, "raw", packageName);
    public static int getMainDictionaryResourceId(Resources res, Locale locale) {
        final String packageName = LatinIME.class.getPackage().getName();
        int resId;

        // Try to find main_language_country dictionary.
        if (!locale.getCountry().isEmpty()) {
            final String dictLanguageCountry = MAIN_DICT_PREFIX + locale.toString().toLowerCase();
            if ((resId = res.getIdentifier(dictLanguageCountry, "raw", packageName)) != 0) {
                return resId;
            }
        }

        // Try to find main_language dictionary.
        final String dictLanguage = MAIN_DICT_PREFIX + locale.getLanguage();
        if ((resId = res.getIdentifier(dictLanguage, "raw", packageName)) != 0) {
            return resId;
        }

        return res.getIdentifier(DEFAULT_MAIN_DICT, "raw", packageName);
    }
}
+22 −28
Original line number Diff line number Diff line
@@ -493,10 +493,6 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
        final String localeStr = mSubtypeSwitcher.getInputLocaleStr();
        final Locale keyboardLocale = mSubtypeSwitcher.getInputLocale();

        final Context context = this;
        final RunInLocale<Void> job = new RunInLocale<Void>() {
            @Override
            protected Void job(Resources res) {
        final ContactsDictionary oldContactsDictionary;
        if (mSuggest != null) {
            oldContactsDictionary = mSuggest.getContactsDictionary();
@@ -505,25 +501,22 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
            oldContactsDictionary = null;
        }

                int mainDicResId = DictionaryFactory.getMainDictionaryResourceId(res);
                mSuggest = new Suggest(context, mainDicResId, keyboardLocale);
        final int mainDicResId = DictionaryFactory.getMainDictionaryResourceId(
                mResources, keyboardLocale);
        mSuggest = new Suggest(this, mainDicResId, keyboardLocale);
        if (mSettingsValues.mAutoCorrectEnabled) {
            mSuggest.setAutoCorrectionThreshold(mSettingsValues.mAutoCorrectionThreshold);
        }

                mUserDictionary = new UserDictionary(context, localeStr);
        mUserDictionary = new UserDictionary(this, localeStr);
        mSuggest.setUserDictionary(mUserDictionary);
        mIsUserDictionaryAvailable = mUserDictionary.isEnabled();

        resetContactsDictionary(oldContactsDictionary);

                mUserHistoryDictionary
                    = new UserHistoryDictionary(context, localeStr, Suggest.DIC_USER_HISTORY);
        mUserHistoryDictionary = new UserHistoryDictionary(
                this, localeStr, Suggest.DIC_USER_HISTORY);
        mSuggest.setUserHistoryDictionary(mUserHistoryDictionary);
                return null;
            }
        };
        job.runInLocale(mResources, keyboardLocale);
    }

    /**
@@ -560,7 +553,8 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen

    /* package private */ void resetSuggestMainDict() {
        final Locale keyboardLocale = mSubtypeSwitcher.getInputLocale();
        int mainDicResId = DictionaryFactory.getMainDictionaryResourceId(mResources);
        int mainDicResId = DictionaryFactory.getMainDictionaryResourceId(
                mResources, keyboardLocale);
        mSuggest.resetMainDict(this, mainDicResId, keyboardLocale);
    }

+1 −0
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@ public class WhitelistDictionary extends ExpandableDictionary {
    // TODO: Conform to the async load contact of ExpandableDictionary
    public WhitelistDictionary(final Context context, final Locale locale) {
        super(context, Suggest.DIC_WHITELIST);
        // TODO: Move whitelist dictionary into main dictionary.
        final RunInLocale<Void> job = new RunInLocale<Void>() {
            @Override
            protected Void job(Resources res) {
Loading