blob: dc45f8c09510f01b5d4a102973689a931ee4522b [file] [log] [blame]
keunyoungcc449f72015-08-12 10:46:27 -07001/*
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
17package com.android.car;
18
Keun-young Parke54ac272016-02-16 19:02:18 -080019import android.car.hardware.CarSensorEvent;
Steve Paik289ab992017-07-11 22:40:57 -070020import android.hardware.automotive.vehicle.V2_0.VehiclePropValue;
keunyoungcc449f72015-08-12 10:46:27 -070021
Steve Paik360b7a92018-05-01 20:13:08 -070022import java.util.List;
23
keunyoungcc449f72015-08-12 10:46:27 -070024//TODO add memory pool and recycling
25public class CarSensorEventFactory {
26
Jason Tholstrupd72b5352016-09-22 16:32:14 -070027 public static CarSensorEvent createBooleanEvent(int sensorType, long timestamp,
keunyoungcc449f72015-08-12 10:46:27 -070028 boolean value) {
Steve Paik289ab992017-07-11 22:40:57 -070029 CarSensorEvent event = new CarSensorEvent(sensorType, timestamp, 0, 1, 0);
keunyoungcc449f72015-08-12 10:46:27 -070030 event.intValues[0] = value ? 1 : 0;
31 return event;
32 }
33
Jason Tholstrupd72b5352016-09-22 16:32:14 -070034 public static CarSensorEvent createIntEvent(int sensorType, long timestamp, int value) {
Steve Paik289ab992017-07-11 22:40:57 -070035 CarSensorEvent event = new CarSensorEvent(sensorType, timestamp, 0, 1, 0);
keunyoungcc449f72015-08-12 10:46:27 -070036 event.intValues[0] = value;
37 return event;
38 }
39
Steve Paik360b7a92018-05-01 20:13:08 -070040 /**
41 * Create int64 vector event
42 * @param sensorType
43 * @param timestamp
44 * @param value
45 *
46 * @return CarSensorEvent
47 */
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
Jason Tholstrupd72b5352016-09-22 16:32:14 -070057 public static CarSensorEvent createFloatEvent(int sensorType, long timestamp, float value) {
Steve Paik289ab992017-07-11 22:40:57 -070058 CarSensorEvent event = new CarSensorEvent(sensorType, timestamp, 1, 0, 0);
keunyoungcc449f72015-08-12 10:46:27 -070059 event.floatValues[0] = value;
60 return event;
61 }
62
Steve Paik382ceee2018-01-22 19:21:44 -080063 public static CarSensorEvent createMixedEvent(int sensorType, long timestamp,
Steve Paik289ab992017-07-11 22:40:57 -070064 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
keunyoungcc449f72015-08-12 10:46:27 -070083 public static void returnToPool(CarSensorEvent event) {
84 //TODO
85 }
86}