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 import java.util.List;
23 
24 //TODO add memory pool and recycling
25 public class CarSensorEventFactory {
26 
createBooleanEvent(int sensorType, long timestamp, boolean value)27     public static CarSensorEvent createBooleanEvent(int sensorType, long timestamp,
28             boolean value) {
29         CarSensorEvent event = new CarSensorEvent(sensorType, timestamp, 0, 1, 0);
30         event.intValues[0] = value ? 1 : 0;
31         return event;
32     }
33 
createIntEvent(int sensorType, long timestamp, int value)34     public static CarSensorEvent createIntEvent(int sensorType, long timestamp, int value) {
35         CarSensorEvent event = new CarSensorEvent(sensorType, timestamp, 0, 1, 0);
36         event.intValues[0] = value;
37         return event;
38     }
39 
40     /**
41      * Create int64 vector event
42      * @param sensorType
43      * @param timestamp
44      * @param value
45      *
46      * @return CarSensorEvent
47      */
createInt64VecEvent(int sensorType, long timestamp, List<Long> value)48     public static CarSensorEvent createInt64VecEvent(int sensorType, long timestamp,
49                                                      List<Long> value) {
50         CarSensorEvent event = new CarSensorEvent(sensorType, timestamp, 0, 0, value.size());
51         for (int i = 0; i < value.size(); i++) {
52             event.longValues[i] = value.get(i);
53         }
54         return event;
55     }
56 
createFloatEvent(int sensorType, long timestamp, float value)57     public static CarSensorEvent createFloatEvent(int sensorType, long timestamp, float value) {
58         CarSensorEvent event = new CarSensorEvent(sensorType, timestamp, 1, 0, 0);
59         event.floatValues[0] = value;
60         return event;
61     }
62 
createMixedEvent(int sensorType, long timestamp, VehiclePropValue v)63     public static CarSensorEvent createMixedEvent(int sensorType, long timestamp,
64                                                     VehiclePropValue v) {
65         int numFloats = v.value.floatValues.size();
66         int numInts = v.value.int32Values.size();
67         int numLongs = v.value.int64Values.size();
68         CarSensorEvent event = new CarSensorEvent(sensorType, timestamp, numFloats, numInts,
69             numLongs);
70         // Copy arraylist elements into final arrays
71         for (int i=0; i<numFloats; i++) {
72             event.floatValues[i] = v.value.floatValues.get(i);
73         }
74         for (int i=0; i<numInts; i++) {
75             event.intValues[i] = v.value.int32Values.get(i);
76         }
77         for (int i=0; i<numLongs; i++) {
78             event.longValues[i] = v.value.int64Values.get(i);
79         }
80         return event;
81     }
82 
returnToPool(CarSensorEvent event)83     public static void returnToPool(CarSensorEvent event) {
84         //TODO
85     }
86 }
87