/*
 * Copyright 2016, 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.androidthings.driversamples;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;

import com.google.android.things.contrib.driver.bmx280.Bmx280SensorDriver;

import java.io.IOException;

/**
 * TemperatureActivity is an example that use the driver for the BMP280 temperature sensor.
 */
public class TemperatureActivity extends Activity implements SensorEventListener {
    private static final String TAG = TemperatureActivity.class.getSimpleName();

    private Bmx280SensorDriver mTemperatureSensorDriver;
    private SensorManager mSensorManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG, "Starting TemperatureActivity");

        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mSensorManager.registerDynamicSensorCallback(new SensorManager.DynamicSensorCallback() {
            @Override
            public void onDynamicSensorConnected(Sensor sensor) {
                if (sensor.getType() == Sensor.TYPE_AMBIENT_TEMPERATURE) {
                    Log.i(TAG, "Temperature sensor connected");
                    mSensorManager.registerListener(TemperatureActivity.this,
                            sensor, SensorManager.SENSOR_DELAY_NORMAL);
                }
            }
        });

        try {
            mTemperatureSensorDriver = new Bmx280SensorDriver(BoardDefaults.getI2CPort());
            mTemperatureSensorDriver.registerTemperatureSensor();
        } catch (IOException e) {
            Log.e(TAG, "Error configuring sensor", e);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "Closing sensor");
        if (mTemperatureSensorDriver != null) {
            mSensorManager.unregisterListener(this);
            mTemperatureSensorDriver.unregisterTemperatureSensor();
            try {
                mTemperatureSensorDriver.close();
            } catch (IOException e) {
                Log.e(TAG, "Error closing sensor", e);
            } finally {
                mTemperatureSensorDriver = null;
            }
        }
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        Log.i(TAG, "sensor changed: " + event.values[0]);
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        Log.i(TAG, "sensor accuracy changed: " + accuracy);
    }
}

@@ -871,24 +871,85 @@
 sorManager;%0A
+import android.hardware.SensorManager.DynamicSensorCallback;%0A
 import andro
@@ -1466,286 +1466,39 @@
     
-@Override%0A    protected void onCreate(Bundle savedInstanceState) %7B%0A        super.onCreate(savedInstanceState);%0A        Log.i(TAG, %22Starting TemperatureActivity%22);%0A%0A        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);%0A        mSensorManager.register
+private DynamicSensorCallback m
 Dyna
@@ -1518,27 +1518,15 @@
 back
-(
+ = 
 new 
-SensorManager.
 Dyna
@@ -1555,20 +1555,16 @@
         
-   
- 
 @Overrid
@@ -1565,20 +1565,16 @@
 verride%0A
-    
         
@@ -1635,28 +1635,24 @@
             
-    
 if (sensor.g
@@ -1710,28 +1710,24 @@
             
- 
-   
 Log.i(TAG, %22
@@ -1754,28 +1754,24 @@
 onnected%22);%0A
-    
             
@@ -1856,20 +1856,16 @@
         
-   
- 
 sensor, 
@@ -1900,32 +1900,38 @@
 ORMAL);%0A        
+    %7D%0A
         %7D%0A      
@@ -1928,35 +1928,346 @@
   %7D%0A    
-        %7D%0A        %7D
+%7D;%0A%0A    @Override%0A    protected void onCreate(Bundle savedInstanceState) %7B%0A        super.onCreate(savedInstanceState);%0A        Log.i(TAG, %22Starting TemperatureActivity%22);%0A%0A        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);%0A        mSensorManager.registerDynamicSensorCallback(mDynamicSensorCallback
 );%0A%0A    
@@ -2695,24 +2695,108 @@
  != null) %7B%0A
+            mSensorManager.unregisterDynamicSensorCallback(mDynamicSensorCallback);%0A
             

make sure dynamic sensor callback is unregistered