CrownActivity.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. package crown.android;
  24. import android.app.Activity;
  25. import android.os.Bundle;
  26. import android.util.Log;
  27. import android.view.WindowManager;
  28. import android.view.MotionEvent;
  29. import android.hardware.Sensor;
  30. import android.hardware.SensorEvent;
  31. import android.hardware.SensorEventListener;
  32. import android.hardware.SensorManager;
  33. import android.content.Context;
  34. import android.widget.Toast;
  35. import android.content.res.AssetManager;
  36. import android.view.View;
  37. import android.view.Surface;
  38. import android.view.SurfaceView;
  39. import android.view.SurfaceHolder;
  40. import android.view.KeyEvent;
  41. public class CrownActivity extends Activity
  42. {
  43. // Debug
  44. public static String TAG = "crown";
  45. // Resource attributes
  46. static AssetManager mAssetManager;
  47. private CrownSurfaceView mView;
  48. //-----------------------------------------------------------------------------
  49. public void onCreate(Bundle savedInstanceState)
  50. {
  51. super.onCreate(savedInstanceState);
  52. // Initializes low-level systems (memory, os etc.)
  53. CrownLib.initCrown();
  54. // init AssetManager
  55. mAssetManager = getAssets();
  56. CrownLib.initAssetManager(mAssetManager);
  57. // init Native Window
  58. mView = new CrownSurfaceView(this);
  59. setContentView(mView);
  60. Log.i(TAG, "Crown Activity created");
  61. }
  62. //-----------------------------------------------------------------------------
  63. public void onResume()
  64. {
  65. super.onResume();
  66. CrownLib.pushResumeEvent();
  67. Log.i(TAG, "Crown Activity resumed");
  68. }
  69. //-----------------------------------------------------------------------------
  70. public void onPause()
  71. {
  72. super.onPause();
  73. CrownLib.pushPauseEvent();
  74. Log.i(TAG, "Crown Activity paused");
  75. }
  76. //-----------------------------------------------------------------------------
  77. public void onDestroy()
  78. {
  79. super.onDestroy();
  80. CrownLib.pushExitEvent(0);
  81. CrownLib.releaseWindow();
  82. CrownLib.shutdownCrown();
  83. Log.i(TAG, "Crown Activity destroyed");
  84. }
  85. //-----------------------------------------------------------------------------
  86. @Override
  87. public boolean onKeyDown(int keyCode, KeyEvent event)
  88. {
  89. if ((keyCode == KeyEvent.KEYCODE_BACK))
  90. {
  91. CrownLib.pushKeyboardEvent(0, 0x1B, 1);
  92. }
  93. return super.onKeyDown(keyCode, event);
  94. }
  95. //-----------------------------------------------------------------------------
  96. @Override
  97. public boolean onKeyUp(int keyCode, KeyEvent event)
  98. {
  99. if ((keyCode == KeyEvent.KEYCODE_BACK))
  100. {
  101. CrownLib.pushKeyboardEvent(0, 0x1B, 0);
  102. }
  103. return super.onKeyUp(keyCode, event);
  104. }
  105. //-----------------------------------------------------------------------------
  106. public boolean onTouchEvent(MotionEvent event)
  107. {
  108. final int pointerIndex = event.getActionIndex();
  109. final int pointerCount = event.getPointerCount();
  110. final int pointerId = event.getPointerId(pointerIndex);
  111. final float x = event.getX(pointerIndex);
  112. final float y = event.getY(pointerIndex);
  113. final int actionMasked = event.getActionMasked();
  114. switch (actionMasked)
  115. {
  116. case MotionEvent.ACTION_DOWN:
  117. case MotionEvent.ACTION_POINTER_DOWN:
  118. {
  119. CrownLib.pushTouchEventPointer(pointerId, (int) x, (int) y, 1);
  120. break;
  121. }
  122. case MotionEvent.ACTION_UP:
  123. case MotionEvent.ACTION_POINTER_UP:
  124. {
  125. CrownLib.pushTouchEventPointer(pointerId, (int) x, (int) y, 0);
  126. break;
  127. }
  128. case MotionEvent.ACTION_OUTSIDE:
  129. case MotionEvent.ACTION_CANCEL:
  130. {
  131. CrownLib.pushTouchEventPointer(pointerId, (int)x, (int)y, 0);
  132. break;
  133. }
  134. case MotionEvent.ACTION_MOVE:
  135. {
  136. for (int index = 0; index < pointerCount; index++)
  137. {
  138. CrownLib.pushTouchEventMove(event.getPointerId(index), (int)event.getX(index), (int)event.getY(index));
  139. }
  140. break;
  141. }
  142. }
  143. return super.onTouchEvent(event);
  144. }
  145. }