Skip to content

Commit 76c9ec2

Browse files
committed
Move the unpackFiles step into an AsyncTask so that we can show the loading screen while the unpack step is running.
1 parent 8113a1d commit 76c9ec2

File tree

2 files changed

+94
-69
lines changed

2 files changed

+94
-69
lines changed

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

Lines changed: 90 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import android.content.Intent;
1919
import android.util.Log;
2020
import android.widget.Toast;
21+
import android.os.AsyncTask;
2122
import android.os.Bundle;
2223
import android.os.PowerManager;
2324
import android.graphics.PixelFormat;
@@ -61,83 +62,15 @@ public String getAppRoot() {
6162
protected void onCreate(Bundle savedInstanceState) {
6263
Log.v(TAG, "My oncreate running");
6364
resourceManager = new ResourceManager(this);
64-
this.showLoadingScreen();
65-
File app_root_file = new File(getAppRoot());
66-
67-
Log.v(TAG, "Ready to unpack");
68-
unpackData("private", app_root_file);
6965

7066
Log.v(TAG, "About to do super onCreate");
7167
super.onCreate(savedInstanceState);
7268
Log.v(TAG, "Did super onCreate");
7369

7470
this.mActivity = this;
7571
this.showLoadingScreen();
76-
77-
// Figure out the directory where the game is. If the game was
78-
// given to us via an intent, then we use the scheme-specific
79-
// part of that intent to determine the file to launch. We
80-
// also use the android.txt file to determine the orientation.
81-
//
82-
// Otherwise, we use the public data, if we have it, or the
83-
// private data if we do not.
84-
String app_root_dir = getAppRoot();
85-
if (getIntent() != null && getIntent().getAction() != null &&
86-
getIntent().getAction().equals("org.kivy.LAUNCH")) {
87-
File path = new File(getIntent().getData().getSchemeSpecificPart());
88-
89-
Project p = Project.scanDirectory(path);
90-
SDLActivity.nativeSetEnv("ANDROID_ENTRYPOINT", p.dir + "/main.py");
91-
SDLActivity.nativeSetEnv("ANDROID_ARGUMENT", p.dir);
92-
SDLActivity.nativeSetEnv("ANDROID_APP_PATH", p.dir);
93-
94-
if (p != null) {
95-
if (p.landscape) {
96-
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
97-
} else {
98-
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
99-
}
100-
}
101-
102-
// Let old apps know they started.
103-
try {
104-
FileWriter f = new FileWriter(new File(path, ".launch"));
105-
f.write("started");
106-
f.close();
107-
} catch (IOException e) {
108-
// pass
109-
}
110-
} else {
111-
SDLActivity.nativeSetEnv("ANDROID_ENTRYPOINT", "main.pyo");
112-
SDLActivity.nativeSetEnv("ANDROID_ARGUMENT", app_root_dir);
113-
SDLActivity.nativeSetEnv("ANDROID_APP_PATH", app_root_dir);
114-
}
115-
116-
String mFilesDirectory = mActivity.getFilesDir().getAbsolutePath();
117-
Log.v(TAG, "Setting env vars for start.c and Python to use");
118-
SDLActivity.nativeSetEnv("ANDROID_PRIVATE", mFilesDirectory);
119-
SDLActivity.nativeSetEnv("PYTHONHOME", app_root_dir);
120-
SDLActivity.nativeSetEnv("PYTHONPATH", app_root_dir + ":" + app_root_dir + "/lib");
121-
SDLActivity.nativeSetEnv("PYTHONOPTIMIZE", "2");
12272

123-
try {
124-
Log.v(TAG, "Access to our meta-data...");
125-
this.mMetaData = this.mActivity.getPackageManager().getApplicationInfo(
126-
this.mActivity.getPackageName(), PackageManager.GET_META_DATA).metaData;
127-
128-
PowerManager pm = (PowerManager) this.mActivity.getSystemService(Context.POWER_SERVICE);
129-
if ( this.mMetaData.getInt("wakelock") == 1 ) {
130-
this.mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "Screen On");
131-
}
132-
if ( this.mMetaData.getInt("surface.transparent") != 0 ) {
133-
Log.v(TAG, "Surface will be transparent.");
134-
getSurface().setZOrderOnTop(true);
135-
getSurface().getHolder().setFormat(PixelFormat.TRANSPARENT);
136-
} else {
137-
Log.i(TAG, "Surface will NOT be transparent");
138-
}
139-
} catch (PackageManager.NameNotFoundException e) {
140-
}
73+
new UnpackFilesTask().execute(getAppRoot());
14174
}
14275

14376
public void loadLibraries() {
@@ -178,6 +111,94 @@ public void run() {
178111
}
179112
}
180113

