From c3e7a4fadec256c6dd3e97602f6627cffc2a5daa Mon Sep 17 00:00:00 2001 From: Abodunrinwa Toki Date: Thu, 9 Feb 2017 22:54:47 +0000 Subject: [PATCH] Introduce a model file for language detection. Test: cts-tradefed run cts-dev -m CtsViewTestCases -t android.view.textclassifier.cts.TextClassificationManagerTest#testLanguageDetection Bug: 34777495 Change-Id: Id4102732d303151839ae54630746f882a98dce29 --- .../TextClassificationManager.java | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/core/java/android/view/textclassifier/TextClassificationManager.java b/core/java/android/view/textclassifier/TextClassificationManager.java index 4daa110648b89..c4d64d14f8e04 100644 --- a/core/java/android/view/textclassifier/TextClassificationManager.java +++ b/core/java/android/view/textclassifier/TextClassificationManager.java @@ -44,8 +44,9 @@ public final class TextClassificationManager { private final Object mLangIdLock = new Object(); private final Context mContext; - // TODO: Implement a way to close the file descriptor. - private ParcelFileDescriptor mFd; + // TODO: Implement a way to close the file descriptors. + private ParcelFileDescriptor mSmartSelectionFd; + private ParcelFileDescriptor mLangIdFd; private TextClassifier mDefault; private LangId mLangId; @@ -61,10 +62,10 @@ public final class TextClassificationManager { synchronized (mTextClassifierLock) { if (mDefault == null) { try { - mFd = ParcelFileDescriptor.open( + mSmartSelectionFd = ParcelFileDescriptor.open( new File("/etc/assistant/smart-selection.model"), ParcelFileDescriptor.MODE_READ_ONLY); - mDefault = new TextClassifierImpl(mContext, mFd); + mDefault = new TextClassifierImpl(mContext, mSmartSelectionFd); } catch (FileNotFoundException e) { Log.e(LOG_TAG, "Error accessing 'text classifier selection' model file.", e); mDefault = TextClassifier.NO_OP; @@ -99,12 +100,13 @@ public final class TextClassificationManager { return Collections.emptyList(); } - private LangId getLanguageDetector() { + private LangId getLanguageDetector() throws FileNotFoundException { 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); + mLangIdFd = ParcelFileDescriptor.open( + new File("/etc/assistant/lang-id.model"), + ParcelFileDescriptor.MODE_READ_ONLY); + mLangId = new LangId(mLangIdFd.getFd()); } return mLangId; }