38
38
import android .support .v4 .graphics .drawable .DrawableCompat ;
39
39
import android .support .v4 .media .MediaBrowserServiceCompat ;
40
40
import android .support .v4 .media .MediaMetadataCompat ;
41
+ import android .support .v4 .media .RatingCompat ;
41
42
import android .support .v4 .media .session .MediaControllerCompat ;
42
43
import android .support .v4 .media .session .MediaSessionCompat ;
43
44
import android .support .v4 .media .session .PlaybackStateCompat ;
@@ -122,6 +123,7 @@ public class MediaAppControllerActivity extends AppCompatActivity {
122
123
private MediaAppDetails mMediaAppDetails ;
123
124
private MediaControllerCompat mController ;
124
125
private AudioFocusHelper mAudioFocusHelper ;
126
+ private RatingUiHelper mRatingUiHelper ;
125
127
private CustomControlsAdapter mCustomControlsAdapter = new CustomControlsAdapter ();
126
128
127
129
private Spinner mInputTypeView ;
@@ -136,6 +138,8 @@ public class MediaAppControllerActivity extends AppCompatActivity {
136
138
private ModeHelper mShuffleToggle ;
137
139
private ModeHelper mRepeatToggle ;
138
140
141
+ private ViewGroup mRatingViewGroup ;
142
+
139
143
private final SparseArray <ImageButton > mActionButtonMap = new SparseArray <>();
140
144
141
145
/**
@@ -171,6 +175,8 @@ protected void onCreate(Bundle savedInstanceState) {
171
175
mShuffleToggle = new ShuffleModeHelper ();
172
176
mRepeatToggle = new RepeatModeHelper ();
173
177
178
+ mRatingViewGroup = findViewById (R .id .rating );
179
+
174
180
if (savedInstanceState != null ) {
175
181
mMediaAppDetails = savedInstanceState .getParcelable (STATE_APP_DETAILS_KEY );
176
182
mUriInput .setText (savedInstanceState .getString (STATE_URI_KEY ));
@@ -323,6 +329,7 @@ private void setupMediaController() {
323
329
try {
324
330
mController = new MediaControllerCompat (this , mMediaAppDetails .sessionToken );
325
331
mController .registerCallback (mCallback );
332
+ mRatingUiHelper = ratingUiHelperFor (mController .getRatingType ());
326
333
327
334
// Force update on connect.
328
335
mCallback .onPlaybackStateChanged (mController .getPlaybackState ());
@@ -447,9 +454,17 @@ private String fetchMediaInfo() {
447
454
} else {
448
455
mMediaAlbumArtView .setImageResource (R .drawable .ic_album_black_24dp );
449
456
}
457
+ // Prefer user rating, but fall back to global rating if available.
458
+ RatingCompat rating =
459
+ mediaMetadata .getRating (MediaMetadataCompat .METADATA_KEY_USER_RATING );
460
+ if (rating == null ) {
461
+ rating = mediaMetadata .getRating (MediaMetadataCompat .METADATA_KEY_RATING );
462
+ }
463
+ mRatingUiHelper .setRating (rating );
450
464
} else {
451
465
mMediaArtistView .setText (R .string .media_info_default );
452
466
mMediaAlbumArtView .setImageResource (R .drawable .ic_album_black_24dp );
467
+ mRatingUiHelper .setRating (null );
453
468
}
454
469
455
470
final long actions = playbackState .getActions ();
@@ -525,6 +540,26 @@ private String playbackStateToName(final int playbackState) {
525
540
}
526
541
}
527
542
543
+ private RatingUiHelper ratingUiHelperFor (int ratingStyle ) {
544
+ switch (ratingStyle ) {
545
+ case RatingCompat .RATING_3_STARS :
546
+ return new RatingUiHelper .Stars3 (mRatingViewGroup , mController );
547
+ case RatingCompat .RATING_4_STARS :
548
+ return new RatingUiHelper .Stars4 (mRatingViewGroup , mController );
549
+ case RatingCompat .RATING_5_STARS :
550
+ return new RatingUiHelper .Stars5 (mRatingViewGroup , mController );
551
+ case RatingCompat .RATING_HEART :
552
+ return new RatingUiHelper .Heart (mRatingViewGroup , mController );
553
+ case RatingCompat .RATING_THUMB_UP_DOWN :
554
+ return new RatingUiHelper .Thumbs (mRatingViewGroup , mController );
555
+ case RatingCompat .RATING_PERCENTAGE :
556
+ return new RatingUiHelper .Percentage (mRatingViewGroup , mController );
557
+ case RatingCompat .RATING_NONE :
558
+ default :
559
+ return new RatingUiHelper .None (mRatingViewGroup , mController );
560
+ }
561
+ }
562
+
528
563
private void addMediaInfo (Map <String , String > mediaInfos , String key , String value ) {
529
564
if (!TextUtils .isEmpty (value )) {
530
565
mediaInfos .put (key , value );
0 commit comments