summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--PermissionController/src/com/android/permissioncontroller/hibernation/HibernationPolicy.kt22
-rw-r--r--PermissionController/src/com/android/permissioncontroller/permission/service/LocationAccessCheck.java26
-rw-r--r--PermissionController/src/com/android/permissioncontroller/permission/service/v34/SafetyLabelChangesJobService.kt27
-rw-r--r--PermissionController/src/com/android/permissioncontroller/permission/utils/KotlinUtils.kt36
-rw-r--r--PermissionController/src/com/android/permissioncontroller/privacysources/AccessibilitySourceService.kt31
-rw-r--r--PermissionController/src/com/android/permissioncontroller/privacysources/NotificationListenerCheck.kt24
-rw-r--r--SafetyCenter/Resources/shared_res/values-night/colors.xml20
-rw-r--r--SafetyCenter/Resources/shared_res/values/colors.xml20
-rw-r--r--SafetyCenter/ResourcesLib/java/com/android/safetycenter/resources/SafetyCenterResourcesContext.java47
-rw-r--r--SafetyCenter/ResourcesLib/tests/SafetyCenterResourcesLibTestResources/res/values/colors.xml19
-rw-r--r--SafetyCenter/ResourcesLib/tests/java/com/android/safetycenter/resources/SafetyCenterResourcesContextTest.kt31
11 files changed, 179 insertions, 124 deletions
diff --git a/PermissionController/src/com/android/permissioncontroller/hibernation/HibernationPolicy.kt b/PermissionController/src/com/android/permissioncontroller/hibernation/HibernationPolicy.kt
index d1fc2db09..1f6b5272a 100644
--- a/PermissionController/src/com/android/permissioncontroller/hibernation/HibernationPolicy.kt
+++ b/PermissionController/src/com/android/permissioncontroller/hibernation/HibernationPolicy.kt
@@ -71,7 +71,6 @@ import android.service.voice.VoiceInteractionService
import android.service.wallpaper.WallpaperService
import android.telephony.TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS
import android.telephony.TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS
-import android.text.Html
import android.util.Log
import android.view.inputmethod.InputMethod
import androidx.annotation.ChecksSdkIntAtLeast
@@ -892,21 +891,12 @@ class HibernationJobService : JobService() {
val extras = Bundle()
if (SdkLevel.isAtLeastT() &&
getSystemService(SafetyCenterManager::class.java)!!.isSafetyCenterEnabled) {
- if (KotlinUtils.shouldShowSafetyProtectionResources(this)) {
- // Use Protected by Android branding
- extras.putString(Notification.EXTRA_SUBSTITUTE_APP_NAME,
- Html.fromHtml(getString(android.R.string.safety_protection_display_text),
- /* flags= */ 0).toString())
- b.setSmallIcon(android.R.drawable.ic_safety_protection)
- .setColor(getColor(R.color.safety_center_info))
- .addExtras(extras)
- } else {
- // Use non-GMS PbA branding
- extras.putString(Notification.EXTRA_SUBSTITUTE_APP_NAME,
- getString(R.string.safety_center_notification_app_label))
- b.setSmallIcon(R.drawable.ic_settings_notification)
- .addExtras(extras)
- }
+ val notificationResources = KotlinUtils.getSafetyCenterNotificationResources(this)
+
+ extras.putString(Notification.EXTRA_SUBSTITUTE_APP_NAME, notificationResources.appLabel)
+ b.setSmallIcon(notificationResources.smallIcon)
+ .setColor(notificationResources.color)
+ .addExtras(extras)
} else {
// Use standard Settings branding
Utils.getSettingsLabelForNotifications(applicationContext.packageManager)?.let {
diff --git a/PermissionController/src/com/android/permissioncontroller/permission/service/LocationAccessCheck.java b/PermissionController/src/com/android/permissioncontroller/permission/service/LocationAccessCheck.java
index 6af03ebfe..e21be6a05 100644
--- a/PermissionController/src/com/android/permissioncontroller/permission/service/LocationAccessCheck.java
+++ b/PermissionController/src/com/android/permissioncontroller/permission/service/LocationAccessCheck.java
@@ -94,6 +94,7 @@ import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
+import android.graphics.drawable.Icon;
import android.location.LocationManager;
import android.net.Uri;
import android.os.AsyncTask;
@@ -109,7 +110,6 @@ import android.safetycenter.SafetySourceData;
import android.safetycenter.SafetySourceIssue;
import android.safetycenter.SafetySourceIssue.Action;
import android.service.notification.StatusBarNotification;
-import android.text.Html;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.ArraySet;
@@ -672,20 +672,16 @@ public class LocationAccessCheck {
R.string.background_location_access_reminder_notification_content);
CharSequence appLabel = appName;
- int smallIconResId;
- int colorResId = android.R.color.system_notification_accent_color;
+ Icon smallIcon;
+ int color = mContext.getColor(android.R.color.system_notification_accent_color);
if (safetyCenterBgLocationReminderEnabled) {
- if (KotlinUtils.INSTANCE.shouldShowSafetyProtectionResources(mContext)) {
- appLabel = Html.fromHtml(
- mContext.getString(android.R.string.safety_protection_display_text), 0);
- smallIconResId = android.R.drawable.ic_safety_protection;
- colorResId = R.color.safety_center_info;
- } else {
- appLabel = mContext.getString(R.string.safety_center_notification_app_label);
- smallIconResId = R.drawable.ic_settings_notification;
- }
+ KotlinUtils.NotificationResources notifRes =
+ KotlinUtils.INSTANCE.getSafetyCenterNotificationResources(mContext);
+ appLabel = notifRes.getAppLabel();
+ smallIcon = notifRes.getSmallIcon();
+ color = notifRes.getColor();
} else {
- smallIconResId = R.drawable.ic_pin_drop;
+ smallIcon = Icon.createWithResource(mContext, R.drawable.ic_pin_drop);
}
Notification.Builder b = (new Notification.Builder(mContext,
@@ -694,8 +690,8 @@ public class LocationAccessCheck {
.setContentTitle(notificationTitle)
.setContentText(notificationContent)
.setStyle(new Notification.BigTextStyle().bigText(notificationContent))
- .setSmallIcon(smallIconResId)
- .setColor(mContext.getColor(colorResId))
+ .setSmallIcon(smallIcon)
+ .setColor(color)
.setDeleteIntent(createNotificationDismissIntent(pkgName, sessionId, uid))
.setContentIntent(createNotificationClickIntent(pkgName, user, sessionId, uid))
.setAutoCancel(true);
diff --git a/PermissionController/src/com/android/permissioncontroller/permission/service/v34/SafetyLabelChangesJobService.kt b/PermissionController/src/com/android/permissioncontroller/permission/service/v34/SafetyLabelChangesJobService.kt
index 56d6b8efa..8ada0e23c 100644
--- a/PermissionController/src/com/android/permissioncontroller/permission/service/v34/SafetyLabelChangesJobService.kt
+++ b/PermissionController/src/com/android/permissioncontroller/permission/service/v34/SafetyLabelChangesJobService.kt
@@ -41,6 +41,7 @@ import android.provider.DeviceConfig
import android.util.Log
import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat
+import androidx.core.graphics.drawable.IconCompat
import com.android.permission.safetylabel.DataCategoryConstants.CATEGORY_LOCATION
import com.android.permission.safetylabel.SafetyLabel as AppMetadataSafetyLabel
import com.android.permissioncontroller.Constants.EXTRA_SESSION_ID
@@ -64,7 +65,6 @@ import com.android.permissioncontroller.permission.model.livedatatypes.AppPermGr
import com.android.permissioncontroller.permission.model.livedatatypes.AppPermGroupUiInfo.PermGrantState.PERMS_ALLOWED_FOREGROUND_ONLY
import com.android.permissioncontroller.permission.model.v34.AppDataSharingUpdate
import com.android.permissioncontroller.permission.utils.KotlinUtils
-import com.android.permissioncontroller.permission.utils.Utils
import com.android.permissioncontroller.permission.utils.Utils.getSystemServiceSafe
import com.android.permissioncontroller.safetylabel.AppsSafetyLabelHistory
import com.android.permissioncontroller.safetylabel.AppsSafetyLabelHistory.AppInfo
@@ -525,11 +525,15 @@ class SafetyLabelChangesJobService : JobService() {
val notificationManager = getSystemServiceSafe(context, NotificationManager::class.java)
createNotificationChannel(context, notificationManager)
+ val (appLabel, smallIcon, color) = KotlinUtils.getSafetyCenterNotificationResources(this)
+ val smallIconCompat = IconCompat.createFromIcon(smallIcon)
+ ?: IconCompat.createWithResource(this, R.drawable.ic_info)
val title = context.getString(R.string.safety_label_changes_notification_title)
val text = context.getString(R.string.safety_label_changes_notification_desc)
var notificationBuilder =
NotificationCompat.Builder(context, PERMISSION_REMINDER_CHANNEL_ID)
- .setSmallIcon(R.drawable.ic_info)
+ .setColor(color)
+ .setSmallIcon(smallIconCompat)
.setContentTitle(title)
.setContentText(text)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
@@ -544,22 +548,9 @@ class SafetyLabelChangesJobService : JobService() {
numberOfAppUpdates
)
)
-
- val settingsAppLabel =
- Utils.getSettingsLabelForNotifications(applicationContext.packageManager)
- if (settingsAppLabel != null) {
- notificationBuilder =
- notificationBuilder
- .setSmallIcon(R.drawable.ic_settings_24dp)
- .addExtras(
- Bundle().apply {
- putString(
- Notification.EXTRA_SUBSTITUTE_APP_NAME,
- settingsAppLabel.toString()
- )
- }
- )
- }
+ notificationBuilder.addExtras(
+ Bundle().apply { putString(Notification.EXTRA_SUBSTITUTE_APP_NAME, appLabel) }
+ )
notificationManager.notify(
SAFETY_LABEL_CHANGES_NOTIFICATION_ID,
diff --git a/PermissionController/src/com/android/permissioncontroller/permission/utils/KotlinUtils.kt b/PermissionController/src/com/android/permissioncontroller/permission/utils/KotlinUtils.kt
index 8f5a62303..f9345ef58 100644
--- a/PermissionController/src/com/android/permissioncontroller/permission/utils/KotlinUtils.kt
+++ b/PermissionController/src/com/android/permissioncontroller/permission/utils/KotlinUtils.kt
@@ -54,6 +54,7 @@ import android.content.res.Resources
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.drawable.Drawable
+import android.graphics.drawable.Icon
import android.health.connect.HealthConnectManager
import android.os.Build
import android.os.Bundle
@@ -63,6 +64,7 @@ import android.provider.DeviceConfig
import android.provider.Settings
import android.safetylabel.SafetyLabelConstants.PERMISSION_RATIONALE_ENABLED
import android.safetylabel.SafetyLabelConstants.SAFETY_LABEL_CHANGE_NOTIFICATIONS_ENABLED
+import android.text.Html
import android.text.TextUtils
import android.util.Log
import androidx.annotation.ChecksSdkIntAtLeast
@@ -84,6 +86,7 @@ import com.android.permissioncontroller.permission.model.livedatatypes.LightPerm
import com.android.permissioncontroller.permission.model.livedatatypes.PermState
import com.android.permissioncontroller.permission.service.LocationAccessCheck
import com.android.permissioncontroller.permission.ui.handheld.SettingsWithLargeHeader
+import com.android.safetycenter.resources.SafetyCenterResourcesContext
import java.time.Duration
import java.util.concurrent.atomic.AtomicReference
import kotlin.coroutines.Continuation
@@ -1500,6 +1503,39 @@ object KotlinUtils {
null
}
}
+
+ data class NotificationResources(val appLabel: String, val smallIcon: Icon, val color: Int)
+
+ fun getSafetyCenterNotificationResources(context: Context): NotificationResources {
+ val appLabel: String
+ val smallIcon: Icon
+ val color: Int
+ // If U resources are available, and this is a U+ device, use those
+ if (SdkLevel.isAtLeastU()) {
+ val scContext = SafetyCenterResourcesContext(context)
+ val uIcon = scContext.getIconByDrawableName("ic_notification_badge_general")
+ val uColor = scContext.getColorByName("notification_tint_normal")
+ if (uIcon != null && uColor != null) {
+ appLabel = context.getString(R.string.safety_privacy_qs_tile_title)
+ return NotificationResources(appLabel, uIcon, uColor)
+ }
+ }
+
+ // Use PbA branding if available, otherwise default to more generic branding
+ if (shouldShowSafetyProtectionResources(context)) {
+ appLabel = Html.fromHtml(context.getString(
+ android.R.string.safety_protection_display_text), 0).toString()
+ smallIcon =
+ Icon.createWithResource(context, android.R.drawable.ic_safety_protection)
+ color = context.getColor(R.color.safety_center_info)
+ } else {
+ appLabel = context.getString(R.string.safety_center_notification_app_label)
+ smallIcon =
+ Icon.createWithResource(context, R.drawable.ic_settings_notification)
+ color = context.getColor(android.R.color.system_notification_accent_color)
+ }
+ return NotificationResources(appLabel, smallIcon, color)
+ }
}
/**
diff --git a/PermissionController/src/com/android/permissioncontroller/privacysources/AccessibilitySourceService.kt b/PermissionController/src/com/android/permissioncontroller/privacysources/AccessibilitySourceService.kt
index 4ba685589..1d5c9c9fa 100644
--- a/PermissionController/src/com/android/permissioncontroller/privacysources/AccessibilitySourceService.kt
+++ b/PermissionController/src/com/android/permissioncontroller/privacysources/AccessibilitySourceService.kt
@@ -39,7 +39,6 @@ import android.safetycenter.SafetyEvent
import android.safetycenter.SafetySourceData
import android.safetycenter.SafetySourceIssue
import android.service.notification.StatusBarNotification
-import android.text.Html
import android.util.Log
import android.view.accessibility.AccessibilityManager
import androidx.annotation.ChecksSdkIntAtLeast
@@ -221,7 +220,8 @@ class AccessibilitySourceService(
pkgLabel
)
- val notificationResource = getNotificationResource()
+ val (appLabel, smallIcon, color) =
+ KotlinUtils.getSafetyCenterNotificationResources(parentUserContext)
val b: Notification.Builder =
Notification.Builder(parentUserContext, Constants.PERMISSION_REMINDER_CHANNEL_ID)
.setLocalOnly(true)
@@ -229,8 +229,8 @@ class AccessibilitySourceService(
.setContentText(summary)
// Ensure entire text can be displayed, instead of being truncated to one line
.setStyle(Notification.BigTextStyle().bigText(summary))
- .setSmallIcon(notificationResource.smallIconResId)
- .setColor(context.getColor(notificationResource.colorResId))
+ .setSmallIcon(smallIcon)
+ .setColor(color)
.setAutoCancel(true)
.setDeleteIntent(
PendingIntent.getBroadcast(
@@ -242,8 +242,7 @@ class AccessibilitySourceService(
.setContentIntent(getSafetyCenterActivityIntent(context, uid, sessionId))
val appNameExtras = Bundle()
- appNameExtras.putString(Notification.EXTRA_SUBSTITUTE_APP_NAME,
- notificationResource.appLabel)
+ appNameExtras.putString(Notification.EXTRA_SUBSTITUTE_APP_NAME, appLabel)
b.addExtras(appNameExtras)
notificationsManager.notify(
@@ -272,26 +271,6 @@ class AccessibilitySourceService(
)
}
- class NotificationResource(val appLabel: String, val smallIconResId: Int, val colorResId: Int)
-
- private fun getNotificationResource(): NotificationResource {
- // Use PbA branding if available, otherwise default to more generic branding
- val appLabel: String
- val smallIconResId: Int
- val colorResId: Int
- if (KotlinUtils.shouldShowSafetyProtectionResources(parentUserContext)) {
- appLabel = Html.fromHtml(parentUserContext.getString(
- android.R.string.safety_protection_display_text), 0).toString()
- smallIconResId = android.R.drawable.ic_safety_protection
- colorResId = R.color.safety_center_info
- } else {
- appLabel = parentUserContext.getString(R.string.safety_center_notification_app_label)
- smallIconResId = R.drawable.ic_settings_notification
- colorResId = android.R.color.system_notification_accent_color
- }
- return NotificationResource(appLabel, smallIconResId, colorResId)
- }
-
/** Create the channel for a11y notifications */
private fun createPermissionReminderChannel() {
val permissionReminderChannel = NotificationChannel(
diff --git a/PermissionController/src/com/android/permissioncontroller/privacysources/NotificationListenerCheck.kt b/PermissionController/src/com/android/permissioncontroller/privacysources/NotificationListenerCheck.kt
index b7709cf35..7be1d7e2b 100644
--- a/PermissionController/src/com/android/permissioncontroller/privacysources/NotificationListenerCheck.kt
+++ b/PermissionController/src/com/android/permissioncontroller/privacysources/NotificationListenerCheck.kt
@@ -51,7 +51,6 @@ import android.safetycenter.SafetyEvent
import android.safetycenter.SafetySourceData
import android.safetycenter.SafetySourceIssue
import android.service.notification.StatusBarNotification
-import android.text.Html
import android.util.Log
import androidx.annotation.ChecksSdkIntAtLeast
import androidx.annotation.GuardedBy
@@ -519,23 +518,8 @@ internal class NotificationListenerCheckInternal(
parentUserContext.getString(
R.string.notification_listener_reminder_notification_content, pkgLabel)
- // Use PbA branding if available, otherwise default to more generic branding
- val appLabel: String
- val smallIconResId: Int
- val colorResId: Int
- if (KotlinUtils.shouldShowSafetyProtectionResources(parentUserContext)) {
- // PbA branding and colors
- val pbaHtmlString =
- parentUserContext.getString(android.R.string.safety_protection_display_text)
- appLabel = Html.fromHtml(pbaHtmlString, 0).toString()
- smallIconResId = android.R.drawable.ic_safety_protection
- colorResId = R.color.safety_center_info
- } else {
- // Generic branding. Settings label, gear icon, and system accent color
- appLabel = parentUserContext.getString(R.string.safety_center_notification_app_label)
- smallIconResId = R.drawable.ic_settings_notification
- colorResId = android.R.color.system_notification_accent_color
- }
+ val (appLabel, smallIcon, color) =
+ KotlinUtils.getSafetyCenterNotificationResources(parentUserContext)
val b: Notification.Builder =
Notification.Builder(parentUserContext, Constants.PERMISSION_REMINDER_CHANNEL_ID)
@@ -544,8 +528,8 @@ internal class NotificationListenerCheckInternal(
.setContentText(text)
// Ensure entire text can be displayed, instead of being truncated to one line
.setStyle(Notification.BigTextStyle().bigText(text))
- .setSmallIcon(smallIconResId)
- .setColor(parentUserContext.getColor(colorResId))
+ .setSmallIcon(smallIcon)
+ .setColor(color)
.setAutoCancel(true)
.setDeleteIntent(deletePendingIntent)
.setContentIntent(clickPendingIntent)
diff --git a/SafetyCenter/Resources/shared_res/values-night/colors.xml b/SafetyCenter/Resources/shared_res/values-night/colors.xml
new file mode 100644
index 000000000..be7a96628
--- /dev/null
+++ b/SafetyCenter/Resources/shared_res/values-night/colors.xml
@@ -0,0 +1,20 @@
+<!--
+ ~ Copyright (C) 2023 The Android Open Source Project
+ ~
+ ~ Licensed under the Apache License, Version 2.0 (the "License");
+ ~ you may not use this file except in compliance with the License.
+ ~ You may obtain a copy of the License at
+ ~
+ ~ http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing, software
+ ~ distributed under the License is distributed on an "AS IS" BASIS,
+ ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ ~ See the License for the specific language governing permissions and
+ ~ limitations under the License.
+ -->
+
+<resources>
+ <color name="notification_tint_critical">#EE675C</color>
+ <color name="notification_tint_normal">#8AB4F8</color>
+</resources>
diff --git a/SafetyCenter/Resources/shared_res/values/colors.xml b/SafetyCenter/Resources/shared_res/values/colors.xml
new file mode 100644
index 000000000..ce5e97b40
--- /dev/null
+++ b/SafetyCenter/Resources/shared_res/values/colors.xml
@@ -0,0 +1,20 @@
+<!--
+ ~ Copyright (C) 2023 The Android Open Source Project
+ ~
+ ~ Licensed under the Apache License, Version 2.0 (the "License");
+ ~ you may not use this file except in compliance with the License.
+ ~ You may obtain a copy of the License at
+ ~
+ ~ http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing, software
+ ~ distributed under the License is distributed on an "AS IS" BASIS,
+ ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ ~ See the License for the specific language governing permissions and
+ ~ limitations under the License.
+ -->
+
+<resources>
+ <color name="notification_tint_critical">#D93025</color>
+ <color name="notification_tint_normal">#1A73E8</color>
+</resources>
diff --git a/SafetyCenter/ResourcesLib/java/com/android/safetycenter/resources/SafetyCenterResourcesContext.java b/SafetyCenter/ResourcesLib/java/com/android/safetycenter/resources/SafetyCenterResourcesContext.java
index 99b236157..7ca257887 100644
--- a/SafetyCenter/ResourcesLib/java/com/android/safetycenter/resources/SafetyCenterResourcesContext.java
+++ b/SafetyCenter/ResourcesLib/java/com/android/safetycenter/resources/SafetyCenterResourcesContext.java
@@ -29,6 +29,7 @@ import android.graphics.drawable.Drawable;
import android.graphics.drawable.Icon;
import android.util.Log;
+import androidx.annotation.ColorInt;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.annotation.VisibleForTesting;
@@ -256,6 +257,10 @@ public class SafetyCenterResourcesContext extends ContextWrapper {
@StringRes
private int getStringRes(String name) {
+ return getResId(name, "string");
+ }
+
+ private int getResId(String name, String type) {
String resourcePkgName = getResourcesApkPkgName();
if (resourcePkgName == null) {
return Resources.ID_NULL;
@@ -266,7 +271,7 @@ public class SafetyCenterResourcesContext extends ContextWrapper {
}
// TODO(b/227738283): profile the performance of this operation and consider adding caching
// or finding some alternative solution.
- return resources.getIdentifier(name, "string", resourcePkgName);
+ return resources.getIdentifier(name, type, resourcePkgName);
}
@Nullable
@@ -329,19 +334,9 @@ public class SafetyCenterResourcesContext extends ContextWrapper {
*/
@Nullable
public Drawable getDrawableByName(String name, @Nullable Resources.Theme theme) {
- String resourcePkgName = getResourcesApkPkgName();
- if (resourcePkgName == null) {
- return null;
- }
-
- Resources resources = getResources();
- if (resources == null) {
- return null;
- }
-
- int resId = resources.getIdentifier(name, "drawable", resourcePkgName);
+ int resId = getResId(name, "drawable");
if (resId != Resources.ID_NULL) {
- return resources.getDrawable(resId, theme);
+ return getResources().getDrawable(resId, theme);
}
if (!mShouldFallbackIfNamedResourceNotFound) {
@@ -358,26 +353,34 @@ public class SafetyCenterResourcesContext extends ContextWrapper {
*/
@Nullable
public Icon getIconByDrawableName(String drawableResName) {
- String packageName = getResourcesApkPkgName();
- if (packageName == null) {
- return null;
+ int resId = getResId(drawableResName, "drawable");
+ if (resId != Resources.ID_NULL) {
+ return Icon.createWithResource(getResourcesApkPkgName(), resId);
}
- Resources resources = getResources();
- if (resources == null) {
- return null;
+ if (!mShouldFallbackIfNamedResourceNotFound) {
+ throw new Resources.NotFoundException();
}
- int resId = resources.getIdentifier(drawableResName, "drawable", packageName);
+ Log.w(TAG, "Drawable resource " + drawableResName + " not found");
+ return null;
+ }
+
+ /**
+ * Gets a color by resource name
+ */
+ @ColorInt
+ public Integer getColorByName(String name) {
+ int resId = getResId(name, "color");
if (resId != Resources.ID_NULL) {
- return Icon.createWithResource(packageName, resId);
+ return getResources().getColor(resId, getTheme());
}
if (!mShouldFallbackIfNamedResourceNotFound) {
throw new Resources.NotFoundException();
}
- Log.w(TAG, "Drawable resource " + drawableResName + " not found");
+ Log.w(TAG, "Color resource " + name + " not found");
return null;
}
}
diff --git a/SafetyCenter/ResourcesLib/tests/SafetyCenterResourcesLibTestResources/res/values/colors.xml b/SafetyCenter/ResourcesLib/tests/SafetyCenterResourcesLibTestResources/res/values/colors.xml
new file mode 100644
index 000000000..709c842cd
--- /dev/null
+++ b/SafetyCenter/ResourcesLib/tests/SafetyCenterResourcesLibTestResources/res/values/colors.xml
@@ -0,0 +1,19 @@
+<!--
+ ~ Copyright (C) 2023 The Android Open Source Project
+ ~
+ ~ Licensed under the Apache License, Version 2.0 (the "License");
+ ~ you may not use this file except in compliance with the License.
+ ~ You may obtain a copy of the License at
+ ~
+ ~ http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing, software
+ ~ distributed under the License is distributed on an "AS IS" BASIS,
+ ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ ~ See the License for the specific language governing permissions and
+ ~ limitations under the License.
+ -->
+
+<resources>
+ <color name="valid_color">#FFFFFF</color>
+</resources>
diff --git a/SafetyCenter/ResourcesLib/tests/java/com/android/safetycenter/resources/SafetyCenterResourcesContextTest.kt b/SafetyCenter/ResourcesLib/tests/java/com/android/safetycenter/resources/SafetyCenterResourcesContextTest.kt
index f7af0c712..1a82460d2 100644
--- a/SafetyCenter/ResourcesLib/tests/java/com/android/safetycenter/resources/SafetyCenterResourcesContextTest.kt
+++ b/SafetyCenter/ResourcesLib/tests/java/com/android/safetycenter/resources/SafetyCenterResourcesContextTest.kt
@@ -182,6 +182,29 @@ class SafetyCenterResourcesContextTest {
}
}
+ @Test
+ fun getColorByName_validColor_returnsColor() {
+ val resourcesContext = createNewResourcesContext()
+
+ assertThat(resourcesContext.getColorByName("valid_color")).isNotNull()
+ }
+
+ @Test
+ fun getColorByName_invalidColorWithFallback_returnsNull() {
+ val resourcesContext = createNewResourcesContext(fallback = true)
+
+ assertThat(resourcesContext.getColorByName("invalid_color")).isNull()
+ }
+
+ @Test
+ fun getColorByName_invalidColorWithoutFallback_throws() {
+ val resourcesContext = createNewResourcesContext(fallback = false)
+
+ assertFailsWith(Resources.NotFoundException::class) {
+ resourcesContext.getColorByName("invalid_color")
+ }
+ }
+
private fun createNewResourcesContext(
resourcesApkAction: String = RESOURCES_APK_ACTION,
resourcesApkPath: String? = null,
@@ -190,13 +213,7 @@ class SafetyCenterResourcesContextTest {
fallback: Boolean = false
) =
SafetyCenterResourcesContext(
- context,
- resourcesApkAction,
- resourcesApkPath,
- configName,
- flags,
- fallback
- )
+ context, resourcesApkAction, resourcesApkPath, configName, flags, fallback)
companion object {
const val RESOURCES_APK_ACTION =