Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/middleware/AsyncDataDownload.java
Show All 39 Lines | |||||
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 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; | ||||
▲ Show 20 Lines • Show All 151 Lines • ▼ Show 20 Lines | protected void onPostExecute(Object o) { | ||||
switch (t){ | switch (t){ | ||||
case ARRIVALS: | case ARRIVALS: | ||||
Palina palina = (Palina) o; | Palina palina = (Palina) o; | ||||
fh.createOrUpdateStopFragment(palina, replaceFragment); | fh.createOrUpdateStopFragment(palina, replaceFragment); | ||||
break; | break; | ||||
case STOPS: | case STOPS: | ||||
//this should never be a problem | //this should never be a problem | ||||
List<Stop> stopList = (List<Stop>) o; | if(!(o instanceof List<?>)){ | ||||
throw new IllegalStateException(); | |||||
} | |||||
List<?> list = (List<?>) o; | |||||
if (list.size() ==0) return; | |||||
Object firstItem = list.get(0); | |||||
if(!(firstItem instanceof Stop)) return; | |||||
ArrayList<Stop> stops = new ArrayList<>(); | |||||
for(Object x: list){ | |||||
if(x instanceof Stop) stops.add((Stop) x); | |||||
} | |||||
if(list.size() != stops.size()){ | |||||
Log.w(DEBUG_TAG, "Wrong stop list size:\n incoming: "+ | |||||
list.size()+" out: "+stops.size()); | |||||
} | |||||
//List<Stop> stopList = (List<Stop>) list; | |||||
if(query!=null && !isCancelled()) { | if(query!=null && !isCancelled()) { | ||||
fh.createStopListFragment(stopList,query, replaceFragment); | fh.createStopListFragment(stops,query, replaceFragment); | ||||
} else Log.e(TAG,"QUERY NULL, COULD NOT CREATE FRAGMENT"); | } else Log.e(TAG,"QUERY NULL, COULD NOT CREATE FRAGMENT"); | ||||
break; | break; | ||||
case DBUPDATE: | case DBUPDATE: | ||||
break; | break; | ||||
} | } | ||||
} | } | ||||
@Override | @Override | ||||
protected void onCancelled() { | protected void onCancelled() { | ||||
FragmentHelper fh = helperRef.get(); | FragmentHelper fh = helperRef.get(); | ||||
if (fh!=null) fh.toggleSpinner(false); | if (fh!=null) fh.toggleSpinner(false); | ||||
} | } | ||||
@Override | @Override | ||||
protected void onPreExecute() { | protected void onPreExecute() { | ||||
FragmentHelper fh = helperRef.get(); | FragmentHelper fh = helperRef.get(); | ||||
if (fh!=null) fh.toggleSpinner(true); | if (fh!=null) fh.toggleSpinner(true); | ||||
} | } | ||||
public enum RequestType { | public enum RequestType { | ||||
ARRIVALS,STOPS,DBUPDATE | ARRIVALS,STOPS,DBUPDATE | ||||
} | } | ||||
public class BranchInserter implements Runnable{ | public static class BranchInserter implements Runnable{ | ||||
private final List<Route> routesToInsert; | private final List<Route> routesToInsert; | ||||
private final Context context; | private final Context context; | ||||
private final NextGenDB nextGenDB; | //private final NextGenDB nextGenDB; | ||||
public BranchInserter(List<Route> routesToInsert,@NonNull Context con) { | public BranchInserter(List<Route> routesToInsert,@NonNull Context con) { | ||||
this.routesToInsert = routesToInsert; | this.routesToInsert = routesToInsert; | ||||
this.context = con; | 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); | |||||
ContentValues[] values = new ContentValues[routesToInsert.size()]; | ContentValues[] values = new ContentValues[routesToInsert.size()]; | ||||
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(); | ||||
▲ Show 20 Lines • Show All 55 Lines • ▼ Show 20 Lines | public static class BranchInserter implements Runnable{ | ||||
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(); | |||||
} | } | ||||
} | } | ||||
} | } |
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