Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/middleware/AsyncDataDownload.java
Show All 29 Lines | |||||
import it.reyboz.bustorino.backend.*; | import it.reyboz.bustorino.backend.*; | ||||
import it.reyboz.bustorino.data.AppDataProvider; | import it.reyboz.bustorino.data.AppDataProvider; | ||||
import it.reyboz.bustorino.data.NextGenDB; | import it.reyboz.bustorino.data.NextGenDB; | ||||
import it.reyboz.bustorino.fragments.FragmentHelper; | import it.reyboz.bustorino.fragments.FragmentHelper; | ||||
import it.reyboz.bustorino.data.NextGenDB.Contract.*; | import it.reyboz.bustorino.data.NextGenDB.Contract.*; | ||||
import java.lang.ref.WeakReference; | import java.lang.ref.WeakReference; | ||||
import java.util.ArrayList; | import java.util.ArrayList; | ||||
import java.util.Collections; | |||||
import java.util.List; | import java.util.List; | ||||
import java.util.concurrent.atomic.AtomicReference; | import java.util.concurrent.atomic.AtomicReference; | ||||
import java.util.Calendar; | import java.util.Calendar; | ||||
/** | /** | ||||
* This should be used to download data, but not to display it | * This should be used to download data, but not to display it | ||||
*/ | */ | ||||
public class AsyncDataDownload extends AsyncTask<String,Fetcher.result,Object>{ | public class AsyncDataDownload extends AsyncTask<String, Fetcher.Result,Object>{ | ||||
private static final String TAG = "BusTO-DataDownload"; | private static final String TAG = "BusTO-DataDownload"; | ||||
private static final String DEBUG_TAG = TAG; | private static final String DEBUG_TAG = TAG; | ||||
private boolean failedAll = false; | private boolean failedAll = false; | ||||
private final AtomicReference<Fetcher.result> res; | private final AtomicReference<Fetcher.Result> res; | ||||
private final RequestType t; | private final RequestType t; | ||||
private String query; | private String query; | ||||
WeakReference<FragmentHelper> helperRef; | WeakReference<FragmentHelper> helperRef; | ||||
private final ArrayList<Thread> otherActivities = new ArrayList<>(); | private final ArrayList<Thread> otherActivities = new ArrayList<>(); | ||||
private final Fetcher[] theFetchers; | private final Fetcher[] theFetchers; | ||||
private Context context; | private Context context; | ||||
private final boolean replaceFragment; | private final boolean replaceFragment; | ||||
▲ Show 20 Lines • Show All 46 Lines • ▼ Show 20 Lines | protected Object doInBackground(String... params) { | ||||
Stop lastSearchedBusStop = fh.getLastSuccessfullySearchedBusStop(); | Stop lastSearchedBusStop = fh.getLastSuccessfullySearchedBusStop(); | ||||
Palina p; | Palina p; | ||||
String stopID; | String stopID; | ||||
if(params.length>0) | if(params.length>0) | ||||
stopID=params[0]; //(it's a Palina) | stopID=params[0]; //(it's a Palina) | ||||
else if(lastSearchedBusStop!=null) | else if(lastSearchedBusStop!=null) | ||||
stopID = lastSearchedBusStop.ID; //(it's a Palina) | stopID = lastSearchedBusStop.ID; //(it's a Palina) | ||||
else { | else { | ||||
publishProgress(Fetcher.result.QUERY_TOO_SHORT); | publishProgress(Fetcher.Result.QUERY_TOO_SHORT); | ||||
return null; | return null; | ||||
} | } | ||||
//Skip the FiveTAPIFetcher for the Metro Stops because it shows incomprehensible arrival times | //Skip the FiveTAPIFetcher for the Metro Stops because it shows incomprehensible arrival times | ||||
if(f instanceof FiveTAPIFetcher && Integer.parseInt(stopID)>= 8200) | if(f instanceof FiveTAPIFetcher && Integer.parseInt(stopID)>= 8200) | ||||
continue; | continue; | ||||
p= f.ReadArrivalTimesAll(stopID,res); | p= f.ReadArrivalTimesAll(stopID,res); | ||||
publishProgress(res.get()); | publishProgress(res.get()); | ||||
//if (res.get()!= Fetcher.Result.OK) | |||||
Log.d(DEBUG_TAG, "Arrivals fetcher: "+f+"\n\tProgress: "+res.get()); | |||||
if(f instanceof FiveTAPIFetcher){ | if(f instanceof FiveTAPIFetcher){ | ||||
AtomicReference<Fetcher.result> gres = new AtomicReference<>(); | AtomicReference<Fetcher.Result> gres = new AtomicReference<>(); | ||||
List<Route> branches = ((FiveTAPIFetcher) f).getDirectionsForStop(stopID,gres); | List<Route> branches = ((FiveTAPIFetcher) f).getDirectionsForStop(stopID,gres); | ||||
if(gres.get() == Fetcher.result.OK){ | Log.d(DEBUG_TAG, "FiveTArrivals fetcher: "+f+"\n\tDetails req: "+gres.get()); | ||||
if(gres.get() == Fetcher.Result.OK){ | |||||
p.addInfoFromRoutes(branches); | p.addInfoFromRoutes(branches); | ||||
Thread t = new Thread(new BranchInserter(branches, context)); | Thread t = new Thread(new BranchInserter(branches, context)); | ||||
t.start(); | t.start(); | ||||
otherActivities.add(t); | otherActivities.add(t); | ||||
} | } | ||||
//put updated values into Database | //put updated values into Database | ||||
} | } | ||||
if(lastSearchedBusStop != null && res.get()== Fetcher.result.OK) { | if(lastSearchedBusStop != null && res.get()== Fetcher.Result.OK) { | ||||
// check that we don't have the same stop | // check that we don't have the same stop | ||||
if(lastSearchedBusStop.ID.equals(p.ID)) { | if(lastSearchedBusStop.ID.equals(p.ID)) { | ||||
// searched and it's the same | // searched and it's the same | ||||
String sn = lastSearchedBusStop.getStopDisplayName(); | String sn = lastSearchedBusStop.getStopDisplayName(); | ||||
if(sn != null) { | if(sn != null) { | ||||
// "merge" Stop over Palina and we're good to go | // "merge" Stop over Palina and we're good to go | ||||
p.mergeNameFrom(lastSearchedBusStop); | p.mergeNameFrom(lastSearchedBusStop); | ||||
} | } | ||||
Show All 10 Lines | protected Object doInBackground(String... params) { | ||||
Log.d(TAG,"Using the StopFinderByName: "+finder.getClass()); | Log.d(TAG,"Using the StopFinderByName: "+finder.getClass()); | ||||
query =params[0]; | query =params[0]; | ||||
result = resultList; //dummy result | result = resultList; //dummy result | ||||
break; | break; | ||||
default: | default: | ||||
result = null; | result = null; | ||||
} | } | ||||
//find if it went well | //find if it went well | ||||
if(res.get()== Fetcher.result.OK) { | if(res.get()== Fetcher.Result.OK) { | ||||
//wait for other threads to finish | //wait for other threads to finish | ||||
for(Thread t: otherActivities){ | for(Thread t: otherActivities){ | ||||
try { | try { | ||||
t.join(); | t.join(); | ||||
} catch (InterruptedException e) { | } catch (InterruptedException e) { | ||||
//do nothing | //do nothing | ||||
} | } | ||||
} | } | ||||
return result; | return result; | ||||
} | } | ||||
} | } | ||||
//at this point, we are sure that the result has been negative | //at this point, we are sure that the result has been negative | ||||
failedAll=true; | failedAll=true; | ||||
return null; | return null; | ||||
} | } | ||||
@Override | @Override | ||||
protected void onProgressUpdate(Fetcher.result... values) { | protected void onProgressUpdate(Fetcher.Result... values) { | ||||
FragmentHelper fh = helperRef.get(); | FragmentHelper fh = helperRef.get(); | ||||
if (fh!=null) | if (fh!=null) | ||||
for (Fetcher.result r : values){ | for (Fetcher.Result r : values){ | ||||
//TODO: make Toast | //TODO: make Toast | ||||
fh.showErrorMessage(r); | fh.showErrorMessage(r); | ||||
} | } | ||||
else { | else { | ||||
Log.w(TAG,"We had to show some progress but activity was destroyed"); | Log.w(TAG,"We had to show some progress but activity was destroyed"); | ||||
} | } | ||||
} | } | ||||
▲ Show 20 Lines • Show All 70 Lines • ▼ Show 20 Lines | public static class BranchInserter implements Runnable{ | ||||
this.routesToInsert = routesToInsert; | this.routesToInsert = routesToInsert; | ||||
this.context = con.getApplicationContext(); | this.context = con.getApplicationContext(); | ||||
//nextGenDB = new NextGenDB(context); | //nextGenDB = new NextGenDB(context); | ||||
} | } | ||||
@Override | @Override | ||||
public void run() { | public void run() { | ||||
final NextGenDB nextGenDB = new NextGenDB(context); | final NextGenDB nextGenDB = new NextGenDB(context); | ||||
ContentValues[] values = new ContentValues[routesToInsert.size()]; | //ContentValues[] values = new ContentValues[routesToInsert.size()]; | ||||
ArrayList<ContentValues> branchesValues = new ArrayList<>(routesToInsert.size()*4); | |||||
ArrayList<ContentValues> connectionsVals = new ArrayList<>(routesToInsert.size()*4); | ArrayList<ContentValues> connectionsVals = new ArrayList<>(routesToInsert.size()*4); | ||||
long starttime,endtime; | long starttime,endtime; | ||||
for (Route r:routesToInsert){ | for (Route r:routesToInsert){ | ||||
//if it has received an interrupt, stop | //if it has received an interrupt, stop | ||||
if(Thread.interrupted()) return; | if(Thread.interrupted()) return; | ||||
//otherwise, build contentValues | //otherwise, build contentValues | ||||
final ContentValues cv = new ContentValues(); | final ContentValues cv = new ContentValues(); | ||||
cv.put(BranchesTable.COL_BRANCHID,r.branchid); | cv.put(BranchesTable.COL_BRANCHID,r.branchid); | ||||
Show All 23 Lines | public static class BranchInserter implements Runnable{ | ||||
case Calendar.SUNDAY: | case Calendar.SUNDAY: | ||||
cv.put(BranchesTable.COL_DOM,1); | cv.put(BranchesTable.COL_DOM,1); | ||||
break; | break; | ||||
} | } | ||||
} | } | ||||
if(r.type!=null) cv.put(BranchesTable.COL_TYPE, r.type.getCode()); | if(r.type!=null) cv.put(BranchesTable.COL_TYPE, r.type.getCode()); | ||||
cv.put(BranchesTable.COL_FESTIVO, r.festivo.getCode()); | cv.put(BranchesTable.COL_FESTIVO, r.festivo.getCode()); | ||||
values[routesToInsert.indexOf(r)] = cv; | //values[routesToInsert.indexOf(r)] = cv; | ||||
branchesValues.add(cv); | |||||
if(r.getStopsList() != null) | |||||
for(int i=0; i<r.getStopsList().size();i++){ | for(int i=0; i<r.getStopsList().size();i++){ | ||||
String stop = r.getStopsList().get(i); | String stop = r.getStopsList().get(i); | ||||
final ContentValues connVal = new ContentValues(); | final ContentValues connVal = new ContentValues(); | ||||
connVal.put(ConnectionsTable.COLUMN_STOP_ID,stop); | connVal.put(ConnectionsTable.COLUMN_STOP_ID,stop); | ||||
connVal.put(ConnectionsTable.COLUMN_ORDER,i); | connVal.put(ConnectionsTable.COLUMN_ORDER,i); | ||||
connVal.put(ConnectionsTable.COLUMN_BRANCH,r.branchid); | connVal.put(ConnectionsTable.COLUMN_BRANCH,r.branchid); | ||||
//add to global connVals | //add to global connVals | ||||
connectionsVals.add(connVal); | connectionsVals.add(connVal); | ||||
} | } | ||||
} | } | ||||
starttime = System.currentTimeMillis(); | starttime = System.currentTimeMillis(); | ||||
ContentResolver cr = context.getContentResolver(); | ContentResolver cr = context.getContentResolver(); | ||||
try { | try { | ||||
cr.bulkInsert(Uri.parse("content://" + AppDataProvider.AUTHORITY + "/branches/"), values); | cr.bulkInsert(Uri.parse("content://" + AppDataProvider.AUTHORITY + "/branches/"), branchesValues.toArray(new ContentValues[0])); | ||||
endtime = System.currentTimeMillis(); | endtime = System.currentTimeMillis(); | ||||
Log.d("DataDownload", "Inserted branches, took " + (endtime - starttime) + " ms"); | Log.d("DataDownload", "Inserted branches, took " + (endtime - starttime) + " ms"); | ||||
} catch (SQLException exc){ | } catch (SQLException exc){ | ||||
Log.e("AsyncDataDownload","Inserting data: some error happened, aborting the database insert"); | Log.e("AsyncDataDownload","Inserting data: some error happened, aborting the database insert"); | ||||
exc.printStackTrace(); | exc.printStackTrace(); | ||||
return; | return; | ||||
} | } | ||||
if (connectionsVals.size()>0) { | |||||
starttime = System.currentTimeMillis(); | starttime = System.currentTimeMillis(); | ||||
ContentValues[] valArr = connectionsVals.toArray(new ContentValues[0]); | ContentValues[] valArr = connectionsVals.toArray(new ContentValues[0]); | ||||
Log.d("DataDownloadInsert","inserting "+valArr.length+" connections"); | Log.d("DataDownloadInsert", "inserting " + valArr.length + " connections"); | ||||
int rows = nextGenDB.insertBatchContent(valArr,ConnectionsTable.TABLE_NAME); | int rows = nextGenDB.insertBatchContent(valArr, ConnectionsTable.TABLE_NAME); | ||||
endtime = System.currentTimeMillis(); | endtime = System.currentTimeMillis(); | ||||
Log.d("DataDownload","Inserted connections found, took "+(endtime-starttime)+" ms, inserted "+rows+" rows"); | Log.d("DataDownload", "Inserted connections found, took " + (endtime - starttime) + " ms, inserted " + rows + " rows"); | ||||
} | |||||
nextGenDB.close(); | nextGenDB.close(); | ||||
} | } | ||||
} | } | ||||
} | } |
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