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

Commit e6269759 authored by Jean Chalard's avatar Jean Chalard
Browse files

Read the dictionary resource in a more sensical place.

We don't need to pass this down all the way from LatinIME any more.
It fetched be done exactly where it needs to be.

Change-Id: I9f277f9c4f9de70ae755a1334d86c67bbb24c988
parent 9d95a996
Loading
Loading
Loading
Loading
+4 −2
Original line number Diff line number Diff line
@@ -255,13 +255,13 @@ class BinaryDictionaryGetter {
     * - Uses a content provider to get a public dictionary set, as per the protocol described
     *   in BinaryDictionaryFileDumper.
     * If that fails:
     * - Gets a file name from the fallback resource passed as an argument.
     * - Gets a file name from the built-in dictionary for this locale, if any.
     * If that fails:
     * - Returns null.
     * @return The list of addresses of valid dictionary files, or null.
     */
    public static ArrayList<AssetFileAddress> getDictionaryFiles(final Locale locale,
            final Context context, final int fallbackResId) {
            final Context context) {

        // cacheWordListsFromContentProvider returns the list of files it copied to local
        // storage, but we don't really care about what was copied NOW: what we want is the
@@ -290,6 +290,8 @@ class BinaryDictionaryGetter {
        }

        if (!foundMainDict && dictPackSettings.isWordListActive(mainDictId)) {
            final int fallbackResId =
                    DictionaryFactory.getMainDictionaryResourceId(context.getResources(), locale);
            final AssetFileAddress fallbackAsset = loadFallbackResource(context, fallbackResId);
            if (null != fallbackAsset) {
                fileList.add(fallbackAsset);
+10 −14
Original line number Diff line number Diff line
@@ -36,24 +36,22 @@ public class DictionaryFactory {
     * Initializes a dictionary from a dictionary pack, with explicit flags.
     *
     * This searches for a content provider providing a dictionary pack for the specified
     * locale. If none is found, it falls back to using the resource passed as fallBackResId
     * as a dictionary.
     * locale. If none is found, it falls back to the built-in dictionary - if any.
     * @param context application context for reading resources
     * @param locale the locale for which to create the dictionary
     * @param fallbackResId the id of the resource to use as a fallback if no pack is found
     * @param useFullEditDistance whether to use the full edit distance in suggestions
     * @return an initialized instance of DictionaryCollection
     */
    public static DictionaryCollection createDictionaryFromManager(final Context context,
            final Locale locale, final int fallbackResId, final boolean useFullEditDistance) {
            final Locale locale, final boolean useFullEditDistance) {
        if (null == locale) {
            Log.e(TAG, "No locale defined for dictionary");
            return new DictionaryCollection(createBinaryDictionary(context, fallbackResId, locale));
            return new DictionaryCollection(createBinaryDictionary(context, locale));
        }

        final LinkedList<Dictionary> dictList = new LinkedList<Dictionary>();
        final ArrayList<AssetFileAddress> assetFileList =
                BinaryDictionaryGetter.getDictionaryFiles(locale, context, fallbackResId);
                BinaryDictionaryGetter.getDictionaryFiles(locale, context);
        if (null != assetFileList) {
            for (final AssetFileAddress f : assetFileList) {
                final BinaryDictionary binaryDictionary =
@@ -75,17 +73,14 @@ public class DictionaryFactory {
     * Initializes a dictionary from a dictionary pack, with default flags.
     *
     * This searches for a content provider providing a dictionary pack for the specified
     * locale. If none is found, it falls back to using the resource passed as fallBackResId
     * as a dictionary.
     * locale. If none is found, it falls back to the built-in dictionary, if any.
     * @param context application context for reading resources
     * @param locale the locale for which to create the dictionary
     * @param fallbackResId the id of the resource to use as a fallback if no pack is found
     * @return an initialized instance of DictionaryCollection
     */
    public static DictionaryCollection createDictionaryFromManager(final Context context,
            final Locale locale, final int fallbackResId) {
        return createDictionaryFromManager(context, locale, fallbackResId,
                false /* useFullEditDistance */);
            final Locale locale) {
        return createDictionaryFromManager(context, locale, false /* useFullEditDistance */);
    }

    /**
@@ -96,9 +91,10 @@ public class DictionaryFactory {
     * @return an initialized instance of BinaryDictionary
     */
    protected static BinaryDictionary createBinaryDictionary(final Context context,
            final int resId, final Locale locale) {
            final Locale locale) {
        AssetFileDescriptor afd = null;
        try {
            final int resId = getMainDictionaryResourceId(context.getResources(), locale);
            afd = context.getResources().openRawResourceFd(resId);
            if (afd == null) {
                Log.e(TAG, "Found the resource but it is compressed. resId=" + resId);
@@ -115,7 +111,7 @@ public class DictionaryFactory {
            return new BinaryDictionary(context, sourceDir, afd.getStartOffset(), afd.getLength(),
                    false /* useFullEditDistance */, locale);
        } catch (android.content.res.Resources.NotFoundException e) {
            Log.e(TAG, "Could not find the resource. resId=" + resId);
            Log.e(TAG, "Could not find the resource");
            return null;
        } finally {
            if (null != afd) {
+2 −7
Original line number Diff line number Diff line
@@ -501,9 +501,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
            oldContactsDictionary = null;
        }

        final int mainDicResId = DictionaryFactory.getMainDictionaryResourceId(
                mResources, keyboardLocale);
        mSuggest = new Suggest(this, mainDicResId, keyboardLocale);
        mSuggest = new Suggest(this, keyboardLocale);
        if (mSettingsValues.mAutoCorrectEnabled) {
            mSuggest.setAutoCorrectionThreshold(mSettingsValues.mAutoCorrectionThreshold);
        }
@@ -552,10 +550,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
    }

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

    @Override
+6 −8
Original line number Diff line number Diff line
@@ -104,8 +104,8 @@ public class Suggest implements Dictionary.WordCallback {

    private static final int MINIMUM_SAFETY_NET_CHAR_LENGTH = 4;

    public Suggest(final Context context, final int dictionaryResId, final Locale locale) {
        initAsynchronously(context, dictionaryResId, locale);
    public Suggest(final Context context, final Locale locale) {
        initAsynchronously(context, locale);
    }

    /* package for test */ Suggest(final Context context, final File dictionary,
@@ -119,9 +119,8 @@ public class Suggest implements Dictionary.WordCallback {
        addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_WHITELIST, mWhiteListDictionary);
    }

    private void initAsynchronously(final Context context, final int dictionaryResId,
            final Locale locale) {
        resetMainDict(context, dictionaryResId, locale);
    private void initAsynchronously(final Context context, final Locale locale) {
        resetMainDict(context, locale);

        // TODO: read the whitelist and init the pool asynchronously too.
        // initPool should be done asynchronously now that the pool is thread-safe.
@@ -146,14 +145,13 @@ public class Suggest implements Dictionary.WordCallback {
        }
    }

    public void resetMainDict(final Context context, final int dictionaryResId,
            final Locale locale) {
    public void resetMainDict(final Context context, final Locale locale) {
        mMainDict = null;
        new Thread("InitializeBinaryDictionary") {
            @Override
            public void run() {
                final Dictionary newMainDict = DictionaryFactory.createDictionaryFromManager(
                        context, locale, dictionaryResId);
                        context, locale);
                mMainDict = newMainDict;
                addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_MAIN, newMainDict);
                addOrReplaceDictionary(mBigramDictionaries, DICT_KEY_MAIN, newMainDict);
+1 −4
Original line number Diff line number Diff line
@@ -386,11 +386,8 @@ public class AndroidSpellCheckerService extends SpellCheckerService
        final int script = getScriptFromLocale(locale);
        final ProximityInfo proximityInfo = ProximityInfo.createSpellCheckerProximityInfo(
                SpellCheckerProximityInfo.getProximityForScript(script));
        final Resources resources = getResources();
        final int fallbackResourceId = DictionaryFactory.getMainDictionaryResourceId(
                resources, locale);
        final DictionaryCollection dictionaryCollection =
                DictionaryFactory.createDictionaryFromManager(this, locale, fallbackResourceId,
                DictionaryFactory.createDictionaryFromManager(this, locale,
                        true /* useFullEditDistance */);
        final String localeStr = locale.toString();
        Dictionary userDictionary = mUserDictionaries.get(localeStr);