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