Home | History | Annotate | Download | only in media
      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 
     17 package android.media;
     18 
     19 import android.annotation.NonNull;
     20 import android.annotation.Nullable;
     21 import android.annotation.IntDef;
     22 import android.media.update.ApiLoader;
     23 import android.media.update.Rating2Provider;
     24 import android.os.Bundle;
     25 import android.util.Log;
     26 
     27 import java.lang.annotation.Retention;
     28 import java.lang.annotation.RetentionPolicy;
     29 
     30 /**
     31  * @hide
     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.
     41 public 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 bundle bundle
    130      * @return new Rating2 instance or {@code null} for error
    131      */
    132     public static Rating2 fromBundle(@Nullable Bundle bundle) {
    133         return ApiLoader.getProvider().fromBundle_Rating2(bundle);
    134     }
    135 
    136     /**
    137      * Return bundle for this object to share across the process.
    138      * @return bundle of this object
    139      */
    140     public Bundle toBundle() {
    141         return mProvider.toBundle_impl();
    142     }
    143 
    144     /**
    145      * Return a Rating2 instance with no rating.
    146      * Create and return a new Rating2 instance with no rating known for the given
    147      * rating style.
    148      * @param ratingStyle one of {@link #RATING_HEART}, {@link #RATING_THUMB_UP_DOWN},
    149      *    {@link #RATING_3_STARS}, {@link #RATING_4_STARS}, {@link #RATING_5_STARS},
    150      *    or {@link #RATING_PERCENTAGE}.
    151      * @return null if an invalid rating style is passed, a new Rating2 instance otherwise.
    152      */
    153     public static @Nullable Rating2 newUnratedRating(@Style int ratingStyle) {
    154         return ApiLoader.getProvider().newUnratedRating_Rating2(ratingStyle);
    155     }
    156 
    157     /**
    158      * Return a Rating2 instance with a heart-based rating.
    159      * Create and return a new Rating2 instance with a rating style of {@link #RATING_HEART},
    160      * and a heart-based rating.
    161      * @param hasHeart true for a "heart selected" rating, false for "heart unselected".
    162      * @return a new Rating2 instance.
    163      */
    164     public static @Nullable Rating2 newHeartRating(boolean hasHeart) {
    165         return ApiLoader.getProvider().newHeartRating_Rating2(hasHeart);
    166     }
    167 
    168     /**
    169      * Return a Rating2 instance with a thumb-based rating.
    170      * Create and return a new Rating2 instance with a {@link #RATING_THUMB_UP_DOWN}
    171      * rating style, and a "thumb up" or "thumb down" rating.
    172      * @param thumbIsUp true for a "thumb up" rating, false for "thumb down".
    173      * @return a new Rating2 instance.
    174      */
    175     public static @Nullable Rating2 newThumbRating(boolean thumbIsUp) {
    176         return ApiLoader.getProvider().newThumbRating_Rating2(thumbIsUp);
    177     }
    178 
    179     /**
    180      * Return a Rating2 instance with a star-based rating.
    181      * Create and return a new Rating2 instance with one of the star-base rating styles
    182      * and the given integer or fractional number of stars. Non integer values can for instance
    183      * be used to represent an average rating value, which might not be an integer number of stars.
    184      * @param starRatingStyle one of {@link #RATING_3_STARS}, {@link #RATING_4_STARS},
    185      *     {@link #RATING_5_STARS}.
    186      * @param starRating a number ranging from 0.0f to 3.0f, 4.0f or 5.0f according to
    187      *     the rating style.
    188      * @return null if the rating style is invalid, or the rating is out of range,
    189      *     a new Rating2 instance otherwise.
    190      */
    191     public static @Nullable Rating2 newStarRating(
    192             @StarStyle int starRatingStyle, float starRating) {
    193         return ApiLoader.getProvider().newStarRating_Rating2(starRatingStyle, starRating);
    194     }
    195 
    196     /**
    197      * Return a Rating2 instance with a percentage-based rating.
    198      * Create and return a new Rating2 instance with a {@link #RATING_PERCENTAGE}
    199      * rating style, and a rating of the given percentage.
    200      * @param percent the value of the rating
    201      * @return null if the rating is out of range, a new Rating2 instance otherwise.
    202      */
    203     public static @Nullable Rating2 newPercentageRating(float percent) {
    204         return ApiLoader.getProvider().newPercentageRating_Rating2(percent);
    205     }
    206 
    207     /**
    208      * Return whether there is a rating value available.
    209      * @return true if the instance was not created with {@link #newUnratedRating(int)}.
    210      */
    211     public boolean isRated() {
    212         return mProvider.isRated_impl();
    213     }
    214 
    215     /**
    216      * Return the rating style.
    217      * @return one of {@link #RATING_HEART}, {@link #RATING_THUMB_UP_DOWN},
    218      *    {@link #RATING_3_STARS}, {@link #RATING_4_STARS}, {@link #RATING_5_STARS},
    219      *    or {@link #RATING_PERCENTAGE}.
    220      */
    221     public @Style int getRatingStyle() {
    222         return mProvider.getRatingStyle_impl();
    223     }
    224 
    225     /**
    226      * Return whether the rating is "heart selected".
    227      * @return true if the rating is "heart selected", false if the rating is "heart unselected",
    228      *    if the rating style is not {@link #RATING_HEART} or if it is unrated.
    229      */
    230     public boolean hasHeart() {
    231         return mProvider.hasHeart_impl();
    232     }
    233 
    234     /**
    235      * Return whether the rating is "thumb up".
    236      * @return true if the rating is "thumb up", false if the rating is "thumb down",
    237      *    if the rating style is not {@link #RATING_THUMB_UP_DOWN} or if it is unrated.
    238      */
    239     public boolean isThumbUp() {
    240         return mProvider.isThumbUp_impl();
    241     }
    242 
    243     /**
    244      * Return the star-based rating value.
    245      * @return a rating value greater or equal to 0.0f, or a negative value if the rating style is
    246      *    not star-based, or if it is unrated.
    247      */
    248     public float getStarRating() {
    249         return mProvider.getStarRating_impl();
    250     }
    251 
    252     /**
    253      * Return the percentage-based rating value.
    254      * @return a rating value greater or equal to 0.0f, or a negative value if the rating style is
    255      *    not percentage-based, or if it is unrated.
    256      */
    257     public float getPercentRating() {
    258         return mProvider.getPercentRating_impl();
    259     }
    260 }
    261