2011-08-16 08:37:18 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 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
|
2011-08-16 08:37:18 +00:00
|
|
|
*
|
2013-01-21 12:52:57 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-08-16 08:37:18 +00:00
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
2013-01-21 12:52:57 +00:00
|
|
|
* 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.
|
2011-08-16 08:37:18 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.inputmethod.latin.spellcheck;
|
|
|
|
|
2012-08-14 06:11:53 +00:00
|
|
|
import android.util.Log;
|
|
|
|
|
2012-08-15 07:09:38 +00:00
|
|
|
import com.android.inputmethod.keyboard.ProximityInfo;
|
2012-08-21 07:34:55 +00:00
|
|
|
import com.android.inputmethod.latin.CollectionUtils;
|
2012-08-15 07:09:38 +00:00
|
|
|
import com.android.inputmethod.latin.Dictionary;
|
|
|
|
import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
|
|
|
|
import com.android.inputmethod.latin.WordComposer;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2011-08-16 08:37:18 +00:00
|
|
|
import java.util.Locale;
|
|
|
|
import java.util.concurrent.LinkedBlockingQueue;
|
2012-08-14 06:11:53 +00:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2011-08-16 08:37:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A blocking queue that creates dictionaries up to a certain limit as necessary.
|
2012-08-14 06:11:53 +00:00
|
|
|
* As a deadlock-detecting device, if waiting for more than TIMEOUT = 3 seconds, we
|
|
|
|
* will clear the queue and generate its contents again. This is transparent for
|
|
|
|
* the client code, but may help with sloppy clients.
|
2011-08-16 08:37:18 +00:00
|
|
|
*/
|
2011-10-04 01:54:23 +00:00
|
|
|
@SuppressWarnings("serial")
|
2012-09-27 09:16:16 +00:00
|
|
|
public final class DictionaryPool extends LinkedBlockingQueue<DictAndProximity> {
|
2012-08-14 06:11:53 +00:00
|
|
|
private final static String TAG = DictionaryPool.class.getSimpleName();
|
|
|
|
// How many seconds we wait for a dictionary to become available. Past this delay, we give up in
|
|
|
|
// fear some bug caused a deadlock, and reset the whole pool.
|
|
|
|
private final static int TIMEOUT = 3;
|
2011-08-16 08:37:18 +00:00
|
|
|
private final AndroidSpellCheckerService mService;
|
|
|
|
private final int mMaxSize;
|
|
|
|
private final Locale mLocale;
|
2011-08-16 10:43:16 +00:00
|
|
|
private int mSize;
|
|
|
|
private volatile boolean mClosed;
|
2012-08-21 07:34:55 +00:00
|
|
|
final static ArrayList<SuggestedWordInfo> noSuggestions = CollectionUtils.newArrayList();
|
2012-08-15 07:09:38 +00:00
|
|
|
private final static DictAndProximity dummyDict = new DictAndProximity(
|
|
|
|
new Dictionary(Dictionary.TYPE_MAIN) {
|
|
|
|
@Override
|
|
|
|
public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer,
|
2012-10-03 06:19:43 +00:00
|
|
|
final String prevWord, final ProximityInfo proximityInfo) {
|
2012-08-15 07:09:38 +00:00
|
|
|
return noSuggestions;
|
|
|
|
}
|
|
|
|
@Override
|
2012-10-03 06:19:43 +00:00
|
|
|
public boolean isValidWord(final String word) {
|
2012-08-15 07:09:38 +00:00
|
|
|
// This is never called. However if for some strange reason it ever gets
|
|
|
|
// called, returning true is less destructive (it will not underline the
|
|
|
|
// word in red).
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}, null);
|
|
|
|
|
|
|
|
static public boolean isAValidDictionary(final DictAndProximity dictInfo) {
|
|
|
|
return null != dictInfo && dummyDict != dictInfo;
|
|
|
|
}
|
2011-08-16 08:37:18 +00:00
|
|
|
|
|
|
|
public DictionaryPool(final int maxSize, final AndroidSpellCheckerService service,
|
|
|
|
final Locale locale) {
|
|
|
|
super();
|
|
|
|
mMaxSize = maxSize;
|
|
|
|
mService = service;
|
|
|
|
mLocale = locale;
|
2011-08-16 10:43:16 +00:00
|
|
|
mSize = 0;
|
|
|
|
mClosed = false;
|
2011-08-16 08:37:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-08-14 06:11:53 +00:00
|
|
|
public DictAndProximity poll(final long timeout, final TimeUnit unit)
|
|
|
|
throws InterruptedException {
|
2011-08-16 08:37:18 +00:00
|
|
|
final DictAndProximity dict = poll();
|
|
|
|
if (null != dict) return dict;
|
|
|
|
synchronized(this) {
|
|
|
|
if (mSize >= mMaxSize) {
|
2012-08-14 06:11:53 +00:00
|
|
|
// Our pool is already full. Wait until some dictionary is ready, or TIMEOUT
|
|
|
|
// expires to avoid a deadlock.
|
|
|
|
final DictAndProximity result = super.poll(timeout, unit);
|
|
|
|
if (null == result) {
|
|
|
|
Log.e(TAG, "Deadlock detected ! Resetting dictionary pool");
|
|
|
|
clear();
|
|
|
|
mSize = 1;
|
|
|
|
return mService.createDictAndProximity(mLocale);
|
|
|
|
} else {
|
|
|
|
return result;
|
|
|
|
}
|
2011-08-16 08:37:18 +00:00
|
|
|
} else {
|
|
|
|
++mSize;
|
|
|
|
return mService.createDictAndProximity(mLocale);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-08-16 10:43:16 +00:00
|
|
|
|
2011-09-16 09:26:02 +00:00
|
|
|
// Convenience method
|
2012-08-14 06:11:53 +00:00
|
|
|
public DictAndProximity pollWithDefaultTimeout() {
|
2011-09-16 09:26:02 +00:00
|
|
|
try {
|
2012-08-14 06:11:53 +00:00
|
|
|
return poll(TIMEOUT, TimeUnit.SECONDS);
|
2011-09-16 09:26:02 +00:00
|
|
|
} catch (InterruptedException e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-16 10:43:16 +00:00
|
|
|
public void close() {
|
|
|
|
synchronized(this) {
|
|
|
|
mClosed = true;
|
|
|
|
for (DictAndProximity dict : this) {
|
|
|
|
dict.mDictionary.close();
|
|
|
|
}
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean offer(final DictAndProximity dict) {
|
|
|
|
if (mClosed) {
|
|
|
|
dict.mDictionary.close();
|
2012-08-15 07:09:38 +00:00
|
|
|
return super.offer(dummyDict);
|
2011-08-16 10:43:16 +00:00
|
|
|
} else {
|
|
|
|
return super.offer(dict);
|
|
|
|
}
|
|
|
|
}
|
2011-08-16 08:37:18 +00:00
|
|
|
}
|