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) { |
| 44 | case "set-mode": |
| 45 | return runSetMode(); |
| 46 | default: |
| 47 | return handleDefaultCommands(cmd); |
| 48 | } |
| 49 | } catch (RemoteException e) { |
| 50 | pw.println("Remote exception: " + e); |
| 51 | } |
| 52 | return -1; |
| 53 | } |
| 54 | |
| 55 | private int runSetMode() throws RemoteException { |
| 56 | final PrintWriter pw = getOutPrintWriter(); |
| 57 | int mode = -1; |
| 58 | try { |
| 59 | mode = Integer.parseInt(getNextArgRequired()); |
| 60 | } catch (RuntimeException ex) { |
| 61 | pw.println("Error: " + ex.toString()); |
| 62 | return -1; |
| 63 | } |
| 64 | mInterface.setPowerSaveMode(mode == LOW_POWER_MODE_ON); |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public void onHelp() { |
| 70 | final PrintWriter pw = getOutPrintWriter(); |
| 71 | pw.println("Power manager (power) commands:"); |
| 72 | pw.println(" help"); |
| 73 | pw.println(" Print this help text."); |
| 74 | pw.println(""); |
| 75 | pw.println(" set-mode MODE"); |
| 76 | pw.println(" sets the power mode of the device to MODE."); |
| 77 | pw.println(" 1 turns low power mode on and 0 turns low power mode off."); |
| 78 | pw.println(); |
| 79 | Intent.printIntentArgsHelp(pw , ""); |
| 80 | } |
| 81 | } |