Convert notification debug filter to CommandRegistry

Bug: 235268992
Test: PlatformScenarioTests
Change-Id: I646820a93b9b3129029a39564f3627f6773709aa
This commit is contained in:
Jeff DeCew
2021-12-28 20:41:25 +00:00
parent abcff90b23
commit 1149017c45

View File

@@ -25,6 +25,8 @@ import android.util.Log
import com.android.systemui.Dumpable import com.android.systemui.Dumpable
import com.android.systemui.dagger.SysUISingleton import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dump.DumpManager import com.android.systemui.dump.DumpManager
import com.android.systemui.statusbar.commandline.Command
import com.android.systemui.statusbar.commandline.CommandRegistry
import com.android.systemui.statusbar.notification.collection.NotificationEntry import com.android.systemui.statusbar.notification.collection.NotificationEntry
import com.android.systemui.util.Assert import com.android.systemui.util.Assert
import com.android.systemui.util.ListenerSet import com.android.systemui.util.ListenerSet
@@ -43,15 +45,20 @@ import javax.inject.Inject
* *
* `$ adb shell am broadcast -a com.android.systemui.action.SET_NOTIF_DEBUG_MODE * `$ adb shell am broadcast -a com.android.systemui.action.SET_NOTIF_DEBUG_MODE
* --esal allowed_packages <comma-separated-packages>` * --esal allowed_packages <comma-separated-packages>`
* or
* `$ adb shell cmd statusbar notif-filter allowed-pkgs <package> ...`
* *
* To disable filtering, send the action without a list: * To disable filtering, send the action without a list:
* *
* `$ adb shell am broadcast -a com.android.systemui.action.SET_NOTIF_DEBUG_MODE` * `$ adb shell am broadcast -a com.android.systemui.action.SET_NOTIF_DEBUG_MODE`
* or
* `$ adb shell cmd statusbar notif-filter reset`
* *
* NOTE: this feature only works on debug builds, and when the broadcaster is root. * NOTE: this feature only works on debug builds, and when the broadcaster is root.
*/ */
@SysUISingleton @SysUISingleton
class DebugModeFilterProvider @Inject constructor( class DebugModeFilterProvider @Inject constructor(
private val commandRegistry: CommandRegistry,
private val context: Context, private val context: Context,
dumpManager: DumpManager dumpManager: DumpManager
) : Dumpable { ) : Dumpable {
@@ -74,6 +81,7 @@ class DebugModeFilterProvider @Inject constructor(
val needsInitialization = listeners.isEmpty() val needsInitialization = listeners.isEmpty()
listeners.addIfAbsent(listener) listeners.addIfAbsent(listener)
if (needsInitialization) { if (needsInitialization) {
commandRegistry.registerCommand("notif-filter") { NotifFilterCommand() }
val filter = IntentFilter().apply { addAction(ACTION_SET_NOTIF_DEBUG_MODE) } val filter = IntentFilter().apply { addAction(ACTION_SET_NOTIF_DEBUG_MODE) }
val permission = NOTIF_DEBUG_MODE_PERMISSION val permission = NOTIF_DEBUG_MODE_PERMISSION
context.registerReceiver(mReceiver, filter, permission, null, Context.RECEIVER_EXPORTED) context.registerReceiver(mReceiver, filter, permission, null, Context.RECEIVER_EXPORTED)
@@ -126,4 +134,46 @@ class DebugModeFilterProvider @Inject constructor(
"com.android.systemui.permission.NOTIF_DEBUG_MODE" "com.android.systemui.permission.NOTIF_DEBUG_MODE"
private const val EXTRA_ALLOWED_PACKAGES = "allowed_packages" private const val EXTRA_ALLOWED_PACKAGES = "allowed_packages"
} }
inner class NotifFilterCommand : Command {
override fun execute(pw: PrintWriter, args: List<String>) {
when (args.firstOrNull()) {
"reset" -> {
if (args.size > 1) {
return invalidCommand(pw, "Unexpected arguments for 'reset' command")
}
allowedPackages = emptyList()
}
"allowed-pkgs" -> {
allowedPackages = args.drop(1)
}
null -> return invalidCommand(pw, "Missing command")
else -> return invalidCommand(pw, "Unknown command: ${args.firstOrNull()}")
}
Log.d(TAG, "Updated allowedPackages: $allowedPackages")
if (allowedPackages.isEmpty()) {
pw.print("Resetting allowedPackages ... ")
} else {
pw.print("Updating allowedPackages: $allowedPackages ... ")
}
listeners.forEach(Runnable::run)
pw.println("DONE")
}
private fun invalidCommand(pw: PrintWriter, reason: String) {
pw.println("Error: $reason")
pw.println()
help(pw)
}
override fun help(pw: PrintWriter) {
pw.println("Usage: adb shell cmd statusbar notif-filter <command>")
pw.println("Available commands:")
pw.println(" reset")
pw.println(" Restore the default system behavior.")
pw.println(" allowed-pkgs <package> ...")
pw.println(" Hide all notification except from packages listed here.")
pw.println(" Providing no packages is treated as a reset.")
}
}
} }