commit e2b1814d57a54a638ad94d61050b62d625dc9554 Author: Fabio Mazza Date: Mon Oct 10 17:35:14 2022 +0200 Enable choice of fetchers for arrival times. Fix rotation of fetchers. Set default values on start (if the values are empty). diff --git a/res/values-it/strings.xml b/res/values-it/strings.xml index 8205fda..d82904f 100644 --- a/res/values-it/strings.xml +++ b/res/values-it/strings.xml @@ -150,6 +150,9 @@ App Muoversi a Torino Sconosciuta + Fonti orari di arrivo + Scegli le fonti di orari da usare + Cambiamento sorgente orari… Premi a lungo per cambiare la sorgente degli orari diff --git a/res/values/keys.xml b/res/values/keys.xml index 93ad292..eb33af9 100644 --- a/res/values/keys.xml +++ b/res/values/keys.xml @@ -10,4 +10,17 @@ map lines + + + matofetcher + fivetapifetcher + gttjsonfetcher + fivetscraper + + + + matofetcher + gttjsonfetcher + fivetscraper + \ No newline at end of file diff --git a/res/values/strings.xml b/res/values/strings.xml index a0f3e37..822d4e5 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -164,6 +164,15 @@ Undetermined Changing arrival times source… Long press to change the source of arrivals + + @string/source_mato + @string/fivetapifetcher + @string/gttjsonfetcher + @string/fivetscraper + + Sources of arrival times + Select which sources of arrival times to use + + + setSelected = mainSharedPref.getStringSet(SettingsFragment.KEY_ARRIVALS_FETCHERS_USE, new HashSet<>()); + if (setSelected.isEmpty()){ + String[] defaultVals = getResources().getStringArray(R.array.arrivals_sources_values_default); + editor.putStringSet(SettingsFragment.KEY_ARRIVALS_FETCHERS_USE, utils.convertArrayToSet(defaultVals)); + edit=true; + } + if (edit){ + editor.commit(); + } + + + } } diff --git a/src/it/reyboz/bustorino/backend/utils.java b/src/it/reyboz/bustorino/backend/utils.java index 02d13c4..5996b14 100644 --- a/src/it/reyboz/bustorino/backend/utils.java +++ b/src/it/reyboz/bustorino/backend/utils.java @@ -19,6 +19,7 @@ package it.reyboz.bustorino.backend; import android.content.Context; import android.content.Intent; +import android.content.SharedPreferences; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.Uri; @@ -26,16 +27,21 @@ import android.util.Log; import android.util.TypedValue; import android.view.View; import androidx.annotation.Nullable; +import androidx.preference.PreferenceManager; import java.io.PrintWriter; import java.io.StringWriter; +import java.util.ArrayList; import java.util.Arrays; +import java.util.HashSet; import java.util.List; import java.util.Locale; +import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; import it.reyboz.bustorino.backend.mato.MatoAPIFetcher; +import it.reyboz.bustorino.fragments.SettingsFragment; public abstract class utils { private static final double EarthRadius = 6371e3; @@ -213,9 +219,56 @@ public abstract class utils { } } + /** + * Get the default list of fetchers for arrival times + * @return array of ArrivalsFetchers to use + */ public static ArrivalsFetcher[] getDefaultArrivalsFetchers(){ return new ArrivalsFetcher[]{ new MatoAPIFetcher(), - new FiveTAPIFetcher(), new GTTJSONFetcher(), new FiveTScraperFetcher()}; + new GTTJSONFetcher(), new FiveTScraperFetcher()}; + } + /** + * Get the default list of fetchers for arrival times + * @return array of ArrivalsFetchers to use + */ + public static List getDefaultArrivalsFetchers(Context context){ + SharedPreferences defSharPref = PreferenceManager.getDefaultSharedPreferences(context); + final Set setSelected = defSharPref.getStringSet(SettingsFragment.KEY_ARRIVALS_FETCHERS_USE, new HashSet<>()); + if (setSelected.isEmpty()) { + return Arrays.asList(new MatoAPIFetcher(), + new GTTJSONFetcher(), new FiveTScraperFetcher()); + }else{ + ArrayList outFetchers = new ArrayList<>(4); + for(String s: setSelected){ + switch (s){ + case "matofetcher": + outFetchers.add(new MatoAPIFetcher()); + break; + case "fivetapifetcher": + outFetchers.add(new FiveTAPIFetcher()); + break; + case "gttjsonfetcher": + outFetchers.add(new GTTJSONFetcher()); + break; + case "fivetscraper": + outFetchers.add(new FiveTScraperFetcher()); + break; + default: + throw new IllegalArgumentException(); + } + } + /* + if (setSelected.contains("matofetcher")) + outFetchers.add(new MatoAPIFetcher()); + if (setSelected.contains("fivetapifetcher")) + outFetchers.add(new FiveTAPIFetcher()); + if (setSelected.contains("gttjsonfetcher")) + outFetchers.add(new GTTJSONFetcher()); + if (setSelected.contains("fivetscraper")) + outFetchers.add(new FiveTScraperFetcher()); + */ + return outFetchers; + } } /** * Print the first i lines of the the trace of an exception @@ -253,4 +306,15 @@ public abstract class utils { } return sb.toString(); } + + public static Set convertArrayToSet(T[] array) + { + // Create an empty Set + Set set = new HashSet<>(); + // Add each element into the set + set.addAll(Arrays.asList(array)); + + // Return the converted Set + return set; + } } diff --git a/src/it/reyboz/bustorino/fragments/ArrivalsFragment.java b/src/it/reyboz/bustorino/fragments/ArrivalsFragment.java index ea9ad22..47a203f 100644 --- a/src/it/reyboz/bustorino/fragments/ArrivalsFragment.java +++ b/src/it/reyboz/bustorino/fragments/ArrivalsFragment.java @@ -18,6 +18,7 @@ package it.reyboz.bustorino.fragments; +import android.content.Context; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; @@ -85,7 +86,7 @@ public class ArrivalsFragment extends ResultListFragment implements LoaderManage protected ImageButton addToFavorites; protected TextView timesSourceTextView; - private List fetchers = new ArrayList<>(Arrays.asList(utils.getDefaultArrivalsFetchers())); + private List fetchers = null; //new ArrayList<>(Arrays.asList(utils.getDefaultArrivalsFetchers())); private boolean reloadOnResume = true; @@ -247,6 +248,14 @@ public class ArrivalsFragment extends ResultListFragment implements LoaderManage } + @Override + public void onAttach(@NonNull Context context) { + super.onAttach(context); + + //get fetchers + fetchers = utils.getDefaultArrivalsFetchers(context); + } + @Nullable public String getStopID() { return stopID; @@ -274,7 +283,7 @@ public class ArrivalsFragment extends ResultListFragment implements LoaderManage } private void rotateFetchers(){ - Collections.rotate(fetchers, -1); + Collections.rotate(fetchers, 1); } @@ -332,7 +341,7 @@ public class ArrivalsFragment extends ResultListFragment implements LoaderManage } int count = 0; if (source!= Passaggio.Source.UNDETERMINED) - while (source != fetchers.get(0).getSourceForFetcher() && count < 100){ + while (source != fetchers.get(0).getSourceForFetcher() && count < 10){ //we need to update the fetcher that is requested rotateFetchers(); count++; @@ -431,9 +440,13 @@ public class ArrivalsFragment extends ResultListFragment implements LoaderManage case loaderStopId: if(data.getCount()>0){ data.moveToFirst(); - stopName = data.getString(data.getColumnIndex( + int index = data.getColumnIndex( NextGenDB.Contract.StopsTable.COL_NAME - )); + ); + if (index == -1){ + Log.e(DEBUG_TAG, "Index is -1, column not present. App may explode now..."); + } + stopName = data.getString(index); updateMessage(); } else { Log.w("ArrivalsFragment"+getTag(),"Stop is not inside the database... CLOISTER BELL"); diff --git a/src/it/reyboz/bustorino/fragments/MainScreenFragment.java b/src/it/reyboz/bustorino/fragments/MainScreenFragment.java index a62eff8..8897cc3 100644 --- a/src/it/reyboz/bustorino/fragments/MainScreenFragment.java +++ b/src/it/reyboz/bustorino/fragments/MainScreenFragment.java @@ -41,6 +41,7 @@ import android.widget.Toast; import com.google.android.material.floatingactionbutton.FloatingActionButton; +import java.util.List; import java.util.Map; import it.reyboz.bustorino.R; @@ -107,6 +108,10 @@ public class MainScreenFragment extends ScreenBaseFragment implements FragmentL private final Runnable refreshStop = new Runnable() { public void run() { if(getContext() == null) return; + List fetcherList = utils.getDefaultArrivalsFetchers(getContext()); + ArrivalsFetcher[] arrivalsFetchers = new ArrivalsFetcher[fetcherList.size()]; + arrivalsFetchers = fetcherList.toArray(arrivalsFetchers); + if (fragMan.findFragmentById(R.id.resultFrame) instanceof ArrivalsFragment) { ArrivalsFragment fragment = (ArrivalsFragment) fragMan.findFragmentById(R.id.resultFrame); if (fragment == null){ diff --git a/src/it/reyboz/bustorino/fragments/SettingsFragment.java b/src/it/reyboz/bustorino/fragments/SettingsFragment.java index b7937e2..a2a8c01 100644 --- a/src/it/reyboz/bustorino/fragments/SettingsFragment.java +++ b/src/it/reyboz/bustorino/fragments/SettingsFragment.java @@ -34,6 +34,7 @@ import it.reyboz.bustorino.R; import it.reyboz.bustorino.data.DatabaseUpdate; import java.lang.ref.WeakReference; +import java.util.HashSet; public class SettingsFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener { private static final String TAG = SettingsFragment.class.getName(); @@ -43,6 +44,8 @@ public class SettingsFragment extends PreferenceFragmentCompat implements Shared //private static final Handler mHandler; public final static String PREF_KEY_STARTUP_SCREEN="startup_screen_to_show"; + public final static String KEY_ARRIVALS_FETCHERS_USE = "arrivals_fetchers_use_setting"; + private boolean setSummaryStartupPref = false; @Override @@ -102,6 +105,10 @@ public class SettingsFragment extends PreferenceFragmentCompat implements Shared public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { Preference pref = findPreference(key); Log.d(TAG,"Preference key "+key+" changed"); + if (key.equals(SettingsFragment.KEY_ARRIVALS_FETCHERS_USE)){ + Log.d(TAG, "New value is: "+sharedPreferences.getStringSet(key, new HashSet<>())); + } + //sometimes this happens if(getContext()==null) return; if(key.equals(PREF_KEY_STARTUP_SCREEN) && setSummaryStartupPref && pref !=null){