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

Commit 67fd0c24 authored by Jean Chalard's avatar Jean Chalard
Browse files

Remove deprecated classes.

Should we maybe rename back *BinaryDict* to *Dict* in another
change for simplicity?

Change-Id: I8f7dcb78b9bdf1a13ce403a997fbb8619f2b453b
parent e91f32d8
Loading
Loading
Loading
Loading
+0 −178
Original line number Diff line number Diff line
/*
 * Copyright (C) 2009 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.ContentResolver;
import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.os.SystemClock;
import android.provider.BaseColumns;
import android.provider.ContactsContract.Contacts;
import android.text.TextUtils;
import android.util.Log;

import com.android.inputmethod.keyboard.Keyboard;

// TODO: This class is superseded by {@link ContactsBinaryDictionary}. Should be cleaned up.
/**
 * An expandable dictionary that stores the words from Contacts provider.
 *
 * @deprecated Use {@link ContactsBinaryDictionary}.
 */
@Deprecated
public class ContactsDictionary extends ExpandableDictionary {

    private static final String[] PROJECTION = {
        BaseColumns._ID,
        Contacts.DISPLAY_NAME,
    };

    private static final String TAG = "ContactsDictionary";

    /**
     * Frequency for contacts information into the dictionary
     */
    private static final int FREQUENCY_FOR_CONTACTS = 40;
    private static final int FREQUENCY_FOR_CONTACTS_BIGRAM = 90;

    private static final int INDEX_NAME = 1;

    private ContentObserver mObserver;

    private long mLastLoadedContacts;

    public ContactsDictionary(final Context context, final int dicTypeId) {
        super(context, dicTypeId);
        registerObserver(context);
        loadDictionary();
    }

    private synchronized void registerObserver(final Context context) {
        // Perform a managed query. The Activity will handle closing and requerying the cursor
        // when needed.
        if (mObserver != null) return;
        ContentResolver cres = context.getContentResolver();
        cres.registerContentObserver(
                Contacts.CONTENT_URI, true, mObserver = new ContentObserver(null) {
                    @Override
                    public void onChange(boolean self) {
                        setRequiresReload(true);
                    }
                });
    }

    public void reopen(final Context context) {
        registerObserver(context);
    }

    @Override
    public synchronized void close() {
        if (mObserver != null) {
            getContext().getContentResolver().unregisterContentObserver(mObserver);
            mObserver = null;
        }
        super.close();
    }

    @Override
    public void startDictionaryLoadingTaskLocked() {
        long now = SystemClock.uptimeMillis();
        if (mLastLoadedContacts == 0
                || now - mLastLoadedContacts > 30 * 60 * 1000 /* 30 minutes */) {
            super.startDictionaryLoadingTaskLocked();
        }
    }

    @Override
    public void loadDictionaryAsync() {
        try {
            Cursor cursor = getContext().getContentResolver()
                    .query(Contacts.CONTENT_URI, PROJECTION, null, null, null);
            if (cursor != null) {
                addWords(cursor);
            }
        } catch(IllegalStateException e) {
            Log.e(TAG, "Contacts DB is having problems");
        }
        mLastLoadedContacts = SystemClock.uptimeMillis();
    }

    @Override
    public void getBigrams(final WordComposer codes, final CharSequence previousWord,
            final WordCallback callback) {
        // Do not return bigrams from Contacts when nothing was typed.
        if (codes.size() <= 0) return;
        super.getBigrams(codes, previousWord, callback);
    }

