2222#include < set>
2323#include < sstream>
2424
25- namespace aidl ::android::os {
26-
25+ // Include llndk-versioning.h only for vendor build as it is not available for NDK headers.
2726#if defined(__ANDROID_VENDOR__)
28- #define AT_LEAST_V_OR_202404 constexpr (__ANDROID_VENDOR_API__ >= 202404 )
29- #else
30- // TODO(b/322384429) switch this to __ANDROID_API_V__ when V is finalized
31- #define AT_LEAST_V_OR_202404 (__builtin_available(android __ANDROID_API_FUTURE__, *))
27+ #include < android/llndk-versioning.h>
28+ #else // __ANDROID_VENDOR__
29+ #if defined(API_LEVEL_AT_LEAST)
30+ // Redefine API_LEVEL_AT_LEAST here to replace the version to __ANDROID_API_FUTURE__ as a workaround
31+ #undef API_LEVEL_AT_LEAST
3232#endif
33+ // TODO(b/322384429) switch this __ANDROID_API_FUTURE__ to sdk_api_level when V is finalized
34+ #define API_LEVEL_AT_LEAST (sdk_api_level, vendor_api_level ) \
35+ (__builtin_available(android __ANDROID_API_FUTURE__, *))
36+ #endif // __ANDROID_VENDOR__
37+
38+ namespace aidl ::android::os {
3339
3440/* *
3541 * Wrapper class that enables interop with AIDL NDK generation
@@ -39,7 +45,7 @@ namespace aidl::android::os {
3945class PersistableBundle {
4046 public:
4147 PersistableBundle () noexcept {
42- if AT_LEAST_V_OR_202404 {
48+ if API_LEVEL_AT_LEAST (__ANDROID_API_V__, 202404 ) {
4349 mPBundle = APersistableBundle_new ();
4450 }
4551 }
@@ -49,13 +55,13 @@ class PersistableBundle {
4955 PersistableBundle (PersistableBundle&& other) noexcept : mPBundle (other.release()) {}
5056 // duplicates, does not take ownership of the APersistableBundle*
5157 PersistableBundle (const PersistableBundle& other) {
52- if AT_LEAST_V_OR_202404 {
58+ if API_LEVEL_AT_LEAST (__ANDROID_API_V__, 202404 ) {
5359 mPBundle = APersistableBundle_dup (other.mPBundle );
5460 }
5561 }
5662 // duplicates, does not take ownership of the APersistableBundle*
5763 PersistableBundle& operator =(const PersistableBundle& other) {
58- if AT_LEAST_V_OR_202404 {
64+ if API_LEVEL_AT_LEAST (__ANDROID_API_V__, 202404 ) {
5965 mPBundle = APersistableBundle_dup (other.mPBundle );
6066 }
6167 return *this ;
@@ -65,7 +71,7 @@ class PersistableBundle {
6571
6672 binder_status_t readFromParcel (const AParcel* _Nonnull parcel) {
6773 reset ();
68- if AT_LEAST_V_OR_202404 {
74+ if API_LEVEL_AT_LEAST (__ANDROID_API_V__, 202404 ) {
6975 return APersistableBundle_readFromParcel (parcel, &mPBundle );
7076 } else {
7177 return STATUS_INVALID_OPERATION;
@@ -76,7 +82,7 @@ class PersistableBundle {
7682 if (!mPBundle ) {
7783 return STATUS_BAD_VALUE;
7884 }
79- if AT_LEAST_V_OR_202404 {
85+ if API_LEVEL_AT_LEAST (__ANDROID_API_V__, 202404 ) {
8086 return APersistableBundle_writeToParcel (mPBundle , parcel);
8187 } else {
8288 return STATUS_INVALID_OPERATION;
@@ -91,7 +97,7 @@ class PersistableBundle {
9197 */
9298 void reset (APersistableBundle* _Nullable pBundle = nullptr ) noexcept {
9399 if (mPBundle ) {
94- if AT_LEAST_V_OR_202404 {
100+ if API_LEVEL_AT_LEAST (__ANDROID_API_V__, 202404 ) {
95101 APersistableBundle_delete (mPBundle );
96102 }
97103 mPBundle = nullptr ;
@@ -104,7 +110,7 @@ class PersistableBundle {
104110 * what should be used to check for equality.
105111 */
106112 bool deepEquals (const PersistableBundle& rhs) const {
107- if AT_LEAST_V_OR_202404 {
113+ if API_LEVEL_AT_LEAST (__ANDROID_API_V__, 202404 ) {
108114 return APersistableBundle_isEqual (get (), rhs.get ());
109115 } else {
110116 return false ;
@@ -143,7 +149,7 @@ class PersistableBundle {
143149 inline std::string toString () const {
144150 if (!mPBundle ) {
145151 return " <PersistableBundle: null>" ;
146- } else if AT_LEAST_V_OR_202404 {
152+ } else if API_LEVEL_AT_LEAST (__ANDROID_API_V__, 202404 ) {
147153 std::ostringstream os;
148154 os << " <PersistableBundle: " ;
149155 os << " size: " << std::to_string (APersistableBundle_size (mPBundle ));
@@ -154,53 +160,53 @@ class PersistableBundle {
154160 }
155161
156162 int32_t size () const {
157- if AT_LEAST_V_OR_202404 {
163+ if API_LEVEL_AT_LEAST (__ANDROID_API_V__, 202404 ) {
158164 return APersistableBundle_size (mPBundle );
159165 } else {
160166 return 0 ;
161167 }
162168 }
163169
164170 int32_t erase (const std::string& key) {
165- if AT_LEAST_V_OR_202404 {
171+ if API_LEVEL_AT_LEAST (__ANDROID_API_V__, 202404 ) {
166172 return APersistableBundle_erase (mPBundle , key.c_str ());
167173 } else {
168174 return 0 ;
169175 }
170176 }
171177
172178 void putBoolean (const std::string& key, bool val) {
173- if AT_LEAST_V_OR_202404 {
179+ if API_LEVEL_AT_LEAST (__ANDROID_API_V__, 202404 ) {
174180 APersistableBundle_putBoolean (mPBundle , key.c_str (), val);
175181 }
176182 }
177183
178184 void putInt (const std::string& key, int32_t val) {
179- if AT_LEAST_V_OR_202404 {
185+ if API_LEVEL_AT_LEAST (__ANDROID_API_V__, 202404 ) {
180186 APersistableBundle_putInt (mPBundle , key.c_str (), val);
181187 }
182188 }
183189
184190 void putLong (const std::string& key, int64_t val) {
185- if AT_LEAST_V_OR_202404 {
191+ if API_LEVEL_AT_LEAST (__ANDROID_API_V__, 202404 ) {
186192 APersistableBundle_putLong (mPBundle , key.c_str (), val);
187193 }
188194 }
189195
190196 void putDouble (const std::string& key, double val) {
191- if AT_LEAST_V_OR_202404 {
197+ if API_LEVEL_AT_LEAST (__ANDROID_API_V__, 202404 ) {
192198 APersistableBundle_putDouble (mPBundle , key.c_str (), val);
193199 }
194200 }
195201
196202 void putString (const std::string& key, const std::string& val) {
197- if AT_LEAST_V_OR_202404 {
203+ if API_LEVEL_AT_LEAST (__ANDROID_API_V__, 202404 ) {
198204 APersistableBundle_putString (mPBundle , key.c_str (), val.c_str ());
199205 }
200206 }
201207
202208 void putBooleanVector (const std::string& key, const std::vector<bool >& vec) {
203- if AT_LEAST_V_OR_202404 {
209+ if API_LEVEL_AT_LEAST (__ANDROID_API_V__, 202404 ) {
204210 // std::vector<bool> has no ::data().
205211 int32_t num = vec.size ();
206212 if (num > 0 ) {
@@ -217,31 +223,31 @@ class PersistableBundle {
217223 }
218224
219225 void putIntVector (const std::string& key, const std::vector<int32_t >& vec) {
220- if AT_LEAST_V_OR_202404 {
226+ if API_LEVEL_AT_LEAST (__ANDROID_API_V__, 202404 ) {
221227 int32_t num = vec.size ();
222228 if (num > 0 ) {
223229 APersistableBundle_putIntVector (mPBundle , key.c_str (), vec.data (), num);
224230 }
225231 }
226232 }
227233 void putLongVector (const std::string& key, const std::vector<int64_t >& vec) {
228- if AT_LEAST_V_OR_202404 {
234+ if API_LEVEL_AT_LEAST (__ANDROID_API_V__, 202404 ) {
229235 int32_t num = vec.size ();
230236 if (num > 0 ) {
231237 APersistableBundle_putLongVector (mPBundle , key.c_str (), vec.data (), num);
232238 }
233239 }
234240 }
235241 void putDoubleVector (const std::string& key, const std::vector<double >& vec) {
236- if AT_LEAST_V_OR_202404 {
242+ if API_LEVEL_AT_LEAST (__ANDROID_API_V__, 202404 ) {
237243 int32_t num = vec.size ();
238244 if (num > 0 ) {
239245 APersistableBundle_putDoubleVector (mPBundle , key.c_str (), vec.data (), num);
240246 }
241247 }
242248 }
243249 void putStringVector (const std::string& key, const std::vector<std::string>& vec) {
244- if AT_LEAST_V_OR_202404 {
250+ if API_LEVEL_AT_LEAST (__ANDROID_API_V__, 202404 ) {
245251 int32_t num = vec.size ();
246252 if (num > 0 ) {
247253 char ** inVec = (char **)malloc (num * sizeof (char *));
@@ -256,37 +262,37 @@ class PersistableBundle {
256262 }
257263 }
258264 void putPersistableBundle (const std::string& key, const PersistableBundle& pBundle) {
259- if AT_LEAST_V_OR_202404 {
265+ if API_LEVEL_AT_LEAST (__ANDROID_API_V__, 202404 ) {
260266 APersistableBundle_putPersistableBundle (mPBundle , key.c_str (), pBundle.mPBundle );
261267 }
262268 }
263269
264270 bool getBoolean (const std::string& key, bool * _Nonnull val) {
265- if AT_LEAST_V_OR_202404 {
271+ if API_LEVEL_AT_LEAST (__ANDROID_API_V__, 202404 ) {
266272 return APersistableBundle_getBoolean (mPBundle , key.c_str (), val);
267273 } else {
268274 return false ;
269275 }
270276 }
271277
272278 bool getInt (const std::string& key, int32_t * _Nonnull val) {
273- if AT_LEAST_V_OR_202404 {
279+ if API_LEVEL_AT_LEAST (__ANDROID_API_V__, 202404 ) {
274280 return APersistableBundle_getInt (mPBundle , key.c_str (), val);
275281 } else {
276282 return false ;
277283 }
278284 }
279285
280286 bool getLong (const std::string& key, int64_t * _Nonnull val) {
281- if AT_LEAST_V_OR_202404 {
287+ if API_LEVEL_AT_LEAST (__ANDROID_API_V__, 202404 ) {
282288 return APersistableBundle_getLong (mPBundle , key.c_str (), val);
283289 } else {
284290 return false ;
285291 }
286292 }
287293
288294 bool getDouble (const std::string& key, double * _Nonnull val) {
289- if AT_LEAST_V_OR_202404 {
295+ if API_LEVEL_AT_LEAST (__ANDROID_API_V__, 202404 ) {
290296 return APersistableBundle_getDouble (mPBundle , key.c_str (), val);
291297 } else {
292298 return false ;
@@ -298,7 +304,7 @@ class PersistableBundle {
298304 }
299305
300306 bool getString (const std::string& key, std::string* _Nonnull val) {
301- if AT_LEAST_V_OR_202404 {
307+ if API_LEVEL_AT_LEAST (__ANDROID_API_V__, 202404 ) {
302308 char * outString = nullptr ;
303309 bool ret = APersistableBundle_getString (mPBundle , key.c_str (), &outString,
304310 &stringAllocator, nullptr );
@@ -316,7 +322,7 @@ class PersistableBundle {
316322 const char * _Nonnull, T* _Nullable, int32_t ),
317323 const APersistableBundle* _Nonnull pBundle, const char* _Nonnull key,
318324 std::vector<T>* _Nonnull vec) {
319- if AT_LEAST_V_OR_202404 {
325+ if API_LEVEL_AT_LEAST (__ANDROID_API_V__, 202404 ) {
320326 int32_t bytes = 0 ;
321327 // call first with nullptr to get required size in bytes
322328 bytes = getVec (pBundle, key, nullptr , 0 );
@@ -338,28 +344,28 @@ class PersistableBundle {
338344 }
339345
340346 bool getBooleanVector (const std::string& key, std::vector<bool >* _Nonnull vec) {
341- if AT_LEAST_V_OR_202404 {
347+ if API_LEVEL_AT_LEAST (__ANDROID_API_V__, 202404 ) {
342348 return getVecInternal<bool >(&APersistableBundle_getBooleanVector, mPBundle , key.c_str (),
343349 vec);
344350 }
345351 return false ;
346352 }
347353 bool getIntVector (const std::string& key, std::vector<int32_t >* _Nonnull vec) {
348- if AT_LEAST_V_OR_202404 {
354+ if API_LEVEL_AT_LEAST (__ANDROID_API_V__, 202404 ) {
349355 return getVecInternal<int32_t >(&APersistableBundle_getIntVector, mPBundle , key.c_str (),
350356 vec);
351357 }
352358 return false ;
353359 }
354360 bool getLongVector (const std::string& key, std::vector<int64_t >* _Nonnull vec) {
355- if AT_LEAST_V_OR_202404 {
361+ if API_LEVEL_AT_LEAST (__ANDROID_API_V__, 202404 ) {
356362 return getVecInternal<int64_t >(&APersistableBundle_getLongVector, mPBundle , key.c_str (),
357363 vec);
358364 }
359365 return false ;
360366 }
361367 bool getDoubleVector (const std::string& key, std::vector<double >* _Nonnull vec) {
362- if AT_LEAST_V_OR_202404 {
368+ if API_LEVEL_AT_LEAST (__ANDROID_API_V__, 202404 ) {
363369 return getVecInternal<double >(&APersistableBundle_getDoubleVector, mPBundle ,
364370 key.c_str (), vec);
365371 }
@@ -384,7 +390,7 @@ class PersistableBundle {
384390 }
385391
386392 bool getStringVector (const std::string& key, std::vector<std::string>* _Nonnull vec) {
387- if AT_LEAST_V_OR_202404 {
393+ if API_LEVEL_AT_LEAST (__ANDROID_API_V__, 202404 ) {
388394 int32_t bytes = APersistableBundle_getStringVector (mPBundle , key.c_str (), nullptr , 0 ,
389395 &stringAllocator, nullptr );
390396 if (bytes > 0 ) {
@@ -401,7 +407,7 @@ class PersistableBundle {
401407 }
402408
403409 bool getPersistableBundle (const std::string& key, PersistableBundle* _Nonnull val) {
404- if AT_LEAST_V_OR_202404 {
410+ if API_LEVEL_AT_LEAST (__ANDROID_API_V__, 202404 ) {
405411 APersistableBundle* bundle = nullptr ;
406412 bool ret = APersistableBundle_getPersistableBundle (mPBundle , key.c_str (), &bundle);
407413 if (ret) {
@@ -433,77 +439,77 @@ class PersistableBundle {
433439 }
434440
435441 std::set<std::string> getBooleanKeys () {
436- if AT_LEAST_V_OR_202404 {
442+ if API_LEVEL_AT_LEAST (__ANDROID_API_V__, 202404 ) {
437443 return getKeys (&APersistableBundle_getBooleanKeys, mPBundle );
438444 } else {
439445 return {};
440446 }
441447 }
442448 std::set<std::string> getIntKeys () {
443- if AT_LEAST_V_OR_202404 {
449+ if API_LEVEL_AT_LEAST (__ANDROID_API_V__, 202404 ) {
444450 return getKeys (&APersistableBundle_getIntKeys, mPBundle );
445451 } else {
446452 return {};
447453 }
448454 }
449455 std::set<std::string> getLongKeys () {
450- if AT_LEAST_V_OR_202404 {
456+ if API_LEVEL_AT_LEAST (__ANDROID_API_V__, 202404 ) {
451457 return getKeys (&APersistableBundle_getLongKeys, mPBundle );
452458 } else {
453459 return {};
454460 }
455461 }
456462 std::set<std::string> getDoubleKeys () {
457- if AT_LEAST_V_OR_202404 {
463+ if API_LEVEL_AT_LEAST (__ANDROID_API_V__, 202404 ) {
458464 return getKeys (&APersistableBundle_getDoubleKeys, mPBundle );
459465 } else {
460466 return {};
461467 }
462468 }
463469 std::set<std::string> getStringKeys () {
464- if AT_LEAST_V_OR_202404 {
470+ if API_LEVEL_AT_LEAST (__ANDROID_API_V__, 202404 ) {
465471 return getKeys (&APersistableBundle_getStringKeys, mPBundle );
466472 } else {
467473 return {};
468474 }
469475 }
470476 std::set<std::string> getBooleanVectorKeys () {
471- if AT_LEAST_V_OR_202404 {
477+ if API_LEVEL_AT_LEAST (__ANDROID_API_V__, 202404 ) {
472478 return getKeys (&APersistableBundle_getBooleanVectorKeys, mPBundle );
473479 } else {
474480 return {};
475481 }
476482 }
477483 std::set<std::string> getIntVectorKeys () {
478- if AT_LEAST_V_OR_202404 {
484+ if API_LEVEL_AT_LEAST (__ANDROID_API_V__, 202404 ) {
479485 return getKeys (&APersistableBundle_getIntVectorKeys, mPBundle );
480486 } else {
481487 return {};
482488 }
483489 }
484490 std::set<std::string> getLongVectorKeys () {
485- if AT_LEAST_V_OR_202404 {
491+ if API_LEVEL_AT_LEAST (__ANDROID_API_V__, 202404 ) {
486492 return getKeys (&APersistableBundle_getLongVectorKeys, mPBundle );
487493 } else {
488494 return {};
489495 }
490496 }
491497 std::set<std::string> getDoubleVectorKeys () {
492- if AT_LEAST_V_OR_202404 {
498+ if API_LEVEL_AT_LEAST (__ANDROID_API_V__, 202404 ) {
493499 return getKeys (&APersistableBundle_getDoubleVectorKeys, mPBundle );
494500 } else {
495501 return {};
496502 }
497503 }
498504 std::set<std::string> getStringVectorKeys () {
499- if AT_LEAST_V_OR_202404 {
505+ if API_LEVEL_AT_LEAST (__ANDROID_API_V__, 202404 ) {
500506 return getKeys (&APersistableBundle_getStringVectorKeys, mPBundle );
501507 } else {
502508 return {};
503509 }
504510 }
505511 std::set<std::string> getPersistableBundleKeys () {
506- if AT_LEAST_V_OR_202404 {
512+ if API_LEVEL_AT_LEAST (__ANDROID_API_V__, 202404 ) {
507513 return getKeys (&APersistableBundle_getPersistableBundleKeys, mPBundle );
508514 } else {
509515 return {};
0 commit comments