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 23KB

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