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

Cherry-pick of #598 #603

Merged
merged 1 commit into from
Oct 22, 2020
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 @@ -209,6 +209,7 @@ public class MapboxConstants {
public static final String STATE_COMPASS_MARGIN_BOTTOM = "mapbox_compassMarginBottom";
public static final String STATE_COMPASS_FADE_WHEN_FACING_NORTH = "mapbox_compassFade";
public static final String STATE_COMPASS_IMAGE_BITMAP = "mapbox_compassImage";
public static final String STATE_COMPASS_IMAGE_RES = "mapbox_compassImageRes";
public static final String STATE_LOGO_GRAVITY = "mapbox_logoGravity";
public static final String STATE_LOGO_MARGIN_LEFT = "mapbox_logoMarginLeft";
public static final String STATE_LOGO_MARGIN_TOP = "mapbox_logoMarginTop";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.ColorInt;
import android.support.annotation.DrawableRes;
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.VisibleForTesting;
import android.support.v4.content.res.ResourcesCompat;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
Expand Down Expand Up @@ -48,6 +48,8 @@ public class MapboxMapOptions implements Parcelable {
private boolean fadeCompassFacingNorth = true;
private int compassGravity = Gravity.TOP | Gravity.END;
private int[] compassMargins;
@DrawableRes
private int compassImageResource;
private Drawable compassImage;

private boolean logoEnabled = true;
Expand Down Expand Up @@ -112,6 +114,7 @@ private MapboxMapOptions(Parcel in) {
if (compassBitmap != null) {
compassImage = new BitmapDrawable(compassBitmap);
}
compassImageResource = in.readInt();

logoEnabled = in.readByte() != 0;
logoGravity = in.readInt();
Expand Down Expand Up @@ -219,12 +222,12 @@ static MapboxMapOptions createFromAttributes(@NonNull MapboxMapOptions mapboxMap
FOUR_DP * pxlRatio))});
mapboxMapOptions.compassFadesWhenFacingNorth(typedArray.getBoolean(
R.styleable.mapbox_MapView_mapbox_uiCompassFadeFacingNorth, true));
Drawable compassDrawable = typedArray.getDrawable(
R.styleable.mapbox_MapView_mapbox_uiCompassDrawable);
if (compassDrawable == null) {
compassDrawable = ResourcesCompat.getDrawable(context.getResources(), R.drawable.mapbox_compass_icon, null);
}

Drawable compassDrawable = typedArray.getDrawable(R.styleable.mapbox_MapView_mapbox_uiCompassDrawable);
mapboxMapOptions.compassImage(compassDrawable);
mapboxMapOptions.compassImageResource(
typedArray.getInt(R.styleable.mapbox_MapView_mapbox_uiCompassDrawableRes, R.drawable.mapbox_compass_icon)
);

