• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 package com.android.systemui.lowlightclock
17 
18 import android.text.TextUtils
19 import android.util.Log
20 import com.android.systemui.dagger.qualifiers.Background
21 import com.android.systemui.shared.condition.Condition
22 import com.android.systemui.statusbar.commandline.Command
23 import com.android.systemui.statusbar.commandline.CommandRegistry
24 import java.io.PrintWriter
25 import javax.inject.Inject
26 import kotlinx.coroutines.CoroutineScope
27 
28 /**
29  * This condition registers for and fulfills cmd shell commands to force a device into or out of
30  * low-light conditions.
31  */
32 class ForceLowLightCondition
33 @Inject
34 constructor(@Background scope: CoroutineScope, commandRegistry: CommandRegistry) :
35     Condition(scope, null, true) {
36     /**
37      * Default Constructor.
38      *
39      * @param commandRegistry command registry to register commands with.
40      */
41     init {
42         if (DEBUG) {
43             Log.d(TAG, "registering commands")
44         }
<lambda>null45         commandRegistry.registerCommand(COMMAND_ROOT) {
46             object : Command {
47                 override fun execute(pw: PrintWriter, args: List<String>) {
48                     if (args.size != 1) {
49                         pw.println("no command specified")
50                         help(pw)
51                         return
52                     }
53 
54                     val cmd = args[0]
55 
56                     if (TextUtils.equals(cmd, COMMAND_ENABLE_LOW_LIGHT)) {
57                         logAndPrint(pw, "forcing low light")
58                         updateCondition(true)
59                     } else if (TextUtils.equals(cmd, COMMAND_DISABLE_LOW_LIGHT)) {
60                         logAndPrint(pw, "forcing to not enter low light")
61                         updateCondition(false)
62                     } else if (TextUtils.equals(cmd, COMMAND_CLEAR_LOW_LIGHT)) {
63                         logAndPrint(pw, "clearing any forced low light")
64                         clearCondition()
65                     } else {
66                         pw.println("invalid command")
67                         help(pw)
68                     }
69                 }
70 
71                 override fun help(pw: PrintWriter) {
72                     pw.println("Usage: adb shell cmd statusbar low-light <cmd>")
73                     pw.println("Supported commands:")
74                     pw.println("  - enable")
75                     pw.println("    forces device into low-light")
76                     pw.println("  - disable")
77                     pw.println("    forces device to not enter low-light")
78                     pw.println("  - clear")
79                     pw.println("    clears any previously forced state")
80                 }
81 
82                 private fun logAndPrint(pw: PrintWriter, message: String) {
83                     pw.println(message)
84                     if (DEBUG) {
85                         Log.d(TAG, message)
86                     }
87                 }
88             }
89         }
90     }
91 
startnull92     override suspend fun start() {}
93 
stopnull94     override fun stop() {}
95 
96     override val startStrategy: Int
97         get() = START_EAGERLY
98 
99     companion object {
100         /** Command root */
101         const val COMMAND_ROOT: String = "low-light"
102 
103         /** Command for forcing device into low light. */
104         const val COMMAND_ENABLE_LOW_LIGHT: String = "enable"
105 
106         /** Command for preventing a device from entering low light. */
107         const val COMMAND_DISABLE_LOW_LIGHT: String = "disable"
108 
109         /** Command for clearing previously forced low-light conditions. */
110         const val COMMAND_CLEAR_LOW_LIGHT: String = "clear"
111 
112         private const val TAG = "ForceLowLightCondition"
113         private val DEBUG = Log.isLoggable(TAG, Log.DEBUG)
114     }
115 }
116