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

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