Skip to content

Commit

Permalink
refactor(wearos-lib): adapt listeners and messaging handler
Browse files Browse the repository at this point in the history
  • Loading branch information
matey97 committed Jul 26, 2022
1 parent 3ae4666 commit 6346de6
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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();
}
}

Expand Down Expand Up @@ -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<String> getRequiredPermissions();
Expand Down

0 comments on commit 6346de6

Please sign in to comment.