PowerCommand.java revision 0e39ea83c5578e0d55e120c91ff7cfeeb0c1cb2f
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.os.Binder; 20import android.os.IBinder; 21import android.os.IPowerManager; 22import android.os.PowerManager; 23import android.os.ServiceManager; 24import android.os.RemoteException; 25import android.os.BatteryManager; 26import android.content.Context; 27 28public class PowerCommand extends Svc.Command { 29 public PowerCommand() { 30 super("power"); 31 } 32 33 public String shortHelp() { 34 return "Control the power manager"; 35 } 36 37 public String longHelp() { 38 return shortHelp() + "\n" 39 + "\n" 40 + "usage: svc power stayon [true|false|usb|ac]\n" 41 + " Set the 'keep awake while plugged in' setting.\n"; 42 } 43 44 public void run(String[] args) { 45 fail: { 46 if (args.length >= 2) { 47 if ("stayon".equals(args[1]) && args.length == 3) { 48 int val; 49 if ("true".equals(args[2])) { 50 val = BatteryManager.BATTERY_PLUGGED_AC | 51 BatteryManager.BATTERY_PLUGGED_USB; 52 } 53 else if ("false".equals(args[2])) { 54 val = 0; 55 } else if ("usb".equals(args[2])) { 56 val = BatteryManager.BATTERY_PLUGGED_USB; 57 } else if ("ac".equals(args[2])) { 58 val = BatteryManager.BATTERY_PLUGGED_AC; 59 } 60 else { 61 break fail; 62 } 63 IPowerManager pm 64 = IPowerManager.Stub.asInterface(ServiceManager.getService(Context.POWER_SERVICE)); 65 try { 66 IBinder lock = new Binder(); 67 pm.acquireWakeLock(PowerManager.FULL_WAKE_LOCK, lock, "svc power"); 68 pm.setStayOnSetting(val); 69 pm.releaseWakeLock(lock, 0); 70 } 71 catch (RemoteException e) { 72 System.err.println("Faild to set setting: " + e); 73 } 74 return; 75 } 76 } 77 } 78 System.err.println(longHelp()); 79 } 80} 81