Hypatia/app/src/main/java/us/spotco/malwarescanner/Utils.java

82 lines
2.8 KiB
Java
Raw Normal View History

2017-12-16 06:15:17 -05:00
package us.spotco.malwarescanner;
2017-12-17 06:36:16 -05:00
import android.app.ActivityManager;
import android.content.Context;
2017-12-27 16:14:33 -05:00
import android.content.Intent;
import android.content.SharedPreferences;
2017-12-17 06:36:16 -05:00
2017-12-16 06:15:17 -05:00
import java.io.File;
2017-12-26 21:04:23 -05:00
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
2017-12-17 20:02:26 -05:00
import gnu.trove.set.hash.THashSet;
2017-12-16 06:15:17 -05:00
2017-12-16 15:02:10 -05:00
class Utils {
2017-12-16 06:15:17 -05:00
public final static int MAX_SCAN_SIZE = (1000 * 1000) * 80; //80MB
public final static int MAX_SCAN_SIZE_REALTIME = MAX_SCAN_SIZE / 2; //40MB
2017-12-16 06:15:17 -05:00
public static int FILES_SCANNED = 0;
2017-12-26 21:04:23 -05:00
private static ThreadPoolExecutor threadPoolExecutor = null;
public static ThreadPoolExecutor getThreadPoolExecutor() {
if (threadPoolExecutor == null) {
threadPoolExecutor = (ThreadPoolExecutor) Executors.newScheduledThreadPool(getMaxThreads());
}
return threadPoolExecutor;
}
public static int getMaxThreads() {
int maxTheads = Runtime.getRuntime().availableProcessors();
if (maxTheads >= 2) {
maxTheads /= 2;
}
return maxTheads;
}
2017-12-17 20:02:26 -05:00
public static THashSet<File> getFilesRecursive(File root) {
THashSet<File> filesAll = new THashSet<>();
2017-12-16 06:15:17 -05:00
File[] files = root.listFiles();
if (files != null && files.length > 0) {
for (File f : files) {
if (f.isDirectory()) {
2017-12-17 20:02:26 -05:00
THashSet<File> filesTmp = getFilesRecursive(f);
2017-12-16 06:15:17 -05:00
if (filesTmp != null) {
filesAll.addAll(filesTmp);
}
} else {
2017-12-27 16:10:06 -05:00
if (f.length() <= MAX_SCAN_SIZE && f.canRead()) {//Exclude files larger than limit for performance
2017-12-16 06:15:17 -05:00
filesAll.add(f);
}
}
}
}
return filesAll;
}
2017-12-17 06:36:16 -05:00
//Credit: https://stackoverflow.com/a/5921190
public static boolean isServiceRunning(Class<?> serviceClass, Context context) {
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
2017-12-27 16:14:33 -05:00
public static void considerStartService(Context context) {
if (!Utils.isServiceRunning(MalwareScannerService.class, context)) {
SharedPreferences prefs = context.getSharedPreferences(BuildConfig.APPLICATION_ID, Context.MODE_PRIVATE);
boolean autostart = prefs.getBoolean("autostart", false);
if (autostart) {
Intent realtimeScanner = new Intent(context, MalwareScannerService.class);
context.startService(realtimeScanner);
}
}
}
2017-12-26 18:54:57 -05:00
}