• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 com.android.car.internal.property;
18 
19 import android.annotation.NonNull;
20 import android.car.builtin.os.ParcelHelper;
21 import android.os.Parcel;
22 import android.os.Parcelable;
23 
24 import com.android.car.internal.util.DataClass;
25 
26 import java.nio.charset.Charset;
27 import java.nio.charset.StandardCharsets;
28 import java.util.Arrays;
29 import java.util.Objects;
30 
31 /**
32  * This is a custom parcelable that stores a property's actual value.
33  *
34  * @param <T> maybe one of the following types:
35  * <ul>
36  * <li>String for STRING type property.</li>
37  * <li>Boolean for BOOLEAN type property.</li>
38  * <li>Integer for INT32 type property.</li>
39  * <li>Integer[] for INT32_VEC type property.</li>
40  * <li>Long for INT64 type property.</li>
41  * <li>Long[] for INT64_VEC type property.</li>
42  * <li>Float for FLOAT type property.</li>
43  * <li>Float[] for FLOAT_VEC.</li>
44  * <li>byte[] for BTYES type property.</li>
45  * <li>Object[] for MIXED type property.</li>
46  * </ul>
47  */
48 @DataClass(genConstructor = false)
49 public final class RawPropertyValue<T> implements Parcelable {
50     private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
51     private static final int TYPE_OTHER = 0;
52     private static final int TYPE_STRING = 1;
53     private static final int TYPE_BYTE_ARRAY = 2;
54 
55     private final @NonNull T mTypedValue;
56 
RawPropertyValue(T typedValue)57     public RawPropertyValue(T typedValue) {
58         mTypedValue = typedValue;
59     }
60 
61     /**
62      * Creates an instance of {@code RawPropertyValue}.
63      *
64      * @param in Parcel to read
65      */
66     @SuppressWarnings("unchecked")
RawPropertyValue(Parcel in)67     public RawPropertyValue(Parcel in) {
68         int type = in.readInt();
69         switch (type) {
70             case TYPE_STRING:
71                 byte[] bytes = ParcelHelper.readBlob(in);
72                 mTypedValue = (T) new String(bytes, DEFAULT_CHARSET);
73                 break;
74             case TYPE_BYTE_ARRAY:
75                 mTypedValue = (T) ParcelHelper.readBlob(in);
76                 break;
77             default:
78                 mTypedValue = (T) in.readValue(getClass().getClassLoader());
79         }
80     }
81 
getTypedValue()82     public T getTypedValue() {
83         return mTypedValue;
84     }
85 
86     @Override
writeToParcel(Parcel dest, int flags)87     public void writeToParcel(Parcel dest, int flags) {
88         Class<?> valueClass =  mTypedValue.getClass();
89 
90         // Special handling for String and byte[] to mitigate transaction buffer limitations.
91         if (valueClass == String.class) {
92             dest.writeInt(TYPE_STRING);
93             ParcelHelper.writeBlob(dest, ((String) mTypedValue).getBytes(DEFAULT_CHARSET));
94         } else if (valueClass == byte[].class) {
95             dest.writeInt(TYPE_BYTE_ARRAY);
96             ParcelHelper.writeBlob(dest, (byte[]) mTypedValue);
97         } else {
98             dest.writeInt(TYPE_OTHER);
99             dest.writeValue(mTypedValue);
100         }
101     }
102 
103     @Override
equals(@ndroid.annotation.Nullable Object o)104     public boolean equals(@android.annotation.Nullable Object o) {
105         if (this == o) {
106             return true;
107         }
108         if (o == null || getClass() != o.getClass()) {
109             return false;
110         }
111         @SuppressWarnings("unchecked")
112         RawPropertyValue<?> that = (RawPropertyValue<?>) o;
113         return Objects.deepEquals(mTypedValue, that.mTypedValue);
114     }
115 
116     @Override
hashCode()117     public int hashCode() {
118         return Arrays.deepHashCode(new Object[]{mTypedValue});
119     }
120 
121     @Override
toString()122     public String toString() {
123         String typedValueString = "";
124         Class<?> valueClass =  mTypedValue.getClass();
125         if (valueClass == Integer[].class) {
126             typedValueString = Arrays.toString((Integer[]) mTypedValue);
127         } else if (valueClass == Long[].class) {
128             typedValueString = Arrays.toString((Long[]) mTypedValue);
129         } else if (valueClass == Float[].class) {
130             typedValueString = Arrays.toString((Float[]) mTypedValue);
131         } else if (valueClass == byte[].class) {
132             typedValueString = Arrays.toString((byte[]) mTypedValue);
133         } else if (valueClass == Object[].class) {
134             typedValueString = Arrays.toString((Object[]) mTypedValue);
135         } else {
136             typedValueString = mTypedValue.toString();
137         }
138         return "RawPropertyValue{typedValue=" + typedValueString + "}";
139     }
140 
141 
142     // Code below generated by codegen v1.0.23.
143     //
144     // DO NOT MODIFY!
145     // CHECKSTYLE:OFF Generated code
146     //
147     // To regenerate run:
148     // $ codegen $ANDROID_BUILD_TOP/packages/services/Car/car-lib/src/com/android/car/internal/property/RawPropertyValue.java
149     //
150     // To exclude the generated code from IntelliJ auto-formatting enable (one-time):
151     //   Settings > Editor > Code Style > Formatter Control
152     //@formatter:off
153 
154     @Override
155     @DataClass.Generated.Member
describeContents()156     public int describeContents() { return 0; }
157 
158     @DataClass.Generated.Member
159     public static final @NonNull Parcelable.Creator<RawPropertyValue> CREATOR
160             = new Parcelable.Creator<RawPropertyValue>() {
161         @Override
162         public RawPropertyValue[] newArray(int size) {
163             return new RawPropertyValue[size];
164         }
165 
166         @Override
167         public RawPropertyValue createFromParcel(@NonNull Parcel in) {
168             return new RawPropertyValue(in);
169         }
170     };
171 
172     @DataClass.Generated(
173             time = 1728329458051L,
174             codegenVersion = "1.0.23",
175             sourceFile = "packages/services/Car/car-lib/src/com/android/car/internal/property/RawPropertyValue.java",
176             inputSignatures = "private static final  java.nio.charset.Charset DEFAULT_CHARSET\nprivate final @android.annotation.NonNull com.android.car.internal.property.T mTypedValue\npublic  com.android.car.internal.property.T getTypedValue()\npublic @java.lang.Override void writeToParcel(android.os.Parcel,int)\npublic @java.lang.Override boolean equals(java.lang.Object)\npublic @java.lang.Override int hashCode()\nclass RawPropertyValue extends java.lang.Object implements [android.os.Parcelable]\n@com.android.car.internal.util.DataClass(genConstructor=false, genToString=true)")
177     @Deprecated
__metadata()178     private void __metadata() {}
179 
180 
181     //@formatter:on
182     // End of generated code
183 
184 }
185