[android] #2421 - Checking for TelemetryService configuration in AndroidManifest.xml

This commit is contained in:
Brad Leege
2016-01-29 15:16:27 -06:00
parent cf107d85bf
commit 9ce4c8bafc
3 changed files with 48 additions and 0 deletions

View File

@@ -0,0 +1,18 @@
package com.mapbox.mapboxsdk.exceptions;
import android.os.Bundle;
import com.mapbox.mapboxsdk.maps.MapView;
/**
* A {@code TelemetryServiceNotConfiguredException} is thrown by {@link MapView} when it checks and finds that
* TelemetryService has not been configured in the app's AndroidManifest.xml {@link MapView#onCreate(Bundle)}
*
* @see MapView#onCreate(Bundle)
*/
public class TelemetryServiceNotConfiguredException extends RuntimeException {
public TelemetryServiceNotConfiguredException() {
super("Using Mapbox Android SDK requires configuring TelemetryService. See the INSTALL.md");
}
}

View File

@@ -10,7 +10,9 @@ import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ServiceInfo;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
@@ -73,6 +75,7 @@ import com.mapbox.mapboxsdk.constants.MyLocationTracking;
import com.mapbox.mapboxsdk.constants.Style;
import com.mapbox.mapboxsdk.exceptions.IconBitmapChangedException;
import com.mapbox.mapboxsdk.exceptions.InvalidAccessTokenException;
import com.mapbox.mapboxsdk.exceptions.TelemetryServiceNotConfiguredException;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.geometry.LatLngBounds;
import com.mapbox.mapboxsdk.layers.CustomLayer;
@@ -366,6 +369,9 @@ public class MapView extends FrameLayout {
// Force a check for an access token
validateAccessToken(getAccessToken());
// Force a check for Telemetry
validateTelemetryServiceConfigured();
// Initialize EGL
mNativeMapView.initializeDisplay();
mNativeMapView.initializeContext();
@@ -866,6 +872,28 @@ public class MapView extends FrameLayout {
}
}
// Checks that TelemetryService has been configured by developer
private void validateTelemetryServiceConfigured() {
try {
// Check Implementing app's AndroidManifest.xml
PackageInfo packageInfo = getContext().getPackageManager().getPackageInfo(getContext().getPackageName(), PackageManager.GET_SERVICES);
if (packageInfo.services != null) {
for (ServiceInfo service : packageInfo.services) {
if (TextUtils.equals("com.mapbox.mapboxsdk.telemetry.TelemetryService", service.name)) {
return;
}
}
}
} catch (Exception e) {
Log.w(TAG, "Error checking for Telemetry Service Config: " + e);
}
throw new TelemetryServiceNotConfiguredException();
}
/**
* <p>
* Sets the current Mapbox access token used to load map styles and tiles.