Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/middleware/AsyncDataDownload.java
Show All 14 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.database.SQLException; | import android.database.SQLException; | ||||
import android.database.sqlite.SQLiteDatabase; | |||||
import android.net.Uri; | import android.net.Uri; | ||||
import android.os.AsyncTask; | import android.os.AsyncTask; | ||||
import android.support.v7.app.AppCompatActivity; | |||||
import androidx.annotation.NonNull; | |||||
import android.util.Log; | import android.util.Log; | ||||
import it.reyboz.bustorino.R; | |||||
import it.reyboz.bustorino.backend.*; | import it.reyboz.bustorino.backend.*; | ||||
import it.reyboz.bustorino.fragments.FragmentHelper; | import it.reyboz.bustorino.fragments.FragmentHelper; | ||||
import it.reyboz.bustorino.middleware.NextGenDB.Contract.*; | import it.reyboz.bustorino.middleware.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 AtomicReference<Fetcher.result> res; | private final AtomicReference<Fetcher.result> res; | ||||
private RequestType t; | private final RequestType t; | ||||
private String query; | private String query; | ||||
WeakReference<FragmentHelper> helperRef; | WeakReference<FragmentHelper> helperRef; | ||||
private ArrayList<Thread> otherActivities = new ArrayList<>(); | private final ArrayList<Thread> otherActivities = new ArrayList<>(); | ||||
private final Fetcher[] theFetchers; | |||||
public AsyncDataDownload(RequestType type,FragmentHelper fh) { | public AsyncDataDownload(FragmentHelper fh, @NonNull Fetcher[] fetchers) { | ||||
t = type; | RequestType type; | ||||
helperRef = new WeakReference<>(fh); | helperRef = new WeakReference<>(fh); | ||||
fh.setLastTaskRef(new WeakReference<AsyncDataDownload>(this)); | fh.setLastTaskRef(new WeakReference<>(this)); | ||||
res = new AtomicReference<>(); | res = new AtomicReference<>(); | ||||
theFetchers = fetchers; | |||||
if (theFetchers.length < 1){ | |||||
throw new IllegalArgumentException("You have to put at least one Fetcher, idiot!"); | |||||
} | |||||
if (theFetchers[0] instanceof ArrivalsFetcher){ | |||||
type = RequestType.ARRIVALS; | |||||
} else if (theFetchers[0] instanceof StopsFinderByName){ | |||||
type = RequestType.STOPS; | |||||
} else{ | |||||
type = null; | |||||
} | |||||
t = type; | |||||
} | } | ||||
@Override | @Override | ||||
protected Object doInBackground(String... params) { | protected Object doInBackground(String... params) { | ||||
RecursionHelper r; | RecursionHelper<Fetcher> r = new RecursionHelper<>(theFetchers); | ||||
boolean success=false; | boolean success=false; | ||||
Object result; | Object result; | ||||
switch (t){ | |||||
case ARRIVALS: | |||||
r = new RecursionHelper<>(new ArrivalsFetcher[] {new FiveTAPIFetcher(),new GTTJSONFetcher(), new FiveTScraperFetcher()}); | |||||
break; | |||||
case STOPS: | |||||
r = new RecursionHelper<>(new StopsFinderByName[] {new GTTStopsFetcher(), new FiveTStopsFetcher()}); | |||||
break; | |||||
default: | |||||
//TODO put error message | |||||
return null; | |||||
} | |||||
FragmentHelper fh = helperRef.get(); | FragmentHelper fh = helperRef.get(); | ||||
//If the FragmentHelper is null, that means the activity doesn't exist anymore | //If the FragmentHelper is null, that means the activity doesn't exist anymore | ||||
if (fh == null){ | if (fh == null){ | ||||
return null; | return null; | ||||
} | } | ||||
//Log.d(TAG,"refresh layout reference is: "+fh.isRefreshLayoutReferenceTrue()); | //Log.d(TAG,"refresh layout reference is: "+fh.isRefreshLayoutReferenceTrue()); | ||||
while(r.valid()) { | while(r.valid()) { | ||||
▲ Show 20 Lines • Show All 139 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 List<Route> routesToInsert; | private final List<Route> routesToInsert; | ||||
private String stopID; | private String stopID; | ||||
private FragmentHelper fragmentHelper; | private final FragmentHelper fragmentHelper; | ||||
public BranchInserter(List<Route> routesToInsert,FragmentHelper fh,String stopID) { | public BranchInserter(List<Route> routesToInsert,FragmentHelper fh,String stopID) { | ||||
this.routesToInsert = routesToInsert; | this.routesToInsert = routesToInsert; | ||||
this.stopID = stopID; | this.stopID = stopID; | ||||
this.fragmentHelper = fh; | this.fragmentHelper = fh; | ||||
} | } | ||||
@Override | @Override | ||||
▲ Show 20 Lines • Show All 75 Lines • Show Last 20 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