From c98d3dde68d1d18486279f4a2ea0a4655cc18834 Mon Sep 17 00:00:00 2001 From: Les Lee Date: Sat, 5 Jun 2021 10:41:12 +0800 Subject: [PATCH] Usage Settings: Fix NPE when subscriberId is Null. For mobile, a old API: buildTemplateMobileAll doesn't includes the merged wifi network, call the new API: buildTemplateCarrierMetered to replace the old one. But new API: buildTemplateCarrierMetered requires non-null subscriberId. Call old API: buildTemplateMobileAll when subscriberId is NULL since the matched result is always empty when subscriberId is NULL. No any different between buildTemplateCarrierMetered and buildTemplateMobileAll. Bug: 190233044 Bug: 190135429 Test: make RunSettingsRoboTests -j ROBOTEST_FILTER=DataUsageUtilsTest Change-Id: I3c7166e60e32142d5995d41722955dd54c21938f --- .../src/com/android/settingslib/net/DataUsageUtils.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/SettingsLib/src/com/android/settingslib/net/DataUsageUtils.java b/packages/SettingsLib/src/com/android/settingslib/net/DataUsageUtils.java index 51e533abebd24..3f95a07cc7500 100644 --- a/packages/SettingsLib/src/com/android/settingslib/net/DataUsageUtils.java +++ b/packages/SettingsLib/src/com/android/settingslib/net/DataUsageUtils.java @@ -73,7 +73,10 @@ public class DataUsageUtils { private static NetworkTemplate getMobileTemplateForSubId( TelephonyManager telephonyManager, int subId) { - return NetworkTemplate.buildTemplateCarrierMetered( - telephonyManager.getSubscriberId(subId)); + // The null subscriberId means that no any mobile/carrier network will be matched. + // Using old API: buildTemplateMobileAll for the null subscriberId to avoid NPE. + String subscriberId = telephonyManager.getSubscriberId(subId); + return subscriberId != null ? NetworkTemplate.buildTemplateCarrierMetered(subscriberId) + : NetworkTemplate.buildTemplateMobileAll(subscriberId); } }