|
11 | 11 | import android.os.Handler; |
12 | 12 | import android.os.Looper; |
13 | 13 | import android.os.SystemClock; |
| 14 | +import android.util.Log; |
14 | 15 | import androidx.annotation.NonNull; |
15 | 16 | import androidx.annotation.Nullable; |
16 | 17 | import androidx.annotation.VisibleForTesting; |
@@ -467,18 +468,28 @@ public void registerLifecycleCallbacks(final @NotNull Application application) { |
467 | 468 | final @Nullable ActivityManager activityManager = |
468 | 469 | (ActivityManager) application.getSystemService(Context.ACTIVITY_SERVICE); |
469 | 470 | if (activityManager != null) { |
470 | | - final List<ApplicationStartInfo> historicalProcessStartReasons = |
471 | | - activityManager.getHistoricalProcessStartReasons(1); |
472 | | - if (!historicalProcessStartReasons.isEmpty()) { |
473 | | - final @NotNull ApplicationStartInfo info = historicalProcessStartReasons.get(0); |
474 | | - cachedStartInfo = info; |
475 | | - if (info.getStartupState() == ApplicationStartInfo.STARTUP_STATE_STARTED) { |
476 | | - if (info.getStartType() == ApplicationStartInfo.START_TYPE_COLD) { |
477 | | - appStartType = AppStartType.COLD; |
478 | | - } else { |
479 | | - appStartType = AppStartType.WARM; |
| 471 | + try { |
| 472 | + final List<ApplicationStartInfo> historicalProcessStartReasons = |
| 473 | + activityManager.getHistoricalProcessStartReasons(1); |
| 474 | + if (!historicalProcessStartReasons.isEmpty()) { |
| 475 | + final @NotNull ApplicationStartInfo info = historicalProcessStartReasons.get(0); |
| 476 | + cachedStartInfo = info; |
| 477 | + if (info.getStartupState() == ApplicationStartInfo.STARTUP_STATE_STARTED) { |
| 478 | + if (info.getStartType() == ApplicationStartInfo.START_TYPE_COLD) { |
| 479 | + appStartType = AppStartType.COLD; |
| 480 | + } else { |
| 481 | + appStartType = AppStartType.WARM; |
| 482 | + } |
480 | 483 | } |
481 | 484 | } |
| 485 | + } catch (RuntimeException ignored) { |
| 486 | + // getHistoricalProcessStartReasons may throw different kinds of exceptions, namely: |
| 487 | + // - SecurityException when called from an isolated process |
| 488 | + // - IllegalArgumentException when called with a wrong userId |
| 489 | + // - others |
| 490 | + // See impl: |
| 491 | + // https://cs.android.com/android/platform/superproject/+/android-latest-release:frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java;l=10866-10893 |
| 492 | + Log.w("AppStartMetrics", ignored); // no logger instance here, so we just Log |
482 | 493 | } |
483 | 494 | } |
484 | 495 | } |
|
0 commit comments