mirror of
https://github.com/zxing/zxing.git
synced 2025-03-05 20:48:51 -08:00
Renamed the receiver, but forgot in the previous commit.
git-svn-id: https://zxing.googlecode.com/svn/trunk@1471 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
parent
77207acb31
commit
9e1dbf07d9
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue