Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

SettingsView.tsx 23KB

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