1 /* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.media; 18 19 import android.annotation.IntDef; 20 import android.os.Parcel; 21 import android.os.Parcelable; 22 import android.util.Log; 23 24 import java.lang.annotation.Retention; 25 import java.lang.annotation.RetentionPolicy; 26 27 /** 28 * A class to encapsulate rating information used as content metadata. 29 * A rating is defined by its rating style (see {@link #RATING_HEART}, 30 * {@link #RATING_THUMB_UP_DOWN}, {@link #RATING_3_STARS}, {@link #RATING_4_STARS}, 31 * {@link #RATING_5_STARS} or {@link #RATING_PERCENTAGE}) and the actual rating value (which may 32 * be defined as "unrated"), both of which are defined when the rating instance is constructed 33 * through one of the factory methods. 34 */ 35 public final class Rating implements Parcelable { 36 private static final String TAG = "Rating"; 37 38 /** 39 * @hide 40 */ 41 @IntDef({RATING_NONE, RATING_HEART, RATING_THUMB_UP_DOWN, RATING_3_STARS, RATING_4_STARS, 42 RATING_5_STARS, RATING_PERCENTAGE}) 43 @Retention(RetentionPolicy.SOURCE) 44 public @interface Style {} 45 46 /** 47 * @hide 48 */ 49 @IntDef({RATING_3_STARS, RATING_4_STARS, RATING_5_STARS}) 50 @Retention(RetentionPolicy.SOURCE) 51 public @interface StarStyle {} 52 53 /** 54 * Indicates a rating style is not supported. A Rating will never have this 55 * type, but can be used by other classes to indicate they do not support 56 * Rating. 57 */ 58 public static final int RATING_NONE = 0; 59 60 /** 61 * A rating style with a single degree of rating, "heart" vs "no heart". Can be used to 62 * indicate the content referred to is a favorite (or not). 63 */ 64 public static final int RATING_HEART = 1; 65 66 /** 67 * A rating style for "thumb up" vs "thumb down". 68 */ 69 public static final int RATING_THUMB_UP_DOWN = 2; 70 71 /** 72 * A rating style with 0 to 3 stars. 73 */ 74 public static final int RATING_3_STARS = 3; 75 76 /** 77 * A rating style with 0 to 4 stars. 78 */ 79 public static final int RATING_4_STARS = 4; 80 81 /** 82 * A rating style with 0 to 5 stars. 83 */ 84 public static final int RATING_5_STARS = 5; 85 86 /** 87 * A rating style expressed as a percentage. 88 */ 89 public static final int RATING_PERCENTAGE = 6; 90 91 private static final float RATING_NOT_RATED = -1.0f; 92 93 private final int mRatingStyle; 94 95 private final float mRatingValue; 96 Rating(@tyle int ratingStyle, float rating)97 private Rating(@Style int ratingStyle, float rating) { 98 mRatingStyle = ratingStyle; 99 mRatingValue = rating; 100 } 101 102 @Override toString()103 public String toString() { 104 return "Rating:style=" + mRatingStyle + " rating=" 105 + (mRatingValue < 0.0f ? "unrated" : String.valueOf(mRatingValue)); 106 } 107 108 @Override describeContents()109 public int describeContents() { 110 return mRatingStyle; 111 } 112 113 @Override writeToParcel(Parcel dest, int flags)114 public void writeToParcel(Parcel dest, int flags) { 115 dest.writeInt(mRatingStyle); 116 dest.writeFloat(mRatingValue); 117 } 118 119 public static final @android.annotation.NonNull Parcelable.Creator<Rating> CREATOR = new Parcelable.Creator<Rating>() { 120 /** 121 * Rebuilds a Rating previously stored with writeToParcel(). 122 * @param p Parcel object to read the Rating from 123 * @return a new Rating created from the data in the parcel 124 */ 125 @Override 126 public Rating createFromParcel(Parcel p) { 127 return new Rating(p.readInt(), p.readFloat()); 128 } 129 130 @Override 131 public Rating[] newArray(int size) { 132 return new Rating[size]; 133 } 134 }; 135 136 /** 137 * Return a Rating instance with no rating. 138 * Create and return a new Rating instance with no rating known for the given 139 * rating style. 140 * @param ratingStyle one of {@link #RATING_HEART}, {@link #RATING_THUMB_UP_DOWN}, 141 * {@link #RATING_3_STARS}, {@link #RATING_4_STARS}, {@link #RATING_5_STARS}, 142 * or {@link #RATING_PERCENTAGE}. 143 * @return null if an invalid rating style is passed, a new Rating instance otherwise. 144 */ newUnratedRating(@tyle int ratingStyle)145 public static Rating newUnratedRating(@Style int ratingStyle) { 146 switch(ratingStyle) { 147 case RATING_HEART: 148 case RATING_THUMB_UP_DOWN: 149 case RATING_3_STARS: 150 case RATING_4_STARS: 151 case RATING_5_STARS: 152 case RATING_PERCENTAGE: 153 return new Rating(ratingStyle, RATING_NOT_RATED); 154 default: 155 return null; 156 } 157 } 158 159 /** 160 * Return a Rating instance with a heart-based rating. 161 * Create and return a new Rating instance with a rating style of {@link #RATING_HEART}, 162 * and a heart-based rating. 163 * @param hasHeart true for a "heart selected" rating, false for "heart unselected". 164 * @return a new Rating instance. 165 */ newHeartRating(boolean hasHeart)166 public static Rating newHeartRating(boolean hasHeart) { 167 return new Rating(RATING_HEART, hasHeart ? 1.0f : 0.0f); 168 } 169 170 /** 171 * Return a Rating instance with a thumb-based rating. 172 * Create and return a new Rating instance with a {@link #RATING_THUMB_UP_DOWN} 173 * rating style, and a "thumb up" or "thumb down" rating. 174 * @param thumbIsUp true for a "thumb up" rating, false for "thumb down". 175 * @return a new Rating instance. 176 */ newThumbRating(boolean thumbIsUp)177 public static Rating newThumbRating(boolean thumbIsUp) { 178 return new Rating(RATING_THUMB_UP_DOWN, thumbIsUp ? 1.0f : 0.0f); 179 } 180 181 /** 182 * Return a Rating instance with a star-based rating. 183 * Create and return a new Rating instance with one of the star-base rating styles 184 * and the given integer or fractional number of stars. Non integer values can for instance 185 * be used to represent an average rating value, which might not be an integer number of stars. 186 * @param starRatingStyle one of {@link #RATING_3_STARS}, {@link #RATING_4_STARS}, 187 * {@link #RATING_5_STARS}. 188 * @param starRating a number ranging from 0.0f to 3.0f, 4.0f or 5.0f according to 189 * the rating style. 190 * @return null if the rating style is invalid, or the rating is out of range, 191 * a new Rating instance otherwise. 192 */ newStarRating(@tarStyle int starRatingStyle, float starRating)193 public static Rating newStarRating(@StarStyle int starRatingStyle, float starRating) { 194 float maxRating = -1.0f; 195 switch(starRatingStyle) { 196 case RATING_3_STARS: 197 maxRating = 3.0f; 198 break; 199 case RATING_4_STARS: 200 maxRating = 4.0f; 201 break; 202 case RATING_5_STARS: 203 maxRating = 5.0f; 204 break; 205 default: 206 Log.e(TAG, "Invalid rating style (" + starRatingStyle + ") for a star rating"); 207 return null; 208 } 209 if (starRating >= 0.0f && starRating <= maxRating) { 210 return new Rating(starRatingStyle, starRating); 211 } else { 212 Log.e(TAG, "Trying to set out of range star-based rating"); 213 return null; 214 } 215 } 216 217 /** 218 * Return a Rating instance with a percentage-based rating. 219 * Create and return a new Rating instance with a {@link #RATING_PERCENTAGE} 220 * rating style, and a rating of the given percentage. 221 * @param percent the value of the rating 222 * @return null if the rating is out of range, a new Rating instance otherwise. 223 */ newPercentageRating(float percent)224 public static Rating newPercentageRating(float percent) { 225 if (percent >= 0.0f && percent <= 100.0f) { 226 return new Rating(RATING_PERCENTAGE, percent); 227 } else { 228 Log.e(TAG, "Invalid percentage-based rating value"); 229 return null; 230 } 231 } 232 233 /** 234 * Return whether there is a rating value available. 235 * @return true if the instance was not created with {@link #newUnratedRating(int)}. 236 */ isRated()237 public boolean isRated() { 238 return mRatingValue >= 0.0f; 239 } 240 241 /** 242 * Return the rating style. 243 * @return one of {@link #RATING_HEART}, {@link #RATING_THUMB_UP_DOWN}, 244 * {@link #RATING_3_STARS}, {@link #RATING_4_STARS}, {@link #RATING_5_STARS}, 245 * or {@link #RATING_PERCENTAGE}. 246 */ 247 @Style getRatingStyle()248 public int getRatingStyle() { 249 return mRatingStyle; 250 } 251 252 /** 253 * Return whether the rating is "heart selected". 254 * @return true if the rating is "heart selected", false if the rating is "heart unselected", 255 * if the rating style is not {@link #RATING_HEART} or if it is unrated. 256 */ hasHeart()257 public boolean hasHeart() { 258 if (mRatingStyle != RATING_HEART) { 259 return false; 260 } else { 261 return (mRatingValue == 1.0f); 262 } 263 } 264 265 /** 266 * Return whether the rating is "thumb up". 267 * @return true if the rating is "thumb up", false if the rating is "thumb down", 268 * if the rating style is not {@link #RATING_THUMB_UP_DOWN} or if it is unrated. 269 */ isThumbUp()270 public boolean isThumbUp() { 271 if (mRatingStyle != RATING_THUMB_UP_DOWN) { 272 return false; 273 } else { 274 return (mRatingValue == 1.0f); 275 } 276 } 277 278 /** 279 * Return the star-based rating value. 280 * @return a rating value greater or equal to 0.0f, or a negative value if the rating style is 281 * not star-based, or if it is unrated. 282 */ getStarRating()283 public float getStarRating() { 284 float ratingValue = -1.0f; 285 switch (mRatingStyle) { 286 case RATING_3_STARS: 287 case RATING_4_STARS: 288 case RATING_5_STARS: 289 if (isRated()) { 290 ratingValue = mRatingValue; 291 } 292 } 293 return ratingValue; 294 } 295 296 /** 297 * Return the percentage-based rating value. 298 * @return a rating value greater or equal to 0.0f, or a negative value if the rating style is 299 * not percentage-based, or if it is unrated. 300 */ getPercentRating()301 public float getPercentRating() { 302 if ((mRatingStyle != RATING_PERCENTAGE) || !isRated()) { 303 return -1.0f; 304 } else { 305 return mRatingValue; 306 } 307 } 308 } 309