Last active
August 29, 2015 14:10
-
-
Save shirish87/77fb2826ccae34e092a9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.buggycoder.io.lib; | |
import android.content.BroadcastReceiver; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.IntentFilter; | |
import android.net.ConnectivityManager; | |
import android.net.NetworkInfo; | |
import com.squareup.otto.Bus; | |
import timber.log.Timber; | |
public class ConnectivityNotifier implements HWStatusNotifier { | |
private final Context mContext; | |
private final Bus mBus; | |
private NetStateChangeEvent mLastNetStateChangeEvent; | |
public ConnectivityNotifier(Context context, Bus bus) { | |
mContext = context; | |
mBus = bus; | |
mLastNetStateChangeEvent = new NetStateChangeEvent(NetworkType.UNKNOWN, false); | |
} | |
public void registerReceiver() { | |
mContext.registerReceiver(mConnStatusReceiver, | |
new IntentFilter(android.net.ConnectivityManager.CONNECTIVITY_ACTION)); | |
} | |
public void unregisterReceiver() { | |
mContext.unregisterReceiver(mConnStatusReceiver); | |
} | |
public boolean isDisconnected() { | |
return mLastNetStateChangeEvent.isDisconnected; | |
} | |
@Override | |
public void publish(Object o) { | |
if (mBus != null) { | |
mBus.post(o); | |
} | |
} | |
/** | |
* Check whether the device is connected, and if so, whether the connection | |
* is wifi or mobile (it could be something else). | |
*/ | |
public NetworkType detectConnNetworkType(Context context) { | |
android.net.ConnectivityManager connMgr = | |
(android.net.ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); | |
NetworkInfo activeInfo = connMgr.getActiveNetworkInfo(); | |
if (activeInfo != null && activeInfo.isConnected()) { | |
switch (activeInfo.getType()) { | |
case android.net.ConnectivityManager.TYPE_WIFI: | |
return NetworkType.WIFI; | |
case android.net.ConnectivityManager.TYPE_MOBILE: | |
return NetworkType.MOBILE; | |
default: | |
return NetworkType.OTHER; | |
} | |
} else { | |
return NetworkType.NONE; | |
} | |
} | |
public static enum NetworkType { | |
WIFI, MOBILE, OTHER, NONE, UNKNOWN | |
} | |
private final BroadcastReceiver mConnStatusReceiver = new BroadcastReceiver() { | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
NetworkType networkType = detectConnNetworkType(context); | |
boolean isDisconnected = intent.getBooleanExtra(android.net.ConnectivityManager.EXTRA_NO_CONNECTIVITY, | |
(networkType == NetworkType.NONE)); | |
if (mLastNetStateChangeEvent.hasChanged(networkType, isDisconnected)) { | |
NetStateChangeEvent netStateChangeEvent = new NetStateChangeEvent(networkType, isDisconnected); | |
Timber.d("NETWORKCHANGE: %s / %s", mLastNetStateChangeEvent.networkType.name(), networkType.name()); | |
publish(netStateChangeEvent); | |
mLastNetStateChangeEvent = netStateChangeEvent; | |
} | |
Timber.d("NETWORKTYPE: %s | %s", networkType.name(), isDisconnected); | |
} | |
}; | |
public static class NetStateChangeEvent { | |
public final NetworkType networkType; | |
public final boolean isDisconnected; | |
public NetStateChangeEvent(NetworkType networkType, boolean isDisconnected) { | |
this.networkType = networkType; | |
this.isDisconnected = isDisconnected; | |
} | |
public boolean hasChanged(NetworkType networkType, boolean isDisconnected) { | |
return (networkType != this.networkType || isDisconnected != this.isDisconnected); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment