PowerCommand.java revision 37a37f445cbcd64376af153dd79ea2a123d9405d
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|wireless]\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                                BatteryManager.BATTERY_PLUGGED_WIRELESS;
53                    }
54                    else if ("false".equals(args[2])) {
55                        val = 0;
56                    } else if ("usb".equals(args[2])) {
57                        val = BatteryManager.BATTERY_PLUGGED_USB;
58                    } else if ("ac".equals(args[2])) {
59                        val = BatteryManager.BATTERY_PLUGGED_AC;
60                    } else if ("wireless".equals(args[2])) {
61                        val = BatteryManager.BATTERY_PLUGGED_WIRELESS;
62                    } else {
63                        break fail;
64                    }
65                    IPowerManager pm
66                            = IPowerManager.Stub.asInterface(ServiceManager.getService(Context.POWER_SERVICE));
67                    try {
68                        IBinder lock = new Binder();
69                        pm.acquireWakeLock(lock, PowerManager.FULL_WAKE_LOCK, "svc power", null);
70                        pm.setStayOnSetting(val);
71                        pm.releaseWakeLock(lock, 0);
72                    }
73                    catch (RemoteException e) {
74                        System.err.println("Faild to set setting: " + e);
75                    }
76                    return;
77                }
78            }
79        }
80        System.err.println(longHelp());
81    }
82}
83