Renamed the receiver, but forgot in the previous commit.
authorvikrama <vikrama@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Fri, 9 Jul 2010 05:55:08 +0000 (05:55 +0000)
committervikrama <vikrama@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Fri, 9 Jul 2010 05:55:08 +0000 (05:55 +0000)
git-svn-id: http://zxing.googlecode.com/svn/trunk@1471 59b500cc-1b3d-0410-9834-0bbf25fbcc57

android/src/com/google/zxing/client/android/wifi/WifiReceiver.java [new file with mode: 0644]

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 (file)
index 0000000..1e640b8
--- /dev/null
@@ -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