Home | History | Annotate | Download | only in car
      1 /*
      2  * Copyright (C) 2015 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.android.car;
     18 
     19 import android.car.hardware.CarSensorEvent;
     20 import android.hardware.automotive.vehicle.V2_0.VehiclePropValue;
     21 
     22 //TODO add memory pool and recycling
     23 public class CarSensorEventFactory {
     24 
     25     public static CarSensorEvent createBooleanEvent(int sensorType, long timestamp,
     26             boolean value) {
     27         CarSensorEvent event = new CarSensorEvent(sensorType, timestamp, 0, 1, 0);
     28         event.intValues[0] = value ? 1 : 0;
     29         return event;
     30     }
     31 
     32     public static CarSensorEvent createIntEvent(int sensorType, long timestamp, int value) {
     33         CarSensorEvent event = new CarSensorEvent(sensorType, timestamp, 0, 1, 0);
     34         event.intValues[0] = value;
     35         return event;
     36     }
     37 
     38     public static CarSensorEvent createFloatEvent(int sensorType, long timestamp, float value) {
     39         CarSensorEvent event = new CarSensorEvent(sensorType, timestamp, 1, 0, 0);
     40         event.floatValues[0] = value;
     41         return event;
     42     }
     43 
     44     public static CarSensorEvent createComplexEvent(int sensorType, long timestamp,
     45                                                     VehiclePropValue v) {
     46         int numFloats = v.value.floatValues.size();
     47         int numInts = v.value.int32Values.size();
     48         int numLongs = v.value.int64Values.size();
     49         CarSensorEvent event = new CarSensorEvent(sensorType, timestamp, numFloats, numInts,
     50             numLongs);
     51         // Copy arraylist elements into final arrays
     52         for (int i=0; i<numFloats; i++) {
     53             event.floatValues[i] = v.value.floatValues.get(i);
     54         }
     55         for (int i=0; i<numInts; i++) {
     56             event.intValues[i] = v.value.int32Values.get(i);
     57         }
     58         for (int i=0; i<numLongs; i++) {
     59             event.longValues[i] = v.value.int64Values.get(i);
     60         }
     61         return event;
     62     }
     63 
     64     public static void returnToPool(CarSensorEvent event) {
     65         //TODO
     66     }
     67 }
     68