diff options
| author | Lukas Zilka <zilka@google.com> | 2018-03-14 11:06:26 +0100 |
|---|---|---|
| committer | Lukas Zilka <zilka@google.com> | 2018-03-21 12:25:41 +0100 |
| commit | 0fcacdddf466b22cc4fdbb3a16992bc34686cacb (patch) | |
| tree | 355eaba076455a1482fb59011be63f806e7cf979 /core/java/android | |
| parent | d854d917ecf7826ab5a48c62d0d8496c0b399d6e (diff) | |
Adds support for a fall-back model when the language-specific model is not found.
Bug: 74720318
Test: Builds and fall-back works.
Test: bit FrameworksCoreTests:android.view.textclassifier.TextClassificationManagerTest
Test: bit CtsViewTestCases:android.view.textclassifier.cts.TextClassificationManagerTest
Change-Id: I5491af628c8406e42ca2cc971ad3a8b708ceb315
Diffstat (limited to 'core/java/android')
| -rw-r--r-- | core/java/android/view/textclassifier/TextClassifierImpl.java | 34 |
1 files changed, 28 insertions, 6 deletions
diff --git a/core/java/android/view/textclassifier/TextClassifierImpl.java b/core/java/android/view/textclassifier/TextClassifierImpl.java index a09982053091..89e6262257c5 100644 --- a/core/java/android/view/textclassifier/TextClassifierImpl.java +++ b/core/java/android/view/textclassifier/TextClassifierImpl.java @@ -355,12 +355,10 @@ public final class TextClassifierImpl implements TextClassifier { final List<Locale.LanguageRange> languageRangeList = Locale.LanguageRange.parse(languages); ModelFile bestModel = null; - int bestModelVersion = -1; for (ModelFile model : listAllModelsLocked()) { if (model.isAnyLanguageSupported(languageRangeList)) { - if (model.getVersion() >= bestModelVersion) { + if (model.isPreferredTo(bestModel)) { bestModel = model; - bestModelVersion = model.getVersion(); } } } @@ -482,6 +480,7 @@ public final class TextClassifierImpl implements TextClassifier { private final String mName; private final int mVersion; private final List<Locale> mSupportedLocales; + private final boolean mLanguageIndependent; /** Returns null if the path did not point to a compatible model. */ static @Nullable ModelFile fromPath(String path) { @@ -496,12 +495,14 @@ public final class TextClassifierImpl implements TextClassifier { Log.d(DEFAULT_LOG_TAG, "Ignoring " + file.getAbsolutePath()); return null; } + final boolean languageIndependent = supportedLocalesStr.equals("*"); final List<Locale> supportedLocales = new ArrayList<>(); for (String langTag : supportedLocalesStr.split(",")) { supportedLocales.add(Locale.forLanguageTag(langTag)); } closeAndLogError(modelFd); - return new ModelFile(path, file.getName(), version, supportedLocales); + return new ModelFile(path, file.getName(), version, supportedLocales, + languageIndependent); } catch (FileNotFoundException e) { Log.e(DEFAULT_LOG_TAG, "Failed to peek " + file.getAbsolutePath(), e); return null; @@ -525,7 +526,7 @@ public final class TextClassifierImpl implements TextClassifier { /** Returns whether the language supports any language in the given ranges. */ boolean isAnyLanguageSupported(List<Locale.LanguageRange> languageRanges) { - return Locale.lookup(languageRanges, mSupportedLocales) != null; + return mLanguageIndependent || Locale.lookup(languageRanges, mSupportedLocales) != null; } /** All locales supported by the model. */ @@ -533,6 +534,25 @@ public final class TextClassifierImpl implements TextClassifier { return Collections.unmodifiableList(mSupportedLocales); } + public boolean isPreferredTo(ModelFile model) { + // A model is preferred to no model. + if (model == null) { + return true; + } + + // A language-specific model is preferred to a language independent + // model. + if (!mLanguageIndependent && model.mLanguageIndependent) { + return true; + } + + // A higher-version model is preferred. + if (getVersion() > model.getVersion()) { + return true; + } + return false; + } + @Override public boolean equals(Object other) { if (this == other) { @@ -555,11 +575,13 @@ public final class TextClassifierImpl implements TextClassifier { mPath, mName, mVersion, localesJoiner.toString()); } - private ModelFile(String path, String name, int version, List<Locale> supportedLocales) { + private ModelFile(String path, String name, int version, List<Locale> supportedLocales, + boolean languageIndependent) { mPath = path; mName = name; mVersion = version; mSupportedLocales = supportedLocales; + mLanguageIndependent = languageIndependent; } } |
