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 package com.google.android.car.kitchensink.volume; 17 18 import android.content.Context; 19 import android.graphics.Color; 20 import android.view.LayoutInflater; 21 import android.view.View; 22 import android.view.ViewGroup; 23 import android.widget.ArrayAdapter; 24 import android.widget.Button; 25 import android.widget.TextView; 26 import android.widget.ToggleButton; 27 28 import com.google.android.car.kitchensink.R; 29 import com.google.android.car.kitchensink.volume.VolumeTestFragment.CarAudioZoneVolumeInfo; 30 31 public final class CarAudioZoneVolumeAdapter extends ArrayAdapter<CarAudioZoneVolumeInfo> { 32 33 private final Context mContext; 34 private final boolean mGroupMuteEnabled; 35 private CarAudioZoneVolumeInfo[] mVolumeList; 36 private final int mLayoutResourceId; 37 private CarAudioZoneVolumeFragment mFragment; 38 CarAudioZoneVolumeAdapter(Context context, int layoutResourceId, CarAudioZoneVolumeInfo[] volumeList, CarAudioZoneVolumeFragment fragment, boolean groupMuteEnabled)39 public CarAudioZoneVolumeAdapter(Context context, 40 int layoutResourceId, CarAudioZoneVolumeInfo[] volumeList, 41 CarAudioZoneVolumeFragment fragment, boolean groupMuteEnabled) { 42 super(context, layoutResourceId, volumeList); 43 mFragment = fragment; 44 mContext = context; 45 mLayoutResourceId = layoutResourceId; 46 mVolumeList = volumeList; 47 mGroupMuteEnabled = groupMuteEnabled; 48 } 49 50 @Override getView(int position, View convertView, ViewGroup parent)51 public View getView(int position, View convertView, ViewGroup parent) { 52 ViewHolder vh = new ViewHolder(); 53 if (convertView == null) { 54 LayoutInflater inflater = LayoutInflater.from(mContext); 55 convertView = inflater.inflate(mLayoutResourceId, parent, false); 56 vh.id = convertView.findViewById(R.id.stream_id); 57 vh.maxVolume = convertView.findViewById(R.id.volume_limit); 58 vh.currentVolume = convertView.findViewById(R.id.current_volume); 59 vh.muteButton = convertView.findViewById(R.id.volume_mute); 60 vh.upButton = convertView.findViewById(R.id.volume_up); 61 vh.downButton = convertView.findViewById(R.id.volume_down); 62 vh.requestButton = convertView.findViewById(R.id.request); 63 convertView.setTag(vh); 64 } else { 65 vh = (ViewHolder) convertView.getTag(); 66 } 67 if (mVolumeList[position] != null) { 68 vh.id.setText(mVolumeList[position].id); 69 vh.currentVolume.setText(String.valueOf(mVolumeList[position].currentGain)); 70 int color = mVolumeList[position].hasAudioFocus ? Color.GREEN : Color.GRAY; 71 vh.requestButton.setBackgroundColor(color); 72 if (position == 0) { 73 vh.maxVolume.setText("Max"); 74 vh.upButton.setVisibility(View.INVISIBLE); 75 vh.downButton.setVisibility(View.INVISIBLE); 76 vh.requestButton.setVisibility(View.INVISIBLE); 77 vh.muteButton.setVisibility(View.INVISIBLE); 78 } else { 79 vh.maxVolume.setText(String.valueOf(mVolumeList[position].maxGain)); 80 vh.upButton.setVisibility(View.VISIBLE); 81 vh.downButton.setVisibility(View.VISIBLE); 82 vh.requestButton.setVisibility(View.VISIBLE); 83 vh.muteButton.setVisibility(mGroupMuteEnabled ? View.VISIBLE : View.INVISIBLE); 84 vh.upButton.setOnClickListener((view) -> { 85 mFragment.adjustVolumeUp(mVolumeList[position].groupId); 86 }); 87 vh.downButton.setOnClickListener((view) -> { 88 mFragment.adjustVolumeDown(mVolumeList[position].groupId); 89 }); 90 vh.muteButton.setChecked(mVolumeList[position].isMuted); 91 vh.muteButton.setOnClickListener((view) -> { 92 mFragment.toggleMute(mVolumeList[position].groupId); 93 }); 94 95 vh.requestButton.setOnClickListener((view) -> { 96 mFragment.requestFocus(mVolumeList[position].groupId); 97 }); 98 } 99 } 100 return convertView; 101 } 102 103 @Override getCount()104 public int getCount() { 105 return mVolumeList.length; 106 } 107 refreshVolumes(CarAudioZoneVolumeInfo[] volumes)108 public void refreshVolumes(CarAudioZoneVolumeInfo[] volumes) { 109 mVolumeList = volumes; 110 notifyDataSetChanged(); 111 } 112 113 private static final class ViewHolder { 114 public TextView id; 115 public TextView maxVolume; 116 public TextView currentVolume; 117 public ToggleButton muteButton; 118 public Button upButton; 119 public Button downButton; 120 public Button requestButton; 121 } 122 } 123