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

[android] MapStrictMode implementation [frappe-backport] #12817

Merged
merged 1 commit into from
Sep 5, 2018
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ public static void load() {
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);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ private static void initializeTelemetry() {
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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import android.util.DisplayMetrics;
import android.view.WindowManager;

import com.mapbox.mapboxsdk.MapStrictMode;
import com.mapbox.mapboxsdk.R;
import com.mapbox.mapboxsdk.exceptions.TooManyIconsException;

Expand Down Expand Up @@ -148,6 +149,7 @@ public Icon fromAsset(@NonNull String assetName) {
try {
is = context.getAssets().open(assetName);
} catch (IOException ioException) {
MapStrictMode.strictModeViolation(ioException);
return null;
}
return fromInputStream(is);
Expand Down Expand Up @@ -178,6 +180,7 @@ public Icon fromFile(@NonNull String fileName) {
try {
is = context.openFileInput(fileName);
} catch (FileNotFoundException fileNotFoundException) {
MapStrictMode.strictModeViolation(fileNotFoundException);
return null;
}
return fromInputStream(is);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
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 {
Expand Down Expand Up @@ -30,6 +32,7 @@ private static String getIdentifier(@NonNull Context context) {
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 "";
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

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;

Expand Down Expand Up @@ -42,7 +44,9 @@ private static byte[] loadFile(AssetManager assets, String path) {
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
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;
Expand Down Expand Up @@ -151,6 +153,7 @@ private void showWebPage(@NonNull String url) {
} 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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
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;
Expand All @@ -45,8 +47,6 @@
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;
Expand All @@ -55,6 +55,9 @@
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;

Expand Down Expand Up @@ -1271,4 +1274,14 @@ public void onClick(View v) {
}
}
}

/**
* 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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;
Expand Down Expand Up @@ -297,7 +298,9 @@ public <T extends Layer> T getLayerAs(@NonNull String layerId) {
// 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;
}
}
Expand Down Expand Up @@ -409,7 +412,9 @@ public <T extends Source> T getSourceAs(@NonNull String sourceId) {
// 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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
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;
Expand Down Expand Up @@ -113,10 +115,12 @@ private boolean checkState(String callingMethod) {

// 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;
}
Expand Down Expand Up @@ -907,6 +911,7 @@ protected void onMapChanged(int rawChange) {
onMapChangedListener.onMapChanged(rawChange);
} catch (RuntimeException err) {
Logger.e(TAG, "Exception in MapView.OnMapChangedListener", err);
MapStrictMode.strictModeViolation(err);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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;
Expand Down Expand Up @@ -141,6 +142,7 @@ public static boolean updateSessionIdRotationInterval(SessionInterval interval)
}
} catch (Exception exception) {
Logger.e(TAG, "Exception occurred when updating session id rotation interval", exception);
MapStrictMode.strictModeViolation(exception);
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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;
Expand Down Expand Up @@ -117,6 +118,7 @@ public void run() {
}
} catch (Exception exception) {
Logger.e(TAG, "Failed to delete old ambient cache database: ", exception);
MapStrictMode.strictModeViolation(exception);
}
}
}).start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
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;
Expand Down Expand Up @@ -88,8 +90,10 @@ public static String getCachePath(Context context) {
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;
Expand All @@ -99,6 +103,7 @@ public static String getCachePath(Context context) {
cachePath = context.getExternalFilesDir(null).getAbsolutePath();
} catch (NullPointerException exception) {
Logger.e(TAG, "Failed to obtain the external storage path: ", exception);
MapStrictMode.strictModeViolation(exception);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
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;
Expand Down Expand Up @@ -108,6 +109,7 @@ public Integer getColorInt() {
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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
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;
Expand Down Expand Up @@ -78,6 +80,8 @@ private void initializeMapbox() {
throw new IllegalStateException("Telemetry was unavailable during test application start.");
}
telemetry.setDebugLoggingEnabled(true);

MapStrictMode.setStrictModeEnabled(true);
}

private static void validateAccessToken(String accessToken) {
Expand Down