1 /*
2  * Copyright (C) 2016 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 android.car.hardware.property;
18 
19 import android.annotation.NonNull;
20 import android.car.hardware.CarPropertyValue;
21 import android.os.Parcel;
22 import android.os.Parcelable;
23 
24 /** @hide */
25 public class CarPropertyEvent implements Parcelable {
26     public static final int PROPERTY_EVENT_PROPERTY_CHANGE = 0;
27     public static final int PROPERTY_EVENT_ERROR = 1;
28 
29     /**
30      * EventType of this message
31      */
32     private final int mEventType;
33     private final CarPropertyValue<?> mCarPropertyValue;
34 
35     // Use it as default value for error events.
36     private static final int ERROR_EVENT_VALUE = -1;
37 
38     /**
39      * @return EventType field
40      */
getEventType()41     public int getEventType() { return mEventType; }
42 
43     /**
44      * Returns {@link CarPropertyValue} associated with this event.
45      */
getCarPropertyValue()46     public CarPropertyValue<?> getCarPropertyValue() { return mCarPropertyValue; }
47 
48     @Override
describeContents()49     public int describeContents() {
50         return 0;
51     }
52 
53     @Override
writeToParcel(Parcel dest, int flags)54     public void writeToParcel(Parcel dest, int flags) {
55         dest.writeInt(mEventType);
56         dest.writeParcelable(mCarPropertyValue, flags);
57     }
58 
59     public static final Parcelable.Creator<CarPropertyEvent> CREATOR
60             = new Parcelable.Creator<CarPropertyEvent>() {
61         public CarPropertyEvent createFromParcel(Parcel in) {
62             return new CarPropertyEvent(in);
63         }
64 
65         public CarPropertyEvent[] newArray(int size) {
66             return new CarPropertyEvent[size];
67         }
68     };
69 
70     /**
71      * Constructor for {@link CarPropertyEvent}.
72      */
CarPropertyEvent(int eventType, @NonNull CarPropertyValue<?> carPropertyValue)73     public CarPropertyEvent(int eventType, @NonNull CarPropertyValue<?> carPropertyValue) {
74         mEventType  = eventType;
75         mCarPropertyValue = carPropertyValue;
76     }
77 
78     /**
79      * Constructor for {@link CarPropertyEvent} when it is an error event.
80      *
81      * The status of {@link CarPropertyValue} should be {@link CarPropertyValue#STATUS_ERROR}.
82      * In {@link CarPropertyManager}, the value of {@link CarPropertyValue} will be dropped.
83      */
createErrorEvent(int propertyId, int areaId)84     public static CarPropertyEvent createErrorEvent(int propertyId, int areaId) {
85         // valueWithErrorCode will not be propagated to listeners
86         CarPropertyValue<Integer> valueWithErrorCode = new CarPropertyValue<>(propertyId, areaId,
87                     CarPropertyValue.STATUS_ERROR, 0, ERROR_EVENT_VALUE);
88         return new CarPropertyEvent(PROPERTY_EVENT_ERROR, valueWithErrorCode);
89     }
90 
CarPropertyEvent(Parcel in)91     private CarPropertyEvent(Parcel in) {
92         mEventType  = in.readInt();
93         mCarPropertyValue = in.readParcelable(CarPropertyValue.class.getClassLoader());
94     }
95 
96     @Override
toString()97     public String toString() {
98         return "CarPropertyEvent{" +
99                 "mEventType=" + mEventType +
100                 ", mCarPropertyValue=" + mCarPropertyValue +
101                 '}';
102     }
103 }
104