[android] set connectivity state pro-actively
This commit is contained in:
@@ -8,6 +8,7 @@ import android.text.TextUtils;
|
||||
import com.mapbox.mapboxsdk.constants.MapboxConstants;
|
||||
import com.mapbox.mapboxsdk.exceptions.InvalidAccessTokenException;
|
||||
import com.mapbox.mapboxsdk.exceptions.MapboxAccountManagerNotStartedException;
|
||||
import com.mapbox.mapboxsdk.net.ConnectivityReceiver;
|
||||
import com.mapbox.mapboxsdk.telemetry.MapboxEventManager;
|
||||
|
||||
public class MapboxAccountManager {
|
||||
@@ -40,11 +41,16 @@ public class MapboxAccountManager {
|
||||
*/
|
||||
public static MapboxAccountManager start(Context context, String accessToken) {
|
||||
if (mapboxAccountManager == null) {
|
||||
//Create a new account manager
|
||||
mapboxAccountManager = new MapboxAccountManager(context, accessToken);
|
||||
|
||||
//Initialize the event manager
|
||||
MapboxEventManager.getMapboxEventManager().initialize(context, accessToken);
|
||||
|
||||
//Register a receiver to listen for connectivity updates
|
||||
ConnectivityReceiver.instance(context);
|
||||
}
|
||||
|
||||
MapboxEventManager eventManager = MapboxEventManager.getMapboxEventManager();
|
||||
eventManager.initialize(mapboxAccountManager.applicationContext, mapboxAccountManager.accessToken);
|
||||
return mapboxAccountManager;
|
||||
}
|
||||
|
||||
@@ -92,8 +98,7 @@ public class MapboxAccountManager {
|
||||
public boolean isConnected() {
|
||||
ConnectivityManager cm = (ConnectivityManager) applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
|
||||
boolean result = (activeNetwork != null && activeNetwork.isConnected());
|
||||
return result;
|
||||
return (activeNetwork != null && activeNetwork.isConnected());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.mapbox.mapboxsdk.net;
|
||||
|
||||
/**
|
||||
* Receives updates on connectivity state
|
||||
*/
|
||||
public interface ConnectivityListener {
|
||||
|
||||
void onNetworkStateChanged(boolean connected);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package com.mapbox.mapboxsdk.net;
|
||||
|
||||
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 android.support.annotation.NonNull;
|
||||
import android.util.Log;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
/**
|
||||
* Interface definition for a callback to be invoked when connectivity changes.
|
||||
* Not public api.
|
||||
*/
|
||||
public class ConnectivityReceiver extends BroadcastReceiver {
|
||||
private static final String TAG = ConnectivityReceiver.class.getSimpleName();
|
||||
private static ConnectivityReceiver INSTANCE;
|
||||
|
||||
/**
|
||||
* Get or create the singleton instance
|
||||
*/
|
||||
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
|
||||
INSTANCE.addListener(new NativeConnectivityListener());
|
||||
}
|
||||
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
private List<ConnectivityListener> listeners = new CopyOnWriteArrayList<>();
|
||||
|
||||
private ConnectivityReceiver() {
|
||||
}
|
||||
|
||||
/**
|
||||
* @see BroadcastReceiver#onReceive(Context, Intent)
|
||||
*/
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
boolean connected = isConnected(context);
|
||||
Log.v(TAG, "Connected: " + connected);
|
||||
|
||||
//Loop over listeners
|
||||
for (ConnectivityListener listener : listeners) {
|
||||
listener.onNetworkStateChanged(connected);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a listener to be notified
|
||||
*
|
||||
* @param listener the listener to add
|
||||
*/
|
||||
public void addListener(@NonNull ConnectivityListener listener) {
|
||||
listeners.add(listener);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a listener
|
||||
*
|
||||
* @param listener the listener to remove
|
||||
*/
|
||||
public void removeListener(@NonNull ConnectivityListener listener) {
|
||||
listeners.remove(listener);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current connectivity state
|
||||
*
|
||||
* @param context current Context
|
||||
* @return true if connected
|
||||
*/
|
||||
public boolean isConnected(Context context) {
|
||||
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
|
||||
return (activeNetwork != null && activeNetwork.isConnected());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.mapbox.mapboxsdk.net;
|
||||
|
||||
/**
|
||||
* Updates the native library's connectivity state
|
||||
*/
|
||||
class NativeConnectivityListener implements ConnectivityListener {
|
||||
|
||||
static {
|
||||
System.loadLibrary("mapbox-gl");
|
||||
}
|
||||
|
||||
private long nativePtr;
|
||||
private boolean invalidated;
|
||||
|
||||
NativeConnectivityListener(long nativePtr) {
|
||||
this.nativePtr = nativePtr;
|
||||
}
|
||||
|
||||
NativeConnectivityListener() {
|
||||
initialize();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNetworkStateChanged(boolean connected) {
|
||||
nativeOnConnectivityStateChanged(connected);
|
||||
}
|
||||
|
||||
protected native void nativeOnConnectivityStateChanged(boolean connected);
|
||||
|
||||
protected native void initialize();
|
||||
|
||||
@Override
|
||||
protected native void finalize() throws Throwable;
|
||||
}
|
||||
Reference in New Issue
Block a user