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;
|
2018-04-05 21:08:52 -04:00
|
|
|
import android.os.Build;
|
2017-12-17 06:36:16 -05:00
|
|
|
|
2017-12-16 06:15:17 -05:00
|
|
|
import java.io.File;
|
2018-01-20 01:50:37 -05:00
|
|
|
import java.util.HashSet;
|
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
|
|
|
|
2017-12-16 15:02:10 -05:00
|
|
|
class Utils {
|
2017-12-16 06:15:17 -05:00
|
|
|
|
2017-12-17 07:32:14 -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
|
|
|
|
2017-12-27 17:15:38 -05:00
|
|
|
public final static int MAX_HASH_LENGTH = 16;
|
2017-12-26 17:28:44 -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-26 17:28:44 -05:00
|
|
|
|
2018-01-20 01:50:37 -05:00
|
|
|
public static HashSet<File> getFilesRecursive(File root) {
|
|
|
|
HashSet<File> filesAll = new HashSet<>();
|
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()) {
|
2018-01-20 01:50:37 -05:00
|
|
|
HashSet<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);
|
2018-04-05 21:08:52 -04:00
|
|
|
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
|
context.startForegroundService(realtimeScanner);
|
|
|
|
} else {
|
|
|
|
context.startService(realtimeScanner);
|
|
|
|
}
|
2017-12-27 16:14:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-26 18:54:57 -05:00
|
|
|
}
|