From 3c5f90bf1472312aa1abdb4ead613501701b3a96 Mon Sep 17 00:00:00 2001 From: akinwale Date: Tue, 22 Aug 2017 22:03:05 +0100 Subject: [PATCH] Path utility class implementation (#9) * Paths utility class implementation * Updated buildozer requirements list * lbry repository in buildozer.spec requirements changed to https://github.com/lbryio/lbry --- buildozer.spec.sample | 2 +- buildozer.spec.travis | 2 +- .../java/io/lbry/lbrynet/LbrynetService.java | 31 ++++++++---- src/main/java/io/lbry/lbrynet/Utils.java | 47 +++++++++++++++++++ src/main/python/lbrynetservice.py | 27 +++++++---- 5 files changed, 87 insertions(+), 22 deletions(-) diff --git a/buildozer.spec.sample b/buildozer.spec.sample index 12afea56..9c5ecf6f 100644 --- a/buildozer.spec.sample +++ b/buildozer.spec.sample @@ -36,7 +36,7 @@ version = 0.1 # (list) Application requirements # comma seperated e.g. requirements = sqlite3,kivy -requirements = openssl, sqlite3, hostpython2, pycrypto==2.6.1, android, pyjnius, constantly, incremental, functools32, miniupnpc==1.9, gmpy==1.17, twisted==16.6.0, appdirs==1.4.3, argparse==1.2.1, docopt==0.6.2, base58==0.2.2, colorama==0.3.7, dnspython==1.12.0, ecdsa==0.13, envparse==0.2.0, jsonrpc==1.2, jsonrpclib==0.1.7, jsonschema==2.5.1, pbkdf2==1.3, pycrypto==2.6.1, pyyaml==3.12, qrcode==5.2.2, requests==2.9.1, txrequests==0.9.5, seccure==0.3.1.3, service_identity==16.0.0, six==1.9.0, slowaes==0.1a1, txJSON-RPC==0.5, wsgiref==0.1.2, zope.interface==4.3.3, protobuf==3.2.0, git+https://github.com/lbryio/lbryschema.git#egg=lbryschema, git+https://github.com/lbryio/lbryum.git#egg=lbryum, git+https://github.com/akinwale/lbry.git#egg=lbry +requirements = openssl, sqlite3, hostpython2, pycrypto==2.6.1, android, pyjnius, constantly, incremental, functools32, miniupnpc==1.9, gmpy==1.17, twisted==16.6.0, appdirs==1.4.3, argparse==1.2.1, docopt==0.6.2, base58==0.2.2, colorama==0.3.7, dnspython==1.12.0, ecdsa==0.13, envparse==0.2.0, jsonrpc==1.2, jsonrpclib==0.1.7, jsonschema==2.5.1, pbkdf2==1.3, pycrypto==2.6.1, pyyaml==3.12, qrcode==5.2.2, requests==2.9.1, txrequests==0.9.5, seccure==0.3.1.3, service_identity==16.0.0, six==1.9.0, slowaes==0.1a1, txJSON-RPC==0.5, wsgiref==0.1.2, zope.interface==4.3.3, protobuf==3.2.0, git+https://github.com/lbryio/lbryschema.git#egg=lbryschema, git+https://github.com/lbryio/lbryum.git#egg=lbryum, git+https://github.com/lbryio/lbry.git#egg=lbry # (str) Custom source folders for requirements # Sets custom source for any requirements with recipes diff --git a/buildozer.spec.travis b/buildozer.spec.travis index 5a6cb530..d4abac05 100644 --- a/buildozer.spec.travis +++ b/buildozer.spec.travis @@ -36,7 +36,7 @@ version = 0.1 # (list) Application requirements # comma seperated e.g. requirements = sqlite3,kivy -requirements = openssl, sqlite3, hostpython2, pycrypto==2.6.1, android, pyjnius, constantly, incremental, functools32, miniupnpc==1.9, gmpy==1.17, twisted==16.6.0, appdirs==1.4.3, argparse==1.2.1, docopt==0.6.2, base58==0.2.2, colorama==0.3.7, dnspython==1.12.0, ecdsa==0.13, envparse==0.2.0, jsonrpc==1.2, jsonrpclib==0.1.7, jsonschema==2.5.1, pbkdf2==1.3, pycrypto==2.6.1, pyyaml==3.12, qrcode==5.2.2, requests==2.9.1, txrequests==0.9.5, seccure==0.3.1.3, service_identity==16.0.0, six==1.9.0, slowaes==0.1a1, txJSON-RPC==0.5, wsgiref==0.1.2, zope.interface==4.3.3, protobuf==3.2.0, git+https://github.com/lbryio/lbryschema.git#egg=lbryschema, git+https://github.com/lbryio/lbryum.git#egg=lbryum, git+https://github.com/akinwale/lbry.git#egg=lbry +requirements = openssl, sqlite3, hostpython2, pycrypto==2.6.1, android, pyjnius, constantly, incremental, functools32, miniupnpc==1.9, gmpy==1.17, twisted==16.6.0, appdirs==1.4.3, argparse==1.2.1, docopt==0.6.2, base58==0.2.2, colorama==0.3.7, dnspython==1.12.0, ecdsa==0.13, envparse==0.2.0, jsonrpc==1.2, jsonrpclib==0.1.7, jsonschema==2.5.1, pbkdf2==1.3, pycrypto==2.6.1, pyyaml==3.12, qrcode==5.2.2, requests==2.9.1, txrequests==0.9.5, seccure==0.3.1.3, service_identity==16.0.0, six==1.9.0, slowaes==0.1a1, txJSON-RPC==0.5, wsgiref==0.1.2, zope.interface==4.3.3, protobuf==3.2.0, git+https://github.com/lbryio/lbryschema.git#egg=lbryschema, git+https://github.com/lbryio/lbryum.git#egg=lbryum, git+https://github.com/lbryio/lbry.git#egg=lbry # (str) Custom source folders for requirements # Sets custom source for any requirements with recipes diff --git a/src/main/java/io/lbry/lbrynet/LbrynetService.java b/src/main/java/io/lbry/lbrynet/LbrynetService.java index 60f70221..c3efb17a 100644 --- a/src/main/java/io/lbry/lbrynet/LbrynetService.java +++ b/src/main/java/io/lbry/lbrynet/LbrynetService.java @@ -29,8 +29,23 @@ public class LbrynetService extends PythonService { public static String TAG = "LbrynetService"; + public static LbrynetService serviceInstance; + + @Override + public int startType() { + return START_STICKY; + } + + @Override + public boolean canDisplayNotification() { + return false; + } + @Override public int onStartCommand(Intent intent, int flags, int startId) { + // Assign service instance + serviceInstance = this; + // Extract files File app_root_file = new File(getAppRoot()); unpackData("private", app_root_file); @@ -43,21 +58,17 @@ public class LbrynetService extends PythonService { } @Override - public boolean canDisplayNotification() { - return false; + public void onDestroy() { + super.onDestroy(); + serviceInstance = null; } - @Override - public int startType() { - return START_STICKY; - } - - private String getAppRoot() { + public String getAppRoot() { String app_root = getApplicationContext().getFilesDir().getAbsolutePath() + "/app"; return app_root; } - private void recursiveDelete(File f) { + public void recursiveDelete(File f) { if (f.isDirectory()) { for (File r : f.listFiles()) { recursiveDelete(r); @@ -66,7 +77,7 @@ public class LbrynetService extends PythonService { f.delete(); } - private void unpackData(final String resource, File target) { + public void unpackData(final String resource, File target) { Log.v(TAG, "UNPACKING!!! " + resource + " " + target.getName()); // The version of data in memory and on disk. diff --git a/src/main/java/io/lbry/lbrynet/Utils.java b/src/main/java/io/lbry/lbrynet/Utils.java index 438127e2..da4a6a7c 100644 --- a/src/main/java/io/lbry/lbrynet/Utils.java +++ b/src/main/java/io/lbry/lbrynet/Utils.java @@ -1,5 +1,8 @@ package io.lbry.lbrynet; +import android.content.Context; +import java.io.File; + public final class Utils { public static String getAndroidRelease() { return android.os.Build.VERSION.RELEASE; @@ -8,4 +11,48 @@ public final class Utils { public static int getAndroidSdk() { return android.os.Build.VERSION.SDK_INT; } + + public static String getFilesDir(Context context) { + return context.getFilesDir().getAbsolutePath(); + } + + public static String getAppInternalStorageDir(Context context) { + File[] dirs = context.getExternalFilesDirs(null); + return dirs[0].getAbsolutePath(); + } + + public static String getAppExternalStorageDir(Context context) { + File[] dirs = context.getExternalFilesDirs(null); + if (dirs.length > 1) { + return dirs[1].getAbsolutePath(); + } + return null; + } + + public static String getInternalStorageDir(Context context) { + String appInternal = getAppInternalStorageDir(context); + return writableRootForPath(appInternal); + } + + public static String getExternalStorageDir(Context context) { + String appExternal = getAppInternalStorageDir(context); + if (appExternal == null) { + return null; + } + + return writableRootForPath(appExternal); + } + + public static String writableRootForPath(String path) { + File file = new File(path); + while (file != null && file.canWrite()) { + File parent = file.getParentFile(); + if (parent != null && !parent.canWrite()) { + break; + } + file = parent; + } + + return file.getAbsolutePath(); + } } diff --git a/src/main/python/lbrynetservice.py b/src/main/python/lbrynetservice.py index 4479ecd3..bf6f7ea6 100644 --- a/src/main/python/lbrynetservice.py +++ b/src/main/python/lbrynetservice.py @@ -1,8 +1,23 @@ import platform +import ssl + +# Fixes / patches / overrides +# platform.platform() in libc_ver: IOError: [Errno 21] Is a directory +from jnius import autoclass +lbrynet_utils = autoclass('io.lbry.lbrynet.Utils') +service = autoclass('io.lbry.lbrynet.LbrynetService').serviceInstance +platform.platform = lambda: 'Android %s (API %s)' % (lbrynet_utils.getAndroidRelease(), lbrynet_utils.getAndroidSdk()) + +import lbrynet.androidhelpers +lbrynet.androidhelpers.paths.android_files_dir = lambda: lbrynet_utils.getFilesDir(service.getApplicationContext()) +lbrynet.androidhelpers.paths.android_internal_storage_dir = lambda: lbrynet_utils.getInternalStorageDir(service.getApplicationContext()) +lbrynet.androidhelpers.paths.android_external_storage_dir = lambda: lbrynet_utils.getExternalStorageDir(service.getApplicationContext()) +lbrynet.androidhelpers.paths.android_app_internal_storage_dir = lambda: lbrynet_utils.getAppInternalStorageDir(service.getApplicationContext()) +lbrynet.androidhelpers.paths.android_app_external_storage_dir = lambda: lbrynet_utils.getAppExternalStorageDir(service.getApplicationContext()) import logging.handlers -from lbrynet.core import log_support +from lbrynet.core import log_support from twisted.internet import defer, reactor from jsonrpc.proxy import JSONRPCProxy @@ -12,14 +27,6 @@ from lbrynet.core import utils, system_info from lbrynet.daemon.auth.client import LBRYAPIClient from lbrynet.daemon.DaemonServer import DaemonServer -import ssl - -# Fixes / patches / overrides -# platform.platform() in libc_ver: IOError: [Errno 21] Is a directory -from jnius import autoclass -util = autoclass('io.lbry.lbrynet.Utils') -platform.platform = lambda: 'Android %s (API %s)' % (util.getAndroidRelease(), util.getAndroidSdk()) - # https certificate verification # TODO: this is bad. Need to find a way to properly verify https requests def https_context(): @@ -47,7 +54,6 @@ def https_context(): requests.Session.request = partialmethod(default_request, verify=False) ''' - # LBRY Daemon log = logging.getLogger(__name__) @@ -89,5 +95,6 @@ def start_server_and_listen(use_auth, analytics_manager, max_tries=5): analytics_manager.send_server_startup_error(str(e)) reactor.fireSystemEvent("shutdown") + if __name__ == '__main__': start()