Skip to content

Commit

Permalink
refactor: implement api changes from backgroundsensors lib
Browse files Browse the repository at this point in the history
  • Loading branch information
matey97 committed Jul 28, 2022
1 parent 9e35978 commit 7f1b658
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
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;
Expand Down Expand Up @@ -98,9 +100,11 @@ private void setupButtons() {
private void setupSpinner() {
sensorSpinner = findViewById(R.id.sensor_spinner);

SensorManager sensorManager = new SensorManager(this);

ArrayAdapter<String> 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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,21 @@
import java.util.List;

import es.uji.geotec.backgroundsensors.sensor.Sensor;
import es.uji.geotec.wearossensors.collection.WearCollectorManager;
import es.uji.geotec.backgroundsensors.sensor.SensorManager;
import es.uji.geotec.wearossensors.sensor.WearSensor;

public class CapabilityAdvertisementHandler {

private MessageClient messageClient;
private WearCollectorManager collectorManager;
private SensorManager sensorManager;

public CapabilityAdvertisementHandler(Context context) {
this.messageClient = Wearable.getMessageClient(context);
this.collectorManager = new WearCollectorManager(context);
this.sensorManager = new SensorManager(context);
}

public void handleRequest(MessageEvent event) {
List<Sensor> availableSensors = this.collectorManager.availableSensors();
List<Sensor> availableSensors = this.sensorManager.availableSensors(WearSensor.values());

StringBuilder sb = new StringBuilder();
for (int i = 0; i < availableSensors.size(); i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.Priority;

import java.util.Arrays;

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;

Expand All @@ -24,7 +24,11 @@ public class WearCollectorManager extends CollectorManager {
private LocationCallback locationListener;

public WearCollectorManager(Context context) {
super(context, Arrays.asList(WearSensor.values()));
super(context, new DefaultTimeProvider());
}

public WearCollectorManager(Context context, TimeProvider timeProvider) {
super(context, timeProvider);
}

@SuppressLint("MissingPermission")
Expand All @@ -35,7 +39,7 @@ public boolean startCollectingFrom(
) {
WearSensor sensor = (WearSensor) collectionConfiguration.getSensor();

if (!isSensorAvailable(sensor))
if (!sensorManager.isSensorAvailable(sensor))
return false;

RecordAccumulator accumulator = new RecordAccumulator(
Expand All @@ -48,20 +52,20 @@ public boolean startCollectingFrom(
case GYROSCOPE:
case MAGNETOMETER:
case HEART_RATE:
SensorEventListener listener = SensorListenerProvider.getListenerFor(sensor, accumulator);
SensorEventListener listener = SensorListenerProvider.getListenerFor(sensor, accumulator, timeProvider);
if (listener == null)
return false;

listeners.put(sensor, listener);

android.hardware.Sensor androidSensor = this.getAndroidSensor(sensor);
return sensorManager.registerListener(
return androidSensorManager.registerListener(
listener,
androidSensor,
collectionConfiguration.getSensorDelay()
);
case LOCATION:
locationListener = SensorListenerProvider.getLocationListener(accumulator);
locationListener = SensorListenerProvider.getLocationListener(accumulator, timeProvider);
if (locationListener == null)
return false;

Expand Down Expand Up @@ -90,7 +94,7 @@ public void stopCollectingFrom(Sensor sensor) {

listeners.remove(sensor);
android.hardware.Sensor androidSensor = this.getAndroidSensor(sensor);
sensorManager.unregisterListener(listener, androidSensor);
androidSensorManager.unregisterListener(listener, androidSensor);
return;
case LOCATION:
if (locationListener == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,23 @@
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;

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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

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 {
Expand All @@ -14,21 +15,22 @@ 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:
return null;
}
}

public static LocationCallback getLocationListener(RecordAccumulator recordAccumulator) {
return new LocationSensorListener(recordAccumulator);
public static LocationCallback getLocationListener(RecordAccumulator recordAccumulator, TimeProvider timeProvider) {
return new LocationSensorListener(recordAccumulator, timeProvider);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import java.util.ArrayList;

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.collection.WearCollectorManager;
import es.uji.geotec.wearossensors.messaging.MessagingClient;
import es.uji.geotec.wearossensors.messaging.MessagingProtocol;
import es.uji.geotec.wearossensors.messaging.ResultMessagingProtocol;
Expand Down Expand Up @@ -111,9 +111,9 @@ private void handleStopRequest() {
}

private boolean isSensorSupported() {
WearCollectorManager collectorManager = new WearCollectorManager(context);
SensorManager sensorManager = new SensorManager(context);
WearSensor wearSensor = getWearSensorType();
return collectorManager.isSensorAvailable(wearSensor);
return sensorManager.isSensorAvailable(wearSensor);
}

protected abstract ArrayList<String> getRequiredPermissions();
Expand Down

0 comments on commit 7f1b658

Please sign in to comment.