File indexing completed on 2024-12-22 04:41:41
0001 /* 0002 * SPDX-FileCopyrightText: 2021 Art Pinch <leonardo906@mail.ru> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0005 */ 0006 0007 package org.kde.kdeconnect.Plugins.SystemVolumePlugin; 0008 0009 import android.view.View; 0010 import android.widget.CompoundButton; 0011 import android.widget.ImageButton; 0012 import android.widget.RadioButton; 0013 import android.widget.SeekBar; 0014 import android.widget.Toast; 0015 0016 import androidx.annotation.NonNull; 0017 import androidx.core.util.Consumer; 0018 import androidx.recyclerview.widget.RecyclerView; 0019 0020 import org.kde.kdeconnect_tp.R; 0021 import org.kde.kdeconnect_tp.databinding.ListItemSystemvolumeBinding; 0022 0023 class SinkItemHolder extends RecyclerView.ViewHolder 0024 implements 0025 SeekBar.OnSeekBarChangeListener, 0026 ImageButton.OnClickListener, 0027 CompoundButton.OnCheckedChangeListener, 0028 View.OnLongClickListener { 0029 0030 private final ListItemSystemvolumeBinding viewBinding; 0031 private final SystemVolumePlugin plugin; 0032 private final Consumer<Boolean> seekBarTracking; 0033 0034 private Sink sink; 0035 0036 public SinkItemHolder( 0037 @NonNull ListItemSystemvolumeBinding viewBinding, 0038 @NonNull SystemVolumePlugin plugin, 0039 @NonNull Consumer<Boolean> seekBarTracking 0040 ) { 0041 super(viewBinding.getRoot()); 0042 this.viewBinding = viewBinding; 0043 this.plugin = plugin; 0044 this.seekBarTracking = seekBarTracking; 0045 0046 viewBinding.sinkCard.setOnLongClickListener(this); 0047 viewBinding.systemvolumeLabel.setOnLongClickListener(this); 0048 0049 viewBinding.systemvolumeLabel.setOnCheckedChangeListener(this); 0050 viewBinding.systemvolumeMute.setOnClickListener(this); 0051 viewBinding.systemvolumeSeek.setOnSeekBarChangeListener(this); 0052 } 0053 0054 @Override 0055 public void onProgressChanged(final SeekBar seekBar, int i, boolean triggeredByUser) { 0056 if (triggeredByUser) { 0057 plugin.sendVolume(sink.getName(), seekBar.getProgress()); 0058 } 0059 } 0060 0061 @Override 0062 public void onStartTrackingTouch(SeekBar seekBar) { 0063 seekBarTracking.accept(true); 0064 } 0065 0066 @Override 0067 public void onStopTrackingTouch(final SeekBar seekBar) { 0068 seekBarTracking.accept(false); 0069 plugin.sendVolume(sink.getName(), seekBar.getProgress()); 0070 } 0071 0072 @Override 0073 public void onClick(View view) { 0074 plugin.sendMute(sink.getName(), !sink.isMute()); 0075 } 0076 0077 @Override 0078 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 0079 if (isChecked) { 0080 plugin.sendEnable(sink.getName()); 0081 } 0082 } 0083 0084 @Override 0085 public boolean onLongClick(View v) { 0086 Toast.makeText(v.getContext(), sink.getName(), Toast.LENGTH_SHORT).show(); 0087 return true; 0088 } 0089 0090 public void bind(Sink sink) { 0091 this.sink = sink; 0092 0093 final RadioButton radioButton = viewBinding.systemvolumeLabel; 0094 radioButton.setChecked(sink.isDefault()); 0095 radioButton.setText(sink.getDescription()); 0096 0097 final SeekBar seekBar = viewBinding.systemvolumeSeek; 0098 seekBar.setMax(sink.getMaxVolume()); 0099 seekBar.setProgress(sink.getVolume()); 0100 0101 int iconRes = sink.isMute() ? R.drawable.ic_volume_mute_black : R.drawable.ic_volume_black; 0102 0103 ImageButton button = viewBinding.systemvolumeMute; 0104 button.setImageResource(iconRes); 0105 } 0106 }