From: pk910 Date: Fri, 5 Feb 2016 20:29:46 +0000 (+0100) Subject: added wifi scanner to wifi settings page X-Git-Url: http://git.pk910.de/?p=DHBWCampusApp.git;a=commitdiff_plain;h=304cb748086811c3c2dc0eaef097e781a7777686 added wifi scanner to wifi settings page --- diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index fe7a3d8..4923c3b 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -5,7 +5,9 @@ + + scanResultList = wifiManager.getScanResults(); + for(int i = 0; i < wifiPresets.length; i++) { + boolean networkFound = false; + for(ScanResult result : scanResultList) { + if(result.SSID != null && result.SSID.equalsIgnoreCase(wifiPresets[i].ssid)) { + networkFound = true; + break; + } + } + wifiPresets[i].isInScanResult = networkFound; + } + updateWifiConnectButton(); + } + + private void updateWifiConnectButton() { + if(selectedNetworkIndex >= wifiPresets.length) + return; + WifiNetworkSettingsSet preset = wifiPresets[selectedNetworkIndex]; + + boolean buttonEnabled = false; + int buttonTextId = 0; + NetworkInfo networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); + WifiInfo wifiInfo = wifiManager.getConnectionInfo(); + + if(!wifiManager.isWifiEnabled()) { + buttonTextId = R.string.wifisettings_connect_button_disabled; + } else if(networkInfo.isConnectedOrConnecting() && wifiInfo.getSSID() != null && wifiInfo.getSSID().equalsIgnoreCase(preset.ssid)) { + buttonTextId = R.string.wifisettings_connect_button_ready; + } else if(!networkScanned) { + buttonTextId = R.string.wifisettings_connect_button_scan; + } else if(!preset.isInScanResult) { + buttonTextId = R.string.wifisettings_connect_button_notfound; + buttonEnabled = true; + } else { + buttonEnabled = true; + buttonTextId = R.string.wifisettings_connect_button; } + Button connectBtn = (Button) view.findViewById(R.id.wifiConnectBtn); + connectBtn.setEnabled(buttonEnabled); + connectBtn.setText(buttonTextId); } @Override @@ -347,6 +470,13 @@ public class WifiSettings extends CampusAppFragment { public void onClick(View v) { Button connectBtn = (Button) v; connectBtn.setEnabled(false); + WifiNetworkSettingsSet settings = wifiPresets[selectedNetworkIndex]; + if(!settings.isInScanResult) { + networkScanned = false; + wifiManager.startScan(); + updateWifiConnectButton(); + return; + } EditText usernameEdt = (EditText) view.findViewById(R.id.wifiUsername); EditText passwordEdt = (EditText) view.findViewById(R.id.wifiPassword); @@ -358,7 +488,7 @@ public class WifiSettings extends CampusAppFragment { // connectToDHWiFi not supported! // do something else? } else if (username.length() > 0 && password.length() > 0) { - WifiNetworkSettingsSet settings = wifiPresets[selectedNetworkIndex]; + if(settings != null) { settings.setAuthData(username, password); connectToWiFi(settings); @@ -369,7 +499,37 @@ public class WifiSettings extends CampusAppFragment { } }); + updateWifiState(); + updateWifiScanState(); + return view; } + @Override + public void onResume() { + super.onResume(); + + if(AppContext == null) + AppContext = CampusAppContext.getInstance(); + + AppContext.getMainActivity().registerReceiver(this.wifiStateReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); + AppContext.getMainActivity().registerReceiver(this.wifiScanReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); + + networkScanned = false; + wifiManager.startScan(); + updateWifiConnectButton(); + + Log.i("WifiSettings", "registered wifi state receiver"); + } + + @Override + public void onPause() { + super.onPause(); + + AppContext.getMainActivity().unregisterReceiver(this.wifiStateReceiver); + AppContext.getMainActivity().unregisterReceiver(this.wifiScanReceiver); + + Log.i("WifiSettings", "unregistered wifi state receiver"); + } + } diff --git a/app/src/main/res/layout/fragment_wifi_settings.xml b/app/src/main/res/layout/fragment_wifi_settings.xml index 7c9fc46..4b756ac 100644 --- a/app/src/main/res/layout/fragment_wifi_settings.xml +++ b/app/src/main/res/layout/fragment_wifi_settings.xml @@ -13,6 +13,28 @@ android:layout_height="wrap_content" android:text="@string/wifisettings_about_wifi" /> + + + + + + + - + android:layout_height="wrap_content"> - + - + - + - + - + - + - + + - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + android:text="@string/wifisettings_insecure_network" + android:id="@+id/textView12" + android:layout_weight="2" + android:textColor="#FF0000" /> + GTC Jetzt Verbinden + Netzwerk nicht in Reichweite + Scanning... + WLAN Adapter deaktiviert + Verbunden! Settings + + WLAN Status: + Verbunden mit %1$s + Verbinde mit %1$s... + Nicht Verbunden + + @@ -93,4 +104,6 @@ Hello blank fragment Dein Guthaben: + PSK: + Achtung: Dieses Netzwerk ist potentiell unsicher!