Skip to content

Commit 5182831

Browse files
committed
Merge pull request #568 from tito/sdl2-fix-activity-result-new-intent
sdl2/bootstrap: add support for onActivityResult and onNewIntent
2 parents 6301cff + 20246da commit 5182831

File tree

1 file changed

+89
-14
lines changed

1 file changed

+89
-14
lines changed

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

Lines changed: 89 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@
66
import java.io.FileOutputStream;
77
import java.io.FileWriter;
88
import java.io.File;
9+
import java.util.Collections;
10+
import java.util.Iterator;
11+
import java.util.List;
12+
import java.util.ArrayList;
913

1014
import android.view.ViewGroup;
1115
import android.view.SurfaceView;
1216
import android.app.Activity;
17+
import android.content.Intent;
1318
import android.util.Log;
1419
import android.widget.Toast;
1520
import android.os.Bundle;
@@ -30,7 +35,7 @@ public class PythonActivity extends SDLActivity {
3035
private static final String TAG = "PythonActivity";
3136

3237
public static PythonActivity mActivity = null;
33-
38+
3439
private ResourceManager resourceManager = null;
3540
private Bundle mMetaData = null;
3641
private PowerManager.WakeLock mWakeLock = null;
@@ -46,9 +51,9 @@ protected void onCreate(Bundle savedInstanceState) {
4651
Log.v(TAG, "About to do super onCreate");
4752
super.onCreate(savedInstanceState);
4853
Log.v(TAG, "Did super onCreate");
49-
54+
5055
this.mActivity = this;
51-
56+
5257
String mFilesDirectory = mActivity.getFilesDir().getAbsolutePath();
5358
Log.v(TAG, "Setting env vars for start.c and Python to use");
5459
SDLActivity.nativeSetEnv("ANDROID_PRIVATE", mFilesDirectory);
@@ -57,7 +62,7 @@ protected void onCreate(Bundle savedInstanceState) {
5762
SDLActivity.nativeSetEnv("PYTHONHOME", mFilesDirectory);
5863
SDLActivity.nativeSetEnv("", mFilesDirectory + ":" + mFilesDirectory + "/lib");
5964

60-
65+
6166
// nativeSetEnv("ANDROID_ARGUMENT", getFilesDir());
6267

6368
try {
@@ -79,7 +84,7 @@ protected void onCreate(Bundle savedInstanceState) {
7984
} catch (PackageManager.NameNotFoundException e) {
8085
}
8186
}
82-
87+
8388
// This is just overrides the normal SDLActivity, which just loads
8489
// SDL2 and main
8590
protected String[] getLibraries() {
@@ -92,16 +97,16 @@ protected String[] getLibraries() {
9297
"main"
9398
};
9499
}
95-
100+
96101
public void loadLibraries() {
97102
// AND: This should probably be replaced by a call to super
98103
for (String lib : getLibraries()) {
99104
System.loadLibrary(lib);
100105
}
101-
106+
102107
System.load(getFilesDir() + "/lib/python2.7/lib-dynload/_io.so");
103108
System.load(getFilesDir() + "/lib/python2.7/lib-dynload/unicodedata.so");
104-
109+
105110
try {
106111
// System.loadLibrary("ctypes");
107112
System.load(getFilesDir() + "/lib/python2.7/lib-dynload/_ctypes.so");
@@ -111,7 +116,7 @@ public void loadLibraries() {
111116

112117
Log.v(TAG, "Loaded everything!");
113118
}
114-
119+
115120
public void recursiveDelete(File f) {
116121
if (f.isDirectory()) {
117122
for (File r : f.listFiles()) {
@@ -143,15 +148,15 @@ public void run() {
143148
}
144149
}
145150
}
146-
151+
147152
public void unpackData(final String resource, File target) {
148-
153+
149154
Log.v(TAG, "UNPACKING!!! " + resource + " " + target.getName());
150-
155+
151156
// The version of data in memory and on disk.
152157
String data_version = resourceManager.getString(resource + "_version");
153158
String disk_version = null;
154-
159+
155160
Log.v(TAG, "Data version is " + data_version);
156161

157162
// If no version, no unpacking is necessary.
@@ -200,12 +205,82 @@ public void unpackData(final String resource, File target) {
200205
}
201206
}
202207
}
203-
208+
204209
public static ViewGroup getLayout() {
205210
return mLayout;
206211
}
207212

208213
public static SurfaceView getSurface() {
209214
return mSurface;
210215
}
216+
217+
//----------------------------------------------------------------------------
218+
// Listener interface for onNewIntent
219+
//
220+
221+
public interface NewIntentListener {
222+
void onNewIntent(Intent intent);
223+
}
224+
225+
private List<NewIntentListener> newIntentListeners = null;
226+
227+
public void registerNewIntentListener(NewIntentListener listener) {
228+
if ( this.newIntentListeners == null )
229+
this.newIntentListeners = Collections.synchronizedList(new ArrayList<NewIntentListener>());
230+
this.newIntentListeners.add(listener);
231+
}
232+
233+
public void unregisterNewIntentListener(NewIntentListener listener) {
234+
if ( this.newIntentListeners == null )
235+
return;
236+
this.newIntentListeners.remove(listener);
237+
}
238+
239+
@Override
240+
protected void onNewIntent(Intent intent) {
241+
if ( this.newIntentListeners == null )
242+
return;
243+
this.onResume();
244+
synchronized ( this.newIntentListeners ) {
245+
Iterator<NewIntentListener> iterator = this.newIntentListeners.iterator();
246+
while ( iterator.hasNext() ) {
247+
(iterator.next()).onNewIntent(intent);
248+
}
249+
}
250+
}
251+
252+
//----------------------------------------------------------------------------
253+
// Listener interface for onActivityResult
254+
//
255+
256+
public interface ActivityResultListener {
257+
void onActivityResult(int requestCode, int resultCode, Intent data);
258+
}
259+
260+
private List<ActivityResultListener> activityResultListeners = null;
261+
262+
public void registerActivityResultListener(ActivityResultListener listener) {
263+
if ( this.activityResultListeners == null )
264+
this.activityResultListeners = Collections.synchronizedList(new ArrayList<ActivityResultListener>());
265+
this.activityResultListeners.add(listener);
266+
}
267+
268+
public void unregisterActivityResultListener(ActivityResultListener listener) {
269+
if ( this.activityResultListeners == null )
270+
return;
271+
this.activityResultListeners.remove(listener);
272+
}
273+
274+
@Override
275+
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
276+
if ( this.activityResultListeners == null )
277+
return;
278+
this.onResume();
279+
synchronized ( this.activityResultListeners ) {
280+
Iterator<ActivityResultListener> iterator = this.activityResultListeners.iterator();
281+
while ( iterator.hasNext() )
282+
(iterator.next()).onActivityResult(requestCode, resultCode, intent);
283+
}
284+
}
285+
211286
}

0 commit comments

Comments
 (0)