From 3d465a9fb4bec4ed117876698d24f74cd33da397 Mon Sep 17 00:00:00 2001 From: jamesconradshea Date: Fri, 1 Apr 2016 13:57:15 -0500 Subject: [PATCH] Add HTML attributions to Photos --- .../places/GooglePlacesInterface.java | 9 ++++-- src/main/java/se/walkercrou/places/Photo.java | 28 +++++++++++++++++-- src/main/java/se/walkercrou/places/Place.java | 19 +++++++++---- 3 files changed, 45 insertions(+), 11 deletions(-) diff --git a/src/main/java/se/walkercrou/places/GooglePlacesInterface.java b/src/main/java/se/walkercrou/places/GooglePlacesInterface.java index e90d3be..6bd3bb8 100644 --- a/src/main/java/se/walkercrou/places/GooglePlacesInterface.java +++ b/src/main/java/se/walkercrou/places/GooglePlacesInterface.java @@ -1,9 +1,9 @@ package se.walkercrou.places; -import se.walkercrou.places.exception.GooglePlacesException; - import java.util.List; +import se.walkercrou.places.exception.GooglePlacesException; + /** * A Java binding for the Google Places API: * https://developers.google.com/places/ @@ -107,6 +107,11 @@ public interface GooglePlacesInterface extends Types, Statuses { */ String ARRAY_ALT_IDS = "alt_ids"; + /** + * Array of required HTML attributions on photos + */ + String ARRAY_HTML_ATTRIBUTIONS = "html_attributions"; + /** * If the place is opened now */ diff --git a/src/main/java/se/walkercrou/places/Photo.java b/src/main/java/se/walkercrou/places/Photo.java index 8b076d8..d85f406 100644 --- a/src/main/java/se/walkercrou/places/Photo.java +++ b/src/main/java/se/walkercrou/places/Photo.java @@ -1,12 +1,15 @@ package se.walkercrou.places; -import se.walkercrou.places.exception.GooglePlacesException; +import static se.walkercrou.places.GooglePlaces.MAX_PHOTO_SIZE; -import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.InputStream; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import javax.imageio.ImageIO; -import static se.walkercrou.places.GooglePlaces.MAX_PHOTO_SIZE; +import se.walkercrou.places.exception.GooglePlacesException; /** * Represents a referenced photo. @@ -15,6 +18,7 @@ public class Photo { private final Place place; private final String reference; private final int width, height; + private final List htmlAttributions = new ArrayList<>(); private InputStream image; protected Photo(Place place, String reference, int width, int height) { @@ -96,4 +100,22 @@ public int getWidth() { public int getHeight() { return height; } + + /** + * Returns the HTML attributions of the photo. This array may be empty but is never null. + * + * @return photo attributions + */ + public List getHtmlAttributions() { + return Collections.unmodifiableList(htmlAttributions); + } + + /** + * Add an HTML attribution to the photo. + * + * @param attribution the attribution to add + */ + public void addHtmlAttribution(String attribution) { + htmlAttributions.add(attribution); + } } diff --git a/src/main/java/se/walkercrou/places/Place.java b/src/main/java/se/walkercrou/places/Place.java index 51354bf..ac8b498 100644 --- a/src/main/java/se/walkercrou/places/Place.java +++ b/src/main/java/se/walkercrou/places/Place.java @@ -1,18 +1,18 @@ package se.walkercrou.places; -import org.json.JSONArray; -import org.json.JSONObject; -import se.walkercrou.places.exception.GooglePlacesException; +import static se.walkercrou.places.GooglePlaces.*; -import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.InputStream; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; +import javax.imageio.ImageIO; -import static se.walkercrou.places.GooglePlaces.*; +import org.json.JSONArray; +import org.json.JSONObject; +import se.walkercrou.places.exception.GooglePlacesException; /** * Represents a place returned by Google Places API_ @@ -130,7 +130,14 @@ public static Place parseDetails(GooglePlaces client, String rawJson) { JSONObject jsonPhoto = jsonPhotos.getJSONObject(i); String photoReference = jsonPhoto.getString(STRING_PHOTO_REFERENCE); int width = jsonPhoto.getInt(INTEGER_WIDTH), height = jsonPhoto.getInt(INTEGER_HEIGHT); - photos.add(new Photo(place, photoReference, width, height)); + Photo photo = new Photo(place, photoReference, width, height); + JSONArray jsonAttributions = jsonPhoto.optJSONArray(ARRAY_HTML_ATTRIBUTIONS); + if (jsonAttributions != null) { + for (int j = 0; j < jsonAttributions.length(); j++) { + photo.addHtmlAttribution(jsonAttributions.getString(j)); + } + } + photos.add(photo); } }