-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathProcessUtils.java
More file actions
217 lines (204 loc) · 8.45 KB
/
ProcessUtils.java
File metadata and controls
217 lines (204 loc) · 8.45 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package com.xzy.utils.process;
import android.app.ActivityManager;
import android.app.AppOpsManager;
import android.app.usage.UsageStats;
import android.app.usage.UsageStatsManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.provider.Settings;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresPermission;
import com.xzy.utils.common.Utils;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static android.Manifest.permission.KILL_BACKGROUND_PROCESSES;
/**
* 进程相关的工具类。
* 参考 https://github.com/Blankj/AndroidUtilCode/blob/master/lib/utilcode/src/main/
* java/com/blankj/utilcode/util/ProcessUtils.java
*
* @author xzy
*/
@SuppressWarnings("all")
public final class ProcessUtils {
private ProcessUtils() {
throw new UnsupportedOperationException("u can't instantiate me...");
}
/**
* Return the foreground process name.
* <p>Target APIs greater than 21 must hold
* {@code <uses-permission android:name="android.permission.PACKAGE_USAGE_STATS" />}</p>
*
* @return the foreground process name
*/
public static String getForegroundProcessName() {
ActivityManager am =
(ActivityManager) Utils.getApp().getSystemService(Context.ACTIVITY_SERVICE);
//noinspection ConstantConditions
List<ActivityManager.RunningAppProcessInfo> pInfo = am.getRunningAppProcesses();
if (pInfo != null && pInfo.size() > 0) {
for (ActivityManager.RunningAppProcessInfo aInfo : pInfo) {
if (aInfo.importance
== ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
return aInfo.processName;
}
}
}
if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.LOLLIPOP) {
PackageManager pm = Utils.getApp().getPackageManager();
Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
List<ResolveInfo> list =
pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Log.i("ProcessUtils", list.toString());
if (list.size() <= 0) {
Log.i("ProcessUtils",
"getForegroundProcessName: noun of access to usage information.");
return "";
}
try {// Access to usage information.
ApplicationInfo info =
pm.getApplicationInfo(Utils.getApp().getPackageName(), 0);
AppOpsManager aom =
(AppOpsManager) Utils.getApp().getSystemService(Context.APP_OPS_SERVICE);
//noinspection ConstantConditions
if (aom.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS,
info.uid,
info.packageName) != AppOpsManager.MODE_ALLOWED) {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Utils.getApp().startActivity(intent);
}
if (aom.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS,
info.uid,
info.packageName) != AppOpsManager.MODE_ALLOWED) {
Log.i("ProcessUtils",
"getForegroundProcessName: refuse to device usage stats.");
return "";
}
UsageStatsManager usageStatsManager = (UsageStatsManager) Utils.getApp()
.getSystemService(Context.USAGE_STATS_SERVICE);
List<UsageStats> usageStatsList = null;
if (usageStatsManager != null) {
long endTime = System.currentTimeMillis();
long beginTime = endTime - 86400000 * 7;
usageStatsList = usageStatsManager
.queryUsageStats(UsageStatsManager.INTERVAL_BEST,
beginTime, endTime);
}
if (usageStatsList == null || usageStatsList.isEmpty()) return "";
UsageStats recentStats = null;
for (UsageStats usageStats : usageStatsList) {
if (recentStats == null
|| usageStats.getLastTimeUsed() > recentStats.getLastTimeUsed()) {
recentStats = usageStats;
}
}
return recentStats == null ? null : recentStats.getPackageName();
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}
return "";
}
/**
* Return all background processes.
* <p>Must hold {@code <uses-permission
* android:name="android.permission.KILL_BACKGROUND_PROCESSES" />}</p>
*
* @return all background processes
*/
@RequiresPermission(KILL_BACKGROUND_PROCESSES)
public static Set<String> getAllBackgroundProcesses() {
ActivityManager am =
(ActivityManager) Utils.getApp().getSystemService(Context.ACTIVITY_SERVICE);
//noinspection ConstantConditions
List<ActivityManager.RunningAppProcessInfo> info = am.getRunningAppProcesses();
Set<String> set = new HashSet<>();
if (info != null) {
for (ActivityManager.RunningAppProcessInfo aInfo : info) {
Collections.addAll(set, aInfo.pkgList);
}
}
return set;
}
/**
* Kill all background processes.
* <p>Must hold {@code <uses-permission
* android:name="android.permission.KILL_BACKGROUND_PROCESSES" />}</p>
*
* @return background processes were killed
*/
@RequiresPermission(KILL_BACKGROUND_PROCESSES)
public static Set<String> killAllBackgroundProcesses() {
ActivityManager am =
(ActivityManager) Utils.getApp().getSystemService(Context.ACTIVITY_SERVICE);
//noinspection ConstantConditions
List<ActivityManager.RunningAppProcessInfo> info = am.getRunningAppProcesses();
Set<String> set = new HashSet<>();
if (info == null) return set;
for (ActivityManager.RunningAppProcessInfo aInfo : info) {
for (String pkg : aInfo.pkgList) {
am.killBackgroundProcesses(pkg);
set.add(pkg);
}
}
info = am.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo aInfo : info) {
for (String pkg : aInfo.pkgList) {
set.remove(pkg);
}
}
return set;
}
/**
* Kill background processes.
* <p>Must hold {@code <uses-permission
* android:name="android.permission.KILL_BACKGROUND_PROCESSES" />}</p>
*
* @param packageName The name of the package.
* @return {@code true}: success<br>{@code false}: fail
*/
@RequiresPermission(KILL_BACKGROUND_PROCESSES)
public static boolean killBackgroundProcesses(@NonNull final String packageName) {
ActivityManager am =
(ActivityManager) Utils.getApp().getSystemService(Context.ACTIVITY_SERVICE);
//noinspection ConstantConditions
List<ActivityManager.RunningAppProcessInfo> info = am.getRunningAppProcesses();
if (info == null || info.size() == 0) return true;
for (ActivityManager.RunningAppProcessInfo aInfo : info) {
if (Arrays.asList(aInfo.pkgList).contains(packageName)) {
am.killBackgroundProcesses(packageName);
}
}
info = am.getRunningAppProcesses();
if (info == null || info.size() == 0) return true;
for (ActivityManager.RunningAppProcessInfo aInfo : info) {
if (Arrays.asList(aInfo.pkgList).contains(packageName)) {
return false;
}
}
return true;
}
/**
* Return whether app running in the main process.
*
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isMainProcess() {
return Utils.getApp().getPackageName().equals(Utils.getCurrentProcessName());
}
/**
* Return the name of current process.
*
* @return the name of current process
*/
public static String getCurrentProcessName() {
return Utils.getCurrentProcessName();
}
}