Skip to content

sdl2/bootstrap: add support for onActivityResult and onNewIntent #568

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 10, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.File;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;

import android.view.ViewGroup;
import android.view.SurfaceView;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
import android.os.Bundle;
Expand All @@ -30,7 +35,7 @@ public class PythonActivity extends SDLActivity {
private static final String TAG = "PythonActivity";

public static PythonActivity mActivity = null;

private ResourceManager resourceManager = null;
private Bundle mMetaData = null;
private PowerManager.WakeLock mWakeLock = null;
Expand All @@ -46,9 +51,9 @@ protected void onCreate(Bundle savedInstanceState) {
Log.v(TAG, "About to do super onCreate");
super.onCreate(savedInstanceState);
Log.v(TAG, "Did super onCreate");

this.mActivity = this;

String mFilesDirectory = mActivity.getFilesDir().getAbsolutePath();
Log.v(TAG, "Setting env vars for start.c and Python to use");
SDLActivity.nativeSetEnv("ANDROID_PRIVATE", mFilesDirectory);
Expand All @@ -57,7 +62,7 @@ protected void onCreate(Bundle savedInstanceState) {
SDLActivity.nativeSetEnv("PYTHONHOME", mFilesDirectory);
SDLActivity.nativeSetEnv("", mFilesDirectory + ":" + mFilesDirectory + "/lib");


// nativeSetEnv("ANDROID_ARGUMENT", getFilesDir());

try {
Expand All @@ -79,7 +84,7 @@ protected void onCreate(Bundle savedInstanceState) {
} catch (PackageManager.NameNotFoundException e) {
}
}

// This is just overrides the normal SDLActivity, which just loads
// SDL2 and main
protected String[] getLibraries() {
Expand All @@ -92,16 +97,16 @@ protected String[] getLibraries() {
"main"
};
}

public void loadLibraries() {
// AND: This should probably be replaced by a call to super
for (String lib : getLibraries()) {
System.loadLibrary(lib);
}

System.load(getFilesDir() + "/lib/python2.7/lib-dynload/_io.so");
System.load(getFilesDir() + "/lib/python2.7/lib-dynload/unicodedata.so");

try {
// System.loadLibrary("ctypes");
System.load(getFilesDir() + "/lib/python2.7/lib-dynload/_ctypes.so");
Expand All @@ -111,7 +116,7 @@ public void loadLibraries() {

Log.v(TAG, "Loaded everything!");
}

public void recursiveDelete(File f) {
if (f.isDirectory()) {
for (File r : f.listFiles()) {
Expand Down Expand Up @@ -143,15 +148,15 @@ public void run() {
}
}
}

public void unpackData(final String resource, File target) {

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

// The version of data in memory and on disk.
String data_version = resourceManager.getString(resource + "_version");
String disk_version = null;

Log.v(TAG, "Data version is " + data_version);

// If no version, no unpacking is necessary.
Expand Down Expand Up @@ -200,12 +205,82 @@ public void unpackData(final String resource, File target) {
}
}
}

public static ViewGroup getLayout() {
return mLayout;
}

public static SurfaceView getSurface() {
return mSurface;
}

//----------------------------------------------------------------------------
// Listener interface for onNewIntent
//

public interface NewIntentListener {
void onNewIntent(Intent intent);
}

private List<NewIntentListener> newIntentListeners = null;

public void registerNewIntentListener(NewIntentListener listener) {
if ( this.newIntentListeners == null )
this.newIntentListeners = Collections.synchronizedList(new ArrayList<NewIntentListener>());
this.newIntentListeners.add(listener);
}

public void unregisterNewIntentListener(NewIntentListener listener) {
if ( this.newIntentListeners == null )
return;
this.newIntentListeners.remove(listener);
}

@Override
protected void onNewIntent(Intent intent) {
if ( this.newIntentListeners == null )
return;
this.onResume();
synchronized ( this.newIntentListeners ) {
Iterator<NewIntentListener> iterator = this.newIntentListeners.iterator();
while ( iterator.hasNext() ) {
(iterator.next()).onNewIntent(intent);
}
}
}

//----------------------------------------------------------------------------
// Listener interface for onActivityResult
//

public interface ActivityResultListener {
void onActivityResult(int requestCode, int resultCode, Intent data);
}

private List<ActivityResultListener> activityResultListeners = null;

public void registerActivityResultListener(ActivityResultListener listener) {
if ( this.activityResultListeners == null )
this.activityResultListeners = Collections.synchronizedList(new ArrayList<ActivityResultListener>());
this.activityResultListeners.add(listener);
}

public void unregisterActivityResultListener(ActivityResultListener listener) {
if ( this.activityResultListeners == null )
return;
this.activityResultListeners.remove(listener);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if ( this.activityResultListeners == null )
return;
this.onResume();
synchronized ( this.activityResultListeners ) {
Iterator<ActivityResultListener> iterator = this.activityResultListeners.iterator();
while ( iterator.hasNext() )
(iterator.next()).onActivityResult(requestCode, resultCode, intent);
}
}

}