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.content.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.net.ConnectivityManager;
23 import android.net.NetworkInfo;
24 import android.net.wifi.SupplicantState;
25 import android.net.wifi.WifiManager;
26 import android.util.Log;
27 import android.widget.TextView;
29 import com.google.zxing.client.android.R;
32 * Get a broadcast when the network is connected, and kill the activity.
34 final class WifiReceiver extends BroadcastReceiver {
36 private static final String TAG = WifiReceiver.class.getSimpleName();
38 private final WifiManager mWifiManager;
39 private final WifiActivity parent;
40 private final TextView statusView;
42 WifiReceiver(WifiManager wifiManager, WifiActivity wifiActivity, TextView statusView, String ssid) {
43 this.parent = wifiActivity;
44 this.statusView = statusView;
45 this.mWifiManager = wifiManager;
49 public void onReceive(Context context, Intent intent) {
50 if (intent.getAction().equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)) {
52 (SupplicantState) intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE),
53 intent.hasExtra(WifiManager.EXTRA_SUPPLICANT_ERROR));
54 } else if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)){
55 handleNetworkStateChanged((NetworkInfo) intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO));
56 } else if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
57 ConnectivityManager con = (ConnectivityManager) parent.getSystemService(Context.CONNECTIVITY_SERVICE);
58 NetworkInfo[] s = con.getAllNetworkInfo();
59 for (NetworkInfo i : s){
60 if (i.getTypeName().contentEquals("WIFI")){
61 NetworkInfo.State state = i.getState();
62 String ssid = mWifiManager.getConnectionInfo().getSSID();
64 if (state == NetworkInfo.State.CONNECTED && ssid != null){
65 mWifiManager.saveConfiguration();
66 String label = parent.getString(R.string.wifi_connected);
67 statusView.setText(label + '\n' + ssid);
68 Runnable delayKill = new Killer(parent);
71 if (state == NetworkInfo.State.DISCONNECTED){
72 Log.d(TAG, "Got state: " + state + " for ssid: " + ssid);
73 ((WifiActivity)parent).gotError();
80 private void handleNetworkStateChanged(NetworkInfo networkInfo) {
81 NetworkInfo.DetailedState state = networkInfo.getDetailedState();
82 if (state == NetworkInfo.DetailedState.FAILED){
83 Log.d(TAG, "Detailed Network state failed");
88 private void handleChange(SupplicantState state, boolean hasError) {
89 if (hasError || state == SupplicantState.INACTIVE){
90 Log.d(TAG, "Found an error");