security_section_widget.dart 8.4 KB

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