| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.view.textclassifier; |
| 18 |
|
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.annotation.WorkerThread; |
| 21 |
import android.view.textclassifier.SelectionEvent.InvocationMethod; |
| 22 |
|
| 23 |
import com.android.internal.annotations.GuardedBy; |
| 24 |
import com.android.internal.util.Preconditions; |
| 25 |
|
| 26 |
import java.util.Objects; |
| 27 |
import java.util.function.Supplier; |
| 28 |
|
| 29 |
import sun.misc.Cleaner; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
@WorkerThread |
| 35 |
final class TextClassificationSession implements TextClassifier { |
| 36 |
|
| 37 |
private static final String LOG_TAG = "TextClassificationSession"; |
| 38 |
|
| 39 |
private final TextClassifier mDelegate; |
| 40 |
private final SelectionEventHelper mEventHelper; |
| 41 |
private final TextClassificationSessionId mSessionId; |
| 42 |
private final TextClassificationContext mClassificationContext; |
| 43 |
private final Cleaner mCleaner; |
| 44 |
|
| 45 |
private final Object mLock = new Object(); |
| 46 |
|
| 47 |
@GuardedBy("mLock") |
| 48 |
private boolean mDestroyed; |
| 49 |
|
| 50 |
TextClassificationSession(TextClassificationContext context, TextClassifier delegate) { |
| 51 |
mClassificationContext = Objects.requireNonNull(context); |
| 52 |
mDelegate = Objects.requireNonNull(delegate); |
| 53 |
mSessionId = new TextClassificationSessionId(); |
| 54 |
mEventHelper = new SelectionEventHelper(mSessionId, mClassificationContext); |
| 55 |
initializeRemoteSession(); |
| 56 |
|
| 57 |
mCleaner = Cleaner.create(this, new CleanerRunnable(mEventHelper, mDelegate)); |
| 58 |
} |
| 59 |
|
| 60 |
@Override |
| 61 |
public TextSelection suggestSelection(TextSelection.Request request) { |
| 62 |
return checkDestroyedAndRun(() -> mDelegate.suggestSelection(request)); |
| 63 |
} |
| 64 |
|
| 65 |
private void initializeRemoteSession() { |
| 66 |
if (mDelegate instanceof SystemTextClassifier) { |
| 67 |
((SystemTextClassifier) mDelegate).initializeRemoteSession( |
| 68 |
mClassificationContext, mSessionId); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
@Override |
| 73 |
public TextClassification classifyText(TextClassification.Request request) { |
| 74 |
return checkDestroyedAndRun(() -> mDelegate.classifyText(request)); |
| 75 |
} |
| 76 |
|
| 77 |
@Override |
| 78 |
public TextLinks generateLinks(TextLinks.Request request) { |
| 79 |
return checkDestroyedAndRun(() -> mDelegate.generateLinks(request)); |
| 80 |
} |
| 81 |
|
| 82 |
@Override |
| 83 |
public ConversationActions suggestConversationActions(ConversationActions.Request request) { |
| 84 |
return checkDestroyedAndRun(() -> mDelegate.suggestConversationActions(request)); |
| 85 |
} |
| 86 |
|
| 87 |
@Override |
| 88 |
public TextLanguage detectLanguage(TextLanguage.Request request) { |
| 89 |
return checkDestroyedAndRun(() -> mDelegate.detectLanguage(request)); |
| 90 |
} |
| 91 |
|
| 92 |
@Override |
| 93 |
public int getMaxGenerateLinksTextLength() { |
| 94 |
return checkDestroyedAndRun(mDelegate::getMaxGenerateLinksTextLength); |
| 95 |
} |
| 96 |
|
| 97 |
@Override |
| 98 |
public void onSelectionEvent(SelectionEvent event) { |
| 99 |
checkDestroyedAndRun(() -> { |
| 100 |
try { |
| 101 |
if (mEventHelper.sanitizeEvent(event)) { |
| 102 |
mDelegate.onSelectionEvent(event); |
| 103 |
} |
| 104 |
} catch (Exception e) { |
| 105 |
|
| 106 |
Log.e(LOG_TAG, "Error reporting text classifier selection event", e); |
| 107 |
} |
| 108 |
return null; |
| 109 |
}); |
| 110 |
} |
| 111 |
|
| 112 |
@Override |
| 113 |
public void onTextClassifierEvent(TextClassifierEvent event) { |
| 114 |
checkDestroyedAndRun(() -> { |
| 115 |
try { |
| 116 |
event.mHiddenTempSessionId = mSessionId; |
| 117 |
mDelegate.onTextClassifierEvent(event); |
| 118 |
} catch (Exception e) { |
| 119 |
|
| 120 |
Log.e(LOG_TAG, "Error reporting text classifier event", e); |
| 121 |
} |
| 122 |
return null; |
| 123 |
}); |
| 124 |
} |
| 125 |
|
| 126 |
@Override |
| 127 |
public void destroy() { |
| 128 |
synchronized (mLock) { |
| 129 |
if (!mDestroyed) { |
| 130 |
mCleaner.clean(); |
| 131 |
mDestroyed = true; |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
@Override |
| 137 |
public boolean isDestroyed() { |
| 138 |
synchronized (mLock) { |
| 139 |
return mDestroyed; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
private <T> T checkDestroyedAndRun(Supplier<T> responseSupplier) { |
| 155 |
if (!isDestroyed()) { |
| 156 |
T response = responseSupplier.get(); |
| 157 |
synchronized (mLock) { |
| 158 |
if (!mDestroyed) { |
| 159 |
return response; |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
throw new IllegalStateException( |
| 164 |
"This TextClassification session has been destroyed"); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
private static final class SelectionEventHelper { |
| 171 |
|
| 172 |
private final TextClassificationSessionId mSessionId; |
| 173 |
private final TextClassificationContext mContext; |
| 174 |
|
| 175 |
@InvocationMethod |
| 176 |
private int mInvocationMethod = SelectionEvent.INVOCATION_UNKNOWN; |
| 177 |
private SelectionEvent mPrevEvent; |
| 178 |
private SelectionEvent mSmartEvent; |
| 179 |
private SelectionEvent mStartEvent; |
| 180 |
|
| 181 |
SelectionEventHelper( |
| 182 |
TextClassificationSessionId sessionId, TextClassificationContext context) { |
| 183 |
mSessionId = Objects.requireNonNull(sessionId); |
| 184 |
mContext = Objects.requireNonNull(context); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
boolean sanitizeEvent(SelectionEvent event) { |
| 193 |
updateInvocationMethod(event); |
| 194 |
modifyAutoSelectionEventType(event); |
| 195 |
|
| 196 |
if (event.getEventType() != SelectionEvent.EVENT_SELECTION_STARTED |
| 197 |
&& mStartEvent == null) { |
| 198 |
Log.d(LOG_TAG, "Selection session not yet started. Ignoring event"); |
| 199 |
return false; |
| 200 |
} |
| 201 |
|
| 202 |
final long now = System.currentTimeMillis(); |
| 203 |
switch (event.getEventType()) { |
| 204 |
case SelectionEvent.EVENT_SELECTION_STARTED: |
| 205 |
Preconditions.checkArgument( |
| 206 |
event.getAbsoluteEnd() == event.getAbsoluteStart() + 1); |
| 207 |
event.setSessionId(mSessionId); |
| 208 |
mStartEvent = event; |
| 209 |
break; |
| 210 |
case SelectionEvent.EVENT_SMART_SELECTION_SINGLE: |
| 211 |
case SelectionEvent.EVENT_SMART_SELECTION_MULTI: |
| 212 |
case SelectionEvent.EVENT_AUTO_SELECTION: |
| 213 |
mSmartEvent = event; |
| 214 |
break; |
| 215 |
case SelectionEvent.ACTION_ABANDON: |
| 216 |
case SelectionEvent.ACTION_OVERTYPE: |
| 217 |
if (mPrevEvent != null) { |
| 218 |
event.setEntityType(mPrevEvent.getEntityType()); |
| 219 |
} |
| 220 |
break; |
| 221 |
case SelectionEvent.EVENT_SELECTION_MODIFIED: |
| 222 |
if (mPrevEvent != null |
| 223 |
&& mPrevEvent.getAbsoluteStart() == event.getAbsoluteStart() |
| 224 |
&& mPrevEvent.getAbsoluteEnd() == event.getAbsoluteEnd()) { |
| 225 |
|
| 226 |
return false; |
| 227 |
} |
| 228 |
break; |
| 229 |
default: |
| 230 |
|
| 231 |
} |
| 232 |
|
| 233 |
event.setEventTime(now); |
| 234 |
if (mStartEvent != null) { |
| 235 |
event.setSessionId(mStartEvent.getSessionId()) |
| 236 |
.setDurationSinceSessionStart(now - mStartEvent.getEventTime()) |
| 237 |
.setStart(event.getAbsoluteStart() - mStartEvent.getAbsoluteStart()) |
| 238 |
.setEnd(event.getAbsoluteEnd() - mStartEvent.getAbsoluteStart()); |
| 239 |
} |
| 240 |
if (mSmartEvent != null) { |
| 241 |
event.setResultId(mSmartEvent.getResultId()) |
| 242 |
.setSmartStart( |
| 243 |
mSmartEvent.getAbsoluteStart() - mStartEvent.getAbsoluteStart()) |
| 244 |
.setSmartEnd(mSmartEvent.getAbsoluteEnd() - mStartEvent.getAbsoluteStart()); |
| 245 |
} |
| 246 |
if (mPrevEvent != null) { |
| 247 |
event.setDurationSincePreviousEvent(now - mPrevEvent.getEventTime()) |
| 248 |
.setEventIndex(mPrevEvent.getEventIndex() + 1); |
| 249 |
} |
| 250 |
mPrevEvent = event; |
| 251 |
return true; |
| 252 |
} |
| 253 |
|
| 254 |
void endSession() { |
| 255 |
mPrevEvent = null; |
| 256 |
mSmartEvent = null; |
| 257 |
mStartEvent = null; |
| 258 |
} |
| 259 |
|
| 260 |
private void updateInvocationMethod(SelectionEvent event) { |
| 261 |
event.setTextClassificationSessionContext(mContext); |
| 262 |
if (event.getInvocationMethod() == SelectionEvent.INVOCATION_UNKNOWN) { |
| 263 |
event.setInvocationMethod(mInvocationMethod); |
| 264 |
} else { |
| 265 |
mInvocationMethod = event.getInvocationMethod(); |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
private void modifyAutoSelectionEventType(SelectionEvent event) { |
| 270 |
switch (event.getEventType()) { |
| 271 |
case SelectionEvent.EVENT_SMART_SELECTION_SINGLE: |
| 272 |
case SelectionEvent.EVENT_SMART_SELECTION_MULTI: |
| 273 |
case SelectionEvent.EVENT_AUTO_SELECTION: |
| 274 |
if (SelectionSessionLogger.isPlatformLocalTextClassifierSmartSelection( |
| 275 |
event.getResultId())) { |
| 276 |
if (event.getAbsoluteEnd() - event.getAbsoluteStart() > 1) { |
| 277 |
event.setEventType(SelectionEvent.EVENT_SMART_SELECTION_MULTI); |
| 278 |
} else { |
| 279 |
event.setEventType(SelectionEvent.EVENT_SMART_SELECTION_SINGLE); |
| 280 |
} |
| 281 |
} else { |
| 282 |
event.setEventType(SelectionEvent.EVENT_AUTO_SELECTION); |
| 283 |
} |
| 284 |
return; |
| 285 |
default: |
| 286 |
return; |
| 287 |
} |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
private static class CleanerRunnable implements Runnable { |
| 294 |
@NonNull |
| 295 |
private final SelectionEventHelper mEventHelper; |
| 296 |
@NonNull |
| 297 |
private final TextClassifier mDelegate; |
| 298 |
|
| 299 |
CleanerRunnable( |
| 300 |
@NonNull SelectionEventHelper eventHelper, @NonNull TextClassifier delegate) { |
| 301 |
mEventHelper = Objects.requireNonNull(eventHelper); |
| 302 |
mDelegate = Objects.requireNonNull(delegate); |
| 303 |
} |
| 304 |
|
| 305 |
@Override |
| 306 |
public void run() { |
| 307 |
mEventHelper.endSession(); |
| 308 |
mDelegate.destroy(); |
| 309 |
} |
| 310 |
} |
| 311 |
} |