1 package com.android.systemui.broadcast
2 
3 import android.content.BroadcastReceiver
4 import android.os.UserHandle
5 import android.util.SparseSetArray
6 import androidx.annotation.GuardedBy
7 import com.android.systemui.Dumpable
8 import com.android.systemui.broadcast.logging.BroadcastDispatcherLogger
9 import com.android.systemui.util.indentIfPossible
10 import java.io.PrintWriter
11 import javax.inject.Inject
12 
13 /**
14  * Store information about requests for unregistering receivers from [BroadcastDispatcher], before
15  * they have been completely removed from the system.
16  *
17  * This helps make unregistering a receiver a *sync* operation.
18  */
19 class PendingRemovalStore @Inject constructor(
20     private val logger: BroadcastDispatcherLogger
21 ) : Dumpable {
22     @GuardedBy("pendingRemoval")
23     private val pendingRemoval: SparseSetArray<BroadcastReceiver> = SparseSetArray()
24 
tagForRemovalnull25     fun tagForRemoval(broadcastReceiver: BroadcastReceiver, userId: Int) {
26         logger.logTagForRemoval(userId, broadcastReceiver)
27         synchronized(pendingRemoval) {
28             pendingRemoval.add(userId, broadcastReceiver)
29         }
30     }
31 
isPendingRemovalnull32     fun isPendingRemoval(broadcastReceiver: BroadcastReceiver, userId: Int): Boolean {
33         return synchronized(pendingRemoval) {
34             pendingRemoval.contains(userId, broadcastReceiver) ||
35                 pendingRemoval.contains(UserHandle.USER_ALL, broadcastReceiver)
36         }
37     }
38 
clearPendingRemovalnull39     fun clearPendingRemoval(broadcastReceiver: BroadcastReceiver, userId: Int) {
40         synchronized(pendingRemoval) {
41             pendingRemoval.remove(userId, broadcastReceiver)
42         }
43         logger.logClearedAfterRemoval(userId, broadcastReceiver)
44     }
45 
dumpnull46     override fun dump(pw: PrintWriter, args: Array<out String>) {
47         synchronized(pendingRemoval) {
48             pw.indentIfPossible {
49                 val size = pendingRemoval.size()
50                 for (i in 0 until size) {
51                     val user = pendingRemoval.keyAt(i)
52                     print(user)
53                     print("->")
54                     println(pendingRemoval.get(user))
55                 }
56             }
57         }
58     }
59 }