/*
 * Copyright (C) 2010 Google Inc.
 *
 * 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.google.ase.facade;

import android.app.Service;
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 com.google.ase.jsonrpc.RpcReceiver;
import com.google.ase.rpc.Rpc;

/**
 * Exposes the SensorManager related functionality.
 * 
 * @author Damon Kohler (damonkohler@gmail.com)
 *         Felix Arends (felix.arends@gmail.com)
 */
public class SensorManagerFacade implements RpcReceiver {
  private final EventFacade mEventFacade;
  private final SensorManager mSensorManager;
  private Bundle mSensorReadings;
  private final SensorEventListener mSensorListener = new SensorEventListener() {
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
      if (mSensorReadings == null) {
        mSensorReadings = new Bundle();
      }
      mSensorReadings.putInt("accuracy", accuracy);
      mEventFacade.postEvent("sensors", mSensorReadings);
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
      if (mSensorReadings == null) {
        mSensorReadings = new Bundle();
      }
      switch (event.sensor.getType()) {
        case Sensor.TYPE_ORIENTATION:
          mSensorReadings.putFloat("azimuth", event.values[0]);
          mSensorReadings.putFloat("pitch", event.values[1]);
          mSensorReadings.putFloat("roll", event.values[2]);
          break;
        case Sensor.TYPE_ACCELEROMETER:
          mSensorReadings.putFloat("xforce", event.values[0]);
          mSensorReadings.putFloat("yforce", event.values[1]);
          mSensorReadings.putFloat("zforce", event.values[2]);
          break;
        case Sensor.TYPE_MAGNETIC_FIELD:
          mSensorReadings.putFloat("xmag", event.values[0]);
          mSensorReadings.putFloat("ymag", event.values[1]);
          mSensorReadings.putFloat("zmag", event.values[2]);
          break;
      }
      mEventFacade.postEvent("sensors", mSensorReadings);
    }
  };
  
  public SensorManagerFacade(Service service, EventFacade eventFacade) {
    this.mEventFacade = eventFacade;    
    this.mSensorManager = (SensorManager)service.getSystemService(Context.SENSOR_SERVICE);
  }

  @Rpc(description = "Starts recording sensor data to be available for polling.")
  public void startSensing() {
    for (Sensor sensor : mSensorManager.getSensorList(Sensor.TYPE_ALL)) {
      mSensorManager.registerListener(mSensorListener, sensor, SensorManager.SENSOR_DELAY_NORMAL);
    }
  }

  @Rpc(description = "Starts recording sensor data to be available for polling.")
  public Bundle readSensors() {
    return mSensorReadings;
  }

  @Rpc(description = "Stops collecting sensor data.")
  public void stopSensing() {
    mSensorManager.unregisterListener(mSensorListener);
    mSensorReadings = null;
  }

  @Override
  public void shutdown() {
    stopSensing();
  }
}

@@ -1079,20 +1079,8 @@
 com)
-%0D%0A *        
  Fel
@@ -1115,16 +1115,16 @@
 l.com)%0D%0A
+
  */%0D%0Apub
@@ -1873,34 +1873,32 @@
 ype()) %7B%0D%0A      
-  
 case Sensor.TYPE
@@ -1912,18 +1912,16 @@
 ATION:%0D%0A
-  
         
@@ -1975,34 +1975,32 @@
 s%5B0%5D);%0D%0A        
- 
- 
 mSensorReadings.
@@ -2036,34 +2036,32 @@
 s%5B1%5D);%0D%0A        
-  
 mSensorReadings.
@@ -2100,34 +2100,32 @@
 );%0D%0A        
- 
- 
 break;%0D%0A
         case
@@ -2104,34 +2104,32 @@
         break;%0D%0A
-  
       case Senso
@@ -2151,34 +2151,32 @@
 METER:%0D%0A        
- 
- 
 mSensorReadings.
@@ -2205,34 +2205,32 @@
 nt.values%5B0%5D);%0D%0A
-  
         mSensorR
@@ -2275,34 +2275,32 @@
 s%5B1%5D);%0D%0A        
- 
- 
 mSensorReadings.
@@ -2341,34 +2341,32 @@
 );%0D%0A        
-  
 break;%0D%0A
         case
@@ -2349,26 +2349,24 @@
     break;%0D%0A
-  
       case S
@@ -2393,34 +2393,32 @@
 FIELD:%0D%0A        
-  
 mSensorReadings.
@@ -2449,26 +2449,24 @@
 alues%5B0%5D);%0D%0A
-  
         mSen
@@ -2517,26 +2517,24 @@
 );%0D%0A        
-  
 mSensorReadi
@@ -2571,24 +2571,130 @@
 ues%5B2%5D);%0D%0A  
+      break;%0D%0A      case Sensor.TYPE_LIGHT:%0D%0A        mSensorReadings.putFloat(%22light%22, event.values%5B0%5D);%0D%0A
         brea
@@ -2778,20 +2778,18 @@
 %0D%0A  %7D;%0D%0A
-  
 %0D%0A
+
   public
@@ -2890,20 +2890,16 @@
 tFacade;
-    
 %0D%0A    th
@@ -2933,16 +2933,17 @@
 Manager)
+ 
 service.

Add light sensor support.