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

81 lines
2.8 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.Service;
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.ArrayList;
import java.util.HashSet;
import java.util.Set;
public class MalwareScannerService extends Service {
private ArrayList<RecursiveFileObserver> malwareMonitors = new ArrayList<>();
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());
2017-12-16 07:11:02 -05:00
for (RecursiveFileObserver malwareMonitor : malwareMonitors) {
2017-12-16 09:31:42 -05:00
malwareMonitor.startWatching();
2017-12-16 07:11:02 -05:00
}
2017-12-16 09:31:42 -05:00
//Toast.makeText(this, "Theia: Realtime Scanning Started", Toast.LENGTH_SHORT).show();
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);
if (file.exists() && file.length() > 0 && file.length() <= Utils.MAX_SCAN_SIZE_REALTIME) {
Set<File> filesToScan = new HashSet<>();
filesToScan.add(file);
new MalwareScanner(null, getApplicationContext(), false).execute(filesToScan);
}
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() {
Notification notification =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(getText(R.string.lblNotificationRealtimeTitle))
.setContentText(getText(R.string.lblNotificationRealtimeText))
.setPriority(Notification.PRIORITY_MIN)
2017-12-16 08:53:15 -05:00
.setShowWhen(false)
2017-12-16 07:11:02 -05:00
.build();
startForeground(-1, notification);
}
}