[android] #3304 - refactor SpriteFactory to IconFactory, changed all the sprite names to icon for convention
This commit is contained in:
@@ -2,11 +2,11 @@ package com.mapbox.mapboxsdk.annotations;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
|
||||
public final class Sprite {
|
||||
public final class Icon {
|
||||
private Bitmap mBitmap;
|
||||
private String mId;
|
||||
|
||||
Sprite(String id, Bitmap bitmap) {
|
||||
Icon(String id, Bitmap bitmap) {
|
||||
mId = id;
|
||||
mBitmap = bitmap;
|
||||
}
|
||||
@@ -24,10 +24,10 @@ public final class Sprite {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
Sprite sprite = (Sprite) o;
|
||||
Icon icon = (Icon) o;
|
||||
|
||||
if (!mBitmap.equals(sprite.mBitmap)) return false;
|
||||
return mId.equals(sprite.mId);
|
||||
if (!mBitmap.equals(icon.mBitmap)) return false;
|
||||
return mId.equals(icon.mId);
|
||||
|
||||
}
|
||||
|
||||
@@ -15,32 +15,32 @@ import android.util.DisplayMetrics;
|
||||
import android.view.WindowManager;
|
||||
|
||||
import com.mapbox.mapboxsdk.R;
|
||||
import com.mapbox.mapboxsdk.exceptions.TooManySpritesException;
|
||||
import com.mapbox.mapboxsdk.exceptions.TooManyIconsException;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
public final class SpriteFactory {
|
||||
public final class IconFactory {
|
||||
|
||||
private static final String SPRITE_ID_PREFIX = "com.mapbox.sprites.sprite_";
|
||||
private static final String ICON_ID_PREFIX = "com.mapbox.icons.icon_";
|
||||
|
||||
private Context mContext;
|
||||
private static SpriteFactory sInstance;
|
||||
private Sprite mDefaultMarker;
|
||||
private static IconFactory sInstance;
|
||||
private Icon mDefaultMarker;
|
||||
private BitmapFactory.Options mOptions;
|
||||
|
||||
private int mNextId = 0;
|
||||
|
||||
public static synchronized SpriteFactory getInstance(@NonNull Context context) {
|
||||
public static synchronized IconFactory getInstance(@NonNull Context context) {
|
||||
if (sInstance == null) {
|
||||
sInstance = new SpriteFactory(context.getApplicationContext());
|
||||
sInstance = new IconFactory(context.getApplicationContext());
|
||||
}
|
||||
return sInstance;
|
||||
}
|
||||
|
||||
private SpriteFactory(@NonNull Context context) {
|
||||
private IconFactory(@NonNull Context context) {
|
||||
mContext = context;
|
||||
DisplayMetrics realMetrics = null;
|
||||
DisplayMetrics metrics = new DisplayMetrics();
|
||||
@@ -61,21 +61,21 @@ public final class SpriteFactory {
|
||||
}
|
||||
}
|
||||
|
||||
public Sprite fromBitmap(@NonNull Bitmap bitmap) {
|
||||
public Icon fromBitmap(@NonNull Bitmap bitmap) {
|
||||
if (mNextId < 0) {
|
||||
throw new TooManySpritesException();
|
||||
throw new TooManyIconsException();
|
||||
}
|
||||
String id = SPRITE_ID_PREFIX + ++mNextId;
|
||||
return new Sprite(id, bitmap);
|
||||
String id = ICON_ID_PREFIX + ++mNextId;
|
||||
return new Icon(id, bitmap);
|
||||
}
|
||||
|
||||
public Sprite fromDrawable(@NonNull Drawable drawable) {
|
||||
public Icon fromDrawable(@NonNull Drawable drawable) {
|
||||
int width = drawable.getIntrinsicWidth();
|
||||
int height = drawable.getIntrinsicHeight();
|
||||
return fromDrawable(drawable, width, height);
|
||||
}
|
||||
|
||||
public Sprite fromDrawable(@NonNull Drawable drawable, int width, int height) {
|
||||
public Icon fromDrawable(@NonNull Drawable drawable, int width, int height) {
|
||||
if ((width < 0) || (height < 0)) {
|
||||
return null;
|
||||
}
|
||||
@@ -90,7 +90,7 @@ public final class SpriteFactory {
|
||||
return fromBitmap(bitmap);
|
||||
}
|
||||
|
||||
public Sprite fromResource(@DrawableRes int resourceId) {
|
||||
public Icon fromResource(@DrawableRes int resourceId) {
|
||||
Drawable drawable = ContextCompat.getDrawable(mContext, resourceId);
|
||||
Bitmap bitmap;
|
||||
if (drawable instanceof BitmapDrawable) {
|
||||
@@ -110,19 +110,19 @@ public final class SpriteFactory {
|
||||
return fromBitmap(bitmap);
|
||||
}
|
||||
|
||||
public Sprite defaultMarker() {
|
||||
public Icon defaultMarker() {
|
||||
if (mDefaultMarker == null) {
|
||||
mDefaultMarker = fromResource(R.drawable.default_marker);
|
||||
}
|
||||
return mDefaultMarker;
|
||||
}
|
||||
|
||||
private Sprite fromInputStream(@NonNull InputStream is) {
|
||||
private Icon fromInputStream(@NonNull InputStream is) {
|
||||
Bitmap bitmap = BitmapFactory.decodeStream(is, null, mOptions);
|
||||
return fromBitmap(bitmap);
|
||||
}
|
||||
|
||||
public Sprite fromAsset(@NonNull String assetName) {
|
||||
public Icon fromAsset(@NonNull String assetName) {
|
||||
InputStream is;
|
||||
try {
|
||||
is = mContext.getAssets().open(assetName);
|
||||
@@ -132,12 +132,12 @@ public final class SpriteFactory {
|
||||
return fromInputStream(is);
|
||||
}
|
||||
|
||||
public Sprite fromPath(@NonNull String absolutePath) {
|
||||
public Icon fromPath(@NonNull String absolutePath) {
|
||||
Bitmap bitmap = BitmapFactory.decodeFile(absolutePath, mOptions);
|
||||
return fromBitmap(bitmap);
|
||||
}
|
||||
|
||||
public Sprite fromFile(@NonNull String fileName) {
|
||||
public Icon fromFile(@NonNull String fileName) {
|
||||
FileInputStream is;
|
||||
try {
|
||||
is = mContext.openFileInput(fileName);
|
||||
@@ -16,7 +16,7 @@ public final class Marker extends Annotation {
|
||||
|
||||
private LatLng position;
|
||||
private String snippet;
|
||||
private Sprite icon;
|
||||
private Icon icon;
|
||||
private String title;
|
||||
private InfoWindow infoWindow = null;
|
||||
private boolean infoWindowShown = false;
|
||||
@@ -69,11 +69,11 @@ public final class Marker extends Annotation {
|
||||
/**
|
||||
* Do not use this method. Used internally by the SDK.
|
||||
*/
|
||||
public void setIcon(@Nullable Sprite icon) {
|
||||
public void setIcon(@Nullable Icon icon) {
|
||||
this.icon = icon;
|
||||
}
|
||||
|
||||
public Sprite getIcon() {
|
||||
public Icon getIcon() {
|
||||
return icon;
|
||||
}
|
||||
|
||||
|
||||
@@ -41,9 +41,9 @@ public final class MarkerOptions implements Parcelable {
|
||||
marker = new Marker();
|
||||
position((LatLng) in.readParcelable(LatLng.class.getClassLoader()));
|
||||
snippet(in.readString());
|
||||
String spriteId = in.readString();
|
||||
Bitmap spriteBitmap = in.readParcelable(Bitmap.class.getClassLoader());
|
||||
Sprite icon = new Sprite(spriteId, spriteBitmap);
|
||||
String iconId = in.readString();
|
||||
Bitmap iconBitmap = in.readParcelable(Bitmap.class.getClassLoader());
|
||||
Icon icon = new Icon(iconId, iconBitmap);
|
||||
icon(icon);
|
||||
title(in.readString());
|
||||
}
|
||||
@@ -87,7 +87,7 @@ public final class MarkerOptions implements Parcelable {
|
||||
return marker.getTitle();
|
||||
}
|
||||
|
||||
public Sprite getIcon() {
|
||||
public Icon getIcon() {
|
||||
return marker.getIcon();
|
||||
}
|
||||
|
||||
@@ -101,7 +101,7 @@ public final class MarkerOptions implements Parcelable {
|
||||
return this;
|
||||
}
|
||||
|
||||
public MarkerOptions icon(@Nullable Sprite icon) {
|
||||
public MarkerOptions icon(@Nullable Icon icon) {
|
||||
marker.setIcon(icon);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.mapbox.mapboxsdk.exceptions;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
|
||||
import com.mapbox.mapboxsdk.annotations.Icon;
|
||||
import com.mapbox.mapboxsdk.annotations.Marker;
|
||||
import com.mapbox.mapboxsdk.views.MapView;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* A {@code IconBitmapChangedException} is thrown by {@link MapView} when a {@link Marker} is added
|
||||
* that has a {@link Icon} with a {@link Bitmap} that has been modified.
|
||||
* </p>
|
||||
* You cannot modify a {@code Icon} after it has been added to the map in a {@code Marker}
|
||||
*
|
||||
* @see MapView
|
||||
* @see Icon
|
||||
* @see Marker
|
||||
*/
|
||||
public class IconBitmapChangedException extends RuntimeException {
|
||||
|
||||
public IconBitmapChangedException() {
|
||||
super("The added Marker has a Icon with a Bitmap that has been modified. You cannot modufy" +
|
||||
"a Icon after it has been added in a Marker.");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
package com.mapbox.mapboxsdk.exceptions;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
|
||||
import com.mapbox.mapboxsdk.annotations.Marker;
|
||||
import com.mapbox.mapboxsdk.annotations.Sprite;
|
||||
import com.mapbox.mapboxsdk.views.MapView;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* A {@code SpriteBitmapChangedException} is thrown by {@link MapView} when a {@link Marker} is added
|
||||
* that has a {@link Sprite} with a {@link Bitmap} that has been modified.
|
||||
* </p>
|
||||
* You cannot modify a {@code Sprite} after it has been added to the map in a {@code Marker}
|
||||
*
|
||||
* @see MapView
|
||||
* @see Sprite
|
||||
* @see Marker
|
||||
*/
|
||||
public class SpriteBitmapChangedException extends RuntimeException {
|
||||
|
||||
public SpriteBitmapChangedException() {
|
||||
super("The added Marker has a Sprite with a Bitmap that has been modified. You cannot modufy" +
|
||||
"a Sprite after it has been added in a Marker.");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.mapbox.mapboxsdk.exceptions;
|
||||
|
||||
import com.mapbox.mapboxsdk.annotations.Icon;
|
||||
import com.mapbox.mapboxsdk.annotations.IconFactory;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* A {@code TooManyIconsException} is thrown by {@link IconFactory} when it
|
||||
* cannot create a {@link Icon} because there are already too many.
|
||||
* </p>
|
||||
* You should try to reuse Icon objects whenever possible.
|
||||
*
|
||||
* @see IconFactory
|
||||
*/
|
||||
public class TooManyIconsException extends RuntimeException {
|
||||
|
||||
public TooManyIconsException() {
|
||||
super("Cannot create a Icon because there are already too many. Try reusing Icons.");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
package com.mapbox.mapboxsdk.exceptions;
|
||||
|
||||
import com.mapbox.mapboxsdk.annotations.Sprite;
|
||||
import com.mapbox.mapboxsdk.annotations.SpriteFactory;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* A {@code TooManySpritesException} is thrown by {@link SpriteFactory} when it
|
||||
* cannot create a {@link Sprite} because there are already too many.
|
||||
* </p>
|
||||
* You should try to reuse Sprite objects whenever possible.
|
||||
*
|
||||
* @see SpriteFactory
|
||||
*/
|
||||
public class TooManySpritesException extends RuntimeException {
|
||||
|
||||
public TooManySpritesException() {
|
||||
super("Cannot create a Sprite because there are already too many. Try reusing Sprites.");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -56,6 +56,7 @@ import com.almeros.android.multitouch.gesturedetectors.ShoveGestureDetector;
|
||||
import com.almeros.android.multitouch.gesturedetectors.TwoFingerGestureDetector;
|
||||
import com.mapbox.mapboxsdk.R;
|
||||
import com.mapbox.mapboxsdk.annotations.Annotation;
|
||||
import com.mapbox.mapboxsdk.annotations.Icon;
|
||||
import com.mapbox.mapboxsdk.annotations.InfoWindow;
|
||||
import com.mapbox.mapboxsdk.annotations.Marker;
|
||||
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
|
||||
@@ -63,16 +64,15 @@ import com.mapbox.mapboxsdk.annotations.Polygon;
|
||||
import com.mapbox.mapboxsdk.annotations.PolygonOptions;
|
||||
import com.mapbox.mapboxsdk.annotations.Polyline;
|
||||
import com.mapbox.mapboxsdk.annotations.PolylineOptions;
|
||||
import com.mapbox.mapboxsdk.annotations.Sprite;
|
||||
import com.mapbox.mapboxsdk.annotations.SpriteFactory;
|
||||
import com.mapbox.mapboxsdk.annotations.IconFactory;
|
||||
import com.mapbox.mapboxsdk.camera.CameraPosition;
|
||||
import com.mapbox.mapboxsdk.camera.CameraUpdate;
|
||||
import com.mapbox.mapboxsdk.constants.MathConstants;
|
||||
import com.mapbox.mapboxsdk.constants.MyBearingTracking;
|
||||
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.SpriteBitmapChangedException;
|
||||
import com.mapbox.mapboxsdk.geometry.BoundingBox;
|
||||
import com.mapbox.mapboxsdk.geometry.CoordinateBounds;
|
||||
import com.mapbox.mapboxsdk.geometry.LatLng;
|
||||
@@ -223,7 +223,7 @@ public final class MapView extends FrameLayout {
|
||||
private List<Marker> mSelectedMarkers = new ArrayList<>();
|
||||
private List<InfoWindow> mInfoWindows = new ArrayList<>();
|
||||
private InfoWindowAdapter mInfoWindowAdapter;
|
||||
private List<Sprite> mSprites = new ArrayList<>();
|
||||
private List<Icon> mIcons = new ArrayList<>();
|
||||
|
||||
// Used for the Mapbox Logo
|
||||
private ImageView mLogoView;
|
||||
@@ -907,7 +907,7 @@ public final class MapView extends FrameLayout {
|
||||
@Override
|
||||
public void onMapChanged(@MapChange int change) {
|
||||
if (change == DID_FINISH_LOADING_MAP) {
|
||||
reloadSprites();
|
||||
reloadIcons();
|
||||
reloadMarkers();
|
||||
adjustTopOffsetPixels();
|
||||
}
|
||||
@@ -1954,13 +1954,13 @@ public final class MapView extends FrameLayout {
|
||||
// Annotations
|
||||
//
|
||||
|
||||
public SpriteFactory getSpriteFactory() {
|
||||
return SpriteFactory.getInstance(getContext());
|
||||
public IconFactory getIconFactory() {
|
||||
return IconFactory.getInstance(getContext());
|
||||
}
|
||||
|
||||
private void loadSprite(Sprite sprite) {
|
||||
Bitmap bitmap = sprite.getBitmap();
|
||||
String id = sprite.getId();
|
||||
private void loadIcon(Icon icon) {
|
||||
Bitmap bitmap = icon.getBitmap();
|
||||
String id = icon.getId();
|
||||
if (bitmap.getConfig() != Bitmap.Config.ARGB_8888) {
|
||||
bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, false);
|
||||
}
|
||||
@@ -1980,31 +1980,31 @@ public final class MapView extends FrameLayout {
|
||||
scale, buffer.array());
|
||||
}
|
||||
|
||||
private void reloadSprites() {
|
||||
int count = mSprites.size();
|
||||
private void reloadIcons() {
|
||||
int count = mIcons.size();
|
||||
for (int i = 0; i < count; i++) {
|
||||
Sprite sprite = mSprites.get(i);
|
||||
loadSprite(sprite);
|
||||
Icon icon = mIcons.get(i);
|
||||
loadIcon(icon);
|
||||
}
|
||||
}
|
||||
|
||||
private Marker prepareMarker(MarkerOptions markerOptions) {
|
||||
Marker marker = markerOptions.getMarker();
|
||||
Sprite icon = marker.getIcon();
|
||||
Icon icon = marker.getIcon();
|
||||
if (icon == null) {
|
||||
icon = getSpriteFactory().defaultMarker();
|
||||
icon = getIconFactory().defaultMarker();
|
||||
marker.setIcon(icon);
|
||||
}
|
||||
if (!mSprites.contains(icon)) {
|
||||
mSprites.add(icon);
|
||||
loadSprite(icon);
|
||||
if (!mIcons.contains(icon)) {
|
||||
mIcons.add(icon);
|
||||
loadIcon(icon);
|
||||
} else {
|
||||
Sprite oldSprite = mSprites.get(mSprites.indexOf(icon));
|
||||
if (!oldSprite.getBitmap().sameAs(icon.getBitmap())) {
|
||||
throw new SpriteBitmapChangedException();
|
||||
Icon oldIcon = mIcons.get(mIcons.indexOf(icon));
|
||||
if (!oldIcon.getBitmap().sameAs(icon.getBitmap())) {
|
||||
throw new IconBitmapChangedException();
|
||||
}
|
||||
}
|
||||
marker.setTopOffsetPixels(getTopOffsetPixelsForSprite(icon));
|
||||
marker.setTopOffsetPixels(getTopOffsetPixelsForIcon(icon));
|
||||
return marker;
|
||||
}
|
||||
|
||||
@@ -2296,14 +2296,14 @@ public final class MapView extends FrameLayout {
|
||||
return new ArrayList<>(annotations);
|
||||
}
|
||||
|
||||
private int getTopOffsetPixelsForSprite(Sprite sprite) {
|
||||
private int getTopOffsetPixelsForIcon(Icon icon) {
|
||||
// This method will dead lock if map paused. Causes a freeze if you add a marker in an
|
||||
// activity's onCreate()
|
||||
if (mNativeMapView.isPaused()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (int) (mNativeMapView.getTopOffsetPixelsForAnnotationSymbol(sprite.getId())
|
||||
return (int) (mNativeMapView.getTopOffsetPixelsForAnnotationSymbol(icon.getId())
|
||||
* mScreenDensity);
|
||||
}
|
||||
|
||||
@@ -2525,7 +2525,7 @@ public final class MapView extends FrameLayout {
|
||||
if (annotation instanceof Marker) {
|
||||
Marker marker = (Marker) annotation;
|
||||
marker.setTopOffsetPixels(
|
||||
getTopOffsetPixelsForSprite(marker.getIcon()));
|
||||
getTopOffsetPixelsForIcon(marker.getIcon()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user