1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.hardware;
18 
19 /**
20  * This class is the listener used to handle Trigger Sensors.
21  * Trigger Sensors are sensors that trigger an event and are automatically
22  * disabled. {@link Sensor#TYPE_SIGNIFICANT_MOTION} is one such example.
23  * <p>
24  * {@link SensorManager} lets you access the device's {@link android.hardware.Sensor
25  * sensors}. Get an instance of {@link SensorManager} by calling
26  * {@link android.content.Context#getSystemService(java.lang.String)
27  * Context.getSystemService()} with the argument
28  * {@link android.content.Context#SENSOR_SERVICE}.
29  * <p>Here's an example setup for a TriggerEventListener:
30  *
31  * <pre>
32  * class TriggerListener extends TriggerEventListener {
33  *     public void onTrigger(TriggerEvent event) {
34  *          // Do Work.
35  *
36  *     // As it is a one shot sensor, it will be canceled automatically.
37  *     // SensorManager.requestTriggerSensor(this, mSigMotion); needs to
38  *     // be called again, if needed.
39  *     }
40  * }
41  * public class SensorActivity extends Activity {
42  *     private final SensorManager mSensorManager;
43  *     private final Sensor mSigMotion;
44  *     private final TriggerEventListener mListener = new TriggerEventListener();
45  *
46  *     public SensorActivity() {
47  *         mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
48  *         mSigMotion = mSensorManager.getDefaultSensor(Sensor.TYPE_SIGNIFICANT_MOTION);
49  *     }
50  *
51  *     protected void onResume() {
52  *         super.onResume();
53  *         mSensorManager.requestTriggerSensor(mListener, mSigMotion);
54  *     }
55  *
56  *     protected void onPause() {
57  *         super.onPause();
58  *         // Call disable to ensure that the trigger request has been canceled.
59  *         mSensorManager.cancelTriggerSensor(mListener, mSigMotion);
60  *     }
61  *
62  * }
63  * </pre>
64  *
65  * @see TriggerEvent
66  * @see Sensor
67  */
68 public abstract class TriggerEventListener {
69     /**
70      * The method that will be called when the sensor
71      * is triggered. Override this method in your implementation
72      * of this class.
73      *
74      * @param event The details of the event.
75      */
onTrigger(TriggerEvent event)76     public abstract void onTrigger(TriggerEvent event);
77 }
78