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.android.settings.connecteddevice.audiosharing; 18 19 import android.content.Context; 20 import android.util.Log; 21 import android.view.LayoutInflater; 22 import android.view.View; 23 import android.view.ViewGroup; 24 import android.widget.Button; 25 26 import androidx.annotation.NonNull; 27 import androidx.recyclerview.widget.RecyclerView; 28 29 import com.android.settings.R; 30 31 import com.google.common.collect.ImmutableList; 32 33 import java.util.ArrayList; 34 import java.util.List; 35 36 public class AudioSharingDeviceAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { 37 38 private static final String TAG = "AudioSharingDeviceAdapter"; 39 40 private final Context mContext; 41 private List<AudioSharingDeviceItem> mDevices; 42 private final OnClickListener mOnClickListener; 43 private final ActionType mType; 44 AudioSharingDeviceAdapter( @onNull Context context, @NonNull List<AudioSharingDeviceItem> devices, @NonNull OnClickListener listener, @NonNull ActionType type)45 public AudioSharingDeviceAdapter( 46 @NonNull Context context, 47 @NonNull List<AudioSharingDeviceItem> devices, 48 @NonNull OnClickListener listener, 49 @NonNull ActionType type) { 50 mContext = context; 51 mDevices = devices; 52 mOnClickListener = listener; 53 mType = type; 54 } 55 56 /** 57 * The action type when user click on the item. 58 * 59 * <p>We choose the item text based on this type. 60 */ 61 public enum ActionType { 62 // Click on the item will add the item to audio sharing 63 SHARE, 64 // Click on the item will remove the item from audio sharing 65 REMOVE, 66 } 67 68 private class AudioSharingDeviceViewHolder extends RecyclerView.ViewHolder { 69 private final Button mButtonView; 70 AudioSharingDeviceViewHolder(View view)71 AudioSharingDeviceViewHolder(View view) { 72 super(view); 73 mButtonView = view.findViewById(R.id.device_button); 74 } 75 bindView(int position)76 public void bindView(int position) { 77 if (mButtonView != null) { 78 String btnText = switch (mType) { 79 case SHARE -> 80 mContext.getString( 81 R.string.audio_sharing_share_with_button_label, 82 mDevices.get(position).getName()); 83 case REMOVE -> 84 mContext.getString( 85 R.string.audio_sharing_disconnect_device_button_label, 86 mDevices.get(position).getName()); 87 }; 88 mButtonView.setText(btnText); 89 mButtonView.setOnClickListener( 90 v -> mOnClickListener.onClick(mDevices.get(position))); 91 if (position == 0) { 92 mButtonView.setBackgroundResource( 93 com.android.settingslib.R.drawable.audio_sharing_rounded_bg_ripple_top); 94 } 95 } else { 96 Log.w(TAG, "bind view skipped due to button view is null"); 97 } 98 } 99 } 100 101 @Override onCreateViewHolder(ViewGroup parent, int viewType)102 public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 103 View view = 104 LayoutInflater.from(parent.getContext()) 105 .inflate(R.layout.audio_sharing_device_item, parent, false); 106 return new AudioSharingDeviceViewHolder(view); 107 } 108 109 @Override onBindViewHolder(RecyclerView.ViewHolder holder, int position)110 public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 111 ((AudioSharingDeviceViewHolder) holder).bindView(position); 112 } 113 114 @Override getItemCount()115 public int getItemCount() { 116 return mDevices.size(); 117 } 118 119 /** Updates the data set and notify the change. */ updateItems(@onNull List<AudioSharingDeviceItem> items)120 public void updateItems(@NonNull List<AudioSharingDeviceItem> items) { 121 if (mDevices.size() != items.size()) { 122 List<AudioSharingDeviceItem> oldItems = new ArrayList<>(mDevices); 123 oldItems.removeAll(items); 124 if (oldItems.isEmpty()) { 125 Log.d(TAG, "Skip updateItems, no change"); 126 return; 127 } 128 } 129 mDevices = ImmutableList.copyOf(items); 130 Log.d(TAG, "updateItems, items = " + mDevices); 131 notifyDataSetChanged(); 132 } 133 134 public interface OnClickListener { 135 /** Called when an item has been clicked. */ onClick(AudioSharingDeviceItem item)136 void onClick(AudioSharingDeviceItem item); 137 } 138 } 139