mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge.git
synced 2025-01-10 09:01:55 +01:00
clean old exportToCSV method
use try-with-resources statement
This commit is contained in:
parent
5525938669
commit
a867c06507
@ -22,10 +22,6 @@ import android.support.annotation.Nullable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@ -48,7 +44,6 @@ import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
|
||||
* @param <T> the sample type
|
||||
*/
|
||||
public abstract class AbstractSampleProvider<T extends AbstractActivitySample> implements SampleProvider<T> {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(AbstractSampleProvider.class);
|
||||
private static final WhereCondition[] NO_CONDITIONS = new WhereCondition[0];
|
||||
private final DaoSession mSession;
|
||||
private final GBDevice mDevice;
|
||||
@ -99,29 +94,6 @@ public abstract class AbstractSampleProvider<T extends AbstractActivitySample> i
|
||||
getSampleDao().insertOrReplaceInTx(activitySamples);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exportToCSV(T[] activitySamples, File outFile) {
|
||||
String separator = ",";
|
||||
|
||||
LOG.info("Exporting samples into csv file: " + outFile.getName());
|
||||
try {
|
||||
BufferedWriter bw = new BufferedWriter(new FileWriter(outFile));
|
||||
|
||||
for (T sample : activitySamples){
|
||||
String line = sample.getTimestamp() + separator + sample.getDeviceId() + separator + sample.getUserId() + separator + sample.getRawIntensity() + separator + sample.getSteps() + separator + sample.getRawKind() + separator + sample.getHeartRate();
|
||||
|
||||
//LOG.debug("Adding line into buffer: " + line);
|
||||
bw.write(line);
|
||||
bw.newLine();
|
||||
}
|
||||
|
||||
bw.flush();
|
||||
bw.close();
|
||||
} catch (IOException e) {
|
||||
LOG.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public T getLatestActivitySample() {
|
||||
|
@ -20,9 +20,7 @@ package nodomain.freeyourgadget.gadgetbridge.devices;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample;
|
||||
|
||||
/**
|
||||
@ -89,12 +87,6 @@ public interface SampleProvider<T extends AbstractActivitySample> {
|
||||
*/
|
||||
void addGBActivitySamples(T[] activitySamples);
|
||||
|
||||
/**
|
||||
* Exports samples into a CSV file
|
||||
* @param activitySamples the samples to export
|
||||
*/
|
||||
void exportToCSV(T[] activitySamples, File outFile);
|
||||
|
||||
/**
|
||||
* Factory method to creates an empty sample of the correct type for this sample provider
|
||||
* @return the newly created "empty" sample
|
||||
|
@ -23,7 +23,6 @@ import android.net.Uri;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.GBException;
|
||||
@ -70,10 +69,6 @@ public class UnknownDeviceCoordinator extends AbstractDeviceCoordinator {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exportToCSV(AbstractActivitySample[] activitySamples, File outFile) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addGBActivitySample(AbstractActivitySample activitySample) {
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.devices.AbstractSampleProvider;
|
||||
import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider;
|
||||
import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample;
|
||||
|
||||
/**
|
||||
@ -16,15 +16,13 @@ import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample;
|
||||
*/
|
||||
|
||||
public class CSVExport {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(AbstractSampleProvider.class);
|
||||
private static final Logger LOG = LoggerFactory.getLogger(SampleProvider.class);
|
||||
|
||||
public static void exportToCSV(AbstractActivitySample[] activitySamples, File outFile) {
|
||||
String separator = ",";
|
||||
BufferedWriter bw = null;
|
||||
|
||||
LOG.info("Exporting samples into csv file: " + outFile.getName());
|
||||
try {
|
||||
bw = new BufferedWriter(new FileWriter(outFile));
|
||||
try (BufferedWriter bw = new BufferedWriter(new FileWriter(outFile))) {
|
||||
bw.write("TIMESTAMP" + separator + "DEVICE_ID" + separator + "USER_ID" + separator + "RAW_INTENSITY" + separator + "STEPS" + separator + "RAW_KIND" + separator + "HEART_RATE");
|
||||
bw.newLine();
|
||||
|
||||
@ -35,17 +33,10 @@ public class CSVExport {
|
||||
bw.write(line);
|
||||
bw.newLine();
|
||||
}
|
||||
|
||||
bw.flush();
|
||||
} catch (IOException e) {
|
||||
LOG.error(e.getMessage());
|
||||
} finally {
|
||||
if (bw != null){
|
||||
try {
|
||||
bw.flush();
|
||||
bw.close();
|
||||
} catch (IOException e) {
|
||||
LOG.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
LOG.error("Error related to " + outFile.getName() + " file: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user