Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bb6ca486 authored by Jaewan Kim's avatar Jaewan Kim
Browse files

MediaSession2: Move Rating2 to updatable

Test: Run all MediaComponents tests once
Bug: 72670051
Change-Id: I5c405e34569c1ccc469dad09bbc8aad1cd7fdcf4
parent 3117c0c2
Loading
Loading
Loading
Loading
+62 −92
Original line number Original line Diff line number Diff line
@@ -16,7 +16,13 @@


package android.media;
package android.media;


import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.IntDef;
import android.annotation.IntDef;
import android.annotation.SystemApi;
import android.content.Context;
import android.media.update.ApiLoader;
import android.media.update.Rating2Provider;
import android.os.Bundle;
import android.os.Bundle;
import android.util.Log;
import android.util.Log;


@@ -33,10 +39,8 @@ import java.lang.annotation.RetentionPolicy;
 * @hide
 * @hide
 */
 */
public final class Rating2 {
public final class Rating2 {
    private static final String TAG = "Rating2";
    // Mostly same as the android.media.Rating, but it's no longer implements Parcelable for

    // updatable support.
    private static final String KEY_STYLE = "android.media.rating2.style";
    private static final String KEY_VALUE = "android.media.rating2.value";


    /**
    /**
     * @hide
     * @hide
@@ -91,31 +95,48 @@ public final class Rating2 {
     */
     */
    public final static int RATING_PERCENTAGE = 6;
    public final static int RATING_PERCENTAGE = 6;


    private final static float RATING_NOT_RATED = -1.0f;
    private final Rating2Provider mProvider;


    private final int mRatingStyle;
    /**
     * @hide
     */
    @SystemApi
    public Rating2(@NonNull Rating2Provider provider) {
        mProvider = provider;
    }


    private final float mRatingValue;
    @Override
    public String toString() {
        return mProvider.toString_impl();
    }


    private Rating2(@Style int ratingStyle, float rating) {
    /**
        mRatingStyle = ratingStyle;
     * @hide
        mRatingValue = rating;
     */
    @SystemApi
    public Rating2Provider getProvider() {
        return mProvider;
    }
    }


    @Override
    @Override
    public String toString() {
    public boolean equals(Object obj) {
        return "Rating2:style=" + mRatingStyle + " rating="
        return mProvider.equals_impl(obj);
                + (mRatingValue < 0.0f ? "unrated" : String.valueOf(mRatingValue));
    }

    @Override
    public int hashCode() {
        return mProvider.hashCode_impl();
    }
    }


    /**
    /**
     * Create an instance from bundle object, previoulsy created by {@link #toBundle()}
     * Create an instance from bundle object, previoulsy created by {@link #toBundle()}
     *
     *
     * @param context context
     * @param bundle bundle
     * @param bundle bundle
     * @return new Rating2 instance
     * @return new Rating2 instance or {@code null} for error
     */
     */
    public static Rating2 fromBundle(Bundle bundle) {
    public static Rating2 fromBundle(@NonNull Context context, @Nullable Bundle bundle) {
        return new Rating2(bundle.getInt(KEY_STYLE), bundle.getFloat(KEY_VALUE));
        return ApiLoader.getProvider(context).fromBundle_Rating2(context, bundle);
    }
    }


    /**
    /**
@@ -123,55 +144,45 @@ public final class Rating2 {
     * @return bundle of this object
     * @return bundle of this object
     */
     */
    public Bundle toBundle() {
    public Bundle toBundle() {
        Bundle bundle = new Bundle();
        return mProvider.toBundle_impl();
        bundle.putInt(KEY_STYLE, mRatingStyle);
        bundle.putFloat(KEY_VALUE, mRatingValue);
        return bundle;
    }
    }


