Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/middleware/AsyncDataDownload.java
Show All 19 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.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; | ||||
▲ Show 20 Lines • Show All 45 Lines • ▼ Show 20 Lines | public class AsyncDataDownload extends AsyncTask<String, Fetcher.Result,Object>{ | ||||
} | } | ||||
@Override | @Override | ||||
protected Object doInBackground(String... params) { | protected Object doInBackground(String... params) { | ||||
RecursionHelper<Fetcher> r = new RecursionHelper<>(theFetchers); | RecursionHelper<Fetcher> r = new RecursionHelper<>(theFetchers); | ||||
boolean success=false; | boolean success=false; | ||||
Object result; | Object result; | ||||
FragmentHelper fh = helperRef.get(); | FragmentHelper fh = helperRef.get(); | ||||
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()); | ||||
while(r.valid()) { | while(r.valid()) { | ||||
if(this.isCancelled()) { | if(this.isCancelled()) { | ||||
▲ Show 20 Lines • Show All 63 Lines • ▼ Show 20 Lines | protected Object doInBackground(String... params) { | ||||
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]; | ||||
result = resultList; //dummy result | result = resultList; //dummy result | ||||
Log.d(DEBUG_TAG, "Result: "+res.get()+", "+resultList.size()+" stops"); | |||||
break; | break; | ||||
default: | default: | ||||
result = null; | result = null; | ||||
} | } | ||||
//find if it went well | //find if it went well | ||||
results.add(res.get()); | |||||
if(res.get()== Fetcher.Result.OK) { | if(res.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 result; | return result; | ||||
} | } | ||||
} | } | ||||
boolean emptyResults = true; | |||||
for (Fetcher.Result re: results){ | |||||
if (!re.equals(Fetcher.Result.EMPTY_RESULT_SET)) { | |||||
emptyResults = false; | |||||
break; | |||||
} | |||||
} | |||||
if(emptyResults){ | |||||
if(t==RequestType.STOPS) | |||||
publishProgress(Fetcher.Result.EMPTY_RESULT_SET); | |||||
} | |||||
//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 null; | return null; | ||||
} | } | ||||
@Override | @Override | ||||
protected void onProgressUpdate(Fetcher.Result... values) { | protected void onProgressUpdate(Fetcher.Result... values) { | ||||
Show All 34 Lines | protected void onPostExecute(Object o) { | ||||
} | } | ||||
List<?> list = (List<?>) o; | List<?> list = (List<?>) o; | ||||
if (list.size() ==0) return; | if (list.size() ==0) return; | ||||
Object firstItem = list.get(0); | Object firstItem = list.get(0); | ||||
if(!(firstItem instanceof Stop)) return; | if(!(firstItem instanceof Stop)) return; | ||||
ArrayList<Stop> stops = new ArrayList<>(); | ArrayList<Stop> stops = new ArrayList<>(); | ||||
for(Object x: list){ | for(Object x: list){ | ||||
if(x instanceof Stop) stops.add((Stop) x); | if(x instanceof Stop) stops.add((Stop) x); | ||||
//Log.d(DEBUG_TAG, "Parsing Stop: "+x); | |||||
} | } | ||||
if(list.size() != stops.size()){ | if(list.size() != stops.size()){ | ||||
Log.w(DEBUG_TAG, "Wrong stop list size:\n incoming: "+ | Log.w(DEBUG_TAG, "Wrong stop list size:\n incoming: "+ | ||||
list.size()+" out: "+stops.size()); | list.size()+" out: "+stops.size()); | ||||
} | } | ||||
//List<Stop> stopList = (List<Stop>) list; | //List<Stop> stopList = (List<Stop>) list; | ||||
if(query!=null && !isCancelled()) { | if(query!=null && !isCancelled()) { | ||||
fh.createStopListFragment(stops,query, replaceFragment); | fh.createStopListFragment(stops,query, replaceFragment); | ||||
▲ Show 20 Lines • Show All 118 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