Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discontinue old API used for Mapbox projects/classic, replace with Mapbox Studio styled raster tiles #1902

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions Android/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
<!-- You must insert your own Google Maps for Android API v2 key in here. -->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyA2oC6zsx-Ir4be8bWFWrAej7FcKdrWGdA"/>
android:value="AddYourOwnKeyFromGoogleCloudPlatformHere"/>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>
Expand Down Expand Up @@ -173,4 +173,4 @@

</application>

</manifest>
</manifest>
4 changes: 2 additions & 2 deletions Android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ dependencies {
//Charts and graph library
compile 'com.github.lecho:hellocharts-library:1.5.5@aar'

//Leak canary (https://github.com/square/leakcanary)
/*Leak canary (https://github.com/square/leakcanary)
debugCompile 'com.squareup.leakcanary:leakcanary-android:1.4-beta2'
releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.4-beta2'
testCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.4-beta2'
testCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.4-beta2' */
}

def versionPrefix = "Tower-v"
Expand Down
6 changes: 5 additions & 1 deletion Android/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -448,9 +448,12 @@
<string name="pref_title_mapbox_tile_provider">Mapbox Tile Provider</string>
<string name="pref_title_mapbox_map_download">Download Offline Map</string>
<string name="pref_summary_mapbox_map_download">Click to cache map for use while offline</string>
<string name="pref_title_mapbox_Userid">Mapbox UserId</string>
<string name="pref_hint_mapbox_Userid">Mapbox user id</string>
<string name="pref_summary_mapbox_Userid">Enter your mapbox user id</string>
<string name="pref_title_mapbox_id">Mapbox Id</string>
<string name="pref_hint_mapbox_id">Mapbox map id</string>
<string name="pref_summary_mapbox_id">Enter your mapbox map id</string>
<string name="pref_summary_mapbox_id">Enter your mapbox styled map id</string>
<string name="pref_title_mapbox_access_token">Mapbox Access Token</string>
<string name="pref_hint_mapbox_access_token">Mapbox access token</string>
<string name="pref_summary_mapbox_access_token">Enter your mapbox access token</string>
Expand All @@ -461,6 +464,7 @@
<string name="instructions_map_download_selection">Pan and zoom to adjust the map area to save</string>
<string name="instructions_tap_to_save_map">Tap to save the map</string>
<string name="label_map_saved">Map area saved!</string>
<string name="label_invalid_mapbox_userid">Invalid mapbox user id</string>
<string name="label_invalid_mapbox_id">Invalid mapbox id</string>
<string name="label_invalid_mapbox_access_token">Invalid mapbox access token</string>
<string name="alert_invalid_mapbox_credentials">Invalid mapbox credentials! Please update your mapbox settings.</string>
Expand Down
8 changes: 8 additions & 0 deletions Android/res/xml/preferences_google_maps.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@
android:key="pref_download_menu_option"
android:title="@string/pref_title_download_menu_option"/>

<EditTextPreference
android:gravity="center"
android:hint="@string/pref_hint_mapbox_Userid"
android:key="pref_mapbox_userid"
android:selectAllOnFocus="true"
android:summary="@string/pref_summary_mapbox_Userid"
android:title="@string/pref_title_mapbox_Userid"/>

<EditTextPreference
android:gravity="center"
android:hint="@string/pref_hint_mapbox_id"
Expand Down
5 changes: 3 additions & 2 deletions Android/src/org/droidplanner/android/DroidPlannerApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.o3dr.services.android.lib.drone.connection.ConnectionType;
import com.o3dr.services.android.lib.gcs.link.LinkConnectionStatus;
import com.o3dr.services.android.lib.model.AbstractCommandListener;
import com.squareup.leakcanary.LeakCanary;

