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

137 lines
5.4 KiB
Java
Raw Normal View History

2018-04-12 22:28:47 -04:00
/*
2020-11-17 10:31:08 -05:00
Hypatia: A realtime malware scanner for Android
2019-06-18 13:30:41 -04:00
Copyright (c) 2017-2018 Divested Computing Group
2018-04-12 22:28:47 -04:00
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2017-12-16 07:11:02 -05:00
package us.spotco.malwarescanner;
import android.app.Notification;
import android.app.NotificationChannel;
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.Build;
2017-12-16 07:11:02 -05:00
import android.os.Environment;
import android.os.FileObserver;
import android.os.IBinder;
import android.widget.Toast;
2019-09-22 21:17:21 -04:00
import androidx.core.app.NotificationCompat;
2017-12-16 07:11:02 -05:00
import java.io.File;
import java.util.HashSet;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
2017-12-17 20:02:26 -05:00
2017-12-16 07:11:02 -05:00
public class MalwareScannerService extends Service {
private final HashSet<RecursiveFileObserver> malwareMonitors = new HashSet<>();
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) {
Utils.setContext(getApplicationContext());
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);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
2018-04-05 21:46:24 -04:00
NotificationChannel foregroundChannel = new NotificationChannel("FOREGROUND", getString(R.string.lblNotificationRealtimeTitle), NotificationManager.IMPORTANCE_LOW);
foregroundChannel.setDescription(getString(R.string.lblNotificationRealtimeDescription));
foregroundChannel.setShowBadge(false);
notificationManager.createNotificationChannel(foregroundChannel);
}
2017-12-16 09:31:42 -05:00
setForeground();
2017-12-16 07:11:02 -05:00
return START_STICKY;
}
private 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) {
HashSet<File> filesToScan = new HashSet<>();
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();
Toast.makeText(this, "Hypatia: Realtime Scanning Stopped", Toast.LENGTH_SHORT).show(); //TODO: Move to strings.xml
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);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
foregroundNotification.setChannelId("FOREGROUND");
}
startForeground(-1, foregroundNotification.build());
}
2017-12-16 07:11:02 -05:00
private void updateForegroundNotification() {
foregroundNotification.setSubText(getString(R.string.main_files_scanned_count, Utils.FILES_SCANNED + ""));
notificationManager.notify(-1, foregroundNotification.build());
2017-12-16 07:11:02 -05:00
}
2018-07-25 21:08:35 -04:00
}