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 * EventType of this message 30 */ 31 private final int mEventType; 32 33 /** 34 * ErrorCode of this message. 35 */ 36 private final @CarPropertyManager.CarSetPropertyErrorCode int mErrorCode; 37 private final CarPropertyValue<?> mCarPropertyValue; 38 39 // Use it as default value for error events. 40 private static final int ERROR_EVENT_VALUE = -1; 41 42 /** 43 * @return EventType field 44 */ getEventType()45 public int getEventType() { 46 return mEventType; 47 } 48 49 /** 50 * Returns {@link CarPropertyValue} associated with this event. 51 */ getCarPropertyValue()52 public CarPropertyValue<?> getCarPropertyValue() { 53 return mCarPropertyValue; 54 } 55 56 @Override describeContents()57 public int describeContents() { 58 return 0; 59 } 60 61 @Override writeToParcel(Parcel dest, int flags)62 public void writeToParcel(Parcel dest, int flags) { 63 dest.writeInt(mEventType); 64 dest.writeInt(mErrorCode); 65 dest.writeParcelable(mCarPropertyValue, flags); 66 } 67 68 public static final Parcelable.Creator<CarPropertyEvent> CREATOR = 69 new Parcelable.Creator<CarPropertyEvent>() { 70 71 @Override 72 public CarPropertyEvent createFromParcel(Parcel in) { 73 return new CarPropertyEvent(in); 74 } 75 76 @Override 77 public CarPropertyEvent[] newArray(int size) { 78 return new CarPropertyEvent[size]; 79 } 80 }; 81 82 /** 83 * Constructor for {@link CarPropertyEvent}. 84 */ CarPropertyEvent(int eventType, @NonNull CarPropertyValue<?> carPropertyValue)85 public CarPropertyEvent(int eventType, @NonNull CarPropertyValue<?> carPropertyValue) { 86 mEventType = eventType; 87 mErrorCode = CarPropertyManager.CAR_SET_PROPERTY_ERROR_CODE_UNKNOWN; 88 mCarPropertyValue = carPropertyValue; 89 } 90 91 /** 92 * Constructor for {@link CarPropertyEvent} with an error code. 93 */ CarPropertyEvent(int eventType, @NonNull CarPropertyValue<?> carPropertyValue, @CarPropertyManager.CarSetPropertyErrorCode int errorCode)94 public CarPropertyEvent(int eventType, @NonNull CarPropertyValue<?> carPropertyValue, 95 @CarPropertyManager.CarSetPropertyErrorCode int errorCode) { 96 mEventType = eventType; 97 mErrorCode = errorCode; 98 mCarPropertyValue = carPropertyValue; 99 } 100 101 102 /** 103 * Constructor for {@link CarPropertyEvent} when it is an error event. 104 * 105 * The status of {@link CarPropertyValue} should be {@link CarPropertyValue#STATUS_ERROR}. 106 * In {@link CarPropertyManager}, the value of {@link CarPropertyValue} will be dropped. 107 */ createErrorEventWithErrorCode(int propertyId, int areaId, @CarPropertyManager.CarSetPropertyErrorCode int errorCode)108 public static CarPropertyEvent createErrorEventWithErrorCode(int propertyId, int areaId, 109 @CarPropertyManager.CarSetPropertyErrorCode int errorCode) { 110 CarPropertyValue<Integer> valueWithErrorCode = new CarPropertyValue<>(propertyId, areaId, 111 CarPropertyValue.STATUS_ERROR, 0, ERROR_EVENT_VALUE); 112 CarPropertyEvent event = new CarPropertyEvent(PROPERTY_EVENT_ERROR, valueWithErrorCode, 113 errorCode); 114 return event; 115 } 116 getErrorCode()117 public @CarPropertyManager.CarSetPropertyErrorCode int getErrorCode() { 118 return mErrorCode; 119 } 120 CarPropertyEvent(Parcel in)121 private CarPropertyEvent(Parcel in) { 122 mEventType = in.readInt(); 123 mErrorCode = in.readInt(); 124 mCarPropertyValue = in.readParcelable(CarPropertyValue.class.getClassLoader()); 125 } 126 127 @Override toString()128 public String toString() { 129 return "CarPropertyEvent{" 130 + "mEventType=" + mEventType 131 + ", mErrorCode=" + mErrorCode 132 + ", mCarPropertyValue=" + mCarPropertyValue 133 + '}'; 134 } 135 } 136