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 static com.android.car.internal.ExcludeFromCodeCoverageGeneratedReport.BOILERPLATE_CODE; 20 21 import android.annotation.NonNull; 22 import android.car.hardware.CarPropertyValue; 23 import android.os.Parcel; 24 import android.os.Parcelable; 25 26 import com.android.car.internal.ExcludeFromCodeCoverageGeneratedReport; 27 28 import java.util.Objects; 29 30 /** @hide */ 31 public class CarPropertyEvent implements Parcelable { 32 public static final int PROPERTY_EVENT_PROPERTY_CHANGE = 0; 33 public static final int PROPERTY_EVENT_ERROR = 1; 34 /** 35 * EventType of this message 36 */ 37 private final int mEventType; 38 39 /** 40 * ErrorCode of this message. 41 */ 42 private final @CarPropertyManager.CarSetPropertyErrorCode int mErrorCode; 43 private final CarPropertyValue<?> mCarPropertyValue; 44 45 // Use it as default value for error events. 46 private static final int ERROR_EVENT_VALUE = -1; 47 48 /** 49 * @return EventType field 50 */ getEventType()51 public int getEventType() { 52 return mEventType; 53 } 54 55 /** 56 * Returns {@link CarPropertyValue} associated with this event. 57 */ getCarPropertyValue()58 public CarPropertyValue<?> getCarPropertyValue() { 59 return mCarPropertyValue; 60 } 61 62 @Override 63 @ExcludeFromCodeCoverageGeneratedReport(reason = BOILERPLATE_CODE) describeContents()64 public int describeContents() { 65 return 0; 66 } 67 68 @Override writeToParcel(Parcel dest, int flags)69 public void writeToParcel(Parcel dest, int flags) { 70 dest.writeInt(mEventType); 71 dest.writeInt(mErrorCode); 72 dest.writeParcelable(mCarPropertyValue, flags); 73 } 74 75 public static final Parcelable.Creator<CarPropertyEvent> CREATOR = 76 new Parcelable.Creator<CarPropertyEvent>() { 77 78 @Override 79 public CarPropertyEvent createFromParcel(Parcel in) { 80 return new CarPropertyEvent(in); 81 } 82 83 @Override 84 public CarPropertyEvent[] newArray(int size) { 85 return new CarPropertyEvent[size]; 86 } 87 }; 88 89 /** 90 * Constructor for {@link CarPropertyEvent}. 91 */ CarPropertyEvent(int eventType, @NonNull CarPropertyValue<?> carPropertyValue)92 public CarPropertyEvent(int eventType, @NonNull CarPropertyValue<?> carPropertyValue) { 93 mEventType = eventType; 94 mErrorCode = CarPropertyManager.CAR_SET_PROPERTY_ERROR_CODE_UNKNOWN; 95 mCarPropertyValue = carPropertyValue; 96 } 97 98 /** 99 * Constructor for {@link CarPropertyEvent} with an error code. 100 */ CarPropertyEvent(int eventType, @NonNull CarPropertyValue<?> carPropertyValue, @CarPropertyManager.CarSetPropertyErrorCode int errorCode)101 public CarPropertyEvent(int eventType, @NonNull CarPropertyValue<?> carPropertyValue, 102 @CarPropertyManager.CarSetPropertyErrorCode int errorCode) { 103 mEventType = eventType; 104 mErrorCode = errorCode; 105 mCarPropertyValue = carPropertyValue; 106 } 107 108 109 /** 110 * Constructor for {@link CarPropertyEvent} when it is an error event. 111 * 112 * The {@link CarPropertyValue} in the event is only used to pass property ID and area ID to 113 * {@link CarPropertyManager}. In {@link CarPropertyManager}, the value of 114 * {@link CarPropertyValue} will be dropped. 115 */ createErrorEventWithErrorCode(int propertyId, int areaId, @CarPropertyManager.CarSetPropertyErrorCode int errorCode)116 public static CarPropertyEvent createErrorEventWithErrorCode(int propertyId, int areaId, 117 @CarPropertyManager.CarSetPropertyErrorCode int errorCode) { 118 // We don't care about about timestamp and value here. We are only using the 119 // CarPropertyValue to pass propertyId and areaId. 120 CarPropertyValue<Integer> valueWithErrorCode = new CarPropertyValue<>(propertyId, areaId, 121 /* timestampNanos= */ 0, ERROR_EVENT_VALUE); 122 CarPropertyEvent event = new CarPropertyEvent(PROPERTY_EVENT_ERROR, valueWithErrorCode, 123 errorCode); 124 return event; 125 } 126 getErrorCode()127 public @CarPropertyManager.CarSetPropertyErrorCode int getErrorCode() { 128 return mErrorCode; 129 } 130 CarPropertyEvent(Parcel in)131 private CarPropertyEvent(Parcel in) { 132 mEventType = in.readInt(); 133 mErrorCode = in.readInt(); 134 mCarPropertyValue = in.readParcelable(CarPropertyValue.class.getClassLoader()); 135 } 136 137 @Override toString()138 public String toString() { 139 return "CarPropertyEvent{" 140 + "mEventType=" + mEventType 141 + ", mErrorCode=" + mErrorCode 142 + ", mCarPropertyValue=" + mCarPropertyValue 143 + '}'; 144 } 145 146 /** Checks equality with passed {@code object}. */ 147 @Override equals(Object object)148 public boolean equals(Object object) { 149 if (this == object) { 150 return true; 151 } 152 if (!(object instanceof CarPropertyEvent)) { 153 return false; 154 } 155 CarPropertyEvent carPropertyEvent = (CarPropertyEvent) object; 156 return mEventType == carPropertyEvent.mEventType 157 && mErrorCode == carPropertyEvent.mErrorCode && mCarPropertyValue.equals( 158 carPropertyEvent.mCarPropertyValue); 159 } 160 161 /** Generates hash code for this instance. */ 162 @Override hashCode()163 public int hashCode() { 164 return Objects.hash(mEventType, mErrorCode, mCarPropertyValue); 165 } 166 } 167