1 /**
2  * Copyright (C) 2018 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.hardware.radio;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.os.Parcel;
22 import android.os.Parcelable;
23 import android.os.RemoteException;
24 
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.Map;
30 import java.util.Objects;
31 import java.util.Set;
32 
33 final class Utils {
34     private static final String TAG = "BroadcastRadio.utils";
35 
writeStringMap(@onNull Parcel dest, @Nullable Map<String, String> map)36     static void writeStringMap(@NonNull Parcel dest, @Nullable Map<String, String> map) {
37         if (map == null) {
38             dest.writeInt(0);
39             return;
40         }
41         dest.writeInt(map.size());
42         for (Map.Entry<String, String> entry : map.entrySet()) {
43             dest.writeString(entry.getKey());
44             dest.writeString(entry.getValue());
45         }
46     }
47 
readStringMap(@onNull Parcel in)48     static @NonNull Map<String, String> readStringMap(@NonNull Parcel in) {
49         int size = in.readInt();
50         Map<String, String> map = new HashMap<>();
51         while (size-- > 0) {
52             String key = in.readString();
53             String value = in.readString();
54             map.put(key, value);
55         }
56         return map;
57     }
58 
writeStringIntMap(@onNull Parcel dest, @Nullable Map<String, Integer> map)59     static void writeStringIntMap(@NonNull Parcel dest, @Nullable Map<String, Integer> map) {
60         if (map == null) {
61             dest.writeInt(0);
62             return;
63         }
64         dest.writeInt(map.size());
65         for (Map.Entry<String, Integer> entry : map.entrySet()) {
66             dest.writeString(entry.getKey());
67             dest.writeInt(entry.getValue());
68         }
69     }
70 
readStringIntMap(@onNull Parcel in)71     static @NonNull Map<String, Integer> readStringIntMap(@NonNull Parcel in) {
72         int size = in.readInt();
73         Map<String, Integer> map = new HashMap<>();
74         while (size-- > 0) {
75             String key = in.readString();
76             int value = in.readInt();
77             map.put(key, value);
78         }
79         return map;
80     }
81 
writeSet(@onNull Parcel dest, @Nullable Set<T> set)82     static <T extends Parcelable> void writeSet(@NonNull Parcel dest, @Nullable Set<T> set) {
83         if (set == null) {
84             dest.writeInt(0);
85             return;
86         }
87         dest.writeInt(set.size());
88         set.stream().forEach(elem -> dest.writeTypedObject(elem, 0));
89     }
90 
createSet(@onNull Parcel in, Parcelable.Creator<T> c)91     static <T> Set<T> createSet(@NonNull Parcel in, Parcelable.Creator<T> c) {
92         int size = in.readInt();
93         Set<T> set = new HashSet<>();
94         while (size-- > 0) {
95             set.add(in.readTypedObject(c));
96         }
97         return set;
98     }
99 
writeIntSet(@onNull Parcel dest, @Nullable Set<Integer> set)100     static void writeIntSet(@NonNull Parcel dest, @Nullable Set<Integer> set) {
101         if (set == null) {
102             dest.writeInt(0);
103             return;
104         }
105         dest.writeInt(set.size());
106         set.stream().forEach(elem -> dest.writeInt(Objects.requireNonNull(elem)));
107     }
108 
createIntSet(@onNull Parcel in)109     static Set<Integer> createIntSet(@NonNull Parcel in) {
110         return createSet(in, new Parcelable.Creator<Integer>() {
111             public Integer createFromParcel(Parcel in) {
112                 return in.readInt();
113             }
114 
115             public Integer[] newArray(int size) {
116                 return new Integer[size];
117             }
118         });
119     }
120 
121     static <T extends Parcelable> void writeTypedCollection(@NonNull Parcel dest,
122             @Nullable Collection<T> coll) {
123         ArrayList<T> list = null;
124         if (coll != null) {
125             if (coll instanceof ArrayList) {
126                 list = (ArrayList) coll;
127             } else {
128                 list = new ArrayList<>(coll);
129             }
130         }
131         dest.writeTypedList(list);
132     }
133 
134     static void close(ICloseHandle handle) {
135         try {
136             handle.close();
137         } catch (RemoteException ex) {
138             ex.rethrowFromSystemServer();
139         }
140     }
141 }
142