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

Commit 252fb2b6 authored by Aaron Liu's avatar Aaron Liu
Browse files

Add a shell command interface for layouts.

Uses a map that will change layouts based on the shell command.

Bug: 288242927
Test: run relevant shell commands

Change-Id: I9b6121a7389c2003b9a6275fa9e3098d8b8a5b69
parent 02ec5b8b
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ import com.android.systemui.flags.Flags
import com.android.systemui.keyguard.ui.binder.KeyguardIndicationAreaBinder
import com.android.systemui.keyguard.ui.view.KeyguardRootView
import com.android.systemui.keyguard.ui.view.layout.KeyguardLayoutManager
import com.android.systemui.keyguard.ui.view.layout.KeyguardLayoutManagerCommandListener
import com.android.systemui.keyguard.ui.viewmodel.KeyguardIndicationAreaViewModel
import com.android.systemui.shade.NotificationShadeWindowView
import com.android.systemui.statusbar.KeyguardIndicationController
@@ -44,6 +45,7 @@ constructor(
    private val featureFlags: FeatureFlags,
    private val indicationController: KeyguardIndicationController,
    private val keyguardLayoutManager: KeyguardLayoutManager,
    private val keyguardLayoutManagerCommandListener: KeyguardLayoutManagerCommandListener,
) : CoreStartable {

    private var indicationAreaHandle: DisposableHandle? = null
@@ -54,6 +56,7 @@ constructor(
        bindIndicationArea(notificationPanel)
        bindLockIconView(notificationPanel)
        keyguardLayoutManager.layoutViews()
        keyguardLayoutManagerCommandListener.start()
    }

    fun bindIndicationArea(legacyParent: ViewGroup) {
+63 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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.keyguard.ui.view.layout

import com.android.systemui.statusbar.commandline.Command
import com.android.systemui.statusbar.commandline.CommandRegistry
import java.io.PrintWriter
import javax.inject.Inject

/** Uses $ adb shell cmd statusbar layout <LayoutId> */
class KeyguardLayoutManagerCommandListener
@Inject
constructor(
    private val commandRegistry: CommandRegistry,
    private val keyguardLayoutManager: KeyguardLayoutManager
) {
    private val layoutCommand = KeyguardLayoutManagerCommand()

    fun start() {
        commandRegistry.registerCommand(COMMAND) { layoutCommand }
    }

    internal inner class KeyguardLayoutManagerCommand : Command {
        override fun execute(pw: PrintWriter, args: List<String>) {
            val arg = args.getOrNull(0)
            if (arg == null || arg.lowercase() == "help") {
                help(pw)
                return
            }

            if (keyguardLayoutManager.transitionToLayout(arg)) {
                pw.println("Transition succeeded!")
            } else {
                pw.println("Invalid argument! To see available layout ids, run:")
                pw.println("$ adb shell cmd statusbar layout help")
            }
        }

        override fun help(pw: PrintWriter) {
            pw.println("Usage: $ adb shell cmd statusbar layout <layoutId>")
            pw.println("Existing Layout Ids: ")
            keyguardLayoutManager.layoutIdMap.forEach { entry -> pw.println("${entry.key}") }
        }
    }

    companion object {
        internal const val COMMAND = "layout"
    }
}
+88 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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.keyguard.ui.view.layout

import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.statusbar.commandline.Command
import com.android.systemui.statusbar.commandline.CommandRegistry
import com.android.systemui.util.mockito.eq
import com.android.systemui.util.mockito.whenever
import com.android.systemui.util.mockito.withArgCaptor
import java.io.PrintWriter
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.mockito.ArgumentMatchers.anyString
import org.mockito.Mock
import org.mockito.Mockito.atLeastOnce
import org.mockito.Mockito.mock
import org.mockito.Mockito.never
import org.mockito.Mockito.verify
import org.mockito.MockitoAnnotations

@RunWith(JUnit4::class)
@SmallTest
class KeyguardLayoutManagerCommandListenerTest : SysuiTestCase() {
    private lateinit var keyguardLayoutManagerCommandListener: KeyguardLayoutManagerCommandListener
    @Mock private lateinit var commandRegistry: CommandRegistry
    @Mock private lateinit var keyguardLayoutManager: KeyguardLayoutManager
    @Mock private lateinit var pw: PrintWriter
    private lateinit var command: () -> Command

    @Before
    fun setup() {
        MockitoAnnotations.initMocks(this)
        keyguardLayoutManagerCommandListener =
            KeyguardLayoutManagerCommandListener(
                commandRegistry,
                keyguardLayoutManager,
            )
        keyguardLayoutManagerCommandListener.start()
        command =
            withArgCaptor<() -> Command> {
                verify(commandRegistry).registerCommand(eq("layout"), capture())
            }
    }

    @Test
    fun testHelp() {
        command().execute(pw, listOf("help"))
        verify(pw, atLeastOnce()).println(anyString())
        verify(keyguardLayoutManager, never()).transitionToLayout(anyString())
    }

    @Test
    fun testBlank() {
        command().execute(pw, listOf())
        verify(pw, atLeastOnce()).println(anyString())
        verify(keyguardLayoutManager, never()).transitionToLayout(anyString())
    }

    @Test
    fun testValidArg() {
        bindFakeIdMapToLayoutManager()
        command().execute(pw, listOf("fake"))
        verify(keyguardLayoutManager).transitionToLayout("fake")
    }

    private fun bindFakeIdMapToLayoutManager() {
        val map = mapOf("fake" to mock(LockscreenLayout::class.java))
        whenever(keyguardLayoutManager.layoutIdMap).thenReturn(map)
    }
}