2020-05-08 17:04:37 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2020-10-30 13:59:44 +00:00
|
|
|
import { Switch } from 'react-native';
|
2020-05-08 17:04:37 +00:00
|
|
|
import { connect } from 'react-redux';
|
|
|
|
|
|
|
|
import I18n from '../i18n';
|
|
|
|
import { withTheme } from '../theme';
|
2021-10-01 18:12:19 +00:00
|
|
|
import { SWITCH_TRACK_COLOR, themes } from '../constants/colors';
|
2020-05-08 17:04:37 +00:00
|
|
|
import StatusBar from '../containers/StatusBar';
|
2020-10-30 13:59:44 +00:00
|
|
|
import * as List from '../containers/List';
|
2020-05-08 17:04:37 +00:00
|
|
|
import database from '../lib/database';
|
2021-10-01 18:12:19 +00:00
|
|
|
import { changePasscode, checkHasPasscode, supportedBiometryLabel } from '../utils/localAuthentication';
|
2020-05-18 17:35:21 +00:00
|
|
|
import { DEFAULT_AUTO_LOCK } from '../constants/localAuthentication';
|
2020-06-15 14:00:46 +00:00
|
|
|
import SafeAreaView from '../containers/SafeAreaView';
|
2020-07-30 19:51:13 +00:00
|
|
|
import { events, logEvent } from '../utils/log';
|
2020-05-08 17:04:37 +00:00
|
|
|
|
2020-05-11 14:20:14 +00:00
|
|
|
const DEFAULT_BIOMETRY = false;
|
2020-05-08 17:04:37 +00:00
|
|
|
|
|
|
|
class ScreenLockConfigView extends React.Component {
|
2020-07-31 18:30:36 +00:00
|
|
|
static navigationOptions = () => ({
|
2020-06-15 14:00:46 +00:00
|
|
|
title: I18n.t('Screen_lock')
|
2020-07-31 18:30:36 +00:00
|
|
|
});
|
2020-05-08 17:04:37 +00:00
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
theme: PropTypes.string,
|
|
|
|
server: PropTypes.string,
|
|
|
|
Force_Screen_Lock: PropTypes.string,
|
|
|
|
Force_Screen_Lock_After: PropTypes.string
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2020-05-08 17:04:37 +00:00
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
autoLock: false,
|
|
|
|
autoLockTime: null,
|
2020-05-11 14:20:14 +00:00
|
|
|
biometry: DEFAULT_BIOMETRY,
|
2020-05-08 17:04:37 +00:00
|
|
|
biometryLabel: null
|
|
|
|
};
|
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
|
2020-05-22 18:04:43 +00:00
|
|
|
componentWillUnmount() {
|
|
|
|
if (this.observable && this.observable.unsubscribe) {
|
|
|
|
this.observable.unsubscribe();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-18 17:35:21 +00:00
|
|
|
defaultAutoLockOptions = [
|
|
|
|
{
|
|
|
|
title: I18n.t('Local_authentication_auto_lock_60'),
|
|
|
|
value: 60
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: I18n.t('Local_authentication_auto_lock_300'),
|
|
|
|
value: 300
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: I18n.t('Local_authentication_auto_lock_900'),
|
|
|
|
value: 900
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: I18n.t('Local_authentication_auto_lock_1800'),
|
|
|
|
value: 1800
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: I18n.t('Local_authentication_auto_lock_3600'),
|
|
|
|
value: 3600
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2021-10-01 18:12:19 +00:00
|
|
|
init = async () => {
|
2020-05-08 17:04:37 +00:00
|
|
|
const { server } = this.props;
|
|
|
|
const serversDB = database.servers;
|
2021-03-15 20:16:34 +00:00
|
|
|
const serversCollection = serversDB.get('servers');
|
2020-05-08 17:04:37 +00:00
|
|
|
try {
|
|
|
|
this.serverRecord = await serversCollection.find(server);
|
|
|
|
this.setState({
|
|
|
|
autoLock: this.serverRecord?.autoLock,
|
|
|
|
autoLockTime: this.serverRecord?.autoLockTime === null ? DEFAULT_AUTO_LOCK : this.serverRecord?.autoLockTime,
|
|
|
|
biometry: this.serverRecord.biometry === null ? DEFAULT_BIOMETRY : this.serverRecord.biometry
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
const biometryLabel = await supportedBiometryLabel();
|
|
|
|
this.setState({ biometryLabel });
|
2020-05-22 18:04:43 +00:00
|
|
|
|
|
|
|
this.observe();
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2020-05-22 18:04:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We should observe biometry value
|
|
|
|
* because it can be changed by PasscodeChange
|
|
|
|
* when the user set his first passcode
|
2021-10-01 18:12:19 +00:00
|
|
|
*/
|
2020-05-22 18:04:43 +00:00
|
|
|
observe = () => {
|
|
|
|
this.observable = this.serverRecord?.observe()?.subscribe(({ biometry }) => {
|
|
|
|
this.setState({ biometry });
|
|
|
|
});
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2020-05-08 17:04:37 +00:00
|
|
|
|
2021-10-01 18:12:19 +00:00
|
|
|
save = async () => {
|
2020-07-30 19:51:13 +00:00
|
|
|
logEvent(events.SLC_SAVE_SCREEN_LOCK);
|
2020-05-08 17:04:37 +00:00
|
|
|
const { autoLock, autoLockTime, biometry } = this.state;
|
|
|
|
const serversDB = database.servers;
|
2021-10-01 18:12:19 +00:00
|
|
|
await serversDB.action(async () => {
|
|
|
|
await this.serverRecord?.update(record => {
|
2020-05-08 17:04:37 +00:00
|
|
|
record.autoLock = autoLock;
|
|
|
|
record.autoLockTime = autoLockTime === null ? DEFAULT_AUTO_LOCK : autoLockTime;
|
|
|
|
record.biometry = biometry === null ? DEFAULT_BIOMETRY : biometry;
|
|
|
|
});
|
|
|
|
});
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2020-05-08 17:04:37 +00:00
|
|
|
|
2021-10-01 18:12:19 +00:00
|
|
|
changePasscode = async ({ force }) => {
|
2020-07-30 19:51:13 +00:00
|
|
|
logEvent(events.SLC_CHANGE_PASSCODE);
|
2020-05-08 17:04:37 +00:00
|
|
|
await changePasscode({ force });
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2020-05-08 17:04:37 +00:00
|
|
|
|
|
|
|
toggleAutoLock = () => {
|
2020-07-30 19:51:13 +00:00
|
|
|
logEvent(events.SLC_TOGGLE_AUTOLOCK);
|
2021-10-01 18:12:19 +00:00
|
|
|
this.setState(
|
|
|
|
({ autoLock }) => ({ autoLock: !autoLock, autoLockTime: DEFAULT_AUTO_LOCK }),
|
|
|
|
async () => {
|
|
|
|
const { autoLock } = this.state;
|
|
|
|
if (autoLock) {
|
|
|
|
try {
|
|
|
|
await checkHasPasscode({ force: false, serverRecord: this.serverRecord });
|
|
|
|
} catch {
|
|
|
|
this.toggleAutoLock();
|
|
|
|
}
|
2020-05-08 17:04:37 +00:00
|
|
|
}
|
2021-10-01 18:12:19 +00:00
|
|
|
this.save();
|
2020-05-08 17:04:37 +00:00
|
|
|
}
|
2021-10-01 18:12:19 +00:00
|
|
|
);
|
|
|
|
};
|
2020-05-08 17:04:37 +00:00
|
|
|
|
|
|
|
toggleBiometry = () => {
|
2020-07-30 19:51:13 +00:00
|
|
|
logEvent(events.SLC_TOGGLE_BIOMETRY);
|
2021-10-01 18:12:19 +00:00
|
|
|
this.setState(
|
|
|
|
({ biometry }) => ({ biometry: !biometry }),
|
|
|
|
() => this.save()
|
|
|
|
);
|
|
|
|
};
|
2020-05-08 17:04:37 +00:00
|
|
|
|
2021-10-01 18:12:19 +00:00
|
|
|
isSelected = value => {
|
2020-05-08 17:04:37 +00:00
|
|
|
const { autoLockTime } = this.state;
|
|
|
|
return autoLockTime === value;
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2020-05-08 17:04:37 +00:00
|
|
|
|
2021-10-01 18:12:19 +00:00
|
|
|
changeAutoLockTime = autoLockTime => {
|
2020-07-30 19:51:13 +00:00
|
|
|
logEvent(events.SLC_CHANGE_AUTOLOCK_TIME);
|
2020-05-08 17:04:37 +00:00
|
|
|
this.setState({ autoLockTime }, () => this.save());
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2020-05-08 17:04:37 +00:00
|
|
|
|
|
|
|
renderIcon = () => {
|
|
|
|
const { theme } = this.props;
|
2020-10-30 13:59:44 +00:00
|
|
|
return <List.Icon name='check' color={themes[theme].tintColor} />;
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2020-05-08 17:04:37 +00:00
|
|
|
|
|
|
|
renderItem = ({ item }) => {
|
|
|
|
const { title, value, disabled } = item;
|
|
|
|
return (
|
|
|
|
<>
|
2020-10-30 13:59:44 +00:00
|
|
|
<List.Item
|
2020-05-08 17:04:37 +00:00
|
|
|
title={title}
|
|
|
|
onPress={() => this.changeAutoLockTime(value)}
|
|
|
|
right={this.isSelected(value) ? this.renderIcon : null}
|
|
|
|
disabled={disabled}
|
2020-10-30 13:59:44 +00:00
|
|
|
translateTitle={false}
|
2020-05-08 17:04:37 +00:00
|
|
|
/>
|
2020-10-30 13:59:44 +00:00
|
|
|
<List.Separator />
|
2020-05-08 17:04:37 +00:00
|
|
|
</>
|
|
|
|
);
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2020-05-08 17:04:37 +00:00
|
|
|
|
|
|
|
renderAutoLockSwitch = () => {
|
|
|
|
const { autoLock } = this.state;
|
|
|
|
const { Force_Screen_Lock } = this.props;
|
|
|
|
return (
|
2021-10-01 18:12:19 +00:00
|
|
|
<Switch value={autoLock} trackColor={SWITCH_TRACK_COLOR} onValueChange={this.toggleAutoLock} disabled={Force_Screen_Lock} />
|
2020-05-08 17:04:37 +00:00
|
|
|
);
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2020-05-08 17:04:37 +00:00
|
|
|
|
|
|
|
renderBiometrySwitch = () => {
|
|
|
|
const { biometry } = this.state;
|
2021-10-01 18:12:19 +00:00
|
|
|
return <Switch value={biometry} trackColor={SWITCH_TRACK_COLOR} onValueChange={this.toggleBiometry} />;
|
|
|
|
};
|
2020-05-08 17:04:37 +00:00
|
|
|
|
|
|
|
renderAutoLockItems = () => {
|
2020-05-18 17:35:21 +00:00
|
|
|
const { autoLock, autoLockTime } = this.state;
|
2020-10-30 13:59:44 +00:00
|
|
|
const { Force_Screen_Lock_After, Force_Screen_Lock } = this.props;
|
2020-05-08 17:04:37 +00:00
|
|
|
if (!autoLock) {
|
|
|
|
return null;
|
|
|
|
}
|
2020-05-18 17:35:21 +00:00
|
|
|
let items = this.defaultAutoLockOptions;
|
2020-05-08 17:04:37 +00:00
|
|
|
if (Force_Screen_Lock && Force_Screen_Lock_After > 0) {
|
2021-10-01 18:12:19 +00:00
|
|
|
items = [
|
|
|
|
{
|
|
|
|
title: I18n.t('After_seconds_set_by_admin', { seconds: Force_Screen_Lock_After }),
|
|
|
|
value: Force_Screen_Lock_After,
|
|
|
|
disabled: true
|
|
|
|
}
|
|
|
|
];
|
|
|
|
// if Force_Screen_Lock is disabled and autoLockTime is a value that isn't on our defaultOptions we'll show it
|
2020-05-18 17:35:21 +00:00
|
|
|
} else if (Force_Screen_Lock_After === autoLockTime && !items.find(item => item.value === autoLockTime)) {
|
|
|
|
items.push({
|
|
|
|
title: I18n.t('After_seconds_set_by_admin', { seconds: Force_Screen_Lock_After }),
|
|
|
|
value: Force_Screen_Lock_After
|
|
|
|
});
|
2020-05-08 17:04:37 +00:00
|
|
|
}
|
|
|
|
return (
|
2020-10-30 13:59:44 +00:00
|
|
|
<List.Section>
|
|
|
|
<List.Separator />
|
2020-05-08 17:04:37 +00:00
|
|
|
{items.map(item => this.renderItem({ item }))}
|
2020-10-30 13:59:44 +00:00
|
|
|
</List.Section>
|
2020-05-08 17:04:37 +00:00
|
|
|
);
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2020-05-08 17:04:37 +00:00
|
|
|
|
|
|
|
renderBiometry = () => {
|
|
|
|
const { autoLock, biometryLabel } = this.state;
|
|
|
|
if (!autoLock || !biometryLabel) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return (
|
2020-10-30 13:59:44 +00:00
|
|
|
<List.Section>
|
|
|
|
<List.Separator />
|
|
|
|
<List.Item
|
2020-05-08 17:04:37 +00:00
|
|
|
title={I18n.t('Local_authentication_unlock_with_label', { label: biometryLabel })}
|
|
|
|
right={() => this.renderBiometrySwitch()}
|
2020-10-30 13:59:44 +00:00
|
|
|
translateTitle={false}
|
2020-05-08 17:04:37 +00:00
|
|
|
/>
|
2020-10-30 13:59:44 +00:00
|
|
|
<List.Separator />
|
|
|
|
</List.Section>
|
2020-05-08 17:04:37 +00:00
|
|
|
);
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2020-05-08 17:04:37 +00:00
|
|
|
|
|
|
|
render() {
|
|
|
|
const { autoLock } = this.state;
|
|
|
|
return (
|
2020-10-30 13:59:44 +00:00
|
|
|
<SafeAreaView>
|
|
|
|
<StatusBar />
|
|
|
|
<List.Container>
|
|
|
|
<List.Section>
|
|
|
|
<List.Separator />
|
2021-10-01 18:12:19 +00:00
|
|
|
<List.Item title='Local_authentication_unlock_option' right={() => this.renderAutoLockSwitch()} />
|
|
|
|
{autoLock ? (
|
|
|
|
<>
|
|
|
|
<List.Separator />
|
|
|
|
<List.Item title='Local_authentication_change_passcode' onPress={this.changePasscode} showActionIndicator />
|
|
|
|
</>
|
|
|
|
) : null}
|
2020-10-30 13:59:44 +00:00
|
|
|
<List.Separator />
|
|
|
|
<List.Info info='Local_authentication_info' />
|
|
|
|
</List.Section>
|
2020-05-08 17:04:37 +00:00
|
|
|
{this.renderBiometry()}
|
|
|
|
{this.renderAutoLockItems()}
|
2020-10-30 13:59:44 +00:00
|
|
|
</List.Container>
|
2020-05-08 17:04:37 +00:00
|
|
|
</SafeAreaView>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const mapStateToProps = state => ({
|
|
|
|
server: state.server.server,
|
|
|
|
Force_Screen_Lock: state.settings.Force_Screen_Lock,
|
|
|
|
Force_Screen_Lock_After: state.settings.Force_Screen_Lock_After
|
|
|
|
});
|
|
|
|
|
|
|
|
export default connect(mapStateToProps)(withTheme(ScreenLockConfigView));
|