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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
|
/*
* Copyright (C) 2024 Leonard Kugis
* Copyright (C) 2022 E FOUNDATION
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package foundation.e.advancedprivacy.fakelocation.services
import android.app.Service
import android.content.Context
import android.content.Intent
import android.os.CountDownTimer
import android.os.IBinder
import android.util.Log
import foundation.e.advancedprivacy.fakelocation.domain.usecases.FakeLocationModule
import foundation.e.advancedprivacy.domain.entities.FakeLocationCoordinate
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import kotlin.math.sqrt
import kotlin.random.Random
class FakeLocationService : Service() {
enum class Actions {
START_FAKE_LOCATION,
START_ROUTE
}
companion object {
private const val PERIOD_LOCATION_UPDATE = 1000L
private const val PERIOD_UPDATES_SERIE = 2 * 60 * 1000L
private const val PARAM_ALTITUDE = "PARAM_ALTITUDE"
private const val PARAM_SPEED = "PARAM_SPEED"
private const val PARAM_BEARING = "PARAM_BEARING"
private const val PARAM_JITTER = "PARAM_JITTER"
private const val PARAM_JITTER_ALTITUDE = "PARAM_JITTER_ALTITUDE"
private const val PARAM_JITTER_SPEED = "PARAM_JITTER_SPEED"
private const val PARAM_JITTER_BEARING = "PARAM_JITTER_BEARING"
private const val PARAM_LATITUDE = "PARAM_LATITUDE"
private const val PARAM_LONGITUDE = "PARAM_LONGITUDE"
private const val PARAM_ROUTE = "PARAM_ROUTE"
private const val PARAM_ROUTE_LOOP = "PARAM_ROUTE_LOOP"
fun buildFakeLocationIntent(context: Context, altitude: Double, speed: Float, bearing: Float, jitter: Float, jitterAltitude: Double, jitterSpeed: Float, jitterBearing: Float, latitude: Double, longitude: Double): Intent {
return Intent(context, FakeLocationService::class.java).apply {
action = Actions.START_FAKE_LOCATION.name
putExtra(PARAM_ALTITUDE, altitude)
putExtra(PARAM_SPEED, speed)
putExtra(PARAM_BEARING, bearing)
putExtra(PARAM_JITTER, jitter)
putExtra(PARAM_JITTER_ALTITUDE, jitterAltitude)
putExtra(PARAM_JITTER_SPEED, jitterSpeed)
putExtra(PARAM_JITTER_BEARING, jitterBearing)
putExtra(PARAM_LATITUDE, latitude)
putExtra(PARAM_LONGITUDE, longitude)
}
}
fun buildRouteIntent(context: Context, route: List<FakeLocationCoordinate>, loopRoute: Boolean): Intent {
return Intent(context, FakeLocationService::class.java).apply {
action = Actions.START_ROUTE.name
putExtra(PARAM_ROUTE, Gson().toJson(route))
putExtra(PARAM_ROUTE_LOOP, loopRoute)
}
}
fun buildStopIntent(context: Context) = Intent(context, FakeLocationService::class.java)
}
private lateinit var fakeLocationModule: FakeLocationModule
private var cdtFakeLocation: CountDownTimer? = null
private var cdtRoute: CountDownTimer? = null
private var routeTime: Float = 0f
private var altitude: Double? = null
private var speed: Float? = null
private var bearing: Float? = null
private var jitter: Float? = null
private var jitterAltitude: Double? = null
private var jitterSpeed: Float? = null
private var jitterBearing: Float? = null
private var fakeLocation: Pair<Double, Double>? = null
private var route: List<FakeLocationCoordinate>? = null
private var loopRoute: Boolean = false
private var routeReversed: Boolean = false
override fun onCreate() {
super.onCreate()
fakeLocationModule = FakeLocationModule(applicationContext)
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
intent?.let {
when (it.action?.let { str -> Actions.valueOf(str) }) {
Actions.START_FAKE_LOCATION -> {
altitude = it.getDoubleExtra(PARAM_ALTITUDE, 3.0)
speed = it.getFloatExtra(PARAM_SPEED, 1.0f)
bearing = it.getFloatExtra(PARAM_BEARING, 0.0f)
jitter = it.getFloatExtra(PARAM_JITTER, 3.0f)
jitterAltitude = it.getDoubleExtra(PARAM_JITTER_ALTITUDE, 3.0)
jitterSpeed = it.getFloatExtra(PARAM_JITTER_SPEED, 0.1f)
jitterBearing = it.getFloatExtra(PARAM_JITTER_BEARING, 10.0f)
fakeLocation = Pair(
it.getDoubleExtra(PARAM_LATITUDE, 0.0),
it.getDoubleExtra(PARAM_LONGITUDE, 0.0)
)
initTimerFakeLocation()
}
Actions.START_ROUTE -> {
route = Gson().fromJson(it.getStringExtra(PARAM_ROUTE) ?: "[]", object : TypeToken<List<FakeLocationCoordinate>>() {}.type)
loopRoute = it.getBooleanExtra(PARAM_ROUTE_LOOP, false)
initTimerRoute()
}
else -> {}
}
}
return START_STICKY
}
override fun onDestroy() {
cdtFakeLocation?.cancel()
cdtRoute?.cancel()
super.onDestroy()
}
private fun initTimerFakeLocation() {
cdtFakeLocation?.cancel()
cdtFakeLocation = object : CountDownTimer(PERIOD_UPDATES_SERIE, PERIOD_LOCATION_UPDATE) {
override fun onTick(millisUntilFinished: Long) {
var altitude_buf: Double = altitude ?: return
var speed_buf: Float = speed ?: return
var bearing_buf: Float = bearing ?: return
var jitter_buf: Float = jitter ?: return
var jitterAltitude_buf: Double = jitterAltitude ?: return
var jitterSpeed_buf: Float = jitterSpeed ?: return
var jitterBearing_buf: Float = jitterBearing ?: return
var fakeLocation_buf: Pair<Double, Double> = fakeLocation ?: return
if(fakeLocation != null && altitude != null && speed != null && jitter != null) {
try {
fakeLocationModule.setTestProviderLocation(
(altitude_buf + ((Random.nextFloat() * jitterAltitude_buf) - (jitterAltitude_buf * 0.5f))).toDouble(),
speed_buf + ((Random.nextFloat() * jitterSpeed_buf) - (jitterSpeed_buf * 0.5f)),
bearing_buf + ((Random.nextFloat() * jitterBearing_buf) - (jitterBearing_buf * 0.5f)),
jitter_buf,
jitterAltitude_buf.toFloat(),
jitterSpeed_buf,
jitterBearing_buf,
(fakeLocation_buf.first + (((Random.nextFloat() * jitter_buf) - (jitter_buf * 0.5f)) / 111139.0f)).toDouble(),
(fakeLocation_buf.second + (((Random.nextFloat() * jitter_buf) - (jitter_buf * 0.5f)) / 111139.0f)).toDouble()
)
} catch (e: Exception) {
Log.d("FakeLocationService", "setting fake location", e)
}
}
}
override fun onFinish() {
initTimerFakeLocation()
}
}.start()
}
private fun calculateRouteSegment(): Pair<FakeLocationCoordinate,Pair<Float,Float>>? {
var route_buf: List<FakeLocationCoordinate> = route ?: return null
if(route_buf.size < 2)
return null
var segment: Pair<FakeLocationCoordinate,FakeLocationCoordinate>? = null
var factor: Float? = null
var iter = route_buf.iterator()
while(iter.hasNext()) {
segment = Pair<FakeLocationCoordinate,FakeLocationCoordinate>(
if(segment == null) iter.next() else segment.second,
iter.next()
)
if(routeTime >= segment.first.timestamp && routeTime < segment.second.timestamp) {
factor = (routeTime - segment.first.timestamp) / (segment.second.timestamp - segment.first.timestamp)
break
}
}
if(factor == null && loopRoute) {
iter = route_buf.reversed().iterator()
while(iter.hasNext()) {
segment = Pair<FakeLocationCoordinate,FakeLocationCoordinate>(
if(segment == null) iter.next() else segment.second,
iter.next()
)
if(routeTime >= ((2 * route_buf.last().timestamp) - segment.first.timestamp) && routeTime < ((2 * route_buf.last().timestamp) - segment.second.timestamp)) {
factor = (routeTime - ((2 * route_buf.last().timestamp) - segment.first.timestamp)) / (((2 * route_buf.last().timestamp) - segment.second.timestamp) - ((2 * route_buf.last().timestamp) - segment.first.timestamp))
break
}
}
if(factor == null) {
routeTime -= (2 * route_buf.last().timestamp)
return calculateRouteSegment()
}
}
if(segment != null && factor != null) {
return Pair<FakeLocationCoordinate,Pair<Float,Float>>(
segment.first,
Pair<Float,Float>(
segment.first.latitude + ((segment.second.latitude - segment.first.latitude) * factor),
segment.first.longitude + ((segment.second.longitude - segment.first.longitude) * factor)
)
)
}
return null
}
private fun initTimerRoute() {
cdtRoute?.cancel()
routeTime = 0f
cdtRoute = object : CountDownTimer(PERIOD_UPDATES_SERIE, PERIOD_LOCATION_UPDATE) {
override fun onTick(millisUntilFinished: Long) {
var coord = calculateRouteSegment()
if(coord == null) {
fakeLocationModule.routeStop()
return
}
try {
fakeLocationModule.setTestProviderLocation(
(coord.first.altitude + ((Random.nextFloat() * coord.first.jitterAltitude) - (coord.first.jitterAltitude * 0.5f))).toDouble(),
coord.first.speed + ((Random.nextFloat() * coord.first.jitterSpeed) - (coord.first.jitterSpeed * 0.5f)),
coord.first.bearing + ((Random.nextFloat() * coord.first.jitterBearing) - (coord.first.jitterBearing * 0.5f)),
coord.first.jitter,
coord.first.jitterAltitude,
coord.first.jitterSpeed,
coord.first.jitterBearing,
(coord.second.first + (((Random.nextFloat() * coord.first.jitter) - (coord.first.jitter * 0.5f)) / 111139.0f)).toDouble(),
(coord.second.second + (((Random.nextFloat() * coord.first.jitter) - (coord.first.jitter * 0.5f)) / 111139.0f)).toDouble()
)
} catch (e: Exception) {
Log.d("FakeLocationService", "setting fake location", e)
}
routeTime += (PERIOD_LOCATION_UPDATE / 1000f)
}
override fun onFinish() {
initTimerRoute()
}
}.start()
}
override fun onBind(intent: Intent?): IBinder? {
return null
}
}
|