1 /*
2  * Copyright 2020 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 androidx.work.multiprocess.parcelable;
18 
19 import android.os.Build;
20 import android.os.Parcel;
21 import android.os.Parcelable;
22 
23 import androidx.annotation.RestrictTo;
24 
25 import org.jspecify.annotations.NonNull;
26 
27 /**
28  *
29  */
30 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
31 public final class ParcelUtils {
ParcelUtils()32     private ParcelUtils() {
33         // Does nothing
34     }
35 
36     /**
37      * Reads a boolean value from a parcel.
38      */
readBooleanValue(@onNull Parcel parcel)39     public static boolean readBooleanValue(@NonNull Parcel parcel) {
40         int value = parcel.readInt();
41         return value == 1;
42     }
43 
44     /**
45      * Writes a boolean value into a parcel,
46      */
writeBooleanValue(@onNull Parcel parcel, boolean value)47     public static void writeBooleanValue(@NonNull Parcel parcel, boolean value) {
48         parcel.writeInt(value ? 1 : 0);
49     }
50 
51     /**
52      * Reads a parcelable from a parcel.
53      * <p>
54      * It's safe for us to suppress warnings here, given the correct API is being used starting
55      * API 33.
56      */
57     @SuppressWarnings("deprecation")
readParcelable( @onNull Parcel parcel, @NonNull Class<T> klass )58     public static <T extends Parcelable> T readParcelable(
59             @NonNull Parcel parcel,
60             @NonNull Class<T> klass
61     ) {
62         ClassLoader classLoader = klass.getClassLoader();
63         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
64             return parcel.readParcelable(classLoader, klass);
65         } else {
66             return parcel.readParcelable(classLoader);
67         }
68     }
69 }
70