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

105 lines
3.9 KiB
Java
Raw Normal View History

2017-12-16 07:11:02 -05:00
package us.spotco.malwarescanner;
import android.app.Notification;
import android.app.NotificationManager;
2017-12-16 07:11:02 -05:00
import android.app.Service;
import android.content.Context;
2017-12-16 07:11:02 -05:00
import android.content.Intent;
import android.os.Environment;
import android.os.FileObserver;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import java.io.File;
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 07:11:02 -05:00
public class MalwareScannerService extends Service {
2017-12-17 20:02:26 -05:00
private THashSet<RecursiveFileObserver> malwareMonitors = new THashSet<>();
private ThreadPoolExecutor threadPoolExecutor = null;
private NotificationCompat.Builder foregroundNotification = null;
private NotificationManager notificationManager = null;
2017-12-16 07:11:02 -05:00
@Override
2017-12-16 15:02:10 -05:00
public final IBinder onBind(Intent intent) {
2017-12-16 07:11:02 -05:00
return null;
}
@Override
2017-12-16 15:02:10 -05:00
public final int onStartCommand(Intent intent, int flags, int startId) {
malwareMonitors.clear();
addMalwareMonitor(Environment.getExternalStorageDirectory().toString());
threadPoolExecutor = (ThreadPoolExecutor) Executors.newScheduledThreadPool(Utils.getMaxThreads() + malwareMonitors.size());
threadPoolExecutor.execute(new Runnable() {
@Override
public void run() {
Database.loadDatabase(getApplicationContext(), true, Database.signatureDatabases);
}
});
for (final RecursiveFileObserver malwareMonitor : malwareMonitors) {
threadPoolExecutor.execute(new Runnable() {
@Override
public void run() {
malwareMonitor.startWatching();
}
});
2017-12-16 07:11:02 -05:00
}
notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
2017-12-16 09:31:42 -05:00
setForeground();
2017-12-16 07:11:02 -05:00
return START_STICKY;
}
private final void addMalwareMonitor(String monitorPath) {
malwareMonitors.add(new RecursiveFileObserver(monitorPath) {
@Override
public void onEvent(int event, String path) {
switch (event) {
case FileObserver.MOVED_TO:
case FileObserver.CLOSE_WRITE:
File file = new File(path);
2017-12-17 10:36:44 -05:00
if (file.exists() && /*file.length() > 0 &&*/ file.length() <= Utils.MAX_SCAN_SIZE_REALTIME) {
2017-12-17 20:02:26 -05:00
THashSet<File> filesToScan = new THashSet<>();
filesToScan.add(file);
new MalwareScanner(null, getApplicationContext(), false).executeOnExecutor(threadPoolExecutor, filesToScan);
}
updateForegroundNotification();
break;
}
}
});
}
2017-12-16 07:11:02 -05:00
@Override
2017-12-16 15:02:10 -05:00
public final void onDestroy() {
for (RecursiveFileObserver malwareMonitor : malwareMonitors) {
2017-12-16 07:11:02 -05:00
malwareMonitor.stopWatching();
}
malwareMonitors.clear();
System.gc();
2017-12-16 09:31:42 -05:00
//Toast.makeText(this, "Theia: Realtime Scanning Stopped", Toast.LENGTH_SHORT).show();
2017-12-16 07:11:02 -05:00
}
private void setForeground() {
foregroundNotification =
2017-12-16 07:11:02 -05:00
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(getText(R.string.lblNotificationRealtimeTitle))
.setContentText(getText(R.string.lblNotificationRealtimeText))
.setPriority(Notification.PRIORITY_MIN)
.setShowWhen(false);
startForeground(-1, foregroundNotification.build());
}
2017-12-16 07:11:02 -05:00
private void updateForegroundNotification() {
foregroundNotification.setSubText(Utils.FILES_SCANNED + " files scanned");
notificationManager.notify(-1, foregroundNotification.build());
2017-12-16 07:11:02 -05:00
}
2017-12-26 18:54:57 -05:00
}