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