Skip to content

Commit

Permalink
Merge pull request #324 from Ghazghkull/export_csv_error_for_samsung_s6
Browse files Browse the repository at this point in the history
Solving the CSV Export problem on Samsung S6
  • Loading branch information
paolorotolo authored Jan 4, 2017
2 parents 48ef109 + 7f2ba24 commit 313e0b6
Show file tree
Hide file tree
Showing 5 changed files with 278 additions and 96 deletions.
5 changes: 5 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ dependencies {
}
testCompile 'io.reactivex:rxjava:1.2.4'

testCompile "org.powermock:powermock-module-junit4:1.6.4"
testCompile "org.powermock:powermock-module-junit4-rule:1.6.4"
testCompile "org.powermock:powermock-api-mockito:1.6.4"
testCompile "org.powermock:powermock-classloading-xstream:1.6.4"

// Testing libraries
androidTestCompile 'com.android.support.test:rules:0.4.1'
androidTestCompile 'com.android.support.test:runner:0.4.1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ protected String doInBackground(Void... params) {

if (dirExists()) {
Log.i("glucosio", "Dir exists");
return ReadingToCSV.createCSVFile(mActivity, realm, readings, preferredUnit);
return new ReadingToCSV(mActivity, preferredUnit).createCSVFile(realm, readings);
} else {
Log.i("glucosio", "Dir NOT exists");
return null;
Expand Down
26 changes: 4 additions & 22 deletions app/src/main/java/org/glucosio/android/tools/FormatDateTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,23 +114,13 @@ public String convertDate(String datetime) {
return finalData + "";
}

public String convertRawDate(String datetime) {
DateFormat inputFormat = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");
public String convertRawDate(Date datetime) {
DateFormat finalDataFormat = DateFormat.getDateInstance(DateFormat.SHORT);

Date parsed = null;
try {
parsed = inputFormat.parse(datetime);
} catch (ParseException e) {
reportToFirebase(e);
e.printStackTrace();
}
String finalData = finalDataFormat.format(parsed);
return finalData + "";
return finalDataFormat.format(datetime);
}

public String convertRawTime(String datetime) {
DateFormat inputFormat = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");
public String convertRawTime(Date datetime) {
DateFormat finalTimeFormat;

if (android.text.format.DateFormat.is24HourFormat(context)) {
Expand All @@ -139,15 +129,7 @@ public String convertRawTime(String datetime) {
finalTimeFormat = new SimpleDateFormat("hh:mm a");
}

Date parsed = null;
try {
parsed = inputFormat.parse(datetime);
} catch (ParseException e) {
reportToFirebase(e);
e.printStackTrace();
}
String finalTime = finalTimeFormat.format(parsed);
return finalTime + "";
return finalTimeFormat.format(datetime);
}

public String getCurrentTime() {
Expand Down
151 changes: 78 additions & 73 deletions app/src/main/java/org/glucosio/android/tools/ReadingToCSV.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package org.glucosio.android.tools;

import android.content.Context;
import android.content.res.Resources;
import android.os.Environment;
import android.util.Log;

Expand All @@ -29,99 +30,103 @@

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;

import io.realm.Realm;

public final class ReadingToCSV {

public static String createCSVFile(Context context, Realm realm, final List<GlucoseReading> readings, String um) {
try {
final File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/glucosio", "glucosio_export_ " + System.currentTimeMillis() / 1000 + ".csv");
final File sd = Environment.getExternalStorageDirectory();
if (sd.canWrite()) {
FileOutputStream fileOutputStream = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fileOutputStream);

// CSV Structure
// Date | Time | Concentration | Unit | Measured | Notes
osw.append(context.getResources().getString(R.string.dialog_add_date));
osw.append(',');

osw.append(context.getResources().getString(R.string.dialog_add_time));
osw.append(',');

osw.append(context.getResources().getString(R.string.dialog_add_concentration));
osw.append(',');

osw.append(context.getResources().getString(R.string.helloactivity_spinner_preferred_glucose_unit));
osw.append(',');

osw.append(context.getResources().getString(R.string.dialog_add_measured));
osw.append(',');

osw.append(context.getResources().getString(R.string.dialog_add_notes));
osw.append('\n');


FormatDateTime dateTool = new FormatDateTime(context);

// Concentration | Measured | Date | Time | Notes | Unit of Measurement
if ("mg/dL".equals(um)) {
for (int i = 0; i < readings.size(); i++) {
GlucoseReading reading = readings.get(i);
osw.append(dateTool.convertRawDate(reading.getCreated() + ""));
osw.append(',');

osw.append(dateTool.convertRawTime(reading.getCreated() + ""));
osw.append(',');

osw.append(String.valueOf(reading.getReading()));
osw.append(',');
private final Context context;
private final String um;
private final FormatDateTime dateTool;

osw.append("mg/dL");
osw.append(',');
public ReadingToCSV(Context context, String um) {
this.context = context;
this.um = um;

osw.append(String.valueOf(reading.getReading_type()));
osw.append(',');

osw.append(reading.getNotes());
osw.append('\n');

}
} else {
for (int i = 0; i < readings.size(); i++) {
GlucoseReading reading = readings.get(i);
osw.append(dateTool.convertRawDate(reading.getCreated() + ""));
osw.append(',');

osw.append(dateTool.convertRawTime(reading.getCreated() + ""));
osw.append(',');

osw.append(GlucosioConverter.glucoseToMmolL(reading.getReading()) + "");
osw.append(',');

osw.append("mmol/L");
osw.append(',');
this.dateTool = new FormatDateTime(context);
}

osw.append(reading.getReading_type() + "");
osw.append(',');
public String createCSVFile(Realm realm, final List<GlucoseReading> readings) {
try {
File file = null;
final File sd = Environment.getExternalStorageDirectory();
if (sd.canWrite()) {
file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/glucosio", "glucosio_export_" + System.currentTimeMillis() / 1000 + ".csv");

FileOutputStream fileOutputStream = null;
OutputStreamWriter osw = null;

try {
fileOutputStream = new FileOutputStream(file);
osw = new OutputStreamWriter(fileOutputStream);

// CSV Structure
// Date | Time | Concentration | Unit | Measured | Notes
final Resources resources = this.context.getResources();
writeLine(osw,
resources.getString(R.string.dialog_add_date),
resources.getString(R.string.dialog_add_time),
resources.getString(R.string.dialog_add_concentration),
resources.getString(R.string.helloactivity_spinner_preferred_glucose_unit),
resources.getString(R.string.dialog_add_measured),
resources.getString(R.string.dialog_add_notes)
);

// Concentration | Measured | Date | Time | Notes | Unit of Measurement
if ("mg/dL".equals(um)) {
for (int i = 0; i < readings.size(); i++) {
GlucoseReading reading = readings.get(i);

writeLine(osw,
this.dateTool.convertRawDate(reading.getCreated()),
this.dateTool.convertRawTime(reading.getCreated()),
String.valueOf(reading.getReading()),
"mg/dL",
String.valueOf(reading.getReading_type()),
reading.getNotes()
);

}
} else {
for (int i = 0; i < readings.size(); i++) {
GlucoseReading reading = readings.get(i);

writeLine(osw,
this.dateTool.convertRawDate(reading.getCreated()),
this.dateTool.convertRawTime(reading.getCreated()),
GlucosioConverter.glucoseToMmolL(reading.getReading()) + "",
"mmol/L",
reading.getReading_type() + "",
reading.getNotes()
);
}

osw.append(reading.getNotes());
osw.append('\n');
}
osw.flush();
} catch (Exception e) {
Log.e("Glucosio", "Error exporting readings", e);
} finally {
if (osw != null) osw.close();
if (fileOutputStream != null) fileOutputStream.close();
}
osw.flush();
osw.close();
Log.i("Glucosio", "Done exporting readings");
}
realm.close();
return file.getPath();
return file == null ? null : file.getPath();
} catch (Exception e) {
realm.close();
e.printStackTrace();
return null;
}
}

private void writeLine(OutputStreamWriter osw, String... values) throws IOException {
for (int i = 0; i < values.length; i++) {
osw.append(values[i]);
osw.append(i == values.length - 1 ? '\n' : ',');
}
}
}
Loading

0 comments on commit 313e0b6

Please sign in to comment.