Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bcb81913 authored by Fabian Kozynski's avatar Fabian Kozynski
Browse files

Use null as default argument

This improves mocking it, as the Java code that results in creating the
overloads doesn't need to call context.getMainExecutor or
context.getUser.

As an example, with a mock mDispatcher, calls to
`mDispatcher(receiver, filter)` with some configurations of Mockito
would fail, as the actual Java method would create the default parameters
calling `context.getMainExecutor()` and `context.getUser()`, but `context`
is null in the mock.

Test: atest SystemUITests
Change-Id: Ia97b62134532c60525d1df33023cc2dd99c1f6a8
parent 1af66d72
Loading
Loading
Loading
Loading
+7 −7
Original line number Diff line number Diff line
@@ -62,17 +62,17 @@ Acquire the dispatcher by using `@Inject` to obtain a `BroadcastDispatcher`. The
 * @param executor An executor to dispatch [BroadcastReceiver.onReceive]. Pass null to use an
 *                 executor in the main thread (default).
 * @param user A user handle to determine which broadcast should be dispatched to this receiver.
 *             By default, it is the user of the context (system user in SystemUI).
 *             Pass `null` to use the user of the context (system user in SystemUI).
 * @throws IllegalArgumentException if the filter has other constraints that are not actions or
 *                                  categories or the filter has no actions.
 */
@JvmOverloads
fun registerReceiver(
        BroadcastReceiver, 
        IntentFilter, 
        Executor? = context.mainExecutor,
        UserHandle = context.user
) {
open fun registerReceiver(
    receiver: BroadcastReceiver,
    filter: IntentFilter,
    executor: Executor? = null,
    user: UserHandle? = null
)
```

All subscriptions are done with the same overloaded method. As specified in the doc, in order to pass a `UserHandle` with the default `Executor`, pass `null` for the `Executor`.
+9 −5
Original line number Diff line number Diff line
@@ -112,7 +112,7 @@ open class BroadcastDispatcher constructor (
     * @param executor An executor to dispatch [BroadcastReceiver.onReceive]. Pass null to use an
     *                 executor in the main thread (default).
     * @param user A user handle to determine which broadcast should be dispatched to this receiver.
     *             By default, it is the user of the context (system user in SystemUI).
     *             Pass `null` to use the user of the context (system user in SystemUI).
     * @throws IllegalArgumentException if the filter has other constraints that are not actions or
     *                                  categories or the filter has no actions.
     */
@@ -120,13 +120,17 @@ open class BroadcastDispatcher constructor (
    open fun registerReceiver(
        receiver: BroadcastReceiver,
        filter: IntentFilter,
        executor: Executor? = context.mainExecutor,
        user: UserHandle = context.user
        executor: Executor? = null,
        user: UserHandle? = null
    ) {
        checkFilter(filter)
        this.handler
                .obtainMessage(MSG_ADD_RECEIVER,
                        ReceiverData(receiver, filter, executor ?: context.mainExecutor, user))
                .obtainMessage(MSG_ADD_RECEIVER, ReceiverData(
                        receiver,
                        filter,
                        executor ?: context.mainExecutor,
                        user ?: context.user
                ))
                .sendToTarget()
    }

+1 −1
Original line number Diff line number Diff line
@@ -53,7 +53,7 @@ class FakeBroadcastDispatcher(
        receiver: BroadcastReceiver,
        filter: IntentFilter,
        executor: Executor?,
        user: UserHandle
        user: UserHandle?
    ) {
        registeredReceivers.add(receiver)
    }