Use multiple threads for the realtime scanner

This commit is contained in:
Tad 2017-12-26 17:56:39 -05:00
parent 147d99480c
commit 2f07219c02

View file

@ -11,12 +11,15 @@ import android.os.IBinder;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import java.io.File; import java.io.File;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import gnu.trove.set.hash.THashSet; import gnu.trove.set.hash.THashSet;
public class MalwareScannerService extends Service { public class MalwareScannerService extends Service {
private THashSet<RecursiveFileObserver> malwareMonitors = new THashSet<>(); private THashSet<RecursiveFileObserver> malwareMonitors = new THashSet<>();
private ThreadPoolExecutor threadPoolExecutor = null;
private NotificationCompat.Builder foregroundNotification = null; private NotificationCompat.Builder foregroundNotification = null;
private NotificationManager notificationManager = null; private NotificationManager notificationManager = null;
@ -27,13 +30,23 @@ public class MalwareScannerService extends Service {
@Override @Override
public final int onStartCommand(Intent intent, int flags, int startId) { public final int onStartCommand(Intent intent, int flags, int startId) {
int maxTheads = Runtime.getRuntime().availableProcessors();
if (maxTheads >= 2) {
maxTheads /= 2;
}
threadPoolExecutor = (ThreadPoolExecutor) Executors.newScheduledThreadPool(maxTheads);
threadPoolExecutor.execute(new Runnable() {
@Override
public void run() {
Database.loadDatabase(getApplicationContext(), true, Database.signatureDatabases);
}
});
malwareMonitors.clear(); malwareMonitors.clear();
addMalwareMonitor(Environment.getExternalStorageDirectory().toString()); addMalwareMonitor(Environment.getExternalStorageDirectory().toString());
for (RecursiveFileObserver malwareMonitor : malwareMonitors) { for (RecursiveFileObserver malwareMonitor : malwareMonitors) {
malwareMonitor.startWatching(); malwareMonitor.startWatching();
} }
//Toast.makeText(this, "Theia: Realtime Scanning Started", Toast.LENGTH_SHORT).show();
notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE); notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
setForeground(); setForeground();
@ -51,7 +64,7 @@ public class MalwareScannerService extends Service {
if (file.exists() && /*file.length() > 0 &&*/ file.length() <= Utils.MAX_SCAN_SIZE_REALTIME) { if (file.exists() && /*file.length() > 0 &&*/ file.length() <= Utils.MAX_SCAN_SIZE_REALTIME) {
THashSet<File> filesToScan = new THashSet<>(); THashSet<File> filesToScan = new THashSet<>();
filesToScan.add(file); filesToScan.add(file);
new MalwareScanner(null, getApplicationContext(), false).execute(filesToScan); new MalwareScanner(null, getApplicationContext(), false).executeOnExecutor(threadPoolExecutor, filesToScan);
} }
updateForegroundNotification(); updateForegroundNotification();
break; break;