mobile-insight / mobileinsight-mobile

Mobile Network Intelligence Made Easy -- Android version of MobileInsight app
http://mobileinsight.net
Other
88 stars 54 forks source link

Cannot cast Android Java API in plugin #10

Closed zwyuan closed 7 years ago

zwyuan commented 7 years ago

The below demo code will lead to crash if saved as a plugin (main.mi2app):

#!/usr/bin/python

import os
import sys
import kivy
import jnius
from jnius import autoclass, cast

print "Start"

activity = cast("android.app.Activity", autoclass("org.kivy.android.PythonActivity").mActivity)
print activity.getFilesDir().getAbsolutePath()

print "End"

The crash log is attached here:

10-23 16:10:00.769 24857 24872 I python2.7: Phone model: Nexus 6P
10-23 16:10:00.769 24857 24872 I python2.7: Running app: /storage/emulated/0/mobileinsight/plugins/WiFi/main.mi2app
10-23 16:10:00.791 24857 24872 I python2.7: Start
10-23 16:10:00.877   646  1206 I slim_daemon: [QMILOC] requestTimeData: Sending Time Data to LocApi
10-23 16:10:00.916 24857 24872 F libc    : Fatal signal 11 (SIGSEGV), code 2, fault addr 0xd61e7d00 in tid 24872 (Thread-2)
10-23 16:10:00.916   361   361 W         : debuggerd: handling request: pid=24857 uid=10226 gid=10226 tid=24872
10-23 16:10:00.919 24875 24875 I debuggerd: type=1400 audit(0.0:426): avc: denied { read } for name="libpymodules.so" dev="dm-0" ino=426736 scontext=u:r:debuggerd:s0 tcontext=u:object_r:app_data_file:s0:c512,c768 tclass=file permissive=1
10-23 16:10:00.919 24875 24875 I debuggerd: type=1400 audit(0.0:427): avc: denied { open } for path="/data/data/net.mobileinsight.app/files/app/libpymodules.so" dev="dm-0" ino=426736 scontext=u:r:debuggerd:s0 tcontext=u:object_r:app_data_file:s0:c512,c768 tclass=file permissive=1
10-23 16:10:00.919 24875 24875 I debuggerd: type=1400 audit(0.0:428): avc: denied { getattr } for path="/data/data/net.mobileinsight.app/files/app/libpymodules.so" dev="dm-0" ino=426736 scontext=u:r:debuggerd:s0 tcontext=u:object_r:app_data_file:s0:c512,c768 tclass=file permissive=1
10-23 16:10:00.988 24875 24875 F DEBUG   : *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
10-23 16:10:00.988 24875 24875 F DEBUG   : Build fingerprint: 'google/angler/angler:7.1.2/N2G47H/3783593:user/release-keys'
10-23 16:10:00.989 24875 24875 F DEBUG   : Revision: '0'
10-23 16:10:00.989 24875 24875 F DEBUG   : ABI: 'arm'
10-23 16:10:00.989 24875 24875 F DEBUG   : pid: 24857, tid: 24872, name: Thread-2  >>> net.mobileinsight.app:pythonservice <<<
10-23 16:10:00.989 24875 24875 F DEBUG   : signal 11 (SIGSEGV), code 2 (SEGV_ACCERR), fault addr 0xd61e7d00
10-23 16:10:00.989 24875 24875 F DEBUG   :     r0 d5425d50  r1 d61e7d00  r2 d5be5145  r3 656e6f4f
10-23 16:10:00.989 24875 24875 F DEBUG   :     r4 d5db1ef0  r5 d5425d50  r6 d5d7c4b0  r7 d61e7d00
10-23 16:10:00.989 24875 24875 F DEBUG   :     r8 d5d44b04  r9 d621f90c  sl d55904b8  fp d6254fe4
10-23 16:10:00.989 24875 24875 F DEBUG   :     ip d5d44bb4  sp ee3eca80  lr d5bf3d23  pc d5c04fd6  cpsr 00010030
10-23 16:10:00.990 24875 24875 F DEBUG   :
10-23 16:10:00.990 24875 24875 F DEBUG   : backtrace:
10-23 16:10:00.990 24875 24875 F DEBUG   :     #00 pc 00224fd6  /data/data/net.mobileinsight.app/files/app/libpymodules.so
10-23 16:10:00.990 24875 24875 F DEBUG   :     #01 pc 00213d1f  /data/data/net.mobileinsight.app/files/app/libpymodules.so
10-23 16:10:01.804   361   361 W         : debuggerd: resuming target 24857
10-23 16:10:01.805  1001  1023 I BootReceiver: Copying /data/tombstones/tombstone_02 to DropBox (SYSTEM_TOMBSTONE)
10-23 16:10:01.833  1001  4160 I ActivityManager: Process net.mobileinsight.app:pythonservice (pid 24857) has died
10-23 16:10:01.834  1001  4160 D ActivityManager: cleanUpApplicationRecord -- 24857
10-23 16:10:01.834  1001  4160 W ActivityManager: Scheduling restart of crashed service net.mobileinsight.app/org.kivy.android.PythonService in 1000ms
10-23 16:10:01.837   610   610 I Zygote  : Process 24857 exited due to signal (11)
zwyuan commented 7 years ago

The bug is cause by using PythonActivity to cast an Android class. In fact, the plugins are run as PythonService in MobileInsight, therefore the type of pyActivity will be <type 'NoneType'> and cause error. Users should use PythonService to cast classes, see below an example:

#!/usr/bin/python

import os
import sys
from jnius import autoclass, cast

'''
Don't use the following block in a plugin:

    PythonActivity = autoclass('org.kivy.android.PythonActivity')
    pyActivity = PythonActivity.mActivity

The plugins are run as PythonService in MobileInsight, therefore
the type of pyActivity will be <type 'NoneType'> and cause error
'''

PythonService  = autoclass('org.kivy.android.PythonService')
pyService = PythonService.mService

Context = autoclass('android.content.Context')

mWifiManager = pyService.getSystemService(Context.WIFI_SERVICE)
mWifiInfo = mWifiManager.getConnectionInfo()

print mWifiInfo.getBSSID()
print mWifiInfo.getFrequency()

I will commit an example plugin for this.

zwyuan commented 7 years ago

See commit 222861d