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