DataCommand.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.IConnectivityManager;
22import android.net.ConnectivityManager;
23import android.content.Context;
24import com.android.internal.telephony.ITelephony;
25
26public class DataCommand extends Svc.Command {
27    public DataCommand() {
28        super("data");
29    }
30
31    public String shortHelp() {
32        return "Control mobile data connectivity";
33    }
34
35    public String longHelp() {
36        return shortHelp() + "\n"
37                + "\n"
38                + "usage: svc data [enable|disable]\n"
39                + "         Turn mobile data 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                ITelephony phoneMgr
55                        = ITelephony.Stub.asInterface(ServiceManager.getService(Context.TELEPHONY_SERVICE));
56                try {
57                    if (flag) {
58                        phoneMgr.enableDataConnectivity();
59                    } else
60                        phoneMgr.disableDataConnectivity();
61                }
62                catch (RemoteException e) {
63                    System.err.println("Mobile data operation failed: " + e);
64                }
65                return;
66            }
67        }
68        System.err.println(longHelp());
69    }
70}
71