Skip to content

Commit 6f2302d

Browse files
author
Kim Rinnewitz
committed
Fix Outdated SDLActivity
While updating the SDL2 recipe to version 2.0.4 in PR #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.
1 parent 0ac1013 commit 6f2302d

File tree

1 file changed

+136
-26
lines changed
  • pythonforandroid/bootstraps/sdl2/build/src/main/java/org/libsdl/app

1 file changed

+136
-26
lines changed

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

Lines changed: 136 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import android.app.*;
1313
import android.content.*;
14+
import android.text.InputType;
1415
import android.view.*;
1516
import android.view.inputmethod.BaseInputConnection;
1617
import android.view.inputmethod.EditorInfo;
@@ -27,6 +28,7 @@
2728
import android.graphics.drawable.Drawable;
2829
import android.media.*;
2930
import android.hardware.*;
31+
import android.content.pm.ActivityInfo;
3032

3133
/**
3234
SDL Activity
@@ -114,9 +116,9 @@ public static void initialize() {
114116
// Setup
115117
@Override
116118
protected void onCreate(Bundle savedInstanceState) {
117-
Log.v("SDL", "Device: " + android.os.Build.DEVICE);
118-
Log.v("SDL", "Model: " + android.os.Build.MODEL);
119-
Log.v("SDL", "onCreate():" + mSingleton);
119+
Log.v(TAG, "Device: " + android.os.Build.DEVICE);
120+
Log.v(TAG, "Model: " + android.os.Build.MODEL);
121+
Log.v(TAG, "onCreate(): " + mSingleton);
120122
super.onCreate(savedInstanceState);
121123

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

179181
setContentView(mLayout);
182+
183+
// Get filename from "Open with" of another application
184+
Intent intent = getIntent();
185+
186+
if (intent != null && intent.getData() != null) {
187+
String filename = intent.getData().getPath();
188+
if (filename != null) {
189+
Log.v(TAG, "Got filename: " + filename);
190+
SDLActivity.onNativeDropFile(filename);
191+
}
192+
}
180193
}
181194

182195
// Events
183196
@Override
184197
protected void onPause() {
185-
Log.v("SDL", "onPause()");
198+
Log.v(TAG, "onPause()");
186199
super.onPause();
187200

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

195208
@Override
196209
protected void onResume() {
197-
Log.v("SDL", "onResume()");
210+
Log.v(TAG, "onResume()");
198211
super.onResume();
199212

200213
if (SDLActivity.mBrokenLibraries) {
@@ -208,7 +221,7 @@ protected void onResume() {
208221
@Override
209222
public void onWindowFocusChanged(boolean hasFocus) {
210223
super.onWindowFocusChanged(hasFocus);
211-
Log.v("SDL", "onWindowFocusChanged(): " + hasFocus);
224+
Log.v(TAG, "onWindowFocusChanged(): " + hasFocus);
212225

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

223236
@Override
224237
public void onLowMemory() {
225-
Log.v("SDL", "onLowMemory()");
238+
Log.v(TAG, "onLowMemory()");
226239
super.onLowMemory();
227240

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

235248
@Override
236249
protected void onDestroy() {
237-
Log.v("SDL", "onDestroy()");
250+
Log.v(TAG, "onDestroy()");
238251

239252
if (SDLActivity.mBrokenLibraries) {
240253
super.onDestroy();
@@ -252,11 +265,11 @@ protected void onDestroy() {
252265
try {
253266
SDLActivity.mSDLThread.join();
254267
} catch(Exception e) {
255-
Log.v("SDL", "Problem stopping thread: " + e);
268+
Log.v(TAG, "Problem stopping thread: " + e);
256269
}
257270
SDLActivity.mSDLThread = null;
258271

259-
//Log.v("SDL", "Finished waiting for SDL thread");
272+
//Log.v(TAG, "Finished waiting for SDL thread");
260273
}
261274

262275
super.onDestroy();
@@ -295,7 +308,7 @@ public static void handlePause() {
295308
if (!SDLActivity.mIsPaused && SDLActivity.mIsSurfaceReady) {
296309
SDLActivity.mIsPaused = true;
297310
SDLActivity.nativePause();
298-
mSurface.enableSensor(Sensor.TYPE_ACCELEROMETER, false);
311+
mSurface.handlePause();
299312
}
300313
}
301314

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

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

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

568582
if (mAudioTrack.getState() != AudioTrack.STATE_INITIALIZED) {
569-
Log.e("SDL", "Failed during initialization of Audio Track");
583+
Log.e(TAG, "Failed during initialization of Audio Track");
570584
mAudioTrack = null;
571585
return -1;
572586
}
573587

574588
mAudioTrack.play();
575589
}
576590

577-
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");
591+
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");
578592

579593
return 0;
580594
}
@@ -594,7 +608,7 @@ public static void audioWriteShortBuffer(short[] buffer) {
594608
// Nom nom
595609
}
596610
} else {
597-
Log.w("SDL", "SDL audio: error return from write(short)");
611+
Log.w(TAG, "SDL audio: error return from write(short)");
598612
return;
599613
}
600614
}
@@ -615,7 +629,7 @@ public static void audioWriteByteBuffer(byte[] buffer) {
615629
// Nom nom
616630
}
617631
} else {
618-
Log.w("SDL", "SDL audio: error return from write(byte)");
632+
Log.w(TAG, "SDL audio: error return from write(byte)");
619633
return;
620634
}
621635
}
@@ -671,7 +685,7 @@ public static void pollInputDevices() {
671685
public void keepActive() {
672686
}
673687

674-
// APK extension files support
688+
// APK expansion files support
675689

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

682696
/**
683-
* This method is called by SDL using JNI.
697+
* This method was called by SDL using JNI.
698+
* @deprecated because of an incorrect name
684699
*/
700+
@Deprecated
685701
public InputStream openAPKExtensionInputStream(String fileName) throws IOException {
702+
return openAPKExpansionInputStream(fileName);
703+
}
704+
705+
/**
706+
* This method is called by SDL using JNI.
707+
* @return an InputStream on success or null if no expansion file was used.
708+
* @throws IOException on errors. Message is set for the SDL error message.
709+
*/
710+
public InputStream openAPKExpansionInputStream(String fileName) throws IOException {
686711
// Get a ZipResourceFile representing a merger of both the main and patch files
687712
if (expansionFile == null) {
688-
Integer mainVersion = Integer.valueOf(nativeGetHint("SDL_ANDROID_APK_EXPANSION_MAIN_FILE_VERSION"));
689-
Integer patchVersion = Integer.valueOf(nativeGetHint("SDL_ANDROID_APK_EXPANSION_PATCH_FILE_VERSION"));
713+
String mainHint = nativeGetHint("SDL_ANDROID_APK_EXPANSION_MAIN_FILE_VERSION");
714+
if (mainHint == null) {
715+
return null; // no expansion use if no main version was set
716+
}
717+
String patchHint = nativeGetHint("SDL_ANDROID_APK_EXPANSION_PATCH_FILE_VERSION");
718+
if (patchHint == null) {
719+
return null; // no expansion use if no patch version was set
720+
}
721+
722+
Integer mainVersion;
723+
Integer patchVersion;
724+
try {
725+
mainVersion = Integer.valueOf(mainHint);
726+
patchVersion = Integer.valueOf(patchHint);
727+
} catch (NumberFormatException ex) {
728+
ex.printStackTrace();
729+
throw new IOException("No valid file versions set for APK expansion files", ex);
730+
}
690731

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

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

716759
if (fileStream == null) {
717-
throw new IOException();
760+
// calling "getInputStream" was successful but null was returned
761+
throw new IOException("Could not find path in APK expansion file");
718762
}
719763

720764
return fileStream;
@@ -979,6 +1023,10 @@ public SDLSurface(Context context) {
9791023
mHeight = 1.0f;
9801024
}
9811025

1026+
public void handlePause() {
1027+
enableSensor(Sensor.TYPE_ACCELEROMETER, false);
1028+
}
1029+
9821030
public void handleResume() {
9831031
setFocusable(true);
9841032
setFocusableInTouchMode(true);
@@ -1063,7 +1111,43 @@ public void surfaceChanged(SurfaceHolder holder,
10631111
mWidth = width;
10641112
mHeight = height;
10651113
SDLActivity.onNativeResize(width, height, sdlFormat, mDisplay.getRefreshRate());
1066-
Log.v("SDL", "Window size:" + width + "x"+height);
1114+
Log.v("SDL", "Window size: " + width + "x" + height);
1115+
1116+
1117+
boolean skip = false;
1118+
int requestedOrientation = SDLActivity.mSingleton.getRequestedOrientation();
1119+
1120+
if (requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
1121+
{
1122+
// Accept any
1123+
}
1124+
else if (requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
1125+
{
1126+
if (mWidth > mHeight) {
1127+
skip = true;
1128+
}
1129+
} else if (requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
1130+
if (mWidth < mHeight) {
1131+
skip = true;
1132+
}
1133+
}
1134+
1135+
// Special Patch for Square Resolution: Black Berry Passport
1136+
if (skip) {
1137+
double min = Math.min(mWidth, mHeight);
1138+
double max = Math.max(mWidth, mHeight);
1139+
1140+
if (max / min < 1.20) {
1141+
Log.v("SDL", "Don't skip on such aspect-ratio. Could be a square resolution.");
1142+
skip = false;
1143+
}
1144+
}
1145+
1146+
if (skip) {
1147+
Log.v("SDL", "Skip .. Surface is not ready.");
1148+
return;
1149+
}
1150+
10671151

10681152
// Set mIsSurfaceReady to 'true' *before* making a call to handleResume
10691153
SDLActivity.mIsSurfaceReady = true;
@@ -1096,6 +1180,10 @@ public void run(){
10961180
}, "SDLThreadListener");
10971181
SDLActivity.mSDLThread.start();
10981182
}
1183+
1184+
if (SDLActivity.mHasFocus) {
1185+
SDLActivity.handleResume();
1186+
}
10991187
}
11001188

11011189
// unused
@@ -1171,6 +1259,11 @@ public boolean onTouch(View v, MotionEvent event) {
11711259
x = event.getX(i) / mWidth;
11721260
y = event.getY(i) / mHeight;
11731261
p = event.getPressure(i);
1262+
if (p > 1.0f) {
1263+
// may be larger than 1.0f on some devices
1264+
// see the documentation of getPressure(i)
1265+
p = 1.0f;
1266+
}
11741267
SDLActivity.onNativeTouch(touchDevId, pointerFingerId, action, x, y, p);
11751268
}
11761269
break;
@@ -1190,6 +1283,11 @@ public boolean onTouch(View v, MotionEvent event) {
11901283
x = event.getX(i) / mWidth;
11911284
y = event.getY(i) / mHeight;
11921285
p = event.getPressure(i);
1286+
if (p > 1.0f) {
1287+
// may be larger than 1.0f on some devices
1288+
// see the documentation of getPressure(i)
1289+
p = 1.0f;
1290+
}
11931291
SDLActivity.onNativeTouch(touchDevId, pointerFingerId, action, x, y, p);
11941292
break;
11951293

@@ -1199,6 +1297,11 @@ public boolean onTouch(View v, MotionEvent event) {
11991297
x = event.getX(i) / mWidth;
12001298
y = event.getY(i) / mHeight;
12011299
p = event.getPressure(i);
1300+
if (p > 1.0f) {
1301+
// may be larger than 1.0f on some devices
1302+
// see the documentation of getPressure(i)
1303+
p = 1.0f;
1304+
}
12021305
SDLActivity.onNativeTouch(touchDevId, pointerFingerId, MotionEvent.ACTION_UP, x, y, p);
12031306
}
12041307
break;
@@ -1319,6 +1422,7 @@ public boolean onKeyPreIme (int keyCode, KeyEvent event) {
13191422
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
13201423
ic = new SDLInputConnection(this, true);
13211424

1425+
outAttrs.inputType = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD;
13221426
outAttrs.imeOptions = EditorInfo.IME_FLAG_NO_EXTRACT_UI
13231427
| 33554432 /* API 11: EditorInfo.IME_FLAG_NO_FULLSCREEN */;
13241428

@@ -1443,7 +1547,13 @@ public void pollInputDevices() {
14431547
if (joystick == null) {
14441548
joystick = new SDLJoystick();
14451549
InputDevice joystickDevice = InputDevice.getDevice(deviceIds[i]);
1446-
if( (joystickDevice.getSources() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
1550+
1551+
if (
1552+
(joystickDevice.getSources() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0
1553+
||
1554+
(joystickDevice.getSources() & InputDevice.SOURCE_CLASS_BUTTON) != 0
1555+
)
1556+
{
14471557
joystick.device_id = deviceIds[i];
14481558
joystick.name = joystickDevice.getName();
14491559
joystick.axes = new ArrayList<InputDevice.MotionRange>();
@@ -1539,7 +1649,6 @@ class SDLGenericMotionListener_API12 implements View.OnGenericMotionListener {
15391649
@Override
15401650
public boolean onGenericMotion(View v, MotionEvent event) {
15411651
float x, y;
1542-
int mouseButton;
15431652
int action;
15441653

15451654
switch ( event.getSource() ) {
@@ -1568,6 +1677,7 @@ public boolean onGenericMotion(View v, MotionEvent event) {
15681677
default:
15691678
break;
15701679
}
1680+
break;
15711681

15721682
default:
15731683
break;

0 commit comments

Comments
 (0)