Many changes

- Add back MalwareScannerService
- Performance fix
- More strings
- Comment some unimplemented features
This commit is contained in:
Tad 2018-10-21 23:14:25 -04:00
parent c40ff53cb0
commit b9c646d64d
5 changed files with 141 additions and 9 deletions

View file

@ -28,6 +28,12 @@
</intent-filter>
</activity>
<service
android:name=".MalwareScannerService"
android:label="Realtime Malware Scanner"
android:enabled="true"
android:exported="false"/>
<receiver
android:name=".EventReceiver"
android:label="Event Handler"

View file

@ -17,5 +17,125 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package us.spotco.malwarescanner;
public class MalwareScannerService {
}
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.support.v4.app.NotificationCompat;
import java.io.File;
import java.util.HashSet;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
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;
private int maxScanSize = 0;
@Override
public final IBinder onBind(Intent intent) {
return null;
}
@Override
public final int onStartCommand(Intent intent, int flags, int startId) {
maxScanSize = Utils.prefs.getInt("scanner_max_file_size", (1000 * 1000) * 80) * (1000 * 1000);
malwareMonitors.clear();
if(Utils.prefs.getBoolean("scanner_realtime_path_external", false)) {
addMalwareMonitor("/storage");
} else if(Utils.prefs.getBoolean("scanner_realtime_path_internal", true)) {
addMalwareMonitor(Environment.getExternalStorageDirectory().toString());
}
threadPoolExecutor = (ThreadPoolExecutor) Executors.newScheduledThreadPool(Utils.getMaxThreads() + malwareMonitors.size());
threadPoolExecutor.execute(new Runnable() {
@Override
public void run() {
DatabaseManager.loadAllAvailableDatabases(false);
}
});
for (final RecursiveFileObserver malwareMonitor : malwareMonitors) {
threadPoolExecutor.execute(new Runnable() {
@Override
public void run() {
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.notif_scanner_realtime), NotificationManager.IMPORTANCE_LOW);
foregroundChannel.setDescription(getString(R.string.notif_scanner_realtime_summary));
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() > 0 &&*/ file.length() <= maxScanSize) {
HashSet<File> filesToScan = new HashSet<>();
filesToScan.add(file);
new MalwareScanner(false).executeOnExecutor(threadPoolExecutor, filesToScan);
}
updateForegroundNotification();
break;
}
}
});
}
@Override
public final void onDestroy() {
for (RecursiveFileObserver malwareMonitor : malwareMonitors) {
malwareMonitor.stopWatching();
}
malwareMonitors.clear();
System.gc();
//Toast.makeText(this, "Hypatia: Realtime Scanning Stopped", Toast.LENGTH_SHORT).show();
}
private void setForeground() {
foregroundNotification =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(getText(R.string.notif_scanner_realtime))
.setContentText(getText(R.string.notif_scanner_realtime_summary))
.setPriority(Notification.PRIORITY_MIN)
.setShowWhen(false);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
foregroundNotification.setChannelId("FOREGROUND");
}
startForeground(-1, foregroundNotification.build());
}
private void updateForegroundNotification() {
foregroundNotification.setSubText(Utils.amtFilesScanned + " files scanned");
notificationManager.notify(-1, foregroundNotification.build());
}
}

View file

@ -61,6 +61,7 @@ public class Utils {
}
public static HashSet<File> getFilesRecursive(File root) {
int maxScanSize = prefs.getInt("scanner_max_file_size", (1000 * 1000) * 80) * (1000 * 1000);
HashSet<File> filesAll = new HashSet<>();
File[] files = root.listFiles();
@ -72,7 +73,7 @@ public class Utils {
filesAll.addAll(filesTmp);
}
} else {
if (f.length() <= (prefs.getInt("scanner_max_file_size", (1000 * 1000) * 80) * (1000 * 1000)) && f.canRead()) {//Exclude files larger than limit for performance
if (f.length() <= maxScanSize && f.canRead()) {//Exclude files larger than limit for performance
filesAll.add(f);
}
}

View file

@ -8,6 +8,11 @@
<string name="title_settings">Settings</string>
<!-- END MainActivity -->
<!-- START NOTIFICATIONS -->
<string name="notif_scanner_realtime">Malware Detection</string>
<string name="notif_scanner_realtime_summary">Used to alert when malware is detected</string>
<!-- END NOTIFICATIONS -->
<!-- START TOASTS -->
<string name="toast_orbot_unavailable">Orbot is required for this feature</string>
<!-- END TOASTS -->

View file

@ -8,11 +8,11 @@
android:title="@string/settings_pref_scanner_realtime_enabled"
android:summary="@string/settings_pref_scanner_realtime_enabled_summary"
android:defaultValue="false" />
<CheckBoxPreference
<!-- <CheckBoxPreference
android:key="scanner_realtime_path_apps"
android:title="@string/settings_pref_scanner_realtime_path_apps"
android:summary="@string/settings_pref_scanner_realtime_path_apps_summary"
android:defaultValue="true" />
android:defaultValue="true" />-->
<CheckBoxPreference
android:key="scanner_realtime_path_internal"
android:title="@string/settings_pref_scanner_realtime_path_internal"
@ -23,11 +23,11 @@
android:title="@string/settings_pref_scanner_realtime_path_external"
android:summary="@string/settings_pref_scanner_realtime_path_external_summary"
android:defaultValue="true" />
<CheckBoxPreference
<!-- <CheckBoxPreference
android:key="scanner_realtime_path_system"
android:title="@string/settings_pref_scanner_realtime_path_system"
android:summary="@string/settings_pref_scanner_realtime_path_system_summary"
android:defaultValue="true" />
android:defaultValue="true" />-->
</PreferenceCategory>
<PreferenceCategory android:title="@string/settings_category_optimizations">
@ -64,12 +64,12 @@
android:defaultValue="false" />
</PreferenceCategory>
<PreferenceCategory android:title="@string/settings_category_other">
<!-- <PreferenceCategory android:title="@string/settings_category_other">
<CheckBoxPreference
android:key="access_third_party"
android:title="@string/settings_pref_other_third_party"
android:summary="@string/settings_pref_other_third_party_summary"
android:defaultValue="false" />
</PreferenceCategory>
</PreferenceCategory>-->
</PreferenceScreen>