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