Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/backend/utils.java
Show All 13 Lines | BusTO (backend components) | ||||
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.backend; | package it.reyboz.bustorino.backend; | ||||
import android.content.Context; | import android.content.Context; | ||||
import android.content.Intent; | import android.content.Intent; | ||||
import android.content.SharedPreferences; | |||||
import android.net.ConnectivityManager; | import android.net.ConnectivityManager; | ||||
import android.net.NetworkInfo; | import android.net.NetworkInfo; | ||||
import android.net.Uri; | import android.net.Uri; | ||||
import android.util.Log; | import android.util.Log; | ||||
import android.util.TypedValue; | import android.util.TypedValue; | ||||
import android.view.View; | import android.view.View; | ||||
import androidx.annotation.Nullable; | import androidx.annotation.Nullable; | ||||
import androidx.preference.PreferenceManager; | |||||
import java.io.PrintWriter; | import java.io.PrintWriter; | ||||
import java.io.StringWriter; | import java.io.StringWriter; | ||||
import java.util.ArrayList; | |||||
import java.util.Arrays; | import java.util.Arrays; | ||||
import java.util.HashSet; | |||||
import java.util.List; | import java.util.List; | ||||
import java.util.Locale; | import java.util.Locale; | ||||
import java.util.Set; | |||||
import java.util.regex.Matcher; | import java.util.regex.Matcher; | ||||
import java.util.regex.Pattern; | import java.util.regex.Pattern; | ||||
import it.reyboz.bustorino.backend.mato.MatoAPIFetcher; | import it.reyboz.bustorino.backend.mato.MatoAPIFetcher; | ||||
import it.reyboz.bustorino.fragments.SettingsFragment; | |||||
public abstract class utils { | public abstract class utils { | ||||
private static final double EarthRadius = 6371e3; | private static final double EarthRadius = 6371e3; | ||||
public static Double measuredistanceBetween(double lat1,double long1,double lat2,double long2){ | public static Double measuredistanceBetween(double lat1,double long1,double lat2,double long2){ | ||||
final double phi1 = Math.toRadians(lat1); | final double phi1 = Math.toRadians(lat1); | ||||
final double phi2 = Math.toRadians(lat2); | final double phi2 = Math.toRadians(lat2); | ||||
▲ Show 20 Lines • Show All 161 Lines • ▼ Show 20 Lines | public static void openIceweasel(String url, Context context) { | ||||
if (browserIntent1.resolveActivity(context.getPackageManager()) != null) { | if (browserIntent1.resolveActivity(context.getPackageManager()) != null) { | ||||
//check we have an activity ready to receive intents (otherwise, there will be a crash) | //check we have an activity ready to receive intents (otherwise, there will be a crash) | ||||
context.startActivity(browserIntent1); | context.startActivity(browserIntent1); | ||||
} else{ | } else{ | ||||
Log.e("BusTO","openIceweasel can't find a browser"); | Log.e("BusTO","openIceweasel can't find a browser"); | ||||
} | } | ||||
} | } | ||||
/** | |||||
* Get the default list of fetchers for arrival times | |||||
* @return array of ArrivalsFetchers to use | |||||
*/ | |||||
public static ArrivalsFetcher[] getDefaultArrivalsFetchers(){ | public static ArrivalsFetcher[] getDefaultArrivalsFetchers(){ | ||||
return new ArrivalsFetcher[]{ new MatoAPIFetcher(), | 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<ArrivalsFetcher> getDefaultArrivalsFetchers(Context context){ | |||||
SharedPreferences defSharPref = PreferenceManager.getDefaultSharedPreferences(context); | |||||
final Set<String> setSelected = new HashSet<>(defSharPref.getStringSet(SettingsFragment.KEY_ARRIVALS_FETCHERS_USE, | |||||
new HashSet<>())); | |||||
if (setSelected.isEmpty()) { | |||||
return Arrays.asList(new MatoAPIFetcher(), | |||||
new GTTJSONFetcher(), new FiveTScraperFetcher()); | |||||
}else{ | |||||
ArrayList<ArrivalsFetcher> 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()); | |||||
setSelected.remove("matofetcher"); | |||||
} | |||||
if (setSelected.contains("fivetapifetcher")) { | |||||
outFetchers.add(new FiveTAPIFetcher()); | |||||
setSelected.remove("fivetapifetcher"); | |||||
} | |||||
if (setSelected.contains("gttjsonfetcher")){ | |||||
outFetchers.add(new GTTJSONFetcher()); | |||||
setSelected.remove("gttjsonfetcher"); | |||||
} | |||||
if (setSelected.contains("fivetscraper")) { | |||||
outFetchers.add(new FiveTScraperFetcher()); | |||||
setSelected.remove("fivetscraper"); | |||||
} | |||||
if(!setSelected.isEmpty()){ | |||||
Log.e("BusTO-Utils","Getting some fetchers values which are not contemplated: "+setSelected); | |||||
} | |||||
return outFetchers; | |||||
} | |||||
} | } | ||||
/** | /** | ||||
* Print the first i lines of the the trace of an exception | * Print the first i lines of the the trace of an exception | ||||
* https://stackoverflow.com/questions/21706722/fetch-only-first-n-lines-of-a-stack-trace | * https://stackoverflow.com/questions/21706722/fetch-only-first-n-lines-of-a-stack-trace | ||||
*/ | */ | ||||
/* | /* | ||||
public static String traceCaller(Exception ex, int i) { | public static String traceCaller(Exception ex, int i) { | ||||
StringWriter sw = new StringWriter(); | StringWriter sw = new StringWriter(); | ||||
Show All 21 Lines | public static String joinList(@Nullable List<String> dat, String separator){ | ||||
return dat.get(0); | return dat.get(0); | ||||
sb.append(dat.get(0)); | sb.append(dat.get(0)); | ||||
for (int i=1; i<dat.size(); i++){ | for (int i=1; i<dat.size(); i++){ | ||||
sb.append(separator); | sb.append(separator); | ||||
sb.append(dat.get(i)); | sb.append(dat.get(i)); | ||||
} | } | ||||
return sb.toString(); | return sb.toString(); | ||||
} | } | ||||
public static <T> Set<T> convertArrayToSet(T[] array) | |||||
{ | |||||
// Create an empty Set | |||||
Set<T> set = new HashSet<>(); | |||||
// Add each element into the set | |||||
set.addAll(Arrays.asList(array)); | |||||
// Return the converted Set | |||||
return set; | |||||
} | |||||
public static <T> String giveClassesForArray(T[] array){ | |||||
StringBuilder sb = new StringBuilder(); | |||||
for (T f: array){ | |||||
sb.append(""); | |||||
sb.append(f.getClass().getSimpleName()); | |||||
sb.append("; "); | |||||
} | |||||
return sb.toString(); | |||||
} | |||||
} | } |
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