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

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