1 /*
2  * Copyright (C) 2016 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 package android.car.cluster.demorenderer;
17 
18 import android.car.cluster.demorenderer.MediaStateMonitor.MediaStateListener;
19 import android.graphics.Bitmap;
20 import android.media.MediaMetadata;
21 import android.media.session.PlaybackState;
22 import android.util.Log;
23 
24 /**
25  * Demo of rendering media data in instrument cluster.
26  */
27 public class DemoMediaRenderer implements MediaStateListener {
28 
29     private static final String TAG = DemoMediaRenderer.class.getSimpleName();
30 
31 
32     private final DemoInstrumentClusterView mView;
33 
34     private static final String[] PREFERRED_BITMAP_ORDER = {
35             MediaMetadata.METADATA_KEY_ALBUM_ART,
36             MediaMetadata.METADATA_KEY_ART,
37             MediaMetadata.METADATA_KEY_DISPLAY_ICON
38     };
39 
40 
DemoMediaRenderer(DemoInstrumentClusterView view)41     public DemoMediaRenderer(DemoInstrumentClusterView view) {
42         mView = view;
43     }
44 
45     @Override
onPlaybackStateChanged(PlaybackState playbackState)46     public void onPlaybackStateChanged(PlaybackState playbackState) {
47         Log.d(TAG, "onPlaybackStateChanged: " + playbackState);
48     }
49 
50     @Override
onMetadataChanged(MediaMetadata metadata)51     public void onMetadataChanged(MediaMetadata metadata) {
52         Log.d(TAG, "onMetadataChanged: " + metadata);
53         if (metadata != null) {
54             CharSequence artist = metadata.getText(MediaMetadata.METADATA_KEY_ARTIST);
55             CharSequence album = metadata.getText(MediaMetadata.METADATA_KEY_ALBUM);
56             CharSequence track = metadata.getText(MediaMetadata.METADATA_KEY_TITLE);
57             Bitmap bitmap = getMetadataBitmap(metadata);
58 
59             mView.setMediaData(artist, album, track, bitmap);
60             mView.showMedia();
61         } else {
62             mView.setMediaData(null, null, null, null);
63             mView.hideMedia();
64         }
65     }
66 
getMetadataBitmap(MediaMetadata metadata)67     private Bitmap getMetadataBitmap(MediaMetadata metadata) {
68         // Get the best art bitmap we can find
69         for (int i = 0; i < PREFERRED_BITMAP_ORDER.length; i++) {
70             Bitmap bitmap = metadata.getBitmap(PREFERRED_BITMAP_ORDER[i]);
71             if (bitmap != null) {
72                 return bitmap;
73             }
74         }
75         return null;
76     }
77 
78 }
79