Skip to content

Move PythonActivityUtil.unpackData to PythonUtil.unpackData #2462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
package org.kivy.android;

import java.io.InputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.File;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.util.Log;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.regex.Pattern;

import org.renpy.android.AssetExtract;

public class PythonUtil {
private static final String TAG = "pythonutil";
Expand Down Expand Up @@ -77,4 +86,110 @@ public static void loadLibraries(File filesDir, File libsDir) {

Log.v(TAG, "Loaded everything!");
}

public static String getAppRoot(Context ctx) {
String appRoot = ctx.getFilesDir().getAbsolutePath() + "/app";
return appRoot;
}

public static String getResourceString(Context ctx, String name) {
// Taken from org.renpy.android.ResourceManager
Resources res = ctx.getResources();
int id = res.getIdentifier(name, "string", ctx.getPackageName());
return res.getString(id);
}

/**
* Show an error using a toast. (Only makes sense from non-UI threads.)
*/
protected static void toastError(final Activity activity, final String msg) {
activity.runOnUiThread(new Runnable () {
public void run() {
Toast.makeText(activity, msg, Toast.LENGTH_LONG).show();
}
});

// Wait to show the error.
synchronized (activity) {
try {
activity.wait(1000);
} catch (InterruptedException e) {
}
}
}

protected static void recursiveDelete(File f) {
if (f.isDirectory()) {
for (File r : f.listFiles()) {
recursiveDelete(r);
}
}
f.delete();
}

public static void unpackData(
Context ctx,
final String resource,
File target,
boolean cleanup_on_version_update) {

Log.v(TAG, "Unpacking " + resource + " " + target.getName());

// The version of data in memory and on disk.
String dataVersion = getResourceString(ctx, resource + "_version");
String diskVersion = null;

Log.v(TAG, "Data version is " + dataVersion);

// If no version, no unpacking is necessary.
if (dataVersion == null) {
return;
}

// Check the current disk version, if any.
String filesDir = target.getAbsolutePath();
String diskVersionFn = filesDir + "/" + resource + ".version";

try {
byte buf[] = new byte[64];
InputStream is = new FileInputStream(diskVersionFn);
int len = is.read(buf);
diskVersion = new String(buf, 0, len);
is.close();
} catch (Exception e) {
diskVersion = "";
}

// If the disk data is out of date, extract it and write the version file.
if (! dataVersion.equals(diskVersion)) {
Log.v(TAG, "Extracting " + resource + " assets.");

if (cleanup_on_version_update) {
recursiveDelete(target);
}
target.mkdirs();

AssetExtract ae = new AssetExtract(ctx);
if (!ae.extractTar(resource + ".mp3", target.getAbsolutePath())) {
String msg = "Could not extract " + resource + " data.";
if (ctx instanceof Activity) {
toastError((Activity)ctx, msg);
} else {
Log.v(TAG, msg);
}
}

try {
// Write .nomedia.
new File(target, ".nomedia").createNewFile();

// Write version file.
FileOutputStream os = new FileOutputStream(diskVersionFn);
os.write(dataVersion.getBytes());
os.close();
} catch (Exception e) {
Log.w("python", e);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ private class UnpackFilesTask extends AsyncTask<String, Void, String> {
protected String doInBackground(String... params) {
File app_root_file = new File(params[0]);
Log.v(TAG, "Ready to unpack");
PythonActivityUtil pythonActivityUtil = new PythonActivityUtil(mActivity, resourceManager);
pythonActivityUtil.unpackData("private", app_root_file);
PythonUtil.unpackData(mActivity, "private", app_root_file, true);
return null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
package {{ args.package }};

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

import android.os.Build;
import android.content.Intent;
import android.content.Context;
import android.content.res.Resources;
import android.util.Log;

import org.renpy.android.AssetExtract;
import org.kivy.android.PythonService;
import org.kivy.android.PythonUtil;

public class Service{{ name|capitalize }} extends PythonService {

private static final String TAG = "PythonService";

public static void prepare(Context ctx) {
String appRoot = getAppRoot(ctx);
String appRoot = PythonUtil.getAppRoot(ctx);
Log.v(TAG, "Ready to unpack");
File app_root_file = new File(appRoot);
unpackData(ctx, "private", app_root_file);
PythonUtil.unpackData(ctx, "private", app_root_file, false);
}

public static void start(Context ctx, String pythonServiceArgument) {
String appRoot = getAppRoot(ctx);
String appRoot = PythonUtil.getAppRoot(ctx);
Intent intent = new Intent(ctx, Service{{ name|capitalize }}.class);
intent.putExtra("androidPrivate", appRoot);
intent.putExtra("androidArgument", appRoot);
Expand All @@ -51,76 +48,4 @@ public static void start(Context ctx, String pythonServiceArgument) {
ctx.startService(intent);
{% endif %}
}

public static String getAppRoot(Context ctx) {
String app_root = ctx.getFilesDir().getAbsolutePath() + "/app";
return app_root;
}

public static String getResourceString(Context ctx, String name) {
// Taken from org.renpy.android.ResourceManager
Resources res = ctx.getResources();
int id = res.getIdentifier(name, "string", ctx.getPackageName());
return res.getString(id);
}

public static void unpackData(Context ctx, final String resource, File target) {
// Taken from PythonActivity class

Log.v(TAG, "UNPACKING!!! " + resource + " " + target.getName());

// The version of data in memory and on disk.
String data_version = getResourceString(ctx, 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.");

// Don't delete existing files
// recursiveDelete(target);
target.mkdirs();

AssetExtract ae = new AssetExtract(ctx);
if (!ae.extractTar(resource + ".mp3", target.getAbsolutePath())) {
Log.v(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);
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ protected void onCreate(Bundle savedInstanceState) {

Log.v(TAG, "Ready to unpack");
File app_root_file = new File(getAppRoot());
PythonActivityUtil pythonActivityUtil = new PythonActivityUtil(mActivity, resourceManager);
pythonActivityUtil.unpackData("private", app_root_file);
PythonUtil.unpackData(mActivity, "private", app_root_file, true);

Log.v(TAG, "About to do super onCreate");
super.onCreate(savedInstanceState);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ private class UnpackFilesTask extends AsyncTask<String, Void, String> {
protected String doInBackground(String... params) {
File app_root_file = new File(params[0]);
Log.v(TAG, "Ready to unpack");
PythonActivityUtil pythonActivityUtil = new PythonActivityUtil(mActivity, resourceManager);
pythonActivityUtil.unpackData("private", app_root_file);
PythonUtil.unpackData(mActivity, "private", app_root_file, true);
return null;
}

Expand Down