1package eu.siacs.conversations.services;
2
3import android.content.Context;
4import android.media.AudioManager;
5import android.media.ToneGenerator;
6import android.net.Uri;
7import android.os.Build;
8import android.telecom.CallAudioState;
9import android.telecom.CallEndpoint;
10import android.telecom.Connection;
11import android.telecom.DisconnectCause;
12import android.util.Log;
13
14import androidx.annotation.NonNull;
15import androidx.annotation.RequiresApi;
16import androidx.core.content.ContextCompat;
17
18import com.google.common.collect.ImmutableSet;
19import com.google.common.collect.Iterables;
20import com.google.common.collect.Lists;
21
22import eu.siacs.conversations.Config;
23import eu.siacs.conversations.R;
24import eu.siacs.conversations.ui.util.MainThreadExecutor;
25import eu.siacs.conversations.xmpp.Jid;
26import eu.siacs.conversations.xmpp.jingle.JingleConnectionManager;
27import eu.siacs.conversations.xmpp.jingle.Media;
28
29import java.util.Collections;
30import java.util.List;
31import java.util.Set;
32import java.util.concurrent.TimeUnit;
33import java.util.concurrent.atomic.AtomicBoolean;
34
35public class CallIntegration extends Connection {
36
37 private static final int DEFAULT_VOLUME = 80;
38
39 private final Context context;
40
41 private final AppRTCAudioManager appRTCAudioManager;
42 private AudioDevice initialAudioDevice = null;
43 private final AtomicBoolean initialAudioDeviceConfigured = new AtomicBoolean(false);
44 private final AtomicBoolean delayedDestructionInitiated = new AtomicBoolean(false);
45
46 private List<CallEndpoint> availableEndpoints = Collections.emptyList();
47
48 private Callback callback = null;
49
50 public CallIntegration(final Context context) {
51 this.context = context.getApplicationContext();
52 if (selfManaged()) {
53 setConnectionProperties(Connection.PROPERTY_SELF_MANAGED);
54 this.appRTCAudioManager = null;
55 } else {
56 this.appRTCAudioManager = new AppRTCAudioManager(context);
57 ContextCompat.getMainExecutor(context)
58 .execute(() -> this.appRTCAudioManager.start(this::onAudioDeviceChanged));
59 }
60 setRingbackRequested(true);
61 }
62
63 public void setCallback(final Callback callback) {
64 this.callback = callback;
65 }
66
67 @Override
68 public void onShowIncomingCallUi() {
69 Log.d(Config.LOGTAG, "onShowIncomingCallUi");
70 this.callback.onCallIntegrationShowIncomingCallUi();
71 }
72
73 @Override
74 public void onAnswer() {
75 this.callback.onCallIntegrationAnswer();
76 }
77
78 @Override
79 public void onDisconnect() {
80 Log.d(Config.LOGTAG, "onDisconnect()");
81 this.callback.onCallIntegrationDisconnect();
82 }
83
84 @Override
85 public void onReject() {
86 this.callback.onCallIntegrationReject();
87 }
88
89 @Override
90 public void onReject(final String replyMessage) {
91 Log.d(Config.LOGTAG, "onReject(" + replyMessage + ")");
92 this.callback.onCallIntegrationReject();
93 }
94
95 @RequiresApi(api = Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
96 @Override
97 public void onAvailableCallEndpointsChanged(@NonNull List<CallEndpoint> availableEndpoints) {
98 Log.d(Config.LOGTAG, "onAvailableCallEndpointsChanged(" + availableEndpoints + ")");
99 this.availableEndpoints = availableEndpoints;
100 this.onAudioDeviceChanged(
101 getAudioDeviceUpsideDownCake(getCurrentCallEndpoint()),
102 ImmutableSet.copyOf(
103 Lists.transform(
104 availableEndpoints,
105 CallIntegration::getAudioDeviceUpsideDownCake)));
106 }
107
108 @RequiresApi(api = Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
109 @Override
110 public void onCallEndpointChanged(@NonNull final CallEndpoint callEndpoint) {
111 Log.d(Config.LOGTAG, "onCallEndpointChanged()");
112 this.onAudioDeviceChanged(
113 getAudioDeviceUpsideDownCake(callEndpoint),
114 ImmutableSet.copyOf(
115 Lists.transform(
116 this.availableEndpoints,
117 CallIntegration::getAudioDeviceUpsideDownCake)));
118 }
119
120 @Override
121 public void onCallAudioStateChanged(final CallAudioState state) {
122 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
123 Log.d(Config.LOGTAG, "ignoring onCallAudioStateChange() on Upside Down Cake");
124 return;
125 }
126 Log.d(Config.LOGTAG, "onCallAudioStateChange(" + state + ")");
127 this.onAudioDeviceChanged(getAudioDeviceOreo(state), getAudioDevicesOreo(state));
128 }
129
130 public Set<AudioDevice> getAudioDevices() {
131 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
132 return getAudioDevicesUpsideDownCake();
133 } else if (selfManaged()) {
134 return getAudioDevicesOreo();
135 } else {
136 return getAudioDevicesFallback();
137 }
138 }
139
140 public AudioDevice getSelectedAudioDevice() {
141 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
142 return getAudioDeviceUpsideDownCake();
143 } else if (selfManaged()) {
144 return getAudioDeviceOreo();
145 } else {
146 return getAudioDeviceFallback();
147 }
148 }
149
150 public void setAudioDevice(final AudioDevice audioDevice) {
151 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
152 setAudioDeviceUpsideDownCake(audioDevice);
153 } else if (selfManaged()) {
154 setAudioDeviceOreo(audioDevice);
155 } else {
156 setAudioDeviceFallback(audioDevice);
157 }
158 }
159
160 public void setAudioDeviceWhenAvailable(final AudioDevice audioDevice) {
161 final var available = getAudioDevices();
162 if (available.contains(audioDevice)) {
163 this.setAudioDevice(audioDevice);
164 } else {
165 Log.d(
166 Config.LOGTAG,
167 "application requested to switch to "
168 + audioDevice
169 + " but device was not available");
170 }
171 }
172
173 @RequiresApi(api = Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
174 private Set<AudioDevice> getAudioDevicesUpsideDownCake() {
175 return ImmutableSet.copyOf(
176 Lists.transform(
177 this.availableEndpoints, CallIntegration::getAudioDeviceUpsideDownCake));
178 }
179
180 @RequiresApi(api = Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
181 private AudioDevice getAudioDeviceUpsideDownCake() {
182 return getAudioDeviceUpsideDownCake(getCurrentCallEndpoint());
183 }
184
185 @RequiresApi(api = Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
186 private static AudioDevice getAudioDeviceUpsideDownCake(final CallEndpoint callEndpoint) {
187 if (callEndpoint == null) {
188 return AudioDevice.NONE;
189 }
190 final var endpointType = callEndpoint.getEndpointType();
191 return switch (endpointType) {
192 case CallEndpoint.TYPE_BLUETOOTH -> AudioDevice.BLUETOOTH;
193 case CallEndpoint.TYPE_EARPIECE -> AudioDevice.EARPIECE;
194 case CallEndpoint.TYPE_SPEAKER -> AudioDevice.SPEAKER_PHONE;
195 case CallEndpoint.TYPE_WIRED_HEADSET -> AudioDevice.WIRED_HEADSET;
196 case CallEndpoint.TYPE_STREAMING -> AudioDevice.STREAMING;
197 case CallEndpoint.TYPE_UNKNOWN -> AudioDevice.NONE;
198 default -> throw new IllegalStateException("Unknown endpoint type " + endpointType);
199 };
200 }
201
202 @RequiresApi(api = Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
203 private void setAudioDeviceUpsideDownCake(final AudioDevice audioDevice) {
204 final var callEndpointOptional =
205 Iterables.tryFind(
206 this.availableEndpoints,
207 e -> getAudioDeviceUpsideDownCake(e) == audioDevice);
208 if (callEndpointOptional.isPresent()) {
209 final var endpoint = callEndpointOptional.get();
210 requestCallEndpointChange(
211 endpoint,
212 MainThreadExecutor.getInstance(),
213 result -> Log.d(Config.LOGTAG, "switched to endpoint " + endpoint));
214 } else {
215 Log.w(Config.LOGTAG, "no endpoint found matching " + audioDevice);
216 }
217 }
218
219 private Set<AudioDevice> getAudioDevicesOreo() {
220 final var audioState = getCallAudioState();
221 if (audioState == null) {
222 Log.d(
223 Config.LOGTAG,
224 "no CallAudioState available. returning empty set for audio devices");
225 return Collections.emptySet();
226 }
227 return getAudioDevicesOreo(audioState);
228 }
229
230 private static Set<AudioDevice> getAudioDevicesOreo(final CallAudioState callAudioState) {
231 final ImmutableSet.Builder<AudioDevice> supportedAudioDevicesBuilder =
232 new ImmutableSet.Builder<>();
233 final var supportedRouteMask = callAudioState.getSupportedRouteMask();
234 if ((supportedRouteMask & CallAudioState.ROUTE_BLUETOOTH)
235 == CallAudioState.ROUTE_BLUETOOTH) {
236 supportedAudioDevicesBuilder.add(AudioDevice.BLUETOOTH);
237 }
238 if ((supportedRouteMask & CallAudioState.ROUTE_EARPIECE) == CallAudioState.ROUTE_EARPIECE) {
239 supportedAudioDevicesBuilder.add(AudioDevice.EARPIECE);
240 }
241 if ((supportedRouteMask & CallAudioState.ROUTE_SPEAKER) == CallAudioState.ROUTE_SPEAKER) {
242 supportedAudioDevicesBuilder.add(AudioDevice.SPEAKER_PHONE);
243 }
244 if ((supportedRouteMask & CallAudioState.ROUTE_WIRED_HEADSET)
245 == CallAudioState.ROUTE_WIRED_HEADSET) {
246 supportedAudioDevicesBuilder.add(AudioDevice.WIRED_HEADSET);
247 }
248 return supportedAudioDevicesBuilder.build();
249 }
250
251 private AudioDevice getAudioDeviceOreo() {
252 final var audioState = getCallAudioState();
253 if (audioState == null) {
254 Log.d(Config.LOGTAG, "no CallAudioState available. returning NONE as audio device");
255 return AudioDevice.NONE;
256 }
257 return getAudioDeviceOreo(audioState);
258 }
259
260 private static AudioDevice getAudioDeviceOreo(final CallAudioState audioState) {
261 // technically we get a mask here; maybe we should query the mask instead
262 return switch (audioState.getRoute()) {
263 case CallAudioState.ROUTE_BLUETOOTH -> AudioDevice.BLUETOOTH;
264 case CallAudioState.ROUTE_EARPIECE -> AudioDevice.EARPIECE;
265 case CallAudioState.ROUTE_SPEAKER -> AudioDevice.SPEAKER_PHONE;
266 case CallAudioState.ROUTE_WIRED_HEADSET -> AudioDevice.WIRED_HEADSET;
267 default -> AudioDevice.NONE;
268 };
269 }
270
271 @RequiresApi(api = Build.VERSION_CODES.O)
272 private void setAudioDeviceOreo(final AudioDevice audioDevice) {
273 switch (audioDevice) {
274 case EARPIECE -> setAudioRoute(CallAudioState.ROUTE_EARPIECE);
275 case BLUETOOTH -> setAudioRoute(CallAudioState.ROUTE_BLUETOOTH);
276 case WIRED_HEADSET -> setAudioRoute(CallAudioState.ROUTE_WIRED_HEADSET);
277 case SPEAKER_PHONE -> setAudioRoute(CallAudioState.ROUTE_SPEAKER);
278 }
279 }
280
281 private Set<AudioDevice> getAudioDevicesFallback() {
282 return requireAppRtcAudioManager().getAudioDevices();
283 }
284
285 private AudioDevice getAudioDeviceFallback() {
286 return requireAppRtcAudioManager().getSelectedAudioDevice();
287 }
288
289 private void setAudioDeviceFallback(final AudioDevice audioDevice) {
290 final var audioManager = requireAppRtcAudioManager();
291 audioManager.executeOnMain(() -> audioManager.setDefaultAudioDevice(audioDevice));
292 }
293
294 @NonNull
295 private AppRTCAudioManager requireAppRtcAudioManager() {
296 if (this.appRTCAudioManager == null) {
297 throw new IllegalStateException(
298 "You are trying to access the fallback audio manager on a modern device");
299 }
300 return this.appRTCAudioManager;
301 }
302
303 @Override
304 public void onStateChanged(final int state) {
305 Log.d(Config.LOGTAG, "onStateChanged(" + state + ")");
306 // TODO devices before selfManaged() will likely have to play their own ringback sound
307 if (state == STATE_ACTIVE) {
308 playConnectedSound();
309 } else if (state == STATE_DISCONNECTED) {
310 final var audioManager = this.appRTCAudioManager;
311 if (audioManager != null) {
312 audioManager.executeOnMain(audioManager::stop);
313 }
314 }
315 }
316
317 private void playConnectedSound() {
318 final var mediaPlayer = MediaPlayer.create(context, R.raw.connected);
319 mediaPlayer.setVolume(DEFAULT_VOLUME / 100f, DEFAULT_VOLUME / 100f);
320 mediaPlayer.start();
321 }
322
323 public void success() {
324 Log.d(Config.LOGTAG, "CallIntegration.success()");
325 final var toneGenerator = new ToneGenerator(AudioManager.STREAM_MUSIC, DEFAULT_VOLUME);
326 toneGenerator.startTone(ToneGenerator.TONE_CDMA_CALLDROP_LITE, 375);
327 this.destroyWithDelay(new DisconnectCause(DisconnectCause.LOCAL, null), 375);
328 }
329
330 public void accepted() {
331 Log.d(Config.LOGTAG, "CallIntegration.accepted()");
332 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
333 this.destroyWith(new DisconnectCause(DisconnectCause.ANSWERED_ELSEWHERE, null));
334 } else {
335 this.destroyWith(new DisconnectCause(DisconnectCause.CANCELED, null));
336 }
337 }
338
339 public void error() {
340 Log.d(Config.LOGTAG, "CallIntegration.error()");
341 final var toneGenerator = new ToneGenerator(AudioManager.STREAM_MUSIC, DEFAULT_VOLUME);
342 toneGenerator.startTone(ToneGenerator.TONE_CDMA_CALLDROP_LITE, 375);
343 this.destroyWithDelay(new DisconnectCause(DisconnectCause.ERROR, null), 375);
344 this.destroyWith(new DisconnectCause(DisconnectCause.ERROR, null));
345 }
346
347 public void retracted() {
348 Log.d(Config.LOGTAG, "CallIntegration.retracted()");
349 // an alternative cause would be LOCAL
350 this.destroyWith(new DisconnectCause(DisconnectCause.CANCELED, null));
351 }
352
353 public void rejected() {
354 Log.d(Config.LOGTAG, "CallIntegration.rejected()");
355 this.destroyWith(new DisconnectCause(DisconnectCause.REJECTED, null));
356 }
357
358 public void busy() {
359 Log.d(Config.LOGTAG, "CallIntegration.busy()");
360 final var toneGenerator = new ToneGenerator(AudioManager.STREAM_MUSIC, 80);
361 toneGenerator.startTone(ToneGenerator.TONE_CDMA_NETWORK_BUSY, 2500);
362 this.destroyWithDelay(new DisconnectCause(DisconnectCause.BUSY, null), 2500);
363 }
364
365 private void destroyWithDelay(final DisconnectCause disconnectCause, final int delay) {
366 if (this.delayedDestructionInitiated.compareAndSet(false, true)) {
367 JingleConnectionManager.SCHEDULED_EXECUTOR_SERVICE.schedule(
368 () -> {
369 this.setDisconnected(disconnectCause);
370 this.destroy();
371 },
372 delay,
373 TimeUnit.MILLISECONDS);
374 } else {
375 Log.w(Config.LOGTAG, "CallIntegration destruction has already been scheduled!");
376 }
377 }
378
379 private void destroyWith(final DisconnectCause disconnectCause) {
380 if (this.getState() == STATE_DISCONNECTED || this.delayedDestructionInitiated.get()) {
381 Log.d(Config.LOGTAG, "CallIntegration has already been destroyed");
382 return;
383 }
384 this.setDisconnected(disconnectCause);
385 this.destroy();
386 Log.d(Config.LOGTAG, "destroyed!");
387 }
388
389 public static Uri address(final Jid contact) {
390 return Uri.parse(String.format("xmpp:%s", contact.toEscapedString()));
391 }
392
393 public void verifyDisconnected() {
394 if (this.getState() == STATE_DISCONNECTED || this.delayedDestructionInitiated.get()) {
395 return;
396 }
397 throw new AssertionError("CallIntegration has not been disconnected");
398 }
399
400 private void onAudioDeviceChanged(
401 final CallIntegration.AudioDevice selectedAudioDevice,
402 final Set<CallIntegration.AudioDevice> availableAudioDevices) {
403 if (this.initialAudioDevice != null
404 && this.initialAudioDeviceConfigured.compareAndSet(false, true)) {
405 if (availableAudioDevices.contains(this.initialAudioDevice)) {
406 setAudioDevice(this.initialAudioDevice);
407 Log.d(Config.LOGTAG, "configured initial audio device");
408 } else {
409 Log.d(
410 Config.LOGTAG,
411 "initial audio device not available. available devices: "
412 + availableAudioDevices);
413 }
414 }
415 final var callback = this.callback;
416 if (callback == null) {
417 return;
418 }
419 callback.onAudioDeviceChanged(selectedAudioDevice, availableAudioDevices);
420 }
421
422 public static boolean selfManaged() {
423 return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O;
424 }
425
426 public static boolean notSelfManaged() {
427 return Build.VERSION.SDK_INT < Build.VERSION_CODES.O;
428 }
429
430 public void setInitialAudioDevice(final AudioDevice audioDevice) {
431 Log.d(Config.LOGTAG, "setInitialAudioDevice(" + audioDevice + ")");
432 this.initialAudioDevice = audioDevice;
433 if (CallIntegration.selfManaged()) {
434 // once the 'CallIntegration' gets added to the system we receive calls to update audio
435 // state
436 return;
437 }
438 final var audioManager = requireAppRtcAudioManager();
439 audioManager.executeOnMain(
440 () ->
441 this.onAudioDeviceChanged(
442 audioManager.getSelectedAudioDevice(),
443 audioManager.getAudioDevices()));
444 }
445
446 /** AudioDevice is the names of possible audio devices that we currently support. */
447 public enum AudioDevice {
448 NONE,
449 SPEAKER_PHONE,
450 WIRED_HEADSET,
451 EARPIECE,
452 BLUETOOTH,
453 STREAMING
454 }
455
456 public static AudioDevice initialAudioDevice(final Set<Media> media) {
457 if (Media.audioOnly(media)) {
458 return AudioDevice.EARPIECE;
459 } else {
460 return AudioDevice.SPEAKER_PHONE;
461 }
462 }
463
464 public interface Callback {
465 void onCallIntegrationShowIncomingCallUi();
466
467 void onCallIntegrationDisconnect();
468
469 void onAudioDeviceChanged(
470 CallIntegration.AudioDevice selectedAudioDevice,
471 Set<CallIntegration.AudioDevice> availableAudioDevices);
472
473 void onCallIntegrationReject();
474
475 void onCallIntegrationAnswer();
476 }
477}