diff --git a/android-app/src/main/AndroidManifest.xml b/android-app/src/main/AndroidManifest.xml index 2002c47..1315c0f 100644 --- a/android-app/src/main/AndroidManifest.xml +++ b/android-app/src/main/AndroidManifest.xml @@ -69,6 +69,7 @@ - + + diff --git a/android-app/src/main/java/com/maptology/android/app/PositionService.java b/android-app/src/main/java/com/maptology/android/app/LoggerService.java similarity index 74% rename from android-app/src/main/java/com/maptology/android/app/PositionService.java rename to android-app/src/main/java/com/maptology/android/app/LoggerService.java index 4e4f0b9..084ab38 100644 --- a/android-app/src/main/java/com/maptology/android/app/PositionService.java +++ b/android-app/src/main/java/com/maptology/android/app/LoggerService.java @@ -24,8 +24,6 @@ import androidx.core.content.pm.PackageInfoCompat; import com.maptology.android.app.util.DeltaCodec; import com.maptology.android.feign.model.Coordinate; import com.maptology.android.feign.model.DeviceHandle; -import com.maptology.android.feign.model.LocationUpdate; -import com.maptology.android.feign.model.Position; import com.maptology.android.feign.model.ReleasedLocation; import java.util.ArrayList; @@ -36,12 +34,12 @@ import java.util.List; * * @author Marcus */ -public class PositionService extends Service { +public class LoggerService extends Service { /** * The log tag for the service. */ - private static final String TAG = "PositionService"; + private static final String TAG = "LoggerService"; /** * The tag for the cpu wakelock. @@ -69,7 +67,7 @@ public class PositionService extends Service { * The amount of time between the creation of a * position update in milliseconds. */ - private int UPDATE_MILLISECONDS = 60000; + private int UPDATE_DELAY = 60000; /** * A scale factor to ensure that there are enough data bins, @@ -77,37 +75,27 @@ public class PositionService extends Service { * exactly the right frequency. 2 means that we provide twice * as many data bins as we would require if the timing is perfect. */ - private int UPDATE_LIMIT_FACTOR = 2; + private int UPDATE_FACTOR = 2; /** - * A gps listener + * A gps listener that memorizes a number of locations. */ - private LocationListener gpsListener = new PositionListener(LocationManager.GPS_PROVIDER, UPDATE_MILLISECONDS * UPDATE_LIMIT_FACTOR / SAMPLING_DELAY_GPS); - - /** - * A flag to determine whether the gps listener is registered. - */ - private boolean gpsRegistered = false; + private GpsListener gpsListener = new GpsListener(LocationManager.GPS_PROVIDER, UPDATE_DELAY * UPDATE_FACTOR / SAMPLING_DELAY_GPS); /** * A listener that is attached to the acceleration sensor. */ - private SensorEventListener accelerationListener = new SensorListener(3, 7, UPDATE_MILLISECONDS * UPDATE_LIMIT_FACTOR / SAMPLING_DELAY_SENSOR); + private SensorListener accelerationListener = new SensorListener(3, PRECISION_SENSOR, UPDATE_DELAY * UPDATE_FACTOR / SAMPLING_DELAY_SENSOR); /** * A listener that is attached to the gyroscope sensor. */ - private SensorEventListener gyroscopeListener = new SensorListener(3, 7, UPDATE_MILLISECONDS * UPDATE_LIMIT_FACTOR / SAMPLING_DELAY_SENSOR); + private SensorListener gyroscopeListener = new SensorListener(3, PRECISION_SENSOR, UPDATE_DELAY * UPDATE_FACTOR / SAMPLING_DELAY_SENSOR); /** * A listener that is attached to the magnetometer sensor. */ - private SensorEventListener magnetometerListener = new SensorListener(3, 7, UPDATE_MILLISECONDS * UPDATE_LIMIT_FACTOR / SAMPLING_DELAY_SENSOR); - - /** - * A flag to indicate whether the sensor listeners are registered. - */ - private boolean sensorsRegistered = false; + private SensorListener magnetometerListener = new SensorListener(3, PRECISION_SENSOR, UPDATE_DELAY * UPDATE_FACTOR / SAMPLING_DELAY_SENSOR); /** * The cpu wake lock. @@ -161,48 +149,134 @@ public class PositionService extends Service { super.onCreate(); app = (MaptologyApp)getApplicationContext(); locationManager = (LocationManager)getSystemService(LOCATION_SERVICE); + sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE); PowerManager pm = (PowerManager)getSystemService(POWER_SERVICE); wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_TAG); wakeLock.acquire(); + prepareCapture(); + } + + /** + * Flushes all listeners and registers the listeners that are not registered + * already and can be registered due to sensor availability and schedules the + * next capturing iteration. + */ + public void prepareCapture() { + flushListeners(); + registerSensorListeners(); + registerGpsListener(); + Log.d(TAG, "Scheduling capture operation."); + handler.postDelayed(captureListener, UPDATE_DELAY); } public void onCapture() { + Log.d(TAG, "Performing capture operation."); + + + prepareCapture(); } @Override public void onDestroy() { + handler.removeCallbacks(captureListener); + unregisterGpsListener(); + unregisterSensorListeners(); if (wakeLock.isHeld()) wakeLock.release(); super.onDestroy(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { - return START_STICKY; } + /** + * Executes a flush operation on all listeners. + */ + private void flushListeners() { + gpsListener.flush(); + accelerationListener.flush(); + magnetometerListener.flush(); + gyroscopeListener.flush(); + } + /** * Registers the gps listener, if that is possible and did not happen already. */ private void registerGpsListener() { - if (!gpsRegistered && canUseGps()) { + if (!gpsListener.isRegistered() && canUseGps()) { try { - locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, gpsListener); - gpsRegistered = true; + locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, SAMPLING_DELAY_GPS, 0, gpsListener); + gpsListener.setRegistered(true); } catch (SecurityException e) { Log.e(TAG, "Failed to register gps listener.", e); - gpsRegistered = false; } } } - private void registerSensorListeners() { - - - + /** + * Unregisters the gps listener, if it was registered successfully already. + */ + private void unregisterGpsListener() { + if (gpsListener.isRegistered()) { + locationManager.removeUpdates(gpsListener); + gpsListener.setRegistered(false); + } } + /** + * Registers all sensor listeners for the sensors that are available. + */ + private void registerSensorListeners() { + registerSensorListener(Sensor.TYPE_ACCELEROMETER, accelerationListener); + registerSensorListener(Sensor.TYPE_GYROSCOPE, gyroscopeListener); + registerSensorListener(Sensor.TYPE_MAGNETIC_FIELD, magnetometerListener); + } + + + /** + * Registers the sensor listener for the specified sensor type, if the sensor + * is available. + * + * @param sensorType The sensor type. + * @param listener The listener. + */ + private void registerSensorListener(int sensorType, SensorListener listener) { + if (listener.isRegistered()) return; + Sensor sensor = sensorManager.getDefaultSensor(sensorType); + if (sensor != null) { + int period = SAMPLING_DELAY_SENSOR * 1000; + if (Build.VERSION.SDK_INT > 21) { + period = Math.max(sensor.getMinDelay(), Math.min(sensor.getMaxDelay(), period)); + } + boolean registered = sensorManager.registerListener(listener, sensor, period); + listener.setRegistered(registered); + } + } + + /** + * Unregisters all sensor listeners that are registered. + */ + private void unregisterSensorListeners() { + unregisterSensorListener(accelerationListener); + unregisterSensorListener(gyroscopeListener); + unregisterSensorListener(magnetometerListener); + } + + /** + * Unregisters a particular sensor listener, if it was registered already. + * + * @param listener The listener to unregister. + */ + private void unregisterSensorListener(SensorListener listener) { + if (listener.isRegistered()) { + sensorManager.unregisterListener(listener); + listener.setRegistered(false); + } + } + + /** * Determines whether the location is analyzable. * @@ -266,7 +340,7 @@ public class PositionService extends Service { * * @author Marcus */ - private class PositionListener implements LocationListener { + private class GpsListener implements LocationListener { /** * The flag for a valid accuracy. @@ -322,6 +396,12 @@ public class PositionService extends Service { */ private String provider; + /** + * A flag to indicate whether the listener is + * registered. + */ + private boolean registered; + /** * Creates a position listener with * the specified limit on positions. @@ -329,8 +409,9 @@ public class PositionService extends Service { * @param limit The limit. * @param provider The location provider name. */ - public PositionListener(String provider, int limit) { + GpsListener(String provider, int limit) { int values = Math.max(1, limit); + this.provider = provider; cache = new ArrayList<>(values); data = new ArrayList<>(values); codec = new DeltaCodec(11); @@ -447,6 +528,25 @@ public class PositionService extends Service { location.setVersion((int)getVersion()); location.setProvider(provider); } + + + /** + * Determines whether the listener is registered. + * + * @return True if registered, false otherwise. + */ + public boolean isRegistered() { + return registered; + } + + /** + * Sets the flag to indicate whether the listener is registered. + * + * @param registered True if registered, false otherwise. + */ + public void setRegistered(boolean registered) { + this.registered = registered; + } }; @@ -476,6 +576,11 @@ public class PositionService extends Service { */ private List data; + /** + * A flag to indicate whether the listener is registered. + */ + private boolean registered; + /** * Creates a new sensor listener that will buffer * at most limit values @@ -548,6 +653,24 @@ public class PositionService extends Service { cache.addAll(data); data.clear(); } + + /** + * Determines whether the listener is registered. + * + * @return True if registered, false otherwise. + */ + public boolean isRegistered() { + return registered; + } + + /** + * Sets the flag to indicate whether the listener is registered. + * + * @param registered True if registered, false otherwise. + */ + public void setRegistered(boolean registered) { + this.registered = registered; + } } } diff --git a/android-app/src/main/java/com/maptology/android/app/location/LocationContract.java b/android-app/src/main/java/com/maptology/android/app/location/LocationContract.java index 7c4c744..0513c65 100644 --- a/android-app/src/main/java/com/maptology/android/app/location/LocationContract.java +++ b/android-app/src/main/java/com/maptology/android/app/location/LocationContract.java @@ -72,6 +72,33 @@ public class LocationContract { public static final String VERSION = "version"; } + /** + * The table to store the metadata for updates. + */ + public class Metadata { + /** + * The name of the table. + */ + public static final String TABLE = "location_metadata"; + /** + * The id of the row. + */ + public static final String ID = "_id"; + /** + * The id of the update to which this metadata refers to. + */ + public static final String UPDATE_ID = "update_id"; + /** + * The tag describing the metadata type. + */ + public static final String TAG = "tag"; + /** + * The value containing the actual data. + */ + public static final String VALUE = "value"; + } + + /** * The table to store locations with their associated * networks so that we do not have to compute them diff --git a/android-app/src/main/java/com/maptology/android/app/location/LocationHelper.java b/android-app/src/main/java/com/maptology/android/app/location/LocationHelper.java index 06d5d75..c7020bf 100644 --- a/android-app/src/main/java/com/maptology/android/app/location/LocationHelper.java +++ b/android-app/src/main/java/com/maptology/android/app/location/LocationHelper.java @@ -5,6 +5,8 @@ import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; +import com.maptology.android.app.notice.NoticeContract; + /** * Implements an sqlite open helper for the the location db. * @@ -99,6 +101,33 @@ public class LocationHelper extends SQLiteOpenHelper { */ private static String SQL_DROP_UPDATE = "DROP TABLE IF EXISTS " + LocationContract.Update.TABLE; + /** + * Creates the metadata table. + */ + private static String SQL_CREATE_METADATA = "CREATE TABLE " + LocationContract.Metadata.TABLE + " ( " + + LocationContract.Metadata.ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + + LocationContract.Metadata.TAG + " TEXT, " + + LocationContract.Metadata.VALUE + " TEXT, " + + LocationContract.Metadata.UPDATE_ID + " INTEGER NOT NULL " + + "REFERENCES " + LocationContract.Update.TABLE + " ( " + LocationContract.Update.ID + " ) ON DELETE CASCADE" + + " )"; + + /** + * Drops the metadata table. + */ + private static String SQL_DROP_METADATA = "DROP TABLE IF EXISTS " + LocationContract.Metadata.TABLE; + + /** + * Creates an index over the metadata update id to resolve the foreign key. + */ + private static String SQL_INDEX_METADATA_UI = "CREATE INDEX " + LocationContract.Metadata.TABLE + "_UI " + + "ON " + LocationContract.Metadata.TABLE + " ( " +LocationContract.Metadata.UPDATE_ID +" )"; + + /** + * Drops the index on the metadata's update id. + */ + private static String SQL_DROP_METADATA_UI = "DROP INDEX IF EXISTS " + LocationContract.Metadata.TABLE + "_UI"; + /** * Creates a new helper from the context. * @@ -130,11 +159,13 @@ public class LocationHelper extends SQLiteOpenHelper { try { // create tables db.execSQL(SQL_CREATE_UPDATE); + db.execSQL(SQL_CREATE_METADATA); db.execSQL(SQL_CREATE_POSITION); db.execSQL(SQL_CREATE_NETWORK); // create indexes db.execSQL(SQL_INDEX_POSITION_UT); db.execSQL(SQL_INDEX_NETWORK_BS); + db.execSQL(SQL_INDEX_METADATA_UI); db.setTransactionSuccessful(); } finally { db.endTransaction(); @@ -156,11 +187,13 @@ public class LocationHelper extends SQLiteOpenHelper { db.beginTransaction(); try { // drop indexes + db.execSQL(SQL_DROP_METADATA_UI); db.execSQL(SQL_DROP_NETWORK_BS); db.execSQL(SQL_DROP_POSITION_UT); // drop tables db.execSQL(SQL_DROP_NETWORK); db.execSQL(SQL_DROP_POSITION); + db.execSQL(SQL_DROP_METADATA); db.execSQL(SQL_DROP_UPDATE); db.setTransactionSuccessful(); } finally { diff --git a/android-app/src/main/java/com/maptology/android/app/location/LocationStore.java b/android-app/src/main/java/com/maptology/android/app/location/LocationStore.java index 074439a..a681187 100644 --- a/android-app/src/main/java/com/maptology/android/app/location/LocationStore.java +++ b/android-app/src/main/java/com/maptology/android/app/location/LocationStore.java @@ -1,7 +1,9 @@ package com.maptology.android.app.location; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import com.maptology.android.feign.model.Coordinate; import com.maptology.android.feign.model.ReleasedLocation; @@ -98,6 +100,13 @@ public class LocationStore { return getCreatedId(LocationContract.Network.TABLE); } + /** + * Returns the last created update id. + * + * @return The last created update id. + */ + private long getUpdateId() { return getCreatedId(LocationContract.Update.TABLE); } + /** * Returns the last created id of the specified table. * @@ -358,6 +367,25 @@ public class LocationStore { values.put(LocationContract.Update.SHARABLE, location.isSharable()==null?0:(location.isSharable()?1:0)); values.put(LocationContract.Update.VERSION, location.getVersion()==null?0:location.getVersion()); db.insert(LocationContract.Update.TABLE, null, values); + addMetadata(getUpdateId(), location.getMetadata()); + } + + /** + * Stores the meta for the update id. + * + * @param updateId The update id. + * @param metadata The metadata to store. + */ + private void addMetadata(long updateId, Map metadata) { + if (metadata != null) { + for (Map.Entry entry: metadata.entrySet()) { + ContentValues values = new ContentValues(); + values.put(LocationContract.Metadata.UPDATE_ID, updateId); + values.put(LocationContract.Metadata.TAG, entry.getKey()); + values.put(LocationContract.Metadata.VALUE, entry.getValue()); + db.insert(LocationContract.Metadata.TABLE, null, values); + } + } } /** @@ -403,11 +431,35 @@ public class LocationStore { cor.setLongitude(c.getDouble(11)); r.setCoordinate(cor); } + r.setMetadata(getMetadata(id)); } c.close();; return r; } + /** + * Returns the metadata stored for the update. + * + * @param updateId The update id. + * @return The metadata (never null). + */ + private Map getMetadata(long updateId) { + Map result = new HashMap<>(); + if (db != null) return result; + Cursor c = db.query(LocationContract.Metadata.TABLE, new String[]{ + LocationContract.Metadata.TAG, + LocationContract.Metadata.VALUE + }, LocationContract.Metadata.UPDATE_ID + " = " + updateId, null, null, null, null); + while (c.moveToNext()) { + String tag = c.isNull(0)?null:c.getString(0); + String value = c.isNull(1)?null:c.getString(1); + result.put(tag, value); + } + c.close(); + return result; + } + + /** * Removes the update with the specified id. * @@ -416,6 +468,17 @@ public class LocationStore { public void removeUpdate(long id) { if (db == null) return; db.execSQL("DELETE FROM " + LocationContract.Update.TABLE + " WHERE " + LocationContract.Update.ID + " = " + id); + removeMetadata(id); + } + + /** + * Removes the metadata fr the update with the specified id. + * + * @param updateId The id of the update. + */ + private void removeMetadata(long updateId) { + if (db == null) return; + db.execSQL("DELETE FROM " + LocationContract.Metadata.TABLE + " WHERE " + LocationContract.Metadata.UPDATE_ID + " = " + updateId); } /** diff --git a/build.gradle b/build.gradle index 88ee317..727cab7 100644 --- a/build.gradle +++ b/build.gradle @@ -52,7 +52,7 @@ allprojects { // android stuff versionAndroidx = '1.0.0' - versionRobolectric = '4.3' + versionRobolectric = '4.3.1' versionPlay = '17.0.0' versionAndroidxTest = '1.2.0' } diff --git a/common-api/src/main/java/com/maptology/api/location/LocationUpdate.java b/common-api/src/main/java/com/maptology/api/location/LocationUpdate.java index 084cc70..97eb7b8 100644 --- a/common-api/src/main/java/com/maptology/api/location/LocationUpdate.java +++ b/common-api/src/main/java/com/maptology/api/location/LocationUpdate.java @@ -18,7 +18,7 @@ import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; /** - * Represents a location update conisting of a batch of locations. + * Represents a location update consisting of a batch of locations. * * @author Marcus */ diff --git a/common-api/src/main/java/com/maptology/api/location/ReleasedLocation.java b/common-api/src/main/java/com/maptology/api/location/ReleasedLocation.java index 298aaaf..ad52507 100644 --- a/common-api/src/main/java/com/maptology/api/location/ReleasedLocation.java +++ b/common-api/src/main/java/com/maptology/api/location/ReleasedLocation.java @@ -1,5 +1,8 @@ package com.maptology.api.location; +import java.util.HashMap; +import java.util.Map; + import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlRootElement; @@ -29,41 +32,65 @@ public class ReleasedLocation extends DetailedLocation { @XmlAttribute(required=true) @ApiModelProperty(value="A boolean value to mark the update as analyzable to compute places and visits.", required=true) private boolean analyzable; + + /** + * Additional metadata about the position. + */ + @XmlAttribute(required=false) + @ApiModelProperty(value="Additional data about the position such as readings from other sensors.", required=false) + private Map metadata = new HashMap<>(); /** - * Returns the sharable. + * Returns the sharable flag. * - * @return The sharable. + * @return The sharable flag. */ public boolean isSharable() { return sharable; } /** - * Sets the sharable. + * Sets the sharable flag. * - * @param sharable The sharable to set. + * @param sharable The sharable flag to set. */ public void setSharable(boolean sharable) { this.sharable = sharable; } /** - * Returns the analyzable. + * Returns the analyzable flag. * - * @return The analyzable. + * @return The analyzable flag. */ public boolean isAnalyzable() { return analyzable; } /** - * Sets the analyzable. + * Sets the analyzable flag. * - * @param analyzable The analyzable to set. + * @param analyzable The analyzable flag to set. */ public void setAnalyzable(boolean analyzable) { this.analyzable = analyzable; } - + + /** + * Returns the meta data collected for the location. + * + * @return The meta data collected for the location. + */ + public Map getMetadata() { + return metadata; + } + + /** + * Sets the meta data collected for the location. + * + * @param metadata The meta data for the location. + */ + public void setMetadata(Map metadata) { + this.metadata = metadata; + } } diff --git a/service-ejb/src/main/java/com/maptology/service/session/PositionSessionBean.java b/service-ejb/src/main/java/com/maptology/service/session/PositionSessionBean.java index 8b9d083..8fb2cf4 100644 --- a/service-ejb/src/main/java/com/maptology/service/session/PositionSessionBean.java +++ b/service-ejb/src/main/java/com/maptology/service/session/PositionSessionBean.java @@ -99,10 +99,23 @@ public class PositionSessionBean implements IPositionSessionBean { p.setSpeed(location.getSpeed()); p.setTimestamp(location.getTimestamp()); p.setVersion(location.getVersion()); + HashSet metadata = new HashSet<>(); + if (location.getMetadata() != null) { + for (Entry entry: location.getMetadata().entrySet()) { + MetadataEntity entity = new MetadataEntity(); + entity.setPosition(p); + entity.setTag(entry.getKey()); + entity.setValue(entry.getValue()); + } + } + p.setMetadata(metadata); try { t.begin(); try { em.persist(p); + for (MetadataEntity e: p.getMetadata()) { + em.persist(e); + } } finally { try { t.commit();