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