Path utility class implementation #9

Merged
akinwale merged 3 commits from path_utils into master 2017-08-22 23:03:06 +02:00
3 changed files with 85 additions and 20 deletions
Showing only changes of commit 95e4dc9fbf - Show all commits

View file

@ -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.

View file

@ -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();
}
}

View file

@ -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()