diff options
author | Łukasz Paczos <lukasz.paczos@mapbox.com> | 2018-08-29 18:50:01 +0200 |
---|---|---|
committer | Łukasz Paczos <lukasz.paczos@mapbox.com> | 2018-09-04 19:46:51 +0200 |
commit | 7227b4a7ec078d5eced98473c5a7181e5564b471 (patch) | |
tree | bc6ab3472f9728e1557828e4d83b998a9db2f74a /platform | |
parent | e4bcd1abed607117b21fa518782a694873d0686b (diff) | |
download | qtlocation-mapboxgl-7227b4a7ec078d5eced98473c5a7181e5564b471.tar.gz |
[android] MapStrictMode implementation
Diffstat (limited to 'platform')
16 files changed, 136 insertions, 10 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/LibraryLoader.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/LibraryLoader.java index ea3d053d4b..fb0904f051 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/LibraryLoader.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/LibraryLoader.java @@ -41,7 +41,9 @@ public abstract class LibraryLoader { try { loader.load("mapbox-gl"); } catch (UnsatisfiedLinkError error) { - Logger.e(TAG, "Failed to load native shared library.", error); + String message = "Failed to load native shared library."; + Logger.e(TAG, message, error); + MapStrictMode.strictModeViolation(message, error); } } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/MapStrictMode.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/MapStrictMode.java new file mode 100644 index 0000000000..aed392876b --- /dev/null +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/MapStrictMode.java @@ -0,0 +1,64 @@ +package com.mapbox.mapboxsdk; + +/** + * Using this class you can enable a strict mode that will throw the {@link MapStrictModeException} + * whenever the map would fail silently otherwise. + */ +public class MapStrictMode { + private static volatile boolean strictModeEnabled; + + /** + * Set the strict mode that will throw the {@link MapStrictModeException} + * whenever the map would fail silently otherwise. + * + * @param strictModeEnabled true to enable the strict mode, false otherwise + */ + public static synchronized void setStrictModeEnabled(boolean strictModeEnabled) { + MapStrictMode.strictModeEnabled = strictModeEnabled; + } + + /** + * Internal use. Called whenever the strict mode violation occurs. + */ + public static void strictModeViolation(String message) { + if (strictModeEnabled) { + throw new MapStrictModeException(message); + } + } + + /** + * Internal use. Called whenever the strict mode violation occurs. + */ + public static void strictModeViolation(String message, Exception exception) { + if (strictModeEnabled) { + throw new MapStrictModeException(String.format("%s - %s", message, exception)); + } + } + + /** + * Internal use. Called whenever the strict mode violation occurs. + */ + public static void strictModeViolation(String message, Error error) { + if (strictModeEnabled) { + throw new MapStrictModeException(String.format("%s - %s", message, error)); + } + } + + /** + * Internal use. Called whenever the strict mode violation occurs. + */ + public static void strictModeViolation(Error error) { + if (strictModeEnabled) { + throw new MapStrictModeException(String.format("%s", error)); + } + } + + /** + * Internal use. Called whenever the strict mode violation occurs. + */ + public static void strictModeViolation(Exception exception) { + if (strictModeEnabled) { + throw new MapStrictModeException(String.format("%s", exception)); + } + } +} diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/MapStrictModeException.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/MapStrictModeException.java new file mode 100644 index 0000000000..5bfddabf9a --- /dev/null +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/MapStrictModeException.java @@ -0,0 +1,7 @@ +package com.mapbox.mapboxsdk; + +class MapStrictModeException extends RuntimeException { + MapStrictModeException(String message) { + super(String.format("Map detected an error that would fail silently otherwise: %s", message)); + } +} diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/Mapbox.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/Mapbox.java index ba632ebb20..3959d2cf3c 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/Mapbox.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/Mapbox.java @@ -127,7 +127,9 @@ public final class Mapbox { try { INSTANCE.telemetry = getModuleProvider().obtainTelemetry(); } catch (Exception exception) { - Logger.e(TAG, "Error occured while initializing telemetry", exception); + String message = "Error occurred while initializing telemetry"; + Logger.e(TAG, message, exception); + MapStrictMode.strictModeViolation(message, exception); } } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/IconFactory.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/IconFactory.java index 3c9cb31211..61d4a877c2 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/IconFactory.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/IconFactory.java @@ -13,6 +13,7 @@ import android.support.v4.content.ContextCompat; import android.util.DisplayMetrics; import android.view.WindowManager; +import com.mapbox.mapboxsdk.MapStrictMode; import com.mapbox.mapboxsdk.R; import com.mapbox.mapboxsdk.exceptions.TooManyIconsException; @@ -148,6 +149,7 @@ public final class IconFactory { try { is = context.getAssets().open(assetName); } catch (IOException ioException) { + MapStrictMode.strictModeViolation(ioException); return null; } return fromInputStream(is); @@ -178,6 +180,7 @@ public final class IconFactory { try { is = context.openFileInput(fileName); } catch (FileNotFoundException fileNotFoundException) { + MapStrictMode.strictModeViolation(fileNotFoundException); return null; } return fromInputStream(is); diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/HttpIdentifier.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/HttpIdentifier.java index d50be55878..5f4f85ea78 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/HttpIdentifier.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/HttpIdentifier.java @@ -3,6 +3,8 @@ package com.mapbox.mapboxsdk.http; import android.content.Context; import android.content.pm.PackageInfo; import android.support.annotation.NonNull; + +import com.mapbox.mapboxsdk.MapStrictMode; import com.mapbox.mapboxsdk.Mapbox; public class HttpIdentifier { @@ -30,6 +32,7 @@ public class HttpIdentifier { PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0); return String.format("%s/%s (%s)", context.getPackageName(), packageInfo.versionName, packageInfo.versionCode); } catch (Exception exception) { + MapStrictMode.strictModeViolation(exception); return ""; } } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/LocalRequestTask.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/LocalRequestTask.java index b8ed407417..7884033de7 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/LocalRequestTask.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/LocalRequestTask.java @@ -2,6 +2,8 @@ package com.mapbox.mapboxsdk.http; import android.content.res.AssetManager; import android.os.AsyncTask; + +import com.mapbox.mapboxsdk.MapStrictMode; import com.mapbox.mapboxsdk.Mapbox; import com.mapbox.mapboxsdk.log.Logger; @@ -42,7 +44,9 @@ class LocalRequestTask extends AsyncTask<String, Void, byte[]> { buffer = new byte[size]; input.read(buffer); } catch (IOException exception) { - Logger.e(TAG, "Load file failed", exception); + String message = "Load file failed"; + Logger.e(TAG, message, exception); + MapStrictMode.strictModeViolation(message, exception); } return buffer; } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/AttributionDialogManager.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/AttributionDialogManager.java index 8510806e79..5cba2109e2 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/AttributionDialogManager.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/AttributionDialogManager.java @@ -11,6 +11,8 @@ import android.support.annotation.NonNull; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Toast; + +import com.mapbox.mapboxsdk.MapStrictMode; import com.mapbox.mapboxsdk.Mapbox; import com.mapbox.mapboxsdk.R; import com.mapbox.mapboxsdk.attribution.Attribution; @@ -151,6 +153,7 @@ public class AttributionDialogManager implements View.OnClickListener, DialogInt } catch (ActivityNotFoundException exception) { // explicitly handling if the device hasn't have a web browser installed. #8899 Toast.makeText(context, R.string.mapbox_attributionErrorNoBrowser, Toast.LENGTH_LONG).show(); + MapStrictMode.strictModeViolation(exception); } } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java index e10eec7ea7..6fcc2c199a 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java @@ -24,7 +24,9 @@ import android.view.ViewTreeObserver; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.ZoomButtonsController; + import com.mapbox.android.gestures.AndroidGesturesManager; +import com.mapbox.mapboxsdk.MapStrictMode; import com.mapbox.mapboxsdk.Mapbox; import com.mapbox.mapboxsdk.R; import com.mapbox.mapboxsdk.annotations.Annotation; @@ -45,8 +47,6 @@ import com.mapbox.mapboxsdk.offline.OfflineTilePyramidRegionDefinition; import com.mapbox.mapboxsdk.storage.FileSource; import com.mapbox.mapboxsdk.utils.BitmapUtils; -import javax.microedition.khronos.egl.EGLConfig; -import javax.microedition.khronos.opengles.GL10; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.ref.WeakReference; @@ -55,6 +55,9 @@ import java.util.Iterator; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; +import javax.microedition.khronos.egl.EGLConfig; +import javax.microedition.khronos.opengles.GL10; + import static com.mapbox.mapboxsdk.maps.widgets.CompassView.TIME_MAP_NORTH_ANIMATION; import static com.mapbox.mapboxsdk.maps.widgets.CompassView.TIME_WAIT_IDLE; @@ -1271,4 +1274,14 @@ public class MapView extends FrameLayout implements NativeMapView.ViewCallback { } } } + + /** + * Sets the strict mode that will throw the {@link com.mapbox.mapboxsdk.MapStrictModeException} + * whenever the map would fail silently otherwise. + * + * @param strictModeEnabled true to enable the strict mode, false otherwise + */ + public static void setMapStrictModeEnabled(boolean strictModeEnabled) { + MapStrictMode.setStrictModeEnabled(strictModeEnabled); + } } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java index 1d823455b5..08369bb566 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java @@ -23,6 +23,7 @@ import com.mapbox.android.gestures.ShoveGestureDetector; import com.mapbox.android.gestures.StandardScaleGestureDetector; import com.mapbox.geojson.Feature; import com.mapbox.geojson.Geometry; +import com.mapbox.mapboxsdk.MapStrictMode; import com.mapbox.mapboxsdk.annotations.Annotation; import com.mapbox.mapboxsdk.annotations.BaseMarkerOptions; import com.mapbox.mapboxsdk.annotations.BaseMarkerViewOptions; @@ -297,7 +298,9 @@ public final class MapboxMap { // noinspection unchecked return (T) nativeMapView.getLayer(layerId); } catch (ClassCastException exception) { - Logger.e(TAG, String.format("Layer: %s is a different type: ", layerId), exception); + String message = String.format("Layer: %s is a different type: ", layerId); + Logger.e(TAG, message, exception); + MapStrictMode.strictModeViolation(message, exception); return null; } } @@ -409,7 +412,9 @@ public final class MapboxMap { // noinspection unchecked return (T) nativeMapView.getSource(sourceId); } catch (ClassCastException exception) { - Logger.e(TAG, String.format("Source: %s is a different type: ", sourceId), exception); + String message = String.format("Source: %s is a different type: ", sourceId); + Logger.e(TAG, message, exception); + MapStrictMode.strictModeViolation(message, exception); return null; } } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/NativeMapView.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/NativeMapView.java index d427032b01..75359f7f44 100755 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/NativeMapView.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/NativeMapView.java @@ -12,9 +12,11 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.text.TextUtils; import android.util.DisplayMetrics; + import com.mapbox.geojson.Feature; import com.mapbox.geojson.Geometry; import com.mapbox.mapboxsdk.LibraryLoader; +import com.mapbox.mapboxsdk.MapStrictMode; import com.mapbox.mapboxsdk.annotations.Icon; import com.mapbox.mapboxsdk.annotations.Marker; import com.mapbox.mapboxsdk.annotations.Polygon; @@ -113,10 +115,12 @@ final class NativeMapView { // validate if map has already been destroyed if (destroyed && !TextUtils.isEmpty(callingMethod)) { - Logger.e(TAG, String.format( + String message = String.format( "You're calling `%s` after the `MapView` was destroyed, were you invoking it after `onDestroy()`?", - callingMethod) - ); + callingMethod); + Logger.e(TAG, message); + + MapStrictMode.strictModeViolation(message); } return destroyed; } @@ -907,6 +911,7 @@ final class NativeMapView { onMapChangedListener.onMapChanged(rawChange); } catch (RuntimeException err) { Logger.e(TAG, "Exception in MapView.OnMapChangedListener", err); + MapStrictMode.strictModeViolation(err); } } } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/module/telemetry/TelemetryImpl.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/module/telemetry/TelemetryImpl.java index 4a0b425a86..16a99a3272 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/module/telemetry/TelemetryImpl.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/module/telemetry/TelemetryImpl.java @@ -9,6 +9,7 @@ import com.mapbox.android.telemetry.MapboxTelemetry; import com.mapbox.android.telemetry.SessionInterval; import com.mapbox.android.telemetry.TelemetryEnabler; import com.mapbox.mapboxsdk.BuildConfig; +import com.mapbox.mapboxsdk.MapStrictMode; import com.mapbox.mapboxsdk.Mapbox; import com.mapbox.mapboxsdk.log.Logger; import com.mapbox.mapboxsdk.maps.TelemetryDefinition; @@ -141,6 +142,7 @@ public class TelemetryImpl implements TelemetryDefinition { } } catch (Exception exception) { Logger.e(TAG, "Exception occurred when updating session id rotation interval", exception); + MapStrictMode.strictModeViolation(exception); } return false; } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/offline/OfflineManager.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/offline/OfflineManager.java index b91c4e6b36..08b58fa796 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/offline/OfflineManager.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/offline/OfflineManager.java @@ -7,6 +7,7 @@ import android.os.Looper; import android.support.annotation.Keep; import android.support.annotation.NonNull; import com.mapbox.mapboxsdk.LibraryLoader; +import com.mapbox.mapboxsdk.MapStrictMode; import com.mapbox.mapboxsdk.R; import com.mapbox.mapboxsdk.geometry.LatLngBounds; import com.mapbox.mapboxsdk.log.Logger; @@ -117,6 +118,7 @@ public class OfflineManager { } } catch (Exception exception) { Logger.e(TAG, "Failed to delete old ambient cache database: ", exception); + MapStrictMode.strictModeViolation(exception); } } }).start(); diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/storage/FileSource.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/storage/FileSource.java index 590a9f2862..c242ef3699 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/storage/FileSource.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/storage/FileSource.java @@ -9,6 +9,8 @@ import android.os.Environment; import android.support.annotation.Keep; import android.support.annotation.NonNull; import android.support.annotation.UiThread; + +import com.mapbox.mapboxsdk.MapStrictMode; import com.mapbox.mapboxsdk.Mapbox; import com.mapbox.mapboxsdk.constants.MapboxConstants; import com.mapbox.mapboxsdk.utils.ThreadUtils; @@ -88,8 +90,10 @@ public class FileSource { MapboxConstants.DEFAULT_SET_STORAGE_EXTERNAL); } catch (PackageManager.NameNotFoundException exception) { Logger.e(TAG, "Failed to read the package metadata: ", exception); + MapStrictMode.strictModeViolation(exception); } catch (Exception exception) { Logger.e(TAG, "Failed to read the storage key: ", exception); + MapStrictMode.strictModeViolation(exception); } String cachePath = null; @@ -99,6 +103,7 @@ public class FileSource { cachePath = context.getExternalFilesDir(null).getAbsolutePath(); } catch (NullPointerException exception) { Logger.e(TAG, "Failed to obtain the external storage path: ", exception); + MapStrictMode.strictModeViolation(exception); } } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/PropertyValue.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/PropertyValue.java index fe6d510a53..848165f00f 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/PropertyValue.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/PropertyValue.java @@ -4,6 +4,7 @@ import android.support.annotation.ColorInt; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import com.google.gson.JsonArray; +import com.mapbox.mapboxsdk.MapStrictMode; import com.mapbox.mapboxsdk.exceptions.ConversionException; import com.mapbox.mapboxsdk.log.Logger; import com.mapbox.mapboxsdk.style.expressions.Expression; @@ -108,6 +109,7 @@ public class PropertyValue<T> { return ColorUtils.rgbaToColor((String) value); } catch (ConversionException ex) { Logger.e(TAG, String.format("%s could not be converted to a Color int: %s", name, ex.getMessage())); + MapStrictMode.strictModeViolation(ex); return null; } } diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/MapboxApplication.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/MapboxApplication.java index d95fcf745a..9ade97f91e 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/MapboxApplication.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/MapboxApplication.java @@ -3,6 +3,8 @@ package com.mapbox.mapboxsdk.testapp; import android.app.Application; import android.os.StrictMode; import android.text.TextUtils; + +import com.mapbox.mapboxsdk.MapStrictMode; import com.mapbox.mapboxsdk.Mapbox; import com.mapbox.mapboxsdk.log.Logger; import com.mapbox.mapboxsdk.maps.TelemetryDefinition; @@ -78,6 +80,8 @@ public class MapboxApplication extends Application { throw new IllegalStateException("Telemetry was unavailable during test application start."); } telemetry.setDebugLoggingEnabled(true); + + MapStrictMode.setStrictModeEnabled(true); } private static void validateAccessToken(String accessToken) { |