OSDN Git Service

457749630bf1359c2318ea31f78ec4dbc4f3d5f8
[android-x86/packages-apps-Settings.git] / src / com / android / settings / wifi / WifiStatusTest.java
1 /*
2  * Copyright (C) 2009 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.android.settings.wifi;
18
19 import android.app.Activity;
20 import android.content.BroadcastReceiver;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.IntentFilter;
24 import android.net.NetworkInfo;
25 import android.net.wifi.ScanResult;
26 import android.net.wifi.SupplicantState;
27 import android.net.wifi.WifiConfiguration;
28 import android.net.wifi.WifiInfo;
29 import android.net.wifi.WifiManager;
30 import android.os.Bundle;
31 import android.os.Handler;
32 import android.text.TextUtils;
33 import android.util.Log;
34 import android.view.View;
35 import android.view.View.OnClickListener;
36 import android.widget.Button;
37 import android.widget.TextView;
38
39 import com.android.settings.R;
40 import com.android.settingslib.wifi.AccessPoint;
41
42 import java.io.IOException;
43 import java.net.HttpURLConnection;
44 import java.net.URL;
45 import java.net.UnknownHostException;
46 import java.util.List;
47
48
49 /**
50  * Show the current status details of Wifi related fields
51  */
52 public class WifiStatusTest extends Activity {
53
54     private static final String TAG = "WifiStatusTest";
55
56     private Button updateButton;
57     private TextView mWifiState;
58     private TextView mNetworkState;
59     private TextView mSupplicantState;
60     private TextView mRSSI;
61     private TextView mBSSID;
62     private TextView mSSID;
63     private TextView mHiddenSSID;
64     private TextView mIPAddr;
65     private TextView mMACAddr;
66     private TextView mNetworkId;
67     private TextView mLinkSpeed;
68     private TextView mScanList;
69
70
71     private TextView mPingIpAddr;
72     private TextView mPingHostname;
73     private TextView mHttpClientTest;
74     private Button pingTestButton;
75
76     private String mPingIpAddrResult;
77     private String mPingHostnameResult;
78     private String mHttpClientTestResult;
79
80
81     private WifiManager mWifiManager;
82     private IntentFilter mWifiStateFilter;
83
84
85     //============================
86     // Activity lifecycle
87     //============================
88
89     private final BroadcastReceiver mWifiStateReceiver = new BroadcastReceiver() {
90         @Override
91         public void onReceive(Context context, Intent intent) {
92             if (intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
93                 handleWifiStateChanged(intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
94                             WifiManager.WIFI_STATE_UNKNOWN));
95             } else if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
96                 handleNetworkStateChanged(
97                         (NetworkInfo) intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO));
98             } else if (intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
99                 handleScanResultsAvailable();
100             } else if (intent.getAction().equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)) {
101                 /* TODO: handle supplicant connection change later */
102             } else if (intent.getAction().equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)) {
103                 handleSupplicantStateChanged(
104                         (SupplicantState) intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE),
105                         intent.hasExtra(WifiManager.EXTRA_SUPPLICANT_ERROR),
106                         intent.getIntExtra(WifiManager.EXTRA_SUPPLICANT_ERROR, 0));
107             } else if (intent.getAction().equals(WifiManager.RSSI_CHANGED_ACTION)) {
108                 handleSignalChanged(intent.getIntExtra(WifiManager.EXTRA_NEW_RSSI, 0));
109             } else if (intent.getAction().equals(WifiManager.NETWORK_IDS_CHANGED_ACTION)) {
110                 /* TODO: handle network id change info later */
111             } else {
112                 Log.e(TAG, "Received an unknown Wifi Intent");
113             }
114         }
115     };
116
117     @Override
118     protected void onCreate(Bundle savedInstanceState) {
119         super.onCreate(savedInstanceState);
120
121         mWifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
122
123         mWifiStateFilter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION);
124         mWifiStateFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
125         mWifiStateFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
126         mWifiStateFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
127         mWifiStateFilter.addAction(WifiManager.RSSI_CHANGED_ACTION);
128         mWifiStateFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
129
130         registerReceiver(mWifiStateReceiver, mWifiStateFilter);
131
132         setContentView(R.layout.wifi_status_test);
133
134         updateButton = (Button) findViewById(R.id.update);
135         updateButton.setOnClickListener(updateButtonHandler);
136
137         mWifiState = (TextView) findViewById(R.id.wifi_state);
138         mNetworkState = (TextView) findViewById(R.id.network_state);
139         mSupplicantState = (TextView) findViewById(R.id.supplicant_state);
140         mRSSI = (TextView) findViewById(R.id.rssi);
141         mBSSID = (TextView) findViewById(R.id.bssid);
142         mSSID = (TextView) findViewById(R.id.ssid);
143         mHiddenSSID = (TextView) findViewById(R.id.hidden_ssid);
144         mIPAddr = (TextView) findViewById(R.id.ipaddr);
145         mMACAddr = (TextView) findViewById(R.id.macaddr);
146         mNetworkId = (TextView) findViewById(R.id.networkid);
147         mLinkSpeed = (TextView) findViewById(R.id.link_speed);
148         mScanList = (TextView) findViewById(R.id.scan_list);
149
150
151         mPingHostname = (TextView) findViewById(R.id.pingHostnameV4);
152         mHttpClientTest = (TextView) findViewById(R.id.httpClientTest);
153
154         pingTestButton = (Button) findViewById(R.id.ping_test);
155         pingTestButton.setOnClickListener(mPingButtonHandler);
156     }
157
158     @Override
159     protected void onResume() {
160         super.onResume();
161         registerReceiver(mWifiStateReceiver, mWifiStateFilter);
162     }
163
164     @Override
165     protected void onPause() {
166         super.onPause();
167         unregisterReceiver(mWifiStateReceiver);
168     }
169
170     OnClickListener mPingButtonHandler = new OnClickListener() {
171         public void onClick(View v) {
172             updatePingState();
173         }
174     };
175
176     OnClickListener updateButtonHandler = new OnClickListener() {
177         public void onClick(View v) {
178             final WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
179
180             setWifiStateText(mWifiManager.getWifiState());
181             mBSSID.setText(wifiInfo.getBSSID());
182             mHiddenSSID.setText(String.valueOf(wifiInfo.getHiddenSSID()));
183             int ipAddr = wifiInfo.getIpAddress();
184             StringBuffer ipBuf = new StringBuffer();
185             ipBuf.append(ipAddr  & 0xff).append('.').
186                 append((ipAddr >>>= 8) & 0xff).append('.').
187                 append((ipAddr >>>= 8) & 0xff).append('.').
188                 append((ipAddr >>>= 8) & 0xff);
189
190             mIPAddr.setText(ipBuf);
191             mLinkSpeed.setText(String.valueOf(wifiInfo.getLinkSpeed())+" Mbps");
192             mMACAddr.setText(wifiInfo.getMacAddress());
193             mNetworkId.setText(String.valueOf(wifiInfo.getNetworkId()));
194             mRSSI.setText(String.valueOf(wifiInfo.getRssi()));
195             mSSID.setText(wifiInfo.getSSID());
196
197             SupplicantState supplicantState = wifiInfo.getSupplicantState();
198             setSupplicantStateText(supplicantState);
199         }
200     };
201
202     private void setSupplicantStateText(SupplicantState supplicantState) {
203         if(SupplicantState.FOUR_WAY_HANDSHAKE.equals(supplicantState)) {
204             mSupplicantState.setText("FOUR WAY HANDSHAKE");
205         } else if(SupplicantState.ASSOCIATED.equals(supplicantState)) {
206             mSupplicantState.setText("ASSOCIATED");
207         } else if(SupplicantState.ASSOCIATING.equals(supplicantState)) {
208             mSupplicantState.setText("ASSOCIATING");
209         } else if(SupplicantState.COMPLETED.equals(supplicantState)) {
210             mSupplicantState.setText("COMPLETED");
211         } else if(SupplicantState.DISCONNECTED.equals(supplicantState)) {
212             mSupplicantState.setText("DISCONNECTED");
213         } else if(SupplicantState.DORMANT.equals(supplicantState)) {
214             mSupplicantState.setText("DORMANT");
215         } else if(SupplicantState.GROUP_HANDSHAKE.equals(supplicantState)) {
216             mSupplicantState.setText("GROUP HANDSHAKE");
217         } else if(SupplicantState.INACTIVE.equals(supplicantState)) {
218             mSupplicantState.setText("INACTIVE");
219         } else if(SupplicantState.INVALID.equals(supplicantState)) {
220             mSupplicantState.setText("INVALID");
221         } else if(SupplicantState.SCANNING.equals(supplicantState)) {
222             mSupplicantState.setText("SCANNING");
223         } else if(SupplicantState.UNINITIALIZED.equals(supplicantState)) {
224             mSupplicantState.setText("UNINITIALIZED");
225         } else {
226             mSupplicantState.setText("BAD");
227             Log.e(TAG, "supplicant state is bad");
228         }
229     }
230
231     private void setWifiStateText(int wifiState) {
232         String wifiStateString;
233         switch(wifiState) {
234             case WifiManager.WIFI_STATE_DISABLING:
235                 wifiStateString = getString(R.string.wifi_state_disabling);
236                 break;
237             case WifiManager.WIFI_STATE_DISABLED:
238                 wifiStateString = getString(R.string.wifi_state_disabled);
239                 break;
240             case WifiManager.WIFI_STATE_ENABLING:
241                 wifiStateString = getString(R.string.wifi_state_enabling);
242                 break;
243             case WifiManager.WIFI_STATE_ENABLED:
244                 wifiStateString = getString(R.string.wifi_state_enabled);
245                 break;
246             case WifiManager.WIFI_STATE_UNKNOWN:
247                 wifiStateString = getString(R.string.wifi_state_unknown);
248                 break;
249             default:
250                 wifiStateString = "BAD";
251                 Log.e(TAG, "wifi state is bad");
252                 break;
253         }
254
255         mWifiState.setText(wifiStateString);
256     }
257
258     private void handleSignalChanged(int rssi) {
259         mRSSI.setText(String.valueOf(rssi));
260     }
261
262     private void handleWifiStateChanged(int wifiState) {
263         setWifiStateText(wifiState);
264     }
265
266     private void handleScanResultsAvailable() {
267         List<ScanResult> list = mWifiManager.getScanResults();
268
269         StringBuffer scanList = new StringBuffer();
270         if (list != null) {
271             for (int i = list.size() - 1; i >= 0; i--) {
272                 final ScanResult scanResult = list.get(i);
273
274                 if (scanResult == null) {
275                     continue;
276                 }
277
278                 if (TextUtils.isEmpty(scanResult.SSID)) {
279                     continue;
280                 }
281
282                 scanList.append(scanResult.SSID+" ");
283             }
284         }
285         mScanList.setText(scanList);
286     }
287
288     private void handleSupplicantStateChanged(SupplicantState state, boolean hasError, int error) {
289         if (hasError) {
290             mSupplicantState.setText("ERROR AUTHENTICATING");
291         } else {
292             setSupplicantStateText(state);
293         }
294     }
295
296     private void handleNetworkStateChanged(NetworkInfo networkInfo) {
297         if (mWifiManager.isWifiEnabled()) {
298             WifiInfo info = mWifiManager.getConnectionInfo();
299             String summary = AccessPoint.getSummary(this, info.getSSID(),
300                     networkInfo.getDetailedState(),
301                     info.getNetworkId() == WifiConfiguration.INVALID_NETWORK_ID, null);
302             mNetworkState.setText(summary);
303         }
304     }
305
306     private final void updatePingState() {
307         final Handler handler = new Handler();
308         // Set all to unknown since the threads will take a few secs to update.
309         mPingIpAddrResult = getResources().getString(R.string.radioInfo_unknown);
310         mPingHostnameResult = getResources().getString(R.string.radioInfo_unknown);
311         mHttpClientTestResult = getResources().getString(R.string.radioInfo_unknown);
312
313         mPingIpAddr.setText(mPingIpAddrResult);
314         mPingHostname.setText(mPingHostnameResult);
315         mHttpClientTest.setText(mHttpClientTestResult);
316
317         final Runnable updatePingResults = new Runnable() {
318             public void run() {
319                 mPingHostname.setText(mPingHostnameResult);
320                 mHttpClientTest.setText(mHttpClientTestResult);
321             }
322         };
323
324         Thread hostnameThread = new Thread() {
325             @Override
326             public void run() {
327                 pingHostname();
328                 handler.post(updatePingResults);
329             }
330         };
331         hostnameThread.start();
332
333         Thread httpClientThread = new Thread() {
334             @Override
335             public void run() {
336                 httpClientTest();
337                 handler.post(updatePingResults);
338             }
339         };
340         httpClientThread.start();
341     }
342
343     private final void pingHostname() {
344         try {
345             // TODO: Hardcoded for now, make it UI configurable
346             Process p = Runtime.getRuntime().exec("ping -c 1 -w 100 www.google.com");
347             int status = p.waitFor();
348             if (status == 0) {
349                 mPingHostnameResult = "Pass";
350             } else {
351                 mPingHostnameResult = "Fail: Host unreachable";
352             }
353         } catch (UnknownHostException e) {
354             mPingHostnameResult = "Fail: Unknown Host";
355         } catch (IOException e) {
356             mPingHostnameResult= "Fail: IOException";
357         } catch (InterruptedException e) {
358             mPingHostnameResult = "Fail: InterruptedException";
359         }
360     }
361
362     private void httpClientTest() {
363         HttpURLConnection urlConnection = null;
364         try {
365             // TODO: Hardcoded for now, make it UI configurable
366             URL url = new URL("https://www.google.com");
367             urlConnection = (HttpURLConnection) url.openConnection();
368             if (urlConnection.getResponseCode() == 200) {
369                 mHttpClientTestResult = "Pass";
370             } else {
371                 mHttpClientTestResult = "Fail: Code: " + urlConnection.getResponseMessage();
372             }
373         } catch (IOException e) {
374             mHttpClientTestResult = "Fail: IOException";
375         } finally {
376             if (urlConnection != null) {
377                 urlConnection.disconnect();
378             }
379         }
380     }
381
382 }