Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/backend/utils.java
| 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.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 java.io.PrintWriter; | import java.io.PrintWriter; | ||||
| import java.io.StringWriter; | import java.io.StringWriter; | ||||
| import java.util.Arrays; | import java.util.Arrays; | ||||
| import java.util.List; | import java.util.List; | ||||
| import java.util.Locale; | |||||
| import it.reyboz.bustorino.backend.mato.MatoAPIFetcher; | |||||
| 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); | ||||
| final double deltaPhi = Math.toRadians(lat2-lat1); | final double deltaPhi = Math.toRadians(lat2-lat1); | ||||
| ▲ Show 20 Lines • Show All 75 Lines • ▼ Show 20 Lines | public static String getBusStopIDFromUri(Uri uri) { | ||||
| break; | break; | ||||
| default: | default: | ||||
| Log.e("ActivityMain", "Unexpected intent URL: " + uri); | Log.e("ActivityMain", "Unexpected intent URL: " + uri); | ||||
| busStopID = null; | busStopID = null; | ||||
| } | } | ||||
| return busStopID; | return busStopID; | ||||
| } | } | ||||
| public static String toTitleCase(String givenString) { | public static String toTitleCase(String givenString, boolean lowercaseRest) { | ||||
| String[] arr = givenString.split(" "); | String[] arr = givenString.split(" "); | ||||
| StringBuffer sb = new StringBuffer(); | StringBuilder sb = new StringBuilder(); | ||||
| //Log.d("BusTO chars", "String parsing: "+givenString+" in array: "+ Arrays.toString(arr)); | //Log.d("BusTO chars", "String parsing: "+givenString+" in array: "+ Arrays.toString(arr)); | ||||
| for (int i = 0; i < arr.length; i++) { | for (int i = 0; i < arr.length; i++) { | ||||
| if (arr[i].length() > 1) | if (arr[i].length() > 1) { | ||||
| sb.append(Character.toUpperCase(arr[i].charAt(0))) | sb.append(Character.toUpperCase(arr[i].charAt(0))); | ||||
| .append(arr[i].substring(1)).append(" "); | if (lowercaseRest) | ||||
| sb.append(arr[i].substring(1).toLowerCase(Locale.ROOT)); | |||||
| else | |||||
| sb.append(arr[i].substring(1)); | |||||
| sb.append(" "); | |||||
| } | |||||
| else sb.append(arr[i]); | else sb.append(arr[i]); | ||||
| } | } | ||||
| return sb.toString().trim(); | return sb.toString().trim(); | ||||
| } | } | ||||
| /** | /** | ||||
| * Open an URL in the default browser. | * Open an URL in the default browser. | ||||
| * | * | ||||
| * @param url URL | * @param url URL | ||||
| */ | */ | ||||
| public static void openIceweasel(String url, Context context) { | public static void openIceweasel(String url, Context context) { | ||||
| Intent browserIntent1 = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); | Intent browserIntent1 = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); | ||||
| 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); | ||||
| } | } | ||||
| } | } | ||||
| public static ArrivalsFetcher[] getDefaultArrivalsFetchers(){ | |||||
| return new ArrivalsFetcher[]{ new MatoAPIFetcher(), | |||||
| new FiveTAPIFetcher(), new GTTJSONFetcher(), new FiveTScraperFetcher()}; | |||||
| } | |||||
| /** | /** | ||||
| * 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(); | ||||
| PrintWriter pw = new PrintWriter(sw); | PrintWriter pw = new PrintWriter(sw); | ||||
| Show All 29 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