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

Commit d794d73

Browse files
authored
[compass] - deprecate bitmap API, introduce drawable resource ID API instead (#598) (#603)
1 parent a969cf3 commit d794d73

File tree

6 files changed

+118
-16
lines changed

6 files changed

+118
-16
lines changed

MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/constants/MapboxConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ public class MapboxConstants {
209209
public static final String STATE_COMPASS_MARGIN_BOTTOM = "mapbox_compassMarginBottom";
210210
public static final String STATE_COMPASS_FADE_WHEN_FACING_NORTH = "mapbox_compassFade";
211211
public static final String STATE_COMPASS_IMAGE_BITMAP = "mapbox_compassImage";
212+
public static final String STATE_COMPASS_IMAGE_RES = "mapbox_compassImageRes";
212213
public static final String STATE_LOGO_GRAVITY = "mapbox_logoGravity";
213214
public static final String STATE_LOGO_MARGIN_LEFT = "mapbox_logoMarginLeft";
214215
public static final String STATE_LOGO_MARGIN_TOP = "mapbox_logoMarginTop";

MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMapOptions.java

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
import android.os.Parcel;
99
import android.os.Parcelable;
1010
import android.support.annotation.ColorInt;
11+
import android.support.annotation.DrawableRes;
1112
import android.support.annotation.IntRange;
1213
import android.support.annotation.NonNull;
1314
import android.support.annotation.Nullable;
1415
import android.support.annotation.VisibleForTesting;
15-
import android.support.v4.content.res.ResourcesCompat;
1616
import android.text.TextUtils;
1717
import android.util.AttributeSet;
1818
import android.view.Gravity;
@@ -48,6 +48,8 @@ public class MapboxMapOptions implements Parcelable {
4848
private boolean fadeCompassFacingNorth = true;
4949
private int compassGravity = Gravity.TOP | Gravity.END;
5050
private int[] compassMargins;
51+
@DrawableRes
52+
private int compassImageResource;
5153
private Drawable compassImage;
5254

5355
private boolean logoEnabled = true;
@@ -112,6 +114,7 @@ private MapboxMapOptions(Parcel in) {
112114
if (compassBitmap != null) {
113115
compassImage = new BitmapDrawable(compassBitmap);
114116
}
117+
compassImageResource = in.readInt();
115118

116119
logoEnabled = in.readByte() != 0;
117120
logoGravity = in.readInt();
@@ -219,12 +222,12 @@ static MapboxMapOptions createFromAttributes(@NonNull MapboxMapOptions mapboxMap
219222
FOUR_DP * pxlRatio))});
220223
mapboxMapOptions.compassFadesWhenFacingNorth(typedArray.getBoolean(
221224
R.styleable.mapbox_MapView_mapbox_uiCompassFadeFacingNorth, true));
222-
Drawable compassDrawable = typedArray.getDrawable(
223-
R.styleable.mapbox_MapView_mapbox_uiCompassDrawable);
224-
if (compassDrawable == null) {
225-
compassDrawable = ResourcesCompat.getDrawable(context.getResources(), R.drawable.mapbox_compass_icon, null);
226-
}
225+
226+
Drawable compassDrawable = typedArray.getDrawable(R.styleable.mapbox_MapView_mapbox_uiCompassDrawable);
227227
mapboxMapOptions.compassImage(compassDrawable);
228+
mapboxMapOptions.compassImageResource(
229+
typedArray.getInt(R.styleable.mapbox_MapView_mapbox_uiCompassDrawableRes, R.drawable.mapbox_compass_icon)
230+
);
228231

229232
mapboxMapOptions.logoEnabled(typedArray.getBoolean(R.styleable.mapbox_MapView_mapbox_uiLogo, true));
230233
mapboxMapOptions.logoGravity(typedArray.getInt(R.styleable.mapbox_MapView_mapbox_uiLogoGravity,
@@ -431,13 +434,29 @@ public MapboxMapOptions compassFadesWhenFacingNorth(boolean compassFadeWhenFacin
431434
*
432435
* @param compass the drawable to show as image compass
433436
* @return This
437+
* @deprecated use {@link #compassImageResource} instead
434438
*/
435439
@NonNull
436440
public MapboxMapOptions compassImage(Drawable compass) {
437441
this.compassImage = compass;
438442
return this;
439443
}
440444

445+
/**
446+
* Specifies the image of the CompassView.
447+
* <p>
448+
* By default this value is R.drawable.mapbox_compass_icon.
449+
* </p>
450+
*
451+
* @param compassImageResource the drawable resource id to show as image compass
452+
* @return This
453+
*/
454+
@NonNull
455+
public MapboxMapOptions compassImageResource(@DrawableRes int compassImageResource) {
456+
this.compassImageResource = compassImageResource;
457+
return this;
458+
}
459+
441460
/**
442461
* Specifies the visibility state of a logo for a map view.
443462
*
@@ -877,10 +896,21 @@ public boolean getCompassFadeFacingNorth() {
877896
*
878897
* @return the drawable used as compass image
879898
*/
899+
@Deprecated
880900
public Drawable getCompassImage() {
881901
return compassImage;
882902
}
883903

904+
/**
905+
* Get the current configured CompassView image resource id.
906+
*
907+
* @return the resource id of the used as compass image
908+
*/
909+
@DrawableRes
910+
public int getCompassImageResource() {
911+
return compassImageResource;
912+
}
913+
884914
/**
885915
* Get the current configured visibility state for mapbox_compass_icon for a map view.
886916
*
@@ -1093,7 +1123,7 @@ public void writeToParcel(@NonNull Parcel dest, int flags) {
10931123
dest.writeByte((byte) (fadeCompassFacingNorth ? 1 : 0));
10941124
dest.writeParcelable(compassImage != null
10951125
? BitmapUtils.getBitmapFromDrawable(compassImage) : null, flags);
1096-
1126+
dest.writeInt(compassImageResource);
10971127
dest.writeByte((byte) (logoEnabled ? 1 : 0));
10981128
dest.writeInt(logoGravity);
10991129
dest.writeIntArray(logoMargins);
@@ -1152,6 +1182,9 @@ public boolean equals(@Nullable Object o) {
11521182
: options.compassImage != null) {
11531183
return false;
11541184
}
1185+
if (compassImageResource != options.compassImageResource) {
1186+
return false;
1187+
}
11551188
if (compassGravity != options.compassGravity) {
11561189
return false;
11571190
}
@@ -1249,6 +1282,7 @@ public int hashCode() {
12491282
result = 31 * result + (fadeCompassFacingNorth ? 1 : 0);
12501283
result = 31 * result + compassGravity;
12511284
result = 31 * result + (compassImage != null ? compassImage.hashCode() : 0);
1285+
result = 31 * result + compassImageResource;
12521286
result = 31 * result + Arrays.hashCode(compassMargins);
12531287
result = 31 * result + (logoEnabled ? 1 : 0);
12541288
result = 31 * result + logoGravity;

MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/UiSettings.java

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
package com.mapbox.mapboxsdk.maps;
23

34
import android.content.Context;
@@ -8,13 +9,13 @@
89
import android.os.Build;
910
import android.os.Bundle;
1011
import android.support.annotation.ColorInt;
12+
import android.support.annotation.DrawableRes;
1113
import android.support.annotation.FloatRange;
1214
import android.support.annotation.NonNull;
1315
import android.support.annotation.Nullable;
1416
import android.support.annotation.Px;
1517
import android.support.annotation.UiThread;
1618
import android.support.v4.content.ContextCompat;
17-
import android.support.v4.content.res.ResourcesCompat;
1819
import android.view.View;
1920
import android.widget.FrameLayout;
2021
import android.widget.ImageView;
@@ -168,10 +169,10 @@ private void initialiseCompass(MapboxMapOptions options, @NonNull Resources reso
168169
setCompassMargins(tenDp, tenDp, tenDp, tenDp);
169170
}
170171
setCompassFadeFacingNorth(options.getCompassFadeFacingNorth());
171-
if (options.getCompassImage() == null) {
172-
options.compassImage(ResourcesCompat.getDrawable(resources, R.drawable.mapbox_compass_icon, null));
172+
if (options.getCompassImage() != null) {
173+
setCompassImage(options.getCompassImage());
173174
}
174-
setCompassImage(options.getCompassImage());
175+
setCompassImageResource(options.getCompassImageResource());
175176
}
176177

177178
private void saveCompass(Bundle outState) {
@@ -182,8 +183,14 @@ private void saveCompass(Bundle outState) {
182183
outState.putInt(MapboxConstants.STATE_COMPASS_MARGIN_BOTTOM, getCompassMarginBottom());
183184
outState.putInt(MapboxConstants.STATE_COMPASS_MARGIN_RIGHT, getCompassMarginRight());
184185
outState.putBoolean(MapboxConstants.STATE_COMPASS_FADE_WHEN_FACING_NORTH, isCompassFadeWhenFacingNorth());
185-
outState.putByteArray(MapboxConstants.STATE_COMPASS_IMAGE_BITMAP,
186-
BitmapUtils.getByteArrayFromDrawable(getCompassImage()));
186+
187+
// Remove below when we remove deprecated code for bitmap API, only leave else clause
188+
if (compassView != null && compassView.isLegacyImageDrawableSetter()) {
189+
outState.putByteArray(MapboxConstants.STATE_COMPASS_IMAGE_BITMAP,
190+
BitmapUtils.getByteArrayFromDrawable(getCompassImage()));
191+
} else {
192+
outState.putInt(MapboxConstants.STATE_COMPASS_IMAGE_RES, getCompassImageResource());
193+
}
187194
}
188195

189196
private void restoreCompass(Bundle savedInstanceState) {
@@ -194,8 +201,13 @@ private void restoreCompass(Bundle savedInstanceState) {
194201
savedInstanceState.getInt(MapboxConstants.STATE_COMPASS_MARGIN_RIGHT),
195202
savedInstanceState.getInt(MapboxConstants.STATE_COMPASS_MARGIN_BOTTOM));
196203
setCompassFadeFacingNorth(savedInstanceState.getBoolean(MapboxConstants.STATE_COMPASS_FADE_WHEN_FACING_NORTH));
197-
setCompassImage(BitmapUtils.getDrawableFromByteArray(
198-
compassView.getContext(), savedInstanceState.getByteArray(MapboxConstants.STATE_COMPASS_IMAGE_BITMAP)));
204+
205+
if (savedInstanceState.containsKey(MapboxConstants.STATE_COMPASS_IMAGE_BITMAP)) {
206+
setCompassImage(BitmapUtils.getDrawableFromByteArray(
207+
compassView.getContext(), savedInstanceState.getByteArray(MapboxConstants.STATE_COMPASS_IMAGE_BITMAP)));
208+
} else {
209+
setCompassImageResource(savedInstanceState.getInt(MapboxConstants.STATE_COMPASS_IMAGE_RES));
210+
}
199211
}
200212

201213
private void initialiseLogo(MapboxMapOptions options, @NonNull Resources resources) {
@@ -329,11 +341,27 @@ public void setCompassFadeFacingNorth(boolean compassFadeFacingNorth) {
329341
* </p>
330342
*
331343
* @param compass the drawable to show as image compass
344+
* @deprecated use {@link #setCompassImageResource(int)} instead
332345
*/
346+
@Deprecated
333347
public void setCompassImage(@NonNull Drawable compass) {
334348
compassView.setCompassImage(compass);
335349
}
336350

351+
/**
352+
* Specifies the CompassView image.
353+
* <p>
354+
* By default this value is R.drawable.mapbox_compass_icon.
355+
* </p>
356+
*
357+
* @param drawableRes the resource id of the drawable to show as image compass
358+
*/
359+
public void setCompassImageResource(@DrawableRes int drawableRes) {
360+
if (compassView != null) {
361+
compassView.setCompassImageResource(drawableRes);
362+
}
363+
}
364+
337365
/**
338366
* Returns whether the compass performs a fading animation out when facing north.
339367
*
@@ -410,12 +438,29 @@ public int getCompassMarginBottom() {
410438
* Get the current configured CompassView image.
411439
*
412440
* @return the drawable used as compass image
441+
* @deprecated use {@link #getCompassImageResource()} instead
413442
*/
414-
@NonNull
443+
@Nullable
444+
@Deprecated
415445
public Drawable getCompassImage() {
416446
return compassView.getCompassImage();
417447
}
418448

449+
/**
450+
* Get the current configured id of the CompassView drawable resource.
451+
*
452+
* @return the drawable resource id used as compass image
453+
*/
454+
@DrawableRes
455+
@Nullable
456+
public int getCompassImageResource() {
457+
if (compassView != null) {
458+
return compassView.getCompassImageResource();
459+
} else {
460+
return R.drawable.mapbox_compass_icon;
461+
}
462+
}
463+
419464
void update(@NonNull CameraPosition cameraPosition) {
420465
double clockwiseBearing = -cameraPosition.bearing;
421466
compassView.update(clockwiseBearing);

MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/widgets/CompassView.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.content.Context;
44
import android.graphics.drawable.Drawable;
5+
import android.support.annotation.DrawableRes;
56
import android.support.annotation.NonNull;
67
import android.support.annotation.Nullable;
78
import android.support.v4.view.ViewCompat;
@@ -35,6 +36,9 @@ public final class CompassView extends ImageView implements Runnable {
3536
private ViewPropertyAnimatorCompat fadeAnimator;
3637
private MapboxMap.OnCompassAnimationListener compassAnimationListener;
3738
private boolean isAnimating = false;
39+
@DrawableRes
40+
private int compassImageResource;
41+
private boolean legacyImageDrawableSetter = false;
3842

3943
public CompassView(@NonNull Context context) {
4044
super(context);
@@ -139,15 +143,18 @@ public boolean isFadeCompassViewFacingNorth() {
139143
* Set the CompassView image.
140144
*
141145
* @param compass the drawable to use as compass image
146+
* @deprecated use {@link #setCompassImageResource(int)} instead
142147
*/
143148
public void setCompassImage(Drawable compass) {
149+
legacyImageDrawableSetter = true;
144150
setImageDrawable(compass);
145151
}
146152

147153
/**
148154
* Get the current configured CompassView image.
149155
*
150156
* @return the drawable used as compass image
157+
* @deprecated use {@link #getCompassImageResource()} instead
151158
*/
152159
public Drawable getCompassImage() {
153160
return getDrawable();
@@ -176,4 +183,17 @@ private void notifyCompassAnimationListenerWhenAnimating() {
176183
compassAnimationListener.onCompassAnimation();
177184
}
178185
}
186+
187+
public int getCompassImageResource() {
188+
return compassImageResource;
189+
}
190+
191+
public void setCompassImageResource(int drawableRes) {
192+
this.compassImageResource = drawableRes;
193+
setImageResource(compassImageResource);
194+
}
195+
196+
public boolean isLegacyImageDrawableSetter() {
197+
return legacyImageDrawableSetter;
198+
}
179199
}

MapboxGLAndroidSDK/src/main/res-public/values/public.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
<public name="mapbox_uiCompassMarginBottom" type="attr" />
4646
<public name="mapbox_uiCompassFadeFacingNorth" type="attr" />
4747
<public name="mapbox_uiCompassDrawable" type="attr" />
48+
<public name="mapbox_uiCompassDrawableRes" type="attr" />
4849

4950
<!--Logo-->
5051
<public name="mapbox_uiLogo" type="attr" />

MapboxGLAndroidSDK/src/main/res/values/attrs.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
<attr name="mapbox_uiCompassMarginBottom" format="dimension"/>
5656
<attr name="mapbox_uiCompassFadeFacingNorth" format="boolean"/>
5757
<attr name="mapbox_uiCompassDrawable" format="reference"/>
58+
<attr name="mapbox_uiCompassDrawableRes" format="integer"/>
5859

5960
<!--Logo-->
6061
<attr name="mapbox_uiLogo" format="boolean"/>

0 commit comments

Comments
 (0)