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 java.io.PrintWriter; | import java.io.PrintWriter; | ||||
import java.io.StringWriter; | import java.io.StringWriter; | ||||
import java.util.Arrays; | |||||
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 All 11 Lines | public abstract class utils { | ||||
{ | { | ||||
return (int) (dips * con.getResources().getDisplayMetrics().density + 0.5f); | return (int) (dips * con.getResources().getDisplayMetrics().density + 0.5f); | ||||
} | } | ||||
*/ | */ | ||||
public static float convertDipToPixels(Context con, float dp){ | public static float convertDipToPixels(Context con, float dp){ | ||||
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,con.getResources().getDisplayMetrics()); | return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,con.getResources().getDisplayMetrics()); | ||||
} | } | ||||
/* | |||||
public static int calculateNumColumnsFromSize(View containerView, int pixelsize){ | public static int calculateNumColumnsFromSize(View containerView, int pixelsize){ | ||||
int width = containerView.getWidth(); | int width = containerView.getWidth(); | ||||
float ncols = ((float)width)/pixelsize; | float ncols = ((float)width)/pixelsize; | ||||
return (int) Math.floor(ncols); | return (int) Math.floor(ncols); | ||||
} | } | ||||
*/ | |||||
/** | /** | ||||
* Check if there is an internet connection | * Check if there is an internet connection | ||||
* @param con context object to get the system service | * @param con context object to get the system service | ||||
* @return true if we are | * @return true if we are | ||||
*/ | */ | ||||
public static boolean isConnected(Context con) { | public static boolean isConnected(Context con) { | ||||
ConnectivityManager connMgr = (ConnectivityManager) con.getSystemService(Context.CONNECTIVITY_SERVICE); | ConnectivityManager connMgr = (ConnectivityManager) con.getSystemService(Context.CONNECTIVITY_SERVICE); | ||||
Show All 38 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) { | |||||
String[] arr = givenString.split(" "); | |||||
StringBuffer sb = new StringBuffer(); | |||||
//Log.d("BusTO chars", "String parsing: "+givenString+" in array: "+ Arrays.toString(arr)); | |||||
for (int i = 0; i < arr.length; i++) { | |||||
if (arr[i].length() > 1) | |||||
sb.append(Character.toUpperCase(arr[i].charAt(0))) | |||||
.append(arr[i].substring(1)).append(" "); | |||||
else sb.append(arr[i]); | |||||
} | |||||
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)); | ||||
context.startActivity(browserIntent1); | context.startActivity(browserIntent1); | ||||
} | } | ||||
/** | /** | ||||
* 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); | ||||
StringBuilder sb = new StringBuilder(); | StringBuilder sb = new StringBuilder(); | ||||
ex.printStackTrace(pw); | ex.printStackTrace(pw); | ||||
String ss = sw.toString(); | String ss = sw.toString(); | ||||
String[] splitted = ss.split("\n"); | String[] splitted = ss.split("\n"); | ||||
sb.append("\n"); | sb.append("\n"); | ||||
if(splitted.length > 2 + i) { | if(splitted.length > 2 + i) { | ||||
for(int x = 2; x < i+2; x++) { | for(int x = 2; x < i+2; x++) { | ||||
sb.append(splitted[x].trim()); | sb.append(splitted[x].trim()); | ||||
sb.append("\n"); | sb.append("\n"); | ||||
} | } | ||||
return sb.toString(); | return sb.toString(); | ||||
} | } | ||||
return "Trace too Short."; | return "Trace too Short."; | ||||
} | } | ||||
*/ | |||||
} | } |
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