AI2 Component  (Version nb184)
GyroscopeSensor.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright 2009-2011 Google, All Rights reserved
3 // Copyright 2011-2012 MIT, All rights reserved
4 // Released under the Apache License, Version 2.0
5 // http://www.apache.org/licenses/LICENSE-2.0
6 
7 package com.google.appinventor.components.runtime;
8 
18 
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;
24 
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,
34  nonVisible = true,
35  iconName = "images/gyroscopesensor.png")
36 
37 @SimpleObject
39  implements SensorEventListener, Deleteable, OnPauseListener, OnResumeListener {
40 
41  // Properties
42  private boolean enabled;
43  private float xAngularVelocity; // degrees per second
44  private float yAngularVelocity; // degrees per second
45  private float zAngularVelocity; // degrees per second
46 
47  // Sensor information
48  private final SensorManager sensorManager;
49  private final Sensor gyroSensor;
50  private boolean listening;
51 
55  public GyroscopeSensor(ComponentContainer container) {
56  super(container.$form());
57 
58  // Get sensors, and start listening.
59  sensorManager = (SensorManager) form.getSystemService(Context.SENSOR_SERVICE);
60  gyroSensor = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
61 
62  // Begin listening in onResume() and stop listening in onPause().
65 
66  // Set default property values.
67  Enabled(true);
68  }
69 
70  private void startListening() {
71  if (!listening) {
72  sensorManager.registerListener(this, gyroSensor, SensorManager.SENSOR_DELAY_FASTEST);
73  listening = true;
74  }
75  }
76 
77  private void stopListening() {
78  if (listening) {
79  sensorManager.unregisterListener(this);
80  listening = false;
81 
82  // Throw out sensor information that will go stale.
83  xAngularVelocity = 0;
84  yAngularVelocity = 0;
85  zAngularVelocity = 0;
86  }
87  }
88 
89  // Events
90 
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.")
97  public void GyroscopeChanged(
98  float xAngularVelocity, float yAngularVelocity, float zAngularVelocity, long timestamp) {
99  EventDispatcher.dispatchEvent(this, "GyroscopeChanged",
100  xAngularVelocity, yAngularVelocity, zAngularVelocity, timestamp);
101  }
102 
103  // Properties
104 
112  @SimpleProperty(description = "Indicates whether a gyroscope sensor is available.",
113  category = PropertyCategory.BEHAVIOR)
114  public boolean Available() {
115  return sensorManager.getSensorList(Sensor.TYPE_GYROSCOPE).size() > 0;
116  }
117 
126  public boolean Enabled() {
127  return enabled;
128  }
129 
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.")
142  public void Enabled(boolean enabled) {
143  if (this.enabled != enabled) {
144  this.enabled = enabled;
145  if (enabled) {
146  startListening();
147  } else {
148  stopListening();
149  }
150  }
151  }
152 
161  @SimpleProperty(description = "The angular velocity around the X axis, in degrees per second.",
162  category = PropertyCategory.BEHAVIOR)
163  public float XAngularVelocity() {
164  return xAngularVelocity;
165  }
166 
175  @SimpleProperty(description = "The angular velocity around the Y axis, in degrees per second.",
176  category = PropertyCategory.BEHAVIOR)
177  public float YAngularVelocity() {
178  return yAngularVelocity;
179  }
180 
189  @SimpleProperty(description = "The angular velocity around the Z axis, in degrees per second.",
190  category = PropertyCategory.BEHAVIOR)
191  public float ZAngularVelocity() {
192  return zAngularVelocity;
193  }
194 
195  // SensorListener implementation
196 
202  @Override
203  public void onSensorChanged(SensorEvent sensorEvent) {
204  if (enabled) {
205 
206  xAngularVelocity = (float) Math.toDegrees(sensorEvent.values[0]);
207  yAngularVelocity = (float) Math.toDegrees(sensorEvent.values[1]);
208  zAngularVelocity = (float) Math.toDegrees(sensorEvent.values[2]);
209 
210  // Raise event.
211  GyroscopeChanged(xAngularVelocity, yAngularVelocity, zAngularVelocity,
212  sensorEvent.timestamp);
213  }
214  }
215 
216  @Override
217  public void onAccuracyChanged(Sensor sensor, int accuracy) {
218  }
219 
220  // Deleteable implementation
221 
222  @Override
223  public void onDelete() {
224  stopListening();
225  }
226 
227  // OnPauseListener implementation
228 
229  public void onPause() {
230  stopListening();
231  }
232 
233  // OnResumeListener implementation
234 
235  public void onResume() {
236  if (enabled) {
237  startListening();
238  }
239  }
240 }
com.google.appinventor.components.runtime.EventDispatcher
Definition: EventDispatcher.java:22
com.google.appinventor.components.common.YaVersion
Definition: YaVersion.java:14
com.google.appinventor.components.annotations.DesignerProperty
Definition: DesignerProperty.java:25
com.google.appinventor.components.runtime.Form.registerForOnResume
void registerForOnResume(OnResumeListener component)
Definition: Form.java:740
com.google.appinventor.components
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_BOOLEAN
static final String PROPERTY_TYPE_BOOLEAN
Definition: PropertyTypeConstants.java:35
com.google.appinventor.components.runtime.GyroscopeSensor.onResume
void onResume()
Definition: GyroscopeSensor.java:235
com.google.appinventor.components.annotations.DesignerComponent
Definition: DesignerComponent.java:22
com.google.appinventor.components.annotations.SimpleEvent
Definition: SimpleEvent.java:20
com.google.appinventor.components.annotations.PropertyCategory.BEHAVIOR
BEHAVIOR
Definition: PropertyCategory.java:15
com.google.appinventor.components.runtime.OnResumeListener
Definition: OnResumeListener.java:14
com.google.appinventor.components.runtime.EventDispatcher.dispatchEvent
static boolean dispatchEvent(Component component, String eventName, Object...args)
Definition: EventDispatcher.java:188
com.google.appinventor.components.runtime.AndroidNonvisibleComponent
Definition: AndroidNonvisibleComponent.java:17
com.google.appinventor.components.runtime.OnPauseListener
Definition: OnPauseListener.java:14
com.google.appinventor.components.annotations.SimpleProperty
Definition: SimpleProperty.java:23
com.google.appinventor.components.runtime.GyroscopeSensor.Available
boolean Available()
Definition: GyroscopeSensor.java:114
com.google.appinventor.components.annotations.PropertyCategory
Definition: PropertyCategory.java:13
com.google.appinventor.components.runtime.GyroscopeSensor.XAngularVelocity
float XAngularVelocity()
Definition: GyroscopeSensor.java:163
com.google.appinventor.components.runtime.GyroscopeSensor.onDelete
void onDelete()
Definition: GyroscopeSensor.java:223
com.google.appinventor.components.runtime.ComponentContainer
Definition: ComponentContainer.java:16
com.google.appinventor.components.runtime.GyroscopeSensor.onAccuracyChanged
void onAccuracyChanged(Sensor sensor, int accuracy)
Definition: GyroscopeSensor.java:217
com.google.appinventor.components.runtime.Deleteable
Definition: Deleteable.java:15
com.google.appinventor.components.common
Definition: ComponentCategory.java:7
com.google.appinventor.components.common.ComponentCategory
Definition: ComponentCategory.java:48
com.google.appinventor.components.runtime.GyroscopeSensor
Definition: GyroscopeSensor.java:38
com.google.appinventor.components.runtime.GyroscopeSensor.Enabled
boolean Enabled()
Definition: GyroscopeSensor.java:126
com.google.appinventor.components.runtime.GyroscopeSensor.GyroscopeChanged
void GyroscopeChanged(float xAngularVelocity, float yAngularVelocity, float zAngularVelocity, long timestamp)
Definition: GyroscopeSensor.java:97
com.google.appinventor.components.runtime.Form.registerForOnPause
void registerForOnPause(OnPauseListener component)
Definition: Form.java:780
com.google.appinventor.components.annotations.SimpleObject
Definition: SimpleObject.java:23
com.google.appinventor.components.runtime.GyroscopeSensor.GyroscopeSensor
GyroscopeSensor(ComponentContainer container)
Definition: GyroscopeSensor.java:55
com.google
com
com.google.appinventor.components.runtime.ComponentContainer.$form
Form $form()
com.google.appinventor.components.runtime.GyroscopeSensor.ZAngularVelocity
float ZAngularVelocity()
Definition: GyroscopeSensor.java:191
com.google.appinventor.components.runtime.AndroidNonvisibleComponent.form
final Form form
Definition: AndroidNonvisibleComponent.java:19
com.google.appinventor.components.common.PropertyTypeConstants
Definition: PropertyTypeConstants.java:14
com.google.appinventor.components.runtime.GyroscopeSensor.onSensorChanged
void onSensorChanged(SensorEvent sensorEvent)
Definition: GyroscopeSensor.java:203
com.google.appinventor.components.runtime.GyroscopeSensor.YAngularVelocity
float YAngularVelocity()
Definition: GyroscopeSensor.java:177
com.google.appinventor.components.annotations
com.google.appinventor.components.runtime.GyroscopeSensor.onPause
void onPause()
Definition: GyroscopeSensor.java:229
com.google.appinventor