Implement password protection
This commit is contained in:
parent
52a0dd006f
commit
565ddb090f
7 changed files with 209 additions and 10 deletions
|
|
@ -28,7 +28,9 @@
|
|||
"trackingLabel": "Continuous tracking",
|
||||
"motionLabel": "Active movement",
|
||||
"advancedLabel": "Advanced settings",
|
||||
"passwordLabel": "Password",
|
||||
"optimizationMessage": "To ensure reliable tracking, please disable battery optimization for this app.",
|
||||
"passwordError": "Wrong password",
|
||||
"startAction": "Start service",
|
||||
"stopAction": "Stop service",
|
||||
"sosAction": "Send SOS"
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
import 'dart:developer' as developer;
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:traccar_client/password_service.dart';
|
||||
import 'package:traccar_client/preferences.dart';
|
||||
import 'package:flutter_background_geolocation/flutter_background_geolocation.dart' as bg;
|
||||
|
||||
|
|
@ -93,12 +94,14 @@ class _MainScreenState extends State<MainScreen> {
|
|||
contentPadding: EdgeInsets.zero,
|
||||
title: Text(AppLocalizations.of(context)!.trackingLabel),
|
||||
value: trackingEnabled,
|
||||
onChanged: (bool value) {
|
||||
if (value) {
|
||||
bg.BackgroundGeolocation.start();
|
||||
_checkBatteryOptimizations(context);
|
||||
} else {
|
||||
bg.BackgroundGeolocation.stop();
|
||||
onChanged: (bool value) async {
|
||||
if (await PasswordService.authenticate(context) && mounted) {
|
||||
if (value) {
|
||||
bg.BackgroundGeolocation.start();
|
||||
_checkBatteryOptimizations(context);
|
||||
} else {
|
||||
bg.BackgroundGeolocation.stop();
|
||||
}
|
||||
}
|
||||
},
|
||||
),
|
||||
|
|
@ -166,10 +169,12 @@ class _MainScreenState extends State<MainScreen> {
|
|||
children: [
|
||||
FilledButton.tonal(
|
||||
onPressed: () async {
|
||||
await Navigator.push(context, MaterialPageRoute(builder: (_) => const SettingsScreen()));
|
||||
setState(() {
|
||||
stopDetection = Preferences.instance.getBool(Preferences.stopDetection);
|
||||
});
|
||||
if (await PasswordService.authenticate(context) && mounted) {
|
||||
await Navigator.push(context, MaterialPageRoute(builder: (_) => const SettingsScreen()));
|
||||
setState(() {
|
||||
stopDetection = Preferences.instance.getBool(Preferences.stopDetection);
|
||||
});
|
||||
}
|
||||
},
|
||||
child: Text(AppLocalizations.of(context)!.settingsButton),
|
||||
),
|
||||
|
|
|
|||
57
lib/password_service.dart
Normal file
57
lib/password_service.dart
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
import 'package:traccar_client/l10n/app_localizations.dart';
|
||||
|
||||
class PasswordService {
|
||||
static final FlutterSecureStorage _secureStorage = const FlutterSecureStorage();
|
||||
static const String _passwordKey = 'password';
|
||||
|
||||
static Future<bool> authenticate(BuildContext context) async {
|
||||
if (!await _secureStorage.containsKey(key: _passwordKey)) return true;
|
||||
final controller = TextEditingController();
|
||||
bool? result;
|
||||
if (context.mounted) {
|
||||
result = await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
content: TextField(
|
||||
controller: controller,
|
||||
autofocus: true,
|
||||
obscureText: true,
|
||||
decoration: InputDecoration(labelText: AppLocalizations.of(context)!.passwordLabel),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context, false),
|
||||
child: Text(AppLocalizations.of(context)!.cancelButton),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () async {
|
||||
final password = await _secureStorage.read(key: _passwordKey);
|
||||
if (context.mounted) {
|
||||
Navigator.pop(context, password == controller.text);
|
||||
}
|
||||
},
|
||||
child: Text(AppLocalizations.of(context)!.okButton),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
if (result != true && context.mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text(AppLocalizations.of(context)!.passwordError)),
|
||||
);
|
||||
return false;
|
||||
}
|
||||
return result == true;
|
||||
}
|
||||
|
||||
static Future<void> setPassword(String password) async {
|
||||
if (password.isNotEmpty) {
|
||||
await _secureStorage.write(key: _passwordKey, value: password);
|
||||
} else {
|
||||
await _secureStorage.delete(key: _passwordKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -3,6 +3,7 @@ import 'dart:io';
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_background_geolocation/flutter_background_geolocation.dart' as bg;
|
||||
import 'package:traccar_client/password_service.dart';
|
||||
import 'package:traccar_client/qr_code_screen.dart';
|
||||
import 'package:wakelock_partial_android/wakelock_partial_android.dart';
|
||||
|
||||
|
|
@ -83,6 +84,33 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
|||
}
|
||||
}
|
||||
|
||||
Future<void> _changePassword() async {
|
||||
final controller = TextEditingController();
|
||||
final result = await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
content: TextField(
|
||||
controller: controller,
|
||||
decoration: InputDecoration(labelText: AppLocalizations.of(context)!.passwordLabel),
|
||||
obscureText: true,
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context, false),
|
||||
child: Text(AppLocalizations.of(context)!.cancelButton),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context, true),
|
||||
child: Text(AppLocalizations.of(context)!.saveButton),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
if (result == true) {
|
||||
await PasswordService.setPassword(controller.text);
|
||||
}
|
||||
}
|
||||
|
||||
Widget _buildListTile(String title, String key, bool isInt) {
|
||||
String? value;
|
||||
if (isInt) {
|
||||
|
|
@ -201,6 +229,11 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
|||
setState(() {});
|
||||
},
|
||||
),
|
||||
if (advanced)
|
||||
ListTile(
|
||||
title: Text(AppLocalizations.of(context)!.passwordLabel),
|
||||
onTap: _changePassword,
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue