7 package com.google.appinventor.components.runtime;
19 import android.content.Context;
20 import android.hardware.Sensor;
21 import android.hardware.SensorEvent;
22 import android.hardware.SensorEventListener;
23 import android.hardware.SensorManager;
28 @DesignerComponent(version = YaVersion.GYROSCOPESENSOR_COMPONENT_VERSION,
29 description =
"<p>Non-visible component that can measure angular velocity in three " +
30 "dimensions in units of degrees per second.</p>" +
31 "<p>In order to function, the component must have its <code>Enabled</code> property set to " +
32 "True, and the device must have a gyroscope sensor.</p>",
33 category = ComponentCategory.SENSORS,
35 iconName =
"images/gyroscopesensor.png")
42 private boolean enabled;
43 private float xAngularVelocity;
44 private float yAngularVelocity;
45 private float zAngularVelocity;
48 private final SensorManager sensorManager;
49 private final Sensor gyroSensor;
50 private boolean listening;
56 super(container.
$form());
59 sensorManager = (SensorManager)
form.getSystemService(Context.SENSOR_SERVICE);
60 gyroSensor = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
70 private void startListening() {
72 sensorManager.registerListener(
this, gyroSensor, SensorManager.SENSOR_DELAY_FASTEST);
77 private void stopListening() {
79 sensorManager.unregisterListener(
this);
95 @SimpleEvent(description =
"Indicates that the gyroscope sensor data has changed. The " +
96 "timestamp parameter is the time in nanoseconds at which the event occurred.")
98 float xAngularVelocity,
float yAngularVelocity,
float zAngularVelocity,
long timestamp) {
100 xAngularVelocity, yAngularVelocity, zAngularVelocity, timestamp);
112 @
SimpleProperty(description =
"Indicates whether a gyroscope sensor is available.",
115 return sensorManager.getSensorList(Sensor.TYPE_GYROSCOPE).size() > 0;
138 defaultValue =
"True")
139 @
SimpleProperty(description =
"If enabled, then sensor events will be generated and " +
140 "XAngularVelocity, YAngularVelocity, and ZAngularVelocity properties will have " +
141 "meaningful values.")
143 if (this.enabled != enabled) {
144 this.enabled = enabled;
161 @
SimpleProperty(description =
"The angular velocity around the X axis, in degrees per second.",
164 return xAngularVelocity;
175 @
SimpleProperty(description =
"The angular velocity around the Y axis, in degrees per second.",
178 return yAngularVelocity;
189 @
SimpleProperty(description =
"The angular velocity around the Z axis, in degrees per second.",
192 return zAngularVelocity;
206 xAngularVelocity = (float) Math.toDegrees(sensorEvent.values[0]);
207 yAngularVelocity = (float) Math.toDegrees(sensorEvent.values[1]);
208 zAngularVelocity = (float) Math.toDegrees(sensorEvent.values[2]);
212 sensorEvent.timestamp);