Commit 1f7459c5 authored by Chris Scott's avatar Chris Scott

Merge pull request #1 from christocracy/android

Android
parents 3f569226 2038b1c0
.DS_Store
......@@ -18,13 +18,32 @@
<!-- android -->
<platform name="android">
<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/bgloc" />
<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/bgloc/data" />
<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/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/android-support-v4.jar" target-dir="libs" />
<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 target="AndroidManifest.xml" parent="/manifest">
<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_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.INTERNET" />
</config-file>
<config-file target="res/xml/config.xml" parent="/*">
<feature name="BackgroundGeoLocation">
<param name="android-package" value="CDVBackgroundGeolocation"/>
<param name="android-package" value="com.tenforwardconsulting.cordova.bgloc.BackgroundGpsPlugin"/>
</feature>
</config-file>
<source-file src="src/android/CDVBackgroundGeoLocation.java" target-dir="src/org/transistorsoft/background-geolocation" />
</config-file>
</platform>
<platform name="ios">
......
package com.tenforwardconsulting.cordova.bgloc;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import android.app.Activity;
import android.content.Intent;
public class BackgroundGpsPlugin extends CordovaPlugin {
public static final String ACTION_START = "start";
public static final String ACTION_STOP = "stop";
public static final String ACTION_CONFIGURE = "configure";
private String authToken;
private String url;
@Override
public boolean execute(String action, JSONArray data, CallbackContext callbackContext) {
Activity activity = this.cordova.getActivity();
Intent updateServiceIntent = new Intent(activity, LocationUpdateService.class);
if (ACTION_START.equalsIgnoreCase(action)) {
if (authToken == null || url == null) {
callbackContext.error("Call configure before calling start");
return false;
}
updateServiceIntent.putExtra("authToken", authToken);
updateServiceIntent.putExtra("url", url);
activity.startService(updateServiceIntent);
} else if (ACTION_STOP.equalsIgnoreCase(action)) {
activity.stopService(updateServiceIntent);
} else if (ACTION_CONFIGURE.equalsIgnoreCase(action)) {
try {
this.authToken = data.getString(0);
this.url = data.getString(1);
} catch (JSONException e) {
callbackContext.error("authToken/url required as parameters: " + e.getMessage());
return false;
}
}
return true;
}
}
This diff is collapsed.
package com.tenforwardconsulting.cordova.bgloc.data;
import android.content.Context;
import com.tenforwardconsulting.cordova.bgloc.data.sqlite.SQLiteLocationDAO;
public abstract class DAOFactory {
public static LocationDAO createLocationDAO(Context context) {
//Very basic for now
return new SQLiteLocationDAO(context);
}
}
package com.tenforwardconsulting.cordova.bgloc.data;
import java.util.Date;
import android.os.SystemClock;
public class Location {
private String latitude;
private String longitude;
private Date recordedAt;
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getLatitude() {
return latitude;
}
public void setLatitude(String latitude) {
this.latitude = latitude;
}
public String getLongitude() {
return longitude;
}
public void setLongitude(String longitude) {
this.longitude = longitude;
}
public Date getRecordedAt() {
return recordedAt;
}
public void setRecordedAt(Date recordedAt) {
this.recordedAt = recordedAt;
}
public static Location fromAndroidLocation(android.location.Location originalLocation) {
Location location = new Location();
location.setRecordedAt(new Date(originalLocation.getTime()));
location.setLongitude(String.valueOf(originalLocation.getLongitude()));
location.setLatitude(String.valueOf(originalLocation.getLatitude()));
return location;
}
}
package com.tenforwardconsulting.cordova.bgloc.data;
public interface LocationDAO {
public Location[] getAllLocations();
public boolean persistLocation(Location l);
public void deleteLocation(Location l);
}
package com.tenforwardconsulting.cordova.bgloc.data.sqlite;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class LocationOpenHelper extends SQLiteOpenHelper {
private static final String SQLITE_DATABASE_NAME = "cordova_bg_locations";
private static final int DATABASE_VERSION = 1;
public static final String LOCATION_TABLE_NAME = "location";
private static final String LOCATION_TABLE_COLUMNS =
" id INTEGER PRIMARY KEY AUTOINCREMENT," +
" recordedAt TEXT," +
" latitude TEXT," +
" longitude TEXT";
private static final String LOCATION_TABLE_CREATE =
"CREATE TABLE " + LOCATION_TABLE_NAME + " (" +
LOCATION_TABLE_COLUMNS +
");";
LocationOpenHelper(Context context) {
super(context, SQLITE_DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(LOCATION_TABLE_CREATE);
Log.d(this.getClass().getName(), LOCATION_TABLE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
\ No newline at end of file
package com.tenforwardconsulting.cordova.bgloc.data.sqlite;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import com.tenforwardconsulting.cordova.bgloc.data.Location;
import com.tenforwardconsulting.cordova.bgloc.data.LocationDAO;
public class SQLiteLocationDAO implements LocationDAO {
public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
private static final String TAG = "SQLiteLocationDAO";
private Context context;
public SQLiteLocationDAO(Context context) {
this.context = context;
}
public Location[] getAllLocations() {
SQLiteDatabase db = null;
Cursor c = null;
List<Location> all = new ArrayList<Location>();
try {
db = new LocationOpenHelper(context).getReadableDatabase();
c = db.query(LocationOpenHelper.LOCATION_TABLE_NAME, null, null, null, null, null, null);
while (c.moveToNext()) {
all.add(hydrate(c));
}
} finally {
if (c != null) {
c.close();
}
if (db != null) {
db.close();
}
}
return all.toArray(new Location[all.size()]);
}
public boolean persistLocation(Location location) {
SQLiteDatabase db = new LocationOpenHelper(context).getWritableDatabase();
db.beginTransaction();
ContentValues values = getContentValues(location);
long rowId = db.insert(LocationOpenHelper.LOCATION_TABLE_NAME, null, values);
Log.d(TAG, "After insert, rowId = " + rowId);
db.setTransactionSuccessful();
db.endTransaction();
db.close();
if (rowId > -1) {
location.setId(rowId);
return true;
} else {
return false;
}
}
public void deleteLocation(Location location) {
SQLiteDatabase db = new LocationOpenHelper(context).getWritableDatabase();
db.beginTransaction();
db.delete(LocationOpenHelper.LOCATION_TABLE_NAME, "id = ?", new String[]{location.getId().toString()});
db.setTransactionSuccessful();
db.endTransaction();
db.close();
}
private Location hydrate(Cursor c) {
Location l = new Location();
l.setId(c.getLong(c.getColumnIndex("id")));
l.setRecordedAt(stringToDate(c.getString(c.getColumnIndex("recordedAt"))));
l.setLatitude(c.getString(c.getColumnIndex("latitude")));
l.setLongitude(c.getString(c.getColumnIndex("longitude")));
return l;
}
private ContentValues getContentValues(Location location) {
ContentValues values = new ContentValues();
values.put("latitude", location.getLatitude());
values.put("longitude", location.getLongitude());
values.put("recordedAt", dateToString(location.getRecordedAt()));
return values;
}
public Date stringToDate(String dateTime) {
SimpleDateFormat iso8601Format = new SimpleDateFormat(DATE_FORMAT);
Date date = null;
try {
date = iso8601Format.parse(dateTime);
} catch (ParseException e) {
Log.e("DBUtil", "Parsing ISO8601 datetime ("+ dateTime +") failed", e);
}
return date;
}
public String dateToString(Date date) {
SimpleDateFormat iso8601Format = new SimpleDateFormat(DATE_FORMAT);
return iso8601Format.format(date);
}
}
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