2020-05-08 17:04:37 +00:00
|
|
|
import React from 'react';
|
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';
|
2021-12-02 13:21:10 +00:00
|
|
|
import { StackNavigationOptions } from '@react-navigation/stack';
|
|
|
|
import { Subscription } from 'rxjs';
|
2020-05-08 17:04:37 +00:00
|
|
|
|
|
|
|
import I18n from '../i18n';
|
|
|
|
import { withTheme } from '../theme';
|
2021-09-13 20:41:05 +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-09-13 20:41:05 +00:00
|
|
|
import { changePasscode, checkHasPasscode, supportedBiometryLabel } from '../utils/localAuthentication';
|
2022-02-08 16:44:34 +00:00
|
|
|
import { BIOMETRY_ENABLED_KEY, 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';
|
2022-01-12 12:54:04 +00:00
|
|
|
import { TServerModel } from '../definitions/IServer';
|
2022-02-08 16:44:34 +00:00
|
|
|
import userPreferences from '../lib/userPreferences';
|
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
|
|
|
|
2021-12-02 13:21:10 +00:00
|
|
|
interface IItem {
|
|
|
|
title: string;
|
|
|
|
value: number;
|
|
|
|
disabled?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IScreenLockConfigViewProps {
|
|
|
|
theme: string;
|
|
|
|
server: string;
|
|
|
|
Force_Screen_Lock: boolean;
|
|
|
|
Force_Screen_Lock_After: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IScreenLockConfigViewState {
|
2022-01-12 12:54:04 +00:00
|
|
|
autoLock: boolean;
|
2021-12-02 13:21:10 +00:00
|
|
|
autoLockTime?: number | null;
|
2022-02-08 16:44:34 +00:00
|
|
|
biometry: boolean;
|
2022-01-12 12:54:04 +00:00
|
|
|
biometryLabel: string | null;
|
2021-12-02 13:21:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class ScreenLockConfigView extends React.Component<IScreenLockConfigViewProps, IScreenLockConfigViewState> {
|
2022-01-12 12:54:04 +00:00
|
|
|
private serverRecord?: TServerModel;
|
2021-12-02 13:21:10 +00:00
|
|
|
|
|
|
|
private observable?: Subscription;
|
|
|
|
|
|
|
|
static navigationOptions = (): StackNavigationOptions => ({
|
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
|
|
|
|
2021-12-02 13:21:10 +00:00
|
|
|
constructor(props: IScreenLockConfigViewProps) {
|
2020-05-08 17:04:37 +00:00
|
|
|
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-09-13 20:41:05 +00:00
|
|
|
init = async () => {
|
2020-05-08 17:04:37 +00:00
|
|
|
const { server } = this.props;
|
|
|
|
const serversDB = database.servers;
|
2021-02-26 16:25:51 +00:00
|
|
|
const serversCollection = serversDB.get('servers');
|
2022-02-08 16:44:34 +00:00
|
|
|
const hasBiometry = (await userPreferences.getBoolAsync(BIOMETRY_ENABLED_KEY)) ?? DEFAULT_BIOMETRY;
|
2020-05-08 17:04:37 +00:00
|
|
|
try {
|
2022-02-01 13:39:09 +00:00
|
|
|
this.serverRecord = await serversCollection.find(server);
|
2020-05-08 17:04:37 +00:00
|
|
|
this.setState({
|
|
|
|
autoLock: this.serverRecord?.autoLock,
|
|
|
|
autoLockTime: this.serverRecord?.autoLockTime === null ? DEFAULT_AUTO_LOCK : this.serverRecord?.autoLockTime,
|
2022-02-08 16:44:34 +00:00
|
|
|
biometry: hasBiometry
|
2020-05-08 17:04:37 +00:00
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
const biometryLabel = await supportedBiometryLabel();
|
|
|
|
this.setState({ biometryLabel });
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-05-08 17:04:37 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
save = async () => {
|
2020-07-30 19:51:13 +00:00
|
|
|
logEvent(events.SLC_SAVE_SCREEN_LOCK);
|
2022-02-08 16:44:34 +00:00
|
|
|
const { autoLock, autoLockTime } = this.state;
|
2020-05-08 17:04:37 +00:00
|
|
|
const serversDB = database.servers;
|
2021-12-02 13:21:10 +00:00
|
|
|
await serversDB.write(async () => {
|
2021-09-13 20:41:05 +00:00
|
|
|
await this.serverRecord?.update(record => {
|
2020-05-08 17:04:37 +00:00
|
|
|
record.autoLock = autoLock;
|
|
|
|
record.autoLockTime = autoLockTime === null ? DEFAULT_AUTO_LOCK : autoLockTime;
|
|
|
|
});
|
|
|
|
});
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-05-08 17:04:37 +00:00
|
|
|
|
2021-12-02 13:21:10 +00:00
|
|
|
changePasscode = async ({ force }: { force: boolean }) => {
|
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-09-13 20:41:05 +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-09-13 20:41:05 +00:00
|
|
|
this.setState(
|
|
|
|
({ autoLock }) => ({ autoLock: !autoLock, autoLockTime: DEFAULT_AUTO_LOCK }),
|
|
|
|
async () => {
|
|
|
|
const { autoLock } = this.state;
|
|
|
|
if (autoLock) {
|
|
|
|
try {
|
2022-02-08 16:44:34 +00:00
|
|
|
await checkHasPasscode({ force: false });
|
2021-09-13 20:41:05 +00:00
|
|
|
} catch {
|
|
|
|
this.toggleAutoLock();
|
|
|
|
}
|
2020-05-08 17:04:37 +00:00
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
this.save();
|
2020-05-08 17:04:37 +00:00
|
|
|
}
|
2021-09-13 20:41:05 +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-09-13 20:41:05 +00:00
|
|
|
this.setState(
|
|
|
|
({ biometry }) => ({ biometry: !biometry }),
|
2022-02-08 16:44:34 +00:00
|
|
|
async () => {
|
|
|
|
const { biometry } = this.state;
|
|
|
|
await userPreferences.setBoolAsync(BIOMETRY_ENABLED_KEY, biometry);
|
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
);
|
|
|
|
};
|
2020-05-08 17:04:37 +00:00
|
|
|
|
2021-12-02 13:21:10 +00:00
|
|
|
isSelected = (value: number) => {
|
2020-05-08 17:04:37 +00:00
|
|
|
const { autoLockTime } = this.state;
|
|
|
|
return autoLockTime === value;
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-05-08 17:04:37 +00:00
|
|
|
|
2021-12-02 13:21:10 +00:00
|
|
|
changeAutoLockTime = (autoLockTime: number) => {
|
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-09-13 20:41:05 +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-09-13 20:41:05 +00:00
|
|
|
};
|
2020-05-08 17:04:37 +00:00
|
|
|
|
2021-12-02 13:21:10 +00:00
|
|
|
renderItem = ({ item }: { item: IItem }) => {
|
2020-05-08 17:04:37 +00:00
|
|
|
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)}
|
2022-01-17 16:10:39 +00:00
|
|
|
right={() => (this.isSelected(value) ? this.renderIcon() : null)}
|
2020-05-08 17:04:37 +00:00
|
|
|
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-09-13 20:41:05 +00:00
|
|
|
};
|
2020-05-08 17:04:37 +00:00
|
|
|
|
|
|
|
renderAutoLockSwitch = () => {
|
|
|
|
const { autoLock } = this.state;
|
|
|
|
const { Force_Screen_Lock } = this.props;
|
|
|
|
return (
|
2021-09-13 20:41:05 +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-09-13 20:41:05 +00:00
|
|
|
};
|
2020-05-08 17:04:37 +00:00
|
|
|
|
|
|
|
renderBiometrySwitch = () => {
|
|
|
|
const { biometry } = this.state;
|
2021-09-13 20:41:05 +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;
|
|
|
|
}
|
2021-12-02 13:21:10 +00:00
|
|
|
let items: IItem[] = this.defaultAutoLockOptions;
|
2020-05-08 17:04:37 +00:00
|
|
|
if (Force_Screen_Lock && Force_Screen_Lock_After > 0) {
|
2021-09-13 20:41:05 +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-09-13 20:41:05 +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-09-13 20:41:05 +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-09-13 20:41:05 +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>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-02 13:21:10 +00:00
|
|
|
const mapStateToProps = (state: any) => ({
|
2020-05-08 17:04:37 +00:00
|
|
|
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));
|