platform_sensor_android.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2016 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "services/device/generic_sensor/platform_sensor_android.h"
  5. #include "base/bind.h"
  6. #include "services/device/generic_sensor/jni_headers/PlatformSensor_jni.h"
  7. using base::android::AttachCurrentThread;
  8. using base::android::JavaRef;
  9. namespace device {
  10. // static
  11. scoped_refptr<PlatformSensorAndroid> PlatformSensorAndroid::Create(
  12. mojom::SensorType type,
  13. SensorReadingSharedBuffer* reading_buffer,
  14. PlatformSensorProvider* provider,
  15. const JavaRef<jobject>& java_provider) {
  16. auto sensor = base::MakeRefCounted<PlatformSensorAndroid>(
  17. type, reading_buffer, provider);
  18. JNIEnv* env = AttachCurrentThread();
  19. sensor->j_object_.Reset(
  20. Java_PlatformSensor_create(env, java_provider, static_cast<jint>(type),
  21. reinterpret_cast<jlong>(sensor.get())));
  22. if (!sensor->j_object_) {
  23. return nullptr;
  24. }
  25. return sensor;
  26. }
  27. PlatformSensorAndroid::PlatformSensorAndroid(
  28. mojom::SensorType type,
  29. SensorReadingSharedBuffer* reading_buffer,
  30. PlatformSensorProvider* provider)
  31. : PlatformSensor(type, reading_buffer, provider) {}
  32. PlatformSensorAndroid::~PlatformSensorAndroid() {
  33. JNIEnv* env = AttachCurrentThread();
  34. if (j_object_) {
  35. Java_PlatformSensor_sensorDestroyed(env, j_object_);
  36. }
  37. }
  38. mojom::ReportingMode PlatformSensorAndroid::GetReportingMode() {
  39. JNIEnv* env = AttachCurrentThread();
  40. return static_cast<mojom::ReportingMode>(
  41. Java_PlatformSensor_getReportingMode(env, j_object_));
  42. }
  43. PlatformSensorConfiguration PlatformSensorAndroid::GetDefaultConfiguration() {
  44. JNIEnv* env = AttachCurrentThread();
  45. jdouble frequency =
  46. Java_PlatformSensor_getDefaultConfiguration(env, j_object_);
  47. return PlatformSensorConfiguration(frequency);
  48. }
  49. double PlatformSensorAndroid::GetMaximumSupportedFrequency() {
  50. JNIEnv* env = AttachCurrentThread();
  51. return Java_PlatformSensor_getMaximumSupportedFrequency(env, j_object_);
  52. }
  53. bool PlatformSensorAndroid::StartSensor(
  54. const PlatformSensorConfiguration& configuration) {
  55. JNIEnv* env = AttachCurrentThread();
  56. return Java_PlatformSensor_startSensor(env, j_object_,
  57. configuration.frequency());
  58. }
  59. void PlatformSensorAndroid::StopSensor() {
  60. JNIEnv* env = AttachCurrentThread();
  61. Java_PlatformSensor_stopSensor(env, j_object_);
  62. }
  63. bool PlatformSensorAndroid::CheckSensorConfiguration(
  64. const PlatformSensorConfiguration& configuration) {
  65. JNIEnv* env = AttachCurrentThread();
  66. return Java_PlatformSensor_checkSensorConfiguration(
  67. env, j_object_, configuration.frequency());
  68. }
  69. void PlatformSensorAndroid::NotifyPlatformSensorError(
  70. JNIEnv*,
  71. const JavaRef<jobject>& caller) {
  72. PostTaskToMainSequence(
  73. FROM_HERE,
  74. base::BindOnce(&PlatformSensorAndroid::NotifySensorError, this));
  75. }
  76. void PlatformSensorAndroid::UpdatePlatformSensorReading(
  77. JNIEnv*,
  78. const base::android::JavaRef<jobject>& caller,
  79. jdouble timestamp,
  80. jdouble value1,
  81. jdouble value2,
  82. jdouble value3,
  83. jdouble value4) {
  84. SensorReading reading;
  85. reading.raw.timestamp = timestamp;
  86. reading.raw.values[0] = value1;
  87. reading.raw.values[1] = value2;
  88. reading.raw.values[2] = value3;
  89. reading.raw.values[3] = value4;
  90. UpdateSharedBufferAndNotifyClients(reading);
  91. }
  92. } // namespace device