Rating2.java revision 17b19b730596eacb2582496dbe77b5328c46c65d
1/* 2 * Copyright 2018 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 17package android.media; 18 19import android.annotation.IntDef; 20import android.os.Bundle; 21import android.util.Log; 22 23import java.lang.annotation.Retention; 24import java.lang.annotation.RetentionPolicy; 25 26/** 27 * A class to encapsulate rating information used as content metadata. 28 * A rating is defined by its rating style (see {@link #RATING_HEART}, 29 * {@link #RATING_THUMB_UP_DOWN}, {@link #RATING_3_STARS}, {@link #RATING_4_STARS}, 30 * {@link #RATING_5_STARS} or {@link #RATING_PERCENTAGE}) and the actual rating value (which may 31 * be defined as "unrated"), both of which are defined when the rating instance is constructed 32 * through one of the factory methods. 33 * @hide 34 */ 35// TODO(jaewan): Move this to updatable 36public final class Rating2 { 37 private static final String TAG = "Rating2"; 38 39 private static final String KEY_STYLE = "android.media.rating2.style"; 40 private static final String KEY_VALUE = "android.media.rating2.value"; 41 42 /** 43 * @hide 44 */ 45 @IntDef({RATING_NONE, RATING_HEART, RATING_THUMB_UP_DOWN, RATING_3_STARS, RATING_4_STARS, 46 RATING_5_STARS, RATING_PERCENTAGE}) 47 @Retention(RetentionPolicy.SOURCE) 48 public @interface Style {} 49 50 /** 51 * @hide 52 */ 53 @IntDef({RATING_3_STARS, RATING_4_STARS, RATING_5_STARS}) 54 @Retention(RetentionPolicy.SOURCE) 55 public @interface StarStyle {} 56 57 /** 58 * Indicates a rating style is not supported. A Rating2 will never have this 59 * type, but can be used by other classes to indicate they do not support 60 * Rating2. 61 */ 62 public final static int RATING_NONE = 0; 63 64 /** 65 * A rating style with a single degree of rating, "heart" vs "no heart". Can be used to 66 * indicate the content referred to is a favorite (or not). 67 */ 68 public final static int RATING_HEART = 1; 69 70 /** 71 * A rating style for "thumb up" vs "thumb down". 72 */ 73 public final static int RATING_THUMB_UP_DOWN = 2; 74 75 /** 76 * A rating style with 0 to 3 stars. 77 */ 78 public final static int RATING_3_STARS = 3; 79 80 /** 81 * A rating style with 0 to 4 stars. 82 */ 83 public final static int RATING_4_STARS = 4; 84 85 /** 86 * A rating style with 0 to 5 stars. 87 */ 88 public final static int RATING_5_STARS = 5; 89 90 /** 91 * A rating style expressed as a percentage. 92 */ 93 public final static int RATING_PERCENTAGE = 6; 94 95 private final static float RATING_NOT_RATED = -1.0f; 96 97 private final int mRatingStyle; 98 99 private final float mRatingValue; 100 101 private Rating2(@Style int ratingStyle, float rating) { 102 mRatingStyle = ratingStyle; 103 mRatingValue = rating; 104 } 105 106 @Override 107 public String toString() { 108 return "Rating2:style=" + mRatingStyle + " rating=" 109 + (mRatingValue < 0.0f ? "unrated" : String.valueOf(mRatingValue)); 110 } 111 112 /** 113 * Create an instance from bundle object, previoulsy created by {@link #toBundle()} 114 * 115 * @param bundle bundle 116 * @return new Rating2 instance 117 */ 118 public static Rating2 fromBundle(Bundle bundle) { 119 return new Rating2(bundle.getInt(KEY_STYLE), bundle.getFloat(KEY_VALUE)); 120 } 121 122 /** 123 * Return bundle for this object to share across the process. 124 * @return bundle of this object 125 */ 126 public Bundle toBundle() { 127 Bundle bundle = new Bundle(); 128 bundle.putInt(KEY_STYLE, mRatingStyle); 129 bundle.putFloat(KEY_VALUE, mRatingValue); 130 return bundle; 131 } 132 133 /** 134 * Return a Rating2 instance with no rating. 135 * Create and return a new Rating2 instance with no rating known for the given 136 * rating style. 137 * @param ratingStyle one of {@link #RATING_HEART}, {@link #RATING_THUMB_UP_DOWN}, 138 * {@link #RATING_3_STARS}, {@link #RATING_4_STARS}, {@link #RATING_5_STARS}, 139 * or {@link #RATING_PERCENTAGE}. 140 * @return null if an invalid rating style is passed, a new Rating2 instance otherwise. 141 */ 142 public static Rating2 newUnratedRating(@Style int ratingStyle) { 143 switch(ratingStyle) { 144 case RATING_HEART: 145 case RATING_THUMB_UP_DOWN: 146 case RATING_3_STARS: 147 case RATING_4_STARS: 148 case RATING_5_STARS: 149 case RATING_PERCENTAGE: 150 return new Rating2(ratingStyle, RATING_NOT_RATED); 151 default: 152 return null; 153 } 154 } 155 156 /** 157 * Return a Rating2 instance with a heart-based rating. 158 * Create and return a new Rating2 instance with a rating style of {@link #RATING_HEART}, 159 * and a heart-based rating. 160 * @param hasHeart true for a "heart selected" rating, false for "heart unselected". 161 * @return a new Rating2 instance. 162 */ 163 public static Rating2 newHeartRating(boolean hasHeart) { 164 return new Rating2(RATING_HEART, hasHeart ? 1.0f : 0.0f); 165 } 166 167 /** 168 * Return a Rating2 instance with a thumb-based rating. 169 * Create and return a new Rating2 instance with a {@link #RATING_THUMB_UP_DOWN} 170 * rating style, and a "thumb up" or "thumb down" rating. 171 * @param thumbIsUp true for a "thumb up" rating, false for "thumb down". 172 * @return a new Rating2 instance. 173 */ 174 public static Rating2 newThumbRating(boolean thumbIsUp) { 175 return new Rating2(RATING_THUMB_UP_DOWN, thumbIsUp ? 1.0f : 0.0f); 176 } 177 178 /** 179 * Return a Rating2 instance with a star-based rating. 180 * Create and return a new Rating2 instance with one of the star-base rating styles 181 * and the given integer or fractional number of stars. Non integer values can for instance 182 * be used to represent an average rating value, which might not be an integer number of stars. 183 * @param starRatingStyle one of {@link #RATING_3_STARS}, {@link #RATING_4_STARS}, 184 * {@link #RATING_5_STARS}. 185 * @param starRating a number ranging from 0.0f to 3.0f, 4.0f or 5.0f according to 186 * the rating style. 187 * @return null if the rating style is invalid, or the rating is out of range, 188 * a new Rating2 instance otherwise. 189 */ 190 public static Rating2 newStarRating(@StarStyle int starRatingStyle, float starRating) { 191 float maxRating = -1.0f; 192 switch(starRatingStyle) { 193 case RATING_3_STARS: 194 maxRating = 3.0f; 195 break; 196 case RATING_4_STARS: 197 maxRating = 4.0f; 198 break; 199 case RATING_5_STARS: 200 maxRating = 5.0f; 201 break; 202 default: 203 Log.e(TAG, "Invalid rating style (" + starRatingStyle + ") for a star rating"); 204 return null; 205 } 206 if ((starRating < 0.0f) || (starRating > maxRating)) { 207 Log.e(TAG, "Trying to set out of range star-based rating"); 208 return null; 209 } 210 return new Rating2(starRatingStyle, starRating); 211 } 212 213 /** 214 * Return a Rating2 instance with a percentage-based rating. 215 * Create and return a new Rating2 instance with a {@link #RATING_PERCENTAGE} 216 * rating style, and a rating of the given percentage. 217 * @param percent the value of the rating 218 * @return null if the rating is out of range, a new Rating2 instance otherwise. 219 */ 220 public static Rating2 newPercentageRating(float percent) { 221 if ((percent < 0.0f) || (percent > 100.0f)) { 222 Log.e(TAG, "Invalid percentage-based rating value"); 223 return null; 224 } else { 225 return new Rating2(RATING_PERCENTAGE, percent); 226 } 227 } 228 229 /** 230 * Return whether there is a rating value available. 231 * @return true if the instance was not created with {@link #newUnratedRating(int)}. 232 */ 233 public boolean isRated() { 234 return mRatingValue >= 0.0f; 235 } 236 237 /** 238 * Return the rating style. 239 * @return one of {@link #RATING_HEART}, {@link #RATING_THUMB_UP_DOWN}, 240 * {@link #RATING_3_STARS}, {@link #RATING_4_STARS}, {@link #RATING_5_STARS}, 241 * or {@link #RATING_PERCENTAGE}. 242 */ 243 @Style 244 public int getRatingStyle() { 245 return mRatingStyle; 246 } 247 248 /** 249 * Return whether the rating is "heart selected". 250 * @return true if the rating is "heart selected", false if the rating is "heart unselected", 251 * if the rating style is not {@link #RATING_HEART} or if it is unrated. 252 */ 253 public boolean hasHeart() { 254 if (mRatingStyle != RATING_HEART) { 255 return false; 256 } else { 257 return (mRatingValue == 1.0f); 258 } 259 } 260 261 /** 262 * Return whether the rating is "thumb up". 263 * @return true if the rating is "thumb up", false if the rating is "thumb down", 264 * if the rating style is not {@link #RATING_THUMB_UP_DOWN} or if it is unrated. 265 */ 266 public boolean isThumbUp() { 267 if (mRatingStyle != RATING_THUMB_UP_DOWN) { 268 return false; 269 } else { 270 return (mRatingValue == 1.0f); 271 } 272 } 273 274 /** 275 * Return the star-based rating value. 276 * @return a rating value greater or equal to 0.0f, or a negative value if the rating style is 277 * not star-based, or if it is unrated. 278 */ 279 public float getStarRating() { 280 switch (mRatingStyle) { 281 case RATING_3_STARS: 282 case RATING_4_STARS: 283 case RATING_5_STARS: 284 if (isRated()) { 285 return mRatingValue; 286 } 287 default: 288 return -1.0f; 289 } 290 } 291 292 /** 293 * Return the percentage-based rating value. 294 * @return a rating value greater or equal to 0.0f, or a negative value if the rating style is 295 * not percentage-based, or if it is unrated. 296 */ 297 public float getPercentRating() { 298 if ((mRatingStyle != RATING_PERCENTAGE) || !isRated()) { 299 return -1.0f; 300 } else { 301 return mRatingValue; 302 } 303 } 304} 305