[android] MapStrictMode implementation

This commit is contained in:
Łukasz Paczos
2018-08-29 18:50:01 +02:00
parent e4bcd1abed
commit 7227b4a7ec
16 changed files with 136 additions and 10 deletions

View File

@@ -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);
}
}

View File

@@ -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));
}
}
}

View File

@@ -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));
}
}

View File

@@ -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);
}
}

View File

@@ -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);

View File

@@ -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 "";
}
}

View File

@@ -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;
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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;
}
}

View File

@@ -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);
}
}
}

View File

@@ -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;
}

View File

@@ -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();

View File

@@ -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);
}
}

View File

@@ -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;
}
}