[android] #6617 - Introduce a setConnected method to manually set a connected flag

* [android] introduce a setConnected method to manually set a connected flag

* extend connected state to ConnectivityReceiver

* extend connected state to ConnectivityReceiver
This commit is contained in:
Antonio Zugaldia
2016-10-11 09:17:07 -04:00
committed by GitHub
parent e3c79bfd34
commit 4cc7e7de4f
3 changed files with 37 additions and 1 deletions

View File

@@ -18,6 +18,8 @@ public class MapboxAccountManager {
private final String accessToken;
private final Context applicationContext;
private Boolean connected = null;
/**
* MapboxAccountManager should NOT be instantiated directly.
* Use @see MapboxAccountManager#getInstance() instead.
@@ -89,13 +91,30 @@ public class MapboxAccountManager {
}
}
/**
* Manually sets the connectivity state of the app. This is useful for apps that control their
* own connectivity state and want to bypass any checks to the ConnectivityManager.
*
* @param connected flag to determine the connectivity state, true for connected, false for
* disconnected, null for ConnectivityManager to determine.
*/
public void setConnected(Boolean connected) {
// Connectivity state overridden by app
this.connected = connected;
}
/**
* Determines whether we have an Internet connection available. Please do not rely on this
* method in your apps, this method is used internally by the SDK.
*
* @return true if there is an Internet connection, false otherwise
*/
public boolean isConnected() {
public Boolean isConnected() {
if (connected != null) {
// Connectivity state overridden by app
return connected;
}
ConnectivityManager cm = (ConnectivityManager) applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
return (activeNetwork != null && activeNetwork.isConnected());

View File

@@ -9,6 +9,8 @@ import android.net.NetworkInfo;
import android.support.annotation.NonNull;
import android.util.Log;
import com.mapbox.mapboxsdk.MapboxAccountManager;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
@@ -80,6 +82,12 @@ public class ConnectivityReceiver extends BroadcastReceiver {
* @return true if connected
*/
public boolean isConnected(Context context) {
Boolean connected = MapboxAccountManager.getInstance().isConnected();
if (connected != null) {
// Connectivity state overridden by app
return connected;
}
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
return (activeNetwork != null && activeNetwork.isConnected());