huangcm
2025-04-09 02d4ce54b909bd733f12e9f3fa4c1b03cf2d6f45
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
package com.DeviceTest;
 
import static android.view.WindowManager.LayoutParams.FLAG_FULLSCREEN;
import static android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
 
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
 
import org.apache.http.HttpClientConnection;
import org.apache.http.HttpConnection;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
 
import com.DeviceTest.helper.ControlButtonUtil;
import com.DeviceTest.helper.SystemUtil;
 
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.Parcelable;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
//import android.webkit.WebView;
import android.widget.ProgressBar;
import android.widget.RemoteViews;
import android.widget.TextView;
 
public class WifiTestActivity extends Activity {
 
   private static final String TAG = WifiTestActivity.class.getSimpleName();
   private static final String HTTP_TEST_URL = "http://172.16.9.1/";
   private static final String PING_TEST_ADDR = "172.16.9.1";
   private static final int TEST_FAILED_DELAY = 5000;
   private static final int MSG_ERROR = 1;
   private static final int MSG_HTTP_TEST = 2;
   private static final int MSG_SCAN = 3;
   private static final int MSG_HTTP_TEST_PASS = 4;
   private static final int MSG_HTTP_TEST_FAILED = 5;
   private static final int MSG_FAIL = 6;
   private static final int MSG_PING_TEST_PASS = 7;
   private static final int MSG_PING_TEST_FAILED = 8;
   private static final int MSG_PING_TEST = 9;
   private static final int MSG_FINISH_TEST = 10;
   private Handler mHandler;
   private BroadcastReceiver mReceiver;
   TextView mResult;
   TextView mInfoText;
   boolean stop = false;
   private boolean mReadyToTest = false;
   TextView mText;
   TextView mTitle;
   ProgressBar wifiProgressBar;
 
   private List<String> mWifiList;
   private WifiManager mWifiManager;
 
   private final static String ERRMSG = "Wifi test failed!";
 
   NetworkInfo mNetworkInfo = new NetworkInfo(ConnectivityManager.TYPE_WIFI, 0, "WIFI", "");
 
   public WifiTestActivity() {
 
       this.mWifiList = new ArrayList<String>();
 
       this.mReceiver = new MyBroadcastReceiver();
 
       mHandler = new MyHandler();
   }
 
   //
   protected void onCreate(Bundle savedInstanceState) {
 
       super.onCreate(savedInstanceState);
       // requestWindowFeature(Window.FEATURE_NO_TITLE);
       getWindow().addFlags(FLAG_FULLSCREEN | FLAG_KEEP_SCREEN_ON);
       setContentView(R.layout.wifitest);
 
       this.mResult = (TextView) findViewById(R.id.wifiresultText);
       this.mResult.setVisibility(View.VISIBLE);
       // this.mResult.setGravity(17);
       mInfoText = (TextView) findViewById(R.id.wifiInfoText);
 
       wifiProgressBar = (ProgressBar) findViewById(R.id.wifiprogress);
       wifiProgressBar.setVisibility(View.VISIBLE);
       ControlButtonUtil.initControlButtonView(this);
 
       this.mWifiManager = (WifiManager) getSystemService("wifi");
       findViewById(R.id.btn_Pass).setVisibility(View.INVISIBLE);
   }
 
   protected void onResume() {
       super.onResume();
       IntentFilter localIntentFilter = new IntentFilter();
       localIntentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
       localIntentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
       localIntentFilter
               .addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
       localIntentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
       registerReceiver(mReceiver, localIntentFilter);
       this.mWifiList.clear();
 
       stop = false;
       Log.i("Jeffy", "try to enable wifi");
       mWifiManager.setWifiEnabled(true);
       mHandler.sendEmptyMessage(MSG_SCAN);
 
       Log.i("Jeffy", "start test");
   }
 
