2012-03-26 13:31:20 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 The Android Open Source Project
|
|
|
|
*
|
2013-01-21 12:52:57 +00:00
|
|
|
* 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
|
2012-03-26 13:31:20 +00:00
|
|
|
*
|
2013-01-21 12:52:57 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-03-26 13:31:20 +00:00
|
|
|
*
|
2013-01-21 12:52:57 +00:00
|
|
|
* 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.
|
2012-03-26 13:31:20 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.inputmethod.latin;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2013-05-08 04:00:37 +00:00
|
|
|
import android.net.Uri;
|
|
|
|
import android.provider.ContactsContract;
|
2012-03-26 13:31:20 +00:00
|
|
|
import android.provider.ContactsContract.Contacts;
|
|
|
|
import android.util.Log;
|
|
|
|
|
2014-10-22 05:04:07 +00:00
|
|
|
import com.android.inputmethod.annotations.ExternallyReferenced;
|
2015-02-09 20:22:47 +00:00
|
|
|
import com.android.inputmethod.latin.ContactsManager.ContactsChangedListener;
|
2014-10-28 12:31:09 +00:00
|
|
|
import com.android.inputmethod.latin.common.StringUtils;
|
2013-12-26 09:26:28 +00:00
|
|
|
import com.android.inputmethod.latin.personalization.AccountUtils;
|
2013-06-23 16:11:32 +00:00
|
|
|
|
2013-12-13 05:48:43 +00:00
|
|
|
import java.io.File;
|
2014-05-08 03:25:32 +00:00
|
|
|
import java.util.ArrayList;
|
2013-05-08 07:08:56 +00:00
|
|
|
import java.util.List;
|
2012-03-26 13:31:20 +00:00
|
|
|
import java.util.Locale;
|
|
|
|
|
2014-11-24 21:48:16 +00:00
|
|
|
import javax.annotation.Nullable;
|
|
|
|
|
2015-02-09 20:22:47 +00:00
|
|
|
public class ContactsBinaryDictionary extends ExpandableBinaryDictionary
|
|
|
|
implements ContactsChangedListener {
|
2012-03-26 13:31:20 +00:00
|
|
|
private static final String TAG = ContactsBinaryDictionary.class.getSimpleName();
|
|
|
|
private static final String NAME = "contacts";
|
|
|
|
|
2013-11-20 05:07:44 +00:00
|
|
|
private static final boolean DEBUG = false;
|
|
|
|
private static final boolean DEBUG_DUMP = false;
|
2012-04-27 06:50:21 +00:00
|
|
|
|
2012-03-26 13:31:20 +00:00
|
|
|
/**
|
|
|
|
* Whether to use "firstname lastname" in bigram predictions.
|
|
|
|
*/
|
|
|
|
private final boolean mUseFirstLastBigrams;
|
2015-02-09 20:22:47 +00:00
|
|
|
private final ContactsManager mContactsManager;
|
2012-03-26 13:31:20 +00:00
|
|
|
|
2014-04-28 03:29:48 +00:00
|
|
|
protected ContactsBinaryDictionary(final Context context, final Locale locale,
|
|
|
|
final File dictFile, final String name) {
|
|
|
|
super(context, getDictName(name, locale, dictFile), locale, Dictionary.TYPE_CONTACTS,
|
2014-02-27 14:21:09 +00:00
|
|
|
dictFile);
|
2015-02-09 20:22:47 +00:00
|
|
|
mUseFirstLastBigrams = ContactsDictionaryUtils.useFirstLastBigramsForLocale(locale);
|
|
|
|
mContactsManager = new ContactsManager(context);
|
|
|
|
mContactsManager.registerForUpdates(this /* listener */);
|
2014-04-28 07:58:27 +00:00
|
|
|
reloadDictionaryIfRequired();
|
2012-03-26 13:31:20 +00:00
|
|
|
}
|
|
|
|
|
2014-10-22 05:04:07 +00:00
|
|
|
// Note: This method is called by {@link DictionaryFacilitator} using Java reflection.
|
|
|
|
@ExternallyReferenced
|
2014-05-01 08:22:23 +00:00
|
|
|
public static ContactsBinaryDictionary getDictionary(final Context context, final Locale locale,
|
2014-11-24 21:48:16 +00:00
|
|
|
final File dictFile, final String dictNamePrefix, @Nullable final String account) {
|
2014-06-10 09:05:10 +00:00
|
|
|
return new ContactsBinaryDictionary(context, locale, dictFile, dictNamePrefix + NAME);
|
2014-05-01 08:22:23 +00:00
|
|
|
}
|
|
|
|
|
2012-03-26 13:31:20 +00:00
|
|
|
@Override
|
|
|
|
public synchronized void close() {
|
2015-02-09 20:22:47 +00:00
|
|
|
mContactsManager.close();
|
2012-03-26 13:31:20 +00:00
|
|
|
super.close();
|
|
|
|
}
|
|
|
|
|
2015-02-09 20:22:47 +00:00
|
|
|
/**
|
|
|
|
* Typically called whenever the dictionary is created for the first time or
|
|
|
|
* recreated when we think that there are updates to the dictionary.
|
|
|
|
* This is called asynchronously.
|
|
|
|
*/
|
2012-03-26 13:31:20 +00:00
|
|
|
@Override
|
2014-02-27 14:21:09 +00:00
|
|
|
public void loadInitialContentsLocked() {
|
|
|
|
loadDeviceAccountsEmailAddressesLocked();
|
|
|
|
loadDictionaryForUriLocked(ContactsContract.Profile.CONTENT_URI);
|
2013-05-08 04:00:37 +00:00
|
|
|
// TODO: Switch this URL to the newer ContactsContract too
|
2014-02-27 14:21:09 +00:00
|
|
|
loadDictionaryForUriLocked(Contacts.CONTENT_URI);
|
2013-05-08 04:00:37 +00:00
|
|
|
}
|
|
|
|
|
2015-02-09 20:22:47 +00:00
|
|
|
/**
|
|
|
|
* Loads device accounts to the dictionary.
|
|
|
|
*/
|
2014-02-27 14:21:09 +00:00
|
|
|
private void loadDeviceAccountsEmailAddressesLocked() {
|
2013-05-08 07:08:56 +00:00
|
|
|
final List<String> accountVocabulary =
|
|
|
|
AccountUtils.getDeviceAccountsEmailAddresses(mContext);
|
|
|
|
if (accountVocabulary == null || accountVocabulary.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (String word : accountVocabulary) {
|
|
|
|
if (DEBUG) {
|
|
|
|
Log.d(TAG, "loadAccountVocabulary: " + word);
|
|
|
|
}
|
2014-02-27 14:21:09 +00:00
|
|
|
runGCIfRequiredLocked(true /* mindsBlockByGC */);
|
2015-02-09 20:22:47 +00:00
|
|
|
addUnigramLocked(word, ContactsDictionaryConstants.FREQUENCY_FOR_CONTACTS,
|
2015-02-10 22:54:38 +00:00
|
|
|
false /* isNotAWord */, false /* isPossiblyOffensive */,
|
2014-02-27 14:21:09 +00:00
|
|
|
BinaryDictionary.NOT_A_VALID_TIMESTAMP);
|
2013-05-08 07:08:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-09 20:22:47 +00:00
|
|
|
/**
|
|
|
|
* Loads data within content providers to the dictionary.
|
|
|
|
*/
|
2014-02-27 14:21:09 +00:00
|
|
|
private void loadDictionaryForUriLocked(final Uri uri) {
|
2015-02-09 20:22:47 +00:00
|
|
|
final ArrayList<String> validNames = mContactsManager.getValidNames(uri);
|
|
|
|
for (final String name : validNames) {
|
|
|
|
addNameLocked(name);
|
2012-03-26 13:31:20 +00:00
|
|
|
}
|
2015-02-09 20:22:47 +00:00
|
|
|
if (uri.equals(Contacts.CONTENT_URI)) {
|
|
|
|
// Since we were able to add content successfully, update the local
|
|
|
|
// state of the manager.
|
|
|
|
mContactsManager.updateLocalState(validNames);
|
2012-03-26 13:31:20 +00:00
|
|
|
}
|
2012-04-27 06:50:21 +00:00
|
|
|
}
|
|
|
|
|
2012-03-26 13:31:20 +00:00
|
|
|
/**
|
|
|
|
* Adds the words in a name (e.g., firstname/lastname) to the binary dictionary along with their
|
|
|
|
* bigrams depending on locale.
|
|
|
|
*/
|
2014-02-27 14:21:09 +00:00
|
|
|
private void addNameLocked(final String name) {
|
2012-06-27 05:35:24 +00:00
|
|
|
int len = StringUtils.codePointCount(name);
|
2015-03-17 18:43:16 +00:00
|
|
|
NgramContext ngramContext = NgramContext.getEmptyPrevWordsContext(
|
|
|
|
BinaryDictionary.MAX_PREV_WORD_COUNT_FOR_N_GRAM);
|
2012-03-26 13:31:20 +00:00
|
|
|
// TODO: Better tokenization for non-Latin writing systems
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
if (Character.isLetter(name.codePointAt(i))) {
|
2015-02-09 20:22:47 +00:00
|
|
|
int end = ContactsDictionaryUtils.getWordEndPosition(name, len, i);
|
2012-04-27 06:50:21 +00:00
|
|
|
String word = name.substring(i, end);
|
2013-11-20 05:07:44 +00:00
|
|
|
if (DEBUG_DUMP) {
|
|
|
|
Log.d(TAG, "addName word = " + word);
|
|
|
|
}
|
2012-04-27 06:50:21 +00:00
|
|
|
i = end - 1;
|
2012-03-26 13:31:20 +00:00
|
|
|
// Don't add single letter words, possibly confuses
|
|
|
|
// capitalization of i.
|
2012-06-27 05:35:24 +00:00
|
|
|
final int wordLen = StringUtils.codePointCount(word);
|
2014-07-28 10:18:04 +00:00
|
|
|
if (wordLen <= MAX_WORD_LENGTH && wordLen > 1) {
|
2013-05-08 04:00:37 +00:00
|
|
|
if (DEBUG) {
|
2014-09-29 01:52:18 +00:00
|
|
|
Log.d(TAG, "addName " + name + ", " + word + ", " + ngramContext);
|
2013-05-08 04:00:37 +00:00
|
|
|
}
|
2014-02-27 14:21:09 +00:00
|
|
|
runGCIfRequiredLocked(true /* mindsBlockByGC */);
|
2015-02-09 20:22:47 +00:00
|
|
|
addUnigramLocked(word,
|
|
|
|
ContactsDictionaryConstants.FREQUENCY_FOR_CONTACTS, false /* isNotAWord */,
|
2014-10-14 03:13:11 +00:00
|
|
|
false /* isPossiblyOffensive */,
|
|
|
|
BinaryDictionary.NOT_A_VALID_TIMESTAMP);
|
2015-02-09 20:22:47 +00:00
|
|
|
if (ngramContext.isValid() && mUseFirstLastBigrams) {
|
2014-02-27 14:21:09 +00:00
|
|
|
runGCIfRequiredLocked(true /* mindsBlockByGC */);
|
2015-02-09 20:22:47 +00:00
|
|
|
addNgramEntryLocked(ngramContext,
|
|
|
|
word,
|
|
|
|
ContactsDictionaryConstants.FREQUENCY_FOR_CONTACTS_BIGRAM,
|
2014-02-27 14:21:09 +00:00
|
|
|
BinaryDictionary.NOT_A_VALID_TIMESTAMP);
|
2012-03-26 13:31:20 +00:00
|
|
|
}
|
2014-09-29 01:52:18 +00:00
|
|
|
ngramContext = ngramContext.getNextNgramContext(
|
|
|
|
new NgramContext.WordInfo(word));
|
2012-03-26 13:31:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-04-27 06:50:21 +00:00
|
|
|
|
2015-02-09 20:22:47 +00:00
|
|
|
@Override
|
|
|
|
public void onContactsChange() {
|
|
|
|
setNeedsToRecreate();
|
2012-04-27 06:50:21 +00:00
|
|
|
}
|
2012-03-26 13:31:20 +00:00
|
|
|
}
|