Home | History | Annotate | Download | only in volume
      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 
     39     public VolumeAdapter(Context c, int layoutResourceId, VolumeInfo[] locations,
     40             VolumeTestFragment fragment) {
     41         super(c, layoutResourceId, locations);
     42         mFragment = fragment;
     43         mContext = c;
     44         this.mLayoutResourceId = layoutResourceId;
     45         this.mVolumeList = locations;
     46     }
     47 
     48     @Override
     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 = (TextView) convertView.findViewById(R.id.stream_id);
     55             vh.maxVolume = (TextView) convertView.findViewById(R.id.volume_limit);
     56             vh.currentVolume = (TextView) convertView.findViewById(R.id.current_volume);
     57             vh.logicalVolume = (TextView) convertView.findViewById(R.id.logical_volume);
     58             vh.logicalMax = (TextView) convertView.findViewById(R.id.logical_max);
     59             vh.upButton = (Button) convertView.findViewById(R.id.volume_up);
     60             vh.downButton = (Button) convertView.findViewById(R.id.volume_down);
     61             vh.requestButton = (Button) convertView.findViewById(R.id.request);
     62             convertView.setTag(vh);
     63         } else {
     64             vh = (ViewHolder) convertView.getTag();
     65         }
     66         if (mVolumeList[position] != null) {
     67             vh.id.setText(mVolumeList[position].mId);
     68             vh.maxVolume.setText(String.valueOf(mVolumeList[position].mMax));
     69             vh.currentVolume.setText(String.valueOf(mVolumeList[position].mCurrent));
     70             vh.logicalVolume.setText(String.valueOf(mVolumeList[position].mLogicalCurrent));
     71             vh.logicalMax.setText(String.valueOf(mVolumeList[position].mLogicalMax));
     72             int color = mVolumeList[position].mHasFocus ? Color.GREEN : Color.GRAY;
     73             vh.requestButton.setBackgroundColor(color);
     74             if (position == 0) {
     75                 vh.upButton.setVisibility(View.INVISIBLE);
     76                 vh.downButton.setVisibility(View.INVISIBLE);
     77                 vh.requestButton.setVisibility(View.INVISIBLE);
     78             } else {
     79                 vh.upButton.setVisibility(View.VISIBLE);
     80                 vh.downButton.setVisibility(View.VISIBLE);
     81                 vh.requestButton.setVisibility(View.VISIBLE);
     82                 vh.upButton.setOnClickListener(new View.OnClickListener() {
     83                     @Override
     84                     public void onClick(View view) {
     85                         mFragment.adjustVolumeByOne(mVolumeList[position].logicalStream, true);
     86                     }
     87                 });
     88 
     89                 vh.downButton.setOnClickListener(new View.OnClickListener() {
     90                     @Override
     91                     public void onClick(View view) {
     92                         mFragment.adjustVolumeByOne(mVolumeList[position].logicalStream, false);
     93                     }
     94                 });
     95 
     96                 vh.requestButton.setOnClickListener(new View.OnClickListener() {
     97                     @Override
     98                     public void onClick(View view) {
     99                         mFragment.requestFocus(mVolumeList[position].logicalStream);
    100                     }
    101                 });
    102             }
    103         }
    104         return convertView;
    105     }
    106 
    107     @Override
    108     public int getCount() {
    109         return mVolumeList.length;
    110     }
    111 
    112 
    113     public void refreshVolumes(VolumeInfo[] volumes) {
    114         mVolumeList = volumes;
    115         notifyDataSetChanged();
    116     }
    117 
    118     static class ViewHolder {
    119         TextView id;
    120         TextView maxVolume;
    121         TextView currentVolume;
    122         TextView logicalMax;
    123         TextView logicalVolume;
    124         Button upButton;
    125         Button downButton;
    126         Button requestButton;
    127     }
    128 }
    129