Skip to content

Commit dc2806e

Browse files
authored
Move PythonActivityUtil.unpackData to PythonUtil.unpackData (#2462)
* Move PythonActivityUtil.unpackData to PythonUtil.unpackData * Move toastError and some fixes * declare activity final in toastError * Remove PythonActivityUtil
1 parent 5a51f2b commit dc2806e

File tree

6 files changed

+122
-195
lines changed

6 files changed

+122
-195
lines changed

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

Lines changed: 0 additions & 110 deletions
This file was deleted.

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

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
package org.kivy.android;
22

3+
import java.io.InputStream;
4+
import java.io.FileInputStream;
5+
import java.io.FileOutputStream;
36
import java.io.File;
47

8+
import android.app.Activity;
9+
import android.content.Context;
10+
import android.content.res.Resources;
511
import android.util.Log;
12+
import android.widget.Toast;
13+
614
import java.util.ArrayList;
715
import java.util.regex.Pattern;
816

17+
import org.renpy.android.AssetExtract;
918

1019
public class PythonUtil {
1120
private static final String TAG = "pythonutil";
@@ -77,4 +86,110 @@ public static void loadLibraries(File filesDir, File libsDir) {
7786

7887
Log.v(TAG, "Loaded everything!");
7988
}
89+
90+
public static String getAppRoot(Context ctx) {
91+
String appRoot = ctx.getFilesDir().getAbsolutePath() + "/app";
92+
return appRoot;
93+
}
94+
95+
public static String getResourceString(Context ctx, String name) {
96+
// Taken from org.renpy.android.ResourceManager
97+
Resources res = ctx.getResources();
98+
int id = res.getIdentifier(name, "string", ctx.getPackageName());
99+
return res.getString(id);
100+
}
101+
102+
/**
103+
* Show an error using a toast. (Only makes sense from non-UI threads.)
104+
*/
105+
protected static void toastError(final Activity activity, final String msg) {
106+
activity.runOnUiThread(new Runnable () {
107+
public void run() {
108+
Toast.makeText(activity, msg, Toast.LENGTH_LONG).show();
109+
}
110+
});
111+
112+
// Wait to show the error.
113+
synchronized (activity) {
114+
try {
115+
activity.wait(1000);
116+
} catch (InterruptedException e) {
117+
}
118+
}
119+
}
120+
121+
protected static void recursiveDelete(File f) {
122+
if (f.isDirectory()) {
123+
for (File r : f.listFiles()) {
124+
recursiveDelete(r);
125+
}
126+
}
127+
f.delete();
128+
}
129+
130+
public static void unpackData(
131+
Context ctx,
132+
final String resource,
133+
File target,
134+
boolean cleanup_on_version_update) {
135+
136+
Log.v(TAG, "Unpacking " + resource + " " + target.getName());
137+
138+
// The version of data in memory and on disk.
139+
String dataVersion = getResourceString(ctx, resource + "_version");
140+
String diskVersion = null;
141+
142+
Log.v(TAG, "Data version is " + dataVersion);
143+
144+
// If no version, no unpacking is necessary.
145+
if (dataVersion == null) {
146+
return;
147+
}
148+
149+
// Check the current disk version, if any.
150+
String filesDir = target.getAbsolutePath();
151+
String diskVersionFn = filesDir + "/" + resource + ".version";
152+
153+
try {
154+
byte buf[] = new byte[64];
155+
InputStream is = new FileInputStream(diskVersionFn);
156+
int len = is.read(buf);
157+
diskVersion = new String(buf, 0, len);
158+
is.close();
159+
} catch (Exception e) {
160+
diskVersion = "";
161+
}
162+
163+
// If the disk data is out of date, extract it and write the version file.
164+
if (! dataVersion.equals(diskVersion)) {
165+
Log.v(TAG, "Extracting " + resource + " assets.");
166+
167+
if (cleanup_on_version_update) {
168+
recursiveDelete(target);
169+
}
170+
target.mkdirs();
171+
172+
AssetExtract ae = new AssetExtract(ctx);
173+
if (!ae.extractTar(resource + ".mp3", target.getAbsolutePath())) {
174+
String msg = "Could not extract " + resource + " data.";
175+
if (ctx instanceof Activity) {
176+
toastError((Activity)ctx, msg);
177+
} else {
178+
Log.v(TAG, msg);
179+
}
180+
}
181+
182+
try {
183+
// Write .nomedia.
184+
new File(target, ".nomedia").createNewFile();
185+
186+
// Write version file.
187+
FileOutputStream os = new FileOutputStream(diskVersionFn);
188+
os.write(dataVersion.getBytes());
189+
os.close();
190+
} catch (Exception e) {
191+
Log.w("python", e);
192+
}
193+
}
194+
}
80195
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,7 @@ private class UnpackFilesTask extends AsyncTask<String, Void, String> {
104104
protected String doInBackground(String... params) {
105105
File app_root_file = new File(params[0]);
106106
Log.v(TAG, "Ready to unpack");
107-
PythonActivityUtil pythonActivityUtil = new PythonActivityUtil(mActivity, resourceManager);
108-
pythonActivityUtil.unpackData("private", app_root_file);
107+
PythonUtil.unpackData(mActivity, "private", app_root_file, true);
109108
return null;
110109
}
111110

Lines changed: 4 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,29 @@
11
package {{ args.package }};
22

33
import java.io.File;
4-
import java.io.FileInputStream;
5-
import java.io.FileOutputStream;
6-
import java.io.InputStream;
74

85
import android.os.Build;
96
import android.content.Intent;
107
import android.content.Context;
118
import android.content.res.Resources;
129
import android.util.Log;
1310

14-
import org.renpy.android.AssetExtract;
1511
import org.kivy.android.PythonService;
12+
import org.kivy.android.PythonUtil;
1613

1714
public class Service{{ name|capitalize }} extends PythonService {
1815

1916
private static final String TAG = "PythonService";
2017

2118
public static void prepare(Context ctx) {
22-
String appRoot = getAppRoot(ctx);
19+
String appRoot = PythonUtil.getAppRoot(ctx);
2320
Log.v(TAG, "Ready to unpack");
2421
File app_root_file = new File(appRoot);
25-
unpackData(ctx, "private", app_root_file);
22+
PythonUtil.unpackData(ctx, "private", app_root_file, false);
2623
}
2724

2825
public static void start(Context ctx, String pythonServiceArgument) {
29-
String appRoot = getAppRoot(ctx);
26+
String appRoot = PythonUtil.getAppRoot(ctx);
3027
Intent intent = new Intent(ctx, Service{{ name|capitalize }}.class);
3128
intent.putExtra("androidPrivate", appRoot);
3229
intent.putExtra("androidArgument", appRoot);
@@ -51,76 +48,4 @@ public static void start(Context ctx, String pythonServiceArgument) {
5148
ctx.startService(intent);
5249
{% endif %}
5350
}
54-
55-
public static String getAppRoot(Context ctx) {
56-
String app_root = ctx.getFilesDir().getAbsolutePath() + "/app";
57-
return app_root;
58-
}
59-
60-
public static String getResourceString(Context ctx, String name) {
61-
// Taken from org.renpy.android.ResourceManager
62-
Resources res = ctx.getResources();
63-
int id = res.getIdentifier(name, "string", ctx.getPackageName());
64-
return res.getString(id);
65-
}
66-
67-
public static void unpackData(Context ctx, final String resource, File target) {
68-
// Taken from PythonActivity class
69-
70-
Log.v(TAG, "UNPACKING!!! " + resource + " " + target.getName());
71-
72-
// The version of data in memory and on disk.
73-
String data_version = getResourceString(ctx, resource + "_version");
74-
String disk_version = null;
75-
76-
Log.v(TAG, "Data version is " + data_version);
77-
78-
// If no version, no unpacking is necessary.
79-
if (data_version == null) {
80-
return;
81-
}
82-
83-
// Check the current disk version, if any.
84-
String filesDir = target.getAbsolutePath();
85-
String disk_version_fn = filesDir + "/" + resource + ".version";
86-
87-
try {
88-
byte buf[] = new byte[64];
89-
InputStream is = new FileInputStream(disk_version_fn);
90-
int len = is.read(buf);
91-
disk_version = new String(buf, 0, len);
92-
is.close();
93-
} catch (Exception e) {
94-
disk_version = "";
95-
}
96-
97-
// If the disk data is out of date, extract it and write the
98-
// version file.
99-
// if (! data_version.equals(disk_version)) {
100-
if (! data_version.equals(disk_version)) {
101-
Log.v(TAG, "Extracting " + resource + " assets.");
102-
103-
// Don't delete existing files
104-
// recursiveDelete(target);
105-
target.mkdirs();
106-
107-
AssetExtract ae = new AssetExtract(ctx);
108-
if (!ae.extractTar(resource + ".mp3", target.getAbsolutePath())) {
109-
Log.v(TAG, "Could not extract " + resource + " data.");
110-
}
111-
112-
try {
113-
// Write .nomedia.
114-
new File(target, ".nomedia").createNewFile();
115-
116-
// Write version file.
117-
FileOutputStream os = new FileOutputStream(disk_version_fn);
118-
os.write(data_version.getBytes());
119-
os.close();
120-
} catch (Exception e) {
121-
Log.w("python", e);
122-
}
123-
}
124-
}
125-
12651
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ protected void onCreate(Bundle savedInstanceState) {
7474

7575
Log.v(TAG, "Ready to unpack");
7676
File app_root_file = new File(getAppRoot());
77-
PythonActivityUtil pythonActivityUtil = new PythonActivityUtil(mActivity, resourceManager);
78-
pythonActivityUtil.unpackData("private", app_root_file);
77+
PythonUtil.unpackData(mActivity, "private", app_root_file, true);
7978

8079
Log.v(TAG, "About to do super onCreate");
8180
super.onCreate(savedInstanceState);

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,7 @@ private class UnpackFilesTask extends AsyncTask<String, Void, String> {
106106
protected String doInBackground(String... params) {
107107
File app_root_file = new File(params[0]);
108108
Log.v(TAG, "Ready to unpack");
109-
PythonActivityUtil pythonActivityUtil = new PythonActivityUtil(mActivity, resourceManager);
110-
pythonActivityUtil.unpackData("private", app_root_file);
109+
PythonUtil.unpackData(mActivity, "private", app_root_file, true);
111110
return null;
112111
}
113112

0 commit comments

Comments
 (0)