1 /* 2 * Copyright (C) 2022 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.queryable.util; 18 19 import android.os.Parcel; 20 import android.os.Parcelable; 21 22 import java.util.ArrayList; 23 import java.util.HashSet; 24 import java.util.List; 25 import java.util.Set; 26 27 /** Utilities for {@link Parcelable}. */ 28 public final class ParcelableUtils { ParcelableUtils()29 private ParcelableUtils() { 30 31 } 32 33 /** Write a {@link java.lang.Boolean} value. */ writeNullableBoolean(Parcel parcel, Boolean b)34 public static void writeNullableBoolean(Parcel parcel, Boolean b) { 35 writeBoolean(parcel, b != null); 36 if (b != null) { 37 writeBoolean(parcel, b); 38 } 39 } 40 41 /** Read a {@link java.lang.Boolean} value written with {@link #writeNullableBoolean}). */ readNullableBoolean(Parcel parcel)42 public static Boolean readNullableBoolean(Parcel parcel) { 43 if (!readBoolean(parcel)) { 44 return null; 45 } 46 return readBoolean(parcel); 47 } 48 49 /** Write a {@link java.lang.Integer} value. */ writeNullableInt(Parcel parcel, Integer integer)50 public static void writeNullableInt(Parcel parcel, Integer integer) { 51 writeBoolean(parcel, integer != null); 52 if (integer != null) { 53 parcel.writeInt(integer); 54 } 55 } 56 57 /** Read a {@link java.lang.Integer} value written with {@link #writeNullableInt). */ readNullableInt(Parcel parcel)58 public static Integer readNullableInt(Parcel parcel) { 59 if (!readBoolean(parcel)) { 60 return null; 61 } 62 return parcel.readInt(); 63 } 64 65 /** Write a {@link java.lang.Long} value. */ writeNullableLong(Parcel parcel, Long value)66 public static void writeNullableLong(Parcel parcel, Long value) { 67 writeBoolean(parcel, value != null); 68 if (value != null) { 69 parcel.writeLong(value); 70 } 71 } 72 73 /** Read a {@link java.lang.Long} value written with {@link #writeNullableLong). */ readNullableLong(Parcel parcel)74 public static Long readNullableLong(Parcel parcel) { 75 if (!readBoolean(parcel)) { 76 return null; 77 } 78 return parcel.readLong(); 79 } 80 81 /** Write a {@link Set} of {@link android.os.Parcelable} objects. */ writeParcelableSet(Parcel parcel, Set<? extends Parcelable> set, int flags)82 public static void writeParcelableSet(Parcel parcel, Set<? extends Parcelable> set, int flags) { 83 parcel.writeList(new ArrayList<>(set)); 84 } 85 86 /** Read a {@link Set} of {@link android.os.Parcelable} objects. */ readParcelableSet(Parcel parcel)87 public static Set<? extends Parcelable> readParcelableSet(Parcel parcel) { 88 List<? extends Parcelable> l = new ArrayList<>(); 89 parcel.readList(l, ParcelableUtils.class.getClassLoader()); 90 return new HashSet<>(l); 91 } 92 93 /** Write a {@link Set}. */ writeSet(Parcel parcel, Set<?> set)94 public static void writeSet(Parcel parcel, Set<?> set) { 95 parcel.writeList(new ArrayList<>(set)); 96 } 97 98 /** Read a {@link Set}. */ readSet(Parcel parcel)99 public static Set<?> readSet(Parcel parcel) { 100 List<?> l = new ArrayList<>(); 101 parcel.readList(l, ParcelableUtils.class.getClassLoader()); 102 return new HashSet<>(l); 103 } 104 105 /** Write a {@link Set} of {@link String}. */ writeStringSet(Parcel parcel, Set<String> set)106 public static void writeStringSet(Parcel parcel, Set<String> set) { 107 parcel.writeStringList(new ArrayList<>(set)); 108 } 109 110 /** Read a {@link Set} of {@link String}. */ readStringSet(Parcel parcel)111 public static Set<String> readStringSet(Parcel parcel) { 112 List<String> s = new ArrayList<>(); 113 parcel.readStringList(s); 114 return new HashSet<>(s); 115 } 116 117 /** Write a {@link boolean}. */ writeBoolean(Parcel parcel, boolean b)118 public static void writeBoolean(Parcel parcel, boolean b) { 119 parcel.writeInt(b ? 1 : 0); 120 } 121 122 /** Read a {@link boolean}. */ readBoolean(Parcel parcel)123 public static boolean readBoolean(Parcel parcel) { 124 return parcel.readInt() == 1; 125 } 126 } 127