Skip to content

Commit 47edb65

Browse files
committed
Merge branch 'loadingscreen_fix' of https://github.com/kollivier/python-for-android into kollivier-loadingscreen_fix
2 parents 924d00b + 4b104ba commit 47edb65

File tree

2 files changed

+100
-69
lines changed

2 files changed

+100
-69
lines changed

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

Lines changed: 96 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,100 @@ 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+
// finishLoad called setContentView with the SDL view, which
135+
// removed the loading screen. However, we still need it to
136+
// show until the app is ready to render, so pop it back up
137+
// on top of the SDL view.
138+
this.showLoadingScreen();
139+
140+
String app_root_dir = getAppRoot();
141+
if (getIntent() != null && getIntent().getAction() != null &&
142+
getIntent().getAction().equals("org.kivy.LAUNCH")) {
143+
File path = new File(getIntent().getData().getSchemeSpecificPart());
144+
145+
Project p = Project.scanDirectory(path);
146+
SDLActivity.nativeSetEnv("ANDROID_ENTRYPOINT", p.dir + "/main.py");
147+
SDLActivity.nativeSetEnv("ANDROID_ARGUMENT", p.dir);
148+
SDLActivity.nativeSetEnv("ANDROID_APP_PATH", p.dir);
149+
150+
if (p != null) {
151+
if (p.landscape) {
152+
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
153+
} else {
154+
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
155+
}
156+
}
157+
158+
// Let old apps know they started.
159+
try {
160+
FileWriter f = new FileWriter(new File(path, ".launch"));
161+
f.write("started");
162+
f.close();
163+
} catch (IOException e) {
164+
// pass
165+
}
166+
} else {
167+
SDLActivity.nativeSetEnv("ANDROID_ENTRYPOINT", "main.pyo");
168+
SDLActivity.nativeSetEnv("ANDROID_ARGUMENT", app_root_dir);
169+
SDLActivity.nativeSetEnv("ANDROID_APP_PATH", app_root_dir);
170+
}
171+
172+
String mFilesDirectory = mActivity.getFilesDir().getAbsolutePath();
173+
Log.v(TAG, "Setting env vars for start.c and Python to use");
174+
SDLActivity.nativeSetEnv("ANDROID_PRIVATE", mFilesDirectory);
175+
SDLActivity.nativeSetEnv("PYTHONHOME", app_root_dir);
176+
SDLActivity.nativeSetEnv("PYTHONPATH", app_root_dir + ":" + app_root_dir + "/lib");
177+
SDLActivity.nativeSetEnv("PYTHONOPTIMIZE", "2");
178+
179+
try {
180+
Log.v(TAG, "Access to our meta-data...");
181+
mActivity.mMetaData = mActivity.getPackageManager().getApplicationInfo(
182+
mActivity.getPackageName(), PackageManager.GET_META_DATA).metaData;
183+
184+
PowerManager pm = (PowerManager) mActivity.getSystemService(Context.POWER_SERVICE);
185+
if ( mActivity.mMetaData.getInt("wakelock") == 1 ) {
186+
mActivity.mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "Screen On");
187+
}
188+
if ( mActivity.mMetaData.getInt("surface.transparent") != 0 ) {
189+
Log.v(TAG, "Surface will be transparent.");
190+
getSurface().setZOrderOnTop(true);
191+
getSurface().getHolder().setFormat(PixelFormat.TRANSPARENT);
192+
} else {
193+
Log.i(TAG, "Surface will NOT be transparent");
194+
}
195+
} catch (PackageManager.NameNotFoundException e) {
196+
}
197+
}
198+
199+
@Override
200+
protected void onPreExecute() {
201+
}
202+
203+
@Override
204+
protected void onProgressUpdate(Void... values) {
205+
}
206+
}
207+
181208
public void unpackData(final String resource, File target) {
182209

183210
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)