2017-10-06 20:15:51 +00:00
|
|
|
// @flow
|
2017-09-08 09:28:44 +00:00
|
|
|
|
2017-09-28 21:25:04 +00:00
|
|
|
import { NativeModules } from 'react-native';
|
2017-09-08 09:28:44 +00:00
|
|
|
import uuid from 'uuid';
|
|
|
|
|
2017-10-16 19:58:38 +00:00
|
|
|
import { sendAnalyticsEvent } from '../../analytics';
|
2017-09-28 21:25:04 +00:00
|
|
|
import { APP_WILL_MOUNT, APP_WILL_UNMOUNT, appNavigate } from '../../app';
|
2017-09-08 09:28:44 +00:00
|
|
|
import {
|
|
|
|
CONFERENCE_FAILED,
|
|
|
|
CONFERENCE_LEFT,
|
|
|
|
CONFERENCE_WILL_JOIN,
|
2017-09-28 21:25:04 +00:00
|
|
|
CONFERENCE_JOINED,
|
|
|
|
getCurrentConference
|
2017-09-08 09:28:44 +00:00
|
|
|
} from '../../base/conference';
|
|
|
|
import { getInviteURL } from '../../base/connection';
|
|
|
|
import {
|
2017-09-28 21:25:04 +00:00
|
|
|
isVideoMutedByAudioOnly,
|
2017-09-08 09:28:44 +00:00
|
|
|
SET_AUDIO_MUTED,
|
|
|
|
SET_VIDEO_MUTED,
|
|
|
|
setAudioMuted
|
|
|
|
} from '../../base/media';
|
2017-09-28 21:25:04 +00:00
|
|
|
import { MiddlewareRegistry } from '../../base/redux';
|
|
|
|
|
|
|
|
import { _SET_CALLKIT_SUBSCRIPTIONS } from './actionTypes';
|
2017-09-08 09:28:44 +00:00
|
|
|
import CallKit from './CallKit';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Middleware that captures several system actions and hooks up CallKit.
|
|
|
|
*
|
|
|
|
* @param {Store} store - The redux store.
|
|
|
|
* @returns {Function}
|
|
|
|
*/
|
2017-09-28 21:25:04 +00:00
|
|
|
CallKit && MiddlewareRegistry.register(store => next => action => {
|
2017-09-08 09:28:44 +00:00
|
|
|
switch (action.type) {
|
2017-09-28 21:25:04 +00:00
|
|
|
case _SET_CALLKIT_SUBSCRIPTIONS:
|
|
|
|
return _setCallKitSubscriptions(store, next, action);
|
2017-09-08 09:28:44 +00:00
|
|
|
|
2017-09-28 21:25:04 +00:00
|
|
|
case APP_WILL_MOUNT:
|
|
|
|
return _appWillMount(store, next, action);
|
2017-09-08 09:28:44 +00:00
|
|
|
|
|
|
|
case APP_WILL_UNMOUNT:
|
2017-09-28 21:25:04 +00:00
|
|
|
store.dispatch({
|
|
|
|
type: _SET_CALLKIT_SUBSCRIPTIONS,
|
|
|
|
subscriptions: undefined
|
2017-09-08 09:28:44 +00:00
|
|
|
});
|
|
|
|
break;
|
|
|
|
|
2017-09-28 21:25:04 +00:00
|
|
|
case CONFERENCE_FAILED:
|
|
|
|
return _conferenceFailed(store, next, action);
|
2017-09-08 09:28:44 +00:00
|
|
|
|
2017-09-28 21:25:04 +00:00
|
|
|
case CONFERENCE_JOINED:
|
|
|
|
return _conferenceJoined(store, next, action);
|
2017-09-08 09:28:44 +00:00
|
|
|
|
2017-09-28 21:25:04 +00:00
|
|
|
case CONFERENCE_LEFT:
|
|
|
|
return _conferenceLeft(store, next, action);
|
2017-09-08 09:28:44 +00:00
|
|
|
|
2017-09-28 21:25:04 +00:00
|
|
|
case CONFERENCE_WILL_JOIN:
|
|
|
|
return _conferenceWillJoin(store, next, action);
|
2017-09-08 09:28:44 +00:00
|
|
|
|
2017-09-28 21:25:04 +00:00
|
|
|
case SET_AUDIO_MUTED:
|
|
|
|
return _setAudioMuted(store, next, action);
|
2017-09-08 09:28:44 +00:00
|
|
|
|
2017-09-28 21:25:04 +00:00
|
|
|
case SET_VIDEO_MUTED:
|
|
|
|
return _setVideoMuted(store, next, action);
|
2017-09-08 09:28:44 +00:00
|
|
|
}
|
|
|
|
|
2017-09-28 21:25:04 +00:00
|
|
|
return next(action);
|
|
|
|
});
|
2017-09-08 09:28:44 +00:00
|
|
|
|
2017-09-28 21:25:04 +00:00
|
|
|
/**
|
|
|
|
* Notifies the feature jwt that the action {@link APP_WILL_MOUNT} is being
|
2017-10-01 06:35:19 +00:00
|
|
|
* dispatched within a specific redux {@code store}.
|
2017-09-28 21:25:04 +00:00
|
|
|
*
|
2017-10-01 06:35:19 +00:00
|
|
|
* @param {Store} store - The redux store in which the specified {@code action}
|
2017-09-28 21:25:04 +00:00
|
|
|
* is being dispatched.
|
|
|
|
* @param {Dispatch} next - The redux dispatch function to dispatch the
|
2017-10-01 06:35:19 +00:00
|
|
|
* specified {@code action} to the specified {@code store}.
|
|
|
|
* @param {Action} action - The redux action {@code APP_WILL_MOUNT} which is
|
|
|
|
* being dispatched in the specified {@code store}.
|
2017-09-28 21:25:04 +00:00
|
|
|
* @private
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
function _appWillMount({ dispatch, getState }, next, action) {
|
|
|
|
const result = next(action);
|
2017-09-08 09:28:44 +00:00
|
|
|
|
2017-09-28 21:25:04 +00:00
|
|
|
CallKit.setProviderConfiguration({
|
2017-10-09 20:23:25 +00:00
|
|
|
iconTemplateImageName: 'CallKitIcon',
|
2017-09-28 21:25:04 +00:00
|
|
|
localizedName: NativeModules.AppInfo.name
|
|
|
|
});
|
|
|
|
|
|
|
|
const context = {
|
|
|
|
dispatch,
|
|
|
|
getState
|
|
|
|
};
|
|
|
|
const subscriptions = [
|
|
|
|
CallKit.addListener(
|
|
|
|
'performEndCallAction',
|
|
|
|
_onPerformEndCallAction,
|
|
|
|
context),
|
|
|
|
CallKit.addListener(
|
|
|
|
'performSetMutedCallAction',
|
|
|
|
_onPerformSetMutedCallAction,
|
|
|
|
context),
|
|
|
|
|
|
|
|
// According to CallKit's documentation, when the system resets we
|
|
|
|
// should terminate all calls. Hence, providerDidReset is the same
|
|
|
|
// to us as performEndCallAction.
|
|
|
|
CallKit.addListener(
|
|
|
|
'providerDidReset',
|
|
|
|
_onPerformEndCallAction,
|
|
|
|
context)
|
|
|
|
];
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: _SET_CALLKIT_SUBSCRIPTIONS,
|
|
|
|
subscriptions
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notifies the feature jwt that the action {@link CONFERENCE_FAILED} is being
|
2017-10-01 06:35:19 +00:00
|
|
|
* dispatched within a specific redux {@code store}.
|
2017-09-28 21:25:04 +00:00
|
|
|
*
|
2017-10-01 06:35:19 +00:00
|
|
|
* @param {Store} store - The redux store in which the specified {@code action}
|
2017-09-28 21:25:04 +00:00
|
|
|
* is being dispatched.
|
|
|
|
* @param {Dispatch} next - The redux dispatch function to dispatch the
|
2017-10-01 06:35:19 +00:00
|
|
|
* specified {@code action} to the specified {@code store}.
|
|
|
|
* @param {Action} action - The redux action {@code CONFERENCE_FAILED} which is
|
|
|
|
* being dispatched in the specified {@code store}.
|
2017-09-28 21:25:04 +00:00
|
|
|
* @private
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
function _conferenceFailed(store, next, action) {
|
|
|
|
const result = next(action);
|
|
|
|
|
2017-10-19 18:29:25 +00:00
|
|
|
// XXX Certain CONFERENCE_FAILED errors are recoverable i.e. they have
|
|
|
|
// prevented the user from joining a specific conference but the app may be
|
|
|
|
// able to eventually join the conference.
|
|
|
|
if (!action.error.recoverable) {
|
|
|
|
const { callUUID } = action.conference;
|
|
|
|
|
|
|
|
if (callUUID) {
|
|
|
|
CallKit.reportCallFailed(callUUID);
|
|
|
|
}
|
2017-09-08 09:28:44 +00:00
|
|
|
}
|
|
|
|
|
2017-09-28 21:25:04 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notifies the feature jwt that the action {@link CONFERENCE_JOINED} is being
|
2017-10-01 06:35:19 +00:00
|
|
|
* dispatched within a specific redux {@code store}.
|
2017-09-28 21:25:04 +00:00
|
|
|
*
|
2017-10-01 06:35:19 +00:00
|
|
|
* @param {Store} store - The redux store in which the specified {@code action}
|
2017-09-28 21:25:04 +00:00
|
|
|
* is being dispatched.
|
|
|
|
* @param {Dispatch} next - The redux dispatch function to dispatch the
|
2017-10-01 06:35:19 +00:00
|
|
|
* specified {@code action} to the specified {@code store}.
|
|
|
|
* @param {Action} action - The redux action {@code CONFERENCE_JOINED} which is
|
|
|
|
* being dispatched in the specified {@code store}.
|
2017-09-28 21:25:04 +00:00
|
|
|
* @private
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
function _conferenceJoined(store, next, action) {
|
|
|
|
const result = next(action);
|
2017-09-08 09:28:44 +00:00
|
|
|
|
2017-09-28 21:25:04 +00:00
|
|
|
const { callUUID } = action.conference;
|
2017-09-08 09:28:44 +00:00
|
|
|
|
2017-09-28 21:25:04 +00:00
|
|
|
if (callUUID) {
|
|
|
|
CallKit.reportConnectedOutgoingCall(callUUID);
|
2017-09-08 09:28:44 +00:00
|
|
|
}
|
|
|
|
|
2017-09-28 21:25:04 +00:00
|
|
|
return result;
|
|
|
|
}
|
2017-09-08 09:28:44 +00:00
|
|
|
|
2017-09-28 21:25:04 +00:00
|
|
|
/**
|
|
|
|
* Notifies the feature jwt that the action {@link CONFERENCE_LEFT} is being
|
2017-10-01 06:35:19 +00:00
|
|
|
* dispatched within a specific redux {@code store}.
|
2017-09-28 21:25:04 +00:00
|
|
|
*
|
2017-10-01 06:35:19 +00:00
|
|
|
* @param {Store} store - The redux store in which the specified {@code action}
|
2017-09-28 21:25:04 +00:00
|
|
|
* is being dispatched.
|
|
|
|
* @param {Dispatch} next - The redux dispatch function to dispatch the
|
2017-10-01 06:35:19 +00:00
|
|
|
* specified {@code action} to the specified {@code store}.
|
|
|
|
* @param {Action} action - The redux action {@code CONFERENCE_LEFT} which is
|
|
|
|
* being dispatched in the specified {@code store}.
|
2017-09-28 21:25:04 +00:00
|
|
|
* @private
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
function _conferenceLeft(store, next, action) {
|
|
|
|
const result = next(action);
|
2017-09-08 09:28:44 +00:00
|
|
|
|
2017-09-28 21:25:04 +00:00
|
|
|
const { callUUID } = action.conference;
|
|
|
|
|
|
|
|
if (callUUID) {
|
|
|
|
CallKit.endCall(callUUID);
|
2017-09-08 09:28:44 +00:00
|
|
|
}
|
|
|
|
|
2017-09-28 21:25:04 +00:00
|
|
|
return result;
|
|
|
|
}
|
2017-09-08 09:28:44 +00:00
|
|
|
|
2017-09-28 21:25:04 +00:00
|
|
|
/**
|
|
|
|
* Notifies the feature jwt that the action {@link CONFERENCE_WILL_JOIN} is
|
2017-10-01 06:35:19 +00:00
|
|
|
* being dispatched within a specific redux {@code store}.
|
2017-09-28 21:25:04 +00:00
|
|
|
*
|
2017-10-01 06:35:19 +00:00
|
|
|
* @param {Store} store - The redux store in which the specified {@code action}
|
2017-09-28 21:25:04 +00:00
|
|
|
* is being dispatched.
|
|
|
|
* @param {Dispatch} next - The redux dispatch function to dispatch the
|
2017-10-01 06:35:19 +00:00
|
|
|
* specified {@code action} to the specified {@code store}.
|
|
|
|
* @param {Action} action - The redux action {@code CONFERENCE_WILL_JOIN} which
|
|
|
|
* is being dispatched in the specified {@code store}.
|
2017-09-28 21:25:04 +00:00
|
|
|
* @private
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
function _conferenceWillJoin({ getState }, next, action) {
|
|
|
|
const result = next(action);
|
2017-09-08 09:28:44 +00:00
|
|
|
|
2017-10-10 20:07:40 +00:00
|
|
|
const { conference } = action;
|
|
|
|
const state = getState();
|
|
|
|
const url = getInviteURL(state);
|
|
|
|
const hasVideo = !isVideoMutedByAudioOnly(state);
|
2017-09-08 09:28:44 +00:00
|
|
|
|
2017-09-28 21:25:04 +00:00
|
|
|
// When assigning the call UUID, do so in upper case, since iOS will
|
|
|
|
// return it upper cased.
|
|
|
|
conference.callUUID = uuid.v4().toUpperCase();
|
|
|
|
CallKit.startCall(conference.callUUID, url.toString(), hasVideo)
|
|
|
|
.then(() => {
|
2017-10-10 20:07:40 +00:00
|
|
|
const { room } = state['features/base/conference'];
|
2017-10-05 22:54:13 +00:00
|
|
|
const { callee } = state['features/base/jwt'];
|
2017-09-28 21:25:04 +00:00
|
|
|
|
2017-10-10 20:07:40 +00:00
|
|
|
CallKit.updateCall(
|
|
|
|
conference.callUUID,
|
|
|
|
{ displayName: (callee && callee.name) || room });
|
2017-09-28 21:25:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-01 06:35:19 +00:00
|
|
|
* Handles CallKit's event {@code performEndCallAction}.
|
2017-09-28 21:25:04 +00:00
|
|
|
*
|
|
|
|
* @param {Object} event - The details of the CallKit event
|
2017-10-01 06:35:19 +00:00
|
|
|
* {@code performEndCallAction}.
|
2017-09-28 21:25:04 +00:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
function _onPerformEndCallAction({ callUUID }) {
|
|
|
|
const { dispatch, getState } = this; // eslint-disable-line no-invalid-this
|
|
|
|
const conference = getCurrentConference(getState);
|
|
|
|
|
|
|
|
if (conference && conference.callUUID === callUUID) {
|
2017-10-06 20:15:51 +00:00
|
|
|
// We arrive here when a call is ended by the system, for example, when
|
|
|
|
// another incoming call is received and the user selects "End &
|
|
|
|
// Accept".
|
2017-09-28 21:25:04 +00:00
|
|
|
delete conference.callUUID;
|
|
|
|
dispatch(appNavigate(undefined));
|
2017-09-08 09:28:44 +00:00
|
|
|
}
|
2017-09-28 21:25:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-01 06:35:19 +00:00
|
|
|
* Handles CallKit's event {@code performSetMutedCallAction}.
|
2017-09-28 21:25:04 +00:00
|
|
|
*
|
|
|
|
* @param {Object} event - The details of the CallKit event
|
2017-10-01 06:35:19 +00:00
|
|
|
* {@code performSetMutedCallAction}.
|
2017-09-28 21:25:04 +00:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
function _onPerformSetMutedCallAction({ callUUID, muted: newValue }) {
|
|
|
|
const { dispatch, getState } = this; // eslint-disable-line no-invalid-this
|
|
|
|
const conference = getCurrentConference(getState);
|
|
|
|
|
|
|
|
if (conference && conference.callUUID === callUUID) {
|
|
|
|
// Break the loop. Audio can be muted from both CallKit and Jitsi Meet.
|
|
|
|
// We must keep them in sync, but at some point the loop needs to be
|
|
|
|
// broken. We are doing it here, on the CallKit handler.
|
|
|
|
const { muted: oldValue } = getState()['features/base/media'].audio;
|
|
|
|
|
|
|
|
if (oldValue !== newValue) {
|
2017-10-09 21:40:38 +00:00
|
|
|
const value = Boolean(newValue);
|
|
|
|
|
2017-10-16 19:58:38 +00:00
|
|
|
sendAnalyticsEvent(`callkit.audio.${value ? 'muted' : 'unmuted'}`);
|
2017-10-09 21:40:38 +00:00
|
|
|
dispatch(setAudioMuted(value));
|
2017-09-28 21:25:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notifies the feature jwt that the action {@link SET_AUDIO_MUTED} is being
|
2017-10-01 06:35:19 +00:00
|
|
|
* dispatched within a specific redux {@code store}.
|
2017-09-28 21:25:04 +00:00
|
|
|
*
|
2017-10-01 06:35:19 +00:00
|
|
|
* @param {Store} store - The redux store in which the specified {@code action}
|
2017-09-28 21:25:04 +00:00
|
|
|
* is being dispatched.
|
|
|
|
* @param {Dispatch} next - The redux dispatch function to dispatch the
|
2017-10-01 06:35:19 +00:00
|
|
|
* specified {@code action} to the specified {@code store}.
|
|
|
|
* @param {Action} action - The redux action {@code SET_AUDIO_MUTED} which is
|
|
|
|
* being dispatched in the specified {@code store}.
|
2017-09-28 21:25:04 +00:00
|
|
|
* @private
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
function _setAudioMuted({ getState }, next, action) {
|
|
|
|
const result = next(action);
|
|
|
|
|
|
|
|
const conference = getCurrentConference(getState);
|
|
|
|
|
|
|
|
if (conference && conference.callUUID) {
|
|
|
|
CallKit.setMuted(conference.callUUID, action.muted);
|
2017-09-08 09:28:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2017-09-28 21:25:04 +00:00
|
|
|
}
|
2017-09-08 09:28:44 +00:00
|
|
|
|
|
|
|
/**
|
2017-09-28 21:25:04 +00:00
|
|
|
* Notifies the feature jwt that the action {@link _SET_CALLKIT_SUBSCRIPTIONS}
|
2017-10-01 06:35:19 +00:00
|
|
|
* is being dispatched within a specific redux {@code store}.
|
2017-09-08 09:28:44 +00:00
|
|
|
*
|
2017-10-01 06:35:19 +00:00
|
|
|
* @param {Store} store - The redux store in which the specified {@code action}
|
2017-09-28 21:25:04 +00:00
|
|
|
* is being dispatched.
|
|
|
|
* @param {Dispatch} next - The redux dispatch function to dispatch the
|
2017-10-01 06:35:19 +00:00
|
|
|
* specified {@code action} to the specified {@code store}.
|
|
|
|
* @param {Action} action - The redux action {@code _SET_CALLKIT_SUBSCRIPTIONS}
|
|
|
|
* which is being dispatched in the specified {@code store}.
|
2017-09-28 21:25:04 +00:00
|
|
|
* @private
|
|
|
|
* @returns {*}
|
2017-09-08 09:28:44 +00:00
|
|
|
*/
|
2017-09-28 21:25:04 +00:00
|
|
|
function _setCallKitSubscriptions({ getState }, next, action) {
|
|
|
|
const { subscriptions } = getState()['features/callkit'];
|
2017-09-08 09:28:44 +00:00
|
|
|
|
2017-09-28 21:25:04 +00:00
|
|
|
if (subscriptions) {
|
|
|
|
for (const subscription of subscriptions) {
|
|
|
|
subscription.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return next(action);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notifies the feature jwt that the action {@link SET_VIDEO_MUTED} is being
|
2017-10-01 06:35:19 +00:00
|
|
|
* dispatched within a specific redux {@code store}.
|
2017-09-28 21:25:04 +00:00
|
|
|
*
|
2017-10-01 06:35:19 +00:00
|
|
|
* @param {Store} store - The redux store in which the specified {@code action}
|
2017-09-28 21:25:04 +00:00
|
|
|
* is being dispatched.
|
|
|
|
* @param {Dispatch} next - The redux dispatch function to dispatch the
|
2017-10-01 06:35:19 +00:00
|
|
|
* specified {@code action} to the specified {@code store}.
|
|
|
|
* @param {Action} action - The redux action {@code SET_VIDEO_MUTED} which is
|
|
|
|
* being dispatched in the specified {@code store}.
|
2017-09-28 21:25:04 +00:00
|
|
|
* @private
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
function _setVideoMuted({ getState }, next, action) {
|
|
|
|
const result = next(action);
|
|
|
|
|
|
|
|
const conference = getCurrentConference(getState);
|
|
|
|
|
|
|
|
if (conference && conference.callUUID) {
|
|
|
|
CallKit.updateCall(
|
|
|
|
conference.callUUID,
|
|
|
|
{ hasVideo: !isVideoMutedByAudioOnly(getState) });
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2017-09-08 09:28:44 +00:00
|
|
|
}
|