Skip to content

Commit

Permalink
Merge pull request #13 from GeoTecINIT/background-sensors
Browse files Browse the repository at this point in the history
Background sensors
  • Loading branch information
matey97 authored Jul 28, 2022
2 parents 4f363ae + 7f1b658 commit f38eb3f
Show file tree
Hide file tree
Showing 40 changed files with 256 additions and 682 deletions.
4 changes: 2 additions & 2 deletions wear-app/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"

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

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
1 change: 1 addition & 0 deletions wear-app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ allprojects {
repositories {
google()
jcenter()
maven { url 'https://jitpack.io' }
}
}

Expand Down
Empty file modified wear-app/gradlew
100644 → 100755
Empty file.
5 changes: 3 additions & 2 deletions wear-app/wearossensors/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion wear-app/wearossensors/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@
</intent-filter>
</service>

<service android:name=".services.SensorRecordingService" android:exported="true" />
<service android:name=".services.WearSensorRecordingService" android:exported="true" />
</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -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<WearSensor> availableSensors = this.sensorManager.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
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

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

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() {
}

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);
}
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit f38eb3f

Please sign in to comment.