~tomdroid-dev/tomdroid/check-for-linebreak

« back to all changes in this revision

Viewing changes to src/org/tomdroid/sync/sd/SdCardSyncService.java

  • Committer: Olivier Bilodeau
  • Date: 2012-03-31 14:36:55 UTC
  • mfrom: (254.2.8 loggers-cleanup)
  • Revision ID: olivier@bottomlesspit.org-20120331143655-0osjgyvnxvy3c90f
Our own mini-logging layer. Fixes lp:882218

Thanks to Piotr Adamski for the work

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
 */
25
25
package org.tomdroid.sync.sd;
26
26
 
27
 
import java.io.File;
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;
36
 
 
37
 
import javax.xml.parsers.SAXParser;
38
 
import javax.xml.parsers.SAXParserFactory;
39
 
 
 
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;
47
38
 
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;
 
41
import java.io.*;
 
42
import java.util.regex.Matcher;
 
43
import java.util.regex.Pattern;
52
44
 
53
45
public class SdCardSyncService extends SyncService {
54
46
        
90
82
                setSyncProgress(0);
91
83
 
92
84
                // start loading local notes
93
 
                if (Tomdroid.LOGGING_ENABLED) Log.v(TAG, "Loading local notes");
 
85
                TLog.v(TAG, "Loading local notes");
94
86
                
95
87
                File path = new File(Tomdroid.NOTES_PATH);
96
88
                
97
89
                if (!path.exists())
98
90
                        path.mkdir();
99
91
                
100
 
                Log.i(TAG, "Path "+path+" exists: "+path.exists());
 
92
                TLog.i(TAG, "Path {0} exists: {1}", path, path.exists());
101
93
                
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);
107
99
                        return;
112
104
                
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);
118
110
                        return;
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));
175
167
                                onWorkDone();
176
168
                                return;
194
186
                        StringReader sr = new StringReader(contents);
195
187
                        InputSource is = new InputSource(sr);
196
188
                        
197
 
                                if (Tomdroid.LOGGING_ENABLED) Log.d(TAG, "parsing note");
 
189
                                TLog.d(TAG, "parsing note");
198
190
                                xr.parse(is);
199
191
 
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));
205
197
                                onWorkDone();
206
198
                                return;
207
199
                        }
208
200
 
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");
211
203
                        
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);
214
206
                        if (m.find()) {
215
207
                                note.setXmlContent(m.group(1));
216
208
                        } else {
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));
219
211
                                onWorkDone();
220
212
                                return;