-
Notifications
You must be signed in to change notification settings - Fork 8
/
SystemHook.java
713 lines (628 loc) · 26.1 KB
/
SystemHook.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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
package me.piebridge.prevent.framework;
import android.app.AppGlobals;
import android.app.AppOpsManager;
import android.app.usage.IUsageStatsManager;
import android.content.Context;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Process;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.text.TextUtils;
import com.android.internal.app.IAppOpsService;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import me.piebridge.prevent.BuildConfig;
import me.piebridge.prevent.common.Configuration;
import me.piebridge.prevent.common.GmsUtils;
import me.piebridge.prevent.common.PackageUtils;
import me.piebridge.prevent.common.PreventIntent;
import me.piebridge.prevent.framework.util.AccountUtils;
import me.piebridge.prevent.framework.util.ActivityManagerServiceUtils;
import me.piebridge.prevent.framework.util.ActivityRecordUtils;
import me.piebridge.prevent.framework.util.HideApiUtils;
import me.piebridge.prevent.framework.util.LogUtils;
import me.piebridge.prevent.framework.util.NotificationManagerServiceUtils;
import me.piebridge.prevent.framework.util.PreventListUtils;
public final class SystemHook {
public static final int TIME_SUICIDE = 6;
public static final int TIME_DESTROY = 6;
public static final int TIME_IMMEDIATE = 1;
public static final int TIME_CHECK_SERVICE = 30;
public static final int TIME_KILL = 1;
public static final int TIME_CHECK_GMS = 30;
public static final int TIME_CHECK_DISALLOW = 5;
public static final int TIME_CHECK_USER_LEAVING = 60;
private static Context mContext;
private static boolean activated;
private static ClassLoader mClassLoader;
private static Map<String, Boolean> mPreventPackages;
private static Set<String> checkingPackageNames = new TreeSet<String>();
private static Set<String> checkingWhiteList = new TreeSet<String>();
private static Set<String> runningGapps = new TreeSet<String>();
private static final Object CHECKING_LOCK = new Object();
private static ScheduledThreadPoolExecutor singleExecutor = new ScheduledThreadPoolExecutor(0x2);
private static ScheduledThreadPoolExecutor checkingExecutor = new ScheduledThreadPoolExecutor(0x2);
private static ScheduledThreadPoolExecutor forceStopExecutor = new ScheduledThreadPoolExecutor(0x1);
private static ScheduledThreadPoolExecutor moveBackExecutor = new ScheduledThreadPoolExecutor(0x2);
private static ScheduledFuture<?> checkingFuture;
private static ScheduledFuture<?> killingFuture;
private static Map<String, ScheduledFuture<?>> serviceFutures = new HashMap<String, ScheduledFuture<?>>();
private static final Object REGISTER_LOCK = new Object();
private static SystemReceiver systemReceiver;
private static ScheduledThreadPoolExecutor restoreExecutor = new ScheduledThreadPoolExecutor(0x2);
private static final Object RESTORE_LOCK = new Object();
private static Map<String, ScheduledFuture<?>> restoreFutures = new HashMap<String, ScheduledFuture<?>>();
private static Map<String, Boolean> syncPackages = new HashMap<String, Boolean>();
private static String currentPackageName;
private static Set<String> currentPackageNames;
private static volatile boolean expired = true;
private static Object lock = new Object();
private static int version;
private static String method;
private static boolean supported = true;
private SystemHook() {
}
public static void setClassLoader(ClassLoader classLoader) {
mClassLoader = classLoader;
}
public static ClassLoader getClassLoader() {
return mClassLoader;
}
public static boolean registerReceiver() {
HandlerThread thread = new HandlerThread("PreventService");
thread.start();
Handler handler = new Handler(thread.getLooper());
systemReceiver = new SystemReceiver(mContext, mPreventPackages);
IntentFilter manager = new IntentFilter();
for (String action : SystemReceiver.MANAGER_ACTIONS) {
manager.addAction(action);
}
manager.addDataScheme(PreventIntent.SCHEME);
mContext.registerReceiver(systemReceiver, manager, PreventIntent.PERMISSION_MANAGER, handler);
IntentFilter filter = new IntentFilter();
for (String action : SystemReceiver.PACKAGE_ACTIONS) {
filter.addAction(action);
}
filter.addDataScheme("package");
mContext.registerReceiver(systemReceiver, filter, null, handler);
IntentFilter noSchemeFilter = new IntentFilter();
for (String action : SystemReceiver.NON_SCHEME_ACTIONS) {
noSchemeFilter.addAction(action);
}
noSchemeFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
mContext.registerReceiver(systemReceiver, noSchemeFilter, null, handler);
PreventLog.i("prevent running " + BuildConfig.VERSION_NAME + " activated");
activated = true;
return true;
}
public static void retrievePreventsIfNeeded(final Context context) {
if (mContext == null) {
synchronized (REGISTER_LOCK) {
if (mContext == null) {
mContext = context;
mPreventPackages = new ConcurrentHashMap<String, Boolean>();
loadPreventList(mContext, mPreventPackages);
ActivityManagerServiceHook.setContext(mContext, mPreventPackages);
IntentFilterHook.setContext(mContext, mPreventPackages);
registerReceiver();
}
}
}
}
private static void loadPreventList(Context context, Map<String, Boolean> preventPackages) {
if (PreventListUtils.getInstance().canLoad(context)) {
Set<String> prevents = PreventListUtils.getInstance().load(context);
PreventLog.d("prevent list size: " + prevents.size());
for (String packageName : prevents) {
preventPackages.put(packageName, true);
}
}
}
public static boolean isSystemHook() {
return Process.myUid() == Process.SYSTEM_UID;
}
static String getProcessName(int pid) {
File file = new File(new File("/proc", String.valueOf(pid)), "cmdline");
return getContent(file);
}
private static String getContent(File file) {
if (!file.isFile() || !file.canRead()) {
return null;
}
try {
InputStream is = new BufferedInputStream(new FileInputStream(file));
ByteArrayOutputStream os = new ByteArrayOutputStream();
int length;
byte[] buffer = new byte[0x1000];
while ((length = is.read(buffer)) != -1) {
os.write(buffer, 0, length);
}
is.close();
return os.toString().trim();
} catch (IOException e) {
PreventLog.e("cannot read file " + file, e);
return null;
}
}
public static void checkRunningServices(final String packageName, final boolean forcestop) {
if (packageName == null) {
return;
}
ScheduledFuture<?> serviceFuture;
synchronized (CHECKING_LOCK) {
serviceFuture = serviceFutures.get(packageName);
if (serviceFuture != null && serviceFuture.getDelay(TimeUnit.SECONDS) > 0) {
GmsUtils.decreaseGmsCount(mContext, packageName);
serviceFuture.cancel(false);
}
if (!GmsUtils.isGms(packageName)) {
checkingWhiteList.add(packageName);
}
}
GmsUtils.increaseGmsCount(mContext, packageName);
serviceFuture = checkingExecutor.schedule(new CheckingRunningService(mContext, mPreventPackages) {
@Override
protected Collection<String> preparePackageNames() {
return Collections.singletonList(packageName);
}
@Override
protected Collection<String> prepareWhiteList() {
return prepareServiceWhiteList(packageName, forcestop);
}
}, GmsUtils.isGms(packageName) ? TIME_CHECK_GMS : TIME_CHECK_SERVICE, TimeUnit.SECONDS);
synchronized (CHECKING_LOCK) {
serviceFutures.put(packageName, serviceFuture);
}
}
private static Collection<String> prepareServiceWhiteList(String packageName, boolean forcestop) {
GmsUtils.decreaseGmsCount(mContext, packageName);
if (!GmsUtils.isGms(packageName)) {
synchronized (CHECKING_LOCK) {
checkingWhiteList.remove(packageName);
}
}
if (canStopGms()) {
if (forcestop) {
forceStopPackageIfNeeded(packageName);
}
return Collections.emptyList();
} else {
return GmsUtils.getGmsPackages();
}
}
public static void cancelCheck(String packageName) {
if (packageName != null) {
synchronized (CHECKING_LOCK) {
checkingPackageNames.remove(packageName);
}
}
}
public static boolean checkRunningServices(final String packageName, int seconds) {
if (packageName != null) {
synchronized (CHECKING_LOCK) {
checkingPackageNames.add(packageName);
}
}
if (checkingFuture != null && checkingFuture.getDelay(TimeUnit.SECONDS) > 0) {
return false;
}
checkingFuture = singleExecutor.schedule(new CheckingRunningService(mContext, mPreventPackages) {
@Override
protected Collection<String> preparePackageNames() {
return prepareCheckingPackageNames();
}
@Override
protected Collection<String> prepareWhiteList() {
return prepareCheckingWhiteList();
}
}, seconds, TimeUnit.SECONDS);
return true;
}
private static Collection<String> prepareCheckingPackageNames() {
Set<String> packageNames = new TreeSet<String>();
synchronized (CHECKING_LOCK) {
packageNames.addAll(checkingPackageNames);
checkingPackageNames.clear();
}
return packageNames;
}
private static Collection<String> prepareCheckingWhiteList() {
Set<String> whiteList = new TreeSet<String>();
synchronized (CHECKING_LOCK) {
whiteList.addAll(checkingWhiteList);
}
if (!canStopGms()) {
whiteList.addAll(GmsUtils.getGmsPackages());
}
return whiteList;
}
public static void forceStopPackageIfNeeded(final String packageName) {
if (!Boolean.TRUE.equals(mPreventPackages.get(packageName))) {
return;
}
forceStopExecutor.execute(new Runnable() {
@Override
public void run() {
if (!Boolean.TRUE.equals(mPreventPackages.get(packageName))) {
return;
}
forceStopPackage(packageName, false);
}
});
}
public static boolean inactive(String packageName) {
IUsageStatsManager usm = IUsageStatsManager.Stub.asInterface(ServiceManager.getService(Context.USAGE_STATS_SERVICE));
Object usageStatsService = HideApiUtils.getThis0(usm);
if (usageStatsService == null) {
return false;
}
if (isAppInactive(usm, packageName)) {
PreventLog.d(packageName + " already inactive");
} else {
if (!setAppInactive(usageStatsService, packageName)) {
return false;
}
PreventLog.d("set " + packageName + " to inactive, current inactive: " + isAppInactive(usm, packageName));
}
return true;
}
public static boolean isAppInactive(String packageName) {
IUsageStatsManager usm = IUsageStatsManager.Stub.asInterface(ServiceManager.getService(Context.USAGE_STATS_SERVICE));
return isAppInactive(usm, packageName);
}
private static boolean setAppInactive(Object usageStatsService, String packageName) {
try {
Method method = usageStatsService.getClass().getDeclaredMethod("setAppIdle", String.class, boolean.class, int.class);
method.setAccessible(true);
method.invoke(usageStatsService, packageName, true, 0);
return true;
} catch (NoSuchMethodException e) {
PreventLog.d("cannot inactive(no method) " + packageName, e);
return false;
} catch (InvocationTargetException e) {
PreventLog.d("cannot inactive(invoke) " + packageName, e);
return false;
} catch (IllegalAccessException e) {
PreventLog.d("cannot inactive(illegal access) " + packageName, e);
return false;
}
}
private static boolean isAppInactive(IUsageStatsManager usm, String packageName) {
try {
return usm.isAppInactive(packageName, 0);
} catch (RemoteException e) {
PreventLog.d("remote exception " + packageName, e);
return false;
}
}
public static boolean killNoFather() {
if (killingFuture != null && killingFuture.getDelay(TimeUnit.SECONDS) > 0) {
return false;
}
killingFuture = singleExecutor.schedule(new Runnable() {
@Override
public void run() {
try {
doKillNoFather();
} catch (Throwable t) { // NOSONAR
PreventLog.e("cannot killNoFather", t);
}
}
}, TIME_KILL, TimeUnit.SECONDS);
return true;
}
private static void doKillNoFather() {
File proc = new File("/proc");
for (File file : proc.listFiles()) {
if (file.isDirectory() && TextUtils.isDigitsOnly(file.getName())) {
int pid = Integer.parseInt(file.getName());
int uid = HideApiUtils.getUidForPid(pid);
if (HideApiUtils.getParentPid(pid) == 1 && uid >= PackageUtils.FIRST_APPLICATION_UID) {
killIfNeed(uid, pid);
}
}
}
}
private static void killIfNeed(int uid, int pid) {
String[] names = mContext.getPackageManager().getPackagesForUid(uid);
if (names == null || isPrevent(names)) {
Process.killProcess(pid);
String name;
if (names == null) {
name = "(uid: " + uid + ", process: + " + getProcessName(pid) + ")";
} else if (names.length == 1) {
name = names[0];
} else {
name = Arrays.asList(names).toString();
}
LogUtils.logKill(pid, "without parent", name);
}
}
private static boolean isPrevent(String[] names) {
for (String name : names) {
if (!Boolean.TRUE.equals(mPreventPackages.get(name))) {
return false;
}
}
return true;
}
public static void restorePrevent(String packageName) {
if (systemReceiver != null && 0 == systemReceiver.countCounter(packageName) && Boolean.FALSE.equals(mPreventPackages.get(packageName))) {
PreventLog.v("restore prevent for " + packageName);
mPreventPackages.put(packageName, true);
checkRunningServices(packageName, TIME_DESTROY);
}
}
public static void onLaunchActivity(Object activityRecord) {
expired = true;
currentPackageName = ActivityRecordUtils.getPackageName(activityRecord);
PreventLog.v("launch, current: " + currentPackageName);
if (systemReceiver != null) {
systemReceiver.onLaunchActivity(activityRecord);
}
}
public static void onDestroyActivity(Object activityRecord) {
expired = true;
if (systemReceiver != null) {
if (systemReceiver.onDestroyActivity(activityRecord)) {
String packageName = ActivityRecordUtils.getPackageName(activityRecord);
PreventLog.v("destroy all activity: " + packageName);
}
}
}
public static void onResumeActivity(Object activityRecord) {
expired = true;
currentPackageName = ActivityRecordUtils.getPackageName(activityRecord);
PreventLog.v("resume, current: " + currentPackageName);
if (systemReceiver != null) {
systemReceiver.onResumeActivity(activityRecord);
}
}
public static void onUserLeavingActivity(Object activityRecord) {
expired = true;
String packageName = ActivityRecordUtils.getPackageName(activityRecord);
PreventLog.v("leaving, current: " + packageName);
if (systemReceiver != null) {
systemReceiver.onUserLeavingActivity(activityRecord);
}
}
public static void onStartHomeActivity(String packageName) {
expired = true;
if (systemReceiver != null) {
systemReceiver.onDestroyActivity("start home activity", packageName);
}
}
public static void onMoveActivityToBack(final String packageName) {
expired = true;
PreventLog.v("move activity to back, package: " + packageName);
moveBackExecutor.schedule(new Runnable() {
@Override
public void run() {
if (systemReceiver != null) {
systemReceiver.onDestroyActivity("move activity to back", packageName);
}
}
}, 0x200, TimeUnit.MILLISECONDS);
}
public static void onAppDied(Object processRecord) {
expired = true;
if (systemReceiver != null) {
systemReceiver.onAppDied(processRecord);
}
}
public static boolean hasRunningActivity(String packageName) {
if (packageName != null && systemReceiver != null && systemReceiver.countCounter(packageName) != 0) {
return true;
}
// for temp allow
ScheduledFuture<?> restoreFuture = restoreFutures.get(packageName);
return restoreFuture != null && restoreFuture.getDelay(TimeUnit.SECONDS) > 0;
}
public static boolean isFramework(String packageName) {
return "android".equals(packageName);
}
public static boolean isSystemPackage(String packageName) {
if (isFramework(packageName) || GmsUtils.isGms(packageName)) {
return true;
}
if (packageName == null || GmsUtils.isGapps(packageName)) {
return false;
}
try {
PackageManager pm = mContext.getPackageManager();
int flags = pm.getApplicationInfo(packageName, 0).flags;
return PackageUtils.isSystemPackage(flags);
} catch (PackageManager.NameNotFoundException e) {
PreventLog.d("cannot find package: " + packageName, e);
return false;
}
}
public static boolean isActivated() {
return activated;
}
public static String getCurrentPackageName() {
return currentPackageName;
}
public static Set<String> getCurrentPackageNames() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
return Collections.singleton(currentPackageName);
} else {
if (currentPackageNames == null || expired) {
synchronized (lock) {
if (expired) {
expired = false;
currentPackageNames = ActivityManagerServiceUtils.getCurrentPackages();
}
}
}
return currentPackageNames;
}
}
public static void checkSync(final String packageName) {
singleExecutor.submit(new Runnable() {
@Override
public void run() {
Boolean syncable = AccountUtils.isPackageSyncable(mContext, packageName);
if (syncable != null) {
PreventLog.d("sync for " + packageName + ": " + syncable);
syncPackages.put(packageName, syncable);
}
}
});
}
public static void resetSync(final String packageName) {
singleExecutor.submit(new Runnable() {
@Override
public void run() {
if (Boolean.FALSE.equals(syncPackages.get(packageName)) && Boolean.TRUE.equals(mPreventPackages.get(packageName))
&& Configuration.getDefault().isLockSyncSettings()) {
PreventLog.d("reset sync for " + packageName + " to false");
AccountUtils.setSyncable(mContext, packageName, false);
}
}
});
}
public static int getVersion() {
return SystemHook.version;
}
public static String getMethod() {
return SystemHook.method;
}
public static void setMethod(String method) {
SystemHook.method = method;
}
public static void setVersion(int version) {
SystemHook.version = version;
}
public static boolean isUseAppStandby() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && Configuration.getDefault().isUseAppStandby();
}
public static void setNotSupported() {
if (isSupported()) {
SystemHook.supported = false;
}
}
public static void setSupported(boolean supported) {
SystemHook.supported = supported;
}
public static boolean isSupported() {
return supported;
}
public static void updateRunningGapps(String packageName, boolean added) {
if (mContext == null || packageName == null) {
return;
}
if (!added) {
resetSync(packageName);
}
PackageManager pm = mContext.getPackageManager();
if (GmsUtils.isGapps(packageName) && pm.getLaunchIntentForPackage(packageName) != null) {
if (added) {
if (!runningGapps.contains(packageName)) {
PreventLog.d("add " + packageName + " to running gapps: " + runningGapps);
}
runningGapps.add(packageName);
} else {
if (runningGapps.contains(packageName)) {
PreventLog.d("remove " + packageName + " from running gapps: " + runningGapps);
checkRunningServices(null, SystemHook.TIME_CHECK_SERVICE);
}
runningGapps.remove(packageName);
}
}
}
public static boolean hasRunningGapps() {
Iterator<String> it = runningGapps.iterator();
int count = 0;
while (it.hasNext()) {
String packageName = it.next();
int counter = systemReceiver.countCounter(packageName);
if (counter == 0) {
it.remove();
} else if (counter > 1 || !PackageUtils.isLauncher(mContext.getPackageManager(), packageName)) {
count += 1;
}
}
if (count > 0) {
PreventLog.d("running gapps: " + runningGapps);
return true;
} else {
return false;
}
}
public static void restoreLater(final String packageName) {
systemReceiver.cancelCheckLeaving(packageName);
synchronized (RESTORE_LOCK) {
ScheduledFuture<?> restoreFuture = restoreFutures.get(packageName);
if (restoreFuture != null && restoreFuture.getDelay(TimeUnit.SECONDS) > 0) {
restoreFuture.cancel(false);
}
restoreFuture = restoreExecutor.schedule(new Runnable() {
@Override
public void run() {
restorePrevent(packageName);
}
}, TIME_CHECK_DISALLOW, TimeUnit.SECONDS);
restoreFutures.put(packageName, restoreFuture);
}
}
public static boolean forceStopPackage(String packageName, boolean force) {
if (!Configuration.getDefault().isStopSignatureApps() && ActivityManagerServiceHook.cannotPrevent(packageName)) {
PreventLog.i("wont force-stop important system package: " + packageName + ", force: " + force);
return false;
} else if (force) {
HideApiUtils.forceStopPackage(mContext, packageName);
return true;
} else if (isUseAppStandby()) {
inactive(packageName);
setBackground(packageName, AppOpsManager.MODE_IGNORED);
return false;
} else if (getCurrentPackageNames().contains(packageName)) {
PreventLog.i(packageName + " seems not safe to force stop");
return false;
} else {
NotificationManagerServiceUtils.keepNotification(packageName);
HideApiUtils.forceStopPackage(mContext, packageName);
return true;
}
}
public static void setBackground(String packageName, int mode) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
try {
IAppOpsService appOpsService = IAppOpsService.Stub.asInterface(ServiceManager.getService(Context.APP_OPS_SERVICE));
int packageUid = AppGlobals.getPackageManager().getPackageUid(packageName, PackageManager.MATCH_UNINSTALLED_PACKAGES, 0);
appOpsService.setMode(AppOpsManager.OP_RUN_IN_BACKGROUND, packageUid, packageName, mode);
} catch (RemoteException e) {
PreventLog.d("cannot set background for " + packageName, e);
}
}
}
public static Context getContext() {
return mContext;
}
public static boolean canStopGms() {
return GmsUtils.canStopGms() && !SystemHook.hasRunningGapps();
}
}