Skip to content

Commit e486766

Browse files
committed
PythonActivityUtil helper for unpacking data
This helper class shares code to be reused between the `PythonActivity` classes of the different bootstraps. Also adds few minor adjustments to existing classes.
1 parent eeed13c commit e486766

File tree

7 files changed

+127
-279
lines changed

7 files changed

+127
-279
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package org.kivy.android;
2+
3+
import java.io.InputStream;
4+
import java.io.FileInputStream;
5+
import java.io.FileOutputStream;
6+
import java.io.File;
7+
8+
import android.app.Activity;
9+
import android.util.Log;
10+
import android.widget.Toast;
11+
12+
import org.renpy.android.ResourceManager;
13+
import org.renpy.android.AssetExtract;
14+
15+
16+
public class PythonActivityUtil extends Activity {
17+
private static final String TAG = "pythonactivityutil";
18+
private ResourceManager mResourceManager = null;
19+
private Activity mActivity = null;
20+
21+
22+
public PythonActivityUtil(Activity activity, ResourceManager resourceManager) {
23+
this.mActivity = activity;
24+
this.mResourceManager = resourceManager;
25+
}
26+
27+
/**
28+
* Show an error using a toast. (Only makes sense from non-UI threads.)
29+
*/
30+
private void toastError(final String msg) {
31+
32+
// final Activity thisActivity = this;
33+
final Activity thisActivity = mActivity;
34+
35+
thisActivity.runOnUiThread(new Runnable () {
36+
public void run() {
37+
Toast.makeText(thisActivity, msg, Toast.LENGTH_LONG).show();
38+
}
39+
});
40+
41+
// Wait to show the error.
42+
synchronized (this) {
43+
try {
44+
this.wait(1000);
45+
} catch (InterruptedException e) {
46+
}
47+
}
48+
}
49+
50+
private void recursiveDelete(File f) {
51+
if (f.isDirectory()) {
52+
for (File r : f.listFiles()) {
53+
recursiveDelete(r);
54+
}
55+
}
56+
f.delete();
57+
}
58+
59+
public void unpackData(final String resource, File target) {
60+
61+
Log.v(TAG, "UNPACKING!!! " + resource + " " + target.getName());
62+
63+
// The version of data in memory and on disk.
64+
String dataVersion = mResourceManager.getString(resource + "_version");
65+
String diskVersion = null;
66+
67+
Log.v(TAG, "Data version is " + dataVersion);
68+
69+
// If no version, no unpacking is necessary.
70+
if (dataVersion == null) {
71+
return;
72+
}
73+
74+
// Check the current disk version, if any.
75+
String filesDir = target.getAbsolutePath();
76+
String diskVersionFn = filesDir + "/" + resource + ".version";
77+
78+
try {
79+
byte buf[] = new byte[64];
80+
InputStream is = new FileInputStream(diskVersionFn);
81+
int len = is.read(buf);
82+
diskVersion = new String(buf, 0, len);
83+
is.close();
84+
} catch (Exception e) {
85+
diskVersion = "";
86+
}
87+
88+
// If the disk data is out of date, extract it and write the version file.
89+
if (! dataVersion.equals(diskVersion)) {
90+
Log.v(TAG, "Extracting " + resource + " assets.");
91+
92+
recursiveDelete(target);
93+
target.mkdirs();
94+
95+
AssetExtract ae = new AssetExtract(mActivity);
96+
if (!ae.extractTar(resource + ".mp3", target.getAbsolutePath())) {
97+
toastError("Could not extract " + resource + " data.");
98+
}
99+
100+
try {
101+
// Write .nomedia.
102+
new File(target, ".nomedia").createNewFile();
103+
104+
// Write version file.
105+
FileOutputStream os = new FileOutputStream(diskVersionFn);
106+
os.write(dataVersion.getBytes());
107+
os.close();
108+
} catch (Exception e) {
109+
Log.w("python", e);
110+
}
111+
}
112+
}
113+
114+
}

pythonforandroid/bootstraps/common/build/src/main/java/org/kivy/android/PythonService.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
import android.os.Process;
1515
import java.io.File;
1616

