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

Commit 24f525c3 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: I755cfedd8a06ac008ea7538c4a93f5d956c9923d
parent e6d6cf49
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -158,7 +158,7 @@ public class MediaMetadata2Impl implements MediaMetadata2Provider {
        // TODO(jaewan): Add backward compatibility
        Rating2 rating = null;
        try {
            rating = Rating2.fromBundle(mBundle.getBundle(key));
            rating = Rating2.fromBundle(mContext, mBundle.getBundle(key));
        } catch (Exception e) {
            // ignore, value was not a rating
            Log.w(TAG, "Failed to retrieve a key as Rating.", e);
+193 −0
Original line number 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 com.android.media;

import static android.media.Rating2.*;

import android.content.Context;
import android.media.Rating2;
import android.media.Rating2.Style;
import android.media.update.Rating2Provider;
import android.os.Bundle;
import android.util.Log;

import java.util.Objects;

public final class Rating2Impl implements Rating2Provider {
    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";

    private final static float RATING_NOT_RATED = -1.0f;

    private final Rating2 mInstance;
    private final int mRatingStyle;
    private final float mRatingValue;

    private Rating2Impl(Context context, @Style int ratingStyle, float rating) {
        mRatingStyle = ratingStyle;
        mRatingValue = rating;
        mInstance = new Rating2(this);
    }

    @Override
    public String toString_impl() {
        return "Rating2:style=" + mRatingStyle + " rating="
                + (mRatingValue < 0.0f ? "unrated" : String.valueOf(mRatingValue));
    }

    @Override
    public boolean equals_impl(Object obj) {
        if (!(obj instanceof Rating2)) {
            return false;
        }
        Rating2Impl other = (Rating2Impl) ((Rating2) obj).getProvider();
        return mRatingStyle == other.mRatingStyle
                && mRatingValue == other.mRatingValue;
    }

    @Override
    public int hashCode_impl() {
        return Objects.hash(mRatingStyle, mRatingValue);
    }

    public Rating2 getInstance() {
        return mInstance;
    }

    public static Rating2 fromBundle(Context context, Bundle bundle) {
        if (bundle == null) {
            return null;
        }
        return new Rating2Impl(context, bundle.getInt(KEY_STYLE), bundle.getFloat(KEY_VALUE))
                .getInstance();
    }

    public Bundle toBundle_impl() {
        Bundle bundle = new Bundle();
        bundle.putInt(KEY_STYLE, mRatingStyle);
        bundle.putFloat(KEY_VALUE, mRatingValue);
        return bundle;
    }

    public static Rating2 newUnratedRating(Context context, @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 Rating2Impl(context, ratingStyle, RATING_NOT_RATED).getInstance();
            default:
                return null;
        }
    }

    public static Rating2 newHeartRating(Context context, boolean hasHeart) {
        return new Rating2Impl(context, RATING_HEART, hasHeart ? 1.0f : 0.0f).getInstance();
    }

    public static Rating2 newThumbRating(Context context, boolean thumbIsUp) {
        return new Rating2Impl(context, RATING_THUMB_UP_DOWN, thumbIsUp ? 1.0f : 0.0f)
                .getInstance();
    }

    public static Rating2 newStarRating(Context context, int starRatingStyle, float starRating) {
        float maxRating = RATING_NOT_RATED;
        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 Rating2Impl(context, starRatingStyle, starRating).getInstance();
    }

    public static Rating2 newPercentageRating(Context context, float percent) {
        if ((percent < 0.0f) || (percent > 100.0f)) {
            Log.e(TAG, "Invalid percentage-based rating value");
            return null;
        } else {
            return new Rating2Impl(context, RATING_PERCENTAGE, percent).getInstance();
        }
    }

    @Override
    public boolean isRated_impl() {
        return mRatingValue >= 0.0f;
    }

    @Override
    public int getRatingStyle_impl() {
        return mRatingStyle;
    }

    @Override
    public boolean hasHeart_impl() {
        if (mRatingStyle != RATING_HEART) {
            return false;
        } else {
            return (mRatingValue == 1.0f);
        }
    }

    @Override
    public boolean isThumbUp_impl() {
        if (mRatingStyle != RATING_THUMB_UP_DOWN) {
            return false;
        } else {
            return (mRatingValue == 1.0f);
        }
    }

    @Override
    public float getStarRating_impl() {
        switch (mRatingStyle) {
            case RATING_3_STARS:
            case RATING_4_STARS:
            case RATING_5_STARS:
                if (mInstance.isRated()) {
                    return mRatingValue;
                }
            default:
                return -1.0f;
        }
    }

    @Override
    public float getPercentRating_impl() {
        if ((mRatingStyle != RATING_PERCENTAGE) || !mInstance.isRated()) {
            return -1.0f;
        } else {
            return mRatingValue;
        }
    }
}
+32 −0
Original line number Diff line number Diff line
@@ -39,6 +39,7 @@ import android.media.MediaSession2.ControllerInfo;
import android.media.MediaSession2.PlaylistParams;
import android.media.MediaSession2.SessionCallback;
import android.media.MediaSessionService2;
import android.media.Rating2;
import android.media.SessionPlayer2;
import android.media.SessionToken2;
import android.media.VolumeProvider;
@@ -74,6 +75,7 @@ import com.android.media.MediaMetadata2Impl;
import com.android.media.MediaSession2Impl;
import com.android.media.MediaSession2Impl.PlaylistParamsImpl;
import com.android.media.MediaSessionService2Impl;
import com.android.media.Rating2Impl;
import com.android.media.SessionToken2Impl;
import com.android.widget.MediaControlView2Impl;
import com.android.widget.VideoView2Impl;
@@ -234,4 +236,34 @@ public class ApiFactory implements StaticProvider {
            Context context, MediaMetadata2.Builder builder, MediaMetadata2 source) {
        return new MediaMetadata2Impl.BuilderImpl(context, builder, source);
    }

    @Override
    public Rating2 fromBundle_Rating2(Context context, Bundle bundle) {
        return Rating2Impl.fromBundle(context, bundle);
    }

    @Override
    public Rating2 newUnratedRating_Rating2(Context context, int ratingStyle) {
        return Rating2Impl.newUnratedRating(context, ratingStyle);
    }

    @Override
    public Rating2 newHeartRating_Rating2(Context context, boolean hasHeart) {
        return Rating2Impl.newHeartRating(context, hasHeart);
    }

    @Override
    public Rating2 newThumbRating_Rating2(Context context, boolean thumbIsUp) {
        return Rating2Impl.newThumbRating(context, thumbIsUp);
    }

    @Override
    public Rating2 newStarRating_Rating2(Context context, int starRatingStyle, float starRating) {
        return Rating2Impl.newStarRating(context, starRatingStyle, starRating);
    }

    @Override
    public Rating2 newPercentageRating_Rating2(Context context, float percent) {
        return Rating2Impl.newPercentageRating(context, percent);
    }
}
+2 −4
Original line number Diff line number Diff line
@@ -46,7 +46,7 @@ public class MediaMetadata2Test {
        extra.putString("MediaMetadata2Test", "testBuilder");
        final String title = "title";
        final long discNumber = 10;
        final Rating2 rating = Rating2.newThumbRating(true);
        final Rating2 rating = Rating2.newThumbRating(mContext, true);

        MediaMetadata2.Builder builder = new Builder(mContext);
        builder.setExtra(extra);
@@ -58,8 +58,6 @@ public class MediaMetadata2Test {
        assertTrue(TestUtils.equals(extra, metadata.getExtra()));
        assertEquals(title, metadata.getString(MediaMetadata2.METADATA_KEY_DISPLAY_TITLE));
        assertEquals(discNumber, metadata.getLong(MediaMetadata2.METADATA_KEY_DISC_NUMBER));

        // TODO(jaewan): Uncomment here when Rating2.equals are there.
        //assertEquals(rating, metadata.getRating(MediaMetadata2.METADATA_KEY_USER_RATING));
        assertEquals(rating, metadata.getRating(MediaMetadata2.METADATA_KEY_USER_RATING));
    }
}