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;
|
|
|
|
|
|
|
|
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 09:31:42 -05:00
|
|
|
malwareMonitors = new ArrayList<>();
|
|
|
|
malwareMonitors.add(new MalwareMonitor(Environment.getExternalStorageDirectory().toString()));
|
|
|
|
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 09:31:42 -05:00
|
|
|
for (MalwareMonitor malwareMonitor : malwareMonitors) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
@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();
|
|
|
|
}
|
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() {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|