Skip to content

Instantly share code, notes, and snippets.

@tito
Created February 8, 2019 10:41
Show Gist options
  • Save tito/4f75385054222182e4f0cf56c90dcdf8 to your computer and use it in GitHub Desktop.
Save tito/4f75385054222182e4f0cf56c90dcdf8 to your computer and use it in GitHub Desktop.
Using P4A Hooks

P4A hook example with buildozer

  1. create a tools/hooks/
  2. put your hook.py + hook_something.py...
  3. in buildozer.spec, reference p4a.hook = tools/hooks/hook.py
  4. adjust
import sys
from glob import glob
from os.path import dirname, join, basename
curdir = dirname(__file__)
sys.path += [curdir]
files = glob(join(curdir, "hook_*.py"))
def after_apk_build(toolchain):
for filename in files:
modname = basename(filename)[:-3]
module = __import__(modname)
module.after_apk_build(toolchain)
"""
HOOK for Orange Beacon
"""
SERVICE_NAME = "net.ov.orangebeacon.lib.OBGService"
SERVICE_TPL = """
<service
android:name="{}"
android:exported="false"
android:label="Orange Beacon Service"/>
<receiver
android:name="net.ov.orangebeacon.lib.server.ServerUpdaterReceiver"
android:exported="false" />
<receiver android:name="net.ov.orangebeacon.lib.BootReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<activity android:name="net.ov.orangebeacon.lib.ClickListenerActivity" />
""".format(SERVICE_NAME)
def after_apk_build(toolchain):
# mutate AndroidManifest.xml
manifest_fn = "{}/src/main/AndroidManifest.xml".format(
toolchain._dist.dist_dir)
with open(manifest_fn, "r") as fd:
manifest = fd.read()
# check if the service exists
if SERVICE_NAME not in manifest:
# add service
manifest = manifest.replace("</application>",
SERVICE_TPL + "</application>")
# rewrite manifest
with open(manifest_fn, "w") as fd:
fd.write(manifest)
# check if the requestPermissions exists
pythonactivity_fn = "{}/src/main/java/org/kivy/android/PythonActivity.java".format(
toolchain._dist.dist_dir)
with open(pythonactivity_fn, "r") as fd:
originalContent = content = fd.read()
if "import android.support.v4.app.ActivityCompat" not in content:
# add request permissions callback
content = content.replace(
"public class PythonActivity",
("import android.support.v4.app.ActivityCompat;\n"
"public class PythonActivity"))
if "ActivityCompat.OnRequestPermissionsResultCallback" not in content:
content = content.replace(
"public void unpackData",
(
"static public ActivityCompat.OnRequestPermissionsResultCallback onRequestPermissionsResultCallback = null;\n"
" @Override\n"
" public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {\n"
" if (onRequestPermissionsResultCallback != null)\n"
" onRequestPermissionsResultCallback.onRequestPermissionsResult(requestCode, permissions, grantResults);\n"
" }\n"
" public void unpackData"
)
)
if originalContent != content:
with open(pythonactivity_fn, "w") as fd:
fd.write(content)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment