From 0fdd409c0f5941785f455b2f00782544d0bb6708 Mon Sep 17 00:00:00 2001 From: Yi-an Chen Date: Thu, 9 Mar 2023 23:27:44 +0000 Subject: [PATCH] Fix SafetyProtectionUtils This util was expecting getString() and getDrawable() to return null when resource is not found but it's actually throwing an exception. Changed the logic in this class to expect an exception when the resource it not available Bug: 269874157 Test: SafetyProtectionTest Change-Id: I0f3490b59dc2427c2889e25fea529c85d3e1f150 --- .../android/util/SafetyProtectionUtils.java | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/core/java/android/util/SafetyProtectionUtils.java b/core/java/android/util/SafetyProtectionUtils.java index af985c5730630..75eaa9ed84af6 100644 --- a/core/java/android/util/SafetyProtectionUtils.java +++ b/core/java/android/util/SafetyProtectionUtils.java @@ -40,14 +40,20 @@ public class SafetyProtectionUtils { * @hide */ public static boolean shouldShowSafetyProtectionResources(Context context) { - return DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_PRIVACY, - SAFETY_PROTECTION_RESOURCES_ENABLED, false) - && context.getResources().getBoolean( - Resources.getSystem() - .getIdentifier("config_safetyProtectionEnabled", - "bool", "android")) - && context.getDrawable(android.R.drawable.ic_safety_protection) != null - && context.getString(android.R.string.safety_protection_display_text) != null - && !context.getString(android.R.string.safety_protection_display_text).isEmpty(); + try { + return DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_PRIVACY, + SAFETY_PROTECTION_RESOURCES_ENABLED, false) + && context.getResources().getBoolean( + Resources.getSystem() + .getIdentifier("config_safetyProtectionEnabled", + "bool", "android")) + && context.getDrawable(android.R.drawable.ic_safety_protection) != null + && !context.getString( + android.R.string.safety_protection_display_text).isEmpty(); + } catch (Resources.NotFoundException e) { + // We should expect the resources to not exist for non-pixel devices + // (except for the OEMs that opt-in) + return false; + } } }