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 {
|
|
|
|
|
2017-12-16 16:58:43 -05:00
|
|
|
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) {
|
2017-12-16 16:58:43 -05:00
|
|
|
malwareMonitors.clear();
|
|
|
|
addMalwareMonitor(Environment.getExternalStorageDirectory().toString());
|
2017-12-16 07:11:02 -05:00
|
|
|
|
2017-12-16 16:58:43 -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;
|
|
|
|
}
|
|
|
|
|
2017-12-16 16:58:43 -05:00
|
|
|
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 07:32:14 -05:00
|
|
|
if (file.exists() && file.length() > 0 && file.length() <= Utils.MAX_SCAN_SIZE_REALTIME) {
|
2017-12-16 16:58:43 -05:00
|
|
|
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() {
|
2017-12-16 16:58:43 -05:00
|
|
|
for (RecursiveFileObserver malwareMonitor : malwareMonitors) {
|
2017-12-16 07:11:02 -05:00
|
|
|
malwareMonitor.stopWatching();
|
|
|
|
}
|
2017-12-16 16:58:43 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|