-
Notifications
You must be signed in to change notification settings - Fork 0
/
autofc.py
98 lines (85 loc) · 2.47 KB
/
autofc.py
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
#!/usr/bin/python2
'''
Small script to auto force stop unused app activity/proc.
Tested on jellybean.
'''
import os, re
__author__ = 'bluesec7'
__site__ = 'https://kagurasuki.blogspot.com'
# protect your apps proc from being killed
protect = ['com.google.android.gms.persistent',
#'yarolegovich.materialterminal',
'com.android.vending.billing.InAppBillingService.COIN',
'org.pocketworkstation.pckeyboard',
'com.google.android.gms',
#'com.android.vending',
'com.internet.speed.meter.lite',
]
def recent():
'get recent apps'
method_1 = "dumpsys window tokens|grep 'App #'"
method_2 = "dumpsys activity activities|grep \"Hist #\""
method_3 = "dumpsys window tokens|grep \"AppWindowToken\""
return re.findall('[a-zA-Z_0-9\.]+/', os.popen(method_2).read())
def activities():
'get all activities'
method_1 = "dumpsys activity|grep \"Proc #\""
return os.popen(method_1).read()
def stop_activities():
states = {'\(service\)':(('adj=vis', '/FS'), ('adj=prcp', '/F')),
'\(started\-services\)':(('adj=svc ', '/B'), ('adj=svcb', '/B')),
'\(fg\-service\)':(('adj=prcp', '/FS')),
#'\(bg\-empty\)':(('adj=bak', '/F', 'adj=bak', '/B')),
}
services = {}
for proc in activities().splitlines():
ppkg = re.search('[0-9]+\:[a-zA-Z_0-9\.]+(\:[a-zA-Z_0-9]+)?', proc)
#print ppkg.group()
for state in states:
m = re.search(state, proc, re.M)
if m:
#print m.group(),'in', proc
for t in states[state]:
if t[0] in proc and t[1] in proc:
#print 'kill', ppkg.group()
s = ppkg.group().split(':')
if not services.has_key(s[1]):
services[s[1]] = []
if len(s)==3:
services[s[1]].append(s[2])
break
break
#print services
# step to check activities
kill_queue = []
for s in services:
meminfo = os.popen('dumpsys meminfo %s'%s).read()
m = re.search('Activities\:([\s]+)?[0-1]', meminfo)
if m:
#print 'proc for %s'%s
if '1' in m.group():
if s not in recent():
# stop proc
#print 'active proc but not in recent: %s'%s
#kill_queue.append(s)
pass
else:
#print 'not an active proc: %s'%s
# stop proc
kill_queue.append(s)
else:
#print 'no proc found for %s'%s
if services[s]:
#print 'sub services stopped: %s'%', '.join(services[s])
# stop proc
kill_queue.append(s)
else:
pass
#print 'no sub services found: %s'%s
for q in kill_queue:
if q not in protect:
print os.popen('am force-stop %s'%q).read()
#print recent()
if __name__=='__main__':
while 1:
stop_activities()