2009-07-28 23:48:47 +00:00
|
|
|
/*
|
|
|
|
* 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;
|
2009-11-12 02:49:36 +00:00
|
|
|
import android.os.SystemClock;
|
2010-12-10 06:24:28 +00:00
|
|
|
import android.provider.BaseColumns;
|
2009-08-27 18:24:26 +00:00
|
|
|
import android.provider.ContactsContract.Contacts;
|
2010-08-20 05:35:02 +00:00
|
|
|
import android.text.TextUtils;
|
|
|
|
import android.util.Log;
|
2009-07-28 23:48:47 +00:00
|
|
|
|
2011-04-28 06:39:39 +00:00
|
|
|
import com.android.inputmethod.keyboard.Keyboard;
|
|
|
|
|
2009-07-28 23:48:47 +00:00
|
|
|
public class ContactsDictionary extends ExpandableDictionary {
|
2009-08-27 18:24:26 +00:00
|
|
|
|
2009-07-28 23:48:47 +00:00
|
|
|
private static final String[] PROJECTION = {
|
2010-12-10 06:24:28 +00:00
|
|
|
BaseColumns._ID,
|
2009-08-27 18:24:26 +00:00
|
|
|
Contacts.DISPLAY_NAME,
|
2009-07-28 23:48:47 +00:00
|
|
|
};
|
2009-08-27 18:24:26 +00:00
|
|
|
|
2010-08-20 11:09:58 +00:00
|
|
|
private static final String TAG = "ContactsDictionary";
|
|
|
|
|
2010-08-20 05:35:02 +00:00
|
|
|
/**
|
|
|
|
* Frequency for contacts information into the dictionary
|
|
|
|
*/
|
2011-06-10 08:36:12 +00:00
|
|
|
private static final int FREQUENCY_FOR_CONTACTS = 40;
|
2010-08-20 05:35:02 +00:00
|
|
|
private static final int FREQUENCY_FOR_CONTACTS_BIGRAM = 90;
|
|
|
|
|
2009-07-28 23:48:47 +00:00
|
|
|
private static final int INDEX_NAME = 1;
|
2009-08-27 18:24:26 +00:00
|
|
|
|
2009-07-28 23:48:47 +00:00
|
|
|
private ContentObserver mObserver;
|
2009-08-27 18:24:26 +00:00
|
|
|
|
2009-10-08 20:06:37 +00:00
|
|
|
private long mLastLoadedContacts;
|
|
|
|
|
2011-08-09 02:54:10 +00:00
|
|
|
public ContactsDictionary(final Context context, final int dicTypeId) {
|
2010-08-20 05:35:02 +00:00
|
|
|
super(context, dicTypeId);
|
2011-08-09 02:54:10 +00:00
|
|
|
registerObserver(context);
|
|
|
|
loadDictionary();
|
|
|
|
}
|
|
|
|
|
|
|
|
private synchronized void registerObserver(final Context context) {
|
2009-07-28 23:48:47 +00:00
|
|
|
// Perform a managed query. The Activity will handle closing and requerying the cursor
|
|
|
|
// when needed.
|
2011-08-09 02:54:10 +00:00
|
|
|
if (mObserver != null) return;
|
2009-07-28 23:48:47 +00:00
|
|
|
ContentResolver cres = context.getContentResolver();
|
2010-08-20 05:35:02 +00:00
|
|
|
cres.registerContentObserver(
|
2011-08-09 02:54:10 +00:00
|
|
|
Contacts.CONTENT_URI, true, mObserver = new ContentObserver(null) {
|
2010-08-20 05:35:02 +00:00
|
|
|
@Override
|
|
|
|
public void onChange(boolean self) {
|
|
|
|
setRequiresReload(true);
|
|
|
|
}
|
|
|
|
});
|
2011-08-09 02:54:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void reopen(final Context context) {
|
|
|
|
registerObserver(context);
|
2009-07-28 23:48:47 +00:00
|
|
|
}
|
2009-08-27 18:24:26 +00:00
|
|
|
|
2010-08-20 05:35:02 +00:00
|
|
|
@Override
|
2009-07-28 23:48:47 +00:00
|
|
|
public synchronized void close() {
|
|
|
|
if (mObserver != null) {
|
|
|
|
getContext().getContentResolver().unregisterContentObserver(mObserver);
|
|
|
|
mObserver = null;
|
|
|
|
}
|
2010-03-10 19:39:06 +00:00
|
|
|
super.close();
|
2009-07-28 23:48:47 +00:00
|
|
|
}
|
2009-08-27 18:24:26 +00:00
|
|
|
|
2010-03-10 19:39:06 +00:00
|
|
|
@Override
|
|
|
|
public void startDictionaryLoadingTaskLocked() {
|
2009-11-12 02:49:36 +00:00
|
|
|
long now = SystemClock.uptimeMillis();
|
2009-10-08 20:06:37 +00:00
|
|
|
if (mLastLoadedContacts == 0
|
|
|
|
|| now - mLastLoadedContacts > 30 * 60 * 1000 /* 30 minutes */) {
|
2010-03-10 19:39:06 +00:00
|
|
|
super.startDictionaryLoadingTaskLocked();
|
2009-11-12 02:49:36 +00:00
|
|
|
}
|
2009-07-28 23:48:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2010-03-10 19:39:06 +00:00
|
|
|
public void loadDictionaryAsync() {
|
2010-08-20 11:09:58 +00:00
|
|
|
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");
|
2009-11-12 02:49:36 +00:00
|
|
|
}
|
2010-03-10 19:39:06 +00:00
|
|
|
mLastLoadedContacts = SystemClock.uptimeMillis();
|
2009-07-28 23:48:47 +00:00
|
|
|
}
|
|
|
|
|
2011-04-26 05:08:16 +00:00
|
|
|
@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);
|
|
|
|
}
|
|
|
|
|
2009-07-28 23:48:47 +00:00
|
|
|
private void addWords(Cursor cursor) {
|
|
|
|
clearDictionary();
|
|
|
|
|
|
|
|
final int maxWordLength = getMaxWordLength();
|
2010-08-20 11:09:58 +00:00
|
|
|
try {
|
|
|
|
if (cursor.moveToFirst()) {
|
|
|
|
while (!cursor.isAfterLast()) {
|
|
|
|
String name = cursor.getString(INDEX_NAME);
|
|
|
|
|
2011-06-10 08:47:16 +00:00
|
|
|
if (name != null && -1 == name.indexOf('@')) {
|
2010-08-20 11:09:58 +00:00
|
|
|
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);
|
|
|
|
|
2011-04-28 06:39:39 +00:00
|
|
|
if (!(c == Keyboard.CODE_DASH
|
|
|
|
|| c == Keyboard.CODE_SINGLE_QUOTE
|
|
|
|
|| Character.isLetter(c))) {
|
2010-08-20 11:09:58 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-07-30 00:02:20 +00:00
|
|
|
}
|
2009-07-28 23:48:47 +00:00
|
|
|
|
2010-08-20 11:09:58 +00:00
|
|
|
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, FREQUENCY_FOR_CONTACTS);
|
|
|
|
if (!TextUtils.isEmpty(prevWord)) {
|
|
|
|
super.setBigram(prevWord, word,
|
|
|
|
FREQUENCY_FOR_CONTACTS_BIGRAM);
|
|
|
|
}
|
|
|
|
prevWord = word;
|
2010-08-20 05:35:02 +00:00
|
|
|
}
|
2009-07-30 00:02:20 +00:00
|
|
|
}
|
2009-07-28 23:48:47 +00:00
|
|
|
}
|
|
|
|
}
|
2010-08-20 11:09:58 +00:00
|
|
|
cursor.moveToNext();
|
2009-07-28 23:48:47 +00:00
|
|
|
}
|
|
|
|
}
|
2010-08-20 11:09:58 +00:00
|
|
|
cursor.close();
|
|
|
|
} catch(IllegalStateException e) {
|
|
|
|
Log.e(TAG, "Contacts DB is having problems");
|
2009-07-28 23:48:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|