    private void addWords(Cursor cursor) {
        clearDictionary();

        final int maxWordLength = getMaxWordLength();
        try {
            if (cursor.moveToFirst()) {
                while (!cursor.isAfterLast()) {
                    String name = cursor.getString(INDEX_NAME);

                    if (name != null && -1 == name.indexOf('@')) {
                        int len = name.length();
                        String prevWord = null;

                        // TODO: Better tokenization for non-Latin writing systems
                        for (int i = 0; i < len; i++) {
                            if (Character.isLetter(name.charAt(i))) {
                                int j;
                                for (j = i + 1; j < len; j++) {
                                    char c = name.charAt(j);

                                    if (!(c == Keyboard.CODE_DASH
                                            || c == Keyboard.CODE_SINGLE_QUOTE
                                            || Character.isLetter(c))) {
                                        break;
                                    }
                                }

                                String word = name.substring(i, j);
                                i = j - 1;

                                // Safeguard against adding really long words. Stack
                                // may overflow due to recursion
                                // Also don't add single letter words, possibly confuses
                                // capitalization of i.
                                final int wordLen = word.length();
                                if (wordLen < maxWordLength && wordLen > 1) {
                                    super.addWord(word, null /* shortcut */,
                                            FREQUENCY_FOR_CONTACTS);
                                    if (!TextUtils.isEmpty(prevWord)) {
                                        super.setBigramAndGetFrequency(prevWord, word,
                                                FREQUENCY_FOR_CONTACTS_BIGRAM);
                                    }
                                    prevWord = word;
                                }
                            }
                        }
                    }
                    cursor.moveToNext();
                }
            }
            cursor.close();
        } catch(IllegalStateException e) {
            Log.e(TAG, "Contacts DB is having problems");
        }
    }
}
+17 −45
Original line number Diff line number Diff line
@@ -103,12 +103,6 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
     */
    private static final String SCHEME_PACKAGE = "package";

    /** 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_VISIBILITY_SHOW_VALUE
@@ -162,8 +156,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
    private boolean mShouldSwitchToLastSubtype = true;

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

@@ -469,7 +462,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
        final Locale subtypeLocale = mSubtypeSwitcher.getCurrentSubtypeLocale();
        final String localeStr = subtypeLocale.toString();

        final Dictionary oldContactsDictionary;
        final ContactsBinaryDictionary oldContactsDictionary;
        if (mSuggest != null) {
            oldContactsDictionary = mSuggest.getContactsDictionary();
            mSuggest.close();
@@ -483,13 +476,8 @@ 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();
        }
        mIsUserDictionaryAvailable = mUserDictionary.isEnabled();
        mSuggest.setUserDictionary(mUserDictionary);

        resetContactsDictionary(oldContactsDictionary);
@@ -510,10 +498,10 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
     *
     * @param oldContactsDictionary an optional dictionary to use, or null
     */
    private void resetContactsDictionary(final Dictionary oldContactsDictionary) {
    private void resetContactsDictionary(final ContactsBinaryDictionary oldContactsDictionary) {
        final boolean shouldSetDictionary = (null != mSuggest && mSettingsValues.mUseContactsDict);

        final Dictionary dictionaryToUse;
        final ContactsBinaryDictionary dictionaryToUse;
        if (!shouldSetDictionary) {
            // Make sure the dictionary is closed. If it is already closed, this is a no-op,
            // so it's safe to call it anyways.
@@ -522,10 +510,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
        } else {
            final Locale locale = mSubtypeSwitcher.getCurrentSubtypeLocale();
            if (null != oldContactsDictionary) {
                if (USE_BINARY_CONTACTS_DICTIONARY) {
                    ContactsBinaryDictionary oldContactsBinaryDictionary =
                            (ContactsBinaryDictionary)oldContactsDictionary;
                    if (!oldContactsBinaryDictionary.mLocale.equals(locale)) {
                if (!oldContactsDictionary.mLocale.equals(locale)) {
                    // If the locale has changed then recreate the contacts dictionary. This
                    // allows locale dependent rules for handling bigram name predictions.
                    oldContactsDictionary.close();
@@ -534,20 +519,11 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
                } else {
                    // Make sure the old contacts dictionary is opened. If it is already open,
                    // this is a no-op, so it's safe to call it anyways.
                        oldContactsBinaryDictionary.reopen(this);
                    oldContactsDictionary.reopen(this);
                    dictionaryToUse = oldContactsDictionary;
                }
            } else {
                    ((ContactsDictionary)oldContactsDictionary).reopen(this);
                    dictionaryToUse = oldContactsDictionary;
                }
            } else {
                if (USE_BINARY_CONTACTS_DICTIONARY) {
                    dictionaryToUse = new ContactsBinaryDictionary(this, Suggest.DIC_CONTACTS,
                            locale);
                } else {
                    dictionaryToUse = new ContactsDictionary(this, Suggest.DIC_CONTACTS);
                }
                dictionaryToUse = new ContactsBinaryDictionary(this, Suggest.DIC_CONTACTS, locale);
            }
        }

@@ -1173,11 +1149,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen

    @Override
    public boolean addWordToDictionary(String word) {
        if (USE_BINARY_USER_DICTIONARY) {
            ((UserBinaryDictionary)mUserDictionary).addWordToUserDictionary(word, 128);
        } else {
            ((UserDictionary)mUserDictionary).addWordToUserDictionary(word, 128);
        }
        mUserDictionary.addWordToUserDictionary(word, 128);
        // Suggestion strip should be updated after the operation of adding word to the
        // user dictionary
        mHandler.postUpdateSuggestions();
+5 −5
Original line number Diff line number Diff line
@@ -66,7 +66,7 @@ public class Suggest implements Dictionary.WordCallback {
    private static final boolean DBG = LatinImeLogger.sDBG;

    private boolean mHasMainDictionary;
    private Dictionary mContactsDict;
    private ContactsBinaryDictionary mContactsDict;
    private WhitelistDictionary mWhiteListDictionary;
    private final ConcurrentHashMap<String, Dictionary> mUnigramDictionaries =
            new ConcurrentHashMap<String, Dictionary>();
@@ -148,7 +148,7 @@ public class Suggest implements Dictionary.WordCallback {
        return mHasMainDictionary;
    }

    public Dictionary getContactsDictionary() {
    public ContactsBinaryDictionary getContactsDictionary() {
        return mContactsDict;
    }

@@ -164,7 +164,7 @@ public class Suggest implements Dictionary.WordCallback {
     * Sets an optional user dictionary resource to be loaded. The user dictionary is consulted
     * before the main dictionary, if set. This refers to the system-managed user dictionary.
     */
    public void setUserDictionary(Dictionary userDictionary) {
    public void setUserDictionary(UserBinaryDictionary userDictionary) {
        addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_USER, userDictionary);
    }

@@ -173,13 +173,13 @@ public class Suggest implements Dictionary.WordCallback {
     * the contacts dictionary by passing null to this method. In this case no contacts dictionary
     * won't be used.
     */
    public void setContactsDictionary(Dictionary contactsDictionary) {
    public void setContactsDictionary(ContactsBinaryDictionary contactsDictionary) {
        mContactsDict = contactsDictionary;
        addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_CONTACTS, contactsDictionary);
        addOrReplaceDictionary(mBigramDictionaries, DICT_KEY_CONTACTS, contactsDictionary);
    }

    public void setUserHistoryDictionary(Dictionary userHistoryDictionary) {
    public void setUserHistoryDictionary(UserHistoryDictionary userHistoryDictionary) {
        addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_USER_HISTORY_UNIGRAM,
                userHistoryDictionary);
        addOrReplaceDictionary(mBigramDictionaries, DICT_KEY_USER_HISTORY_BIGRAM,
+0 −54
Original line number Diff line number Diff line
/*
 * Copyright (C) 2011 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 SynchronouslyLoadedContactsDictionary extends ContactsDictionary {
    private boolean mClosed;

    public SynchronouslyLoadedContactsDictionary(final Context context) {
        super(context, Suggest.DIC_CONTACTS);
        mClosed = false;
    }

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

    @Override
    public synchronized boolean isValidWord(CharSequence word) {
        blockingReloadDictionaryIfRequired();
        return getWordFrequency(word) > -1;
    }

    // Protect against multiple closing
    @Override
    public synchronized void close() {
        // Actually with the current implementation of ContactsDictionary it's safe to close
        // several times, so the following protection is really only for foolproofing
        if (mClosed) return;
        mClosed = true;
        super.close();
    }
}
+0 −56
Original line number Diff line number Diff line
/*
 * Copyright (C) 2011 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 SynchronouslyLoadedUserDictionary extends UserDictionary {
    private boolean mClosed;

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

    public SynchronouslyLoadedUserDictionary(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) {
        blockingReloadDictionaryIfRequired();
        getWordsInner(codes, prevWordForBigrams, callback, proximityInfo);
    }

    @Override
    public synchronized boolean isValidWord(CharSequence word) {
        blockingReloadDictionaryIfRequired();
        return super.isValidWord(word);
    }

    // Protect against multiple closing
    @Override
    public synchronized void close() {
        if (mClosed) return;
        mClosed = true;
        super.close();
    }
}
Loading