-
Notifications
You must be signed in to change notification settings - Fork 320
/
main.dart
210 lines (187 loc) · 5.3 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import 'dart:async';
import 'dart:isolate';
import 'dart:math';
import 'dart:ui';
import 'package:background_locator/background_locator.dart';
import 'package:background_locator/location_dto.dart';
import 'package:background_locator/location_settings.dart';
import 'package:flutter/material.dart';
import 'package:location_permissions/location_permissions.dart';
import 'file_manager.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
ReceivePort port = ReceivePort();
String logStr = '';
bool isRunning;
LocationDto lastLocation;
DateTime lastTimeLocation;
static const String _isolateName = 'LocatorIsolate';
@override
void initState() {
super.initState();
if (IsolateNameServer.lookupPortByName(_isolateName) != null) {
IsolateNameServer.removePortNameMapping(_isolateName);
}
IsolateNameServer.registerPortWithName(port.sendPort, _isolateName);
port.listen(
(dynamic data) async {
await updateUI(data);
},
);
initPlatformState();
}
static double dp(double val, int places) {
double mod = pow(10.0, places);
return ((val * mod).round().toDouble() / mod);
}
static String formatDateLog(DateTime date) {
return date.hour.toString() +
":" +
date.minute.toString() +
":" +
date.second.toString();
}
static String formatLog(LocationDto locationDto) {
return dp(locationDto.latitude, 4).toString() +
" " +
dp(locationDto.longitude, 4).toString();
}
static Future<void> setLog(LocationDto data) async {
final date = DateTime.now();
await FileManager.writeToLogFile(
'${formatDateLog(date)} --> ${formatLog(data)}\n');
}
Future<void> updateUI(LocationDto data) async {
final log = await FileManager.readLogFile();
setState(() {
lastLocation = data;
lastTimeLocation = DateTime.now();
logStr = log;
});
}
Future<void> initPlatformState() async {
print('Initializing...');
await BackgroundLocator.initialize();
logStr = await FileManager.readLogFile();
print('Initialization done');
final _isRunning = await BackgroundLocator.isRegisterLocationUpdate();
setState(() {
isRunning = _isRunning;
});
print('Running ${isRunning.toString()}');
}
static void callback(LocationDto locationDto) async {
print('location in dart: ${locationDto.toString()}');
await setLog(locationDto);
final SendPort send = IsolateNameServer.lookupPortByName(_isolateName);
send?.send(locationDto);
}
static void notificationCallback() {
print('notificationCallback');
}
@override
Widget build(BuildContext context) {
final start = SizedBox(
width: double.maxFinite,
child: RaisedButton(
child: Text('Start'),
onPressed: () {
_checkLocationPermission();
},
),
);
final stop = SizedBox(
width: double.maxFinite,
child: RaisedButton(
child: Text('Stop'),
onPressed: () {
BackgroundLocator.unRegisterLocationUpdate();
setState(() {
isRunning = false;
});
},
),
);
final clear = SizedBox(
width: double.maxFinite,
child: RaisedButton(
child: Text('Clear Log'),
onPressed: () {
FileManager.clearLogFile();
setState(() {
logStr = '';
});
},
),
);
String msgStatus = "-";
if (isRunning != null) {
if (isRunning) {
msgStatus = 'Is running';
} else {
msgStatus = 'Is not running';
}
}
final status = Text("Status: $msgStatus");
final log = Text(
logStr,
);
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Flutter background Locator'),
),
body: Container(
width: double.maxFinite,
padding: const EdgeInsets.all(22),
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[start, stop, clear, status, log],
),
),
),
),
);
}
void _checkLocationPermission() async {
final access = await LocationPermissions().checkPermissionStatus();
switch (access) {
case PermissionStatus.unknown:
case PermissionStatus.denied:
case PermissionStatus.restricted:
final permission = await LocationPermissions().requestPermissions(
permissionLevel: LocationPermissionLevel.locationAlways,
);
if (permission == PermissionStatus.granted) {
_startLocator();
} else {
// show error
}
break;
case PermissionStatus.granted:
_startLocator();
break;
}
}
void _startLocator() {
BackgroundLocator.registerLocationUpdate(
callback,
androidNotificationCallback: notificationCallback,
settings: LocationSettings(
notificationTitle: "Start Location Tracking example",
notificationMsg: "Track location in background exapmle",
wakeLockTime: 20,
autoStop: false,
interval: 5
),
);
setState(() {
isRunning = true;
});
}
}