Android Open Source - bv-android-sdk Bazaar Review






From Project

Back to project page bv-android-sdk.

License

The source code is released under:

Apache License

If you think the Android project bv-android-sdk 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

package com.example.productwidgetexample;
/*from   w w  w.j a v a 2s  .  com*/

import java.io.ByteArrayOutputStream;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

/**
 * BazaarReview.java <br>
 * ProductWidgetExample<br>
 * 
 * <p>
 * This is a very basic class used to represent a review in the Bazaarvoice
 * system for the use of this example app.
 * 
 * When fetching images, this chooses the thumbnail over the normal size. This
 * results in blurry images when blown up but it reduces download times and
 * memory usage. You might want to try using normal sized images and see what
 * the usage is like.
 * 
 * <p>
 * <b>Note: This does not include all functionality of the Bazaarvoice
 * system.</b>
 * 
 * <p>
 * Created on 6/29/12. Copyright (c) 2012 BazaarVoice. All rights reserved.
 * 
 * @author Bazaarvoice Engineering
 */
public class BazaarReview implements Parcelable {
  private int rating;
  private String title;
  private String authorId;
  private String dateString;
  private String reviewText;
  private String imageUrl;
  private Bitmap imageBitmap;

  /**
   * Parses the json response of a review query and builds the structure of
   * the object.
   * 
   * @param review
   *            a json object representing a review
   * @throws NumberFormatException
   *             if the rating is not formatted as an <code>int</code> (this
   *             should never occur)
   * @throws JSONException
   *             if there is a missing field in the json response
   */
  public BazaarReview(JSONObject review) throws NumberFormatException,
      JSONException {
    String ratingText = review.getString("Rating");
    if (!"null".equals(ratingText))
      rating = Integer.parseInt(ratingText);
    else
      rating = 0;

    title = review.getString("Title");
    authorId = review.getString("AuthorId");
    dateString = formatDateString(review.getString("SubmissionTime"));
    reviewText = review.getString("ReviewText");

    JSONArray photos = review.getJSONArray("Photos");
    if (photos != null && photos.length() != 0) {
      Log.i("Photo", photos.toString());
      JSONObject photo = photos.getJSONObject(0);
      JSONObject sizes = photo.getJSONObject("Sizes");
      JSONObject thumb = sizes.getJSONObject("thumbnail");
      imageUrl = thumb.getString("Url");
    } else {
      imageUrl = "";
    }
    imageBitmap = null;
  }

  /**
   * Formats the date string from a json response into a readable format.
   * 
   * <p>
   * Assumes "yyyy-mm-ddThh:mm:ss.000-06:00" format
   * 
   * @param timestamp
   *            the timestamp string from the json response
   * @return the formatted string
   */
  private String formatDateString(String timestamp) {
    // assuming "2011-12-09T08:55:35.000-06:00" format
    String year = timestamp.substring(0, 4);
    int monthNum = Integer.parseInt(timestamp.substring(5, 7));
    String day = timestamp.substring(8, 10);
    String month = "";

    switch (monthNum) {
    case 1:
      month = "January";
      break;
    case 2:
      month = "February";
      break;
    case 3:
      month = "March";
      break;
    case 4:
      month = "April";
      break;
    case 5:
      month = "May";
      break;
    case 6:
      month = "June";
      break;
    case 7:
      month = "July";
      break;
    case 8:
      month = "August";
      break;
    case 9:
      month = "September";
      break;
    case 10:
      month = "October";
      break;
    case 11:
      month = "November";
      break;
    case 12:
      month = "December";
      break;
    }

    return month + " " + day + ", " + year;

  }

  /**
   * Downloads the image from the URL stored in the object. This can be set by
   * constructing the object with a json response that has an image associated
   * with it. It also allows for passing a callback function for when the
   * download completes.
   * 
   * @param listener
   *            the callback function, or null if not needed
   */
  public boolean downloadImage(final OnImageDownloadComplete listener) {
    if ("".equals(imageUrl))
      return false;

    Thread t = new Thread(new Runnable() {

      @Override
      public void run() {
        ImageDownloader downloader = new ImageDownloader();
        imageBitmap = downloader.download(imageUrl);
        // imageBitmap = Bitmap.createScaledBitmap(temp, 150, 150,
        // true);
        if (listener != null)
          listener.onFinish(imageBitmap);
      }

    });
    t.start();
    return true;
  }

