ente/lib/ui/settings/security_section_widget.dart

320 lines
11 KiB
Dart
Raw Normal View History

2021-06-29 09:48:01 +00:00
import 'dart:async';
import 'dart:io';
import 'package:expandable/expandable.dart';
import 'package:flutter/material.dart';
import 'package:flutter_windowmanager/flutter_windowmanager.dart';
import 'package:photos/core/configuration.dart';
2021-06-29 09:48:01 +00:00
import 'package:photos/core/event_bus.dart';
import 'package:photos/ente_theme_data.dart';
2021-06-29 09:48:01 +00:00
import 'package:photos/events/two_factor_status_change_event.dart';
import 'package:photos/services/user_service.dart';
import 'package:photos/ui/app_lock.dart';
2021-06-29 09:48:01 +00:00
import 'package:photos/ui/loading_widget.dart';
import 'package:photos/ui/sessions_page.dart';
import 'package:photos/ui/settings/common_settings.dart';
import 'package:photos/ui/settings/settings_section_title.dart';
import 'package:photos/ui/settings/settings_text_item.dart';
import 'package:photos/utils/auth_util.dart';
import 'package:photos/utils/toast_util.dart';
class SecuritySectionWidget extends StatefulWidget {
SecuritySectionWidget({Key key}) : super(key: key);
@override
_SecuritySectionWidgetState createState() => _SecuritySectionWidgetState();
}
class _SecuritySectionWidgetState extends State<SecuritySectionWidget> {
2021-11-26 04:48:19 +00:00
static const kAuthToViewSessions =
2022-05-17 11:38:21 +00:00
"Please authenticate to view your active sessions";
2021-11-26 04:48:19 +00:00
final _config = Configuration.instance;
2021-06-29 09:48:01 +00:00
StreamSubscription<TwoFactorStatusChangeEvent> _twoFactorStatusChangeEvent;
@override
void initState() {
super.initState();
2021-06-29 09:48:01 +00:00
_twoFactorStatusChangeEvent =
Bus.instance.on<TwoFactorStatusChangeEvent>().listen((event) async {
if (mounted) {
setState(() {});
}
});
}
@override
void dispose() {
_twoFactorStatusChangeEvent.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return ExpandablePanel(
header: SettingsSectionTitle("Security"),
collapsed: Container(),
expanded: _getSectionOptions(context),
theme: getExpandableTheme(context),
);
}
Widget _getSectionOptions(BuildContext context) {
final List<Widget> children = [];
if (_config.hasConfiguredAccount()) {
children.addAll(
[
Padding(padding: EdgeInsets.all(2)),
2021-07-28 15:41:45 +00:00
SizedBox(
height: 48,
2021-06-29 09:48:01 +00:00
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
2022-05-17 11:38:21 +00:00
Text(
"Two-factor",
style: Theme.of(context).textTheme.subtitle1,
),
2021-06-29 09:48:01 +00:00
FutureBuilder(
future: UserService.instance.fetchTwoFactorStatus(),
builder: (_, snapshot) {
if (snapshot.hasData) {
return Switch.adaptive(
2021-06-29 09:48:01 +00:00
value: snapshot.data,
onChanged: (value) async {
AppLock.of(context).setEnabled(false);
String reason =
2022-05-30 04:50:21 +00:00
"Please authenticate to configure two-factor authentication";
final result = await requestAuthentication(reason);
AppLock.of(context).setEnabled(
2022-06-11 08:23:52 +00:00
Configuration.instance.shouldShowLockScreen(),
);
2021-06-29 11:18:11 +00:00
if (!result) {
2022-06-10 14:29:56 +00:00
showToast(context, reason);
2021-06-29 11:18:11 +00:00
return;
}
2021-06-29 09:48:01 +00:00
if (value) {
UserService.instance.setupTwoFactor(context);
} else {
_disableTwoFactor();
2021-06-29 09:48:01 +00:00
}
},
);
} else if (snapshot.hasError) {
return Icon(
Icons.error_outline,
color: Colors.white.withOpacity(0.8),
);
}
return loadWidget;
},
),
],
),
),
],
);
}
children.addAll([
SectionOptionDivider,
2021-07-28 15:41:45 +00:00
SizedBox(
height: 48,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
2022-05-17 11:38:21 +00:00
Text(
"Lockscreen",
style: Theme.of(context).textTheme.subtitle1,
),
Switch.adaptive(
value: _config.shouldShowLockScreen(),
onChanged: (value) async {
AppLock.of(context).disable();
final result = await requestAuthentication(
2022-06-11 08:23:52 +00:00
"Please authenticate to change lockscreen setting",
);
if (result) {
AppLock.of(context).setEnabled(value);
_config.setShouldShowLockScreen(value);
setState(() {});
} else {
AppLock.of(context)
.setEnabled(_config.shouldShowLockScreen());
}
},
),
],
),
),
]);
if (Platform.isAndroid) {
children.addAll(
[
SectionOptionDivider,
SizedBox(
height: 48,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
2022-06-11 08:23:52 +00:00
Text(
"Hide from recents",
style: Theme.of(context).textTheme.subtitle1,
),
Switch.adaptive(
value: _config.shouldHideFromRecents(),
onChanged: (value) async {
if (value) {
AlertDialog alert = AlertDialog(
title: Text("Hide from recents?"),
content: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: const [
Text(
"Hiding from the task switcher will prevent you from taking screenshots in this app.",
style: TextStyle(
height: 1.5,
),
),
Padding(padding: EdgeInsets.all(8)),
Text(
"Are you sure?",
style: TextStyle(
height: 1.5,
),
),
],
),
),
actions: [
TextButton(
child: Text(
"No",
style: TextStyle(
2022-06-11 08:23:52 +00:00
color: Theme.of(context)
.colorScheme
.defaultTextColor,
),
),
onPressed: () {
Navigator.of(context, rootNavigator: true)
.pop('dialog');
},
),
TextButton(
child: Text(
"Yes",
style: TextStyle(
color: Theme.of(context)
.colorScheme
.defaultTextColor,
),
),
onPressed: () async {
Navigator.of(context, rootNavigator: true)
.pop('dialog');
await _config.setShouldHideFromRecents(true);
await FlutterWindowManager.addFlags(
2022-06-11 08:23:52 +00:00
FlutterWindowManager.FLAG_SECURE,
);
setState(() {});
},
),
],
);
showDialog(
context: context,
builder: (BuildContext context) {
return alert;
},
);
} else {
await _config.setShouldHideFromRecents(false);
await FlutterWindowManager.clearFlags(
2022-06-11 08:23:52 +00:00
FlutterWindowManager.FLAG_SECURE,
);
setState(() {});
}
},
),
],
),
),
],
);
}
children.addAll([
SectionOptionDivider,
GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: () async {
AppLock.of(context).setEnabled(false);
2021-11-26 04:48:19 +00:00
final result = await requestAuthentication(kAuthToViewSessions);
AppLock.of(context)
.setEnabled(Configuration.instance.shouldShowLockScreen());
if (!result) {
2022-06-10 14:29:56 +00:00
showToast(context, kAuthToViewSessions);
return;
}
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) {
return SessionsPage();
},
),
);
},
child: SettingsTextItem(
2022-06-11 08:23:52 +00:00
text: "Active sessions",
icon: Icons.navigate_next,
),
),
]);
2021-07-28 15:41:45 +00:00
return Column(
children: children,
);
}
void _disableTwoFactor() {
AlertDialog alert = AlertDialog(
title: Text("Disable two-factor"),
content:
Text("Are you sure you want to disable two-factor authentication?"),
actions: [
TextButton(
child: Text(
2022-06-18 12:23:51 +00:00
"No",
style: TextStyle(
color: Theme.of(context).buttonColor,
),
),
onPressed: () {
Navigator.of(context, rootNavigator: true).pop('dialog');
},
),
TextButton(
child: Text(
2022-06-18 12:23:51 +00:00
"Yes",
style: TextStyle(
color: Colors.red,
),
),
onPressed: () async {
await UserService.instance.disableTwoFactor(context);
Navigator.of(context, rootNavigator: true).pop('dialog');
},
),
],
);
showDialog(
context: context,
builder: (BuildContext context) {
return alert;
},
);
}
}