17-
import org.kivy.android.PythonUtil;
18-
1917
//imports for channel definition
2018
import android.app.NotificationManager;
2119
import android.app.NotificationChannel;
@@ -33,7 +31,6 @@ public class PythonService extends Service implements Runnable {
3331
private String pythonHome;
3432
private String pythonPath;
3533
private String serviceEntrypoint;
36-
private boolean serviceStartAsForeground;
3734
// Argument to pass to Python code,
3835
private String pythonServiceArgument;
3936

@@ -76,7 +73,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
7673
pythonName = extras.getString("pythonName");
7774
pythonHome = extras.getString("pythonHome");
7875
pythonPath = extras.getString("pythonPath");
79-
serviceStartAsForeground = (
76+
boolean serviceStartAsForeground = (
8077
extras.getString("serviceStartAsForeground").equals("true")
8178
);
8279
pythonServiceArgument = extras.getString("pythonServiceArgument");

pythonforandroid/bootstraps/common/build/src/main/java/org/renpy/android/AssetExtract.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ public boolean extractTar(String asset, String target) {
7373

7474
try {
7575
out = new BufferedOutputStream(new FileOutputStream(path), 8192);
76-
} catch ( FileNotFoundException e ) {
77-
} catch ( SecurityException e ) { };
76+
} catch ( FileNotFoundException | SecurityException e ) {}
7877

7978
if ( out == null ) {
8079
Log.e("python", "could not open " + path);

pythonforandroid/bootstraps/common/build/src/main/java/org/renpy/android/Hardware.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class Hardware {
3131
// The context.
3232
static Context context;
3333
static View view;
34+
public static final float defaultRv[] = { 0f, 0f, 0f };
3435

3536
/**
3637
* Vibrate for s seconds.
@@ -107,8 +108,7 @@ public float[] readSensor() {
107108
if (sSensorEvent != null) {
108109
return sSensorEvent.values;
109110
} else {
110-
float rv[] = { 0f, 0f, 0f };
111-
return rv;
111+
return defaultRv;
112112
}
113113
}
114114
}
@@ -127,9 +127,8 @@ public static void accelerometerEnable(boolean enable) {
127127
accelerometerSensor.changeStatus(enable);
128128
}
129129
public static float[] accelerometerReading() {
130-
float rv[] = { 0f, 0f, 0f };
131130
if ( accelerometerSensor == null )
132-
return rv;
131+
return defaultRv;
133132
return (float[]) accelerometerSensor.readSensor();
134133
}
135134
public static void orientationSensorEnable(boolean enable) {
@@ -138,9 +137,8 @@ public static void orientationSensorEnable(boolean enable) {
138137
orientationSensor.changeStatus(enable);
139138
}
140139
public static float[] orientationSensorReading() {
141-
float rv[] = { 0f, 0f, 0f };
142140
if ( orientationSensor == null )
143-
return rv;
141+
return defaultRv;
144142
return (float[]) orientationSensor.readSensor();
145143
}
146144
public static void magneticFieldSensorEnable(boolean enable) {
@@ -149,9 +147,8 @@ public static void magneticFieldSensorEnable(boolean enable) {
149147
magneticFieldSensor.changeStatus(enable);
150148
}
151149
public static float[] magneticFieldSensorReading() {
152-
float rv[] = { 0f, 0f, 0f };
153150
if ( magneticFieldSensor == null )
154-
return rv;
151+
return defaultRv;
155152
return (float[]) magneticFieldSensor.readSensor();
156153
}
157154

pythonforandroid/bootstraps/sdl2/build/src/main/java/org/kivy/android/PythonActivity.java

Lines changed: 2 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
21
package org.kivy.android;
32

43
import java.io.InputStream;
5-
import java.io.FileInputStream;
6-
import java.io.FileOutputStream;
74
import java.io.FileWriter;
85
import java.io.File;
96
import java.io.IOException;
@@ -35,11 +32,9 @@
3532

3633
import org.libsdl.app.SDLActivity;
3734

38-
import org.kivy.android.PythonUtil;
3935
import org.kivy.android.launcher.Project;
4036

4137
import org.renpy.android.ResourceManager;
42-
import org.renpy.android.AssetExtract;
4338

4439

4540
public class PythonActivity extends SDLActivity {
@@ -78,15 +73,6 @@ public void loadLibraries() {
7873
new File(getApplicationInfo().nativeLibraryDir));
7974
}
8075

81-
public void recursiveDelete(File f) {
82-
if (f.isDirectory()) {
83-
for (File r : f.listFiles()) {
84-
recursiveDelete(r);
85-
}
86-
}
87-
f.delete();
88-
}
89-
9076
/**
9177
* Show an error using a toast. (Only makes sense from non-UI
9278
* threads.)
@@ -115,7 +101,8 @@ private class UnpackFilesTask extends AsyncTask<String, Void, String> {
115101
protected String doInBackground(String... params) {
116102
File app_root_file = new File(params[0]);
117103
Log.v(TAG, "Ready to unpack");
118-
unpackData("private", app_root_file);
104+
PythonActivityUtil pythonActivityUtil = new PythonActivityUtil(mActivity, resourceManager);
105+
pythonActivityUtil.unpackData("private", app_root_file);
119106
return null;
120107
}
121108

@@ -222,63 +209,6 @@ protected void onProgressUpdate(Void... values) {
222209
}
223210
}
224211

225-
public void unpackData(final String resource, File target) {
226-
227-
Log.v(TAG, "UNPACKING!!! " + resource + " " + target.getName());
228-
229-
// The version of data in memory and on disk.
230-
String data_version = resourceManager.getString(resource + "_version");
231-
String disk_version = null;
232-
233-
Log.v(TAG, "Data version is " + data_version);
234-
235-
// If no version, no unpacking is necessary.
236-
if (data_version == null) {
237-
return;
238-
}
239-
240-
// Check the current disk version, if any.
241-
String filesDir = target.getAbsolutePath();
242-
String disk_version_fn = filesDir + "/" + resource + ".version";
243-
244-
try {
245-
byte buf[] = new byte[64];
246-
InputStream is = new FileInputStream(disk_version_fn);
247-
int len = is.read(buf);
248-
disk_version = new String(buf, 0, len);
249-
is.close();
250-
} catch (Exception e) {
251-
disk_version = "";
252-
}
253-
254-
// If the disk data is out of date, extract it and write the
255-
// version file.
256-
// if (! data_version.equals(disk_version)) {
257-
if (! data_version.equals(disk_version)) {
258-
Log.v(TAG, "Extracting " + resource + " assets.");
259-
260-
recursiveDelete(target);
261-
target.mkdirs();
262-
263-
AssetExtract ae = new AssetExtract(this);
264-
if (!ae.extractTar(resource + ".mp3", target.getAbsolutePath())) {
265-
toastError("Could not extract " + resource + " data.");
266-
}
267-
268-
try {
269-
// Write .nomedia.
270-
new File(target, ".nomedia").createNewFile();
271-
272-
// Write version file.
273-
FileOutputStream os = new FileOutputStream(disk_version_fn);
274-
os.write(data_version.getBytes());
275-
os.close();
276-
} catch (Exception e) {
277-
Log.w("python", e);
278-
}
279-
}
280-
}
281-
282212
public static ViewGroup getLayout() {
283213
return mLayout;
284214
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy