2018-03-07 00:17:20 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2018-09-25 19:28:42 +00:00
|
|
|
import {
|
2020-06-15 14:00:46 +00:00
|
|
|
View, PermissionsAndroid, Text
|
2018-09-25 19:28:42 +00:00
|
|
|
} from 'react-native';
|
2018-03-07 00:17:20 +00:00
|
|
|
import { AudioRecorder, AudioUtils } from 'react-native-audio';
|
2018-09-27 11:43:19 +00:00
|
|
|
import { BorderlessButton } from 'react-native-gesture-handler';
|
2020-05-08 12:55:26 +00:00
|
|
|
import { activateKeepAwake, deactivateKeepAwake } from 'expo-keep-awake';
|
2020-06-26 20:22:56 +00:00
|
|
|
import * as FileSystem from 'expo-file-system';
|
2019-01-29 19:52:56 +00:00
|
|
|
|
2018-03-07 00:17:20 +00:00
|
|
|
import styles from './styles';
|
2018-06-01 17:38:13 +00:00
|
|
|
import I18n from '../../i18n';
|
2019-01-29 19:52:56 +00:00
|
|
|
import { isIOS, isAndroid } from '../../utils/deviceInfo';
|
2019-03-01 16:49:11 +00:00
|
|
|
import { CustomIcon } from '../../lib/Icons';
|
2019-12-04 16:39:53 +00:00
|
|
|
import { themes } from '../../constants/colors';
|
2020-06-15 14:00:46 +00:00
|
|
|
import SafeAreaView from '../SafeAreaView';
|
2018-03-07 00:17:20 +00:00
|
|
|
|
|
|
|
export const _formatTime = function(seconds) {
|
|
|
|
let minutes = Math.floor(seconds / 60);
|
|
|
|
seconds %= 60;
|
|
|
|
if (minutes < 10) { minutes = `0${ minutes }`; }
|
|
|
|
if (seconds < 10) { seconds = `0${ seconds }`; }
|
|
|
|
return `${ minutes }:${ seconds }`;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default class extends React.PureComponent {
|
|
|
|
static async permission() {
|
2019-01-29 19:52:56 +00:00
|
|
|
if (!isAndroid) {
|
2018-03-07 00:17:20 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const rationale = {
|
2018-06-01 17:38:13 +00:00
|
|
|
title: I18n.t('Microphone_Permission'),
|
|
|
|
message: I18n.t('Microphone_Permission_Message')
|
2018-03-07 00:17:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const result = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.RECORD_AUDIO, rationale);
|
|
|
|
return result === true || result === PermissionsAndroid.RESULTS.GRANTED;
|
|
|
|
}
|
|
|
|
|
2018-06-13 01:29:18 +00:00
|
|
|
static propTypes = {
|
2019-12-04 16:39:53 +00:00
|
|
|
theme: PropTypes.string,
|
2018-06-13 01:29:18 +00:00
|
|
|
onFinish: PropTypes.func.isRequired
|
|
|
|
}
|
|
|
|
|
2018-03-07 00:17:20 +00:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.recordingCanceled = false;
|
2018-05-29 17:10:40 +00:00
|
|
|
this.recording = true;
|
2019-06-28 19:07:20 +00:00
|
|
|
this.name = `${ Date.now() }.aac`;
|
2018-03-07 00:17:20 +00:00
|
|
|
this.state = {
|
|
|
|
currentTime: '00:00'
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2019-06-28 19:07:20 +00:00
|
|
|
const audioPath = `${ AudioUtils.CachesDirectoryPath }/${ this.name }`;
|
2018-03-07 00:17:20 +00:00
|
|
|
|
|
|
|
AudioRecorder.prepareRecordingAtPath(audioPath, {
|
|
|
|
SampleRate: 22050,
|
|
|
|
Channels: 1,
|
|
|
|
AudioQuality: 'Low',
|
2020-04-30 18:54:27 +00:00
|
|
|
AudioEncoding: 'aac',
|
|
|
|
OutputFormat: 'aac_adts'
|
2018-03-07 00:17:20 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
AudioRecorder.onProgress = (data) => {
|
|
|
|
this.setState({
|
|
|
|
currentTime: _formatTime(Math.floor(data.currentTime))
|
|
|
|
});
|
|
|
|
};
|
|
|
|
//
|
|
|
|
AudioRecorder.onFinished = (data) => {
|
2019-01-29 19:52:56 +00:00
|
|
|
if (!this.recordingCanceled && isIOS) {
|
2019-09-24 20:16:59 +00:00
|
|
|
this.finishRecording(data.status === 'OK', data.audioFileURL, data.audioFileSize);
|
2018-03-07 00:17:20 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
AudioRecorder.startRecording();
|
2020-05-08 12:55:26 +00:00
|
|
|
|
|
|
|
activateKeepAwake();
|
2018-03-07 00:17:20 +00:00
|
|
|
}
|
|
|
|
|
2018-05-29 17:10:40 +00:00
|
|
|
componentWillUnmount() {
|
|
|
|
if (this.recording) {
|
|
|
|
this.cancelAudioMessage();
|
|
|
|
}
|
2020-05-08 12:55:26 +00:00
|
|
|
|
|
|
|
deactivateKeepAwake();
|
2018-05-29 17:10:40 +00:00
|
|
|
}
|
|
|
|
|
2019-09-24 20:16:59 +00:00
|
|
|
finishRecording = (didSucceed, filePath, size) => {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { onFinish } = this.props;
|
2018-03-07 00:17:20 +00:00
|
|
|
if (!didSucceed) {
|
2018-09-25 19:28:42 +00:00
|
|
|
return onFinish && onFinish(didSucceed);
|
2018-03-07 00:17:20 +00:00
|
|
|
}
|
|
|
|
const fileInfo = {
|
2019-06-28 19:07:20 +00:00
|
|
|
name: this.name,
|
2019-09-24 20:16:59 +00:00
|
|
|
mime: 'audio/aac',
|
2018-03-07 00:17:20 +00:00
|
|
|
type: 'audio/aac',
|
|
|
|
store: 'Uploads',
|
2019-09-24 20:16:59 +00:00
|
|
|
path: filePath,
|
|
|
|
size
|
2018-03-07 00:17:20 +00:00
|
|
|
};
|
2018-09-25 19:28:42 +00:00
|
|
|
return onFinish && onFinish(fileInfo);
|
2018-03-07 00:17:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
finishAudioMessage = async() => {
|
|
|
|
try {
|
2018-05-29 17:10:40 +00:00
|
|
|
this.recording = false;
|
2020-07-03 14:32:22 +00:00
|
|
|
let filePath = await AudioRecorder.stopRecording();
|
2019-01-29 19:52:56 +00:00
|
|
|
if (isAndroid) {
|
2020-07-03 14:32:22 +00:00
|
|
|
filePath = filePath.startsWith('file://') ? filePath : `file://${ filePath }`;
|
2020-06-26 20:22:56 +00:00
|
|
|
const data = await FileSystem.getInfoAsync(decodeURIComponent(filePath), { size: true });
|
2019-09-24 20:16:59 +00:00
|
|
|
this.finishRecording(true, filePath, data.size);
|
2018-03-07 00:17:20 +00:00
|
|
|
}
|
|
|
|
} catch (err) {
|
2018-09-25 19:28:42 +00:00
|
|
|
this.finishRecording(false);
|
2018-03-07 00:17:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cancelAudioMessage = async() => {
|
2018-05-29 17:10:40 +00:00
|
|
|
this.recording = false;
|
2018-03-07 00:17:20 +00:00
|
|
|
this.recordingCanceled = true;
|
|
|
|
await AudioRecorder.stopRecording();
|
2018-09-25 19:28:42 +00:00
|
|
|
return this.finishRecording(false);
|
2018-03-07 00:17:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { currentTime } = this.state;
|
2019-12-04 16:39:53 +00:00
|
|
|
const { theme } = this.props;
|
2018-09-25 19:28:42 +00:00
|
|
|
|
2018-03-07 00:17:20 +00:00
|
|
|
return (
|
|
|
|
<SafeAreaView
|
2018-05-23 13:39:18 +00:00
|
|
|
testID='messagebox-recording'
|
2020-06-15 14:00:46 +00:00
|
|
|
theme={theme}
|
2019-12-04 16:39:53 +00:00
|
|
|
style={[
|
|
|
|
styles.textBox,
|
|
|
|
{ borderTopColor: themes[theme].borderColor }
|
|
|
|
]}
|
2018-03-07 00:17:20 +00:00
|
|
|
>
|
2019-12-04 16:39:53 +00:00
|
|
|
<View style={[styles.textArea, { backgroundColor: themes[theme].messageboxBackground }]}>
|
2018-09-27 11:43:19 +00:00
|
|
|
<BorderlessButton
|
|
|
|
onPress={this.cancelAudioMessage}
|
2018-06-01 17:38:13 +00:00
|
|
|
accessibilityLabel={I18n.t('Cancel_recording')}
|
2018-03-07 00:17:20 +00:00
|
|
|
accessibilityTraits='button'
|
2018-09-27 11:43:19 +00:00
|
|
|
style={styles.actionButton}
|
|
|
|
>
|
2019-03-01 16:49:11 +00:00
|
|
|
<CustomIcon
|
2018-09-27 11:43:19 +00:00
|
|
|
size={22}
|
2019-12-04 16:39:53 +00:00
|
|
|
color={themes[theme].dangerColor}
|
2020-06-05 13:28:58 +00:00
|
|
|
name='Cross'
|
2018-09-27 11:43:19 +00:00
|
|
|
/>
|
|
|
|
</BorderlessButton>
|
2019-12-04 16:39:53 +00:00
|
|
|
<Text key='currentTime' style={[styles.textBoxInput, { color: themes[theme].titleText }]}>{currentTime}</Text>
|
2018-09-27 11:43:19 +00:00
|
|
|
<BorderlessButton
|
|
|
|
onPress={this.finishAudioMessage}
|
2018-06-01 17:38:13 +00:00
|
|
|
accessibilityLabel={I18n.t('Finish_recording')}
|
2018-03-07 00:17:20 +00:00
|
|
|
accessibilityTraits='button'
|
2018-09-27 11:43:19 +00:00
|
|
|
style={styles.actionButton}
|
|
|
|
>
|
2019-03-01 16:49:11 +00:00
|
|
|
<CustomIcon
|
2018-09-27 11:43:19 +00:00
|
|
|
size={22}
|
2019-12-04 16:39:53 +00:00
|
|
|
color={themes[theme].successColor}
|
2018-09-27 11:43:19 +00:00
|
|
|
name='check'
|
|
|
|
/>
|
|
|
|
</BorderlessButton>
|
2018-03-07 00:17:20 +00:00
|
|
|
</View>
|
2019-02-07 15:48:10 +00:00
|
|
|
</SafeAreaView>
|
|
|
|
);
|
2018-03-07 00:17:20 +00:00
|
|
|
}
|
|
|
|
}
|