114+
private class UnpackFilesTask extends AsyncTask<String, Void, String> {
115+
@Override
116+
protected String doInBackground(String... params) {
117+
File app_root_file = new File(params[0]);
118+
Log.v(TAG, "Ready to unpack");
119+
unpackData("private", app_root_file);
120+
return null;
121+
}
122+
123+
@Override
124+
protected void onPostExecute(String result) {
125+
// Figure out the directory where the game is. If the game was
126+
// given to us via an intent, then we use the scheme-specific
127+
// part of that intent to determine the file to launch. We
128+
// also use the android.txt file to determine the orientation.
129+
//
130+
// Otherwise, we use the public data, if we have it, or the
131+
// private data if we do not.
132+
mActivity.finishLoad();
133+
134+
String app_root_dir = getAppRoot();
135+
if (getIntent() != null && getIntent().getAction() != null &&
136+
getIntent().getAction().equals("org.kivy.LAUNCH")) {
137+
File path = new File(getIntent().getData().getSchemeSpecificPart());
138+
139+
Project p = Project.scanDirectory(path);
140+
SDLActivity.nativeSetEnv("ANDROID_ENTRYPOINT", p.dir + "/main.py");
141+
SDLActivity.nativeSetEnv("ANDROID_ARGUMENT", p.dir);
142+
SDLActivity.nativeSetEnv("ANDROID_APP_PATH", p.dir);
143+
144+
if (p != null) {
145+
if (p.landscape) {
146+
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
147+
} else {
148+
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
149+
}
150+
}
151+
152+
// Let old apps know they started.
153+
try {
154+
FileWriter f = new FileWriter(new File(path, ".launch"));
155+
f.write("started");
156+
f.close();
157+
} catch (IOException e) {
158+
// pass
159+
}
160+
} else {
161+
SDLActivity.nativeSetEnv("ANDROID_ENTRYPOINT", "main.pyo");
162+
SDLActivity.nativeSetEnv("ANDROID_ARGUMENT", app_root_dir);
163+
SDLActivity.nativeSetEnv("ANDROID_APP_PATH", app_root_dir);
164+
}
165+
166+
String mFilesDirectory = mActivity.getFilesDir().getAbsolutePath();
167+
Log.v(TAG, "Setting env vars for start.c and Python to use");
168+
SDLActivity.nativeSetEnv("ANDROID_PRIVATE", mFilesDirectory);
169+
SDLActivity.nativeSetEnv("PYTHONHOME", app_root_dir);
170+
SDLActivity.nativeSetEnv("PYTHONPATH", app_root_dir + ":" + app_root_dir + "/lib");
171+
SDLActivity.nativeSetEnv("PYTHONOPTIMIZE", "2");
172+
173+
try {
174+
Log.v(TAG, "Access to our meta-data...");
175+
mActivity.mMetaData = mActivity.getPackageManager().getApplicationInfo(
176+
mActivity.getPackageName(), PackageManager.GET_META_DATA).metaData;
177+
178+
PowerManager pm = (PowerManager) mActivity.getSystemService(Context.POWER_SERVICE);
179+
if ( mActivity.mMetaData.getInt("wakelock") == 1 ) {
180+
mActivity.mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "Screen On");
181+
}
182+
if ( mActivity.mMetaData.getInt("surface.transparent") != 0 ) {
183+
Log.v(TAG, "Surface will be transparent.");
184+
getSurface().setZOrderOnTop(true);
185+
getSurface().getHolder().setFormat(PixelFormat.TRANSPARENT);
186+
} else {
187+
Log.i(TAG, "Surface will NOT be transparent");
188+
}
189+
} catch (PackageManager.NameNotFoundException e) {
190+
}
191+
}
192+
193+
@Override
194+
protected void onPreExecute() {
195+
}
196+
197+
@Override
198+
protected void onProgressUpdate(Void... values) {
199+
}
200+
}
201+
181202
public void unpackData(final String resource, File target) {
182203

183204
Log.v(TAG, "UNPACKING!!! " + resource + " " + target.getName());

pythonforandroid/bootstraps/sdl2/build/src/org/libsdl/app/SDLActivity.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,11 @@ protected void onCreate(Bundle savedInstanceState) {
122122
SDLActivity.initialize();
123123
// So we can call stuff from static callbacks
124124
mSingleton = this;
125+
}
125126

127+
// We don't do this in onCreate because we unpack and load the app data on a thread
128+
// and we can't run setup tasks until that thread completes.
129+
protected void finishLoad() {
126130
// Load shared libraries
127131
String errorMsgBrokenLib = "";
128132
try {

0 commit comments

Comments
 (0)