    /**
    /**
     * Return a Rating2 instance with no rating.
     * Return a Rating2 instance with no rating.
     * Create and return a new Rating2 instance with no rating known for the given
     * Create and return a new Rating2 instance with no rating known for the given
     * rating style.
     * rating style.
     * @param context context
     * @param ratingStyle one of {@link #RATING_HEART}, {@link #RATING_THUMB_UP_DOWN},
     * @param ratingStyle one of {@link #RATING_HEART}, {@link #RATING_THUMB_UP_DOWN},
     *    {@link #RATING_3_STARS}, {@link #RATING_4_STARS}, {@link #RATING_5_STARS},
     *    {@link #RATING_3_STARS}, {@link #RATING_4_STARS}, {@link #RATING_5_STARS},
     *    or {@link #RATING_PERCENTAGE}.
     *    or {@link #RATING_PERCENTAGE}.
     * @return null if an invalid rating style is passed, a new Rating2 instance otherwise.
     * @return null if an invalid rating style is passed, a new Rating2 instance otherwise.
     */
     */
    public static Rating2 newUnratedRating(@Style int ratingStyle) {
    public static @Nullable Rating2 newUnratedRating(@NonNull Context context, @Style int ratingStyle) {
        switch(ratingStyle) {
        return ApiLoader.getProvider(context).newUnratedRating_Rating2(context, 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;
        }
    }
    }


    /**
    /**
     * Return a Rating2 instance with a heart-based rating.
     * Return a Rating2 instance with a heart-based rating.
     * Create and return a new Rating2 instance with a rating style of {@link #RATING_HEART},
     * Create and return a new Rating2 instance with a rating style of {@link #RATING_HEART},
     * and a heart-based rating.
     * and a heart-based rating.
     * @param context context
     * @param hasHeart true for a "heart selected" rating, false for "heart unselected".
     * @param hasHeart true for a "heart selected" rating, false for "heart unselected".
     * @return a new Rating2 instance.
     * @return a new Rating2 instance.
     */
     */
    public static Rating2 newHeartRating(boolean hasHeart) {
    public static @Nullable Rating2 newHeartRating(@NonNull Context context, boolean hasHeart) {
        return new Rating2(RATING_HEART, hasHeart ? 1.0f : 0.0f);
        return ApiLoader.getProvider(context).newHeartRating_Rating2(context, hasHeart);
    }
    }


    /**
    /**
     * Return a Rating2 instance with a thumb-based rating.
     * Return a Rating2 instance with a thumb-based rating.
     * Create and return a new Rating2 instance with a {@link #RATING_THUMB_UP_DOWN}
     * Create and return a new Rating2 instance with a {@link #RATING_THUMB_UP_DOWN}
     * rating style, and a "thumb up" or "thumb down" rating.
     * rating style, and a "thumb up" or "thumb down" rating.
     * @param context context
     * @param thumbIsUp true for a "thumb up" rating, false for "thumb down".
     * @param thumbIsUp true for a "thumb up" rating, false for "thumb down".
     * @return a new Rating2 instance.
     * @return a new Rating2 instance.
     */
     */
    public static Rating2 newThumbRating(boolean thumbIsUp) {
    public static @Nullable Rating2 newThumbRating(@NonNull Context context, boolean thumbIsUp) {
        return new Rating2(RATING_THUMB_UP_DOWN, thumbIsUp ? 1.0f : 0.0f);
        return ApiLoader.getProvider(context).newThumbRating_Rating2(context, thumbIsUp);
    }
    }


    /**
    /**
@@ -179,6 +190,7 @@ public final class Rating2 {
     * Create and return a new Rating2 instance with one of the star-base rating styles
     * Create and return a new Rating2 instance with one of the star-base rating styles
     * and the given integer or fractional number of stars. Non integer values can for instance
     * and the given integer or fractional number of stars. Non integer values can for instance
     * be used to represent an average rating value, which might not be an integer number of stars.
     * be used to represent an average rating value, which might not be an integer number of stars.
     * @param context context
     * @param starRatingStyle one of {@link #RATING_3_STARS}, {@link #RATING_4_STARS},
     * @param starRatingStyle one of {@link #RATING_3_STARS}, {@link #RATING_4_STARS},
     *     {@link #RATING_5_STARS}.
     *     {@link #RATING_5_STARS}.
     * @param starRating a number ranging from 0.0f to 3.0f, 4.0f or 5.0f according to
     * @param starRating a number ranging from 0.0f to 3.0f, 4.0f or 5.0f according to
@@ -186,51 +198,30 @@ public final class Rating2 {
     * @return null if the rating style is invalid, or the rating is out of range,
     * @return null if the rating style is invalid, or the rating is out of range,
     *     a new Rating2 instance otherwise.
     *     a new Rating2 instance otherwise.
     */
     */
    public static Rating2 newStarRating(@StarStyle int starRatingStyle, float starRating) {
    public static @Nullable Rating2 newStarRating(@NonNull Context context,
        float maxRating = -1.0f;
            @StarStyle int starRatingStyle, float starRating) {
        switch(starRatingStyle) {
        return ApiLoader.getProvider(context).newStarRating_Rating2(
            case RATING_3_STARS:
                context, starRatingStyle, starRating);
                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);
    }
    }


    /**
    /**
     * Return a Rating2 instance with a percentage-based rating.
     * Return a Rating2 instance with a percentage-based rating.
     * Create and return a new Rating2 instance with a {@link #RATING_PERCENTAGE}
     * Create and return a new Rating2 instance with a {@link #RATING_PERCENTAGE}
     * rating style, and a rating of the given percentage.
     * rating style, and a rating of the given percentage.
     * @param context context
     * @param percent the value of the rating
     * @param percent the value of the rating
     * @return null if the rating is out of range, a new Rating2 instance otherwise.
     * @return null if the rating is out of range, a new Rating2 instance otherwise.
     */
     */
    public static Rating2 newPercentageRating(float percent) {
    public static @Nullable Rating2 newPercentageRating(@NonNull Context context, float percent) {
        if ((percent < 0.0f) || (percent > 100.0f)) {
        return ApiLoader.getProvider(context).newPercentageRating_Rating2(context, percent);
            Log.e(TAG, "Invalid percentage-based rating value");
            return null;
        } else {
            return new Rating2(RATING_PERCENTAGE, percent);
        }
    }
    }


    /**
    /**
     * Return whether there is a rating value available.
     * Return whether there is a rating value available.
     * @return true if the instance was not created with {@link #newUnratedRating(int)}.
     * @return true if the instance was not created with {@link #newUnratedRating(Context, int)}.
     */
     */
    public boolean isRated() {
    public boolean isRated() {
        return mRatingValue >= 0.0f;
        return mProvider.isRated_impl();
    }
    }


    /**
    /**
@@ -241,7 +232,7 @@ public final class Rating2 {
     */
     */
    @Style
    @Style
    public int getRatingStyle() {
    public int getRatingStyle() {
        return mRatingStyle;
        return mProvider.getRatingStyle_impl();
    }
    }


    /**
    /**
@@ -250,11 +241,7 @@ public final class Rating2 {
     *    if the rating style is not {@link #RATING_HEART} or if it is unrated.
     *    if the rating style is not {@link #RATING_HEART} or if it is unrated.
     */
     */
    public boolean hasHeart() {
    public boolean hasHeart() {
        if (mRatingStyle != RATING_HEART) {
        return mProvider.hasHeart_impl();
            return false;
        } else {
            return (mRatingValue == 1.0f);
        }
    }
    }


    /**
    /**
@@ -263,11 +250,7 @@ public final class Rating2 {
     *    if the rating style is not {@link #RATING_THUMB_UP_DOWN} or if it is unrated.
     *    if the rating style is not {@link #RATING_THUMB_UP_DOWN} or if it is unrated.
     */
     */
    public boolean isThumbUp() {
    public boolean isThumbUp() {
        if (mRatingStyle != RATING_THUMB_UP_DOWN) {
        return mProvider.isThumbUp_impl();
            return false;
        } else {
            return (mRatingValue == 1.0f);
        }
    }
    }


    /**
    /**
@@ -276,16 +259,7 @@ public final class Rating2 {
     *    not star-based, or if it is unrated.
     *    not star-based, or if it is unrated.
     */
     */
    public float getStarRating() {
    public float getStarRating() {
        switch (mRatingStyle) {
        return mProvider.getStarRating_impl();
            case RATING_3_STARS:
            case RATING_4_STARS:
            case RATING_5_STARS:
                if (isRated()) {
                    return mRatingValue;
                }
            default:
                return -1.0f;
        }
    }
    }


    /**
    /**
@@ -294,10 +268,6 @@ public final class Rating2 {
     *    not percentage-based, or if it is unrated.
     *    not percentage-based, or if it is unrated.
     */
     */
    public float getPercentRating() {
    public float getPercentRating() {
        if ((mRatingStyle != RATING_PERCENTAGE) || !isRated()) {
        return mProvider.getPercentRating_impl();
            return -1.0f;
        } else {
            return mRatingValue;
        }
    }
    }
}
}
+37 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.media.update;

import android.annotation.SystemApi;
import android.os.Bundle;

/**
 * @hide
 */
// TODO(jaewan): @SystemApi
public interface Rating2Provider {
    String toString_impl();
    boolean equals_impl(Object obj);
    int hashCode_impl();
    Bundle toBundle_impl();
    boolean isRated_impl();
    int getRatingStyle_impl();
    boolean hasHeart_impl();
    boolean isThumbUp_impl();
    float getStarRating_impl();
    float getPercentRating_impl();
}
 No newline at end of file
+8 −0
Original line number Original line Diff line number Diff line
@@ -34,6 +34,7 @@ import android.media.MediaSession2;
import android.media.MediaSession2.PlaylistParams;
import android.media.MediaSession2.PlaylistParams;
import android.media.MediaSession2.SessionCallback;
import android.media.MediaSession2.SessionCallback;
import android.media.MediaSessionService2;
import android.media.MediaSessionService2;
import android.media.Rating2;
import android.media.SessionPlayer2;
import android.media.SessionPlayer2;
import android.media.SessionToken2;
import android.media.SessionToken2;
import android.media.VolumeProvider;
import android.media.VolumeProvider;
@@ -109,4 +110,11 @@ public interface StaticProvider {
            Context context, MediaMetadata2.Builder builder);
            Context context, MediaMetadata2.Builder builder);
    MediaMetadata2Provider.BuilderProvider createMediaMetadata2Builder(
    MediaMetadata2Provider.BuilderProvider createMediaMetadata2Builder(
            Context context, MediaMetadata2.Builder builder, MediaMetadata2 source);
            Context context, MediaMetadata2.Builder builder, MediaMetadata2 source);

    Rating2 newUnratedRating_Rating2(Context context, int ratingStyle);
    Rating2 fromBundle_Rating2(Context context, Bundle bundle);
    Rating2 newHeartRating_Rating2(Context context, boolean hasHeart);
    Rating2 newThumbRating_Rating2(Context context, boolean thumbIsUp);
    Rating2 newStarRating_Rating2(Context context, int starRatingStyle, float starRating);
    Rating2 newPercentageRating_Rating2(Context context, float percent);
}
}