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.js 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. // @flow
  2. import React from 'react';
  3. import { Alert, NativeModules, ScrollView, Text } from 'react-native';
  4. import { Divider, Switch, TextInput, withTheme } from 'react-native-paper';
  5. import { translate } from '../../../base/i18n';
  6. import { JitsiModal } from '../../../base/modal';
  7. import { connect } from '../../../base/redux';
  8. import { SETTINGS_VIEW_ID } from '../../constants';
  9. import { normalizeUserInputURL, isServerURLChangeEnabled } from '../../functions';
  10. import {
  11. AbstractSettingsView,
  12. _mapStateToProps as _abstractMapStateToProps,
  13. type Props as AbstractProps
  14. } from '../AbstractSettingsView';
  15. import FormRow from './FormRow';
  16. import FormSectionAccordion from './FormSectionAccordion';
  17. import styles, { THUMB_COLOR } from './styles';
  18. /**
  19. * Application information module.
  20. */
  21. const { AppInfo } = NativeModules;
  22. type State = {
  23. /**
  24. * State variable for the disable call integration switch.
  25. */
  26. disableCallIntegration: boolean,
  27. /**
  28. * State variable for the disable p2p switch.
  29. */
  30. disableP2P: boolean,
  31. /**
  32. * State variable for the disable crash reporting switch.
  33. */
  34. disableCrashReporting: boolean,
  35. /**
  36. * State variable for the display name field.
  37. */
  38. displayName: string,
  39. /**
  40. * State variable for the email field.
  41. */
  42. email: string,
  43. /**
  44. * State variable for the server URL field.
  45. */
  46. serverURL: string,
  47. /**
  48. * State variable for the start with audio muted switch.
  49. */
  50. startWithAudioMuted: boolean,
  51. /**
  52. * State variable for the start with video muted switch.
  53. */
  54. startWithVideoMuted: boolean
  55. }
  56. /**
  57. * The type of the React {@code Component} props of
  58. * {@link SettingsView}.
  59. */
  60. type Props = AbstractProps & {
  61. /**
  62. * Flag indicating if URL can be changed by user.
  63. *
  64. * @protected
  65. */
  66. _serverURLChangeEnabled: boolean,
  67. /**
  68. * Theme used for styles.
  69. */
  70. theme: Object
  71. }
  72. /**
  73. * The native container rendering the app settings page.
  74. *
  75. * @extends AbstractSettingsView
  76. */
  77. class SettingsView extends AbstractSettingsView<Props, State> {
  78. _urlField: Object;
  79. /**
  80. * Initializes a new {@code SettingsView} instance.
  81. *
  82. * @inheritdoc
  83. */
  84. constructor(props) {
  85. super(props);
  86. const {
  87. disableCallIntegration,
  88. disableCrashReporting,
  89. disableP2P,
  90. displayName,
  91. email,
  92. serverURL,
  93. startWithAudioMuted,
  94. startWithVideoMuted
  95. } = props._settings || {};
  96. this.state = {
  97. disableCallIntegration,
  98. disableCrashReporting,
  99. disableP2P,
  100. displayName,
  101. email,
  102. serverURL,
  103. startWithAudioMuted,
  104. startWithVideoMuted
  105. };
  106. // Bind event handlers so they are only bound once per instance.
  107. this._onBlurServerURL = this._onBlurServerURL.bind(this);
  108. this._onClose = this._onClose.bind(this);
  109. this._onDisableCallIntegration = this._onDisableCallIntegration.bind(this);
  110. this._onDisableCrashReporting = this._onDisableCrashReporting.bind(this);
  111. this._onDisableP2P = this._onDisableP2P.bind(this);
  112. this._setURLFieldReference = this._setURLFieldReference.bind(this);
  113. this._showURLAlert = this._showURLAlert.bind(this);
  114. }
  115. /**
  116. * Implements React's {@link Component#render()}, renders the settings page.
  117. *
  118. * @inheritdoc
  119. * @returns {ReactElement}
  120. */
  121. render() {
  122. const {
  123. disableCallIntegration,
  124. disableCrashReporting,
  125. disableP2P,
  126. displayName,
  127. email,
  128. serverURL,
  129. startWithAudioMuted,
  130. startWithVideoMuted
  131. } = this.state;
  132. const { palette } = this.props.theme;
  133. return (
  134. <JitsiModal
  135. headerProps = {{
  136. headerLabelKey: 'settingsView.header'
  137. }}
  138. modalId = { SETTINGS_VIEW_ID }
  139. onClose = { this._onClose }
  140. showHeaderWithNavigation = { true }>
  141. <ScrollView>
  142. <FormSectionAccordion
  143. accordion = { false }
  144. expandable = { false }
  145. label = 'settingsView.profileSection'>
  146. <TextInput
  147. autoCorrect = { false }
  148. label = { this.props.t('settingsView.displayName') }
  149. mode = 'outlined'
  150. onChangeText = { this._onChangeDisplayName }
  151. placeholder = 'John Doe'
  152. style = { styles.textInputContainer }
  153. textContentType = { 'name' } // iOS only
  154. theme = {{
  155. colors: {
  156. primary: palette.action01Active,
  157. underlineColor: 'transparent'
  158. }
  159. }}
  160. value = { displayName } />
  161. <Divider style = { styles.fieldSeparator } />
  162. <TextInput
  163. autoCapitalize = 'none'
  164. autoCorrect = { false }
  165. keyboardType = { 'email-address' }
  166. label = { this.props.t('settingsView.email') }
  167. mode = 'outlined'
  168. onChangeText = { this._onChangeEmail }
  169. placeholder = 'email@example.com'
  170. style = { styles.textInputContainer }
  171. textContentType = { 'emailAddress' } // iOS only
  172. theme = {{
  173. colors: {
  174. primary: palette.action01Active,
  175. underlineColor: 'transparent'
  176. }
  177. }}
  178. value = { email } />
  179. </FormSectionAccordion>
  180. <FormSectionAccordion
  181. accordion = { false }
  182. expandable = { false }
  183. label = 'settingsView.conferenceSection'>
  184. <TextInput
  185. autoCapitalize = 'none'
  186. autoCorrect = { false }
  187. editable = { this.props._serverURLChangeEnabled }
  188. keyboardType = { 'url' }
  189. label = { this.props.t('settingsView.serverURL') }
  190. mode = 'outlined'
  191. onBlur = { this._onBlurServerURL }
  192. onChangeText = { this._onChangeServerURL }
  193. placeholder = { this.props._serverURL }
  194. style = { styles.textInputContainer }
  195. textContentType = { 'URL' } // iOS only
  196. theme = {{
  197. colors: {
  198. primary: palette.action01Active,
  199. underlineColor: 'transparent'
  200. }
  201. }}
  202. value = { serverURL } />
  203. <Divider style = { styles.fieldSeparator } />
  204. <FormRow
  205. label = 'settingsView.startWithAudioMuted'>
  206. <Switch
  207. onValueChange = { this._onStartAudioMutedChange }
  208. thumbColor = { THUMB_COLOR }
  209. trackColor = {{ true: palette.action01Active }}
  210. value = { startWithAudioMuted } />
  211. </FormRow>
  212. <Divider style = { styles.fieldSeparator } />
  213. <FormRow label = 'settingsView.startWithVideoMuted'>
  214. <Switch
  215. onValueChange = { this._onStartVideoMutedChange }
  216. thumbColor = { THUMB_COLOR }
  217. trackColor = {{ true: palette.action01Active }}
  218. value = { startWithVideoMuted } />
  219. </FormRow>
  220. </FormSectionAccordion>
  221. <FormSectionAccordion
  222. accordion = { false }
  223. expandable = { false }
  224. label = 'settingsView.buildInfoSection'>
  225. <FormRow
  226. label = 'settingsView.version'>
  227. <Text>
  228. {`${AppInfo.version} build ${AppInfo.buildNumber}`}
  229. </Text>
  230. </FormRow>
  231. </FormSectionAccordion>
  232. <FormSectionAccordion
  233. accordion = { true }
  234. expandable = { true }
  235. label = 'settingsView.advanced'>
  236. <FormRow
  237. label = 'settingsView.disableCallIntegration'>
  238. <Switch
  239. onValueChange = { this._onDisableCallIntegration }
  240. thumbColor = { THUMB_COLOR }
  241. trackColor = {{ true: palette.action01Active }}
  242. value = { disableCallIntegration } />
  243. </FormRow>
  244. <Divider style = { styles.fieldSeparator } />
  245. <FormRow
  246. label = 'settingsView.disableP2P'>
  247. <Switch
  248. onValueChange = { this._onDisableP2P }
  249. thumbColor = { THUMB_COLOR }
  250. trackColor = {{ true: palette.action01Active }}
  251. value = { disableP2P } />
  252. </FormRow>
  253. <Divider style = { styles.fieldSeparator } />
  254. {AppInfo.GOOGLE_SERVICES_ENABLED && (
  255. <FormRow
  256. fieldSeparator = { true }
  257. label = 'settingsView.disableCrashReporting'>
  258. <Switch
  259. onValueChange = { this._onDisableCrashReporting }
  260. thumbColor = { THUMB_COLOR }
  261. trackColor = {{ true: palette.action01Active }}
  262. value = { disableCrashReporting } />
  263. </FormRow>
  264. )}
  265. </FormSectionAccordion>
  266. </ScrollView>
  267. </JitsiModal>
  268. );
  269. }
  270. _onBlurServerURL: () => void;
  271. /**
  272. * Handler the server URL lose focus event. Here we validate the server URL
  273. * and update it to the normalized version, or show an error if incorrect.
  274. *
  275. * @private
  276. * @returns {void}
  277. */
  278. _onBlurServerURL() {
  279. this._processServerURL(false /* hideOnSuccess */);
  280. }
  281. /**
  282. * Callback to update the display name.
  283. *
  284. * @param {string} displayName - The new value to set.
  285. * @returns {void}
  286. */
  287. _onChangeDisplayName(displayName) {
  288. super._onChangeDisplayName(displayName);
  289. this.setState({
  290. displayName
  291. });
  292. }
  293. /**
  294. * Callback to update the email.
  295. *
  296. * @param {string} email - The new value to set.
  297. * @returns {void}
  298. */
  299. _onChangeEmail(email) {
  300. super._onChangeEmail(email);
  301. this.setState({
  302. email
  303. });
  304. }
  305. /**
  306. * Callback to update the server URL.
  307. *
  308. * @param {string} serverURL - The new value to set.
  309. * @returns {void}
  310. */
  311. _onChangeServerURL(serverURL) {
  312. super._onChangeServerURL(serverURL);
  313. this.setState({
  314. serverURL
  315. });
  316. }
  317. _onDisableCallIntegration: (boolean) => void;
  318. /**
  319. * Handles the disable call integration change event.
  320. *
  321. * @param {boolean} disableCallIntegration - The new value
  322. * option.
  323. * @private
  324. * @returns {void}
  325. */
  326. _onDisableCallIntegration(disableCallIntegration) {
  327. this._updateSettings({
  328. disableCallIntegration
  329. });
  330. this.setState({
  331. disableCallIntegration
  332. });
  333. }
  334. _onDisableP2P: (boolean) => void;
  335. /**
  336. * Handles the disable P2P change event.
  337. *
  338. * @param {boolean} disableP2P - The new value
  339. * option.
  340. * @private
  341. * @returns {void}
  342. */
  343. _onDisableP2P(disableP2P) {
  344. this._updateSettings({
  345. disableP2P
  346. });
  347. this.setState({
  348. disableP2P
  349. });
  350. }
  351. _onDisableCrashReporting: (boolean) => void;
  352. /**
  353. * Handles the disable crash reporting change event.
  354. *
  355. * @param {boolean} disableCrashReporting - The new value
  356. * option.
  357. * @private
  358. * @returns {void}
  359. */
  360. _onDisableCrashReporting(disableCrashReporting) {
  361. if (disableCrashReporting) {
  362. this._showCrashReportingDisableAlert();
  363. } else {
  364. this._disableCrashReporting(disableCrashReporting);
  365. }
  366. }
  367. _onClose: () => void;
  368. /**
  369. * Callback to be invoked on closing the modal. Also invokes normalizeUserInputURL to validate
  370. * the URL entered by the user.
  371. *
  372. * @returns {boolean} - True if the modal can be closed.
  373. */
  374. _onClose() {
  375. return this._processServerURL(true /* hideOnSuccess */);
  376. }
  377. /**
  378. * Callback to update the start with audio muted value.
  379. *
  380. * @param {boolean} startWithAudioMuted - The new value to set.
  381. * @returns {void}
  382. */
  383. _onStartAudioMutedChange(startWithAudioMuted) {
  384. super._onStartAudioMutedChange(startWithAudioMuted);
  385. this.setState({
  386. startWithAudioMuted
  387. });
  388. }
  389. /**
  390. * Callback to update the start with video muted value.
  391. *
  392. * @param {boolean} startWithVideoMuted - The new value to set.
  393. * @returns {void}
  394. */
  395. _onStartVideoMutedChange(startWithVideoMuted) {
  396. super._onStartVideoMutedChange(startWithVideoMuted);
  397. this.setState({
  398. startWithVideoMuted
  399. });
  400. }
  401. /**
  402. * Processes the server URL. It normalizes it and an error alert is
  403. * displayed in case it's incorrect.
  404. *
  405. * @param {boolean} hideOnSuccess - True if the dialog should be hidden if
  406. * normalization / validation succeeds, false otherwise.
  407. * @private
  408. * @returns {void}
  409. */
  410. _processServerURL(hideOnSuccess: boolean) {
  411. const { serverURL } = this.props._settings;
  412. const normalizedURL = normalizeUserInputURL(serverURL);
  413. if (normalizedURL === null) {
  414. this._showURLAlert();
  415. return false;
  416. }
  417. this._onChangeServerURL(normalizedURL);
  418. return hideOnSuccess;
  419. }
  420. _setURLFieldReference: (React$ElementRef<*> | null) => void;
  421. /**
  422. * Stores a reference to the URL field for later use.
  423. *
  424. * @param {Object} component - The field component.
  425. * @protected
  426. * @returns {void}
  427. */
  428. _setURLFieldReference(component) {
  429. this._urlField = component;
  430. }
  431. _showURLAlert: () => void;
  432. /**
  433. * Shows an alert telling the user that the URL he/she entered was invalid.
  434. *
  435. * @returns {void}
  436. */
  437. _showURLAlert() {
  438. const { t } = this.props;
  439. Alert.alert(
  440. t('settingsView.alertTitle'),
  441. t('settingsView.alertURLText'),
  442. [
  443. {
  444. onPress: () => this._urlField.focus(),
  445. text: t('settingsView.alertOk')
  446. }
  447. ]
  448. );
  449. }
  450. /**
  451. * Shows an alert warning the user about disabling crash reporting.
  452. *
  453. * @returns {void}
  454. */
  455. _showCrashReportingDisableAlert() {
  456. const { t } = this.props;
  457. Alert.alert(
  458. t('settingsView.alertTitle'),
  459. t('settingsView.disableCrashReportingWarning'),
  460. [
  461. {
  462. onPress: () => this._disableCrashReporting(true),
  463. text: t('settingsView.alertOk')
  464. },
  465. {
  466. text: t('settingsView.alertCancel')
  467. }
  468. ]
  469. );
  470. }
  471. _updateSettings: (Object) => void;
  472. /**
  473. * Updates the settings and sets state for disableCrashReporting.
  474. *
  475. * @param {boolean} disableCrashReporting - Whether crash reporting is disabled or not.
  476. * @returns {void}
  477. */
  478. _disableCrashReporting(disableCrashReporting) {
  479. this._updateSettings({ disableCrashReporting });
  480. this.setState({ disableCrashReporting });
  481. }
  482. }
  483. /**
  484. * Maps part of the Redux state to the props of this component.
  485. *
  486. * @param {Object} state - The Redux state.
  487. * @returns {Props}
  488. */
  489. function _mapStateToProps(state) {
  490. return {
  491. ..._abstractMapStateToProps(state),
  492. _serverURLChangeEnabled: isServerURLChangeEnabled(state)
  493. };
  494. }
  495. export default translate(connect(_mapStateToProps)(withTheme(SettingsView)));