-
Notifications
You must be signed in to change notification settings - Fork 1
/
DemoActivity.java
73 lines (57 loc) · 2.71 KB
/
DemoActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package es.uji.geotec.backgroundsensorsdemo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import java.util.List;
import es.uji.geotec.backgroundsensors.collection.CollectionConfiguration;
import es.uji.geotec.backgroundsensors.sensor.BaseSensor;
import es.uji.geotec.backgroundsensors.sensor.Sensor;
import es.uji.geotec.backgroundsensors.sensor.SensorManager;
import es.uji.geotec.backgroundsensors.service.BaseSensorRecordingService;
import es.uji.geotec.backgroundsensors.service.manager.ServiceManager;
public class DemoActivity extends AppCompatActivity {
private static final String TAG = "Background Sensors Demo";
private SensorManager sensorManager;
private ServiceManager serviceManager;
private Spinner sensorSpinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_demo);
sensorManager = new SensorManager(this);
serviceManager = new ServiceManager(this, BaseSensorRecordingService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
serviceManager.enableServiceNotification();
}
sensorSpinner = findViewById(R.id.sensors_spinner);
List<Sensor> sensors = sensorManager.availableSensors(BaseSensor.values());
ArrayAdapter<Sensor> adapter = new ArrayAdapter<>(this, androidx.appcompat.R.layout.support_simple_spinner_dropdown_item, sensors);
sensorSpinner.setAdapter(adapter);
sensorSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Log.d(TAG, "onItemSelected: " + adapterView.getItemAtPosition(i));
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
public void onStartTap(View view) {
Sensor sensor = (Sensor) sensorSpinner.getSelectedItem();
CollectionConfiguration config = new CollectionConfiguration(sensor, android.hardware.SensorManager.SENSOR_DELAY_GAME, 50);
serviceManager.startCollection(config, records -> {
Log.d(TAG, "onRecordsCollected: " + records.size() + " records");
Log.d(TAG, "a sample: " + records.get(0));
});
}
public void onStopTap(View view) {
Sensor sensor = (Sensor) sensorSpinner.getSelectedItem();
this.serviceManager.stopCollection(sensor);
}
}