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