1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.view.textclassifier;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.annotation.SystemService;
22 import android.content.Context;
23 import android.os.ParcelFileDescriptor;
24 import android.util.Log;
25 
26 import com.android.internal.util.Preconditions;
27 
28 import java.io.File;
29 import java.io.FileNotFoundException;
30 import java.util.Arrays;
31 import java.util.Collections;
32 import java.util.List;
33 import java.util.Locale;
34 
35 /**
36  * Interface to the text classification service.
37  */
38 @SystemService(Context.TEXT_CLASSIFICATION_SERVICE)
39 public final class TextClassificationManager {
40 
41     private static final String LOG_TAG = "TextClassificationManager";
42 
43     private final Object mTextClassifierLock = new Object();
44     private final Object mLangIdLock = new Object();
45 
46     private final Context mContext;
47     private ParcelFileDescriptor mLangIdFd;
48     private TextClassifier mTextClassifier;
49     private LangId mLangId;
50 
51     /** @hide */
TextClassificationManager(Context context)52     public TextClassificationManager(Context context) {
53         mContext = Preconditions.checkNotNull(context);
54     }
55 
56     /**
57      * Returns the text classifier.
58      */
getTextClassifier()59     public TextClassifier getTextClassifier() {
60         synchronized (mTextClassifierLock) {
61             if (mTextClassifier == null) {
62                 mTextClassifier = new TextClassifierImpl(mContext);
63             }
64             return mTextClassifier;
65         }
66     }
67 
68     /**
69      * Sets the text classifier.
70      * Set to null to use the system default text classifier.
71      * Set to {@link TextClassifier#NO_OP} to disable text classifier features.
72      */
setTextClassifier(@ullable TextClassifier textClassifier)73     public void setTextClassifier(@Nullable TextClassifier textClassifier) {
74         synchronized (mTextClassifierLock) {
75             mTextClassifier = textClassifier;
76         }
77     }
78 
79     /**
80      * Returns information containing languages that were detected in the provided text.
81      * This is a blocking operation you should avoid calling it on the UI thread.
82      *
83      * @throws IllegalArgumentException if text is null
84      * @hide
85      */
detectLanguages(@onNull CharSequence text)86     public List<TextLanguage> detectLanguages(@NonNull CharSequence text) {
87         Preconditions.checkArgument(text != null);
88         try {
89             if (text.length() > 0) {
90                 final LangId.ClassificationResult[] results =
91                         getLanguageDetector().findLanguages(text.toString());
92                 final TextLanguage.Builder tlBuilder = new TextLanguage.Builder(0, text.length());
93                 final int size = results.length;
94                 for (int i = 0; i < size; i++) {
95                     tlBuilder.setLanguage(
96                             new Locale.Builder().setLanguageTag(results[i].mLanguage).build(),
97                             results[i].mScore);
98                 }
99 
100                 return Collections.unmodifiableList(Arrays.asList(tlBuilder.build()));
101             }
102         } catch (Throwable t) {
103             // Avoid throwing from this method. Log the error.
104             Log.e(LOG_TAG, "Error detecting languages for text. Returning empty result.", t);
105         }
106         // Getting here means something went wrong. Return an empty result.
107         return Collections.emptyList();
108     }
109 
getLanguageDetector()110     private LangId getLanguageDetector() throws FileNotFoundException {
111         synchronized (mLangIdLock) {
112             if (mLangId == null) {
113                 mLangIdFd = ParcelFileDescriptor.open(
114                         new File("/etc/textclassifier/textclassifier.langid.model"),
115                         ParcelFileDescriptor.MODE_READ_ONLY);
116                 mLangId = new LangId(mLangIdFd.getFd());
117             }
118             return mLangId;
119         }
120     }
121 }
122