   public void onPause() {
       stop = true;
       super.onPause();
       if (this.mWifiManager == null) {
           return;
       }
 
       Log.i("Jeffy", "end test");
       this.mHandler.removeMessages(MSG_SCAN);
       this.mHandler.removeMessages(MSG_ERROR);
 
       unregisterReceiver(mReceiver);
       // mWifiManager.setWifiEnabled(false);
       mHandler.removeMessages(MSG_FAIL);
   }
 
   class MyHandler extends Handler {
 
       public void handleMessage(Message msg) {
           if (stop) {
               return;
           }
           switch (msg.what) {
 
           case MSG_SCAN:
               Log.i(TAG, "try to scan");
               removeMessages(MSG_SCAN);
               mWifiManager.startScan();
               break;
 
           case MSG_HTTP_TEST:
               removeMessages(MSG_HTTP_TEST);
               if (mReadyToTest) {
                   Log.i("Jeffy", "do http test:" + HTTP_TEST_URL);
                   mInfoText.setText(mInfoText.getText() + "\ntesting "
                           + HTTP_TEST_URL);
                   new Thread(new Runnable() {
 
                       public void run() {
 
                           mHandler.sendEmptyMessage(httpClientTest() ? MSG_HTTP_TEST_PASS
                                   : MSG_HTTP_TEST_FAILED);
                       }
                   }).start();
               }
               break;
 
           case MSG_PING_TEST:
               removeMessages(MSG_PING_TEST);
               if (mReadyToTest) {
                   Log.i("Jeffy", "do ping test:" + PING_TEST_ADDR);
                   mInfoText.setText(mInfoText.getText() + "\ntesting "
                           + PING_TEST_ADDR);
                   new Thread(new Runnable() {
 
                       public void run() {
 
                           mHandler.sendEmptyMessage(pingTest() ? MSG_PING_TEST_PASS
                                   : MSG_PING_TEST_FAILED);
                       }
                   }).start();
               }
               break;
           case MSG_PING_TEST_PASS: {
               mInfoText.setText(mInfoText.getText() + "\n" + getString(R.string.WifiPing)+getString(R.string.btnPassText));
               wifiProgressBar.setVisibility(View.GONE);
               mHandler.sendEmptyMessageDelayed(MSG_FINISH_TEST, 2000);
               findViewById(R.id.btn_Pass).performClick();
               break;
           }
           case MSG_PING_TEST_FAILED: {
               mInfoText.setText(mInfoText.getText() + "\n" + getString(R.string.WifiPing)+getString(R.string.btnFailText));
               wifiProgressBar.setVisibility(View.GONE);
               mHandler.sendEmptyMessageDelayed(MSG_FAIL,
                       TEST_FAILED_DELAY);
               break;
           }
           case MSG_HTTP_TEST_PASS: {
               mInfoText.setText(mInfoText.getText() + "\n" + getString(R.string.WifiHttp)+getString(R.string.btnPassText));
               wifiProgressBar.setVisibility(View.GONE);
               findViewById(R.id.btn_Pass).performClick();
               break;
           }
 
           case MSG_HTTP_TEST_FAILED: {
               mInfoText.setText(getString(R.string.WifiHttp)+getString(R.string.btnFailText));
               wifiProgressBar.setVisibility(View.GONE);
               mHandler.sendEmptyMessageDelayed(MSG_FAIL,
                       TEST_FAILED_DELAY);
               break;
           }
 
           case MSG_ERROR:
 
               removeMessages(MSG_ERROR);
               wifiProgressBar.setVisibility(View.GONE);
               mResult.setText(getString(R.string.WifiError));
               mHandler.sendEmptyMessageDelayed(MSG_FAIL,
                       TEST_FAILED_DELAY);
               break;
           case MSG_FAIL:
               removeMessages(MSG_FAIL);
               wifiProgressBar.setVisibility(View.GONE);
               findViewById(R.id.btn_Fail).performClick();
               break;
           case MSG_FINISH_TEST:
               wifiProgressBar.setVisibility(View.GONE);
               findViewById(R.id.btn_Pass).performClick();
               break;
           }
       }
   }
 
