Files
LeanKeyboard/leankeykeyboard/src/main/java/com/liskovsoft/utils/Helpers.java
2018-05-25 22:58:41 +03:00

145 lines
4.5 KiB
Java

package com.liskovsoft.utils;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.text.DateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Helpers {
/**
* Simple wildcard matching routine. Implemented without regex. So you may expect huge performance boost.
* @param host
* @param mask
* @return
*/
public static boolean matchSubstr(String host, String mask) {
String[] sections = mask.split("\\*");
String text = host;
for (String section : sections) {
int index = text.indexOf(section);
if (index == -1) {
return false;
}
text = text.substring(index + section.length());
}
return true;
}
public static boolean matchSubstrNoCase(String host, String mask) {
return matchSubstr(host.toLowerCase(), mask.toLowerCase());
}
public static InputStream getAsset(Context ctx, String fileName) {
InputStream is = null;
try {
is = ctx.getAssets().open(fileName);
} catch (IOException e) {
e.printStackTrace();
}
return is;
}
public static String encodeURI(byte[] data) {
try {
// make behaviour of java uri-encode the same as javascript's one
return URLEncoder.encode(new String(data, "UTF-8"), "UTF-8").replace("+", "%20");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
public static boolean floatEquals(float num1, float num2) {
float epsilon = 0.1f;
return Math.abs(num1 - num2) < epsilon;
}
public static String getDeviceName() {
return String.format("%s (%s)", Build.MODEL, Build.PRODUCT);
}
public static boolean deviceMatch(String[] devicesToProcess) {
String thisDeviceName = Helpers.getDeviceName();
for (String deviceName : devicesToProcess) {
boolean match = matchSubstrNoCase(thisDeviceName, deviceName);
if (match) {
return true;
}
}
return false;
}
public static String toString(InputStream inputStream) {
if (inputStream == null) {
return null;
}
Scanner s = new Scanner(inputStream).useDelimiter("\\A");
String result = s.hasNext() ? s.next() : "";
return result;
}
public static InputStream toStream(String content) {
return new ByteArrayInputStream(content.getBytes(Charset.forName("UTF8")));
}
public static void postOnUiThread(Runnable runnable) {
new Handler(Looper.getMainLooper()).post(runnable);
}
public static String unixToLocalDate(Context ctx, String timestamp) {
Locale current = ctx.getResources().getConfiguration().locale;
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG, current);
Date date;
if (timestamp == null) {
date = new Date();
} else {
date = new Date((long) Integer.parseInt(timestamp) * 1000);
}
return dateFormat.format(date);
}
public static String runMultiMatcher(String input, String... patterns) {
if (input == null) {
return null;
}
Pattern regex;
Matcher matcher;
String result = null;
for (String pattern : patterns) {
regex = Pattern.compile(pattern);
matcher = regex.matcher(input);
if (matcher.find()) {
result = matcher.group(matcher.groupCount()); // get last group
break;
}
}
return result;
}
public static boolean isCallable(Context ctx, Intent intent) {
List<ResolveInfo> list = ctx.getPackageManager().queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
}
}