Commit a940ddd8 authored by Brian Samson's avatar Brian Samson

plugin installs and is working with previous api

parent c7d0d19c
...@@ -18,16 +18,18 @@ ...@@ -18,16 +18,18 @@
<!-- android --> <!-- android -->
<platform name="android"> <platform name="android">
<source-file src="src/android/BackgroundGpsPlugin.java" target-dir="src/com/tenforwardconsulting/cordova/backgroundgeolocation" /> <source-file src="src/android/BackgroundGpsPlugin.java" target-dir="src/com/tenforwardconsulting/cordova/bgloc" />
<source-file src="src/android/LocationUpdateService.java" target-dir="src/com/tenforwardconsulting/cordova/backgroundgeolocation" /> <source-file src="src/android/LocationUpdateService.java" target-dir="src/com/tenforwardconsulting/cordova/bgloc" />
<source-file src="src/android/data/DAOFactory.java" target-dir="src/com/tenforwardconsulting/cordova/backgroundgeolocation" /> <source-file src="src/android/data/DAOFactory.java" target-dir="src/com/tenforwardconsulting/cordova/bgloc/data" />
<source-file src="src/android/data/Location.java" target-dir="src/com/tenforwardconsulting/cordova/backgroundgeolocation" /> <source-file src="src/android/data/Location.java" target-dir="src/com/tenforwardconsulting/cordova/bgloc/data" />
<source-file src="src/android/data/LocationDAO.java" target-dir="src/com/tenforwardconsulting/cordova/backgroundgeolocation" /> <source-file src="src/android/data/LocationDAO.java" target-dir="src/com/tenforwardconsulting/cordova/bgloc/data" />
<source-file src="src/android/data/sqlite/LocationOpenHelper.java" target-dir="src/com/tenforwardconsulting/cordova/backgroundgeolocation" /> <source-file src="src/android/data/sqlite/LocationOpenHelper.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/backgroundgeolocation" /> <source-file src="src/android/data/sqlite/SQLiteLocationDAO.java" target-dir="src/com/tenforwardconsulting/cordova/bgloc/data/sqlite" />
<config-file target="AndroidManifest.xml" parent="/manifest/application"> <source-file src="src/android/android-support-v4.jar" target-dir="libs" />
<service android:enabled="true" android:name="com.tenforwardconsulting.cordova.backgroundgeolocation.LocationUpdateService" android:process=":remote" />
<config-file target="AndroidManifest.xml" parent="/manifest/application">
<service android:enabled="true" android:name="com.tenforwardconsulting.cordova.bgloc.LocationUpdateService" android:process=":remote" />
</config-file> </config-file>
<config-file target="AndroidManifest.xml" parent="/manifest"> <config-file target="AndroidManifest.xml" parent="/manifest">
...@@ -38,7 +40,7 @@ ...@@ -38,7 +40,7 @@
</config-file> </config-file>
<config-file target="res/xml/config.xml" parent="/*"> <config-file target="res/xml/config.xml" parent="/*">
<feature name="BackgroundGeoLocation"> <feature name="BackgroundGeoLocation">
<param name="android-package" value="com.tenforwardconsulting.cordova.background-geolocation"/> <param name="android-package" value="com.tenforwardconsulting.cordova.bgloc.BackgroundGpsPlugin"/>
</feature> </feature>
</config-file> </config-file>
</platform> </platform>
......
package com.tenforwardconsulting.cordova.backgroundgeolocation; package com.tenforwardconsulting.cordova.bgloc;
import org.apache.cordova.api.CallbackContext; import org.apache.cordova.CallbackContext;
import org.apache.cordova.api.CordovaPlugin; import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
......
package com.tenforwardconsulting.cordova.backgroundgeolocation; package com.tenforwardconsulting.cordova.bgloc;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPost;
...@@ -6,9 +6,10 @@ import org.apache.http.entity.StringEntity; ...@@ -6,9 +6,10 @@ import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject; import org.json.JSONObject;
import com.tenforwardconsulting.cordova.backgroundgeolocation.data.DAOFactory; import com.tenforwardconsulting.cordova.bgloc.data.DAOFactory;
import com.tenforwardconsulting.cordova.backgroundgeolocation.data.LocationDAO; import com.tenforwardconsulting.cordova.bgloc.data.LocationDAO;
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.support.v4.app.NotificationCompat;
...@@ -110,11 +111,15 @@ public class LocationUpdateService extends Service implements LocationListener { ...@@ -110,11 +111,15 @@ public class LocationUpdateService extends Service implements LocationListener {
PackageManager pm = this.getPackageManager(); PackageManager pm = this.getPackageManager();
Intent notificationIntent = pm.getLaunchIntentForPackage(this.getPackageName()); Intent notificationIntent = pm.getLaunchIntentForPackage(this.getPackageName());
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
Application application = this.getApplication();
int backgroundIconId = application.getResources().getIdentifier("background_notification", "drawable", application.getPackageName());
int appNameId = application.getResources().getIdentifier("app_name", "string", application.getPackageName());
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this) NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification) .setSmallIcon(backgroundIconId)
.setContentTitle(this.getString(R.string.app_name)) .setContentTitle(this.getString(appNameId))
.setOngoing(true) .setOngoing(true)
.setContentIntent(contentIntent) .setContentIntent(contentIntent)
.setWhen(System.currentTimeMillis()); .setWhen(System.currentTimeMillis());
...@@ -164,7 +169,7 @@ public class LocationUpdateService extends Service implements LocationListener { ...@@ -164,7 +169,7 @@ public class LocationUpdateService extends Service implements LocationListener {
Log.d(TAG, "afterexecute " + task.getStatus()); Log.d(TAG, "afterexecute " + task.getStatus());
} }
private boolean postLocation(com.tenforwardconsulting.cordova.backgroundgeolocation.data.Location l) { private boolean postLocation(com.tenforwardconsulting.cordova.bgloc.data.Location l) {
if (l == null) { if (l == null) {
Log.w(TAG, "postLocation: null location"); Log.w(TAG, "postLocation: null location");
return false; return false;
...@@ -203,8 +208,8 @@ public class LocationUpdateService extends Service implements LocationListener { ...@@ -203,8 +208,8 @@ public class LocationUpdateService extends Service implements LocationListener {
} }
} }
private void persistLocation(Location location) { private void persistLocation(Location location) {
LocationDAO dao = DAOFactory.createLocationDAO(); LocationDAO dao = DAOFactory.createLocationDAO(this.getApplicationContext());
com.tenforwardconsulting.cordova.backgroundgeolocation.data.Location savedLocation = com.tenforwardconsulting.cordova.backgroundgeolocation.data.Location.fromAndroidLocation(location); com.tenforwardconsulting.cordova.bgloc.data.Location savedLocation = com.tenforwardconsulting.cordova.bgloc.data.Location.fromAndroidLocation(location);
if (dao.persistLocation(savedLocation)) { if (dao.persistLocation(savedLocation)) {
Log.d(TAG, "Persisted Location: " + savedLocation); Log.d(TAG, "Persisted Location: " + savedLocation);
...@@ -239,7 +244,7 @@ public class LocationUpdateService extends Service implements LocationListener { ...@@ -239,7 +244,7 @@ public class LocationUpdateService extends Service implements LocationListener {
Log.d(TAG, "#timestamp " + System.currentTimeMillis()); Log.d(TAG, "#timestamp " + System.currentTimeMillis());
if (lastUpdateTime + 5*60*1000 < SystemClock.elapsedRealtime()) { if (lastUpdateTime + 5*60*1000 < SystemClock.elapsedRealtime()) {
Log.d(TAG, "5 minutes, forcing update with last location"); Log.d(TAG, "5 minutes, forcing update with last location");
postLocation(com.tenforwardconsulting.cordova.backgroundgeolocation.Location.fromAndroidLocation( postLocation(com.tenforwardconsulting.cordova.bgloc.data.Location.fromAndroidLocation(
locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER))); locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER)));
} }
try { try {
...@@ -258,8 +263,8 @@ public class LocationUpdateService extends Service implements LocationListener { ...@@ -258,8 +263,8 @@ public class LocationUpdateService extends Service implements LocationListener {
@Override @Override
protected Boolean doInBackground(Object...objects) { protected Boolean doInBackground(Object...objects) {
Log.d(TAG, "Executing PostLocationTask#doInBackground"); Log.d(TAG, "Executing PostLocationTask#doInBackground");
LocationDAO locationDAO = DAOFactory.createLocationDAO(); LocationDAO locationDAO = DAOFactory.createLocationDAO(LocationUpdateService.this.getApplicationContext());
for (com.tenforwardconsulting.cordova.backgroundgeolocation.data.Location savedLocation : locationDAO.getAllLocations()) { for (com.tenforwardconsulting.cordova.bgloc.data.Location savedLocation : locationDAO.getAllLocations()) {
Log.d(TAG, "Posting saved location"); Log.d(TAG, "Posting saved location");
if (postLocation(savedLocation)) { if (postLocation(savedLocation)) {
locationDAO.deleteLocation(savedLocation); locationDAO.deleteLocation(savedLocation);
......
package com.tenforwardconsulting.cordova.backgroundgeolocation.data; package com.tenforwardconsulting.cordova.bgloc.data;
import com.tenforwardconsulting.cordova.backgroundgeolocation.data.sqlite.SQLiteLocationDAO; import android.content.Context;
import com.tenforwardconsulting.cordova.bgloc.data.sqlite.SQLiteLocationDAO;
public abstract class DAOFactory { public abstract class DAOFactory {
public static LocationDAO createLocationDAO() { public static LocationDAO createLocationDAO(Context context) {
//Very basic for now //Very basic for now
return new SQLiteLocationDAO(); return new SQLiteLocationDAO(context);
} }
} }
package com.tenforwardconsulting.cordova.backgroundgeolocation.data; package com.tenforwardconsulting.cordova.bgloc.data;
import java.util.Date; import java.util.Date;
......
package com.tenforwardconsulting.cordova.backgroundgeolocation.data; package com.tenforwardconsulting.cordova.bgloc.data;
public interface LocationDAO { public interface LocationDAO {
public Location[] getAllLocations(); public Location[] getAllLocations();
......
package com.tenforwardconsulting.cordova.backgroundgeolocation; package com.tenforwardconsulting.cordova.bgloc.data.sqlite;
import com.geocrowd.android.core.GeoCrowdApplication;
import android.content.Context; import android.content.Context;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log; import android.util.Log;
public class LocationOpenHelper extends SQLiteOpenHelper { public class LocationOpenHelper extends SQLiteOpenHelper {
private static final String SQLITE_DATABASE_NAME = "cordova_bg_locations";
private static final int DATABASE_VERSION = 1; private static final int DATABASE_VERSION = 1;
public static final String LOCATION_TABLE_NAME = "location"; public static final String LOCATION_TABLE_NAME = "location";
private static final String LOCATION_TABLE_COLUMNS = private static final String LOCATION_TABLE_COLUMNS =
...@@ -23,8 +21,8 @@ public class LocationOpenHelper extends SQLiteOpenHelper { ...@@ -23,8 +21,8 @@ public class LocationOpenHelper extends SQLiteOpenHelper {
");"; ");";
LocationOpenHelper(Context context) { LocationOpenHelper(Context context) {
super(context, GeoCrowdApplication.SQLITE_DATABASE_NAME, null, DATABASE_VERSION); super(context, SQLITE_DATABASE_NAME, null, DATABASE_VERSION);
} }
@Override @Override
public void onCreate(SQLiteDatabase db) { public void onCreate(SQLiteDatabase db) {
......
package com.tenforwardconsulting.cordova.backgroundgeolocation.data.sqlite; package com.tenforwardconsulting.cordova.bgloc.data.sqlite;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
...@@ -7,22 +7,29 @@ import java.util.Date; ...@@ -7,22 +7,29 @@ import java.util.Date;
import java.util.List; import java.util.List;
import android.content.ContentValues; import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor; import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.util.Log; import android.util.Log;
import com.tenforwardconsulting.cordova.backgroundgeolocation.data.Location; import com.tenforwardconsulting.cordova.bgloc.data.Location;
import com.tenforwardconsulting.cordova.backgroundgeolocation.LocationDAO; import com.tenforwardconsulting.cordova.bgloc.data.LocationDAO;
public class SQLiteLocationDAO implements LocationDAO { public class SQLiteLocationDAO implements LocationDAO {
public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss"; public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
private static final String TAG = "SQLiteLocationDAO"; private static final String TAG = "SQLiteLocationDAO";
private Context context;
public SQLiteLocationDAO(Context context) {
this.context = context;
}
public Location[] getAllLocations() { public Location[] getAllLocations() {
SQLiteDatabase db = null; SQLiteDatabase db = null;
Cursor c = null; Cursor c = null;
List<Location> all = new ArrayList<Location>(); List<Location> all = new ArrayList<Location>();
try { try {
db = new LocationOpenHelper(GeoCrowdApplication.getApplication().getApplicationContext()).getReadableDatabase(); db = new LocationOpenHelper(context).getReadableDatabase();
c = db.query(LocationOpenHelper.LOCATION_TABLE_NAME, null, null, null, null, null, null); c = db.query(LocationOpenHelper.LOCATION_TABLE_NAME, null, null, null, null, null, null);
while (c.moveToNext()) { while (c.moveToNext()) {
all.add(hydrate(c)); all.add(hydrate(c));
...@@ -39,7 +46,7 @@ public class SQLiteLocationDAO implements LocationDAO { ...@@ -39,7 +46,7 @@ public class SQLiteLocationDAO implements LocationDAO {
} }
public boolean persistLocation(Location location) { public boolean persistLocation(Location location) {
SQLiteDatabase db = new LocationOpenHelper(GeoCrowdApplication.getApplication().getApplicationContext()).getWritableDatabase(); SQLiteDatabase db = new LocationOpenHelper(context).getWritableDatabase();
db.beginTransaction(); db.beginTransaction();
ContentValues values = getContentValues(location); ContentValues values = getContentValues(location);
long rowId = db.insert(LocationOpenHelper.LOCATION_TABLE_NAME, null, values); long rowId = db.insert(LocationOpenHelper.LOCATION_TABLE_NAME, null, values);
...@@ -57,7 +64,7 @@ public class SQLiteLocationDAO implements LocationDAO { ...@@ -57,7 +64,7 @@ public class SQLiteLocationDAO implements LocationDAO {
} }
public void deleteLocation(Location location) { public void deleteLocation(Location location) {
SQLiteDatabase db = new LocationOpenHelper(GeoCrowdApplication.getApplication().getApplicationContext()).getWritableDatabase(); SQLiteDatabase db = new LocationOpenHelper(context).getWritableDatabase();
db.beginTransaction(); db.beginTransaction();
db.delete(LocationOpenHelper.LOCATION_TABLE_NAME, "id = ?", new String[]{location.getId().toString()}); db.delete(LocationOpenHelper.LOCATION_TABLE_NAME, "id = ?", new String[]{location.getId().toString()});
db.setTransactionSuccessful(); db.setTransactionSuccessful();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment