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