package net.mvla.mvhs;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebView;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* Utilities
*/
public abstract class Utils {
public static int getNavDrawerWidth(Context context) {
int navDrawerMargin = context.getResources().getDimensionPixelSize(R.dimen.navigation_drawer_margin);
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
int navDrawerWidthLimit = context.getResources().getDimensionPixelSize(R.dimen.navigation_drawer_limit);
int navDrawerWidth = displayMetrics.widthPixels - navDrawerMargin;
if (navDrawerWidth > navDrawerWidthLimit) {
navDrawerWidth = navDrawerWidthLimit;
}
return navDrawerWidth;
}
public static void addOnGlobalLayoutListener(final View view, final Runnable r) {
ViewTreeObserver vto = view.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
new Handler(Looper.getMainLooper()).post(r);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
//noinspection deprecation
view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
}
});
}
public static boolean sameDay(Calendar cal, Calendar cal2) {
return cal.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) &&
cal.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR);
}
public static int convertDpToPx(Context context, float dp) {
return (int) (dp * context.getResources().getDisplayMetrics().density + 0.5f);
}
public static void executeJavascript(WebView view, String javascript) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
view.evaluateJavascript(javascript, null);
} else {
view.loadUrl("javascript:" + javascript);
}
}
public static void hideSoftKeyBoard(Activity context) {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
if (imm.isAcceptingText()) {
//If keyboard is open
imm.hideSoftInputFromWindow(context.getCurrentFocus().getWindowToken(), 0);
}
}
public static double bearing(double lat1, double lon1, double lat2, double lon2) {
double latitude1 = Math.toRadians(lat1);
double latitude2 = Math.toRadians(lat2);
double longDiff = Math.toRadians(lon2 - lon1);
double y = Math.sin(longDiff) * Math.cos(latitude2);
double x = Math.cos(latitude1) * Math.sin(latitude2) - Math.sin(latitude1) * Math.cos(latitude2) * Math.cos(longDiff);
return (Math.toDegrees(Math.atan2(y, x)) + 360) % 360;
}
public static void saveBytesToFile(byte[] bytes, String path) {
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(path);
fileOutputStream.write(bytes);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fileOutputStream != null) {
fileOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
@SuppressLint("SimpleDateFormat")
public static String formatTime(Date date) {
return new SimpleDateFormat("hh:mm").format(date);
}
}