import org.droidplanner.android.activities.helpers.BluetoothDevicesActivity;
import org.droidplanner.android.droneshare.UploaderService;
Expand Down Expand Up @@ -162,11 +161,12 @@ public void onCreate() {
lbm = LocalBroadcastManager.getInstance(context);
soundManager = new SoundManager(context);

initLoggingAndAnalytics();
/*initLoggingAndAnalytics();*/
initDronekit();
initDatabases();
}

/*
private void initLoggingAndAnalytics(){
//Init leak canary
LeakCanary.install(this);
Expand Down Expand Up @@ -195,6 +195,7 @@ public void uncaughtException(Thread thread, Throwable ex) {
Fabric.with(context, new Crashlytics());
}
}
*/

private void initDronekit(){
Context context = getApplicationContext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1233,11 +1233,13 @@ private void setupMapboxTileProvider(Context context, GoogleMap map){
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);

final GoogleMapPrefFragment.PrefManager prefManager = GoogleMapPrefFragment.PrefManager;
final String mapboxUserId = prefManager.getMapboxUserId(context);
final String mapboxId = prefManager.getMapboxId(context);
final String mapboxAccessToken = prefManager.getMapboxAccessToken(context);
final int maxZoomLevel = (int) map.getMaxZoomLevel();

if (!(tileProviderManager instanceof MapboxTileProviderManager)
|| !mapboxUserId.equals(((MapboxTileProviderManager) tileProviderManager).getMapboxUserId())
|| !mapboxId.equals(((MapboxTileProviderManager) tileProviderManager).getMapboxId())
|| !mapboxAccessToken.equals(((MapboxTileProviderManager) tileProviderManager).getMapboxAccessToken())) {

Expand All @@ -1247,7 +1249,7 @@ private void setupMapboxTileProvider(Context context, GoogleMap map){
onlineTileOverlay = null;
}

tileProviderManager = new MapboxTileProviderManager(context, mapboxId, mapboxAccessToken, maxZoomLevel);
tileProviderManager = new MapboxTileProviderManager(context, mapboxUserId, mapboxId, mapboxAccessToken, maxZoomLevel);
TileOverlayOptions options = new TileOverlayOptions()
.tileProvider(tileProviderManager.getOnlineTileProvider())
.zIndex(ONLINE_TILE_PROVIDER_Z_INDEX);
Expand Down Expand Up @@ -1275,7 +1277,7 @@ private void setupMapboxTileProvider(Context context, GoogleMap map){
@Override
protected Integer doInBackground(Void... params) {
final Context context = getContext();
return MapboxUtils.fetchReferenceTileUrl(context, mapboxId, mapboxAccessToken);
return MapboxUtils.fetchReferenceTileUrl(context, mapboxUserId, mapboxId, mapboxAccessToken);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class GoogleMapPrefFragment : MapProviderPreferences(), EditInputDialog.Listener

companion object PrefManager {

private val MAPBOX_USER_ID_DIALOG_TAG = "Mapbox user id dialog"
private val MAPBOX_ACCESS_TOKEN_DIALOG_TAG = "Mapbox access token dialog"
private val MAPBOX_ID_DIALOG_TAG = "Mapbox map credentials dialog"

Expand Down Expand Up @@ -50,6 +51,7 @@ class GoogleMapPrefFragment : MapProviderPreferences(), EditInputDialog.Listener
val PREF_DOWNLOAD_MENU_OPTION = "pref_download_menu_option"
val DEFAULT_DOWNLOAD_MENU_OPTION = false

val PREF_MAPBOX_USERID = "pref_mapbox_userid"
val PREF_MAPBOX_ID = "pref_mapbox_id"
val PREF_MAPBOX_ACCESS_TOKEN = "pref_mapbox_access_token"

Expand Down Expand Up @@ -119,6 +121,20 @@ class GoogleMapPrefFragment : MapProviderPreferences(), EditInputDialog.Listener
}
}

fun getMapboxUserId(context: Context?): String {
return if (context == null) "" else {
val sharedPref = PreferenceManager.getDefaultSharedPreferences(context)
sharedPref.getString(PREF_MAPBOX_USERID, "")
}
}

private fun setMapboxUserId(context: Context?, mapboxUserId: String?){
context?.let {
val sharedPref = PreferenceManager.getDefaultSharedPreferences(context)
sharedPref.edit().putString(PREF_MAPBOX_USERID, mapboxUserId).apply()
}
}

fun getMapboxId(context: Context?): String {
return if(context == null) "" else{
val sharedPref = PreferenceManager.getDefaultSharedPreferences(context)
Expand Down Expand Up @@ -174,11 +190,13 @@ class GoogleMapPrefFragment : MapProviderPreferences(), EditInputDialog.Listener
setupTileProvidersPreferences(sharedPref)
}

private fun isMapboxUserIdSet() = !TextUtils.isEmpty(getMapboxUserId(getContext()))

private fun isMapboxIdSet() = !TextUtils.isEmpty(getMapboxId(getContext()))

private fun isMapboxAccessTokenSet() = !TextUtils.isEmpty(getMapboxAccessToken(getContext()))

private fun areMapboxCredentialsSet() = isMapboxAccessTokenSet() && isMapboxIdSet()
private fun areMapboxCredentialsSet() = isMapboxAccessTokenSet() && isMapboxIdSet() && isMapboxUserIdSet()

private fun enableTileProvider(provider: String, persistPreference: Boolean){
val tileProviderPref = findPreference(PREF_TILE_PROVIDERS) as ListPreference?
Expand All @@ -203,6 +221,27 @@ class GoogleMapPrefFragment : MapProviderPreferences(), EditInputDialog.Listener
val context = getContext()

when (dialogTag) {
MAPBOX_USER_ID_DIALOG_TAG -> {
if (TextUtils.isEmpty(input)) {
Toast.makeText(context, R.string.label_invalid_mapbox_userid, Toast.LENGTH_LONG)
.show()
} else {
//Save the mapbox user id to preferences
updateMapboxUserId(input?.toString() ?: "", true)

//Check if the mapbox access token is set enable the mapbox tile
// provider
if (isMapboxAccessTokenSet()) {
enableTileProvider(tileProvidersPref, MAPBOX_TILE_PROVIDER, true)
}
else {
//Check if the mapbox access token is set
accessTokenDialog?.show(fragmentManager,
MAPBOX_ACCESS_TOKEN_DIALOG_TAG)
}
}
}

MAPBOX_ID_DIALOG_TAG -> {
if (TextUtils.isEmpty(input)) {
Toast.makeText(context, R.string.label_invalid_mapbox_id, Toast.LENGTH_LONG)
Expand Down Expand Up @@ -360,6 +399,23 @@ class GoogleMapPrefFragment : MapProviderPreferences(), EditInputDialog.Listener
downloadMenuPref.isChecked = sharedPref.getBoolean(PREF_DOWNLOAD_MENU_OPTION, DEFAULT_DOWNLOAD_MENU_OPTION)
}

//Setup mapbox user id
val mapboxUserIdPref = findPreference(PREF_MAPBOX_USERID)
if(mapboxUserIdPref != null) {
val mapboxUserId = sharedPref.getString(PREF_MAPBOX_USERID, null)
mapboxUserId?.let { mapboxUserIdPref.summary = mapboxUserId }
mapboxUserIdPref.setOnPreferenceChangeListener { preference, newValue ->
val newMapboxUserId = newValue.toString()
if(TextUtils.isEmpty(newMapboxUserId)){
Toast.makeText(context,"invalid_mapbox user id", Toast.LENGTH_LONG)
.show()
}

updateMapboxUserId(newMapboxUserId, false)
true
}
}

//Setup mapbox map id
val mapboxIdPref = findPreference(PREF_MAPBOX_ID)
if(mapboxIdPref != null) {
Expand Down Expand Up @@ -402,6 +458,23 @@ class GoogleMapPrefFragment : MapProviderPreferences(), EditInputDialog.Listener
}
}

private fun updateMapboxUserId(id: String, persist: Boolean){
val mapboxUserIdPref = findPreference(PREF_MAPBOX_USERID)
mapboxUserIdPref?.let {
val summary = if (TextUtils.isEmpty(id)) {
enableTileProvider(GOOGLE_TILE_PROVIDER, true)
getString(R.string.pref_hint_mapbox_Userid)
} else {
id
}

mapboxUserIdPref.summary = summary
}

if(persist)
setMapboxUserId(getContext(), id)
}

private fun updateMapboxId(id: String, persist: Boolean){
val mapboxIdPref = findPreference(PREF_MAPBOX_ID)
mapboxIdPref?.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ public class MapboxTileProvider extends UrlTileProvider {

private final static String TAG = MapboxTileProvider.class.getSimpleName();

private final String mapboxUserId;
private final String mapboxId;
private final String mapboxAccessToken;
private final int maxZoomLevel;

public MapboxTileProvider(String mapboxId, String mapboxAccessToken, int maxZoomLevel) {
public MapboxTileProvider(String mapboxUserId, String mapboxId, String mapboxAccessToken, int maxZoomLevel) {
super(MapboxUtils.TILE_WIDTH, MapboxUtils.TILE_HEIGHT);
this.mapboxUserId = mapboxUserId;
this.mapboxId = mapboxId;
this.mapboxAccessToken = mapboxAccessToken;
this.maxZoomLevel = maxZoomLevel;
Expand All @@ -28,7 +30,7 @@ public MapboxTileProvider(String mapboxId, String mapboxAccessToken, int maxZoom
@Override
public URL getTileUrl(int x, int y, int zoom) {
if (zoom <= maxZoomLevel) {
final String tileUrl = MapboxUtils.getMapTileURL(mapboxId, mapboxAccessToken, zoom, x, y);
final String tileUrl = MapboxUtils.getMapTileURL(mapboxUserId, mapboxId, mapboxAccessToken, zoom, x, y);
try {
return new URL(tileUrl);
} catch (MalformedURLException e) {
Expand All @@ -45,4 +47,8 @@ public String getMapboxAccessToken() {
public String getMapboxId() {
return mapboxId;
}

public String getMapboxUserId() {
return mapboxUserId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,24 @@ public class MapboxTileProviderManager extends TileProviderManager {
private final Handler handler = new Handler();

private final Context context;
private final String mapboxUserId;
private final String mapboxId;
private final String mapboxAccessToken;

public MapboxTileProviderManager(Context context, String mapboxId, String mapboxAccessToken, int maxZoomLevel) {
super(new MapboxTileProvider(mapboxId, mapboxAccessToken, maxZoomLevel),
new OfflineTileProvider(context, mapboxId, mapboxAccessToken, maxZoomLevel));
public MapboxTileProviderManager(Context context, String mapboxUserId, String mapboxId, String mapboxAccessToken, int maxZoomLevel) {
super(new MapboxTileProvider(mapboxUserId, mapboxId, mapboxAccessToken, maxZoomLevel),
new OfflineTileProvider(context, mapboxUserId, mapboxId, mapboxAccessToken, maxZoomLevel));

this.context = context;
this.mapboxUserId = mapboxUserId;
this.mapboxId = mapboxId;
this.mapboxAccessToken = mapboxAccessToken;
}

public String getMapboxUserId() {
return mapboxUserId;
}

public String getMapboxAccessToken() {
return mapboxAccessToken;
}
Expand All @@ -60,27 +66,27 @@ public String getMapboxId() {
@Override
public void downloadMapTiles(MapDownloader mapDownloader, DPMap.VisibleMapArea mapRegion, int
minimumZ, int maximumZ) {
beginDownloadingMapID(mapDownloader, this.mapboxId, this.mapboxAccessToken, mapRegion, minimumZ, maximumZ);
beginDownloadingMapID(mapDownloader, this.mapboxUserId, this.mapboxId, this.mapboxAccessToken, mapRegion, minimumZ, maximumZ);
}

private void beginDownloadingMapID(final MapDownloader mapDownloader, final String mapId, final String accessToken, DPMap.VisibleMapArea mapRegion, int
private void beginDownloadingMapID(final MapDownloader mapDownloader, final String userId, final String mapId, final String accessToken, DPMap.VisibleMapArea mapRegion, int
minimumZ, int maximumZ) {
beginDownloadingMapID(mapDownloader, mapId, accessToken, mapRegion, minimumZ, maximumZ, true, true);
beginDownloadingMapID(mapDownloader, userId, mapId, accessToken, mapRegion, minimumZ, maximumZ, true, true);
}

private void beginDownloadingMapID(final MapDownloader mapDownloader, final String mapId, final String accessToken, DPMap.VisibleMapArea mapRegion, int
private void beginDownloadingMapID(final MapDownloader mapDownloader, final String userId, final String mapId, final String accessToken, DPMap.VisibleMapArea mapRegion, int
minimumZ, int maximumZ, boolean includeMetadata,
boolean includeMarkers) {

final ArrayList<String> urls = new ArrayList<String>();
String dataName = "features.json"; // Only using API V4 for now

// Include URLs for the metadata and markers json if applicable
if (includeMetadata) {
if (false) {
urls.add(String.format(Locale.US, MapboxUtils.MAPBOX_BASE_URL_V4 + "%s.json?secure&access_token=%s",
mapId, accessToken));
}
if (includeMarkers) {
if (false) {
urls.add(String.format(Locale.US, MapboxUtils.MAPBOX_BASE_URL_V4 + "%s/%s?access_token=%s", mapId,
dataName, accessToken));
}
Expand Down Expand Up @@ -117,7 +123,7 @@ private void beginDownloadingMapID(final MapDownloader mapDownloader, final Stri
maxY = Double.valueOf(Math.floor((1.0 - (Math.log(Math.tan(minLat * Math.PI / 180.0) + 1.0 / Math.cos(minLat * Math.PI / 180.0)) / Math.PI)) / 2.0 * tilesPerSide)).intValue();
for (int x = minX; x <= maxX; x++) {
for (int y = minY; y <= maxY; y++) {
urls.add(MapboxUtils.getMapTileURL(mapId, accessToken, zoom, x, y));
urls.add(MapboxUtils.getMapTileURL(userId, mapId, accessToken, zoom, x, y));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ private MapboxUtils(){}

public static final String MAPBOX_BASE_URL_V4 = "https://a.tiles.mapbox.com/v4/";

public static String getMapTileURL(String mapID, String accessToken, int zoom, int x, int y) {
return String.format(Locale.US, "https://a.tiles.mapbox.com/v4/%s/%d/%d/%d%s.%s?access_token=%s",
mapID, zoom, x, y, "@2x", "png", accessToken);
public static String getMapTileURL(String userId, String mapID, String accessToken, int zoom, int x, int y) {
return String.format(Locale.US, "https://api.mapbox.com/styles/v1/%s/%s/tiles/%d/%d/%d%s?access_token=%s",
userId, mapID, zoom, x, y, "@2x", accessToken);
}

public static int fetchReferenceTileUrl(Context context, String mapId, String accessToken){
public static int fetchReferenceTileUrl(Context context, String userId, String mapId, String accessToken){
if(!NetworkUtils.isNetworkAvailable(context)){
Timber.d("Network is not available. Aborting reference tile fetching.");
return -1;
}

final String referenceUrl = getMapTileURL(mapId, accessToken, 0, 0, 0);
final String referenceUrl = getMapTileURL(userId, mapId, accessToken, 0, 0, 0);

HttpURLConnection conn = null;
try{
Expand Down
Loading