1 /*
2  * Copyright (C) 2014 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 com.example.android.mediabrowserservice;
17 
18 import android.app.Activity;
19 import android.media.session.MediaSession;
20 import android.view.LayoutInflater;
21 import android.view.View;
22 import android.view.ViewGroup;
23 import android.widget.ArrayAdapter;
24 import android.widget.ImageView;
25 import android.widget.TextView;
26 
27 import java.util.ArrayList;
28 
29 /**
30  * A list adapter for items in a queue
31  */
32 public class QueueAdapter extends ArrayAdapter<MediaSession.QueueItem> {
33 
34     // The currently selected/active queue item Id.
35     private long mActiveQueueItemId = MediaSession.QueueItem.UNKNOWN_ID;
36 
QueueAdapter(Activity context)37     public QueueAdapter(Activity context) {
38         super(context, R.layout.media_list_item, new ArrayList<MediaSession.QueueItem>());
39     }
40 
setActiveQueueItemId(long id)41     public void setActiveQueueItemId(long id) {
42         this.mActiveQueueItemId = id;
43     }
44 
45     private static class ViewHolder {
46         ImageView mImageView;
47         TextView mTitleView;
48         TextView mDescriptionView;
49     }
50 
getView(int position, View convertView, ViewGroup parent)51     public View getView(int position, View convertView, ViewGroup parent) {
52         ViewHolder holder;
53 
54         if (convertView == null) {
55             convertView = LayoutInflater.from(getContext())
56                     .inflate(R.layout.media_list_item, parent, false);
57             holder = new ViewHolder();
58             holder.mImageView = (ImageView) convertView.findViewById(R.id.play_eq);
59             holder.mTitleView = (TextView) convertView.findViewById(R.id.title);
60             holder.mDescriptionView = (TextView) convertView.findViewById(R.id.description);
61             convertView.setTag(holder);
62         } else {
63             holder = (ViewHolder) convertView.getTag();
64         }
65 
66         MediaSession.QueueItem item = getItem(position);
67         holder.mTitleView.setText(item.getDescription().getTitle());
68         if (item.getDescription().getDescription() != null) {
69             holder.mDescriptionView.setText(item.getDescription().getDescription());
70         }
71 
72         // If the itemId matches the active Id then use a different icon
73         if (mActiveQueueItemId == item.getQueueId()) {
74             holder.mImageView.setImageDrawable(
75                     getContext().getDrawable(R.drawable.ic_equalizer_white_24dp));
76         } else {
77             holder.mImageView.setImageDrawable(
78                     getContext().getDrawable(R.drawable.ic_play_arrow_white_24dp));
79         }
80         return convertView;
81     }
82 }
83