AI2 Component  (Version nb184)
ProximitySensor.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-2014 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 
9 import android.content.Context;
10 import android.hardware.Sensor;
11 import android.hardware.SensorEvent;
12 import android.hardware.SensorEventListener;
13 import android.hardware.SensorManager;
18 import java.util.List;
19 
30 @DesignerComponent(version = YaVersion.PROXIMITYSENSOR_COMPONENT_VERSION,
31  description = "<p>Non-visible component that can measures the proximity of an object in cm " +
32  "relative to the view screen of a device. This sensor is typically used to determine " +
33  "whether a handset is being held up to a persons ear; " +
34  "i.e. lets you determine how far away an object is from a device. " +
35  "Many devices return the absolute distance, in cm, but some return only near and far values. " +
36  "In this case, the sensor usually reports its maximum range value in the far state " +
37  "and a lesser value in the near state.</p>",
38  category = ComponentCategory.SENSORS,
39  nonVisible = true,
40  iconName = "images/proximitysensor.png")
44  SensorEventListener, Deleteable {
45 
46  private Sensor proximitySensor;
47 
48  private final SensorManager sensorManager;
49 
50  // Indicates whether the sensor should generate events
51  private boolean enabled;
52  private float distance=0f;
53 
54  // Indicates if the sensor should be running when screen is off (on pause)
55  private boolean keepRunningWhenOnPause;
56 
62  public ProximitySensor(ComponentContainer container) {
63  super(container.$form());
65  form.registerForOnStop(this);
67 
68  enabled = true;
69  sensorManager = (SensorManager) container.$context().getSystemService(Context.SENSOR_SERVICE);
70  proximitySensor = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
71  startListening();
72  }
73 
81  @SimpleProperty(category = PropertyCategory.BEHAVIOR, description = "Reports whether or not the device has a proximity sensor.")
82  public boolean Available() {
83  List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_PROXIMITY);
84  return (sensors.size() > 0);
85  }
86 
87  @Override
88  public void onResume() {
89  if (enabled) {
90  startListening();
91  }
92  }
93 
94  @Override
95  public void onStop() {
96  if (enabled) {
97  stopListening();
98  }
99  }
100 
101  @Override
102  public void onDelete() {
103  if (enabled) {
104  stopListening();
105  }
106  }
107 
108  @Override
109  public void onPause() {
110  if (enabled && !keepRunningWhenOnPause) {
111  stopListening();
112  }
113  }
114 
115 
119  private void startListening() {
120  sensorManager.registerListener(this, proximitySensor, SensorManager.SENSOR_DELAY_NORMAL);
121  }
122 
126  private void stopListening() {
127  sensorManager.unregisterListener(this);
128  }
129 
135  @Override
136  public void onSensorChanged(SensorEvent sensorEvent) {
137  if (enabled) {
138  final float[] values = sensorEvent.values.clone();
139  distance = values[0];
140  ProximityChanged(distance);
141  }
142  }
143 
152  @SimpleProperty(category = PropertyCategory.BEHAVIOR, description = "Reports the Maximum Range of the device's ProximitySensor")
153  public float MaximumRange() {
154  return proximitySensor.getMaximumRange();
155  }
156 
165  public boolean Enabled() {
166  return enabled;
167  }
168 
177  @DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_BOOLEAN, defaultValue = "True")
178  @SimpleProperty (description = "If enabled, then device will listen for changes in proximity.")
179  public void Enabled(boolean enabled) {
180  if (this.enabled == enabled) {
181  return;
182  }
183 
184  this.enabled = enabled;
185  if (enabled) {
186  startListening();
187  } else {
188  stopListening();
189  }
190  }
191 
197  public boolean KeepRunningWhenOnPause() {
198  return keepRunningWhenOnPause;
199  }
200 
207  @DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_BOOLEAN, defaultValue = "False")
208  @SimpleProperty (description = "If set to true, it will keep sensing for proximity changes even when the app is not visible")
209  public void KeepRunningWhenOnPause(boolean enabled) {
210 
211  this.keepRunningWhenOnPause = enabled;
212  }
213 
214  @SimpleEvent(description = "Triggered when distance (in cm) of the object to the device changes. ")
215  public void ProximityChanged(float distance) {
216  this.distance = distance;
217  EventDispatcher.dispatchEvent(this, "ProximityChanged", this.distance);
218  }
219 
226  @SimpleProperty(category = PropertyCategory.BEHAVIOR, description = "Returns the distance from the object to the device")
227  public float Distance() {
228  return distance;
229  }
230 
236  @Override
237  public void onAccuracyChanged(Sensor sensor, int accuracy) {
238  //To change body of implemented methods use File | Settings | File Templates.
239  }
240 }
com.google.appinventor.components.runtime.EventDispatcher
Definition: EventDispatcher.java:22
com.google.appinventor.components.runtime.ProximitySensor.onStop
void onStop()
Definition: ProximitySensor.java:95
com.google.appinventor.components.runtime.ProximitySensor.onDelete
void onDelete()
Definition: ProximitySensor.java:102
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.runtime.ProximitySensor.onAccuracyChanged
void onAccuracyChanged(Sensor sensor, int accuracy)
Definition: ProximitySensor.java:237
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_BOOLEAN
static final String PROPERTY_TYPE_BOOLEAN
Definition: PropertyTypeConstants.java:35
com.google.appinventor.components.runtime.ProximitySensor.onSensorChanged
void onSensorChanged(SensorEvent sensorEvent)
Definition: ProximitySensor.java:136
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.ProximitySensor.ProximityChanged
void ProximityChanged(float distance)
Definition: ProximitySensor.java:215
com.google.appinventor.components.common.ComponentCategory.SENSORS
SENSORS
Definition: ComponentCategory.java:55
com.google.appinventor.components.runtime.OnResumeListener
Definition: OnResumeListener.java:14
com.google.appinventor.components.runtime.ProximitySensor.Available
boolean Available()
Definition: ProximitySensor.java:82
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.ProximitySensor.KeepRunningWhenOnPause
boolean KeepRunningWhenOnPause()
Definition: ProximitySensor.java:197
com.google.appinventor.components.runtime.OnPauseListener
Definition: OnPauseListener.java:14
com.google.appinventor.components.runtime.ProximitySensor.onResume
void onResume()
Definition: ProximitySensor.java:88
com.google.appinventor.components.annotations.SimpleProperty
Definition: SimpleProperty.java:23
com.google.appinventor.components.runtime.ProximitySensor.Distance
float Distance()
Definition: ProximitySensor.java:227
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.ProximitySensor.onPause
void onPause()
Definition: ProximitySensor.java:109
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.ProximitySensor.ProximitySensor
ProximitySensor(ComponentContainer container)
Definition: ProximitySensor.java:62
com.google.appinventor.components.runtime.ComponentContainer.$form
Form $form()
com.google.appinventor.components.runtime.ProximitySensor
Definition: ProximitySensor.java:42
com.google.appinventor.components.runtime.ComponentContainer.$context
Activity $context()
com.google.appinventor.components.runtime.ProximitySensor.Enabled
boolean Enabled()
Definition: ProximitySensor.java:165
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
com.google.appinventor.components.runtime.ProximitySensor.MaximumRange
float MaximumRange()
Definition: ProximitySensor.java:153