From 41e63011c6beec75e3ebe8ee9e190214e26f6ae3 Mon Sep 17 00:00:00 2001 From: vikrama Date: Fri, 9 Jul 2010 05:55:08 +0000 Subject: [PATCH] Renamed the receiver, but forgot in the previous commit. git-svn-id: http://zxing.googlecode.com/svn/trunk@1471 59b500cc-1b3d-0410-9834-0bbf25fbcc57 --- .../client/android/wifi/WifiReceiver.java | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 android/src/com/google/zxing/client/android/wifi/WifiReceiver.java diff --git a/android/src/com/google/zxing/client/android/wifi/WifiReceiver.java b/android/src/com/google/zxing/client/android/wifi/WifiReceiver.java new file mode 100644 index 00000000..1e640b8d --- /dev/null +++ b/android/src/com/google/zxing/client/android/wifi/WifiReceiver.java @@ -0,0 +1,93 @@ +/* + * Copyright (C) 2010 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.zxing.client.android.wifi; + +import android.app.Activity; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.net.ConnectivityManager; +import android.net.NetworkInfo; +import android.net.wifi.SupplicantState; +import android.net.wifi.WifiManager; +import android.util.Log; +import android.widget.TextView; + +import com.google.zxing.client.android.R; + +/** + * Get a broadcast when the network is connected, and kill the activity. + */ +final class WifiReceiver extends BroadcastReceiver { + private final String TAG = "WifiReceiver"; + private final WifiManager mWifiManager; + private final Activity parent; + private final TextView statusView; + + WifiReceiver(WifiManager wifiManager, Activity wifiActivity, TextView statusView, String ssid) { + this.parent = wifiActivity; + this.statusView = statusView; + this.mWifiManager = wifiManager; + } + + @Override + public void onReceive(Context context, Intent intent) { + if (intent.getAction().equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)) { + handleChange((SupplicantState) intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE), + intent.hasExtra(WifiManager.EXTRA_SUPPLICANT_ERROR), + intent.getIntExtra(WifiManager.EXTRA_SUPPLICANT_ERROR, 0)); + } else if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)){ + handleNetworkStateChanged((NetworkInfo) intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO)); + } else if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) { + final ConnectivityManager con = (ConnectivityManager) parent.getSystemService(Context.CONNECTIVITY_SERVICE); + final NetworkInfo[] s = con.getAllNetworkInfo(); + for (final NetworkInfo i : s){ + if (i.getTypeName().contentEquals("WIFI")){ + final NetworkInfo.State state = i.getState(); + final String ssid = mWifiManager.getConnectionInfo().getSSID(); + + if (state == NetworkInfo.State.CONNECTED){ + mWifiManager.saveConfiguration(); + final String label = parent.getString(R.string.wifi_connected); + statusView.setText(label + "\n" + ssid); + Runnable delayKill = new Killer(parent); + delayKill.run(); + } + if (state == NetworkInfo.State.DISCONNECTED){ + Log.d(TAG, "Got state: " + state.toString() + " for ssid: " + ssid); + ((WifiActivity)parent).gotError(); + } + } + } + } + } + + private void handleNetworkStateChanged(NetworkInfo networkInfo) { + final NetworkInfo.DetailedState state = networkInfo.getDetailedState(); + if (state == NetworkInfo.DetailedState.FAILED){ + Log.d(TAG, "Detailed Network state failed"); + ((WifiActivity)parent).gotError(); + } + } + + private void handleChange(SupplicantState state, boolean hasError, int error) { + if (hasError || state == SupplicantState.INACTIVE){ + Log.d(TAG, "Found an error"); + ((WifiActivity)parent).gotError(); + } + } +} \ No newline at end of file -- 2.20.1