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; |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 22 | import android.annotation.UnsupportedAppUsage; |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 23 | import android.content.Context; |
Abodunrinwa Toki | c2449b8 | 2018-05-01 21:36:48 +0100 | [diff] [blame] | 24 | import android.database.ContentObserver; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 25 | import android.os.ServiceManager; |
Abodunrinwa Toki | db8fc31 | 2018-02-26 21:37:51 +0000 | [diff] [blame] | 26 | import android.provider.Settings; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 27 | import android.service.textclassifier.TextClassifierService; |
Abodunrinwa Toki | c7073a4 | 2018-02-28 23:02:13 +0000 | [diff] [blame] | 28 | import android.view.textclassifier.TextClassifier.TextClassifierType; |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 29 | |
Abodunrinwa Toki | c7073a4 | 2018-02-28 23:02:13 +0000 | [diff] [blame] | 30 | import com.android.internal.annotations.GuardedBy; |
Tony Mak | f93e9e5 | 2018-07-16 14:46:29 +0200 | [diff] [blame] | 31 | import com.android.internal.util.IndentingPrintWriter; |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 32 | import com.android.internal.util.Preconditions; |
| 33 | |
Abodunrinwa Toki | c2449b8 | 2018-05-01 21:36:48 +0100 | [diff] [blame] | 34 | import java.lang.ref.WeakReference; |
| 35 | |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 36 | /** |
| 37 | * Interface to the text classification service. |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 38 | */ |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 39 | @SystemService(Context.TEXT_CLASSIFICATION_SERVICE) |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 40 | public final class TextClassificationManager { |
| 41 | |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 42 | private static final String LOG_TAG = "TextClassificationManager"; |
| 43 | |
| 44 | private final Object mLock = new Object(); |
Abodunrinwa Toki | 88be5a6 | 2018-03-23 04:01:28 +0000 | [diff] [blame] | 45 | private final TextClassificationSessionFactory mDefaultSessionFactory = |
| 46 | classificationContext -> new TextClassificationSession( |
| 47 | classificationContext, getTextClassifier()); |
Abodunrinwa Toki | b89cf02 | 2017-02-06 19:53:22 +0000 | [diff] [blame] | 48 | |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 49 | private final Context mContext; |
Abodunrinwa Toki | c2449b8 | 2018-05-01 21:36:48 +0100 | [diff] [blame] | 50 | private final SettingsObserver mSettingsObserver; |
Abodunrinwa Toki | c7073a4 | 2018-02-28 23:02:13 +0000 | [diff] [blame] | 51 | |
| 52 | @GuardedBy("mLock") |
Abodunrinwa Toki | c2449b8 | 2018-05-01 21:36:48 +0100 | [diff] [blame] | 53 | @Nullable |
| 54 | private TextClassifier mCustomTextClassifier; |
Abodunrinwa Toki | c7073a4 | 2018-02-28 23:02:13 +0000 | [diff] [blame] | 55 | @GuardedBy("mLock") |
Abodunrinwa Toki | c2449b8 | 2018-05-01 21:36:48 +0100 | [diff] [blame] | 56 | @Nullable |
Abodunrinwa Toki | c7073a4 | 2018-02-28 23:02:13 +0000 | [diff] [blame] | 57 | private TextClassifier mLocalTextClassifier; |
| 58 | @GuardedBy("mLock") |
Abodunrinwa Toki | c2449b8 | 2018-05-01 21:36:48 +0100 | [diff] [blame] | 59 | @Nullable |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 60 | private TextClassifier mSystemTextClassifier; |
Abodunrinwa Toki | 88be5a6 | 2018-03-23 04:01:28 +0000 | [diff] [blame] | 61 | @GuardedBy("mLock") |
| 62 | private TextClassificationSessionFactory mSessionFactory; |
Abodunrinwa Toki | c2449b8 | 2018-05-01 21:36:48 +0100 | [diff] [blame] | 63 | @GuardedBy("mLock") |
| 64 | private TextClassificationConstants mSettings; |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 65 | |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 66 | /** @hide */ |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 67 | public TextClassificationManager(Context context) { |
| 68 | mContext = Preconditions.checkNotNull(context); |
Abodunrinwa Toki | 88be5a6 | 2018-03-23 04:01:28 +0000 | [diff] [blame] | 69 | mSessionFactory = mDefaultSessionFactory; |
Abodunrinwa Toki | c2449b8 | 2018-05-01 21:36:48 +0100 | [diff] [blame] | 70 | mSettingsObserver = new SettingsObserver(this); |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 71 | } |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 72 | |
Abodunrinwa Toki | df61b03 | 2017-03-29 22:41:57 +0100 | [diff] [blame] | 73 | /** |
Abodunrinwa Toki | c7073a4 | 2018-02-28 23:02:13 +0000 | [diff] [blame] | 74 | * Returns the text classifier that was set via {@link #setTextClassifier(TextClassifier)}. |
| 75 | * If this is null, this method returns a default text classifier (i.e. either the system text |
Tony Mak | 31ff135 | 2019-01-22 21:26:05 +0000 | [diff] [blame] | 76 | * classifier if one exists, or a local text classifier running in this process.) |
| 77 | * <p> |
Abodunrinwa Toki | 324572e | 2019-02-12 19:01:01 +0000 | [diff] [blame] | 78 | * Note that requests to the TextClassifier may be handled in an OEM-provided process rather |
| 79 | * than in the calling app's process. |
Abodunrinwa Toki | c7073a4 | 2018-02-28 23:02:13 +0000 | [diff] [blame] | 80 | * |
| 81 | * @see #setTextClassifier(TextClassifier) |
Abodunrinwa Toki | df61b03 | 2017-03-29 22:41:57 +0100 | [diff] [blame] | 82 | */ |
Abodunrinwa Toki | 88be5a6 | 2018-03-23 04:01:28 +0000 | [diff] [blame] | 83 | @NonNull |
Abodunrinwa Toki | df61b03 | 2017-03-29 22:41:57 +0100 | [diff] [blame] | 84 | public TextClassifier getTextClassifier() { |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 85 | synchronized (mLock) { |
Abodunrinwa Toki | c2449b8 | 2018-05-01 21:36:48 +0100 | [diff] [blame] | 86 | if (mCustomTextClassifier != null) { |
| 87 | return mCustomTextClassifier; |
| 88 | } else if (isSystemTextClassifierEnabled()) { |
| 89 | return getSystemTextClassifier(); |
| 90 | } else { |
| 91 | return getLocalTextClassifier(); |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 92 | } |
Abodunrinwa Toki | df61b03 | 2017-03-29 22:41:57 +0100 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Sets the text classifier. |
| 98 | * Set to null to use the system default text classifier. |
| 99 | * Set to {@link TextClassifier#NO_OP} to disable text classifier features. |
| 100 | */ |
| 101 | public void setTextClassifier(@Nullable TextClassifier textClassifier) { |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 102 | synchronized (mLock) { |
Abodunrinwa Toki | c2449b8 | 2018-05-01 21:36:48 +0100 | [diff] [blame] | 103 | mCustomTextClassifier = textClassifier; |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 104 | } |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 105 | } |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 106 | |
Abodunrinwa Toki | c7073a4 | 2018-02-28 23:02:13 +0000 | [diff] [blame] | 107 | /** |
| 108 | * Returns a specific type of text classifier. |
| 109 | * If the specified text classifier cannot be found, this returns {@link TextClassifier#NO_OP}. |
| 110 | * |
| 111 | * @see TextClassifier#LOCAL |
| 112 | * @see TextClassifier#SYSTEM |
| 113 | * @hide |
| 114 | */ |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 115 | @UnsupportedAppUsage |
Abodunrinwa Toki | c7073a4 | 2018-02-28 23:02:13 +0000 | [diff] [blame] | 116 | public TextClassifier getTextClassifier(@TextClassifierType int type) { |
| 117 | switch (type) { |
| 118 | case TextClassifier.LOCAL: |
| 119 | return getLocalTextClassifier(); |
| 120 | default: |
| 121 | return getSystemTextClassifier(); |
| 122 | } |
| 123 | } |
| 124 | |
Abodunrinwa Toki | c2449b8 | 2018-05-01 21:36:48 +0100 | [diff] [blame] | 125 | private TextClassificationConstants getSettings() { |
| 126 | synchronized (mLock) { |
| 127 | if (mSettings == null) { |
| 128 | mSettings = TextClassificationConstants.loadFromString(Settings.Global.getString( |
Michael Wright | b112f26 | 2018-05-24 13:41:50 +0100 | [diff] [blame] | 129 | getApplicationContext().getContentResolver(), |
Abodunrinwa Toki | c2449b8 | 2018-05-01 21:36:48 +0100 | [diff] [blame] | 130 | Settings.Global.TEXT_CLASSIFIER_CONSTANTS)); |
| 131 | } |
| 132 | return mSettings; |
| 133 | } |
Abodunrinwa Toki | c7073a4 | 2018-02-28 23:02:13 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Abodunrinwa Toki | 88be5a6 | 2018-03-23 04:01:28 +0000 | [diff] [blame] | 136 | /** |
| 137 | * Call this method to start a text classification session with the given context. |
| 138 | * A session is created with a context helping the classifier better understand |
| 139 | * what the user needs and consists of queries and feedback events. The queries |
| 140 | * are directly related to providing useful functionality to the user and the events |
| 141 | * are a feedback loop back to the classifier helping it learn and better serve |
| 142 | * future queries. |
| 143 | * |
| 144 | * <p> All interactions with the returned classifier are considered part of a single |
| 145 | * session and are logically grouped. For example, when a text widget is focused |
| 146 | * all user interactions around text editing (selection, editing, etc) can be |
| 147 | * grouped together to allow the classifier get better. |
| 148 | * |
| 149 | * @param classificationContext The context in which classification would occur |
| 150 | * |
| 151 | * @return An instance to perform classification in the given context |
| 152 | */ |
| 153 | @NonNull |
| 154 | public TextClassifier createTextClassificationSession( |
| 155 | @NonNull TextClassificationContext classificationContext) { |
| 156 | Preconditions.checkNotNull(classificationContext); |
| 157 | final TextClassifier textClassifier = |
| 158 | mSessionFactory.createTextClassificationSession(classificationContext); |
| 159 | Preconditions.checkNotNull(textClassifier, "Session Factory should never return null"); |
| 160 | return textClassifier; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * @see #createTextClassificationSession(TextClassificationContext, TextClassifier) |
| 165 | * @hide |
| 166 | */ |
| 167 | public TextClassifier createTextClassificationSession( |
| 168 | TextClassificationContext classificationContext, TextClassifier textClassifier) { |
| 169 | Preconditions.checkNotNull(classificationContext); |
| 170 | Preconditions.checkNotNull(textClassifier); |
| 171 | return new TextClassificationSession(classificationContext, textClassifier); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Sets a TextClassificationSessionFactory to be used to create session-aware TextClassifiers. |
| 176 | * |
| 177 | * @param factory the textClassification session factory. If this is null, the default factory |
| 178 | * will be used. |
| 179 | */ |
| 180 | public void setTextClassificationSessionFactory( |
| 181 | @Nullable TextClassificationSessionFactory factory) { |
| 182 | synchronized (mLock) { |
| 183 | if (factory != null) { |
| 184 | mSessionFactory = factory; |
| 185 | } else { |
| 186 | mSessionFactory = mDefaultSessionFactory; |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
Abodunrinwa Toki | c2449b8 | 2018-05-01 21:36:48 +0100 | [diff] [blame] | 191 | @Override |
| 192 | protected void finalize() throws Throwable { |
| 193 | try { |
| 194 | // Note that fields could be null if the constructor threw. |
Michael Wright | b112f26 | 2018-05-24 13:41:50 +0100 | [diff] [blame] | 195 | if (mSettingsObserver != null) { |
| 196 | getApplicationContext().getContentResolver() |
Abodunrinwa Toki | c2449b8 | 2018-05-01 21:36:48 +0100 | [diff] [blame] | 197 | .unregisterContentObserver(mSettingsObserver); |
| 198 | } |
| 199 | } finally { |
| 200 | super.finalize(); |
| 201 | } |
| 202 | } |
| 203 | |
Abodunrinwa Toki | c7073a4 | 2018-02-28 23:02:13 +0000 | [diff] [blame] | 204 | private TextClassifier getSystemTextClassifier() { |
| 205 | synchronized (mLock) { |
| 206 | if (mSystemTextClassifier == null && isSystemTextClassifierEnabled()) { |
| 207 | try { |
Abodunrinwa Toki | c2449b8 | 2018-05-01 21:36:48 +0100 | [diff] [blame] | 208 | mSystemTextClassifier = new SystemTextClassifier(mContext, getSettings()); |
Abodunrinwa Toki | c7073a4 | 2018-02-28 23:02:13 +0000 | [diff] [blame] | 209 | Log.d(LOG_TAG, "Initialized SystemTextClassifier"); |
| 210 | } catch (ServiceManager.ServiceNotFoundException e) { |
| 211 | Log.e(LOG_TAG, "Could not initialize SystemTextClassifier", e); |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | if (mSystemTextClassifier != null) { |
| 216 | return mSystemTextClassifier; |
| 217 | } |
| 218 | return TextClassifier.NO_OP; |
| 219 | } |
| 220 | |
Tony Mak | 31ff135 | 2019-01-22 21:26:05 +0000 | [diff] [blame] | 221 | /** |
| 222 | * Returns a local textclassifier, which is running in this process. |
Tony Mak | 31ff135 | 2019-01-22 21:26:05 +0000 | [diff] [blame] | 223 | */ |
| 224 | @NonNull |
Abodunrinwa Toki | 324572e | 2019-02-12 19:01:01 +0000 | [diff] [blame] | 225 | private TextClassifier getLocalTextClassifier() { |
Abodunrinwa Toki | c7073a4 | 2018-02-28 23:02:13 +0000 | [diff] [blame] | 226 | synchronized (mLock) { |
| 227 | if (mLocalTextClassifier == null) { |
Abodunrinwa Toki | c2449b8 | 2018-05-01 21:36:48 +0100 | [diff] [blame] | 228 | if (getSettings().isLocalTextClassifierEnabled()) { |
Abodunrinwa Toki | 253827f | 2018-04-24 19:19:48 +0100 | [diff] [blame] | 229 | mLocalTextClassifier = |
Abodunrinwa Toki | c2449b8 | 2018-05-01 21:36:48 +0100 | [diff] [blame] | 230 | new TextClassifierImpl(mContext, getSettings(), TextClassifier.NO_OP); |
Abodunrinwa Toki | c7073a4 | 2018-02-28 23:02:13 +0000 | [diff] [blame] | 231 | } else { |
| 232 | Log.d(LOG_TAG, "Local TextClassifier disabled"); |
Abodunrinwa Toki | 253827f | 2018-04-24 19:19:48 +0100 | [diff] [blame] | 233 | mLocalTextClassifier = TextClassifier.NO_OP; |
Abodunrinwa Toki | c7073a4 | 2018-02-28 23:02:13 +0000 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | return mLocalTextClassifier; |
| 237 | } |
| 238 | } |
| 239 | |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 240 | private boolean isSystemTextClassifierEnabled() { |
Abodunrinwa Toki | c2449b8 | 2018-05-01 21:36:48 +0100 | [diff] [blame] | 241 | return getSettings().isSystemTextClassifierEnabled() |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 242 | && TextClassifierService.getServiceComponentName(mContext) != null; |
| 243 | } |
Abodunrinwa Toki | db8fc31 | 2018-02-26 21:37:51 +0000 | [diff] [blame] | 244 | |
Abodunrinwa Toki | c2449b8 | 2018-05-01 21:36:48 +0100 | [diff] [blame] | 245 | private void invalidate() { |
| 246 | synchronized (mLock) { |
| 247 | mSettings = null; |
| 248 | mLocalTextClassifier = null; |
| 249 | mSystemTextClassifier = null; |
| 250 | } |
| 251 | } |
| 252 | |
Michael Wright | b112f26 | 2018-05-24 13:41:50 +0100 | [diff] [blame] | 253 | Context getApplicationContext() { |
| 254 | return mContext.getApplicationContext() != null |
| 255 | ? mContext.getApplicationContext() |
| 256 | : mContext; |
| 257 | } |
| 258 | |
Tony Mak | f93e9e5 | 2018-07-16 14:46:29 +0200 | [diff] [blame] | 259 | /** @hide **/ |
| 260 | public void dump(IndentingPrintWriter pw) { |
| 261 | getLocalTextClassifier().dump(pw); |
| 262 | getSystemTextClassifier().dump(pw); |
| 263 | getSettings().dump(pw); |
| 264 | } |
| 265 | |
Abodunrinwa Toki | db8fc31 | 2018-02-26 21:37:51 +0000 | [diff] [blame] | 266 | /** @hide */ |
| 267 | public static TextClassificationConstants getSettings(Context context) { |
| 268 | Preconditions.checkNotNull(context); |
| 269 | final TextClassificationManager tcm = |
| 270 | context.getSystemService(TextClassificationManager.class); |
| 271 | if (tcm != null) { |
Abodunrinwa Toki | c2449b8 | 2018-05-01 21:36:48 +0100 | [diff] [blame] | 272 | return tcm.getSettings(); |
Abodunrinwa Toki | db8fc31 | 2018-02-26 21:37:51 +0000 | [diff] [blame] | 273 | } else { |
| 274 | return TextClassificationConstants.loadFromString(Settings.Global.getString( |
Abodunrinwa Toki | c2449b8 | 2018-05-01 21:36:48 +0100 | [diff] [blame] | 275 | context.getApplicationContext().getContentResolver(), |
| 276 | Settings.Global.TEXT_CLASSIFIER_CONSTANTS)); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | private static final class SettingsObserver extends ContentObserver { |
| 281 | |
| 282 | private final WeakReference<TextClassificationManager> mTcm; |
| 283 | |
| 284 | SettingsObserver(TextClassificationManager tcm) { |
| 285 | super(null); |
| 286 | mTcm = new WeakReference<>(tcm); |
Michael Wright | b112f26 | 2018-05-24 13:41:50 +0100 | [diff] [blame] | 287 | tcm.getApplicationContext().getContentResolver().registerContentObserver( |
Abodunrinwa Toki | c2449b8 | 2018-05-01 21:36:48 +0100 | [diff] [blame] | 288 | Settings.Global.getUriFor(Settings.Global.TEXT_CLASSIFIER_CONSTANTS), |
| 289 | false /* notifyForDescendants */, |
| 290 | this); |
| 291 | } |
| 292 | |
| 293 | @Override |
| 294 | public void onChange(boolean selfChange) { |
| 295 | final TextClassificationManager tcm = mTcm.get(); |
| 296 | if (tcm != null) { |
| 297 | tcm.invalidate(); |
| 298 | } |
Abodunrinwa Toki | db8fc31 | 2018-02-26 21:37:51 +0000 | [diff] [blame] | 299 | } |
| 300 | } |
Abodunrinwa Toki | f001fef | 2017-01-04 23:51:42 +0000 | [diff] [blame] | 301 | } |