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 if (state == STATE_ACTIVE) {
307 playConnectedSound();
308 } else if (state == STATE_DISCONNECTED) {
309 final var audioManager = this.appRTCAudioManager;
310 if (audioManager != null) {
311 audioManager.executeOnMain(audioManager::stop);
312 }
313 }
314 }
315
316 private void playConnectedSound() {
317 final var mediaPlayer = MediaPlayer.create(context, R.raw.connected);
318 mediaPlayer.setVolume(DEFAULT_VOLUME / 100f, DEFAULT_VOLUME / 100f);
319 mediaPlayer.start();
320 }
321
322 public void success() {
323 Log.d(Config.LOGTAG, "CallIntegration.success()");
324 final var toneGenerator = new ToneGenerator(AudioManager.STREAM_MUSIC, DEFAULT_VOLUME);
325 toneGenerator.startTone(ToneGenerator.TONE_CDMA_CALLDROP_LITE, 375);
326 this.destroyWithDelay(new DisconnectCause(DisconnectCause.LOCAL, null), 375);
327 }
328
329 public void accepted() {
330 Log.d(Config.LOGTAG, "CallIntegration.accepted()");
331 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
332 this.destroyWith(new DisconnectCause(DisconnectCause.ANSWERED_ELSEWHERE, null));
333 } else {
334 this.destroyWith(new DisconnectCause(DisconnectCause.CANCELED, null));
335 }
336 }
337
338 public void error() {
339 Log.d(Config.LOGTAG, "CallIntegration.error()");
340 final var toneGenerator = new ToneGenerator(AudioManager.STREAM_MUSIC, DEFAULT_VOLUME);
341 toneGenerator.startTone(ToneGenerator.TONE_CDMA_CALLDROP_LITE, 375);
342 this.destroyWithDelay(new DisconnectCause(DisconnectCause.ERROR, null), 375);
343 this.destroyWith(new DisconnectCause(DisconnectCause.ERROR, null));
344 }
345
346 public void retracted() {
347 Log.d(Config.LOGTAG, "CallIntegration.retracted()");
348 // an alternative cause would be LOCAL
349 this.destroyWith(new DisconnectCause(DisconnectCause.CANCELED, null));
350 }
351
352 public void rejected() {
353 Log.d(Config.LOGTAG, "CallIntegration.rejected()");
354 this.destroyWith(new DisconnectCause(DisconnectCause.REJECTED, null));
355 }
356
357 public void busy() {
358 Log.d(Config.LOGTAG, "CallIntegration.busy()");
359 final var toneGenerator = new ToneGenerator(AudioManager.STREAM_MUSIC, 80);
360 toneGenerator.startTone(ToneGenerator.TONE_CDMA_NETWORK_BUSY, 2500);
361 this.destroyWithDelay(new DisconnectCause(DisconnectCause.BUSY, null), 2500);
362 }
363
364 private void destroyWithDelay(final DisconnectCause disconnectCause, final int delay) {
365 if (this.delayedDestructionInitiated.compareAndSet(false, true)) {
366 JingleConnectionManager.SCHEDULED_EXECUTOR_SERVICE.schedule(
367 () -> {
368 this.setDisconnected(disconnectCause);
369 this.destroy();
370 },
371 delay,
372 TimeUnit.MILLISECONDS);
373 } else {
374 Log.w(Config.LOGTAG, "CallIntegration destruction has already been scheduled!");
375 }
376 }
377
378 private void destroyWith(final DisconnectCause disconnectCause) {
379 if (this.getState() == STATE_DISCONNECTED || this.delayedDestructionInitiated.get()) {
380 Log.d(Config.LOGTAG, "CallIntegration has already been destroyed");
381 return;
382 }
383 this.setDisconnected(disconnectCause);
384 this.destroy();
385 Log.d(Config.LOGTAG, "destroyed!");
386 }
387
388 public static Uri address(final Jid contact) {
389 return Uri.parse(String.format("xmpp:%s", contact.toEscapedString()));
390 }
391
392 public void verifyDisconnected() {
393 if (this.getState() == STATE_DISCONNECTED || this.delayedDestructionInitiated.get()) {
394 return;
395 }
396 throw new AssertionError("CallIntegration has not been disconnected");
397 }
398
399 private void onAudioDeviceChanged(
400 final CallIntegration.AudioDevice selectedAudioDevice,
401 final Set<CallIntegration.AudioDevice> availableAudioDevices) {
402 if (this.initialAudioDevice != null
403 && this.initialAudioDeviceConfigured.compareAndSet(false, true)) {
404 if (availableAudioDevices.contains(this.initialAudioDevice)) {
405 setAudioDevice(this.initialAudioDevice);
406 Log.d(Config.LOGTAG, "configured initial audio device");
407 } else {
408 Log.d(
409 Config.LOGTAG,
410 "initial audio device not available. available devices: "
411 + availableAudioDevices);
412 }
413 }
414 final var callback = this.callback;
415 if (callback == null) {
416 return;
417 }
418 callback.onAudioDeviceChanged(selectedAudioDevice, availableAudioDevices);
419 }
420
421 public static boolean selfManaged() {
422 return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O;
423 }
424
425 public static boolean notSelfManaged() {
426 return Build.VERSION.SDK_INT < Build.VERSION_CODES.O;
427 }
428
429 public void setInitialAudioDevice(final AudioDevice audioDevice) {
430 Log.d(Config.LOGTAG, "setInitialAudioDevice(" + audioDevice + ")");
431 this.initialAudioDevice = audioDevice;
432 if (CallIntegration.selfManaged()) {
433 // once the 'CallIntegration' gets added to the system we receive calls to update audio
434 // state
435 return;
436 }
437 final var audioManager = requireAppRtcAudioManager();
438 audioManager.executeOnMain(
439 () ->
440 this.onAudioDeviceChanged(
441 audioManager.getSelectedAudioDevice(),
442 audioManager.getAudioDevices()));
443 }
444
445 /** AudioDevice is the names of possible audio devices that we currently support. */
446 public enum AudioDevice {
447 NONE,
448 SPEAKER_PHONE,
449 WIRED_HEADSET,
450 EARPIECE,
451 BLUETOOTH,
452 STREAMING
453 }
454
455 public static AudioDevice initialAudioDevice(final Set<Media> media) {
456 if (Media.audioOnly(media)) {
457 return AudioDevice.EARPIECE;
458 } else {
459 return AudioDevice.SPEAKER_PHONE;
460 }
461 }
462
463 public interface Callback {
464 void onCallIntegrationShowIncomingCallUi();
465
466 void onCallIntegrationDisconnect();
467
468 void onAudioDeviceChanged(
469 CallIntegration.AudioDevice selectedAudioDevice,
470 Set<CallIntegration.AudioDevice> availableAudioDevices);
471
472 void onCallIntegrationReject();
473
474 void onCallIntegrationAnswer();
475 }
476}