CallIntegration.java

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