25
25
package org.tomdroid.sync.sd;
28
import java.io.FileInputStream;
29
import java.io.FilenameFilter;
30
import java.io.IOException;
31
import java.io.InputStreamReader;
32
import java.io.Reader;
33
import java.io.StringReader;
34
import java.util.regex.Matcher;
35
import java.util.regex.Pattern;
37
import javax.xml.parsers.SAXParser;
38
import javax.xml.parsers.SAXParserFactory;
27
import android.app.Activity;
28
import android.os.Handler;
29
import android.util.TimeFormatException;
40
30
import org.tomdroid.Note;
41
31
import org.tomdroid.R;
42
32
import org.tomdroid.sync.SyncService;
43
33
import org.tomdroid.ui.Tomdroid;
44
34
import org.tomdroid.util.ErrorList;
35
import org.tomdroid.util.TLog;
45
36
import org.xml.sax.InputSource;
46
37
import org.xml.sax.XMLReader;
48
import android.app.Activity;
49
import android.os.Handler;
50
import android.util.Log;
51
import android.util.TimeFormatException;
39
import javax.xml.parsers.SAXParser;
40
import javax.xml.parsers.SAXParserFactory;
42
import java.util.regex.Matcher;
43
import java.util.regex.Pattern;
53
45
public class SdCardSyncService extends SyncService {
90
82
setSyncProgress(0);
92
84
// start loading local notes
93
if (Tomdroid.LOGGING_ENABLED) Log.v(TAG, "Loading local notes");
85
TLog.v(TAG, "Loading local notes");
95
87
File path = new File(Tomdroid.NOTES_PATH);
97
89
if (!path.exists())
100
Log.i(TAG, "Path "+path+" exists: "+path.exists());
92
TLog.i(TAG, "Path {0} exists: {1}", path, path.exists());
102
94
// Check a second time, if not the most likely cause is the volume doesn't exist
103
95
if(!path.exists()) {
104
if (Tomdroid.LOGGING_ENABLED) Log.w(TAG, "Couldn't create "+path);
96
TLog.w(TAG, "Couldn't create {0}", path);
105
97
sendMessage(NO_SD_CARD);
106
98
setSyncProgress(100);
113
105
// If there are no notes, warn the UI through an empty message
114
106
if (fileList == null || fileList.length == 0) {
115
if (Tomdroid.LOGGING_ENABLED) Log.i(TAG, "There are no notes in "+path);
107
TLog.i(TAG, "There are no notes in {0}", path);
116
108
sendMessage(PARSING_NO_NOTES);
117
109
setSyncProgress(100);
170
162
contents = readFile();
171
163
} catch (IOException e) {
172
164
e.printStackTrace();
173
if (Tomdroid.LOGGING_ENABLED) Log.w(TAG, "Something went wrong trying to read the note");
165
TLog.w(TAG, "Something went wrong trying to read the note");
174
166
sendMessage(PARSING_FAILED, ErrorList.createError(note, e));
194
186
StringReader sr = new StringReader(contents);
195
187
InputSource is = new InputSource(sr);
197
if (Tomdroid.LOGGING_ENABLED) Log.d(TAG, "parsing note");
189
TLog.d(TAG, "parsing note");
200
192
// TODO wrap and throw a new exception here
201
193
} catch (Exception e) {
202
194
e.printStackTrace();
203
if(e instanceof TimeFormatException) Log.e(TAG, "Problem parsing the note's date and time");
195
if(e instanceof TimeFormatException) TLog.e(TAG, "Problem parsing the note's date and time");
204
196
sendMessage(PARSING_FAILED, ErrorList.createErrorWithContents(note, e, contents));
209
201
// extract the <note-content..>...</note-content>
210
if (Tomdroid.LOGGING_ENABLED) Log.d(TAG, "retrieving what is inside of note-content");
202
TLog.d(TAG, "retrieving what is inside of note-content");
212
204
// FIXME here we are re-reading the whole note just to grab note-content out, there is probably a best way to do this (I'm talking to you xmlpull.org!)
213
205
Matcher m = note_content.matcher(contents);
215
207
note.setXmlContent(m.group(1));
217
if (Tomdroid.LOGGING_ENABLED) Log.w(TAG, "Something went wrong trying to grab the note-content out of a note");
209
TLog.w(TAG, "Something went wrong trying to grab the note-content out of a note");
218
210
sendMessage(PARSING_FAILED, ErrorList.createErrorWithContents(note, "Something went wrong trying to grab the note-content out of a note", contents));