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 java.util.List;
32 
33 public class AudioSharingDeviceAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
34 
35     private static final String TAG = "AudioSharingDeviceAdapter";
36 
37     private final Context mContext;
38     private final List<AudioSharingDeviceItem> mDevices;
39     private final OnClickListener mOnClickListener;
40     private final ActionType mType;
41 
AudioSharingDeviceAdapter( @onNull Context context, @NonNull List<AudioSharingDeviceItem> devices, @NonNull OnClickListener listener, @NonNull ActionType type)42     public AudioSharingDeviceAdapter(
43             @NonNull Context context,
44             @NonNull List<AudioSharingDeviceItem> devices,
45             @NonNull OnClickListener listener,
46             @NonNull ActionType type) {
47         mContext = context;
48         mDevices = devices;
49         mOnClickListener = listener;
50         mType = type;
51     }
52 
53     /**
54      * The action type when user click on the item.
55      *
56      * <p>We choose the item text based on this type.
57      */
58     public enum ActionType {
59         // Click on the item will add the item to audio sharing
60         SHARE,
61         // Click on the item will remove the item from audio sharing
62         REMOVE,
63     }
64 
65     private class AudioSharingDeviceViewHolder extends RecyclerView.ViewHolder {
66         private final Button mButtonView;
67 
AudioSharingDeviceViewHolder(View view)68         AudioSharingDeviceViewHolder(View view) {
69             super(view);
70             mButtonView = view.findViewById(R.id.device_button);
71         }
72 
bindView(int position)73         public void bindView(int position) {
74             if (mButtonView != null) {
75                 String btnText = switch (mType) {
76                     case SHARE ->
77                             mContext.getString(
78                                     R.string.audio_sharing_share_with_button_label,
79                                     mDevices.get(position).getName());
80                     case REMOVE ->
81                             mContext.getString(
82                                     R.string.audio_sharing_disconnect_device_button_label,
83                                     mDevices.get(position).getName());
84                 };
85                 mButtonView.setText(btnText);
86                 mButtonView.setOnClickListener(
87                         v -> mOnClickListener.onClick(mDevices.get(position)));
88             } else {
89                 Log.w(TAG, "bind view skipped due to button view is null");
90             }
91         }
92     }
93 
94     @Override
onCreateViewHolder(ViewGroup parent, int viewType)95     public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
96         View view =
97                 LayoutInflater.from(parent.getContext())
98                         .inflate(R.layout.audio_sharing_device_item, parent, false);
99         return new AudioSharingDeviceViewHolder(view);
100     }
101 
102     @Override
onBindViewHolder(RecyclerView.ViewHolder holder, int position)103     public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
104         ((AudioSharingDeviceViewHolder) holder).bindView(position);
105     }
106 
107     @Override
getItemCount()108     public int getItemCount() {
109         return mDevices.size();
110     }
111 
112     public interface OnClickListener {
113         /** Called when an item has been clicked. */
onClick(AudioSharingDeviceItem item)114         void onClick(AudioSharingDeviceItem item);
115     }
116 }
117