Skip to content

Commit

Permalink
Add ability to mute speakers
Browse files Browse the repository at this point in the history
  • Loading branch information
Popalay committed Feb 25, 2021
1 parent d6bf4d9 commit 3ba052d
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 27 deletions.
84 changes: 60 additions & 24 deletions Houseclub/src/main/java/me/grishka/houseclub/VoiceService.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public class VoiceService extends Service{
private RtcEngine engine;
private Channel channel;
private boolean muted=true;
private boolean isSpeakerMuted=false;
private Handler uiHandler=new Handler(Looper.getMainLooper());
private Runnable pinger=new Runnable(){
@Override
Expand Down Expand Up @@ -114,36 +115,57 @@ public int onStartCommand(Intent intent, int flags, int startId){
channel=DataProvider.getChannel(id);
updateChannel(channel);

Intent snoozeIntent = new Intent(this, NotificationHandlerBroadcastReceiver.class);
snoozeIntent.setAction(NotificationHandlerBroadcastReceiver.ACTION_LEAVE_ROOM);
PendingIntent leaveRoomPendingIntent = PendingIntent.getBroadcast(this, 0, snoozeIntent, 0);
Notification.Action leaveRoomAction = new Notification.Action.Builder(
Icon.createWithResource(this, R.drawable.ic_leave),
getString(R.string.leave_room),
leaveRoomPendingIntent
).build();

NotificationManager nm=getSystemService(NotificationManager.class);
Notification.Builder n=new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_phone_in_talk)
.setContentTitle(getString(R.string.ongoing_call))
.setContentText(intent.getStringExtra("topic"))
.setContentIntent(PendingIntent.getActivity(this, 1, new Intent(this, MainActivity.class).putExtra("openCurrentChannel", true), PendingIntent.FLAG_UPDATE_CURRENT))
.addAction(leaveRoomAction);
if(Build.VERSION.SDK_INT>=26){
if(nm.getNotificationChannel("ongoing")==null){
NotificationChannel nc=new NotificationChannel("ongoing", "Ongoing calls", NotificationManager.IMPORTANCE_LOW);
nm.createNotificationChannel(nc);
}
n.setChannelId("ongoing");
}
startForeground(10, n.build());
Notification n=buildNotification();
startForeground(10, n);

doJoinChannel();
}
return START_NOT_STICKY;
}

private Notification buildNotification(){
Intent leaveRoomIntent = new Intent(this, NotificationHandlerBroadcastReceiver.class);
leaveRoomIntent.setAction(NotificationHandlerBroadcastReceiver.ACTION_LEAVE_ROOM);
PendingIntent leaveRoomPendingIntent = PendingIntent.getBroadcast(this, 0, leaveRoomIntent, 0);
Notification.Action leaveRoomAction = new Notification.Action.Builder(
Icon.createWithResource(this, R.drawable.ic_leave),
getString(R.string.leave_room),
leaveRoomPendingIntent
).build();

Intent muteSpeakerIntent = new Intent(this, NotificationHandlerBroadcastReceiver.class);
muteSpeakerIntent.setAction(NotificationHandlerBroadcastReceiver.ACTION_TOGGLE_MUTE_SPEAKER);
PendingIntent muteSpeakerPendingIntent = PendingIntent.getBroadcast(this, 0, muteSpeakerIntent, 0);
Notification.Action muteSpeakerAction = new Notification.Action.Builder(
Icon.createWithResource(this, isSpeakerMuted?R.drawable.ic_baseline_volume_off_24:R.drawable.ic_baseline_volume_up_24),
getString(isSpeakerMuted?R.string.unmute_speaker:R.string.mute_speaker),
muteSpeakerPendingIntent
).build();

NotificationManager nm=getSystemService(NotificationManager.class);
Notification.Builder n=new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_phone_in_talk)
.setContentTitle(getString(R.string.ongoing_call))
.setContentText(channel.topic)
.setContentIntent(PendingIntent.getActivity(this, 1, new Intent(this, MainActivity.class).putExtra("openCurrentChannel", true), PendingIntent.FLAG_UPDATE_CURRENT))
.addAction(leaveRoomAction)
.addAction(muteSpeakerAction);
if(Build.VERSION.SDK_INT>=26){
if(nm.getNotificationChannel("ongoing")==null){
NotificationChannel nc=new NotificationChannel("ongoing", "Ongoing calls", NotificationManager.IMPORTANCE_LOW);
nm.createNotificationChannel(nc);
}
n.setChannelId("ongoing");
}
return n.build();
}

