From 204f0215da9b71be2b82f55b119a567b5c44e08f Mon Sep 17 00:00:00 2001 From: Les Lee Date: Sat, 5 Jun 2021 10:35:14 +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 ROBOTEST_FILTER=SettingsDumpServiceTest Test: make RunSettingsRoboTests ROBOTEST_FILTER=DataUsageLibTest Change-Id: Ic6dada8260029e47e044d016ebc701c25dbf9b2a --- src/com/android/settings/SettingsDumpService.java | 10 +++++++--- .../android/settings/datausage/lib/DataUsageLib.java | 7 +++++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/com/android/settings/SettingsDumpService.java b/src/com/android/settings/SettingsDumpService.java index 2a041f33772..7e37e32ee07 100644 --- a/src/com/android/settings/SettingsDumpService.java +++ b/src/com/android/settings/SettingsDumpService.java @@ -111,9 +111,13 @@ public class SettingsDumpService extends Service { for (SubscriptionInfo info : manager.getAvailableSubscriptionInfoList()) { telephonyManager = telephonyManager .createForSubscriptionId(info.getSubscriptionId()); - NetworkTemplate carrier = NetworkTemplate.buildTemplateCarrierMetered( - telephonyManager.getSubscriberId()); - final JSONObject usage = dumpDataUsage(carrier, controller); + String subscriberId = telephonyManager.getSubscriberId(); + // The null subscriberId means that no any mobile/carrier network will be matched. + // Using old API: buildTemplateMobileAll for the null subscriberId to avoid NPE. + NetworkTemplate template = subscriberId != null + ? NetworkTemplate.buildTemplateCarrierMetered(subscriberId) + : NetworkTemplate.buildTemplateMobileAll(subscriberId); + final JSONObject usage = dumpDataUsage(template, controller); usage.put("subId", info.getSubscriptionId()); array.put(usage); } diff --git a/src/com/android/settings/datausage/lib/DataUsageLib.java b/src/com/android/settings/datausage/lib/DataUsageLib.java index 93907aade62..830f1cac33a 100644 --- a/src/com/android/settings/datausage/lib/DataUsageLib.java +++ b/src/com/android/settings/datausage/lib/DataUsageLib.java @@ -73,7 +73,10 @@ public class DataUsageLib { 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); } }