From 3ba052d076a83eeb73c6f76307f081ffe5beb28d Mon Sep 17 00:00:00 2001 From: Denys Nykyforov Date: Thu, 25 Feb 2021 23:30:21 +0800 Subject: [PATCH] Add ability to mute speakers --- .../me/grishka/houseclub/VoiceService.java | 84 +++++++++++++------ .../fragments/InChannelFragment.java | 14 ++++ .../NotificationHandlerBroadcastReceiver.java | 9 +- .../drawable/ic_baseline_volume_off_24.xml | 10 +++ .../res/drawable/ic_baseline_volume_up_24.xml | 10 +++ Houseclub/src/main/res/layout/in_channel.xml | 10 +++ Houseclub/src/main/res/values/strings.xml | 2 + 7 files changed, 112 insertions(+), 27 deletions(-) create mode 100644 Houseclub/src/main/res/drawable/ic_baseline_volume_off_24.xml create mode 100644 Houseclub/src/main/res/drawable/ic_baseline_volume_up_24.xml diff --git a/Houseclub/src/main/java/me/grishka/houseclub/VoiceService.java b/Houseclub/src/main/java/me/grishka/houseclub/VoiceService.java index cf547b3b..7d175182 100644 --- a/Houseclub/src/main/java/me/grishka/houseclub/VoiceService.java +++ b/Houseclub/src/main/java/me/grishka/houseclub/VoiceService.java @@ -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 @@ -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)); @@ -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; } @@ -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); diff --git a/Houseclub/src/main/java/me/grishka/houseclub/fragments/InChannelFragment.java b/Houseclub/src/main/java/me/grishka/houseclub/fragments/InChannelFragment.java index 11f96414..dc152693 100644 --- a/Houseclub/src/main/java/me/grishka/houseclub/fragments/InChannelFragment.java +++ b/Houseclub/src/main/java/me/grishka/houseclub/fragments/InChannelFragment.java @@ -48,6 +48,7 @@ public class InChannelFragment extends BaseRecyclerFragment impleme private MergeRecyclerAdapter adapter; private UserListAdapter speakersAdapter, followedAdapter, othersAdapter; private ImageButton muteBtn; + private ImageButton muteSpeakerBtn; private Button raiseBtn; private Channel channel; private ArrayList speakers=new ArrayList<>(), followedBySpeakers=new ArrayList<>(), otherUsers=new ArrayList<>(); @@ -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(){ @@ -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()); } } @@ -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; @@ -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){ diff --git a/Houseclub/src/main/java/me/grishka/houseclub/notification/NotificationHandlerBroadcastReceiver.java b/Houseclub/src/main/java/me/grishka/houseclub/notification/NotificationHandlerBroadcastReceiver.java index 7dddd23d..b5b67943 100644 --- a/Houseclub/src/main/java/me/grishka/houseclub/notification/NotificationHandlerBroadcastReceiver.java +++ b/Houseclub/src/main/java/me/grishka/houseclub/notification/NotificationHandlerBroadcastReceiver.java @@ -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()); } } } diff --git a/Houseclub/src/main/res/drawable/ic_baseline_volume_off_24.xml b/Houseclub/src/main/res/drawable/ic_baseline_volume_off_24.xml new file mode 100644 index 00000000..bc335ce6 --- /dev/null +++ b/Houseclub/src/main/res/drawable/ic_baseline_volume_off_24.xml @@ -0,0 +1,10 @@ + + + diff --git a/Houseclub/src/main/res/drawable/ic_baseline_volume_up_24.xml b/Houseclub/src/main/res/drawable/ic_baseline_volume_up_24.xml new file mode 100644 index 00000000..74f7c1e6 --- /dev/null +++ b/Houseclub/src/main/res/drawable/ic_baseline_volume_up_24.xml @@ -0,0 +1,10 @@ + + + diff --git a/Houseclub/src/main/res/layout/in_channel.xml b/Houseclub/src/main/res/layout/in_channel.xml index 6daac209..14729c69 100644 --- a/Houseclub/src/main/res/layout/in_channel.xml +++ b/Houseclub/src/main/res/layout/in_channel.xml @@ -37,6 +37,15 @@ style="@style/Widget.Button.Grey" android:text="@string/leave_room"/> + + diff --git a/Houseclub/src/main/res/values/strings.xml b/Houseclub/src/main/res/values/strings.xml index 5d4b63ce..1136b547 100644 --- a/Houseclub/src/main/res/values/strings.xml +++ b/Houseclub/src/main/res/values/strings.xml @@ -17,6 +17,8 @@ Log out Ongoing call Leave room + Mute speaker + Unmute speaker Join this room? Join Cancel