Hypatia/app/src/main/java/us/spotco/malwarescanner/MalwareScannerService.java
Tavi 4e7cb16498
Going the distance... [pt3]
Signed-off-by: Tavi <tavi@divested.dev>
2024-07-27 18:34:19 -04:00

128 lines
5.3 KiB
Java

/*
Hypatia: A realtime malware scanner for Android
Copyright (c) 2017-2018 Divested Computing Group
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package us.spotco.malwarescanner;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Environment;
import android.os.FileObserver;
import android.os.IBinder;
import android.widget.Toast;
import java.io.File;
import java.text.NumberFormat;
import java.util.HashSet;
import java.util.concurrent.ThreadPoolExecutor;
public class MalwareScannerService extends Service {
private final HashSet<RecursiveFileObserver> malwareMonitors = new HashSet<>();
private ThreadPoolExecutor threadPoolExecutor = null;
private Notification.Builder foregroundNotification = null;
private NotificationManager notificationManager = null;
@Override
public final IBinder onBind(Intent intent) {
return null;
}
@Override
public final int onStartCommand(Intent intent, int flags, int startId) {
Utils.setContext(getApplicationContext());
malwareMonitors.clear();
addMalwareMonitor(Environment.getExternalStorageDirectory().toString());
int threadCount = Utils.getMaxThreads() + malwareMonitors.size();
threadPoolExecutor = Utils.getNewThreadPoolExecutor(threadCount);
threadPoolExecutor.execute(() -> Database.loadDatabase(getApplicationContext(), false, Database.signatureDatabases));
for (final RecursiveFileObserver malwareMonitor : malwareMonitors) {
threadPoolExecutor.execute(malwareMonitor::startWatching);
}
notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel foregroundChannel = new NotificationChannel("FOREGROUND", getString(R.string.lblNotificationRealtimeTitle), NotificationManager.IMPORTANCE_LOW);
foregroundChannel.setDescription(getString(R.string.lblNotificationRealtimeDescription));
foregroundChannel.setShowBadge(false);
notificationManager.createNotificationChannel(foregroundChannel);
}
setForeground();
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);
if (file.exists() && 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;
}
}
});
}
@Override
public final void onDestroy() {
for (RecursiveFileObserver malwareMonitor : malwareMonitors) {
malwareMonitor.stopWatching();
}
malwareMonitors.clear();
System.gc();
Toast.makeText(this, getString(R.string.lblNotificationRealtimeStopped), Toast.LENGTH_SHORT).show();
}
private void setForeground() {
foregroundNotification =
new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(getText(R.string.lblNotificationRealtimeTitle))
.setContentText(getText(R.string.lblNotificationRealtimeText))
.setPriority(Notification.PRIORITY_MIN)
.setOngoing(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
foregroundNotification.setShowWhen(false);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
foregroundNotification.setChannelId("FOREGROUND");
}
startForeground(-1, foregroundNotification.build());
}
private void updateForegroundNotification() {
foregroundNotification
.setSubText(NumberFormat.getInstance().format(Database.signaturesCount) + " sigs" + "" + getString(R.string.main_files_scanned_count, NumberFormat.getInstance().format(Utils.FILES_SCANNED)));
notificationManager.notify(-1, foregroundNotification.build());
}
}