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