Skip to content

Commit

Permalink
fix(KmlLayer): Update constructor to take in a Context instead (#631)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
arriolac committed Feb 22, 2020
1 parent 4ce5122 commit 731f7e6
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,23 @@
package com.google.maps.android.utils.demo;

import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.maps.android.collections.GroundOverlayManager;
import com.google.maps.android.collections.MarkerManager;
import com.google.maps.android.collections.PolygonManager;
import com.google.maps.android.collections.PolylineManager;
import com.google.maps.android.data.Feature;
import com.google.maps.android.data.Renderer;
import com.google.maps.android.data.kml.KmlContainer;
import com.google.maps.android.data.kml.KmlLayer;
import com.google.maps.android.data.kml.KmlPlacemark;
Expand Down Expand Up @@ -92,6 +101,35 @@ private void moveCameraToKml(KmlLayer kmlLayer) {
}
}

private Renderer.ImagesCache getImagesCache() {
final RetainFragment retainFragment =
RetainFragment.findOrCreateRetainFragment(getSupportFragmentManager());
return retainFragment.mImagesCache;
}

/**
* Fragment for retaining the bitmap cache between configuration changes.
*/
public static class RetainFragment extends Fragment {
private static final String TAG = RetainFragment.class.getName();
Renderer.ImagesCache mImagesCache;

static RetainFragment findOrCreateRetainFragment(FragmentManager fm) {
RetainFragment fragment = (RetainFragment) fm.findFragmentByTag(TAG);
if (fragment == null) {
fragment = new RetainFragment();
fm.beginTransaction().add(fragment, TAG).commit();
}
return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
}

private class LoadLocalKmlFile extends AsyncTask<String, Void, KmlLayer> {
private final int mResourceId;

Expand Down Expand Up @@ -135,8 +173,14 @@ protected KmlLayer doInBackground(String... params) {
}
buffer.flush();
try {
return new KmlLayer(mMap, new ByteArrayInputStream(buffer.toByteArray()),
KmlDemoActivity.this);
return new KmlLayer(mMap,
new ByteArrayInputStream(buffer.toByteArray()),
KmlDemoActivity.this,
new MarkerManager(mMap),
new PolygonManager(mMap),
new PolylineManager(mMap),
new GroundOverlayManager(mMap),
getImagesCache());
} catch (XmlPullParserException e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public void onFeatureClick(Feature feature) {
KmlLayer kmlPolygonLayer;
try {
// KML Polyline
kmlPolylineLayer = new KmlLayer(getMap(), R.raw.south_london_line_kml, this, markerManager, polygonManager, polylineManager, groundOverlayManager);
kmlPolylineLayer = new KmlLayer(getMap(), R.raw.south_london_line_kml, this, markerManager, polygonManager, polylineManager, groundOverlayManager, null);
kmlPolylineLayer.addLayerToMap();
kmlPolylineLayer.setOnFeatureClickListener(new KmlLayer.OnFeatureClickListener() {
@Override
Expand All @@ -140,7 +140,7 @@ public void onFeatureClick(Feature feature) {
});

// KML Polygon
kmlPolygonLayer = new KmlLayer(getMap(), R.raw.south_london_square_kml, this, markerManager, polygonManager, polylineManager, groundOverlayManager);
kmlPolygonLayer = new KmlLayer(getMap(), R.raw.south_london_square_kml, this, markerManager, polygonManager, polylineManager, groundOverlayManager, null);
kmlPolygonLayer.addLayerToMap();
kmlPolygonLayer.setOnFeatureClickListener(new KmlLayer.OnFeatureClickListener() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void setUp() throws Exception {
mParser = new KmlParser(parser);
mParser.parseKml();

mRenderer = new KmlRenderer(mMap1, null, null, null, null, null);
mRenderer = new KmlRenderer(mMap1, null, null, null, null, null, null);
mRenderer.storeKmlData(mParser.getStyles(), mParser.getStyleMaps(), mParser.getPlacemarks(),
mParser.getContainers(), mParser.getGroundOverlays());
}
Expand Down
60 changes: 17 additions & 43 deletions library/src/main/java/com/google/maps/android/data/Renderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import com.google.maps.android.data.kml.KmlStyle;
import com.google.maps.android.data.kml.KmlUtil;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.Html;
Expand All @@ -69,6 +70,7 @@
import java.util.Map;
import java.util.Set;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
Expand Down Expand Up @@ -108,7 +110,7 @@ public class Renderer {

private boolean mLayerOnMap;

private FragmentActivity mActivity;
private Context mContext;

private ArrayList<KmlContainer> mContainers;

Expand All @@ -127,29 +129,24 @@ public class Renderer {
* Creates a new Renderer object for KML features
*
* @param map map to place objects on
* @param activity activity needed to add info windows and retain bitmap cache fragment
* @param context the Context
* @param markerManager marker manager to create marker collection from
* @param polygonManager polygon manager to create polygon collection from
* @param polylineManager polyline manager to create polyline collection from
* @param groundOverlayManager ground overlay manager to create ground overlay collection from
* @param imagesCache an optional ImagesCache to be used for caching images fetched
*/
public Renderer(GoogleMap map, FragmentActivity activity, MarkerManager markerManager, PolygonManager polygonManager, PolylineManager polylineManager, GroundOverlayManager groundOverlayManager) {
public Renderer(GoogleMap map,
Context context,
MarkerManager markerManager,
PolygonManager polygonManager,
PolylineManager polylineManager,
GroundOverlayManager groundOverlayManager,
@Nullable ImagesCache imagesCache) {
this(map, new HashSet<String>(), null, null, null, new BiMultiMap<Feature>(), markerManager, polygonManager, polylineManager, groundOverlayManager);
mActivity = activity;
ImagesCache imagesCache = null;
RetainFragment retainFragment = null;
if (activity != null) {
retainFragment = RetainFragment.findOrCreateRetainFragment(activity.getSupportFragmentManager());
imagesCache = retainFragment.mImagesCache;
}
if (imagesCache == null) {
imagesCache = new ImagesCache();
if (retainFragment != null) {
retainFragment.mImagesCache = imagesCache;
}
}
mImagesCache = imagesCache;
mContext = context;
mStylesRenderer = new HashMap<>();
mImagesCache = (imagesCache == null) ? new ImagesCache() : imagesCache;
}

/**
Expand Down Expand Up @@ -211,30 +208,7 @@ private Renderer(GoogleMap map,
}
}

/**
* Fragment for retaining the bitmap cache between configuration changes.
*/
public static class RetainFragment extends Fragment {
private static final String TAG = RetainFragment.class.getName();
ImagesCache mImagesCache;

static RetainFragment findOrCreateRetainFragment(FragmentManager fm) {
RetainFragment fragment = (RetainFragment) fm.findFragmentByTag(TAG);
if (fragment == null) {
fragment = new RetainFragment();
fm.beginTransaction().add(fragment, TAG).commit();
}
return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
}

private static class ImagesCache {
public static final class ImagesCache {

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

int unscaledWidth = unscaledBitmap.getWidth();
Expand Down Expand Up @@ -1184,7 +1158,7 @@ public View getInfoWindow(Marker arg0) {
}

public View getInfoContents(Marker arg0) {
View view = LayoutInflater.from(mActivity).inflate(R.layout.amu_info_window, null);
View view = LayoutInflater.from(mContext).inflate(R.layout.amu_info_window, null);
TextView infoWindowText = view.findViewById(R.id.window);
if (arg0.getSnippet() != null) {
infoWindowText.setText(Html.fromHtml(arg0.getTitle() + "<br>" + arg0.getSnippet()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
*/
package com.google.maps.android.data.kml;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import androidx.annotation.RawRes;
import androidx.fragment.app.FragmentActivity;

import com.google.android.gms.maps.GoogleMap;
Expand All @@ -27,6 +29,7 @@
import com.google.maps.android.collections.MarkerManager;
import com.google.maps.android.collections.PolygonManager;
import com.google.maps.android.collections.PolylineManager;
import com.google.maps.android.data.Renderer;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
Expand All @@ -51,13 +54,13 @@ public class KmlLayer extends Layer {
*
* @param map GoogleMap object
* @param resourceId Raw resource KML or KMZ file
* @param activity Activity object
* @param context The Context
* @throws XmlPullParserException if file cannot be parsed
* @throws IOException if I/O error
*/
public KmlLayer(GoogleMap map, int resourceId, FragmentActivity activity)
public KmlLayer(GoogleMap map, int resourceId, Context context)
throws XmlPullParserException, IOException {
this(map, activity.getResources().openRawResource(resourceId), activity, new MarkerManager(map), new PolygonManager(map), new PolylineManager(map), new GroundOverlayManager(map));
this(map, context.getResources().openRawResource(resourceId), context, new MarkerManager(map), new PolygonManager(map), new PolylineManager(map), new GroundOverlayManager(map), null);
}

/**
Expand All @@ -67,13 +70,13 @@ public KmlLayer(GoogleMap map, int resourceId, FragmentActivity activity)
*
* @param map GoogleMap object
* @param stream InputStream containing KML or KMZ file
* @param activity Activity object
* @param context The Context
* @throws XmlPullParserException if file cannot be parsed
* @throws IOException if I/O error
*/
public KmlLayer(GoogleMap map, InputStream stream, FragmentActivity activity)
public KmlLayer(GoogleMap map, InputStream stream, Context context)
throws XmlPullParserException, IOException {
this(map, stream, activity, new MarkerManager(map), new PolygonManager(map), new PolylineManager(map), new GroundOverlayManager(map));
this(map, stream, context, new MarkerManager(map), new PolygonManager(map), new PolylineManager(map), new GroundOverlayManager(map), null);
}

/**
Expand All @@ -86,17 +89,25 @@ public KmlLayer(GoogleMap map, InputStream stream, FragmentActivity activity)
*
* @param map GoogleMap object
* @param resourceId Raw resource KML or KMZ file
* @param activity Activity object
* @param context The Context
* @param markerManager marker manager to create marker collection from
* @param polygonManager polygon manager to create polygon collection from
* @param polylineManager polyline manager to create polyline collection from
* @param groundOverlayManager ground overlay manager to create ground overlay collection from
* @param cache cache to be used for fetched images
* @throws XmlPullParserException if file cannot be parsed
* @throws IOException if I/O error
*/
public KmlLayer(GoogleMap map, int resourceId, FragmentActivity activity, MarkerManager markerManager, PolygonManager polygonManager, PolylineManager polylineManager, GroundOverlayManager groundOverlayManager)
public KmlLayer(GoogleMap map,
@RawRes int resourceId,
Context context,
MarkerManager markerManager,
PolygonManager polygonManager,
PolylineManager polylineManager,
GroundOverlayManager groundOverlayManager,
Renderer.ImagesCache cache)
throws XmlPullParserException, IOException {
this(map, activity.getResources().openRawResource(resourceId), activity, markerManager, polygonManager, polylineManager, groundOverlayManager);
this(map, context.getResources().openRawResource(resourceId), context, markerManager, polygonManager, polylineManager, groundOverlayManager, cache);
}

/**
Expand All @@ -109,20 +120,28 @@ public KmlLayer(GoogleMap map, int resourceId, FragmentActivity activity, Marker
*
* @param map GoogleMap object
* @param stream InputStream containing KML or KMZ file
* @param activity Activity object
* @param context The Context
* @param markerManager marker manager to create marker collection from
* @param polygonManager polygon manager to create polygon collection from
* @param polylineManager polyline manager to create polyline collection from
* @param groundOverlayManager ground overlay manager to create ground overlay collection from
* @param cache cache to be used for fetched images
* @throws XmlPullParserException if file cannot be parsed
* @throws IOException if I/O error
*/
public KmlLayer(GoogleMap map, InputStream stream, FragmentActivity activity, MarkerManager markerManager, PolygonManager polygonManager, PolylineManager polylineManager, GroundOverlayManager groundOverlayManager)
public KmlLayer(GoogleMap map,
InputStream stream,
Context context,
MarkerManager markerManager,
PolygonManager polygonManager,
PolylineManager polylineManager,
GroundOverlayManager groundOverlayManager,
Renderer.ImagesCache cache)
throws XmlPullParserException, IOException {
if (stream == null) {
throw new IllegalArgumentException("KML InputStream cannot be null");
}
KmlRenderer renderer = new KmlRenderer(map, activity, markerManager, polygonManager, polylineManager, groundOverlayManager);
KmlRenderer renderer = new KmlRenderer(map, context, markerManager, polygonManager, polylineManager, groundOverlayManager, cache);

BufferedInputStream bis = new BufferedInputStream(stream);
bis.mark(1024);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
* Copyright 2020 Google Inc.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -15,12 +15,13 @@
*/
package com.google.maps.android.data.kml;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;

import androidx.fragment.app.FragmentActivity;
import androidx.annotation.Nullable;

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

private ArrayList<KmlContainer> mContainers;

/* package */ KmlRenderer(GoogleMap map, FragmentActivity activity, MarkerManager markerManager, PolygonManager polygonManager, PolylineManager polylineManager, GroundOverlayManager groundOverlayManager) {
super(map, activity, markerManager, polygonManager, polylineManager, groundOverlayManager);
/* package */ KmlRenderer(GoogleMap map,
Context context,
MarkerManager markerManager,
PolygonManager polygonManager,
PolylineManager polylineManager,
GroundOverlayManager groundOverlayManager,
@Nullable ImagesCache imagesCache) {
super(map, context, markerManager, polygonManager, polylineManager, groundOverlayManager, imagesCache);
mGroundOverlayUrls = new HashSet<>();
mMarkerIconsDownloaded = false;
mGroundOverlayImagesDownloaded = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void testAssignStyleMap() {
KmlStyle redStyle = new KmlStyle();
styles.put("BlueValue", blueStyle);
styles.put("RedValue", redStyle);
KmlRenderer renderer = new KmlRenderer(null, null, null, null, null, null);
KmlRenderer renderer = new KmlRenderer(null, null, null, null, null, null, null);
renderer.assignStyleMap(styleMap, styles);
assertNotNull(styles.get("BlueKey"));
assertEquals(styles.get("BlueKey"), styles.get("BlueValue"));
Expand Down

0 comments on commit 731f7e6

Please sign in to comment.