From b6ad99651e5f5c1bf4c3611aa10eaee7b7788590 Mon Sep 17 00:00:00 2001 From: Ahan Wu Date: Tue, 7 Mar 2023 03:24:32 +0000 Subject: [PATCH] Catch SercurityException while reading device properties This is cherry-pcked to T QPR since it is required by ag/22099962. Bug: 270682188 Bug: 236948821 Test: abtd, see b/270682188#comment14 Change-Id: I204fc4b8bd7af3ca6432bddfe4e82c8f48208daf Merged-In: I204fc4b8bd7af3ca6432bddfe4e82c8f48208daf --- .../internal/jank/InteractionJankMonitor.java | 46 ++++++++++++++----- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/core/java/com/android/internal/jank/InteractionJankMonitor.java b/core/java/com/android/internal/jank/InteractionJankMonitor.java index e7217def76897..475f7fd2bdae1 100644 --- a/core/java/com/android/internal/jank/InteractionJankMonitor.java +++ b/core/java/com/android/internal/jank/InteractionJankMonitor.java @@ -16,6 +16,10 @@ package com.android.internal.jank; +import static android.Manifest.permission.READ_DEVICE_CONFIG; +import static android.content.pm.PackageManager.PERMISSION_GRANTED; +import static android.provider.DeviceConfig.NAMESPACE_INTERACTION_JANK_MONITOR; + import static com.android.internal.jank.FrameTracker.REASON_CANCEL_NORMAL; import static com.android.internal.jank.FrameTracker.REASON_CANCEL_TIMEOUT; import static com.android.internal.jank.FrameTracker.REASON_END_NORMAL; @@ -94,6 +98,7 @@ import android.annotation.IntDef; import android.annotation.NonNull; import android.annotation.UiThread; import android.annotation.WorkerThread; +import android.app.ActivityThread; import android.content.Context; import android.os.Build; import android.os.Handler; @@ -436,18 +441,37 @@ public class InteractionJankMonitor { mWorker = worker; mWorker.start(); mSamplingInterval = DEFAULT_SAMPLING_INTERVAL; - - // Post initialization to the background in case we're running on the main - // thread. - mWorker.getThreadHandler().post( - () -> mPropertiesChangedListener.onPropertiesChanged( - DeviceConfig.getProperties( - DeviceConfig.NAMESPACE_INTERACTION_JANK_MONITOR))); - DeviceConfig.addOnPropertiesChangedListener( - DeviceConfig.NAMESPACE_INTERACTION_JANK_MONITOR, - new HandlerExecutor(mWorker.getThreadHandler()), - mPropertiesChangedListener); mEnabled = DEFAULT_ENABLED; + + final Context context = ActivityThread.currentApplication(); + if (context.checkCallingOrSelfPermission(READ_DEVICE_CONFIG) != PERMISSION_GRANTED) { + if (DEBUG) { + Log.d(TAG, "Initialized the InteractionJankMonitor." + + " (No READ_DEVICE_CONFIG permission to change configs)" + + " enabled=" + mEnabled + ", interval=" + mSamplingInterval + + ", missedFrameThreshold=" + mTraceThresholdMissedFrames + + ", frameTimeThreshold=" + mTraceThresholdFrameTimeMillis + + ", package=" + context.getPackageName()); + } + return; + } + + // Post initialization to the background in case we're running on the main thread. + mWorker.getThreadHandler().post( + () -> { + try { + mPropertiesChangedListener.onPropertiesChanged( + DeviceConfig.getProperties(NAMESPACE_INTERACTION_JANK_MONITOR)); + DeviceConfig.addOnPropertiesChangedListener( + NAMESPACE_INTERACTION_JANK_MONITOR, + new HandlerExecutor(mWorker.getThreadHandler()), + mPropertiesChangedListener); + } catch (SecurityException ex) { + Log.d(TAG, "Can't get properties: READ_DEVICE_CONFIG granted=" + + context.checkCallingOrSelfPermission(READ_DEVICE_CONFIG) + + ", package=" + context.getPackageName()); + } + }); } /**