Close dictionary pools when they are not used any more.

Bug: 5156851
Change-Id: Icaba54734eb790b40dc2012aac25df5b2af71dbb
main
Jean Chalard 2011-08-16 19:43:16 +09:00
parent 29ea7b79c7
commit c160373b6a
2 changed files with 39 additions and 3 deletions

View File

@ -16,6 +16,7 @@
package com.android.inputmethod.latin.spellcheck; package com.android.inputmethod.latin.spellcheck;
import android.content.Intent;
import android.content.res.Resources; import android.content.res.Resources;
import android.service.textservice.SpellCheckerService; import android.service.textservice.SpellCheckerService;
import android.service.textservice.SpellCheckerService.Session; import android.service.textservice.SpellCheckerService.Session;
@ -48,7 +49,7 @@ public class AndroidSpellCheckerService extends SpellCheckerService {
private static final int POOL_SIZE = 2; private static final int POOL_SIZE = 2;
private final static String[] emptyArray = new String[0]; private final static String[] emptyArray = new String[0];
private final Map<String, DictionaryPool> mDictionaryPools = private Map<String, DictionaryPool> mDictionaryPools =
Collections.synchronizedMap(new TreeMap<String, DictionaryPool>()); Collections.synchronizedMap(new TreeMap<String, DictionaryPool>());
@Override @Override
@ -104,6 +105,16 @@ public class AndroidSpellCheckerService extends SpellCheckerService {
} }
} }
@Override
public boolean onUnbind(final Intent intent) {
final Map<String, DictionaryPool> oldPools = mDictionaryPools;
mDictionaryPools = Collections.synchronizedMap(new TreeMap<String, DictionaryPool>());
for (DictionaryPool pool : oldPools.values()) {
pool.close();
}
return false;
}
private DictionaryPool getDictionaryPool(final String locale) { private DictionaryPool getDictionaryPool(final String locale) {
DictionaryPool pool = mDictionaryPools.get(locale); DictionaryPool pool = mDictionaryPools.get(locale);
if (null == pool) { if (null == pool) {
@ -167,7 +178,9 @@ public class AndroidSpellCheckerService extends SpellCheckerService {
dictInfo.mDictionary.getWords(composer, suggestionsGatherer, dictInfo.mDictionary.getWords(composer, suggestionsGatherer,
dictInfo.mProximityInfo); dictInfo.mProximityInfo);
isInDict = dictInfo.mDictionary.isValidWord(text); isInDict = dictInfo.mDictionary.isValidWord(text);
mDictionaryPool.offer(dictInfo); if (!mDictionaryPool.offer(dictInfo)) {
Log.e(TAG, "Can't re-insert a dictionary into its pool");
}
} catch (InterruptedException e) { } catch (InterruptedException e) {
// I don't think this can happen. // I don't think this can happen.
return new SuggestionsInfo(0, new String[0]); return new SuggestionsInfo(0, new String[0]);

View File

@ -28,7 +28,8 @@ public class DictionaryPool extends LinkedBlockingQueue<DictAndProximity> {
private final AndroidSpellCheckerService mService; private final AndroidSpellCheckerService mService;
private final int mMaxSize; private final int mMaxSize;
private final Locale mLocale; private final Locale mLocale;
private int mSize = 0; private int mSize;
private volatile boolean mClosed;
public DictionaryPool(final int maxSize, final AndroidSpellCheckerService service, public DictionaryPool(final int maxSize, final AndroidSpellCheckerService service,
final Locale locale) { final Locale locale) {
@ -36,6 +37,8 @@ public class DictionaryPool extends LinkedBlockingQueue<DictAndProximity> {
mMaxSize = maxSize; mMaxSize = maxSize;
mService = service; mService = service;
mLocale = locale; mLocale = locale;
mSize = 0;
mClosed = false;
} }
@Override @Override
@ -52,4 +55,24 @@ public class DictionaryPool extends LinkedBlockingQueue<DictAndProximity> {
} }
} }
} }
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();
return false;
} else {
return super.offer(dict);
}
}
} }