1 /* 2 * Copyright (C) 2008 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.commands.svc; 18 19 import android.content.Context; 20 import android.os.BatteryManager; 21 import android.os.IPowerManager; 22 import android.os.RemoteException; 23 import android.os.ServiceManager; 24 import android.os.SystemClock; 25 import android.os.SystemProperties; 26 27 public class PowerCommand extends Svc.Command { 28 public PowerCommand() { 29 super("power"); 30 } 31 32 public String shortHelp() { 33 return "Control the power manager"; 34 } 35 36 public String longHelp() { 37 return shortHelp() + "\n" 38 + "\n" 39 + "usage: svc power stayon [true|false|usb|ac|wireless]\n" 40 + " Set the 'keep awake while plugged in' setting.\n" 41 + " svc power reboot [reason]\n" 42 + " Perform a runtime shutdown and reboot device with specified reason.\n" 43 + " svc power shutdown\n" 44 + " Perform a runtime shutdown and power off the device.\n"; 45 } 46 47 public void run(String[] args) { 48 fail: { 49 if (args.length >= 2) { 50 IPowerManager pm = IPowerManager.Stub.asInterface( 51 ServiceManager.getService(Context.POWER_SERVICE)); 52 if ("stayon".equals(args[1]) && args.length == 3) { 53 int val; 54 if ("true".equals(args[2])) { 55 val = BatteryManager.BATTERY_PLUGGED_AC | 56 BatteryManager.BATTERY_PLUGGED_USB | 57 BatteryManager.BATTERY_PLUGGED_WIRELESS; 58 } 59 else if ("false".equals(args[2])) { 60 val = 0; 61 } else if ("usb".equals(args[2])) { 62 val = BatteryManager.BATTERY_PLUGGED_USB; 63 } else if ("ac".equals(args[2])) { 64 val = BatteryManager.BATTERY_PLUGGED_AC; 65 } else if ("wireless".equals(args[2])) { 66 val = BatteryManager.BATTERY_PLUGGED_WIRELESS; 67 } else { 68 break fail; 69 } 70 try { 71 if (val != 0) { 72 // if the request is not to set it to false, wake up the screen so that 73 // it can stay on as requested 74 pm.wakeUp(SystemClock.uptimeMillis(), "PowerCommand", null); 75 } 76 pm.setStayOnSetting(val); 77 } 78 catch (RemoteException e) { 79 System.err.println("Faild to set setting: " + e); 80 } 81 return; 82 } else if ("reboot".equals(args[1])) { 83 String mode = null; 84 if (args.length == 3) { 85 mode = args[2]; 86 } 87 try { 88 // no confirm, wait till device is rebooted 89 pm.reboot(false, mode, true); 90 } catch (RemoteException e) { 91 maybeLogRemoteException("Failed to reboot."); 92 } 93 return; 94 } else if ("shutdown".equals(args[1])) { 95 try { 96 // no confirm, wait till device is off 97 pm.shutdown(false, null, true); 98 } catch (RemoteException e) { 99 maybeLogRemoteException("Failed to shutdown."); 100 } 101 return; 102 } 103 } 104 } 105 System.err.println(longHelp()); 106 } 107 108 // Check if remote exception is benign during shutdown. Pm can be killed 109 // before system server during shutdown, so remote exception can be ignored 110 // if it is already in shutdown flow. 111 private void maybeLogRemoteException(String msg) { 112 String powerProp = SystemProperties.get("sys.powerctl"); 113 if (powerProp.isEmpty()) { 114 System.err.println(msg); 115 } 116 } 117 } 118