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
8db28c85
Commit
8db28c85
authored
Feb 28, 2014
by
Chris Scott
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'android'
parents
37f2cd9f
3ca53fdf
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
76 additions
and
47 deletions
+76
-47
LocationUpdateService.java
src/android/LocationUpdateService.java
+76
-47
No files found.
src/android/LocationUpdateService.java
View file @
8db28c85
...
...
@@ -12,16 +12,13 @@ import com.tenforwardconsulting.cordova.bgloc.data.DAOFactory;
import
com.tenforwardconsulting.cordova.bgloc.data.LocationDAO
;
import
android.annotation.TargetApi
;
import
android.app.AlarmManager
;
import
android.media.AudioManager
;
import
android.media.ToneGenerator
;
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 +28,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 +44,15 @@ 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
STATIONARY_ALARM_ACTION
=
"com.tenforwardconsulting.cordova.bgloc.STATIONARY_ALARM_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
*
1
;
public
static
final
int
NOTIFICATION_ID
=
555
;
private
PowerManager
.
WakeLock
wakeLock
;
private
Location
lastLocation
;
private
long
lastUpdateTime
=
0
l
;
...
...
@@ -65,8 +62,11 @@ public class LocationUpdateService extends Service implements LocationListener {
private
float
stationaryRadius
;
private
Location
stationaryLocation
;
private
PendingIntent
stationaryAlarmPI
;
private
Integer
desiredAccuracy
;
private
Integer
distanceFilter
;
private
Integer
scaledDistanceFilter
;
private
Integer
locationTimeout
;
private
Boolean
isDebugging
;
...
...
@@ -74,9 +74,8 @@ public class LocationUpdateService extends Service implements LocationListener {
private
PendingIntent
proximityPI
;
private
Notification
notification
;
private
NotificationManager
notificationManager
;
private
LocationManager
locationManager
;
private
AlarmManager
alarmManager
;
private
ConnectivityManager
connectivityManager
;
private
Criteria
criteria
;
...
...
@@ -96,10 +95,13 @@ 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
);
alarmManager
=
(
AlarmManager
)
this
.
getSystemService
(
Context
.
ALARM_SERVICE
);
stationaryAlarmPI
=
PendingIntent
.
getBroadcast
(
this
,
0
,
new
Intent
(
STATIONARY_ALARM_ACTION
),
0
);
registerReceiver
(
stationaryAlarmReceiver
,
new
IntentFilter
(
STATIONARY_ALARM_ACTION
));
connectivityManager
=
(
ConnectivityManager
)
getSystemService
(
Context
.
CONNECTIVITY_SERVICE
);
wakeLock
=
pm
.
newWakeLock
(
PowerManager
.
PARTIAL_WAKE_LOCK
,
TAG
);
...
...
@@ -120,6 +122,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 +197,26 @@ 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
;
crta
.
setAccuracy
(
Criteria
.
ACCURACY_FINE
);
crta
.
setHorizontalAccuracy
(
translateDesiredAccuracy
(
desiredAccuracy
));
crta
.
setPowerRequirement
(
Criteria
.
POWER_HIGH
);
locationManager
.
requestLocationUpdates
(
locationManager
.
getBestProvider
(
crta
,
true
),
locationTimeout
*
1000
,
distanceFilter
,
this
);
criteria
.
setAccuracy
(
Criteria
.
ACCURACY_FINE
);
criteria
.
setHorizontalAccuracy
(
translateDesiredAccuracy
(
desiredAccuracy
));
criteria
.
setPowerRequirement
(
Criteria
.
POWER_HIGH
);
locationManager
.
requestLocationUpdates
(
locationManager
.
getBestProvider
(
criteria
,
true
),
locationTimeout
*
1000
,
scaledDistanceFilter
,
this
);
// Configure stationary alarm timout. If no location update within configured timeout, tone-down tracking.
/*
if (stationaryAlarmPI != null) {
alarmManager.cancel(stationaryAlarmPI);
stationaryAlarmPI = null;
}
*/
resetStationaryAlarm
();
}
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
)
{
...
...
@@ -219,6 +225,10 @@ public class LocationUpdateService extends Service implements LocationListener {
}
}
public
void
resetStationaryAlarm
()
{
alarmManager
.
set
(
AlarmManager
.
RTC_WAKEUP
,
System
.
currentTimeMillis
()
+
STATIONARY_TIMEOUT
,
stationaryAlarmPI
);
// Millisec * Second * Minute
}
/**
* Returns the most accurate and timely previously detected location.
* Where the last result is beyond the specified maximum distance or
...
...
@@ -259,7 +269,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,19 +278,14 @@ 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
)
{
Log
.
i
(
TAG
,
"- lastLocation is within stationaryRadius"
);
if
(
stationaryLocation
==
null
)
{
stationaryLocation
=
lastLocation
;
}
long
timeDelta
=
location
.
getTime
()
-
stationaryLocation
.
getTime
();
Log
.
i
(
TAG
,
"- timeDelta: "
+
timeDelta
+
">"
+
STATIONARY_TIMEOUT
);
if
(
timeDelta
>
STATIONARY_TIMEOUT
)
{
setPace
(
false
);
if
(
lastLocation
.
distanceTo
(
location
)
>
stationaryRadius
)
{
resetStationaryAlarm
();
Integer
newDistanceFilter
=
calculateDistanceFilter
(
location
.
getSpeed
());
if
(
newDistanceFilter
!=
scaledDistanceFilter
)
{
Log
.
i
(
TAG
,
"- updated distanceFilter, new: "
+
newDistanceFilter
+
", old: "
+
scaledDistanceFilter
);
scaledDistanceFilter
=
newDistanceFilter
;
setPace
(
true
);
}
}
else
{
stationaryLocation
=
null
;
}
}
}
else
if
(
stationaryLocation
==
null
)
{
...
...
@@ -301,6 +307,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 +327,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
);
...
...
@@ -327,6 +344,16 @@ public class LocationUpdateService extends Service implements LocationListener {
registerReceiver
(
stationaryRegionReceiver
,
filter
);
}
private
BroadcastReceiver
stationaryAlarmReceiver
=
new
BroadcastReceiver
()
{
@Override
public
void
onReceive
(
Context
context
,
Intent
intent
)
{
// Put here YOUR code.
Log
.
i
(
TAG
,
"- stationaryAlarm fired"
);
setPace
(
false
);
}
};
private
BroadcastReceiver
stationaryRegionReceiver
=
new
BroadcastReceiver
()
{
@Override
public
void
onReceive
(
Context
context
,
Intent
intent
)
{
...
...
@@ -336,6 +363,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 +488,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