| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package android.view.contentcapture; |
| 17 |
|
| 18 |
import static android.view.contentcapture.ContentCaptureHelper.sDebug; |
| 19 |
import static android.view.contentcapture.ContentCaptureHelper.sVerbose; |
| 20 |
import static android.view.contentcapture.ContentCaptureHelper.toSet; |
| 21 |
|
| 22 |
import android.annotation.CallbackExecutor; |
| 23 |
import android.annotation.IntDef; |
| 24 |
import android.annotation.NonNull; |
| 25 |
import android.annotation.Nullable; |
| 26 |
import android.annotation.RequiresPermission; |
| 27 |
import android.annotation.SystemApi; |
| 28 |
import android.annotation.SystemService; |
| 29 |
import android.annotation.TestApi; |
| 30 |
import android.annotation.UiThread; |
| 31 |
import android.annotation.UserIdInt; |
| 32 |
import android.app.Activity; |
| 33 |
import android.app.Service; |
| 34 |
import android.content.ComponentName; |
| 35 |
import android.content.ContentCaptureOptions; |
| 36 |
import android.content.Context; |
| 37 |
import android.graphics.Canvas; |
| 38 |
import android.os.Binder; |
| 39 |
import android.os.Handler; |
| 40 |
import android.os.IBinder; |
| 41 |
import android.os.Looper; |
| 42 |
import android.os.ParcelFileDescriptor; |
| 43 |
import android.os.RemoteException; |
| 44 |
import android.os.ServiceManager; |
| 45 |
import android.util.Dumpable; |
| 46 |
import android.util.Log; |
| 47 |
import android.util.Slog; |
| 48 |
import android.view.View; |
| 49 |
import android.view.ViewStructure; |
| 50 |
import android.view.WindowManager; |
| 51 |
import android.view.contentcapture.ContentCaptureSession.FlushReason; |
| 52 |
|
| 53 |
import com.android.internal.annotations.GuardedBy; |
| 54 |
import com.android.internal.util.SyncResultReceiver; |
| 55 |
|
| 56 |
import java.io.PrintWriter; |
| 57 |
import java.lang.annotation.Retention; |
| 58 |
import java.lang.annotation.RetentionPolicy; |
| 59 |
import java.lang.ref.WeakReference; |
| 60 |
import java.util.ArrayList; |
| 61 |
import java.util.HashMap; |
| 62 |
import java.util.Map; |
| 63 |
import java.util.Objects; |
| 64 |
import java.util.Set; |
| 65 |
import java.util.concurrent.Executor; |
| 66 |
import java.util.function.Consumer; |
| 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 |
@SystemService(Context.CONTENT_CAPTURE_MANAGER_SERVICE) |
| 213 |
public final class ContentCaptureManager { |
| 214 |
|
| 215 |
private static final String TAG = ContentCaptureManager.class.getSimpleName(); |
| 216 |
|
| 217 |
|
| 218 |
public static final boolean DEBUG = false; |
| 219 |
|
| 220 |
|
| 221 |
@TestApi |
| 222 |
public static final String DUMPABLE_NAME = "ContentCaptureManager"; |
| 223 |
|
| 224 |
|
| 225 |
public static final int DATA_SHARE_ERROR_UNKNOWN = 1; |
| 226 |
|
| 227 |
|
| 228 |
public static final int DATA_SHARE_ERROR_CONCURRENT_REQUEST = 2; |
| 229 |
|
| 230 |
|
| 231 |
public static final int DATA_SHARE_ERROR_TIMEOUT_INTERRUPTED = 3; |
| 232 |
|
| 233 |
|
| 234 |
@IntDef(flag = false, value = { |
| 235 |
DATA_SHARE_ERROR_UNKNOWN, |
| 236 |
DATA_SHARE_ERROR_CONCURRENT_REQUEST, |
| 237 |
DATA_SHARE_ERROR_TIMEOUT_INTERRUPTED |
| 238 |
}) |
| 239 |
@Retention(RetentionPolicy.SOURCE) |
| 240 |
public @interface DataShareError {} |
| 241 |
|
| 242 |
|
| 243 |
public static final int RESULT_CODE_OK = 0; |
| 244 |
|
| 245 |
public static final int RESULT_CODE_TRUE = 1; |
| 246 |
|
| 247 |
public static final int RESULT_CODE_FALSE = 2; |
| 248 |
|
| 249 |
public static final int RESULT_CODE_SECURITY_EXCEPTION = -1; |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
@SystemApi |
| 256 |
public static final int NO_SESSION_ID = 0; |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
private static final int SYNC_CALLS_TIMEOUT_MS = 5000; |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
@TestApi |
| 279 |
public static final String DEVICE_CONFIG_PROPERTY_SERVICE_EXPLICITLY_ENABLED = |
| 280 |
"service_explicitly_enabled"; |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
public static final String DEVICE_CONFIG_PROPERTY_REPORT_LIST_VIEW_CHILDREN = |
| 289 |
"report_list_view_children"; |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
@TestApi |
| 297 |
public static final String DEVICE_CONFIG_PROPERTY_MAX_BUFFER_SIZE = "max_buffer_size"; |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
@TestApi |
| 305 |
public static final String DEVICE_CONFIG_PROPERTY_IDLE_FLUSH_FREQUENCY = "idle_flush_frequency"; |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
@TestApi |
| 314 |
public static final String DEVICE_CONFIG_PROPERTY_TEXT_CHANGE_FLUSH_FREQUENCY = |
| 315 |
"text_change_flush_frequency"; |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
@TestApi |
| 325 |
public static final String DEVICE_CONFIG_PROPERTY_LOG_HISTORY_SIZE = "log_history_size"; |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
@TestApi |
| 336 |
public static final String DEVICE_CONFIG_PROPERTY_LOGGING_LEVEL = "logging_level"; |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
public static final String DEVICE_CONFIG_PROPERTY_IDLE_UNBIND_TIMEOUT = "idle_unbind_timeout"; |
| 346 |
|
| 347 |
|
| 348 |
@TestApi |
| 349 |
public static final int LOGGING_LEVEL_OFF = 0; |
| 350 |
|
| 351 |
|
| 352 |
@TestApi |
| 353 |
public static final int LOGGING_LEVEL_DEBUG = 1; |
| 354 |
|
| 355 |
|
| 356 |
@TestApi |
| 357 |
public static final int LOGGING_LEVEL_VERBOSE = 2; |
| 358 |
|
| 359 |
|
| 360 |
@IntDef(flag = false, value = { |
| 361 |
LOGGING_LEVEL_OFF, |
| 362 |
LOGGING_LEVEL_DEBUG, |
| 363 |
LOGGING_LEVEL_VERBOSE |
| 364 |
}) |
| 365 |
@Retention(RetentionPolicy.SOURCE) |
| 366 |
public @interface LoggingLevel {} |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
public static final int DEFAULT_MAX_BUFFER_SIZE = 500; |
| 371 |
|
| 372 |
public static final int DEFAULT_IDLE_FLUSHING_FREQUENCY_MS = 5_000; |
| 373 |
|
| 374 |
public static final int DEFAULT_TEXT_CHANGE_FLUSHING_FREQUENCY_MS = 1_000; |
| 375 |
|
| 376 |
public static final int DEFAULT_LOG_HISTORY_SIZE = 10; |
| 377 |
|
| 378 |
private final Object mLock = new Object(); |
| 379 |
|
| 380 |
@NonNull |
| 381 |
private final StrippedContext mContext; |
| 382 |
|
| 383 |
@NonNull |
| 384 |
private final IContentCaptureManager mService; |
| 385 |
|
| 386 |
@GuardedBy("mLock") |
| 387 |
private final LocalDataShareAdapterResourceManager mDataShareAdapterResourceManager; |
| 388 |
|
| 389 |
@NonNull |
| 390 |
final ContentCaptureOptions mOptions; |
| 391 |
|
| 392 |
|
| 393 |
@GuardedBy("mLock") |
| 394 |
private int mFlags; |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
@NonNull |
| 399 |
private final Handler mHandler; |
| 400 |
|
| 401 |
@GuardedBy("mLock") |
| 402 |
private MainContentCaptureSession mMainSession; |
| 403 |
|
| 404 |
@Nullable |
| 405 |
private Dumper mDumpable; |
| 406 |
|
| 407 |
|
| 408 |
public interface ContentCaptureClient { |
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
@NonNull |
| 413 |
ComponentName contentCaptureClientGetComponentName(); |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
static class StrippedContext { |
| 418 |
final String mPackageName; |
| 419 |
final String mContext; |
| 420 |
final @UserIdInt int mUserId; |
| 421 |
|
| 422 |
private StrippedContext(Context context) { |
| 423 |
mPackageName = context.getPackageName(); |
| 424 |
mContext = context.toString(); |
| 425 |
mUserId = context.getUserId(); |
| 426 |
} |
| 427 |
|
| 428 |
@Override |
| 429 |
public String toString() { |
| 430 |
return mContext; |
| 431 |
} |
| 432 |
|
| 433 |
public String getPackageName() { |
| 434 |
return mPackageName; |
| 435 |
} |
| 436 |
|
| 437 |
@UserIdInt |
| 438 |
public int getUserId() { |
| 439 |
return mUserId; |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
|
| 444 |
public ContentCaptureManager(@NonNull Context context, |
| 445 |
@NonNull IContentCaptureManager service, @NonNull ContentCaptureOptions options) { |
| 446 |
Objects.requireNonNull(context, "context cannot be null"); |
| 447 |
mContext = new StrippedContext(context); |
| 448 |
mService = Objects.requireNonNull(service, "service cannot be null"); |
| 449 |
mOptions = Objects.requireNonNull(options, "options cannot be null"); |
| 450 |
|
| 451 |
ContentCaptureHelper.setLoggingLevel(mOptions.loggingLevel); |
| 452 |
|
| 453 |
if (sVerbose) Log.v(TAG, "Constructor for " + context.getPackageName()); |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
mHandler = Handler.createAsync(Looper.getMainLooper()); |
| 459 |
|
| 460 |
mDataShareAdapterResourceManager = new LocalDataShareAdapterResourceManager(); |
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
@NonNull |
| 473 |
@UiThread |
| 474 |
public MainContentCaptureSession getMainContentCaptureSession() { |
| 475 |
synchronized (mLock) { |
| 476 |
if (mMainSession == null) { |
| 477 |
mMainSession = new MainContentCaptureSession(mContext, this, mHandler, mService); |
| 478 |
if (sVerbose) Log.v(TAG, "getMainContentCaptureSession(): created " + mMainSession); |
| 479 |
} |
| 480 |
return mMainSession; |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
@UiThread |
| 486 |
public void onActivityCreated(@NonNull IBinder applicationToken, |
| 487 |
@NonNull IBinder shareableActivityToken, @NonNull ComponentName activityComponent) { |
| 488 |
if (mOptions.lite) return; |
| 489 |
synchronized (mLock) { |
| 490 |
getMainContentCaptureSession().start(applicationToken, shareableActivityToken, |
| 491 |
activityComponent, mFlags); |
| 492 |
} |
| 493 |
} |
| 494 |
|
| 495 |
|
| 496 |
@UiThread |
| 497 |
public void onActivityResumed() { |
| 498 |
if (mOptions.lite) return; |
| 499 |
getMainContentCaptureSession().notifySessionResumed(); |
| 500 |
} |
| 501 |
|
| 502 |
|
| 503 |
@UiThread |
| 504 |
public void onActivityPaused() { |
| 505 |
if (mOptions.lite) return; |
| 506 |
getMainContentCaptureSession().notifySessionPaused(); |
| 507 |
} |
| 508 |
|
| 509 |
|
| 510 |
@UiThread |
| 511 |
public void onActivityDestroyed() { |
| 512 |
if (mOptions.lite) return; |
| 513 |
getMainContentCaptureSession().destroy(); |
| 514 |
} |
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
@UiThread |
| 524 |
public void flush(@FlushReason int reason) { |
| 525 |
if (mOptions.lite) return; |
| 526 |
getMainContentCaptureSession().flush(reason); |
| 527 |
} |
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
@Nullable |
| 536 |
public ComponentName getServiceComponentName() { |
| 537 |
if (!isContentCaptureEnabled() && !mOptions.lite) return null; |
| 538 |
|
| 539 |
final SyncResultReceiver resultReceiver = new SyncResultReceiver(SYNC_CALLS_TIMEOUT_MS); |
| 540 |
try { |
| 541 |
mService.getServiceComponentName(resultReceiver); |
| 542 |
return resultReceiver.getParcelableResult(); |
| 543 |
} catch (RemoteException e) { |
| 544 |
throw e.rethrowFromSystemServer(); |
| 545 |
} catch (SyncResultReceiver.TimeoutException e) { |
| 546 |
throw new RuntimeException("Fail to get service componentName."); |
| 547 |
} |
| 548 |
} |
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
@Nullable |
| 560 |
public static ComponentName getServiceSettingsComponentName() { |
| 561 |
final IBinder binder = ServiceManager |
| 562 |
.checkService(Context.CONTENT_CAPTURE_MANAGER_SERVICE); |
| 563 |
if (binder == null) return null; |
| 564 |
|
| 565 |
final IContentCaptureManager service = IContentCaptureManager.Stub.asInterface(binder); |
| 566 |
final SyncResultReceiver resultReceiver = new SyncResultReceiver(SYNC_CALLS_TIMEOUT_MS); |
| 567 |
try { |
| 568 |
service.getServiceSettingsActivity(resultReceiver); |
| 569 |
final int resultCode = resultReceiver.getIntResult(); |
| 570 |
if (resultCode == RESULT_CODE_SECURITY_EXCEPTION) { |
| 571 |
throw new SecurityException(resultReceiver.getStringResult()); |
| 572 |
} |
| 573 |
return resultReceiver.getParcelableResult(); |
| 574 |
} catch (RemoteException e) { |
| 575 |
throw e.rethrowFromSystemServer(); |
| 576 |
} catch (SyncResultReceiver.TimeoutException e) { |
| 577 |
Log.e(TAG, "Fail to get service settings componentName: " + e); |
| 578 |
return null; |
| 579 |
} |
| 580 |
} |
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
public boolean isContentCaptureEnabled() { |
| 597 |
if (mOptions.lite) return false; |
| 598 |
|
| 599 |
final MainContentCaptureSession mainSession; |
| 600 |
synchronized (mLock) { |
| 601 |
mainSession = mMainSession; |
| 602 |
} |
| 603 |
|
| 604 |
|
| 605 |
if (mainSession != null && mainSession.isDisabled()) return false; |
| 606 |
|
| 607 |
return true; |
| 608 |
} |
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
@Nullable |
| 621 |
public Set<ContentCaptureCondition> getContentCaptureConditions() { |
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
if (!isContentCaptureEnabled() && !mOptions.lite) return null; |
| 626 |
|
| 627 |
final SyncResultReceiver resultReceiver = syncRun( |
| 628 |
(r) -> mService.getContentCaptureConditions(mContext.getPackageName(), r)); |
| 629 |
|
| 630 |
try { |
| 631 |
final ArrayList<ContentCaptureCondition> result = resultReceiver |
| 632 |
.getParcelableListResult(); |
| 633 |
return toSet(result); |
| 634 |
} catch (SyncResultReceiver.TimeoutException e) { |
| 635 |
throw new RuntimeException("Fail to get content capture conditions."); |
| 636 |
} |
| 637 |
} |
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
public void setContentCaptureEnabled(boolean enabled) { |
| 646 |
if (sDebug) { |
| 647 |
Log.d(TAG, "setContentCaptureEnabled(): setting to " + enabled + " for " + mContext); |
| 648 |
} |
| 649 |
|
| 650 |
MainContentCaptureSession mainSession; |
| 651 |
synchronized (mLock) { |
| 652 |
if (enabled) { |
| 653 |
mFlags &= ~ContentCaptureContext.FLAG_DISABLED_BY_APP; |
| 654 |
} else { |
| 655 |
mFlags |= ContentCaptureContext.FLAG_DISABLED_BY_APP; |
| 656 |
} |
| 657 |
mainSession = mMainSession; |
| 658 |
} |
| 659 |
if (mainSession != null) { |
| 660 |
mainSession.setDisabled(!enabled); |
| 661 |
} |
| 662 |
} |
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
public void updateWindowAttributes(@NonNull WindowManager.LayoutParams params) { |
| 670 |
if (sDebug) { |
| 671 |
Log.d(TAG, "updateWindowAttributes(): window flags=" + params.flags); |
| 672 |
} |
| 673 |
final boolean flagSecureEnabled = |
| 674 |
(params.flags & WindowManager.LayoutParams.FLAG_SECURE) != 0; |
| 675 |
|
| 676 |
MainContentCaptureSession mainSession; |
| 677 |
synchronized (mLock) { |
| 678 |
if (flagSecureEnabled) { |
| 679 |
mFlags |= ContentCaptureContext.FLAG_DISABLED_BY_FLAG_SECURE; |
| 680 |
} else { |
| 681 |
mFlags &= ~ContentCaptureContext.FLAG_DISABLED_BY_FLAG_SECURE; |
| 682 |
} |
| 683 |
mainSession = mMainSession; |
| 684 |
} |
| 685 |
if (mainSession != null) { |
| 686 |
mainSession.setDisabled(flagSecureEnabled); |
| 687 |
} |
| 688 |
} |
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
@SystemApi |
| 702 |
public boolean isContentCaptureFeatureEnabled() { |
| 703 |
final SyncResultReceiver resultReceiver = syncRun( |
| 704 |
(r) -> mService.isContentCaptureFeatureEnabled(r)); |
| 705 |
|
| 706 |
try { |
| 707 |
final int resultCode = resultReceiver.getIntResult(); |
| 708 |
switch (resultCode) { |
| 709 |
case RESULT_CODE_TRUE: |
| 710 |
return true; |
| 711 |
case RESULT_CODE_FALSE: |
| 712 |
return false; |
| 713 |
default: |
| 714 |
Log.wtf(TAG, "received invalid result: " + resultCode); |
| 715 |
return false; |
| 716 |
} |
| 717 |
} catch (SyncResultReceiver.TimeoutException e) { |
| 718 |
Log.e(TAG, "Fail to get content capture feature enable status: " + e); |
| 719 |
return false; |
| 720 |
} |
| 721 |
} |
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
public void removeData(@NonNull DataRemovalRequest request) { |
| 730 |
Objects.requireNonNull(request); |
| 731 |
|
| 732 |
try { |
| 733 |
mService.removeData(request); |
| 734 |
} catch (RemoteException e) { |
| 735 |
throw e.rethrowFromSystemServer(); |
| 736 |
} |
| 737 |
} |
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
public void shareData(@NonNull DataShareRequest request, |
| 752 |
@NonNull @CallbackExecutor Executor executor, |
| 753 |
@NonNull DataShareWriteAdapter dataShareWriteAdapter) { |
| 754 |
Objects.requireNonNull(request); |
| 755 |
Objects.requireNonNull(dataShareWriteAdapter); |
| 756 |
Objects.requireNonNull(executor); |
| 757 |
|
| 758 |
try { |
| 759 |
mService.shareData(request, |
| 760 |
new DataShareAdapterDelegate(executor, dataShareWriteAdapter, |
| 761 |
mDataShareAdapterResourceManager)); |
| 762 |
} catch (RemoteException e) { |
| 763 |
throw e.rethrowFromSystemServer(); |
| 764 |
} |
| 765 |
} |
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
@NonNull |
| 773 |
private SyncResultReceiver syncRun(@NonNull MyRunnable r) { |
| 774 |
final SyncResultReceiver resultReceiver = new SyncResultReceiver(SYNC_CALLS_TIMEOUT_MS); |
| 775 |
try { |
| 776 |
r.run(resultReceiver); |
| 777 |
final int resultCode = resultReceiver.getIntResult(); |
| 778 |
if (resultCode == RESULT_CODE_SECURITY_EXCEPTION) { |
| 779 |
throw new SecurityException(resultReceiver.getStringResult()); |
| 780 |
} |
| 781 |
} catch (RemoteException e) { |
| 782 |
throw e.rethrowFromSystemServer(); |
| 783 |
} catch (SyncResultReceiver.TimeoutException e) { |
| 784 |
throw new RuntimeException("Fail to get syn run result from SyncResultReceiver."); |
| 785 |
} |
| 786 |
return resultReceiver; |
| 787 |
} |
| 788 |
|
| 789 |
|
| 790 |
public void addDumpable(Activity activity) { |
| 791 |
if (mDumpable == null) { |
| 792 |
mDumpable = new Dumper(); |
| 793 |
} |
| 794 |
activity.addDumpable(mDumpable); |
| 795 |
} |
| 796 |
|
| 797 |
|
| 798 |
private final class Dumper implements Dumpable { |
| 799 |
@Override |
| 800 |
public void dump(@NonNull PrintWriter pw, @Nullable String[] args) { |
| 801 |
String prefix = ""; |
| 802 |
pw.print(prefix); pw.println("ContentCaptureManager"); |
| 803 |
final String prefix2 = prefix + " "; |
| 804 |
synchronized (mLock) { |
| 805 |
pw.print(prefix2); pw.print("isContentCaptureEnabled(): "); |
| 806 |
pw.println(isContentCaptureEnabled()); |
| 807 |
pw.print(prefix2); pw.print("Debug: "); pw.print(sDebug); |
| 808 |
pw.print(" Verbose: "); pw.println(sVerbose); |
| 809 |
pw.print(prefix2); pw.print("Context: "); pw.println(mContext); |
| 810 |
pw.print(prefix2); pw.print("User: "); pw.println(mContext.getUserId()); |
| 811 |
pw.print(prefix2); pw.print("Service: "); pw.println(mService); |
| 812 |
pw.print(prefix2); pw.print("Flags: "); pw.println(mFlags); |
| 813 |
pw.print(prefix2); pw.print("Options: "); mOptions.dumpShort(pw); pw.println(); |
| 814 |
if (mMainSession != null) { |
| 815 |
final String prefix3 = prefix2 + " "; |
| 816 |
pw.print(prefix2); pw.println("Main session:"); |
| 817 |
mMainSession.dump(prefix3, pw); |
| 818 |
} else { |
| 819 |
pw.print(prefix2); pw.println("No sessions"); |
| 820 |
} |
| 821 |
} |
| 822 |
} |
| 823 |
|
| 824 |
@Override |
| 825 |
public String getDumpableName() { |
| 826 |
return DUMPABLE_NAME; |
| 827 |
} |
| 828 |
} |
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
@TestApi |
| 836 |
@RequiresPermission(android.Manifest.permission.MANAGE_CONTENT_CAPTURE) |
| 837 |
public static void resetTemporaryService(@UserIdInt int userId) { |
| 838 |
final IContentCaptureManager service = getService(); |
| 839 |
if (service == null) { |
| 840 |
Log.e(TAG, "IContentCaptureManager is null"); |
| 841 |
} |
| 842 |
try { |
| 843 |
service.resetTemporaryService(userId); |
| 844 |
} catch (RemoteException e) { |
| 845 |
throw e.rethrowFromSystemServer(); |
| 846 |
} |
| 847 |
} |
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
@TestApi |
| 860 |
@RequiresPermission(android.Manifest.permission.MANAGE_CONTENT_CAPTURE) |
| 861 |
public static void setTemporaryService( |
| 862 |
@UserIdInt int userId, @NonNull String serviceName, int duration) { |
| 863 |
final IContentCaptureManager service = getService(); |
| 864 |
if (service == null) { |
| 865 |
Log.e(TAG, "IContentCaptureManager is null"); |
| 866 |
} |
| 867 |
try { |
| 868 |
service.setTemporaryService(userId, serviceName, duration); |
| 869 |
} catch (RemoteException e) { |
| 870 |
throw e.rethrowFromSystemServer(); |
| 871 |
} |
| 872 |
} |
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
@TestApi |
| 880 |
@RequiresPermission(android.Manifest.permission.MANAGE_CONTENT_CAPTURE) |
| 881 |
public static void setDefaultServiceEnabled(@UserIdInt int userId, boolean enabled) { |
| 882 |
final IContentCaptureManager service = getService(); |
| 883 |
if (service == null) { |
| 884 |
Log.e(TAG, "IContentCaptureManager is null"); |
| 885 |
} |
| 886 |
try { |
| 887 |
service.setDefaultServiceEnabled(userId, enabled); |
| 888 |
} catch (RemoteException e) { |
| 889 |
throw e.rethrowFromSystemServer(); |
| 890 |
} |
| 891 |
} |
| 892 |
|
| 893 |
private static IContentCaptureManager getService() { |
| 894 |
return IContentCaptureManager.Stub.asInterface(ServiceManager.getService( |
| 895 |
Service.CONTENT_CAPTURE_MANAGER_SERVICE)); |
| 896 |
} |
| 897 |
|
| 898 |
private interface MyRunnable { |
| 899 |
void run(@NonNull SyncResultReceiver receiver) throws RemoteException; |
| 900 |
} |
| 901 |
|
| 902 |
private static class DataShareAdapterDelegate extends IDataShareWriteAdapter.Stub { |
| 903 |
|
| 904 |
private final WeakReference<LocalDataShareAdapterResourceManager> mResourceManagerReference; |
| 905 |
|
| 906 |
private DataShareAdapterDelegate(Executor executor, DataShareWriteAdapter adapter, |
| 907 |
LocalDataShareAdapterResourceManager resourceManager) { |
| 908 |
Objects.requireNonNull(executor); |
| 909 |
Objects.requireNonNull(adapter); |
| 910 |
Objects.requireNonNull(resourceManager); |
| 911 |
|
| 912 |
resourceManager.initializeForDelegate(this, adapter, executor); |
| 913 |
mResourceManagerReference = new WeakReference<>(resourceManager); |
| 914 |
} |
| 915 |
|
| 916 |
@Override |
| 917 |
public void write(ParcelFileDescriptor destination) |
| 918 |
throws RemoteException { |
| 919 |
executeAdapterMethodLocked(adapter -> adapter.onWrite(destination), "onWrite"); |
| 920 |
} |
| 921 |
|
| 922 |
@Override |
| 923 |
public void error(int errorCode) throws RemoteException { |
| 924 |
executeAdapterMethodLocked(adapter -> adapter.onError(errorCode), "onError"); |
| 925 |
clearHardReferences(); |
| 926 |
} |
| 927 |
|
| 928 |
@Override |
| 929 |
public void rejected() throws RemoteException { |
| 930 |
executeAdapterMethodLocked(DataShareWriteAdapter::onRejected, "onRejected"); |
| 931 |
clearHardReferences(); |
| 932 |
} |
| 933 |
|
| 934 |
@Override |
| 935 |
public void finish() throws RemoteException { |
| 936 |
clearHardReferences(); |
| 937 |
} |
| 938 |
|
| 939 |
private void executeAdapterMethodLocked(Consumer<DataShareWriteAdapter> adapterFn, |
| 940 |
String methodName) { |
| 941 |
LocalDataShareAdapterResourceManager resourceManager = mResourceManagerReference.get(); |
| 942 |
if (resourceManager == null) { |
| 943 |
Slog.w(TAG, "Can't execute " + methodName + "(), resource manager has been GC'ed"); |
| 944 |
return; |
| 945 |
} |
| 946 |
|
| 947 |
DataShareWriteAdapter adapter = resourceManager.getAdapter(this); |
| 948 |
Executor executor = resourceManager.getExecutor(this); |
| 949 |
|
| 950 |
if (adapter == null || executor == null) { |
| 951 |
Slog.w(TAG, "Can't execute " + methodName + "(), references are null"); |
| 952 |
return; |
| 953 |
} |
| 954 |
|
| 955 |
final long identity = Binder.clearCallingIdentity(); |
| 956 |
try { |
| 957 |
executor.execute(() -> adapterFn.accept(adapter)); |
| 958 |
} finally { |
| 959 |
Binder.restoreCallingIdentity(identity); |
| 960 |
} |
| 961 |
} |
| 962 |
|
| 963 |
private void clearHardReferences() { |
| 964 |
LocalDataShareAdapterResourceManager resourceManager = mResourceManagerReference.get(); |
| 965 |
if (resourceManager == null) { |
| 966 |
Slog.w(TAG, "Can't clear references, resource manager has been GC'ed"); |
| 967 |
return; |
| 968 |
} |
| 969 |
|
| 970 |
resourceManager.clearHardReferences(this); |
| 971 |
} |
| 972 |
} |
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
private static class LocalDataShareAdapterResourceManager { |
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
private Map<DataShareAdapterDelegate, DataShareWriteAdapter> mWriteAdapterHardReferences = |
| 985 |
new HashMap<>(); |
| 986 |
private Map<DataShareAdapterDelegate, Executor> mExecutorHardReferences = |
| 987 |
new HashMap<>(); |
| 988 |
|
| 989 |
void initializeForDelegate(DataShareAdapterDelegate delegate, DataShareWriteAdapter adapter, |
| 990 |
Executor executor) { |
| 991 |
mWriteAdapterHardReferences.put(delegate, adapter); |
| 992 |
mExecutorHardReferences.put(delegate, executor); |
| 993 |
} |
| 994 |
|
| 995 |
Executor getExecutor(DataShareAdapterDelegate delegate) { |
| 996 |
return mExecutorHardReferences.get(delegate); |
| 997 |
} |
| 998 |
|
| 999 |
DataShareWriteAdapter getAdapter(DataShareAdapterDelegate delegate) { |
| 1000 |
return mWriteAdapterHardReferences.get(delegate); |
| 1001 |
} |
| 1002 |
|
| 1003 |
void clearHardReferences(DataShareAdapterDelegate delegate) { |
| 1004 |
mWriteAdapterHardReferences.remove(delegate); |
| 1005 |
mExecutorHardReferences.remove(delegate); |
| 1006 |
} |
| 1007 |
} |
| 1008 |
} |