private void updateNotification(){
NotificationManager nm=getSystemService(NotificationManager.class);
Notification n= buildNotification();
nm.notify(10, n);
}

private void doJoinChannel(){
engine.setChannelProfile(isSelfSpeaker ? Constants.CHANNEL_PROFILE_COMMUNICATION : Constants.CHANNEL_PROFILE_LIVE_BROADCASTING);
engine.joinChannel(channel.token, channel.channel, "", Integer.parseInt(ClubhouseSession.userID));
Expand Down Expand Up @@ -305,10 +327,23 @@ public void setMuted(boolean muted){
engine.muteLocalAudioStream(muted);
}

public void setSpeakerMuted(boolean muted){
this.isSpeakerMuted=muted;
engine.adjustPlaybackSignalVolume(muted?0:100);
engine.adjustAudioMixingPlayoutVolume(muted?0:100);
updateNotification();
for(ChannelEventListener l:listeners)
l.onSpeakerMuted(muted);
}

public boolean isMuted(){
return muted;
}

public boolean isSpeakerMuted(){
return isSpeakerMuted;
}

public Channel getChannel(){
return channel;
}
Expand Down Expand Up @@ -416,6 +451,7 @@ public void run(){

public interface ChannelEventListener{
void onUserMuteChanged(int id, boolean muted);
void onSpeakerMuted(boolean muted);
void onUserJoined(ChannelUser user);
void onUserLeft(int id);
void onCanSpeak(String inviterName, int inviterID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class InChannelFragment extends BaseRecyclerFragment<ChannelUser> impleme
private MergeRecyclerAdapter adapter;
private UserListAdapter speakersAdapter, followedAdapter, othersAdapter;
private ImageButton muteBtn;
private ImageButton muteSpeakerBtn;
private Button raiseBtn;
private Channel channel;
private ArrayList<ChannelUser> speakers=new ArrayList<>(), followedBySpeakers=new ArrayList<>(), otherUsers=new ArrayList<>();
Expand All @@ -70,9 +71,11 @@ public void onViewCreated(View view, Bundle savedInstanceState){

raiseBtn=view.findViewById(R.id.raise);
muteBtn=view.findViewById(R.id.mute);
muteSpeakerBtn=view.findViewById(R.id.mute_speaker);

raiseBtn.setOnClickListener(this::onRaiseClick);
muteBtn.setOnClickListener(this::onMuteClick);
muteSpeakerBtn.setOnClickListener(this::onMuteSpeakerClick);

GridLayoutManager lm=new GridLayoutManager(getActivity(), 12);
lm.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup(){
Expand All @@ -97,6 +100,7 @@ public int getSpanSize(int position){
VoiceService svc=VoiceService.getInstance();
if(svc!=null){
muteBtn.setImageResource(svc.isMuted() ? R.drawable.ic_mic_off : R.drawable.ic_mic);
muteSpeakerBtn.setImageResource(svc.isSpeakerMuted() ? R.drawable.ic_baseline_volume_off_24 : R.drawable.ic_baseline_volume_up_24);
onUserMuteChanged(Integer.parseInt(ClubhouseSession.userID), svc.isMuted());
}
}
Expand Down Expand Up @@ -168,6 +172,11 @@ private void onMuteClick(View v){
onUserMuteChanged(Integer.parseInt(ClubhouseSession.userID), svc.isMuted());
}

private void onMuteSpeakerClick(View v){
VoiceService svc=VoiceService.getInstance();
svc.setSpeakerMuted(!svc.isSpeakerMuted());
}

@Override
public void onUserMuteChanged(int id, boolean muted){
int i=0;
Expand All @@ -189,6 +198,11 @@ public void onUserMuteChanged(int id, boolean muted){
}
}

@Override
public void onSpeakerMuted(boolean muted) {
muteSpeakerBtn.setImageResource(muted ? R.drawable.ic_baseline_volume_off_24 : R.drawable.ic_baseline_volume_up_24);
}

@Override
public void onUserJoined(ChannelUser user){
if(user.isSpeaker){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@

public class NotificationHandlerBroadcastReceiver extends BroadcastReceiver {
public static final String ACTION_LEAVE_ROOM = "ACTION_LEAVE_ROOM";
public static final String ACTION_TOGGLE_MUTE_SPEAKER = "ACTION_TOGGLE_MUTE_SPEAKER";

@Override
public void onReceive(Context context, Intent intent) {
VoiceService svc=VoiceService.getInstance();
if(svc==null)return;
if (Objects.equals(intent.getAction(), ACTION_LEAVE_ROOM)) {
if (VoiceService.getInstance() != null) {
VoiceService.getInstance().leaveCurrentChannel();
}
svc.leaveCurrentChannel();
}else if (Objects.equals(intent.getAction(), ACTION_TOGGLE_MUTE_SPEAKER)) {
svc.setSpeakerMuted(!svc.isSpeakerMuted());
}
}
}
10 changes: 10 additions & 0 deletions Houseclub/src/main/res/drawable/ic_baseline_volume_off_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="#000">
<path
android:fillColor="@android:color/white"
android:pathData="M16.5,12c0,-1.77 -1.02,-3.29 -2.5,-4.03v2.21l2.45,2.45c0.03,-0.2 0.05,-0.41 0.05,-0.63zM19,12c0,0.94 -0.2,1.82 -0.54,2.64l1.51,1.51C20.63,14.91 21,13.5 21,12c0,-4.28 -2.99,-7.86 -7,-8.77v2.06c2.89,0.86 5,3.54 5,6.71zM4.27,3L3,4.27 7.73,9L3,9v6h4l5,5v-6.73l4.25,4.25c-0.67,0.52 -1.42,0.93 -2.25,1.18v2.06c1.38,-0.31 2.63,-0.95 3.69,-1.81L19.73,21 21,19.73l-9,-9L4.27,3zM12,4L9.91,6.09 12,8.18L12,4z"/>
</vector>
10 changes: 10 additions & 0 deletions Houseclub/src/main/res/drawable/ic_baseline_volume_up_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="#000">
<path
android:fillColor="@android:color/white"
android:pathData="M3,9v6h4l5,5L12,4L7,9L3,9zM16.5,12c0,-1.77 -1.02,-3.29 -2.5,-4.03v8.05c1.48,-0.73 2.5,-2.25 2.5,-4.02zM14,3.23v2.06c2.89,0.86 5,3.54 5,6.71s-2.11,5.85 -5,6.71v2.06c4.01,-0.91 7,-4.49 7,-8.77s-2.99,-7.86 -7,-8.77z"/>
</vector>
10 changes: 10 additions & 0 deletions Houseclub/src/main/res/layout/in_channel.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@
style="@style/Widget.Button.Grey"
android:text="@string/leave_room"/>

<ImageButton
android:id="@+id/mute_speaker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="0dp"
android:layout_marginStart="4dp"
android:src="@drawable/ic_baseline_volume_up_24"
style="@style/Widget.Button.Grey"/>

<View
android:layout_width="0dp"
android:layout_height="1dp"
Expand All @@ -54,6 +63,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="0dp"
android:layout_marginStart="4dp"
android:src="@drawable/ic_mic_off"
style="@style/Widget.Button.Grey"/>

Expand Down
2 changes: 2 additions & 0 deletions Houseclub/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
<string name="log_out">Log out</string>
<string name="ongoing_call">Ongoing call</string>
<string name="leave_room">Leave room</string>
<string name="mute_speaker">Mute speaker</string>
<string name="unmute_speaker">Unmute speaker</string>
<string name="join_this_room">Join this room?</string>
<string name="join">Join</string>
<string name="cancel">Cancel</string>
Expand Down

0 comments on commit 3ba052d

Please sign in to comment.