1a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim/*
2a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * Copyright (C) 2015 The Android Open Source Project
3a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim *
4a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * Licensed under the Apache License, Version 2.0 (the "License");
5a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * you may not use this file except in compliance with the License.
6a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * You may obtain a copy of the License at
7a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim *
8a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim *      http://www.apache.org/licenses/LICENSE-2.0
9a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim *
10a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * Unless required by applicable law or agreed to in writing, software
11a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * distributed under the License is distributed on an "AS IS" BASIS,
12a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * See the License for the specific language governing permissions and
14a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * limitations under the License.
15a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim */
16a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
17a907614755847b2630561a1e5949b2b416600d97Sungsoo Limpackage com.example.android.supportv4.media;
18a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
19a907614755847b2630561a1e5949b2b416600d97Sungsoo Limimport com.example.android.supportv4.R;
20a907614755847b2630561a1e5949b2b416600d97Sungsoo Limimport android.app.Activity;
21a907614755847b2630561a1e5949b2b416600d97Sungsoo Limimport android.support.v4.media.session.MediaSessionCompat;
22a907614755847b2630561a1e5949b2b416600d97Sungsoo Limimport android.view.LayoutInflater;
23a907614755847b2630561a1e5949b2b416600d97Sungsoo Limimport android.view.View;
24a907614755847b2630561a1e5949b2b416600d97Sungsoo Limimport android.view.ViewGroup;
25a907614755847b2630561a1e5949b2b416600d97Sungsoo Limimport android.widget.ArrayAdapter;
26a907614755847b2630561a1e5949b2b416600d97Sungsoo Limimport android.widget.ImageView;
27a907614755847b2630561a1e5949b2b416600d97Sungsoo Limimport android.widget.TextView;
28a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
29a907614755847b2630561a1e5949b2b416600d97Sungsoo Limimport java.util.ArrayList;
30a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
31a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim/**
32a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim * A list adapter for items in a queue
33a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim */
34a907614755847b2630561a1e5949b2b416600d97Sungsoo Limpublic class QueueAdapter extends ArrayAdapter<MediaSessionCompat.QueueItem> {
35a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
36a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim    // The currently selected/active queue item Id.
37a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim    private long mActiveQueueItemId = MediaSessionCompat.QueueItem.UNKNOWN_ID;
38a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
39a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim    public QueueAdapter(Activity context) {
40a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        super(context, R.layout.media_list_item, new ArrayList<MediaSessionCompat.QueueItem>());
41a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim    }
42a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
43a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim    public void setActiveQueueItemId(long id) {
44a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        this.mActiveQueueItemId = id;
45a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim    }
46a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
47a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim    private static class ViewHolder {
48a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        ImageView mImageView;
49a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        TextView mTitleView;
50a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        TextView mDescriptionView;
51a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim    }
52a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
53a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim    public View getView(int position, View convertView, ViewGroup parent) {
54a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        ViewHolder holder;
55a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
56a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        if (convertView == null) {
57a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            convertView = LayoutInflater.from(getContext())
58a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                    .inflate(R.layout.media_list_item, parent, false);
59a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            holder = new ViewHolder();
60a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            holder.mImageView = (ImageView) convertView.findViewById(R.id.play_eq);
61a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            holder.mTitleView = (TextView) convertView.findViewById(R.id.title);
62a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            holder.mDescriptionView = (TextView) convertView.findViewById(R.id.description);
63a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            convertView.setTag(holder);
64a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        } else {
65a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            holder = (ViewHolder) convertView.getTag();
66a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        }
67a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
68a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        MediaSessionCompat.QueueItem item = getItem(position);
69a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        holder.mTitleView.setText(item.getDescription().getTitle());
70a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        if (item.getDescription().getDescription() != null) {
71a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            holder.mDescriptionView.setText(item.getDescription().getDescription());
72a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        }
73a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim
74a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        // If the itemId matches the active Id then use a different icon
75a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        if (mActiveQueueItemId == item.getQueueId()) {
76a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            holder.mImageView.setImageDrawable(
77a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                    getContext().getResources().getDrawable(R.drawable.ic_equalizer_white_24dp));
78a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        } else {
79a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim            holder.mImageView.setImageDrawable(
80a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim                    getContext().getResources().getDrawable(R.drawable.ic_play_arrow_white_24dp));
81a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        }
82a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim        return convertView;
83a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim    }
84a907614755847b2630561a1e5949b2b416600d97Sungsoo Lim}
85