   class MyBroadcastReceiver extends BroadcastReceiver {
 
       public void onReceive(Context context, Intent intent) {
           if (stop) {
               return;
           }
           String action = intent.getAction();
 
           Log.i("Jeffy", "action:" + action);
 
           if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) {
               int state = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
                       WifiManager.WIFI_STATE_UNKNOWN);
               if (state == WifiManager.WIFI_STATE_ENABLED) {
                   mHandler.sendEmptyMessage(MSG_SCAN);
               }
           }
 
           if (WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION.equals(action)) {
               boolean connected = intent.getBooleanExtra(
                       WifiManager.EXTRA_SUPPLICANT_CONNECTED, false);
               if (connected && mReadyToTest) {
                   Log.i("Jeffy", "already connect to:"
                           + mWifiManager.getConnectionInfo().getSSID());
                   mInfoText.setText("connect to "
                           + mWifiManager.getConnectionInfo().getSSID());
//                    mHandler.sendEmptyMessage(MSG_PING_TEST);    //change no pingtest
                   mHandler.sendEmptyMessageDelayed(MSG_FINISH_TEST, 3000);
               }
           }
 
           if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) {
               mNetworkInfo = (NetworkInfo) intent.getParcelableExtra(
               WifiManager.EXTRA_NETWORK_INFO);
               // reset & clear notification on a network connect & disconnect
               switch(mNetworkInfo.getDetailedState()) {
                   case CONNECTED:
                       Log.i("Jeffy", "connect to:"
                       + mWifiManager.getConnectionInfo().getSSID());
                       mInfoText.setText("Connected to "
                       + mWifiManager.getConnectionInfo().getSSID());
                       mHandler.sendEmptyMessageDelayed(MSG_FINISH_TEST, 3000);
                       break;
               }
           }
 
           if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(action)) {
               List<ScanResult> resultList = mWifiManager.getScanResults();
               Collections.sort(resultList, new Comparator<ScanResult>() {
 
                   public int compare(ScanResult s1, ScanResult s2) {
                       return s2.level - s1.level;
                   }
               });
 
               if ((resultList != null) && (!resultList.isEmpty())) {
 
                   String str3 = getString(R.string.wififindtest);
                   StringBuilder sb = new StringBuilder().append(str3).append(
                           "\n");
                   ScanResult selectAp = null;
                   for (ScanResult scanResult : resultList) {
                       sb.append(
                               scanResult.SSID
                                       + "\t- "
                                       + scanResult.capabilities
                                       + "\t- level:"
                                       + WifiManager.calculateSignalLevel(
                                               scanResult.level, 4)).append(
                               "\n");
                       if (scanResult.capabilities.length() < 6) {
                           if (null == selectAp
                                   || selectAp.level < scanResult.level) {
                               selectAp = scanResult;
                           }
                       }
                   }
                   mResult.setText(sb.toString());
 
                   mReadyToTest = true;
 
                   if (mWifiManager.getConnectionInfo().getIpAddress() != 0) {
                       Log.i("Jeffy", "--already connect to:"
                               + mWifiManager.getConnectionInfo().getSSID());
                       mInfoText.setText(getString(R.string.WifiConnect)
                               + mWifiManager.getConnectionInfo().getSSID());
//                        mHandler.sendEmptyMessage(MSG_PING_TEST);    //change no pingtest
                       mHandler.sendEmptyMessageDelayed(MSG_FINISH_TEST, 3000);
                       return;
                   }
                   Log.i("Jeffy", "--selected ap:" + selectAp);
                   if (null == selectAp) {
                       mInfoText.setText(getString(R.string.WifiConnectErr));
                       wifiProgressBar.setVisibility(View.GONE);
                       //mHandler.sendEmptyMessageDelayed(MSG_FAIL,
                       //         TEST_FAILED_DELAY);
 
                   mHandler.sendEmptyMessageDelayed(MSG_PING_TEST_PASS, 500);
                       return;
                   }
 
                   int networkId = getNetworkId(selectAp.BSSID, selectAp.SSID);
                   mInfoText.setText(getString(R.string.WifiTry) + " " + selectAp.SSID);
                   Log.i("Jeffy", "--try connect to ap:" + selectAp.SSID);
                   mWifiManager.enableNetwork(networkId, true);
 
                   //
                   // wifiProgressBar.setVisibility(View.GONE);
                   // isTestFinish = true;
                   // mResult.setText("Find wifi network...\n Pass!");
 
                   mHandler.sendEmptyMessageDelayed(MSG_PING_TEST_PASS, 500);
               }
           }
 
       }
   }
 
   private int getNetworkId(String BSSID, String SSID) {
       for (WifiConfiguration wifiConfiguration : mWifiManager
               .getConfiguredNetworks()) {
           if (BSSID.equals(wifiConfiguration.BSSID)) {
               Log.i("Jeffy", "--get existed config:" + wifiConfiguration.SSID);
               return wifiConfiguration.networkId;
           }
       }
 
       WifiConfiguration wc = new WifiConfiguration();
       wc.BSSID = BSSID;
       wc.SSID = "\"" + SSID + "\"";
       wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
       wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
       wc.status = WifiConfiguration.Status.ENABLED;
       wc.networkId = mWifiManager.addNetwork(wc);
       Log.i("Jeffy", "--new config:" + wc.SSID);
       return wc.networkId;
   }
 
   private boolean httpClientTest() {
       HttpClient client = new DefaultHttpClient();
       try {
           // TODO: Hardcoded for now, make it UI configurable
           HttpGet request = new HttpGet(HTTP_TEST_URL);
           HttpResponse response = client.execute(request);
           Log.i("Jeffy", "http result code:"
                   + response.getStatusLine().getStatusCode());
           if (response.getStatusLine().getStatusCode() == 401) {
               request.abort();
               return true;
           } else {
               request.abort();
               return false;
           }
       } catch (IOException e) {
           Log.i("Jeffy", "http exception");
           e.printStackTrace();
           return false;
       } finally {
       }
   }
 
   private boolean pingTest() {
//        int status = SystemUtil.execShellCmdForStatue("ping -c 1 -w 5 "
//                + PING_TEST_ADDR);
//        return (status == 0);
       boolean ret = false;
       URL serverURL;
       try {
           serverURL = new URL("http://www.baidu.com");
           Log.d(TAG, " __________________-------- serverURL = " + serverURL.toString());
           // connect to server
           URLConnection uc2 = serverURL.openConnection();
           HttpURLConnection conn = (HttpURLConnection) uc2;
           Log.d(TAG, " __________________--------00 serverURL = " + serverURL.toString());
           uc2.setAllowUserInteraction(true);
           uc2.setConnectTimeout(55000);
           uc2.setDoInput(true);
           uc2.setDoOutput(true);
//            conn.setConnectTimeout(1000);
           conn.setReadTimeout(1000);
           int numBytesRead = 0;
           int allBytesRead = 0;
           Log.d(TAG, " __________________--------11 conn.getContentLength() = " + conn.getContentLength());
           InputStream in = conn.getInputStream();
           byte[] buffer = new byte[4096];
           do {
               numBytesRead = in.read(buffer);
               allBytesRead = allBytesRead + numBytesRead;
           } while (numBytesRead > 0);
           Log.d(TAG, " __________________-------- allBytesRead = " + allBytesRead + "   " + conn.getContentLength());
           if(allBytesRead > 10)
               ret = true;
       } catch (MalformedURLException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       } catch (IOException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
       return ret;
   }
 
   public boolean dispatchKeyEvent(KeyEvent event) {
       if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
           return false;
       }
       return super.dispatchKeyEvent(event);
   }
}