From 0e3504c0fafb3506b1590f37b1633024384a6d28 Mon Sep 17 00:00:00 2001 From: jackqdyulei Date: Thu, 3 Aug 2017 15:46:52 -0700 Subject: [PATCH] Add static lock in PowerProfile Before this cl, there exists nullpointer crash in PowerProfile in following part: "sPowerMap.containsKey(key) && (Double) sPowerMap.get(key) > 0" To my best knowledge, it is because in PowerUsageSummary.SummaryProvider, we may create batterystats in meantime: 1. setListening is invoked in worker thread, in which we will force update the batterystats. 2. In setListening we will register receiver, which may update batterystats through onReceive in UI thread. This cl adds static lock in PowerProfile, making the init part can only be accessed by an thread at a time. Bug: 64209576 Test: Manual Change-Id: I00bc094f44416669b4f0e11e647b0fae2ff61013 --- core/java/com/android/internal/os/PowerProfile.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/core/java/com/android/internal/os/PowerProfile.java b/core/java/com/android/internal/os/PowerProfile.java index 51cf2eae851f9..872b465a9ca58 100644 --- a/core/java/com/android/internal/os/PowerProfile.java +++ b/core/java/com/android/internal/os/PowerProfile.java @@ -205,13 +205,17 @@ public class PowerProfile { private static final String TAG_ARRAYITEM = "value"; private static final String ATTR_NAME = "name"; + private static final Object sLock = new Object(); + public PowerProfile(Context context) { // Read the XML file for the given profile (normally only one per // device) - if (sPowerMap.size() == 0) { - readPowerValuesFromXml(context); + synchronized (sLock) { + if (sPowerMap.size() == 0) { + readPowerValuesFromXml(context); + } + initCpuClusters(); } - initCpuClusters(); } private void readPowerValuesFromXml(Context context) {