Search code examples
javaandroidrating-system

Thumbs up/ Thumbs down rating implementation using Rating class


In my media player app (Android based), I want to implement a Thumbs Up/ Thumbs Down rating system. Now, since the framework provides a default android.media.Rating class to handle different types of ratings, I was figuring out a way to implement Thumbs based rating using the Rating class.

I have written the following methods to set either Up, Down or Unrated rating.

MediaMetadataCompat metadata; // initial metadata

public void setLiked() {
    RatingCompat likedRating = RatingCompat.newThumbRating(true);

    metadata = new MediaMetadataCompat
            .Builder(metadata)
            .putRating(MediaMetadataCompat.METADATA_KEY_USER_RATING, likedRating)
            .build();
}

public void setDisliked() {
    RatingCompat dislikedRating = RatingCompat.newThumbRating(false);

    metadata = new MediaMetadataCompat
            .Builder(metadata)
            .putRating(MediaMetadataCompat.METADATA_KEY_USER_RATING, dislikedRating)
            .build();
}

public void setUnrated() {
    RatingCompat unratedRating = RatingCompat.newUnratedRating(RatingCompat.RATING_NONE);

    metadata = new MediaMetadataCompat
            .Builder(metadata)
            .putRating(MediaMetadataCompat.METADATA_KEY_USER_RATING, unratedRating)
            .build();
}

Although I haven't tested the methods, since the both app and the module hasn't been finished yet. Is it the right way to apply rating to a song? Also I can't find a way to retrieve the ratings back.

I've rolled through the documentation numerous times without getting much clue; and any examples available online are only based on star based rating. If someone has experience implementing Thumbs based rating in Android using Ratings class, help me out here. Thanks.

TLDR

To make this question not too specific, I'm offering a more generalized explanation so that this question will be more helpful for other people in future.
Basically, I want an explanation/example for the Rating class in context of Thumbs based rating.


Solution

  • Your implementation seems correct to me, you can retrieve ratings with this method:

    metadata = new MediaMetadataCompat
                .Builder(metadata).build();
    RatingCompat rating = metadata.getRating(MediaMetadataCompat.METADATA_KEY_USER_RATING);
    //you can use rating.isRated() for getting is set or not
    //you can use rating.isThumbUp() for getting boolean value