diff --git a/wear-app/app/build.gradle b/wear-app/app/build.gradle index e0ce595..ed821d6 100644 --- a/wear-app/app/build.gradle +++ b/wear-app/app/build.gradle @@ -3,12 +3,12 @@ plugins { } android { - compileSdkVersion 30 + compileSdkVersion 31 defaultConfig { applicationId "org.nativescript.demo" minSdkVersion 26 - targetSdkVersion 30 + targetSdkVersion 31 versionCode 1 versionName "1.0" diff --git a/wear-app/app/src/main/java/org/nativescript/demo/MainActivity.java b/wear-app/app/src/main/java/org/nativescript/demo/MainActivity.java index adad42a..d799a65 100644 --- a/wear-app/app/src/main/java/org/nativescript/demo/MainActivity.java +++ b/wear-app/app/src/main/java/org/nativescript/demo/MainActivity.java @@ -10,11 +10,13 @@ import android.widget.LinearLayout; import android.widget.Spinner; +import es.uji.geotec.backgroundsensors.sensor.Sensor; +import es.uji.geotec.backgroundsensors.sensor.SensorManager; import es.uji.geotec.wearossensors.command.CommandClient; import es.uji.geotec.wearossensors.freemessage.FreeMessage; import es.uji.geotec.wearossensors.freemessage.FreeMessageClient; import es.uji.geotec.wearossensors.permissions.PermissionsManager; -import es.uji.geotec.wearossensors.sensoring.WearSensor; +import es.uji.geotec.wearossensors.sensor.WearSensor; public class MainActivity extends Activity { @@ -98,9 +100,11 @@ private void setupButtons() { private void setupSpinner() { sensorSpinner = findViewById(R.id.sensor_spinner); + SensorManager sensorManager = new SensorManager(this); + ArrayAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); - for (WearSensor sensor : WearSensor.values()) { + for (Sensor sensor : sensorManager.availableSensors(WearSensor.values())) { adapter.add(sensor.toString()); } diff --git a/wear-app/build.gradle b/wear-app/build.gradle index dfbc7c1..1141f80 100644 --- a/wear-app/build.gradle +++ b/wear-app/build.gradle @@ -16,6 +16,7 @@ allprojects { repositories { google() jcenter() + maven { url 'https://jitpack.io' } } } diff --git a/wear-app/gradlew b/wear-app/gradlew old mode 100644 new mode 100755 diff --git a/wear-app/wearossensors/build.gradle b/wear-app/wearossensors/build.gradle index 3022e23..37e870f 100644 --- a/wear-app/wearossensors/build.gradle +++ b/wear-app/wearossensors/build.gradle @@ -3,11 +3,11 @@ plugins { } android { - compileSdkVersion 30 + compileSdkVersion 31 defaultConfig { minSdkVersion 26 - targetSdkVersion 30 + targetSdkVersion 31 testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" consumerProguardFiles "consumer-rules.pro" @@ -29,6 +29,7 @@ dependencies { implementation 'com.google.android.gms:play-services-wearable:17.1.0' implementation 'com.google.code.gson:gson:2.9.0' implementation 'com.google.android.gms:play-services-location:20.0.0' + api 'com.github.GeotecINIT:BackgroundSensors:1.0.1' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' diff --git a/wear-app/wearossensors/src/main/AndroidManifest.xml b/wear-app/wearossensors/src/main/AndroidManifest.xml index 721ecaa..05e605e 100644 --- a/wear-app/wearossensors/src/main/AndroidManifest.xml +++ b/wear-app/wearossensors/src/main/AndroidManifest.xml @@ -36,6 +36,6 @@ - + \ No newline at end of file diff --git a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/capabilities/CapabilityAdvertisementHandler.java b/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/capabilities/CapabilityAdvertisementHandler.java index 5115856..33cb011 100644 --- a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/capabilities/CapabilityAdvertisementHandler.java +++ b/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/capabilities/CapabilityAdvertisementHandler.java @@ -8,21 +8,22 @@ import java.util.List; -import es.uji.geotec.wearossensors.sensoring.WearSensor; -import es.uji.geotec.wearossensors.sensoring.WearSensorManager; +import es.uji.geotec.backgroundsensors.sensor.Sensor; +import es.uji.geotec.backgroundsensors.sensor.SensorManager; +import es.uji.geotec.wearossensors.sensor.WearSensor; public class CapabilityAdvertisementHandler { private MessageClient messageClient; - private WearSensorManager sensorManager; + private SensorManager sensorManager; public CapabilityAdvertisementHandler(Context context) { this.messageClient = Wearable.getMessageClient(context); - this.sensorManager = new WearSensorManager(context); + this.sensorManager = new SensorManager(context); } public void handleRequest(MessageEvent event) { - List availableSensors = this.sensorManager.availableSensors(); + List availableSensors = this.sensorManager.availableSensors(WearSensor.values()); StringBuilder sb = new StringBuilder(); for (int i = 0; i < availableSensors.size(); i++) { diff --git a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/collection/WearCollectorManager.java b/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/collection/WearCollectorManager.java new file mode 100644 index 0000000..dbc6ebe --- /dev/null +++ b/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/collection/WearCollectorManager.java @@ -0,0 +1,117 @@ +package es.uji.geotec.wearossensors.collection; + +import android.annotation.SuppressLint; +import android.content.Context; +import android.hardware.SensorEventListener; + +import com.google.android.gms.location.LocationCallback; +import com.google.android.gms.location.LocationRequest; +import com.google.android.gms.location.LocationServices; +import com.google.android.gms.location.Priority; + +import es.uji.geotec.backgroundsensors.collection.CollectionConfiguration; +import es.uji.geotec.backgroundsensors.collection.CollectorManager; +import es.uji.geotec.backgroundsensors.record.accumulator.RecordAccumulator; +import es.uji.geotec.backgroundsensors.record.callback.RecordCallback; +import es.uji.geotec.backgroundsensors.sensor.Sensor; +import es.uji.geotec.backgroundsensors.time.DefaultTimeProvider; +import es.uji.geotec.backgroundsensors.time.TimeProvider; +import es.uji.geotec.wearossensors.listeners.SensorListenerProvider; +import es.uji.geotec.wearossensors.sensor.WearSensor; + +public class WearCollectorManager extends CollectorManager { + + private LocationCallback locationListener; + + public WearCollectorManager(Context context) { + super(context, new DefaultTimeProvider()); + } + + public WearCollectorManager(Context context, TimeProvider timeProvider) { + super(context, timeProvider); + } + + @SuppressLint("MissingPermission") + @Override + public boolean startCollectingFrom( + CollectionConfiguration collectionConfiguration, + RecordCallback recordCallback + ) { + WearSensor sensor = (WearSensor) collectionConfiguration.getSensor(); + + if (!sensorManager.isSensorAvailable(sensor)) + return false; + + RecordAccumulator accumulator = new RecordAccumulator( + recordCallback, + collectionConfiguration.getBatchSize() + ); + + switch(sensor) { + case ACCELEROMETER: + case GYROSCOPE: + case MAGNETOMETER: + case HEART_RATE: + SensorEventListener listener = SensorListenerProvider.getListenerFor(sensor, accumulator, timeProvider); + if (listener == null) + return false; + + listeners.put(sensor, listener); + + android.hardware.Sensor androidSensor = this.getAndroidSensor(sensor); + return androidSensorManager.registerListener( + listener, + androidSensor, + collectionConfiguration.getSensorDelay() + ); + case LOCATION: + locationListener = SensorListenerProvider.getLocationListener(accumulator, timeProvider); + if (locationListener == null) + return false; + + LocationRequest request = LocationRequest.create() + .setPriority(Priority.PRIORITY_HIGH_ACCURACY) + .setInterval(collectionConfiguration.getSensorDelay()); + + LocationServices.getFusedLocationProviderClient(context) + .requestLocationUpdates(request, locationListener, null); + return true; + default: + return false; + } + } + + @Override + public void stopCollectingFrom(Sensor sensor) { + switch ((WearSensor) sensor) { + case ACCELEROMETER: + case GYROSCOPE: + case MAGNETOMETER: + case HEART_RATE: + SensorEventListener listener = listeners.get(sensor); + if (listener == null) + return; + + listeners.remove(sensor); + android.hardware.Sensor androidSensor = this.getAndroidSensor(sensor); + androidSensorManager.unregisterListener(listener, androidSensor); + return; + case LOCATION: + if (locationListener == null) + return; + + LocationServices.getFusedLocationProviderClient(context) + .removeLocationUpdates(locationListener); + + locationListener = null; + } + + } + + @Override + public void ensureStopCollecting() { + for (Sensor sensor : listeners.keySet()) { + stopCollectingFrom(sensor); + } + } +} diff --git a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/intent/IntentManager.java b/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/intent/IntentManager.java index e622854..bc2aa68 100644 --- a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/intent/IntentManager.java +++ b/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/intent/IntentManager.java @@ -7,7 +7,6 @@ import java.util.ArrayList; import es.uji.geotec.wearossensors.messaging.ResultMessagingProtocol; -import es.uji.geotec.wearossensors.services.SensorRecordingService; public class IntentManager { @@ -20,10 +19,6 @@ public class IntentManager { private IntentManager() { } - public static Intent intentForSensorRecordingService(Context context) { - return new Intent(context, SensorRecordingService.class); - } - public static PendingIntent pendingIntentFromPermissionsToRequest( Context context, Class permissionsActivity, diff --git a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/listeners/HeartRateSensorListener.java b/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/listeners/HeartRateSensorListener.java index 8990bdc..ada01a5 100644 --- a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/listeners/HeartRateSensorListener.java +++ b/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/listeners/HeartRateSensorListener.java @@ -4,9 +4,9 @@ import android.hardware.SensorEvent; import android.hardware.SensorEventListener; +import es.uji.geotec.backgroundsensors.record.accumulator.RecordAccumulator; import es.uji.geotec.wearossensors.records.HeartRateRecord; -import es.uji.geotec.wearossensors.records.accumulator.RecordAccumulator; -import es.uji.geotec.wearossensors.sensoring.WearSensor; +import es.uji.geotec.wearossensors.sensor.WearSensor; public class HeartRateSensorListener implements SensorEventListener { @@ -21,7 +21,7 @@ public HeartRateSensorListener(RecordAccumulator recordAccumulator) { @Override public void onSensorChanged(SensorEvent event) { - if (event.sensor.getType() != sensor.getSensorType()) + if (event.sensor.getType() != sensor.getType()) return; int value = (int) event.values[0]; diff --git a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/listeners/LocationSensorListener.java b/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/listeners/LocationSensorListener.java index 182474b..88316b8 100644 --- a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/listeners/LocationSensorListener.java +++ b/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/listeners/LocationSensorListener.java @@ -6,20 +6,24 @@ import com.google.android.gms.location.LocationCallback; import com.google.android.gms.location.LocationResult; +import es.uji.geotec.backgroundsensors.record.accumulator.RecordAccumulator; +import es.uji.geotec.backgroundsensors.time.TimeProvider; import es.uji.geotec.wearossensors.records.LocationRecord; -import es.uji.geotec.wearossensors.records.accumulator.RecordAccumulator; public class LocationSensorListener extends LocationCallback { private RecordAccumulator accumulator; + private TimeProvider timeProvider; - public LocationSensorListener(RecordAccumulator accumulator) { + public LocationSensorListener(RecordAccumulator accumulator, TimeProvider timeProvider) { this.accumulator = accumulator; + this.timeProvider = timeProvider; } @Override public void onLocationResult(LocationResult locationResult) { for (Location location : locationResult.getLocations()) { + location.setTime(timeProvider.getTimestamp()); LocationRecord record = new LocationRecord(location); accumulator.accumulateRecord(record); } diff --git a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/listeners/SensorListenerProvider.java b/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/listeners/SensorListenerProvider.java index c74b279..b78bdca 100644 --- a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/listeners/SensorListenerProvider.java +++ b/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/listeners/SensorListenerProvider.java @@ -4,8 +4,10 @@ import com.google.android.gms.location.LocationCallback; -import es.uji.geotec.wearossensors.records.accumulator.RecordAccumulator; -import es.uji.geotec.wearossensors.sensoring.WearSensor; +import es.uji.geotec.backgroundsensors.listener.TriAxialSensorListener; +import es.uji.geotec.backgroundsensors.record.accumulator.RecordAccumulator; +import es.uji.geotec.backgroundsensors.time.TimeProvider; +import es.uji.geotec.wearossensors.sensor.WearSensor; public class SensorListenerProvider { private SensorListenerProvider() { @@ -13,13 +15,14 @@ private SensorListenerProvider() { public static SensorEventListener getListenerFor( WearSensor wearSensor, - RecordAccumulator recordAccumulator + RecordAccumulator recordAccumulator, + TimeProvider timeProvider ) { switch (wearSensor) { case ACCELEROMETER: case GYROSCOPE: case MAGNETOMETER: - return new TriAxialSensorListener(wearSensor, recordAccumulator); + return new TriAxialSensorListener(wearSensor, recordAccumulator, timeProvider); case HEART_RATE: return new HeartRateSensorListener(recordAccumulator); default: @@ -27,7 +30,7 @@ public static SensorEventListener getListenerFor( } } - public static LocationCallback getLocationListener(RecordAccumulator recordAccumulator) { - return new LocationSensorListener(recordAccumulator); + public static LocationCallback getLocationListener(RecordAccumulator recordAccumulator, TimeProvider timeProvider) { + return new LocationSensorListener(recordAccumulator, timeProvider); } } diff --git a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/listeners/TriAxialSensorListener.java b/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/listeners/TriAxialSensorListener.java deleted file mode 100644 index 2cfc9f5..0000000 --- a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/listeners/TriAxialSensorListener.java +++ /dev/null @@ -1,38 +0,0 @@ -package es.uji.geotec.wearossensors.listeners; - -import android.hardware.Sensor; -import android.hardware.SensorEvent; -import android.hardware.SensorEventListener; - -import es.uji.geotec.wearossensors.records.TriAxialRecord; -import es.uji.geotec.wearossensors.records.accumulator.RecordAccumulator; -import es.uji.geotec.wearossensors.sensoring.WearSensor; - -public class TriAxialSensorListener implements SensorEventListener { - - private WearSensor sensor; - private RecordAccumulator accumulator; - - public TriAxialSensorListener(WearSensor sensor, RecordAccumulator recordAccumulator) { - this.sensor = sensor; - this.accumulator = recordAccumulator; - } - - @Override - public void onSensorChanged(SensorEvent event) { - if (event.sensor.getType() != sensor.getSensorType()) - return; - - float xValue = event.values[0]; - float yValue = event.values[1]; - float zValue = event.values[2]; - - TriAxialRecord record = new TriAxialRecord(System.currentTimeMillis(), xValue, yValue, zValue); - accumulator.accumulateRecord(record); - } - - @Override - public void onAccuracyChanged(Sensor sensor, int accuracy) { - - } -} diff --git a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/listeners/WearSensorListener.java b/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/listeners/WearSensorListener.java deleted file mode 100644 index bf76d56..0000000 --- a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/listeners/WearSensorListener.java +++ /dev/null @@ -1,9 +0,0 @@ -package es.uji.geotec.wearossensors.listeners; - -import android.hardware.SensorEventListener; - -import es.uji.geotec.wearossensors.records.accumulator.RecordAccumulator; - -public interface WearSensorListener extends SensorEventListener { - void setRecordAccumulator(RecordAccumulator callback); -} diff --git a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/messaging/handlers/AbstractMessagingHandler.java b/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/messaging/handlers/AbstractMessagingHandler.java index 979b160..3ac0dbc 100644 --- a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/messaging/handlers/AbstractMessagingHandler.java +++ b/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/messaging/handlers/AbstractMessagingHandler.java @@ -1,33 +1,31 @@ package es.uji.geotec.wearossensors.messaging.handlers; -import android.content.ComponentName; import android.content.Context; -import android.content.Intent; -import android.content.ServiceConnection; -import android.os.IBinder; import com.google.android.gms.wearable.MessageEvent; import java.util.ArrayList; -import es.uji.geotec.wearossensors.intent.IntentManager; +import es.uji.geotec.backgroundsensors.collection.CollectionConfiguration; +import es.uji.geotec.backgroundsensors.sensor.SensorManager; +import es.uji.geotec.backgroundsensors.service.manager.ServiceManager; import es.uji.geotec.wearossensors.messaging.MessagingClient; import es.uji.geotec.wearossensors.messaging.MessagingProtocol; import es.uji.geotec.wearossensors.messaging.ResultMessagingProtocol; import es.uji.geotec.wearossensors.notifications.NotificationProvider; import es.uji.geotec.wearossensors.permissions.PermissionsManager; -import es.uji.geotec.wearossensors.sensoring.SensoringConfiguration; -import es.uji.geotec.wearossensors.sensoring.WearSensor; -import es.uji.geotec.wearossensors.sensoring.WearSensorManager; -import es.uji.geotec.wearossensors.services.SensorRecordingService; -import es.uji.geotec.wearossensors.services.ServiceAction; +import es.uji.geotec.wearossensors.records.callbacks.RecordCallbackProvider; +import es.uji.geotec.wearossensors.sensor.WearSensor; +import es.uji.geotec.wearossensors.services.WearSensorRecordingService; public abstract class AbstractMessagingHandler { private Context context; + private ServiceManager serviceManager; public AbstractMessagingHandler(Context context) { this.context = context; + this.serviceManager = new ServiceManager(context, WearSensorRecordingService.class); } public void handleMessage(MessageEvent event) { @@ -42,7 +40,7 @@ public void handleMessage(MessageEvent event) { } else if (path.equals(protocol.getStartMessagePath())) { handleStartRequest(sourceNodeId, event.getData()); } else if (path.equals(protocol.getStopMessagePath())) { - handleStopRequest(sourceNodeId); + handleStopRequest(); } } @@ -91,66 +89,31 @@ private void handlePrepareRequest(String sourceNodeId) { private void handleStartRequest(String sourceNodeId, byte[] configuration) { String[] configParams = new String(configuration).split("#"); - Intent intent = IntentManager.intentForSensorRecordingService(context); - context.bindService( - intent, - getServiceConnectionForAction( - ServiceAction.START_COLLECTING, + CollectionConfiguration wearConfig = new CollectionConfiguration( + getWearSensorType(), + Integer.parseInt(configParams[0]), + Integer.parseInt(configParams[1]) + ); + + serviceManager.startCollection( + wearConfig, + RecordCallbackProvider.getRecordCallbackFor( + getWearSensorType(), + context, sourceNodeId, - Integer.parseInt(configParams[0]), - Integer.parseInt(configParams[1]) - ), - 0); - context.startForegroundService(intent); + getProtocol().getNewRecordMessagePath() + ) + ); } - private void handleStopRequest(String sourceNodeId) { - Intent intent = IntentManager.intentForSensorRecordingService(context); - context.bindService( - intent, - getServiceConnectionForAction(ServiceAction.STOP_COLLECTING, sourceNodeId, -1, -1), - 0 - ); - //context.startForegroundService(intent); + private void handleStopRequest() { + serviceManager.stopCollection(getWearSensorType()); } private boolean isSensorSupported() { - WearSensorManager wearSensorManager = new WearSensorManager(context); + SensorManager sensorManager = new SensorManager(context); WearSensor wearSensor = getWearSensorType(); - return wearSensorManager.isSensorAvailable(wearSensor); - } - - private ServiceConnection getServiceConnectionForAction(final ServiceAction action, final String sourceNodeId, final int sensorDelay, final int batchSize) { - return new ServiceConnection() { - - @Override - public void onServiceConnected(ComponentName name, IBinder service) { - SensorRecordingService.SensorRecordingBinder binder = - (SensorRecordingService.SensorRecordingBinder) service; - WearSensor wearSensor = getWearSensorType(); - switch (action) { - case START_COLLECTING: - SensoringConfiguration sensoringConfiguration = new SensoringConfiguration( - wearSensor, - sourceNodeId, - getProtocol().getNewRecordMessagePath(), - sensorDelay, - batchSize - ); - binder.startRecordingFor(sensoringConfiguration); - break; - case STOP_COLLECTING: - binder.stopRecordingFor(wearSensor); - break; - } - context.unbindService(this); - } - - @Override - public void onServiceDisconnected(ComponentName name) { - return; - } - }; + return sensorManager.isSensorAvailable(wearSensor); } protected abstract ArrayList getRequiredPermissions(); diff --git a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/messaging/handlers/AccelerometerMessagingHandler.java b/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/messaging/handlers/AccelerometerMessagingHandler.java index fde7a5d..c7f12a9 100644 --- a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/messaging/handlers/AccelerometerMessagingHandler.java +++ b/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/messaging/handlers/AccelerometerMessagingHandler.java @@ -6,7 +6,7 @@ import es.uji.geotec.wearossensors.messaging.MessagingProtocol; import es.uji.geotec.wearossensors.messaging.ResultMessagingProtocol; -import es.uji.geotec.wearossensors.sensoring.WearSensor; +import es.uji.geotec.wearossensors.sensor.WearSensor; public class AccelerometerMessagingHandler extends AbstractMessagingHandler { diff --git a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/messaging/handlers/GyroscopeMessagingHandler.java b/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/messaging/handlers/GyroscopeMessagingHandler.java index 591df5b..755fb26 100644 --- a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/messaging/handlers/GyroscopeMessagingHandler.java +++ b/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/messaging/handlers/GyroscopeMessagingHandler.java @@ -6,7 +6,7 @@ import es.uji.geotec.wearossensors.messaging.MessagingProtocol; import es.uji.geotec.wearossensors.messaging.ResultMessagingProtocol; -import es.uji.geotec.wearossensors.sensoring.WearSensor; +import es.uji.geotec.wearossensors.sensor.WearSensor; public class GyroscopeMessagingHandler extends AbstractMessagingHandler { diff --git a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/messaging/handlers/HeartRateMessagingHandler.java b/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/messaging/handlers/HeartRateMessagingHandler.java index bba3e4a..4dbf26c 100644 --- a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/messaging/handlers/HeartRateMessagingHandler.java +++ b/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/messaging/handlers/HeartRateMessagingHandler.java @@ -8,7 +8,7 @@ import es.uji.geotec.wearossensors.messaging.MessagingProtocol; import es.uji.geotec.wearossensors.messaging.ResultMessagingProtocol; -import es.uji.geotec.wearossensors.sensoring.WearSensor; +import es.uji.geotec.wearossensors.sensor.WearSensor; public class HeartRateMessagingHandler extends AbstractMessagingHandler { diff --git a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/messaging/handlers/LocationMessagingHandler.java b/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/messaging/handlers/LocationMessagingHandler.java index 853da4e..a3c60ae 100644 --- a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/messaging/handlers/LocationMessagingHandler.java +++ b/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/messaging/handlers/LocationMessagingHandler.java @@ -8,7 +8,7 @@ import es.uji.geotec.wearossensors.messaging.MessagingProtocol; import es.uji.geotec.wearossensors.messaging.ResultMessagingProtocol; -import es.uji.geotec.wearossensors.sensoring.WearSensor; +import es.uji.geotec.wearossensors.sensor.WearSensor; public class LocationMessagingHandler extends AbstractMessagingHandler{ diff --git a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/messaging/handlers/MagnetometerMessagingHandler.java b/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/messaging/handlers/MagnetometerMessagingHandler.java index 442a4d0..a02e1b4 100644 --- a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/messaging/handlers/MagnetometerMessagingHandler.java +++ b/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/messaging/handlers/MagnetometerMessagingHandler.java @@ -6,7 +6,7 @@ import es.uji.geotec.wearossensors.messaging.MessagingProtocol; import es.uji.geotec.wearossensors.messaging.ResultMessagingProtocol; -import es.uji.geotec.wearossensors.sensoring.WearSensor; +import es.uji.geotec.wearossensors.sensor.WearSensor; public class MagnetometerMessagingHandler extends AbstractMessagingHandler { diff --git a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/notifications/NotificationProvider.java b/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/notifications/NotificationProvider.java index de32840..c4ff062 100644 --- a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/notifications/NotificationProvider.java +++ b/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/notifications/NotificationProvider.java @@ -21,10 +21,6 @@ public class NotificationProvider { private static final int REQUEST_PERMISSIONS_DESCRIPTION = R.string.request_permissions_channel_description; private static final int REQUEST_PERMISSIONS_NOTIFICATION_ID = 23; - private static final String SENSOR_RECORDING_CHANNEL = "SENSOR_RECORDING_CHANNEL"; - private static final int SENSOR_RECORDING_DESCRIPTION = R.string.sensor_recording_channel_description; - private static final int SENSOR_RECORDING_NOTIFICATION_ID = 24; - private Context context; private NotificationManagerCompat notificationManager; @@ -62,24 +58,6 @@ public void createNotificationForPermissions( notificationManager.notify(REQUEST_PERMISSIONS_NOTIFICATION_ID, notification); } - public Notification getNotificationForRecordingService() { - setupNotificationChannelIfNeeded( - SENSOR_RECORDING_CHANNEL, - context.getString(SENSOR_RECORDING_DESCRIPTION) - ); - - return buildNotification( - SENSOR_RECORDING_CHANNEL, - context.getString(R.string.sensorization_notification_title), - context.getString(R.string.sensorization_notification_text), - null - ); - } - - public int getRecordingServiceNotificationId() { - return SENSOR_RECORDING_NOTIFICATION_ID; - } - private void setupNotificationChannelIfNeeded(String id, String name) { if (notificationManager.getNotificationChannel(id) != null) { return; diff --git a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/records/HeartRateRecord.java b/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/records/HeartRateRecord.java index fb1efe0..6d34f73 100644 --- a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/records/HeartRateRecord.java +++ b/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/records/HeartRateRecord.java @@ -1,11 +1,14 @@ package es.uji.geotec.wearossensors.records; +import es.uji.geotec.backgroundsensors.record.Record; +import es.uji.geotec.wearossensors.sensor.WearSensor; + public class HeartRateRecord extends Record { private int value; public HeartRateRecord(long timestamp, int value) { - super(timestamp); + super(WearSensor.HEART_RATE, timestamp); this.value = value; } diff --git a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/records/LocationRecord.java b/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/records/LocationRecord.java index 7dc2dc6..24247a4 100644 --- a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/records/LocationRecord.java +++ b/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/records/LocationRecord.java @@ -2,6 +2,9 @@ import android.location.Location; +import es.uji.geotec.backgroundsensors.record.Record; +import es.uji.geotec.wearossensors.sensor.WearSensor; + public class LocationRecord extends Record { private double latitude; @@ -9,14 +12,14 @@ public class LocationRecord extends Record { private double altitude; public LocationRecord(long timestamp, double latitude, double longitude, double altitude) { - super(timestamp); + super(WearSensor.LOCATION, timestamp); this.latitude = latitude; this.longitude = longitude; this.altitude = altitude; } public LocationRecord(Location location) { - super(location.getTime()); + super(WearSensor.LOCATION, location.getTime()); this.latitude = location.getLatitude(); this.longitude = location.getLongitude(); this.altitude = location.getAltitude(); diff --git a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/records/Record.java b/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/records/Record.java deleted file mode 100644 index 60e510d..0000000 --- a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/records/Record.java +++ /dev/null @@ -1,13 +0,0 @@ -package es.uji.geotec.wearossensors.records; - -public class Record { - private long timestamp; - - public Record(long timestamp) { - this.timestamp = timestamp; - } - - public long getTimestamp() { - return timestamp; - } -} diff --git a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/records/TriAxialRecord.java b/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/records/TriAxialRecord.java deleted file mode 100644 index 297cac8..0000000 --- a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/records/TriAxialRecord.java +++ /dev/null @@ -1,25 +0,0 @@ -package es.uji.geotec.wearossensors.records; - -public class TriAxialRecord extends Record { - - private float x, y, z; - - public TriAxialRecord(long timestamp, float x, float y, float z) { - super(timestamp); - this.x = x; - this.y = y; - this.z = z; - } - - public float getX() { - return x; - } - - public float getY() { - return y; - } - - public float getZ() { - return z; - } -} diff --git a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/records/accumulator/RecordAccumulator.java b/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/records/accumulator/RecordAccumulator.java deleted file mode 100644 index 853d964..0000000 --- a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/records/accumulator/RecordAccumulator.java +++ /dev/null @@ -1,30 +0,0 @@ -package es.uji.geotec.wearossensors.records.accumulator; - - -import java.util.LinkedList; -import java.util.List; - -import es.uji.geotec.wearossensors.records.Record; -import es.uji.geotec.wearossensors.records.callbacks.AbstractRecordCallback; - -public class RecordAccumulator { - - private AbstractRecordCallback callback; - private int limit; - private List records; - - public RecordAccumulator(AbstractRecordCallback callback, int limit) { - this.callback = callback; - this.limit = limit; - this.records = new LinkedList<>(); - } - - public void accumulateRecord(T record) { - records.add(record); - - if (records.size() == limit) { - callback.onRecordsCollected(new LinkedList(records)); - records.clear(); - } - } -} diff --git a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/records/accumulator/RecordAccumulatorProvider.java b/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/records/accumulator/RecordAccumulatorProvider.java deleted file mode 100644 index ab6eb6d..0000000 --- a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/records/accumulator/RecordAccumulatorProvider.java +++ /dev/null @@ -1,27 +0,0 @@ -package es.uji.geotec.wearossensors.records.accumulator; - - -import es.uji.geotec.wearossensors.records.HeartRateRecord; -import es.uji.geotec.wearossensors.records.LocationRecord; -import es.uji.geotec.wearossensors.records.TriAxialRecord; -import es.uji.geotec.wearossensors.records.callbacks.AbstractRecordCallback; -import es.uji.geotec.wearossensors.sensoring.WearSensor; - -public class RecordAccumulatorProvider { - private RecordAccumulatorProvider() {} - - public static RecordAccumulator getRecordAccumulatorProviderFor(WearSensor wearSensor, AbstractRecordCallback callback, int limit) { - switch (wearSensor) { - case ACCELEROMETER: - case GYROSCOPE: - case MAGNETOMETER: - return new RecordAccumulator(callback, limit); - case HEART_RATE: - return new RecordAccumulator(callback, limit); - case LOCATION: - return new RecordAccumulator(callback, limit); - default: - return null; - } - } -} diff --git a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/records/callbacks/AbstractRecordCallback.java b/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/records/callbacks/AbstractRecordCallback.java index 6ce7ffe..0414f48 100644 --- a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/records/callbacks/AbstractRecordCallback.java +++ b/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/records/callbacks/AbstractRecordCallback.java @@ -4,10 +4,11 @@ import java.util.List; +import es.uji.geotec.backgroundsensors.record.Record; +import es.uji.geotec.backgroundsensors.record.callback.RecordCallback; import es.uji.geotec.wearossensors.messaging.MessagingClient; -import es.uji.geotec.wearossensors.records.Record; -public abstract class AbstractRecordCallback { +public abstract class AbstractRecordCallback implements RecordCallback { private MessagingClient messagingClient; private String requesterId; @@ -19,6 +20,7 @@ public AbstractRecordCallback(Context context, String requesterId, String sendin this.sendingPath = sendingPath; } + @Override public void onRecordsCollected(List records) { byte[] recordsEncoded = encodeRecords(records); this.messagingClient.sendNewRecord(requesterId, sendingPath, recordsEncoded); diff --git a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/records/callbacks/RecordCallbackProvider.java b/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/records/callbacks/RecordCallbackProvider.java index 850cd68..beae7c4 100644 --- a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/records/callbacks/RecordCallbackProvider.java +++ b/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/records/callbacks/RecordCallbackProvider.java @@ -2,7 +2,7 @@ import android.content.Context; -import es.uji.geotec.wearossensors.sensoring.WearSensor; +import es.uji.geotec.wearossensors.sensor.WearSensor; public class RecordCallbackProvider { private RecordCallbackProvider() { diff --git a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/records/callbacks/TriAxialRecordCallback.java b/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/records/callbacks/TriAxialRecordCallback.java index b363026..c816fbd 100644 --- a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/records/callbacks/TriAxialRecordCallback.java +++ b/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/records/callbacks/TriAxialRecordCallback.java @@ -5,7 +5,7 @@ import java.nio.ByteBuffer; import java.util.List; -import es.uji.geotec.wearossensors.records.TriAxialRecord; +import es.uji.geotec.backgroundsensors.record.TriAxialRecord; public class TriAxialRecordCallback extends AbstractRecordCallback { diff --git a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/sensor/WearSensor.java b/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/sensor/WearSensor.java new file mode 100644 index 0000000..4fb0813 --- /dev/null +++ b/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/sensor/WearSensor.java @@ -0,0 +1,30 @@ +package es.uji.geotec.wearossensors.sensor; + +import android.content.pm.PackageManager; + +import es.uji.geotec.backgroundsensors.sensor.Sensor; + +public enum WearSensor implements Sensor { + ACCELEROMETER(android.hardware.Sensor.TYPE_ACCELEROMETER, PackageManager.FEATURE_SENSOR_ACCELEROMETER), + GYROSCOPE(android.hardware.Sensor.TYPE_GYROSCOPE, PackageManager.FEATURE_SENSOR_GYROSCOPE), + MAGNETOMETER(android.hardware.Sensor.TYPE_MAGNETIC_FIELD, PackageManager.FEATURE_SENSOR_COMPASS), + HEART_RATE(android.hardware.Sensor.TYPE_HEART_RATE, PackageManager.FEATURE_SENSOR_HEART_RATE), + LOCATION(-1, PackageManager.FEATURE_LOCATION_GPS); + + private int sensorType; + private String feature; + WearSensor(int sensorType, String feature) { + this.sensorType = sensorType; + this.feature = feature; + } + + @Override + public int getType() { + return sensorType; + } + + @Override + public String getSystemFeature() { + return feature; + } +} diff --git a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/sensoring/CollectorManager.java b/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/sensoring/CollectorManager.java deleted file mode 100644 index e3b79c7..0000000 --- a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/sensoring/CollectorManager.java +++ /dev/null @@ -1,93 +0,0 @@ -package es.uji.geotec.wearossensors.sensoring; - -import android.content.Context; -import android.hardware.SensorEventListener; - -import com.google.android.gms.location.LocationCallback; - -import java.util.HashMap; - -import es.uji.geotec.wearossensors.listeners.SensorListenerProvider; -import es.uji.geotec.wearossensors.records.accumulator.RecordAccumulator; -import es.uji.geotec.wearossensors.records.accumulator.RecordAccumulatorProvider; -import es.uji.geotec.wearossensors.records.callbacks.AbstractRecordCallback; -import es.uji.geotec.wearossensors.records.callbacks.RecordCallbackProvider; - -public class CollectorManager { - - private Context context; - private WearSensorManager wearSensorManager; - - private HashMap listeners; - private LocationCallback locationListener; - - public CollectorManager(Context context) { - this.context = context; - this.wearSensorManager = new WearSensorManager(context); - this.listeners = new HashMap<>(); - } - - public boolean startCollectingFrom(SensoringConfiguration sensoringConfiguration) { - WearSensor wearSensor = sensoringConfiguration.getWearSensor(); - AbstractRecordCallback callback = RecordCallbackProvider.getRecordCallbackFor( - wearSensor, - context, - sensoringConfiguration.getRequesterId(), - sensoringConfiguration.getSendingPath() - ); - - RecordAccumulator accumulator = RecordAccumulatorProvider.getRecordAccumulatorProviderFor( - wearSensor, - callback, - sensoringConfiguration.getBatchSize() - ); - - switch (wearSensor) { - case ACCELEROMETER: - case GYROSCOPE: - case MAGNETOMETER: - case HEART_RATE: - SensorEventListener listener = SensorListenerProvider.getListenerFor(wearSensor, accumulator); - if (listener == null) - return false; - - listeners.put(wearSensor, listener); - return wearSensorManager.startCollectingFrom(wearSensor, sensoringConfiguration.getSensorDelay(), listener); - case LOCATION: - locationListener = SensorListenerProvider.getLocationListener(accumulator); - if (locationListener == null) - return false; - - return wearSensorManager.startCollectingLocations(locationListener, sensoringConfiguration.getSensorDelay()); - default: - return false; - } - } - - public void stopCollectingFrom(WearSensor wearSensor) { - switch (wearSensor) { - case ACCELEROMETER: - case GYROSCOPE: - case MAGNETOMETER: - case HEART_RATE: - SensorEventListener listener = listeners.get(wearSensor); - if (listener == null) - return; - - listeners.remove(wearSensor); - wearSensorManager.stopCollectingFrom(wearSensor, listener); - break; - case LOCATION: - if (locationListener == null) - return; - - wearSensorManager.stopCollectingLocations(locationListener); - locationListener = null; - break; - } - } - - public void ensureStopCollecting() { - wearSensorManager.stopCollectingFromAllSources(listeners, locationListener); - } -} diff --git a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/sensoring/SensoringConfiguration.java b/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/sensoring/SensoringConfiguration.java deleted file mode 100644 index c4f8f42..0000000 --- a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/sensoring/SensoringConfiguration.java +++ /dev/null @@ -1,47 +0,0 @@ -package es.uji.geotec.wearossensors.sensoring; - -import android.hardware.SensorManager; - -public class SensoringConfiguration { - - private static final int DEFAULT_SENSOR_DELAY = SensorManager.SENSOR_DELAY_NORMAL; - private static final int DEFAULT_BATCH_SIZE = 50; - - private WearSensor wearSensor; - private String requesterId; - private String sendingPath; - private int sensorDelay; - private int batchSize; - - public SensoringConfiguration(WearSensor wearSensor, String requesterId, String sendingPath, int sensorDelay, int batchSize) { - this.wearSensor = wearSensor; - this.requesterId = requesterId; - this.sendingPath = sendingPath; - this.sensorDelay = sensorDelay; - this.batchSize = batchSize; - } - - public WearSensor getWearSensor() { - return wearSensor; - } - - public String getRequesterId() { - return requesterId; - } - - public String getSendingPath() { - return sendingPath; - } - - public int getSensorDelay() { - if (sensorDelay == -1) - return DEFAULT_SENSOR_DELAY; - return sensorDelay; - } - - public int getBatchSize() { - if (batchSize == -1) - return DEFAULT_BATCH_SIZE; - return batchSize; - } -} diff --git a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/sensoring/WearSensor.java b/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/sensoring/WearSensor.java deleted file mode 100644 index 328e80d..0000000 --- a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/sensoring/WearSensor.java +++ /dev/null @@ -1,27 +0,0 @@ -package es.uji.geotec.wearossensors.sensoring; - -import android.content.pm.PackageManager; -import android.hardware.Sensor; - -public enum WearSensor { - ACCELEROMETER(PackageManager.FEATURE_SENSOR_ACCELEROMETER, Sensor.TYPE_ACCELEROMETER), - GYROSCOPE(PackageManager.FEATURE_SENSOR_GYROSCOPE, Sensor.TYPE_GYROSCOPE), - MAGNETOMETER(PackageManager.FEATURE_SENSOR_COMPASS, Sensor.TYPE_MAGNETIC_FIELD), - HEART_RATE(PackageManager.FEATURE_SENSOR_HEART_RATE, Sensor.TYPE_HEART_RATE), - LOCATION(PackageManager.FEATURE_LOCATION_GPS, -1); - - private String feature; - private int sensorType; - WearSensor(String feature, int sensorType) { - this.feature = feature; - this.sensorType = sensorType; - } - - public String getSensorFeature() { - return feature; - } - - public int getSensorType() { - return sensorType; - } -} diff --git a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/sensoring/WearSensorManager.java b/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/sensoring/WearSensorManager.java deleted file mode 100644 index da1f1ac..0000000 --- a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/sensoring/WearSensorManager.java +++ /dev/null @@ -1,113 +0,0 @@ -package es.uji.geotec.wearossensors.sensoring; - -import android.annotation.SuppressLint; -import android.content.Context; -import android.hardware.Sensor; -import android.hardware.SensorEventListener; -import android.hardware.SensorManager; - -import com.google.android.gms.location.LocationCallback; -import com.google.android.gms.location.LocationRequest; -import com.google.android.gms.location.LocationServices; -import com.google.android.gms.location.Priority; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; - -public class WearSensorManager { - - private static WearSensor[] SENSORS = new WearSensor[] { - WearSensor.ACCELEROMETER, - WearSensor.GYROSCOPE, - WearSensor.MAGNETOMETER, - WearSensor.LOCATION, - WearSensor.HEART_RATE - }; - - private Context context; - private SensorManager sensorManager; - - public WearSensorManager(Context context) { - this.context = context; - this.sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE); - } - - public List availableSensors() { - List availableSensors = new ArrayList<>(); - for (WearSensor sensor : SENSORS) { - if (isSensorAvailable(sensor)) - availableSensors.add(sensor); - } - - return availableSensors; - } - - public boolean isSensorAvailable(WearSensor sensor) { - boolean hasFeature = context.getPackageManager().hasSystemFeature(sensor.getSensorFeature()); - - // Heart rate appears to be not available as system feature in the emulator - // but it is available from the sensor manager. This is probably due to the - // recent inclusion of this sensor in the emulator. - // TODO: check in the future if the heart rate is available as system feature to undo this patch - int sensorType = sensor.getSensorType(); - if (sensorType == -1) - return hasFeature; - - boolean hasSensor = sensorManager.getDefaultSensor(sensor.getSensorType()) != null; - - return hasFeature || hasSensor; - } - - public boolean startCollectingFrom(WearSensor wearSensor, int samplingPeriod, SensorEventListener listener) { - if (!isSensorAvailable(wearSensor) || listener == null) - return false; - - Sensor sensor = getWearSensor(wearSensor); - return sensorManager.registerListener(listener, sensor, samplingPeriod); - } - - @SuppressLint("MissingPermission") - public boolean startCollectingLocations(LocationCallback listener, int interval) { - if (!isSensorAvailable(WearSensor.LOCATION) || listener == null) - return false; - - LocationRequest request = LocationRequest.create() - .setPriority(Priority.PRIORITY_HIGH_ACCURACY) - .setInterval(interval); - - LocationServices.getFusedLocationProviderClient(context) - .requestLocationUpdates(request, listener, null); - - //FIXME: return result of request - return true; - } - - public void stopCollectingFrom(WearSensor wearSensor, SensorEventListener listener) { - if (listener == null) - return; - - Sensor sensor = getWearSensor(wearSensor); - sensorManager.unregisterListener(listener, sensor); - } - - public void stopCollectingLocations(LocationCallback listener) { - if (listener == null) - return; - - LocationServices.getFusedLocationProviderClient(context) - .removeLocationUpdates(listener); - } - - public void stopCollectingFromAllSources(HashMap listeners, LocationCallback locationCallback) { - for (WearSensor wearSensor : listeners.keySet()) { - stopCollectingFrom(wearSensor, listeners.get(wearSensor)); - } - - stopCollectingLocations(locationCallback); - } - - private Sensor getWearSensor(WearSensor sensor) { - return sensorManager.getDefaultSensor(sensor.getSensorType()); - } -} diff --git a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/services/SensorRecordingService.java b/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/services/SensorRecordingService.java deleted file mode 100644 index 2dd1279..0000000 --- a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/services/SensorRecordingService.java +++ /dev/null @@ -1,124 +0,0 @@ -package es.uji.geotec.wearossensors.services; - -import android.app.Notification; -import android.app.Service; -import android.content.Intent; -import android.os.Binder; -import android.os.IBinder; -import android.os.PowerManager; -import android.util.Log; - -import androidx.annotation.Nullable; - -import java.util.HashSet; -import java.util.Set; - -import es.uji.geotec.wearossensors.notifications.NotificationProvider; -import es.uji.geotec.wearossensors.sensoring.CollectorManager; -import es.uji.geotec.wearossensors.sensoring.SensoringConfiguration; -import es.uji.geotec.wearossensors.sensoring.WearSensor; - -public class SensorRecordingService extends Service { - - private final IBinder binder = new SensorRecordingBinder(); - public class SensorRecordingBinder extends Binder { - SensorRecordingService service = SensorRecordingService.this; - - public void startRecordingFor(SensoringConfiguration sensoringConfiguration) { - service.startRecordingFor(sensoringConfiguration); - } - - public void stopRecordingFor(WearSensor wearSensor) { - service.stopRecordingFor(wearSensor); - } - } - - @Nullable - @Override - public IBinder onBind(Intent intent) { - return binder; - } - - private static final String TAG = "SensorRecordingService"; - - private static final int TIMEOUT = 1000 * 60; - - private PowerManager.WakeLock wakeLock; - private Set sensorsBeingRecorded; - private CollectorManager collectorManager; - - @Override - public void onCreate() { - super.onCreate(); - - PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE); - wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "wearapp:sensorrecordingservice"); - - sensorsBeingRecorded = new HashSet<>(); - - collectorManager = new CollectorManager(this); - } - - @Override - public int onStartCommand(Intent intent, int flags, int startId) { - super.onStartCommand(intent, flags, startId); - - if (!wakeLock.isHeld()) { - wakeLock.acquire(TIMEOUT); - } - - runInForegroundWithNotification(); - - return START_STICKY; - } - - @Override - public void onDestroy() { - collectorManager.ensureStopCollecting(); - super.onDestroy(); - } - - private void runInForegroundWithNotification() { - NotificationProvider notificationProvider = new NotificationProvider(this); - - int notificationId = notificationProvider.getRecordingServiceNotificationId(); - Notification notification = notificationProvider.getNotificationForRecordingService(); - - startForeground(notificationId, notification); - } - - private void startRecordingFor(SensoringConfiguration sensoringConfiguration) { - WearSensor sensor = sensoringConfiguration.getWearSensor(); - if (sensorsBeingRecorded.contains(sensor)) { - Log.d(TAG, "already recording: " + sensor.toString()); - return; - } - - sensorsBeingRecorded.add(sensor); - collectorManager.startCollectingFrom(sensoringConfiguration); - Log.d(TAG, "startRecordingFor: " + sensor.toString()); - } - - private void stopRecordingFor(WearSensor sensor) { - if (!sensorsBeingRecorded.contains(sensor)) { - Log.d(TAG, "wasn't being recorded: " + sensor.toString()); - } else { - Log.d(TAG, "stopRecordingFor: " + sensor.toString()); - collectorManager.stopCollectingFrom(sensor); - sensorsBeingRecorded.remove(sensor); - } - - if (sensorsBeingRecorded.size() == 0) { - Log.d(TAG, "no more sensors being recorded"); - gracefullyStop(); - } - } - - private void gracefullyStop() { - stopForeground(true); - if (wakeLock.isHeld()) { - wakeLock.release(); - } - stopSelf(); - } -} diff --git a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/services/ServiceAction.java b/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/services/ServiceAction.java deleted file mode 100644 index b0f3722..0000000 --- a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/services/ServiceAction.java +++ /dev/null @@ -1,6 +0,0 @@ -package es.uji.geotec.wearossensors.services; - -public enum ServiceAction { - START_COLLECTING, - STOP_COLLECTING; -} diff --git a/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/services/WearSensorRecordingService.java b/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/services/WearSensorRecordingService.java new file mode 100644 index 0000000..c39b03c --- /dev/null +++ b/wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/services/WearSensorRecordingService.java @@ -0,0 +1,12 @@ +package es.uji.geotec.wearossensors.services; + +import es.uji.geotec.backgroundsensors.collection.CollectorManager; +import es.uji.geotec.backgroundsensors.service.SensorRecordingService; +import es.uji.geotec.wearossensors.collection.WearCollectorManager; + +public class WearSensorRecordingService extends SensorRecordingService { + @Override + public CollectorManager getCollectorManager() { + return new WearCollectorManager(this); + } +} diff --git a/wear-app/wearossensors/src/main/res/drawable/ic_sensor_service.xml b/wear-app/wearossensors/src/main/res/drawable/ic_sensor_service.xml new file mode 100644 index 0000000..e242792 --- /dev/null +++ b/wear-app/wearossensors/src/main/res/drawable/ic_sensor_service.xml @@ -0,0 +1,10 @@ + + + diff --git a/wear-app/wearossensors/src/main/res/values/strings.xml b/wear-app/wearossensors/src/main/res/values/strings.xml index c5bd53d..973f009 100644 --- a/wear-app/wearossensors/src/main/res/values/strings.xml +++ b/wear-app/wearossensors/src/main/res/values/strings.xml @@ -3,12 +3,11 @@ Request of permissions - Sensorization - Permissions required The app needs some permissions in order to work properly - Sensorization - Collecting data from sensors + Sensorization + WearOS Sensorization + Collecting data from wearable sensors \ No newline at end of file