Skip to content

Commit 731f7e6

Browse files
authored
fix(KmlLayer): Update constructor to take in a Context instead (#631)
* fix: Update KmlLayer to take in a Context and not a FragmentActivity. * Passing ImagesCache via constructor. * Have tests compile. * Remove import. * Fix compile error. * Address PR feedback.
1 parent 4ce5122 commit 731f7e6

File tree

7 files changed

+111
-67
lines changed

7 files changed

+111
-67
lines changed

demo/src/main/java/com/google/maps/android/utils/demo/KmlDemoActivity.java

+46-2
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,23 @@
1717
package com.google.maps.android.utils.demo;
1818

1919
import android.os.AsyncTask;
20+
import android.os.Bundle;
2021
import android.util.Log;
2122
import android.widget.Toast;
2223

24+
import androidx.fragment.app.Fragment;
25+
import androidx.fragment.app.FragmentManager;
26+
2327
import com.google.android.gms.maps.CameraUpdateFactory;
2428
import com.google.android.gms.maps.GoogleMap;
2529
import com.google.android.gms.maps.model.LatLng;
2630
import com.google.android.gms.maps.model.LatLngBounds;
31+
import com.google.maps.android.collections.GroundOverlayManager;
32+
import com.google.maps.android.collections.MarkerManager;
33+
import com.google.maps.android.collections.PolygonManager;
34+
import com.google.maps.android.collections.PolylineManager;
2735
import com.google.maps.android.data.Feature;
36+
import com.google.maps.android.data.Renderer;
2837
import com.google.maps.android.data.kml.KmlContainer;
2938
import com.google.maps.android.data.kml.KmlLayer;
3039
import com.google.maps.android.data.kml.KmlPlacemark;
@@ -92,6 +101,35 @@ private void moveCameraToKml(KmlLayer kmlLayer) {
92101
}
93102
}
94103

