security_section_widget.dart 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. import 'dart:async';
  2. import 'dart:io';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter_windowmanager/flutter_windowmanager.dart';
  5. import 'package:photos/core/configuration.dart';
  6. import 'package:photos/core/event_bus.dart';
  7. import 'package:photos/ente_theme_data.dart';
  8. import 'package:photos/events/two_factor_status_change_event.dart';
  9. import 'package:photos/services/local_authentication_service.dart';
  10. import 'package:photos/services/user_service.dart';
  11. import 'package:photos/theme/ente_theme.dart';
  12. import 'package:photos/ui/account/sessions_page.dart';
  13. import 'package:photos/ui/components/captioned_text_widget.dart';
  14. import 'package:photos/ui/components/expandable_menu_item_widget.dart';
  15. import 'package:photos/ui/components/menu_item_widget.dart';
  16. import 'package:photos/ui/components/toggle_switch_widget.dart';
  17. import 'package:photos/ui/settings/common_settings.dart';
  18. class SecuritySectionWidget extends StatefulWidget {
  19. const SecuritySectionWidget({Key? key}) : super(key: key);
  20. @override
  21. State<SecuritySectionWidget> createState() => _SecuritySectionWidgetState();
  22. }
  23. class _SecuritySectionWidgetState extends State<SecuritySectionWidget> {
  24. final _config = Configuration.instance;
  25. late StreamSubscription<TwoFactorStatusChangeEvent>
  26. _twoFactorStatusChangeEvent;
  27. @override
  28. void initState() {
  29. super.initState();
  30. _twoFactorStatusChangeEvent =
  31. Bus.instance.on<TwoFactorStatusChangeEvent>().listen((event) async {
  32. if (mounted) {
  33. setState(() {});
  34. }
  35. });
  36. }
  37. @override
  38. void dispose() {
  39. _twoFactorStatusChangeEvent.cancel();
  40. super.dispose();
  41. }
  42. @override
  43. Widget build(BuildContext context) {
  44. return ExpandableMenuItemWidget(
  45. title: "Security",
  46. selectionOptionsWidget: _getSectionOptions(context),
  47. leadingIcon: Icons.local_police_outlined,
  48. );
  49. }
  50. Widget _getSectionOptions(BuildContext context) {
  51. final Completer completer = Completer();
  52. final List<Widget> children = [];
  53. if (_config.hasConfiguredAccount()) {
  54. children.addAll(
  55. [
  56. sectionOptionSpacing,
  57. MenuItemWidget(
  58. captionedTextWidget: const CaptionedTextWidget(
  59. title: "Two-factor",
  60. ),
  61. trailingWidget: ToggleSwitchWidget(
  62. value: () => UserService.instance.hasEnabledTwoFactor(),
  63. onChanged: () async {
  64. final hasAuthenticated = await LocalAuthenticationService
  65. .instance
  66. .requestLocalAuthentication(
  67. context,
  68. "Please authenticate to configure two-factor authentication",
  69. );
  70. final isTwoFactorEnabled =
  71. UserService.instance.hasEnabledTwoFactor();
  72. if (hasAuthenticated) {
  73. if (isTwoFactorEnabled) {
  74. await _disableTwoFactor();
  75. completer.isCompleted ? null : completer.complete();
  76. } else {
  77. await UserService.instance
  78. .setupTwoFactor(context, completer);
  79. }
  80. return completer.future;
  81. }
  82. },
  83. ),
  84. ),
  85. sectionOptionSpacing,
  86. ],
  87. );
  88. }
  89. children.addAll([
  90. MenuItemWidget(
  91. captionedTextWidget: const CaptionedTextWidget(
  92. title: "Lockscreen",
  93. ),
  94. trailingWidget: ToggleSwitchWidget(
  95. value: () => _config.shouldShowLockScreen(),
  96. onChanged: () async {
  97. await LocalAuthenticationService.instance
  98. .requestLocalAuthForLockScreen(
  99. context,
  100. !_config.shouldShowLockScreen(),
  101. "Please authenticate to change lockscreen setting",
  102. "To enable lockscreen, please setup device passcode or screen lock in your system settings.",
  103. );
  104. },
  105. ),
  106. ),
  107. sectionOptionSpacing,
  108. ]);
  109. if (Platform.isAndroid) {
  110. children.addAll(
  111. [
  112. MenuItemWidget(
  113. captionedTextWidget: const CaptionedTextWidget(
  114. title: "Hide from recents",
  115. ),
  116. trailingWidget: ToggleSwitchWidget(
  117. value: () => _config.shouldHideFromRecents(),
  118. onChanged: _hideFromRecentsOnChanged,
  119. ),
  120. ),
  121. sectionOptionSpacing,
  122. ],
  123. );
  124. }
  125. children.addAll([
  126. MenuItemWidget(
  127. captionedTextWidget: const CaptionedTextWidget(
  128. title: "View active sessions",
  129. ),
  130. pressedColor: getEnteColorScheme(context).fillFaint,
  131. trailingIcon: Icons.chevron_right_outlined,
  132. trailingIconIsMuted: true,
  133. onTap: () async {
  134. final hasAuthenticated = await LocalAuthenticationService.instance
  135. .requestLocalAuthentication(
  136. context,
  137. "Please authenticate to view your active sessions",
  138. );
  139. if (hasAuthenticated) {
  140. unawaited(
  141. Navigator.of(context).push(
  142. MaterialPageRoute(
  143. builder: (BuildContext context) {
  144. return const SessionsPage();
  145. },
  146. ),
  147. ),
  148. );
  149. }
  150. },
  151. ),
  152. sectionOptionSpacing,
  153. ]);
  154. return Column(
  155. children: children,
  156. );
  157. }
  158. Future<void> _disableTwoFactor() async {
  159. final AlertDialog alert = AlertDialog(
  160. title: const Text("Disable two-factor"),
  161. content: const Text(
  162. "Are you sure you want to disable two-factor authentication?",
  163. ),
  164. actions: [
  165. TextButton(
  166. child: Text(
  167. "No",
  168. style: TextStyle(
  169. color: Theme.of(context).colorScheme.greenAlternative,
  170. ),
  171. ),
  172. onPressed: () {
  173. Navigator.of(context, rootNavigator: true).pop('dialog');
  174. },
  175. ),
  176. TextButton(
  177. child: const Text(
  178. "Yes",
  179. style: TextStyle(
  180. color: Colors.red,
  181. ),
  182. ),
  183. onPressed: () async {
  184. await UserService.instance.disableTwoFactor(context);
  185. Navigator.of(context, rootNavigator: true).pop('dialog');
  186. },
  187. ),
  188. ],
  189. );
  190. await showDialog(
  191. context: context,
  192. builder: (BuildContext context) {
  193. return alert;
  194. },
  195. );
  196. }
  197. Future<void> _hideFromRecentsOnChanged() async {
  198. if (!_config.shouldHideFromRecents()) {
  199. final AlertDialog alert = AlertDialog(
  200. title: const Text("Hide from recents?"),
  201. content: SingleChildScrollView(
  202. child: Column(
  203. mainAxisAlignment: MainAxisAlignment.start,
  204. crossAxisAlignment: CrossAxisAlignment.start,
  205. children: const [
  206. Text(
  207. "Hiding from the task switcher will prevent you from taking screenshots in this app.",
  208. style: TextStyle(
  209. height: 1.5,
  210. ),
  211. ),
  212. Padding(padding: EdgeInsets.all(8)),
  213. Text(
  214. "Are you sure?",
  215. style: TextStyle(
  216. height: 1.5,
  217. ),
  218. ),
  219. ],
  220. ),
  221. ),
  222. actions: [
  223. TextButton(
  224. child: Text(
  225. "No",
  226. style: TextStyle(
  227. color: Theme.of(context).colorScheme.defaultTextColor,
  228. ),
  229. ),
  230. onPressed: () {
  231. Navigator.of(context, rootNavigator: true).pop('dialog');
  232. },
  233. ),
  234. TextButton(
  235. child: Text(
  236. "Yes",
  237. style: TextStyle(
  238. color: Theme.of(context).colorScheme.defaultTextColor,
  239. ),
  240. ),
  241. onPressed: () async {
  242. Navigator.of(context, rootNavigator: true).pop('dialog');
  243. await _config.setShouldHideFromRecents(true);
  244. await FlutterWindowManager.addFlags(
  245. FlutterWindowManager.FLAG_SECURE,
  246. );
  247. setState(() {});
  248. },
  249. ),
  250. ],
  251. );
  252. await showDialog(
  253. context: context,
  254. builder: (BuildContext context) {
  255. return alert;
  256. },
  257. );
  258. } else {
  259. await _config.setShouldHideFromRecents(false);
  260. await FlutterWindowManager.clearFlags(
  261. FlutterWindowManager.FLAG_SECURE,
  262. );
  263. }
  264. }
  265. }