diff options
Diffstat (limited to 'android/media/Rating2.java')
-rw-r--r-- | android/media/Rating2.java | 148 |
1 files changed, 52 insertions, 96 deletions
diff --git a/android/media/Rating2.java b/android/media/Rating2.java index 67e5e728..92131901 100644 --- a/android/media/Rating2.java +++ b/android/media/Rating2.java @@ -16,7 +16,11 @@ package android.media; +import android.annotation.NonNull; +import android.annotation.Nullable; import android.annotation.IntDef; +import android.media.update.ApiLoader; +import android.media.update.Rating2Provider; import android.os.Bundle; import android.util.Log; @@ -24,21 +28,17 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; /** + * @hide * A class to encapsulate rating information used as content metadata. * A rating is defined by its rating style (see {@link #RATING_HEART}, * {@link #RATING_THUMB_UP_DOWN}, {@link #RATING_3_STARS}, {@link #RATING_4_STARS}, * {@link #RATING_5_STARS} or {@link #RATING_PERCENTAGE}) and the actual rating value (which may * be defined as "unrated"), both of which are defined when the rating instance is constructed * through one of the factory methods. - * @hide */ -// TODO(jaewan): Move this to updatable +// New version of Rating with following change +// - Don't implement Parcelable for updatable support. public final class Rating2 { - private static final String TAG = "Rating2"; - - private static final String KEY_STYLE = "android.media.rating2.style"; - private static final String KEY_VALUE = "android.media.rating2.value"; - /** * @hide */ @@ -92,31 +92,45 @@ public final class Rating2 { */ public final static int RATING_PERCENTAGE = 6; - private final static float RATING_NOT_RATED = -1.0f; + private final Rating2Provider mProvider; + + /** + * @hide + */ + public Rating2(@NonNull Rating2Provider provider) { + mProvider = provider; + } - private final int mRatingStyle; + @Override + public String toString() { + return mProvider.toString_impl(); + } - private final float mRatingValue; + /** + * @hide + */ + public Rating2Provider getProvider() { + return mProvider; + } - private Rating2(@Style int ratingStyle, float rating) { - mRatingStyle = ratingStyle; - mRatingValue = rating; + @Override + public boolean equals(Object obj) { + return mProvider.equals_impl(obj); } @Override - public String toString() { - return "Rating2:style=" + mRatingStyle + " rating=" - + (mRatingValue < 0.0f ? "unrated" : String.valueOf(mRatingValue)); + public int hashCode() { + return mProvider.hashCode_impl(); } /** * Create an instance from bundle object, previoulsy created by {@link #toBundle()} * * @param bundle bundle - * @return new Rating2 instance + * @return new Rating2 instance or {@code null} for error */ - public static Rating2 fromBundle(Bundle bundle) { - return new Rating2(bundle.getInt(KEY_STYLE), bundle.getFloat(KEY_VALUE)); + public static Rating2 fromBundle(@Nullable Bundle bundle) { + return ApiLoader.getProvider().fromBundle_Rating2(bundle); } /** @@ -124,10 +138,7 @@ public final class Rating2 { * @return bundle of this object */ public Bundle toBundle() { - Bundle bundle = new Bundle(); - bundle.putInt(KEY_STYLE, mRatingStyle); - bundle.putFloat(KEY_VALUE, mRatingValue); - return bundle; + return mProvider.toBundle_impl(); } /** @@ -139,18 +150,8 @@ public final class Rating2 { * or {@link #RATING_PERCENTAGE}. * @return null if an invalid rating style is passed, a new Rating2 instance otherwise. */ - public static Rating2 newUnratedRating(@Style int ratingStyle) { - switch(ratingStyle) { - case RATING_HEART: - case RATING_THUMB_UP_DOWN: - case RATING_3_STARS: - case RATING_4_STARS: - case RATING_5_STARS: - case RATING_PERCENTAGE: - return new Rating2(ratingStyle, RATING_NOT_RATED); - default: - return null; - } + public static @Nullable Rating2 newUnratedRating(@Style int ratingStyle) { + return ApiLoader.getProvider().newUnratedRating_Rating2(ratingStyle); } /** @@ -160,8 +161,8 @@ public final class Rating2 { * @param hasHeart true for a "heart selected" rating, false for "heart unselected". * @return a new Rating2 instance. */ - public static Rating2 newHeartRating(boolean hasHeart) { - return new Rating2(RATING_HEART, hasHeart ? 1.0f : 0.0f); + public static @Nullable Rating2 newHeartRating(boolean hasHeart) { + return ApiLoader.getProvider().newHeartRating_Rating2(hasHeart); } /** @@ -171,8 +172,8 @@ public final class Rating2 { * @param thumbIsUp true for a "thumb up" rating, false for "thumb down". * @return a new Rating2 instance. */ - public static Rating2 newThumbRating(boolean thumbIsUp) { - return new Rating2(RATING_THUMB_UP_DOWN, thumbIsUp ? 1.0f : 0.0f); + public static @Nullable Rating2 newThumbRating(boolean thumbIsUp) { + return ApiLoader.getProvider().newThumbRating_Rating2(thumbIsUp); } /** @@ -187,27 +188,9 @@ public final class Rating2 { * @return null if the rating style is invalid, or the rating is out of range, * a new Rating2 instance otherwise. */ - public static Rating2 newStarRating(@StarStyle int starRatingStyle, float starRating) { - float maxRating = -1.0f; - switch(starRatingStyle) { - case RATING_3_STARS: - maxRating = 3.0f; - break; - case RATING_4_STARS: - maxRating = 4.0f; - break; - case RATING_5_STARS: - maxRating = 5.0f; - break; - default: - Log.e(TAG, "Invalid rating style (" + starRatingStyle + ") for a star rating"); - return null; - } - if ((starRating < 0.0f) || (starRating > maxRating)) { - Log.e(TAG, "Trying to set out of range star-based rating"); - return null; - } - return new Rating2(starRatingStyle, starRating); + public static @Nullable Rating2 newStarRating( + @StarStyle int starRatingStyle, float starRating) { + return ApiLoader.getProvider().newStarRating_Rating2(starRatingStyle, starRating); } /** @@ -217,13 +200,8 @@ public final class Rating2 { * @param percent the value of the rating * @return null if the rating is out of range, a new Rating2 instance otherwise. */ - public static Rating2 newPercentageRating(float percent) { - if ((percent < 0.0f) || (percent > 100.0f)) { - Log.e(TAG, "Invalid percentage-based rating value"); - return null; - } else { - return new Rating2(RATING_PERCENTAGE, percent); - } + public static @Nullable Rating2 newPercentageRating(float percent) { + return ApiLoader.getProvider().newPercentageRating_Rating2(percent); } /** @@ -231,7 +209,7 @@ public final class Rating2 { * @return true if the instance was not created with {@link #newUnratedRating(int)}. */ public boolean isRated() { - return mRatingValue >= 0.0f; + return mProvider.isRated_impl(); } /** @@ -240,9 +218,8 @@ public final class Rating2 { * {@link #RATING_3_STARS}, {@link #RATING_4_STARS}, {@link #RATING_5_STARS}, * or {@link #RATING_PERCENTAGE}. */ - @Style - public int getRatingStyle() { - return mRatingStyle; + public @Style int getRatingStyle() { + return mProvider.getRatingStyle_impl(); } /** @@ -251,11 +228,7 @@ public final class Rating2 { * if the rating style is not {@link #RATING_HEART} or if it is unrated. */ public boolean hasHeart() { - if (mRatingStyle != RATING_HEART) { - return false; - } else { - return (mRatingValue == 1.0f); - } + return mProvider.hasHeart_impl(); } /** @@ -264,11 +237,7 @@ public final class Rating2 { * if the rating style is not {@link #RATING_THUMB_UP_DOWN} or if it is unrated. */ public boolean isThumbUp() { - if (mRatingStyle != RATING_THUMB_UP_DOWN) { - return false; - } else { - return (mRatingValue == 1.0f); - } + return mProvider.isThumbUp_impl(); } /** @@ -277,16 +246,7 @@ public final class Rating2 { * not star-based, or if it is unrated. */ public float getStarRating() { - switch (mRatingStyle) { - case RATING_3_STARS: - case RATING_4_STARS: - case RATING_5_STARS: - if (isRated()) { - return mRatingValue; - } - default: - return -1.0f; - } + return mProvider.getStarRating_impl(); } /** @@ -295,10 +255,6 @@ public final class Rating2 { * not percentage-based, or if it is unrated. */ public float getPercentRating() { - if ((mRatingStyle != RATING_PERCENTAGE) || !isRated()) { - return -1.0f; - } else { - return mRatingValue; - } + return mProvider.getPercentRating_impl(); } } |