25
25
import java .util .Locale ;
26
26
import java .util .Map ;
27
27
import java .util .StringJoiner ;
28
+ import java .util .TimeZone ;
28
29
29
30
import okhttp3 .MediaType ;
30
31
import okhttp3 .MultipartBody ;
34
35
public class ActivityFileHandler {
35
36
36
37
final static private String ACTIVITY_FILE_NAME ="activity.csv" ;
37
- final static private String DATE_FORMAT ="dd-MM-yyy HH:mm:ss.SSS" ;
38
+ final static private String DATE_FORMAT ="dd-MM-yyyy HH:mm:ss.SSS" ;
38
39
final private SimpleDateFormat dataFormat ;
39
40
private boolean isFirstWrite ;
40
41
41
42
public ActivityFileHandler (Context appContext ){
42
- this .dataFormat = new SimpleDateFormat (DATE_FORMAT , Locale . getDefault () );
43
+ this .dataFormat = new SimpleDateFormat (DATE_FORMAT );
43
44
this .isFirstWrite = isFirstWrite (appContext );
44
45
}
45
46
46
- private String getCurrentTimestamp (){
47
+ private String getCurrentTimestamp (TimeZone tz ){
47
48
return this .dataFormat .format (
48
- Calendar .getInstance ().getTime ()
49
+ Calendar .getInstance (tz ).getTime ()
49
50
);
50
51
}
51
52
52
- public static String cal2Str (Calendar calendar , Locale locale ){
53
+ public static String cal2Str (Calendar calendar ){
53
54
// TODO critical, set timezone according to activity instance
54
- return new SimpleDateFormat (DATE_FORMAT , locale ).format (calendar .getTime ());
55
+ return new SimpleDateFormat (DATE_FORMAT ).format (calendar .getTime ());
55
56
}
56
- public static Calendar str2Cal (String timestamp , Locale locale ) throws ParseException {
57
- SimpleDateFormat formatter = new SimpleDateFormat ("dd-MM-yyyy HH:mm:s.S" , locale );
57
+
58
+ public static Calendar str2Cal (String timestamp ) throws ParseException {
59
+ SimpleDateFormat formatter = new SimpleDateFormat (DATE_FORMAT );
58
60
Date ts = formatter .parse (timestamp );
59
61
Calendar res = Calendar .getInstance ();
60
62
res .setTime (ts );
61
63
return res ;
62
64
}
63
65
64
66
public String getActivity (Context appContext , WeekViewEvent event ) throws FileNotFoundException {
65
- String startTime = cal2Str (event .getStartTime (), Locale . getDefault () );
66
- String endTime = cal2Str (event .getEndTime (), Locale . getDefault () );
67
+ String startTime = cal2Str (event .getStartTime ());
68
+ String endTime = cal2Str (event .getEndTime ());
67
69
String activity = event .getName ().toString ();
68
70
String line = startTime + "," + endTime + "," + activity ;
69
71
@@ -83,8 +85,8 @@ public String getActivity(Context appContext, WeekViewEvent event) throws FileNo
83
85
}
84
86
public boolean isActivityInFile (Context appContext , WeekViewEvent activity ){
85
87
List <String []> cur_file = null ;
86
- String startTime = this .cal2Str (activity .getStartTime (), Locale . getDefault () );
87
- String endTime = this .cal2Str (activity .getEndTime (), Locale . getDefault () );
88
+ String startTime = this .cal2Str (activity .getStartTime ());
89
+ String endTime = this .cal2Str (activity .getEndTime ());
88
90
String strActivity = activity .getName ().toString ();
89
91
try {
90
92
cur_file = new CSVFile (appContext .openFileInput (ACTIVITY_FILE_NAME )).read ();
@@ -104,8 +106,8 @@ public void deleteActivity(Context appContext, WeekViewEvent event) throws IOExc
104
106
}
105
107
106
108
public void deleteActivity (Context appContext , Calendar startTime , Calendar endTime , String activity ) throws IOException {
107
- deleteActivity (appContext , cal2Str (startTime , Locale . getDefault () ),
108
- cal2Str (endTime , Locale . getDefault () ), activity );
109
+ deleteActivity (appContext , cal2Str (startTime ),
110
+ cal2Str (endTime ), activity );
109
111
}
110
112
111
113
public void deleteActivity (Context appContext , String startTime , String endTime , String activity ) throws IOException {
@@ -129,11 +131,13 @@ public void deleteActivity(Context appContext, String startTime, String endTime,
129
131
}
130
132
131
133
public void insertActivity (Context appContext , WeekViewEvent event ) throws IOException {
132
- insertActivity (appContext , event .getStartTime (), event .getEndTime (), event .getName ());
134
+ Calendar startTime = event .getStartTime ();
135
+ // todo check if timezone matches
136
+ insertActivity (appContext , startTime , event .getEndTime (), event .getName ());
133
137
}
134
138
135
139
public void insertActivity (Context appContext , Calendar startTime , Calendar endTime , String activity ) throws IOException {
136
- insertActivity (appContext , cal2Str (startTime , Locale . getDefault ()) , cal2Str (endTime , Locale . getDefault () ), activity );
140
+ insertActivity (appContext , cal2Str (startTime ) , cal2Str (endTime ), activity );
137
141
}
138
142
139
143
public void insertActivity (Context appContext , String startTime , String endTime , String activity ) throws IOException {
@@ -142,7 +146,7 @@ public void insertActivity(Context appContext, String startTime, String endTime,
142
146
StringBuffer inputBuffer = new StringBuffer ();
143
147
Calendar st = null ;
144
148
try {
145
- st = ActivityFileHandler .str2Cal (startTime , Locale . getDefault () );
149
+ st = ActivityFileHandler .str2Cal (startTime );
146
150
} catch (ParseException e ){};
147
151
boolean lineInserted = false ;
148
152
for (int i = 0 ; i < cur_file .size (); i ++) {
@@ -152,7 +156,7 @@ public void insertActivity(Context appContext, String startTime, String endTime,
152
156
Calendar csv_st = null ;
153
157
boolean couldParseSt = true ;
154
158
try {
155
- csv_st = ActivityFileHandler .str2Cal (cur_file .get (i )[0 ], Locale . getDefault () );
159
+ csv_st = ActivityFileHandler .str2Cal (cur_file .get (i )[0 ]);
156
160
}catch (ParseException e ){couldParseSt = false ;};
157
161
158
162
if (couldParseSt && st .compareTo (csv_st ) < 0 && !lineInserted ) {
@@ -182,12 +186,12 @@ public void overwriteActivity(Context appContext, WeekViewEvent oldActivity, Wee
182
186
/* This
183
187
184
188
*/
185
- String startTime = this .cal2Str (oldActivity .getStartTime (), Locale . getDefault () );
186
- String endTime = this .cal2Str (oldActivity .getEndTime (), Locale . getDefault () );
189
+ String startTime = this .cal2Str (oldActivity .getStartTime ());
190
+ String endTime = this .cal2Str (oldActivity .getEndTime ());
187
191
String strActivity = oldActivity .getName ().toString ();
188
192
189
- String newStartTime = this .cal2Str (newActivity .getStartTime (), Locale . getDefault () );
190
- String newEndTime = this .cal2Str (newActivity .getEndTime (), Locale . getDefault () );
193
+ String newStartTime = this .cal2Str (newActivity .getStartTime ());
194
+ String newEndTime = this .cal2Str (newActivity .getEndTime ());
191
195
String newStrActivity = newActivity .getName ().toString ();
192
196
193
197
final String new_row = newStartTime + "," + newEndTime + "," + newStrActivity ;
@@ -248,8 +252,8 @@ public ArrayList<? extends WeekViewEvent> getActivitiesAsEvents(Context appconte
248
252
String activity = values [2 ];
249
253
250
254
try {
251
- startTime = str2Cal (values [0 ], Locale . getDefault () );
252
- endTime = str2Cal (values [1 ], Locale . getDefault () );
255
+ startTime = str2Cal (values [0 ]);
256
+ endTime = str2Cal (values [1 ]);
253
257
} catch (ParseException e ){}
254
258
WeekViewEvent event = new WeekViewEvent (
255
259
startTime .getTimeInMillis (), activity , startTime , endTime );
@@ -413,31 +417,31 @@ public MultipartBody.Part getActivityMultipart(Context appContext){
413
417
return body ;
414
418
}
415
419
416
- public void createActivity (Context appContext , String new_activity ) throws IOException {
420
+ public void createActivity (Context appContext , String new_activity , TimeZone tz ) throws IOException {
417
421
FileOutputStream os = appContext .openFileOutput (ACTIVITY_FILE_NAME , Context .MODE_APPEND );
418
422
String row ;
419
423
if (this .isFirstWrite ){
420
- row = getCurrentTimestamp ();
424
+ row = getCurrentTimestamp (tz );
421
425
this .isFirstWrite = false ;
422
426
}
423
427
else {
424
- row = "\n " + getCurrentTimestamp ();
428
+ row = "\n " + getCurrentTimestamp (tz );
425
429
}
426
430
os .write (row .getBytes ());
427
431
os .close ();
428
432
}
429
433
430
- public void addActivity (Context appContext , String old_activity , String new_activity ) throws IOException {
434
+ public void addActivity (Context appContext , String old_activity , String new_activity , TimeZone tz ) throws IOException {
431
435
/** finishes a begun line and starts a new one
432
436
* */
433
- this .finishActivity (appContext , old_activity );
434
- this .createActivity (appContext , new_activity );
437
+ this .finishActivity (appContext , old_activity , tz );
438
+ this .createActivity (appContext , new_activity , tz );
435
439
}
436
440
437
- public void finishActivity (Context appContext , String activity ) throws IOException {
441
+ public void finishActivity (Context appContext , String activity , TimeZone tz ) throws IOException {
438
442
/** finishes a begun line
439
443
* */
440
- final String cur_ts = getCurrentTimestamp ();
444
+ final String cur_ts = getCurrentTimestamp (tz );
441
445
List <String []> cur_file = new CSVFile (appContext .openFileInput (ACTIVITY_FILE_NAME )).read ();
442
446
String old_ts = cur_file .get (cur_file .size () - 1 )[0 ];
443
447
final String new_row = old_ts + "," + cur_ts + "," + activity ;
0 commit comments