AI2 Component  (Version nb184)
SingleValueSensor.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright 2019 MIT, All rights reserved
3 // Released under the Apache License, Version 2.0
4 // http://www.apache.org/licenses/LICENSE-2.0
5 
6 
7 package com.google.appinventor.components.runtime;
8 
13 
14 import android.content.Context;
15 import android.hardware.Sensor;
16 import android.hardware.SensorEvent;
17 import android.hardware.SensorEventListener;
18 import android.hardware.SensorManager;
19 import android.os.Build;
20 
21 import java.util.List;
22 
26 @SimpleObject
27 public abstract class SingleValueSensor extends AndroidNonvisibleComponent
29  SensorEventListener, Deleteable {
30  private static final int DEFAULT_REFRESH_TIME = 1000; // ms
31  private Sensor sensor;
32  protected int sensorType;
33  protected float value; // most recent value read
34  protected final SensorManager sensorManager;
35  protected boolean enabled;
36  protected int refreshTime;
37 
39  super(container.$form());
40  this.sensorType = sensorType;
43 
44  refreshTime = DEFAULT_REFRESH_TIME;
45  enabled = true;
46  sensorManager = (SensorManager) container.$context().getSystemService(Context.SENSOR_SERVICE);
47  sensor = sensorManager.getDefaultSensor(sensorType);
49  }
50 
51  protected void startListening() {
52  // Before Gingerbread, the only legal values for the third argument
53  // to registerListener() were SENSOR_DELAY_NORMAL, SENSOR_DELAY_UI,
54  // SENSOR_DELAY_GAME, or SENSOR_DELAY_FASTEST. From Gingerbread,
55  // the refresh rate can be requested in microseconds.
56  if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
57  // App Inventor users specify time in milliseconds.
58  int timeInMicroseconds = refreshTime * 1000;
59  sensorManager.registerListener(this, sensor, timeInMicroseconds);
60  } else {
61  sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_UI);
62  }
63  }
64 
65  protected void stopListening() {
66  sensorManager.unregisterListener(this);
67  }
68 
75  @SimpleProperty(description = "Specifies whether or not the device has the "
76  + "hardware to support the %type% component.")
77  public boolean Available() {
78  return isAvailable();
79  }
80 
88  @SimpleProperty(description = "If enabled, then device will listen for changes.")
89  public boolean Enabled() {
90  return enabled;
91  }
92 
102  defaultValue = "True")
104  public void Enabled(boolean enabled) {
106  }
107 
115  description = "The requested minimum time in milliseconds between " +
116  "changes in readings being reported. Android is not guaranteed to honor the request. " +
117  "Setting this property has no effect on pre-Gingerbread devices.")
118  public int RefreshTime() {
119  return refreshTime;
120  }
121 
130  defaultValue = DEFAULT_REFRESH_TIME + "")
132  public void RefreshTime(int time) {
133  refreshTime = time;
134  if (enabled) {
135  stopListening();
136  startListening();
137  }
138  }
139 
140  @Override
141  public void onSensorChanged(SensorEvent sensorEvent) {
142  if (enabled && sensorEvent.sensor.getType() == sensorType) {
143  final float[] values = sensorEvent.values;
144  value = values[0];
146  }
147  }
148 
149  protected abstract void onValueChanged(float value);
150 
151  protected boolean isAvailable() {
152  return sensorManager.getSensorList(sensorType).size() > 0;
153  }
154 
155  protected void setEnabled(boolean enabled) {
156  if (this.enabled == enabled) {
157  return;
158  }
159  this.enabled = enabled;
160  if (enabled) {
161  startListening();
162  } else {
163  stopListening();
164  }
165  }
166 
167  @Override
168  public void onAccuracyChanged(Sensor sensor, int accuracy) {}
169 
170  @Override
171  public void onPause() {
172  if (enabled) {
173  stopListening();
174  }
175  }
176 
177  @Override
178  public void onResume() {
179  if (enabled) {
180  startListening();
181  }
182  }
183 
184  @Override
185  public void onDelete() {
186  if (enabled) {
187  stopListening();
188  }
189  }
190 
191  protected float getValue() {
192  return value;
193  }
194 }
com.google.appinventor.components.runtime.SingleValueSensor.RefreshTime
int RefreshTime()
Definition: SingleValueSensor.java:118
com.google.appinventor.components.runtime.SingleValueSensor.SingleValueSensor
SingleValueSensor(ComponentContainer container, int sensorType)
Definition: SingleValueSensor.java:38
com.google.appinventor.components.runtime.SingleValueSensor.refreshTime
int refreshTime
Definition: SingleValueSensor.java:36
com.google.appinventor.components.runtime.SingleValueSensor.Available
boolean Available()
Definition: SingleValueSensor.java:77
com.google.appinventor.components.annotations.DesignerProperty
Definition: DesignerProperty.java:25
com.google.appinventor.components.runtime.SingleValueSensor.onResume
void onResume()
Definition: SingleValueSensor.java:178
com.google.appinventor.components.runtime.SingleValueSensor.Enabled
boolean Enabled()
Definition: SingleValueSensor.java:89
com.google.appinventor.components.runtime.Form.registerForOnResume
void registerForOnResume(OnResumeListener component)
Definition: Form.java:740
com.google.appinventor.components.runtime.SingleValueSensor.enabled
boolean enabled
Definition: SingleValueSensor.java:35
com.google.appinventor.components.runtime.SingleValueSensor.onPause
void onPause()
Definition: SingleValueSensor.java:171
com.google.appinventor.components
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_NON_NEGATIVE_INTEGER
static final String PROPERTY_TYPE_NON_NEGATIVE_INTEGER
Definition: PropertyTypeConstants.java:206
com.google.appinventor.components.runtime.SingleValueSensor.Enabled
void Enabled(boolean enabled)
Definition: SingleValueSensor.java:104
com.google.appinventor.components.runtime.SingleValueSensor.setEnabled
void setEnabled(boolean enabled)
Definition: SingleValueSensor.java:155
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_BOOLEAN
static final String PROPERTY_TYPE_BOOLEAN
Definition: PropertyTypeConstants.java:35
com.google.appinventor.components.runtime.SingleValueSensor.onValueChanged
abstract void onValueChanged(float value)
com.google.appinventor.components.runtime.SingleValueSensor.RefreshTime
void RefreshTime(int time)
Definition: SingleValueSensor.java:132
com.google.appinventor.components.runtime.SingleValueSensor.getValue
float getValue()
Definition: SingleValueSensor.java:191
com.google.appinventor.components.runtime.OnResumeListener
Definition: OnResumeListener.java:14
com.google.appinventor.components.runtime.SingleValueSensor
Definition: SingleValueSensor.java:27
com.google.appinventor.components.runtime.SingleValueSensor.stopListening
void stopListening()
Definition: SingleValueSensor.java:65
com.google.appinventor.components.runtime.AndroidNonvisibleComponent
Definition: AndroidNonvisibleComponent.java:17
com.google.appinventor.components.runtime.SingleValueSensor.sensorManager
final SensorManager sensorManager
Definition: SingleValueSensor.java:34
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.SingleValueSensor.isAvailable
boolean isAvailable()
Definition: SingleValueSensor.java:151
com.google.appinventor.components.runtime.SingleValueSensor.onSensorChanged
void onSensorChanged(SensorEvent sensorEvent)
Definition: SingleValueSensor.java:141
com.google.appinventor.components.runtime.ComponentContainer
Definition: ComponentContainer.java:16
com.google.appinventor.components.runtime.SingleValueSensor.sensorType
int sensorType
Definition: SingleValueSensor.java:32
com.google.appinventor.components.runtime.Deleteable
Definition: Deleteable.java:15
com.google.appinventor.components.common
Definition: ComponentCategory.java:7
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
com
com.google.appinventor.components.runtime.SingleValueSensor.onDelete
void onDelete()
Definition: SingleValueSensor.java:185
com.google.appinventor.components.runtime.SingleValueSensor.value
float value
Definition: SingleValueSensor.java:33
com.google.appinventor.components.runtime.ComponentContainer.$form
Form $form()
com.google.appinventor.components.runtime.SingleValueSensor.onAccuracyChanged
void onAccuracyChanged(Sensor sensor, int accuracy)
Definition: SingleValueSensor.java:168
com.google.appinventor.components.runtime.ComponentContainer.$context
Activity $context()
com.google.appinventor.components.runtime.SingleValueSensor.startListening
void startListening()
Definition: SingleValueSensor.java:51
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.common.PropertyTypeConstants
Definition: PropertyTypeConstants.java:14
com.google.appinventor.components.annotations
com.google.appinventor