Skip to content
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

PythonActivityUtil helper for unpacking data #2189

Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
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.util.Log;
import android.widget.Toast;

import org.renpy.android.ResourceManager;
import org.renpy.android.AssetExtract;


public class PythonActivityUtil {
private static final String TAG = "pythonactivityutil";
private ResourceManager mResourceManager = null;
private Activity mActivity = null;


public PythonActivityUtil(Activity activity, ResourceManager resourceManager) {
this.mActivity = activity;
this.mResourceManager = resourceManager;
}

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

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

private 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.
String dataVersion = mResourceManager.getString(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.");

recursiveDelete(target);
target.mkdirs();

AssetExtract ae = new AssetExtract(mActivity);
if (!ae.extractTar(resource + ".mp3", target.getAbsolutePath())) {
toastError("Could not extract " + resource + " data.");
}

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 @@ -14,8 +14,6 @@
import android.os.Process;
import java.io.File;

import org.kivy.android.PythonUtil;

//imports for channel definition
import android.app.NotificationManager;
import android.app.NotificationChannel;
Expand All @@ -33,7 +31,6 @@ public class PythonService extends Service implements Runnable {
private String pythonHome;
private String pythonPath;
private String serviceEntrypoint;
private boolean serviceStartAsForeground;
// Argument to pass to Python code,
private String pythonServiceArgument;

Expand Down Expand Up @@ -76,7 +73,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
pythonName = extras.getString("pythonName");
pythonHome = extras.getString("pythonHome");
pythonPath = extras.getString("pythonPath");
serviceStartAsForeground = (
boolean serviceStartAsForeground = (
extras.getString("serviceStartAsForeground").equals("true")
);
pythonServiceArgument = extras.getString("pythonServiceArgument");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ public boolean extractTar(String asset, String target) {

try {
out = new BufferedOutputStream(new FileOutputStream(path), 8192);
} catch ( FileNotFoundException e ) {
} catch ( SecurityException e ) { };
} catch ( FileNotFoundException | SecurityException e ) {}

if ( out == null ) {
Log.e("python", "could not open " + path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class Hardware {
// The context.
static Context context;
static View view;
public static final float defaultRv[] = { 0f, 0f, 0f };

/**
* Vibrate for s seconds.
Expand Down Expand Up @@ -107,8 +108,7 @@ public float[] readSensor() {
if (sSensorEvent != null) {
return sSensorEvent.values;
} else {
float rv[] = { 0f, 0f, 0f };
return rv;
return defaultRv;
}
}
}
Expand All @@ -127,9 +127,8 @@ public static void accelerometerEnable(boolean enable) {
accelerometerSensor.changeStatus(enable);
}
public static float[] accelerometerReading() {
float rv[] = { 0f, 0f, 0f };
if ( accelerometerSensor == null )
return rv;
return defaultRv;
return (float[]) accelerometerSensor.readSensor();
}
public static void orientationSensorEnable(boolean enable) {
Expand All @@ -138,9 +137,8 @@ public static void orientationSensorEnable(boolean enable) {
orientationSensor.changeStatus(enable);
}
public static float[] orientationSensorReading() {
float rv[] = { 0f, 0f, 0f };
if ( orientationSensor == null )
return rv;
return defaultRv;
return (float[]) orientationSensor.readSensor();
}
public static void magneticFieldSensorEnable(boolean enable) {
Expand All @@ -149,9 +147,8 @@ public static void magneticFieldSensorEnable(boolean enable) {
magneticFieldSensor.changeStatus(enable);
}
public static float[] magneticFieldSensorReading() {
float rv[] = { 0f, 0f, 0f };
if ( magneticFieldSensor == null )
return rv;
return defaultRv;
return (float[]) magneticFieldSensor.readSensor();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@

package org.kivy.android;

import java.io.InputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -35,11 +32,9 @@

import org.libsdl.app.SDLActivity;

import org.kivy.android.PythonUtil;
import org.kivy.android.launcher.Project;

import org.renpy.android.ResourceManager;
import org.renpy.android.AssetExtract;


public class PythonActivity extends SDLActivity {
Expand Down Expand Up @@ -78,15 +73,6 @@ public void loadLibraries() {
new File(getApplicationInfo().nativeLibraryDir));
}

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

/**
* Show an error using a toast. (Only makes sense from non-UI
* threads.)
Expand Down Expand Up @@ -115,7 +101,8 @@ 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");
unpackData("private", app_root_file);
PythonActivityUtil pythonActivityUtil = new PythonActivityUtil(mActivity, resourceManager);
pythonActivityUtil.unpackData("private", app_root_file);
return null;
}

Expand Down Expand Up @@ -222,63 +209,6 @@ protected void onProgressUpdate(Void... values) {
}
}

public void unpackData(final String resource, File target) {

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

// The version of data in memory and on disk.
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(this);
if (!ae.extractTar(resource + ".mp3", target.getAbsolutePath())) {
toastError("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);
}
}
}

public static ViewGroup getLayout() {
return mLayout;
}
Expand Down
Loading