  /**
   * @return the rating
   */
  public int getRating() {
    return rating;
  }

  /**
   * @param rating
   *            the rating to set
   */
  public void setRating(int rating) {
    this.rating = rating;
  }

  /**
   * @return the title
   */
  public String getTitle() {
    return title;
  }

  /**
   * @param title
   *            the title to set
   */
  public void setTitle(String title) {
    this.title = title;
  }

  /**
   * @return the authorId
   */
  public String getAuthorId() {
    return authorId;
  }

  /**
   * @param authorId
   *            the authorId to set
   */
  public void setAuthorId(String authorId) {
    this.authorId = authorId;
  }

  /**
   * @return the dateString
   */
  public String getDateString() {
    return dateString;
  }

  /**
   * @param dateString
   *            the dateString to set
   */
  public void setDateString(String dateString) {
    this.dateString = dateString;
  }

  /**
   * @return the reviewText
   */
  public String getReviewText() {
    return reviewText;
  }

  /**
   * @param reviewText
   *            the reviewText to set
   */
  public void setReviewText(String reviewText) {
    this.reviewText = reviewText;
  }

  /**
   * @return the imageBitmap
   */
  public Bitmap getImageBitmap() {
    return imageBitmap;
  }

  /**
   * @param imageBitmap
   *            the imageBitmap to set
   */
  public void setImageBitmap(Bitmap imageBitmap) {
    this.imageBitmap = imageBitmap;
  }

  /*
   * Parcel methods and constructors
   */

  /**
   * Constructs a BazaarReview from a parcel that has another instance of this
   * class in it.
   * 
   * @param parcel the parcel holding an instance of BazaarReview
   */
  private BazaarReview(Parcel parcel) {
    readFromParcel(parcel);
  }

  /**
   * A Creator object used by the android system when unpacking a parcelled
   * instance of a BazaarReview object.
   */
  public static final Parcelable.Creator<BazaarReview> CREATOR = new Parcelable.Creator<BazaarReview>() {

    @Override
    public BazaarReview createFromParcel(Parcel source) {
      return new BazaarReview(source);
    }

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

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

  /**
   * Writes the object to a parcel. It writes the image as a byte array, or an
   * empty one if it is null.
   */
  @Override
  public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(rating);
    dest.writeString(title);
    dest.writeString(authorId);
    dest.writeString(dateString);
    dest.writeString(reviewText);
    dest.writeString(imageUrl);

    if (imageBitmap != null) {
      ByteArrayOutputStream stream = new ByteArrayOutputStream();
      imageBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
      byte[] byteArray = stream.toByteArray();
      dest.writeByteArray(byteArray);
    } else {
      dest.writeByteArray(new byte[0]);
    }
  }

  /**
   * Reads the fields of the parcelled object into this instance.
   * 
   * <b>Note: </b>The fields must be read in the same order they were written.
   * @param parcel
   */
  public void readFromParcel(Parcel parcel) {
    rating = parcel.readInt();
    title = parcel.readString();
    authorId = parcel.readString();
    dateString = parcel.readString();
    reviewText = parcel.readString();
    imageUrl = parcel.readString();

    byte[] byteArray = parcel.createByteArray();
    if (byteArray.length == 0) {
      imageBitmap = null;
    } else {
      imageBitmap = BitmapFactory.decodeByteArray(byteArray, 0,
          byteArray.length);
    }
  }

}




Java Source Code List

