TextClassificationManager: Avoid exposing lock.

Test: CtsViewTestCases passes
Bug: 35040457
Change-Id: Ia27730816f8aa4832a16aa9ebcafac68acb79e27
This commit is contained in:
Abodunrinwa Toki
2017-02-06 19:53:22 +00:00
parent d9b6cf1fe9
commit b89cf026cf
5 changed files with 35 additions and 24 deletions

View File

@@ -41,6 +41,9 @@ public final class TextClassificationManager {
private static final String LOG_TAG = "TextClassificationManager";
private final Object mTextClassifierLock = new Object();
private final Object mLangIdLock = new Object();
private final Context mContext;
// TODO: Implement a way to close the file descriptor.
private ParcelFileDescriptor mFd;
@@ -55,19 +58,21 @@ public final class TextClassificationManager {
/**
* Returns the default text classifier.
*/
public synchronized TextClassifier getDefaultTextClassifier() {
if (mDefault == null) {
try {
mFd = ParcelFileDescriptor.open(
new File("/etc/assistant/smart-selection.model"),
ParcelFileDescriptor.MODE_READ_ONLY);
mDefault = new TextClassifierImpl(mContext, mFd);
} catch (FileNotFoundException e) {
Log.e(LOG_TAG, "Error accessing 'text classifier selection' model file.", e);
mDefault = TextClassifier.NO_OP;
public TextClassifier getDefaultTextClassifier() {
synchronized (mTextClassifierLock) {
if (mDefault == null) {
try {
mFd = ParcelFileDescriptor.open(
new File("/etc/assistant/smart-selection.model"),
ParcelFileDescriptor.MODE_READ_ONLY);
mDefault = new TextClassifierImpl(mContext, mFd);
} catch (FileNotFoundException e) {
Log.e(LOG_TAG, "Error accessing 'text classifier selection' model file.", e);
mDefault = TextClassifier.NO_OP;
}
}
return mDefault;
}
return mDefault;
}
/**
@@ -95,12 +100,14 @@ public final class TextClassificationManager {
return Collections.emptyList();
}
private synchronized LangId getLanguageDetector() {
if (mLangId == null) {
// TODO: Use a file descriptor as soon as we start to depend on a model file
// for language detection.
mLangId = new LangId(0);
private LangId getLanguageDetector() {
synchronized (mLangIdLock) {
if (mLangId == null) {
// TODO: Use a file descriptor as soon as we start to depend on a model file
// for language detection.
mLangId = new LangId(0);
}
return mLangId;
}
return mLangId;
}
}

View File

@@ -60,6 +60,8 @@ final class TextClassifierImpl implements TextClassifier {
private static final String LOG_TAG = "TextClassifierImpl";
private final Object mSmartSelectionLock = new Object();
private final Context mContext;
private final ParcelFileDescriptor mFd;
private SmartSelection mSmartSelection;
@@ -140,11 +142,13 @@ final class TextClassifierImpl implements TextClassifier {
return TextClassifier.NO_OP.getLinks(text, linkMask);
}
private synchronized SmartSelection getSmartSelection() throws FileNotFoundException {
if (mSmartSelection == null) {
mSmartSelection = new SmartSelection(mFd.getFd());
private SmartSelection getSmartSelection() throws FileNotFoundException {
synchronized (mSmartSelectionLock) {
if (mSmartSelection == null) {
mSmartSelection = new SmartSelection(mFd.getFd());
}
return mSmartSelection;
}
return mSmartSelection;
}
private TextClassificationResult createClassificationResult(String type, CharSequence text) {