lbry-android/src/main/java/io/lbry/browser/LbrynetService.java

137 lines
4 KiB
Java
Raw Normal View History

package io.lbry.browser;
2017-08-13 03:24:00 +02:00
import android.app.Activity;
2017-08-13 03:24:00 +02:00
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.Context;
import android.os.Bundle;
2017-12-21 08:24:31 +01:00
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStream;
2017-08-13 03:24:00 +02:00
import org.kivy.android.PythonService;
import org.renpy.android.AssetExtract;
import org.renpy.android.ResourceManager;
2017-08-13 03:24:00 +02:00
/**
* This service class is based on the auto-generated P4A service class
* which changes the service start type to START_STICKY and lets it run
* properly as a background service.
*
* @author akinwale
* @version 0.1
*/
public class LbrynetService extends PythonService {
2017-12-21 08:24:31 +01:00
public static String TAG = "LbrynetService";
public static LbrynetService serviceInstance;
@Override
public int startType() {
return START_STICKY;
}
@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);
2017-08-13 03:24:00 +02:00
if (intent == null) {
intent = ServiceHelper.buildIntent(
getApplicationContext(), "", LbrynetService.class, "lbrynetservice");
}
2017-08-13 03:24:00 +02:00
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
serviceInstance = null;
}
2017-12-21 08:24:31 +01:00
public String getAppRoot() {
String app_root = getApplicationContext().getFilesDir().getAbsolutePath() + "/app";
return app_root;
}
public void recursiveDelete(File f) {
if (f.isDirectory()) {
for (File r : f.listFiles()) {
recursiveDelete(r);
}
}
f.delete();
}
public void unpackData(final String resource, File target) {
Log.v(TAG, "UNPACKING!!! " + resource + " " + target.getName());
// The version of data in memory and on disk.
ResourceManager resourceManager = new ResourceManager(getApplicationContext());
String data_version = resourceManager.getString(resource + "_version");
String disk_version = null;
Log.v(TAG, "Data version is " + data_version);
// If no version, no unpacking is necessary.
if (data_version == null) {
return;
}
// Check the current disk version, if any.
String filesDir = target.getAbsolutePath();
String disk_version_fn = filesDir + "/" + resource + ".version";
try {
byte buf[] = new byte[64];
InputStream is = new FileInputStream(disk_version_fn);
int len = is.read(buf);
disk_version = new String(buf, 0, len);
is.close();
} catch (Exception e) {
disk_version = "";
}
// If the disk data is out of date, extract it and write the
// version file.
// if (! data_version.equals(disk_version)) {
if (! data_version.equals(disk_version)) {
Log.v(TAG, "Extracting " + resource + " assets.");
recursiveDelete(target);
target.mkdirs();
AssetExtract ae = new AssetExtract(getApplicationContext());
if (!ae.extractTar(resource + ".mp3", target.getAbsolutePath())) {
//toastError("Could not extract " + resource + " data.");
Log.e(TAG, "Could not extract " + resource + " data.");
}
try {
// Write .nomedia.
new File(target, ".nomedia").createNewFile();
// Write version file.
FileOutputStream os = new FileOutputStream(disk_version_fn);
os.write(data_version.getBytes());
os.close();
} catch (Exception e) {
Log.w("python", e);
}
}
}
}