Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6fc4318b authored by Tony Mak's avatar Tony Mak
Browse files

Switch to a better model file if any

The logic is already in annotator, just applies the same logic in
langid and actions.

Test: Change locale to non-english. Observe no more suggestion
      on notification.

FIXES: 122912634

Change-Id: Icf80a1e3edf04db1947fe98c598ecfca3ed6139f
parent 9a3ebed8
Loading
Loading
Loading
Loading
+21 −13
Original line number Original line Diff line number Diff line
@@ -84,12 +84,17 @@ public final class TextClassifierImpl implements TextClassifier {
    private final GenerateLinksLogger mGenerateLinksLogger;
    private final GenerateLinksLogger mGenerateLinksLogger;


    private final Object mLock = new Object();
    private final Object mLock = new Object();

    @GuardedBy("mLock") // Do not access outside this lock.
    @GuardedBy("mLock") // Do not access outside this lock.
    private ModelFileManager.ModelFile mAnnotatorModelInUse;
    private ModelFileManager.ModelFile mAnnotatorModelInUse;
    @GuardedBy("mLock") // Do not access outside this lock.
    @GuardedBy("mLock") // Do not access outside this lock.
    private AnnotatorModel mAnnotatorImpl;
    private AnnotatorModel mAnnotatorImpl;

    @GuardedBy("mLock") // Do not access outside this lock.
    private ModelFileManager.ModelFile mLangIdModelInUse;
    @GuardedBy("mLock") // Do not access outside this lock.
    @GuardedBy("mLock") // Do not access outside this lock.
    private LangIdModel mLangIdImpl;
    private LangIdModel mLangIdImpl;

    @GuardedBy("mLock") // Do not access outside this lock.
    @GuardedBy("mLock") // Do not access outside this lock.
    private ModelFileManager.ModelFile mActionModelInUse;
    private ModelFileManager.ModelFile mActionModelInUse;
    @GuardedBy("mLock") // Do not access outside this lock.
    @GuardedBy("mLock") // Do not access outside this lock.
@@ -504,17 +509,19 @@ public final class TextClassifierImpl implements TextClassifier {


    private LangIdModel getLangIdImpl() throws FileNotFoundException {
    private LangIdModel getLangIdImpl() throws FileNotFoundException {
        synchronized (mLock) {
        synchronized (mLock) {
            if (mLangIdImpl == null) {
            final ModelFileManager.ModelFile bestModel =
            final ModelFileManager.ModelFile bestModel =
                    mLangIdModelFileManager.findBestModelFile(null);
                    mLangIdModelFileManager.findBestModelFile(null);
            if (bestModel == null) {
            if (bestModel == null) {
                throw new FileNotFoundException("No LangID model is found");
                throw new FileNotFoundException("No LangID model is found");
            }
            }
            if (mLangIdImpl == null || !Objects.equals(mLangIdModelInUse, bestModel)) {
                Log.d(DEFAULT_LOG_TAG, "Loading " + bestModel);
                final ParcelFileDescriptor pfd = ParcelFileDescriptor.open(
                final ParcelFileDescriptor pfd = ParcelFileDescriptor.open(
                        new File(bestModel.getPath()), ParcelFileDescriptor.MODE_READ_ONLY);
                        new File(bestModel.getPath()), ParcelFileDescriptor.MODE_READ_ONLY);
                try {
                try {
                    if (pfd != null) {
                    if (pfd != null) {
                        mLangIdImpl = new LangIdModel(pfd.getFd());
                        mLangIdImpl = new LangIdModel(pfd.getFd());
                        mLangIdModelInUse = bestModel;
                    }
                    }
                } finally {
                } finally {
                    maybeCloseAndLogError(pfd);
                    maybeCloseAndLogError(pfd);
@@ -527,13 +534,14 @@ public final class TextClassifierImpl implements TextClassifier {
    @Nullable
    @Nullable
    private ActionsSuggestionsModel getActionsImpl() throws FileNotFoundException {
    private ActionsSuggestionsModel getActionsImpl() throws FileNotFoundException {
        synchronized (mLock) {
        synchronized (mLock) {
            if (mActionsImpl == null) {
            // TODO: Use LangID to determine the locale we should use here?
            // TODO: Use LangID to determine the locale we should use here?
            final ModelFileManager.ModelFile bestModel =
            final ModelFileManager.ModelFile bestModel =
                    mActionsModelFileManager.findBestModelFile(LocaleList.getDefault());
                    mActionsModelFileManager.findBestModelFile(LocaleList.getDefault());
            if (bestModel == null) {
            if (bestModel == null) {
                return null;
                return null;
            }
            }
            if (mActionsImpl == null || !Objects.equals(mActionModelInUse, bestModel)) {
                Log.d(DEFAULT_LOG_TAG, "Loading " + bestModel);
                final ParcelFileDescriptor pfd = ParcelFileDescriptor.open(
                final ParcelFileDescriptor pfd = ParcelFileDescriptor.open(
                        new File(bestModel.getPath()), ParcelFileDescriptor.MODE_READ_ONLY);
                        new File(bestModel.getPath()), ParcelFileDescriptor.MODE_READ_ONLY);
                try {
                try {