diff --git a/pythonforandroid/bootstraps/common/build/src/main/java/org/kivy/android/PythonActivityUtil.java b/pythonforandroid/bootstraps/common/build/src/main/java/org/kivy/android/PythonActivityUtil.java deleted file mode 100644 index f94989b6e4..0000000000 --- a/pythonforandroid/bootstraps/common/build/src/main/java/org/kivy/android/PythonActivityUtil.java +++ /dev/null @@ -1,110 +0,0 @@ -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); - } - } - } - -} diff --git a/pythonforandroid/bootstraps/common/build/src/main/java/org/kivy/android/PythonUtil.java b/pythonforandroid/bootstraps/common/build/src/main/java/org/kivy/android/PythonUtil.java index 820dbbfeb2..d67570e18a 100644 --- a/pythonforandroid/bootstraps/common/build/src/main/java/org/kivy/android/PythonUtil.java +++ b/pythonforandroid/bootstraps/common/build/src/main/java/org/kivy/android/PythonUtil.java @@ -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"; @@ -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); + } + } + } } diff --git a/pythonforandroid/bootstraps/sdl2/build/src/main/java/org/kivy/android/PythonActivity.java b/pythonforandroid/bootstraps/sdl2/build/src/main/java/org/kivy/android/PythonActivity.java index 855598685c..a0d3ee0a2b 100644 --- a/pythonforandroid/bootstraps/sdl2/build/src/main/java/org/kivy/android/PythonActivity.java +++ b/pythonforandroid/bootstraps/sdl2/build/src/main/java/org/kivy/android/PythonActivity.java @@ -104,8 +104,7 @@ private class UnpackFilesTask extends AsyncTask { 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; } diff --git a/pythonforandroid/bootstraps/service_library/build/templates/Service.tmpl.java b/pythonforandroid/bootstraps/service_library/build/templates/Service.tmpl.java index 4f6f7d1e26..1f64060e3b 100644 --- a/pythonforandroid/bootstraps/service_library/build/templates/Service.tmpl.java +++ b/pythonforandroid/bootstraps/service_library/build/templates/Service.tmpl.java @@ -1,9 +1,6 @@ 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; @@ -11,22 +8,22 @@ 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); @@ -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); - } - } - } - } diff --git a/pythonforandroid/bootstraps/service_only/build/src/main/java/org/kivy/android/PythonActivity.java b/pythonforandroid/bootstraps/service_only/build/src/main/java/org/kivy/android/PythonActivity.java index 9d2686204b..8abff40bb6 100644 --- a/pythonforandroid/bootstraps/service_only/build/src/main/java/org/kivy/android/PythonActivity.java +++ b/pythonforandroid/bootstraps/service_only/build/src/main/java/org/kivy/android/PythonActivity.java @@ -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); diff --git a/pythonforandroid/bootstraps/webview/build/src/main/java/org/kivy/android/PythonActivity.java b/pythonforandroid/bootstraps/webview/build/src/main/java/org/kivy/android/PythonActivity.java index a9a3a538b0..4dcddec1f0 100644 --- a/pythonforandroid/bootstraps/webview/build/src/main/java/org/kivy/android/PythonActivity.java +++ b/pythonforandroid/bootstraps/webview/build/src/main/java/org/kivy/android/PythonActivity.java @@ -106,8 +106,7 @@ private class UnpackFilesTask extends AsyncTask { 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; }