diff options
author | Jesse Crocker <jesse@gaiagps.com> | 2017-03-01 11:15:11 -0700 |
---|---|---|
committer | Jesse Crocker <jesse@gaiagps.com> | 2017-03-01 11:15:11 -0700 |
commit | 9e8dc9a9e3e86adb9987ae69766cc42c7d9efece (patch) | |
tree | f5f0abd4d342c89ad0405d01969f9d6caecc1c90 /platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/net/ConnectivityReceiver.java | |
parent | 16fb0672e64a72b7400c321d55858b73cd5d8c3f (diff) | |
parent | f28d75dccd9bf4a7615df87faccc5cf5eff8df89 (diff) | |
download | qtlocation-mapboxgl-9e8dc9a9e3e86adb9987ae69766cc42c7d9efece.tar.gz |
Merge remote-tracking branch 'origin/master' into feature/custom-vector-source
Diffstat (limited to 'platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/net/ConnectivityReceiver.java')
-rw-r--r-- | platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/net/ConnectivityReceiver.java | 45 |
1 files changed, 37 insertions, 8 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/net/ConnectivityReceiver.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/net/ConnectivityReceiver.java index 7c37569ae2..7be56fa694 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/net/ConnectivityReceiver.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/net/ConnectivityReceiver.java @@ -7,6 +7,7 @@ import android.content.IntentFilter; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.support.annotation.NonNull; +import android.support.annotation.UiThread; import com.mapbox.mapboxsdk.Mapbox; @@ -27,11 +28,9 @@ public class ConnectivityReceiver extends BroadcastReceiver { */ public static synchronized ConnectivityReceiver instance(Context context) { if (INSTANCE == null) { - //Register new instance - INSTANCE = new ConnectivityReceiver(); - context.registerReceiver(INSTANCE, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE")); - - //Add default listeners + // Register new instance + INSTANCE = new ConnectivityReceiver(context.getApplicationContext()); + // Add default listeners INSTANCE.addListener(new NativeConnectivityListener()); } @@ -39,8 +38,39 @@ public class ConnectivityReceiver extends BroadcastReceiver { } private List<ConnectivityListener> listeners = new CopyOnWriteArrayList<>(); + private Context context; + private int activationCounter; + + private ConnectivityReceiver(@NonNull Context context) { + this.context = context; + } + + /** + * Activates the connectivity receiver. + * <p> + * if the underlying connectivity receiver isn't active, register the connectivity receiver. + * </p> + */ + @UiThread + public void activate() { + if (activationCounter == 0) { + context.registerReceiver(INSTANCE, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE")); + } + activationCounter++; + } - private ConnectivityReceiver() { + /** + * Deactivates the connectivity receiver. + * <p> + * if no other components are listening, unregister the underlying connectivity receiver. + * </p> + */ + @UiThread + public void deactivate() { + activationCounter--; + if (activationCounter == 0) { + context.unregisterReceiver(INSTANCE); + } } /** @@ -51,7 +81,7 @@ public class ConnectivityReceiver extends BroadcastReceiver { boolean connected = isConnected(context); Timber.v("Connected: " + connected); - //Loop over listeners + // Loop over listeners for (ConnectivityListener listener : listeners) { listener.onNetworkStateChanged(connected); } @@ -92,5 +122,4 @@ public class ConnectivityReceiver extends BroadcastReceiver { NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); return (activeNetwork != null && activeNetwork.isConnected()); } - } |