Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 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 | |
Abodunrinwa Toki | 88be5a6 | 2018-03-23 04:01:28 +0000 | [diff] [blame] | 19 | import android.annotation.NonNull; |
Abodunrinwa Toki | df61b03 | 2017-03-29 22:41:57 +0100 | [diff] [blame] | 20 | import android.annotation.Nullable; |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 21 | import android.annotation.SystemService; |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 22 | import android.content.Context; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 23 | import android.os.ServiceManager; |
Abodunrinwa Toki | db8fc31 | 2018-02-26 21:37:51 +0000 | [diff] [blame] | 24 | import android.provider.Settings; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 25 | import android.service.textclassifier.TextClassifierService; |
Abodunrinwa Toki | c7073a4 | 2018-02-28 23:02:13 +0000 | [diff] [blame] | 26 | import android.view.textclassifier.TextClassifier.TextClassifierType; |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 27 | |
Abodunrinwa Toki | c7073a4 | 2018-02-28 23:02:13 +0000 | [diff] [blame] | 28 | import com.android.internal.annotations.GuardedBy; |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 29 | import com.android.internal.util.Preconditions; |
| 30 | |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 31 | /** |
| 32 | * Interface to the text classification service. |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 33 | */ |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 34 | @SystemService(Context.TEXT_CLASSIFICATION_SERVICE) |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 35 | public final class TextClassificationManager { |
| 36 | |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 37 | private static final String LOG_TAG = "TextClassificationManager"; |
| 38 | |
| 39 | private final Object mLock = new Object(); |
Abodunrinwa Toki | 88be5a6 | 2018-03-23 04:01:28 +0000 | [diff] [blame] | 40 | private final TextClassificationSessionFactory mDefaultSessionFactory = |
| 41 | classificationContext -> new TextClassificationSession( |
| 42 | classificationContext, getTextClassifier()); |
Abodunrinwa Toki | b89cf02 | 2017-02-06 19:53:22 +0000 | [diff] [blame] | 43 | |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 44 | private final Context mContext; |
Abodunrinwa Toki | db8fc31 | 2018-02-26 21:37:51 +0000 | [diff] [blame] | 45 | private final TextClassificationConstants mSettings; |
Abodunrinwa Toki | c7073a4 | 2018-02-28 23:02:13 +0000 | [diff] [blame] | 46 | |
| 47 | @GuardedBy("mLock") |
Abodunrinwa Toki | df61b03 | 2017-03-29 22:41:57 +0100 | [diff] [blame] | 48 | private TextClassifier mTextClassifier; |
Abodunrinwa Toki | c7073a4 | 2018-02-28 23:02:13 +0000 | [diff] [blame] | 49 | @GuardedBy("mLock") |
| 50 | private TextClassifier mLocalTextClassifier; |
| 51 | @GuardedBy("mLock") |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 52 | private TextClassifier mSystemTextClassifier; |
Abodunrinwa Toki | 88be5a6 | 2018-03-23 04:01:28 +0000 | [diff] [blame] | 53 | @GuardedBy("mLock") |
| 54 | private TextClassificationSessionFactory mSessionFactory; |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 55 | |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 56 | /** @hide */ |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 57 | public TextClassificationManager(Context context) { |
| 58 | mContext = Preconditions.checkNotNull(context); |
Abodunrinwa Toki | db8fc31 | 2018-02-26 21:37:51 +0000 | [diff] [blame] | 59 | mSettings = TextClassificationConstants.loadFromString(Settings.Global.getString( |
| 60 | context.getContentResolver(), Settings.Global.TEXT_CLASSIFIER_CONSTANTS)); |
Abodunrinwa Toki | 88be5a6 | 2018-03-23 04:01:28 +0000 | [diff] [blame] | 61 | mSessionFactory = mDefaultSessionFactory; |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 62 | } |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 63 | |
Abodunrinwa Toki | df61b03 | 2017-03-29 22:41:57 +0100 | [diff] [blame] | 64 | /** |
Abodunrinwa Toki | c7073a4 | 2018-02-28 23:02:13 +0000 | [diff] [blame] | 65 | * Returns the text classifier that was set via {@link #setTextClassifier(TextClassifier)}. |
| 66 | * If this is null, this method returns a default text classifier (i.e. either the system text |
| 67 | * classifier if one exists, or a local text classifier running in this app.) |
| 68 | * |
| 69 | * @see #setTextClassifier(TextClassifier) |
Abodunrinwa Toki | df61b03 | 2017-03-29 22:41:57 +0100 | [diff] [blame] | 70 | */ |
Abodunrinwa Toki | 88be5a6 | 2018-03-23 04:01:28 +0000 | [diff] [blame] | 71 | @NonNull |
Abodunrinwa Toki | df61b03 | 2017-03-29 22:41:57 +0100 | [diff] [blame] | 72 | public TextClassifier getTextClassifier() { |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 73 | synchronized (mLock) { |
Abodunrinwa Toki | df61b03 | 2017-03-29 22:41:57 +0100 | [diff] [blame] | 74 | if (mTextClassifier == null) { |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 75 | if (isSystemTextClassifierEnabled()) { |
Abodunrinwa Toki | c7073a4 | 2018-02-28 23:02:13 +0000 | [diff] [blame] | 76 | mTextClassifier = getSystemTextClassifier(); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 77 | } else { |
Abodunrinwa Toki | c7073a4 | 2018-02-28 23:02:13 +0000 | [diff] [blame] | 78 | mTextClassifier = getLocalTextClassifier(); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 79 | } |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 80 | } |
Abodunrinwa Toki | df61b03 | 2017-03-29 22:41:57 +0100 | [diff] [blame] | 81 | return mTextClassifier; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Sets the text classifier. |
| 87 | * Set to null to use the system default text classifier. |
| 88 | * Set to {@link TextClassifier#NO_OP} to disable text classifier features. |
| 89 | */ |
| 90 | public void setTextClassifier(@Nullable TextClassifier textClassifier) { |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 91 | synchronized (mLock) { |
Abodunrinwa Toki | df61b03 | 2017-03-29 22:41:57 +0100 | [diff] [blame] | 92 | mTextClassifier = textClassifier; |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 93 | } |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 94 | } |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 95 | |
Abodunrinwa Toki | c7073a4 | 2018-02-28 23:02:13 +0000 | [diff] [blame] | 96 | /** |
| 97 | * Returns a specific type of text classifier. |
| 98 | * If the specified text classifier cannot be found, this returns {@link TextClassifier#NO_OP}. |
| 99 | * |
| 100 | * @see TextClassifier#LOCAL |
| 101 | * @see TextClassifier#SYSTEM |
| 102 | * @hide |
| 103 | */ |
Abodunrinwa Toki | c7073a4 | 2018-02-28 23:02:13 +0000 | [diff] [blame] | 104 | public TextClassifier getTextClassifier(@TextClassifierType int type) { |
| 105 | switch (type) { |
| 106 | case TextClassifier.LOCAL: |
| 107 | return getLocalTextClassifier(); |
| 108 | default: |
| 109 | return getSystemTextClassifier(); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /** @hide */ |
| 114 | public TextClassificationConstants getSettings() { |
| 115 | return mSettings; |
| 116 | } |
| 117 | |
Abodunrinwa Toki | 88be5a6 | 2018-03-23 04:01:28 +0000 | [diff] [blame] | 118 | /** |
| 119 | * Call this method to start a text classification session with the given context. |
| 120 | * A session is created with a context helping the classifier better understand |
| 121 | * what the user needs and consists of queries and feedback events. The queries |
| 122 | * are directly related to providing useful functionality to the user and the events |
| 123 | * are a feedback loop back to the classifier helping it learn and better serve |
| 124 | * future queries. |
| 125 | * |
| 126 | * <p> All interactions with the returned classifier are considered part of a single |
| 127 | * session and are logically grouped. For example, when a text widget is focused |
| 128 | * all user interactions around text editing (selection, editing, etc) can be |
| 129 | * grouped together to allow the classifier get better. |
| 130 | * |
| 131 | * @param classificationContext The context in which classification would occur |
| 132 | * |
| 133 | * @return An instance to perform classification in the given context |
| 134 | */ |
| 135 | @NonNull |
| 136 | public TextClassifier createTextClassificationSession( |
| 137 | @NonNull TextClassificationContext classificationContext) { |
| 138 | Preconditions.checkNotNull(classificationContext); |
| 139 | final TextClassifier textClassifier = |
| 140 | mSessionFactory.createTextClassificationSession(classificationContext); |
| 141 | Preconditions.checkNotNull(textClassifier, "Session Factory should never return null"); |
| 142 | return textClassifier; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * @see #createTextClassificationSession(TextClassificationContext, TextClassifier) |
| 147 | * @hide |
| 148 | */ |
| 149 | public TextClassifier createTextClassificationSession( |
| 150 | TextClassificationContext classificationContext, TextClassifier textClassifier) { |
| 151 | Preconditions.checkNotNull(classificationContext); |
| 152 | Preconditions.checkNotNull(textClassifier); |
| 153 | return new TextClassificationSession(classificationContext, textClassifier); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Sets a TextClassificationSessionFactory to be used to create session-aware TextClassifiers. |
| 158 | * |
| 159 | * @param factory the textClassification session factory. If this is null, the default factory |
| 160 | * will be used. |
| 161 | */ |
| 162 | public void setTextClassificationSessionFactory( |
| 163 | @Nullable TextClassificationSessionFactory factory) { |
| 164 | synchronized (mLock) { |
| 165 | if (factory != null) { |
| 166 | mSessionFactory = factory; |
| 167 | } else { |
| 168 | mSessionFactory = mDefaultSessionFactory; |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
Abodunrinwa Toki | c7073a4 | 2018-02-28 23:02:13 +0000 | [diff] [blame] | 173 | private TextClassifier getSystemTextClassifier() { |
| 174 | synchronized (mLock) { |
| 175 | if (mSystemTextClassifier == null && isSystemTextClassifierEnabled()) { |
| 176 | try { |
| 177 | mSystemTextClassifier = new SystemTextClassifier(mContext, mSettings); |
| 178 | Log.d(LOG_TAG, "Initialized SystemTextClassifier"); |
| 179 | } catch (ServiceManager.ServiceNotFoundException e) { |
| 180 | Log.e(LOG_TAG, "Could not initialize SystemTextClassifier", e); |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | if (mSystemTextClassifier != null) { |
| 185 | return mSystemTextClassifier; |
| 186 | } |
| 187 | return TextClassifier.NO_OP; |
| 188 | } |
| 189 | |
| 190 | private TextClassifier getLocalTextClassifier() { |
| 191 | synchronized (mLock) { |
| 192 | if (mLocalTextClassifier == null) { |
| 193 | if (mSettings.isLocalTextClassifierEnabled()) { |
Abodunrinwa Toki | 253827f | 2018-04-24 19:19:48 +0100 | [diff] [blame] | 194 | mLocalTextClassifier = |
| 195 | new TextClassifierImpl(mContext, mSettings, TextClassifier.NO_OP); |
Abodunrinwa Toki | c7073a4 | 2018-02-28 23:02:13 +0000 | [diff] [blame] | 196 | } else { |
| 197 | Log.d(LOG_TAG, "Local TextClassifier disabled"); |
Abodunrinwa Toki | 253827f | 2018-04-24 19:19:48 +0100 | [diff] [blame] | 198 | mLocalTextClassifier = TextClassifier.NO_OP; |
Abodunrinwa Toki | c7073a4 | 2018-02-28 23:02:13 +0000 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | return mLocalTextClassifier; |
| 202 | } |
| 203 | } |
| 204 | |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 205 | private boolean isSystemTextClassifierEnabled() { |
Abodunrinwa Toki | c7073a4 | 2018-02-28 23:02:13 +0000 | [diff] [blame] | 206 | return mSettings.isSystemTextClassifierEnabled() |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 207 | && TextClassifierService.getServiceComponentName(mContext) != null; |
| 208 | } |
Abodunrinwa Toki | db8fc31 | 2018-02-26 21:37:51 +0000 | [diff] [blame] | 209 | |
| 210 | /** @hide */ |
| 211 | public static TextClassificationConstants getSettings(Context context) { |
| 212 | Preconditions.checkNotNull(context); |
| 213 | final TextClassificationManager tcm = |
| 214 | context.getSystemService(TextClassificationManager.class); |
| 215 | if (tcm != null) { |
| 216 | return tcm.mSettings; |
| 217 | } else { |
| 218 | return TextClassificationConstants.loadFromString(Settings.Global.getString( |
| 219 | context.getContentResolver(), Settings.Global.TEXT_CLASSIFIER_CONSTANTS)); |
| 220 | } |
| 221 | } |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 222 | } |