104+
private Renderer.ImagesCache getImagesCache() {
105+
final RetainFragment retainFragment =
106+
RetainFragment.findOrCreateRetainFragment(getSupportFragmentManager());
107+
return retainFragment.mImagesCache;
108+
}
109+
110+
/**
111+
* Fragment for retaining the bitmap cache between configuration changes.
112+
*/
113+
public static class RetainFragment extends Fragment {
114+
private static final String TAG = RetainFragment.class.getName();
115+
Renderer.ImagesCache mImagesCache;
116+
117+
static RetainFragment findOrCreateRetainFragment(FragmentManager fm) {
118+
RetainFragment fragment = (RetainFragment) fm.findFragmentByTag(TAG);
119+
if (fragment == null) {
120+
fragment = new RetainFragment();
121+
fm.beginTransaction().add(fragment, TAG).commit();
122+
}
123+
return fragment;
124+
}
125+
126+
@Override
127+
public void onCreate(Bundle savedInstanceState) {
128+
super.onCreate(savedInstanceState);
129+
setRetainInstance(true);
130+
}
131+
}
132+
95133
private class LoadLocalKmlFile extends AsyncTask<String, Void, KmlLayer> {
96134
private final int mResourceId;
97135

@@ -135,8 +173,14 @@ protected KmlLayer doInBackground(String... params) {
135173
}
136174
buffer.flush();
137175
try {
138-
return new KmlLayer(mMap, new ByteArrayInputStream(buffer.toByteArray()),
139-
KmlDemoActivity.this);
176+
return new KmlLayer(mMap,
177+
new ByteArrayInputStream(buffer.toByteArray()),
178+
KmlDemoActivity.this,
179+
new MarkerManager(mMap),
180+
new PolygonManager(mMap),
181+
new PolylineManager(mMap),
182+
new GroundOverlayManager(mMap),
183+
getImagesCache());
140184
} catch (XmlPullParserException e) {
141185
e.printStackTrace();
142186
}

demo/src/main/java/com/google/maps/android/utils/demo/MultiLayerDemoActivity.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public void onFeatureClick(Feature feature) {
128128
KmlLayer kmlPolygonLayer;
129129
try {
130130
// KML Polyline
131-
kmlPolylineLayer = new KmlLayer(getMap(), R.raw.south_london_line_kml, this, markerManager, polygonManager, polylineManager, groundOverlayManager);
131+
kmlPolylineLayer = new KmlLayer(getMap(), R.raw.south_london_line_kml, this, markerManager, polygonManager, polylineManager, groundOverlayManager, null);
132132
kmlPolylineLayer.addLayerToMap();
133133
kmlPolylineLayer.setOnFeatureClickListener(new KmlLayer.OnFeatureClickListener() {
134134
@Override
@@ -140,7 +140,7 @@ public void onFeatureClick(Feature feature) {
140140
});
141141

142142
// KML Polygon
143-
kmlPolygonLayer = new KmlLayer(getMap(), R.raw.south_london_square_kml, this, markerManager, polygonManager, polylineManager, groundOverlayManager);
143+
kmlPolygonLayer = new KmlLayer(getMap(), R.raw.south_london_square_kml, this, markerManager, polygonManager, polylineManager, groundOverlayManager, null);
144144
kmlPolygonLayer.addLayerToMap();
145145
kmlPolygonLayer.setOnFeatureClickListener(new KmlLayer.OnFeatureClickListener() {
146146
@Override

library/src/androidTest/java/com/google/maps/android/data/kml/KmlRendererTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public void setUp() throws Exception {
3636
mParser = new KmlParser(parser);
3737
mParser.parseKml();
3838

39-
mRenderer = new KmlRenderer(mMap1, null, null, null, null, null);
39+
mRenderer = new KmlRenderer(mMap1, null, null, null, null, null, null);
4040
mRenderer.storeKmlData(mParser.getStyles(), mParser.getStyleMaps(), mParser.getPlacemarks(),
4141
mParser.getContainers(), mParser.getGroundOverlays());
4242
}

library/src/main/java/com/google/maps/android/data/Renderer.java

+17-43
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import com.google.maps.android.data.kml.KmlStyle;
5454
import com.google.maps.android.data.kml.KmlUtil;
5555

56+
import android.content.Context;
5657
import android.graphics.Bitmap;
5758
import android.os.Bundle;
5859
import android.text.Html;
@@ -69,6 +70,7 @@
6970
import java.util.Map;
7071
import java.util.Set;
7172

73+
import androidx.annotation.Nullable;
7274
import androidx.fragment.app.Fragment;
7375
import androidx.fragment.app.FragmentActivity;
7476
import androidx.fragment.app.FragmentManager;
@@ -108,7 +110,7 @@ public class Renderer {
108110

109111
private boolean mLayerOnMap;
110112

111-
private FragmentActivity mActivity;
113+
private Context mContext;
112114

113115
private ArrayList<KmlContainer> mContainers;
114116

@@ -127,29 +129,24 @@ public class Renderer {
127129
* Creates a new Renderer object for KML features
128130
*
129131
* @param map map to place objects on
130-
* @param activity activity needed to add info windows and retain bitmap cache fragment
132+
* @param context the Context
131133
* @param markerManager marker manager to create marker collection from
132134
* @param polygonManager polygon manager to create polygon collection from
133135
* @param polylineManager polyline manager to create polyline collection from
134136
* @param groundOverlayManager ground overlay manager to create ground overlay collection from
137+
* @param imagesCache an optional ImagesCache to be used for caching images fetched
135138
*/
136-
public Renderer(GoogleMap map, FragmentActivity activity, MarkerManager markerManager, PolygonManager polygonManager, PolylineManager polylineManager, GroundOverlayManager groundOverlayManager) {
139+
public Renderer(GoogleMap map,
140+
Context context,
141+
MarkerManager markerManager,
142+
PolygonManager polygonManager,
143+
PolylineManager polylineManager,
144+
GroundOverlayManager groundOverlayManager,
145+
@Nullable ImagesCache imagesCache) {
137146
this(map, new HashSet<String>(), null, null, null, new BiMultiMap<Feature>(), markerManager, polygonManager, polylineManager, groundOverlayManager);
138-
mActivity = activity;
139-
ImagesCache imagesCache = null;
140-
RetainFragment retainFragment = null;
141-
if (activity != null) {
142-
retainFragment = RetainFragment.findOrCreateRetainFragment(activity.getSupportFragmentManager());
143-
imagesCache = retainFragment.mImagesCache;
144-
}
145-
if (imagesCache == null) {
146-
imagesCache = new ImagesCache();
147-
if (retainFragment != null) {
148-
retainFragment.mImagesCache = imagesCache;
149-
}
150-
}
151-
mImagesCache = imagesCache;
147+
mContext = context;
152148
mStylesRenderer = new HashMap<>();
149+
mImagesCache = (imagesCache == null) ? new ImagesCache() : imagesCache;
153150
}
154151

155152
/**
@@ -211,30 +208,7 @@ private Renderer(GoogleMap map,
211208
}
212209
}
213210

214-
/**
215-
* Fragment for retaining the bitmap cache between configuration changes.
216-
*/
217-
public static class RetainFragment extends Fragment {
218-
private static final String TAG = RetainFragment.class.getName();
219-
ImagesCache mImagesCache;
220-
221-
static RetainFragment findOrCreateRetainFragment(FragmentManager fm) {
222-
RetainFragment fragment = (RetainFragment) fm.findFragmentByTag(TAG);
223-
if (fragment == null) {
224-
fragment = new RetainFragment();
225-
fm.beginTransaction().add(fragment, TAG).commit();
226-
}
227-
return fragment;
228-
}
229-
230-
@Override
231-
public void onCreate(Bundle savedInstanceState) {
232-
super.onCreate(savedInstanceState);
233-
setRetainInstance(true);
234-
}
235-
}
236-
237-
private static class ImagesCache {
211+
public static final class ImagesCache {
238212

239213
/**
240214
* Map of image URL to map of scale factor to BitmapDescriptors for point marker icons
@@ -405,7 +379,7 @@ protected BitmapDescriptor getCachedMarkerImage(String url, double scale) {
405379
* @return A scaled bitmap image
406380
*/
407381
private BitmapDescriptor scaleIcon(Bitmap unscaledBitmap, double scale) {
408-
float density = mActivity.getResources().getDisplayMetrics().density;
382+
float density = mContext.getResources().getDisplayMetrics().density;
409383
int minSize = (int) (MARKER_ICON_SIZE * density * scale);
410384

411385
int unscaledWidth = unscaledBitmap.getWidth();
@@ -1184,7 +1158,7 @@ public View getInfoWindow(Marker arg0) {
11841158
}
11851159

11861160
public View getInfoContents(Marker arg0) {
1187-
View view = LayoutInflater.from(mActivity).inflate(R.layout.amu_info_window, null);
1161+
View view = LayoutInflater.from(mContext).inflate(R.layout.amu_info_window, null);
11881162
TextView infoWindowText = view.findViewById(R.id.window);
11891163
if (arg0.getSnippet() != null) {
11901164
infoWindowText.setText(Html.fromHtml(arg0.getTitle() + "<br>" + arg0.getSnippet()));

library/src/main/java/com/google/maps/android/data/kml/KmlLayer.java

+31-12
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
*/
1616
package com.google.maps.android.data.kml;
1717

18+
import android.content.Context;
1819
import android.graphics.Bitmap;
1920
import android.graphics.BitmapFactory;
2021
import android.util.Log;
2122

23+
import androidx.annotation.RawRes;
2224
import androidx.fragment.app.FragmentActivity;
2325

2426
import com.google.android.gms.maps.GoogleMap;
@@ -27,6 +29,7 @@
2729
import com.google.maps.android.collections.MarkerManager;
2830
import com.google.maps.android.collections.PolygonManager;
2931
import com.google.maps.android.collections.PolylineManager;
32+
import com.google.maps.android.data.Renderer;
3033

3134
import org.xmlpull.v1.XmlPullParser;
3235
import org.xmlpull.v1.XmlPullParserException;
@@ -51,13 +54,13 @@ public class KmlLayer extends Layer {
5154
*
5255
* @param map GoogleMap object
5356
* @param resourceId Raw resource KML or KMZ file
54-
* @param activity Activity object
57+
* @param context The Context
5558
* @throws XmlPullParserException if file cannot be parsed
5659
* @throws IOException if I/O error
5760
*/
58-
public KmlLayer(GoogleMap map, int resourceId, FragmentActivity activity)
61+
public KmlLayer(GoogleMap map, int resourceId, Context context)
5962
throws XmlPullParserException, IOException {
60-
this(map, activity.getResources().openRawResource(resourceId), activity, new MarkerManager(map), new PolygonManager(map), new PolylineManager(map), new GroundOverlayManager(map));
63+
this(map, context.getResources().openRawResource(resourceId), context, new MarkerManager(map), new PolygonManager(map), new PolylineManager(map), new GroundOverlayManager(map), null);
6164
}
6265

6366
/**
@@ -67,13 +70,13 @@ public KmlLayer(GoogleMap map, int resourceId, FragmentActivity activity)
6770
*
6871
* @param map GoogleMap object
6972
* @param stream InputStream containing KML or KMZ file
70-
* @param activity Activity object
73+
* @param context The Context
7174
* @throws XmlPullParserException if file cannot be parsed
7275
* @throws IOException if I/O error
7376
*/
74-
public KmlLayer(GoogleMap map, InputStream stream, FragmentActivity activity)
77+
public KmlLayer(GoogleMap map, InputStream stream, Context context)
7578
throws XmlPullParserException, IOException {
76-
this(map, stream, activity, new MarkerManager(map), new PolygonManager(map), new PolylineManager(map), new GroundOverlayManager(map));
79+
this(map, stream, context, new MarkerManager(map), new PolygonManager(map), new PolylineManager(map), new GroundOverlayManager(map), null);
7780
}
7881

7982
/**
@@ -86,17 +89,25 @@ public KmlLayer(GoogleMap map, InputStream stream, FragmentActivity activity)
8689
*
8790
* @param map GoogleMap object
8891
* @param resourceId Raw resource KML or KMZ file
89-
* @param activity Activity object
92+
* @param context The Context
9093
* @param markerManager marker manager to create marker collection from
9194
* @param polygonManager polygon manager to create polygon collection from
9295
* @param polylineManager polyline manager to create polyline collection from
9396
* @param groundOverlayManager ground overlay manager to create ground overlay collection from
97+
* @param cache cache to be used for fetched images
9498
* @throws XmlPullParserException if file cannot be parsed
9599
* @throws IOException if I/O error
96100
*/
97-
public KmlLayer(GoogleMap map, int resourceId, FragmentActivity activity, MarkerManager markerManager, PolygonManager polygonManager, PolylineManager polylineManager, GroundOverlayManager groundOverlayManager)
101+
public KmlLayer(GoogleMap map,
102+
@RawRes int resourceId,
103+
Context context,
104+
MarkerManager markerManager,
105+
PolygonManager polygonManager,
106+
PolylineManager polylineManager,
107+
GroundOverlayManager groundOverlayManager,
108+
Renderer.ImagesCache cache)
98109
throws XmlPullParserException, IOException {
99-
this(map, activity.getResources().openRawResource(resourceId), activity, markerManager, polygonManager, polylineManager, groundOverlayManager);
110+
this(map, context.getResources().openRawResource(resourceId), context, markerManager, polygonManager, polylineManager, groundOverlayManager, cache);
100111
}
101112

102113
/**
@@ -109,20 +120,28 @@ public KmlLayer(GoogleMap map, int resourceId, FragmentActivity activity, Marker
109120
*
110121
* @param map GoogleMap object
111122
* @param stream InputStream containing KML or KMZ file
112-
* @param activity Activity object
123+
* @param context The Context
113124
* @param markerManager marker manager to create marker collection from
114125
* @param polygonManager polygon manager to create polygon collection from
115126
* @param polylineManager polyline manager to create polyline collection from
116127
* @param groundOverlayManager ground overlay manager to create ground overlay collection from
128+
* @param cache cache to be used for fetched images
117129
* @throws XmlPullParserException if file cannot be parsed
118130
* @throws IOException if I/O error
119131
*/
120-
public KmlLayer(GoogleMap map, InputStream stream, FragmentActivity activity, MarkerManager markerManager, PolygonManager polygonManager, PolylineManager polylineManager, GroundOverlayManager groundOverlayManager)
132+
public KmlLayer(GoogleMap map,
133+
InputStream stream,
134+
Context context,
135+
MarkerManager markerManager,
136+
PolygonManager polygonManager,
137+
PolylineManager polylineManager,
138+
GroundOverlayManager groundOverlayManager,
139+
Renderer.ImagesCache cache)
121140
throws XmlPullParserException, IOException {
122141
if (stream == null) {
123142
throw new IllegalArgumentException("KML InputStream cannot be null");
124143
}
125-
KmlRenderer renderer = new KmlRenderer(map, activity, markerManager, polygonManager, polylineManager, groundOverlayManager);
144+
KmlRenderer renderer = new KmlRenderer(map, context, markerManager, polygonManager, polylineManager, groundOverlayManager, cache);
126145

127146
BufferedInputStream bis = new BufferedInputStream(stream);
128147
bis.mark(1024);

library/src/main/java/com/google/maps/android/data/kml/KmlRenderer.java

+13-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/*
22
* Copyright 2020 Google Inc.
3-
*
3+
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
7-
*
7+
*
88
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
9+
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -15,12 +15,13 @@
1515
*/
1616
package com.google.maps.android.data.kml;
1717

18+
import android.content.Context;
1819
import android.graphics.Bitmap;
1920
import android.graphics.BitmapFactory;
2021
import android.os.AsyncTask;
2122
import android.util.Log;
2223

23-
import androidx.fragment.app.FragmentActivity;
24+
import androidx.annotation.Nullable;
2425

2526
import com.google.android.gms.maps.GoogleMap;
2627
import com.google.android.gms.maps.model.BitmapDescriptor;
@@ -66,8 +67,14 @@ public class KmlRenderer extends Renderer {
6667

6768
private ArrayList<KmlContainer> mContainers;
6869

69-
/* package */ KmlRenderer(GoogleMap map, FragmentActivity activity, MarkerManager markerManager, PolygonManager polygonManager, PolylineManager polylineManager, GroundOverlayManager groundOverlayManager) {
70-
super(map, activity, markerManager, polygonManager, polylineManager, groundOverlayManager);
70+
/* package */ KmlRenderer(GoogleMap map,
71+
Context context,
72+
MarkerManager markerManager,
73+
PolygonManager polygonManager,
74+
PolylineManager polylineManager,
75+
GroundOverlayManager groundOverlayManager,
76+
@Nullable ImagesCache imagesCache) {
77+
super(map, context, markerManager, polygonManager, polylineManager, groundOverlayManager, imagesCache);
7178
mGroundOverlayUrls = new HashSet<>();
7279
mMarkerIconsDownloaded = false;
7380
mGroundOverlayImagesDownloaded = false;

library/src/test/java/com/google/maps/android/data/kml/KmlRendererTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public void testAssignStyleMap() {
3333
KmlStyle redStyle = new KmlStyle();
3434
styles.put("BlueValue", blueStyle);
3535
styles.put("RedValue", redStyle);
36-
KmlRenderer renderer = new KmlRenderer(null, null, null, null, null, null);
36+
KmlRenderer renderer = new KmlRenderer(null, null, null, null, null, null, null);
3737
renderer.assignStyleMap(styleMap, styles);
3838
assertNotNull(styles.get("BlueKey"));
3939
assertEquals(styles.get("BlueKey"), styles.get("BlueValue"));

0 commit comments

Comments
 (0)