-
Notifications
You must be signed in to change notification settings - Fork 8
/
CheckingRunningService.java
94 lines (80 loc) · 3.56 KB
/
CheckingRunningService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package me.piebridge.prevent.framework;
import android.app.ActivityManager;
import android.content.Context;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import me.piebridge.prevent.BuildConfig;
import me.piebridge.prevent.common.Configuration;
import me.piebridge.prevent.common.PackageUtils;
import me.piebridge.prevent.framework.util.HookUtils;
/**
* Created by thom on 15/7/25.
*/
abstract class CheckingRunningService implements Runnable {
private final Context mContext;
private Map<String, Boolean> mPreventPackages;
CheckingRunningService(Context context, Map<String, Boolean> preventPackages) {
mContext = context;
mPreventPackages = preventPackages;
}
@Override
public void run() {
Collection<String> packageNames = preparePackageNames();
Collection<String> whiteList = prepareWhiteList();
if (!packageNames.isEmpty() && packageNames.equals(whiteList)) {
return;
}
PreventLog.d("checking services, packages: " + packageNames + ", whitelist: " + whiteList);
Set<String> shouldStopPackageNames = new TreeSet<String>();
if (Configuration.getDefault().isDestroyProcesses()) {
shouldStopPackageNames.addAll(packageNames);
shouldStopPackageNames.removeAll(whiteList);
packageNames = Collections.emptyList();
}
for (ActivityManager.RunningServiceInfo service : HookUtils.getServices(mContext)) {
checkService(service, packageNames, whiteList, shouldStopPackageNames);
}
stopServiceIfNeeded(shouldStopPackageNames);
PreventLog.v("complete checking running service");
}
private boolean checkService(ActivityManager.RunningServiceInfo service, Collection<String> packageNames, Collection<String> whiteList, Set<String> shouldStopPackageNames) {
String name = service.service.getPackageName();
boolean prevent = Boolean.TRUE.equals(mPreventPackages.get(name));
logServiceIfNeeded(prevent, name, service);
if (!prevent || whiteList.contains(name)) {
return false;
}
if (packageNames.contains(name) || service.started) {
shouldStopPackageNames.add(name);
}
return true;
}
protected abstract Collection<String> preparePackageNames();
protected abstract Collection<String> prepareWhiteList();
private void logServiceIfNeeded(boolean prevents, String name, ActivityManager.RunningServiceInfo service) {
if (!service.started) {
return;
}
if (BuildConfig.DEBUG || prevents || service.uid >= PackageUtils.FIRST_APPLICATION_UID) {
PreventLog.v("prevents: " + prevents + ", name: " + name + ", count: " + service.clientCount + ", label: " + service.clientLabel
+ ", uid: " + service.uid + ", pid: " + service.pid + ", process: " + service.process + ", flags: " + service.flags);
}
}
private void stopServiceIfNeeded(Set<String> shouldStopPackageNames) {
for (String name : shouldStopPackageNames) {
String forceStop = "force stop";
if (SystemHook.isUseAppStandby()) {
forceStop = "standby";
}
if (Configuration.getDefault().isDestroyProcesses()) {
PreventLog.i(forceStop + " " + name);
} else {
PreventLog.i(name + " has running services, " + forceStop + " it");
}
SystemHook.forceStopPackageIfNeeded(name);
}
}
}