mapboxMapOptions.logoEnabled(typedArray.getBoolean(R.styleable.mapbox_MapView_mapbox_uiLogo, true));
mapboxMapOptions.logoGravity(typedArray.getInt(R.styleable.mapbox_MapView_mapbox_uiLogoGravity,
Expand Down Expand Up @@ -431,13 +434,29 @@ public MapboxMapOptions compassFadesWhenFacingNorth(boolean compassFadeWhenFacin
*
* @param compass the drawable to show as image compass
* @return This
* @deprecated use {@link #compassImageResource} instead
*/
@NonNull
public MapboxMapOptions compassImage(Drawable compass) {
this.compassImage = compass;
return this;
}

/**
* Specifies the image of the CompassView.
* <p>
* By default this value is R.drawable.mapbox_compass_icon.
* </p>
*
* @param compassImageResource the drawable resource id to show as image compass
* @return This
*/
@NonNull
public MapboxMapOptions compassImageResource(@DrawableRes int compassImageResource) {
this.compassImageResource = compassImageResource;
return this;
}

/**
* Specifies the visibility state of a logo for a map view.
*
Expand Down Expand Up @@ -877,10 +896,21 @@ public boolean getCompassFadeFacingNorth() {
*
* @return the drawable used as compass image
*/
@Deprecated
public Drawable getCompassImage() {
return compassImage;
}

/**
* Get the current configured CompassView image resource id.
*
* @return the resource id of the used as compass image
*/
@DrawableRes
public int getCompassImageResource() {
return compassImageResource;
}

/**
* Get the current configured visibility state for mapbox_compass_icon for a map view.
*
Expand Down Expand Up @@ -1093,7 +1123,7 @@ public void writeToParcel(@NonNull Parcel dest, int flags) {
dest.writeByte((byte) (fadeCompassFacingNorth ? 1 : 0));
dest.writeParcelable(compassImage != null
? BitmapUtils.getBitmapFromDrawable(compassImage) : null, flags);

dest.writeInt(compassImageResource);
dest.writeByte((byte) (logoEnabled ? 1 : 0));
dest.writeInt(logoGravity);
dest.writeIntArray(logoMargins);
Expand Down Expand Up @@ -1152,6 +1182,9 @@ public boolean equals(@Nullable Object o) {
: options.compassImage != null) {
return false;
}
if (compassImageResource != options.compassImageResource) {
return false;
}
if (compassGravity != options.compassGravity) {
return false;
}
Expand Down Expand Up @@ -1249,6 +1282,7 @@ public int hashCode() {
result = 31 * result + (fadeCompassFacingNorth ? 1 : 0);
result = 31 * result + compassGravity;
result = 31 * result + (compassImage != null ? compassImage.hashCode() : 0);
result = 31 * result + compassImageResource;
result = 31 * result + Arrays.hashCode(compassMargins);
result = 31 * result + (logoEnabled ? 1 : 0);
result = 31 * result + logoGravity;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

package com.mapbox.mapboxsdk.maps;

import android.content.Context;
Expand All @@ -8,13 +9,13 @@
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.ColorInt;
import android.support.annotation.DrawableRes;
import android.support.annotation.FloatRange;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.Px;
import android.support.annotation.UiThread;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.res.ResourcesCompat;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
Expand Down Expand Up @@ -168,10 +169,10 @@ private void initialiseCompass(MapboxMapOptions options, @NonNull Resources reso
setCompassMargins(tenDp, tenDp, tenDp, tenDp);
}
setCompassFadeFacingNorth(options.getCompassFadeFacingNorth());
if (options.getCompassImage() == null) {
options.compassImage(ResourcesCompat.getDrawable(resources, R.drawable.mapbox_compass_icon, null));
if (options.getCompassImage() != null) {
setCompassImage(options.getCompassImage());
}
setCompassImage(options.getCompassImage());
setCompassImageResource(options.getCompassImageResource());
}

private void saveCompass(Bundle outState) {
Expand All @@ -182,8 +183,14 @@ private void saveCompass(Bundle outState) {
outState.putInt(MapboxConstants.STATE_COMPASS_MARGIN_BOTTOM, getCompassMarginBottom());
outState.putInt(MapboxConstants.STATE_COMPASS_MARGIN_RIGHT, getCompassMarginRight());
outState.putBoolean(MapboxConstants.STATE_COMPASS_FADE_WHEN_FACING_NORTH, isCompassFadeWhenFacingNorth());
outState.putByteArray(MapboxConstants.STATE_COMPASS_IMAGE_BITMAP,
BitmapUtils.getByteArrayFromDrawable(getCompassImage()));

// Remove below when we remove deprecated code for bitmap API, only leave else clause
if (compassView != null && compassView.isLegacyImageDrawableSetter()) {
outState.putByteArray(MapboxConstants.STATE_COMPASS_IMAGE_BITMAP,
BitmapUtils.getByteArrayFromDrawable(getCompassImage()));
} else {
outState.putInt(MapboxConstants.STATE_COMPASS_IMAGE_RES, getCompassImageResource());
}
}

private void restoreCompass(Bundle savedInstanceState) {
Expand All @@ -194,8 +201,13 @@ private void restoreCompass(Bundle savedInstanceState) {
savedInstanceState.getInt(MapboxConstants.STATE_COMPASS_MARGIN_RIGHT),
savedInstanceState.getInt(MapboxConstants.STATE_COMPASS_MARGIN_BOTTOM));
setCompassFadeFacingNorth(savedInstanceState.getBoolean(MapboxConstants.STATE_COMPASS_FADE_WHEN_FACING_NORTH));
setCompassImage(BitmapUtils.getDrawableFromByteArray(
compassView.getContext(), savedInstanceState.getByteArray(MapboxConstants.STATE_COMPASS_IMAGE_BITMAP)));

if (savedInstanceState.containsKey(MapboxConstants.STATE_COMPASS_IMAGE_BITMAP)) {
setCompassImage(BitmapUtils.getDrawableFromByteArray(
compassView.getContext(), savedInstanceState.getByteArray(MapboxConstants.STATE_COMPASS_IMAGE_BITMAP)));
} else {
setCompassImageResource(savedInstanceState.getInt(MapboxConstants.STATE_COMPASS_IMAGE_RES));
}
}

private void initialiseLogo(MapboxMapOptions options, @NonNull Resources resources) {
Expand Down Expand Up @@ -329,11 +341,27 @@ public void setCompassFadeFacingNorth(boolean compassFadeFacingNorth) {
* </p>
*
* @param compass the drawable to show as image compass
* @deprecated use {@link #setCompassImageResource(int)} instead
*/
@Deprecated
public void setCompassImage(@NonNull Drawable compass) {
compassView.setCompassImage(compass);
}

/**
* Specifies the CompassView image.
* <p>
* By default this value is R.drawable.mapbox_compass_icon.
* </p>
*
* @param drawableRes the resource id of the drawable to show as image compass
*/
public void setCompassImageResource(@DrawableRes int drawableRes) {
if (compassView != null) {
compassView.setCompassImageResource(drawableRes);
}
}

/**
* Returns whether the compass performs a fading animation out when facing north.
*
Expand Down Expand Up @@ -410,12 +438,29 @@ public int getCompassMarginBottom() {
* Get the current configured CompassView image.
*
* @return the drawable used as compass image
* @deprecated use {@link #getCompassImageResource()} instead
*/
@NonNull
@Nullable
@Deprecated
public Drawable getCompassImage() {
return compassView.getCompassImage();
}

/**
* Get the current configured id of the CompassView drawable resource.
*
* @return the drawable resource id used as compass image
*/
@DrawableRes
@Nullable
public int getCompassImageResource() {
if (compassView != null) {
return compassView.getCompassImageResource();
} else {
return R.drawable.mapbox_compass_icon;
}
}

void update(@NonNull CameraPosition cameraPosition) {
double clockwiseBearing = -cameraPosition.bearing;
compassView.update(clockwiseBearing);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewCompat;
Expand Down Expand Up @@ -35,6 +36,9 @@ public final class CompassView extends ImageView implements Runnable {
private ViewPropertyAnimatorCompat fadeAnimator;
private MapboxMap.OnCompassAnimationListener compassAnimationListener;
private boolean isAnimating = false;
@DrawableRes
private int compassImageResource;
private boolean legacyImageDrawableSetter = false;

public CompassView(@NonNull Context context) {
super(context);
Expand Down Expand Up @@ -139,15 +143,18 @@ public boolean isFadeCompassViewFacingNorth() {
* Set the CompassView image.
*
* @param compass the drawable to use as compass image
* @deprecated use {@link #setCompassImageResource(int)} instead
*/
public void setCompassImage(Drawable compass) {
legacyImageDrawableSetter = true;
setImageDrawable(compass);
}

/**
* Get the current configured CompassView image.
*
* @return the drawable used as compass image
* @deprecated use {@link #getCompassImageResource()} instead
*/
public Drawable getCompassImage() {
return getDrawable();
Expand Down Expand Up @@ -176,4 +183,17 @@ private void notifyCompassAnimationListenerWhenAnimating() {
compassAnimationListener.onCompassAnimation();
}
}

public int getCompassImageResource() {
return compassImageResource;
}

public void setCompassImageResource(int drawableRes) {
this.compassImageResource = drawableRes;
setImageResource(compassImageResource);
}

public boolean isLegacyImageDrawableSetter() {
return legacyImageDrawableSetter;
}
}
1 change: 1 addition & 0 deletions MapboxGLAndroidSDK/src/main/res-public/values/public.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<public name="mapbox_uiCompassMarginBottom" type="attr" />
<public name="mapbox_uiCompassFadeFacingNorth" type="attr" />
<public name="mapbox_uiCompassDrawable" type="attr" />
<public name="mapbox_uiCompassDrawableRes" type="attr" />

<!--Logo-->
<public name="mapbox_uiLogo" type="attr" />
Expand Down
1 change: 1 addition & 0 deletions MapboxGLAndroidSDK/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
<attr name="mapbox_uiCompassMarginBottom" format="dimension"/>
<attr name="mapbox_uiCompassFadeFacingNorth" format="boolean"/>
<attr name="mapbox_uiCompassDrawable" format="reference"/>
<attr name="mapbox_uiCompassDrawableRes" format="integer"/>

<!--Logo-->
<attr name="mapbox_uiLogo" format="boolean"/>
Expand Down