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