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

Commit 186f366

Browse files
committed
[android] - finalise integration and layout logic
1 parent 632d335 commit 186f366

File tree

12 files changed

+528
-298
lines changed

12 files changed

+528
-298
lines changed

platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/attribution/Attribution.java

+15
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
public class Attribution {
44

5+
private static final String OPENSTREETMAP = "OpenStreetMap";
6+
private static final String OPENSTREETMAP_ABBR = "OSM";
7+
static final String TELEMETRY = "Telemetry Settings";
8+
9+
static final String IMPROVE_MAP_URL = "https://www.mapbox.com/map-feedback/";
10+
static final String MAPBOX_URL = "https://www.mapbox.com/about/maps/";
11+
static final String TELEMETRY_URL = "https://www.mapbox.com/telemetry/";
12+
513
private String title;
614
private String url;
715

@@ -14,6 +22,13 @@ public String getTitle() {
1422
return title;
1523
}
1624

25+
public String getTitleAbbreviated() {
26+
if (title.equals(OPENSTREETMAP)) {
27+
return OPENSTREETMAP_ABBR;
28+
}
29+
return title;
30+
}
31+
1732
public String getUrl() {
1833
return url;
1934
}

platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/attribution/AttributionPlacement.java platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/attribution/AttributionLayout.java

+13-7
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44
import android.graphics.PointF;
55
import android.support.annotation.Nullable;
66

7-
public class AttributionPlacement {
7+
public class AttributionLayout {
88

99
private Bitmap logo;
1010
private PointF anchorPoint;
11+
private boolean shortText;
1112

12-
public AttributionPlacement(@Nullable Bitmap logo, @Nullable PointF anchorPoint) {
13+
public AttributionLayout(@Nullable Bitmap logo, @Nullable PointF anchorPoint, boolean shortText) {
1314
this.logo = logo;
1415
this.anchorPoint = anchorPoint;
16+
this.shortText = shortText;
1517
}
1618

1719
public Bitmap getLogo() {
@@ -22,6 +24,10 @@ public PointF getAnchorPoint() {
2224
return anchorPoint;
2325
}
2426

27+
public boolean isShortText() {
28+
return shortText;
29+
}
30+
2531
@Override
2632
public boolean equals(Object o) {
2733
if (this == o) {
@@ -31,7 +37,7 @@ public boolean equals(Object o) {
3137
return false;
3238
}
3339

34-
AttributionPlacement that = (AttributionPlacement) o;
40+
AttributionLayout that = (AttributionLayout) o;
3541

3642
if (logo != null ? !logo.equals(that.logo) : that.logo != null) {
3743
return false;
@@ -48,9 +54,9 @@ public int hashCode() {
4854

4955
@Override
5056
public String toString() {
51-
return "AttributionPlacement{" +
52-
"logo=" + logo +
53-
", anchorPoint=" + anchorPoint +
54-
'}';
57+
return "AttributionLayout{"
58+
+ "logo=" + logo
59+
+ ", anchorPoint=" + anchorPoint
60+
+ '}';
5561
}
5662
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
package com.mapbox.mapboxsdk.attribution;
2+
3+
import android.graphics.Bitmap;
4+
import android.graphics.PointF;
5+
import android.widget.TextView;
6+
7+
import java.util.Arrays;
8+
import java.util.List;
9+
10+
public class AttributionMeasure {
11+
12+
private Bitmap logo;
13+
private Bitmap logoSmall;
14+
private Bitmap snapshot;
15+
private TextView textView;
16+
private TextView textViewShort;
17+
private float margin;
18+
19+
private boolean shorterText;
20+
21+
AttributionMeasure(Bitmap snapshot, Bitmap logo, Bitmap logoSmall, TextView tv, TextView tvShort, float margin) {
22+
this.snapshot = snapshot;
23+
this.logo = logo;
24+
this.logoSmall = logoSmall;
25+
this.textView = tv;
26+
this.textViewShort = tvShort;
27+
this.margin = margin;
28+
}
29+
30+
public AttributionLayout measure() {
31+
Chain chain = new Chain(
32+
new FullLogoLongTextCommand(),
33+
new FullLogoShortTextCommand(),
34+
new SmallLogoLongTextCommand(),
35+
new SmallLogoShortTextCommand(),
36+
new LongTextCommand(),
37+
new ShortTextCommand(),
38+
new NoTextCommand()
39+
);
40+
41+
AttributionLayout attributionLayout = chain.start(this);
42+
shorterText = attributionLayout.isShortText();
43+
return attributionLayout;
44+
}
45+
46+
47+
private static class FullLogoLongTextCommand implements Command {
48+
public AttributionLayout execute(AttributionMeasure measure) {
49+
float width = measure.getLogoContainerWidth() + measure.getTextViewContainerWidth();
50+
boolean fitBounds = width <= measure.getMaxSize();
51+
if (fitBounds) {
52+
PointF anchor = calculateAnchor(measure.snapshot, measure.textView, measure.margin);
53+
return new AttributionLayout(measure.logo, anchor, false);
54+
}
55+
return null;
56+
}
57+
}
58+
59+
private static class FullLogoShortTextCommand implements Command {
60+
@Override
61+
public AttributionLayout execute(AttributionMeasure measure) {
62+
float width = measure.getLogoContainerWidth() + measure.getTextViewShortContainerWidth();
63+
boolean fitBounds = width <= measure.getMaxSizeShort();
64+
if (fitBounds) {
65+
PointF anchor = calculateAnchor(measure.snapshot, measure.textView, measure.margin);
66+
return new AttributionLayout(measure.logo, anchor, true);
67+
}
68+
return null;
69+
}
70+
}
71+
72+
private static class SmallLogoLongTextCommand implements Command {
73+
@Override
74+
public AttributionLayout execute(AttributionMeasure measure) {
75+
float width = measure.getLogoSmallContainerWidth() + measure.getTextViewContainerWidth();
76+
boolean fitBounds = width <= measure.getMaxSize();
77+
if (fitBounds) {
78+
PointF anchor = calculateAnchor(measure.snapshot, measure.textView, measure.margin);
79+
return new AttributionLayout(measure.logoSmall, anchor, false);
80+
}
81+
return null;
82+
}
83+
}
84+
85+
private static class SmallLogoShortTextCommand implements Command {
86+
@Override
87+
public AttributionLayout execute(AttributionMeasure measure) {
88+
float width = measure.getLogoContainerWidth() + measure.getTextViewShortContainerWidth();
89+
boolean fitBounds = width <= measure.getMaxSizeShort();
90+
if (fitBounds) {
91+
PointF anchor = calculateAnchor(measure.snapshot, measure.textViewShort, measure.margin);
92+
return new AttributionLayout(measure.logoSmall, anchor, true);
93+
}
94+
return null;
95+
}
96+
}
97+
98+
private static class LongTextCommand implements Command {
99+
@Override
100+
public AttributionLayout execute(AttributionMeasure measure) {
101+
float width = measure.getTextViewContainerWidth() + measure.margin;
102+
boolean fitBounds = width <= measure.getMaxSize();
103+
if (fitBounds) {
104+
return new AttributionLayout(null, calculateAnchor(measure.snapshot, measure.textView, measure.margin), false);
105+
}
106+
return null;
107+
}
108+
}
109+
110+
private static class ShortTextCommand implements Command {
111+
@Override
112+
public AttributionLayout execute(AttributionMeasure measure) {
113+
float width = measure.getTextViewShortContainerWidth() + measure.margin;
114+
boolean fitBounds = width <= measure.getMaxSizeShort();
115+
if (fitBounds) {
116+
PointF anchor = calculateAnchor(measure.snapshot, measure.textViewShort, measure.margin);
117+
return new AttributionLayout(null, anchor, true);
118+
}
119+
return null;
120+
}
121+
}
122+
123+
private static class NoTextCommand implements Command {
124+
@Override
125+
public AttributionLayout execute(AttributionMeasure measure) {
126+
return new AttributionLayout(null, null, false);
127+
}
128+
}
129+
130+
private static PointF calculateAnchor(Bitmap snapshot, TextView textView, float margin) {
131+
return new PointF(
132+
snapshot.getWidth() - textView.getMeasuredWidth() - margin,
133+
snapshot.getHeight() - margin - textView.getMeasuredHeight()
134+
);
135+
}
136+
137+
public TextView getTextView() {
138+
return shorterText ? textViewShort : textView;
139+
}
140+
141+
private class Chain {
142+
public List<Command> commands;
143+
144+
Chain(Command... commands) {
145+
this.commands = Arrays.asList(commands);
146+
}
147+
148+
public AttributionLayout start(AttributionMeasure measure) {
149+
AttributionLayout attributionLayout = null;
150+
for (Command command : commands) {
151+
attributionLayout = command.execute(measure);
152+
if (attributionLayout != null) {
153+
break;
154+
}
155+
}
156+
return attributionLayout;
157+
}
158+
}
159+
160+
public interface Command {
161+
AttributionLayout execute(AttributionMeasure measure);
162+
}
163+
164+
private float getTextViewContainerWidth() {
165+
return textView.getMeasuredWidth() + margin;
166+
}
167+
168+
private float getLogoContainerWidth() {
169+
return logo.getWidth() + (2 * margin);
170+
}
171+
172+
private float getTextViewShortContainerWidth() {
173+
return textViewShort.getMeasuredWidth() + margin;
174+
}
175+
176+
private float getLogoSmallContainerWidth() {
177+
return logoSmall.getWidth() + (2 * margin);
178+
}
179+
180+
private float getMaxSize() {
181+
return snapshot.getWidth() * 8 / 10;
182+
}
183+
184+
private float getMaxSizeShort() {
185+
return snapshot.getWidth();
186+
}
187+
188+
public static class Builder {
189+
private Bitmap snapshot;
190+
private Bitmap logo;
191+
private Bitmap logoSmall;
192+
private TextView textView;
193+
private TextView textViewShort;
194+
private float marginPadding;
195+
196+
public Builder setSnapshot(Bitmap snapshot) {
197+
this.snapshot = snapshot;
198+
return this;
199+
}
200+
201+
public Builder setLogo(Bitmap logo) {
202+
this.logo = logo;
203+
return this;
204+
}
205+
206+
public Builder setLogoSmall(Bitmap logoSmall) {
207+
this.logoSmall = logoSmall;
208+
return this;
209+
}
210+
211+
public Builder setTextView(TextView textView) {
212+
this.textView = textView;
213+
return this;
214+
}
215+
216+
public Builder setTextViewShort(TextView textViewShort) {
217+
this.textViewShort = textViewShort;
218+
return this;
219+
}
220+
221+
public Builder setMarginPadding(float marginPadding) {
222+
this.marginPadding = marginPadding;
223+
return this;
224+
}
225+
226+
public AttributionMeasure build() {
227+
return new AttributionMeasure(snapshot, logo, logoSmall, textView, textViewShort, marginPadding);
228+
}
229+
}
230+
}

0 commit comments

Comments
 (0)