| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.app; |
| 18 |
|
| 19 |
import android.annotation.CallbackExecutor; |
| 20 |
import android.annotation.NonNull; |
| 21 |
import android.annotation.Nullable; |
| 22 |
import android.content.Context; |
| 23 |
import android.os.Bundle; |
| 24 |
import android.os.IBinder; |
| 25 |
import android.os.ICancellationSignal; |
| 26 |
import android.os.Looper; |
| 27 |
import android.os.Message; |
| 28 |
import android.os.Parcel; |
| 29 |
import android.os.Parcelable; |
| 30 |
import android.os.RemoteException; |
| 31 |
import android.util.ArrayMap; |
| 32 |
import android.util.DebugUtils; |
| 33 |
import android.util.Log; |
| 34 |
|
| 35 |
import com.android.internal.app.IVoiceInteractor; |
| 36 |
import com.android.internal.app.IVoiceInteractorCallback; |
| 37 |
import com.android.internal.app.IVoiceInteractorRequest; |
| 38 |
import com.android.internal.os.HandlerCaller; |
| 39 |
import com.android.internal.os.SomeArgs; |
| 40 |
import com.android.internal.util.function.pooled.PooledLambda; |
| 41 |
|
| 42 |
import java.io.FileDescriptor; |
| 43 |
import java.io.PrintWriter; |
| 44 |
import java.lang.ref.WeakReference; |
| 45 |
import java.util.ArrayList; |
| 46 |
import java.util.Arrays; |
| 47 |
import java.util.Objects; |
| 48 |
import java.util.concurrent.Executor; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public final class VoiceInteractor { |
| 73 |
static final String TAG = "VoiceInteractor"; |
| 74 |
static final boolean DEBUG = false; |
| 75 |
|
| 76 |
static final Request[] NO_REQUESTS = new Request[0]; |
| 77 |
|
| 78 |
|
| 79 |
public static final String KEY_CANCELLATION_SIGNAL = "key_cancellation_signal"; |
| 80 |
|
| 81 |
@Nullable IVoiceInteractor mInteractor; |
| 82 |
|
| 83 |
@Nullable Context mContext; |
| 84 |
@Nullable Activity mActivity; |
| 85 |
boolean mRetaining; |
| 86 |
|
| 87 |
final HandlerCaller mHandlerCaller; |
| 88 |
final HandlerCaller.Callback mHandlerCallerCallback = new HandlerCaller.Callback() { |
| 89 |
@Override |
| 90 |
public void executeMessage(Message msg) { |
| 91 |
SomeArgs args = (SomeArgs)msg.obj; |
| 92 |
Request request; |
| 93 |
boolean complete; |
| 94 |
switch (msg.what) { |
| 95 |
case MSG_CONFIRMATION_RESULT: |
| 96 |
request = pullRequest((IVoiceInteractorRequest)args.arg1, true); |
| 97 |
if (DEBUG) Log.d(TAG, "onConfirmResult: req=" |
| 98 |
+ ((IVoiceInteractorRequest)args.arg1).asBinder() + "/" + request |
| 99 |
+ " confirmed=" + msg.arg1 + " result=" + args.arg2); |
| 100 |
if (request != null) { |
| 101 |
((ConfirmationRequest)request).onConfirmationResult(msg.arg1 != 0, |
| 102 |
(Bundle) args.arg2); |
| 103 |
request.clear(); |
| 104 |
} |
| 105 |
break; |
| 106 |
case MSG_PICK_OPTION_RESULT: |
| 107 |
complete = msg.arg1 != 0; |
| 108 |
request = pullRequest((IVoiceInteractorRequest)args.arg1, complete); |
| 109 |
if (DEBUG) Log.d(TAG, "onPickOptionResult: req=" |
| 110 |
+ ((IVoiceInteractorRequest)args.arg1).asBinder() + "/" + request |
| 111 |
+ " finished=" + complete + " selection=" + args.arg2 |
| 112 |
+ " result=" + args.arg3); |
| 113 |
if (request != null) { |
| 114 |
((PickOptionRequest)request).onPickOptionResult(complete, |
| 115 |
(PickOptionRequest.Option[]) args.arg2, (Bundle) args.arg3); |
| 116 |
if (complete) { |
| 117 |
request.clear(); |
| 118 |
} |
| 119 |
} |
| 120 |
break; |
| 121 |
case MSG_COMPLETE_VOICE_RESULT: |
| 122 |
request = pullRequest((IVoiceInteractorRequest)args.arg1, true); |
| 123 |
if (DEBUG) Log.d(TAG, "onCompleteVoice: req=" |
| 124 |
+ ((IVoiceInteractorRequest)args.arg1).asBinder() + "/" + request |
| 125 |
+ " result=" + args.arg2); |
| 126 |
if (request != null) { |
| 127 |
((CompleteVoiceRequest)request).onCompleteResult((Bundle) args.arg2); |
| 128 |
request.clear(); |
| 129 |
} |
| 130 |
break; |
| 131 |
case MSG_ABORT_VOICE_RESULT: |
| 132 |
request = pullRequest((IVoiceInteractorRequest)args.arg1, true); |
| 133 |
if (DEBUG) Log.d(TAG, "onAbortVoice: req=" |
| 134 |
+ ((IVoiceInteractorRequest)args.arg1).asBinder() + "/" + request |
| 135 |
+ " result=" + args.arg2); |
| 136 |
if (request != null) { |
| 137 |
((AbortVoiceRequest)request).onAbortResult((Bundle) args.arg2); |
| 138 |
request.clear(); |
| 139 |
} |
| 140 |
break; |
| 141 |
case MSG_COMMAND_RESULT: |
| 142 |
complete = msg.arg1 != 0; |
| 143 |
request = pullRequest((IVoiceInteractorRequest)args.arg1, complete); |
| 144 |
if (DEBUG) Log.d(TAG, "onCommandResult: req=" |
| 145 |
+ ((IVoiceInteractorRequest)args.arg1).asBinder() + "/" + request |
| 146 |
+ " completed=" + msg.arg1 + " result=" + args.arg2); |
| 147 |
if (request != null) { |
| 148 |
((CommandRequest)request).onCommandResult(msg.arg1 != 0, |
| 149 |
(Bundle) args.arg2); |
| 150 |
if (complete) { |
| 151 |
request.clear(); |
| 152 |
} |
| 153 |
} |
| 154 |
break; |
| 155 |
case MSG_CANCEL_RESULT: |
| 156 |
request = pullRequest((IVoiceInteractorRequest)args.arg1, true); |
| 157 |
if (DEBUG) Log.d(TAG, "onCancelResult: req=" |
| 158 |
+ ((IVoiceInteractorRequest)args.arg1).asBinder() + "/" + request); |
| 159 |
if (request != null) { |
| 160 |
request.onCancel(); |
| 161 |
request.clear(); |
| 162 |
} |
| 163 |
break; |
| 164 |
} |
| 165 |
} |
| 166 |
}; |
| 167 |
|
| 168 |
final IVoiceInteractorCallback.Stub mCallback = new IVoiceInteractorCallback.Stub() { |
| 169 |
@Override |
| 170 |
public void deliverConfirmationResult(IVoiceInteractorRequest request, boolean finished, |
| 171 |
Bundle result) { |
| 172 |
mHandlerCaller.sendMessage(mHandlerCaller.obtainMessageIOO( |
| 173 |
MSG_CONFIRMATION_RESULT, finished ? 1 : 0, request, result)); |
| 174 |
} |
| 175 |
|
| 176 |
@Override |
| 177 |
public void deliverPickOptionResult(IVoiceInteractorRequest request, |
| 178 |
boolean finished, PickOptionRequest.Option[] options, Bundle result) { |
| 179 |
mHandlerCaller.sendMessage(mHandlerCaller.obtainMessageIOOO( |
| 180 |
MSG_PICK_OPTION_RESULT, finished ? 1 : 0, request, options, result)); |
| 181 |
} |
| 182 |
|
| 183 |
@Override |
| 184 |
public void deliverCompleteVoiceResult(IVoiceInteractorRequest request, Bundle result) { |
| 185 |
mHandlerCaller.sendMessage(mHandlerCaller.obtainMessageOO( |
| 186 |
MSG_COMPLETE_VOICE_RESULT, request, result)); |
| 187 |
} |
| 188 |
|
| 189 |
@Override |
| 190 |
public void deliverAbortVoiceResult(IVoiceInteractorRequest request, Bundle result) { |
| 191 |
mHandlerCaller.sendMessage(mHandlerCaller.obtainMessageOO( |
| 192 |
MSG_ABORT_VOICE_RESULT, request, result)); |
| 193 |
} |
| 194 |
|
| 195 |
@Override |
| 196 |
public void deliverCommandResult(IVoiceInteractorRequest request, boolean complete, |
| 197 |
Bundle result) { |
| 198 |
mHandlerCaller.sendMessage(mHandlerCaller.obtainMessageIOO( |
| 199 |
MSG_COMMAND_RESULT, complete ? 1 : 0, request, result)); |
| 200 |
} |
| 201 |
|
| 202 |
@Override |
| 203 |
public void deliverCancel(IVoiceInteractorRequest request) { |
| 204 |
mHandlerCaller.sendMessage(mHandlerCaller.obtainMessageOO( |
| 205 |
MSG_CANCEL_RESULT, request, null)); |
| 206 |
} |
| 207 |
|
| 208 |
@Override |
| 209 |
public void destroy() { |
| 210 |
mHandlerCaller.getHandler().sendMessage(PooledLambda.obtainMessage( |
| 211 |
VoiceInteractor::destroy, VoiceInteractor.this)); |
| 212 |
} |
| 213 |
}; |
| 214 |
|
| 215 |
final ArrayMap<IBinder, Request> mActiveRequests = new ArrayMap<>(); |
| 216 |
final ArrayMap<Runnable, Executor> mOnDestroyCallbacks = new ArrayMap<>(); |
| 217 |
|
| 218 |
static final int MSG_CONFIRMATION_RESULT = 1; |
| 219 |
static final int MSG_PICK_OPTION_RESULT = 2; |
| 220 |
static final int MSG_COMPLETE_VOICE_RESULT = 3; |
| 221 |
static final int MSG_ABORT_VOICE_RESULT = 4; |
| 222 |
static final int MSG_COMMAND_RESULT = 5; |
| 223 |
static final int MSG_CANCEL_RESULT = 6; |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
public static abstract class Request { |
| 230 |
IVoiceInteractorRequest mRequestInterface; |
| 231 |
Context mContext; |
| 232 |
Activity mActivity; |
| 233 |
String mName; |
| 234 |
|
| 235 |
Request() { |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
public String getName() { |
| 243 |
return mName; |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
public void cancel() { |
| 250 |
if (mRequestInterface == null) { |
| 251 |
throw new IllegalStateException("Request " + this + " is no longer active"); |
| 252 |
} |
| 253 |
try { |
| 254 |
mRequestInterface.cancel(); |
| 255 |
} catch (RemoteException e) { |
| 256 |
Log.w(TAG, "Voice interactor has died", e); |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
public Context getContext() { |
| 265 |
return mContext; |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
public Activity getActivity() { |
| 273 |
return mActivity; |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
public void onCancel() { |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
public void onAttached(Activity activity) { |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
public void onDetached() { |
| 295 |
} |
| 296 |
|
| 297 |
@Override |
| 298 |
public String toString() { |
| 299 |
StringBuilder sb = new StringBuilder(128); |
| 300 |
DebugUtils.buildShortClassTag(this, sb); |
| 301 |
sb.append(" "); |
| 302 |
sb.append(getRequestTypeName()); |
| 303 |
sb.append(" name="); |
| 304 |
sb.append(mName); |
| 305 |
sb.append('}'); |
| 306 |
return sb.toString(); |
| 307 |
} |
| 308 |
|
| 309 |
void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { |
| 310 |
writer.print(prefix); writer.print("mRequestInterface="); |
| 311 |
writer.println(mRequestInterface.asBinder()); |
| 312 |
writer.print(prefix); writer.print("mActivity="); writer.println(mActivity); |
| 313 |
writer.print(prefix); writer.print("mName="); writer.println(mName); |
| 314 |
} |
| 315 |
|
| 316 |
String getRequestTypeName() { |
| 317 |
return "Request"; |
| 318 |
} |
| 319 |
|
| 320 |
void clear() { |
| 321 |
mRequestInterface = null; |
| 322 |
mContext = null; |
| 323 |
mActivity = null; |
| 324 |
mName = null; |
| 325 |
} |
| 326 |
|
| 327 |
abstract IVoiceInteractorRequest submit(IVoiceInteractor interactor, |
| 328 |
String packageName, IVoiceInteractorCallback callback) throws RemoteException; |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
public static class ConfirmationRequest extends Request { |
| 346 |
final Prompt mPrompt; |
| 347 |
final Bundle mExtras; |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
public ConfirmationRequest(@Nullable Prompt prompt, @Nullable Bundle extras) { |
| 356 |
mPrompt = prompt; |
| 357 |
mExtras = extras; |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
public ConfirmationRequest(CharSequence prompt, Bundle extras) { |
| 368 |
mPrompt = (prompt != null ? new Prompt(prompt) : null); |
| 369 |
mExtras = extras; |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
public void onConfirmationResult(boolean confirmed, Bundle result) { |
| 379 |
} |
| 380 |
|
| 381 |
void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { |
| 382 |
super.dump(prefix, fd, writer, args); |
| 383 |
writer.print(prefix); writer.print("mPrompt="); writer.println(mPrompt); |
| 384 |
if (mExtras != null) { |
| 385 |
writer.print(prefix); writer.print("mExtras="); writer.println(mExtras); |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
String getRequestTypeName() { |
| 390 |
return "Confirmation"; |
| 391 |
} |
| 392 |
|
| 393 |
IVoiceInteractorRequest submit(IVoiceInteractor interactor, String packageName, |
| 394 |
IVoiceInteractorCallback callback) throws RemoteException { |
| 395 |
return interactor.startConfirmation(packageName, callback, mPrompt, mExtras); |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
public static class PickOptionRequest extends Request { |
| 408 |
final Prompt mPrompt; |
| 409 |
final Option[] mOptions; |
| 410 |
final Bundle mExtras; |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
public static final class Option implements Parcelable { |
| 418 |
final CharSequence mLabel; |
| 419 |
final int mIndex; |
| 420 |
ArrayList<CharSequence> mSynonyms; |
| 421 |
Bundle mExtras; |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
public Option(CharSequence label) { |
| 431 |
mLabel = label; |
| 432 |
mIndex = -1; |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
public Option(CharSequence label, int index) { |
| 445 |
mLabel = label; |
| 446 |
mIndex = index; |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
public Option addSynonym(CharSequence synonym) { |
| 456 |
if (mSynonyms == null) { |
| 457 |
mSynonyms = new ArrayList<>(); |
| 458 |
} |
| 459 |
mSynonyms.add(synonym); |
| 460 |
return this; |
| 461 |
} |
| 462 |
|
| 463 |
public CharSequence getLabel() { |
| 464 |
return mLabel; |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
public int getIndex() { |
| 472 |
return mIndex; |
| 473 |
} |
| 474 |
|
| 475 |
public int countSynonyms() { |
| 476 |
return mSynonyms != null ? mSynonyms.size() : 0; |
| 477 |
} |
| 478 |
|
| 479 |
public CharSequence getSynonymAt(int index) { |
| 480 |
return mSynonyms != null ? mSynonyms.get(index) : null; |
| 481 |
} |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
public void setExtras(Bundle extras) { |
| 488 |
mExtras = extras; |
| 489 |
} |
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
public Bundle getExtras() { |
| 498 |
return mExtras; |
| 499 |
} |
| 500 |
|
| 501 |
Option(Parcel in) { |
| 502 |
mLabel = in.readCharSequence(); |
| 503 |
mIndex = in.readInt(); |
| 504 |
mSynonyms = in.readCharSequenceList(); |
| 505 |
mExtras = in.readBundle(); |
| 506 |
} |
| 507 |
|
| 508 |
@Override |
| 509 |
public int describeContents() { |
| 510 |
return 0; |
| 511 |
} |
| 512 |
|
| 513 |
@Override |
| 514 |
public void writeToParcel(Parcel dest, int flags) { |
| 515 |
dest.writeCharSequence(mLabel); |
| 516 |
dest.writeInt(mIndex); |
| 517 |
dest.writeCharSequenceList(mSynonyms); |
| 518 |
dest.writeBundle(mExtras); |
| 519 |
} |
| 520 |
|
| 521 |
public static final @android.annotation.NonNull Parcelable.Creator<Option> CREATOR |
| 522 |
= new Parcelable.Creator<Option>() { |
| 523 |
public Option createFromParcel(Parcel in) { |
| 524 |
return new Option(in); |
| 525 |
} |
| 526 |
|
| 527 |
public Option[] newArray(int size) { |
| 528 |
return new Option[size]; |
| 529 |
} |
| 530 |
}; |
| 531 |
}; |
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
public PickOptionRequest(@Nullable Prompt prompt, Option[] options, |
| 541 |
@Nullable Bundle extras) { |
| 542 |
mPrompt = prompt; |
| 543 |
mOptions = options; |
| 544 |
mExtras = extras; |
| 545 |
} |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
public PickOptionRequest(CharSequence prompt, Option[] options, Bundle extras) { |
| 556 |
mPrompt = (prompt != null ? new Prompt(prompt) : null); |
| 557 |
mOptions = options; |
| 558 |
mExtras = extras; |
| 559 |
} |
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
public void onPickOptionResult(boolean finished, Option[] selections, Bundle result) { |
| 573 |
} |
| 574 |
|
| 575 |
void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { |
| 576 |
super.dump(prefix, fd, writer, args); |
| 577 |
writer.print(prefix); writer.print("mPrompt="); writer.println(mPrompt); |
| 578 |
if (mOptions != null) { |
| 579 |
writer.print(prefix); writer.println("Options:"); |
| 580 |
for (int i=0; i<mOptions.length; i++) { |
| 581 |
Option op = mOptions[i]; |
| 582 |
writer.print(prefix); writer.print(" #"); writer.print(i); writer.println(":"); |
| 583 |
writer.print(prefix); writer.print(" mLabel="); writer.println(op.mLabel); |
| 584 |
writer.print(prefix); writer.print(" mIndex="); writer.println(op.mIndex); |
| 585 |
if (op.mSynonyms != null && op.mSynonyms.size() > 0) { |
| 586 |
writer.print(prefix); writer.println(" Synonyms:"); |
| 587 |
for (int j=0; j<op.mSynonyms.size(); j++) { |
| 588 |
writer.print(prefix); writer.print(" #"); writer.print(j); |
| 589 |
writer.print(": "); writer.println(op.mSynonyms.get(j)); |
| 590 |
} |
| 591 |
} |
| 592 |
if (op.mExtras != null) { |
| 593 |
writer.print(prefix); writer.print(" mExtras="); |
| 594 |
writer.println(op.mExtras); |
| 595 |
} |
| 596 |
} |
| 597 |
} |
| 598 |
if (mExtras != null) { |
| 599 |
writer.print(prefix); writer.print("mExtras="); writer.println(mExtras); |
| 600 |
} |
| 601 |
} |
| 602 |
|
| 603 |
String getRequestTypeName() { |
| 604 |
return "PickOption"; |
| 605 |
} |
| 606 |
|
| 607 |
IVoiceInteractorRequest submit(IVoiceInteractor interactor, String packageName, |
| 608 |
IVoiceInteractorCallback callback) throws RemoteException { |
| 609 |
return interactor.startPickOption(packageName, callback, mPrompt, mOptions, mExtras); |
| 610 |
} |
| 611 |
} |
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
public static class CompleteVoiceRequest extends Request { |
| 623 |
final Prompt mPrompt; |
| 624 |
final Bundle mExtras; |
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
public CompleteVoiceRequest(@Nullable Prompt prompt, @Nullable Bundle extras) { |
| 633 |
mPrompt = prompt; |
| 634 |
mExtras = extras; |
| 635 |
} |
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
public CompleteVoiceRequest(CharSequence message, Bundle extras) { |
| 645 |
mPrompt = (message != null ? new Prompt(message) : null); |
| 646 |
mExtras = extras; |
| 647 |
} |
| 648 |
|
| 649 |
public void onCompleteResult(Bundle result) { |
| 650 |
} |
| 651 |
|
| 652 |
void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { |
| 653 |
super.dump(prefix, fd, writer, args); |
| 654 |
writer.print(prefix); writer.print("mPrompt="); writer.println(mPrompt); |
| 655 |
if (mExtras != null) { |
| 656 |
writer.print(prefix); writer.print("mExtras="); writer.println(mExtras); |
| 657 |
} |
| 658 |
} |
| 659 |
|
| 660 |
String getRequestTypeName() { |
| 661 |
return "CompleteVoice"; |
| 662 |
} |
| 663 |
|
| 664 |
IVoiceInteractorRequest submit(IVoiceInteractor interactor, String packageName, |
| 665 |
IVoiceInteractorCallback callback) throws RemoteException { |
| 666 |
return interactor.startCompleteVoice(packageName, callback, mPrompt, mExtras); |
| 667 |
} |
| 668 |
} |
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
public static class AbortVoiceRequest extends Request { |
| 682 |
final Prompt mPrompt; |
| 683 |
final Bundle mExtras; |
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
public AbortVoiceRequest(@Nullable Prompt prompt, @Nullable Bundle extras) { |
| 692 |
mPrompt = prompt; |
| 693 |
mExtras = extras; |
| 694 |
} |
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
public AbortVoiceRequest(CharSequence message, Bundle extras) { |
| 704 |
mPrompt = (message != null ? new Prompt(message) : null); |
| 705 |
mExtras = extras; |
| 706 |
} |
| 707 |
|
| 708 |
public void onAbortResult(Bundle result) { |
| 709 |
} |
| 710 |
|
| 711 |
void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { |
| 712 |
super.dump(prefix, fd, writer, args); |
| 713 |
writer.print(prefix); writer.print("mPrompt="); writer.println(mPrompt); |
| 714 |
if (mExtras != null) { |
| 715 |
writer.print(prefix); writer.print("mExtras="); writer.println(mExtras); |
| 716 |
} |
| 717 |
} |
| 718 |
|
| 719 |
String getRequestTypeName() { |
| 720 |
return "AbortVoice"; |
| 721 |
} |
| 722 |
|
| 723 |
IVoiceInteractorRequest submit(IVoiceInteractor interactor, String packageName, |
| 724 |
IVoiceInteractorCallback callback) throws RemoteException { |
| 725 |
return interactor.startAbortVoice(packageName, callback, mPrompt, mExtras); |
| 726 |
} |
| 727 |
} |
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
public static class CommandRequest extends Request { |
| 745 |
final String mCommand; |
| 746 |
final Bundle mArgs; |
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
public CommandRequest(String command, Bundle args) { |
| 754 |
mCommand = command; |
| 755 |
mArgs = args; |
| 756 |
} |
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
public void onCommandResult(boolean isCompleted, Bundle result) { |
| 764 |
} |
| 765 |
|
| 766 |
void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { |
| 767 |
super.dump(prefix, fd, writer, args); |
| 768 |
writer.print(prefix); writer.print("mCommand="); writer.println(mCommand); |
| 769 |
if (mArgs != null) { |
| 770 |
writer.print(prefix); writer.print("mArgs="); writer.println(mArgs); |
| 771 |
} |
| 772 |
} |
| 773 |
|
| 774 |
String getRequestTypeName() { |
| 775 |
return "Command"; |
| 776 |
} |
| 777 |
|
| 778 |
IVoiceInteractorRequest submit(IVoiceInteractor interactor, String packageName, |
| 779 |
IVoiceInteractorCallback callback) throws RemoteException { |
| 780 |
return interactor.startCommand(packageName, callback, mCommand, mArgs); |
| 781 |
} |
| 782 |
} |
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
public static class Prompt implements Parcelable { |
| 792 |
|
| 793 |
private final CharSequence[] mVoicePrompts; |
| 794 |
|
| 795 |
|
| 796 |
private final CharSequence mVisualPrompt; |
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
public Prompt(@NonNull CharSequence[] voicePrompts, @NonNull CharSequence visualPrompt) { |
| 804 |
if (voicePrompts == null) { |
| 805 |
throw new NullPointerException("voicePrompts must not be null"); |
| 806 |
} |
| 807 |
if (voicePrompts.length == 0) { |
| 808 |
throw new IllegalArgumentException("voicePrompts must not be empty"); |
| 809 |
} |
| 810 |
if (visualPrompt == null) { |
| 811 |
throw new NullPointerException("visualPrompt must not be null"); |
| 812 |
} |
| 813 |
this.mVoicePrompts = voicePrompts; |
| 814 |
this.mVisualPrompt = visualPrompt; |
| 815 |
} |
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
public Prompt(@NonNull CharSequence prompt) { |
| 822 |
this.mVoicePrompts = new CharSequence[] { prompt }; |
| 823 |
this.mVisualPrompt = prompt; |
| 824 |
} |
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
@NonNull |
| 830 |
public CharSequence getVoicePromptAt(int index) { |
| 831 |
return mVoicePrompts[index]; |
| 832 |
} |
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
public int countVoicePrompts() { |
| 838 |
return mVoicePrompts.length; |
| 839 |
} |
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
@NonNull |
| 845 |
public CharSequence getVisualPrompt() { |
| 846 |
return mVisualPrompt; |
| 847 |
} |
| 848 |
|
| 849 |
@Override |
| 850 |
public String toString() { |
| 851 |
StringBuilder sb = new StringBuilder(128); |
| 852 |
DebugUtils.buildShortClassTag(this, sb); |
| 853 |
if (mVisualPrompt != null && mVoicePrompts != null && mVoicePrompts.length == 1 |
| 854 |
&& mVisualPrompt.equals(mVoicePrompts[0])) { |
| 855 |
sb.append(" "); |
| 856 |
sb.append(mVisualPrompt); |
| 857 |
} else { |
| 858 |
if (mVisualPrompt != null) { |
| 859 |
sb.append(" visual="); sb.append(mVisualPrompt); |
| 860 |
} |
| 861 |
if (mVoicePrompts != null) { |
| 862 |
sb.append(", voice="); |
| 863 |
for (int i=0; i<mVoicePrompts.length; i++) { |
| 864 |
if (i > 0) sb.append(" | "); |
| 865 |
sb.append(mVoicePrompts[i]); |
| 866 |
} |
| 867 |
} |
| 868 |
} |
| 869 |
sb.append('}'); |
| 870 |
return sb.toString(); |
| 871 |
} |
| 872 |
|
| 873 |
|
| 874 |
Prompt(Parcel in) { |
| 875 |
mVoicePrompts = in.readCharSequenceArray(); |
| 876 |
mVisualPrompt = in.readCharSequence(); |
| 877 |
} |
| 878 |
|
| 879 |
@Override |
| 880 |
public int describeContents() { |
| 881 |
return 0; |
| 882 |
} |
| 883 |
|
| 884 |
@Override |
| 885 |
public void writeToParcel(Parcel dest, int flags) { |
| 886 |
dest.writeCharSequenceArray(mVoicePrompts); |
| 887 |
dest.writeCharSequence(mVisualPrompt); |
| 888 |
} |
| 889 |
|
| 890 |
public static final @android.annotation.NonNull Creator<Prompt> CREATOR |
| 891 |
= new Creator<Prompt>() { |
| 892 |
public Prompt createFromParcel(Parcel in) { |
| 893 |
return new Prompt(in); |
| 894 |
} |
| 895 |
|
| 896 |
public Prompt[] newArray(int size) { |
| 897 |
return new Prompt[size]; |
| 898 |
} |
| 899 |
}; |
| 900 |
} |
| 901 |
|
| 902 |
VoiceInteractor(IVoiceInteractor interactor, Context context, Activity activity, |
| 903 |
Looper looper) { |
| 904 |
mInteractor = interactor; |
| 905 |
mContext = context; |
| 906 |
mActivity = activity; |
| 907 |
mHandlerCaller = new HandlerCaller(context, looper, mHandlerCallerCallback, true); |
| 908 |
try { |
| 909 |
mInteractor.setKillCallback(new KillCallback(this)); |
| 910 |
} catch (RemoteException e) { |
| 911 |
|
| 912 |
} |
| 913 |
} |
| 914 |
|
| 915 |
Request pullRequest(IVoiceInteractorRequest request, boolean complete) { |
| 916 |
synchronized (mActiveRequests) { |
| 917 |
Request req = mActiveRequests.get(request.asBinder()); |
| 918 |
if (req != null && complete) { |
| 919 |
mActiveRequests.remove(request.asBinder()); |
| 920 |
} |
| 921 |
return req; |
| 922 |
} |
| 923 |
} |
| 924 |
|
| 925 |
private ArrayList<Request> makeRequestList() { |
| 926 |
final int N = mActiveRequests.size(); |
| 927 |
if (N < 1) { |
| 928 |
return null; |
| 929 |
} |
| 930 |
ArrayList<Request> list = new ArrayList<>(N); |
| 931 |
for (int i=0; i<N; i++) { |
| 932 |
list.add(mActiveRequests.valueAt(i)); |
| 933 |
} |
| 934 |
return list; |
| 935 |
} |
| 936 |
|
| 937 |
void attachActivity(Activity activity) { |
| 938 |
mRetaining = false; |
| 939 |
if (mActivity == activity) { |
| 940 |
return; |
| 941 |
} |
| 942 |
mContext = activity; |
| 943 |
mActivity = activity; |
| 944 |
ArrayList<Request> reqs = makeRequestList(); |
| 945 |
if (reqs != null) { |
| 946 |
for (int i=0; i<reqs.size(); i++) { |
| 947 |
Request req = reqs.get(i); |
| 948 |
req.mContext = activity; |
| 949 |
req.mActivity = activity; |
| 950 |
req.onAttached(activity); |
| 951 |
} |
| 952 |
} |
| 953 |
} |
| 954 |
|
| 955 |
void retainInstance() { |
| 956 |
mRetaining = true; |
| 957 |
} |
| 958 |
|
| 959 |
void detachActivity() { |
| 960 |
ArrayList<Request> reqs = makeRequestList(); |
| 961 |
if (reqs != null) { |
| 962 |
for (int i=0; i<reqs.size(); i++) { |
| 963 |
Request req = reqs.get(i); |
| 964 |
req.onDetached(); |
| 965 |
req.mActivity = null; |
| 966 |
req.mContext = null; |
| 967 |
} |
| 968 |
} |
| 969 |
if (!mRetaining) { |
| 970 |
reqs = makeRequestList(); |
| 971 |
if (reqs != null) { |
| 972 |
for (int i=0; i<reqs.size(); i++) { |
| 973 |
Request req = reqs.get(i); |
| 974 |
req.cancel(); |
| 975 |
} |
| 976 |
} |
| 977 |
mActiveRequests.clear(); |
| 978 |
} |
| 979 |
mContext = null; |
| 980 |
mActivity = null; |
| 981 |
} |
| 982 |
|
| 983 |
void destroy() { |
| 984 |
final int requestCount = mActiveRequests.size(); |
| 985 |
for (int i = requestCount - 1; i >= 0; i--) { |
| 986 |
final Request request = mActiveRequests.valueAt(i); |
| 987 |
mActiveRequests.removeAt(i); |
| 988 |
request.cancel(); |
| 989 |
} |
| 990 |
|
| 991 |
final int callbackCount = mOnDestroyCallbacks.size(); |
| 992 |
for (int i = callbackCount - 1; i >= 0; i--) { |
| 993 |
final Runnable callback = mOnDestroyCallbacks.keyAt(i); |
| 994 |
final Executor executor = mOnDestroyCallbacks.valueAt(i); |
| 995 |
executor.execute(callback); |
| 996 |
mOnDestroyCallbacks.removeAt(i); |
| 997 |
} |
| 998 |
|
| 999 |
|
| 1000 |
mInteractor = null; |
| 1001 |
if (mActivity != null) { |
| 1002 |
mActivity.setVoiceInteractor(null); |
| 1003 |
} |
| 1004 |
} |
| 1005 |
|
| 1006 |
public boolean submitRequest(Request request) { |
| 1007 |
return submitRequest(request, null); |
| 1008 |
} |
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
public boolean submitRequest(Request request, String name) { |
| 1022 |
if (isDestroyed()) { |
| 1023 |
Log.w(TAG, "Cannot interact with a destroyed voice interactor"); |
| 1024 |
return false; |
| 1025 |
} |
| 1026 |
try { |
| 1027 |
if (request.mRequestInterface != null) { |
| 1028 |
throw new IllegalStateException("Given " + request + " is already active"); |
| 1029 |
} |
| 1030 |
IVoiceInteractorRequest ireq = request.submit(mInteractor, |
| 1031 |
mContext.getOpPackageName(), mCallback); |
| 1032 |
request.mRequestInterface = ireq; |
| 1033 |
request.mContext = mContext; |
| 1034 |
request.mActivity = mActivity; |
| 1035 |
request.mName = name; |
| 1036 |
synchronized (mActiveRequests) { |
| 1037 |
mActiveRequests.put(ireq.asBinder(), request); |
| 1038 |
} |
| 1039 |
return true; |
| 1040 |
} catch (RemoteException e) { |
| 1041 |
Log.w(TAG, "Remove voice interactor service died", e); |
| 1042 |
return false; |
| 1043 |
} |
| 1044 |
} |
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
public Request[] getActiveRequests() { |
| 1050 |
if (isDestroyed()) { |
| 1051 |
Log.w(TAG, "Cannot interact with a destroyed voice interactor"); |
| 1052 |
return null; |
| 1053 |
} |
| 1054 |
synchronized (mActiveRequests) { |
| 1055 |
final int N = mActiveRequests.size(); |
| 1056 |
if (N <= 0) { |
| 1057 |
return NO_REQUESTS; |
| 1058 |
} |
| 1059 |
Request[] requests = new Request[N]; |
| 1060 |
for (int i=0; i<N; i++) { |
| 1061 |
requests[i] = mActiveRequests.valueAt(i); |
| 1062 |
} |
| 1063 |
return requests; |
| 1064 |
} |
| 1065 |
} |
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
public Request getActiveRequest(String name) { |
| 1075 |
if (isDestroyed()) { |
| 1076 |
Log.w(TAG, "Cannot interact with a destroyed voice interactor"); |
| 1077 |
return null; |
| 1078 |
} |
| 1079 |
synchronized (mActiveRequests) { |
| 1080 |
final int N = mActiveRequests.size(); |
| 1081 |
for (int i=0; i<N; i++) { |
| 1082 |
Request req = mActiveRequests.valueAt(i); |
| 1083 |
if (name == req.getName() || (name != null && name.equals(req.getName()))) { |
| 1084 |
return req; |
| 1085 |
} |
| 1086 |
} |
| 1087 |
} |
| 1088 |
return null; |
| 1089 |
} |
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
public boolean[] supportsCommands(String[] commands) { |
| 1101 |
if (isDestroyed()) { |
| 1102 |
Log.w(TAG, "Cannot interact with a destroyed voice interactor"); |
| 1103 |
return new boolean[commands.length]; |
| 1104 |
} |
| 1105 |
try { |
| 1106 |
boolean[] res = mInteractor.supportsCommands(mContext.getOpPackageName(), commands); |
| 1107 |
if (DEBUG) { |
| 1108 |
Log.d(TAG, "supportsCommands: cmds=" + Arrays.toString(commands) + " res=" |
| 1109 |
+ Arrays.toString(res)); |
| 1110 |
} |
| 1111 |
return res; |
| 1112 |
} catch (RemoteException e) { |
| 1113 |
throw new RuntimeException("Voice interactor has died", e); |
| 1114 |
} |
| 1115 |
} |
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
public boolean isDestroyed() { |
| 1122 |
return mInteractor == null; |
| 1123 |
} |
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
public boolean registerOnDestroyedCallback(@NonNull @CallbackExecutor Executor executor, |
| 1133 |
@NonNull Runnable callback) { |
| 1134 |
Objects.requireNonNull(executor); |
| 1135 |
Objects.requireNonNull(callback); |
| 1136 |
if (isDestroyed()) { |
| 1137 |
Log.w(TAG, "Cannot interact with a destroyed voice interactor"); |
| 1138 |
return false; |
| 1139 |
} |
| 1140 |
mOnDestroyCallbacks.put(callback, executor); |
| 1141 |
return true; |
| 1142 |
} |
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
|
| 1149 |
|
| 1150 |
public boolean unregisterOnDestroyedCallback(@NonNull Runnable callback) { |
| 1151 |
Objects.requireNonNull(callback); |
| 1152 |
if (isDestroyed()) { |
| 1153 |
Log.w(TAG, "Cannot interact with a destroyed voice interactor"); |
| 1154 |
return false; |
| 1155 |
} |
| 1156 |
return mOnDestroyCallbacks.remove(callback) != null; |
| 1157 |
} |
| 1158 |
|
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
public void notifyDirectActionsChanged() { |
| 1163 |
if (isDestroyed()) { |
| 1164 |
Log.w(TAG, "Cannot interact with a destroyed voice interactor"); |
| 1165 |
return; |
| 1166 |
} |
| 1167 |
try { |
| 1168 |
mInteractor.notifyDirectActionsChanged(mActivity.getTaskId(), |
| 1169 |
mActivity.getAssistToken()); |
| 1170 |
} catch (RemoteException e) { |
| 1171 |
Log.w(TAG, "Voice interactor has died", e); |
| 1172 |
} |
| 1173 |
} |
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
@NonNull |
| 1179 |
public String getPackageName() { |
| 1180 |
String packageName = null; |
| 1181 |
if (mActivity != null && mInteractor != null) { |
| 1182 |
try { |
| 1183 |
packageName = ActivityTaskManager.getService() |
| 1184 |
.getVoiceInteractorPackageName(mInteractor.asBinder()); |
| 1185 |
} catch (RemoteException e) { |
| 1186 |
throw e.rethrowFromSystemServer(); |
| 1187 |
} |
| 1188 |
} |
| 1189 |
return packageName == null ? "" : packageName; |
| 1190 |
} |
| 1191 |
|
| 1192 |
void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { |
| 1193 |
String innerPrefix = prefix + " "; |
| 1194 |
if (mActiveRequests.size() > 0) { |
| 1195 |
writer.print(prefix); writer.println("Active voice requests:"); |
| 1196 |
for (int i=0; i<mActiveRequests.size(); i++) { |
| 1197 |
Request req = mActiveRequests.valueAt(i); |
| 1198 |
writer.print(prefix); writer.print(" #"); writer.print(i); |
| 1199 |
writer.print(": "); |
| 1200 |
writer.println(req); |
| 1201 |
req.dump(innerPrefix, fd, writer, args); |
| 1202 |
} |
| 1203 |
} |
| 1204 |
writer.print(prefix); writer.println("VoiceInteractor misc state:"); |
| 1205 |
writer.print(prefix); writer.print(" mInteractor="); |
| 1206 |
writer.println(mInteractor.asBinder()); |
| 1207 |
writer.print(prefix); writer.print(" mActivity="); writer.println(mActivity); |
| 1208 |
} |
| 1209 |
|
| 1210 |
private static final class KillCallback extends ICancellationSignal.Stub { |
| 1211 |
private final WeakReference<VoiceInteractor> mInteractor; |
| 1212 |
|
| 1213 |
KillCallback(VoiceInteractor interactor) { |
| 1214 |
mInteractor= new WeakReference<>(interactor); |
| 1215 |
} |
| 1216 |
|
| 1217 |
@Override |
| 1218 |
public void cancel() { |
| 1219 |
final VoiceInteractor voiceInteractor = mInteractor.get(); |
| 1220 |
if (voiceInteractor != null) { |
| 1221 |
voiceInteractor.mHandlerCaller.getHandler().sendMessage(PooledLambda |
| 1222 |
.obtainMessage(VoiceInteractor::destroy, voiceInteractor)); |
| 1223 |
} |
| 1224 |
} |
| 1225 |
} |
| 1226 |
} |