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 
27 import com.google.android.car.kitchensink.R;
28 import com.google.android.car.kitchensink.volume.VolumeTestFragment.VolumeInfo;
29 
30 
31 public class VolumeAdapter extends ArrayAdapter<VolumeInfo> {
32 
33     private final Context mContext;
34     private VolumeInfo[] mVolumeList;
35     private final int mLayoutResourceId;
36     private VolumeTestFragment mFragment;
37 
38 
VolumeAdapter(Context c, int layoutResourceId, VolumeInfo[] volumeList, VolumeTestFragment fragment)39     public VolumeAdapter(Context c, int layoutResourceId, VolumeInfo[] volumeList,
40             VolumeTestFragment fragment) {
41         super(c, layoutResourceId, volumeList);
42         mFragment = fragment;
43         mContext = c;
44         this.mLayoutResourceId = layoutResourceId;
45         this.mVolumeList = volumeList;
46     }
47 
48     @Override
getView(int position, View convertView, ViewGroup parent)49     public View getView(int position, View convertView, ViewGroup parent) {
50         ViewHolder vh = new ViewHolder();
51         if (convertView == null) {
52             LayoutInflater inflater = LayoutInflater.from(mContext);
53             convertView = inflater.inflate(mLayoutResourceId, parent, false);
54             vh.id = convertView.findViewById(R.id.stream_id);
55             vh.maxVolume = convertView.findViewById(R.id.volume_limit);
56             vh.currentVolume = convertView.findViewById(R.id.current_volume);
57             vh.upButton = convertView.findViewById(R.id.volume_up);
58             vh.downButton = convertView.findViewById(R.id.volume_down);
59             vh.requestButton = convertView.findViewById(R.id.request);
60             convertView.setTag(vh);
61         } else {
62             vh = (ViewHolder) convertView.getTag();
63         }
64         if (mVolumeList[position] != null) {
65             vh.id.setText(mVolumeList[position].mId);
66             vh.maxVolume.setText(String.valueOf(mVolumeList[position].mMax));
67             vh.currentVolume.setText(String.valueOf(mVolumeList[position].mCurrent));
68             int color = mVolumeList[position].mHasFocus ? Color.GREEN : Color.GRAY;
69             vh.requestButton.setBackgroundColor(color);
70             if (position == 0) {
71                 vh.upButton.setVisibility(View.INVISIBLE);
72                 vh.downButton.setVisibility(View.INVISIBLE);
73                 vh.requestButton.setVisibility(View.INVISIBLE);
74             } else {
75                 vh.upButton.setVisibility(View.VISIBLE);
76                 vh.downButton.setVisibility(View.VISIBLE);
77                 vh.requestButton.setVisibility(View.VISIBLE);
78                 vh.upButton.setOnClickListener((view) -> {
79                     mFragment.adjustVolumeByOne(mVolumeList[position].mGroupId, true);
80                 });
81                 vh.downButton.setOnClickListener((view) -> {
82                     mFragment.adjustVolumeByOne(mVolumeList[position].mGroupId, false);
83                 });
84 
85                 vh.requestButton.setOnClickListener((view) -> {
86                     mFragment.requestFocus(mVolumeList[position].mGroupId);
87                 });
88             }
89         }
90         return convertView;
91     }
92 
93     @Override
getCount()94     public int getCount() {
95         return mVolumeList.length;
96     }
97 
98 
refreshVolumes(VolumeInfo[] volumes)99     public void refreshVolumes(VolumeInfo[] volumes) {
100         mVolumeList = volumes;
101         notifyDataSetChanged();
102     }
103 
104     static class ViewHolder {
105         TextView id;
106         TextView maxVolume;
107         TextView currentVolume;
108         Button upButton;
109         Button downButton;
110         Button requestButton;
111     }
112 }
113