FileDocCategorySizeDatePackage
Rating.javaAPI DocAndroid 5.1 API9965Thu Mar 12 22:22:30 GMT 2015android.media

Rating

public final class Rating extends Object implements android.os.Parcelable
A class to encapsulate rating information used as content metadata. A rating is defined by its rating style (see {@link #RATING_HEART}, {@link #RATING_THUMB_UP_DOWN}, {@link #RATING_3_STARS}, {@link #RATING_4_STARS}, {@link #RATING_5_STARS} or {@link #RATING_PERCENTAGE}) and the actual rating value (which may be defined as "unrated"), both of which are defined when the rating instance is constructed through one of the factory methods.

Fields Summary
private static final String
TAG
public static final int
RATING_NONE
Indicates a rating style is not supported. A Rating will never have this type, but can be used by other classes to indicate they do not support Rating.
public static final int
RATING_HEART
A rating style with a single degree of rating, "heart" vs "no heart". Can be used to indicate the content referred to is a favorite (or not).
public static final int
RATING_THUMB_UP_DOWN
A rating style for "thumb up" vs "thumb down".
public static final int
RATING_3_STARS
A rating style with 0 to 3 stars.
public static final int
RATING_4_STARS
A rating style with 0 to 4 stars.
public static final int
RATING_5_STARS
A rating style with 0 to 5 stars.
public static final int
RATING_PERCENTAGE
A rating style expressed as a percentage.
private static final float
RATING_NOT_RATED
private final int
mRatingStyle
private final float
mRatingValue
public static final Parcelable.Creator
CREATOR
Constructors Summary
private Rating(int ratingStyle, float rating)


         
        mRatingStyle = ratingStyle;
        mRatingValue = rating;
    
Methods Summary
public intdescribeContents()

        return mRatingStyle;
    
public floatgetPercentRating()
Return the percentage-based rating value.

return
a rating value greater or equal to 0.0f, or a negative value if the rating style is not percentage-based, or if it is unrated.

        if ((mRatingStyle != RATING_PERCENTAGE) || !isRated()) {
            return -1.0f;
        } else {
            return mRatingValue;
        }
    
public intgetRatingStyle()
Return the rating style.

return
one of {@link #RATING_HEART}, {@link #RATING_THUMB_UP_DOWN}, {@link #RATING_3_STARS}, {@link #RATING_4_STARS}, {@link #RATING_5_STARS}, or {@link #RATING_PERCENTAGE}.

        return mRatingStyle;
    
public floatgetStarRating()
Return the star-based rating value.

return
a rating value greater or equal to 0.0f, or a negative value if the rating style is not star-based, or if it is unrated.

        switch (mRatingStyle) {
            case RATING_3_STARS:
            case RATING_4_STARS:
            case RATING_5_STARS:
                if (isRated()) {
                    return mRatingValue;
                }
            default:
                return -1.0f;
        }
    
public booleanhasHeart()
Return whether the rating is "heart selected".

return
true if the rating is "heart selected", false if the rating is "heart unselected", if the rating style is not {@link #RATING_HEART} or if it is unrated.

        if (mRatingStyle != RATING_HEART) {
            return false;
        } else {
            return (mRatingValue == 1.0f);
        }
    
public booleanisRated()
Return whether there is a rating value available.

return
true if the instance was not created with {@link #newUnratedRating(int)}.

        return mRatingValue >= 0.0f;
    
public booleanisThumbUp()
Return whether the rating is "thumb up".

return
true if the rating is "thumb up", false if the rating is "thumb down", if the rating style is not {@link #RATING_THUMB_UP_DOWN} or if it is unrated.

        if (mRatingStyle != RATING_THUMB_UP_DOWN) {
            return false;
        } else {
            return (mRatingValue == 1.0f);
        }
    
public static android.media.RatingnewHeartRating(boolean hasHeart)
Return a Rating instance with a heart-based rating. Create and return a new Rating instance with a rating style of {@link #RATING_HEART}, and a heart-based rating.

param
hasHeart true for a "heart selected" rating, false for "heart unselected".
return
a new Rating instance.

        return new Rating(RATING_HEART, hasHeart ? 1.0f : 0.0f);
    
public static android.media.RatingnewPercentageRating(float percent)
Return a Rating instance with a percentage-based rating. Create and return a new Rating instance with a {@link #RATING_PERCENTAGE} rating style, and a rating of the given percentage.

param
percent the value of the rating
return
null if the rating is out of range, a new Rating instance otherwise.

        if ((percent < 0.0f) || (percent > 100.0f)) {
            Log.e(TAG, "Invalid percentage-based rating value");
            return null;
        } else {
            return new Rating(RATING_PERCENTAGE, percent);
        }
    
public static android.media.RatingnewStarRating(int starRatingStyle, float starRating)
Return a Rating instance with a star-based rating. Create and return a new Rating instance with one of the star-base rating styles and the given integer or fractional number of stars. Non integer values can for instance be used to represent an average rating value, which might not be an integer number of stars.

param
starRatingStyle one of {@link #RATING_3_STARS}, {@link #RATING_4_STARS}, {@link #RATING_5_STARS}.
param
starRating a number ranging from 0.0f to 3.0f, 4.0f or 5.0f according to the rating style.
return
null if the rating style is invalid, or the rating is out of range, a new Rating instance otherwise.

        float maxRating = -1.0f;
        switch(starRatingStyle) {
            case RATING_3_STARS:
                maxRating = 3.0f;
                break;
            case RATING_4_STARS:
                maxRating = 4.0f;
                break;
            case RATING_5_STARS:
                maxRating = 5.0f;
                break;
            default:
                Log.e(TAG, "Invalid rating style (" + starRatingStyle + ") for a star rating");
                        return null;
        }
        if ((starRating < 0.0f) || (starRating > maxRating)) {
            Log.e(TAG, "Trying to set out of range star-based rating");
            return null;
        }
        return new Rating(starRatingStyle, starRating);
    
public static android.media.RatingnewThumbRating(boolean thumbIsUp)
Return a Rating instance with a thumb-based rating. Create and return a new Rating instance with a {@link #RATING_THUMB_UP_DOWN} rating style, and a "thumb up" or "thumb down" rating.

param
thumbIsUp true for a "thumb up" rating, false for "thumb down".
return
a new Rating instance.

        return new Rating(RATING_THUMB_UP_DOWN, thumbIsUp ? 1.0f : 0.0f);
    
public static android.media.RatingnewUnratedRating(int ratingStyle)
Return a Rating instance with no rating. Create and return a new Rating instance with no rating known for the given rating style.

param
ratingStyle one of {@link #RATING_HEART}, {@link #RATING_THUMB_UP_DOWN}, {@link #RATING_3_STARS}, {@link #RATING_4_STARS}, {@link #RATING_5_STARS}, or {@link #RATING_PERCENTAGE}.
return
null if an invalid rating style is passed, a new Rating instance otherwise.


                                                                     
         
        switch(ratingStyle) {
            case RATING_HEART:
            case RATING_THUMB_UP_DOWN:
            case RATING_3_STARS:
            case RATING_4_STARS:
            case RATING_5_STARS:
            case RATING_PERCENTAGE:
                return new Rating(ratingStyle, RATING_NOT_RATED);
            default:
                return null;
        }
    
public java.lang.StringtoString()

        return "Rating:style=" + mRatingStyle + " rating="
                + (mRatingValue < 0.0f ? "unrated" : String.valueOf(mRatingValue));
    
public voidwriteToParcel(android.os.Parcel dest, int flags)

        dest.writeInt(mRatingStyle);
        dest.writeFloat(mRatingValue);