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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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. textContentType = { 'name' } // iOS only
  142. value = { displayName } />
  143. </FormRow>
  144. <FormRow
  145. label = 'settingsView.email'
  146. layout = 'column'>
  147. <TextInput
  148. autoCapitalize = 'none'
  149. autoCorrect = { false }
  150. keyboardType = { 'email-address' }
  151. onChangeText = { this._onChangeEmail }
  152. placeholder = 'email@example.com'
  153. textContentType = { 'emailAddress' } // iOS only
  154. value = { email } />
  155. </FormRow>
  156. <FormSectionHeader
  157. label = 'settingsView.conferenceSection' />
  158. <FormRow
  159. fieldSeparator = { true }
  160. label = 'settingsView.serverURL'
  161. layout = 'column'>
  162. <TextInput
  163. autoCapitalize = 'none'
  164. autoCorrect = { false }
  165. editable = { this.props._serverURLChangeEnabled }
  166. keyboardType = { 'url' }
  167. onBlur = { this._onBlurServerURL }
  168. onChangeText = { this._onChangeServerURL }
  169. placeholder = { this.props._serverURL }
  170. textContentType = { 'URL' } // iOS only
  171. value = { serverURL } />
  172. </FormRow>
  173. <FormRow
  174. fieldSeparator = { true }
  175. label = 'settingsView.startWithAudioMuted'>
  176. <Switch
  177. onValueChange = { this._onStartAudioMutedChange }
  178. value = { startWithAudioMuted } />
  179. </FormRow>
  180. <FormRow label = 'settingsView.startWithVideoMuted'>
  181. <Switch
  182. onValueChange = { this._onStartVideoMutedChange }
  183. value = { startWithVideoMuted } />
  184. </FormRow>
  185. <FormSectionHeader
  186. label = 'settingsView.buildInfoSection' />
  187. <FormRow
  188. label = 'settingsView.version'>
  189. <Text>
  190. {`${AppInfo.version} build ${AppInfo.buildNumber}`}
  191. </Text>
  192. </FormRow>
  193. <FormSectionHeader
  194. label = 'settingsView.advanced' />
  195. {this._renderAdvancedSettings()}
  196. </ScrollView>
  197. </JitsiModal>
  198. );
  199. }
  200. _onBlurServerURL: () => void;
  201. /**
  202. * Handler the server URL lose focus event. Here we validate the server URL
  203. * and update it to the normalized version, or show an error if incorrect.
  204. *
  205. * @private
  206. * @returns {void}
  207. */
  208. _onBlurServerURL() {
  209. this._processServerURL(false /* hideOnSuccess */);
  210. }
  211. /**
  212. * Callback to update the display name.
  213. *
  214. * @param {string} displayName - The new value to set.
  215. * @returns {void}
  216. */
  217. _onChangeDisplayName(displayName) {
  218. super._onChangeDisplayName(displayName);
  219. this.setState({
  220. displayName
  221. });
  222. }
  223. /**
  224. * Callback to update the email.
  225. *
  226. * @param {string} email - The new value to set.
  227. * @returns {void}
  228. */
  229. _onChangeEmail(email) {
  230. super._onChangeEmail(email);
  231. this.setState({
  232. email
  233. });
  234. }
  235. /**
  236. * Callback to update the server URL.
  237. *
  238. * @param {string} serverURL - The new value to set.
  239. * @returns {void}
  240. */
  241. _onChangeServerURL(serverURL) {
  242. super._onChangeServerURL(serverURL);
  243. this.setState({
  244. serverURL
  245. });
  246. }
  247. _onDisableCallIntegration: (boolean) => void;
  248. /**
  249. * Handles the disable call integration change event.
  250. *
  251. * @param {boolean} disableCallIntegration - The new value
  252. * option.
  253. * @private
  254. * @returns {void}
  255. */
  256. _onDisableCallIntegration(disableCallIntegration) {
  257. this._updateSettings({
  258. disableCallIntegration
  259. });
  260. this.setState({
  261. disableCallIntegration
  262. });
  263. }
  264. _onDisableP2P: (boolean) => void;
  265. /**
  266. * Handles the disable P2P change event.
  267. *
  268. * @param {boolean} disableP2P - The new value
  269. * option.
  270. * @private
  271. * @returns {void}
  272. */
  273. _onDisableP2P(disableP2P) {
  274. this._updateSettings({
  275. disableP2P
  276. });
  277. this.setState({
  278. disableP2P
  279. });
  280. }
  281. _onDisableCrashReporting: (boolean) => void;
  282. /**
  283. * Handles the disable crash reporting change event.
  284. *
  285. * @param {boolean} disableCrashReporting - The new value
  286. * option.
  287. * @private
  288. * @returns {void}
  289. */
  290. _onDisableCrashReporting(disableCrashReporting) {
  291. if (disableCrashReporting) {
  292. this._showCrashReportingDisableAlert();
  293. } else {
  294. this._disableCrashReporting(disableCrashReporting);
  295. }
  296. }
  297. _onClose: () => void;
  298. /**
  299. * Callback to be invoked on closing the modal. Also invokes normalizeUserInputURL to validate
  300. * the URL entered by the user.
  301. *
  302. * @returns {boolean} - True if the modal can be closed.
  303. */
  304. _onClose() {
  305. this.setState({ showAdvanced: false });
  306. return this._processServerURL(true /* hideOnSuccess */);
  307. }
  308. _onShowAdvanced: () => void;
  309. /**
  310. * Handles the advanced settings button.
  311. *
  312. * @returns {void}
  313. */
  314. _onShowAdvanced() {
  315. this.setState({ showAdvanced: !this.state.showAdvanced });
  316. }
  317. /**
  318. * Callback to update the start with audio muted value.
  319. *
  320. * @param {boolean} startWithAudioMuted - The new value to set.
  321. * @returns {void}
  322. */
  323. _onStartAudioMutedChange(startWithAudioMuted) {
  324. super._onStartAudioMutedChange(startWithAudioMuted);
  325. this.setState({
  326. startWithAudioMuted
  327. });
  328. }
  329. /**
  330. * Callback to update the start with video muted value.
  331. *
  332. * @param {boolean} startWithVideoMuted - The new value to set.
  333. * @returns {void}
  334. */
  335. _onStartVideoMutedChange(startWithVideoMuted) {
  336. super._onStartVideoMutedChange(startWithVideoMuted);
  337. this.setState({
  338. startWithVideoMuted
  339. });
  340. }
  341. /**
  342. * Processes the server URL. It normalizes it and an error alert is
  343. * displayed in case it's incorrect.
  344. *
  345. * @param {boolean} hideOnSuccess - True if the dialog should be hidden if
  346. * normalization / validation succeeds, false otherwise.
  347. * @private
  348. * @returns {void}
  349. */
  350. _processServerURL(hideOnSuccess: boolean) {
  351. const { serverURL } = this.props._settings;
  352. const normalizedURL = normalizeUserInputURL(serverURL);
  353. if (normalizedURL === null) {
  354. this._showURLAlert();
  355. return false;
  356. }
  357. this._onChangeServerURL(normalizedURL);
  358. return hideOnSuccess;
  359. }
  360. /**
  361. * Renders the advanced settings options.
  362. *
  363. * @private
  364. * @returns {React$Element}
  365. */
  366. _renderAdvancedSettings() {
  367. const { disableCallIntegration, disableP2P, disableCrashReporting, showAdvanced } = this.state;
  368. if (!showAdvanced) {
  369. return (
  370. <FormRow
  371. fieldSeparator = { true }
  372. label = 'settingsView.showAdvanced'>
  373. <Switch
  374. onValueChange = { this._onShowAdvanced }
  375. value = { showAdvanced } />
  376. </FormRow>
  377. );
  378. }
  379. return (
  380. <>
  381. <FormRow
  382. fieldSeparator = { true }
  383. label = 'settingsView.disableCallIntegration'>
  384. <Switch
  385. onValueChange = { this._onDisableCallIntegration }
  386. value = { disableCallIntegration } />
  387. </FormRow>
  388. <FormRow
  389. fieldSeparator = { true }
  390. label = 'settingsView.disableP2P'>
  391. <Switch
  392. onValueChange = { this._onDisableP2P }
  393. value = { disableP2P } />
  394. </FormRow>
  395. {AppInfo.GOOGLE_SERVICES_ENABLED && (
  396. <FormRow
  397. fieldSeparator = { true }
  398. label = 'settingsView.disableCrashReporting'>
  399. <Switch
  400. onValueChange = { this._onDisableCrashReporting }
  401. value = { disableCrashReporting } />
  402. </FormRow>
  403. )}
  404. </>
  405. );
  406. }
  407. _setURLFieldReference: (React$ElementRef<*> | null) => void;
  408. /**
  409. * Stores a reference to the URL field for later use.
  410. *
  411. * @param {Object} component - The field component.
  412. * @protected
  413. * @returns {void}
  414. */
  415. _setURLFieldReference(component) {
  416. this._urlField = component;
  417. }
  418. _showURLAlert: () => void;
  419. /**
  420. * Shows an alert telling the user that the URL he/she entered was invalid.
  421. *
  422. * @returns {void}
  423. */
  424. _showURLAlert() {
  425. const { t } = this.props;
  426. Alert.alert(
  427. t('settingsView.alertTitle'),
  428. t('settingsView.alertURLText'),
  429. [
  430. {
  431. onPress: () => this._urlField.focus(),
  432. text: t('settingsView.alertOk')
  433. }
  434. ]
  435. );
  436. }
  437. /**
  438. * Shows an alert warning the user about disabling crash reporting.
  439. *
  440. * @returns {void}
  441. */
  442. _showCrashReportingDisableAlert() {
  443. const { t } = this.props;
  444. Alert.alert(
  445. t('settingsView.alertTitle'),
  446. t('settingsView.disableCrashReportingWarning'),
  447. [
  448. {
  449. onPress: () => this._disableCrashReporting(true),
  450. text: t('settingsView.alertOk')
  451. },
  452. {
  453. text: t('settingsView.alertCancel')
  454. }
  455. ]
  456. );
  457. }
  458. _updateSettings: (Object) => void;
  459. /**
  460. * Updates the settings and sets state for disableCrashReporting.
  461. *
  462. * @param {boolean} disableCrashReporting - Whether crash reporting is disabled or not.
  463. * @returns {void}
  464. */
  465. _disableCrashReporting(disableCrashReporting) {
  466. this._updateSettings({ disableCrashReporting });
  467. this.setState({ disableCrashReporting });
  468. }
  469. }
  470. /**
  471. * Maps part of the Redux state to the props of this component.
  472. *
  473. * @param {Object} state - The Redux state.
  474. * @returns {Props}
  475. */
  476. function _mapStateToProps(state) {
  477. return {
  478. ..._abstractMapStateToProps(state),
  479. _serverURLChangeEnabled: isServerURLChangeEnabled(state)
  480. };
  481. }
  482. export default translate(connect(_mapStateToProps)(SettingsView));