Android Open Source - AndroidPlaces Photo






From Project

Back to project page AndroidPlaces.

License

The source code is released under:

Apache License

If you think the Android project AndroidPlaces listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2014 Brian Lee//www. j av a  2s  . co  m
 *
 * 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.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.tigerpenguin.places.model;

import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.Spanned;
import android.widget.ImageView;
import com.tigerpenguin.places.R;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Photo extends JsonModel implements Parcelable {

    private static final int MAX_RESOLUTION = 1600;
    private static final String BASE_URL = "https://maps.googleapis.com/maps/api/place/photo?";
    private static final String PARAM_API_KEY = "key=";
    private static final String PARAM_PHOTO_REFERENCE = "&photoreference=";
    private static final String PARAM_MAX_WIDTH = "&maxwidth=";
    private static final String PARAM_MAX_HEIGHT = "&maxheight=";

    @JsonProperty(PHOTO_REFERENCE)
    private String photoReference;

    @JsonProperty(HEIGHT)
    private int height;

    @JsonProperty(WIDTH)
    private int width;

    @JsonProperty(HTML_ATTRIBUTIONS)
    private List<String> htmlAttributions;

    private Spanned spannedHtmlAttribution;

    public Photo() {
        // required for Jackson
    }

    public Photo(Parcel in) {
        photoReference = in.readString();
        height = in.readInt();
        width = in.readInt();
        htmlAttributions = new ArrayList<String>();
        in.readStringList(htmlAttributions);
    }

    public String getUrl(Context context, int maxWidthPx, int maxHeightPx) {

        if (maxWidthPx <= 0 && maxHeightPx == 0) {
            throw new IllegalArgumentException("Max width or max height must be defined!");
        }

        if (maxWidthPx > MAX_RESOLUTION) {
            maxWidthPx = MAX_RESOLUTION;
        }

        if (maxHeightPx > MAX_RESOLUTION) {
            maxHeightPx = MAX_RESOLUTION;
        }

        StringBuilder sb = new StringBuilder();
        sb.append(BASE_URL).append(PARAM_API_KEY).append(context.getString(R.string.placesApiKey))
                .append(PARAM_PHOTO_REFERENCE).append(photoReference);
        if (maxWidthPx > 0) {
            sb.append(PARAM_MAX_WIDTH).append(String.valueOf(maxWidthPx));
        }
        if (maxHeightPx > 0) {
            sb.append(PARAM_MAX_HEIGHT).append(String.valueOf(maxHeightPx));
        }
        return sb.toString();
    }

    public void loadImage(Context context, ImageView imageView) {
        int width = imageView.getLayoutParams().width;
        if (width <= 0) {
            width = imageView.getMeasuredWidth();
        }

        int height = imageView.getLayoutParams().height;
        if (height <= 0) {
            height = imageView.getMeasuredHeight();
        }
        Picasso.with(context).load(getUrl(context, width, height)).into(imageView);
    }

    public final boolean hasHtmlAttributions() {
        return htmlAttributions != null && !htmlAttributions.isEmpty();
    }

    public final List<String> getHtmlAttributions() {
        return Collections.unmodifiableList(htmlAttributions);
    }

    public final Spanned getSpannedHtmlAttribution() {
        if (spannedHtmlAttribution == null & hasHtmlAttributions()) {
            spannedHtmlAttribution = squashHtmlStringList(htmlAttributions, "\n");
        }
        return spannedHtmlAttribution;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(photoReference);
        dest.writeInt(height);
        dest.writeInt(width);
        dest.writeStringList(htmlAttributions);
    }

    public static final Creator<Photo> CREATOR = new Creator<Photo>() {
        @Override
        public Photo createFromParcel(Parcel source) {
            return new Photo(source);
        }

        @Override
        public Photo[] newArray(int size) {
            return new Photo[size];
        }
    };
}




Java Source Code List

com.tigerpenguin.demo.places.PlaceDetailActivity.java
com.tigerpenguin.demo.places.PlaceResultAdapter.java
com.tigerpenguin.demo.places.PlaceResultsFragment.java
com.tigerpenguin.demo.places.PlaceReviewAdapter.java
com.tigerpenguin.demo.places.PlaceReviewsFragment.java
com.tigerpenguin.demo.places.PlaceThumbnailAdapter.java
com.tigerpenguin.demo.places.PlaceThumbnailsFragment.java
com.tigerpenguin.demo.places.PlacesDemo.java
com.tigerpenguin.places.PlacesTest.java
com.tigerpenguin.places.deserializer.HtmlStringDeserializer.java
com.tigerpenguin.places.deserializer.PlaceTypeDeserializer.java
com.tigerpenguin.places.model.AddressComponent.java
com.tigerpenguin.places.model.AspectRating.java
com.tigerpenguin.places.model.AspectType.java
com.tigerpenguin.places.model.DayOfWeek.java
com.tigerpenguin.places.model.DayTime.java
com.tigerpenguin.places.model.Geometry.java
com.tigerpenguin.places.model.JsonModel.java
com.tigerpenguin.places.model.Language.java
com.tigerpenguin.places.model.OpeningHours.java
com.tigerpenguin.places.model.Period.java
com.tigerpenguin.places.model.Photo.java
com.tigerpenguin.places.model.PlaceDetail.java
com.tigerpenguin.places.model.PlaceLocation.java
com.tigerpenguin.places.model.PlaceType.java
com.tigerpenguin.places.model.Place.java
com.tigerpenguin.places.model.PriceLevel.java
com.tigerpenguin.places.model.RankBy.java
com.tigerpenguin.places.model.Review.java
com.tigerpenguin.places.model.StatusCode.java
com.tigerpenguin.places.request.InvalidRequestException.java
com.tigerpenguin.places.request.NearbySearchRequestTest.java
com.tigerpenguin.places.request.NearbySearchRequest.java
com.tigerpenguin.places.request.PlaceDetailRequestTest.java
com.tigerpenguin.places.request.PlaceDetailRequest.java
com.tigerpenguin.places.request.PlaceSearchRequestTest.java
com.tigerpenguin.places.request.PlaceSearchRequest.java
com.tigerpenguin.places.request.PlacesRequestTest.java
com.tigerpenguin.places.request.PlacesRequest.java
com.tigerpenguin.places.request.RequestTest.java
com.tigerpenguin.places.response.NearbySearchResponseFactory.java
com.tigerpenguin.places.response.NearbySearchResponse.java
com.tigerpenguin.places.response.PlaceDetailResponseFactory.java
com.tigerpenguin.places.response.PlaceDetailResponse.java
com.tigerpenguin.places.response.PlaceSearchResponse.java
com.tigerpenguin.places.response.PlacesResponseFactory.java
com.tigerpenguin.places.response.PlacesResponse.java
com.tigerpenguin.places.responsehandler.NearbySearchHandlerFragment.java
com.tigerpenguin.places.responsehandler.PlaceDetailResponseHandlerFragment.java
com.tigerpenguin.places.responsehandler.PlacesResponseHandler.java
com.tigerpenguin.widget.simpleratingbar.MockAttributes.java
com.tigerpenguin.widget.simpleratingbar.SimpleRatingBarAttributes.java
com.tigerpenguin.widget.simpleratingbar.SimpleRatingBarTest.java
com.tigerpenguin.widget.simpleratingbar.SimpleRatingBar.java