Skip to content

Commit

Permalink
Fix Outdated SDLActivity
Browse files Browse the repository at this point in the history
While updating the SDL2 recipe to version 2.0.4 in PR kivy#713, the
SDLActivity was not updated accordingly.

For example, SDL 2.0.4 expects the Activity to have a method called
"openAPKExpansionInputStream" while this method was called
"openAPKExtensionInputStream" in older versions. SDL2 is not able to
read files when the method is missing.

Also, text input is partly broken as it is not possible to enter a
"space" character.
  • Loading branch information
Kim Rinnewitz committed Sep 3, 2018
1 parent 333a323 commit 41c9569
Showing 1 changed file with 136 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import android.app.*;
import android.content.*;
import android.text.InputType;
import android.view.*;
import android.view.inputmethod.BaseInputConnection;
import android.view.inputmethod.EditorInfo;
Expand All @@ -27,6 +28,7 @@
import android.graphics.drawable.Drawable;
import android.media.*;
import android.hardware.*;
import android.content.pm.ActivityInfo;

/**
SDL Activity
Expand Down Expand Up @@ -114,9 +116,9 @@ public static void initialize() {
// Setup
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.v("SDL", "Device: " + android.os.Build.DEVICE);
Log.v("SDL", "Model: " + android.os.Build.MODEL);
Log.v("SDL", "onCreate():" + mSingleton);
Log.v(TAG, "Device: " + android.os.Build.DEVICE);
Log.v(TAG, "Model: " + android.os.Build.MODEL);
Log.v(TAG, "onCreate(): " + mSingleton);
super.onCreate(savedInstanceState);

SDLActivity.initialize();
Expand Down Expand Up @@ -177,12 +179,23 @@ public void onClick(DialogInterface dialog,int id) {
mLayout.addView(mSurface);

setContentView(mLayout);

// Get filename from "Open with" of another application
Intent intent = getIntent();

if (intent != null && intent.getData() != null) {
String filename = intent.getData().getPath();
if (filename != null) {
Log.v(TAG, "Got filename: " + filename);
SDLActivity.onNativeDropFile(filename);
}
}
}

// Events
@Override
protected void onPause() {
Log.v("SDL", "onPause()");
Log.v(TAG, "onPause()");
super.onPause();

if (SDLActivity.mBrokenLibraries) {
Expand All @@ -194,7 +207,7 @@ protected void onPause() {

@Override
protected void onResume() {
Log.v("SDL", "onResume()");
Log.v(TAG, "onResume()");
super.onResume();

if (SDLActivity.mBrokenLibraries) {
Expand All @@ -208,7 +221,7 @@ protected void onResume() {
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
Log.v("SDL", "onWindowFocusChanged(): " + hasFocus);
Log.v(TAG, "onWindowFocusChanged(): " + hasFocus);

if (SDLActivity.mBrokenLibraries) {
return;
Expand All @@ -222,7 +235,7 @@ public void onWindowFocusChanged(boolean hasFocus) {

@Override
public void onLowMemory() {
Log.v("SDL", "onLowMemory()");
Log.v(TAG, "onLowMemory()");
super.onLowMemory();

if (SDLActivity.mBrokenLibraries) {
Expand All @@ -234,7 +247,7 @@ public void onLowMemory() {

@Override
protected void onDestroy() {
Log.v("SDL", "onDestroy()");
Log.v(TAG, "onDestroy()");

if (SDLActivity.mBrokenLibraries) {
super.onDestroy();
Expand All @@ -252,11 +265,11 @@ protected void onDestroy() {
try {
SDLActivity.mSDLThread.join();
} catch(Exception e) {
Log.v("SDL", "Problem stopping thread: " + e);
Log.v(TAG, "Problem stopping thread: " + e);
}
SDLActivity.mSDLThread = null;

//Log.v("SDL", "Finished waiting for SDL thread");
//Log.v(TAG, "Finished waiting for SDL thread");
}

super.onDestroy();
Expand Down Expand Up @@ -295,7 +308,7 @@ public static void handlePause() {
if (!SDLActivity.mIsPaused && SDLActivity.mIsSurfaceReady) {
SDLActivity.mIsPaused = true;
SDLActivity.nativePause();
mSurface.enableSensor(Sensor.TYPE_ACCELEROMETER, false);
mSurface.handlePause();
}
}

Expand Down Expand Up @@ -404,6 +417,7 @@ boolean sendCommand(int command, Object data) {
public static native void nativeQuit();
public static native void nativePause();
public static native void nativeResume();
public static native void onNativeDropFile(String filename);
public static native void onNativeResize(int x, int y, int format, float rate);
public static native int onNativePadDown(int device_id, int keycode);
public static native int onNativePadUp(int device_id, int keycode);
Expand Down Expand Up @@ -550,7 +564,7 @@ public static int audioInit(int sampleRate, boolean is16Bit, boolean isStereo, i
int audioFormat = is16Bit ? AudioFormat.ENCODING_PCM_16BIT : AudioFormat.ENCODING_PCM_8BIT;
int frameSize = (isStereo ? 2 : 1) * (is16Bit ? 2 : 1);

Log.v("SDL", "SDL audio: wanted " + (isStereo ? "stereo" : "mono") + " " + (is16Bit ? "16-bit" : "8-bit") + " " + (sampleRate / 1000f) + "kHz, " + desiredFrames + " frames buffer");
Log.v(TAG, "SDL audio: wanted " + (isStereo ? "stereo" : "mono") + " " + (is16Bit ? "16-bit" : "8-bit") + " " + (sampleRate / 1000f) + "kHz, " + desiredFrames + " frames buffer");

// Let the user pick a larger buffer if they really want -- but ye
// gods they probably shouldn't, the minimums are horrifyingly high
Expand All @@ -566,15 +580,15 @@ public static int audioInit(int sampleRate, boolean is16Bit, boolean isStereo, i
// Ref: http://developer.android.com/reference/android/media/AudioTrack.html#getState()

if (mAudioTrack.getState() != AudioTrack.STATE_INITIALIZED) {
Log.e("SDL", "Failed during initialization of Audio Track");
Log.e(TAG, "Failed during initialization of Audio Track");
mAudioTrack = null;
return -1;
}

mAudioTrack.play();
}

Log.v("SDL", "SDL audio: got " + ((mAudioTrack.getChannelCount() >= 2) ? "stereo" : "mono") + " " + ((mAudioTrack.getAudioFormat() == AudioFormat.ENCODING_PCM_16BIT) ? "16-bit" : "8-bit") + " " + (mAudioTrack.getSampleRate() / 1000f) + "kHz, " + desiredFrames + " frames buffer");
Log.v(TAG, "SDL audio: got " + ((mAudioTrack.getChannelCount() >= 2) ? "stereo" : "mono") + " " + ((mAudioTrack.getAudioFormat() == AudioFormat.ENCODING_PCM_16BIT) ? "16-bit" : "8-bit") + " " + (mAudioTrack.getSampleRate() / 1000f) + "kHz, " + desiredFrames + " frames buffer");

return 0;
}
Expand All @@ -594,7 +608,7 @@ public static void audioWriteShortBuffer(short[] buffer) {
// Nom nom
}
} else {
Log.w("SDL", "SDL audio: error return from write(short)");
Log.w(TAG, "SDL audio: error return from write(short)");
return;
}
}
Expand All @@ -615,7 +629,7 @@ public static void audioWriteByteBuffer(byte[] buffer) {
// Nom nom
}
} else {
Log.w("SDL", "SDL audio: error return from write(byte)");
Log.w(TAG, "SDL audio: error return from write(byte)");
return;
}
}
Expand Down Expand Up @@ -671,7 +685,7 @@ public static void pollInputDevices() {
public void keepActive() {
}

// APK extension files support
// APK expansion files support

/** com.android.vending.expansion.zipfile.ZipResourceFile object or null. */
private Object expansionFile;
Expand All @@ -680,16 +694,43 @@ public void keepActive() {
private Method expansionFileMethod;

/**
* This method is called by SDL using JNI.
* This method was called by SDL using JNI.
* @deprecated because of an incorrect name
*/
@Deprecated
public InputStream openAPKExtensionInputStream(String fileName) throws IOException {
return openAPKExpansionInputStream(fileName);
}

/**
* This method is called by SDL using JNI.
* @return an InputStream on success or null if no expansion file was used.
* @throws IOException on errors. Message is set for the SDL error message.
*/
public InputStream openAPKExpansionInputStream(String fileName) throws IOException {
// Get a ZipResourceFile representing a merger of both the main and patch files
if (expansionFile == null) {
Integer mainVersion = Integer.valueOf(nativeGetHint("SDL_ANDROID_APK_EXPANSION_MAIN_FILE_VERSION"));
Integer patchVersion = Integer.valueOf(nativeGetHint("SDL_ANDROID_APK_EXPANSION_PATCH_FILE_VERSION"));
String mainHint = nativeGetHint("SDL_ANDROID_APK_EXPANSION_MAIN_FILE_VERSION");
if (mainHint == null) {
return null; // no expansion use if no main version was set
}
String patchHint = nativeGetHint("SDL_ANDROID_APK_EXPANSION_PATCH_FILE_VERSION");
if (patchHint == null) {
return null; // no expansion use if no patch version was set
}

Integer mainVersion;
Integer patchVersion;
try {
mainVersion = Integer.valueOf(mainHint);
patchVersion = Integer.valueOf(patchHint);
} catch (NumberFormatException ex) {
ex.printStackTrace();
throw new IOException("No valid file versions set for APK expansion files", ex);
}

try {
// To avoid direct dependency on Google APK extension library that is
// To avoid direct dependency on Google APK expansion library that is
// not a part of Android SDK we access it using reflection
expansionFile = Class.forName("com.android.vending.expansion.zipfile.APKExpansionSupport")
.getMethod("getAPKExpansionZipFile", Context.class, int.class, int.class)
Expand All @@ -701,6 +742,7 @@ public InputStream openAPKExtensionInputStream(String fileName) throws IOExcepti
ex.printStackTrace();
expansionFile = null;
expansionFileMethod = null;
throw new IOException("Could not access APK expansion support library", ex);
}
}

Expand All @@ -709,12 +751,14 @@ public InputStream openAPKExtensionInputStream(String fileName) throws IOExcepti
try {
fileStream = (InputStream)expansionFileMethod.invoke(expansionFile, fileName);
} catch (Exception ex) {
// calling "getInputStream" failed
ex.printStackTrace();
fileStream = null;
throw new IOException("Could not open stream from APK expansion file", ex);
}

if (fileStream == null) {
throw new IOException();
// calling "getInputStream" was successful but null was returned
throw new IOException("Could not find path in APK expansion file");
}

return fileStream;
Expand Down Expand Up @@ -979,6 +1023,10 @@ public SDLSurface(Context context) {
mHeight = 1.0f;
}

public void handlePause() {
enableSensor(Sensor.TYPE_ACCELEROMETER, false);
}

public void handleResume() {
setFocusable(true);
setFocusableInTouchMode(true);
Expand Down Expand Up @@ -1063,7 +1111,43 @@ public void surfaceChanged(SurfaceHolder holder,
mWidth = width;
mHeight = height;
SDLActivity.onNativeResize(width, height, sdlFormat, mDisplay.getRefreshRate());
Log.v("SDL", "Window size:" + width + "x"+height);
Log.v("SDL", "Window size: " + width + "x" + height);


boolean skip = false;
int requestedOrientation = SDLActivity.mSingleton.getRequestedOrientation();

if (requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
{
// Accept any
}
else if (requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
{
if (mWidth > mHeight) {
skip = true;
}
} else if (requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
if (mWidth < mHeight) {
skip = true;
}
}

// Special Patch for Square Resolution: Black Berry Passport
if (skip) {
double min = Math.min(mWidth, mHeight);
double max = Math.max(mWidth, mHeight);

if (max / min < 1.20) {
Log.v("SDL", "Don't skip on such aspect-ratio. Could be a square resolution.");
skip = false;
}
}

if (skip) {
Log.v("SDL", "Skip .. Surface is not ready.");
return;
}


// Set mIsSurfaceReady to 'true' *before* making a call to handleResume
SDLActivity.mIsSurfaceReady = true;
Expand Down Expand Up @@ -1096,6 +1180,10 @@ public void run(){
}, "SDLThreadListener");
SDLActivity.mSDLThread.start();
}

if (SDLActivity.mHasFocus) {
SDLActivity.handleResume();
}
}

// unused
Expand Down Expand Up @@ -1171,6 +1259,11 @@ public boolean onTouch(View v, MotionEvent event) {
x = event.getX(i) / mWidth;
y = event.getY(i) / mHeight;
p = event.getPressure(i);
if (p > 1.0f) {
// may be larger than 1.0f on some devices
// see the documentation of getPressure(i)
p = 1.0f;
}
SDLActivity.onNativeTouch(touchDevId, pointerFingerId, action, x, y, p);
}
break;
Expand All @@ -1190,6 +1283,11 @@ public boolean onTouch(View v, MotionEvent event) {
x = event.getX(i) / mWidth;
y = event.getY(i) / mHeight;
p = event.getPressure(i);
if (p > 1.0f) {
// may be larger than 1.0f on some devices
// see the documentation of getPressure(i)
p = 1.0f;
}
SDLActivity.onNativeTouch(touchDevId, pointerFingerId, action, x, y, p);
break;

Expand All @@ -1199,6 +1297,11 @@ public boolean onTouch(View v, MotionEvent event) {
x = event.getX(i) / mWidth;
y = event.getY(i) / mHeight;
p = event.getPressure(i);
if (p > 1.0f) {
// may be larger than 1.0f on some devices
// see the documentation of getPressure(i)
p = 1.0f;
}
SDLActivity.onNativeTouch(touchDevId, pointerFingerId, MotionEvent.ACTION_UP, x, y, p);
}
break;
Expand Down Expand Up @@ -1319,6 +1422,7 @@ public boolean onKeyPreIme (int keyCode, KeyEvent event) {
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
ic = new SDLInputConnection(this, true);

outAttrs.inputType = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD;
outAttrs.imeOptions = EditorInfo.IME_FLAG_NO_EXTRACT_UI
| 33554432 /* API 11: EditorInfo.IME_FLAG_NO_FULLSCREEN */;

Expand Down Expand Up @@ -1443,7 +1547,13 @@ public void pollInputDevices() {
if (joystick == null) {
joystick = new SDLJoystick();
InputDevice joystickDevice = InputDevice.getDevice(deviceIds[i]);
if( (joystickDevice.getSources() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {

if (
(joystickDevice.getSources() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0
||
(joystickDevice.getSources() & InputDevice.SOURCE_CLASS_BUTTON) != 0
)
{
joystick.device_id = deviceIds[i];
joystick.name = joystickDevice.getName();
joystick.axes = new ArrayList<InputDevice.MotionRange>();
Expand Down Expand Up @@ -1539,7 +1649,6 @@ class SDLGenericMotionListener_API12 implements View.OnGenericMotionListener {
@Override
public boolean onGenericMotion(View v, MotionEvent event) {
float x, y;
int mouseButton;
int action;

switch ( event.getSource() ) {
Expand Down Expand Up @@ -1568,6 +1677,7 @@ public boolean onGenericMotion(View v, MotionEvent event) {
default:
break;
}
break;

default:
break;
Expand Down

0 comments on commit 41c9569

Please sign in to comment.