Restart service on update

This commit is contained in:
Tad 2017-12-17 06:36:16 -05:00
parent 7b67a7c6f3
commit 8de76aad66
6 changed files with 40 additions and 22 deletions

View file

@ -6,8 +6,8 @@ android {
applicationId "us.spotco.malwarescanner" applicationId "us.spotco.malwarescanner"
minSdkVersion 21 minSdkVersion 21
targetSdkVersion 26 targetSdkVersion 26
versionCode 15 versionCode 17
versionName "1.7" versionName "1.8"
} }
buildTypes { buildTypes {
debug { debug {

View file

@ -33,13 +33,18 @@
android:exported="false"/> android:exported="false"/>
<receiver <receiver
android:name=".BootReceiver" android:name="EventReceiver"
android:label="Theia Persistence" android:label="Theia Persistence"
android:enabled="true" android:enabled="true"
android:exported="true"> android:exported="true">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" /> <action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" /> <action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.PACKAGE_REPLACED" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<data android:scheme="package" />
</intent-filter> </intent-filter>
</receiver> </receiver>

View file

@ -5,11 +5,20 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
public class BootReceiver extends BroadcastReceiver { public class EventReceiver extends BroadcastReceiver {
@Override @Override
public final void onReceive(Context context, Intent intent) { public final void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) { if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
considerStartService(context);
}
if (intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED) && intent.getDataString().contains(context.getPackageName())) {
considerStartService(context);
}
}
private static void considerStartService(Context context) {
if (!Utils.isServiceRunning(MalwareScannerService.class, context)) {
SharedPreferences prefs = context.getSharedPreferences(BuildConfig.APPLICATION_ID, Context.MODE_PRIVATE); SharedPreferences prefs = context.getSharedPreferences(BuildConfig.APPLICATION_ID, Context.MODE_PRIVATE);
boolean autostart = prefs.getBoolean("autostart", false); boolean autostart = prefs.getBoolean("autostart", false);

View file

@ -1,7 +1,6 @@
package us.spotco.malwarescanner; package us.spotco.malwarescanner;
import android.Manifest; import android.Manifest;
import android.app.ActivityManager;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
@ -97,7 +96,7 @@ public class MainActivity extends AppCompatActivity {
@Override @Override
public final boolean onCreateOptionsMenu(Menu menu) { public final boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu); getMenuInflater().inflate(R.menu.menu_main, menu);
menu.findItem(R.id.toggleRealtime).setChecked(isServiceRunning(MalwareScannerService.class)); menu.findItem(R.id.toggleRealtime).setChecked(Utils.isServiceRunning(MalwareScannerService.class, this));
return true; return true;
} }
@ -147,14 +146,4 @@ public class MainActivity extends AppCompatActivity {
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
} }
private boolean isServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
} }

View file

@ -34,7 +34,7 @@ public abstract class RecursiveFileObserver {
if (files == null) { if (files == null) {
continue; continue;
} }
for(File file : files) { for (File file : files) {
if (file.isDirectory() && !file.getName().equals(".") && !file.getName().equals("..")) { if (file.isDirectory() && !file.getName().equals(".") && !file.getName().equals("..")) {
final String currentPath = file.getAbsolutePath(); final String currentPath = file.getAbsolutePath();
if (depth(file) <= 8 && !stack.contains(currentPath) && !observing(currentPath)) { if (depth(file) <= 8 && !stack.contains(currentPath) && !observing(currentPath)) {
@ -43,22 +43,22 @@ public abstract class RecursiveFileObserver {
} }
} }
} }
for(FileObserver observer : mObservers) { for (FileObserver observer : mObservers) {
observer.startWatching(); observer.startWatching();
} }
} }
private static int depth(File file) { private static int depth(File file) {
int depth = 0; int depth = 0;
while((file = file.getParentFile()) != null) { while ((file = file.getParentFile()) != null) {
depth++; depth++;
} }
return depth; return depth;
} }
private boolean observing(String path) { private boolean observing(String path) {
for(SingleFileObserver observer : mObservers) { for (SingleFileObserver observer : mObservers) {
if(path.equals(observer.path)) { if (path.equals(observer.path)) {
return true; return true;
} }
} }
@ -66,7 +66,7 @@ public abstract class RecursiveFileObserver {
} }
public final synchronized void stopWatching() { public final synchronized void stopWatching() {
for(FileObserver observer : mObservers) { for (FileObserver observer : mObservers) {
observer.stopWatching(); observer.stopWatching();
} }
mObservers.clear(); mObservers.clear();

View file

@ -1,5 +1,8 @@
package us.spotco.malwarescanner; package us.spotco.malwarescanner;
import android.app.ActivityManager;
import android.content.Context;
import java.io.File; import java.io.File;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
@ -29,4 +32,16 @@ class Utils {
return filesAll; return filesAll;
} }
//Credit: https://stackoverflow.com/a/5921190
public static boolean isServiceRunning(Class<?> serviceClass, Context context) {
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
} }