Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/data/DatabaseUpdateService.java
Show First 20 Lines • Show All 83 Lines • ▼ Show 20 Lines | protected void onHandleIntent(Intent intent) { | ||||
if(newVersion==VERSION_UNAIVALABLE){ | if(newVersion==VERSION_UNAIVALABLE){ | ||||
//NOTHING LEFT TO DO | //NOTHING LEFT TO DO | ||||
return; | return; | ||||
} | } | ||||
Log.d(DEBUG_TAG,"newDBVersion: "+newVersion+" oldVersion: "+versionDB); | Log.d(DEBUG_TAG,"newDBVersion: "+newVersion+" oldVersion: "+versionDB); | ||||
if(params.mustUpdate || versionDB==-1 || newVersion>versionDB){ | if(params.mustUpdate || versionDB==-1 || newVersion>versionDB){ | ||||
Log.d(DEBUG_TAG,"Downloading the bus stops info"); | Log.d(DEBUG_TAG,"Downloading the bus stops info"); | ||||
final AtomicReference<Fetcher.result> gres = new AtomicReference<>(); | final AtomicReference<Fetcher.Result> gres = new AtomicReference<>(); | ||||
if(!performDBUpdate(gres)) | if(!performDBUpdate(gres)) | ||||
restartDBUpdateifPossible(params,gres); | restartDBUpdateifPossible(params,gres); | ||||
else { | else { | ||||
editor.putInt(DB_VERSION,newVersion); | editor.putInt(DB_VERSION,newVersion); | ||||
// BY COMMENTING THIS, THE APP WILL CONTINUOUSLY UPDATE THE DATABASE | // BY COMMENTING THIS, THE APP WILL CONTINUOUSLY UPDATE THE DATABASE | ||||
editor.apply(); | editor.apply(); | ||||
} | } | ||||
} else { | } else { | ||||
Show All 11 Lines | private boolean setDBUpdatingFlag(SharedPreferences shPr,boolean value){ | ||||
editor.putBoolean(getString(R.string.databaseUpdatingPref),value); | editor.putBoolean(getString(R.string.databaseUpdatingPref),value); | ||||
return editor.commit(); | return editor.commit(); | ||||
} | } | ||||
private boolean setDBUpdatingFlag(boolean value){ | private boolean setDBUpdatingFlag(boolean value){ | ||||
final SharedPreferences shPr = getSharedPreferences(getString(R.string.mainSharedPreferences),MODE_PRIVATE); | final SharedPreferences shPr = getSharedPreferences(getString(R.string.mainSharedPreferences),MODE_PRIVATE); | ||||
return setDBUpdatingFlag(shPr,value); | return setDBUpdatingFlag(shPr,value); | ||||
} | } | ||||
private boolean performDBUpdate(AtomicReference<Fetcher.result> gres){ | private boolean performDBUpdate(AtomicReference<Fetcher.Result> gres){ | ||||
if(!setDBUpdatingFlag(true)) | if(!setDBUpdatingFlag(true)) | ||||
return false; | return false; | ||||
/* | /* | ||||
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<>(); | ||||
▲ Show 20 Lines • Show All 80 Lines • ▼ Show 20 Lines | private boolean performDBUpdate(AtomicReference<Fetcher.Result> gres){ | ||||
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 true; | return true; | ||||
*/ | */ | ||||
return DatabaseUpdate.performDBUpdate(getApplication(),gres) == DatabaseUpdate.Result.DONE; | return DatabaseUpdate.performDBUpdate(getApplication(),gres) == DatabaseUpdate.Result.DONE; | ||||
} | } | ||||
private int getNewVersion(UpdateRequestParams params){ | private int getNewVersion(UpdateRequestParams params){ | ||||
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){ | ||||
restartDBUpdateifPossible(params,gres); | restartDBUpdateifPossible(params,gres); | ||||
return VERSION_UNAIVALABLE; | return VERSION_UNAIVALABLE; | ||||
} | } | ||||
boolean needed; | boolean needed; | ||||
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 -4; | return -4; | ||||
} | } | ||||
} | } | ||||
private void restartDBUpdateifPossible(UpdateRequestParams pars, AtomicReference<Fetcher.result> res){ | private void restartDBUpdateifPossible(UpdateRequestParams pars, AtomicReference<Fetcher.Result> res){ | ||||
if (pars.trial<MAX_TRIALS && res.get()!= Fetcher.result.PARSER_ERROR){ | if (pars.trial<MAX_TRIALS && res.get()!= Fetcher.Result.PARSER_ERROR){ | ||||
Log.d(DEBUG_TAG,"Update failed, starting new trial ("+pars.trial+")"); | Log.d(DEBUG_TAG,"Update failed, starting new trial ("+pars.trial+")"); | ||||
startDBUpdate(getApplicationContext(),++pars.trial,pars.mustUpdate); | startDBUpdate(getApplicationContext(),++pars.trial,pars.mustUpdate); | ||||
} | } | ||||
} | } | ||||
/** | /** | ||||
* Private class to hold the parameters of the request | * Private class to hold the parameters of the request | ||||
*/ | */ | ||||
Show All 35 Lines |
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