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 new file mode 100644 index 0000000000..f94989b6e4 --- /dev/null +++ b/pythonforandroid/bootstraps/common/build/src/main/java/org/kivy/android/PythonActivityUtil.java @@ -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); + } + } + } + +} diff --git a/pythonforandroid/bootstraps/common/build/src/main/java/org/kivy/android/PythonService.java b/pythonforandroid/bootstraps/common/build/src/main/java/org/kivy/android/PythonService.java index 88063ebdd3..c9693c3eea 100644 --- a/pythonforandroid/bootstraps/common/build/src/main/java/org/kivy/android/PythonService.java +++ b/pythonforandroid/bootstraps/common/build/src/main/java/org/kivy/android/PythonService.java @@ -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; @@ -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; @@ -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"); diff --git a/pythonforandroid/bootstraps/common/build/src/main/java/org/renpy/android/AssetExtract.java b/pythonforandroid/bootstraps/common/build/src/main/java/org/renpy/android/AssetExtract.java index b6e565385d..e7383258f1 100644 --- a/pythonforandroid/bootstraps/common/build/src/main/java/org/renpy/android/AssetExtract.java +++ b/pythonforandroid/bootstraps/common/build/src/main/java/org/renpy/android/AssetExtract.java @@ -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); diff --git a/pythonforandroid/bootstraps/common/build/src/main/java/org/renpy/android/Hardware.java b/pythonforandroid/bootstraps/common/build/src/main/java/org/renpy/android/Hardware.java index 6253d4ceda..847576282e 100644 --- a/pythonforandroid/bootstraps/common/build/src/main/java/org/renpy/android/Hardware.java +++ b/pythonforandroid/bootstraps/common/build/src/main/java/org/renpy/android/Hardware.java @@ -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. @@ -107,8 +108,7 @@ public float[] readSensor() { if (sSensorEvent != null) { return sSensorEvent.values; } else { - float rv[] = { 0f, 0f, 0f }; - return rv; + return defaultRv; } } } @@ -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) { @@ -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) { @@ -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(); } 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 01ac4a858d..fe2586e7af 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 @@ -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; @@ -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 { @@ -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.) @@ -115,7 +101,8 @@ private class UnpackFilesTask extends AsyncTask { 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; } @@ -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; } 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 4254dd5cb4..ffffceeba3 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 @@ -1,11 +1,7 @@ - package org.kivy.android; import android.os.SystemClock; -import java.io.InputStream; -import java.io.FileInputStream; -import java.io.FileOutputStream; import java.io.File; import java.util.Collections; import java.util.Iterator; @@ -20,10 +16,8 @@ import android.os.PowerManager; import android.content.Context; import android.content.pm.PackageManager; -import org.kivy.android.PythonUtil; import org.renpy.android.ResourceManager; -import org.renpy.android.AssetExtract; public class PythonActivity extends Activity { // This activity is modified from a mixture of the SDLActivity and @@ -77,7 +71,8 @@ protected void onCreate(Bundle savedInstanceState) { Log.v(TAG, "Ready to unpack"); File app_root_file = new File(getAppRoot()); - unpackData("private", app_root_file); + PythonActivityUtil pythonActivityUtil = new PythonActivityUtil(mActivity, resourceManager); + pythonActivityUtil.unpackData("private", app_root_file); Log.v(TAG, "About to do super onCreate"); super.onCreate(savedInstanceState); @@ -179,95 +174,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.) - */ - public void toastError(final String msg) { - - final Activity thisActivity = this; - - runOnUiThread(new Runnable () { - public void run() { - Toast.makeText(thisActivity, msg, Toast.LENGTH_LONG).show(); - } - }); - - // Wait to show the error. - synchronized (this) { - try { - this.wait(1000); - } catch (InterruptedException e) { - } - } - } - - 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); - } - } - } - long lastBackClick = SystemClock.elapsedRealtime(); @Override public boolean onKeyDown(int keyCode, KeyEvent event) { 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 2b2dc9a7d9..b5e543c711 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 @@ -3,8 +3,6 @@ import android.os.SystemClock; import java.io.InputStream; -import java.io.FileInputStream; -import java.io.FileOutputStream; import java.io.File; import java.io.IOException; import java.util.Collections; @@ -33,12 +31,7 @@ import android.webkit.WebViewClient; import android.webkit.WebView; -import org.kivy.android.PythonUtil; - -import org.kivy.android.WebViewLoader; - import org.renpy.android.ResourceManager; -import org.renpy.android.AssetExtract; public class PythonActivity extends Activity { // This activity is modified from a mixture of the SDLActivity and @@ -106,7 +99,8 @@ private class UnpackFilesTask extends AsyncTask { 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; } @@ -225,95 +219,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.) - */ - public void toastError(final String msg) { - - final Activity thisActivity = this; - - runOnUiThread(new Runnable () { - public void run() { - Toast.makeText(thisActivity, msg, Toast.LENGTH_LONG).show(); - } - }); - - // Wait to show the error. - synchronized (this) { - try { - this.wait(1000); - } catch (InterruptedException e) { - } - } - } - - 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 void loadUrl(String url) { class LoadUrl implements Runnable { private String mUrl;