You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SettingsView.tsx 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. /* eslint-disable lines-around-comment */
  2. import { Link } from '@react-navigation/native';
  3. import _ from 'lodash';
  4. import React, { Component } from 'react';
  5. import { WithTranslation } from 'react-i18next';
  6. import {
  7. Alert,
  8. NativeModules,
  9. Platform,
  10. ScrollView,
  11. Text,
  12. View
  13. } from 'react-native';
  14. import { Divider } from 'react-native-paper';
  15. import { getDefaultURL } from '../../../app/functions.native';
  16. import { IReduxState } from '../../../app/types';
  17. // @ts-ignore
  18. import { Avatar } from '../../../base/avatar';
  19. import { translate } from '../../../base/i18n/functions';
  20. // @ts-ignore
  21. import JitsiScreen from '../../../base/modal/components/JitsiScreen';
  22. import { getLocalParticipant } from '../../../base/participants/functions';
  23. import { connect } from '../../../base/redux/functions';
  24. import { updateSettings } from '../../../base/settings/actions';
  25. import Input from '../../../base/ui/components/native/Input';
  26. import Switch from '../../../base/ui/components/native/Switch';
  27. // @ts-ignore
  28. import { screen } from '../../../mobile/navigation/routes';
  29. // @ts-ignore
  30. import { AVATAR_SIZE } from '../../../welcome/components/styles';
  31. import { isServerURLChangeEnabled, normalizeUserInputURL } from '../../functions.native';
  32. // @ts-ignore
  33. import FormRow from './FormRow';
  34. // @ts-ignore
  35. import FormSectionAccordion from './FormSectionAccordion';
  36. // @ts-ignore
  37. import styles from './styles';
  38. /**
  39. * Application information module.
  40. */
  41. const { AppInfo } = NativeModules;
  42. interface IState {
  43. /**
  44. * State variable for the disable call integration switch.
  45. */
  46. disableCallIntegration: boolean;
  47. /**
  48. * State variable for the disable crash reporting switch.
  49. */
  50. disableCrashReporting: boolean;
  51. /**
  52. * State variable for the disable p2p switch.
  53. */
  54. disableP2P: boolean;
  55. /**
  56. * Whether the self view is disabled or not.
  57. */
  58. disableSelfView: boolean;
  59. /**
  60. * State variable for the display name field.
  61. */
  62. displayName: string;
  63. /**
  64. * State variable for the email field.
  65. */
  66. email: string;
  67. /**
  68. * State variable for the server URL field.
  69. */
  70. serverURL: string;
  71. /**
  72. * State variable for start car mode.
  73. */
  74. startCarMode: boolean;
  75. /**
  76. * State variable for the start with audio muted switch.
  77. */
  78. startWithAudioMuted: boolean;
  79. /**
  80. * State variable for the start with video muted switch.
  81. */
  82. startWithVideoMuted: boolean;
  83. }
  84. /**
  85. * The type of the React {@code Component} props of
  86. * {@link SettingsView}.
  87. */
  88. interface IProps extends WithTranslation {
  89. /**
  90. * The ID of the local participant.
  91. */
  92. _localParticipantId: string;
  93. /**
  94. * The default URL for when there is no custom URL set in the settings.
  95. *
  96. * @protected
  97. */
  98. _serverURL: string;
  99. /**
  100. * Flag indicating if URL can be changed by user.
  101. *
  102. * @protected
  103. */
  104. _serverURLChangeEnabled: boolean;
  105. /**
  106. * The current settings object.
  107. */
  108. _settings: {
  109. disableCallIntegration: boolean;
  110. disableCrashReporting: boolean;
  111. disableP2P: boolean;
  112. disableSelfView: boolean;
  113. displayName: string;
  114. email: string;
  115. serverURL: string;
  116. startCarMode: boolean;
  117. startWithAudioMuted: boolean;
  118. startWithVideoMuted: boolean;
  119. };
  120. /**
  121. * Whether {@link SettingsView} is visible.
  122. *
  123. * @protected
  124. */
  125. _visible: boolean;
  126. /**
  127. * Add bottom padding to the screen.
  128. */
  129. addBottomInset?: boolean;
  130. /**
  131. * Redux store dispatch function.
  132. */
  133. dispatch: Function;
  134. /**
  135. * Default prop for navigating between screen components(React Navigation).
  136. */
  137. navigation: Object;
  138. /**
  139. * Bounce when scrolling.
  140. */
  141. scrollBounces?: boolean;
  142. }
  143. /**
  144. * The native container rendering the app settings page.
  145. */
  146. class SettingsView extends Component<IProps, IState> {
  147. _urlField: Object;
  148. /**
  149. *
  150. * Initializes a new {@code SettingsView} instance.
  151. *
  152. * @inheritdoc
  153. */
  154. constructor(props: IProps) {
  155. super(props);
  156. const {
  157. disableCallIntegration,
  158. disableCrashReporting,
  159. disableP2P,
  160. disableSelfView,
  161. displayName,
  162. email,
  163. serverURL,
  164. startCarMode,
  165. startWithAudioMuted,
  166. startWithVideoMuted
  167. } = props._settings || {};
  168. this.state = {
  169. disableCallIntegration,
  170. disableCrashReporting,
  171. disableP2P,
  172. disableSelfView,
  173. displayName: displayName || '',
  174. email: email || '',
  175. serverURL: serverURL || '',
  176. startCarMode,
  177. startWithAudioMuted,
  178. startWithVideoMuted
  179. };
  180. // Bind event handlers so they are only bound once per instance.
  181. this._onBlurServerURL = this._onBlurServerURL.bind(this);
  182. this._onChangeDisplayName = this._onChangeDisplayName.bind(this);
  183. this._onChangeEmail = this._onChangeEmail.bind(this);
  184. this._onChangeServerURL = this._onChangeServerURL.bind(this);
  185. this._onClose = this._onClose.bind(this);
  186. this._onDisableCallIntegration = this._onDisableCallIntegration.bind(this);
  187. this._onDisableCrashReporting = this._onDisableCrashReporting.bind(this);
  188. this._onDisableP2P = this._onDisableP2P.bind(this);
  189. this._onDisableSelfView = this._onDisableSelfView.bind(this);
  190. this._onStartAudioMutedChange
  191. = this._onStartAudioMutedChange.bind(this);
  192. this._onStartCarmodeInLowBandwidthMode
  193. = this._onStartCarmodeInLowBandwidthMode.bind(this);
  194. this._onStartVideoMutedChange
  195. = this._onStartVideoMutedChange.bind(this);
  196. this._setURLFieldReference = this._setURLFieldReference.bind(this);
  197. this._showURLAlert = this._showURLAlert.bind(this);
  198. }
  199. /**
  200. * Updates and syncs settings.
  201. *
  202. * @inheritdoc
  203. * @returns {void}
  204. */
  205. componentDidUpdate(prevProps: IProps) {
  206. const { _settings } = this.props;
  207. if (!_.isEqual(prevProps._settings, _settings)) {
  208. // @ts-ignore
  209. // eslint-disable-next-line react/no-did-update-set-state
  210. this.setState(_settings);
  211. }
  212. }
  213. /**
  214. * Implements React's {@link Component#render()}, renders the settings page.
  215. *
  216. * @inheritdoc
  217. * @returns {ReactElement}
  218. */
  219. render() {
  220. const {
  221. disableCallIntegration,
  222. disableCrashReporting,
  223. disableP2P,
  224. disableSelfView,
  225. displayName,
  226. email,
  227. serverURL,
  228. startCarMode,
  229. startWithAudioMuted,
  230. startWithVideoMuted
  231. } = this.state;
  232. const {
  233. addBottomInset = false,
  234. scrollBounces = false,
  235. t
  236. } = this.props;
  237. return (
  238. <JitsiScreen
  239. disableForcedKeyboardDismiss = { true }
  240. safeAreaInsets = { [ addBottomInset && 'bottom', 'left', 'right' ].filter(Boolean) }
  241. style = { styles.settingsViewContainer }>
  242. <ScrollView bounces = { scrollBounces }>
  243. <View style = { styles.avatarContainer }>
  244. <Avatar
  245. participantId = { this.props._localParticipantId }
  246. size = { AVATAR_SIZE } />
  247. </View>
  248. <FormSectionAccordion
  249. label = 'settingsView.profileSection'>
  250. <Input
  251. // @ts-ignore
  252. customStyles = {{ container: styles.customContainer }}
  253. label = { t('settingsView.displayName') }
  254. onChange = { this._onChangeDisplayName }
  255. placeholder = { t('settingsView.displayNamePlaceholderText') }
  256. textContentType = { 'name' } // iOS only
  257. value = { displayName } />
  258. <Divider style = { styles.fieldSeparator } />
  259. <Input
  260. // @ts-ignore
  261. autoCapitalize = 'none'
  262. customStyles = {{ container: styles.customContainer }}
  263. keyboardType = { 'email-address' }
  264. label = { t('settingsView.email') }
  265. onChange = { this._onChangeEmail }
  266. placeholder = { t('settingsView.emailPlaceholderText') }
  267. textContentType = { 'emailAddress' } // iOS only
  268. value = { email } />
  269. </FormSectionAccordion>
  270. <FormSectionAccordion
  271. label = 'settingsView.conferenceSection'>
  272. <Input
  273. // @ts-ignore
  274. autoCapitalize = 'none'
  275. customStyles = {{ container: styles.customContainer }}
  276. editable = { this.props._serverURLChangeEnabled }
  277. keyboardType = { 'url' }
  278. label = { t('settingsView.serverURL') }
  279. onBlur = { this._onBlurServerURL }
  280. onChange = { this._onChangeServerURL }
  281. placeholder = { this.props._serverURL }
  282. textContentType = { 'URL' } // iOS only
  283. value = { serverURL } />
  284. <Divider style = { styles.fieldSeparator } />
  285. <FormRow label = 'settingsView.startCarModeInLowBandwidthMode'>
  286. <Switch
  287. checked = { startCarMode }
  288. // @ts-ignore
  289. onChange = { this._onStartCarmodeInLowBandwidthMode } />
  290. </FormRow>
  291. <Divider style = { styles.fieldSeparator } />
  292. <FormRow
  293. label = 'settingsView.startWithAudioMuted'>
  294. <Switch
  295. checked = { startWithAudioMuted }
  296. // @ts-ignore
  297. onChange = { this._onStartAudioMutedChange } />
  298. </FormRow>
  299. <Divider style = { styles.fieldSeparator } />
  300. <FormRow label = 'settingsView.startWithVideoMuted'>
  301. <Switch
  302. checked = { startWithVideoMuted }
  303. // @ts-ignore
  304. onChange = { this._onStartVideoMutedChange } />
  305. </FormRow>
  306. <Divider style = { styles.fieldSeparator } />
  307. <FormRow label = 'videothumbnail.hideSelfView'>
  308. <Switch
  309. checked = { disableSelfView }
  310. // @ts-ignore
  311. onChange = { this._onDisableSelfView } />
  312. </FormRow>
  313. </FormSectionAccordion>
  314. <FormSectionAccordion
  315. label = 'settingsView.links'>
  316. <Link
  317. style = { styles.sectionLink }
  318. // @ts-ignore
  319. to = {{ screen: screen.settings.links.help }}>
  320. { t('settingsView.help') }
  321. </Link>
  322. <Divider style = { styles.fieldSeparator } />
  323. <Link
  324. style = { styles.sectionLink }
  325. // @ts-ignore
  326. to = {{ screen: screen.settings.links.terms }}>
  327. { t('settingsView.terms') }
  328. </Link>
  329. <Divider style = { styles.fieldSeparator } />
  330. <Link
  331. style = { styles.sectionLink }
  332. // @ts-ignore
  333. to = {{ screen: screen.settings.links.privacy }}>
  334. { t('settingsView.privacy') }
  335. </Link>
  336. </FormSectionAccordion>
  337. <FormSectionAccordion
  338. label = 'settingsView.buildInfoSection'>
  339. <FormRow
  340. label = 'settingsView.version'>
  341. <Text style = { styles.text }>
  342. {`${AppInfo.version} build ${AppInfo.buildNumber}`}
  343. </Text>
  344. </FormRow>
  345. </FormSectionAccordion>
  346. <FormSectionAccordion
  347. label = 'settingsView.advanced'>
  348. { Platform.OS === 'android' && (
  349. <>
  350. <FormRow
  351. label = 'settingsView.disableCallIntegration'>
  352. <Switch
  353. checked = { disableCallIntegration }
  354. // @ts-ignore
  355. onChange = { this._onDisableCallIntegration } />
  356. </FormRow>
  357. <Divider style = { styles.fieldSeparator } />
  358. </>
  359. )}
  360. <FormRow
  361. label = 'settingsView.disableP2P'>
  362. <Switch
  363. checked = { disableP2P }
  364. // @ts-ignore
  365. onChange = { this._onDisableP2P } />
  366. </FormRow>
  367. <Divider style = { styles.fieldSeparator } />
  368. {AppInfo.GOOGLE_SERVICES_ENABLED && (
  369. <FormRow
  370. fieldSeparator = { true }
  371. label = 'settingsView.disableCrashReporting'>
  372. <Switch
  373. checked = { disableCrashReporting }
  374. // @ts-ignore
  375. onChange = { this._onDisableCrashReporting } />
  376. </FormRow>
  377. )}
  378. </FormSectionAccordion>
  379. </ScrollView>
  380. </JitsiScreen>
  381. );
  382. }
  383. /**
  384. * Handler the server URL lose focus event. Here we validate the server URL
  385. * and update it to the normalized version, or show an error if incorrect.
  386. *
  387. * @private
  388. * @returns {void}
  389. */
  390. _onBlurServerURL() {
  391. this._processServerURL(false /* hideOnSuccess */);
  392. }
  393. /**
  394. * Handles the display name field value change.
  395. *
  396. * @param {string} displayName - The value typed in the name field.
  397. * @protected
  398. * @returns {void}
  399. */
  400. _onChangeDisplayName(displayName: string) {
  401. this.setState({
  402. displayName
  403. });
  404. this._updateSettings({
  405. displayName
  406. });
  407. }
  408. /**
  409. * Handles the email field value change.
  410. *
  411. * @param {string} email - The value typed in the email field.
  412. * @protected
  413. * @returns {void}
  414. */
  415. _onChangeEmail(email: string) {
  416. this.setState({
  417. email
  418. });
  419. this._updateSettings({
  420. email
  421. });
  422. }
  423. /**
  424. * Handles the server name field value change.
  425. *
  426. * @param {string} serverURL - The server URL typed in the server field.
  427. * @protected
  428. * @returns {void}
  429. */
  430. _onChangeServerURL(serverURL: string) {
  431. this.setState({
  432. serverURL
  433. });
  434. this._updateSettings({
  435. serverURL
  436. });
  437. }
  438. /**
  439. * Handles the disable call integration change event.
  440. *
  441. * @param {boolean} disableCallIntegration - The new value
  442. * option.
  443. * @private
  444. * @returns {void}
  445. */
  446. _onDisableCallIntegration(disableCallIntegration: boolean) {
  447. this.setState({
  448. disableCallIntegration
  449. });
  450. this._updateSettings({
  451. disableCallIntegration
  452. });
  453. }
  454. /**
  455. * Handles the disable P2P change event.
  456. *
  457. * @param {boolean} disableP2P - The new value
  458. * option.
  459. * @private
  460. * @returns {void}
  461. */
  462. _onDisableP2P(disableP2P: boolean) {
  463. this.setState({
  464. disableP2P
  465. });
  466. this._updateSettings({
  467. disableP2P
  468. });
  469. }
  470. /** .
  471. * Handles the disable self view change event.
  472. *
  473. * @param {boolean} disableSelfView - The new value.
  474. * @private
  475. * @returns {void}
  476. */
  477. _onDisableSelfView(disableSelfView: boolean) {
  478. this.setState({
  479. disableSelfView
  480. });
  481. this._updateSettings({
  482. disableSelfView
  483. });
  484. }
  485. /** .
  486. * Handles car mode in low bandwidth mode.
  487. *
  488. * @param {boolean} startCarMode - The new value.
  489. * @private
  490. * @returns {void}
  491. */
  492. _onStartCarmodeInLowBandwidthMode(startCarMode: boolean) {
  493. this.setState({
  494. startCarMode
  495. });
  496. this._updateSettings({
  497. startCarMode
  498. });
  499. }
  500. /**
  501. * Handles the disable crash reporting change event.
  502. *
  503. * @param {boolean} disableCrashReporting - The new value
  504. * option.
  505. * @private
  506. * @returns {void}
  507. */
  508. _onDisableCrashReporting(disableCrashReporting: boolean) {
  509. if (disableCrashReporting) {
  510. this._showCrashReportingDisableAlert();
  511. } else {
  512. this._disableCrashReporting(disableCrashReporting);
  513. }
  514. }
  515. /**
  516. * Callback to be invoked on closing the modal. Also invokes normalizeUserInputURL to validate
  517. * the URL entered by the user.
  518. *
  519. * @returns {boolean} - True if the modal can be closed.
  520. */
  521. _onClose() {
  522. return this._processServerURL(true /* hideOnSuccess */);
  523. }
  524. /**
  525. * Handles the start audio muted change event.
  526. *
  527. * @param {boolean} startWithAudioMuted - The new value for the start audio muted
  528. * option.
  529. * @protected
  530. * @returns {void}
  531. */
  532. _onStartAudioMutedChange(startWithAudioMuted: boolean) {
  533. this.setState({
  534. startWithAudioMuted
  535. });
  536. this._updateSettings({
  537. startWithAudioMuted
  538. });
  539. }
  540. /**
  541. * Handles the start video muted change event.
  542. *
  543. * @param {boolean} startWithVideoMuted - The new value for the start video muted
  544. * option.
  545. * @protected
  546. * @returns {void}
  547. */
  548. _onStartVideoMutedChange(startWithVideoMuted: boolean) {
  549. this.setState({
  550. startWithVideoMuted
  551. });
  552. this._updateSettings({
  553. startWithVideoMuted
  554. });
  555. }
  556. /**
  557. * Processes the server URL. It normalizes it and an error alert is
  558. * displayed in case it's incorrect.
  559. *
  560. * @param {boolean} hideOnSuccess - True if the dialog should be hidden if
  561. * normalization / validation succeeds, false otherwise.
  562. * @private
  563. * @returns {void}
  564. */
  565. _processServerURL(hideOnSuccess: boolean) {
  566. // @ts-ignore
  567. const { serverURL } = this.props._settings;
  568. const normalizedURL = normalizeUserInputURL(serverURL);
  569. if (normalizedURL === null) {
  570. this._showURLAlert();
  571. return false;
  572. }
  573. this._onChangeServerURL(normalizedURL);
  574. return hideOnSuccess;
  575. }
  576. /**
  577. * Stores a reference to the URL field for later use.
  578. *
  579. * @param {Object} component - The field component.
  580. * @protected
  581. * @returns {void}
  582. */
  583. _setURLFieldReference(component: object) {
  584. this._urlField = component;
  585. }
  586. /**
  587. * Shows an alert telling the user that the URL he/she entered was invalid.
  588. *
  589. * @returns {void}
  590. */
  591. _showURLAlert() {
  592. const { t } = this.props;
  593. Alert.alert(
  594. t('settingsView.alertTitle'),
  595. t('settingsView.alertURLText'),
  596. [
  597. {
  598. // @ts-ignore
  599. onPress: () => this._urlField.focus(),
  600. text: t('settingsView.alertOk')
  601. }
  602. ]
  603. );
  604. }
  605. /**
  606. * Shows an alert warning the user about disabling crash reporting.
  607. *
  608. * @returns {void}
  609. */
  610. _showCrashReportingDisableAlert() {
  611. const { t } = this.props;
  612. Alert.alert(
  613. t('settingsView.alertTitle'),
  614. t('settingsView.disableCrashReportingWarning'),
  615. [
  616. {
  617. onPress: () => this._disableCrashReporting(true),
  618. text: t('settingsView.alertOk')
  619. },
  620. {
  621. text: t('settingsView.alertCancel')
  622. }
  623. ]
  624. );
  625. }
  626. /**
  627. * Updates the settings and sets state for disableCrashReporting.
  628. *
  629. * @param {boolean} disableCrashReporting - Whether crash reporting is disabled or not.
  630. * @returns {void}
  631. */
  632. _disableCrashReporting(disableCrashReporting: boolean) {
  633. this.setState({
  634. disableCrashReporting
  635. });
  636. this._updateSettings({
  637. disableCrashReporting
  638. });
  639. }
  640. /**
  641. * Updates the persisted settings on any change.
  642. *
  643. * @param {Object} updateObject - The partial update object for the
  644. * settings.
  645. * @private
  646. * @returns {void}
  647. */
  648. _updateSettings(updateObject: Object) {
  649. const { dispatch } = this.props;
  650. dispatch(updateSettings(updateObject));
  651. }
  652. }
  653. /**
  654. * Maps part of the Redux state to the props of this component.
  655. *
  656. * @param {Object} state - The Redux state.
  657. * @returns {IProps}
  658. */
  659. function _mapStateToProps(state: IReduxState) {
  660. const localParticipant = getLocalParticipant(state);
  661. return {
  662. _localParticipantId: localParticipant?.id,
  663. _serverURL: getDefaultURL(state),
  664. _serverURLChangeEnabled: isServerURLChangeEnabled(state),
  665. _settings: state['features/base/settings'],
  666. _visible: state['features/settings'].visible
  667. };
  668. }
  669. export default translate(connect(_mapStateToProps)(SettingsView));