Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/middleware/AsyncArrivalsSearcher.java
Show All 20 Lines | |||||
import android.content.ContentResolver; | import android.content.ContentResolver; | ||||
import android.content.ContentValues; | import android.content.ContentValues; | ||||
import android.content.Context; | 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 androidx.core.content.ContextCompat; | |||||
import android.util.Log; | import android.util.Log; | ||||
import android.widget.Toast; | |||||
import it.reyboz.bustorino.R; | |||||
import it.reyboz.bustorino.backend.*; | import it.reyboz.bustorino.backend.*; | ||||
import it.reyboz.bustorino.backend.mato.MatoAPIFetcher; | import it.reyboz.bustorino.backend.mato.MatoAPIFetcher; | ||||
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 AsyncArrivalsSearcher extends AsyncTask<String, Fetcher.Result,Palina>{ | public class AsyncArrivalsSearcher extends AsyncTask<String, Fetcher.Result,Palina>{ | ||||
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> finalResultRef; | ||||
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 ArrivalsFetcher[] theFetchers; | private final ArrivalsFetcher[] theFetchers; | ||||
@SuppressLint("StaticFieldLeak") | @SuppressLint("StaticFieldLeak") | ||||
private final Context context; | private final Context context; | ||||
private final boolean replaceFragment; | private final boolean replaceFragment; | ||||
public AsyncArrivalsSearcher(FragmentHelper fh, @NonNull ArrivalsFetcher[] fetchers, Context context) { | public AsyncArrivalsSearcher(FragmentHelper fh, @NonNull ArrivalsFetcher[] fetchers, Context context) { | ||||
helperRef = new WeakReference<>(fh); | helperRef = new WeakReference<>(fh); | ||||
fh.setLastTaskRef(this); | fh.setLastTaskRef(this); | ||||
res = new AtomicReference<>(); | finalResultRef = new AtomicReference<>(); | ||||
this.context = context.getApplicationContext(); | this.context = context.getApplicationContext(); | ||||
this.replaceFragment = true; | this.replaceFragment = true; | ||||
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!"); | ||||
} | } | ||||
} | } | ||||
@Override | @Override | ||||
protected Palina doInBackground(String... params) { | protected Palina doInBackground(String... params) { | ||||
RecursionHelper<ArrivalsFetcher> r = new RecursionHelper<>(theFetchers); | RecursionHelper<ArrivalsFetcher> r = new RecursionHelper<>(theFetchers); | ||||
Palina result = null; | Palina resultPalina = null; | ||||
FragmentHelper fh = helperRef.get(); | FragmentHelper fh = helperRef.get(); | ||||
ArrayList<Fetcher.Result> results = new ArrayList<>(theFetchers.length); | ArrayList<Fetcher.Result> results = new ArrayList<>(theFetchers.length); | ||||
//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()); | ||||
▲ Show 20 Lines • Show All 61 Lines • ▼ Show 20 Lines | protected Palina doInBackground(String... params) { | ||||
} | } | ||||
p.mergeDuplicateRoutes(0); | p.mergeDuplicateRoutes(0); | ||||
if (resRef.get() == Fetcher.Result.OK && p.getTotalNumberOfPassages() == 0 ) { | if (resRef.get() == Fetcher.Result.OK && p.getTotalNumberOfPassages() == 0 ) { | ||||
resRef.set(Fetcher.Result.EMPTY_RESULT_SET); | resRef.set(Fetcher.Result.EMPTY_RESULT_SET); | ||||
Log.d(DEBUG_TAG, "Setting empty results"); | Log.d(DEBUG_TAG, "Setting empty results"); | ||||
} | } | ||||
publishProgress(resRef.get()); | publishProgress(resRef.get()); | ||||
//TODO: find a way to avoid overloading the user with toasts | //TODO: find a way to avoid overloading the user with toasts | ||||
if (result == null){ | if (resultPalina == null && f instanceof MatoAPIFetcher && p.queryAllRoutes().size() > 0){ | ||||
result = p; | resultPalina = p; | ||||
} | } | ||||
//find if it went well | //find if it went well | ||||
results.add(resRef.get()); | results.add(resRef.get()); | ||||
if(resRef.get()== Fetcher.Result.OK) { | if(resRef.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 p; | return p; | ||||
} | } | ||||
finalResultRef.set(resRef.get()); | |||||
} | } | ||||
/* | |||||
boolean emptyResults = true; | boolean emptyResults = true; | ||||
for (Fetcher.Result re: results){ | for (Fetcher.Result re: results){ | ||||
if (!re.equals(Fetcher.Result.EMPTY_RESULT_SET)) { | if (!re.equals(Fetcher.Result.EMPTY_RESULT_SET)) { | ||||
emptyResults = false; | emptyResults = false; | ||||
break; | break; | ||||
} | } | ||||
} | } | ||||
*/ | |||||
//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 result; | |||||
return resultPalina; | |||||
} | } | ||||
@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, SearchRequestType.ARRIVALS); | fh.showErrorMessage(r, SearchRequestType.ARRIVALS); | ||||
} | } | ||||
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"); | ||||
} | } | ||||
} | } | ||||
@Override | @Override | ||||
protected void onPostExecute(Palina p) { | protected void onPostExecute(Palina p) { | ||||
FragmentHelper fh = helperRef.get(); | FragmentHelper fh = helperRef.get(); | ||||
if(failedAll || p == null || fh == null){ | if(p == null || fh == null){ | ||||
//everything went bad | //everything went bad | ||||
if(fh!=null) fh.toggleSpinner(false); | if(fh!=null) fh.toggleSpinner(false); | ||||
cancel(true); | cancel(true); | ||||
//TODO: send message here | //TODO: send message here | ||||
return; | return; | ||||
} | } | ||||
if(isCancelled()) return; | if(isCancelled()) return; | ||||
▲ Show 20 Lines • Show All 112 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