CCDevice-android.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013-2016 Chukong Technologies Inc.
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #include "platform/CCPlatformConfig.h"
  23. #if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
  24. #include "platform/CCDevice.h"
  25. #include <string.h>
  26. #include <android/log.h>
  27. #include <jni.h>
  28. #include "base/ccTypes.h"
  29. #include "platform/android/jni/JniHelper.h"
  30. #include "platform/CCFileUtils.h"
  31. #include "base/ccUTF8.h"
  32. #include "platform/CCApplication.h"
  33. #ifndef JCLS_HELPER
  34. #define JCLS_HELPER "org/cocos2dx/lib/Cocos2dxHelper"
  35. #endif
  36. NS_CC_BEGIN
  37. int Device::getDPI()
  38. {
  39. static int dpi = -1;
  40. if (dpi == -1)
  41. {
  42. dpi = JniHelper::callStaticIntMethod(JCLS_HELPER, "getDPI");
  43. }
  44. return dpi;
  45. }
  46. void Device::setAccelerometerEnabled(bool isEnabled)
  47. {
  48. if (isEnabled)
  49. {
  50. JniHelper::callStaticVoidMethod(JCLS_HELPER, "enableAccelerometer");
  51. }
  52. else
  53. {
  54. JniHelper::callStaticVoidMethod(JCLS_HELPER, "disableAccelerometer");
  55. }
  56. }
  57. void Device::setAccelerometerInterval(float interval)
  58. {
  59. JniHelper::callStaticVoidMethod(JCLS_HELPER, "setAccelerometerInterval", interval);
  60. }
  61. const Device::MotionValue& Device::getDeviceMotionValue()
  62. {
  63. static MotionValue __motionValue;
  64. float* v = JniHelper::callStaticFloatArrayMethod(JCLS_HELPER, "getDeviceMotionValue");
  65. __motionValue.accelerationX = v[0];
  66. __motionValue.accelerationY = v[1];
  67. __motionValue.accelerationZ = v[2];
  68. __motionValue.accelerationIncludingGravityX = v[3];
  69. __motionValue.accelerationIncludingGravityY = v[4];
  70. __motionValue.accelerationIncludingGravityZ = v[5];
  71. __motionValue.rotationRateAlpha = v[6];
  72. __motionValue.rotationRateBeta = v[7];
  73. __motionValue.rotationRateGamma = v[8];
  74. return __motionValue;
  75. }
  76. Device::Rotation Device::getDeviceRotation()
  77. {
  78. int rotation = JniHelper::callStaticIntMethod(JCLS_HELPER, "getDeviceRotation");
  79. return (Device::Rotation)rotation;
  80. }
  81. std::string Device::getDeviceModel()
  82. {
  83. return JniHelper::callStaticStringMethod(JCLS_HELPER, "getDeviceModel");
  84. }
  85. void Device::setKeepScreenOn(bool value)
  86. {
  87. JniHelper::callStaticVoidMethod(JCLS_HELPER, "setKeepScreenOn", value);
  88. }
  89. void Device::vibrate(float duration)
  90. {
  91. JniHelper::callStaticVoidMethod(JCLS_HELPER, "vibrate", duration);
  92. }
  93. float Device::getBatteryLevel()
  94. {
  95. return JniHelper::callStaticFloatMethod(JCLS_HELPER, "getBatteryLevel");
  96. }
  97. Device::NetworkType Device::getNetworkType()
  98. {
  99. return (Device::NetworkType)JniHelper::callStaticIntMethod(JCLS_HELPER, "getNetworkType");
  100. }
  101. cocos2d::Vec4 Device::getSafeAreaEdge()
  102. {
  103. float *data = JniHelper::callStaticFloatArrayMethod(JCLS_HELPER, "getSafeArea");
  104. return cocos2d::Vec4(data[0], data[1], data[2],data[3]);
  105. }
  106. int Device::getDevicePixelRatio()
  107. {
  108. return 1;
  109. }
  110. NS_CC_END
  111. #endif // CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID