Fabian Kozynski | 42f86a0 | 2019-07-09 12:39:03 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License |
| 15 | */ |
| 16 | |
| 17 | package com.android.systemui.broadcast |
| 18 | |
| 19 | import android.content.BroadcastReceiver |
| 20 | import android.content.Context |
| 21 | import android.content.Intent |
| 22 | import android.content.IntentFilter |
| 23 | import android.os.Handler |
| 24 | import android.os.Looper |
| 25 | import android.os.Message |
| 26 | import android.os.UserHandle |
| 27 | import android.util.ArrayMap |
| 28 | import android.util.ArraySet |
| 29 | import android.util.Log |
Fabian Kozynski | 213049e | 2019-11-06 17:23:17 -0500 | [diff] [blame] | 30 | import androidx.annotation.MainThread |
| 31 | import com.android.internal.util.Preconditions |
Fabian Kozynski | 42f86a0 | 2019-07-09 12:39:03 -0400 | [diff] [blame] | 32 | import com.android.systemui.Dumpable |
| 33 | import java.io.FileDescriptor |
| 34 | import java.io.PrintWriter |
| 35 | import java.util.concurrent.atomic.AtomicBoolean |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 36 | import java.util.concurrent.atomic.AtomicInteger |
Fabian Kozynski | 42f86a0 | 2019-07-09 12:39:03 -0400 | [diff] [blame] | 37 | |
| 38 | private const val MSG_REGISTER_RECEIVER = 0 |
| 39 | private const val MSG_UNREGISTER_RECEIVER = 1 |
Fabian Kozynski | ff5e91f | 2019-09-24 15:38:08 -0400 | [diff] [blame] | 40 | private const val TAG = "UserBroadcastDispatcher" |
Fabian Kozynski | 42f86a0 | 2019-07-09 12:39:03 -0400 | [diff] [blame] | 41 | private const val DEBUG = false |
| 42 | |
| 43 | /** |
| 44 | * Broadcast dispatcher for a given user registration [userId]. |
| 45 | * |
| 46 | * Created by [BroadcastDispatcher] as needed by users. The value of [userId] can be |
| 47 | * [UserHandle.USER_ALL]. |
| 48 | */ |
| 49 | class UserBroadcastDispatcher( |
| 50 | private val context: Context, |
| 51 | private val userId: Int, |
| 52 | private val mainHandler: Handler, |
| 53 | private val bgLooper: Looper |
| 54 | ) : BroadcastReceiver(), Dumpable { |
| 55 | |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 56 | companion object { |
| 57 | // Used only for debugging. If not debugging, this variable will not be accessed and all |
| 58 | // received broadcasts will be tagged with 0. However, as DEBUG is false, nothing will be |
| 59 | // logged |
| 60 | val index = AtomicInteger(0) |
| 61 | } |
| 62 | |
Fabian Kozynski | 42f86a0 | 2019-07-09 12:39:03 -0400 | [diff] [blame] | 63 | private val bgHandler = object : Handler(bgLooper) { |
| 64 | override fun handleMessage(msg: Message) { |
| 65 | when (msg.what) { |
| 66 | MSG_REGISTER_RECEIVER -> handleRegisterReceiver(msg.obj as ReceiverData) |
| 67 | MSG_UNREGISTER_RECEIVER -> handleUnregisterReceiver(msg.obj as BroadcastReceiver) |
| 68 | else -> Unit |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | private val registered = AtomicBoolean(false) |
| 74 | |
| 75 | internal fun isRegistered() = registered.get() |
| 76 | |
Fabian Kozynski | 213049e | 2019-11-06 17:23:17 -0500 | [diff] [blame] | 77 | // Only modify in BG thread |
| 78 | private val actionsToReceivers = ArrayMap<String, MutableSet<ReceiverData>>() |
| 79 | private val receiverToReceiverData = ArrayMap<BroadcastReceiver, MutableSet<ReceiverData>>() |
| 80 | |
| 81 | // Only call on BG thread as it reads from the maps |
| 82 | private fun createFilter(): IntentFilter { |
| 83 | Preconditions.checkState(bgHandler.looper.isCurrentThread, |
| 84 | "This method should only be called from BG thread") |
Fabian Kozynski | 42f86a0 | 2019-07-09 12:39:03 -0400 | [diff] [blame] | 85 | val categories = mutableSetOf<String>() |
| 86 | receiverToReceiverData.values.flatten().forEach { |
| 87 | it.filter.categoriesIterator()?.asSequence()?.let { |
| 88 | categories.addAll(it) |
| 89 | } |
| 90 | } |
| 91 | val intentFilter = IntentFilter().apply { |
Fabian Kozynski | 213049e | 2019-11-06 17:23:17 -0500 | [diff] [blame] | 92 | // The keys of the arrayMap are of type String! so null check is needed |
| 93 | actionsToReceivers.keys.forEach { if (it != null) addAction(it) else Unit } |
Fabian Kozynski | 42f86a0 | 2019-07-09 12:39:03 -0400 | [diff] [blame] | 94 | categories.forEach { addCategory(it) } |
| 95 | } |
Fabian Kozynski | 213049e | 2019-11-06 17:23:17 -0500 | [diff] [blame] | 96 | return intentFilter |
Fabian Kozynski | 42f86a0 | 2019-07-09 12:39:03 -0400 | [diff] [blame] | 97 | } |
| 98 | |
Fabian Kozynski | 42f86a0 | 2019-07-09 12:39:03 -0400 | [diff] [blame] | 99 | override fun onReceive(context: Context, intent: Intent) { |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 100 | val id = if (DEBUG) index.getAndIncrement() else 0 |
| 101 | if (DEBUG) Log.w(TAG, "[$id] Received $intent") |
| 102 | bgHandler.post( |
| 103 | HandleBroadcastRunnable(actionsToReceivers, context, intent, pendingResult, id)) |
Fabian Kozynski | 42f86a0 | 2019-07-09 12:39:03 -0400 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Register a [ReceiverData] for this user. |
| 108 | */ |
| 109 | fun registerReceiver(receiverData: ReceiverData) { |
| 110 | bgHandler.obtainMessage(MSG_REGISTER_RECEIVER, receiverData).sendToTarget() |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Unregister a given [BroadcastReceiver] for this user. |
| 115 | */ |
| 116 | fun unregisterReceiver(receiver: BroadcastReceiver) { |
| 117 | bgHandler.obtainMessage(MSG_UNREGISTER_RECEIVER, receiver).sendToTarget() |
| 118 | } |
| 119 | |
| 120 | private fun handleRegisterReceiver(receiverData: ReceiverData) { |
Fabian Kozynski | 213049e | 2019-11-06 17:23:17 -0500 | [diff] [blame] | 121 | Preconditions.checkState(bgHandler.looper.isCurrentThread, |
| 122 | "This method should only be called from BG thread") |
Fabian Kozynski | 42f86a0 | 2019-07-09 12:39:03 -0400 | [diff] [blame] | 123 | if (DEBUG) Log.w(TAG, "Register receiver: ${receiverData.receiver}") |
| 124 | receiverToReceiverData.getOrPut(receiverData.receiver, { ArraySet() }).add(receiverData) |
| 125 | var changed = false |
| 126 | // Index the BroadcastReceiver by all its actions, that way it's easier to dispatch given |
| 127 | // a received intent. |
| 128 | receiverData.filter.actionsIterator().forEach { |
| 129 | actionsToReceivers.getOrPut(it) { |
| 130 | changed = true |
| 131 | ArraySet() |
| 132 | }.add(receiverData) |
| 133 | } |
| 134 | if (changed) { |
Fabian Kozynski | 213049e | 2019-11-06 17:23:17 -0500 | [diff] [blame] | 135 | createFilterAndRegisterReceiverBG() |
Fabian Kozynski | 42f86a0 | 2019-07-09 12:39:03 -0400 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
| 139 | private fun handleUnregisterReceiver(receiver: BroadcastReceiver) { |
Fabian Kozynski | 213049e | 2019-11-06 17:23:17 -0500 | [diff] [blame] | 140 | Preconditions.checkState(bgHandler.looper.isCurrentThread, |
| 141 | "This method should only be called from BG thread") |
Fabian Kozynski | 42f86a0 | 2019-07-09 12:39:03 -0400 | [diff] [blame] | 142 | if (DEBUG) Log.w(TAG, "Unregister receiver: $receiver") |
| 143 | val actions = receiverToReceiverData.getOrElse(receiver) { return } |
| 144 | .flatMap { it.filter.actionsIterator().asSequence().asIterable() }.toSet() |
| 145 | receiverToReceiverData.get(receiver)?.clear() |
| 146 | var changed = false |
| 147 | actions.forEach { action -> |
| 148 | actionsToReceivers.get(action)?.removeIf { it.receiver == receiver } |
| 149 | if (actionsToReceivers.get(action)?.isEmpty() ?: false) { |
| 150 | changed = true |
| 151 | actionsToReceivers.remove(action) |
| 152 | } |
| 153 | } |
| 154 | if (changed) { |
Fabian Kozynski | 213049e | 2019-11-06 17:23:17 -0500 | [diff] [blame] | 155 | createFilterAndRegisterReceiverBG() |
Fabian Kozynski | 42f86a0 | 2019-07-09 12:39:03 -0400 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
Fabian Kozynski | 213049e | 2019-11-06 17:23:17 -0500 | [diff] [blame] | 159 | // Only call this from a BG thread |
| 160 | private fun createFilterAndRegisterReceiverBG() { |
| 161 | val intentFilter = createFilter() |
| 162 | mainHandler.post(RegisterReceiverRunnable(intentFilter)) |
| 163 | } |
| 164 | |
Lucas Dupin | 64171fe | 2019-10-30 14:28:29 -0700 | [diff] [blame] | 165 | override fun dump(fd: FileDescriptor, pw: PrintWriter, args: Array<out String>) { |
| 166 | pw.println(" Registered=${registered.get()}") |
Fabian Kozynski | 42f86a0 | 2019-07-09 12:39:03 -0400 | [diff] [blame] | 167 | actionsToReceivers.forEach { (action, list) -> |
Lucas Dupin | 64171fe | 2019-10-30 14:28:29 -0700 | [diff] [blame] | 168 | pw.println(" $action:") |
| 169 | list.forEach { pw.println(" ${it.receiver}") } |
Fabian Kozynski | 42f86a0 | 2019-07-09 12:39:03 -0400 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
| 173 | private class HandleBroadcastRunnable( |
| 174 | val actionsToReceivers: Map<String, Set<ReceiverData>>, |
| 175 | val context: Context, |
Fabian Kozynski | ff5e91f | 2019-09-24 15:38:08 -0400 | [diff] [blame] | 176 | val intent: Intent, |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 177 | val pendingResult: PendingResult, |
| 178 | val index: Int |
Fabian Kozynski | 42f86a0 | 2019-07-09 12:39:03 -0400 | [diff] [blame] | 179 | ) : Runnable { |
| 180 | override fun run() { |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 181 | if (DEBUG) Log.w(TAG, "[$index] Dispatching $intent") |
Fabian Kozynski | 42f86a0 | 2019-07-09 12:39:03 -0400 | [diff] [blame] | 182 | actionsToReceivers.get(intent.action) |
| 183 | ?.filter { |
| 184 | it.filter.hasAction(intent.action) && |
| 185 | it.filter.matchCategories(intent.categories) == null } |
| 186 | ?.forEach { |
| 187 | it.handler.post { |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 188 | if (DEBUG) Log.w(TAG, |
| 189 | "[$index] Dispatching ${intent.action} to ${it.receiver}") |
Fabian Kozynski | ff5e91f | 2019-09-24 15:38:08 -0400 | [diff] [blame] | 190 | it.receiver.pendingResult = pendingResult |
Fabian Kozynski | 42f86a0 | 2019-07-09 12:39:03 -0400 | [diff] [blame] | 191 | it.receiver.onReceive(context, intent) |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | } |
Fabian Kozynski | 213049e | 2019-11-06 17:23:17 -0500 | [diff] [blame] | 196 | |
| 197 | private inner class RegisterReceiverRunnable(val intentFilter: IntentFilter) : Runnable { |
| 198 | |
| 199 | /* |
| 200 | * Registers and unregisters the BroadcastReceiver |
| 201 | * |
| 202 | * Must be called from Main Thread |
| 203 | */ |
| 204 | @MainThread |
| 205 | override fun run() { |
| 206 | if (registered.get()) { |
| 207 | context.unregisterReceiver(this@UserBroadcastDispatcher) |
| 208 | registered.set(false) |
| 209 | } |
| 210 | // Short interval without receiver, this can be problematic |
| 211 | if (intentFilter.countActions() > 0 && !registered.get()) { |
| 212 | context.registerReceiverAsUser( |
| 213 | this@UserBroadcastDispatcher, |
| 214 | UserHandle.of(userId), |
| 215 | intentFilter, |
| 216 | null, |
| 217 | bgHandler) |
| 218 | registered.set(true) |
| 219 | } |
| 220 | } |
| 221 | } |
Fabian Kozynski | 42f86a0 | 2019-07-09 12:39:03 -0400 | [diff] [blame] | 222 | } |