Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/data/DatabaseUpdate.java
Show All 30 Lines | public class DatabaseUpdate { | ||||
DONE, ERROR_STOPS_DOWNLOAD, ERROR_LINES_DOWNLOAD | DONE, ERROR_STOPS_DOWNLOAD, ERROR_LINES_DOWNLOAD | ||||
} | } | ||||
/** | /** | ||||
* Request the server the version of the database | * Request the server the version of the database | ||||
* @return the version of the DB, or an error code | * @return the version of the DB, or an error code | ||||
*/ | */ | ||||
public static int getNewVersion(){ | public static int getNewVersion(){ | ||||
AtomicReference<Fetcher.result> gres = new AtomicReference<>(); | AtomicReference<Fetcher.Result> gres = new AtomicReference<>(); | ||||
String networkRequest = FiveTAPIFetcher.performAPIRequest(FiveTAPIFetcher.QueryType.STOPS_VERSION,null,gres); | String networkRequest = FiveTAPIFetcher.performAPIRequest(FiveTAPIFetcher.QueryType.STOPS_VERSION,null,gres); | ||||
if(networkRequest == null){ | if(networkRequest == null){ | ||||
return VERSION_UNAVAILABLE; | return VERSION_UNAVAILABLE; | ||||
} | } | ||||
try { | try { | ||||
JSONObject resp = new JSONObject(networkRequest); | JSONObject resp = new JSONObject(networkRequest); | ||||
return resp.getInt("id"); | return resp.getInt("id"); | ||||
} catch (JSONException e) { | } catch (JSONException e) { | ||||
e.printStackTrace(); | e.printStackTrace(); | ||||
Log.e(DEBUG_TAG,"Error: wrong JSON response\nResponse:\t"+networkRequest); | Log.e(DEBUG_TAG,"Error: wrong JSON response\nResponse:\t"+networkRequest); | ||||
return JSON_PARSING_ERROR; | return JSON_PARSING_ERROR; | ||||
} | } | ||||
} | } | ||||
/** | /** | ||||
* Run the DB Update | * Run the DB Update | ||||
* @param con a context | * @param con a context | ||||
* @param gres a result reference | * @param gres a result reference | ||||
* @return result of the update | * @return result of the update | ||||
*/ | */ | ||||
public static Result performDBUpdate(Context con, AtomicReference<Fetcher.result> gres) { | public static Result performDBUpdate(Context con, AtomicReference<Fetcher.Result> gres) { | ||||
final FiveTAPIFetcher f = new FiveTAPIFetcher(); | final FiveTAPIFetcher f = new FiveTAPIFetcher(); | ||||
final ArrayList<Stop> stops = f.getAllStopsFromGTT(gres); | final ArrayList<Stop> stops = f.getAllStopsFromGTT(gres); | ||||
//final ArrayList<ContentProviderOperation> cpOp = new ArrayList<>(); | //final ArrayList<ContentProviderOperation> cpOp = new ArrayList<>(); | ||||
if (gres.get() != Fetcher.result.OK) { | if (gres.get() != Fetcher.Result.OK) { | ||||
Log.w(DEBUG_TAG, "Something went wrong downloading"); | Log.w(DEBUG_TAG, "Something went wrong downloading"); | ||||
return Result.ERROR_STOPS_DOWNLOAD; | return DatabaseUpdate.Result.ERROR_STOPS_DOWNLOAD; | ||||
} | } | ||||
// return false; //If the commit to the SharedPreferences didn't succeed, simply stop updating the database | // return false; //If the commit to the SharedPreferences didn't succeed, simply stop updating the database | ||||
final NextGenDB dbHelp = new NextGenDB(con.getApplicationContext()); | final NextGenDB dbHelp = new NextGenDB(con.getApplicationContext()); | ||||
final SQLiteDatabase db = dbHelp.getWritableDatabase(); | final SQLiteDatabase db = dbHelp.getWritableDatabase(); | ||||
//Empty the needed tables | //Empty the needed tables | ||||
db.beginTransaction(); | db.beginTransaction(); | ||||
//db.execSQL("DELETE FROM "+StopsTable.TABLE_NAME); | //db.execSQL("DELETE FROM "+StopsTable.TABLE_NAME); | ||||
Show All 27 Lines | public class DatabaseUpdate { | ||||
long endTime = System.currentTimeMillis(); | long endTime = System.currentTimeMillis(); | ||||
Log.d(DEBUG_TAG, "Inserting stops took: " + ((double) (endTime - startTime) / 1000) + " s"); | Log.d(DEBUG_TAG, "Inserting stops took: " + ((double) (endTime - startTime) / 1000) + " s"); | ||||
final ArrayList<Route> routes = f.getAllLinesFromGTT(gres); | final ArrayList<Route> routes = f.getAllLinesFromGTT(gres); | ||||
if (routes == null) { | if (routes == null) { | ||||
Log.w(DEBUG_TAG, "Something went wrong downloading the lines"); | Log.w(DEBUG_TAG, "Something went wrong downloading the lines"); | ||||
dbHelp.close(); | dbHelp.close(); | ||||
return Result.ERROR_LINES_DOWNLOAD; | return DatabaseUpdate.Result.ERROR_LINES_DOWNLOAD; | ||||
} | } | ||||
db.beginTransaction(); | db.beginTransaction(); | ||||
startTime = System.currentTimeMillis(); | startTime = System.currentTimeMillis(); | ||||
for (Route r : routes) { | for (Route r : routes) { | ||||
final ContentValues cv = new ContentValues(); | final ContentValues cv = new ContentValues(); | ||||
cv.put(NextGenDB.Contract.LinesTable.COLUMN_NAME, r.getName()); | cv.put(NextGenDB.Contract.LinesTable.COLUMN_NAME, r.getName()); | ||||
Show All 17 Lines | public class DatabaseUpdate { | ||||
} | } | ||||
} | } | ||||
db.setTransactionSuccessful(); | db.setTransactionSuccessful(); | ||||
db.endTransaction(); | db.endTransaction(); | ||||
endTime = System.currentTimeMillis(); | endTime = System.currentTimeMillis(); | ||||
Log.d(DEBUG_TAG, "Inserting lines took: " + ((double) (endTime - startTime) / 1000) + " s"); | Log.d(DEBUG_TAG, "Inserting lines took: " + ((double) (endTime - startTime) / 1000) + " s"); | ||||
dbHelp.close(); | dbHelp.close(); | ||||
return Result.DONE; | return DatabaseUpdate.Result.DONE; | ||||
} | } | ||||
public static boolean setDBUpdatingFlag(Context con, boolean value){ | public static boolean setDBUpdatingFlag(Context con, boolean value){ | ||||
final SharedPreferences shPr = con.getSharedPreferences(con.getString(R.string.mainSharedPreferences),MODE_PRIVATE); | final SharedPreferences shPr = con.getSharedPreferences(con.getString(R.string.mainSharedPreferences),MODE_PRIVATE); | ||||
return setDBUpdatingFlag(con, shPr, value); | return setDBUpdatingFlag(con, shPr, value); | ||||
} | } | ||||
static boolean setDBUpdatingFlag(Context con, SharedPreferences shPr,boolean value){ | static boolean setDBUpdatingFlag(Context con, SharedPreferences shPr,boolean value){ | ||||
final SharedPreferences.Editor editor = shPr.edit(); | final SharedPreferences.Editor editor = shPr.edit(); | ||||
editor.putBoolean(con.getString(R.string.databaseUpdatingPref),value); | editor.putBoolean(con.getString(R.string.databaseUpdatingPref),value); | ||||
return editor.commit(); | return editor.commit(); | ||||
} | } | ||||
} | } |
Public contents are in Creative Commons Attribution-ShareAlike 4.0 (CC-BY-SA) or GNU Free Documentation License (at your option) unless otherwise noted. · Contact / Register