Silence incoming ringer when power button is pressed

// FREEBIE
This commit is contained in:
Moxie Marlinspike 2017-03-05 10:02:23 -08:00
parent cd28cd172f
commit bdcfabf1ee
2 changed files with 54 additions and 1 deletions

View File

@ -133,6 +133,7 @@ public class WebRtcCallService extends Service implements InjectableType, PeerCo
public static final String ACTION_SET_MUTE_VIDEO = "SET_MUTE_VIDEO";
public static final String ACTION_BLUETOOTH_CHANGE = "BLUETOOTH_CHANGE";
public static final String ACTION_WIRED_HEADSET_CHANGE = "WIRED_HEADSET_CHANGE";
public static final String ACTION_SCREEN_OFF = "SCREEN_OFF";
public static final String ACTION_CHECK_TIMEOUT = "CHECK_TIMEOUT";
public static final String ACTION_IS_IN_CALL_QUERY = "IS_IN_CALL";
@ -160,6 +161,7 @@ public class WebRtcCallService extends Service implements InjectableType, PeerCo
private SignalAudioManager audioManager;
private BluetoothStateManager bluetoothStateManager;
private WiredHeadsetStateReceiver wiredHeadsetStateReceiver;
private PowerButtonReceiver powerButtonReceiver;
private LockManager lockManager;
private IncomingPstnCallReceiver callReceiver;
@ -210,6 +212,7 @@ public class WebRtcCallService extends Service implements InjectableType, PeerCo
else if (intent.getAction().equals(ACTION_SET_MUTE_VIDEO)) handleSetMuteVideo(intent);
else if (intent.getAction().equals(ACTION_BLUETOOTH_CHANGE)) handleBluetoothChange(intent);
else if (intent.getAction().equals(ACTION_WIRED_HEADSET_CHANGE)) handleWiredHeadsetChange(intent);
else if (intent.getAction().equals((ACTION_SCREEN_OFF))) handleScreenOffChange(intent);
else if (intent.getAction().equals(ACTION_REMOTE_VIDEO_MUTE)) handleRemoteVideoMute(intent);
else if (intent.getAction().equals(ACTION_RESPONSE_MESSAGE)) handleResponseMessage(intent);
else if (intent.getAction().equals(ACTION_ICE_MESSAGE)) handleRemoteIceCandidate(intent);
@ -242,6 +245,12 @@ public class WebRtcCallService extends Service implements InjectableType, PeerCo
if (wiredHeadsetStateReceiver != null) {
unregisterReceiver(wiredHeadsetStateReceiver);
wiredHeadsetStateReceiver = null;
}
if (powerButtonReceiver != null) {
unregisterReceiver(powerButtonReceiver);
powerButtonReceiver = null;
}
}
@ -295,6 +304,22 @@ public class WebRtcCallService extends Service implements InjectableType, PeerCo
registerReceiver(wiredHeadsetStateReceiver, new IntentFilter(action));
}
private void registerPowerButtonReceiver() {
if (powerButtonReceiver == null) {
powerButtonReceiver = new PowerButtonReceiver();
registerReceiver(powerButtonReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));
}
}
private void unregisterPowerButtonReceiver() {
if (powerButtonReceiver != null) {
unregisterReceiver(powerButtonReceiver);
powerButtonReceiver = null;
}
}
// Handlers
private void handleIncomingCall(final Intent intent) {
@ -505,6 +530,8 @@ public class WebRtcCallService extends Service implements InjectableType, PeerCo
audioManager.initializeAudioForCall();
audioManager.startIncomingRinger();
registerPowerButtonReceiver();
setCallInProgressNotification(TYPE_INCOMING_RINGING, recipient);
} else if (callState == CallState.STATE_DIALING) {
if (this.recipient == null) throw new AssertionError("assert");
@ -541,6 +568,8 @@ public class WebRtcCallService extends Service implements InjectableType, PeerCo
sendMessage(WebRtcViewModel.State.CALL_CONNECTED, recipient, localVideoEnabled, remoteVideoEnabled, bluetoothAvailable, microphoneEnabled);
unregisterPowerButtonReceiver();
setCallInProgressNotification(TYPE_ESTABLISHED, recipient);
this.peerConnection.setAudioEnabled(microphoneEnabled);
@ -753,6 +782,15 @@ public class WebRtcCallService extends Service implements InjectableType, PeerCo
}
}
private void handleScreenOffChange(Intent intent) {
if (callState == CallState.STATE_ANSWERING ||
callState == CallState.STATE_LOCAL_RINGING)
{
Log.w(TAG, "Silencing incoming ringer...");
audioManager.silenceIncomingRinger();
}
}
private void handleRemoteVideoMute(Intent intent) {
boolean muted = intent.getBooleanExtra(EXTRA_MUTE, false);
long callId = intent.getLongExtra(EXTRA_CALL_ID, -1);
@ -1110,6 +1148,17 @@ public class WebRtcCallService extends Service implements InjectableType, PeerCo
}
}
private static class PowerButtonReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
Intent serviceIntent = new Intent(context, WebRtcCallService.class);
serviceIntent.setAction(WebRtcCallService.ACTION_SCREEN_OFF);
context.startService(serviceIntent);
}
}
}
private class TimeoutRunnable implements Runnable {
private final long callId;

View File

@ -75,6 +75,10 @@ public class SignalAudioManager {
outgoingRinger.start(type);
}
public void silenceIncomingRinger() {
incomingRinger.stop();
}
public void startCommunication(boolean preserveSpeakerphone) {
AudioManager audioManager = ServiceUtil.getAudioManager(context);