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.isMuted = convertView.findViewById(R.id.is_muted);
60             vh.isBlocked = convertView.findViewById(R.id.is_blocked);
61             vh.isAttenuated = convertView.findViewById(R.id.is_attenuated);
62             vh.isSystemMuted = convertView.findViewById(R.id.is_hal_muted);
63             vh.muteButton = convertView.findViewById(R.id.volume_mute);
64             vh.upButton = convertView.findViewById(R.id.volume_up);
65             vh.downButton = convertView.findViewById(R.id.volume_down);
66             vh.requestButton = convertView.findViewById(R.id.request);
67             convertView.setTag(vh);
68         } else {
69             vh = (ViewHolder) convertView.getTag();
70         }
71         if (mVolumeList[position] != null) {
72             vh.id.setText(mVolumeList[position].id);
73             vh.currentVolume.setText(String.valueOf(mVolumeList[position].currentGain));
74             int color = mVolumeList[position].hasAudioFocus ? Color.GREEN : Color.GRAY;
75             vh.requestButton.setBackgroundColor(color);
76             if (position == 0) {
77                 vh.maxVolume.setText("Max");
78                 vh.isMuted.setText(String.valueOf("Muted?"));
79                 vh.isAttenuated.setText(String.valueOf("Attenuated?"));
80                 vh.isBlocked.setText(String.valueOf("Blocked?"));
81                 vh.isSystemMuted.setText(String.valueOf("System Muted?"));
82                 vh.upButton.setVisibility(View.INVISIBLE);
83                 vh.downButton.setVisibility(View.INVISIBLE);
84                 vh.requestButton.setVisibility(View.INVISIBLE);
85                 vh.muteButton.setVisibility(View.INVISIBLE);
86             } else {
87                 vh.maxVolume.setText(String.valueOf(mVolumeList[position].maxGain));
88                 vh.isMuted.setText(String.valueOf(mVolumeList[position].isMuted));
89                 vh.isAttenuated.setText(String.valueOf(mVolumeList[position].isAttenuated));
90                 vh.isBlocked.setText(String.valueOf(mVolumeList[position].isBlocked));
91                 vh.isSystemMuted.setText(String.valueOf(mVolumeList[position].isSystemMuted));
92                 vh.upButton.setVisibility(View.VISIBLE);
93                 vh.downButton.setVisibility(View.VISIBLE);
94                 vh.requestButton.setVisibility(View.VISIBLE);
95                 vh.muteButton.setVisibility(mGroupMuteEnabled ? View.VISIBLE : View.INVISIBLE);
96                 vh.upButton.setOnClickListener((view) -> {
97                     mFragment.adjustVolumeUp(mVolumeList[position].groupId);
98                 });
99                 vh.downButton.setOnClickListener((view) -> {
100                     mFragment.adjustVolumeDown(mVolumeList[position].groupId);
101                 });
102                 vh.muteButton.setChecked(mVolumeList[position].isMuted);
103                 vh.muteButton.setOnClickListener((view) -> {
104                     mFragment.toggleMute(mVolumeList[position].groupId);
105                 });
106 
107                 vh.requestButton.setOnClickListener((view) -> {
108                     mFragment.requestFocus(mVolumeList[position].groupId);
109                 });
110             }
111         }
112         return convertView;
113     }
114 
115     @Override
getCount()116     public int getCount() {
117         return mVolumeList.length;
118     }
119 
refreshVolumes(CarAudioZoneVolumeInfo[] volumes)120     public void refreshVolumes(CarAudioZoneVolumeInfo[] volumes) {
121         mVolumeList = volumes;
122         notifyDataSetChanged();
123     }
124 
125     private static final class ViewHolder {
126         public TextView id;
127         public TextView maxVolume;
128         public TextView currentVolume;
129         public TextView isMuted;
130         public TextView isBlocked;
131         public TextView isAttenuated;
132         public TextView isSystemMuted;
133         public ToggleButton muteButton;
134         public Button upButton;
135         public Button downButton;
136         public Button requestButton;
137     }
138 }
139