AI2 Component  (Version nb184)
MagneticFieldSensor.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright 2016-2019 MIT, All rights reserved
3 // Copyright 2017-2019 Kodular, 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 
9 import android.hardware.Sensor;
10 import android.hardware.SensorEvent;
11 import android.hardware.SensorEventListener;
12 import android.hardware.SensorManager;
13 
22 
23 
24 @DesignerComponent(
25  category = ComponentCategory.SENSORS,
26  description = "<p>Non-visible component that measures the ambient geomagnetic field for all three physical axes " +
27  "(x, y, z) in Tesla https://en.wikipedia.org/wiki/Tesla_(unit).</p>",
28  iconName = "images/magneticSensor.png",
29  nonVisible = true,
30  version = YaVersion.MAGNETICFIELDSENSOR_COMPONENT_VERSION)
31 @SimpleObject
32 
34  private double absoluteStrength;
35  private boolean enabled = true;
36  private boolean listening;
37  private Sensor magneticSensor;
38  private final SensorManager sensorManager;
39  private float xStrength;
40  private float yStrength;
41  private float zStrength;
42 
44  super(container.$form());
46  form.registerForOnStop(this);
48  sensorManager = (SensorManager) container.$context().getSystemService("sensor");
49  magneticSensor = sensorManager.getDefaultSensor(2);
50  startListening();
51  }
52 
53  @SimpleProperty(category = PropertyCategory.BEHAVIOR, description = "Indicates that there is a magnetic field " +
54  "sensor in the device and it is available.")
55  public boolean Available() {
56  return sensorManager.getSensorList(2).size() > 0;
57  }
58 
59  @SimpleProperty(category = PropertyCategory.BEHAVIOR, description = "Indicates the maximum range the magnetic " +
60  "sensor can reach.")
61  public float MaximumRange() {
62  return magneticSensor.getMaximumRange();
63  }
64 
65  @SimpleProperty(category = PropertyCategory.BEHAVIOR, description = "Indicates whether or not the magnetic field " +
66  "sensor is enabled and working.")
67  public boolean Enabled() {
68  return enabled;
69  }
70 
71  @DesignerProperty(defaultValue = "True", editorType = "boolean")
73  public void Enabled(boolean localEnabled) {
74  if (enabled != localEnabled) {
75  enabled = localEnabled;
76  }
77  if (enabled) {
78  startListening();
79  } else {
80  stopListening();
81  }
82  }
83 
84  @SimpleEvent(description = "Triggers when magnetic field has changed, setting the new values in parameters.")
85  public void MagneticChanged(float xStrength, float yStrength, float zStrength, double absoluteStrength) {
86  EventDispatcher.dispatchEvent(this, "MagneticChanged", xStrength, yStrength, zStrength, absoluteStrength);
87  }
88 
89  @SimpleProperty(category = PropertyCategory.BEHAVIOR, description = "Indicates the absolute strength of the field.")
90  public double AbsoluteStrength() {
91  return absoluteStrength;
92  }
93 
94  @SimpleProperty(category = PropertyCategory.BEHAVIOR, description = "Indicates the field's strength in the X-axis.")
95  public float XStrength() {
96  return xStrength;
97  }
98 
99  @SimpleProperty(category = PropertyCategory.BEHAVIOR, description = "Indicates the field's strength in the Y-axis.")
100  public float YStrength() {
101  return yStrength;
102  }
103 
104  @SimpleProperty(category = PropertyCategory.BEHAVIOR, description = "Indicates the field's strength in the Z-axis.")
105  public float ZStrength() {
106  return zStrength;
107  }
108 
109  private Sensor getMagneticSensor() {
110  Sensor sensor = sensorManager.getDefaultSensor(2);
111  return sensor != null ? sensor : sensorManager.getDefaultSensor(2);
112  }
113 
114  public void onResume() {
115  if (enabled) {
116  startListening();
117  }
118  }
119 
120  public void onStop() {
121  if (enabled) {
122  stopListening();
123  }
124  }
125 
126  public void onDelete() {
127  if (enabled) {
128  stopListening();
129  }
130  }
131 
132  public void onPause() {
133  stopListening();
134  }
135 
136  private void startListening() {
137  if (!listening && sensorManager != null && magneticSensor != null) {
138  sensorManager.registerListener(this, magneticSensor, 3);
139  listening = true;
140  }
141  }
142 
143  private void stopListening() {
144  if (listening && sensorManager != null) {
145  sensorManager.unregisterListener(this);
146  listening = false;
147  xStrength = 0.0f;
148  yStrength = 0.0f;
149  zStrength = 0.0f;
150  }
151  }
152 
153  public void onSensorChanged(SensorEvent sensorEvent) {
154  if (enabled && sensorEvent.sensor.getType() == 2) {
155  float[] values = (float[]) sensorEvent.values.clone();
156  xStrength = sensorEvent.values[0];
157  yStrength = sensorEvent.values[1];
158  zStrength = sensorEvent.values[2];
159  absoluteStrength = Math.sqrt((double) (((xStrength * xStrength) + (yStrength * yStrength)) + (zStrength * zStrength)));
160  MagneticChanged(xStrength, yStrength, zStrength, absoluteStrength);
161  }
162  }
163 
164  public void onAccuracyChanged(Sensor sensor, int i) {
165  }
166 }
com.google.appinventor.components.runtime.EventDispatcher
Definition: EventDispatcher.java:22
com.google.appinventor.components.runtime.MagneticFieldSensor.AbsoluteStrength
double AbsoluteStrength()
Definition: MagneticFieldSensor.java:90
com.google.appinventor.components.runtime.MagneticFieldSensor.Enabled
void Enabled(boolean localEnabled)
Definition: MagneticFieldSensor.java:73
com.google.appinventor.components.runtime.MagneticFieldSensor.MaximumRange
float MaximumRange()
Definition: MagneticFieldSensor.java:61
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.MagneticFieldSensor.Available
boolean Available()
Definition: MagneticFieldSensor.java:55
com.google.appinventor.components.runtime.Form.registerForOnResume
void registerForOnResume(OnResumeListener component)
Definition: Form.java:740
com.google.appinventor.components
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.MagneticFieldSensor.onSensorChanged
void onSensorChanged(SensorEvent sensorEvent)
Definition: MagneticFieldSensor.java:153
com.google.appinventor.components.runtime.OnResumeListener
Definition: OnResumeListener.java:14
com.google.appinventor.components.runtime.MagneticFieldSensor.onResume
void onResume()
Definition: MagneticFieldSensor.java:114
com.google.appinventor.components.runtime.MagneticFieldSensor
Definition: MagneticFieldSensor.java:33
com.google.appinventor.components.runtime.MagneticFieldSensor.onAccuracyChanged
void onAccuracyChanged(Sensor sensor, int i)
Definition: MagneticFieldSensor.java:164
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.MagneticFieldSensor.ZStrength
float ZStrength()
Definition: MagneticFieldSensor.java:105
com.google.appinventor.components.runtime.MagneticFieldSensor.MagneticFieldSensor
MagneticFieldSensor(ComponentContainer container)
Definition: MagneticFieldSensor.java:43
com.google.appinventor.components.runtime.MagneticFieldSensor.XStrength
float XStrength()
Definition: MagneticFieldSensor.java:95
com.google.appinventor.components.runtime.OnPauseListener
Definition: OnPauseListener.java:14
com.google.appinventor.components.runtime.MagneticFieldSensor.MagneticChanged
void MagneticChanged(float xStrength, float yStrength, float zStrength, double absoluteStrength)
Definition: MagneticFieldSensor.java:85
com.google.appinventor.components.runtime.MagneticFieldSensor.YStrength
float YStrength()
Definition: MagneticFieldSensor.java:100
com.google.appinventor.components.annotations.SimpleProperty
Definition: SimpleProperty.java:23
com.google.appinventor.components.runtime.MagneticFieldSensor.Enabled
boolean Enabled()
Definition: MagneticFieldSensor.java:67
com.google.appinventor.components.runtime.MagneticFieldSensor.onDelete
void onDelete()
Definition: MagneticFieldSensor.java:126
com.google.appinventor.components.annotations.PropertyCategory
Definition: PropertyCategory.java:13
com.google.appinventor.components.runtime.ComponentContainer
Definition: ComponentContainer.java:16
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.OnStopListener
Definition: OnStopListener.java:15
com.google.appinventor.components.runtime.Form.registerForOnPause
void registerForOnPause(OnPauseListener component)
Definition: Form.java:780
com.google.appinventor.components.runtime.Form.registerForOnStop
void registerForOnStop(OnStopListener component)
Definition: Form.java:793
com.google.appinventor.components.annotations.SimpleObject
Definition: SimpleObject.java:23
com.google
com
com.google.appinventor.components.runtime.MagneticFieldSensor.onStop
void onStop()
Definition: MagneticFieldSensor.java:120
com.google.appinventor.components.runtime.MagneticFieldSensor.onPause
void onPause()
Definition: MagneticFieldSensor.java:132
com.google.appinventor.components.runtime.ComponentContainer.$form
Form $form()
com.google.appinventor.components.runtime.ComponentContainer.$context
Activity $context()
com.google.appinventor.components.runtime.AndroidNonvisibleComponent.form
final Form form
Definition: AndroidNonvisibleComponent.java:19
com.google.appinventor.components.runtime.SensorComponent
Definition: SensorComponent.java:16
com.google.appinventor.components.annotations
com.google.appinventor