Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/middleware/AsyncDataDownload.java
Show All 13 Lines | BusTO (middleware) | ||||
You should have received a copy of the GNU General Public License | You should have received a copy of the GNU General Public License | ||||
along with this program. If not, see <http://www.gnu.org/licenses/>. | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
*/ | */ | ||||
package it.reyboz.bustorino.middleware; | package it.reyboz.bustorino.middleware; | ||||
import android.content.ContentResolver; | import android.content.ContentResolver; | ||||
import android.content.ContentValues; | import android.content.ContentValues; | ||||
import android.content.Context; | |||||
import android.database.SQLException; | import android.database.SQLException; | ||||
import android.net.Uri; | import android.net.Uri; | ||||
import android.os.AsyncTask; | import android.os.AsyncTask; | ||||
import androidx.annotation.NonNull; | import androidx.annotation.NonNull; | ||||
import android.util.Log; | import android.util.Log; | ||||
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.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.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 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; | |||||
public AsyncDataDownload(FragmentHelper fh, @NonNull Fetcher[] fetchers) { | public AsyncDataDownload(FragmentHelper fh, @NonNull Fetcher[] fetchers, Context context) { | ||||
RequestType type; | RequestType type; | ||||
helperRef = new WeakReference<>(fh); | helperRef = new WeakReference<>(fh); | ||||
fh.setLastTaskRef(new WeakReference<>(this)); | fh.setLastTaskRef(new WeakReference<>(this)); | ||||
res = new AtomicReference<>(); | res = new AtomicReference<>(); | ||||
this.context = context.getApplicationContext(); | |||||
theFetchers = fetchers; | theFetchers = fetchers; | ||||
if (theFetchers.length < 1){ | if (theFetchers.length < 1){ | ||||
throw new IllegalArgumentException("You have to put at least one Fetcher, idiot!"); | throw new IllegalArgumentException("You have to put at least one Fetcher, idiot!"); | ||||
} | } | ||||
if (theFetchers[0] instanceof ArrivalsFetcher){ | if (theFetchers[0] instanceof ArrivalsFetcher){ | ||||
type = RequestType.ARRIVALS; | type = RequestType.ARRIVALS; | ||||
} else if (theFetchers[0] instanceof StopsFinderByName){ | } else if (theFetchers[0] instanceof StopsFinderByName){ | ||||
▲ Show 20 Lines • Show All 44 Lines • ▼ Show 20 Lines | protected Object doInBackground(String... params) { | ||||
p= f.ReadArrivalTimesAll(stopID,res); | p= f.ReadArrivalTimesAll(stopID,res); | ||||
publishProgress(res.get()); | publishProgress(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){ | if(gres.get() == Fetcher.result.OK){ | ||||
p.addInfoFromRoutes(branches); | p.addInfoFromRoutes(branches); | ||||
Thread t = new Thread(new BranchInserter(branches,fh,stopID)); | 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) { | ||||
▲ Show 20 Lines • Show All 101 Lines • ▼ Show 20 Lines | public class AsyncDataDownload extends AsyncTask<String,Fetcher.result,Object>{ | ||||
public enum RequestType { | public enum RequestType { | ||||
ARRIVALS,STOPS,DBUPDATE | ARRIVALS,STOPS,DBUPDATE | ||||
} | } | ||||
public class BranchInserter implements Runnable{ | public class BranchInserter implements Runnable{ | ||||
private final List<Route> routesToInsert; | private final List<Route> routesToInsert; | ||||
private final FragmentHelper fragmentHelper; | private final Context context; | ||||
private final NextGenDB nextGenDB; | |||||
public BranchInserter(List<Route> routesToInsert,FragmentHelper fh,String stopID) { | public BranchInserter(List<Route> routesToInsert,@NonNull Context con) { | ||||
this.routesToInsert = routesToInsert; | this.routesToInsert = routesToInsert; | ||||
this.fragmentHelper = fh; | this.context = con; | ||||
nextGenDB = new NextGenDB(context); | |||||
} | } | ||||
@Override | @Override | ||||
public void run() { | public void run() { | ||||
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){ | ||||
▲ Show 20 Lines • Show All 41 Lines • ▼ Show 20 Lines | public class BranchInserter implements Runnable{ | ||||
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 = fragmentHelper.getContentResolver(); | ContentResolver cr = context.getContentResolver(); | ||||
try { | try { | ||||
cr.bulkInsert(Uri.parse("content://" + AppDataProvider.AUTHORITY + "/branches/"), values); | cr.bulkInsert(Uri.parse("content://" + AppDataProvider.AUTHORITY + "/branches/"), values); | ||||
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; | ||||
} | } | ||||
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 = fragmentHelper.insertBatchDataInNextGenDB(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"); | ||||
} | } | ||||
} | } | ||||
} | } |
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