com.bazaarvoice.BazaarException.java
com.bazaarvoice.BazaarParams.java
com.bazaarvoice.BazaarRequest.java
com.bazaarvoice.DisplayParams.java
com.bazaarvoice.Media.java
com.bazaarvoice.OnBazaarResponse.java
com.bazaarvoice.SubmissionMediaParams.java
com.bazaarvoice.SubmissionParams.java
com.bazaarvoice.example.browseproducts.BazaarFunctions.java
com.bazaarvoice.example.browseproducts.BazaarProduct.java
com.bazaarvoice.example.browseproducts.BazaarReview.java
com.bazaarvoice.example.browseproducts.BazaarUIThreadResponse.java
com.bazaarvoice.example.browseproducts.ImageDownloader.java
com.bazaarvoice.example.browseproducts.MainActivity.java
com.bazaarvoice.example.browseproducts.OnImageDownloadComplete.java
com.bazaarvoice.example.browseproducts.ProductAdapter.java
com.bazaarvoice.example.browseproducts.ProductsActivity.java
com.bazaarvoice.example.browseproducts.ReviewAdapter.java
com.bazaarvoice.example.browseproducts.ReviewDisplayActivity.java
com.bazaarvoice.example.browseproducts.ReviewsActivity.java
com.bazaarvoice.example.reviewsubmission.BazaarFunctions.java
com.bazaarvoice.example.reviewsubmission.BazaarReview.java
com.bazaarvoice.example.reviewsubmission.CameraUtils.java
com.bazaarvoice.example.reviewsubmission.ImageDownloader.java
com.bazaarvoice.example.reviewsubmission.MainActivity.java
com.bazaarvoice.example.reviewsubmission.OnImageDownloadComplete.java
com.bazaarvoice.example.reviewsubmission.OnImageUploadComplete.java
com.bazaarvoice.example.reviewsubmission.RatingActivity.java
com.bazaarvoice.example.reviewsubmission.RatingPreviewActivity.java
com.bazaarvoice.example.reviewsubmission.ReviewSubmissionApp.java
com.bazaarvoice.intentexample.BazaarFunctions.java
com.bazaarvoice.intentexample.BazaarUIThreadResponse.java
com.bazaarvoice.intentexample.CameraUtils.java
com.bazaarvoice.intentexample.MainActivity.java
com.bazaarvoice.types.Action.java
com.bazaarvoice.types.ApiVersion.java
com.bazaarvoice.types.Equality.java
com.bazaarvoice.types.FeedbackContentType.java
com.bazaarvoice.types.FeedbackType.java
com.bazaarvoice.types.FeedbackVoteType.java
com.bazaarvoice.types.IncludeStatsType.java
com.bazaarvoice.types.IncludeType.java
com.bazaarvoice.types.MediaParamsContentType.java
com.bazaarvoice.types.RequestType.java
com.chute.android.photopickerplus.adapter.AlbumsAdapter.java
com.chute.android.photopickerplus.adapter.PhotoSelectCursorAdapter.java
com.chute.android.photopickerplus.adapter.PhotosAdapter.java
com.chute.android.photopickerplus.app.AlbumsActivity.java
com.chute.android.photopickerplus.app.ChooseServiceActivity.java
com.chute.android.photopickerplus.app.GridActivity.java
com.chute.android.photopickerplus.app.PhotoPickerPlusApp.java
com.chute.android.photopickerplus.dao.MediaDAO.java
com.chute.android.photopickerplus.util.AppUtil.java
com.chute.android.photopickerplus.util.Constants.java
com.chute.android.photopickerplus.util.NotificationUtil.java
com.chute.android.photopickerplus.util.intent.AlbumsActivityIntentWrapper.java
com.chute.android.photopickerplus.util.intent.IntentUtil.java
com.chute.android.photopickerplus.util.intent.IntentWrapper.java
com.chute.android.photopickerplus.util.intent.PhotoActivityIntentWrapper.java
com.chute.android.photopickerplus.util.intent.PhotoPickerPlusIntentWrapper.java
com.chute.android.photopickerplus.util.intent.PhotosIntentWrapper.java
com.example.productwidgetexample.BazaarFunctions.java
com.example.productwidgetexample.BazaarProduct.java
com.example.productwidgetexample.BazaarReview.java
com.example.productwidgetexample.BazaarUIThreadResponse.java
com.example.productwidgetexample.ImageDownloader.java
com.example.productwidgetexample.OnImageDownloadComplete.java
com.example.productwidgetexample.ProductWidgetProvider.java
com.example.productwidgetexample.ReviewAdapter.java
com.example.productwidgetexample.ReviewsActivity.java