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

Commit

Permalink
[compass] - deprecate bitmap API, introduce drawable resource ID API …
Browse files Browse the repository at this point in the history
…instead (#598)
  • Loading branch information
tobrun committed Oct 21, 2020
1 parent bd76460 commit 1fc8cde
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,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 @@ -9,11 +9,11 @@
import android.os.Parcelable;

import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.core.content.res.ResourcesCompat;

import android.text.TextUtils;
import android.util.AttributeSet;
Expand Down Expand Up @@ -50,6 +50,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 @@ -117,6 +119,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 @@ -233,12 +236,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 @@ -470,13 +473,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 @@ -946,10 +965,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 @@ -1171,7 +1201,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 @@ -1233,6 +1263,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 @@ -1339,6 +1372,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
Expand Up @@ -12,14 +12,14 @@
import android.widget.ImageView;

import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.annotation.FloatRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.Px;
import androidx.annotation.UiThread;
import androidx.annotation.VisibleForTesting;
import androidx.core.content.ContextCompat;
import androidx.core.content.res.ResourcesCompat;

import com.mapbox.mapboxsdk.R;
import com.mapbox.mapboxsdk.camera.CameraPosition;
Expand Down Expand Up @@ -193,10 +193,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 @@ -207,8 +207,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 @@ -224,8 +230,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(
mapView.getContext(), savedInstanceState.getByteArray(MapboxConstants.STATE_COMPASS_IMAGE_BITMAP)));

if (savedInstanceState.containsKey(MapboxConstants.STATE_COMPASS_IMAGE_BITMAP)) {
setCompassImage(BitmapUtils.getDrawableFromByteArray(
mapView.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 @@ -387,13 +398,29 @@ 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) {
if (compassView != null) {
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 @@ -480,8 +507,10 @@ public int getCompassMarginBottom() {
* Get the current configured CompassView image.
*
* @return the drawable used as compass image
* @deprecated use {@link #getCompassImageResource()} instead
*/
@Nullable
@Deprecated
public Drawable getCompassImage() {
if (compassView != null) {
return compassView.getCompassImage();
Expand All @@ -490,6 +519,21 @@ public Drawable 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) {
clockwiseBearing = -cameraPosition.bearing;
if (compassView != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import android.content.Context;
import android.graphics.drawable.Drawable;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.view.ViewCompat;
Expand Down Expand Up @@ -35,6 +37,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 +144,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 +184,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 @@ -50,6 +50,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 @@ -60,6 +60,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

0 comments on commit 1fc8cde

Please sign in to comment.