1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
|
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.view.textclassifier;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.SystemService;
import android.annotation.UnsupportedAppUsage;
import android.app.ActivityThread;
import android.content.Context;
import android.database.ContentObserver;
import android.os.ServiceManager;
import android.provider.DeviceConfig;
import android.provider.DeviceConfig.Properties;
import android.provider.Settings;
import android.service.textclassifier.TextClassifierService;
import android.view.textclassifier.TextClassifier.TextClassifierType;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.util.IndentingPrintWriter;
import com.android.internal.util.Preconditions;
import java.lang.ref.WeakReference;
/**
* Interface to the text classification service.
*/
@SystemService(Context.TEXT_CLASSIFICATION_SERVICE)
public final class TextClassificationManager {
private static final String LOG_TAG = "TextClassificationManager";
private static final TextClassificationConstants sDefaultSettings =
new TextClassificationConstants(() -> null);
private final Object mLock = new Object();
private final TextClassificationSessionFactory mDefaultSessionFactory =
classificationContext -> new TextClassificationSession(
classificationContext, getTextClassifier());
private final Context mContext;
private final SettingsObserver mSettingsObserver;
@GuardedBy("mLock")
@Nullable
private TextClassifier mCustomTextClassifier;
@GuardedBy("mLock")
@Nullable
private TextClassifier mLocalTextClassifier;
@GuardedBy("mLock")
@Nullable
private TextClassifier mSystemTextClassifier;
@GuardedBy("mLock")
private TextClassificationSessionFactory mSessionFactory;
@GuardedBy("mLock")
private TextClassificationConstants mSettings;
/** @hide */
public TextClassificationManager(Context context) {
mContext = Preconditions.checkNotNull(context);
mSessionFactory = mDefaultSessionFactory;
mSettingsObserver = new SettingsObserver(this);
}
/**
* Returns the text classifier that was set via {@link #setTextClassifier(TextClassifier)}.
* If this is null, this method returns a default text classifier (i.e. either the system text
* classifier if one exists, or a local text classifier running in this process.)
* <p>
* Note that requests to the TextClassifier may be handled in an OEM-provided process rather
* than in the calling app's process.
*
* @see #setTextClassifier(TextClassifier)
*/
@NonNull
public TextClassifier getTextClassifier() {
synchronized (mLock) {
if (mCustomTextClassifier != null) {
return mCustomTextClassifier;
} else if (isSystemTextClassifierEnabled()) {
return getSystemTextClassifier();
} else {
return getLocalTextClassifier();
}
}
}
/**
* Sets the text classifier.
* Set to null to use the system default text classifier.
* Set to {@link TextClassifier#NO_OP} to disable text classifier features.
*/
public void setTextClassifier(@Nullable TextClassifier textClassifier) {
synchronized (mLock) {
mCustomTextClassifier = textClassifier;
}
}
/**
* Returns a specific type of text classifier.
* If the specified text classifier cannot be found, this returns {@link TextClassifier#NO_OP}.
*
* @see TextClassifier#LOCAL
* @see TextClassifier#SYSTEM
* @hide
*/
@UnsupportedAppUsage
public TextClassifier getTextClassifier(@TextClassifierType int type) {
switch (type) {
case TextClassifier.LOCAL:
return getLocalTextClassifier();
default:
return getSystemTextClassifier();
}
}
private TextClassificationConstants getSettings() {
synchronized (mLock) {
if (mSettings == null) {
mSettings = new TextClassificationConstants(
() -> Settings.Global.getString(
getApplicationContext().getContentResolver(),
Settings.Global.TEXT_CLASSIFIER_CONSTANTS));
}
return mSettings;
}
}
/**
* Call this method to start a text classification session with the given context.
* A session is created with a context helping the classifier better understand
* what the user needs and consists of queries and feedback events. The queries
* are directly related to providing useful functionality to the user and the events
* are a feedback loop back to the classifier helping it learn and better serve
* future queries.
*
* <p> All interactions with the returned classifier are considered part of a single
* session and are logically grouped. For example, when a text widget is focused
* all user interactions around text editing (selection, editing, etc) can be
* grouped together to allow the classifier get better.
*
* @param classificationContext The context in which classification would occur
*
* @return An instance to perform classification in the given context
*/
@NonNull
public TextClassifier createTextClassificationSession(
@NonNull TextClassificationContext classificationContext) {
Preconditions.checkNotNull(classificationContext);
final TextClassifier textClassifier =
mSessionFactory.createTextClassificationSession(classificationContext);
Preconditions.checkNotNull(textClassifier, "Session Factory should never return null");
return textClassifier;
}
/**
* @see #createTextClassificationSession(TextClassificationContext, TextClassifier)
* @hide
*/
public TextClassifier createTextClassificationSession(
TextClassificationContext classificationContext, TextClassifier textClassifier) {
Preconditions.checkNotNull(classificationContext);
Preconditions.checkNotNull(textClassifier);
return new TextClassificationSession(classificationContext, textClassifier);
}
/**
* Sets a TextClassificationSessionFactory to be used to create session-aware TextClassifiers.
*
* @param factory the textClassification session factory. If this is null, the default factory
* will be used.
*/
public void setTextClassificationSessionFactory(
@Nullable TextClassificationSessionFactory factory) {
synchronized (mLock) {
if (factory != null) {
mSessionFactory = factory;
} else {
mSessionFactory = mDefaultSessionFactory;
}
}
}
@Override
protected void finalize() throws Throwable {
try {
// Note that fields could be null if the constructor threw.
if (mSettingsObserver != null) {
getApplicationContext().getContentResolver()
.unregisterContentObserver(mSettingsObserver);
if (ConfigParser.ENABLE_DEVICE_CONFIG) {
DeviceConfig.removeOnPropertiesChangedListener(mSettingsObserver);
}
}
} finally {
super.finalize();
}
}
private TextClassifier getSystemTextClassifier() {
synchronized (mLock) {
if (mSystemTextClassifier == null && isSystemTextClassifierEnabled()) {
try {
mSystemTextClassifier = new SystemTextClassifier(mContext, getSettings());
Log.d(LOG_TAG, "Initialized SystemTextClassifier");
} catch (ServiceManager.ServiceNotFoundException e) {
Log.e(LOG_TAG, "Could not initialize SystemTextClassifier", e);
}
}
}
if (mSystemTextClassifier != null) {
return mSystemTextClassifier;
}
return TextClassifier.NO_OP;
}
/**
* Returns a local textclassifier, which is running in this process.
*/
@NonNull
private TextClassifier getLocalTextClassifier() {
synchronized (mLock) {
if (mLocalTextClassifier == null) {
if (getSettings().isLocalTextClassifierEnabled()) {
mLocalTextClassifier =
new TextClassifierImpl(mContext, getSettings(), TextClassifier.NO_OP);
} else {
Log.d(LOG_TAG, "Local TextClassifier disabled");
mLocalTextClassifier = TextClassifier.NO_OP;
}
}
return mLocalTextClassifier;
}
}
private boolean isSystemTextClassifierEnabled() {
return getSettings().isSystemTextClassifierEnabled()
&& TextClassifierService.getServiceComponentName(mContext) != null;
}
/** @hide */
@VisibleForTesting
public void invalidateForTesting() {
invalidate();
}
private void invalidate() {
synchronized (mLock) {
mSettings = null;
mLocalTextClassifier = null;
mSystemTextClassifier = null;
}
}
Context getApplicationContext() {
return mContext.getApplicationContext() != null
? mContext.getApplicationContext()
: mContext;
}
/** @hide **/
public void dump(IndentingPrintWriter pw) {
getLocalTextClassifier().dump(pw);
getSystemTextClassifier().dump(pw);
getSettings().dump(pw);
}
/** @hide */
public static TextClassificationConstants getSettings(Context context) {
Preconditions.checkNotNull(context);
final TextClassificationManager tcm =
context.getSystemService(TextClassificationManager.class);
if (tcm != null) {
return tcm.getSettings();
} else {
// Use default settings if there is no tcm.
return sDefaultSettings;
}
}
private static final class SettingsObserver extends ContentObserver
implements DeviceConfig.OnPropertiesChangedListener {
private final WeakReference<TextClassificationManager> mTcm;
SettingsObserver(TextClassificationManager tcm) {
super(null);
mTcm = new WeakReference<>(tcm);
tcm.getApplicationContext().getContentResolver().registerContentObserver(
Settings.Global.getUriFor(Settings.Global.TEXT_CLASSIFIER_CONSTANTS),
false /* notifyForDescendants */,
this);
if (ConfigParser.ENABLE_DEVICE_CONFIG) {
DeviceConfig.addOnPropertiesChangedListener(
DeviceConfig.NAMESPACE_TEXTCLASSIFIER,
ActivityThread.currentApplication().getMainExecutor(),
this);
}
}
@Override
public void onChange(boolean selfChange) {
invalidateSettings();
}
@Override
public void onPropertiesChanged(Properties properties) {
invalidateSettings();
}
private void invalidateSettings() {
final TextClassificationManager tcm = mTcm.get();
if (tcm != null) {
tcm.invalidate();
}
}
}
}
|