File indexing completed on 2024-12-22 04:41:41
0001 /* 0002 * SPDX-FileCopyrightText: 2018 Nicolas Fella <nicolas.fella@gmx.de> 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 androidx.annotation.NonNull; 0010 0011 import org.json.JSONException; 0012 import org.json.JSONObject; 0013 0014 import java.util.ArrayList; 0015 import java.util.List; 0016 0017 class Sink { 0018 0019 interface UpdateListener { 0020 void updateSink(@NonNull Sink sink); 0021 } 0022 0023 private int volume; 0024 private final String description; 0025 private final String name; 0026 private boolean mute; 0027 private final int maxVolume; 0028 private boolean enabled; 0029 0030 private final List<UpdateListener> listeners; 0031 0032 Sink(JSONObject obj) throws JSONException { 0033 listeners = new ArrayList<>(); 0034 name = obj.getString("name"); 0035 volume = obj.getInt("volume"); 0036 mute = obj.getBoolean("muted"); 0037 description = obj.getString("description"); 0038 maxVolume = obj.getInt("maxVolume"); 0039 enabled = obj.optBoolean("enabled", false); 0040 } 0041 0042 int getVolume() { 0043 return volume; 0044 } 0045 0046 0047 void setVolume(int volume) { 0048 this.volume = volume; 0049 for (UpdateListener l : listeners) { 0050 l.updateSink(this); 0051 } 0052 } 0053 0054 String getDescription() { 0055 return description; 0056 } 0057 0058 String getName() { 0059 return name; 0060 } 0061 0062 boolean isMute() { 0063 return mute; 0064 } 0065 0066 void setMute(boolean mute) { 0067 this.mute = mute; 0068 for (UpdateListener l : listeners) { 0069 l.updateSink(this); 0070 } 0071 } 0072 0073 boolean isDefault() { 0074 return enabled; 0075 } 0076 0077 void setDefault(boolean enable) { 0078 this.enabled = enable; 0079 for (UpdateListener l : listeners) { 0080 l.updateSink(this); 0081 } 0082 } 0083 0084 void addListener(UpdateListener l) { 0085 0086 if (!listeners.contains(l)) { 0087 listeners.add(l); 0088 } 0089 } 0090 0091 int getMaxVolume() { 0092 return maxVolume; 0093 } 0094 0095 void removeListener(UpdateListener l) { 0096 listeners.remove(l); 0097 } 0098 0099 }