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 + " svc data prefer\n" 41 + " Set mobile 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_MOBILE); 59 } catch (RemoteException e) { 60 System.err.println("Failed to set preferred network: " + e); 61 } 62 return; 63 } 64 if (validCommand) { 65 ITelephony phoneMgr 66 = ITelephony.Stub.asInterface(ServiceManager.getService(Context.TELEPHONY_SERVICE)); 67 try { 68 if (flag) { 69 phoneMgr.enableDataConnectivity(); 70 } else 71 phoneMgr.disableDataConnectivity(); 72 } 73 catch (RemoteException e) { 74 System.err.println("Mobile data operation failed: " + e); 75 } 76 return; 77 } 78 } 79 System.err.println(longHelp()); 80 } 81}