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

Commit 6e521b22 authored by Michal Brzezinski's avatar Michal Brzezinski
Browse files

Add broadcast listener launching tutorial for keyboard and touchpad

Required when tutorial is launched by non-system apps as it needs to be launched as user 0.

Bug: 361518125
Flag: com.android.systemui.shared.new_touchpad_gestures_tutorial
Test: KeyboardTouchpadTutorialCoreStartableTest
Change-Id: Idfa70d61322aa55eca85b7a7f71b055110b4f0fd
parent 907fb870
Loading
Loading
Loading
Loading
+34 −2
Original line number Diff line number Diff line
@@ -16,9 +16,17 @@

package com.android.systemui.inputdevice.tutorial

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.UserHandle
import com.android.systemui.CoreStartable
import com.android.systemui.broadcast.BroadcastDispatcher
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Application
import com.android.systemui.inputdevice.tutorial.ui.TutorialNotificationCoordinator
import com.android.systemui.inputdevice.tutorial.ui.view.KeyboardTouchpadTutorialActivity
import com.android.systemui.shared.Flags.newTouchpadGesturesTutorial
import dagger.Lazy
import javax.inject.Inject
@@ -27,11 +35,35 @@ import javax.inject.Inject
@SysUISingleton
class KeyboardTouchpadTutorialCoreStartable
@Inject
constructor(private val tutorialNotificationCoordinator: Lazy<TutorialNotificationCoordinator>) :
    CoreStartable {
constructor(
    private val tutorialNotificationCoordinator: Lazy<TutorialNotificationCoordinator>,
    private val broadcastDispatcher: BroadcastDispatcher,
    @Application private val applicationContext: Context,
) : CoreStartable {
    override fun start() {
        if (newTouchpadGesturesTutorial()) {
            tutorialNotificationCoordinator.get().start()
            registerTutorialBroadcastReceiver()
        }
    }

    private fun registerTutorialBroadcastReceiver() {
        broadcastDispatcher.registerReceiver(
            receiver =
                object : BroadcastReceiver() {
                    override fun onReceive(context: Context, intent: Intent) {
                        applicationContext.startActivityAsUser(
                            Intent(
                                applicationContext,
                                KeyboardTouchpadTutorialActivity::class.java
                            ),
                            UserHandle.SYSTEM
                        )
                    }
                },
            filter = IntentFilter("com.android.systemui.action.KEYBOARD_TOUCHPAD_TUTORIAL"),
            flags = Context.RECEIVER_EXPORTED,
            user = UserHandle.ALL,
        )
    }
}
+60 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.inputdevice.tutorial

import android.content.Context
import android.content.Intent
import android.os.UserHandle
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.broadcast.broadcastDispatcher
import com.android.systemui.inputdevice.tutorial.ui.TutorialNotificationCoordinator
import com.android.systemui.testKosmos
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.any
import org.mockito.kotlin.eq
import org.mockito.kotlin.mock
import org.mockito.kotlin.verify

@SmallTest
@RunWith(AndroidJUnit4::class)
class KeyboardTouchpadTutorialCoreStartableTest : SysuiTestCase() {

    private val kosmos = testKosmos()
    private val broadcastDispatcher = kosmos.broadcastDispatcher
    private val context = mock<Context>()
    private val underTest =
        KeyboardTouchpadTutorialCoreStartable(
            { mock<TutorialNotificationCoordinator>() },
            broadcastDispatcher,
            context
        )

    @Test
    fun registersBroadcastReceiverStartingActivityAsSystemUser() {
        underTest.start()

        broadcastDispatcher.sendIntentToMatchingReceiversOnly(
            context,
            Intent("com.android.systemui.action.KEYBOARD_TOUCHPAD_TUTORIAL")
        )

        verify(context).startActivityAsUser(any(), eq(UserHandle.SYSTEM))
    }
}