Rating2.java revision 6cc1a5de46ec18172d75ac589dbe8b306d0fb8d2
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.NonNull;
20import android.annotation.Nullable;
21import android.annotation.IntDef;
22import android.content.Context;
23import android.media.update.ApiLoader;
24import android.media.update.Rating2Provider;
25import android.os.Bundle;
26import android.util.Log;
27
28import java.lang.annotation.Retention;
29import java.lang.annotation.RetentionPolicy;
30
31/**
32 * A class to encapsulate rating information used as content metadata.
33 * A rating is defined by its rating style (see {@link #RATING_HEART},
34 * {@link #RATING_THUMB_UP_DOWN}, {@link #RATING_3_STARS}, {@link #RATING_4_STARS},
35 * {@link #RATING_5_STARS} or {@link #RATING_PERCENTAGE}) and the actual rating value (which may
36 * be defined as "unrated"), both of which are defined when the rating instance is constructed
37 * through one of the factory methods.
38 */
39// New version of Rating with following change
40//   - Don't implement Parcelable for updatable support.
41public final class Rating2 {
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 Rating2Provider mProvider;
96
97    /**
98     * @hide
99     */
100    public Rating2(@NonNull Rating2Provider provider) {
101        mProvider = provider;
102    }
103
104    @Override
105    public String toString() {
106        return mProvider.toString_impl();
107    }
108
109    /**
110     * @hide
111     */
112    public Rating2Provider getProvider() {
113        return mProvider;
114    }
115
116    @Override
117    public boolean equals(Object obj) {
118        return mProvider.equals_impl(obj);
119    }
120
121    @Override
122    public int hashCode() {
123        return mProvider.hashCode_impl();
124    }
125
126    /**
127     * Create an instance from bundle object, previoulsy created by {@link #toBundle()}
128     *
129     * @param context context
130     * @param bundle bundle
131     * @return new Rating2 instance or {@code null} for error
132     */
133    public static Rating2 fromBundle(@NonNull Context context, @Nullable Bundle bundle) {
134        return ApiLoader.getProvider(context).fromBundle_Rating2(context, bundle);
135    }
136
137    /**
138     * Return bundle for this object to share across the process.
139     * @return bundle of this object
140     */
141    public Bundle toBundle() {
142        return mProvider.toBundle_impl();
143    }
144
145    /**
146     * Return a Rating2 instance with no rating.
147     * Create and return a new Rating2 instance with no rating known for the given
148     * rating style.
149     * @param context context
150     * @param ratingStyle one of {@link #RATING_HEART}, {@link #RATING_THUMB_UP_DOWN},
151     *    {@link #RATING_3_STARS}, {@link #RATING_4_STARS}, {@link #RATING_5_STARS},
152     *    or {@link #RATING_PERCENTAGE}.
153     * @return null if an invalid rating style is passed, a new Rating2 instance otherwise.
154     */
155    public static @Nullable Rating2 newUnratedRating(@NonNull Context context, @Style int ratingStyle) {
156        return ApiLoader.getProvider(context).newUnratedRating_Rating2(context, ratingStyle);
157    }
158
159    /**
160     * Return a Rating2 instance with a heart-based rating.
161     * Create and return a new Rating2 instance with a rating style of {@link #RATING_HEART},
162     * and a heart-based rating.
163     * @param context context
164     * @param hasHeart true for a "heart selected" rating, false for "heart unselected".
165     * @return a new Rating2 instance.
166     */
167    public static @Nullable Rating2 newHeartRating(@NonNull Context context, boolean hasHeart) {
168        return ApiLoader.getProvider(context).newHeartRating_Rating2(context, hasHeart);
169    }
170
171    /**
172     * Return a Rating2 instance with a thumb-based rating.
173     * Create and return a new Rating2 instance with a {@link #RATING_THUMB_UP_DOWN}
174     * rating style, and a "thumb up" or "thumb down" rating.
175     * @param context context
176     * @param thumbIsUp true for a "thumb up" rating, false for "thumb down".
177     * @return a new Rating2 instance.
178     */
179    public static @Nullable Rating2 newThumbRating(@NonNull Context context, boolean thumbIsUp) {
180        return ApiLoader.getProvider(context).newThumbRating_Rating2(context, thumbIsUp);
181    }
182
183    /**
184     * Return a Rating2 instance with a star-based rating.
185     * Create and return a new Rating2 instance with one of the star-base rating styles
186     * and the given integer or fractional number of stars. Non integer values can for instance
187     * be used to represent an average rating value, which might not be an integer number of stars.
188     * @param context context
189     * @param starRatingStyle one of {@link #RATING_3_STARS}, {@link #RATING_4_STARS},
190     *     {@link #RATING_5_STARS}.
191     * @param starRating a number ranging from 0.0f to 3.0f, 4.0f or 5.0f according to
192     *     the rating style.
193     * @return null if the rating style is invalid, or the rating is out of range,
194     *     a new Rating2 instance otherwise.
195     */
196    public static @Nullable Rating2 newStarRating(@NonNull Context context,
197            @StarStyle int starRatingStyle, float starRating) {
198        return ApiLoader.getProvider(context).newStarRating_Rating2(
199                context, starRatingStyle, starRating);
200    }
201
202    /**
203     * Return a Rating2 instance with a percentage-based rating.
204     * Create and return a new Rating2 instance with a {@link #RATING_PERCENTAGE}
205     * rating style, and a rating of the given percentage.
206     * @param context context
207     * @param percent the value of the rating
208     * @return null if the rating is out of range, a new Rating2 instance otherwise.
209     */
210    public static @Nullable Rating2 newPercentageRating(@NonNull Context context, float percent) {
211        return ApiLoader.getProvider(context).newPercentageRating_Rating2(context, percent);
212    }
213
214    /**
215     * Return whether there is a rating value available.
216     * @return true if the instance was not created with {@link #newUnratedRating(Context, int)}.
217     */
218    public boolean isRated() {
219        return mProvider.isRated_impl();
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    @Style
229    public int getRatingStyle() {
230        return mProvider.getRatingStyle_impl();
231    }
232
233    /**
234     * Return whether the rating is "heart selected".
235     * @return true if the rating is "heart selected", false if the rating is "heart unselected",
236     *    if the rating style is not {@link #RATING_HEART} or if it is unrated.
237     */
238    public boolean hasHeart() {
239        return mProvider.hasHeart_impl();
240    }
241
242    /**
243     * Return whether the rating is "thumb up".
244     * @return true if the rating is "thumb up", false if the rating is "thumb down",
245     *    if the rating style is not {@link #RATING_THUMB_UP_DOWN} or if it is unrated.
246     */
247    public boolean isThumbUp() {
248        return mProvider.isThumbUp_impl();
249    }
250
251    /**
252     * Return the star-based rating value.
253     * @return a rating value greater or equal to 0.0f, or a negative value if the rating style is
254     *    not star-based, or if it is unrated.
255     */
256    public float getStarRating() {
257        return mProvider.getStarRating_impl();
258    }
259
260    /**
261     * Return the percentage-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 percentage-based, or if it is unrated.
264     */
265    public float getPercentRating() {
266        return mProvider.getPercentRating_impl();
267    }
268}
269