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                + "       svc wifi prefer\n"
41                + "          Set Wi-Fi as the preferred data network\n";
42    }
43
44    public void run(String[] args) {
45        boolean validCommand = false;
46        if (args.length >= 2) {
47            boolean flag = false;
48            if ("enable".equals(args[1])) {
49                flag = true;
50                validCommand = true;
51            } else if ("disable".equals(args[1])) {
52                flag = false;
53                validCommand = true;
54            } else if ("prefer".equals(args[1])) {
55                IConnectivityManager connMgr =
56                        IConnectivityManager.Stub.asInterface(ServiceManager.getService(Context.CONNECTIVITY_SERVICE));
57                try {
58                    connMgr.setNetworkPreference(ConnectivityManager.TYPE_WIFI);
59                } catch (RemoteException e) {
60                    System.err.println("Failed to set preferred network: " + e);
61                }
62                return;
63            }
64            if (validCommand) {
65                IWifiManager wifiMgr
66                        = IWifiManager.Stub.asInterface(ServiceManager.getService(Context.WIFI_SERVICE));
67                try {
68                    wifiMgr.setWifiEnabled(flag);
69                }
70                catch (RemoteException e) {
71                    System.err.println("Wi-Fi operation failed: " + e);
72                }
73                return;
74            }
75        }
76        System.err.println(longHelp());
77    }
78}