com.tigerpenguin.places.model.Photo.java Source code

Java tutorial

Introduction

Here is the source code for com.tigerpenguin.places.model.Photo.java

Source

/*
 * Copyright (C) 2014 Brian Lee
 *
 * 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];
        }
    };
}