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

Commit f6adff62 authored by Tom Ouyang's avatar Tom Ouyang
Browse files

Change to a binary version of the expandable user dictionary.

Bug: 6435677
Change-Id: If83409f699608d443796e64a3c65692ae81b98e6
parent 8ec3a42d
Loading
Loading
Loading
Loading
+2 −6
Original line number Diff line number Diff line
@@ -60,7 +60,7 @@ public class ContactsBinaryDictionary extends ExpandableBinaryDictionary {
    private final boolean mUseFirstLastBigrams;

    public ContactsBinaryDictionary(final Context context, final int dicTypeId, Locale locale) {
        super(context, getFilenameWithLocale(locale), dicTypeId);
        super(context, getFilenameWithLocale(NAME, locale.toString()), dicTypeId);
        mUseFirstLastBigrams = useFirstLastBigramsForLocale(locale);
        registerObserver(context);

@@ -69,10 +69,6 @@ public class ContactsBinaryDictionary extends ExpandableBinaryDictionary {
        loadDictionary();
    }

    private static String getFilenameWithLocale(Locale locale) {
        return NAME + "." + locale.toString() + ".dict";
    }

    private synchronized void registerObserver(final Context context) {
        // Perform a managed query. The Activity will handle closing and requerying the cursor
        // when needed.
@@ -175,7 +171,7 @@ public class ContactsBinaryDictionary extends ExpandableBinaryDictionary {
                // capitalization of i.
                final int wordLen = word.codePointCount(0, word.length());
                if (wordLen < MAX_WORD_LENGTH && wordLen > 1) {
                    super.addWord(word, FREQUENCY_FOR_CONTACTS);
                    super.addWord(word, null /* shortcut */, FREQUENCY_FOR_CONTACTS);
                    if (!TextUtils.isEmpty(prevWord)) {
                        if (mUseFirstLastBigrams) {
                            super.setBigram(prevWord, word, FREQUENCY_FOR_CONTACTS_BIGRAM);
+15 −2
Original line number Diff line number Diff line
@@ -22,11 +22,13 @@ import com.android.inputmethod.keyboard.ProximityInfo;
import com.android.inputmethod.latin.makedict.BinaryDictInputOutput;
import com.android.inputmethod.latin.makedict.FusionDictionary;
import com.android.inputmethod.latin.makedict.FusionDictionary.Node;
import com.android.inputmethod.latin.makedict.FusionDictionary.WeightedString;
import com.android.inputmethod.latin.makedict.UnsupportedFormatException;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.locks.ReentrantLock;

@@ -133,6 +135,10 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
        clearFusionDictionary();
    }

    protected static String getFilenameWithLocale(final String name, final String localeStr) {
        return name + "." + localeStr + ".dict";
    }

    /**
     * Closes and cleans up the binary dictionary.
     */
@@ -166,8 +172,15 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
     */
    // TODO: Create "cache dictionary" to cache fresh words for frequently updated dictionaries,
    // considering performance regression.
    protected void addWord(final String word, final int frequency) {
        mFusionDictionary.add(word, frequency, null /* shortcutTargets */);
    protected void addWord(final String word, final String shortcutTarget, final int frequency) {
        if (shortcutTarget == null) {
            mFusionDictionary.add(word, frequency, null);
        } else {
            // TODO: Do this in the subclass, with this class taking an arraylist.
            final ArrayList<WeightedString> shortcutTargets = new ArrayList<WeightedString>();
            shortcutTargets.add(new WeightedString(shortcutTarget, frequency));
            mFusionDictionary.add(word, frequency, shortcutTargets);
        }
    }

    /**
+17 −4
Original line number Diff line number Diff line
@@ -106,6 +106,9 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
    /** Whether to use the binary version of the contacts dictionary */
    public static final boolean USE_BINARY_CONTACTS_DICTIONARY = true;

    /** Whether to use the binary version of the user dictionary */
    public static final boolean USE_BINARY_USER_DICTIONARY = true;

    // TODO: migrate this to SettingsValues
    private int mSuggestionVisibility;
    private static final int SUGGESTION_VISIBILILTY_SHOW_VALUE
@@ -158,7 +161,8 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
    private boolean mShouldSwitchToLastSubtype = true;

    private boolean mIsMainDictionaryAvailable;
    private UserDictionary mUserDictionary;
    // TODO: revert this back to the concrete class after transition.
    private Dictionary mUserDictionary;
    private UserHistoryDictionary mUserHistoryDictionary;
    private boolean mIsUserDictionaryAvailable;

@@ -476,9 +480,14 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen

        mIsMainDictionaryAvailable = DictionaryFactory.isDictionaryAvailable(this, subtypeLocale);

        if (USE_BINARY_USER_DICTIONARY) {
            mUserDictionary = new UserBinaryDictionary(this, localeStr);
            mIsUserDictionaryAvailable = ((UserBinaryDictionary)mUserDictionary).isEnabled();
        } else {
            mUserDictionary = new UserDictionary(this, localeStr);
            mIsUserDictionaryAvailable = ((UserDictionary)mUserDictionary).isEnabled();
        }
        mSuggest.setUserDictionary(mUserDictionary);
        mIsUserDictionaryAvailable = mUserDictionary.isEnabled();

        resetContactsDictionary(oldContactsDictionary);

@@ -1121,7 +1130,11 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen

    @Override
    public boolean addWordToDictionary(String word) {
        mUserDictionary.addWordToUserDictionary(word, 128);
        if (USE_BINARY_USER_DICTIONARY) {
            ((UserBinaryDictionary)mUserDictionary).addWordToUserDictionary(word, 128);
        } else {
            ((UserDictionary)mUserDictionary).addWordToUserDictionary(word, 128);
        }
        // Suggestion strip should be updated after the operation of adding word to the
        // user dictionary
        mHandler.postUpdateSuggestions();
+0 −1
Original line number Diff line number Diff line
@@ -26,7 +26,6 @@ public class SynchronouslyLoadedContactsBinaryDictionary extends ContactsBinaryD
    public SynchronouslyLoadedContactsBinaryDictionary(final Context context) {
        // TODO: add locale information.
        super(context, Suggest.DIC_CONTACTS, null);
        mClosed = false;
    }

    @Override
+47 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.inputmethod.latin;

import android.content.Context;

import com.android.inputmethod.keyboard.ProximityInfo;

public class SynchronouslyLoadedUserBinaryDictionary extends UserBinaryDictionary {

    public SynchronouslyLoadedUserBinaryDictionary(final Context context, final String locale) {
        this(context, locale, false);
    }

    public SynchronouslyLoadedUserBinaryDictionary(final Context context, final String locale,
            final boolean alsoUseMoreRestrictiveLocales) {
        super(context, locale, alsoUseMoreRestrictiveLocales);
    }

    @Override
    public synchronized void getWords(final WordComposer codes,
            final CharSequence prevWordForBigrams, final WordCallback callback,
            final ProximityInfo proximityInfo) {
        syncReloadDictionaryIfRequired();
        getWordsInner(codes, prevWordForBigrams, callback, proximityInfo);
    }

    @Override
    public synchronized boolean isValidWord(CharSequence word) {
        syncReloadDictionaryIfRequired();
        return isValidWordInner(word);
    }
}
Loading