| 1 |
package com.ridelogger.formats; |
| 2 |
|
| 3 |
import java.text.SimpleDateFormat; |
| 4 |
import java.util.Date; |
| 5 |
import java.util.TimeZone; |
| 6 |
|
| 7 |
import com.ridelogger.R; |
| 8 |
import com.ridelogger.RideService; |
| 9 |
|
| 10 |
import android.content.SharedPreferences; |
| 11 |
import android.preference.PreferenceManager; |
| 12 |
|
| 13 |
public class JsonFormat extends BaseFormat<Object> { |
| 14 |
public JsonFormat(RideService rideService) { |
| 15 |
super(rideService); |
| 16 |
subExt = ".json"; |
| 17 |
} |
| 18 |
|
| 19 |
public void writeHeader() { |
| 20 |
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context); |
| 21 |
Date startDate = new Date(context.startTime); |
| 22 |
|
| 23 |
SimpleDateFormat startTimef = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); |
| 24 |
SimpleDateFormat filef = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss"); |
| 25 |
SimpleDateFormat month = new SimpleDateFormat("MMMMM"); |
| 26 |
SimpleDateFormat year = new SimpleDateFormat("yyyy"); |
| 27 |
SimpleDateFormat day = new SimpleDateFormat("EEEEE"); |
| 28 |
|
| 29 |
startTimef.setTimeZone(TimeZone.getTimeZone("UTC")); |
| 30 |
filef.setTimeZone(TimeZone.getTimeZone("UTC")); |
| 31 |
month.setTimeZone(TimeZone.getTimeZone("UTC")); |
| 32 |
year.setTimeZone(TimeZone.getTimeZone("UTC")); |
| 33 |
day.setTimeZone(TimeZone.getTimeZone("UTC")); |
| 34 |
|
| 35 |
try { |
| 36 |
buf.write("{" + |
| 37 |
"\"RIDE\":{" + |
| 38 |
"\"STARTTIME\":\"" + startTimef.format(startDate) + " UTC\"," + |
| 39 |
"\"RECINTSECS\":1," + |
| 40 |
"\"DEVICETYPE\":\"Android\"," + |
| 41 |
"\"IDENTIFIER\":\"\"," + |
| 42 |
"\"TAGS\":{" + |
| 43 |
"\"Athlete\":\"" + settings.getString(context.getString(R.string.PREF_RIDER_NAME), "") + "\"," + |
| 44 |
"\"Calendar Text\":\"Auto Recored Android Ride\"," + |
| 45 |
"\"Change History\":\"\"," + |
| 46 |
"\"Data\":\"\"," + |
| 47 |
"\"Device\":\"\"," + |
| 48 |
"\"Device Info\":\"\"," + |
| 49 |
"\"File Format\":\"\"," + |
| 50 |
"\"Filename\":\"\"," + |
| 51 |
"\"Month\":\"" + month.format(startDate) +"\"," + |
| 52 |
"\"Notes\":\"\"," + |
| 53 |
"\"Objective\":\"\"," + |
| 54 |
"\"Sport\":\"Bike\"," + |
| 55 |
"\"Weekday\":\"" + day.format(startDate) + "\"," + |
| 56 |
"\"Workout Code\":\"\"," + |
| 57 |
"\"Year\":\"" + year.format(startDate) + "\"" + |
| 58 |
"}," + |
| 59 |
"\"SAMPLES\":[{\"SECS\":0}"); |
| 60 |
} catch (Exception e) {} |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
public void writeValues() { |
| 65 |
try { |
| 66 |
synchronized (buf) { |
| 67 |
buf.write(",{"); |
| 68 |
buf.write("\""); |
| 69 |
buf.write(RideService.KEYS[0]); |
| 70 |
buf.write("\":"); |
| 71 |
buf.write(String.format("%f", context.currentValues[0])); |
| 72 |
|
| 73 |
for (int i = 1; i < context.currentValues.length; i++) { |
| 74 |
buf.write(",\""); |
| 75 |
buf.write(RideService.KEYS[i]); |
| 76 |
buf.write("\":"); |
| 77 |
buf.write(String.format("%f", context.currentValues[i])); |
| 78 |
} |
| 79 |
|
| 80 |
buf.write("}"); |
| 81 |
} |
| 82 |
} catch (Exception e) {} |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
public void writeFooter() { |
| 87 |
try { |
| 88 |
buf.write("]}}"); |
| 89 |
|
| 90 |
} catch (Exception e) {} |
| 91 |
super.writeFooter(); |
| 92 |
} |
| 93 |
} |