From 6346de65fed6907070f3bb8ef11648be72bd0740 Mon Sep 17 00:00:00 2001 From: Miguel Matey Sanz Date: Tue, 26 Jul 2022 16:38:46 +0200 Subject: [PATCH] refactor(wearos-lib): adapt listeners and messaging handler --- .../listeners/HeartRateSensorListener.java | 6 +- .../listeners/LocationSensorListener.java | 2 +- .../listeners/SensorListenerProvider.java | 5 +- .../listeners/TriAxialSensorListener.java | 38 -------- .../listeners/WearSensorListener.java | 9 -- .../handlers/AbstractMessagingHandler.java | 91 ++++++------------- 6 files changed, 34 insertions(+), 117 deletions(-) delete mode 100644 wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/listeners/TriAxialSensorListener.java delete mode 100644 wear-app/wearossensors/src/main/java/es/uji/geotec/wearossensors/listeners/WearSensorListener.java 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..544f812 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,8 +6,8 @@ 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.wearossensors.records.LocationRecord; -import es.uji.geotec.wearossensors.records.accumulator.RecordAccumulator; public class LocationSensorListener extends LocationCallback { 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..af5163d 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,9 @@ 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.wearossensors.sensor.WearSensor; public class SensorListenerProvider { private SensorListenerProvider() { 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..96bde53 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.service.manager.ServiceManager; +import es.uji.geotec.wearossensors.collection.WearCollectorManager; 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); + WearCollectorManager collectorManager = new WearCollectorManager(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 collectorManager.isSensorAvailable(wearSensor); } protected abstract ArrayList getRequiredPermissions();