/
GameActivity.java
810 lines (472 loc) · 17 KB
/
GameActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
package org.haxe.lime;
import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.content.res.Configuration;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Vibrator;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.inputmethod.InputMethodManager;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import dalvik.system.DexClassLoader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Constructor;
import java.lang.Math;
import java.lang.Runnable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;
import java.util.List;
import org.haxe.extension.Extension;
import org.haxe.HXCPP;
public class GameActivity extends Activity implements SensorEventListener {
private static final int DEVICE_ORIENTATION_UNKNOWN = 0;
private static final int DEVICE_ORIENTATION_PORTRAIT = 1;
private static final int DEVICE_ORIENTATION_PORTRAIT_UPSIDE_DOWN = 2;
private static final int DEVICE_ORIENTATION_LANDSCAPE_RIGHT = 3;
private static final int DEVICE_ORIENTATION_LANDSCAPE_LEFT = 4;
private static final int DEVICE_ORIENTATION_FACE_UP = 5;
private static final int DEVICE_ORIENTATION_FACE_DOWN = 6;
private static final int DEVICE_ROTATION_0 = 0;
private static final int DEVICE_ROTATION_90 = 1;
private static final int DEVICE_ROTATION_180 = 2;
private static final int DEVICE_ROTATION_270 = 3;
private static final String GLOBAL_PREF_FILE = "nmeAppPrefs";
private static float[] accelData = new float[3];
private static GameActivity activity;
private static AssetManager mAssets;
private static int bufferedDisplayOrientation = -1;
private static int bufferedNormalOrientation = -1;
private static Context mContext;
private static List<Extension> extensions;
private static float[] inclinationMatrix = new float[16];
private static HashMap<String, Class> mLoadedClasses = new HashMap<String, Class>();
private static float[] magnetData = new float[3];
private static DisplayMetrics metrics;
private static float[] orientData = new float[3];
private static float[] rotationMatrix = new float[16];
private static SensorManager sensorManager;
public Handler mHandler;
private static MainView mMainView;
private MainView mView;
private Sound _sound;
protected void onCreate (Bundle state) {
super.onCreate (state);
activity = this;
mContext = this;
mHandler = new Handler ();
mAssets = getAssets ();
Extension.assetManager = mAssets;
Extension.callbackHandler = mHandler;
Extension.mainActivity = this;
Extension.mainContext = this;
_sound = new Sound (getApplication ());
requestWindowFeature (Window.FEATURE_NO_TITLE);
::if WIN_FULLSCREEN::
::if (ANDROID_TARGET_SDK_VERSION < 19)::
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
::end::
::end::
metrics = new DisplayMetrics ();
getWindowManager ().getDefaultDisplay ().getMetrics (metrics);
::foreach ndlls::
System.loadLibrary ("::name::");
::end::
HXCPP.run ("ApplicationMain");
mView = new MainView (getApplication (), this);
setContentView (mView);
Extension.mainView = mView;
sensorManager = (SensorManager)activity.getSystemService (Context.SENSOR_SERVICE);
if (sensorManager != null) {
sensorManager.registerListener (this, sensorManager.getDefaultSensor (Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
sensorManager.registerListener (this, sensorManager.getDefaultSensor (Sensor.TYPE_MAGNETIC_FIELD), SensorManager.SENSOR_DELAY_GAME);
}
Extension.packageName = getApplicationContext ().getPackageName ();
if (extensions == null) {
extensions = new ArrayList<Extension> ();
::if (ANDROID_EXTENSIONS != null)::::foreach ANDROID_EXTENSIONS::
extensions.add (new ::__current__:: ());::end::::end::
}
for (Extension extension : extensions) {
extension.onCreate (state);
}
}
// IMMERSIVE MODE SUPPORT
::if (WIN_FULLSCREEN)::::if (ANDROID_TARGET_SDK_VERSION >= 19)::
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if(hasFocus) {
hideSystemUi();
}
}
private void hideSystemUi() {
if (Build.VERSION.SDK_INT >= 19) {
View decorView = this.getWindow().getDecorView();
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}
}
::end::::end::
public static double CapabilitiesGetPixelAspectRatio () {
return metrics.xdpi / metrics.ydpi;
}
public static double CapabilitiesGetScreenDPI () {
return metrics.xdpi;
}
public static double CapabilitiesGetScreenResolutionX () {
return metrics.widthPixels;
}
public static double CapabilitiesGetScreenResolutionY () {
return metrics.heightPixels;
}
public static String CapabilitiesGetLanguage () {
return Locale.getDefault ().getLanguage ();
}
public static void clearUserPreference (String inId) {
SharedPreferences prefs = activity.getSharedPreferences (GLOBAL_PREF_FILE, MODE_PRIVATE);
SharedPreferences.Editor prefEditor = prefs.edit ();
prefEditor.putString (inId, "");
prefEditor.commit ();
}
public void doPause () {
_sound.doPause ();
mView.sendActivity (Lime.DEACTIVATE);
mView.onPause ();
if (sensorManager != null) {
sensorManager.unregisterListener (this);
}
}
public void doResume () {
mView.onResume ();
_sound.doResume ();
mView.sendActivity (Lime.ACTIVATE);
if (sensorManager != null) {
sensorManager.registerListener (this, sensorManager.getDefaultSensor (Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
sensorManager.registerListener (this, sensorManager.getDefaultSensor (Sensor.TYPE_MAGNETIC_FIELD), SensorManager.SENSOR_DELAY_GAME);
}
}
public static AssetManager getAssetManager () {
return mAssets;
}
public static Context getContext () {
return mContext;
}
public static GameActivity getInstance () {
return activity;
}
public static MainView getMainView () {
return activity.mView;
}
public static byte[] getResource (String inResource) {
try {
InputStream inputStream = mAssets.open (inResource, AssetManager.ACCESS_BUFFER);
long length = inputStream.available ();
byte[] result = new byte[(int)length];
inputStream.read (result);
inputStream.close ();
return result;
} catch (IOException e) {
Log.e ("GameActivity", "getResource" + ":" + e.toString ());
}
return null;
}
public static int getResourceID (String inFilename) {
//::foreach assets::::if (type == "music")::if (inFilename.equals("::id::")) return ::APP_PACKAGE::.R.raw.::flatName::;
//::end::::end::
//::foreach assets::::if (type == "sound")::if (inFilename.equals("::id::")) return ::APP_PACKAGE::.R.raw.::flatName::;
//::end::::end::
return -1;
}
static public String getSpecialDir (int inWhich) {
//Log.v ("GameActivity", "Get special Dir " + inWhich);
File path = null;
switch (inWhich) {
case 0: // App
return mContext.getPackageCodePath ();
case 1: // Storage
path = mContext.getFilesDir ();
break;
case 2: // Desktop
path = Environment.getDataDirectory ();
break;
case 3: // Docs
path = Environment.getExternalStorageDirectory ();
break;
case 4: // User
path = mContext.getExternalFilesDir (Environment.DIRECTORY_DOWNLOADS);
break;
}
return path == null ? "" : path.getAbsolutePath ();
}
public static String getUserPreference (String inId) {
SharedPreferences prefs = activity.getSharedPreferences (GLOBAL_PREF_FILE, MODE_PRIVATE);
return prefs.getString (inId, "");
}
public static void launchBrowser (String inURL) {
Intent browserIntent = new Intent (Intent.ACTION_VIEW).setData (Uri.parse (inURL));
try {
activity.startActivity (browserIntent);
} catch (Exception e) {
Log.e ("GameActivity", e.toString ());
return;
}
}
private void loadNewSensorData (SensorEvent event) {
final int type = event.sensor.getType ();
if (type == Sensor.TYPE_ACCELEROMETER) {
accelData = event.values.clone ();
Lime.onAccelerate (-accelData[0], -accelData[1], accelData[2]);
}
if (type == Sensor.TYPE_MAGNETIC_FIELD) {
magnetData = event.values.clone ();
//Log.d("GameActivity","new mag: " + magnetData[0] + ", " + magnetData[1] + ", " + magnetData[2]);
}
}
@Override public void onAccuracyChanged (Sensor sensor, int accuracy) {
}
@Override protected void onActivityResult (int requestCode, int resultCode, Intent data) {
for (Extension extension : extensions) {
if (!extension.onActivityResult (requestCode, resultCode, data)) {
return;
}
}
super.onActivityResult (requestCode, resultCode, data);
}
@Override protected void onDestroy () {
for (Extension extension : extensions) {
extension.onDestroy ();
}
// TODO: Wait for result?
mView.sendActivity (Lime.DESTROY);
activity = null;
super.onDestroy ();
}
@Override public void onLowMemory () {
super.onLowMemory ();
for (Extension extension : extensions) {
extension.onLowMemory ();
}
}
@Override protected void onNewIntent (final Intent intent) {
for (Extension extension : extensions) {
extension.onNewIntent (intent);
}
super.onNewIntent (intent);
}
@Override protected void onPause () {
doPause ();
super.onPause ();
for (Extension extension : extensions) {
extension.onPause ();
}
}
@Override protected void onRestart () {
super.onRestart ();
for (Extension extension : extensions) {
extension.onRestart ();
}
}
@Override protected void onResume () {
super.onResume();
doResume();
for (Extension extension : extensions) {
extension.onResume ();
}
}
@Override public void onSensorChanged (SensorEvent event) {
loadNewSensorData (event);
if (accelData != null && magnetData != null) {
boolean foundRotationMatrix = SensorManager.getRotationMatrix (rotationMatrix, inclinationMatrix, accelData, magnetData);
if (foundRotationMatrix) {
SensorManager.getOrientation (rotationMatrix, orientData);
Lime.onOrientationUpdate (orientData[0], orientData[1], orientData[2]);
}
}
Lime.onDeviceOrientationUpdate (prepareDeviceOrientation ());
Lime.onNormalOrientationFound (bufferedNormalOrientation);
}
@Override protected void onStart () {
super.onStart();
::if WIN_FULLSCREEN::::if (ANDROID_TARGET_SDK_VERSION >= 16)::
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
getWindow().getDecorView().setSystemUiVisibility (View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LOW_PROFILE | View.SYSTEM_UI_FLAG_FULLSCREEN);
}
::end::::end::
for (Extension extension : extensions) {
extension.onStart ();
}
}
@Override protected void onStop () {
super.onStop ();
for (Extension extension : extensions) {
extension.onStop ();
}
}
::if (ANDROID_TARGET_SDK_VERSION >= 14)::
@Override public void onTrimMemory (int level) {
if (Build.VERSION.SDK_INT >= 14) {
super.onTrimMemory (level);
for (Extension extension : extensions) {
extension.onTrimMemory (level);
}
}
}
::end::
public static void popView () {
activity.setContentView (activity.mView);
activity.doResume ();
}
public static void postUICallback (final long inHandle) {
activity.mHandler.post (new Runnable () {
@Override public void run () {
Lime.onCallback (inHandle);
}
});
}
private int prepareDeviceOrientation () {
int rawOrientation = getWindow ().getWindowManager ().getDefaultDisplay ().getOrientation ();
if (rawOrientation != bufferedDisplayOrientation) {
bufferedDisplayOrientation = rawOrientation;
}
int screenOrientation = getResources ().getConfiguration ().orientation;
int deviceOrientation = DEVICE_ORIENTATION_UNKNOWN;
if (bufferedNormalOrientation < 0) {
switch (screenOrientation) {
case Configuration.ORIENTATION_LANDSCAPE:
switch (bufferedDisplayOrientation) {
case DEVICE_ROTATION_0:
case DEVICE_ROTATION_180:
bufferedNormalOrientation = DEVICE_ORIENTATION_LANDSCAPE_LEFT;
break;
case DEVICE_ROTATION_90:
case DEVICE_ROTATION_270:
bufferedNormalOrientation = DEVICE_ORIENTATION_PORTRAIT;
break;
default:
bufferedNormalOrientation = DEVICE_ORIENTATION_UNKNOWN;
}
break;
case Configuration.ORIENTATION_PORTRAIT:
switch (bufferedDisplayOrientation) {
case DEVICE_ROTATION_0:
case DEVICE_ROTATION_180:
bufferedNormalOrientation = DEVICE_ORIENTATION_PORTRAIT;
break;
case DEVICE_ROTATION_90:
case DEVICE_ROTATION_270:
bufferedNormalOrientation = DEVICE_ORIENTATION_LANDSCAPE_LEFT;
break;
default:
bufferedNormalOrientation = DEVICE_ORIENTATION_UNKNOWN;
}
break;
default: // ORIENTATION_SQUARE OR ORIENTATION_UNDEFINED
bufferedNormalOrientation = DEVICE_ORIENTATION_UNKNOWN;
}
}
switch (screenOrientation) {
case Configuration.ORIENTATION_LANDSCAPE:
switch (bufferedDisplayOrientation) {
case DEVICE_ROTATION_0:
case DEVICE_ROTATION_270:
deviceOrientation = DEVICE_ORIENTATION_LANDSCAPE_LEFT;
break;
case DEVICE_ROTATION_90:
case DEVICE_ROTATION_180:
deviceOrientation = DEVICE_ORIENTATION_LANDSCAPE_RIGHT;
break;
default: // impossible!
deviceOrientation = DEVICE_ORIENTATION_UNKNOWN;
}
break;
case Configuration.ORIENTATION_PORTRAIT:
switch (bufferedDisplayOrientation) {
case DEVICE_ROTATION_0:
case DEVICE_ROTATION_90:
deviceOrientation = DEVICE_ORIENTATION_PORTRAIT;
break;
case DEVICE_ROTATION_180:
case DEVICE_ROTATION_270:
deviceOrientation = DEVICE_ORIENTATION_PORTRAIT_UPSIDE_DOWN;
break;
default: // impossible!
deviceOrientation = DEVICE_ORIENTATION_UNKNOWN;
}
break;
default: // ORIENTATION_SQUARE OR ORIENTATION_UNDEFINED
deviceOrientation = DEVICE_ORIENTATION_UNKNOWN;
}
return deviceOrientation;
}
public static void pushView (View inView) {
activity.doPause ();
activity.setContentView (inView);
}
public void queueRunnable (Runnable runnable) {
Log.e ("GameActivity", "queueing...");
}
public static void registerExtension (Extension extension) {
if (extensions.indexOf (extension) == -1) {
extensions.add (extension);
}
}
public static void showKeyboard (boolean show) {
if (activity == null) {
return;
}
InputMethodManager mgr = (InputMethodManager)activity.getSystemService (Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow (activity.mView.getWindowToken (), 0);
if (show) {
mgr.toggleSoftInput (InputMethodManager.SHOW_FORCED, 0);
// On the Nexus One, SHOW_FORCED makes it impossible
// to manually dismiss the keyboard.
// On the Droid SHOW_IMPLICIT doesn't bring up the keyboard.
}
}
public static void setUserPreference (String inId, String inPreference) {
SharedPreferences prefs = activity.getSharedPreferences (GLOBAL_PREF_FILE, MODE_PRIVATE);
SharedPreferences.Editor prefEditor = prefs.edit ();
prefEditor.putString (inId, inPreference);
prefEditor.commit ();
}
public static void vibrate (int period, int duration) {
Vibrator v = (Vibrator)activity.getSystemService (Context.VIBRATOR_SERVICE);
if (period == 0) {
v.vibrate (duration);
} else {
int periodMS = (int)Math.ceil (period / 2);
int count = (int)Math.ceil ((duration / period) * 2);
long[] pattern = new long[count];
for (int i = 0; i < count; i++) {
pattern[i] = periodMS;
}
v.vibrate (pattern, -1);
}
}
}