Jeet.Deir Jeet.Deir - 3 months ago 8
Java Question

Writing Text Copying Previous Data

So I have (maybe trivial) a problem that I can't solve. So I am collecting some sensor data using from an Android (Sensor etc. are standard libraries) device that goes like this (pretty much standard):

private final SensorEventListener MultiSensorListener
= new SensorEventListener() {
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}

// Printing of data to screen happens here. File will be written here async.
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_LIGHT) {
String sensorReading = "\nLight Reading: " + String.valueOf(event.values[0]);
side_output.append(sensorReading);
}
//previous code, let's assume that both or any one has been done
if (event.sensor.getType() == Sensor.TYPE_PROXIMITY) {
//textLIGHT_reading.setText("LIGHT: " + event.values[0]);
side_output.append("\nProximity Reading: " + String.valueOf(event.values[0]));
}
if()...

// Many more can go
}
};


So what I want is this: First, initially get all the sensor values
event.values[index]
when
onSensorChanged(SensorEvent event)
is triggered and then create a
string
table (csv, maybe stored in a static variable).

For the example below lets assume that first one is
Acclerometer
, then
Gyro
lastly
Ambient Light
etc. viz:

+--------------------+--------------------+--------------------+
| event.values[0] | event.values[0] | event.values[0] |
+--------------------+--------------------+--------------------+
| event.values[new] | copy from previous | copy from previous |
+--------------------+--------------------+--------------------+
| copy from previous | event.values[new] | copy from previous |
+--------------------+--------------------+--------------------+


What happens here is:


  1. Populate the first row with the first read values

  2. For every
    onSensorChanged(SensorEvent event)
    update the row for that specific sensor and copy the unchanged values from the previous sensors.



How can this be done? I tried string manipulation and using
+=
but fact is with huge data, handling is becoming a big problem and I really don't want my data collection to be slow just for that!

Thanks and regards.

Answer

You can add methods to your sensor listener or any other object that you're instantiating using anonymous-class, then call these logging methods in your logic:

private final SensorEventListener MultiSensorListener
            = new SensorEventListener() {

    // the overriden methods here ...

    private Map<String, String> map = new HashMap<>();

    private void logHistory(String key, String value) {
        // ...
        // for example:
        map.put(key, value);
    }

    private Map<String, String> getLoggedHistory() {
        Map<String, String> mapCopy = new HashMap<>();
        mapCopy.putAll(map);
        return mapCopy;
    }
    ...
    // Printing of data to screen happens here. File will be written here async.
    @Override
    public void onSensorChanged(SensorEvent event) {
       ...
       // log event:
       map.put(event.getId(), event); // just an example
    }