11
11
12
12
import android .app .*;
13
13
import android .content .*;
14
+ import android .text .InputType ;
14
15
import android .view .*;
15
16
import android .view .inputmethod .BaseInputConnection ;
16
17
import android .view .inputmethod .EditorInfo ;
27
28
import android .graphics .drawable .Drawable ;
28
29
import android .media .*;
29
30
import android .hardware .*;
31
+ import android .content .pm .ActivityInfo ;
30
32
31
33
/**
32
34
SDL Activity
@@ -114,9 +116,9 @@ public static void initialize() {
114
116
// Setup
115
117
@ Override
116
118
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 );
120
122
super .onCreate (savedInstanceState );
121
123
122
124
SDLActivity .initialize ();
@@ -177,12 +179,23 @@ public void onClick(DialogInterface dialog,int id) {
177
179
mLayout .addView (mSurface );
178
180
179
181
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
+ }
180
193
}
181
194
182
195
// Events
183
196
@ Override
184
197
protected void onPause () {
185
- Log .v ("SDL" , "onPause()" );
198
+ Log .v (TAG , "onPause()" );
186
199
super .onPause ();
187
200
188
201
if (SDLActivity .mBrokenLibraries ) {
@@ -194,7 +207,7 @@ protected void onPause() {
194
207
195
208
@ Override
196
209
protected void onResume () {
197
- Log .v ("SDL" , "onResume()" );
210
+ Log .v (TAG , "onResume()" );
198
211
super .onResume ();
199
212
200
213
if (SDLActivity .mBrokenLibraries ) {
@@ -208,7 +221,7 @@ protected void onResume() {
208
221
@ Override
209
222
public void onWindowFocusChanged (boolean hasFocus ) {
210
223
super .onWindowFocusChanged (hasFocus );
211
- Log .v ("SDL" , "onWindowFocusChanged(): " + hasFocus );
224
+ Log .v (TAG , "onWindowFocusChanged(): " + hasFocus );
212
225
213
226
if (SDLActivity .mBrokenLibraries ) {
214
227
return ;
@@ -222,7 +235,7 @@ public void onWindowFocusChanged(boolean hasFocus) {
222
235
223
236
@ Override
224
237
public void onLowMemory () {
225
- Log .v ("SDL" , "onLowMemory()" );
238
+ Log .v (TAG , "onLowMemory()" );
226
239
super .onLowMemory ();
227
240
228
241
if (SDLActivity .mBrokenLibraries ) {
@@ -234,7 +247,7 @@ public void onLowMemory() {
234
247
235
248
@ Override
236
249
protected void onDestroy () {
237
- Log .v ("SDL" , "onDestroy()" );
250
+ Log .v (TAG , "onDestroy()" );
238
251
239
252
if (SDLActivity .mBrokenLibraries ) {
240
253
super .onDestroy ();
@@ -252,11 +265,11 @@ protected void onDestroy() {
252
265
try {
253
266
SDLActivity .mSDLThread .join ();
254
267
} catch (Exception e ) {
255
- Log .v ("SDL" , "Problem stopping thread: " + e );
268
+ Log .v (TAG , "Problem stopping thread: " + e );
256
269
}
257
270
SDLActivity .mSDLThread = null ;
258
271
259
- //Log.v("SDL" , "Finished waiting for SDL thread");
272
+ //Log.v(TAG , "Finished waiting for SDL thread");
260
273
}
261
274
262
275
super .onDestroy ();
@@ -295,7 +308,7 @@ public static void handlePause() {
295
308
if (!SDLActivity .mIsPaused && SDLActivity .mIsSurfaceReady ) {
296
309
SDLActivity .mIsPaused = true ;
297
310
SDLActivity .nativePause ();
298
- mSurface .enableSensor ( Sensor . TYPE_ACCELEROMETER , false );
311
+ mSurface .handlePause ( );
299
312
}
300
313
}
301
314
@@ -404,6 +417,7 @@ boolean sendCommand(int command, Object data) {
404
417
public static native void nativeQuit ();
405
418
public static native void nativePause ();
406
419
public static native void nativeResume ();
420
+ public static native void onNativeDropFile (String filename );
407
421
public static native void onNativeResize (int x , int y , int format , float rate );
408
422
public static native int onNativePadDown (int device_id , int keycode );
409
423
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
550
564
int audioFormat = is16Bit ? AudioFormat .ENCODING_PCM_16BIT : AudioFormat .ENCODING_PCM_8BIT ;
551
565
int frameSize = (isStereo ? 2 : 1 ) * (is16Bit ? 2 : 1 );
552
566
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" );
554
568
555
569
// Let the user pick a larger buffer if they really want -- but ye
556
570
// 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
566
580
// Ref: http://developer.android.com/reference/android/media/AudioTrack.html#getState()
567
581
568
582
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" );
570
584
mAudioTrack = null ;
571
585
return -1 ;
572
586
}
573
587
574
588
mAudioTrack .play ();
575
589
}
576
590
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" );
578
592
579
593
return 0 ;
580
594
}
@@ -594,7 +608,7 @@ public static void audioWriteShortBuffer(short[] buffer) {
594
608
// Nom nom
595
609
}
596
610
} else {
597
- Log .w ("SDL" , "SDL audio: error return from write(short)" );
611
+ Log .w (TAG , "SDL audio: error return from write(short)" );
598
612
return ;
599
613
}
600
614
}
@@ -615,7 +629,7 @@ public static void audioWriteByteBuffer(byte[] buffer) {
615
629
// Nom nom
616
630
}
617
631
} else {
618
- Log .w ("SDL" , "SDL audio: error return from write(byte)" );
632
+ Log .w (TAG , "SDL audio: error return from write(byte)" );
619
633
return ;
620
634
}
621
635
}
@@ -671,7 +685,7 @@ public static void pollInputDevices() {
671
685
public void keepActive () {
672
686
}
673
687
674
- // APK extension files support
688
+ // APK expansion files support
675
689
676
690
/** com.android.vending.expansion.zipfile.ZipResourceFile object or null. */
677
691
private Object expansionFile ;
@@ -680,16 +694,43 @@ public void keepActive() {
680
694
private Method expansionFileMethod ;
681
695
682
696
/**
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
684
699
*/
700
+ @ Deprecated
685
701
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 {
686
711
// Get a ZipResourceFile representing a merger of both the main and patch files
687
712
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
+ }
690
731
691
732
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
693
734
// not a part of Android SDK we access it using reflection
694
735
expansionFile = Class .forName ("com.android.vending.expansion.zipfile.APKExpansionSupport" )
695
736
.getMethod ("getAPKExpansionZipFile" , Context .class , int .class , int .class )
@@ -701,6 +742,7 @@ public InputStream openAPKExtensionInputStream(String fileName) throws IOExcepti
701
742
ex .printStackTrace ();
702
743
expansionFile = null ;
703
744
expansionFileMethod = null ;
745
+ throw new IOException ("Could not access APK expansion support library" , ex );
704
746
}
705
747
}
706
748
@@ -709,12 +751,14 @@ public InputStream openAPKExtensionInputStream(String fileName) throws IOExcepti
709
751
try {
710
752
fileStream = (InputStream )expansionFileMethod .invoke (expansionFile , fileName );
711
753
} catch (Exception ex ) {
754
+ // calling "getInputStream" failed
712
755
ex .printStackTrace ();
713
- fileStream = null ;
756
+ throw new IOException ( "Could not open stream from APK expansion file" , ex ) ;
714
757
}
715
758
716
759
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" );
718
762
}
719
763
720
764
return fileStream ;
@@ -979,6 +1023,10 @@ public SDLSurface(Context context) {
979
1023
mHeight = 1.0f ;
980
1024
}
981
1025
1026
+ public void handlePause () {
1027
+ enableSensor (Sensor .TYPE_ACCELEROMETER , false );
1028
+ }
1029
+
982
1030
public void handleResume () {
983
1031
setFocusable (true );
984
1032
setFocusableInTouchMode (true );
@@ -1063,7 +1111,43 @@ public void surfaceChanged(SurfaceHolder holder,
1063
1111
mWidth = width ;
1064
1112
mHeight = height ;
1065
1113
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
+
1067
1151
1068
1152
// Set mIsSurfaceReady to 'true' *before* making a call to handleResume
1069
1153
SDLActivity .mIsSurfaceReady = true ;
@@ -1096,6 +1180,10 @@ public void run(){
1096
1180
}, "SDLThreadListener" );
1097
1181
SDLActivity .mSDLThread .start ();
1098
1182
}
1183
+
1184
+ if (SDLActivity .mHasFocus ) {
1185
+ SDLActivity .handleResume ();
1186
+ }
1099
1187
}
1100
1188
1101
1189
// unused
@@ -1171,6 +1259,11 @@ public boolean onTouch(View v, MotionEvent event) {
1171
1259
x = event .getX (i ) / mWidth ;
1172
1260
y = event .getY (i ) / mHeight ;
1173
1261
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
+ }
1174
1267
SDLActivity .onNativeTouch (touchDevId , pointerFingerId , action , x , y , p );
1175
1268
}
1176
1269
break ;
@@ -1190,6 +1283,11 @@ public boolean onTouch(View v, MotionEvent event) {
1190
1283
x = event .getX (i ) / mWidth ;
1191
1284
y = event .getY (i ) / mHeight ;
1192
1285
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
+ }
1193
1291
SDLActivity .onNativeTouch (touchDevId , pointerFingerId , action , x , y , p );
1194
1292
break ;
1195
1293
@@ -1199,6 +1297,11 @@ public boolean onTouch(View v, MotionEvent event) {
1199
1297
x = event .getX (i ) / mWidth ;
1200
1298
y = event .getY (i ) / mHeight ;
1201
1299
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
+ }
1202
1305
SDLActivity .onNativeTouch (touchDevId , pointerFingerId , MotionEvent .ACTION_UP , x , y , p );
1203
1306
}
1204
1307
break ;
@@ -1319,6 +1422,7 @@ public boolean onKeyPreIme (int keyCode, KeyEvent event) {
1319
1422
public InputConnection onCreateInputConnection (EditorInfo outAttrs ) {
1320
1423
ic = new SDLInputConnection (this , true );
1321
1424
1425
+ outAttrs .inputType = InputType .TYPE_CLASS_TEXT | InputType .TYPE_TEXT_VARIATION_VISIBLE_PASSWORD ;
1322
1426
outAttrs .imeOptions = EditorInfo .IME_FLAG_NO_EXTRACT_UI
1323
1427
| 33554432 /* API 11: EditorInfo.IME_FLAG_NO_FULLSCREEN */ ;
1324
1428
@@ -1443,7 +1547,13 @@ public void pollInputDevices() {
1443
1547
if (joystick == null ) {
1444
1548
joystick = new SDLJoystick ();
1445
1549
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
+ {
1447
1557
joystick .device_id = deviceIds [i ];
1448
1558
joystick .name = joystickDevice .getName ();
1449
1559
joystick .axes = new ArrayList <InputDevice .MotionRange >();
@@ -1539,7 +1649,6 @@ class SDLGenericMotionListener_API12 implements View.OnGenericMotionListener {
1539
1649
@ Override
1540
1650
public boolean onGenericMotion (View v , MotionEvent event ) {
1541
1651
float x , y ;
1542
- int mouseButton ;
1543
1652
int action ;
1544
1653
1545
1654
switch ( event .getSource () ) {
@@ -1568,6 +1677,7 @@ public boolean onGenericMotion(View v, MotionEvent event) {
1568
1677
default :
1569
1678
break ;
1570
1679
}
1680
+ break ;
1571
1681
1572
1682
default :
1573
1683
break ;
0 commit comments