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.os;
18 
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 
22 import libcore.util.NativeAllocationRegistry;
23 
24 /** @hide */
25 public class HwParcel {
26     private static final String TAG = "HwParcel";
27 
28     public static final int STATUS_SUCCESS      = 0;
29     public static final int STATUS_ERROR        = -1;
30 
31     private static final NativeAllocationRegistry sNativeRegistry;
32 
HwParcel(boolean allocate)33     private HwParcel(boolean allocate) {
34         native_setup(allocate);
35 
36         sNativeRegistry.registerNativeAllocation(
37                 this,
38                 mNativeContext);
39     }
40 
HwParcel()41     public HwParcel() {
42         native_setup(true /* allocate */);
43 
44         sNativeRegistry.registerNativeAllocation(
45                 this,
46                 mNativeContext);
47     }
48 
writeInterfaceToken(String interfaceName)49     public native final void writeInterfaceToken(String interfaceName);
writeBool(boolean val)50     public native final void writeBool(boolean val);
writeInt8(byte val)51     public native final void writeInt8(byte val);
writeInt16(short val)52     public native final void writeInt16(short val);
writeInt32(int val)53     public native final void writeInt32(int val);
writeInt64(long val)54     public native final void writeInt64(long val);
writeFloat(float val)55     public native final void writeFloat(float val);
writeDouble(double val)56     public native final void writeDouble(double val);
writeString(String val)57     public native final void writeString(String val);
58 
writeBoolVector(boolean[] val)59     private native final void writeBoolVector(boolean[] val);
writeInt8Vector(byte[] val)60     private native final void writeInt8Vector(byte[] val);
writeInt16Vector(short[] val)61     private native final void writeInt16Vector(short[] val);
writeInt32Vector(int[] val)62     private native final void writeInt32Vector(int[] val);
writeInt64Vector(long[] val)63     private native final void writeInt64Vector(long[] val);
writeFloatVector(float[] val)64     private native final void writeFloatVector(float[] val);
writeDoubleVector(double[] val)65     private native final void writeDoubleVector(double[] val);
writeStringVector(String[] val)66     private native final void writeStringVector(String[] val);
67 
writeBoolVector(ArrayList<Boolean> val)68     public final void writeBoolVector(ArrayList<Boolean> val) {
69         final int n = val.size();
70         boolean[] array = new boolean[n];
71         for (int i = 0; i < n; ++i) {
72             array[i] = val.get(i);
73         }
74 
75         writeBoolVector(array);
76     }
77 
writeInt8Vector(ArrayList<Byte> val)78     public final void writeInt8Vector(ArrayList<Byte> val) {
79         final int n = val.size();
80         byte[] array = new byte[n];
81         for (int i = 0; i < n; ++i) {
82             array[i] = val.get(i);
83         }
84 
85         writeInt8Vector(array);
86     }
87 
writeInt16Vector(ArrayList<Short> val)88     public final void writeInt16Vector(ArrayList<Short> val) {
89         final int n = val.size();
90         short[] array = new short[n];
91         for (int i = 0; i < n; ++i) {
92             array[i] = val.get(i);
93         }
94 
95         writeInt16Vector(array);
96     }
97 
writeInt32Vector(ArrayList<Integer> val)98     public final void writeInt32Vector(ArrayList<Integer> val) {
99         final int n = val.size();
100         int[] array = new int[n];
101         for (int i = 0; i < n; ++i) {
102             array[i] = val.get(i);
103         }
104 
105         writeInt32Vector(array);
106     }
107 
writeInt64Vector(ArrayList<Long> val)108     public final void writeInt64Vector(ArrayList<Long> val) {
109         final int n = val.size();
110         long[] array = new long[n];
111         for (int i = 0; i < n; ++i) {
112             array[i] = val.get(i);
113         }
114 
115         writeInt64Vector(array);
116     }
117 
writeFloatVector(ArrayList<Float> val)118     public final void writeFloatVector(ArrayList<Float> val) {
119         final int n = val.size();
120         float[] array = new float[n];
121         for (int i = 0; i < n; ++i) {
122             array[i] = val.get(i);
123         }
124 
125         writeFloatVector(array);
126     }
127 
writeDoubleVector(ArrayList<Double> val)128     public final void writeDoubleVector(ArrayList<Double> val) {
129         final int n = val.size();
130         double[] array = new double[n];
131         for (int i = 0; i < n; ++i) {
132             array[i] = val.get(i);
133         }
134 
135         writeDoubleVector(array);
136     }
137 
writeStringVector(ArrayList<String> val)138     public final void writeStringVector(ArrayList<String> val) {
139         writeStringVector(val.toArray(new String[val.size()]));
140     }
141 
writeStrongBinder(IHwBinder binder)142     public native final void writeStrongBinder(IHwBinder binder);
143 
enforceInterface(String interfaceName)144     public native final void enforceInterface(String interfaceName);
readBool()145     public native final boolean readBool();
readInt8()146     public native final byte readInt8();
readInt16()147     public native final short readInt16();
readInt32()148     public native final int readInt32();
readInt64()149     public native final long readInt64();
readFloat()150     public native final float readFloat();
readDouble()151     public native final double readDouble();
readString()152     public native final String readString();
153 
readBoolVectorAsArray()154     private native final boolean[] readBoolVectorAsArray();
readInt8VectorAsArray()155     private native final byte[] readInt8VectorAsArray();
readInt16VectorAsArray()156     private native final short[] readInt16VectorAsArray();
readInt32VectorAsArray()157     private native final int[] readInt32VectorAsArray();
readInt64VectorAsArray()158     private native final long[] readInt64VectorAsArray();
readFloatVectorAsArray()159     private native final float[] readFloatVectorAsArray();
readDoubleVectorAsArray()160     private native final double[] readDoubleVectorAsArray();
readStringVectorAsArray()161     private native final String[] readStringVectorAsArray();
162 
readBoolVector()163     public final ArrayList<Boolean> readBoolVector() {
164         Boolean[] array = HwBlob.wrapArray(readBoolVectorAsArray());
165 
166         return new ArrayList<Boolean>(Arrays.asList(array));
167     }
168 
readInt8Vector()169     public final ArrayList<Byte> readInt8Vector() {
170         Byte[] array = HwBlob.wrapArray(readInt8VectorAsArray());
171 
172         return new ArrayList<Byte>(Arrays.asList(array));
173     }
174 
readInt16Vector()175     public final ArrayList<Short> readInt16Vector() {
176         Short[] array = HwBlob.wrapArray(readInt16VectorAsArray());
177 
178         return new ArrayList<Short>(Arrays.asList(array));
179     }
180 
readInt32Vector()181     public final ArrayList<Integer> readInt32Vector() {
182         Integer[] array = HwBlob.wrapArray(readInt32VectorAsArray());
183 
184         return new ArrayList<Integer>(Arrays.asList(array));
185     }
186 
readInt64Vector()187     public final ArrayList<Long> readInt64Vector() {
188         Long[] array = HwBlob.wrapArray(readInt64VectorAsArray());
189 
190         return new ArrayList<Long>(Arrays.asList(array));
191     }
192 
readFloatVector()193     public final ArrayList<Float> readFloatVector() {
194         Float[] array = HwBlob.wrapArray(readFloatVectorAsArray());
195 
196         return new ArrayList<Float>(Arrays.asList(array));
197     }
198 
readDoubleVector()199     public final ArrayList<Double> readDoubleVector() {
200         Double[] array = HwBlob.wrapArray(readDoubleVectorAsArray());
201 
202         return new ArrayList<Double>(Arrays.asList(array));
203     }
204 
readStringVector()205     public final ArrayList<String> readStringVector() {
206         return new ArrayList<String>(Arrays.asList(readStringVectorAsArray()));
207     }
208 
readStrongBinder()209     public native final IHwBinder readStrongBinder();
210 
211     // Handle is stored as part of the blob.
readBuffer(long expectedSize)212     public native final HwBlob readBuffer(long expectedSize);
213 
readEmbeddedBuffer( long expectedSize, long parentHandle, long offset, boolean nullable)214     public native final HwBlob readEmbeddedBuffer(
215             long expectedSize, long parentHandle, long offset,
216             boolean nullable);
217 
writeBuffer(HwBlob blob)218     public native final void writeBuffer(HwBlob blob);
219 
writeStatus(int status)220     public native final void writeStatus(int status);
verifySuccess()221     public native final void verifySuccess();
releaseTemporaryStorage()222     public native final void releaseTemporaryStorage();
release()223     public native final void release();
224 
send()225     public native final void send();
226 
227     // Returns address of the "freeFunction".
native_init()228     private static native final long native_init();
229 
native_setup(boolean allocate)230     private native final void native_setup(boolean allocate);
231 
232     static {
233         long freeFunction = native_init();
234 
235         sNativeRegistry = new NativeAllocationRegistry(
236                 HwParcel.class.getClassLoader(),
237                 freeFunction,
238                 128 /* size */);
239     }
240 
241     private long mNativeContext;
242 }
243 
244