blob: 404611cbba010117df94217e86682d4020c6b53c [file] [log] [blame]
Devin Moore33ce3302023-10-20 16:53:08 +00001/*
2 * Copyright (C) 2023 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#include <android/binder_libbinder.h>
17#include <android/persistable_bundle.h>
18#include <binder/PersistableBundle.h>
19#include <log/log.h>
20#include <persistable_bundle_internal.h>
21#include <string.h>
22
23#include <set>
24
25__BEGIN_DECLS
26
27struct APersistableBundle {
28 APersistableBundle(const APersistableBundle& pBundle) : mPBundle(pBundle.mPBundle) {}
29 APersistableBundle(const android::os::PersistableBundle& pBundle) : mPBundle(pBundle) {}
30 APersistableBundle() = default;
31 android::os::PersistableBundle mPBundle;
32};
33
34APersistableBundle* _Nullable APersistableBundle_new() {
35 return new (std::nothrow) APersistableBundle();
36}
37
38APersistableBundle* _Nullable APersistableBundle_dup(const APersistableBundle* pBundle) {
39 if (pBundle) {
40 return new APersistableBundle(*pBundle);
41 } else {
42 return new APersistableBundle();
43 }
44}
45
46void APersistableBundle_delete(APersistableBundle* pBundle) {
47 free(pBundle);
48}
49
50bool APersistableBundle_isEqual(const APersistableBundle* lhs, const APersistableBundle* rhs) {
51 if (lhs && rhs) {
52 return lhs->mPBundle == rhs->mPBundle;
53 } else if (lhs == rhs) {
54 return true;
55 } else {
56 return false;
57 }
58}
59
60binder_status_t APersistableBundle_readFromParcel(const AParcel* parcel,
61 APersistableBundle* _Nullable* outPBundle) {
62 if (!parcel || !outPBundle) return STATUS_BAD_VALUE;
63 APersistableBundle* newPBundle = APersistableBundle_new();
64 if (newPBundle == nullptr) return STATUS_NO_MEMORY;
65 binder_status_t status =
66 newPBundle->mPBundle.readFromParcel(AParcel_viewPlatformParcel(parcel));
67 if (status == STATUS_OK) {
68 *outPBundle = newPBundle;
69 }
70 return status;
71}
72
73binder_status_t APersistableBundle_writeToParcel(const APersistableBundle* pBundle,
74 AParcel* parcel) {
75 if (!parcel || !pBundle) return STATUS_BAD_VALUE;
76 return pBundle->mPBundle.writeToParcel(AParcel_viewPlatformParcel(parcel));
77}
78
79int32_t APersistableBundle_size(APersistableBundle* pBundle) {
80 size_t size = pBundle->mPBundle.size();
81 LOG_ALWAYS_FATAL_IF(size > INT32_MAX,
82 "The APersistableBundle has gotten too large! There will be an overflow in "
83 "the reported size.");
84 return pBundle->mPBundle.size();
85}
86int32_t APersistableBundle_erase(APersistableBundle* pBundle, const char* key) {
87 return pBundle->mPBundle.erase(android::String16(key));
88}
89void APersistableBundle_putBoolean(APersistableBundle* pBundle, const char* key, bool val) {
90 pBundle->mPBundle.putBoolean(android::String16(key), val);
91}
92void APersistableBundle_putInt(APersistableBundle* pBundle, const char* key, int32_t val) {
93 pBundle->mPBundle.putInt(android::String16(key), val);
94}
95void APersistableBundle_putLong(APersistableBundle* pBundle, const char* key, int64_t val) {
96 pBundle->mPBundle.putLong(android::String16(key), val);
97}
98void APersistableBundle_putDouble(APersistableBundle* pBundle, const char* key, double val) {
99 pBundle->mPBundle.putDouble(android::String16(key), val);
100}
101void APersistableBundle_putString(APersistableBundle* pBundle, const char* key, const char* val) {
102 pBundle->mPBundle.putString(android::String16(key), android::String16(val));
103}
104void APersistableBundle_putBooleanVector(APersistableBundle* pBundle, const char* key,
105 const bool* vec, int32_t num) {
106 LOG_ALWAYS_FATAL_IF(num < 0, "Negative number of elements is invalid.");
107 std::vector<bool> newVec(num);
108 for (int32_t i = 0; i < num; i++) {
109 newVec[i] = vec[i];
110 }
111 pBundle->mPBundle.putBooleanVector(android::String16(key), newVec);
112}
113void APersistableBundle_putIntVector(APersistableBundle* pBundle, const char* key,
114 const int32_t* vec, int32_t num) {
115 LOG_ALWAYS_FATAL_IF(num < 0, "Negative number of elements is invalid.");
116 std::vector<int32_t> newVec(num);
117 for (int32_t i = 0; i < num; i++) {
118 newVec[i] = vec[i];
119 }
120 pBundle->mPBundle.putIntVector(android::String16(key), newVec);
121}
122void APersistableBundle_putLongVector(APersistableBundle* pBundle, const char* key,
123 const int64_t* vec, int32_t num) {
124 LOG_ALWAYS_FATAL_IF(num < 0, "Negative number of elements is invalid.");
125 std::vector<int64_t> newVec(num);
126 for (int32_t i = 0; i < num; i++) {
127 newVec[i] = vec[i];
128 }
129 pBundle->mPBundle.putLongVector(android::String16(key), newVec);
130}
131void APersistableBundle_putDoubleVector(APersistableBundle* pBundle, const char* key,
132 const double* vec, int32_t num) {
133 LOG_ALWAYS_FATAL_IF(num < 0, "Negative number of elements is invalid.");
134 std::vector<double> newVec(num);
135 for (int32_t i = 0; i < num; i++) {
136 newVec[i] = vec[i];
137 }
138 pBundle->mPBundle.putDoubleVector(android::String16(key), newVec);
139}
140void APersistableBundle_putStringVector(APersistableBundle* pBundle, const char* key,
141 const char* const* vec, int32_t num) {
142 LOG_ALWAYS_FATAL_IF(num < 0, "Negative number of elements is invalid.");
143 std::vector<android::String16> newVec(num);
144 for (int32_t i = 0; i < num; i++) {
145 newVec[i] = android::String16(vec[i]);
146 }
147 pBundle->mPBundle.putStringVector(android::String16(key), newVec);
148}
149void APersistableBundle_putPersistableBundle(APersistableBundle* pBundle, const char* key,
150 const APersistableBundle* val) {
151 pBundle->mPBundle.putPersistableBundle(android::String16(key), val->mPBundle);
152}
153bool APersistableBundle_getBoolean(const APersistableBundle* pBundle, const char* key, bool* val) {
154 return pBundle->mPBundle.getBoolean(android::String16(key), val);
155}
156bool APersistableBundle_getInt(const APersistableBundle* pBundle, const char* key, int32_t* val) {
157 return pBundle->mPBundle.getInt(android::String16(key), val);
158}
159bool APersistableBundle_getLong(const APersistableBundle* pBundle, const char* key, int64_t* val) {
160 return pBundle->mPBundle.getLong(android::String16(key), val);
161}
162bool APersistableBundle_getDouble(const APersistableBundle* pBundle, const char* key, double* val) {
163 return pBundle->mPBundle.getDouble(android::String16(key), val);
164}
165int32_t APersistableBundle_getString(const APersistableBundle* pBundle, const char* key, char** val,
166 APersistableBundle_stringAllocator stringAllocator,
167 void* context) {
168 android::String16 outVal;
169 bool ret = pBundle->mPBundle.getString(android::String16(key), &outVal);
170 if (ret) {
171 android::String8 tmp8(outVal);
172 *val = stringAllocator(tmp8.bytes() + 1, context);
173 if (*val) {
174 strncpy(*val, tmp8.c_str(), tmp8.bytes() + 1);
175 return tmp8.bytes();
176 } else {
177 return -1;
178 }
179 }
180 return 0;
181}
182int32_t APersistableBundle_getBooleanVector(const APersistableBundle* pBundle, const char* key,
183 bool* buffer, int32_t bufferSizeBytes) {
184 std::vector<bool> newVec;
185 pBundle->mPBundle.getBooleanVector(android::String16(key), &newVec);
186 return getVecInternal<bool>(newVec, buffer, bufferSizeBytes);
187}
188int32_t APersistableBundle_getIntVector(const APersistableBundle* pBundle, const char* key,
189 int32_t* buffer, int32_t bufferSizeBytes) {
190 std::vector<int32_t> newVec;
191 pBundle->mPBundle.getIntVector(android::String16(key), &newVec);
192 return getVecInternal<int32_t>(newVec, buffer, bufferSizeBytes);
193}
194int32_t APersistableBundle_getLongVector(const APersistableBundle* pBundle, const char* key,
195 int64_t* buffer, int32_t bufferSizeBytes) {
196 std::vector<int64_t> newVec;
197 pBundle->mPBundle.getLongVector(android::String16(key), &newVec);
198 return getVecInternal<int64_t>(newVec, buffer, bufferSizeBytes);
199}
200int32_t APersistableBundle_getDoubleVector(const APersistableBundle* pBundle, const char* key,
201 double* buffer, int32_t bufferSizeBytes) {
202 std::vector<double> newVec;
203 pBundle->mPBundle.getDoubleVector(android::String16(key), &newVec);
204 return getVecInternal<double>(newVec, buffer, bufferSizeBytes);
205}
206int32_t APersistableBundle_getStringVector(const APersistableBundle* pBundle, const char* key,
207 char** vec, int32_t bufferSizeBytes,
208 APersistableBundle_stringAllocator stringAllocator,
209 void* context) {
210 std::vector<android::String16> newVec;
211 pBundle->mPBundle.getStringVector(android::String16(key), &newVec);
212 return getStringsInternal<std::vector<android::String16>>(newVec, vec, bufferSizeBytes,
213 stringAllocator, context);
214}
215bool APersistableBundle_getPersistableBundle(const APersistableBundle* pBundle, const char* key,
216 APersistableBundle** outBundle) {
217 APersistableBundle* bundle = APersistableBundle_new();
218 bool ret = pBundle->mPBundle.getPersistableBundle(android::String16(key), &bundle->mPBundle);
219 if (ret) {
220 *outBundle = bundle;
221 return true;
222 }
223 return false;
224}
225int32_t APersistableBundle_getBooleanKeys(const APersistableBundle* pBundle, char** outKeys,
226 int32_t bufferSizeBytes,
227 APersistableBundle_stringAllocator stringAllocator,
228 void* context) {
229 std::set<android::String16> ret = pBundle->mPBundle.getBooleanKeys();
230 return getStringsInternal<std::set<android::String16>>(ret, outKeys, bufferSizeBytes,
231 stringAllocator, context);
232}
233int32_t APersistableBundle_getIntKeys(const APersistableBundle* pBundle, char** outKeys,
234 int32_t bufferSizeBytes,
235 APersistableBundle_stringAllocator stringAllocator,
236 void* context) {
237 std::set<android::String16> ret = pBundle->mPBundle.getIntKeys();
238 return getStringsInternal<std::set<android::String16>>(ret, outKeys, bufferSizeBytes,
239 stringAllocator, context);
240}
241int32_t APersistableBundle_getLongKeys(const APersistableBundle* pBundle, char** outKeys,
242 int32_t bufferSizeBytes,
243 APersistableBundle_stringAllocator stringAllocator,
244 void* context) {
245 std::set<android::String16> ret = pBundle->mPBundle.getLongKeys();
246 return getStringsInternal<std::set<android::String16>>(ret, outKeys, bufferSizeBytes,
247 stringAllocator, context);
248}
249int32_t APersistableBundle_getDoubleKeys(const APersistableBundle* pBundle, char** outKeys,
250 int32_t bufferSizeBytes,
251 APersistableBundle_stringAllocator stringAllocator,
252 void* context) {
253 std::set<android::String16> ret = pBundle->mPBundle.getDoubleKeys();
254 return getStringsInternal<std::set<android::String16>>(ret, outKeys, bufferSizeBytes,
255 stringAllocator, context);
256}
257int32_t APersistableBundle_getStringKeys(const APersistableBundle* pBundle, char** outKeys,
258 int32_t bufferSizeBytes,
259 APersistableBundle_stringAllocator stringAllocator,
260 void* context) {
261 std::set<android::String16> ret = pBundle->mPBundle.getStringKeys();
262 return getStringsInternal<std::set<android::String16>>(ret, outKeys, bufferSizeBytes,
263 stringAllocator, context);
264}
265int32_t APersistableBundle_getBooleanVectorKeys(const APersistableBundle* pBundle, char** outKeys,
266 int32_t bufferSizeBytes,
267 APersistableBundle_stringAllocator stringAllocator,
268 void* context) {
269 std::set<android::String16> ret = pBundle->mPBundle.getBooleanVectorKeys();
270 return getStringsInternal<std::set<android::String16>>(ret, outKeys, bufferSizeBytes,
271 stringAllocator, context);
272}
273int32_t APersistableBundle_getIntVectorKeys(const APersistableBundle* pBundle, char** outKeys,
274 int32_t bufferSizeBytes,
275 APersistableBundle_stringAllocator stringAllocator,
276 void* context) {
277 std::set<android::String16> ret = pBundle->mPBundle.getIntVectorKeys();
278 return getStringsInternal<std::set<android::String16>>(ret, outKeys, bufferSizeBytes,
279 stringAllocator, context);
280}
281int32_t APersistableBundle_getLongVectorKeys(const APersistableBundle* pBundle, char** outKeys,
282 int32_t bufferSizeBytes,
283 APersistableBundle_stringAllocator stringAllocator,
284 void* context) {
285 std::set<android::String16> ret = pBundle->mPBundle.getLongVectorKeys();
286 return getStringsInternal<std::set<android::String16>>(ret, outKeys, bufferSizeBytes,
287 stringAllocator, context);
288}
289int32_t APersistableBundle_getDoubleVectorKeys(const APersistableBundle* pBundle, char** outKeys,
290 int32_t bufferSizeBytes,
291 APersistableBundle_stringAllocator stringAllocator,
292 void* context) {
293 std::set<android::String16> ret = pBundle->mPBundle.getDoubleVectorKeys();
294 return getStringsInternal<std::set<android::String16>>(ret, outKeys, bufferSizeBytes,
295 stringAllocator, context);
296}
297int32_t APersistableBundle_getStringVectorKeys(const APersistableBundle* pBundle, char** outKeys,
298 int32_t bufferSizeBytes,
299 APersistableBundle_stringAllocator stringAllocator,
300 void* context) {
301 std::set<android::String16> ret = pBundle->mPBundle.getStringVectorKeys();
302 return getStringsInternal<std::set<android::String16>>(ret, outKeys, bufferSizeBytes,
303 stringAllocator, context);
304}
305int32_t APersistableBundle_getPersistableBundleKeys(
306 const APersistableBundle* pBundle, char** outKeys, int32_t bufferSizeBytes,
307 APersistableBundle_stringAllocator stringAllocator, void* context) {
308 std::set<android::String16> ret = pBundle->mPBundle.getPersistableBundleKeys();
309 return getStringsInternal<std::set<android::String16>>(ret, outKeys, bufferSizeBytes,
310 stringAllocator, context);
311}
312
313__END_DECLS