security_section_widget.dart 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 = _config.hasEnabledTwoFactor();
  71. if (hasAuthenticated) {
  72. if (isTwoFactorEnabled) {
  73. await _disableTwoFactor(completer);
  74. } else {
  75. await UserService.instance
  76. .setupTwoFactor(context, completer);
  77. }
  78. return completer.future;
  79. }
  80. },
  81. ),
  82. ),
  83. sectionOptionSpacing,
  84. ],
  85. );
  86. }
  87. children.addAll([
  88. MenuItemWidget(
  89. captionedTextWidget: const CaptionedTextWidget(
  90. title: "Lockscreen",
  91. ),
  92. trailingSwitch: ToggleSwitchWidget(
  93. value: () => _config.shouldShowLockScreen(),
  94. onChanged: () async {
  95. await LocalAuthenticationService.instance
  96. .requestLocalAuthForLockScreen(
  97. context,
  98. !_config.shouldShowLockScreen(),
  99. "Please authenticate to change lockscreen setting",
  100. "To enable lockscreen, please setup device passcode or screen lock in your system settings.",
  101. );
  102. },
  103. ),
  104. ),
  105. sectionOptionSpacing,
  106. ]);
  107. if (Platform.isAndroid) {
  108. children.addAll(
  109. [
  110. MenuItemWidget(
  111. captionedTextWidget: const CaptionedTextWidget(
  112. title: "Hide from recents",
  113. ),
  114. trailingSwitch: ToggleSwitchWidget(
  115. value: () => _config.shouldHideFromRecents(),
  116. onChanged: _hideFromRecentsOnChanged,
  117. ),
  118. ),
  119. sectionOptionSpacing,
  120. ],
  121. );
  122. }
  123. children.addAll([
  124. MenuItemWidget(
  125. captionedTextWidget: const CaptionedTextWidget(
  126. title: "Active sessions",
  127. ),
  128. pressedColor: getEnteColorScheme(context).fillFaint,
  129. trailingIcon: Icons.chevron_right_outlined,
  130. trailingIconIsMuted: true,
  131. onTap: () async {
  132. final hasAuthenticated = await LocalAuthenticationService.instance
  133. .requestLocalAuthentication(
  134. context,
  135. "Please authenticate to view your active sessions",
  136. );
  137. if (hasAuthenticated) {
  138. unawaited(
  139. Navigator.of(context).push(
  140. MaterialPageRoute(
  141. builder: (BuildContext context) {
  142. return const SessionsPage();
  143. },
  144. ),
  145. ),
  146. );
  147. }
  148. },
  149. ),
  150. sectionOptionSpacing,
  151. ]);
  152. return Column(
  153. children: children,
  154. );
  155. }
  156. Future<void> _disableTwoFactor(Completer completer) async {
  157. final AlertDialog alert = AlertDialog(
  158. title: const Text("Disable two-factor"),
  159. content: const Text(
  160. "Are you sure you want to disable two-factor authentication?",
  161. ),
  162. actions: [
  163. TextButton(
  164. child: Text(
  165. "No",
  166. style: TextStyle(
  167. color: Theme.of(context).colorScheme.greenAlternative,
  168. ),
  169. ),
  170. onPressed: () {
  171. Navigator.of(context, rootNavigator: true).pop('dialog');
  172. completer.complete();
  173. },
  174. ),
  175. TextButton(
  176. child: const Text(
  177. "Yes",
  178. style: TextStyle(
  179. color: Colors.red,
  180. ),
  181. ),
  182. onPressed: () async {
  183. await UserService.instance.disableTwoFactor(context);
  184. Navigator.of(context, rootNavigator: true).pop('dialog');
  185. completer.complete();
  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. }