• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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.color;
18 
19 import android.content.pm.PackageManagerInternal;
20 import android.os.ShellCommand;
21 
22 import com.android.server.LocalServices;
23 
24 class ColorDisplayShellCommand extends ShellCommand {
25 
26     private static final String USAGE = "usage: cmd color_display SUBCOMMAND [ARGS]\n"
27             + "    help\n"
28             + "      Shows this message.\n"
29             + "    set-saturation LEVEL\n"
30             + "      Sets the device saturation to the given LEVEL, 0-100 inclusive.\n"
31             + "    set-layer-saturation LEVEL CALLER_PACKAGE TARGET_PACKAGE\n"
32             + "      Sets the saturation LEVEL for all layers of the TARGET_PACKAGE, attributed\n"
33             + "      to the CALLER_PACKAGE. The lowest LEVEL from any CALLER_PACKAGE is applied.\n";
34 
35     private static final int ERROR = -1;
36     private static final int SUCCESS = 0;
37 
38     private final ColorDisplayService mService;
39 
ColorDisplayShellCommand(ColorDisplayService service)40     ColorDisplayShellCommand(ColorDisplayService service) {
41         mService = service;
42     }
43 
44     @Override
onCommand(String cmd)45     public int onCommand(String cmd) {
46         if (cmd == null) {
47             return handleDefaultCommands(cmd);
48         }
49         switch (cmd) {
50             case "set-saturation":
51                 return setSaturation();
52             case "set-layer-saturation":
53                 return setLayerSaturation();
54             default:
55                 return handleDefaultCommands(cmd);
56         }
57     }
58 
setSaturation()59     private int setSaturation() {
60         final int level = getLevel();
61         if (level == ERROR) {
62             return ERROR;
63         }
64         mService.setSaturationLevelInternal(level);
65         return SUCCESS;
66     }
67 
setLayerSaturation()68     private int setLayerSaturation() {
69         final int level = getLevel();
70         if (level == ERROR) {
71             return ERROR;
72         }
73         final String callerPackageName = getPackageName();
74         if (callerPackageName == null) {
75             getErrPrintWriter().println("Error: CALLER_PACKAGE must be an installed package name");
76             return ERROR;
77         }
78         final String targetPackageName = getPackageName();
79         if (targetPackageName == null) {
80             getErrPrintWriter().println("Error: TARGET_PACKAGE must be an installed package name");
81             return ERROR;
82         }
83         mService.setAppSaturationLevelInternal(callerPackageName, targetPackageName, level);
84         return SUCCESS;
85     }
86 
getPackageName()87     private String getPackageName() {
88         final String packageNameArg = getNextArg();
89         return LocalServices.getService(PackageManagerInternal.class).getPackage(packageNameArg)
90                 == null
91                 ? null : packageNameArg;
92     }
93 
getLevel()94     private int getLevel() {
95         final String levelArg = getNextArg();
96         if (levelArg == null) {
97             getErrPrintWriter().println("Error: Required argument LEVEL is unspecified");
98             return ERROR;
99         }
100         final int level;
101         try {
102             level = Integer.parseInt(levelArg);
103         } catch (NumberFormatException e) {
104             getErrPrintWriter().println("Error: LEVEL argument is not an integer");
105             return ERROR;
106         }
107         if (level < 0 || level > 100) {
108             getErrPrintWriter()
109                     .println("Error: LEVEL argument must be an integer between 0 and 100");
110             return ERROR;
111         }
112         return level;
113     }
114 
115     @Override
onHelp()116     public void onHelp() {
117         getOutPrintWriter().print(USAGE);
118     }
119 }
120