PowerCommand.java revision 783f9e6615db5c90c184397cf1ee271e302c398b
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    }
41
42    public void run(String[] args) {
43        fail: {
44            if (args.length >= 2) {
45                if ("stayon".equals(args[1]) && args.length == 3) {
46                    int val;
47                    if ("true".equals(args[2])) {
48                        val = BatteryManager.BATTERY_PLUGGED_AC |
49                                BatteryManager.BATTERY_PLUGGED_USB |
50                                BatteryManager.BATTERY_PLUGGED_WIRELESS;
51                    }
52                    else if ("false".equals(args[2])) {
53                        val = 0;
54                    } else if ("usb".equals(args[2])) {
55                        val = BatteryManager.BATTERY_PLUGGED_USB;
56                    } else if ("ac".equals(args[2])) {
57                        val = BatteryManager.BATTERY_PLUGGED_AC;
58                    } else if ("wireless".equals(args[2])) {
59                        val = BatteryManager.BATTERY_PLUGGED_WIRELESS;
60                    } else {
61                        break fail;
62                    }
63                    IPowerManager pm
64                            = IPowerManager.Stub.asInterface(ServiceManager.getService(Context.POWER_SERVICE));
65                    try {
66                        if (val != 0) {
67                            // if the request is not to set it to false, wake up the screen so that
68                            // it can stay on as requested
69                            pm.wakeUp(SystemClock.uptimeMillis());
70                        }
71                        pm.setStayOnSetting(val);
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