1 /*
2  * Copyright (C) 2018 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.server.display;
18 
19 import android.content.Context;
20 import android.content.Intent;
21 import android.hardware.display.DisplayManager;
22 import android.os.ShellCommand;
23 import android.view.Display;
24 
25 import java.io.PrintWriter;
26 
27 class DisplayManagerShellCommand extends ShellCommand {
28     private static final String TAG = "DisplayManagerShellCommand";
29 
30     private final DisplayManagerService mService;
31 
DisplayManagerShellCommand(DisplayManagerService service)32     DisplayManagerShellCommand(DisplayManagerService service) {
33         mService = service;
34     }
35 
36     @Override
onCommand(String cmd)37     public int onCommand(String cmd) {
38         if (cmd == null) {
39             return handleDefaultCommands(cmd);
40         }
41         final PrintWriter pw = getOutPrintWriter();
42         switch(cmd) {
43             case "set-brightness":
44                 return setBrightness();
45             case "reset-brightness-configuration":
46                 return resetBrightnessConfiguration();
47             case "ab-logging-enable":
48                 return setAutoBrightnessLoggingEnabled(true);
49             case "ab-logging-disable":
50                 return setAutoBrightnessLoggingEnabled(false);
51             case "dwb-logging-enable":
52                 return setDisplayWhiteBalanceLoggingEnabled(true);
53             case "dwb-logging-disable":
54                 return setDisplayWhiteBalanceLoggingEnabled(false);
55             case "dmd-logging-enable":
56                 return setDisplayModeDirectorLoggingEnabled(true);
57             case "dmd-logging-disable":
58                 return setDisplayModeDirectorLoggingEnabled(false);
59             case "dwb-set-cct":
60                 return setAmbientColorTemperatureOverride();
61             default:
62                 return handleDefaultCommands(cmd);
63         }
64     }
65 
66     @Override
onHelp()67     public void onHelp() {
68         final PrintWriter pw = getOutPrintWriter();
69         pw.println("Display manager commands:");
70         pw.println("  help");
71         pw.println("    Print this help text.");
72         pw.println();
73         pw.println("  set-brightness BRIGHTNESS");
74         pw.println("    Sets the current brightness to BRIGHTNESS (a number between 0 and 1).");
75         pw.println("  reset-brightness-configuration");
76         pw.println("    Reset the brightness to its default configuration.");
77         pw.println("  ab-logging-enable");
78         pw.println("    Enable auto-brightness logging.");
79         pw.println("  ab-logging-disable");
80         pw.println("    Disable auto-brightness logging.");
81         pw.println("  dwb-logging-enable");
82         pw.println("    Enable display white-balance logging.");
83         pw.println("  dwb-logging-disable");
84         pw.println("    Disable display white-balance logging.");
85         pw.println("  dmd-logging-enable");
86         pw.println("    Enable display mode director logging.");
87         pw.println("  dmd-logging-disable");
88         pw.println("    Disable display mode director logging.");
89         pw.println("  dwb-set-cct CCT");
90         pw.println("    Sets the ambient color temperature override to CCT (use -1 to disable).");
91         pw.println();
92         Intent.printIntentArgsHelp(pw , "");
93     }
94 
setBrightness()95     private int setBrightness() {
96         String brightnessText = getNextArg();
97         if (brightnessText == null) {
98             getErrPrintWriter().println("Error: no brightness specified");
99             return 1;
100         }
101         float brightness = -1.0f;
102         try {
103             brightness = Float.parseFloat(brightnessText);
104         } catch (NumberFormatException e) {
105         }
106         if (brightness < 0 || brightness > 1) {
107             getErrPrintWriter().println("Error: brightness should be a number between 0 and 1");
108             return 1;
109         }
110 
111         final Context context = mService.getContext();
112         final DisplayManager dm = context.getSystemService(DisplayManager.class);
113         dm.setBrightness(Display.DEFAULT_DISPLAY, brightness);
114         return 0;
115     }
116 
resetBrightnessConfiguration()117     private int resetBrightnessConfiguration() {
118         mService.resetBrightnessConfigurations();
119         return 0;
120     }
121 
setAutoBrightnessLoggingEnabled(boolean enabled)122     private int setAutoBrightnessLoggingEnabled(boolean enabled) {
123         mService.setAutoBrightnessLoggingEnabled(enabled);
124         return 0;
125     }
126 
setDisplayWhiteBalanceLoggingEnabled(boolean enabled)127     private int setDisplayWhiteBalanceLoggingEnabled(boolean enabled) {
128         mService.setDisplayWhiteBalanceLoggingEnabled(enabled);
129         return 0;
130     }
131 
setDisplayModeDirectorLoggingEnabled(boolean enabled)132     private int setDisplayModeDirectorLoggingEnabled(boolean enabled) {
133         mService.setDisplayModeDirectorLoggingEnabled(enabled);
134         return 0;
135     }
136 
setAmbientColorTemperatureOverride()137     private int setAmbientColorTemperatureOverride() {
138         String cctText = getNextArg();
139         if (cctText == null) {
140             getErrPrintWriter().println("Error: no cct specified");
141             return 1;
142         }
143         float cct;
144         try {
145             cct = Float.parseFloat(cctText);
146         } catch (NumberFormatException e) {
147             getErrPrintWriter().println("Error: cct should be a number");
148             return 1;
149         }
150         mService.setAmbientColorTemperatureOverride(cct);
151         return 0;
152     }
153 }
154