1 /* 2 * Copyright (C) 2024 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.shade 18 19 import android.provider.Settings.Global.DEVELOPMENT_SHADE_DISPLAY_AWARENESS 20 import com.android.systemui.CoreStartable 21 import com.android.systemui.dagger.SysUISingleton 22 import com.android.systemui.display.data.repository.DisplayRepository 23 import com.android.systemui.shade.data.repository.ShadeDisplaysRepository 24 import com.android.systemui.shade.display.ShadeDisplayPolicy 25 import com.android.systemui.statusbar.commandline.Command 26 import com.android.systemui.statusbar.commandline.CommandRegistry 27 import com.android.systemui.util.settings.GlobalSettings 28 import java.io.PrintWriter 29 import javax.inject.Inject 30 31 @SysUISingleton 32 class ShadePrimaryDisplayCommand 33 @Inject 34 constructor( 35 private val globalSettings: GlobalSettings, 36 private val commandRegistry: CommandRegistry, 37 private val displaysRepository: DisplayRepository, 38 private val positionRepository: ShadeDisplaysRepository, 39 private val policies: Set<@JvmSuppressWildcards ShadeDisplayPolicy>, 40 private val defaultPolicy: ShadeDisplayPolicy, 41 ) : Command, CoreStartable { 42 startnull43 override fun start() { 44 commandRegistry.registerCommand("shade_display_override") { this } 45 } 46 helpnull47 override fun help(pw: PrintWriter) { 48 pw.println("shade_display_override <policyName> ") 49 pw.println("Set the display which is holding the shade, or the policy that defines it.") 50 pw.println() 51 pw.println("shade_display_override policies") 52 pw.println("Lists available policies") 53 pw.println() 54 pw.println("shade_display_override reset ") 55 pw.println("Reset the display which is holding the shade.") 56 pw.println() 57 pw.println("shade_display_override (list|status) ") 58 pw.println("Lists available displays and which has the shade") 59 } 60 executenull61 override fun execute(pw: PrintWriter, args: List<String>) { 62 CommandHandler(pw, args).execute() 63 } 64 65 /** Wrapper class to avoid propagating [PrintWriter] to all methods. */ 66 private inner class CommandHandler( 67 private val pw: PrintWriter, 68 private val args: List<String>, 69 ) { 70 executenull71 fun execute() { 72 when (val command = args.getOrNull(0)?.lowercase()) { 73 "reset" -> reset() 74 "policies" -> printPolicies() 75 "list", 76 "status" -> printStatus() 77 null -> help(pw) 78 else -> parsePolicy(command) 79 } 80 } 81 parsePolicynull82 private fun parsePolicy(policyIdentifier: String) { 83 if (policies.any { it.name == policyIdentifier }) { 84 globalSettings.putString(DEVELOPMENT_SHADE_DISPLAY_AWARENESS, policyIdentifier) 85 } else { 86 help(pw) 87 } 88 } 89 resetnull90 private fun reset() { 91 globalSettings.putString(DEVELOPMENT_SHADE_DISPLAY_AWARENESS, defaultPolicy.name) 92 pw.println("Reset shade display policy to default policy: ${defaultPolicy.name}") 93 } 94 printStatusnull95 private fun printStatus() { 96 val displays = displaysRepository.displays.value 97 val shadeDisplay = positionRepository.displayId.value 98 pw.println("Available displays: ") 99 displays.forEach { 100 pw.print(" - ${it.displayId}") 101 pw.println(if (it.displayId == shadeDisplay) " (Shade window is here)" else "") 102 } 103 } 104 printPoliciesnull105 private fun printPolicies() { 106 val currentPolicyName = positionRepository.currentPolicy.name 107 pw.println("Available policies: ") 108 policies.forEach { 109 pw.print(" - ${it.name}") 110 pw.println(if (currentPolicyName == it.name) " (Current policy)" else "") 111 } 112 } 113 } 114 } 115