WifiCommand.java revision 7b81602f3c18df8a4ca0342c514af8f7e394c0d7
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.ServiceManager;
20import android.os.RemoteException;
21import android.net.wifi.IWifiManager;
22import android.net.IConnectivityManager;
23import android.net.ConnectivityManager;
24import android.content.Context;
25
26public class WifiCommand extends Svc.Command {
27    public WifiCommand() {
28        super("wifi");
29    }
30
31    public String shortHelp() {
32        return "Control the Wi-Fi manager";
33    }
34
35    public String longHelp() {
36        return shortHelp() + "\n"
37                + "\n"
38                + "usage: svc wifi [enable|disable]\n"
39                + "         Turn Wi-Fi on or off.\n\n";
40    }
41
42    public void run(String[] args) {
43        boolean validCommand = false;
44        if (args.length >= 2) {
45            boolean flag = false;
46            if ("enable".equals(args[1])) {
47                flag = true;
48                validCommand = true;
49            } else if ("disable".equals(args[1])) {
50                flag = false;
51                validCommand = true;
52            }
53            if (validCommand) {
54                IWifiManager wifiMgr
55                        = IWifiManager.Stub.asInterface(ServiceManager.getService(Context.WIFI_SERVICE));
56                try {
57                    wifiMgr.setWifiEnabled(flag);
58                }
59                catch (RemoteException e) {
60                    System.err.println("Wi-Fi operation failed: " + e);
61                }
62                return;
63            }
64        }
65        System.err.println(longHelp());
66    }
67}
68