Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
cordova-plugin-background-geolocation
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Aksimaya
cordova-plugin-background-geolocation
Commits
27b8132e
Commit
27b8132e
authored
Feb 26, 2014
by
Chris Scott
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement distance-filter scaling
parent
0658b684
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
38 additions
and
28 deletions
+38
-28
LocationUpdateService.java
src/android/LocationUpdateService.java
+38
-28
No files found.
src/android/LocationUpdateService.java
View file @
27b8132e
...
...
@@ -18,10 +18,6 @@ import android.telephony.PhoneStateListener;
import
android.telephony.TelephonyManager
;
import
android.telephony.CellLocation
;
import
android.app.Application
;
import
android.app.Notification
;
import
android.app.NotificationManager
;
import
android.app.PendingIntent
;
import
android.app.Service
;
import
android.content.Context
;
...
...
@@ -31,7 +27,6 @@ import android.content.IntentFilter;
import
android.content.BroadcastReceiver
;
import
android.location.Location
;
import
android.location.Criteria
;
//import com.google.android.gms.location.Geofence.Builder;
import
android.location.LocationListener
;
import
android.location.LocationManager
;
...
...
@@ -48,14 +43,14 @@ import android.widget.Toast;
import
static
android
.
app
.
PendingIntent
.*;
import
static
android
.
telephony
.
PhoneStateListener
.*;
import
static
java
.
lang
.
Math
.*;
public
class
LocationUpdateService
extends
Service
implements
LocationListener
{
private
static
final
String
TAG
=
"LocationUpdateService"
;
private
static
final
String
STATIONARY_REGION_ACTION
=
"com.tenforwardconsulting.cordova.bgloc.STATIONARY_REGION_ACTION"
;
private
static
final
String
SINGLE_LOCATION_UPDATE_ACTION
=
"com.tenforwardconsulting.cordova.bgloc.SINGLE_LOCATION_UPDATE_ACTION"
;
private
static
long
STATIONARY_TIMEOUT
=
1000
*
60
;
//60 * 1000 * 15
;
private
static
long
STATIONARY_TIMEOUT
=
60
*
1000
*
10
;
public
static
final
int
NOTIFICATION_ID
=
555
;
private
PowerManager
.
WakeLock
wakeLock
;
private
Location
lastLocation
;
private
long
lastUpdateTime
=
0
l
;
...
...
@@ -67,6 +62,7 @@ public class LocationUpdateService extends Service implements LocationListener {
private
Location
stationaryLocation
;
private
Integer
desiredAccuracy
;
private
Integer
distanceFilter
;
private
Integer
scaledDistanceFilter
;
private
Integer
locationTimeout
;
private
Boolean
isDebugging
;
...
...
@@ -74,8 +70,6 @@ public class LocationUpdateService extends Service implements LocationListener {
private
PendingIntent
proximityPI
;
private
Notification
notification
;
private
NotificationManager
notificationManager
;
private
LocationManager
locationManager
;
private
ConnectivityManager
connectivityManager
;
...
...
@@ -96,7 +90,7 @@ public class LocationUpdateService extends Service implements LocationListener {
public
void
onCreate
()
{
super
.
onCreate
();
Log
.
i
(
TAG
,
"OnCreate"
);
notificationManager
=
(
NotificationManager
)
this
.
getSystemService
(
NOTIFICATION_SERVICE
);
PowerManager
pm
=
(
PowerManager
)
getSystemService
(
Context
.
POWER_SERVICE
);
locationManager
=
(
LocationManager
)
this
.
getSystemService
(
Context
.
LOCATION_SERVICE
);
...
...
@@ -120,6 +114,7 @@ public class LocationUpdateService extends Service implements LocationListener {
url
=
intent
.
getStringExtra
(
"url"
);
stationaryRadius
=
Float
.
parseFloat
(
intent
.
getStringExtra
(
"stationaryRadius"
));
distanceFilter
=
Integer
.
parseInt
(
intent
.
getStringExtra
(
"distanceFilter"
));
scaledDistanceFilter
=
distanceFilter
;
desiredAccuracy
=
Integer
.
parseInt
(
intent
.
getStringExtra
(
"desiredAccuracy"
));
locationTimeout
=
Integer
.
parseInt
(
intent
.
getStringExtra
(
"locationTimeout"
));
isDebugging
=
Boolean
.
parseBoolean
(
intent
.
getStringExtra
(
"isDebugging"
));
...
...
@@ -194,23 +189,17 @@ public class LocationUpdateService extends Service implements LocationListener {
locationManager
.
removeUpdates
(
this
);
Criteria
crta
=
new
Criteria
();
crta
.
setAltitudeRequired
(
false
);
crta
.
setBearingRequired
(
false
);
crta
.
setSpeedRequired
(
true
);
crta
.
setCostAllowed
(
true
);
if
(
isMoving
)
{
stationaryLocation
=
null
;
cr
t
a
.
setAccuracy
(
Criteria
.
ACCURACY_FINE
);
cr
t
a
.
setHorizontalAccuracy
(
translateDesiredAccuracy
(
desiredAccuracy
));
cr
t
a
.
setPowerRequirement
(
Criteria
.
POWER_HIGH
);
locationManager
.
requestLocationUpdates
(
locationManager
.
getBestProvider
(
cr
ta
,
true
),
locationTimeout
*
1000
,
d
istanceFilter
,
this
);
cr
iteri
a
.
setAccuracy
(
Criteria
.
ACCURACY_FINE
);
cr
iteri
a
.
setHorizontalAccuracy
(
translateDesiredAccuracy
(
desiredAccuracy
));
cr
iteri
a
.
setPowerRequirement
(
Criteria
.
POWER_HIGH
);
locationManager
.
requestLocationUpdates
(
locationManager
.
getBestProvider
(
cr
iteria
,
true
),
locationTimeout
*
1000
,
scaledD
istanceFilter
,
this
);
}
else
{
stationaryLocation
=
null
;
cr
t
a
.
setAccuracy
(
Criteria
.
ACCURACY_COARSE
);
cr
t
a
.
setHorizontalAccuracy
(
Criteria
.
ACCURACY_LOW
);
cr
t
a
.
setPowerRequirement
(
Criteria
.
POWER_LOW
);
cr
iteri
a
.
setAccuracy
(
Criteria
.
ACCURACY_COARSE
);
cr
iteri
a
.
setHorizontalAccuracy
(
Criteria
.
ACCURACY_LOW
);
cr
iteri
a
.
setPowerRequirement
(
Criteria
.
POWER_LOW
);
Location
location
=
this
.
getLastBestLocation
((
int
)
stationaryRadius
,
locationTimeout
*
1000
);
if
(
location
!=
null
)
{
...
...
@@ -259,7 +248,8 @@ public class LocationUpdateService extends Service implements LocationListener {
}
public
void
onLocationChanged
(
Location
location
)
{
Log
.
d
(
TAG
,
"- onLocationChanged: "
+
location
.
getLatitude
()
+
","
+
location
.
getLongitude
()
+
", accuracy: "
+
location
.
getAccuracy
()
+
", isMoving: "
+
isMoving
);
Log
.
d
(
TAG
,
"- onLocationChanged: "
+
location
.
getLatitude
()
+
","
+
location
.
getLongitude
()
+
", accuracy: "
+
location
.
getAccuracy
()
+
", isMoving: "
+
isMoving
+
", speed: "
+
location
.
getSpeed
());
if
(
isDebugging
)
{
toneGenerator
.
startTone
(
ToneGenerator
.
TONE_PROP_BEEP
);
}
...
...
@@ -267,8 +257,9 @@ public class LocationUpdateService extends Service implements LocationListener {
// If user hasn't moved beyond the stationaryRadius within time of STATIONARY_TIMEOUT
// assume they've stopped.
if
(
lastLocation
!=
null
)
{
Log
.
i
(
TAG
,
"- has lastLocation "
+
lastLocation
.
distanceTo
(
location
)
+
"<"
+
stationaryRadius
);
if
(
lastLocation
.
distanceTo
(
location
)
<
stationaryRadius
)
{
Float
stationaryDistance
=
(
location
.
getAccuracy
()
<
stationaryRadius
)
?
stationaryRadius
:
location
.
getAccuracy
();
Log
.
i
(
TAG
,
"- has lastLocation "
+
lastLocation
.
distanceTo
(
location
)
+
"<"
+
stationaryDistance
);
if
(
lastLocation
.
distanceTo
(
location
)
<
stationaryDistance
)
{
Log
.
i
(
TAG
,
"- lastLocation is within stationaryRadius"
);
if
(
stationaryLocation
==
null
)
{
stationaryLocation
=
lastLocation
;
...
...
@@ -280,6 +271,12 @@ public class LocationUpdateService extends Service implements LocationListener {
}
}
else
{
stationaryLocation
=
null
;
Integer
newDistanceFilter
=
calculateDistanceFilter
(
location
.
getSpeed
());
if
(
newDistanceFilter
!=
scaledDistanceFilter
)
{
Log
.
i
(
TAG
,
"- updated distanceFilter, new: "
+
newDistanceFilter
+
", old: "
+
scaledDistanceFilter
);
scaledDistanceFilter
=
newDistanceFilter
;
setPace
(
true
);
}
}
}
}
else
if
(
stationaryLocation
==
null
)
{
...
...
@@ -301,6 +298,16 @@ public class LocationUpdateService extends Service implements LocationListener {
}
}
private
Integer
calculateDistanceFilter
(
Float
speed
)
{
Double
newDistanceFilter
=
(
double
)
distanceFilter
;
if
(
speed
>
3
&&
speed
<
100
)
{
float
roundedDistanceFilter
=
(
round
(
speed
/
5
)
*
5
);
newDistanceFilter
=
pow
(
roundedDistanceFilter
,
2
)
+
(
double
)
distanceFilter
;
}
return
(
newDistanceFilter
.
intValue
()
<
1000
)
?
newDistanceFilter
.
intValue
()
:
1000
;
}
private
void
startMonitoringStationaryRegion
(
Location
location
)
{
Log
.
i
(
TAG
,
"- startMonitoringStationaryRegion ("
+
location
.
getLatitude
()
+
","
+
location
.
getLongitude
()
+
")"
);
stationaryLocation
=
location
;
...
...
@@ -311,6 +318,7 @@ public class LocationUpdateService extends Service implements LocationListener {
if
(
proximityPI
!=
null
)
{
locationManager
.
removeProximityAlert
(
proximityPI
);
proximityPI
=
null
;
}
Intent
intent
=
new
Intent
(
STATIONARY_REGION_ACTION
);
proximityPI
=
PendingIntent
.
getBroadcast
(
this
,
0
,
intent
,
0
);
...
...
@@ -336,6 +344,9 @@ public class LocationUpdateService extends Service implements LocationListener {
Boolean
entering
=
intent
.
getBooleanExtra
(
key
,
false
);
if
(
entering
)
{
Log
.
d
(
TAG
,
"- ENTER"
);
if
(
isMoving
)
{
setPace
(
false
);
}
}
else
{
Log
.
d
(
TAG
,
"- EXIT"
);
...
...
@@ -458,9 +469,8 @@ public class LocationUpdateService extends Service implements LocationListener {
// Stationary-region proximity-detector.
if
(
proximityPI
!=
null
)
{
locationManager
.
removeProximityAlert
(
proximityPI
);
proximityPI
=
null
;
}
notificationManager
.
cancel
(
NOTIFICATION_ID
);
wakeLock
.
release
();
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment