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

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