• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 final static 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 final static 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 final static int RATING_HEART = 1;
65 
66     /**
67      * A rating style for "thumb up" vs "thumb down".
68      */
69     public final static int RATING_THUMB_UP_DOWN = 2;
70 
71     /**
72      * A rating style with 0 to 3 stars.
73      */
74     public final static int RATING_3_STARS = 3;
75 
76     /**
77      * A rating style with 0 to 4 stars.
78      */
79     public final static int RATING_4_STARS = 4;
80 
81     /**
82      * A rating style with 0 to 5 stars.
83      */
84     public final static int RATING_5_STARS = 5;
85 
86     /**
87      * A rating style expressed as a percentage.
88      */
89     public final static int RATING_PERCENTAGE = 6;
90 
91     private final static 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 Parcelable.Creator<Rating> CREATOR
120             = new Parcelable.Creator<Rating>() {
121         /**
122          * Rebuilds a Rating previously stored with writeToParcel().
123          * @param p    Parcel object to read the Rating from
124          * @return a new Rating created from the data in the parcel
125          */
126         @Override
127         public Rating createFromParcel(Parcel p) {
128             return new Rating(p.readInt(), p.readFloat());
129         }
130 
131         @Override
132         public Rating[] newArray(int size) {
133             return new Rating[size];
134         }
135     };
136 
137     /**
138      * Return a Rating instance with no rating.
139      * Create and return a new Rating instance with no rating known for the given
140      * rating style.
141      * @param ratingStyle one of {@link #RATING_HEART}, {@link #RATING_THUMB_UP_DOWN},
142      *    {@link #RATING_3_STARS}, {@link #RATING_4_STARS}, {@link #RATING_5_STARS},
143      *    or {@link #RATING_PERCENTAGE}.
144      * @return null if an invalid rating style is passed, a new Rating instance otherwise.
145      */
newUnratedRating(@tyle int ratingStyle)146     public static Rating newUnratedRating(@Style int ratingStyle) {
147         switch(ratingStyle) {
148             case RATING_HEART:
149             case RATING_THUMB_UP_DOWN:
150             case RATING_3_STARS:
151             case RATING_4_STARS:
152             case RATING_5_STARS:
153             case RATING_PERCENTAGE:
154                 return new Rating(ratingStyle, RATING_NOT_RATED);
155             default:
156                 return null;
157         }
158     }
159 
160     /**
161      * Return a Rating instance with a heart-based rating.
162      * Create and return a new Rating instance with a rating style of {@link #RATING_HEART},
163      * and a heart-based rating.
164      * @param hasHeart true for a "heart selected" rating, false for "heart unselected".
165      * @return a new Rating instance.
166      */
newHeartRating(boolean hasHeart)167     public static Rating newHeartRating(boolean hasHeart) {
168         return new Rating(RATING_HEART, hasHeart ? 1.0f : 0.0f);
169     }
170 
171     /**
172      * Return a Rating instance with a thumb-based rating.
173      * Create and return a new Rating instance with a {@link #RATING_THUMB_UP_DOWN}
174      * rating style, and a "thumb up" or "thumb down" rating.
175      * @param thumbIsUp true for a "thumb up" rating, false for "thumb down".
176      * @return a new Rating instance.
177      */
newThumbRating(boolean thumbIsUp)178     public static Rating newThumbRating(boolean thumbIsUp) {
179         return new Rating(RATING_THUMB_UP_DOWN, thumbIsUp ? 1.0f : 0.0f);
180     }
181 
182     /**
183      * Return a Rating instance with a star-based rating.
184      * Create and return a new Rating instance with one of the star-base rating styles
185      * and the given integer or fractional number of stars. Non integer values can for instance
186      * be used to represent an average rating value, which might not be an integer number of stars.
187      * @param starRatingStyle one of {@link #RATING_3_STARS}, {@link #RATING_4_STARS},
188      *     {@link #RATING_5_STARS}.
189      * @param starRating a number ranging from 0.0f to 3.0f, 4.0f or 5.0f according to
190      *     the rating style.
191      * @return null if the rating style is invalid, or the rating is out of range,
192      *     a new Rating instance otherwise.
193      */
newStarRating(@tarStyle int starRatingStyle, float starRating)194     public static Rating newStarRating(@StarStyle int starRatingStyle, float starRating) {
195         float maxRating = -1.0f;
196         switch(starRatingStyle) {
197             case RATING_3_STARS:
198                 maxRating = 3.0f;
199                 break;
200             case RATING_4_STARS:
201                 maxRating = 4.0f;
202                 break;
203             case RATING_5_STARS:
204                 maxRating = 5.0f;
205                 break;
206             default:
207                 Log.e(TAG, "Invalid rating style (" + starRatingStyle + ") for a star rating");
208                         return null;
209         }
210         if ((starRating < 0.0f) || (starRating > maxRating)) {
211             Log.e(TAG, "Trying to set out of range star-based rating");
212             return null;
213         }
214         return new Rating(starRatingStyle, starRating);
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             Log.e(TAG, "Invalid percentage-based rating value");
227             return null;
228         } else {
229             return new Rating(RATING_PERCENTAGE, percent);
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         switch (mRatingStyle) {
285             case RATING_3_STARS:
286             case RATING_4_STARS:
287             case RATING_5_STARS:
288                 if (isRated()) {
289                     return mRatingValue;
290                 }
291             default:
292                 return -1.0f;
293         }
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