RatingCompat.java revision 24fa6c0dd42df057729e1a258388183f94da7f82
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
17package android.support.v4.media;
18
19import android.os.Build;
20import android.os.Parcel;
21import android.os.Parcelable;
22import android.util.Log;
23
24/**
25 * A class to encapsulate rating information used as content metadata.
26 * A rating is defined by its rating style (see {@link #RATING_HEART},
27 * {@link #RATING_THUMB_UP_DOWN}, {@link #RATING_3_STARS}, {@link #RATING_4_STARS},
28 * {@link #RATING_5_STARS} or {@link #RATING_PERCENTAGE}) and the actual rating value (which may
29 * be defined as "unrated"), both of which are defined when the rating instance is constructed
30 * through one of the factory methods.
31 */
32public final class RatingCompat implements Parcelable {
33    private final static String TAG = "Rating";
34
35    /**
36     * Indicates a rating style is not supported. A Rating will never have this
37     * type, but can be used by other classes to indicate they do not support
38     * Rating.
39     */
40    public final static int RATING_NONE = 0;
41
42    /**
43     * A rating style with a single degree of rating, "heart" vs "no heart". Can be used to
44     * indicate the content referred to is a favorite (or not).
45     */
46    public final static int RATING_HEART = 1;
47
48    /**
49     * A rating style for "thumb up" vs "thumb down".
50     */
51    public final static int RATING_THUMB_UP_DOWN = 2;
52
53    /**
54     * A rating style with 0 to 3 stars.
55     */
56    public final static int RATING_3_STARS = 3;
57
58    /**
59     * A rating style with 0 to 4 stars.
60     */
61    public final static int RATING_4_STARS = 4;
62
63    /**
64     * A rating style with 0 to 5 stars.
65     */
66    public final static int RATING_5_STARS = 5;
67
68    /**
69     * A rating style expressed as a percentage.
70     */
71    public final static int RATING_PERCENTAGE = 6;
72
73    private final static float RATING_NOT_RATED = -1.0f;
74
75    private final int mRatingStyle;
76    private final float mRatingValue;
77
78    private Object mRatingObj; // framework Rating object
79
80    private RatingCompat(int ratingStyle, float rating) {
81        mRatingStyle = ratingStyle;
82        mRatingValue = rating;
83    }
84
85    @Override
86    public String toString() {
87        return "Rating:style=" + mRatingStyle + " rating="
88                + (mRatingValue < 0.0f ? "unrated" : String.valueOf(mRatingValue));
89    }
90
91    @Override
92    public int describeContents() {
93        return mRatingStyle;
94    }
95
96    @Override
97    public void writeToParcel(Parcel dest, int flags) {
98        dest.writeInt(mRatingStyle);
99        dest.writeFloat(mRatingValue);
100    }
101
102    public static final Parcelable.Creator<RatingCompat> CREATOR
103            = new Parcelable.Creator<RatingCompat>() {
104        /**
105         * Rebuilds a Rating previously stored with writeToParcel().
106         * @param p    Parcel object to read the Rating from
107         * @return a new Rating created from the data in the parcel
108         */
109        @Override
110        public RatingCompat createFromParcel(Parcel p) {
111            return new RatingCompat(p.readInt(), p.readFloat());
112        }
113
114        @Override
115        public RatingCompat[] newArray(int size) {
116            return new RatingCompat[size];
117        }
118    };
119
120    /**
121     * Return a Rating instance with no rating.
122     * Create and return a new Rating instance with no rating known for the given
123     * rating style.
124     * @param ratingStyle one of {@link #RATING_HEART}, {@link #RATING_THUMB_UP_DOWN},
125     *    {@link #RATING_3_STARS}, {@link #RATING_4_STARS}, {@link #RATING_5_STARS},
126     *    or {@link #RATING_PERCENTAGE}.
127     * @return null if an invalid rating style is passed, a new Rating instance otherwise.
128     */
129    public static RatingCompat newUnratedRating(int ratingStyle) {
130        switch(ratingStyle) {
131            case RATING_HEART:
132            case RATING_THUMB_UP_DOWN:
133            case RATING_3_STARS:
134            case RATING_4_STARS:
135            case RATING_5_STARS:
136            case RATING_PERCENTAGE:
137                return new RatingCompat(ratingStyle, RATING_NOT_RATED);
138            default:
139                return null;
140        }
141    }
142
143    /**
144     * Return a Rating instance with a heart-based rating.
145     * Create and return a new Rating instance with a rating style of {@link #RATING_HEART},
146     * and a heart-based rating.
147     * @param hasHeart true for a "heart selected" rating, false for "heart unselected".
148     * @return a new Rating instance.
149     */
150    public static RatingCompat newHeartRating(boolean hasHeart) {
151        return new RatingCompat(RATING_HEART, hasHeart ? 1.0f : 0.0f);
152    }
153
154    /**
155     * Return a Rating instance with a thumb-based rating.
156     * Create and return a new Rating instance with a {@link #RATING_THUMB_UP_DOWN}
157     * rating style, and a "thumb up" or "thumb down" rating.
158     * @param thumbIsUp true for a "thumb up" rating, false for "thumb down".
159     * @return a new Rating instance.
160     */
161    public static RatingCompat newThumbRating(boolean thumbIsUp) {
162        return new RatingCompat(RATING_THUMB_UP_DOWN, thumbIsUp ? 1.0f : 0.0f);
163    }
164
165    /**
166     * Return a Rating instance with a star-based rating.
167     * Create and return a new Rating instance with one of the star-base rating styles
168     * and the given integer or fractional number of stars. Non integer values can for instance
169     * be used to represent an average rating value, which might not be an integer number of stars.
170     * @param starRatingStyle one of {@link #RATING_3_STARS}, {@link #RATING_4_STARS},
171     *     {@link #RATING_5_STARS}.
172     * @param starRating a number ranging from 0.0f to 3.0f, 4.0f or 5.0f according to
173     *     the rating style.
174     * @return null if the rating style is invalid, or the rating is out of range,
175     *     a new Rating instance otherwise.
176     */
177    public static RatingCompat newStarRating(int starRatingStyle, float starRating) {
178        float maxRating = -1.0f;
179        switch(starRatingStyle) {
180            case RATING_3_STARS:
181                maxRating = 3.0f;
182                break;
183            case RATING_4_STARS:
184                maxRating = 4.0f;
185                break;
186            case RATING_5_STARS:
187                maxRating = 5.0f;
188                break;
189            default:
190                Log.e(TAG, "Invalid rating style (" + starRatingStyle + ") for a star rating");
191                        return null;
192        }
193        if ((starRating < 0.0f) || (starRating > maxRating)) {
194            Log.e(TAG, "Trying to set out of range star-based rating");
195            return null;
196        }
197        return new RatingCompat(starRatingStyle, starRating);
198    }
199
200    /**
201     * Return a Rating instance with a percentage-based rating.
202     * Create and return a new Rating instance with a {@link #RATING_PERCENTAGE}
203     * rating style, and a rating of the given percentage.
204     * @param percent the value of the rating
205     * @return null if the rating is out of range, a new Rating instance otherwise.
206     */
207    public static RatingCompat newPercentageRating(float percent) {
208        if ((percent < 0.0f) || (percent > 100.0f)) {
209            Log.e(TAG, "Invalid percentage-based rating value");
210            return null;
211        } else {
212            return new RatingCompat(RATING_PERCENTAGE, percent);
213        }
214    }
215
216    /**
217     * Return whether there is a rating value available.
218     * @return true if the instance was not created with {@link #newUnratedRating(int)}.
219     */
220    public boolean isRated() {
221        return mRatingValue >= 0.0f;
222    }
223
224    /**
225     * Return the rating style.
226     * @return one of {@link #RATING_HEART}, {@link #RATING_THUMB_UP_DOWN},
227     *    {@link #RATING_3_STARS}, {@link #RATING_4_STARS}, {@link #RATING_5_STARS},
228     *    or {@link #RATING_PERCENTAGE}.
229     */
230    public int getRatingStyle() {
231        return mRatingStyle;
232    }
233
234    /**
235     * Return whether the rating is "heart selected".
236     * @return true if the rating is "heart selected", false if the rating is "heart unselected",
237     *    if the rating style is not {@link #RATING_HEART} or if it is unrated.
238     */
239    public boolean hasHeart() {
240        if (mRatingStyle != RATING_HEART) {
241            return false;
242        } else {
243            return (mRatingValue == 1.0f);
244        }
245    }
246
247    /**
248     * Return whether the rating is "thumb up".
249     * @return true if the rating is "thumb up", false if the rating is "thumb down",
250     *    if the rating style is not {@link #RATING_THUMB_UP_DOWN} or if it is unrated.
251     */
252    public boolean isThumbUp() {
253        if (mRatingStyle != RATING_THUMB_UP_DOWN) {
254            return false;
255        } else {
256            return (mRatingValue == 1.0f);
257        }
258    }
259
260    /**
261     * Return the star-based rating value.
262     * @return a rating value greater or equal to 0.0f, or a negative value if the rating style is
263     *    not star-based, or if it is unrated.
264     */
265    public float getStarRating() {
266        switch (mRatingStyle) {
267            case RATING_3_STARS:
268            case RATING_4_STARS:
269            case RATING_5_STARS:
270                if (isRated()) {
271                    return mRatingValue;
272                }
273            default:
274                return -1.0f;
275        }
276    }
277
278    /**
279     * Return the percentage-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 percentage-based, or if it is unrated.
282     */
283    public float getPercentRating() {
284        if ((mRatingStyle != RATING_PERCENTAGE) || !isRated()) {
285            return -1.0f;
286        } else {
287            return mRatingValue;
288        }
289    }
290
291    /**
292     * Creates an instance from a framework {@link android.media.Rating} object.
293     * <p>
294     * This method is only supported on API 21+.
295     * </p>
296     *
297     * @param ratingObj A {@link android.media.Rating} object, or null if none.
298     * @return An equivalent {@link RatingCompat} object, or null if none.
299     */
300    public static RatingCompat fromRating(Object ratingObj) {
301        if (ratingObj == null || Build.VERSION.SDK_INT < 21) {
302            return null;
303        }
304
305        final int ratingStyle = RatingCompatApi21.getRatingStyle(ratingObj);
306        final RatingCompat rating;
307        if (RatingCompatApi21.isRated(ratingObj)) {
308            switch (ratingStyle) {
309                case RATING_HEART:
310                    rating = newHeartRating(RatingCompatApi21.hasHeart(ratingObj));
311                    break;
312                case RATING_THUMB_UP_DOWN:
313                    rating = newThumbRating(RatingCompatApi21.isThumbUp(ratingObj));
314                    break;
315                case RATING_3_STARS:
316                case RATING_4_STARS:
317                case RATING_5_STARS:
318                    rating = newStarRating(ratingStyle,
319                            RatingCompatApi21.getStarRating(ratingObj));
320                    break;
321                case RATING_PERCENTAGE:
322                    rating = newPercentageRating(RatingCompatApi21.getPercentRating(ratingObj));
323                    break;
324                default:
325                    return null;
326            }
327        } else {
328            rating = newUnratedRating(ratingStyle);
329        }
330        rating.mRatingObj = ratingObj;
331        return rating;
332    }
333
334    /**
335     * Gets the underlying framework {@link android.media.Rating} object.
336     * <p>
337     * This method is only supported on API 21+.
338     * </p>
339     *
340     * @return An equivalent {@link android.media.Rating} object, or null if none.
341     */
342    public Object getRating() {
343        if (mRatingObj != null || Build.VERSION.SDK_INT < 21) {
344            return mRatingObj;
345        }
346
347        if (isRated()) {
348            switch (mRatingStyle) {
349                case RATING_HEART:
350                    mRatingObj = RatingCompatApi21.newHeartRating(hasHeart());
351                    break;
352                case RATING_THUMB_UP_DOWN:
353                    mRatingObj = RatingCompatApi21.newThumbRating(isThumbUp());
354                    break;
355                case RATING_3_STARS:
356                case RATING_4_STARS:
357                case RATING_5_STARS:
358                    mRatingObj = RatingCompatApi21.newStarRating(mRatingStyle, getStarRating());
359                    break;
360                case RATING_PERCENTAGE:
361                    mRatingObj = RatingCompatApi21.newPercentageRating(getPercentRating());
362                default:
363                    return null;
364            }
365        } else {
366            mRatingObj = RatingCompatApi21.newUnratedRating(mRatingStyle);
367        }
368        return mRatingObj;
369    }
370}