Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/middleware/AsyncDataDownload.java
| Show All 11 Lines | BusTO (middleware) | ||||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
| GNU General Public License for more details. | GNU General Public License for more details. | ||||
| 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.annotation.SuppressLint; | |||||
| 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 androidx.core.content.ContextCompat; | ||||
| import android.util.Log; | import android.util.Log; | ||||
| import android.widget.Toast; | import android.widget.Toast; | ||||
| import it.reyboz.bustorino.R; | 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.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.Collections; | ||||
| Show All 11 Lines | public class AsyncDataDownload extends AsyncTask<String, Fetcher.Result,Object>{ | ||||
| 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; | @SuppressLint("StaticFieldLeak") | ||||
| private final Context context; | |||||
| private final boolean replaceFragment; | private final boolean replaceFragment; | ||||
| public AsyncDataDownload(FragmentHelper fh, @NonNull Fetcher[] fetchers, Context context) { | 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<>(); | ||||
| Show All 31 Lines | protected Object doInBackground(String... params) { | ||||
| while(r.valid()) { | while(r.valid()) { | ||||
| if(this.isCancelled()) { | if(this.isCancelled()) { | ||||
| return null; | return null; | ||||
| } | } | ||||
| //get the data from the fetcher | //get the data from the fetcher | ||||
| switch (t){ | switch (t){ | ||||
| case ARRIVALS: | case ARRIVALS: | ||||
| ArrivalsFetcher f = (ArrivalsFetcher) r.getAndMoveForward(); | ArrivalsFetcher f = (ArrivalsFetcher) r.getAndMoveForward(); | ||||
| if (f instanceof MatoAPIFetcher){ | |||||
| ((MatoAPIFetcher)f).setAppContext(context); | |||||
| } | |||||
| Log.d(TAG,"Using the ArrivalsFetcher: "+f.getClass()); | Log.d(TAG,"Using the ArrivalsFetcher: "+f.getClass()); | ||||
| 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 | ||||
| try { | try { | ||||
| if (f instanceof FiveTAPIFetcher && Integer.parseInt(stopID) >= 8200) | if (f instanceof FiveTAPIFetcher && Integer.parseInt(stopID) >= 8200) | ||||
| continue; | continue; | ||||
| } catch (NumberFormatException ex){ | } catch (NumberFormatException ex){ | ||||
| Log.e(DEBUG_TAG, "The stop number is not a valid integer, expect failures"); | Log.e(DEBUG_TAG, "The stop number is not a valid integer, expect failures"); | ||||
| } | } | ||||
| p= f.ReadArrivalTimesAll(stopID,res); | p= f.ReadArrivalTimesAll(stopID,res); | ||||
| publishProgress(res.get()); | |||||
| //if (res.get()!= Fetcher.Result.OK) | //if (res.get()!= Fetcher.Result.OK) | ||||
| Log.d(DEBUG_TAG, "Arrivals fetcher: "+f+"\n\tProgress: "+res.get()); | 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); | ||||
| Log.d(DEBUG_TAG, "FiveTArrivals fetcher: "+f+"\n\tDetails req: "+gres.get()); | Log.d(DEBUG_TAG, "FiveTArrivals fetcher: "+f+"\n\tDetails req: "+gres.get()); | ||||
| Show All 14 Lines | protected Object doInBackground(String... params) { | ||||
| 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); | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| p.mergeDuplicateRoutes(0); | p.mergeDuplicateRoutes(0); | ||||
| if(p.queryAllRoutes().size() == 0) | if (p.getTotalNumberOfPassages() == 0) | ||||
| //skip the rest and go to the next fetcher | res.set(Fetcher.Result.EMPTY_RESULT_SET); | ||||
| continue; | publishProgress(res.get()); | ||||
| //p.sortRoutes(); | |||||
| result = p; | result = p; | ||||
| //TODO: find a way to avoid overloading the user with toasts | //TODO: find a way to avoid overloading the user with toasts | ||||
| break; | break; | ||||
| case STOPS: | case STOPS: | ||||
| StopsFinderByName finder = (StopsFinderByName) r.getAndMoveForward(); | StopsFinderByName finder = (StopsFinderByName) r.getAndMoveForward(); | ||||
| List<Stop> resultList= finder.FindByName(params[0], this.res); //it's a List<Stop> | List<Stop> resultList= finder.FindByName(params[0], this.res); //it's a List<Stop> | ||||
| Log.d(TAG,"Using the StopFinderByName: "+finder.getClass()); | Log.d(TAG,"Using the StopFinderByName: "+finder.getClass()); | ||||
| query =params[0]; | query =params[0]; | ||||
| ▲ Show 20 Lines • Show All 209 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