Fixes NPE in list from PrivacyItemController

NPE is probably caused by concurrent modification of the variable. Added
guards to prevent concurrent modification.

Test: current tests passing
Fixes: 127138070
Change-Id: I19220c2123bcc1b1a759e2a01592dde1bcd1316f
This commit is contained in:
Fabian Kozynski
2019-03-04 10:55:55 -05:00
parent 63727e269b
commit 7209010cc1
2 changed files with 10 additions and 5 deletions

View File

@@ -61,7 +61,8 @@ class PrivacyItemController @Inject constructor(
@VisibleForTesting
internal var privacyList = emptyList<PrivacyItem>()
get() = field.toList() // Provides a shallow copy of the list
@Synchronized get() = field.toList() // Returns a shallow copy of the list
@Synchronized set
private val userManager = context.getSystemService(UserManager::class.java)
private var currentUserIds = emptyList<Int>()
@@ -71,7 +72,8 @@ class PrivacyItemController @Inject constructor(
private val callbacks = mutableListOf<WeakReference<Callback>>()
private val notifyChanges = Runnable {
callbacks.forEach { it.get()?.privacyChanged(privacyList) }
val list = privacyList
callbacks.forEach { it.get()?.privacyChanged(list) }
}
private val updateListAndNotifyChanges = Runnable {
@@ -157,8 +159,10 @@ class PrivacyItemController @Inject constructor(
}
private fun updatePrivacyList() {
privacyList = currentUserIds.flatMap { appOpsController.getActiveAppOpsForUser(it) }
val list = currentUserIds.flatMap { appOpsController.getActiveAppOpsForUser(it) }
.mapNotNull { toPrivacyItem(it) }.distinct()
privacyList = list
}
private fun toPrivacyItem(appOpItem: AppOpItem): PrivacyItem? {

View File

@@ -264,7 +264,8 @@ class PrivacyItemControllerTest : SysuiTestCase() {
val list = listOf(PrivacyItem(PrivacyType.TYPE_CAMERA,
PrivacyApplication("", TEST_UID, mContext)))
privacyItemController.privacyList = list
assertEquals(list, privacyItemController.privacyList)
assertTrue(list !== privacyItemController.privacyList)
val privacyList = privacyItemController.privacyList
assertEquals(list, privacyList)
assertTrue(list !== privacyList)
}
}