From afcd22acaa867da69407aa351e2ad1bba11796e1 Mon Sep 17 00:00:00 2001 From: zhuyunyi Date: Tue, 17 Aug 2021 14:10:39 +0800 Subject: [PATCH] Change Shallow Copy to Deep Copy in UsageStats There are three reference type members in UsageStats's constructor, when using "new UsageStats(stats)" to copy, it will be a Shallow Copy, which may cause concurrent modify problem. For example, in UserUsageStatsService.java, the sUsageStatsCombiner is using "new UsageStats(stats.packageStats.valueAt(i)" to copy, and the value is passing to the computeCacheQuotaHints in CacheQuotaStrategy.java. If we change the UsageStats.mForegroundServices at the same time, IndexOutOfBounds Exception will happen. Therefore, it is necessary to modify the way of copying of the UsageStats. Signed-off-by: zhuyunyi Change-Id: I58a54d17aad6ef5213e52658ee3387f3069339af --- core/java/android/app/usage/UsageStats.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/java/android/app/usage/UsageStats.java b/core/java/android/app/usage/UsageStats.java index 4d73a61c972dc..6d777cf904067 100644 --- a/core/java/android/app/usage/UsageStats.java +++ b/core/java/android/app/usage/UsageStats.java @@ -171,9 +171,9 @@ public final class UsageStats implements Parcelable { mLaunchCount = stats.mLaunchCount; mAppLaunchCount = stats.mAppLaunchCount; mLastEvent = stats.mLastEvent; - mActivities = stats.mActivities; - mForegroundServices = stats.mForegroundServices; - mChooserCounts = stats.mChooserCounts; + mActivities = stats.mActivities.clone(); + mForegroundServices = new ArrayMap<>(stats.mForegroundServices); + mChooserCounts = new ArrayMap<>(stats.mChooserCounts); } /**