13 #include "pkgmgr-info.h"
14 #include "pkgmgrinfo_debug.h"
15 #include "pkgmgrinfo_private.h"
16 #include "pkgmgr_parser.h"
17 #include "pkgmgrinfo_internal.h"
20 static int __free_applications(gpointer key,
21 gpointer value, gpointer user_data)
23 pkgmgrinfo_basic_free_application((application_x *)value);
26 static void __parse_appcontrol(GList **appcontrol, char *appcontrol_str,
27 char *visibility, char *id)
34 if (appcontrol_str == NULL)
37 dup = strdup(appcontrol_str);
39 _LOGE("out of memory");
44 ac = calloc(1, sizeof(appcontrol_x));
46 _LOGE("out of memory");
49 token = strtok_r(dup, "|", &ptr);
50 if (token && strcmp(token, "NULL"))
51 ac->operation = strdup(token);
52 token = strtok_r(NULL, "|", &ptr);
53 if (token && strcmp(token, "NULL"))
54 ac->uri = strdup(token);
55 token = strtok_r(NULL, "|", &ptr);
56 if (token && strcmp(token, "NULL"))
57 ac->mime = strdup(token);
58 ac->visibility = strdup(visibility);
60 *appcontrol = g_list_prepend(*appcontrol, ac);
61 } while ((token = strtok_r(NULL, ";", &ptr)));
66 static int _appinfo_get_splashscreens(sqlite3 *db, const char *appid,
67 GList **splashscreens)
69 static const char query_raw[] =
70 "SELECT src, type, orientation, indicatordisplay, operation, color_depth "
71 "FROM package_app_splash_screen WHERE app_id=%Q";
78 query = sqlite3_mprintf(query_raw, appid);
80 LOGE("out of memory");
81 return PMINFO_R_ERROR;
84 ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
86 if (ret != SQLITE_OK) {
87 LOGE("prepare failed: %s", sqlite3_errmsg(db));
88 return PMINFO_R_ERROR;
91 while (sqlite3_step(stmt) == SQLITE_ROW) {
92 info = calloc(1, sizeof(splashscreen_x));
94 LOGE("out of memory");
95 sqlite3_finalize(stmt);
96 return PMINFO_R_ERROR;
99 _save_column_str(stmt, idx++, &info->src);
100 _save_column_str(stmt, idx++, &info->type);
101 _save_column_str(stmt, idx++, &info->orientation);
102 _save_column_str(stmt, idx++, &info->indicatordisplay);
103 _save_column_str(stmt, idx++, &info->operation);
104 _save_column_str(stmt, idx++, &info->color_depth);
105 *splashscreens = g_list_prepend(*splashscreens, info);
108 sqlite3_finalize(stmt);
113 static int _appinfo_get_metadata(sqlite3 *db, const char *appid,
116 static const char query_raw[] =
117 "SELECT md_key, md_value "
118 "FROM package_app_app_metadata WHERE app_id=%Q";
125 query = sqlite3_mprintf(query_raw, appid);
127 LOGE("out of memory");
128 return PMINFO_R_ERROR;
131 ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
133 if (ret != SQLITE_OK) {
134 LOGE("prepare failed: %s", sqlite3_errmsg(db));
135 return PMINFO_R_ERROR;
138 while (sqlite3_step(stmt) == SQLITE_ROW) {
139 info = calloc(1, sizeof(metadata_x));
141 LOGE("out of memory");
142 sqlite3_finalize(stmt);
143 return PMINFO_R_ERROR;
146 _save_column_str(stmt, idx++, &info->key);
147 _save_column_str(stmt, idx++, &info->value);
148 *metadata = g_list_prepend(*metadata, info);
151 sqlite3_finalize(stmt);
157 static int _appinfo_get_app_control(sqlite3 *db, const char *appid,
160 static const char query_raw[] =
161 "SELECT app_control, visibility, app_control_id "
162 "FROM package_app_app_control WHERE app_id=%Q";
171 query = sqlite3_mprintf(query_raw, appid);
173 LOGE("out of memory");
174 return PMINFO_R_ERROR;
177 ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
179 if (ret != SQLITE_OK) {
180 LOGE("prepare failed: %s", sqlite3_errmsg(db));
181 return PMINFO_R_ERROR;
184 while (sqlite3_step(stmt) == SQLITE_ROW) {
189 _save_column_str(stmt, idx++, &str);
190 _save_column_str(stmt, idx++, &visibility);
191 _save_column_str(stmt, idx++, &id);
193 __parse_appcontrol(appcontrol, str, visibility, id);
199 sqlite3_finalize(stmt);
204 static int _appinfo_get_category(sqlite3 *db, const char *appid,
207 static const char query_raw[] =
208 "SELECT category FROM package_app_app_category WHERE app_id=%Q";
214 query = sqlite3_mprintf(query_raw, appid);
216 LOGE("out of memory");
217 return PMINFO_R_ERROR;
220 ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
222 if (ret != SQLITE_OK) {
223 LOGE("prepare failed: %s", sqlite3_errmsg(db));
224 return PMINFO_R_ERROR;
227 while (sqlite3_step(stmt) == SQLITE_ROW) {
229 _save_column_str(stmt, 0, &val);
231 *category = g_list_prepend(*category, (gpointer)val);
234 sqlite3_finalize(stmt);
239 static GList *__get_background_category(const char *value)
241 GList *category_list = NULL;
242 int convert_value = 0;
243 if (!value || strlen(value) == 0)
246 convert_value = atoi(value);
247 if (convert_value < 0)
250 if (convert_value & APP_BG_CATEGORY_USER_DISABLE_TRUE_VAL)
251 category_list = g_list_prepend(category_list, strdup(APP_BG_CATEGORY_USER_DISABLE_TRUE_STR));
253 category_list = g_list_prepend(category_list, strdup(APP_BG_CATEGORY_USER_DISABLE_FALSE_STR));
255 if (convert_value & APP_BG_CATEGORY_MEDIA_VAL)
256 category_list = g_list_prepend(category_list, strdup(APP_BG_CATEGORY_MEDIA_STR));
258 if (convert_value & APP_BG_CATEGORY_DOWNLOAD_VAL)
259 category_list = g_list_prepend(category_list, strdup(APP_BG_CATEGORY_DOWNLOAD_STR));
261 if (convert_value & APP_BG_CATEGORY_BGNETWORK_VAL)
262 category_list = g_list_prepend(category_list, strdup(APP_BG_CATEGORY_BGNETWORK_STR));
264 if (convert_value & APP_BG_CATEGORY_LOCATION_VAL)
265 category_list = g_list_prepend(category_list, strdup(APP_BG_CATEGORY_LOCATION_STR));
267 if (convert_value & APP_BG_CATEGORY_SENSOR_VAL)
268 category_list = g_list_prepend(category_list, strdup(APP_BG_CATEGORY_SENSOR_STR));
270 if (convert_value & APP_BG_CATEGORY_IOTCOMM_VAL)
271 category_list = g_list_prepend(category_list, strdup(APP_BG_CATEGORY_IOTCOMM_STR));
273 if (convert_value & APP_BG_CATEGORY_SYSTEM_VAL)
274 category_list = g_list_prepend(category_list, strdup(APP_BG_CATEGORY_SYSTEM));
276 return category_list;
280 static int __bind_params(sqlite3_stmt *stmt, GList *params)
282 GList *tmp_list = NULL;
286 if (stmt == NULL || params == NULL)
287 return PMINFO_R_EINVAL;
291 ret = sqlite3_bind_text(stmt, ++idx, (char *)tmp_list->data, -1, SQLITE_STATIC);
292 if (ret != SQLITE_OK)
293 return PMINFO_R_ERROR;
294 tmp_list = tmp_list->next;
300 static const char join_localized_info[] =
301 " LEFT OUTER JOIN package_app_localized_info"
302 " ON ai.app_id=package_app_localized_info.app_id"
303 " AND package_app_localized_info.app_locale=?";
304 static const char join_category[] =
305 " LEFT OUTER JOIN package_app_app_category"
306 " ON ai.app_id=package_app_app_category.app_id";
307 static const char join_app_control[] =
308 " LEFT OUTER JOIN package_app_app_control"
309 " ON ai.app_id=package_app_app_control.app_id";
310 static const char join_metadata[] =
311 " LEFT OUTER JOIN package_app_app_metadata"
312 " ON ai.app_id=package_app_app_metadata.app_id ";
313 static const char join_privilege[] =
314 " LEFT OUTER JOIN package_privilege_info"
315 " ON ai.package=package_privilege_info.package ";
317 static int _get_filtered_query(pkgmgrinfo_filter_x *filter,
318 const char *locale, uid_t uid, char **query, GList **bind_params)
322 char *condition = NULL;
323 char buf[MAX_QUERY_LEN] = { '\0' };
324 char tmp_query[MAX_QUERY_LEN] = { '\0' };
329 strncat(buf, " WHERE 1=1", sizeof(buf) - strlen(buf) - 1);
331 for (list = filter->list; list; list = list->next) {
332 joined |= __get_filter_condition(list->data, uid, &condition, bind_params);
333 if (condition == NULL)
336 strncat(buf, " AND ", sizeof(buf) - strlen(buf) - 1);
338 strncat(buf, condition, sizeof(buf) - strlen(buf) - 1);
343 if (filter->list_metadata)
344 strncat(buf, " AND (", sizeof(buf) - strlen(buf) - 1);
345 for (list = filter->list_metadata; list; list = list->next) {
346 joined |= __get_metadata_filter_condition(list->data,
347 &condition, bind_params);
348 if (condition == NULL)
350 strncat(buf, condition, sizeof(buf) - strlen(buf) - 1);
354 strncat(buf, " OR ", sizeof(buf) - strlen(buf) - 1);
356 if (filter->list_metadata)
357 strncat(buf, "1=0)", sizeof(buf) - strlen(buf) - 1);
359 if (joined & E_PMINFO_APPINFO_JOIN_LOCALIZED_INFO) {
360 strncat(tmp_query, join_localized_info, sizeof(tmp_query) - strlen(tmp_query) - 1);
361 *bind_params = g_list_append(*bind_params, strdup(locale));
363 if (joined & E_PMINFO_APPINFO_JOIN_CATEGORY)
364 strncat(tmp_query, join_category, sizeof(tmp_query) - strlen(tmp_query) - 1);
365 if (joined & E_PMINFO_APPINFO_JOIN_APP_CONTROL)
366 strncat(tmp_query, join_app_control, sizeof(tmp_query) - strlen(tmp_query) - 1);
367 if (joined & E_PMINFO_APPINFO_JOIN_METADATA)
368 strncat(tmp_query, join_metadata, sizeof(tmp_query) - strlen(tmp_query) - 1);
369 if (joined & E_PMINFO_APPINFO_JOIN_PRIVILEGE)
370 strncat(tmp_query, join_privilege, sizeof(tmp_query) - strlen(tmp_query) - 1);
372 size = strlen(tmp_query) + strlen(buf) + 1;
373 *query = (char *)calloc(1, size);
375 return PMINFO_R_ERROR;
376 snprintf(*query, size, "%s%s", tmp_query, buf);
381 static bool __check_app_storage_status(pkgmgrinfo_filter_x *tmp_filter)
383 GSList *tmp_list = NULL;
384 pkgmgrinfo_node_x *tmp_node = NULL;
387 if (tmp_filter == NULL)
390 property = _pminfo_appinfo_convert_to_prop_bool(PMINFO_APPINFO_PROP_APP_CHECK_STORAGE);
391 for (tmp_list = tmp_filter->list; tmp_list != NULL;
392 tmp_list = g_slist_next(tmp_list)) {
393 tmp_node = (pkgmgrinfo_node_x *)tmp_list->data;
394 if (property == tmp_node->prop) {
395 if (strcmp(tmp_node->value, "true") == 0)
405 static int _appinfo_get_applications(sqlite3 *db, uid_t db_uid, uid_t uid,
406 const char *locale, pkgmgrinfo_filter_x *filter, int flag,
407 GHashTable *applications)
409 static const char query_raw[] =
410 "SELECT DISTINCT ai.app_id, ai.app_installed_storage, "
411 "ai.app_external_path";
412 static const char query_basic[] =
413 ", ai.app_component, ai.app_exec, "
414 "ai.app_nodisplay, ai.app_type, ai.app_onboot, "
415 "ai.app_multiple, ai.app_autorestart, ai.app_taskmanage, "
416 "ai.app_hwacceleration, ai.app_screenreader, "
417 "ai.app_mainapp, ai.app_recentimage, ai.app_launchcondition, "
418 "ai.app_indicatordisplay, ai.app_portraitimg, "
419 "ai.app_landscapeimg, ai.app_guestmodevisibility, "
420 "ai.app_permissiontype, ai.app_preload, ai.app_submode, "
421 "ai.app_submode_mainid, ai.app_launch_mode, ai.app_ui_gadget, "
422 "ai.app_support_disable, ai.app_process_pool, "
423 "ai.app_background_category, ai.app_package_type, "
424 "ai.app_root_path, ai.app_api_version, ai.app_effective_appid, "
425 "ai.app_disable, ai.app_splash_screen_display, ai.app_tep_name, "
426 "ai.app_zip_mount_file, ai.component_type, ai.package, "
427 "ai.app_package_system, ai.app_removable, "
428 "ai.app_package_installed_time, ai.app_support_mode, "
429 "ai.app_support_ambient, ai.app_setup_appid";
430 static const char query_uid_info[] =
431 ", ui.is_disabled, ui.is_splash_screen_enabled";
432 static const char query_label[] =
434 "(SELECT app_label FROM package_app_localized_info WHERE ai.app_id=app_id AND app_locale=?), "
435 "(SELECT app_label FROM package_app_localized_info WHERE ai.app_id=app_id AND app_locale='No Locale'))";
436 static const char query_icon[] =
438 "(SELECT app_icon FROM package_app_localized_info WHERE ai.app_id=app_id AND app_locale=?), "
439 "(SELECT app_icon FROM package_app_localized_info WHERE ai.app_id=app_id AND app_locale='No Locale'))";
440 static const char query_from_clause[] = " FROM package_app_info as ai";
441 static const char query_uid_info_clause[] =
442 " LEFT OUTER JOIN package_app_info_for_uid AS ui "
443 "ON (ai.app_id=ui.app_id AND ui.uid=?)";
444 int ret = PMINFO_R_ERROR;
446 char *bg_category_str = NULL;
447 char *constraint = NULL;
448 char *tmp_record = NULL;
449 char query[MAX_QUERY_LEN] = { '\0' };
450 char buf[BUFSIZE] = { '\0' };
451 application_x *info = NULL;
452 GList *bind_params = NULL;
453 sqlite3_stmt *stmt = NULL;
454 bool is_check_storage = true;
455 const uid_t global_user_uid = GLOBAL_USER;
457 snprintf(query, MAX_QUERY_LEN - 1, "%s", query_raw);
459 if (flag & PMINFO_APPINFO_GET_BASICINFO) {
460 strncat(query, query_basic, sizeof(query) - strlen(query) - 1);
461 strncat(query, query_uid_info, sizeof(query) - strlen(query) - 1);
463 if (flag & PMINFO_APPINFO_GET_LABEL) {
464 strncat(query, query_label, sizeof(query) - strlen(query) - 1);
465 bind_params = g_list_append(bind_params, strdup(locale));
467 if (flag & PMINFO_APPINFO_GET_ICON) {
468 strncat(query, query_icon, sizeof(query) - strlen(query) - 1);
469 bind_params = g_list_append(bind_params, strdup(locale));
472 snprintf(buf, MAX_QUERY_LEN - 1, "%d", uid);
473 bind_params = g_list_append(bind_params, strdup(buf));
475 is_check_storage = __check_app_storage_status(filter);
477 ret = _get_filtered_query(filter, locale, uid, &constraint, &bind_params);
478 if (ret != PMINFO_R_OK) {
479 LOGE("Failed to get WHERE clause");
482 strncat(query, query_from_clause, sizeof(query) - strlen(query) - 1);
484 strncat(query, query_uid_info_clause, sizeof(query) - strlen(query) - 1);
487 strncat(query, constraint, sizeof(query) - strlen(query) - 1);
489 ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
490 if (ret != SQLITE_OK) {
491 LOGE("prepare failed: %s", sqlite3_errmsg(db));
492 ret = PMINFO_R_ERROR;
496 if (g_list_length(bind_params) != 0) {
497 ret = __bind_params(stmt, bind_params);
498 if (ret != SQLITE_OK) {
499 LOGE("Failed to bind parameters");
504 while (sqlite3_step(stmt) == SQLITE_ROW) {
505 info = calloc(1, sizeof(application_x));
507 LOGE("out of memory");
508 ret = PMINFO_R_ERROR;
512 _save_column_str(stmt, idx++, &info->appid);
513 _save_column_str(stmt, idx++, &info->installed_storage);
514 _save_column_str(stmt, idx++ , &info->external_path);
516 if (flag & PMINFO_APPINFO_GET_BASICINFO) {
517 _save_column_str(stmt, idx++, &info->component);
518 _save_column_str(stmt, idx++, &info->exec);
519 _save_column_str(stmt, idx++, &info->nodisplay);
520 _save_column_str(stmt, idx++, &info->type);
521 _save_column_str(stmt, idx++, &info->onboot);
522 _save_column_str(stmt, idx++, &info->multiple);
523 _save_column_str(stmt, idx++, &info->autorestart);
524 _save_column_str(stmt, idx++, &info->taskmanage);
525 _save_column_str(stmt, idx++, &info->hwacceleration);
526 _save_column_str(stmt, idx++, &info->screenreader);
527 _save_column_str(stmt, idx++, &info->mainapp);
528 _save_column_str(stmt, idx++, &info->recentimage);
529 _save_column_str(stmt, idx++, &info->launchcondition);
530 _save_column_str(stmt, idx++, &info->indicatordisplay);
531 _save_column_str(stmt, idx++, &info->portraitimg);
532 _save_column_str(stmt, idx++, &info->landscapeimg);
533 _save_column_str(stmt, idx++, &info->guestmode_visibility);
534 _save_column_str(stmt, idx++, &info->permission_type);
535 _save_column_str(stmt, idx++, &info->preload);
536 _save_column_str(stmt, idx++, &info->submode);
537 _save_column_str(stmt, idx++, &info->submode_mainid);
538 _save_column_str(stmt, idx++, &info->launch_mode);
539 _save_column_str(stmt, idx++, &info->ui_gadget);
540 _save_column_str(stmt, idx++, &info->support_disable);
541 _save_column_str(stmt, idx++, &info->process_pool);
542 _save_column_str(stmt, idx++, &bg_category_str);
543 _save_column_str(stmt, idx++, &info->package_type);
544 _save_column_str(stmt, idx++, &info->root_path);
545 _save_column_str(stmt, idx++, &info->api_version);
546 _save_column_str(stmt, idx++, &info->effective_appid);
547 _save_column_str(stmt, idx++, &info->is_disabled);
548 _save_column_str(stmt, idx++, &info->splash_screen_display);
549 _save_column_str(stmt, idx++, &info->tep_name);
550 _save_column_str(stmt, idx++, &info->zip_mount_file);
551 _save_column_str(stmt, idx++, &info->component_type);
552 _save_column_str(stmt, idx++, &info->package);
553 _save_column_str(stmt, idx++, &info->package_system);
554 _save_column_str(stmt, idx++, &info->removable);
555 _save_column_str(stmt, idx++, &info->package_installed_time);
556 _save_column_str(stmt, idx++, &info->support_mode);
557 _save_column_str(stmt, idx++, &info->support_ambient);
558 _save_column_str(stmt, idx++, &info->setup_appid);
559 info->background_category = __get_background_category(
561 free(bg_category_str);
562 bg_category_str = NULL;
565 info->for_all_users =
566 strdup((db_uid != global_user_uid) ? "false" : "true");
568 if (db_uid != global_user_uid) {
572 _save_column_str(stmt, idx++, &tmp_record);
573 if (tmp_record != NULL) {
574 if (strcasecmp(info->is_disabled, "false") == 0 &&
575 strcasecmp(tmp_record, "false") == 0) {
576 free(info->is_disabled);
577 info->is_disabled = tmp_record;
583 _save_column_str(stmt, idx++, &tmp_record);
584 if (tmp_record != NULL) {
585 if (strcasecmp(info->splash_screen_display, "false") == 0 &&
586 strcasecmp(tmp_record, "false") == 0) {
587 free(info->splash_screen_display);
588 info->splash_screen_display = tmp_record;
595 if (flag & PMINFO_APPINFO_GET_LABEL) {
597 _save_column_str(stmt, idx++, &tmp_record);
598 if (_add_label_info_into_list(locale, tmp_record, &info->label)) {
599 ret = PMINFO_R_ERROR;
604 if (flag & PMINFO_APPINFO_GET_ICON) {
606 _save_column_str(stmt, idx++, &tmp_record);
607 if (_add_icon_info_into_list(locale, tmp_record, &info->icon)) {
608 ret = PMINFO_R_ERROR;
613 if (flag & PMINFO_APPINFO_GET_CATEGORY) {
614 if (_appinfo_get_category(db, info->appid,
616 ret = PMINFO_R_ERROR;
621 if (flag & PMINFO_APPINFO_GET_APP_CONTROL) {
622 if (_appinfo_get_app_control(db, info->appid,
623 &info->appcontrol)) {
624 ret = PMINFO_R_ERROR;
629 if (flag & PMINFO_APPINFO_GET_METADATA) {
630 if (_appinfo_get_metadata(db, info->appid,
632 ret = PMINFO_R_ERROR;
637 if (flag & PMINFO_APPINFO_GET_SPLASH_SCREEN) {
638 if (_appinfo_get_splashscreens(db, info->appid,
639 &info->splashscreens)) {
640 ret = PMINFO_R_ERROR;
645 if (is_check_storage &&
646 __appinfo_check_installed_storage(info) != PMINFO_R_OK) {
647 ret = PMINFO_R_ERROR;
648 pkgmgrinfo_basic_free_application(info);
653 g_hash_table_insert(applications, (gpointer)info->appid,
660 sqlite3_finalize(stmt);
665 if (ret != PMINFO_R_OK && info != NULL)
666 pkgmgrinfo_basic_free_application(info);
668 g_list_free_full(bind_params, free);
673 API int appinfo_internal_filter_get_list(
675 pkgmgrinfo_appinfo_filter_h filter,
676 uid_t uid, const char *locale, GHashTable **appinfo_list) {
680 if (db == NULL || filter == NULL || appinfo_list == NULL) {
682 return PMINFO_R_EINVAL;
685 list = g_hash_table_new(g_str_hash, g_str_equal);
687 return PMINFO_R_ERROR;
689 ret = _appinfo_get_applications(db, uid, uid, locale, filter,
690 PMINFO_APPINFO_GET_ALL, list);
691 if (!g_hash_table_size(list) && uid != GLOBAL_USER)
692 ret = _appinfo_get_applications(db, GLOBAL_USER, uid, locale, filter,
693 PMINFO_APPINFO_GET_ALL, list);
695 if (ret != PMINFO_R_OK) {
696 g_hash_table_foreach_remove(list, __free_applications, NULL);
697 g_hash_table_destroy(list);
701 if (!g_hash_table_size(list)) {
702 // TODO: add logs that failed to find appid
703 g_hash_table_destroy(list);
704 return PMINFO_R_ENOENT;
707 *appinfo_list = list;