@@ -14,7 +14,6 @@ using namespace std;
1414
1515#ifdef APPLET_STUB_MAIN
1616#define system_property_set __system_property_set
17- #define system_property_read (...)
1817#define system_property_find __system_property_find
1918#define system_property_read_callback __system_property_read_callback
2019#define system_property_foreach __system_property_foreach
@@ -111,30 +110,21 @@ static bool check_legal_property_name(const char *name) {
111110 return false ;
112111}
113112
114- static void read_prop_with_cb (const prop_info *pi, void *cb) {
115- if (system_property_read_callback) {
116- auto callback = [](void *cb, const char *name, const char *value, uint32_t serial) {
117- static_cast <prop_cb*>(cb)->exec (name, value, serial);
118- };
119- system_property_read_callback (pi, callback, cb);
120- } else {
121- char name[PROP_NAME_MAX ];
122- char value[PROP_VALUE_MAX ];
123- name[0 ] = ' \0 ' ;
124- value[0 ] = ' \0 ' ;
125- system_property_read (pi, name, value);
126- static_cast <prop_cb*>(cb)->exec (name, value, pi->serial );
127- }
113+ void prop_callback::read (const prop_info *pi) {
114+ auto fn = [](void *cb, const char *name, const char *value, uint32_t serial) {
115+ static_cast <prop_callback*>(cb)->exec (name, value, serial);
116+ };
117+ system_property_read_callback (pi, fn, this );
128118}
129119
130120template <class StringType >
131- struct prop_to_string : prop_cb {
121+ struct prop_to_string : prop_callback {
132122 void exec (const char *, const char *value, uint32_t s) override {
133123 val = value;
134124 serial = s;
135125 }
136126 StringType val;
137- uint32_t serial;
127+ uint32_t serial = 0 ;
138128};
139129
140130template <> void prop_to_string<rust::String>::exec(const char *, const char *value, uint32_t s) {
@@ -212,7 +202,7 @@ static StringType get_prop(const char *name, PropFlags flags) {
212202
213203 if (!flags.isPersistOnly ()) {
214204 if (auto pi = system_property_find (name)) {
215- read_prop_with_cb (pi, &cb );
205+ cb. read (pi);
216206 LOGD (" resetprop: get prop [%s]: [%s]\n " , name, cb.val .c_str ());
217207 }
218208 }
@@ -238,28 +228,37 @@ static StringType wait_prop(const char *name, const char *old_value) {
238228 }
239229
240230 prop_to_string<StringType> cb;
241- read_prop_with_cb (pi, &cb );
231+ cb. read (pi);
242232
243233 while (old_value == nullptr || cb.val == old_value) {
244234 LOGD (" resetprop: waiting for prop [%s]\n " , name);
245235 uint32_t new_serial;
246236 system_property_wait (pi, cb.serial , &new_serial, nullptr );
247- read_prop_with_cb (pi, &cb );
237+ cb. read (pi);
248238 if (old_value == nullptr ) break ;
249239 }
250240
251241 LOGD (" resetprop: get prop [%s]: [%s]\n " , name, cb.val .c_str ());
252242 return cb.val ;
253243}
254244
245+ struct prop_collector : prop_callback {
246+ void exec (const char *name, const char *value, uint32_t ) override {
247+ list.insert ({name, value});
248+ }
249+ map<string, string> list;
250+ };
251+
255252static void print_props (PropFlags flags) {
256- prop_list list;
257- prop_collector collector (list);
258- if (!flags.isPersistOnly ())
259- system_property_foreach (read_prop_with_cb, &collector);
253+ prop_collector collector;
254+ if (!flags.isPersistOnly ()) {
255+ system_property_foreach ([](const prop_info *pi, void *cb) {
256+ static_cast <prop_callback*>(cb)->read (pi);
257+ }, &collector);
258+ }
260259 if (flags.isPersist ())
261260 persist_get_props (collector);
262- for (auto &[key, val] : list) {
261+ for (auto &[key, val] : collector. list ) {
263262 const char *v = flags.isContext () ?
264263 (__system_property_get_context (key.data ()) ?: " " ) :
265264 val.data ();
@@ -305,6 +304,17 @@ struct Initialize {
305304 // The platform API only exist on API 26+
306305 system_property_wait = __system_property_wait;
307306 }
307+ if (system_property_read_callback == nullptr ) {
308+ // The platform API only exist on API 26+, create a polyfill
309+ system_property_read_callback = [](const prop_info *pi, auto fn, void *cookie) {
310+ char name[PROP_NAME_MAX ];
311+ char value[PROP_VALUE_MAX ];
312+ name[0 ] = ' \0 ' ;
313+ value[0 ] = ' \0 ' ;
314+ system_property_read (pi, name, value);
315+ fn (cookie, name, value, pi->serial );
316+ };
317+ }
308318#endif
309319 if (__system_properties_init ()) {
310320 LOGE (" resetprop: __system_properties_init error\n " );
@@ -313,7 +323,7 @@ struct Initialize {
313323};
314324
315325static void InitOnce () {
316- static struct Initialize init;
326+ static Initialize init;
317327}
318328
319329#define consume_next (val ) \
0 commit comments