Skip to content

Commit 0feeed9

Browse files
committed
Add apk sdk and grant permission check
1 parent 0629904 commit 0feeed9

File tree

1 file changed

+86
-38
lines changed

1 file changed

+86
-38
lines changed

show_install_options.py

Lines changed: 86 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import sys
44
import re
55
import pipes
6-
from workflow import Workflow3, ICON_WARNING, ICON_INFO, web
6+
from workflow import Workflow3, ICON_WARNING, ICON_INFO, ICON_ERROR, web
77

88
reload(sys)
99
sys.setdefaultencoding("utf-8")
@@ -12,8 +12,9 @@
1212
aapt_path = os.getenv('aapt_path')
1313
serial = os.getenv('serial')
1414
apkFileOrFolder = pipes.quote(os.getenv('apkFile'))
15+
deviceApi = os.getenv('device_api')
1516

16-
def showApkInstallItems(wf):
17+
def showApkInstallItems():
1718

1819
arg = wf.args[0].strip()
1920

@@ -23,46 +24,92 @@ def showApkInstallItems(wf):
2324
wf.add_item(title="aapt not found", subtitle="Please config 'aapt_path' in workflow settings for richer APK info", valid=False, icon=ICON_WARNING)
2425
else:
2526

26-
result = subprocess.check_output("{0} dump badging {1} | grep 'package:\|application-label:'".format(aapt_path, apkFileOrFolder),
27+
result = subprocess.check_output("{0} dump badging {1} | grep 'package:\|application-label:\|dkVersion:'".format(aapt_path, apkFileOrFolder),
2728
stderr=subprocess.STDOUT,
2829
shell=True)
2930

3031
if result:
3132
log.debug(result)
3233
infos = result.rstrip().split('\n')
3334
if infos:
34-
info0 = infos[0].split(' ')
35+
apk = {}
36+
for info in infos:
37+
if info.startswith("package:"):
38+
apk["package"] = info
39+
elif info.startswith("sdkVersion:"):
40+
apk["min"] = int(info.split("'")[1])
41+
elif info.startswith("maxSdkVersion:"):
42+
apk["max"] = int(info.split("'")[1])
43+
elif info.startswith("targetSdkVersion:"):
44+
apk["target"] = int(info.split("'")[1])
45+
elif info.startswith("application-label:"):
46+
apk["label"] = info.split("'")[1]
47+
48+
log.debug(apk)
49+
50+
info0 = apk['package'].split(' ')
3551

3652
if len(info0) > 3:
37-
38-
packName = info0[1][6:-1]
39-
versionCode = info0[2][13:-1]
40-
versionName = info0[3][13:-1]
41-
appName = infos[1][19:-1]
42-
43-
it = wf.add_item(title="{0} - {1}({2})".format(appName, versionName, versionCode), subtitle=packName, copytext=packName, arg=apkFileOrFolder, valid=True)
53+
54+
for info in info0:
55+
if info.startswith("name="):
56+
apk["packName"] = info.split("'")[1]
57+
elif info.startswith("versionCode="):
58+
apk["versionCode"] = info.split("'")[1]
59+
elif info.startswith("versionName="):
60+
apk["versionName"] = info.split("'")[1]
61+
62+
it = wf.add_item(title="{0} - {1}({2})".format(apk['label'], apk["versionName"], apk["versionCode"]), subtitle=apk["packName"], copytext=apk["packName"], arg=apkFileOrFolder, valid=True)
4463
it.setvar('apkFile', [apkFileOrFolder])
45-
shell_cmd = "{0} -s {1} shell dumpsys package {2} | grep 'versionCode\|versionName' | awk '{{print $1}}'".format(adb_path, serial, packName)
46-
47-
try:
48-
result = subprocess.check_output(shell_cmd, stderr=subprocess.STDOUT, shell=True)
49-
except subprocess.CalledProcessError as e:
50-
log.debug(e)
51-
if result:
52-
infos = result.rstrip().split('\n')
53-
log.debug(infos)
54-
it = wf.add_item(title="Current version - {0}({1})".format(infos[1][12:], infos[0][12:]), valid=False)
55-
mod = it.add_modifier('cmd', subtitle='Uninstall currently installed version first, then install selected apk file', valid=True)
56-
mod.setvar("function", "uninstall_app")
57-
mod.setvar("package", packName)
58-
59-
wf.setvar("pack_name", packName)
60-
wf.setvar("version_code", versionCode)
61-
wf.setvar("version_name", versionName)
62-
wf.setvar("app_name", appName)
63-
64-
65-
def showFolerInstallItems(wf):
64+
65+
installOptions = ""
66+
if "t" in arg:
67+
installOptions = installOptions + "t"
68+
69+
if "d" in arg:
70+
installOptions = installOptions + "d"
71+
72+
if "g" in arg:
73+
installOptions = installOptions + "g"
74+
75+
it.setvar('option', installOptions)
76+
77+
if apk.has_key('min'):
78+
it.add_modifier('cmd', "minSdkVersion {0}".format(apk["min"]))
79+
80+
if apk.has_key('max'):
81+
it.add_modifier('alt', "maxSdkVersion {0}".format(apk["max"]))
82+
83+
if apk.has_key('target'):
84+
it.add_modifier('ctrl', "targetSdkVersion {0}".format(apk["target"]))
85+
86+
if deviceApi and apk.has_key("min") and deviceApi < apk["min"]:
87+
wf.add_item(title="Incompatiable device", subtitle="current device api level is {1}, lower than apk minSdkVersion {0}, ".format(deviceApi, apk["min"]), icon=ICON_ERROR, valid=False)
88+
if deviceApi and apk.has_key("max") and deviceApi > apk["maxs"]:
89+
wf.add_item(title="Incompatiable device", subtitle="current device api level is {1}, higher than apk maxSdkVersion {0}, ".format(deviceApi, apk["max"]), icon=ICON_ERROR, valid=False)
90+
91+
if serial:
92+
shell_cmd = "{0} -s {1} shell dumpsys package {2} | grep 'versionCode\|versionName' | awk '{{print $1}}'".format(adb_path, serial, apk["packName"])
93+
94+
try:
95+
result = subprocess.check_output(shell_cmd, stderr=subprocess.STDOUT, shell=True)
96+
except subprocess.CalledProcessError as e:
97+
log.debug(e)
98+
if result:
99+
infos = result.rstrip().split('\n')
100+
log.debug(infos)
101+
it = wf.add_item(title="Current version - {0}({1})".format(infos[1][12:], infos[0][12:]), valid=False)
102+
mod = it.add_modifier('cmd', subtitle='Uninstall currently installed version first, then install selected apk file', valid=True)
103+
mod.setvar("function", "uninstall_app")
104+
mod.setvar("package", apk["packName"])
105+
106+
wf.setvar("pack_name", apk["packName"])
107+
wf.setvar("version_code", apk["versionCode"])
108+
wf.setvar("version_name", apk["versionName"])
109+
wf.setvar("app_name", apk['label'])
110+
111+
112+
def showFolerInstallItems():
66113
arg = wf.args[0].strip()
67114
apkFilesAll = []
68115
apkFileDirect = []
@@ -82,7 +129,7 @@ def main(wf):
82129

83130
fileCount = 1
84131
if os.path.isdir(apkFileOrFolder):
85-
apkFileAll, apkFileDirect = showFolerInstallItems(wf)
132+
apkFileAll, apkFileDirect = showFolerInstallItems()
86133
fileCount = len(apkFileAll)
87134
directFileCount = len(apkFileDirect)
88135
if fileCount > 0:
@@ -96,11 +143,11 @@ def main(wf):
96143
mod = it.add_modifier('cmd', subtitle='only install apks under root directory', valid=True)
97144
mod.setvar('apkFile', apkFileDirect)
98145
else:
99-
showApkInstallItems(wf)
146+
showApkInstallItems()
100147

101148
if fileCount == 0:
102149
wf.warn_empty(title="No APK files find under current path", subtitle=apkFileOrFolder)
103-
else:
150+
elif serial:
104151
installOptions = []
105152
if "t" not in arg:
106153
wf.add_item(title="Add 't' to allow test APK to be installed", icon=ICON_INFO, valid=False)
@@ -111,10 +158,11 @@ def main(wf):
111158
wf.add_item(title="Add 'd' to allow version code downgrade", icon=ICON_INFO, valid=False)
112159
else:
113160
installOptions.append("d")
114-
115-
if "g" not in arg:
161+
log.error("deviceApi")
162+
log.error(deviceApi)
163+
if "g" not in arg and int(deviceApi) > 22:
116164
wf.add_item(title="Add 'g' to grant all permissions listed in the app manifest", icon=ICON_INFO, valid=False)
117-
else:
165+
elif 'g' in arg and int(deviceApi) > 22:
118166
installOptions.append("g")
119167
wf.setvar("option", installOptions)
120168

0 commit comments

Comments
 (0)