Minor changes from code inspection results
[zxing.git] / android / src / com / google / zxing / client / android / wifi / WifiReceiver.java
1 /*
2  * Copyright (C) 2010 ZXing authors
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.google.zxing.client.android.wifi;
18
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;
28
29 import com.google.zxing.client.android.R;
30
31 /**
32  * Get a broadcast when the network is connected, and kill the activity.
33  */
34 final class WifiReceiver extends BroadcastReceiver {
35
36   private static final String TAG = WifiReceiver.class.getSimpleName();
37
38   private final WifiManager mWifiManager;
39   private final WifiActivity parent;
40   private final TextView statusView;
41
42   WifiReceiver(WifiManager wifiManager, WifiActivity wifiActivity, TextView statusView, String ssid) {
43     this.parent = wifiActivity;
44     this.statusView = statusView;
45     this.mWifiManager = wifiManager;
46   }
47
48   @Override
49   public void onReceive(Context context, Intent intent) {
50     if (intent.getAction().equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)) {
51       handleChange(
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();
63
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);
69             delayKill.run();
70           }
71           if (state == NetworkInfo.State.DISCONNECTED){
72             Log.d(TAG, "Got state: " + state + " for ssid: " + ssid);
73             parent.gotError();
74           }
75         }
76       }
77     }
78   }
79
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");
84       parent.gotError();
85     }
86   }
87
88   private void handleChange(SupplicantState state, boolean hasError) {
89     if (hasError || state == SupplicantState.INACTIVE){
90       Log.d(TAG, "Found an error");
91       parent.gotError();
92     }
93   }
94 }