Jocelyn Dang | f2c38c1 | 2017-03-31 14:03:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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.power; |
| 18 | |
| 19 | import android.content.Intent; |
| 20 | import android.os.IPowerManager; |
| 21 | import android.os.RemoteException; |
| 22 | import android.os.ShellCommand; |
| 23 | |
| 24 | import java.io.PrintWriter; |
| 25 | |
| 26 | class PowerManagerShellCommand extends ShellCommand { |
| 27 | private static final int LOW_POWER_MODE_ON = 1; |
| 28 | |
| 29 | final IPowerManager mInterface; |
| 30 | |
| 31 | PowerManagerShellCommand(IPowerManager service) { |
| 32 | mInterface = service; |
| 33 | } |
| 34 | |
| 35 | @Override |
| 36 | public int onCommand(String cmd) { |
| 37 | if (cmd == null) { |
| 38 | return handleDefaultCommands(cmd); |
| 39 | } |
| 40 | |
| 41 | final PrintWriter pw = getOutPrintWriter(); |
| 42 | try { |
| 43 | switch(cmd) { |
Kweku Adams | 9f488e2 | 2019-01-14 16:25:08 -0800 | [diff] [blame] | 44 | case "set-adaptive-power-saver-enabled": |
| 45 | return runSetAdaptiveEnabled(); |
Jocelyn Dang | f2c38c1 | 2017-03-31 14:03:37 -0700 | [diff] [blame] | 46 | case "set-mode": |
| 47 | return runSetMode(); |
| 48 | default: |
| 49 | return handleDefaultCommands(cmd); |
| 50 | } |
| 51 | } catch (RemoteException e) { |
| 52 | pw.println("Remote exception: " + e); |
| 53 | } |
| 54 | return -1; |
| 55 | } |
| 56 | |
Kweku Adams | 9f488e2 | 2019-01-14 16:25:08 -0800 | [diff] [blame] | 57 | private int runSetAdaptiveEnabled() throws RemoteException { |
| 58 | mInterface.setAdaptivePowerSaveEnabled(Boolean.parseBoolean(getNextArgRequired())); |
| 59 | return 0; |
| 60 | } |
| 61 | |
Jocelyn Dang | f2c38c1 | 2017-03-31 14:03:37 -0700 | [diff] [blame] | 62 | private int runSetMode() throws RemoteException { |
| 63 | final PrintWriter pw = getOutPrintWriter(); |
| 64 | int mode = -1; |
| 65 | try { |
| 66 | mode = Integer.parseInt(getNextArgRequired()); |
| 67 | } catch (RuntimeException ex) { |
| 68 | pw.println("Error: " + ex.toString()); |
| 69 | return -1; |
| 70 | } |
Salvador Martinez | c8c4c5d | 2019-03-11 11:11:37 -0700 | [diff] [blame^] | 71 | mInterface.setPowerSaveModeEnabled(mode == LOW_POWER_MODE_ON); |
Jocelyn Dang | f2c38c1 | 2017-03-31 14:03:37 -0700 | [diff] [blame] | 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public void onHelp() { |
| 77 | final PrintWriter pw = getOutPrintWriter(); |
| 78 | pw.println("Power manager (power) commands:"); |
| 79 | pw.println(" help"); |
| 80 | pw.println(" Print this help text."); |
| 81 | pw.println(""); |
Kweku Adams | 9f488e2 | 2019-01-14 16:25:08 -0800 | [diff] [blame] | 82 | pw.println(" set-adaptive-power-saver-enabled [true|false]"); |
| 83 | pw.println(" enables or disables adaptive power saver."); |
Jocelyn Dang | f2c38c1 | 2017-03-31 14:03:37 -0700 | [diff] [blame] | 84 | pw.println(" set-mode MODE"); |
| 85 | pw.println(" sets the power mode of the device to MODE."); |
| 86 | pw.println(" 1 turns low power mode on and 0 turns low power mode off."); |
| 87 | pw.println(); |
| 88 | Intent.printIntentArgsHelp(pw , ""); |
| 89 | } |
| 90 | } |