Clover Coverage Report - RaspberryBusMalaysiaActivity Coverage Report
Coverage timestamp: mar dic 23 2014 15:39:35 EST
../../../img/srcFileCovDistChart8.png 72% of files have more coverage
126   321   51   10,5
48   249   0,4   12
12     4,25  
1    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  SyncTask       Line # 41 126 51 79,6% 0.79569894
 
No Tests
 
1    /*
2    Copyright (C) 2012,2013 Sweetie Piggy Apps <sweetiepiggyapps@gmail.com>
3   
4    This file is part of Raspberry Bus Malaysia.
5   
6    Raspberry Bus Malaysia is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10   
11    Raspberry Bus Malaysia is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14    GNU General Public License for more details.
15   
16    You should have received a copy of the GNU General Public License
17    along with Raspberry Bus Malaysia; if not, see <http://www.gnu.org/licenses/>.
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   
 
41    public class SyncTask extends AsyncTask<Void, Integer, Void>
42    {
43    /* TODO: move to Constants.java */
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   
 
56  1 toggle public SyncTask(Context ctx)
57    {
58  1 this(ctx, true);
59    }
60   
 
61  2 toggle 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   
 
69  2 toggle @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    //mUpdatesFnd = cities.size() + stations.size() + trips.size();
101   
102  2 publishProgress(100);
103   
104  2 dbHelper.open(mCtx);
105  2 dbHelper.setLastUpdate();
106  2 dbHelper.close();
107   
108    /* probably no internet connection */
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   
 
124  2 toggle @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   
 
138  2 toggle @Override
139    protected void onPostExecute(Void result)
140    {
141  2 if (mProgressDialog != null && mCtx != null) {
142  2 try {
143  2 mProgressDialog.dismiss();
144    /* view might no longer be attached to window manager */
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   
 
158  743 toggle @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   
 
167  10 toggle 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   
 
217  10 toggle 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   
 
249  373 toggle 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   
 
263  373 toggle 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    /* not in and is a delim */
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   
 
309  0 toggle 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() {
 
315  0 toggle public void onClick(DialogInterface dialog, int which) {
316    }
317    });
318  0 alert.show();
319    }
320    }
321