2019-06-03 23:25:05 +02:00
|
|
|
package org.mian.gitnex.util;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.pm.PackageInfo;
|
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
import android.content.res.Configuration;
|
|
|
|
import android.content.res.Resources;
|
|
|
|
import android.net.ConnectivityManager;
|
|
|
|
import android.net.NetworkInfo;
|
2019-09-18 10:25:49 +02:00
|
|
|
import android.util.Base64;
|
2019-06-03 23:25:05 +02:00
|
|
|
import android.util.DisplayMetrics;
|
2020-06-28 17:11:59 +02:00
|
|
|
import android.view.View;
|
2019-06-03 23:25:05 +02:00
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.net.URL;
|
2019-09-18 10:25:49 +02:00
|
|
|
import java.nio.charset.StandardCharsets;
|
2019-06-03 23:25:05 +02:00
|
|
|
import java.text.DecimalFormat;
|
2019-11-05 09:34:22 +01:00
|
|
|
import java.util.Arrays;
|
2019-06-03 23:25:05 +02:00
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.Locale;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Author M M Arif
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class AppUtil {
|
|
|
|
|
2020-06-11 15:31:11 +02:00
|
|
|
public static String strReplace(String str, String original, String replace) {
|
2020-03-16 21:01:49 +01:00
|
|
|
|
2020-06-11 15:31:11 +02:00
|
|
|
return str.replace(original, replace);
|
|
|
|
}
|
2020-03-16 21:01:49 +01:00
|
|
|
|
2020-06-28 17:11:59 +02:00
|
|
|
public static boolean hasNetworkConnection(Context context) {
|
2020-03-16 21:01:49 +01:00
|
|
|
|
2020-06-11 15:31:11 +02:00
|
|
|
boolean haveConnectedWifi = false;
|
|
|
|
boolean haveConnectedMobile = false;
|
2020-03-16 21:01:49 +01:00
|
|
|
|
2020-06-11 15:31:11 +02:00
|
|
|
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
|
assert cm != null;
|
|
|
|
NetworkInfo[] netInfo = cm.getAllNetworkInfo();
|
|
|
|
for(NetworkInfo ni : netInfo) {
|
|
|
|
if(ni.getTypeName().equalsIgnoreCase("WIFI")) {
|
|
|
|
if(ni.isConnected()) {
|
|
|
|
haveConnectedWifi = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(ni.getTypeName().equalsIgnoreCase("MOBILE")) {
|
|
|
|
if(ni.isConnected()) {
|
|
|
|
haveConnectedMobile = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return haveConnectedWifi || haveConnectedMobile;
|
|
|
|
}
|
2020-03-16 21:01:49 +01:00
|
|
|
|
2020-06-11 15:31:11 +02:00
|
|
|
public static int getAppBuildNo(Context context) {
|
2020-03-16 21:01:49 +01:00
|
|
|
|
2020-06-11 15:31:11 +02:00
|
|
|
try {
|
|
|
|
PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
|
|
|
|
return packageInfo.versionCode;
|
|
|
|
}
|
|
|
|
catch(PackageManager.NameNotFoundException e) {
|
|
|
|
throw new RuntimeException("Could not get package name: " + e);
|
|
|
|
}
|
|
|
|
}
|
2020-03-16 21:01:49 +01:00
|
|
|
|
2020-06-11 15:31:11 +02:00
|
|
|
public static String getAppVersion(Context context) {
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2020-06-11 15:31:11 +02:00
|
|
|
try {
|
|
|
|
PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
|
|
|
|
return packageInfo.versionName;
|
|
|
|
}
|
|
|
|
catch(PackageManager.NameNotFoundException e) {
|
|
|
|
throw new RuntimeException("Could not get package name: " + e);
|
|
|
|
}
|
|
|
|
}
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2020-06-11 15:31:11 +02:00
|
|
|
public int charactersLength(String str) {
|
|
|
|
|
|
|
|
return str.length();
|
|
|
|
}
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2020-06-11 15:31:11 +02:00
|
|
|
public Boolean checkStringsWithAlphaNumeric(String str) { // [a-zA-Z0-9]
|
|
|
|
return str.matches("^[\\w]+$");
|
|
|
|
}
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2020-06-11 15:31:11 +02:00
|
|
|
public Boolean checkStrings(String str) { // [a-zA-Z0-9-_. ]
|
|
|
|
return str.matches("^[\\w .-]+$");
|
|
|
|
}
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2020-06-11 15:31:11 +02:00
|
|
|
public Boolean checkStringsWithAlphaNumericDashDotUnderscore(String str) { // [a-zA-Z0-9-_]
|
|
|
|
return str.matches("^[\\w.-]+$");
|
|
|
|
}
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2020-06-11 15:31:11 +02:00
|
|
|
public Boolean checkStringsWithDash(String str) { // [a-zA-Z0-9-_. ]
|
|
|
|
return str.matches("^[\\w-]+$");
|
|
|
|
}
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2020-06-11 15:31:11 +02:00
|
|
|
public Boolean checkIntegers(String str) {
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2020-06-11 15:31:11 +02:00
|
|
|
return str.matches("\\d+");
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getResponseStatusCode(String u) throws Exception {
|
|
|
|
|
|
|
|
URL url = new URL(u);
|
|
|
|
HttpURLConnection http = (HttpURLConnection) url.openConnection();
|
|
|
|
return (http.getResponseCode());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void setAppLocale(Resources resource, String locCode) {
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2020-06-11 15:31:11 +02:00
|
|
|
DisplayMetrics dm = resource.getDisplayMetrics();
|
|
|
|
Configuration config = resource.getConfiguration();
|
|
|
|
config.setLocale(new Locale(locCode.toLowerCase()));
|
|
|
|
resource.updateConfiguration(config, dm);
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2020-06-11 15:31:11 +02:00
|
|
|
}
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2020-06-11 15:31:11 +02:00
|
|
|
public static String formatFileSize(long size) {
|
|
|
|
|
2020-06-28 17:11:59 +02:00
|
|
|
String repoSize = size + " B";
|
2020-06-11 15:31:11 +02:00
|
|
|
|
|
|
|
double m = size / 1024.0;
|
|
|
|
double g = ((size / 1024.0) / 1024.0);
|
|
|
|
double t = (((size / 1024.0) / 1024.0) / 1024.0);
|
|
|
|
|
|
|
|
DecimalFormat dec = new DecimalFormat("0.00");
|
|
|
|
|
|
|
|
if(t > 1) {
|
|
|
|
repoSize = dec.format(t).concat(" TB");
|
|
|
|
}
|
|
|
|
else if(g > 1) {
|
|
|
|
repoSize = dec.format(g).concat(" GB");
|
|
|
|
}
|
|
|
|
else if(m > 1) {
|
|
|
|
repoSize = dec.format(m).concat(" MB");
|
|
|
|
}
|
|
|
|
else if((double) size > 1) {
|
|
|
|
repoSize = dec.format((double) size).concat(" KB");
|
|
|
|
}
|
2019-09-18 10:25:49 +02:00
|
|
|
|
2020-06-11 15:31:11 +02:00
|
|
|
return repoSize;
|
2019-09-18 10:25:49 +02:00
|
|
|
|
2020-06-11 15:31:11 +02:00
|
|
|
}
|
2019-09-18 10:25:49 +02:00
|
|
|
|
2020-06-11 15:31:11 +02:00
|
|
|
public static String formatFileSizeInDetail(long size) {
|
2019-09-18 10:25:49 +02:00
|
|
|
|
2020-06-11 15:31:11 +02:00
|
|
|
String fileSize = null;
|
2019-09-18 10:25:49 +02:00
|
|
|
|
2020-06-11 15:31:11 +02:00
|
|
|
double k = size / 1024.0;
|
|
|
|
double m = ((size / 1024.0) / 1024.0);
|
|
|
|
double g = (((size / 1024.0) / 1024.0) / 1024.0);
|
|
|
|
double t = ((((size / 1024.0) / 1024.0) / 1024.0) / 1024.0);
|
2019-09-18 10:25:49 +02:00
|
|
|
|
2020-06-11 15:31:11 +02:00
|
|
|
DecimalFormat dec = new DecimalFormat("0.00");
|
2019-09-18 10:25:49 +02:00
|
|
|
|
2020-06-11 15:31:11 +02:00
|
|
|
if(t > 1) {
|
|
|
|
fileSize = dec.format(t).concat(" TB");
|
|
|
|
}
|
|
|
|
else if(g > 1) {
|
|
|
|
fileSize = dec.format(g).concat(" GB");
|
|
|
|
}
|
|
|
|
else if(m > 1) {
|
|
|
|
fileSize = dec.format(m).concat(" MB");
|
|
|
|
}
|
|
|
|
else if(k > 1) {
|
|
|
|
fileSize = dec.format(k).concat(" KB");
|
|
|
|
}
|
|
|
|
else if((double) size > 1) {
|
|
|
|
fileSize = dec.format((double) size).concat(" B");
|
|
|
|
}
|
2019-09-18 10:25:49 +02:00
|
|
|
|
2020-06-11 15:31:11 +02:00
|
|
|
return fileSize;
|
2019-11-05 09:34:22 +01:00
|
|
|
|
2020-06-11 15:31:11 +02:00
|
|
|
}
|
2019-11-05 09:34:22 +01:00
|
|
|
|
2020-06-11 15:31:11 +02:00
|
|
|
public static String customDateFormat(String customDate) {
|
2019-11-05 09:34:22 +01:00
|
|
|
|
2020-06-11 15:31:11 +02:00
|
|
|
String[] parts = customDate.split("-");
|
|
|
|
final String year = parts[0];
|
|
|
|
final String month = parts[1];
|
|
|
|
final String day = parts[2];
|
2019-11-05 09:34:22 +01:00
|
|
|
|
2020-06-11 15:31:11 +02:00
|
|
|
String sMonth;
|
|
|
|
if(Integer.parseInt(month) < 10) {
|
|
|
|
sMonth = "0" + month;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sMonth = month;
|
|
|
|
}
|
2020-01-30 19:00:31 +01:00
|
|
|
|
2020-06-11 15:31:11 +02:00
|
|
|
String sDay;
|
|
|
|
if(Integer.parseInt(day) < 10) {
|
|
|
|
sDay = "0" + day;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sDay = day;
|
|
|
|
}
|
2020-01-30 19:00:31 +01:00
|
|
|
|
2020-06-11 15:31:11 +02:00
|
|
|
return year + "-" + sMonth + "-" + sDay;
|
2020-01-30 19:00:31 +01:00
|
|
|
|
2020-06-11 15:31:11 +02:00
|
|
|
}
|
2020-01-30 19:00:31 +01:00
|
|
|
|
2020-06-11 15:31:11 +02:00
|
|
|
public static String customDateCombine(String customDate) {
|
2019-11-05 09:34:22 +01:00
|
|
|
|
2020-06-11 15:31:11 +02:00
|
|
|
final Calendar c = Calendar.getInstance();
|
|
|
|
int mHour = c.get(Calendar.HOUR_OF_DAY);
|
|
|
|
int mMinute = c.get(Calendar.MINUTE);
|
|
|
|
int mSeconds = c.get(Calendar.SECOND);
|
2019-11-05 09:34:22 +01:00
|
|
|
|
2020-06-11 15:31:11 +02:00
|
|
|
String sMin;
|
|
|
|
if((mMinute) < 10) {
|
|
|
|
sMin = "0" + mMinute;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sMin = String.valueOf(mMinute);
|
|
|
|
}
|
2019-11-05 09:34:22 +01:00
|
|
|
|
2020-06-11 15:31:11 +02:00
|
|
|
String sSec;
|
|
|
|
if((mSeconds) < 10) {
|
|
|
|
sSec = "0" + mSeconds;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sSec = String.valueOf(mSeconds);
|
|
|
|
}
|
2019-11-15 20:39:09 +01:00
|
|
|
|
2020-06-11 15:31:11 +02:00
|
|
|
return (customDate + "T" + mHour + ":" + sMin + ":" + sSec + "Z");
|
2020-03-09 17:13:40 +01:00
|
|
|
|
2020-06-11 15:31:11 +02:00
|
|
|
}
|
2020-03-09 17:13:40 +01:00
|
|
|
|
2020-06-11 15:31:11 +02:00
|
|
|
public String encodeBase64(String str) {
|
2020-03-09 17:13:40 +01:00
|
|
|
|
2020-06-11 15:31:11 +02:00
|
|
|
String base64Str = str;
|
|
|
|
if(!str.equals("")) {
|
|
|
|
byte[] data = str.getBytes(StandardCharsets.UTF_8);
|
|
|
|
base64Str = Base64.encodeToString(data, Base64.DEFAULT);
|
|
|
|
}
|
2020-03-09 17:13:40 +01:00
|
|
|
|
2020-06-11 15:31:11 +02:00
|
|
|
return base64Str;
|
2019-11-15 20:39:09 +01:00
|
|
|
|
2020-06-11 15:31:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public String decodeBase64(String str) {
|
|
|
|
|
|
|
|
String base64Str = str;
|
|
|
|
if(!str.equals("")) {
|
|
|
|
byte[] data = Base64.decode(base64Str, Base64.DEFAULT);
|
|
|
|
base64Str = new String(data, StandardCharsets.UTF_8);
|
|
|
|
}
|
|
|
|
|
|
|
|
return base64Str;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public Boolean sourceCodeExtension(String ext) {
|
|
|
|
|
2020-06-28 16:45:39 +02:00
|
|
|
String[] extValues = new String[]{"md", "json", "java", "go", "php", "c", "cc", "cpp", "h", "cxx", "cyc", "m", "cs", "bash", "sh", "bsh", "cv", "python", "perl", "pm", "rb", "ruby", "javascript", "coffee", "rc", "rs", "rust", "basic", "clj", "css", "dart", "lisp", "erl", "hs", "lsp", "rkt", "ss", "llvm", "ll", "lua", "matlab", "pascal", "r", "scala", "sql", "latex", "tex", "vb", "vbs", "vhd", "tcl", "wiki.meta", "yaml", "yml", "markdown", "xml", "proto", "regex", "py", "pl", "js", "html", "htm", "volt", "ini", "htaccess", "conf", "gitignore", "gradle", "txt", "properties", "bat", "twig", "cvs", "cmake", "in", "info", "spec", "m4", "am", "dist", "pam"};
|
2020-06-11 15:31:11 +02:00
|
|
|
|
|
|
|
return Arrays.asList(extValues).contains(ext);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public Boolean pdfExtension(String ext) {
|
|
|
|
|
|
|
|
String[] extValues = new String[]{"pdf"};
|
|
|
|
|
|
|
|
return Arrays.asList(extValues).contains(ext);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public Boolean imageExtension(String ext) {
|
|
|
|
|
|
|
|
String[] extValues = new String[]{"jpg", "jpeg", "gif", "png", "ico"};
|
|
|
|
|
|
|
|
return Arrays.asList(extValues).contains(ext);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public Boolean excludeFilesInFileViewerExtension(String ext) {
|
|
|
|
|
|
|
|
String[] extValues = new String[]{"doc", "docx", "ppt", "pptx", "xls", "xlsx", "xlsm", "odt", "ott", "odf", "ods", "ots", "exe", "jar", "odg", "otg", "odp", "otp", "bin", "dmg", "psd", "xcf"};
|
|
|
|
|
|
|
|
return Arrays.asList(extValues).contains(ext);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getLastCharactersOfWord(String str, int count) {
|
|
|
|
|
|
|
|
return str.substring(str.length() - count);
|
|
|
|
|
|
|
|
}
|
2019-11-15 20:39:09 +01:00
|
|
|
|
2020-06-28 17:11:59 +02:00
|
|
|
public static void setMultiVisibility(int visibility, View... views) {
|
|
|
|
|
|
|
|
for(View view : views) {
|
|
|
|
|
|
|
|
view.setVisibility(visibility);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-03 23:25:05 +02:00
|
|
|
}
|