Home | History | Annotate | Download | only in os
      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 com.example.android.apis.os;
     18 
     19 import android.app.Activity;
     20 import android.content.Context;
     21 import android.view.View;
     22 import android.hardware.Sensor;
     23 import android.hardware.TriggerEvent;
     24 import android.hardware.TriggerEventListener;
     25 import android.hardware.SensorManager;
     26 import android.os.Bundle;
     27 import android.widget.TextView;
     28 import com.example.android.apis.R;
     29 
     30 /**
     31  * <h3>Application showing the Trigger Sensor API for the Significant Motion sensor. </h3>
     32 
     33 <p>This demonstrates the {@link android.hardware.SensorManager android.hardware.SensorManager
     34    android.hardware.TriggerEventListener} class.
     35 
     36 <h4>Demo</h4>
     37 OS / TriggerSensors
     38 
     39 <h4>Source files</h4>
     40  * <table class="LinkTable">
     41  *         <tr>
     42  *             <td >src/com.example.android.apis/os/TriggerSensors.java</td>
     43  *             <td >TriggerSensors</td>
     44  *         </tr>
     45  * </table>
     46  */
     47 
     48 
     49 class TriggerListener extends TriggerEventListener {
     50     private Context mContext;
     51     private TextView mTextView;
     52 
     53     TriggerListener(Context context, TextView textView) {
     54         mContext = context;
     55         mTextView = textView;
     56     }
     57 
     58     @Override
     59     public void onTrigger(TriggerEvent event) {
     60         if (event.values[0] == 1) {
     61             mTextView.append(mContext.getString(R.string.sig_motion) + "\n");
     62             mTextView.append(mContext.getString(R.string.sig_motion_auto_disabled) + "\n");
     63         }
     64         // Sensor is auto disabled.
     65     }
     66 }
     67 
     68 public class TriggerSensors extends Activity {
     69     private SensorManager mSensorManager;
     70     private Sensor mSigMotion;
     71     private TriggerListener mListener;
     72     private TextView mTextView;
     73 
     74     @Override
     75     protected void onResume() {
     76         super.onResume();
     77         if (mSigMotion != null && mSensorManager.requestTriggerSensor(mListener, mSigMotion))
     78                 mTextView.append(getString(R.string.sig_motion_enabled) + "\n");
     79     }
     80 
     81     @Override
     82     protected void onPause() {
     83         super.onPause();
     84         // Call disable only if needed for cleanup.
     85         // The sensor is auto disabled when triggered.
     86         if (mSigMotion != null) mSensorManager.cancelTriggerSensor(mListener, mSigMotion);
     87     }
     88 
     89 
     90     /**
     91      * Initialization of the Activity after it is first created.  Must at least
     92      * call {@link android.app.Activity#setContentView setContentView()} to
     93      * describe what is to be displayed in the screen.
     94      */
     95     @Override
     96     protected void onCreate(Bundle savedInstanceState) {
     97         super.onCreate(savedInstanceState);
     98         setContentView(R.layout.trigger_sensors);
     99         mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
    100         mSigMotion = mSensorManager.getDefaultSensor(Sensor.TYPE_SIGNIFICANT_MOTION);
    101         mTextView = (TextView)findViewById(R.id.text);
    102         mListener = new TriggerListener(this, mTextView);
    103         if (mSigMotion == null) {
    104             mTextView.append(getString(R.string.no_sig_motion) + "\n");
    105         }
    106     }
    107 
    108     @Override
    109     protected void onStop() {
    110         if (mSigMotion != null) mSensorManager.cancelTriggerSensor(mListener, mSigMotion);
    111         super.onStop();
    112     }
    113 }
    114