Skip to content

Commit 0a76c4a

Browse files
authored
Merge pull request #2189 from AndreMiras/feature/share_unpack_data_java_code
PythonActivityUtil helper for unpacking data
2 parents c70432d + 5312595 commit 0a76c4a

File tree

7 files changed

+123
-279
lines changed

7 files changed

+123
-279
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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 {
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+
mActivity.runOnUiThread(new Runnable () {
32+
public void run() {
33+
Toast.makeText(mActivity, msg, Toast.LENGTH_LONG).show();
34+
}
35+
});
36+
37+
// Wait to show the error.
38+
synchronized (mActivity) {
39+
try {
40+
mActivity.wait(1000);
41+
} catch (InterruptedException e) {
42+
}
43+
}
44+
}
45+
46+
private void recursiveDelete(File f) {
47+
if (f.isDirectory()) {
48+
for (File r : f.listFiles()) {
49+
recursiveDelete(r);
50+
}
51+
}
52+
f.delete();
53+
}
54+
55+
public void unpackData(final String resource, File target) {
56+
57+
Log.v(TAG, "UNPACKING!!! " + resource + " " + target.getName());
58+
59+
// The version of data in memory and on disk.
60+
String dataVersion = mResourceManager.getString(resource + "_version");
61+
String diskVersion = null;
62+
63+
Log.v(TAG, "Data version is " + dataVersion);
64+
65+
// If no version, no unpacking is necessary.
66+
if (dataVersion == null) {
67+
return;
68+
}
69+
70+
// Check the current disk version, if any.
71+
String filesDir = target.getAbsolutePath();
72+
String diskVersionFn = filesDir + "/" + resource + ".version";
73+
74+
try {
75+
byte buf[] = new byte[64];
76+
InputStream is = new FileInputStream(diskVersionFn);
77+
int len = is.read(buf);
78+
diskVersion = new String(buf, 0, len);
79+
is.close();
80+
} catch (Exception e) {
81+
diskVersion = "";
82+
}
83+
84+
// If the disk data is out of date, extract it and write the version file.
85+
if (! dataVersion.equals(diskVersion)) {
86+
Log.v(TAG, "Extracting " + resource + " assets.");
87+
88+
recursiveDelete(target);
89+
target.mkdirs();
90+
91+
AssetExtract ae = new AssetExtract(mActivity);
92+
if (!ae.extractTar(resource + ".mp3", target.getAbsolutePath())) {
93+
toastError("Could not extract " + resource + " data.");
94+
}
95+
96+
try {
97+
// Write .nomedia.
98+
new File(target, ".nomedia").createNewFile();
99+
100+
// Write version file.
101+
FileOutputStream os = new FileOutputStream(diskVersionFn);
102+
os.write(dataVersion.getBytes());
103+
os.close();
104+
} catch (Exception e) {
105+
Log.w("python", e);
106+
}
107+
}
108+
}
109+
110+
}

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)