2016-11-09 17:37:40 +00:00
|
|
|
package org.thoughtcrime.securesms.service;
|
|
|
|
|
|
|
|
|
2017-11-25 06:00:30 +00:00
|
|
|
import android.Manifest;
|
2016-11-09 17:37:40 +00:00
|
|
|
import android.app.Service;
|
2017-03-04 23:48:10 +00:00
|
|
|
import android.content.BroadcastReceiver;
|
2017-02-05 20:38:08 +00:00
|
|
|
import android.content.Context;
|
2016-11-09 17:37:40 +00:00
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.IntentFilter;
|
|
|
|
import android.media.AudioManager;
|
2018-02-16 19:10:35 +00:00
|
|
|
import android.net.Uri;
|
2017-03-04 23:48:10 +00:00
|
|
|
import android.os.Build;
|
2017-02-05 20:38:08 +00:00
|
|
|
import android.os.Bundle;
|
2016-11-09 17:37:40 +00:00
|
|
|
import android.os.Handler;
|
2017-02-05 20:38:08 +00:00
|
|
|
import android.os.HandlerThread;
|
2016-11-09 17:37:40 +00:00
|
|
|
import android.os.IBinder;
|
2017-02-05 20:38:08 +00:00
|
|
|
import android.os.ResultReceiver;
|
2016-11-09 17:37:40 +00:00
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.annotation.Nullable;
|
2017-02-05 20:38:08 +00:00
|
|
|
import android.support.annotation.WorkerThread;
|
2019-05-23 19:56:05 +00:00
|
|
|
import android.telephony.PhoneStateListener;
|
2016-11-09 17:37:40 +00:00
|
|
|
import android.telephony.TelephonyManager;
|
|
|
|
import android.util.Pair;
|
|
|
|
|
|
|
|
import com.google.protobuf.InvalidProtocolBufferException;
|
|
|
|
|
2017-02-18 04:27:11 +00:00
|
|
|
import org.greenrobot.eventbus.EventBus;
|
2016-11-09 17:37:40 +00:00
|
|
|
import org.thoughtcrime.securesms.ApplicationContext;
|
|
|
|
import org.thoughtcrime.securesms.WebRtcCallActivity;
|
2017-02-17 21:12:48 +00:00
|
|
|
import org.thoughtcrime.securesms.contacts.ContactAccessor;
|
2018-05-22 09:13:10 +00:00
|
|
|
import org.thoughtcrime.securesms.crypto.UnidentifiedAccessUtil;
|
2017-07-26 16:59:15 +00:00
|
|
|
import org.thoughtcrime.securesms.database.Address;
|
2016-11-09 17:37:40 +00:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
2018-02-16 19:10:35 +00:00
|
|
|
import org.thoughtcrime.securesms.database.RecipientDatabase.VibrateState;
|
2016-11-09 17:37:40 +00:00
|
|
|
import org.thoughtcrime.securesms.dependencies.InjectableType;
|
2017-02-03 02:34:51 +00:00
|
|
|
import org.thoughtcrime.securesms.events.WebRtcViewModel;
|
2019-06-24 06:00:15 +00:00
|
|
|
import org.thoughtcrime.securesms.logging.Log;
|
2016-11-09 17:37:40 +00:00
|
|
|
import org.thoughtcrime.securesms.notifications.MessageNotifier;
|
2017-11-25 06:00:30 +00:00
|
|
|
import org.thoughtcrime.securesms.permissions.Permissions;
|
2016-11-09 17:37:40 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
|
|
|
import org.thoughtcrime.securesms.util.FutureTaskListener;
|
|
|
|
import org.thoughtcrime.securesms.util.ListenableFutureTask;
|
|
|
|
import org.thoughtcrime.securesms.util.ServiceUtil;
|
2019-05-23 19:56:05 +00:00
|
|
|
import org.thoughtcrime.securesms.util.TelephonyUtil;
|
2017-02-17 21:12:48 +00:00
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
2016-11-09 17:37:40 +00:00
|
|
|
import org.thoughtcrime.securesms.util.Util;
|
2017-02-22 23:49:05 +00:00
|
|
|
import org.thoughtcrime.securesms.webrtc.CallNotificationBuilder;
|
2018-04-25 18:00:03 +00:00
|
|
|
import org.thoughtcrime.securesms.webrtc.CameraState;
|
2017-03-14 20:24:24 +00:00
|
|
|
import org.thoughtcrime.securesms.webrtc.IncomingPstnCallReceiver;
|
2016-11-09 17:37:40 +00:00
|
|
|
import org.thoughtcrime.securesms.webrtc.PeerConnectionFactoryOptions;
|
|
|
|
import org.thoughtcrime.securesms.webrtc.PeerConnectionWrapper;
|
|
|
|
import org.thoughtcrime.securesms.webrtc.PeerConnectionWrapper.PeerConnectionException;
|
2017-03-14 20:24:24 +00:00
|
|
|
import org.thoughtcrime.securesms.webrtc.UncaughtExceptionHandlerManager;
|
2016-11-09 17:37:40 +00:00
|
|
|
import org.thoughtcrime.securesms.webrtc.WebRtcDataProtos;
|
|
|
|
import org.thoughtcrime.securesms.webrtc.WebRtcDataProtos.Connected;
|
|
|
|
import org.thoughtcrime.securesms.webrtc.WebRtcDataProtos.Data;
|
|
|
|
import org.thoughtcrime.securesms.webrtc.WebRtcDataProtos.Hangup;
|
2017-03-04 23:48:10 +00:00
|
|
|
import org.thoughtcrime.securesms.webrtc.audio.BluetoothStateManager;
|
2016-11-09 17:37:40 +00:00
|
|
|
import org.thoughtcrime.securesms.webrtc.audio.OutgoingRinger;
|
2017-03-04 23:48:10 +00:00
|
|
|
import org.thoughtcrime.securesms.webrtc.audio.SignalAudioManager;
|
2017-03-14 20:24:24 +00:00
|
|
|
import org.thoughtcrime.securesms.webrtc.locks.LockManager;
|
2016-11-09 17:37:40 +00:00
|
|
|
import org.webrtc.AudioTrack;
|
|
|
|
import org.webrtc.DataChannel;
|
2019-01-29 22:25:49 +00:00
|
|
|
import org.webrtc.DefaultVideoDecoderFactory;
|
|
|
|
import org.webrtc.DefaultVideoEncoderFactory;
|
2016-11-09 17:37:40 +00:00
|
|
|
import org.webrtc.EglBase;
|
|
|
|
import org.webrtc.IceCandidate;
|
|
|
|
import org.webrtc.MediaConstraints;
|
|
|
|
import org.webrtc.MediaStream;
|
|
|
|
import org.webrtc.PeerConnection;
|
|
|
|
import org.webrtc.PeerConnectionFactory;
|
2017-02-26 02:31:59 +00:00
|
|
|
import org.webrtc.RtpReceiver;
|
2016-11-09 17:37:40 +00:00
|
|
|
import org.webrtc.SessionDescription;
|
|
|
|
import org.webrtc.SurfaceViewRenderer;
|
2019-01-29 22:25:49 +00:00
|
|
|
import org.webrtc.VideoDecoderFactory;
|
|
|
|
import org.webrtc.VideoEncoderFactory;
|
2016-11-09 17:37:40 +00:00
|
|
|
import org.webrtc.VideoTrack;
|
2017-02-03 02:34:51 +00:00
|
|
|
import org.whispersystems.libsignal.IdentityKey;
|
2016-11-09 17:37:40 +00:00
|
|
|
import org.whispersystems.signalservice.api.SignalServiceAccountManager;
|
|
|
|
import org.whispersystems.signalservice.api.SignalServiceMessageSender;
|
|
|
|
import org.whispersystems.signalservice.api.crypto.UntrustedIdentityException;
|
|
|
|
import org.whispersystems.signalservice.api.messages.calls.AnswerMessage;
|
|
|
|
import org.whispersystems.signalservice.api.messages.calls.BusyMessage;
|
|
|
|
import org.whispersystems.signalservice.api.messages.calls.HangupMessage;
|
|
|
|
import org.whispersystems.signalservice.api.messages.calls.IceUpdateMessage;
|
|
|
|
import org.whispersystems.signalservice.api.messages.calls.OfferMessage;
|
|
|
|
import org.whispersystems.signalservice.api.messages.calls.SignalServiceCallMessage;
|
|
|
|
import org.whispersystems.signalservice.api.messages.calls.TurnServerInfo;
|
|
|
|
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
|
2017-02-03 02:34:51 +00:00
|
|
|
import org.whispersystems.signalservice.api.push.exceptions.UnregisteredUserException;
|
2017-02-05 20:38:08 +00:00
|
|
|
import org.whispersystems.signalservice.internal.util.concurrent.SettableFuture;
|
2016-11-09 17:37:40 +00:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
import java.security.SecureRandom;
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.concurrent.Callable;
|
|
|
|
import java.util.concurrent.ExecutionException;
|
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
import java.util.concurrent.ScheduledExecutorService;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
import javax.inject.Inject;
|
|
|
|
|
2017-02-22 23:49:05 +00:00
|
|
|
import static org.thoughtcrime.securesms.webrtc.CallNotificationBuilder.TYPE_ESTABLISHED;
|
2017-11-27 20:18:14 +00:00
|
|
|
import static org.thoughtcrime.securesms.webrtc.CallNotificationBuilder.TYPE_INCOMING_CONNECTING;
|
2017-02-22 23:49:05 +00:00
|
|
|
import static org.thoughtcrime.securesms.webrtc.CallNotificationBuilder.TYPE_INCOMING_RINGING;
|
|
|
|
import static org.thoughtcrime.securesms.webrtc.CallNotificationBuilder.TYPE_OUTGOING_RINGING;
|
2016-11-09 17:37:40 +00:00
|
|
|
|
2018-04-25 18:00:03 +00:00
|
|
|
public class WebRtcCallService extends Service implements InjectableType,
|
|
|
|
PeerConnection.Observer,
|
|
|
|
DataChannel.Observer,
|
|
|
|
BluetoothStateManager.BluetoothStateListener,
|
|
|
|
PeerConnectionWrapper.CameraEventListener
|
|
|
|
{
|
2016-11-09 17:37:40 +00:00
|
|
|
|
|
|
|
private static final String TAG = WebRtcCallService.class.getSimpleName();
|
|
|
|
|
|
|
|
private enum CallState {
|
|
|
|
STATE_IDLE, STATE_DIALING, STATE_ANSWERING, STATE_REMOTE_RINGING, STATE_LOCAL_RINGING, STATE_CONNECTED
|
|
|
|
}
|
|
|
|
|
|
|
|
private static final String DATA_CHANNEL_NAME = "signaling";
|
|
|
|
|
2017-07-26 16:59:15 +00:00
|
|
|
public static final String EXTRA_REMOTE_ADDRESS = "remote_address";
|
2016-11-09 17:37:40 +00:00
|
|
|
public static final String EXTRA_MUTE = "mute_value";
|
2017-03-04 23:48:10 +00:00
|
|
|
public static final String EXTRA_AVAILABLE = "enabled_value";
|
2016-11-09 17:37:40 +00:00
|
|
|
public static final String EXTRA_REMOTE_DESCRIPTION = "remote_description";
|
|
|
|
public static final String EXTRA_TIMESTAMP = "timestamp";
|
|
|
|
public static final String EXTRA_CALL_ID = "call_id";
|
|
|
|
public static final String EXTRA_ICE_SDP = "ice_sdp";
|
|
|
|
public static final String EXTRA_ICE_SDP_MID = "ice_sdp_mid";
|
|
|
|
public static final String EXTRA_ICE_SDP_LINE_INDEX = "ice_sdp_line_index";
|
2017-02-05 20:38:08 +00:00
|
|
|
public static final String EXTRA_RESULT_RECEIVER = "result_receiver";
|
2016-11-09 17:37:40 +00:00
|
|
|
|
2017-03-04 23:48:10 +00:00
|
|
|
public static final String ACTION_INCOMING_CALL = "CALL_INCOMING";
|
|
|
|
public static final String ACTION_OUTGOING_CALL = "CALL_OUTGOING";
|
|
|
|
public static final String ACTION_ANSWER_CALL = "ANSWER_CALL";
|
|
|
|
public static final String ACTION_DENY_CALL = "DENY_CALL";
|
|
|
|
public static final String ACTION_LOCAL_HANGUP = "LOCAL_HANGUP";
|
|
|
|
public static final String ACTION_SET_MUTE_AUDIO = "SET_MUTE_AUDIO";
|
|
|
|
public static final String ACTION_SET_MUTE_VIDEO = "SET_MUTE_VIDEO";
|
2018-04-25 18:00:03 +00:00
|
|
|
public static final String ACTION_FLIP_CAMERA = "FLIP_CAMERA";
|
2017-03-04 23:48:10 +00:00
|
|
|
public static final String ACTION_BLUETOOTH_CHANGE = "BLUETOOTH_CHANGE";
|
|
|
|
public static final String ACTION_WIRED_HEADSET_CHANGE = "WIRED_HEADSET_CHANGE";
|
2017-03-05 18:02:23 +00:00
|
|
|
public static final String ACTION_SCREEN_OFF = "SCREEN_OFF";
|
2017-03-04 23:48:10 +00:00
|
|
|
public static final String ACTION_CHECK_TIMEOUT = "CHECK_TIMEOUT";
|
|
|
|
public static final String ACTION_IS_IN_CALL_QUERY = "IS_IN_CALL";
|
2016-11-09 17:37:40 +00:00
|
|
|
|
|
|
|
public static final String ACTION_RESPONSE_MESSAGE = "RESPONSE_MESSAGE";
|
|
|
|
public static final String ACTION_ICE_MESSAGE = "ICE_MESSAGE";
|
|
|
|
public static final String ACTION_ICE_CANDIDATE = "ICE_CANDIDATE";
|
|
|
|
public static final String ACTION_CALL_CONNECTED = "CALL_CONNECTED";
|
|
|
|
public static final String ACTION_REMOTE_HANGUP = "REMOTE_HANGUP";
|
|
|
|
public static final String ACTION_REMOTE_BUSY = "REMOTE_BUSY";
|
|
|
|
public static final String ACTION_REMOTE_VIDEO_MUTE = "REMOTE_VIDEO_MUTE";
|
|
|
|
public static final String ACTION_ICE_CONNECTED = "ICE_CONNECTED";
|
|
|
|
|
2018-04-25 18:00:03 +00:00
|
|
|
private CallState callState = CallState.STATE_IDLE;
|
|
|
|
private CameraState localCameraState = CameraState.UNKNOWN;
|
|
|
|
private boolean microphoneEnabled = true;
|
|
|
|
private boolean remoteVideoEnabled = false;
|
|
|
|
private boolean bluetoothAvailable = false;
|
2016-11-09 17:37:40 +00:00
|
|
|
|
2017-09-16 05:38:53 +00:00
|
|
|
@Inject public SignalServiceMessageSender messageSender;
|
2016-11-09 17:37:40 +00:00
|
|
|
@Inject public SignalServiceAccountManager accountManager;
|
|
|
|
|
|
|
|
private PeerConnectionFactory peerConnectionFactory;
|
2017-03-04 23:48:10 +00:00
|
|
|
private SignalAudioManager audioManager;
|
|
|
|
private BluetoothStateManager bluetoothStateManager;
|
|
|
|
private WiredHeadsetStateReceiver wiredHeadsetStateReceiver;
|
2017-03-05 18:02:23 +00:00
|
|
|
private PowerButtonReceiver powerButtonReceiver;
|
2016-11-09 17:37:40 +00:00
|
|
|
private LockManager lockManager;
|
|
|
|
|
2017-02-05 20:38:08 +00:00
|
|
|
private IncomingPstnCallReceiver callReceiver;
|
|
|
|
private UncaughtExceptionHandlerManager uncaughtExceptionHandlerManager;
|
|
|
|
|
2016-11-09 17:37:40 +00:00
|
|
|
@Nullable private Long callId;
|
|
|
|
@Nullable private Recipient recipient;
|
|
|
|
@Nullable private PeerConnectionWrapper peerConnection;
|
|
|
|
@Nullable private DataChannel dataChannel;
|
2017-07-12 18:15:28 +00:00
|
|
|
@Nullable private List<IceUpdateMessage> pendingOutgoingIceUpdates;
|
|
|
|
@Nullable private List<IceCandidate> pendingIncomingIceUpdates;
|
2016-11-09 17:37:40 +00:00
|
|
|
|
2019-01-29 22:25:49 +00:00
|
|
|
@Nullable private SurfaceViewRenderer localRenderer;
|
|
|
|
@Nullable private SurfaceViewRenderer remoteRenderer;
|
|
|
|
@Nullable private EglBase eglBase;
|
2016-11-09 17:37:40 +00:00
|
|
|
|
|
|
|
private ExecutorService serviceExecutor = Executors.newSingleThreadExecutor();
|
|
|
|
private ExecutorService networkExecutor = Executors.newSingleThreadExecutor();
|
|
|
|
private ScheduledExecutorService timeoutExecutor = Executors.newScheduledThreadPool(1);
|
|
|
|
|
2019-05-23 19:56:05 +00:00
|
|
|
private final PhoneStateListener hangUpRtcOnDeviceCallAnswered = new HangUpRtcOnPstnCallAnsweredListener();
|
|
|
|
|
2016-11-09 17:37:40 +00:00
|
|
|
@Override
|
|
|
|
public void onCreate() {
|
|
|
|
super.onCreate();
|
2019-05-23 19:56:05 +00:00
|
|
|
Log.d(TAG, "onCreate");
|
2016-11-09 17:37:40 +00:00
|
|
|
|
|
|
|
initializeResources();
|
2017-02-05 20:38:08 +00:00
|
|
|
|
|
|
|
registerIncomingPstnCallReceiver();
|
2016-11-09 17:37:40 +00:00
|
|
|
registerUncaughtExceptionHandler();
|
2017-03-04 23:48:10 +00:00
|
|
|
registerWiredHeadsetStateReceiver();
|
2019-05-23 19:56:05 +00:00
|
|
|
|
|
|
|
TelephonyUtil.getManager(this)
|
|
|
|
.listen(hangUpRtcOnDeviceCallAnswered, PhoneStateListener.LISTEN_CALL_STATE);
|
2016-11-09 17:37:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int onStartCommand(final Intent intent, int flags, int startId) {
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.i(TAG, "onStartCommand...");
|
2016-11-09 17:37:40 +00:00
|
|
|
if (intent == null || intent.getAction() == null) return START_NOT_STICKY;
|
|
|
|
|
2017-12-15 17:45:00 +00:00
|
|
|
serviceExecutor.execute(() -> {
|
|
|
|
if (intent.getAction().equals(ACTION_INCOMING_CALL) && isBusy()) handleBusyCall(intent);
|
|
|
|
else if (intent.getAction().equals(ACTION_REMOTE_BUSY)) handleBusyMessage(intent);
|
|
|
|
else if (intent.getAction().equals(ACTION_INCOMING_CALL)) handleIncomingCall(intent);
|
|
|
|
else if (intent.getAction().equals(ACTION_OUTGOING_CALL) && isIdle()) handleOutgoingCall(intent);
|
|
|
|
else if (intent.getAction().equals(ACTION_ANSWER_CALL)) handleAnswerCall(intent);
|
|
|
|
else if (intent.getAction().equals(ACTION_DENY_CALL)) handleDenyCall(intent);
|
|
|
|
else if (intent.getAction().equals(ACTION_LOCAL_HANGUP)) handleLocalHangup(intent);
|
|
|
|
else if (intent.getAction().equals(ACTION_REMOTE_HANGUP)) handleRemoteHangup(intent);
|
|
|
|
else if (intent.getAction().equals(ACTION_SET_MUTE_AUDIO)) handleSetMuteAudio(intent);
|
|
|
|
else if (intent.getAction().equals(ACTION_SET_MUTE_VIDEO)) handleSetMuteVideo(intent);
|
2018-04-25 18:00:03 +00:00
|
|
|
else if (intent.getAction().equals(ACTION_FLIP_CAMERA)) handleSetCameraFlip(intent);
|
2017-12-15 17:45:00 +00:00
|
|
|
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);
|
|
|
|
else if (intent.getAction().equals(ACTION_ICE_CANDIDATE)) handleLocalIceCandidate(intent);
|
|
|
|
else if (intent.getAction().equals(ACTION_ICE_CONNECTED)) handleIceConnected(intent);
|
|
|
|
else if (intent.getAction().equals(ACTION_CALL_CONNECTED)) handleCallConnected(intent);
|
|
|
|
else if (intent.getAction().equals(ACTION_CHECK_TIMEOUT)) handleCheckTimeout(intent);
|
|
|
|
else if (intent.getAction().equals(ACTION_IS_IN_CALL_QUERY)) handleIsInCallQuery(intent);
|
2016-11-09 17:37:40 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return START_NOT_STICKY;
|
|
|
|
}
|
|
|
|
|
2017-02-05 20:38:08 +00:00
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
super.onDestroy();
|
2019-05-23 19:56:05 +00:00
|
|
|
Log.d(TAG, "onDestroy");
|
2017-02-05 20:38:08 +00:00
|
|
|
|
|
|
|
if (callReceiver != null) {
|
|
|
|
unregisterReceiver(callReceiver);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (uncaughtExceptionHandlerManager != null) {
|
|
|
|
uncaughtExceptionHandlerManager.unregister();
|
|
|
|
}
|
2017-03-04 23:48:10 +00:00
|
|
|
|
|
|
|
if (bluetoothStateManager != null) {
|
|
|
|
bluetoothStateManager.onDestroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wiredHeadsetStateReceiver != null) {
|
|
|
|
unregisterReceiver(wiredHeadsetStateReceiver);
|
2017-03-05 18:02:23 +00:00
|
|
|
wiredHeadsetStateReceiver = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (powerButtonReceiver != null) {
|
|
|
|
unregisterReceiver(powerButtonReceiver);
|
|
|
|
powerButtonReceiver = null;
|
2017-03-04 23:48:10 +00:00
|
|
|
}
|
2019-05-23 19:56:05 +00:00
|
|
|
|
|
|
|
TelephonyUtil.getManager(this)
|
|
|
|
.listen(hangUpRtcOnDeviceCallAnswered, PhoneStateListener.LISTEN_NONE);
|
2017-02-05 20:38:08 +00:00
|
|
|
}
|
|
|
|
|
2017-03-04 23:48:10 +00:00
|
|
|
@Override
|
|
|
|
public void onBluetoothStateChanged(boolean isAvailable) {
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.i(TAG, "onBluetoothStateChanged: " + isAvailable);
|
2016-11-09 17:37:40 +00:00
|
|
|
|
2017-03-04 23:48:10 +00:00
|
|
|
Intent intent = new Intent(this, WebRtcCallService.class);
|
|
|
|
intent.setAction(ACTION_BLUETOOTH_CHANGE);
|
|
|
|
intent.putExtra(EXTRA_AVAILABLE, isAvailable);
|
|
|
|
|
|
|
|
startService(intent);
|
2016-11-09 17:37:40 +00:00
|
|
|
}
|
|
|
|
|
2018-04-25 18:00:03 +00:00
|
|
|
@Override
|
|
|
|
public void onCameraSwitchCompleted(@NonNull CameraState newCameraState) {
|
|
|
|
this.localCameraState = newCameraState;
|
|
|
|
if (recipient != null) {
|
|
|
|
sendMessage(viewModelStateFor(callState), recipient, localCameraState, remoteVideoEnabled, bluetoothAvailable, microphoneEnabled);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-04 23:48:10 +00:00
|
|
|
// Initializers
|
|
|
|
|
2016-11-09 17:37:40 +00:00
|
|
|
private void initializeResources() {
|
|
|
|
ApplicationContext.getInstance(this).injectDependencies(this);
|
|
|
|
|
|
|
|
this.callState = CallState.STATE_IDLE;
|
|
|
|
this.lockManager = new LockManager(this);
|
2017-03-04 23:48:10 +00:00
|
|
|
this.audioManager = new SignalAudioManager(this);
|
|
|
|
this.bluetoothStateManager = new BluetoothStateManager(this, this);
|
2019-01-29 22:25:49 +00:00
|
|
|
|
2016-11-09 17:37:40 +00:00
|
|
|
this.messageSender.setSoTimeoutMillis(TimeUnit.SECONDS.toMillis(10));
|
|
|
|
this.accountManager.setSoTimeoutMillis(TimeUnit.SECONDS.toMillis(10));
|
|
|
|
}
|
|
|
|
|
2017-02-05 20:38:08 +00:00
|
|
|
private void registerIncomingPstnCallReceiver() {
|
|
|
|
callReceiver = new IncomingPstnCallReceiver();
|
|
|
|
registerReceiver(callReceiver, new IntentFilter("android.intent.action.PHONE_STATE"));
|
|
|
|
}
|
|
|
|
|
2016-11-09 17:37:40 +00:00
|
|
|
private void registerUncaughtExceptionHandler() {
|
2017-02-05 20:38:08 +00:00
|
|
|
uncaughtExceptionHandlerManager = new UncaughtExceptionHandlerManager();
|
2016-11-09 17:37:40 +00:00
|
|
|
uncaughtExceptionHandlerManager.registerHandler(new ProximityLockRelease(lockManager));
|
|
|
|
}
|
|
|
|
|
2017-03-04 23:48:10 +00:00
|
|
|
private void registerWiredHeadsetStateReceiver() {
|
|
|
|
wiredHeadsetStateReceiver = new WiredHeadsetStateReceiver();
|
|
|
|
|
|
|
|
String action;
|
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= 21) {
|
|
|
|
action = AudioManager.ACTION_HEADSET_PLUG;
|
|
|
|
} else {
|
|
|
|
action = Intent.ACTION_HEADSET_PLUG;
|
|
|
|
}
|
|
|
|
|
|
|
|
registerReceiver(wiredHeadsetStateReceiver, new IntentFilter(action));
|
|
|
|
}
|
|
|
|
|
2017-03-05 18:02:23 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-09 17:37:40 +00:00
|
|
|
// Handlers
|
|
|
|
|
|
|
|
private void handleIncomingCall(final Intent intent) {
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.i(TAG, "handleIncomingCall()");
|
2016-11-09 17:37:40 +00:00
|
|
|
if (callState != CallState.STATE_IDLE) throw new IllegalStateException("Incoming on non-idle");
|
|
|
|
|
|
|
|
final String offer = intent.getStringExtra(EXTRA_REMOTE_DESCRIPTION);
|
|
|
|
|
2017-07-12 18:15:28 +00:00
|
|
|
this.callState = CallState.STATE_ANSWERING;
|
|
|
|
this.callId = intent.getLongExtra(EXTRA_CALL_ID, -1);
|
|
|
|
this.pendingIncomingIceUpdates = new LinkedList<>();
|
|
|
|
this.recipient = getRemoteRecipient(intent);
|
2016-11-09 17:37:40 +00:00
|
|
|
|
|
|
|
if (isIncomingMessageExpired(intent)) {
|
|
|
|
insertMissedCall(this.recipient, true);
|
|
|
|
terminate();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-27 20:18:14 +00:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
|
setCallInProgressNotification(TYPE_INCOMING_CONNECTING, this.recipient);
|
|
|
|
}
|
|
|
|
|
2016-11-09 17:37:40 +00:00
|
|
|
timeoutExecutor.schedule(new TimeoutRunnable(this.callId), 2, TimeUnit.MINUTES);
|
|
|
|
|
|
|
|
initializeVideo();
|
2017-03-04 23:48:10 +00:00
|
|
|
|
2016-11-09 17:37:40 +00:00
|
|
|
retrieveTurnServers().addListener(new SuccessOnlyListener<List<PeerConnection.IceServer>>(this.callState, this.callId) {
|
|
|
|
@Override
|
|
|
|
public void onSuccessContinue(List<PeerConnection.IceServer> result) {
|
|
|
|
try {
|
2017-11-25 06:00:30 +00:00
|
|
|
boolean isSystemContact = false;
|
|
|
|
|
2019-10-31 05:09:29 +00:00
|
|
|
/*
|
2017-11-25 06:00:30 +00:00
|
|
|
if (Permissions.hasAny(WebRtcCallService.this, Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS)) {
|
|
|
|
isSystemContact = ContactAccessor.getInstance().isSystemContact(WebRtcCallService.this, recipient.getAddress().serialize());
|
|
|
|
}
|
2020-05-11 06:54:31 +00:00
|
|
|
*/
|
2017-11-25 06:00:30 +00:00
|
|
|
|
|
|
|
boolean isAlwaysTurn = TextSecurePreferences.isTurnOnly(WebRtcCallService.this);
|
2017-02-17 21:12:48 +00:00
|
|
|
|
2019-01-29 22:25:49 +00:00
|
|
|
WebRtcCallService.this.peerConnection = new PeerConnectionWrapper(WebRtcCallService.this, peerConnectionFactory, WebRtcCallService.this, localRenderer, result, WebRtcCallService.this, eglBase, !isSystemContact || isAlwaysTurn);
|
2018-04-25 18:00:03 +00:00
|
|
|
WebRtcCallService.this.localCameraState = WebRtcCallService.this.peerConnection.getCameraState();
|
2016-11-09 17:37:40 +00:00
|
|
|
WebRtcCallService.this.peerConnection.setRemoteDescription(new SessionDescription(SessionDescription.Type.OFFER, offer));
|
|
|
|
WebRtcCallService.this.lockManager.updatePhoneState(LockManager.PhoneState.PROCESSING);
|
|
|
|
|
|
|
|
SessionDescription sdp = WebRtcCallService.this.peerConnection.createAnswer(new MediaConstraints());
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.i(TAG, "Answer SDP: " + sdp.description);
|
2016-11-09 17:37:40 +00:00
|
|
|
WebRtcCallService.this.peerConnection.setLocalDescription(sdp);
|
|
|
|
|
|
|
|
ListenableFutureTask<Boolean> listenableFutureTask = sendMessage(recipient, SignalServiceCallMessage.forAnswer(new AnswerMessage(WebRtcCallService.this.callId, sdp.description)));
|
|
|
|
|
2017-07-12 18:15:28 +00:00
|
|
|
for (IceCandidate candidate : pendingIncomingIceUpdates) WebRtcCallService.this.peerConnection.addIceCandidate(candidate);
|
|
|
|
WebRtcCallService.this.pendingIncomingIceUpdates = null;
|
|
|
|
|
2016-11-09 17:37:40 +00:00
|
|
|
listenableFutureTask.addListener(new FailureListener<Boolean>(WebRtcCallService.this.callState, WebRtcCallService.this.callId) {
|
|
|
|
@Override
|
|
|
|
public void onFailureContinue(Throwable error) {
|
|
|
|
Log.w(TAG, error);
|
2017-06-07 01:03:09 +00:00
|
|
|
insertMissedCall(recipient, true);
|
2016-11-09 17:37:40 +00:00
|
|
|
terminate();
|
|
|
|
}
|
|
|
|
});
|
2018-04-25 18:00:03 +00:00
|
|
|
|
|
|
|
if (recipient != null) {
|
|
|
|
sendMessage(viewModelStateFor(callState), recipient, localCameraState, remoteVideoEnabled, bluetoothAvailable, microphoneEnabled);
|
|
|
|
}
|
2016-11-09 17:37:40 +00:00
|
|
|
} catch (PeerConnectionException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
terminate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleOutgoingCall(Intent intent) {
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.i(TAG, "handleOutgoingCall...");
|
2016-11-09 17:37:40 +00:00
|
|
|
|
|
|
|
if (callState != CallState.STATE_IDLE) throw new IllegalStateException("Dialing from non-idle?");
|
|
|
|
|
2019-03-15 22:31:52 +00:00
|
|
|
this.callState = CallState.STATE_DIALING;
|
|
|
|
this.recipient = getRemoteRecipient(intent);
|
|
|
|
this.callId = new SecureRandom().nextLong();
|
|
|
|
this.pendingOutgoingIceUpdates = new LinkedList<>();
|
2016-11-09 17:37:40 +00:00
|
|
|
|
2019-03-15 22:31:52 +00:00
|
|
|
initializeVideo();
|
2016-11-09 17:37:40 +00:00
|
|
|
|
2019-03-15 22:31:52 +00:00
|
|
|
sendMessage(WebRtcViewModel.State.CALL_OUTGOING, recipient, localCameraState, remoteVideoEnabled, bluetoothAvailable, microphoneEnabled);
|
|
|
|
lockManager.updatePhoneState(LockManager.PhoneState.IN_CALL);
|
|
|
|
audioManager.initializeAudioForCall();
|
|
|
|
audioManager.startOutgoingRinger(OutgoingRinger.Type.RINGING);
|
|
|
|
bluetoothStateManager.setWantsConnection(true);
|
2016-11-09 17:37:40 +00:00
|
|
|
|
2019-03-15 22:31:52 +00:00
|
|
|
setCallInProgressNotification(TYPE_OUTGOING_RINGING, recipient);
|
|
|
|
DatabaseFactory.getSmsDatabase(this).insertOutgoingCall(recipient.getAddress());
|
2016-11-09 17:37:40 +00:00
|
|
|
|
2019-03-15 22:31:52 +00:00
|
|
|
timeoutExecutor.schedule(new TimeoutRunnable(this.callId), 2, TimeUnit.MINUTES);
|
2017-02-17 21:12:48 +00:00
|
|
|
|
2019-03-15 22:31:52 +00:00
|
|
|
retrieveTurnServers().addListener(new SuccessOnlyListener<List<PeerConnection.IceServer>>(this.callState, this.callId) {
|
|
|
|
@Override
|
|
|
|
public void onSuccessContinue(List<PeerConnection.IceServer> result) {
|
|
|
|
try {
|
|
|
|
boolean isAlwaysTurn = TextSecurePreferences.isTurnOnly(WebRtcCallService.this);
|
2016-11-09 17:37:40 +00:00
|
|
|
|
2019-03-15 22:31:52 +00:00
|
|
|
WebRtcCallService.this.peerConnection = new PeerConnectionWrapper(WebRtcCallService.this, peerConnectionFactory, WebRtcCallService.this, localRenderer, result, WebRtcCallService.this, eglBase, isAlwaysTurn);
|
|
|
|
WebRtcCallService.this.localCameraState = WebRtcCallService.this.peerConnection.getCameraState();
|
|
|
|
WebRtcCallService.this.dataChannel = WebRtcCallService.this.peerConnection.createDataChannel(DATA_CHANNEL_NAME);
|
|
|
|
WebRtcCallService.this.dataChannel.registerObserver(WebRtcCallService.this);
|
2016-11-09 17:37:40 +00:00
|
|
|
|
2019-03-15 22:31:52 +00:00
|
|
|
SessionDescription sdp = WebRtcCallService.this.peerConnection.createOffer(new MediaConstraints());
|
|
|
|
WebRtcCallService.this.peerConnection.setLocalDescription(sdp);
|
2016-11-09 17:37:40 +00:00
|
|
|
|
2019-03-15 22:31:52 +00:00
|
|
|
Log.i(TAG, "Sending offer: " + sdp.description);
|
2016-11-09 17:37:40 +00:00
|
|
|
|
2019-03-15 22:31:52 +00:00
|
|
|
ListenableFutureTask<Boolean> listenableFutureTask = sendMessage(recipient, SignalServiceCallMessage.forOffer(new OfferMessage(WebRtcCallService.this.callId, sdp.description)));
|
2017-02-03 02:34:51 +00:00
|
|
|
|
2019-03-15 22:31:52 +00:00
|
|
|
listenableFutureTask.addListener(new FailureListener<Boolean>(callState, callId) {
|
|
|
|
@Override
|
|
|
|
public void onFailureContinue(Throwable error) {
|
|
|
|
Log.w(TAG, error);
|
2016-11-09 17:37:40 +00:00
|
|
|
|
2019-03-15 22:31:52 +00:00
|
|
|
if (error instanceof UntrustedIdentityException) {
|
|
|
|
sendMessage(WebRtcViewModel.State.UNTRUSTED_IDENTITY, recipient, ((UntrustedIdentityException)error).getIdentityKey(), localCameraState, remoteVideoEnabled, bluetoothAvailable, microphoneEnabled);
|
|
|
|
} else if (error instanceof UnregisteredUserException) {
|
|
|
|
sendMessage(WebRtcViewModel.State.NO_SUCH_USER, recipient, localCameraState, remoteVideoEnabled, bluetoothAvailable, microphoneEnabled);
|
|
|
|
} else if (error instanceof IOException) {
|
|
|
|
sendMessage(WebRtcViewModel.State.NETWORK_FAILURE, recipient, localCameraState, remoteVideoEnabled, bluetoothAvailable, microphoneEnabled);
|
2016-11-09 17:37:40 +00:00
|
|
|
}
|
2018-04-25 18:00:03 +00:00
|
|
|
|
2019-03-15 22:31:52 +00:00
|
|
|
terminate();
|
2018-04-25 18:00:03 +00:00
|
|
|
}
|
2019-03-15 22:31:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (recipient != null) {
|
|
|
|
sendMessage(viewModelStateFor(callState), recipient, localCameraState, remoteVideoEnabled, bluetoothAvailable, microphoneEnabled);
|
2016-11-09 17:37:40 +00:00
|
|
|
}
|
2019-03-15 22:31:52 +00:00
|
|
|
} catch (PeerConnectionException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
terminate();
|
2016-11-09 17:37:40 +00:00
|
|
|
}
|
2019-03-15 22:31:52 +00:00
|
|
|
}
|
|
|
|
});
|
2016-11-09 17:37:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void handleResponseMessage(Intent intent) {
|
|
|
|
try {
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.i(TAG, "Got response: " + intent.getStringExtra(EXTRA_REMOTE_DESCRIPTION));
|
2016-11-09 17:37:40 +00:00
|
|
|
|
|
|
|
if (callState != CallState.STATE_DIALING || !getRemoteRecipient(intent).equals(recipient) || !Util.isEquals(this.callId, getCallId(intent))) {
|
|
|
|
Log.w(TAG, "Got answer for recipient and call id we're not currently dialing: " + getCallId(intent) + ", " + getRemoteRecipient(intent));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-12 18:15:28 +00:00
|
|
|
if (peerConnection == null || pendingOutgoingIceUpdates == null) {
|
2016-11-09 17:37:40 +00:00
|
|
|
throw new AssertionError("assert");
|
|
|
|
}
|
|
|
|
|
2017-07-12 18:15:28 +00:00
|
|
|
if (!pendingOutgoingIceUpdates.isEmpty()) {
|
|
|
|
ListenableFutureTask<Boolean> listenableFutureTask = sendMessage(recipient, SignalServiceCallMessage.forIceUpdates(pendingOutgoingIceUpdates));
|
2017-07-12 17:59:10 +00:00
|
|
|
|
|
|
|
listenableFutureTask.addListener(new FailureListener<Boolean>(callState, callId) {
|
|
|
|
@Override
|
|
|
|
public void onFailureContinue(Throwable error) {
|
|
|
|
Log.w(TAG, error);
|
2018-04-25 18:00:03 +00:00
|
|
|
sendMessage(WebRtcViewModel.State.NETWORK_FAILURE, recipient, localCameraState, remoteVideoEnabled, bluetoothAvailable, microphoneEnabled);
|
2017-07-12 17:59:10 +00:00
|
|
|
|
|
|
|
terminate();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-11-09 17:37:40 +00:00
|
|
|
this.peerConnection.setRemoteDescription(new SessionDescription(SessionDescription.Type.ANSWER, intent.getStringExtra(EXTRA_REMOTE_DESCRIPTION)));
|
2017-07-12 18:15:28 +00:00
|
|
|
this.pendingOutgoingIceUpdates = null;
|
2016-11-09 17:37:40 +00:00
|
|
|
} catch (PeerConnectionException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
terminate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleRemoteIceCandidate(Intent intent) {
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.i(TAG, "handleRemoteIceCandidate...");
|
2016-11-09 17:37:40 +00:00
|
|
|
|
2017-07-12 18:15:28 +00:00
|
|
|
if (Util.isEquals(this.callId, getCallId(intent))) {
|
|
|
|
IceCandidate candidate = new IceCandidate(intent.getStringExtra(EXTRA_ICE_SDP_MID),
|
|
|
|
intent.getIntExtra(EXTRA_ICE_SDP_LINE_INDEX, 0),
|
|
|
|
intent.getStringExtra(EXTRA_ICE_SDP));
|
|
|
|
|
|
|
|
if (peerConnection != null) peerConnection.addIceCandidate(candidate);
|
|
|
|
else if (pendingIncomingIceUpdates != null) pendingIncomingIceUpdates.add(candidate);
|
2016-11-09 17:37:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleLocalIceCandidate(Intent intent) {
|
|
|
|
if (callState == CallState.STATE_IDLE || !Util.isEquals(this.callId, getCallId(intent))) {
|
|
|
|
Log.w(TAG, "State is now idle, ignoring ice candidate...");
|
2017-06-13 16:21:42 +00:00
|
|
|
return;
|
2016-11-09 17:37:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (recipient == null || callId == null) {
|
2017-03-01 01:06:05 +00:00
|
|
|
throw new AssertionError("assert: " + callState + ", " + callId);
|
2016-11-09 17:37:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
IceUpdateMessage iceUpdateMessage = new IceUpdateMessage(this.callId, intent.getStringExtra(EXTRA_ICE_SDP_MID),
|
|
|
|
intent.getIntExtra(EXTRA_ICE_SDP_LINE_INDEX, 0),
|
|
|
|
intent.getStringExtra(EXTRA_ICE_SDP));
|
|
|
|
|
2017-07-12 18:15:28 +00:00
|
|
|
if (pendingOutgoingIceUpdates != null) {
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.i(TAG, "Adding to pending ice candidates...");
|
2017-07-12 18:15:28 +00:00
|
|
|
this.pendingOutgoingIceUpdates.add(iceUpdateMessage);
|
2017-07-12 17:59:10 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-09 17:37:40 +00:00
|
|
|
ListenableFutureTask<Boolean> listenableFutureTask = sendMessage(recipient, SignalServiceCallMessage.forIceUpdate(iceUpdateMessage));
|
|
|
|
|
|
|
|
listenableFutureTask.addListener(new FailureListener<Boolean>(callState, callId) {
|
|
|
|
@Override
|
|
|
|
public void onFailureContinue(Throwable error) {
|
|
|
|
Log.w(TAG, error);
|
2018-04-25 18:00:03 +00:00
|
|
|
sendMessage(WebRtcViewModel.State.NETWORK_FAILURE, recipient, localCameraState, remoteVideoEnabled, bluetoothAvailable, microphoneEnabled);
|
2016-11-09 17:37:40 +00:00
|
|
|
|
|
|
|
terminate();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleIceConnected(Intent intent) {
|
|
|
|
if (callState == CallState.STATE_ANSWERING) {
|
|
|
|
if (this.recipient == null) throw new AssertionError("assert");
|
|
|
|
|
|
|
|
this.callState = CallState.STATE_LOCAL_RINGING;
|
|
|
|
this.lockManager.updatePhoneState(LockManager.PhoneState.INTERACTIVE);
|
|
|
|
|
2018-04-25 18:00:03 +00:00
|
|
|
sendMessage(WebRtcViewModel.State.CALL_INCOMING, recipient, localCameraState, remoteVideoEnabled, bluetoothAvailable, microphoneEnabled);
|
2016-11-09 17:37:40 +00:00
|
|
|
startCallCardActivity();
|
2017-03-04 23:48:10 +00:00
|
|
|
audioManager.initializeAudioForCall();
|
2018-02-16 19:10:35 +00:00
|
|
|
|
|
|
|
if (TextSecurePreferences.isCallNotificationsEnabled(this)) {
|
|
|
|
Uri ringtone = recipient.resolve().getCallRingtone();
|
|
|
|
VibrateState vibrateState = recipient.resolve().getCallVibrate();
|
|
|
|
|
|
|
|
if (ringtone == null) ringtone = TextSecurePreferences.getCallNotificationRingtone(this);
|
|
|
|
|
|
|
|
audioManager.startIncomingRinger(ringtone, vibrateState == VibrateState.ENABLED || (vibrateState == VibrateState.DEFAULT && TextSecurePreferences.isCallNotificationVibrateEnabled(this)));
|
|
|
|
}
|
2017-03-04 23:48:10 +00:00
|
|
|
|
2017-03-05 18:02:23 +00:00
|
|
|
registerPowerButtonReceiver();
|
|
|
|
|
2017-02-22 23:49:05 +00:00
|
|
|
setCallInProgressNotification(TYPE_INCOMING_RINGING, recipient);
|
2016-11-09 17:37:40 +00:00
|
|
|
} else if (callState == CallState.STATE_DIALING) {
|
|
|
|
if (this.recipient == null) throw new AssertionError("assert");
|
|
|
|
|
|
|
|
this.callState = CallState.STATE_REMOTE_RINGING;
|
|
|
|
|
2018-04-25 18:00:03 +00:00
|
|
|
sendMessage(WebRtcViewModel.State.CALL_RINGING, recipient, localCameraState, remoteVideoEnabled, bluetoothAvailable, microphoneEnabled);
|
2016-11-09 17:37:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleCallConnected(Intent intent) {
|
|
|
|
if (callState != CallState.STATE_REMOTE_RINGING && callState != CallState.STATE_LOCAL_RINGING) {
|
|
|
|
Log.w(TAG, "Ignoring call connected for unknown state: " + callState);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Util.isEquals(this.callId, getCallId(intent))) {
|
|
|
|
Log.w(TAG, "Ignoring connected for unknown call id: " + getCallId(intent));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (recipient == null || peerConnection == null || dataChannel == null) {
|
|
|
|
throw new AssertionError("assert");
|
|
|
|
}
|
|
|
|
|
2017-03-04 23:48:10 +00:00
|
|
|
audioManager.startCommunication(callState == CallState.STATE_REMOTE_RINGING);
|
|
|
|
bluetoothStateManager.setWantsConnection(true);
|
2016-11-09 17:37:40 +00:00
|
|
|
|
2017-03-04 23:48:10 +00:00
|
|
|
callState = CallState.STATE_CONNECTED;
|
2017-02-19 00:59:45 +00:00
|
|
|
|
2018-04-25 18:00:03 +00:00
|
|
|
if (localCameraState.isEnabled()) lockManager.updatePhoneState(LockManager.PhoneState.IN_VIDEO);
|
|
|
|
else lockManager.updatePhoneState(LockManager.PhoneState.IN_CALL);
|
2016-11-09 17:37:40 +00:00
|
|
|
|
2018-04-25 18:00:03 +00:00
|
|
|
sendMessage(WebRtcViewModel.State.CALL_CONNECTED, recipient, localCameraState, remoteVideoEnabled, bluetoothAvailable, microphoneEnabled);
|
2016-11-09 17:37:40 +00:00
|
|
|
|
2017-03-05 18:02:23 +00:00
|
|
|
unregisterPowerButtonReceiver();
|
|
|
|
|
2017-02-22 23:49:05 +00:00
|
|
|
setCallInProgressNotification(TYPE_ESTABLISHED, recipient);
|
2016-11-09 17:37:40 +00:00
|
|
|
|
2017-12-22 00:37:07 +00:00
|
|
|
this.peerConnection.setCommunicationMode();
|
2017-03-04 23:48:10 +00:00
|
|
|
this.peerConnection.setAudioEnabled(microphoneEnabled);
|
2018-04-25 18:00:03 +00:00
|
|
|
this.peerConnection.setVideoEnabled(localCameraState.isEnabled());
|
2016-11-09 17:37:40 +00:00
|
|
|
|
|
|
|
this.dataChannel.send(new DataChannel.Buffer(ByteBuffer.wrap(Data.newBuilder()
|
|
|
|
.setVideoStreamingStatus(WebRtcDataProtos.VideoStreamingStatus.newBuilder()
|
|
|
|
.setId(this.callId)
|
2018-04-25 18:00:03 +00:00
|
|
|
.setEnabled(localCameraState.isEnabled()))
|
2016-11-09 17:37:40 +00:00
|
|
|
.build().toByteArray()), false));
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleBusyCall(Intent intent) {
|
|
|
|
Recipient recipient = getRemoteRecipient(intent);
|
|
|
|
long callId = getCallId(intent);
|
|
|
|
|
2017-12-15 17:45:00 +00:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
|
switch (callState) {
|
|
|
|
case STATE_DIALING:
|
|
|
|
case STATE_REMOTE_RINGING: setCallInProgressNotification(TYPE_OUTGOING_RINGING, this.recipient); break;
|
2018-06-21 22:33:54 +00:00
|
|
|
case STATE_IDLE: setCallInProgressNotification(TYPE_INCOMING_CONNECTING, recipient); break;
|
2017-12-15 17:45:00 +00:00
|
|
|
case STATE_ANSWERING: setCallInProgressNotification(TYPE_INCOMING_CONNECTING, this.recipient); break;
|
|
|
|
case STATE_LOCAL_RINGING: setCallInProgressNotification(TYPE_INCOMING_RINGING, this.recipient); break;
|
|
|
|
case STATE_CONNECTED: setCallInProgressNotification(TYPE_ESTABLISHED, this.recipient); break;
|
|
|
|
default: throw new AssertionError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-19 18:57:34 +00:00
|
|
|
if (callState == CallState.STATE_IDLE) {
|
|
|
|
stopForeground(true);
|
|
|
|
}
|
|
|
|
|
2016-11-09 17:37:40 +00:00
|
|
|
sendMessage(recipient, SignalServiceCallMessage.forBusy(new BusyMessage(callId)));
|
|
|
|
insertMissedCall(getRemoteRecipient(intent), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleBusyMessage(Intent intent) {
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.i(TAG, "handleBusyMessage...");
|
2016-11-09 17:37:40 +00:00
|
|
|
|
2017-06-01 20:11:48 +00:00
|
|
|
final Recipient recipient = getRemoteRecipient(intent);
|
|
|
|
final long callId = getCallId(intent);
|
2016-11-09 17:37:40 +00:00
|
|
|
|
|
|
|
if (callState != CallState.STATE_DIALING || !Util.isEquals(this.callId, callId) || !recipient.equals(this.recipient)) {
|
|
|
|
Log.w(TAG, "Got busy message for inactive session...");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-25 18:00:03 +00:00
|
|
|
sendMessage(WebRtcViewModel.State.CALL_BUSY, recipient, localCameraState, remoteVideoEnabled, bluetoothAvailable, microphoneEnabled);
|
2016-11-09 17:37:40 +00:00
|
|
|
|
2017-03-04 23:48:10 +00:00
|
|
|
audioManager.startOutgoingRinger(OutgoingRinger.Type.BUSY);
|
2017-09-10 06:46:48 +00:00
|
|
|
Util.runOnMainDelayed(new Runnable() {
|
2016-11-09 17:37:40 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2017-06-01 20:11:48 +00:00
|
|
|
Intent intent = new Intent(WebRtcCallService.this, WebRtcCallService.class);
|
|
|
|
intent.setAction(ACTION_LOCAL_HANGUP);
|
|
|
|
intent.putExtra(EXTRA_CALL_ID, intent.getLongExtra(EXTRA_CALL_ID, -1));
|
2017-07-26 16:59:15 +00:00
|
|
|
intent.putExtra(EXTRA_REMOTE_ADDRESS, intent.getStringExtra(EXTRA_REMOTE_ADDRESS));
|
2017-06-01 20:11:48 +00:00
|
|
|
|
|
|
|
startService(intent);
|
2016-11-09 17:37:40 +00:00
|
|
|
}
|
|
|
|
}, WebRtcCallActivity.BUSY_SIGNAL_DELAY_FINISH);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleCheckTimeout(Intent intent) {
|
|
|
|
if (this.callId != null &&
|
|
|
|
this.callId == intent.getLongExtra(EXTRA_CALL_ID, -1) &&
|
|
|
|
this.callState != CallState.STATE_CONNECTED)
|
|
|
|
{
|
|
|
|
Log.w(TAG, "Timing out call: " + this.callId);
|
2018-04-25 18:00:03 +00:00
|
|
|
sendMessage(WebRtcViewModel.State.CALL_DISCONNECTED, this.recipient, localCameraState, remoteVideoEnabled, bluetoothAvailable, microphoneEnabled);
|
2017-03-08 19:42:00 +00:00
|
|
|
|
|
|
|
if (this.callState == CallState.STATE_ANSWERING || this.callState == CallState.STATE_LOCAL_RINGING) {
|
|
|
|
insertMissedCall(this.recipient, true);
|
|
|
|
}
|
|
|
|
|
2016-11-09 17:37:40 +00:00
|
|
|
terminate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-05 20:38:08 +00:00
|
|
|
private void handleIsInCallQuery(Intent intent) {
|
|
|
|
ResultReceiver resultReceiver = intent.getParcelableExtra(EXTRA_RESULT_RECEIVER);
|
|
|
|
|
|
|
|
if (resultReceiver != null) {
|
|
|
|
resultReceiver.send(callState != CallState.STATE_IDLE ? 1 : 0, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-09 17:37:40 +00:00
|
|
|
private void insertMissedCall(@NonNull Recipient recipient, boolean signal) {
|
2017-07-26 16:59:15 +00:00
|
|
|
Pair<Long, Long> messageAndThreadId = DatabaseFactory.getSmsDatabase(this).insertMissedCall(recipient.getAddress());
|
2020-06-26 06:18:19 +00:00
|
|
|
ApplicationContext.getInstance(this).messageNotifier.updateNotification(this, messageAndThreadId.second, signal);
|
2016-11-09 17:37:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void handleAnswerCall(Intent intent) {
|
|
|
|
if (callState != CallState.STATE_LOCAL_RINGING) {
|
|
|
|
Log.w(TAG, "Can only answer from ringing!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (peerConnection == null || dataChannel == null || recipient == null || callId == null) {
|
|
|
|
throw new AssertionError("assert");
|
|
|
|
}
|
|
|
|
|
2017-07-26 16:59:15 +00:00
|
|
|
DatabaseFactory.getSmsDatabase(this).insertReceivedCall(recipient.getAddress());
|
2016-11-09 17:37:40 +00:00
|
|
|
|
|
|
|
this.peerConnection.setAudioEnabled(true);
|
|
|
|
this.peerConnection.setVideoEnabled(true);
|
|
|
|
this.dataChannel.send(new DataChannel.Buffer(ByteBuffer.wrap(Data.newBuilder().setConnected(Connected.newBuilder().setId(this.callId)).build().toByteArray()), false));
|
|
|
|
|
|
|
|
intent.putExtra(EXTRA_CALL_ID, callId);
|
2017-07-26 16:59:15 +00:00
|
|
|
intent.putExtra(EXTRA_REMOTE_ADDRESS, recipient.getAddress());
|
2016-11-09 17:37:40 +00:00
|
|
|
handleCallConnected(intent);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleDenyCall(Intent intent) {
|
|
|
|
if (callState != CallState.STATE_LOCAL_RINGING) {
|
|
|
|
Log.w(TAG, "Can only deny from ringing!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (recipient == null || callId == null || dataChannel == null) {
|
|
|
|
throw new AssertionError("assert");
|
|
|
|
}
|
|
|
|
|
|
|
|
this.dataChannel.send(new DataChannel.Buffer(ByteBuffer.wrap(Data.newBuilder().setHangup(Hangup.newBuilder().setId(this.callId)).build().toByteArray()), false));
|
|
|
|
sendMessage(this.recipient, SignalServiceCallMessage.forHangup(new HangupMessage(this.callId)));
|
|
|
|
|
2017-07-26 16:59:15 +00:00
|
|
|
DatabaseFactory.getSmsDatabase(this).insertMissedCall(recipient.getAddress());
|
2016-11-09 17:37:40 +00:00
|
|
|
|
|
|
|
this.terminate();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleLocalHangup(Intent intent) {
|
|
|
|
if (this.dataChannel != null && this.recipient != null && this.callId != null) {
|
|
|
|
this.accountManager.cancelInFlightRequests();
|
|
|
|
this.messageSender.cancelInFlightRequests();
|
|
|
|
|
|
|
|
this.dataChannel.send(new DataChannel.Buffer(ByteBuffer.wrap(Data.newBuilder().setHangup(Hangup.newBuilder().setId(this.callId)).build().toByteArray()), false));
|
|
|
|
sendMessage(this.recipient, SignalServiceCallMessage.forHangup(new HangupMessage(this.callId)));
|
2018-04-25 18:00:03 +00:00
|
|
|
sendMessage(WebRtcViewModel.State.CALL_DISCONNECTED, this.recipient, localCameraState, remoteVideoEnabled, bluetoothAvailable, microphoneEnabled);
|
2016-11-09 17:37:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
terminate();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleRemoteHangup(Intent intent) {
|
|
|
|
if (!Util.isEquals(this.callId, getCallId(intent))) {
|
|
|
|
Log.w(TAG, "hangup for non-active call...");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.recipient == null) {
|
|
|
|
throw new AssertionError("assert");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.callState == CallState.STATE_DIALING || this.callState == CallState.STATE_REMOTE_RINGING) {
|
2018-04-25 18:00:03 +00:00
|
|
|
sendMessage(WebRtcViewModel.State.RECIPIENT_UNAVAILABLE, this.recipient, localCameraState, remoteVideoEnabled, bluetoothAvailable, microphoneEnabled);
|
2016-11-09 17:37:40 +00:00
|
|
|
} else {
|
2018-04-25 18:00:03 +00:00
|
|
|
sendMessage(WebRtcViewModel.State.CALL_DISCONNECTED, this.recipient, localCameraState, remoteVideoEnabled, bluetoothAvailable, microphoneEnabled);
|
2016-11-09 17:37:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.callState == CallState.STATE_ANSWERING || this.callState == CallState.STATE_LOCAL_RINGING) {
|
|
|
|
insertMissedCall(this.recipient, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.terminate();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleSetMuteAudio(Intent intent) {
|
|
|
|
boolean muted = intent.getBooleanExtra(EXTRA_MUTE, false);
|
2017-03-04 23:48:10 +00:00
|
|
|
this.microphoneEnabled = !muted;
|
2016-11-09 17:37:40 +00:00
|
|
|
|
|
|
|
if (this.peerConnection != null) {
|
2017-03-04 23:48:10 +00:00
|
|
|
this.peerConnection.setAudioEnabled(this.microphoneEnabled);
|
2016-11-09 17:37:40 +00:00
|
|
|
}
|
2019-05-06 21:10:27 +00:00
|
|
|
|
|
|
|
if (recipient != null) {
|
|
|
|
sendMessage(viewModelStateFor(callState), recipient, localCameraState, remoteVideoEnabled, bluetoothAvailable, microphoneEnabled);
|
|
|
|
}
|
2016-11-09 17:37:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void handleSetMuteVideo(Intent intent) {
|
2017-03-04 23:48:10 +00:00
|
|
|
AudioManager audioManager = ServiceUtil.getAudioManager(this);
|
|
|
|
boolean muted = intent.getBooleanExtra(EXTRA_MUTE, false);
|
|
|
|
|
2016-11-09 17:37:40 +00:00
|
|
|
if (this.peerConnection != null) {
|
2018-04-25 18:00:03 +00:00
|
|
|
this.peerConnection.setVideoEnabled(!muted);
|
|
|
|
this.localCameraState = this.peerConnection.getCameraState();
|
2016-11-09 17:37:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.callId != null && this.dataChannel != null) {
|
|
|
|
this.dataChannel.send(new DataChannel.Buffer(ByteBuffer.wrap(Data.newBuilder()
|
|
|
|
.setVideoStreamingStatus(WebRtcDataProtos.VideoStreamingStatus.newBuilder()
|
|
|
|
.setId(this.callId)
|
2018-04-25 18:00:03 +00:00
|
|
|
.setEnabled(!muted))
|
2016-11-09 17:37:40 +00:00
|
|
|
.build().toByteArray()), false));
|
|
|
|
}
|
2017-02-03 02:53:45 +00:00
|
|
|
|
2017-02-19 00:59:45 +00:00
|
|
|
if (callState == CallState.STATE_CONNECTED) {
|
2018-04-25 18:00:03 +00:00
|
|
|
if (localCameraState.isEnabled()) this.lockManager.updatePhoneState(LockManager.PhoneState.IN_VIDEO);
|
|
|
|
else this.lockManager.updatePhoneState(LockManager.PhoneState.IN_CALL);
|
2017-02-19 00:59:45 +00:00
|
|
|
}
|
|
|
|
|
2018-04-25 18:00:03 +00:00
|
|
|
if (localCameraState.isEnabled() &&
|
2017-03-24 18:11:48 +00:00
|
|
|
!audioManager.isSpeakerphoneOn() &&
|
|
|
|
!audioManager.isBluetoothScoOn() &&
|
|
|
|
!audioManager.isWiredHeadsetOn())
|
|
|
|
{
|
2017-03-04 23:48:10 +00:00
|
|
|
audioManager.setSpeakerphoneOn(true);
|
|
|
|
}
|
|
|
|
|
2018-04-25 18:00:03 +00:00
|
|
|
sendMessage(viewModelStateFor(callState), this.recipient, localCameraState, remoteVideoEnabled, bluetoothAvailable, microphoneEnabled);
|
2017-03-04 23:48:10 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 19:31:03 +00:00
|
|
|
private void handleSetCameraFlip(Intent intent) {
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.i(TAG, "handleSetCameraFlip()...");
|
2018-03-23 19:31:03 +00:00
|
|
|
|
2018-04-25 18:00:03 +00:00
|
|
|
if (localCameraState.isEnabled() && peerConnection != null) {
|
|
|
|
peerConnection.flipCamera();
|
|
|
|
localCameraState = peerConnection.getCameraState();
|
|
|
|
if (recipient != null) {
|
|
|
|
sendMessage(viewModelStateFor(callState), recipient, localCameraState, remoteVideoEnabled, bluetoothAvailable, microphoneEnabled);
|
2018-03-23 19:31:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-04 23:48:10 +00:00
|
|
|
private void handleBluetoothChange(Intent intent) {
|
|
|
|
this.bluetoothAvailable = intent.getBooleanExtra(EXTRA_AVAILABLE, false);
|
2017-03-05 18:02:23 +00:00
|
|
|
|
2017-03-04 23:48:10 +00:00
|
|
|
if (recipient != null) {
|
2018-04-25 18:00:03 +00:00
|
|
|
sendMessage(viewModelStateFor(callState), recipient, localCameraState, remoteVideoEnabled, bluetoothAvailable, microphoneEnabled);
|
2017-03-04 23:48:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleWiredHeadsetChange(Intent intent) {
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.i(TAG, "handleWiredHeadsetChange...");
|
2017-03-04 23:48:10 +00:00
|
|
|
|
|
|
|
if (callState == CallState.STATE_CONNECTED ||
|
|
|
|
callState == CallState.STATE_DIALING ||
|
|
|
|
callState == CallState.STATE_REMOTE_RINGING)
|
|
|
|
{
|
|
|
|
AudioManager audioManager = ServiceUtil.getAudioManager(this);
|
|
|
|
boolean present = intent.getBooleanExtra(EXTRA_AVAILABLE, false);
|
|
|
|
|
|
|
|
if (present && audioManager.isSpeakerphoneOn()) {
|
|
|
|
audioManager.setSpeakerphoneOn(false);
|
|
|
|
audioManager.setBluetoothScoOn(false);
|
2018-04-25 18:00:03 +00:00
|
|
|
} else if (!present && !audioManager.isSpeakerphoneOn() && !audioManager.isBluetoothScoOn() && localCameraState.isEnabled()) {
|
2017-03-04 23:48:10 +00:00
|
|
|
audioManager.setSpeakerphoneOn(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (recipient != null) {
|
2018-04-25 18:00:03 +00:00
|
|
|
sendMessage(viewModelStateFor(callState), recipient, localCameraState, remoteVideoEnabled, bluetoothAvailable, microphoneEnabled);
|
2017-03-04 23:48:10 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-09 17:37:40 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 18:02:23 +00:00
|
|
|
private void handleScreenOffChange(Intent intent) {
|
|
|
|
if (callState == CallState.STATE_ANSWERING ||
|
|
|
|
callState == CallState.STATE_LOCAL_RINGING)
|
|
|
|
{
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.i(TAG, "Silencing incoming ringer...");
|
2017-03-05 18:02:23 +00:00
|
|
|
audioManager.silenceIncomingRinger();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-09 17:37:40 +00:00
|
|
|
private void handleRemoteVideoMute(Intent intent) {
|
|
|
|
boolean muted = intent.getBooleanExtra(EXTRA_MUTE, false);
|
|
|
|
long callId = intent.getLongExtra(EXTRA_CALL_ID, -1);
|
|
|
|
|
|
|
|
if (this.recipient == null || this.callState != CallState.STATE_CONNECTED || !Util.isEquals(this.callId, callId)) {
|
|
|
|
Log.w(TAG, "Got video toggle for inactive call, ignoring...");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-03 02:34:51 +00:00
|
|
|
this.remoteVideoEnabled = !muted;
|
2018-04-25 18:00:03 +00:00
|
|
|
sendMessage(WebRtcViewModel.State.CALL_CONNECTED, this.recipient, localCameraState, remoteVideoEnabled, bluetoothAvailable, microphoneEnabled);
|
2016-11-09 17:37:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Helper Methods
|
|
|
|
|
|
|
|
private boolean isBusy() {
|
2019-05-23 19:56:05 +00:00
|
|
|
return callState != CallState.STATE_IDLE || TelephonyUtil.isAnyPstnLineBusy(this);
|
2016-11-09 17:37:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isIdle() {
|
|
|
|
return callState == CallState.STATE_IDLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isIncomingMessageExpired(Intent intent) {
|
|
|
|
return System.currentTimeMillis() - intent.getLongExtra(WebRtcCallService.EXTRA_TIMESTAMP, -1) > TimeUnit.MINUTES.toMillis(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initializeVideo() {
|
2019-01-29 22:25:49 +00:00
|
|
|
Util.runOnMainSync(() -> {
|
|
|
|
eglBase = EglBase.create();
|
|
|
|
localRenderer = new SurfaceViewRenderer(WebRtcCallService.this);
|
|
|
|
remoteRenderer = new SurfaceViewRenderer(WebRtcCallService.this);
|
|
|
|
|
|
|
|
localRenderer.init(eglBase.getEglBaseContext(), null);
|
|
|
|
remoteRenderer.init(eglBase.getEglBaseContext(), null);
|
|
|
|
|
|
|
|
VideoEncoderFactory encoderFactory = new DefaultVideoEncoderFactory(eglBase.getEglBaseContext(), true, true);
|
|
|
|
VideoDecoderFactory decoderFactory = new DefaultVideoDecoderFactory(eglBase.getEglBaseContext());
|
|
|
|
|
|
|
|
peerConnectionFactory = PeerConnectionFactory.builder()
|
|
|
|
.setOptions(new PeerConnectionFactoryOptions())
|
|
|
|
.setVideoEncoderFactory(encoderFactory)
|
|
|
|
.setVideoDecoderFactory(decoderFactory)
|
|
|
|
.createPeerConnectionFactory();
|
2016-11-09 17:37:40 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-02-22 23:49:05 +00:00
|
|
|
private void setCallInProgressNotification(int type, Recipient recipient) {
|
|
|
|
startForeground(CallNotificationBuilder.WEBRTC_NOTIFICATION,
|
|
|
|
CallNotificationBuilder.getCallInProgressNotification(this, type, recipient));
|
|
|
|
}
|
|
|
|
|
2016-11-09 17:37:40 +00:00
|
|
|
private synchronized void terminate() {
|
|
|
|
lockManager.updatePhoneState(LockManager.PhoneState.PROCESSING);
|
2017-02-22 23:49:05 +00:00
|
|
|
stopForeground(true);
|
2016-11-09 17:37:40 +00:00
|
|
|
|
2017-03-04 23:48:10 +00:00
|
|
|
audioManager.stop(callState == CallState.STATE_DIALING || callState == CallState.STATE_REMOTE_RINGING || callState == CallState.STATE_CONNECTED);
|
|
|
|
bluetoothStateManager.setWantsConnection(false);
|
2016-11-09 17:37:40 +00:00
|
|
|
|
|
|
|
if (peerConnection != null) {
|
|
|
|
peerConnection.dispose();
|
|
|
|
peerConnection = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (eglBase != null && localRenderer != null && remoteRenderer != null) {
|
|
|
|
localRenderer.release();
|
|
|
|
remoteRenderer.release();
|
|
|
|
eglBase.release();
|
2017-02-02 06:55:19 +00:00
|
|
|
|
|
|
|
localRenderer = null;
|
|
|
|
remoteRenderer = null;
|
|
|
|
eglBase = null;
|
2016-11-09 17:37:40 +00:00
|
|
|
}
|
|
|
|
|
2017-07-12 18:15:28 +00:00
|
|
|
this.callState = CallState.STATE_IDLE;
|
2018-04-25 18:00:03 +00:00
|
|
|
this.localCameraState = CameraState.UNKNOWN;
|
2017-07-12 18:15:28 +00:00
|
|
|
this.recipient = null;
|
|
|
|
this.callId = null;
|
|
|
|
this.microphoneEnabled = true;
|
|
|
|
this.remoteVideoEnabled = false;
|
|
|
|
this.pendingOutgoingIceUpdates = null;
|
|
|
|
this.pendingIncomingIceUpdates = null;
|
2016-11-09 17:37:40 +00:00
|
|
|
lockManager.updatePhoneState(LockManager.PhoneState.IDLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-03 02:34:51 +00:00
|
|
|
private void sendMessage(@NonNull WebRtcViewModel.State state,
|
2018-04-25 18:00:03 +00:00
|
|
|
@NonNull Recipient recipient,
|
|
|
|
@NonNull CameraState localCameraState,
|
|
|
|
boolean remoteVideoEnabled,
|
|
|
|
boolean bluetoothAvailable,
|
|
|
|
boolean microphoneEnabled)
|
2016-11-09 17:37:40 +00:00
|
|
|
{
|
2019-01-29 22:25:49 +00:00
|
|
|
EventBus.getDefault().postSticky(new WebRtcViewModel(state, recipient, localCameraState, localRenderer, remoteRenderer, remoteVideoEnabled, bluetoothAvailable, microphoneEnabled));
|
2017-02-03 02:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void sendMessage(@NonNull WebRtcViewModel.State state,
|
2018-04-25 18:00:03 +00:00
|
|
|
@NonNull Recipient recipient,
|
|
|
|
@NonNull IdentityKey identityKey,
|
|
|
|
@NonNull CameraState localCameraState,
|
|
|
|
boolean remoteVideoEnabled,
|
|
|
|
boolean bluetoothAvailable,
|
|
|
|
boolean microphoneEnabled)
|
2017-02-03 02:34:51 +00:00
|
|
|
{
|
2019-01-29 22:25:49 +00:00
|
|
|
EventBus.getDefault().postSticky(new WebRtcViewModel(state, recipient, identityKey, localCameraState, localRenderer, remoteRenderer, remoteVideoEnabled, bluetoothAvailable, microphoneEnabled));
|
2016-11-09 17:37:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private ListenableFutureTask<Boolean> sendMessage(@NonNull final Recipient recipient,
|
|
|
|
@NonNull final SignalServiceCallMessage callMessage)
|
|
|
|
{
|
|
|
|
Callable<Boolean> callable = new Callable<Boolean>() {
|
|
|
|
@Override
|
|
|
|
public Boolean call() throws Exception {
|
2020-05-11 06:54:31 +00:00
|
|
|
messageSender.sendCallMessage(new SignalServiceAddress(recipient.getAddress().toPhoneString()),
|
2018-05-22 09:13:10 +00:00
|
|
|
UnidentifiedAccessUtil.getAccessFor(WebRtcCallService.this, recipient),
|
|
|
|
callMessage);
|
2017-07-26 16:59:15 +00:00
|
|
|
return true;
|
2016-11-09 17:37:40 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ListenableFutureTask<Boolean> listenableFutureTask = new ListenableFutureTask<>(callable, null, serviceExecutor);
|
|
|
|
networkExecutor.execute(listenableFutureTask);
|
|
|
|
|
|
|
|
return listenableFutureTask;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void startCallCardActivity() {
|
|
|
|
Intent activityIntent = new Intent();
|
|
|
|
activityIntent.setClass(this, WebRtcCallActivity.class);
|
|
|
|
activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
|
this.startActivity(activityIntent);
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
private @NonNull Recipient getRemoteRecipient(Intent intent) {
|
2017-07-26 16:59:15 +00:00
|
|
|
Address remoteAddress = intent.getParcelableExtra(EXTRA_REMOTE_ADDRESS);
|
|
|
|
if (remoteAddress == null) throw new AssertionError("No recipient in intent!");
|
2016-11-09 17:37:40 +00:00
|
|
|
|
2017-08-22 01:32:38 +00:00
|
|
|
return Recipient.from(this, remoteAddress, true);
|
2016-11-09 17:37:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private long getCallId(Intent intent) {
|
|
|
|
return intent.getLongExtra(EXTRA_CALL_ID, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public IBinder onBind(Intent intent) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// PeerConnection Observer
|
|
|
|
@Override
|
|
|
|
public void onSignalingChange(PeerConnection.SignalingState newState) {
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.i(TAG, "onSignalingChange: " + newState);
|
2016-11-09 17:37:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onIceConnectionChange(PeerConnection.IceConnectionState newState) {
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.i(TAG, "onIceConnectionChange:" + newState);
|
2016-11-09 17:37:40 +00:00
|
|
|
|
|
|
|
if (newState == PeerConnection.IceConnectionState.CONNECTED ||
|
|
|
|
newState == PeerConnection.IceConnectionState.COMPLETED)
|
|
|
|
{
|
|
|
|
Intent intent = new Intent(this, WebRtcCallService.class);
|
|
|
|
intent.setAction(ACTION_ICE_CONNECTED);
|
|
|
|
|
|
|
|
startService(intent);
|
|
|
|
} else if (newState == PeerConnection.IceConnectionState.FAILED) {
|
|
|
|
Intent intent = new Intent(this, WebRtcCallService.class);
|
|
|
|
intent.setAction(ACTION_REMOTE_HANGUP);
|
|
|
|
intent.putExtra(EXTRA_CALL_ID, this.callId);
|
|
|
|
|
|
|
|
startService(intent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onIceConnectionReceivingChange(boolean receiving) {
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.i(TAG, "onIceConnectionReceivingChange:" + receiving);
|
2016-11-09 17:37:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onIceGatheringChange(PeerConnection.IceGatheringState newState) {
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.i(TAG, "onIceGatheringChange:" + newState);
|
2016-11-09 17:37:40 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onIceCandidate(IceCandidate candidate) {
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.i(TAG, "onIceCandidate:" + candidate);
|
2016-11-09 17:37:40 +00:00
|
|
|
Intent intent = new Intent(this, WebRtcCallService.class);
|
|
|
|
|
|
|
|
intent.setAction(ACTION_ICE_CANDIDATE);
|
|
|
|
intent.putExtra(EXTRA_ICE_SDP_MID, candidate.sdpMid);
|
|
|
|
intent.putExtra(EXTRA_ICE_SDP_LINE_INDEX, candidate.sdpMLineIndex);
|
|
|
|
intent.putExtra(EXTRA_ICE_SDP, candidate.sdp);
|
2017-06-13 16:21:42 +00:00
|
|
|
intent.putExtra(EXTRA_CALL_ID, callId);
|
2016-11-09 17:37:40 +00:00
|
|
|
|
|
|
|
startService(intent);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onIceCandidatesRemoved(IceCandidate[] candidates) {
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.i(TAG, "onIceCandidatesRemoved:" + (candidates != null ? candidates.length : null));
|
2016-11-09 17:37:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAddStream(MediaStream stream) {
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.i(TAG, "onAddStream:" + stream);
|
2016-11-09 17:37:40 +00:00
|
|
|
|
|
|
|
for (AudioTrack audioTrack : stream.audioTracks) {
|
|
|
|
audioTrack.setEnabled(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stream.videoTracks != null && stream.videoTracks.size() == 1) {
|
2017-12-22 00:37:07 +00:00
|
|
|
VideoTrack videoTrack = stream.videoTracks.get(0);
|
2016-11-09 17:37:40 +00:00
|
|
|
videoTrack.setEnabled(true);
|
2018-07-25 00:04:55 +00:00
|
|
|
videoTrack.addSink(remoteRenderer);
|
2016-11-09 17:37:40 +00:00
|
|
|
}
|
2017-02-26 02:31:59 +00:00
|
|
|
}
|
2016-11-09 17:37:40 +00:00
|
|
|
|
2017-02-26 02:31:59 +00:00
|
|
|
@Override
|
|
|
|
public void onAddTrack(RtpReceiver rtpReceiver, MediaStream[] mediaStreams) {
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.i(TAG, "onAddTrack: " + mediaStreams);
|
2016-11-09 17:37:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRemoveStream(MediaStream stream) {
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.i(TAG, "onRemoveStream:" + stream);
|
2016-11-09 17:37:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDataChannel(DataChannel dataChannel) {
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.i(TAG, "onDataChannel:" + dataChannel.label());
|
2016-11-09 17:37:40 +00:00
|
|
|
|
|
|
|
if (dataChannel.label().equals(DATA_CHANNEL_NAME)) {
|
|
|
|
this.dataChannel = dataChannel;
|
|
|
|
this.dataChannel.registerObserver(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRenegotiationNeeded() {
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.i(TAG, "onRenegotiationNeeded");
|
2016-11-09 17:37:40 +00:00
|
|
|
// TODO renegotiate
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBufferedAmountChange(long l) {
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.i(TAG, "onBufferedAmountChange: " + l);
|
2016-11-09 17:37:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onStateChange() {
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.i(TAG, "onStateChange");
|
2016-11-09 17:37:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onMessage(DataChannel.Buffer buffer) {
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.i(TAG, "onMessage...");
|
2016-11-09 17:37:40 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
byte[] data = new byte[buffer.data.remaining()];
|
|
|
|
buffer.data.get(data);
|
|
|
|
|
|
|
|
Data dataMessage = Data.parseFrom(data);
|
|
|
|
|
|
|
|
if (dataMessage.hasConnected()) {
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.i(TAG, "hasConnected...");
|
2016-11-09 17:37:40 +00:00
|
|
|
Intent intent = new Intent(this, WebRtcCallService.class);
|
|
|
|
intent.setAction(ACTION_CALL_CONNECTED);
|
|
|
|
intent.putExtra(EXTRA_CALL_ID, dataMessage.getConnected().getId());
|
|
|
|
startService(intent);
|
|
|
|
} else if (dataMessage.hasHangup()) {
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.i(TAG, "hasHangup...");
|
2016-11-09 17:37:40 +00:00
|
|
|
Intent intent = new Intent(this, WebRtcCallService.class);
|
|
|
|
intent.setAction(ACTION_REMOTE_HANGUP);
|
|
|
|
intent.putExtra(EXTRA_CALL_ID, dataMessage.getHangup().getId());
|
|
|
|
startService(intent);
|
|
|
|
} else if (dataMessage.hasVideoStreamingStatus()) {
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.i(TAG, "hasVideoStreamingStatus...");
|
2016-11-09 17:37:40 +00:00
|
|
|
Intent intent = new Intent(this, WebRtcCallService.class);
|
|
|
|
intent.setAction(ACTION_REMOTE_VIDEO_MUTE);
|
|
|
|
intent.putExtra(EXTRA_CALL_ID, dataMessage.getVideoStreamingStatus().getId());
|
|
|
|
intent.putExtra(EXTRA_MUTE, !dataMessage.getVideoStreamingStatus().getEnabled());
|
|
|
|
startService(intent);
|
|
|
|
}
|
|
|
|
} catch (InvalidProtocolBufferException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private ListenableFutureTask<List<PeerConnection.IceServer>> retrieveTurnServers() {
|
2017-12-15 17:45:00 +00:00
|
|
|
Callable<List<PeerConnection.IceServer>> callable = () -> {
|
|
|
|
LinkedList<PeerConnection.IceServer> results = new LinkedList<>();
|
2016-11-09 17:37:40 +00:00
|
|
|
|
2017-12-15 17:45:00 +00:00
|
|
|
try {
|
|
|
|
TurnServerInfo turnServerInfo = accountManager.getTurnServerInfo();
|
2016-11-09 17:37:40 +00:00
|
|
|
|
2017-12-15 17:45:00 +00:00
|
|
|
for (String url : turnServerInfo.getUrls()) {
|
|
|
|
if (url.startsWith("turn")) {
|
|
|
|
results.add(new PeerConnection.IceServer(url, turnServerInfo.getUsername(), turnServerInfo.getPassword()));
|
|
|
|
} else {
|
|
|
|
results.add(new PeerConnection.IceServer(url));
|
2016-11-09 17:37:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-15 17:45:00 +00:00
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w(TAG, e);
|
2016-11-09 17:37:40 +00:00
|
|
|
}
|
2017-12-15 17:45:00 +00:00
|
|
|
|
|
|
|
return results;
|
2016-11-09 17:37:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ListenableFutureTask<List<PeerConnection.IceServer>> futureTask = new ListenableFutureTask<>(callable, null, serviceExecutor);
|
|
|
|
networkExecutor.execute(futureTask);
|
|
|
|
|
|
|
|
return futureTask;
|
|
|
|
}
|
|
|
|
|
2017-03-04 23:48:10 +00:00
|
|
|
////
|
|
|
|
|
2017-02-03 02:34:51 +00:00
|
|
|
private WebRtcViewModel.State viewModelStateFor(CallState state) {
|
|
|
|
switch (state) {
|
|
|
|
case STATE_CONNECTED: return WebRtcViewModel.State.CALL_CONNECTED;
|
|
|
|
case STATE_DIALING: return WebRtcViewModel.State.CALL_OUTGOING;
|
|
|
|
case STATE_REMOTE_RINGING: return WebRtcViewModel.State.CALL_RINGING;
|
|
|
|
case STATE_LOCAL_RINGING: return WebRtcViewModel.State.CALL_INCOMING;
|
|
|
|
case STATE_ANSWERING: return WebRtcViewModel.State.CALL_INCOMING;
|
|
|
|
case STATE_IDLE: return WebRtcViewModel.State.CALL_DISCONNECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return WebRtcViewModel.State.CALL_DISCONNECTED;
|
|
|
|
}
|
|
|
|
|
2017-03-04 23:48:10 +00:00
|
|
|
///
|
|
|
|
|
|
|
|
private static class WiredHeadsetStateReceiver extends BroadcastReceiver {
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
int state = intent.getIntExtra("state", -1);
|
|
|
|
|
|
|
|
Intent serviceIntent = new Intent(context, WebRtcCallService.class);
|
|
|
|
serviceIntent.setAction(WebRtcCallService.ACTION_WIRED_HEADSET_CHANGE);
|
|
|
|
serviceIntent.putExtra(WebRtcCallService.EXTRA_AVAILABLE, state != 0);
|
|
|
|
context.startService(serviceIntent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 18:02:23 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-09 17:37:40 +00:00
|
|
|
private class TimeoutRunnable implements Runnable {
|
|
|
|
|
|
|
|
private final long callId;
|
|
|
|
|
|
|
|
private TimeoutRunnable(long callId) {
|
|
|
|
this.callId = callId;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void run() {
|
|
|
|
Intent intent = new Intent(WebRtcCallService.this, WebRtcCallService.class);
|
|
|
|
intent.setAction(WebRtcCallService.ACTION_CHECK_TIMEOUT);
|
|
|
|
intent.putExtra(EXTRA_CALL_ID, callId);
|
|
|
|
startService(intent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static class ProximityLockRelease implements Thread.UncaughtExceptionHandler {
|
|
|
|
private final LockManager lockManager;
|
|
|
|
|
|
|
|
private ProximityLockRelease(LockManager lockManager) {
|
|
|
|
this.lockManager = lockManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void uncaughtException(Thread thread, Throwable throwable) {
|
|
|
|
Log.d(TAG, "Uncaught exception - releasing proximity lock", throwable);
|
|
|
|
lockManager.updatePhoneState(LockManager.PhoneState.IDLE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private abstract class StateAwareListener<V> implements FutureTaskListener<V> {
|
|
|
|
|
|
|
|
private final CallState expectedState;
|
|
|
|
private final long expectedCallId;
|
|
|
|
|
|
|
|
StateAwareListener(CallState expectedState, long expectedCallId) {
|
|
|
|
this.expectedState = expectedState;
|
|
|
|
this.expectedCallId = expectedCallId;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSuccess(V result) {
|
|
|
|
if (!isConsistentState()) {
|
|
|
|
Log.w(TAG, "State has changed since request, aborting success callback...");
|
|
|
|
} else {
|
|
|
|
onSuccessContinue(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(ExecutionException throwable) {
|
|
|
|
if (!isConsistentState()) {
|
|
|
|
Log.w(TAG, throwable);
|
|
|
|
Log.w(TAG, "State has changed since request, aborting failure callback...");
|
|
|
|
} else {
|
|
|
|
onFailureContinue(throwable.getCause());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isConsistentState() {
|
|
|
|
return this.expectedState == callState && Util.isEquals(callId, this.expectedCallId);
|
|
|
|
}
|
|
|
|
|
|
|
|
public abstract void onSuccessContinue(V result);
|
|
|
|
public abstract void onFailureContinue(Throwable throwable);
|
|
|
|
}
|
|
|
|
|
|
|
|
private abstract class FailureListener<V> extends StateAwareListener<V> {
|
|
|
|
FailureListener(CallState expectedState, long expectedCallId) {
|
|
|
|
super(expectedState, expectedCallId);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSuccessContinue(V result) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
private abstract class SuccessOnlyListener<V> extends StateAwareListener<V> {
|
|
|
|
SuccessOnlyListener(CallState expectedState, long expectedCallId) {
|
|
|
|
super(expectedState, expectedCallId);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailureContinue(Throwable throwable) {
|
|
|
|
Log.w(TAG, throwable);
|
|
|
|
throw new AssertionError(throwable);
|
|
|
|
}
|
|
|
|
}
|
2017-02-05 20:38:08 +00:00
|
|
|
|
|
|
|
@WorkerThread
|
|
|
|
public static boolean isCallActive(Context context) {
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.i(TAG, "isCallActive()");
|
2017-02-05 20:38:08 +00:00
|
|
|
|
|
|
|
HandlerThread handlerThread = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
handlerThread = new HandlerThread("webrtc-callback");
|
|
|
|
handlerThread.start();
|
|
|
|
|
|
|
|
final SettableFuture<Boolean> future = new SettableFuture<>();
|
|
|
|
|
|
|
|
ResultReceiver resultReceiver = new ResultReceiver(new Handler(handlerThread.getLooper())) {
|
|
|
|
protected void onReceiveResult(int resultCode, Bundle resultData) {
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.i(TAG, "Got result...");
|
2017-02-05 20:38:08 +00:00
|
|
|
future.set(resultCode == 1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Intent intent = new Intent(context, WebRtcCallService.class);
|
|
|
|
intent.setAction(ACTION_IS_IN_CALL_QUERY);
|
|
|
|
intent.putExtra(EXTRA_RESULT_RECEIVER, resultReceiver);
|
|
|
|
|
|
|
|
context.startService(intent);
|
|
|
|
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.i(TAG, "Blocking on result...");
|
2017-02-05 20:38:08 +00:00
|
|
|
return future.get();
|
|
|
|
} catch (InterruptedException | ExecutionException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
return false;
|
|
|
|
} finally {
|
|
|
|
if (handlerThread != null) handlerThread.quit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void isCallActive(Context context, ResultReceiver resultReceiver) {
|
|
|
|
Intent intent = new Intent(context, WebRtcCallService.class);
|
|
|
|
intent.setAction(ACTION_IS_IN_CALL_QUERY);
|
|
|
|
intent.putExtra(EXTRA_RESULT_RECEIVER, resultReceiver);
|
|
|
|
|
|
|
|
context.startService(intent);
|
|
|
|
}
|
2019-05-23 19:56:05 +00:00
|
|
|
|
|
|
|
private class HangUpRtcOnPstnCallAnsweredListener extends PhoneStateListener {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCallStateChanged(int state, String phoneNumber) {
|
|
|
|
super.onCallStateChanged(state, phoneNumber);
|
|
|
|
if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
|
|
|
|
hangup();
|
|
|
|
Log.i(TAG, "Device phone call ended Signal call.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void hangup() {
|
|
|
|
Intent intent = new Intent(WebRtcCallService.this, WebRtcCallService.class);
|
|
|
|
intent.setAction(ACTION_LOCAL_HANGUP);
|
|
|
|
|
|
|
|
startService(intent);
|
|
|
|
}
|
|
|
|
}
|
2016-11-09 17:37:40 +00:00
|
|
|
}
|