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

98 lines
3.2 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;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.os.FileObserver;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
2017-12-16 08:53:15 -05:00
import android.util.Log;
2017-12-16 07:11:02 -05:00
import android.widget.Toast;
import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
public class MalwareScannerService extends Service {
private ArrayList<MalwareMonitor> malwareMonitors = null;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
2017-12-16 07:29:00 -05:00
if (true) {//Check if we're enabled
2017-12-16 07:11:02 -05:00
malwareMonitors = new ArrayList<>();
malwareMonitors.add(new MalwareMonitor(Environment.getExternalStorageDirectory().toString()));
2017-12-16 08:53:15 -05:00
malwareMonitors.add(new MalwareMonitor(Environment.getExternalStorageDirectory() + "/Documents"));
malwareMonitors.add(new MalwareMonitor(Environment.getExternalStorageDirectory() + "/Download"));
2017-12-16 07:11:02 -05:00
2017-12-16 07:29:00 -05:00
for (MalwareMonitor malwareMonitor : malwareMonitors) {
2017-12-16 07:11:02 -05:00
malwareMonitor.startWatching();
}
Toast.makeText(this, "Theia: Realtime Scanning Started", Toast.LENGTH_SHORT).show();
setForeground();
} else {
stopSelf();
}
return START_STICKY;
}
@Override
public void onDestroy() {
2017-12-16 07:29:00 -05:00
for (MalwareMonitor malwareMonitor : malwareMonitors) {
2017-12-16 07:11:02 -05:00
malwareMonitor.stopWatching();
}
Toast.makeText(this, "Theia: Realtime Scanning Stopped", Toast.LENGTH_SHORT).show();
}
private void setForeground() {
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
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);
}
private class MalwareMonitor extends FileObserver {
private String rootPath = null;
public MalwareMonitor(String path) {
super(path);
rootPath = path;
2017-12-16 07:29:00 -05:00
if (!rootPath.endsWith("/")) {
2017-12-16 07:11:02 -05:00
rootPath += "/";
}
}
2017-12-16 07:29:00 -05:00
2017-12-16 07:11:02 -05:00
@Override
public void onEvent(int eventID, String path) {
2017-12-16 07:29:00 -05:00
if (eventID == FileObserver.CLOSE_WRITE) {
2017-12-16 07:11:02 -05:00
File file = new File(rootPath + path);
2017-12-16 08:12:52 -05:00
if (file.exists() && file.length() > 0) {
Set<File> filesToScan = new HashSet<>();
filesToScan.add(file);
new MalwareScanner(null, getApplicationContext(), false).execute(filesToScan);
2017-12-16 07:11:02 -05:00
}
}
}
}
}