2018-03-31 11:28:26 +02:00
|
|
|
package io.lbry.browser;
|
2017-08-13 03:24:00 +02:00
|
|
|
|
2017-08-17 16:31:42 +02:00
|
|
|
import android.app.Activity;
|
2017-08-13 03:24:00 +02:00
|
|
|
import android.app.Notification;
|
|
|
|
import android.app.PendingIntent;
|
2017-10-03 11:16:24 +02:00
|
|
|
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;
|
2017-08-17 16:31:42 +02:00
|
|
|
import android.util.Log;
|
2017-10-03 11:16:24 +02:00
|
|
|
|
|
|
|
import java.io.File;
|
2017-08-17 16:31:42 +02:00
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.FileWriter;
|
2017-10-03 11:16:24 +02:00
|
|
|
import java.io.InputStream;
|
|
|
|
|
2017-08-13 03:24:00 +02:00
|
|
|
import org.kivy.android.PythonService;
|
2017-08-17 16:31:42 +02:00
|
|
|
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
|
|
|
|
2017-08-17 16:31:42 +02:00
|
|
|
public static String TAG = "LbrynetService";
|
|
|
|
|
2017-08-22 23:03:05 +02:00
|
|
|
public static LbrynetService serviceInstance;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int startType() {
|
|
|
|
return START_STICKY;
|
|
|
|
}
|
|
|
|
|
2017-08-17 16:31:42 +02:00
|
|
|
@Override
|
|
|
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
2017-08-22 23:03:05 +02:00
|
|
|
// Assign service instance
|
|
|
|
serviceInstance = this;
|
|
|
|
|
2017-08-17 16:31:42 +02:00
|
|
|
// Extract files
|
|
|
|
File app_root_file = new File(getAppRoot());
|
|
|
|
unpackData("private", app_root_file);
|
2017-08-13 03:24:00 +02:00
|
|
|
|
2017-08-17 16:31:42 +02:00
|
|
|
if (intent == null) {
|
2017-10-03 11:16:24 +02:00
|
|
|
intent = ServiceHelper.buildIntent(
|
|
|
|
getApplicationContext(), "", LbrynetService.class, "lbrynetservice");
|
2017-08-17 16:31:42 +02:00
|
|
|
}
|
2017-08-13 03:24:00 +02:00
|
|
|
|
2017-08-17 16:31:42 +02:00
|
|
|
return super.onStartCommand(intent, flags, startId);
|
|
|
|
}
|
|
|
|
|
2017-08-18 00:06:57 +02:00
|
|
|
@Override
|
2017-08-22 23:03:05 +02:00
|
|
|
public void onDestroy() {
|
|
|
|
super.onDestroy();
|
|
|
|
serviceInstance = null;
|
2017-08-18 00:06:57 +02:00
|
|
|
}
|
2017-12-21 08:24:31 +01:00
|
|
|
|
2017-08-22 23:03:05 +02:00
|
|
|
public String getAppRoot() {
|
2017-08-17 16:31:42 +02:00
|
|
|
String app_root = getApplicationContext().getFilesDir().getAbsolutePath() + "/app";
|
|
|
|
return app_root;
|
|
|
|
}
|
|
|
|
|
2017-08-22 23:03:05 +02:00
|
|
|
public void recursiveDelete(File f) {
|
2017-08-17 16:31:42 +02:00
|
|
|
if (f.isDirectory()) {
|
|
|
|
for (File r : f.listFiles()) {
|
|
|
|
recursiveDelete(r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f.delete();
|
|
|
|
}
|
|
|
|
|
2017-08-22 23:03:05 +02:00
|
|
|
public void unpackData(final String resource, File target) {
|
2017-08-17 16:31:42 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|