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
37f2cd9f
Commit
37f2cd9f
authored
Feb 26, 2014
by
Chris Scott
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'android'
parents
50ff5a81
0658b684
Changes
7
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
514 additions
and
274 deletions
+514
-274
README.md
README.md
+2
-0
index.js
example/www/js/index.js
+2
-0
plugin.xml
plugin.xml
+1
-1
BackgroundGpsPlugin.java
src/android/BackgroundGpsPlugin.java
+69
-35
LocationUpdateService.java
src/android/LocationUpdateService.java
+439
-237
android-support-v4.jar
src/android/android-support-v4.jar
+0
-0
BackgroundGeoLocation.js
www/BackgroundGeoLocation.js
+1
-1
No files found.
README.md
View file @
37f2cd9f
...
@@ -68,6 +68,8 @@ A full example could be:
...
@@ -68,6 +68,8 @@ A full example could be:
// BackgroundGeoLocation is highly configurable.
// BackgroundGeoLocation is highly configurable.
bgGeo.configure(callbackFn, failureFn, {
bgGeo.configure(callbackFn, failureFn, {
url: 'http://only.for.android.com/update_location.json', // <-- only required for Android; ios allows javascript callbacks for your http
authToken: 'user_secret_auth_token', // <-- only required for Android; ios allows javascript callbacks for your http
desiredAccuracy: 10,
desiredAccuracy: 10,
stationaryRadius: 20,
stationaryRadius: 20,
distanceFilter: 30,
distanceFilter: 30,
...
...
example/www/js/index.js
View file @
37f2cd9f
...
@@ -90,6 +90,8 @@ var app = {
...
@@ -90,6 +90,8 @@ var app = {
// BackgroundGeoLocation is highly configurable.
// BackgroundGeoLocation is highly configurable.
bgGeo
.
configure
(
callbackFn
,
failureFn
,
{
bgGeo
.
configure
(
callbackFn
,
failureFn
,
{
url
:
'
http://only.for.android.com/update_location.json
'
,
// <-- only required for Android; ios allows javascript callbacks for your http
authToken
:
'
user_secret_auth_token
'
,
// <-- only required for Android; ios allows javascript callbacks for your http
desiredAccuracy
:
10
,
desiredAccuracy
:
10
,
stationaryRadius
:
20
,
stationaryRadius
:
20
,
distanceFilter
:
30
,
distanceFilter
:
30
,
...
...
plugin.xml
View file @
37f2cd9f
...
@@ -30,13 +30,13 @@
...
@@ -30,13 +30,13 @@
<source-file
src=
"src/android/data/sqlite/SQLiteLocationDAO.java"
target-dir=
"src/com/tenforwardconsulting/cordova/bgloc/data/sqlite"
/>
<source-file
src=
"src/android/data/sqlite/SQLiteLocationDAO.java"
target-dir=
"src/com/tenforwardconsulting/cordova/bgloc/data/sqlite"
/>
<source-file
src=
"src/android/notification.png"
target-dir=
"res/drawable"
/>
<source-file
src=
"src/android/notification.png"
target-dir=
"res/drawable"
/>
<source-file
src=
"src/android/android-support-v4.jar"
target-dir=
"libs"
/>
<config-file
target=
"AndroidManifest.xml"
parent=
"/manifest/application"
>
<config-file
target=
"AndroidManifest.xml"
parent=
"/manifest/application"
>
<service
android:name=
"com.tenforwardconsulting.cordova.bgloc.LocationUpdateService"
android:enabled=
"true"
android:process=
":remote"
/>
<service
android:name=
"com.tenforwardconsulting.cordova.bgloc.LocationUpdateService"
android:enabled=
"true"
android:process=
":remote"
/>
</config-file>
</config-file>
<config-file
target=
"AndroidManifest.xml"
parent=
"/manifest"
>
<config-file
target=
"AndroidManifest.xml"
parent=
"/manifest"
>
<uses-permission
android:name=
"android.permission.ACCESS_NETWORK_STATE"
/>
<uses-permission
android:name=
"android.permission.ACCESS_COARSE_LOCATION"
/>
<uses-permission
android:name=
"android.permission.ACCESS_COARSE_LOCATION"
/>
<uses-permission
android:name=
"android.permission.ACCESS_FINE_LOCATION"
/>
<uses-permission
android:name=
"android.permission.ACCESS_FINE_LOCATION"
/>
<uses-permission
android:name=
"android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"
/>
<uses-permission
android:name=
"android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"
/>
...
...
src/android/BackgroundGpsPlugin.java
View file @
37f2cd9f
...
@@ -6,42 +6,76 @@ import org.json.JSONArray;
...
@@ -6,42 +6,76 @@ import org.json.JSONArray;
import
org.json.JSONException
;
import
org.json.JSONException
;
import
android.app.Activity
;
import
android.app.Activity
;
import
android.content.Context
;
import
android.content.Intent
;
import
android.content.Intent
;
import
android.location.LocationManager
;
import
android.util.Log
;
public
class
BackgroundGpsPlugin
extends
CordovaPlugin
{
public
class
BackgroundGpsPlugin
extends
CordovaPlugin
{
private
static
final
String
TAG
=
"BackgroundGpsPlugin"
;
public
static
final
String
ACTION_START
=
"start"
;
public
static
final
String
ACTION_START
=
"start"
;
public
static
final
String
ACTION_STOP
=
"stop"
;
public
static
final
String
ACTION_STOP
=
"stop"
;
public
static
final
String
ACTION_CONFIGURE
=
"configure"
;
public
static
final
String
ACTION_CONFIGURE
=
"configure"
;
public
static
final
String
ACTION_SET_CONFIG
=
"setConfig"
;
private
Intent
updateServiceIntent
;
private
String
authToken
;
private
String
authToken
;
private
String
url
;
private
String
url
;
private
String
stationaryRadius
=
"30"
;
private
String
desiredAccuracy
=
"100"
;
private
String
distanceFilter
=
"30"
;
private
String
locationTimeout
=
"60"
;
private
String
isDebugging
=
"false"
;
@Override
public
boolean
execute
(
String
action
,
JSONArray
data
,
CallbackContext
callbackContext
)
{
public
boolean
execute
(
String
action
,
JSONArray
data
,
CallbackContext
callbackContext
)
{
Activity
activity
=
this
.
cordova
.
getActivity
();
Activity
activity
=
this
.
cordova
.
getActivity
();
Intent
updateServiceIntent
=
new
Intent
(
activity
,
LocationUpdateService
.
class
);
Boolean
result
=
false
;
updateServiceIntent
=
new
Intent
(
activity
,
LocationUpdateService
.
class
);
if
(
ACTION_START
.
equalsIgnoreCase
(
action
))
{
if
(
ACTION_START
.
equalsIgnoreCase
(
action
))
{
result
=
true
;
if
(
authToken
==
null
||
url
==
null
)
{
if
(
authToken
==
null
||
url
==
null
)
{
callbackContext
.
error
(
"Call configure before calling start"
);
callbackContext
.
error
(
"Call configure before calling start"
);
return
false
;
}
else
{
}
callbackContext
.
success
();
updateServiceIntent
.
putExtra
(
"authToken"
,
authToken
);
updateServiceIntent
.
putExtra
(
"authToken"
,
authToken
);
updateServiceIntent
.
putExtra
(
"url"
,
url
);
updateServiceIntent
.
putExtra
(
"url"
,
url
);
activity
.
startService
(
updateServiceIntent
);
updateServiceIntent
.
putExtra
(
"stationaryRadius"
,
stationaryRadius
);
updateServiceIntent
.
putExtra
(
"desiredAccuracy"
,
desiredAccuracy
);
updateServiceIntent
.
putExtra
(
"distanceFilter"
,
distanceFilter
);
updateServiceIntent
.
putExtra
(
"locationTimeout"
,
locationTimeout
);
updateServiceIntent
.
putExtra
(
"desiredAccuracy"
,
desiredAccuracy
);
updateServiceIntent
.
putExtra
(
"isDebugging"
,
isDebugging
);
activity
.
startService
(
updateServiceIntent
);
}
}
else
if
(
ACTION_STOP
.
equalsIgnoreCase
(
action
))
{
}
else
if
(
ACTION_STOP
.
equalsIgnoreCase
(
action
))
{
result
=
true
;
activity
.
stopService
(
updateServiceIntent
);
activity
.
stopService
(
updateServiceIntent
);
callbackContext
.
success
();
}
else
if
(
ACTION_CONFIGURE
.
equalsIgnoreCase
(
action
))
{
}
else
if
(
ACTION_CONFIGURE
.
equalsIgnoreCase
(
action
))
{
result
=
true
;
try
{
try
{
// [authToken, url, stationaryRadius, distanceFilter, locationTimeout, desiredAccuracy, debug]);
this
.
authToken
=
data
.
getString
(
0
);
this
.
authToken
=
data
.
getString
(
0
);
this
.
url
=
data
.
getString
(
1
);
this
.
url
=
data
.
getString
(
1
);
this
.
stationaryRadius
=
data
.
getString
(
2
);
this
.
distanceFilter
=
data
.
getString
(
3
);
this
.
locationTimeout
=
data
.
getString
(
4
);
this
.
desiredAccuracy
=
data
.
getString
(
5
);
this
.
isDebugging
=
data
.
getString
(
6
);
}
catch
(
JSONException
e
)
{
}
catch
(
JSONException
e
)
{
callbackContext
.
error
(
"authToken/url required as parameters: "
+
e
.
getMessage
());
callbackContext
.
error
(
"authToken/url required as parameters: "
+
e
.
getMessage
());
return
false
;
}
}
}
else
if
(
ACTION_SET_CONFIG
.
equalsIgnoreCase
(
action
))
{
result
=
true
;
// TODO reconfigure Service
callbackContext
.
success
();
}
}
return
true
;
return
result
;
}
}
}
}
src/android/LocationUpdateService.java
View file @
37f2cd9f
package
com
.
tenforwardconsulting
.
cordova
.
bgloc
;
package
com
.
tenforwardconsulting
.
cordova
.
bgloc
;
import
java.util.
Arrays
;
import
java.util.
List
;
import
org.apache.http.HttpResponse
;
import
org.apache.http.HttpResponse
;
import
org.apache.http.client.methods.HttpPost
;
import
org.apache.http.client.methods.HttpPost
;
...
@@ -11,16 +11,28 @@ import org.json.JSONObject;
...
@@ -11,16 +11,28 @@ import org.json.JSONObject;
import
com.tenforwardconsulting.cordova.bgloc.data.DAOFactory
;
import
com.tenforwardconsulting.cordova.bgloc.data.DAOFactory
;
import
com.tenforwardconsulting.cordova.bgloc.data.LocationDAO
;
import
com.tenforwardconsulting.cordova.bgloc.data.LocationDAO
;
import
android.annotation.TargetApi
;
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.Application
;
import
android.app.Notification
;
import
android.app.Notification
;
import
android.app.NotificationManager
;
import
android.app.NotificationManager
;
import
android.support.v4.app.NotificationCompat
;
import
android.app.PendingIntent
;
import
android.app.PendingIntent
;
import
android.app.Service
;
import
android.app.Service
;
import
android.content.Context
;
import
android.content.Context
;
import
android.content.Intent
;
import
android.content.Intent
;
import
android.content.pm.PackageManager
;
import
android.content.IntentFilter
;
import
android.content.BroadcastReceiver
;
import
android.location.Location
;
import
android.location.Location
;
import
android.location.Criteria
;
//import com.google.android.gms.location.Geofence.Builder;
import
android.location.LocationListener
;
import
android.location.LocationListener
;
import
android.location.LocationManager
;
import
android.location.LocationManager
;
import
android.net.ConnectivityManager
;
import
android.net.ConnectivityManager
;
...
@@ -34,20 +46,45 @@ import android.os.SystemClock;
...
@@ -34,20 +46,45 @@ import android.os.SystemClock;
import
android.util.Log
;
import
android.util.Log
;
import
android.widget.Toast
;
import
android.widget.Toast
;
import
static
android
.
app
.
PendingIntent
.*;
import
static
android
.
telephony
.
PhoneStateListener
.*;
public
class
LocationUpdateService
extends
Service
implements
LocationListener
{
public
class
LocationUpdateService
extends
Service
implements
LocationListener
{
private
static
final
String
TAG
=
"LocationService"
;
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;
public
static
final
int
NOTIFICATION_ID
=
555
;
public
static
final
int
NOTIFICATION_ID
=
555
;
private
PowerManager
.
WakeLock
wakeLock
;
private
PowerManager
.
WakeLock
wakeLock
;
private
Location
lastLocation
;
private
Location
lastLocation
;
private
long
lastUpdateTime
=
0
l
;
private
long
lastUpdateTime
=
0
l
;
private
BusyTask
looper
;
private
String
authToken
=
"HypVBMmDxbh76pHpwots"
;
private
String
authToken
=
"HypVBMmDxbh76pHpwots"
;
private
String
url
=
"http://192.168.2.15:3000/users/current_location.json"
;
private
String
url
=
"http://192.168.2.15:3000/users/current_location.json"
;
private
float
stationaryRadius
;
private
Location
stationaryLocation
;
private
Integer
desiredAccuracy
;
private
Integer
distanceFilter
;
private
Integer
locationTimeout
;
private
Boolean
isDebugging
;
private
ToneGenerator
toneGenerator
;
private
PendingIntent
proximityPI
;
private
Notification
notification
;
private
Notification
notification
;
private
NotificationManager
notificationManager
;
private
NotificationManager
notificationManager
;
private
LocationManager
locationManager
;
private
LocationManager
locationManager
;
private
ConnectivityManager
connectivityManager
;
private
ConnectivityManager
connectivityManager
;
private
Criteria
criteria
;
private
Boolean
isMoving
=
false
;
public
static
TelephonyManager
telephonyManager
=
null
;
@Override
@Override
public
IBinder
onBind
(
Intent
intent
)
{
public
IBinder
onBind
(
Intent
intent
)
{
// TODO Auto-generated method stub
// TODO Auto-generated method stub
...
@@ -61,33 +98,68 @@ public class LocationUpdateService extends Service implements LocationListener {
...
@@ -61,33 +98,68 @@ public class LocationUpdateService extends Service implements LocationListener {
Log
.
i
(
TAG
,
"OnCreate"
);
Log
.
i
(
TAG
,
"OnCreate"
);
notificationManager
=
(
NotificationManager
)
this
.
getSystemService
(
NOTIFICATION_SERVICE
);
notificationManager
=
(
NotificationManager
)
this
.
getSystemService
(
NOTIFICATION_SERVICE
);
PowerManager
pm
=
(
PowerManager
)
getSystemService
(
Context
.
POWER_SERVICE
);
PowerManager
pm
=
(
PowerManager
)
getSystemService
(
Context
.
POWER_SERVICE
);
locationManager
=
(
LocationManager
)
this
.
getSystemService
(
Context
.
LOCATION_SERVICE
);
locationManager
=
(
LocationManager
)
this
.
getSystemService
(
Context
.
LOCATION_SERVICE
);
locationManager
.
requestLocationUpdates
(
LocationManager
.
GPS_PROVIDER
,
1
*
60
*
1000
,
0
,
this
);
connectivityManager
=
(
ConnectivityManager
)
getSystemService
(
Context
.
CONNECTIVITY_SERVICE
);
connectivityManager
=
(
ConnectivityManager
)
getSystemService
(
Context
.
CONNECTIVITY_SERVICE
);
wakeLock
=
pm
.
newWakeLock
(
PowerManager
.
PARTIAL_WAKE_LOCK
,
TAG
);
wakeLock
=
pm
.
newWakeLock
(
PowerManager
.
PARTIAL_WAKE_LOCK
,
TAG
);
wakeLock
.
acquire
();
wakeLock
.
acquire
();
// looper = new BusyTask();
criteria
=
new
Criteria
();
// looper.execute("go");
criteria
.
setAltitudeRequired
(
false
);
//scheudlePostLocation(locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER));
criteria
.
setBearingRequired
(
false
);
criteria
.
setSpeedRequired
(
true
);
criteria
.
setCostAllowed
(
true
);
}
}
@Override
@Override
public
int
onStartCommand
(
Intent
intent
,
int
flags
,
int
startId
)
{
public
int
onStartCommand
(
Intent
intent
,
int
flags
,
int
startId
)
{
Log
.
i
(
TAG
,
"Received start id "
+
startId
+
": "
+
intent
);
Log
.
i
(
TAG
,
"Received start id "
+
startId
+
": "
+
intent
);
if
(
intent
!=
null
)
{
if
(
intent
!=
null
)
{
this
.
authToken
=
intent
.
getStringExtra
(
"authToken"
);
authToken
=
intent
.
getStringExtra
(
"authToken"
);
this
.
url
=
intent
.
getStringExtra
(
"url"
);
url
=
intent
.
getStringExtra
(
"url"
);
stationaryRadius
=
Float
.
parseFloat
(
intent
.
getStringExtra
(
"stationaryRadius"
));
distanceFilter
=
Integer
.
parseInt
(
intent
.
getStringExtra
(
"distanceFilter"
));
desiredAccuracy
=
Integer
.
parseInt
(
intent
.
getStringExtra
(
"desiredAccuracy"
));
locationTimeout
=
Integer
.
parseInt
(
intent
.
getStringExtra
(
"locationTimeout"
));
isDebugging
=
Boolean
.
parseBoolean
(
intent
.
getStringExtra
(
"isDebugging"
));
if
(
isDebugging
)
{
toneGenerator
=
new
ToneGenerator
(
AudioManager
.
STREAM_NOTIFICATION
,
100
);
}
Log
.
i
(
TAG
,
"- url: "
+
url
);
Log
.
i
(
TAG
,
"- token: "
+
authToken
);
Log
.
i
(
TAG
,
"- stationaryRadius: "
+
stationaryRadius
);
Log
.
i
(
TAG
,
"- distanceFilter: "
+
distanceFilter
);
Log
.
i
(
TAG
,
"- desiredAccuracy: "
+
desiredAccuracy
);
Log
.
i
(
TAG
,
"- locationTimeout: "
+
locationTimeout
);
Log
.
i
(
TAG
,
"- isDebugging: "
+
isDebugging
);
}
}
Toast
.
makeText
(
this
,
"Background location tracking started"
,
Toast
.
LENGTH_SHORT
).
show
();
Toast
.
makeText
(
this
,
"Background location tracking started"
,
Toast
.
LENGTH_SHORT
).
show
();
this
.
setPace
(
false
);
/**
* Experimental cell-location-change handler
*
telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phoneStateListener, LISTEN_CELL_LOCATION);
*
*/
//We want this service to continue running until it is explicitly
//We want this service to continue running until it is explicitly
// stopped, so return sticky.
// stopped, so return sticky.
notification
=
buildNotification
();
notificationManager
.
notify
(
NOTIFICATION_ID
,
notification
);
return
START_STICKY
;
return
START_STICKY
;
}
}
public
void
onCellLocationChanged
(
CellLocation
cellLocation
)
{
Log
.
i
(
TAG
,
"- onCellLocationChanged"
);
Location
lastKnownLocation
=
locationManager
.
getLastKnownLocation
(
LocationManager
.
GPS_PROVIDER
);
Location
location
=
getLastBestLocation
((
int
)
stationaryRadius
,
locationTimeout
*
1000
);
if
(
location
!=
null
)
{
Log
.
i
(
TAG
,
"location: "
+
location
.
getLatitude
()
+
","
+
location
.
getLongitude
()
+
", accuracy: "
+
location
.
getAccuracy
());
}
}
@Override
@Override
public
boolean
stopService
(
Intent
intent
)
{
public
boolean
stopService
(
Intent
intent
)
{
Log
.
i
(
TAG
,
"Received stop: "
+
intent
);
Log
.
i
(
TAG
,
"Received stop: "
+
intent
);
...
@@ -95,56 +167,130 @@ public class LocationUpdateService extends Service implements LocationListener {
...
@@ -95,56 +167,130 @@ public class LocationUpdateService extends Service implements LocationListener {
Toast
.
makeText
(
this
,
"Background location tracking stopped"
,
Toast
.
LENGTH_SHORT
).
show
();
Toast
.
makeText
(
this
,
"Background location tracking stopped"
,
Toast
.
LENGTH_SHORT
).
show
();
return
super
.
stopService
(
intent
);
return
super
.
stopService
(
intent
);
}
}
@Override
public
void
onDestroy
()
{
Log
.
w
(
TAG
,
"Destroyed Location update Service"
);
cleanUp
();
super
.
onDestroy
();
}
private
void
cleanUp
()
{
locationManager
.
removeUpdates
(
this
);
notificationManager
.
cancel
(
NOTIFICATION_ID
);
wakeLock
.
release
();
if
(
looper
!=
null
)
{
looper
.
stop
=
true
;
}
}
private
Notification
buildNotification
()
{
PackageManager
pm
=
this
.
getPackageManager
();
Intent
notificationIntent
=
pm
.
getLaunchIntentForPackage
(
this
.
getPackageName
());
notificationIntent
.
addFlags
(
Intent
.
FLAG_ACTIVITY_SINGLE_TOP
);
Application
application
=
this
.
getApplication
();
private
Integer
translateDesiredAccuracy
(
Integer
accuracy
)
{
int
backgroundIconId
=
0
;
switch
(
accuracy
)
{
for
(
String
s:
Arrays
.
asList
(
"ic_launcher"
,
"icon"
,
"notification"
)
)
{
case
1000
:
backgroundIconId
=
application
.
getResources
().
getIdentifier
(
s
,
"drawable"
,
application
.
getPackageName
());
accuracy
=
Criteria
.
ACCURACY_LOW
;
if
(
backgroundIconId
!=
0
)
{
break
;
break
;
case
100
:
accuracy
=
Criteria
.
ACCURACY_MEDIUM
;
break
;
case
10
:
accuracy
=
Criteria
.
ACCURACY_MEDIUM
;
break
;
case
0
:
accuracy
=
Criteria
.
ACCURACY_HIGH
;
break
;
default
:
accuracy
=
Criteria
.
ACCURACY_MEDIUM
;
}
}
return
accuracy
;
}
}
int
appNameId
=
application
.
getResources
().
getIdentifier
(
"app_name"
,
"string"
,
application
.
getPackageName
());
private
void
setPace
(
Boolean
value
)
{
Log
.
i
(
TAG
,
"setPace: "
+
value
);
isMoving
=
value
;
PendingIntent
contentIntent
=
PendingIntent
.
getActivity
(
this
,
0
,
notificationIntent
,
0
);
locationManager
.
removeUpdates
(
this
);
NotificationCompat
.
Builder
builder
=
new
NotificationCompat
.
Builder
(
this
)
.
setSmallIcon
(
backgroundIconId
)
Criteria
crta
=
new
Criteria
();
.
setContentTitle
(
this
.
getString
(
appNameId
))
crta
.
setAltitudeRequired
(
false
);
.
setOngoing
(
true
)
crta
.
setBearingRequired
(
false
);
.
setContentIntent
(
contentIntent
)
crta
.
setSpeedRequired
(
true
);
.
setWhen
(
System
.
currentTimeMillis
());
crta
.
setCostAllowed
(
true
);
if
(
lastLocation
!=
null
)
{
builder
.
setContentText
(
"Last location: "
+
lastLocation
.
getLatitude
()
+
", "
+
lastLocation
.
getLongitude
());
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
);
}
else
{
}
else
{
builder
.
setContentText
(
"Tracking your GPS position"
);
stationaryLocation
=
null
;
crta
.
setAccuracy
(
Criteria
.
ACCURACY_COARSE
);
crta
.
setHorizontalAccuracy
(
Criteria
.
ACCURACY_LOW
);
crta
.
setPowerRequirement
(
Criteria
.
POWER_LOW
);
Location
location
=
this
.
getLastBestLocation
((
int
)
stationaryRadius
,
locationTimeout
*
1000
);
if
(
location
!=
null
)
{
this
.
startMonitoringStationaryRegion
(
location
);
}
}
}
}
/**
* Returns the most accurate and timely previously detected location.
* Where the last result is beyond the specified maximum distance or
* latency a one-off location update is returned via the {@link LocationListener}
* specified in {@link setChangedLocationListener}.
* @param minDistance Minimum distance before we require a location update.
* @param minTime Minimum time required between location updates.
* @return The most accurate and / or timely previously detected location.
*/
public
Location
getLastBestLocation
(
int
minDistance
,
long
minTime
)
{
Log
.
i
(
TAG
,
"- fetching last best location"
);
Location
bestResult
=
null
;
float
bestAccuracy
=
Float
.
MAX_VALUE
;
long
bestTime
=
Long
.
MIN_VALUE
;
return
builder
.
build
();
// Iterate through all the providers on the system, keeping
// note of the most accurate result within the acceptable time limit.
// If no result is found within maxTime, return the newest Location.
List
<
String
>
matchingProviders
=
locationManager
.
getAllProviders
();
for
(
String
provider:
matchingProviders
)
{
Location
location
=
locationManager
.
getLastKnownLocation
(
provider
);
if
(
location
!=
null
)
{
float
accuracy
=
location
.
getAccuracy
();
long
time
=
location
.
getTime
();
if
((
time
>
minTime
&&
accuracy
<
bestAccuracy
))
{
bestResult
=
location
;
bestAccuracy
=
accuracy
;
bestTime
=
time
;
}
else
if
(
time
<
minTime
&&
bestAccuracy
==
Float
.
MAX_VALUE
&&
time
>
bestTime
)
{
bestResult
=
location
;
bestTime
=
time
;
}
}
}
return
bestResult
;
}
}
public
void
onLocationChanged
(
Location
location
)
{
public
void
onLocationChanged
(
Location
location
)
{
Log
.
d
(
TAG
,
"Location22Change: "
+
location
.
getLatitude
()
+
", "
+
location
.
getLongitude
());
Log
.
d
(
TAG
,
"- onLocationChanged: "
+
location
.
getLatitude
()
+
","
+
location
.
getLongitude
()
+
", accuracy: "
+
location
.
getAccuracy
()
+
", isMoving: "
+
isMoving
);
if
(
isDebugging
)
{
toneGenerator
.
startTone
(
ToneGenerator
.
TONE_PROP_BEEP
);
}
if
(
isMoving
)
{
// 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
);
}
}
else
{
stationaryLocation
=
null
;
}
}
}
else
if
(
stationaryLocation
==
null
)
{
this
.
startMonitoringStationaryRegion
(
location
);
}
lastLocation
=
location
;
lastLocation
=
location
;
Log
.
d
(
TAG
,
"Location33Change:"
);
Log
.
d
(
TAG
,
"-------- persistLocation DISABLED"
);
// test the measurement to see if it is more accurate than the previous measurement
persistLocation
(
location
);
persistLocation
(
location
);
if
(
this
.
isNetworkConnected
())
{
if
(
this
.
isNetworkConnected
())
{
...
@@ -155,6 +301,68 @@ public class LocationUpdateService extends Service implements LocationListener {
...
@@ -155,6 +301,68 @@ public class LocationUpdateService extends Service implements LocationListener {
}
}
}
}
private
void
startMonitoringStationaryRegion
(
Location
location
)
{
Log
.
i
(
TAG
,
"- startMonitoringStationaryRegion ("
+
location
.
getLatitude
()
+
","
+
location
.
getLongitude
()
+
")"
);
stationaryLocation
=
location
;
if
(
isDebugging
)
{
toneGenerator
.
startTone
(
ToneGenerator
.
TONE_CDMA_ABBR_ALERT
);
}
if
(
proximityPI
!=
null
)
{
locationManager
.
removeProximityAlert
(
proximityPI
);
}
Intent
intent
=
new
Intent
(
STATIONARY_REGION_ACTION
);
proximityPI
=
PendingIntent
.
getBroadcast
(
this
,
0
,
intent
,
0
);
locationManager
.
addProximityAlert
(
location
.
getLatitude
(),
location
.
getLongitude
(),
stationaryRadius
,
-
1
,
proximityPI
);
IntentFilter
filter
=
new
IntentFilter
(
STATIONARY_REGION_ACTION
);
registerReceiver
(
stationaryRegionReceiver
,
filter
);
}
private
BroadcastReceiver
stationaryRegionReceiver
=
new
BroadcastReceiver
()
{
@Override
public
void
onReceive
(
Context
context
,
Intent
intent
)
{
Log
.
i
(
TAG
,
"stationaryRegionReceiver"
);
String
key
=
LocationManager
.
KEY_PROXIMITY_ENTERING
;
Boolean
entering
=
intent
.
getBooleanExtra
(
key
,
false
);
if
(
entering
)
{
Log
.
d
(
TAG
,
"- ENTER"
);
}
else
{
Log
.
d
(
TAG
,
"- EXIT"
);
onExitStationaryRegion
();
}
}
};
private
PhoneStateListener
phoneStateListener
=
new
PhoneStateListener
()
{
@Override
public
void
onCellLocationChanged
(
CellLocation
location
)
{
onCellLocationChanged
(
location
);
}
};
public
void
onExitStationaryRegion
()
{
if
(
isDebugging
)
{
toneGenerator
.
startTone
(
ToneGenerator
.
TONE_CDMA_CONFIRM
);
}
if
(
proximityPI
!=
null
)
{
Log
.
i
(
TAG
,
"- proximityPI: "
+
proximityPI
.
toString
());
locationManager
.
removeProximityAlert
(
proximityPI
);
}
this
.
setPace
(
true
);
}
public
void
onProviderDisabled
(
String
provider
)
{
public
void
onProviderDisabled
(
String
provider
)
{
// TODO Auto-generated method stub
// TODO Auto-generated method stub
...
@@ -239,32 +447,28 @@ public class LocationUpdateService extends Service implements LocationListener {
...
@@ -239,32 +447,28 @@ public class LocationUpdateService extends Service implements LocationListener {
}
}
@Override
@Override
public
void
onTaskRemoved
(
Intent
rootIntent
)
{
public
void
onDestroy
()
{
this
.
stopSelf
();
Log
.
w
(
TAG
,
"------------------------------------------ Destroyed Location update Service"
);
super
.
onTaskRemoved
(
rootIntent
);
cleanUp
();
super
.
onDestroy
();
}
}
private
void
cleanUp
()
{
locationManager
.
removeUpdates
(
this
);
private
class
BusyTask
extends
AsyncTask
<
String
,
Integer
,
Boolean
>{
// Stationary-region proximity-detector.
public
boolean
stop
=
false
;
if
(
proximityPI
!=
null
)
{
locationManager
.
removeProximityAlert
(
proximityPI
);
@Override
protected
Boolean
doInBackground
(
String
...
params
)
{
while
(!
stop
)
{
Log
.
d
(
TAG
,
"#timestamp "
+
System
.
currentTimeMillis
());
if
(
lastUpdateTime
+
5
*
60
*
1000
<
SystemClock
.
elapsedRealtime
())
{
Log
.
d
(
TAG
,
"5 minutes, forcing update with last location"
);
postLocation
(
com
.
tenforwardconsulting
.
cordova
.
bgloc
.
data
.
Location
.
fromAndroidLocation
(
locationManager
.
getLastKnownLocation
(
LocationManager
.
NETWORK_PROVIDER
)));
}
}
try
{
Thread
.
sleep
(
30000
);
notificationManager
.
cancel
(
NOTIFICATION_ID
);
}
catch
(
InterruptedException
e
)
{
wakeLock
.
release
();
// TODO Auto-generated catch block
e
.
printStackTrace
();
}
}
return
true
;
}
}
@TargetApi
(
Build
.
VERSION_CODES
.
ICE_CREAM_SANDWICH
)
@Override
public
void
onTaskRemoved
(
Intent
rootIntent
)
{
this
.
stopSelf
();
super
.
onTaskRemoved
(
rootIntent
);
}
}
private
class
PostLocationTask
extends
AsyncTask
<
Object
,
Integer
,
Boolean
>
{
private
class
PostLocationTask
extends
AsyncTask
<
Object
,
Integer
,
Boolean
>
{
...
@@ -284,8 +488,6 @@ public class LocationUpdateService extends Service implements LocationListener {
...
@@ -284,8 +488,6 @@ public class LocationUpdateService extends Service implements LocationListener {
@Override
@Override
protected
void
onPostExecute
(
Boolean
result
)
{
protected
void
onPostExecute
(
Boolean
result
)
{
Log
.
d
(
TAG
,
"PostLocationTask#onPostExecture"
);
Log
.
d
(
TAG
,
"PostLocationTask#onPostExecture"
);
notification
=
buildNotification
();
notificationManager
.
notify
(
NOTIFICATION_ID
,
notification
);
}
}
}
}
}
}
src/android/android-support-v4.jar
deleted
100644 → 0
View file @
50ff5a81
File deleted
www/BackgroundGeoLocation.js
View file @
37f2cd9f
var
exec
=
require
(
"
cordova/exec
"
);
var
exec
=
require
(
"
cordova/exec
"
);
module
.
exports
=
{
module
.
exports
=
{
configure
:
function
(
success
,
failure
,
config
)
{
configure
:
function
(
success
,
failure
,
config
)
{
var
authToken
=
config
.
auth
_t
oken
||
'
BackgroundGeoLocation_auth_token
'
,
var
authToken
=
config
.
auth
T
oken
||
'
BackgroundGeoLocation_auth_token
'
,
url
=
config
.
url
||
'
BackgroundGeoLocation_url
'
,
url
=
config
.
url
||
'
BackgroundGeoLocation_url
'
,
stationaryRadius
=
(
config
.
stationaryRadius
>=
0
)
?
config
.
stationaryRadius
:
50
,
// meters
stationaryRadius
=
(
config
.
stationaryRadius
>=
0
)
?
config
.
stationaryRadius
:
50
,
// meters
distanceFilter
=
(
config
.
distanceFilter
>=
0
)
?
config
.
distanceFilter
:
500
,
// meters
distanceFilter
=
(
config
.
distanceFilter
>=
0
)
?
config
.
distanceFilter
:
500
,
// meters
...
...
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