2 * Copyright (C) 2010 ZXing authors
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.google.zxing.client.android.wifi;
19 import android.app.Activity;
20 import android.content.BroadcastReceiver;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.net.ConnectivityManager;
24 import android.net.NetworkInfo;
25 import android.net.wifi.SupplicantState;
26 import android.net.wifi.WifiManager;
27 import android.util.Log;
28 import android.widget.TextView;
30 import com.google.zxing.client.android.R;
33 * Get a broadcast when the network is connected, and kill the activity.
35 final class WifiReceiver extends BroadcastReceiver {
36 private final String TAG = "WifiReceiver";
37 private final WifiManager mWifiManager;
38 private final Activity parent;
39 private final TextView statusView;
41 WifiReceiver(WifiManager wifiManager, Activity wifiActivity, TextView statusView, String ssid) {
42 this.parent = wifiActivity;
43 this.statusView = statusView;
44 this.mWifiManager = wifiManager;
48 public void onReceive(Context context, Intent intent) {
49 if (intent.getAction().equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)) {
50 handleChange((SupplicantState) intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE),
51 intent.hasExtra(WifiManager.EXTRA_SUPPLICANT_ERROR),
52 intent.getIntExtra(WifiManager.EXTRA_SUPPLICANT_ERROR, 0));
53 } else if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)){
54 handleNetworkStateChanged((NetworkInfo) intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO));
55 } else if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
56 final ConnectivityManager con = (ConnectivityManager) parent.getSystemService(Context.CONNECTIVITY_SERVICE);
57 final NetworkInfo[] s = con.getAllNetworkInfo();
58 for (final NetworkInfo i : s){
59 if (i.getTypeName().contentEquals("WIFI")){
60 final NetworkInfo.State state = i.getState();
61 final String ssid = mWifiManager.getConnectionInfo().getSSID();
63 if (state == NetworkInfo.State.CONNECTED && ssid != null){
64 mWifiManager.saveConfiguration();
65 final String label = parent.getString(R.string.wifi_connected);
66 statusView.setText(label + "\n" + ssid);
67 Runnable delayKill = new Killer(parent);
70 if (state == NetworkInfo.State.DISCONNECTED){
71 Log.d(TAG, "Got state: " + state.toString() + " for ssid: " + ssid);
72 ((WifiActivity)parent).gotError();
79 private void handleNetworkStateChanged(NetworkInfo networkInfo) {
80 final NetworkInfo.DetailedState state = networkInfo.getDetailedState();
81 if (state == NetworkInfo.DetailedState.FAILED){
82 Log.d(TAG, "Detailed Network state failed");
83 ((WifiActivity)parent).gotError();
87 private void handleChange(SupplicantState state, boolean hasError, int error) {
88 if (hasError || state == SupplicantState.INACTIVE){
89 Log.d(TAG, "Found an error");
90 ((WifiActivity)parent).gotError();