Home | History | Annotate | Download | only in development
      1 /* //device/apps/Settings/src/com/android/settings/Keyguard.java
      2 **
      3 ** Copyright 2006, The Android Open Source Project
      4 **
      5 ** Licensed under the Apache License, Version 2.0 (the "License");
      6 ** you may not use this file except in compliance with the License.
      7 ** You may obtain a copy of the License at
      8 **
      9 **     http://www.apache.org/licenses/LICENSE-2.0
     10 **
     11 ** Unless required by applicable law or agreed to in writing, software
     12 ** distributed under the License is distributed on an "AS IS" BASIS,
     13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14 ** See the License for the specific language governing permissions and
     15 ** limitations under the License.
     16 */
     17 
     18 package com.android.development;
     19 
     20 import android.app.Activity;
     21 import android.app.ActivityManagerNative;
     22 import android.app.AlarmManager;
     23 import android.app.PendingIntent;
     24 import android.content.BroadcastReceiver;
     25 import android.content.Context;
     26 import android.content.Intent;
     27 import android.content.IntentFilter;
     28 import android.content.SharedPreferences;
     29 import android.content.pm.PackageManager.NameNotFoundException;
     30 import android.net.ConnectivityManager;
     31 import android.net.NetworkUtils;
     32 import android.net.RouteInfo;
     33 import android.net.wifi.ScanResult;
     34 import android.net.wifi.WifiManager;
     35 import android.os.RemoteException;
     36 import android.os.Handler;
     37 import android.os.Message;
     38 import android.os.IBinder;
     39 import android.os.INetworkManagementService;
     40 import android.os.Parcel;
     41 import android.os.PowerManager;
     42 import android.os.PowerManager.WakeLock;
     43 import android.os.ServiceManager;
     44 import android.os.ServiceManagerNative;
     45 import android.os.SystemClock;
     46 import android.provider.Settings;
     47 import android.os.Bundle;
     48 import android.util.Log;
     49 import android.view.IWindowManager;
     50 import android.view.View;
     51 import android.widget.ArrayAdapter;
     52 import android.widget.Button;
     53 import android.widget.CheckBox;
     54 import android.widget.CompoundButton;
     55 import android.widget.EditText;
     56 import android.widget.Spinner;
     57 import android.widget.TextView;
     58 import android.widget.Toast;
     59 import android.widget.AdapterView.OnItemSelectedListener;
     60 
     61 import com.android.internal.telephony.Phone;
     62 
     63 import java.io.FileInputStream;
     64 import java.io.FileOutputStream;
     65 import java.io.PrintWriter;
     66 import java.net.InetAddress;
     67 import java.net.NetworkInterface;
     68 import java.net.Socket;
     69 import java.util.ArrayList;
     70 import java.util.Enumeration;
     71 import java.util.List;
     72 import java.util.Map;
     73 
     74 import org.apache.http.client.HttpClient;
     75 import org.apache.http.client.methods.HttpGet;
     76 import org.apache.http.conn.params.ConnRouteParams;
     77 import org.apache.http.params.BasicHttpParams;
     78 import org.apache.http.params.HttpParams;
     79 import org.apache.http.HttpResponse;
     80 import org.apache.http.impl.client.DefaultHttpClient;
     81 
     82 public class Connectivity extends Activity {
     83     private static final String TAG = "DevTools - Connectivity";
     84     private static final String GET_SCAN_RES = "Get Results";
     85     private static final String START_SCAN = "Start Scan";
     86     private static final String PROGRESS_SCAN = "In Progress";
     87 
     88     private static final long SCAN_CYCLES = 15;
     89 
     90     private static final int EVENT_TOGGLE_WIFI = 1;
     91     private static final int EVENT_TOGGLE_SCREEN = 2;
     92 
     93     private EditText mDCOnDurationEdit;
     94     private EditText mDCOffDurationEdit;
     95     private TextView mDCCycleCountView;
     96     private long mDCOnDuration = 120000;
     97     private long mDCOffDuration = 120000;
     98     private int mDCCycleCount = 0;
     99 
    100     private EditText mSCOnDurationEdit;
    101     private EditText mSCOffDurationEdit;
    102     private TextView mSCCycleCountView;
    103     private long mSCOnDuration = 120000;
    104     private long mSCOffDuration = 12000;
    105     private int mSCCycleCount = 0;
    106 
    107     private boolean mDelayedCycleStarted = false;
    108 
    109     private Button mScanButton;
    110     private TextView mScanResults;
    111     private EditText mScanCyclesEdit;
    112     private CheckBox mScanDisconnect;
    113     private long mScanCycles = SCAN_CYCLES;
    114     private long mScanCur = -1;
    115     private long mStartTime = -1;
    116     private long mStopTime;
    117     private long mTotalScanTime = 0;
    118     private long mTotalScanCount = 0;
    119 
    120     private String mTdlsAddr = null;
    121 
    122     private WifiManager mWm;
    123     private PowerManager mPm;
    124     private ConnectivityManager mCm;
    125     private INetworkManagementService mNetd;
    126 
    127     private WifiScanReceiver mScanRecv;
    128     IntentFilter mIntentFilter;
    129 
    130     private WakeLock mWakeLock = null;
    131     private WakeLock mScreenonWakeLock = null;
    132 
    133     private boolean mScreenOffToggleRunning = false;
    134     private boolean mScreenOff = false;
    135 
    136     private static final String CONNECTIVITY_TEST_ALARM =
    137             "com.android.development.CONNECTIVITY_TEST_ALARM";
    138     private static final String TEST_ALARM_EXTRA = "CONNECTIVITY_TEST_EXTRA";
    139     private static final String TEST_ALARM_ON_EXTRA = "CONNECTIVITY_TEST_ON_EXTRA";
    140     private static final String TEST_ALARM_OFF_EXTRA = "CONNECTIVITY_TEST_OFF_EXTRA";
    141     private static final String TEST_ALARM_CYCLE_EXTRA = "CONNECTIVITY_TEST_CYCLE_EXTRA";
    142     private static final String SCREEN_ON = "SCREEN_ON";
    143     private static final String SCREEN_OFF = "SCREEN_OFF";
    144     public BroadcastReceiver mReceiver = new BroadcastReceiver() {
    145         public void onReceive(Context context, Intent intent) {
    146             if (intent.getAction().equals(CONNECTIVITY_TEST_ALARM)) {
    147                 String extra = (String)intent.getExtra(TEST_ALARM_EXTRA);
    148                 PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
    149                 Long on = new Long(120000);
    150                 Long off = new Long(120000);
    151                 int cycle = 0;
    152                 try {
    153                     on = Long.parseLong((String)intent.getExtra(TEST_ALARM_ON_EXTRA));
    154                     off = Long.parseLong((String)intent.getExtra(TEST_ALARM_OFF_EXTRA));
    155                     cycle = Integer.parseInt((String)intent.getExtra(TEST_ALARM_CYCLE_EXTRA));
    156                 } catch (Exception e) {}
    157 
    158                 if (extra.equals(SCREEN_ON)) {
    159                     mScreenonWakeLock = mPm.newWakeLock(PowerManager.FULL_WAKE_LOCK |
    160                             PowerManager.ACQUIRE_CAUSES_WAKEUP,
    161                             "ConnectivityTest");
    162                     mScreenonWakeLock.acquire();
    163 
    164                     mSCCycleCount = cycle+1;
    165                     mSCOnDuration = on;
    166                     mSCOffDuration = off;
    167                     mSCCycleCountView.setText(Integer.toString(mSCCycleCount));
    168 
    169                     scheduleAlarm(mSCOnDuration, SCREEN_OFF);
    170                 } else if (extra.equals(SCREEN_OFF)) {
    171 
    172                     mSCCycleCount = cycle;
    173                     mSCOnDuration = on;
    174                     mSCOffDuration = off;
    175 
    176                     mScreenonWakeLock.release();
    177                     mScreenonWakeLock = null;
    178                     scheduleAlarm(mSCOffDuration, SCREEN_ON);
    179                     pm.goToSleep(SystemClock.uptimeMillis());
    180                 }
    181             }
    182         }
    183     };
    184 
    185     public Handler mHandler2 = new Handler() {
    186         public void handleMessage(Message msg) {
    187             switch(msg.what) {
    188                 case EVENT_TOGGLE_WIFI:
    189                     Log.e(TAG, "EVENT_TOGGLE_WIFI");
    190                     if (mDelayedCycleStarted && mWm != null) {
    191                         long delay;
    192                         switch (mWm.getWifiState()) {
    193                             case WifiManager.WIFI_STATE_ENABLED:
    194                             case WifiManager.WIFI_STATE_ENABLING:
    195                                 mWm.setWifiEnabled(false);
    196                                 delay = mDCOffDuration;
    197                                 break;
    198                             default:
    199                                 mWm.setWifiEnabled(true);
    200                                 delay = mDCOnDuration;
    201                                 mDCCycleCount++;
    202                                 mDCCycleCountView.setText(Integer.toString(mDCCycleCount));
    203                         }
    204                         sendMessageDelayed(obtainMessage(EVENT_TOGGLE_WIFI),
    205                                 delay);
    206                     }
    207                     break;
    208             }
    209         }
    210     };
    211 
    212    /**
    213      * Wifi Scan Listener
    214      */
    215     private class WifiScanReceiver extends BroadcastReceiver {
    216         @Override
    217         public void onReceive(Context context, Intent intent) {
    218             String action = intent.getAction();
    219 
    220             if (action.equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
    221                 mStopTime = SystemClock.elapsedRealtime();
    222                 if (mStartTime != -1) {
    223                     mTotalScanTime += (mStopTime - mStartTime);
    224                     mStartTime = -1;
    225                 }
    226                 Log.d(TAG, "Scan: READY " + mScanCur);
    227 
    228                 List<ScanResult> wifiScanResults = mWm.getScanResults();
    229                 if (wifiScanResults != null) {
    230                     mTotalScanCount += wifiScanResults.size();
    231                     Log.d(TAG, "Scan: Results = " + wifiScanResults.size());
    232                 }
    233 
    234                 mScanCur--;
    235                 mScanCyclesEdit.setText(Long.toString(mScanCur));
    236                 if (mScanCur == 0) {
    237                     unregisterReceiver(mScanRecv);
    238                     mScanButton.setText(GET_SCAN_RES);
    239                 } else {
    240                     Log.d(TAG, "Scan: START " + mScanCur);
    241                     mStartTime = SystemClock.elapsedRealtime();
    242                     mWm.startScan();
    243                 }
    244             }
    245         }
    246     }
    247 
    248 
    249     @Override
    250     public void onCreate(Bundle icicle) {
    251         super.onCreate(icicle);
    252 
    253         setContentView(R.layout.connectivity);
    254 
    255         mWm = (WifiManager)getSystemService(Context.WIFI_SERVICE);
    256         mPm = (PowerManager)getSystemService(Context.POWER_SERVICE);
    257         mCm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    258         IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
    259         mNetd = INetworkManagementService.Stub.asInterface(b);
    260 
    261         findViewById(R.id.enableWifi).setOnClickListener(mClickListener);
    262         findViewById(R.id.disableWifi).setOnClickListener(mClickListener);
    263 
    264         findViewById(R.id.startDelayedCycle).setOnClickListener(mClickListener);
    265         findViewById(R.id.stopDelayedCycle).setOnClickListener(mClickListener);
    266         mDCOnDurationEdit = (EditText)findViewById(R.id.dc_wifi_on_duration);
    267         mDCOnDurationEdit.setText(Long.toString(mDCOnDuration));
    268         mDCOffDurationEdit = (EditText)findViewById(R.id.dc_wifi_off_duration);
    269         mDCOffDurationEdit.setText(Long.toString(mDCOffDuration));
    270         mDCCycleCountView = (TextView)findViewById(R.id.dc_wifi_cycles_done);
    271         mDCCycleCountView.setText(Integer.toString(mDCCycleCount));
    272 
    273         findViewById(R.id.startScreenCycle).setOnClickListener(mClickListener);
    274         findViewById(R.id.stopScreenCycle).setOnClickListener(mClickListener);
    275         mSCOnDurationEdit = (EditText)findViewById(R.id.sc_wifi_on_duration);
    276         mSCOnDurationEdit.setText(Long.toString(mSCOnDuration));
    277         mSCOffDurationEdit = (EditText)findViewById(R.id.sc_wifi_off_duration);
    278         mSCOffDurationEdit.setText(Long.toString(mSCOffDuration));
    279         mSCCycleCountView = (TextView)findViewById(R.id.sc_wifi_cycles_done);
    280         mSCCycleCountView.setText(Integer.toString(mSCCycleCount));
    281 
    282         mScanButton = (Button)findViewById(R.id.startScan);
    283         mScanButton.setOnClickListener(mClickListener);
    284         mScanCyclesEdit = (EditText)findViewById(R.id.sc_scan_cycles);
    285         mScanCyclesEdit.setText(Long.toString(mScanCycles));
    286         mScanDisconnect = (CheckBox)findViewById(R.id.scanDisconnect);
    287         mScanDisconnect.setChecked(true);
    288         mScanResults = (TextView)findViewById(R.id.sc_scan_results);
    289         mScanResults.setVisibility(View.INVISIBLE);
    290 
    291         mScanRecv = new WifiScanReceiver();
    292         mIntentFilter = new IntentFilter();
    293         mIntentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
    294 
    295         findViewById(R.id.startTdls).setOnClickListener(mClickListener);
    296         findViewById(R.id.stopTdls).setOnClickListener(mClickListener);
    297 
    298         findViewById(R.id.start_mms).setOnClickListener(mClickListener);
    299         findViewById(R.id.stop_mms).setOnClickListener(mClickListener);
    300         findViewById(R.id.start_hipri).setOnClickListener(mClickListener);
    301         findViewById(R.id.stop_hipri).setOnClickListener(mClickListener);
    302         findViewById(R.id.crash).setOnClickListener(mClickListener);
    303 
    304         findViewById(R.id.add_default_route).setOnClickListener(mClickListener);
    305         findViewById(R.id.remove_default_route).setOnClickListener(mClickListener);
    306         findViewById(R.id.bound_http_request).setOnClickListener(mClickListener);
    307         findViewById(R.id.bound_socket_request).setOnClickListener(mClickListener);
    308         findViewById(R.id.routed_http_request).setOnClickListener(mClickListener);
    309         findViewById(R.id.routed_socket_request).setOnClickListener(mClickListener);
    310         findViewById(R.id.default_request).setOnClickListener(mClickListener);
    311         findViewById(R.id.default_socket).setOnClickListener(mClickListener);
    312 
    313         registerReceiver(mReceiver, new IntentFilter(CONNECTIVITY_TEST_ALARM));
    314     }
    315 
    316 
    317     @Override
    318     public void onResume() {
    319         super.onResume();
    320         findViewById(R.id.connectivity_layout).requestFocus();
    321     }
    322 
    323     private View.OnClickListener mClickListener = new View.OnClickListener() {
    324         public void onClick(View v) {
    325             switch (v.getId()) {
    326                 case R.id.enableWifi:
    327                     mWm.setWifiEnabled(true);
    328                     break;
    329                 case R.id.disableWifi:
    330                     mWm.setWifiEnabled(false);
    331                     break;
    332                 case R.id.startDelayedCycle:
    333                     onStartDelayedCycle();
    334                     break;
    335                 case R.id.stopDelayedCycle:
    336                     onStopDelayedCycle();
    337                     break;
    338                 case R.id.startScreenCycle:
    339                     onStartScreenCycle();
    340                     break;
    341                 case R.id.stopScreenCycle:
    342                     onStopScreenCycle();
    343                     break;
    344                 case R.id.startScan:
    345                     onStartScanCycle();
    346                     break;
    347                 case R.id.startTdls:
    348                     onStartTdls();
    349                     break;
    350                 case R.id.stopTdls:
    351                     onStopTdls();
    352                     break;
    353                 case R.id.start_mms:
    354                     mCm.startUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE,
    355                             Phone.FEATURE_ENABLE_MMS);
    356                     break;
    357                 case R.id.stop_mms:
    358                     mCm.stopUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE,
    359                             Phone.FEATURE_ENABLE_MMS);
    360                     break;
    361                 case R.id.default_socket:
    362                     onDefaultSocket();
    363                     break;
    364                 case R.id.default_request:
    365                     onDefaultRequest();
    366                     break;
    367                 case R.id.routed_socket_request:
    368                     onRoutedSocketRequest();
    369                     break;
    370                 case R.id.routed_http_request:
    371                     onRoutedHttpRequest();
    372                     break;
    373                 case R.id.bound_socket_request:
    374                     onBoundSocketRequest();
    375                     break;
    376                 case R.id.bound_http_request:
    377                     onBoundHttpRequest();
    378                     break;
    379                 case R.id.remove_default_route:
    380                     onRemoveDefaultRoute();
    381                     break;
    382                 case R.id.add_default_route:
    383                     onAddDefaultRoute();
    384                     break;
    385                 case R.id.crash:
    386                     onCrash();
    387                     break;
    388                 case R.id.start_hipri:
    389                     mCm.startUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE,
    390                             Phone.FEATURE_ENABLE_HIPRI);
    391                     break;
    392                 case R.id.stop_hipri:
    393                     mCm.stopUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE,
    394                             Phone.FEATURE_ENABLE_HIPRI);
    395                     break;
    396             }
    397         }
    398     };
    399 
    400 
    401     private void onStartDelayedCycle() {
    402         if (!mDelayedCycleStarted) {
    403             mDelayedCycleStarted = true;
    404             try {
    405                 mDCOnDuration = Long.parseLong(mDCOnDurationEdit.getText().toString());
    406                 mDCOffDuration = Long.parseLong(mDCOffDurationEdit.getText().toString());
    407             } catch (Exception e) { };
    408             mDCCycleCount = 0;
    409 
    410             mWakeLock = mPm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "ConnectivityTest");
    411             mWakeLock.acquire();
    412             mHandler2.sendMessage(mHandler2.obtainMessage(EVENT_TOGGLE_WIFI));
    413         }
    414     }
    415 
    416     private void onStopDelayedCycle() {
    417         if (mDelayedCycleStarted) {
    418             mDelayedCycleStarted = false;
    419             mWakeLock.release();
    420             mWakeLock = null;
    421             if(mHandler2.hasMessages(EVENT_TOGGLE_WIFI)) {
    422                 mHandler2.removeMessages(EVENT_TOGGLE_WIFI);
    423             }
    424         }
    425     }
    426 
    427     private void onStartScreenCycle() {
    428         try {
    429             mSCOnDuration = Long.parseLong(mSCOnDurationEdit.getText().toString());
    430             mSCOffDuration = Long.parseLong(mSCOffDurationEdit.getText().toString());
    431         } catch (Exception e) { };
    432         mSCCycleCount = 0;
    433 
    434         mScreenonWakeLock = mPm.newWakeLock(PowerManager.FULL_WAKE_LOCK,
    435                 "ConnectivityTest");
    436         mScreenonWakeLock.acquire();
    437 
    438         scheduleAlarm(10, SCREEN_OFF);
    439     }
    440 
    441     private void scheduleAlarm(long delayMs, String eventType) {
    442         AlarmManager am = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    443         Intent i = new Intent(CONNECTIVITY_TEST_ALARM);
    444 
    445         i.putExtra(TEST_ALARM_EXTRA, eventType);
    446         i.putExtra(TEST_ALARM_ON_EXTRA, Long.toString(mSCOnDuration));
    447         i.putExtra(TEST_ALARM_OFF_EXTRA, Long.toString(mSCOffDuration));
    448         i.putExtra(TEST_ALARM_CYCLE_EXTRA, Integer.toString(mSCCycleCount));
    449 
    450         PendingIntent p = PendingIntent.getBroadcast(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
    451 
    452         am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + delayMs, p);
    453     }
    454 
    455     private void onStopScreenCycle() {
    456     }
    457 
    458     private void onCrash() {
    459         ConnectivityManager foo = null;
    460         foo.startUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE,
    461                 Phone.FEATURE_ENABLE_MMS);
    462     }
    463 
    464     private void onStartScanCycle() {
    465         if (mScanCur == -1) {
    466             try {
    467                 mScanCur = Long.parseLong(mScanCyclesEdit.getText().toString());
    468                 mScanCycles = mScanCur;
    469             } catch (Exception e) { };
    470             if (mScanCur <= 0) {
    471                 mScanCur = -1;
    472                 mScanCycles = SCAN_CYCLES;
    473                 return;
    474             }
    475         }
    476         if (mScanCur > 0) {
    477             registerReceiver(mScanRecv, mIntentFilter);
    478             mScanButton.setText(PROGRESS_SCAN);
    479             mScanResults.setVisibility(View.INVISIBLE);
    480             if (mScanDisconnect.isChecked())
    481                 mWm.disconnect();
    482             mTotalScanTime = 0;
    483             mTotalScanCount = 0;
    484             Log.d(TAG, "Scan: START " + mScanCur);
    485             mStartTime = SystemClock.elapsedRealtime();
    486             mWm.startScan();
    487         } else {
    488             // Show results
    489             mScanResults.setText("Average Scan Time = " +
    490                 Long.toString(mTotalScanTime / mScanCycles) + " ms ; Average Scan Amount = " +
    491                 Long.toString(mTotalScanCount / mScanCycles));
    492             mScanResults.setVisibility(View.VISIBLE);
    493             mScanButton.setText(START_SCAN);
    494             mScanCur = -1;
    495             mScanCyclesEdit.setText(Long.toString(mScanCycles));
    496             if (mScanDisconnect.isChecked())
    497                 mWm.reassociate();
    498         }
    499     }
    500 
    501     private void onStartTdls() {
    502         mTdlsAddr = ((EditText)findViewById(R.id.sc_ip_mac)).getText().toString();
    503         Log.d(TAG, "TDLS: START " + mTdlsAddr);
    504         InetAddress inetAddress = null;
    505         try {
    506             inetAddress = InetAddress.getByName(mTdlsAddr);
    507             mWm.setTdlsEnabled(inetAddress, true);
    508         } catch (Exception e) {
    509             mWm.setTdlsEnabledWithMacAddress(mTdlsAddr, true);
    510         }
    511     }
    512 
    513     private void onStopTdls() {
    514         if (mTdlsAddr == null) return;
    515         Log.d(TAG, "TDLS: STOP " + mTdlsAddr);
    516         InetAddress inetAddress = null;
    517         try {
    518             inetAddress = InetAddress.getByName(mTdlsAddr);
    519             mWm.setTdlsEnabled(inetAddress, false);
    520         } catch (Exception e) {
    521             mWm.setTdlsEnabledWithMacAddress(mTdlsAddr, false);
    522         }
    523     }
    524 
    525     private void onAddDefaultRoute() {
    526         try {
    527             mNetd.addRoute("eth0", new RouteInfo(null,
    528                     NetworkUtils.numericToInetAddress("8.8.8.8")));
    529         } catch (Exception e) {
    530             Log.e(TAG, "onAddDefaultRoute got exception: " + e.toString());
    531         }
    532     }
    533 
    534     private void onRemoveDefaultRoute() {
    535         try {
    536             mNetd.removeRoute("eth0", new RouteInfo(null,
    537                     NetworkUtils.numericToInetAddress("8.8.8.8")));
    538         } catch (Exception e) {
    539             Log.e(TAG, "onRemoveDefaultRoute got exception: " + e.toString());
    540         }
    541     }
    542 
    543     private void onRoutedHttpRequest() {
    544         onRoutedRequest(HTTP);
    545     }
    546 
    547     private void onRoutedSocketRequest() {
    548         onRoutedRequest(SOCKET);
    549     }
    550 
    551     private final static int SOCKET = 1;
    552     private final static int HTTP   = 2;
    553 
    554     private void onRoutedRequest(int type) {
    555         String url = "www.google.com";
    556 
    557         InetAddress inetAddress = null;
    558         try {
    559             inetAddress = InetAddress.getByName(url);
    560         } catch (Exception e) {
    561             Log.e(TAG, "error fetching address for " + url);
    562             return;
    563         }
    564 
    565         mCm.requestRouteToHostAddress(ConnectivityManager.TYPE_MOBILE_HIPRI, inetAddress);
    566 
    567         switch (type) {
    568             case SOCKET:
    569                 onBoundSocketRequest();
    570                 break;
    571             case HTTP:
    572                 HttpGet get = new HttpGet("http://" + url);
    573                 HttpClient client = new DefaultHttpClient();
    574                 try {
    575                     HttpResponse httpResponse = client.execute(get);
    576                     Log.d(TAG, "routed http request gives " + httpResponse.getStatusLine());
    577                 } catch (Exception e) {
    578                     Log.e(TAG, "routed http request exception = " + e);
    579                 }
    580         }
    581 
    582     }
    583 
    584     private void onBoundHttpRequest() {
    585         NetworkInterface networkInterface = null;
    586         try {
    587             networkInterface = NetworkInterface.getByName("rmnet0");
    588             Log.d(TAG, "networkInterface is " + networkInterface);
    589         } catch (Exception e) {
    590             Log.e(TAG, " exception getByName: " + e);
    591             return;
    592         }
    593         if (networkInterface != null) {
    594             Enumeration inetAddressess = networkInterface.getInetAddresses();
    595             while(inetAddressess.hasMoreElements()) {
    596                 Log.d(TAG, " inetAddress:" + ((InetAddress)inetAddressess.nextElement()));
    597             }
    598         }
    599 
    600         HttpParams httpParams = new BasicHttpParams();
    601         if (networkInterface != null) {
    602             ConnRouteParams.setLocalAddress(httpParams,
    603                     networkInterface.getInetAddresses().nextElement());
    604         }
    605         HttpGet get = new HttpGet("http://www.bbc.com");
    606         HttpClient client = new DefaultHttpClient(httpParams);
    607         try {
    608             HttpResponse response = client.execute(get);
    609             Log.d(TAG, "response code = " + response.getStatusLine());
    610         } catch (Exception e) {
    611             Log.e(TAG, "Exception = "+ e );
    612         }
    613     }
    614 
    615     private void onBoundSocketRequest() {
    616         NetworkInterface networkInterface = null;
    617         try {
    618             networkInterface = NetworkInterface.getByName("rmnet0");
    619         } catch (Exception e) {
    620             Log.e(TAG, "exception getByName: " + e);
    621             return;
    622         }
    623         if (networkInterface == null) {
    624             try {
    625                 Log.d(TAG, "getting any networkInterface");
    626                 networkInterface = NetworkInterface.getNetworkInterfaces().nextElement();
    627             } catch (Exception e) {
    628                 Log.e(TAG, "exception getting any networkInterface: " + e);
    629                 return;
    630             }
    631         }
    632         if (networkInterface == null) {
    633             Log.e(TAG, "couldn't find a local interface");
    634             return;
    635         }
    636         Enumeration inetAddressess = networkInterface.getInetAddresses();
    637         while(inetAddressess.hasMoreElements()) {
    638             Log.d(TAG, " addr:" + ((InetAddress)inetAddressess.nextElement()));
    639         }
    640         InetAddress local = null;
    641         InetAddress remote = null;
    642         try {
    643             local = networkInterface.getInetAddresses().nextElement();
    644         } catch (Exception e) {
    645             Log.e(TAG, "exception getting local InetAddress: " + e);
    646             return;
    647         }
    648         try {
    649             remote = InetAddress.getByName("www.flickr.com");
    650         } catch (Exception e) {
    651             Log.e(TAG, "exception getting remote InetAddress: " + e);
    652             return;
    653         }
    654         Log.d(TAG, "remote addr ="+remote);
    655         Log.d(TAG, "local addr ="+local);
    656         Socket socket = null;
    657         try {
    658             socket = new Socket(remote, 80, local, 6000);
    659         } catch (Exception e) {
    660             Log.e(TAG, "Exception creating socket: " + e);
    661             return;
    662         }
    663         try {
    664             PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
    665             out.println("Hi flickr");
    666         } catch (Exception e) {
    667             Log.e(TAG, "Exception writing to socket: " + e);
    668             return;
    669         }
    670     }
    671 
    672     private void onDefaultRequest() {
    673         HttpParams params = new BasicHttpParams();
    674         HttpGet get = new HttpGet("http://www.cnn.com");
    675         HttpClient client = new DefaultHttpClient(params);
    676         try {
    677             HttpResponse response = client.execute(get);
    678             Log.e(TAG, "response code = " + response.getStatusLine());
    679         } catch (Exception e) {
    680             Log.e(TAG, "Exception = " + e);
    681         }
    682     }
    683 
    684     private void onDefaultSocket() {
    685         InetAddress remote = null;
    686         try {
    687             remote = InetAddress.getByName("www.flickr.com");
    688         } catch (Exception e) {
    689             Log.e(TAG, "exception getting remote InetAddress: " + e);
    690             return;
    691         }
    692         Log.e(TAG, "remote addr =" + remote);
    693         Socket socket = null;
    694         try {
    695             socket = new Socket(remote, 80);
    696         } catch (Exception e) {
    697             Log.e(TAG, "Exception creating socket: " + e);
    698             return;
    699         }
    700         try {
    701             PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
    702             out.println("Hi flickr");
    703             Log.e(TAG, "written");
    704         } catch (Exception e) {
    705             Log.e(TAG, "Exception writing to socket: " + e);
    706             return;
    707         }
    708     }
    709 }
    710