Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Introduce a setConnected method to manually set a connected flag #6618

Merged
merged 3 commits into from
Oct 11, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -89,13 +91,30 @@ public static void validateAccessToken(String accessToken) throws InvalidAccessT
}
}

/**
* 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() {
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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
import android.widget.ProgressBar;
import android.widget.Toast;

import com.mapbox.mapboxsdk.MapboxAccountManager;
import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
import com.mapbox.mapboxsdk.constants.MapboxConstants;
import com.mapbox.mapboxsdk.constants.Style;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.geometry.LatLngBounds;
Expand Down Expand Up @@ -73,6 +75,13 @@ protected void onCreate(Bundle savedInstanceState) {
actionBar.setDisplayShowHomeEnabled(true);
}

// You can use MapboxAccountManager.setConnected(Boolean) to manually set the connectivity
// state of your app. This will override any checks performed via the ConnectivityManager.
//MapboxAccountManager.getInstance().setConnected(false);
boolean connected = MapboxAccountManager.getInstance().isConnected();
Log.d(LOG_TAG, String.format(MapboxConstants.MAPBOX_LOCALE,
"MapboxAccountManager is connected: %b", connected));

// Set up map
mapView = (MapView) findViewById(R.id.mapView);
mapView.setStyleUrl(Style.MAPBOX_STREETS);
Expand Down