Add Trigger Sensor API Demo.

Add sensor API demo for significant motion sensor.

Change-Id: Ie1a006523e4ef579384a580ddb117188a455dadf
diff --git a/samples/ApiDemos/AndroidManifest.xml b/samples/ApiDemos/AndroidManifest.xml
index 960bdef..8c58bb7 100644
--- a/samples/ApiDemos/AndroidManifest.xml
+++ b/samples/ApiDemos/AndroidManifest.xml
@@ -1205,6 +1205,13 @@
             </intent-filter>
         </activity>
 
+        <activity android:name=".os.TriggerSensors" android:label="OS/TriggerSensors">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.SAMPLE_CODE" />
+            </intent-filter>
+        </activity>
+
         <activity android:name=".os.RotationVectorDemo" android:label="OS/Rotation Vector"  android:screenOrientation="nosensor">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
diff --git a/samples/ApiDemos/res/layout/trigger_sensors.xml b/samples/ApiDemos/res/layout/trigger_sensors.xml
new file mode 100644
index 0000000..c4156e0
--- /dev/null
+++ b/samples/ApiDemos/res/layout/trigger_sensors.xml
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2013 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:orientation="vertical">
+
+    <TextView
+        android:id="@+id/text"
+        android:textSize="20sp"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:background="@drawable/box"
+        android:layout_weight="1"
+        android:scrollbars="vertical"/>
+     />
+</LinearLayout>
diff --git a/samples/ApiDemos/res/values/strings.xml b/samples/ApiDemos/res/values/strings.xml
index ff4b898..0364867 100644
--- a/samples/ApiDemos/res/values/strings.xml
+++ b/samples/ApiDemos/res/values/strings.xml
@@ -879,6 +879,14 @@
     <string name="local_audio">Play Audio from Local File</string>
     <string name="res_audio">Play Audio from Resources</string>
 
+    <!-- =========================== -->
+    <!-- os examples strings    -->
+    <!-- =========================== -->
+    <string name="no_sig_motion">Significant Motion Sensor Not Detected</string>
+    <string name="sig_motion">Significant Motion Sensor Detected</string>
+    <string name="sig_motion_enabled">Significant Motion Sensor Enabled</string>
+    <string name="sig_motion_auto_disabled">Significant Motion Sensor Auto Disabled</string>
+
     <!-- ============================ -->
     <!--  views examples strings  -->
     <!-- ============================ -->
diff --git a/samples/ApiDemos/src/com/example/android/apis/os/TriggerSensors.java b/samples/ApiDemos/src/com/example/android/apis/os/TriggerSensors.java
new file mode 100644
index 0000000..7d4b416
--- /dev/null
+++ b/samples/ApiDemos/src/com/example/android/apis/os/TriggerSensors.java
@@ -0,0 +1,113 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.android.apis.os;
+
+import android.app.Activity;
+import android.content.Context;
+import android.view.View;
+import android.hardware.Sensor;
+import android.hardware.TriggerEvent;
+import android.hardware.TriggerEventListener;
+import android.hardware.SensorManager;
+import android.os.Bundle;
+import android.widget.TextView;
+import com.example.android.apis.R;
+
+/**
+ * <h3>Application showing the Trigger Sensor API for the Significant Motion sensor. </h3>
+
+<p>This demonstrates the {@link android.hardware.SensorManager android.hardware.SensorManager
+   android.hardware.TriggerEventListener} class.
+
+<h4>Demo</h4>
+OS / TriggerSensors
+
+<h4>Source files</h4>
+ * <table class="LinkTable">
+ *         <tr>
+ *             <td >src/com.example.android.apis/os/TriggerSensors.java</td>
+ *             <td >TriggerSensors</td>
+ *         </tr>
+ * </table>
+ */
+
+
+class TriggerListener extends TriggerEventListener {
+    private Context mContext;
+    private TextView mTextView;
+
+    TriggerListener(Context context, TextView textView) {
+        mContext = context;
+        mTextView = textView;
+    }
+
+    @Override
+    public void onTrigger(TriggerEvent event) {
+        if (event.values[0] == 1) {
+            mTextView.append(mContext.getString(R.string.sig_motion) + "\n");
+            mTextView.append(mContext.getString(R.string.sig_motion_auto_disabled) + "\n");
+        }
+        // Sensor is auto disabled.
+    }
+}
+
+public class TriggerSensors extends Activity {
+    private SensorManager mSensorManager;
+    private Sensor mSigMotion;
+    private TriggerListener mListener;
+    private TextView mTextView;
+
+    @Override
+    protected void onResume() {
+        super.onResume();
+        if (mSigMotion != null && mSensorManager.requestTriggerSensor(mListener, mSigMotion))
+                mTextView.append(getString(R.string.sig_motion_enabled) + "\n");
+    }
+
+    @Override
+    protected void onPause() {
+        super.onPause();
+        // Call disable only if needed for cleanup.
+        // The sensor is auto disabled when triggered.
+        if (mSigMotion != null) mSensorManager.cancelTriggerSensor(mListener, mSigMotion);
+    }
+
+
+    /**
+     * Initialization of the Activity after it is first created.  Must at least
+     * call {@link android.app.Activity#setContentView setContentView()} to
+     * describe what is to be displayed in the screen.
+     */
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.trigger_sensors);
+        mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
+        mSigMotion = mSensorManager.getDefaultSensor(Sensor.TYPE_SIGNIFICANT_MOTION);
+        mTextView = (TextView)findViewById(R.id.text);
+        mListener = new TriggerListener(this, mTextView);
+        if (mSigMotion == null) {
+            mTextView.append(getString(R.string.no_sig_motion) + "\n");
+        }
+    }
+
+    @Override
+    protected void onStop() {
+        if (mSigMotion != null) mSensorManager.cancelTriggerSensor(mListener, mSigMotion);
+        super.onStop();
+    }
+}