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

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