2022-09-07 08:30:09 +00:00
|
|
|
// @dart=2.9
|
|
|
|
|
2021-06-29 09:48:01 +00:00
|
|
|
import 'dart:async';
|
2021-04-19 09:35:22 +00:00
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
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';
|
2022-06-07 08:28:34 +00:00
|
|
|
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';
|
2022-09-05 10:32:04 +00:00
|
|
|
import 'package:photos/services/local_authentication_service.dart';
|
2021-04-19 09:35:22 +00:00
|
|
|
import 'package:photos/services/user_service.dart';
|
2022-07-01 14:18:05 +00:00
|
|
|
import 'package:photos/ui/account/sessions_page.dart';
|
|
|
|
import 'package:photos/ui/common/loading_widget.dart';
|
2022-10-04 04:16:40 +00:00
|
|
|
import 'package:photos/ui/components/captioned_text_widget.dart';
|
2022-10-05 11:48:56 +00:00
|
|
|
import 'package:photos/ui/components/expandable_menu_item_widget.dart';
|
2022-10-04 04:16:40 +00:00
|
|
|
import 'package:photos/ui/components/menu_item_widget.dart';
|
2022-10-05 15:57:41 +00:00
|
|
|
import 'package:photos/ui/components/toggle_switch_widget.dart';
|
2022-03-08 19:43:51 +00:00
|
|
|
import 'package:photos/ui/settings/common_settings.dart';
|
2021-04-19 09:35:22 +00:00
|
|
|
|
|
|
|
class SecuritySectionWidget extends StatefulWidget {
|
2022-07-04 08:43:01 +00:00
|
|
|
const SecuritySectionWidget({Key key}) : super(key: key);
|
2021-04-19 09:35:22 +00:00
|
|
|
|
|
|
|
@override
|
2022-07-03 09:45:00 +00:00
|
|
|
State<SecuritySectionWidget> createState() => _SecuritySectionWidgetState();
|
2021-04-19 09:35:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class _SecuritySectionWidgetState extends State<SecuritySectionWidget> {
|
|
|
|
final _config = Configuration.instance;
|
|
|
|
|
2021-06-29 09:48:01 +00:00
|
|
|
StreamSubscription<TwoFactorStatusChangeEvent> _twoFactorStatusChangeEvent;
|
|
|
|
|
2021-04-19 09:35:22 +00:00
|
|
|
@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();
|
2021-04-19 09:35:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2022-10-05 11:48:56 +00:00
|
|
|
return ExpandableMenuItemWidget(
|
|
|
|
title: "Security",
|
|
|
|
selectionOptionsWidget: _getSectionOptions(context),
|
|
|
|
leadingIcon: Icons.local_police_outlined,
|
2022-03-08 19:43:51 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget _getSectionOptions(BuildContext context) {
|
2021-04-19 09:35:22 +00:00
|
|
|
final List<Widget> children = [];
|
|
|
|
if (_config.hasConfiguredAccount()) {
|
|
|
|
children.addAll(
|
|
|
|
[
|
2022-10-05 07:52:48 +00:00
|
|
|
sectionOptionSpacing,
|
2022-10-04 15:52:51 +00:00
|
|
|
FutureBuilder(
|
|
|
|
future: UserService.instance.fetchTwoFactorStatus(),
|
|
|
|
builder: (_, snapshot) {
|
|
|
|
return MenuItemWidget(
|
|
|
|
captionedTextWidget: const CaptionedTextWidget(
|
2022-10-05 13:57:32 +00:00
|
|
|
title: "Two-factor",
|
2022-05-17 11:38:21 +00:00
|
|
|
),
|
2022-10-04 15:52:51 +00:00
|
|
|
trailingSwitch: snapshot.hasData
|
2022-10-05 15:57:41 +00:00
|
|
|
? ToggleSwitchWidget(
|
2021-06-29 09:48:01 +00:00
|
|
|
value: snapshot.data,
|
|
|
|
onChanged: (value) async {
|
2022-09-06 08:35:34 +00:00
|
|
|
final hasAuthenticated =
|
2022-09-05 10:32:04 +00:00
|
|
|
await LocalAuthenticationService.instance
|
|
|
|
.requestLocalAuthentication(
|
2022-09-02 13:51:02 +00:00
|
|
|
context,
|
|
|
|
"Please authenticate to configure two-factor authentication",
|
|
|
|
);
|
2022-09-06 08:35:34 +00:00
|
|
|
if (hasAuthenticated) {
|
2022-09-03 03:02:42 +00:00
|
|
|
if (value) {
|
|
|
|
UserService.instance.setupTwoFactor(context);
|
|
|
|
} else {
|
|
|
|
_disableTwoFactor();
|
|
|
|
}
|
2021-06-29 09:48:01 +00:00
|
|
|
}
|
|
|
|
},
|
2022-10-04 15:52:51 +00:00
|
|
|
)
|
|
|
|
: snapshot.hasError
|
|
|
|
? const Icon(Icons.error_outline_outlined)
|
|
|
|
: const EnteLoadingWidget(),
|
|
|
|
);
|
|
|
|
},
|
2022-10-05 07:52:48 +00:00
|
|
|
),
|
|
|
|
sectionOptionSpacing,
|
2021-04-19 09:35:22 +00:00
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
children.addAll([
|
2022-10-04 15:52:51 +00:00
|
|
|
MenuItemWidget(
|
|
|
|
captionedTextWidget: const CaptionedTextWidget(
|
2022-10-05 13:57:32 +00:00
|
|
|
title: "Lockscreen",
|
2022-10-04 15:52:51 +00:00
|
|
|
),
|
2022-10-05 15:57:41 +00:00
|
|
|
trailingSwitch: ToggleSwitchWidget(
|
2022-10-04 15:52:51 +00:00
|
|
|
value: _config.shouldShowLockScreen(),
|
|
|
|
onChanged: (value) async {
|
|
|
|
final hasAuthenticated = await LocalAuthenticationService.instance
|
|
|
|
.requestLocalAuthForLockScreen(
|
|
|
|
context,
|
|
|
|
value,
|
|
|
|
"Please authenticate to change lockscreen setting",
|
|
|
|
"To enable lockscreen, please setup device passcode or screen lock in your system settings.",
|
|
|
|
);
|
|
|
|
if (hasAuthenticated) {
|
|
|
|
setState(() {});
|
|
|
|
}
|
|
|
|
},
|
2021-04-19 09:35:22 +00:00
|
|
|
),
|
|
|
|
),
|
2022-10-05 07:52:48 +00:00
|
|
|
sectionOptionSpacing,
|
2021-04-19 09:35:22 +00:00
|
|
|
]);
|
|
|
|
if (Platform.isAndroid) {
|
2021-11-23 19:40:50 +00:00
|
|
|
children.addAll(
|
|
|
|
[
|
2022-10-04 15:52:51 +00:00
|
|
|
MenuItemWidget(
|
|
|
|
captionedTextWidget: const CaptionedTextWidget(
|
2022-10-05 13:57:32 +00:00
|
|
|
title: "Hide from recents",
|
2022-10-04 15:52:51 +00:00
|
|
|
),
|
2022-10-15 15:33:46 +00:00
|
|
|
trailingSwitch: ToggleSwitchWidget(
|
2022-10-04 15:52:51 +00:00
|
|
|
value: _config.shouldHideFromRecents(),
|
|
|
|
onChanged: (value) async {
|
|
|
|
if (value) {
|
|
|
|
final AlertDialog alert = AlertDialog(
|
|
|
|
title: const 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,
|
2022-06-07 08:28:34 +00:00
|
|
|
),
|
2021-11-23 19:40:50 +00:00
|
|
|
),
|
2022-10-04 15:52:51 +00:00
|
|
|
Padding(padding: EdgeInsets.all(8)),
|
|
|
|
Text(
|
|
|
|
"Are you sure?",
|
|
|
|
style: TextStyle(
|
|
|
|
height: 1.5,
|
2022-06-07 08:28:34 +00:00
|
|
|
),
|
2021-11-23 19:40:50 +00:00
|
|
|
),
|
|
|
|
],
|
2022-10-04 15:52:51 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
actions: [
|
|
|
|
TextButton(
|
|
|
|
child: Text(
|
|
|
|
"No",
|
|
|
|
style: TextStyle(
|
|
|
|
color:
|
|
|
|
Theme.of(context).colorScheme.defaultTextColor,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
onPressed: () {
|
|
|
|
Navigator.of(context, rootNavigator: true)
|
|
|
|
.pop('dialog');
|
2021-11-23 19:40:50 +00:00
|
|
|
},
|
2022-10-04 15:52:51 +00:00
|
|
|
),
|
|
|
|
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(
|
|
|
|
FlutterWindowManager.FLAG_SECURE,
|
|
|
|
);
|
|
|
|
setState(() {});
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
showDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (BuildContext context) {
|
|
|
|
return alert;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
await _config.setShouldHideFromRecents(false);
|
|
|
|
await FlutterWindowManager.clearFlags(
|
|
|
|
FlutterWindowManager.FLAG_SECURE,
|
|
|
|
);
|
|
|
|
setState(() {});
|
|
|
|
}
|
|
|
|
},
|
2021-11-23 19:40:50 +00:00
|
|
|
),
|
2021-04-19 09:35:22 +00:00
|
|
|
),
|
2022-10-05 07:52:48 +00:00
|
|
|
sectionOptionSpacing,
|
2021-11-23 19:40:50 +00:00
|
|
|
],
|
|
|
|
);
|
2021-04-19 09:35:22 +00:00
|
|
|
}
|
2021-11-27 05:39:50 +00:00
|
|
|
children.addAll([
|
2022-10-04 15:52:51 +00:00
|
|
|
MenuItemWidget(
|
|
|
|
captionedTextWidget: const CaptionedTextWidget(
|
2022-10-05 13:57:32 +00:00
|
|
|
title: "Active sessions",
|
2022-10-04 15:52:51 +00:00
|
|
|
),
|
|
|
|
trailingIcon: Icons.chevron_right_outlined,
|
|
|
|
trailingIconIsMuted: true,
|
2021-11-27 05:39:50 +00:00
|
|
|
onTap: () async {
|
2022-09-06 08:35:34 +00:00
|
|
|
final hasAuthenticated = await LocalAuthenticationService.instance
|
2022-09-05 10:32:04 +00:00
|
|
|
.requestLocalAuthentication(
|
2022-09-03 03:02:42 +00:00
|
|
|
context,
|
|
|
|
"Please authenticate to view your active sessions",
|
2021-11-27 05:39:50 +00:00
|
|
|
);
|
2022-09-06 08:35:34 +00:00
|
|
|
if (hasAuthenticated) {
|
2022-09-03 03:02:42 +00:00
|
|
|
Navigator.of(context).push(
|
|
|
|
MaterialPageRoute(
|
|
|
|
builder: (BuildContext context) {
|
|
|
|
return const SessionsPage();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2021-11-27 05:39:50 +00:00
|
|
|
},
|
|
|
|
),
|
2022-10-05 07:52:48 +00:00
|
|
|
sectionOptionSpacing,
|
2021-11-27 05:39:50 +00:00
|
|
|
]);
|
2021-07-28 15:41:45 +00:00
|
|
|
return Column(
|
|
|
|
children: children,
|
2021-04-19 09:35:22 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-06-29 09:58:04 +00:00
|
|
|
void _disableTwoFactor() {
|
2022-08-29 14:43:31 +00:00
|
|
|
final AlertDialog alert = AlertDialog(
|
2022-07-04 06:02:17 +00:00
|
|
|
title: const Text("Disable two-factor"),
|
|
|
|
content: const Text(
|
|
|
|
"Are you sure you want to disable two-factor authentication?",
|
|
|
|
),
|
2021-06-29 09:58:04 +00:00
|
|
|
actions: [
|
|
|
|
TextButton(
|
|
|
|
child: Text(
|
2022-06-18 12:23:51 +00:00
|
|
|
"No",
|
2021-06-29 09:58:04 +00:00
|
|
|
style: TextStyle(
|
2022-07-12 06:30:02 +00:00
|
|
|
color: Theme.of(context).colorScheme.greenAlternative,
|
2021-06-29 09:58:04 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
onPressed: () {
|
|
|
|
Navigator.of(context, rootNavigator: true).pop('dialog');
|
|
|
|
},
|
|
|
|
),
|
|
|
|
TextButton(
|
2022-07-04 06:02:17 +00:00
|
|
|
child: const Text(
|
2022-06-18 12:23:51 +00:00
|
|
|
"Yes",
|
2021-06-29 09:58:04 +00:00
|
|
|
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;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2021-04-19 09:35:22 +00:00
|
|
|
}
|