1 /* 2 * Copyright (C) 2023 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.google.android.car.kitchensink.radio; 18 19 import android.content.Context; 20 import android.hardware.radio.RadioManager; 21 import android.hardware.radio.RadioMetadata; 22 import android.view.LayoutInflater; 23 import android.view.View; 24 import android.view.ViewGroup; 25 import android.widget.ArrayAdapter; 26 import android.widget.Button; 27 import android.widget.TextView; 28 29 import com.google.android.car.kitchensink.R; 30 31 public class ProgramInfoAdapter extends ArrayAdapter<RadioManager.ProgramInfo> { 32 33 private RadioTunerFragment mFragment; 34 private Context mContext; 35 private int mLayoutResourceId; 36 protected RadioManager.ProgramInfo[] mProgramInfos; ProgramInfoAdapter(Context context, int layoutResourceId, RadioManager.ProgramInfo[] programInfos, RadioTunerFragment fragment)37 public ProgramInfoAdapter(Context context, int layoutResourceId, 38 RadioManager.ProgramInfo[] programInfos, 39 RadioTunerFragment fragment) { 40 super(context, layoutResourceId, programInfos); 41 mFragment = fragment; 42 mContext = context; 43 mLayoutResourceId = layoutResourceId; 44 mProgramInfos = programInfos; 45 } 46 47 @Override getView(int position, View convertView, ViewGroup parent)48 public View getView(int position, View convertView, ViewGroup parent) { 49 ViewHolder vh = new ViewHolder(); 50 if (convertView == null) { 51 LayoutInflater inflater = LayoutInflater.from(mContext); 52 convertView = inflater.inflate(mLayoutResourceId, parent, /* attachToRoot= */ false); 53 vh.programSelectorText = convertView.findViewById(R.id.text_selector); 54 vh.tuneButton = convertView.findViewById(R.id.button_tune); 55 convertView.setTag(vh); 56 } else { 57 vh = (ViewHolder) convertView.getTag(); 58 } 59 if (mProgramInfos[position] != null) { 60 CharSequence programSelectorText = mFragment.getMetadataText(mProgramInfos[position], 61 RadioMetadata.METADATA_KEY_RDS_PS) + "\n" + getChannelDisplayName(position); 62 vh.programSelectorText.setText(programSelectorText); 63 vh.tuneButton.setVisibility(View.VISIBLE); 64 65 vh.tuneButton.setOnClickListener((view) -> { 66 mFragment.handleTune(mProgramInfos[position].getSelector()); 67 }); 68 } 69 return convertView; 70 } 71 72 @Override getCount()73 public int getCount() { 74 return mProgramInfos.length; 75 } 76 updateProgramInfos(RadioManager.ProgramInfo[] programInfos)77 void updateProgramInfos(RadioManager.ProgramInfo[] programInfos) { 78 mProgramInfos = programInfos; 79 notifyDataSetChanged(); 80 } 81 getChannelDisplayName(int position)82 String getChannelDisplayName(int position) { 83 return ""; 84 } 85 86 private static final class ViewHolder { 87 public TextView programSelectorText; 88 public Button tuneButton; 89 } 90 } 91