OLD | NEW |
(Empty) | |
| 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 |
| 5 #include "device/generic_sensor/platform_sensor_android.h" |
| 6 |
| 7 #include "base/android/context_utils.h" |
| 8 #include "base/bind.h" |
| 9 #include "base/message_loop/message_loop.h" |
| 10 #include "jni/PlatformSensor_jni.h" |
| 11 |
| 12 using base::android::AttachCurrentThread; |
| 13 using base::android::JavaRef; |
| 14 |
| 15 namespace device { |
| 16 |
| 17 // static |
| 18 bool PlatformSensorAndroid::RegisterJNI(JNIEnv* env) { |
| 19 return RegisterNativesImpl(env); |
| 20 } |
| 21 |
| 22 PlatformSensorAndroid::PlatformSensorAndroid( |
| 23 mojom::SensorType type, |
| 24 mojo::ScopedSharedBufferMapping mapping, |
| 25 uint64_t buffer_size, |
| 26 PlatformSensorProvider* provider, |
| 27 const JavaRef<jobject>& java_sensor) |
| 28 : PlatformSensor(type, std::move(mapping), provider), |
| 29 task_runner_(base::MessageLoop::current()->task_runner()) { |
| 30 JNIEnv* env = AttachCurrentThread(); |
| 31 j_object_.Reset(java_sensor); |
| 32 |
| 33 jobject byte_buffer = |
| 34 env->NewDirectByteBuffer(shared_buffer_mapping_.get(), buffer_size); |
| 35 Java_PlatformSensor_initPlatformSensorAndroid( |
| 36 env, j_object_.obj(), reinterpret_cast<jlong>(this), byte_buffer); |
| 37 } |
| 38 |
| 39 PlatformSensorAndroid::~PlatformSensorAndroid() { |
| 40 StopSensor(); |
| 41 } |
| 42 |
| 43 mojom::ReportingMode PlatformSensorAndroid::GetReportingMode() { |
| 44 JNIEnv* env = AttachCurrentThread(); |
| 45 return static_cast<mojom::ReportingMode>( |
| 46 Java_PlatformSensor_getReportingMode(env, j_object_.obj())); |
| 47 } |
| 48 |
| 49 PlatformSensorConfiguration PlatformSensorAndroid::GetDefaultConfiguration() { |
| 50 JNIEnv* env = AttachCurrentThread(); |
| 51 jdouble frequency = |
| 52 Java_PlatformSensor_getDefaultConfiguration(env, j_object_.obj()); |
| 53 return PlatformSensorConfiguration(frequency); |
| 54 } |
| 55 |
| 56 bool PlatformSensorAndroid::StartSensor( |
| 57 const PlatformSensorConfiguration& configuration) { |
| 58 JNIEnv* env = AttachCurrentThread(); |
| 59 return Java_PlatformSensor_startSensor(env, j_object_.obj(), |
| 60 configuration.frequency()); |
| 61 } |
| 62 |
| 63 void PlatformSensorAndroid::StopSensor() { |
| 64 JNIEnv* env = AttachCurrentThread(); |
| 65 Java_PlatformSensor_stopSensor(env, j_object_.obj()); |
| 66 } |
| 67 |
| 68 bool PlatformSensorAndroid::CheckSensorConfiguration( |
| 69 const PlatformSensorConfiguration& configuration) { |
| 70 JNIEnv* env = AttachCurrentThread(); |
| 71 return Java_PlatformSensor_checkSensorConfiguration( |
| 72 env, j_object_.obj(), configuration.frequency()); |
| 73 } |
| 74 |
| 75 void PlatformSensorAndroid::NotifyPlatformSensorReadingChanged( |
| 76 JNIEnv*, |
| 77 const JavaRef<jobject>& caller) { |
| 78 task_runner_->PostTask( |
| 79 FROM_HERE, |
| 80 base::Bind(&PlatformSensorAndroid::NotifySensorReadingChanged, this)); |
| 81 } |
| 82 |
| 83 void PlatformSensorAndroid::NotifyPlatformSensorError( |
| 84 JNIEnv*, |
| 85 const JavaRef<jobject>& caller) { |
| 86 task_runner_->PostTask( |
| 87 FROM_HERE, base::Bind(&PlatformSensorAndroid::NotifySensorError, this)); |
| 88 } |
| 89 |
| 90 } // namespace device |
OLD | NEW |