1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package com.sweetiepiggy.raspberrybusmalaysia; |
21 |
|
|
22 |
|
import java.io.BufferedReader; |
23 |
|
import java.io.FileNotFoundException; |
24 |
|
import java.io.IOException; |
25 |
|
import java.io.InputStreamReader; |
26 |
|
import java.io.UnsupportedEncodingException; |
27 |
|
import java.net.MalformedURLException; |
28 |
|
import java.net.URL; |
29 |
|
import java.net.UnknownHostException; |
30 |
|
import java.util.Iterator; |
31 |
|
import java.util.LinkedList; |
32 |
|
|
33 |
|
import android.app.AlertDialog; |
34 |
|
import android.app.ProgressDialog; |
35 |
|
import android.content.ContentValues; |
36 |
|
import android.content.Context; |
37 |
|
import android.content.DialogInterface; |
38 |
|
import android.os.AsyncTask; |
39 |
|
import android.widget.Toast; |
40 |
|
|
|
|
| 79,6% |
Uncovered Elements: 38 (186) |
Complexity: 51 |
Complexity Density: 0,4 |
|
41 |
|
public class SyncTask extends AsyncTask<Void, Integer, Void> |
42 |
|
{ |
43 |
|
|
44 |
|
private final String BASE_URL = "https://raw.github.com/sweetiepiggy/Raspberry-Bus-Malaysia/trips/"; |
45 |
|
private final String TRIPS_URL = BASE_URL + "trips.csv"; |
46 |
|
private final String CITIES_URL = BASE_URL + "cities.csv"; |
47 |
|
private final String STATIONS_URL = BASE_URL + "stations.csv"; |
48 |
|
private final String AGENTS_URL = BASE_URL + "agents.csv"; |
49 |
|
private final String OPERATORS_URL = BASE_URL + "operators.csv"; |
50 |
|
|
51 |
|
private Context mCtx; |
52 |
|
private int mUpdatesFnd = 0; |
53 |
|
private String mAlertMsg = null; |
54 |
|
private ProgressDialog mProgressDialog = null; |
55 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
56 |
1
|
public SyncTask(Context ctx)... |
57 |
|
{ |
58 |
1
|
this(ctx, true); |
59 |
|
} |
60 |
|
|
|
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 3 |
Complexity Density: 1 |
|
61 |
2
|
public SyncTask(Context ctx, boolean showProgress)... |
62 |
|
{ |
63 |
2
|
mCtx = ctx; |
64 |
2
|
if (ctx != null && showProgress) { |
65 |
2
|
mProgressDialog = new ProgressDialog(mCtx); |
66 |
|
} |
67 |
|
} |
68 |
|
|
|
|
| 80,8% |
Uncovered Elements: 5 (26) |
Complexity: 6 |
Complexity Density: 0,23 |
|
69 |
2
|
@Override... |
70 |
|
protected Void doInBackground(Void... params) |
71 |
|
{ |
72 |
2
|
mUpdatesFnd = 0; |
73 |
|
|
74 |
2
|
try { |
75 |
2
|
DbAdapter dbHelper = new DbAdapter(); |
76 |
2
|
dbHelper.open(mCtx); |
77 |
2
|
String lastUpdate = dbHelper.getLastUpdate(); |
78 |
2
|
dbHelper.close(); |
79 |
|
|
80 |
2
|
LinkedList<ContentValues> cities = parse_csv(CITIES_URL, |
81 |
|
lastUpdate, 0, 3); |
82 |
2
|
mUpdatesFnd += sync_table(cities, DbAdapter.TABLE_CITIES, 3, 20); |
83 |
|
|
84 |
2
|
LinkedList<ContentValues> stations = parse_csv(STATIONS_URL, |
85 |
|
lastUpdate, 20, 23); |
86 |
2
|
mUpdatesFnd += sync_table(stations, DbAdapter.TABLE_STATIONS, 23, 40); |
87 |
|
|
88 |
2
|
LinkedList<ContentValues> trips = parse_csv(TRIPS_URL, |
89 |
|
lastUpdate, 40, 43); |
90 |
2
|
mUpdatesFnd += sync_table(trips, DbAdapter.TABLE_TRIPS, 43, 60); |
91 |
|
|
92 |
2
|
LinkedList<ContentValues> agents = parse_csv(AGENTS_URL, |
93 |
|
lastUpdate, 60, 63); |
94 |
2
|
mUpdatesFnd += sync_table(agents, DbAdapter.TABLE_AGENTS, 63, 80); |
95 |
|
|
96 |
2
|
LinkedList<ContentValues> operators = parse_csv(OPERATORS_URL, |
97 |
|
lastUpdate, 80, 83); |
98 |
2
|
mUpdatesFnd += sync_table(operators, DbAdapter.TABLE_OPERATORS, 83, 100); |
99 |
|
|
100 |
|
|
101 |
|
|
102 |
2
|
publishProgress(100); |
103 |
|
|
104 |
2
|
dbHelper.open(mCtx); |
105 |
2
|
dbHelper.setLastUpdate(); |
106 |
2
|
dbHelper.close(); |
107 |
|
|
108 |
|
|
109 |
|
} catch (UnknownHostException e) { |
110 |
0
|
mAlertMsg = mCtx.getResources().getString(R.string.unknown_host); |
111 |
|
} catch (java.io.FileNotFoundException e) { |
112 |
0
|
mAlertMsg = mCtx.getResources().getString(R.string.file_not_found) + ":\n" + e.getMessage(); |
113 |
|
} catch (MalformedURLException e) { |
114 |
0
|
throw new Error(e); |
115 |
|
} catch (UnsupportedEncodingException e) { |
116 |
0
|
throw new Error(e); |
117 |
|
} catch (IOException e) { |
118 |
0
|
throw new Error(e); |
119 |
|
} |
120 |
|
|
121 |
2
|
return null; |
122 |
|
} |
123 |
|
|
|
|
| 90% |
Uncovered Elements: 1 (10) |
Complexity: 3 |
Complexity Density: 0,38 |
|
124 |
2
|
@Override... |
125 |
|
protected void onPreExecute() |
126 |
|
{ |
127 |
2
|
super.onPreExecute(); |
128 |
2
|
if (mProgressDialog != null && mCtx != null) { |
129 |
2
|
mProgressDialog.setMessage(mCtx.getResources().getString(R.string.syncing)); |
130 |
2
|
mProgressDialog.setIndeterminate(false); |
131 |
2
|
mProgressDialog.setMax(100); |
132 |
2
|
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); |
133 |
2
|
mProgressDialog.setProgress(0); |
134 |
2
|
mProgressDialog.show(); |
135 |
|
} |
136 |
|
} |
137 |
|
|
|
|
| 69,2% |
Uncovered Elements: 4 (13) |
Complexity: 8 |
Complexity Density: 1,14 |
|
138 |
2
|
@Override... |
139 |
|
protected void onPostExecute(Void result) |
140 |
|
{ |
141 |
2
|
if (mProgressDialog != null && mCtx != null) { |
142 |
2
|
try { |
143 |
2
|
mProgressDialog.dismiss(); |
144 |
|
|
145 |
|
} catch (IllegalArgumentException e) { |
146 |
|
} |
147 |
|
} |
148 |
2
|
if (mAlertMsg != null && mCtx != null) { |
149 |
0
|
alert(mAlertMsg); |
150 |
2
|
} else if (mProgressDialog != null && mCtx != null) { |
151 |
2
|
Toast.makeText(mCtx, |
152 |
|
Integer.toString(mUpdatesFnd) + " " + |
153 |
|
mCtx.getResources().getString(R.string.updates_found), |
154 |
|
Toast.LENGTH_SHORT).show(); |
155 |
|
} |
156 |
|
} |
157 |
|
|
|
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 2 |
Complexity Density: 0,67 |
|
158 |
743
|
@Override... |
159 |
|
protected void onProgressUpdate(Integer... values) |
160 |
|
{ |
161 |
743
|
super.onProgressUpdate(values); |
162 |
743
|
if (mProgressDialog != null) { |
163 |
743
|
mProgressDialog.setProgress(values[0]); |
164 |
|
} |
165 |
|
} |
166 |
|
|
|
|
| 84,4% |
Uncovered Elements: 5 (32) |
Complexity: 8 |
Complexity Density: 0,36 |
|
167 |
10
|
private LinkedList<ContentValues> parse_csv(String url_name, String lastUpdate,... |
168 |
|
int progress_offset, int progress_max) throws |
169 |
|
UnknownHostException, MalformedURLException, UnsupportedEncodingException, |
170 |
|
IOException |
171 |
|
{ |
172 |
10
|
LinkedList<ContentValues> ret = new LinkedList<ContentValues>(); |
173 |
|
|
174 |
10
|
URL url = new URL(url_name); |
175 |
10
|
BufferedReader in = new BufferedReader( |
176 |
|
new InputStreamReader(url.openStream(), "utf-8")); |
177 |
|
|
178 |
10
|
String line = in.readLine(); |
179 |
10
|
String[] field_names = null; |
180 |
|
|
181 |
10
|
if (line != null) { |
182 |
10
|
field_names = line.split(","); |
183 |
|
} |
184 |
|
|
185 |
10
|
long max_id = 0; |
186 |
10
|
int added = 0; |
187 |
10
|
boolean done = false; |
188 |
|
|
189 |
?
|
while ((line = in.readLine()) != null && !done) { |
190 |
373
|
ContentValues cv = parse_line(line, field_names); |
191 |
|
|
192 |
373
|
if (cv.containsKey(DbAdapter.KEY_UPDATE_DATE) && |
193 |
|
lastUpdate.compareTo(cv.getAsString(DbAdapter.KEY_UPDATE_DATE)) >= 0) { |
194 |
5
|
done = true; |
195 |
|
} else { |
196 |
368
|
ret.addFirst(cv); |
197 |
368
|
++added; |
198 |
|
} |
199 |
|
|
200 |
373
|
if (cv.containsKey(DbAdapter.KEY_ROWID)) { |
201 |
373
|
max_id = java.lang.Math.max(max_id, cv.getAsLong(DbAdapter.KEY_ROWID)); |
202 |
|
} |
203 |
373
|
if (max_id != 0) { |
204 |
373
|
publishProgress(java.lang.Math.min(progress_max, |
205 |
|
progress_offset + |
206 |
|
(int)((progress_max - progress_offset) * |
207 |
|
(double) added / max_id))); |
208 |
|
} |
209 |
|
} |
210 |
|
|
211 |
10
|
in.close(); |
212 |
|
|
213 |
10
|
return ret; |
214 |
|
} |
215 |
|
|
216 |
|
|
|
|
| 90,9% |
Uncovered Elements: 2 (22) |
Complexity: 4 |
Complexity Density: 0,25 |
|
217 |
10
|
private int sync_table(LinkedList<ContentValues> values, String table,... |
218 |
|
int progress_offset, int progress_max) |
219 |
|
{ |
220 |
10
|
int new_added = 0; |
221 |
|
|
222 |
10
|
DbAdapter dbHelper = new DbAdapter(); |
223 |
10
|
dbHelper.open_readwrite(mCtx, false); |
224 |
|
|
225 |
10
|
long add_total = values.size(); |
226 |
10
|
final long orig_max_id = dbHelper.fetch_max_id(table); |
227 |
|
|
228 |
10
|
int added = 0; |
229 |
|
|
230 |
10
|
Iterator<ContentValues> itr = values.listIterator(); |
231 |
378
|
while (itr.hasNext()) { |
232 |
368
|
ContentValues cv = itr.next(); |
233 |
368
|
if (dbHelper.replace(cv, table) > orig_max_id) { |
234 |
368
|
++new_added; |
235 |
|
} |
236 |
368
|
++added; |
237 |
368
|
if (add_total != 0) { |
238 |
368
|
publishProgress(java.lang.Math.min(progress_max, |
239 |
|
progress_offset + |
240 |
|
(int)((progress_max - progress_offset) * |
241 |
|
(double) added / add_total))); |
242 |
|
} |
243 |
|
} |
244 |
10
|
dbHelper.close(); |
245 |
|
|
246 |
10
|
return new_added; |
247 |
|
} |
248 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 2 |
Complexity Density: 0,29 |
|
249 |
373
|
private ContentValues parse_line(String line, String[] field_names)... |
250 |
|
{ |
251 |
373
|
ContentValues ret = new ContentValues(); |
252 |
|
|
253 |
373
|
String[] fields = splitNotIn(line, '"', ','); |
254 |
4107
|
for (int i = 0; i < fields.length; ++i) { |
255 |
3734
|
fields[i] = fields[i].replaceAll("^\"", ""); |
256 |
3734
|
fields[i] = fields[i].replaceAll("\"$", ""); |
257 |
3734
|
ret.put(field_names[i], fields[i]); |
258 |
|
} |
259 |
|
|
260 |
373
|
return ret; |
261 |
|
} |
262 |
|
|
|
|
| 73,9% |
Uncovered Elements: 12 (46) |
Complexity: 12 |
Complexity Density: 0,43 |
|
263 |
373
|
private String[] splitNotIn(String str, char notInChar, char delim)... |
264 |
|
{ |
265 |
373
|
LinkedList<String> tkns = new LinkedList<String>(); |
266 |
|
|
267 |
373
|
int len = str.length(); |
268 |
373
|
boolean isIn = false; |
269 |
373
|
boolean needJoin = false; |
270 |
373
|
int beg = 0; |
271 |
|
|
272 |
30466
|
for (int i=0; i < len; ++i) { |
273 |
30093
|
if (str.charAt(i) == notInChar) { |
274 |
2774
|
if (isIn) { |
275 |
1387
|
if (needJoin) { |
276 |
0
|
String tmp = tkns.removeLast(); |
277 |
0
|
tkns.add(tmp + notInChar + |
278 |
|
str.substring(beg+1, i) + |
279 |
|
notInChar); |
280 |
0
|
needJoin = false; |
281 |
|
} else { |
282 |
1387
|
tkns.add(str.substring(beg+1, i)); |
283 |
|
} |
284 |
1387
|
beg = i + 1; |
285 |
1387
|
} else if (i - beg != 0) { |
286 |
0
|
tkns.add(str.substring(beg, i)); |
287 |
0
|
beg = i; |
288 |
0
|
needJoin = true; |
289 |
|
} |
290 |
2774
|
isIn = !isIn; |
291 |
|
|
292 |
27319
|
} else if (!isIn && str.charAt(i) == delim) { |
293 |
3361
|
if (i - beg != 0) { |
294 |
2347
|
tkns.add(str.substring(beg, i)); |
295 |
|
} |
296 |
3361
|
beg = i + 1; |
297 |
|
} |
298 |
|
} |
299 |
|
|
300 |
373
|
if (beg != 0 && beg != len) { |
301 |
0
|
tkns.add(str.substring(beg)); |
302 |
373
|
} else if (beg == 0) { |
303 |
0
|
tkns.add(str); |
304 |
|
} |
305 |
|
|
306 |
373
|
return tkns.toArray(new String[tkns.size()]); |
307 |
|
} |
308 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 1 |
Complexity Density: 0,2 |
|
309 |
0
|
private void alert(String msg)... |
310 |
|
{ |
311 |
0
|
AlertDialog.Builder alert = new AlertDialog.Builder(mCtx); |
312 |
0
|
alert.setTitle(mCtx.getResources().getString(android.R.string.dialog_alert_title)); |
313 |
0
|
alert.setMessage(msg); |
314 |
0
|
alert.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
315 |
0
|
public void onClick(DialogInterface dialog, int which) {... |
316 |
|
} |
317 |
|
}); |
318 |
0
|
alert.show(); |
319 |
|
} |
320 |
|
} |
321 |
|
|