[RN] Remove denied permission alert from WelcomePage

This commit is contained in:
Lyubo Marinov 2017-08-22 16:36:25 -05:00
parent 0b8c12de0e
commit 24db52ef0f
3 changed files with 110 additions and 64 deletions

View File

@ -83,19 +83,10 @@ export function createLocalTracksA(options = {}) {
},
/* firePermissionPromptIsShownEvent */ false,
store)
.then(localTracks => dispatch(_updateLocalTracks(localTracks)))
.catch(({ gum }) => {
// If permissions are not allowed, alert the user.
if (gum
&& gum.error
&& gum.error.name === 'DOMException'
&& gum.error.message === 'NotAllowedError') {
dispatch({
type: TRACK_PERMISSION_ERROR,
trackType: device
});
}
});
.then(
localTracks => dispatch(_updateLocalTracks(localTracks)),
reason =>
dispatch(_onCreateLocalTracksRejected(reason, device)));
}
};
}
@ -390,6 +381,52 @@ function _getLocalTracksToChange(currentTracks, newTracks) {
};
}
/**
* Implements the <tt>Promise</tt> rejection handler of
* <tt>createLocalTracksA</tt> and <tt>createLocalTracksF</tt>.
*
* @param {Object} reason - The <tt>Promise</tt> rejection reason.
* @param {string} device - The device/<tt>MEDIA_TYPE</tt> associated with the
* rejection.
* @private
* @returns {Function}
*/
function _onCreateLocalTracksRejected({ gum }, device) {
return dispatch => {
// If permissions are not allowed, alert the user.
if (gum) {
const { error } = gum;
if (error) {
// FIXME For whatever reason (which is probably an
// implementation fault), react-native-webrtc will give the
// error in one of the following formats depending on whether it
// is attached to a remote debugger or not. (The remote debugger
// scenario suggests that react-native-webrtc is at fault
// because the remote debugger is Google Chrome and then its
// JavaScript engine will define DOMException. I suspect I wrote
// react-native-webrtc to return the error in the alternative
// format if DOMException is not defined.)
let trackPermissionError;
switch (error.name) {
case 'DOMException':
trackPermissionError = error.message === 'NotAllowedError';
break;
case 'NotAllowedError':
trackPermissionError = error instanceof DOMException;
break;
}
trackPermissionError && dispatch({
type: TRACK_PERMISSION_ERROR,
trackType: device
});
}
}
};
}
/**
* Returns true if the provided JitsiTrack should be rendered as a mirror.
*

View File

@ -1,47 +0,0 @@
import { Alert, Linking, NativeModules } from 'react-native';
import { Platform } from '../../base/react';
/**
* Shows an alert panel which tells the user they have to manually grant some
* permissions by opening Settings. A button which opens Settings is provided.
*
* FIXME: translate.
*
* @param {string} trackType - Type of track that failed with a permission
* error.
* @returns {void}
*/
export function alertPermissionErrorWithSettings(trackType) {
const type = trackType === 'video' ? 'Camera' : 'Microphone';
Alert.alert(
'Permissions Error',
`${type} permission is required, please enable it in Settings.`,
[
{ text: 'Cancel' },
{
onPress: _openSettings,
text: 'Settings'
}
],
{ cancelable: false });
}
/**
* Opens the settings panel for the current platform.
*
* @private
* @returns {void}
*/
function _openSettings() {
switch (Platform.OS) {
case 'android':
NativeModules.AndroidSettings.open();
break;
case 'ios':
Linking.openURL('app-settings:');
break;
}
}

View File

@ -1,10 +1,12 @@
/* @flow */
import { Alert, Linking, NativeModules } from 'react-native';
import { isRoomValid } from '../../base/conference';
import { Platform } from '../../base/react';
import { MiddlewareRegistry } from '../../base/redux';
import { TRACK_PERMISSION_ERROR } from '../../base/tracks';
import { alertPermissionErrorWithSettings } from './functions';
/**
* Middleware that captures track permission errors and alerts the user so they
* can enable the permission themselves.
@ -12,14 +14,68 @@ import { alertPermissionErrorWithSettings } from './functions';
* @param {Store} store - The redux store.
* @returns {Function}
*/
MiddlewareRegistry.register(() => next => action => {
MiddlewareRegistry.register(store => next => action => {
const result = next(action);
switch (action.type) {
case TRACK_PERMISSION_ERROR:
alertPermissionErrorWithSettings(action.trackType);
// XXX We do not currently have user interface outside of a conference
// which the user may tap and cause a permission-related error. If we
// alert whenever we (intend to) ask for a permission, the scenario of
// entering the WelcomePage, being asked for the camera permission, me
// denying it, and being alerted that there is an error is overwhelming
// me.
if (isRoomValid(store.getState()['features/base/conference'].room)) {
_alertPermissionErrorWithSettings(action.trackType);
}
break;
}
return result;
});
/**
* Shows an alert panel which tells the user they have to manually grant some
* permissions by opening Settings. A button which opens Settings is provided.
*
* @param {string} trackType - Type of track that failed with a permission
* error.
* @private
* @returns {void}
*/
function _alertPermissionErrorWithSettings(trackType) {
// TODO i18n
const deviceType = trackType === 'video' ? 'Camera' : 'Microphone';
Alert.alert(
'Permission required',
`${deviceType
} permission is required to participate in conferences with ${
trackType}. Please grant it in Settings.`,
[
{ text: 'Cancel' },
{
onPress: _openSettings,
text: 'Settings'
}
],
{ cancelable: false });
}
/**
* Opens the settings panel for the current platform.
*
* @private
* @returns {void}
*/
function _openSettings() {
switch (Platform.OS) {
case 'android':
NativeModules.AndroidSettings.open();
break;
case 'ios':
Linking.openURL('app-settings:');
break;
}
}