13 #include "pkgmgr-info.h"
14 #include "pkgmgrinfo_debug.h"
15 #include "pkgmgrinfo_private.h"
16 #include "pkgmgr_parser.h"
19 static void __free_applications(gpointer data)
21 pkgmgrinfo_basic_free_application((application_x *)data);
24 static void __parse_appcontrol(GList **appcontrol, char *appcontrol_str,
25 char *visibility, char *id)
32 if (appcontrol_str == NULL)
35 dup = strdup(appcontrol_str);
37 _LOGE("out of memory");
42 ac = calloc(1, sizeof(appcontrol_x));
44 _LOGE("out of memory");
47 token = strtok_r(dup, "|", &ptr);
48 if (token && strcmp(token, "NULL"))
49 ac->operation = strdup(token);
50 token = strtok_r(NULL, "|", &ptr);
51 if (token && strcmp(token, "NULL"))
52 ac->uri = strdup(token);
53 token = strtok_r(NULL, "|", &ptr);
54 if (token && strcmp(token, "NULL"))
55 ac->mime = strdup(token);
56 ac->visibility = strdup(visibility);
58 *appcontrol = g_list_prepend(*appcontrol, ac);
59 } while ((token = strtok_r(NULL, ";", &ptr)));
64 static int _appinfo_get_splashscreens(sqlite3 *db, const char *appid,
65 GList **splashscreens)
67 static const char query_raw[] =
68 "SELECT src, type, orientation, indicatordisplay, operation, color_depth "
69 "FROM package_app_splash_screen WHERE app_id=%Q";
76 query = sqlite3_mprintf(query_raw, appid);
78 LOGE("out of memory");
79 return PMINFO_R_ERROR;
82 ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
84 if (ret != SQLITE_OK) {
85 LOGE("prepare failed: %s", sqlite3_errmsg(db));
86 return PMINFO_R_ERROR;
89 while (sqlite3_step(stmt) == SQLITE_ROW) {
90 info = calloc(1, sizeof(splashscreen_x));
92 LOGE("out of memory");
93 sqlite3_finalize(stmt);
94 return PMINFO_R_ERROR;
97 _save_column_str(stmt, idx++, &info->src);
98 _save_column_str(stmt, idx++, &info->type);
99 _save_column_str(stmt, idx++, &info->orientation);
100 _save_column_str(stmt, idx++, &info->indicatordisplay);
101 _save_column_str(stmt, idx++, &info->operation);
102 _save_column_str(stmt, idx++, &info->color_depth);
103 *splashscreens = g_list_prepend(*splashscreens, info);
106 sqlite3_finalize(stmt);
111 static int _appinfo_get_metadata(sqlite3 *db, const char *appid,
114 static const char query_raw[] =
115 "SELECT md_key, md_value "
116 "FROM package_app_app_metadata WHERE app_id=%Q";
123 query = sqlite3_mprintf(query_raw, appid);
125 LOGE("out of memory");
126 return PMINFO_R_ERROR;
129 ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
131 if (ret != SQLITE_OK) {
132 LOGE("prepare failed: %s", sqlite3_errmsg(db));
133 return PMINFO_R_ERROR;
136 while (sqlite3_step(stmt) == SQLITE_ROW) {
137 info = calloc(1, sizeof(metadata_x));
139 LOGE("out of memory");
140 sqlite3_finalize(stmt);
141 return PMINFO_R_ERROR;
144 _save_column_str(stmt, idx++, &info->key);
145 _save_column_str(stmt, idx++, &info->value);
146 *metadata = g_list_prepend(*metadata, info);
149 sqlite3_finalize(stmt);
155 static int _appinfo_get_app_control(sqlite3 *db, const char *appid,
158 static const char query_raw[] =
159 "SELECT app_control, visibility, app_control_id "
160 "FROM package_app_app_control WHERE app_id=%Q";
169 query = sqlite3_mprintf(query_raw, appid);
171 LOGE("out of memory");
172 return PMINFO_R_ERROR;
175 ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
177 if (ret != SQLITE_OK) {
178 LOGE("prepare failed: %s", sqlite3_errmsg(db));
179 return PMINFO_R_ERROR;
182 while (sqlite3_step(stmt) == SQLITE_ROW) {
187 _save_column_str(stmt, idx++, &str);
188 _save_column_str(stmt, idx++, &visibility);
189 _save_column_str(stmt, idx++, &id);
191 __parse_appcontrol(appcontrol, str, visibility, id);
197 sqlite3_finalize(stmt);
202 static int _appinfo_get_category(sqlite3 *db, const char *appid,
205 static const char query_raw[] =
206 "SELECT category FROM package_app_app_category WHERE app_id=%Q";
212 query = sqlite3_mprintf(query_raw, appid);
214 LOGE("out of memory");
215 return PMINFO_R_ERROR;
218 ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
220 if (ret != SQLITE_OK) {
221 LOGE("prepare failed: %s", sqlite3_errmsg(db));
222 return PMINFO_R_ERROR;
225 while (sqlite3_step(stmt) == SQLITE_ROW) {
227 _save_column_str(stmt, 0, &val);
229 *category = g_list_prepend(*category, (gpointer)val);
232 sqlite3_finalize(stmt);
237 static GList *__get_background_category(const char *value)
239 GList *category_list = NULL;
240 int convert_value = 0;
241 if (!value || strlen(value) == 0)
244 convert_value = atoi(value);
245 if (convert_value < 0)
248 if (convert_value & APP_BG_CATEGORY_USER_DISABLE_TRUE_VAL)
249 category_list = g_list_prepend(category_list, strdup(APP_BG_CATEGORY_USER_DISABLE_TRUE_STR));
251 category_list = g_list_prepend(category_list, strdup(APP_BG_CATEGORY_USER_DISABLE_FALSE_STR));
253 if (convert_value & APP_BG_CATEGORY_MEDIA_VAL)
254 category_list = g_list_prepend(category_list, strdup(APP_BG_CATEGORY_MEDIA_STR));
256 if (convert_value & APP_BG_CATEGORY_DOWNLOAD_VAL)
257 category_list = g_list_prepend(category_list, strdup(APP_BG_CATEGORY_DOWNLOAD_STR));
259 if (convert_value & APP_BG_CATEGORY_BGNETWORK_VAL)
260 category_list = g_list_prepend(category_list, strdup(APP_BG_CATEGORY_BGNETWORK_STR));
262 if (convert_value & APP_BG_CATEGORY_LOCATION_VAL)
263 category_list = g_list_prepend(category_list, strdup(APP_BG_CATEGORY_LOCATION_STR));
265 if (convert_value & APP_BG_CATEGORY_SENSOR_VAL)
266 category_list = g_list_prepend(category_list, strdup(APP_BG_CATEGORY_SENSOR_STR));
268 if (convert_value & APP_BG_CATEGORY_IOTCOMM_VAL)
269 category_list = g_list_prepend(category_list, strdup(APP_BG_CATEGORY_IOTCOMM_STR));
271 if (convert_value & APP_BG_CATEGORY_SYSTEM_VAL)
272 category_list = g_list_prepend(category_list, strdup(APP_BG_CATEGORY_SYSTEM));
274 return category_list;
278 static int __bind_params(sqlite3_stmt *stmt, GList *params)
280 GList *tmp_list = NULL;
284 if (stmt == NULL || params == NULL)
285 return PMINFO_R_EINVAL;
289 ret = sqlite3_bind_text(stmt, ++idx, (char *)tmp_list->data, -1, SQLITE_STATIC);
290 if (ret != SQLITE_OK)
291 return PMINFO_R_ERROR;
292 tmp_list = tmp_list->next;
298 static const char join_localized_info[] =
299 " LEFT OUTER JOIN package_app_localized_info"
300 " ON ai.app_id=package_app_localized_info.app_id"
301 " AND package_app_localized_info.app_locale=?";
302 static const char join_category[] =
303 " LEFT OUTER JOIN package_app_app_category"
304 " ON ai.app_id=package_app_app_category.app_id";
305 static const char join_app_control[] =
306 " LEFT OUTER JOIN package_app_app_control"
307 " ON ai.app_id=package_app_app_control.app_id";
308 static const char join_metadata[] =
309 " LEFT OUTER JOIN package_app_app_metadata"
310 " ON ai.app_id=package_app_app_metadata.app_id ";
311 static const char join_privilege[] =
312 " LEFT OUTER JOIN package_privilege_info"
313 " ON ai.package=package_privilege_info.package ";
315 static int _get_filtered_query(pkgmgrinfo_filter_x *filter,
316 const char *locale, uid_t uid, char **query, GList **bind_params)
320 char *condition = NULL;
321 char buf[MAX_QUERY_LEN] = { '\0' };
322 char tmp_query[MAX_QUERY_LEN] = { '\0' };
327 strncat(buf, " WHERE 1=1", sizeof(buf) - strlen(buf) - 1);
329 for (list = filter->list; list; list = list->next) {
330 joined |= __get_filter_condition(list->data, uid, &condition, bind_params);
331 if (condition == NULL)
334 strncat(buf, " AND ", sizeof(buf) - strlen(buf) - 1);
336 strncat(buf, condition, sizeof(buf) - strlen(buf) - 1);
341 if (filter->list_metadata)
342 strncat(buf, " AND (", sizeof(buf) - strlen(buf) - 1);
343 for (list = filter->list_metadata; list; list = list->next) {
344 joined |= __get_metadata_filter_condition(list->data,
345 &condition, bind_params);
346 if (condition == NULL)
348 strncat(buf, condition, sizeof(buf) - strlen(buf) - 1);
352 strncat(buf, " OR ", sizeof(buf) - strlen(buf) - 1);
354 if (filter->list_metadata)
355 strncat(buf, "1=0)", sizeof(buf) - strlen(buf) - 1);
357 if (joined & E_PMINFO_APPINFO_JOIN_LOCALIZED_INFO) {
358 strncat(tmp_query, join_localized_info, sizeof(tmp_query) - strlen(tmp_query) - 1);
359 *bind_params = g_list_append(*bind_params, strdup(locale));
361 if (joined & E_PMINFO_APPINFO_JOIN_CATEGORY)
362 strncat(tmp_query, join_category, sizeof(tmp_query) - strlen(tmp_query) - 1);
363 if (joined & E_PMINFO_APPINFO_JOIN_APP_CONTROL)
364 strncat(tmp_query, join_app_control, sizeof(tmp_query) - strlen(tmp_query) - 1);
365 if (joined & E_PMINFO_APPINFO_JOIN_METADATA)
366 strncat(tmp_query, join_metadata, sizeof(tmp_query) - strlen(tmp_query) - 1);
367 if (joined & E_PMINFO_APPINFO_JOIN_PRIVILEGE)
368 strncat(tmp_query, join_privilege, sizeof(tmp_query) - strlen(tmp_query) - 1);
370 size = strlen(tmp_query) + strlen(buf) + 1;
371 *query = (char *)calloc(1, size);
373 return PMINFO_R_ERROR;
374 snprintf(*query, size, "%s%s", tmp_query, buf);
379 static bool __check_app_storage_status(pkgmgrinfo_filter_x *tmp_filter)
381 GSList *tmp_list = NULL;
382 pkgmgrinfo_node_x *tmp_node = NULL;
385 if (tmp_filter == NULL)
388 property = _pminfo_appinfo_convert_to_prop_bool(PMINFO_APPINFO_PROP_APP_CHECK_STORAGE);
389 for (tmp_list = tmp_filter->list; tmp_list != NULL;
390 tmp_list = g_slist_next(tmp_list)) {
391 tmp_node = (pkgmgrinfo_node_x *)tmp_list->data;
392 if (property == tmp_node->prop) {
393 if (strcmp(tmp_node->value, "true") == 0)
403 static int _appinfo_get_applications(sqlite3 *db, uid_t db_uid, uid_t uid,
404 const char *locale, pkgmgrinfo_filter_x *filter, int flag,
405 GHashTable *applications)
407 static const char query_raw[] =
408 "SELECT DISTINCT ai.app_id, ai.app_installed_storage, "
409 "ai.app_external_path";
410 static const char query_basic[] =
411 ", ai.app_component, ai.app_exec, "
412 "ai.app_nodisplay, ai.app_type, ai.app_onboot, "
413 "ai.app_multiple, ai.app_autorestart, ai.app_taskmanage, "
414 "ai.app_hwacceleration, ai.app_screenreader, "
415 "ai.app_mainapp, ai.app_recentimage, ai.app_launchcondition, "
416 "ai.app_indicatordisplay, ai.app_portraitimg, "
417 "ai.app_landscapeimg, ai.app_guestmodevisibility, "
418 "ai.app_permissiontype, ai.app_preload, ai.app_submode, "
419 "ai.app_submode_mainid, ai.app_launch_mode, ai.app_ui_gadget, "
420 "ai.app_support_disable, ai.app_process_pool, "
421 "ai.app_background_category, ai.app_package_type, "
422 "ai.app_root_path, ai.app_api_version, ai.app_effective_appid, "
423 "ai.app_disable, ai.app_splash_screen_display, ai.app_tep_name, "
424 "ai.app_zip_mount_file, ai.component_type, ai.package, "
425 "ai.app_package_system, ai.app_removable, "
426 "ai.app_package_installed_time, ai.app_support_mode, "
427 "ai.app_support_ambient, ai.app_setup_appid";
428 static const char query_uid_info[] =
429 ", ui.is_disabled, ui.is_splash_screen_enabled";
430 static const char query_label[] =
432 "(SELECT app_label FROM package_app_localized_info WHERE ai.app_id=app_id AND app_locale=?), "
433 "(SELECT app_label FROM package_app_localized_info WHERE ai.app_id=app_id AND app_locale='No Locale'))";
434 static const char query_icon[] =
436 "(SELECT app_icon FROM package_app_localized_info WHERE ai.app_id=app_id AND app_locale=?), "
437 "(SELECT app_icon FROM package_app_localized_info WHERE ai.app_id=app_id AND app_locale='No Locale'))";
438 static const char query_from_clause[] = " FROM package_app_info as ai";
439 static const char query_uid_info_clause[] =
440 " LEFT OUTER JOIN package_app_info_for_uid AS ui "
441 "ON (ai.app_id=ui.app_id AND ui.uid=?)";
442 int ret = PMINFO_R_ERROR;
444 char *bg_category_str = NULL;
445 char *constraint = NULL;
446 char *tmp_record = NULL;
447 char query[MAX_QUERY_LEN] = { '\0' };
448 char buf[BUFSIZE] = { '\0' };
449 application_x *info = NULL;
450 GList *bind_params = NULL;
451 sqlite3_stmt *stmt = NULL;
452 bool is_check_storage = true;
453 const uid_t global_user_uid = GLOBAL_USER;
455 snprintf(query, MAX_QUERY_LEN - 1, "%s", query_raw);
457 if (flag & PMINFO_APPINFO_GET_BASICINFO) {
458 strncat(query, query_basic, sizeof(query) - strlen(query) - 1);
459 strncat(query, query_uid_info, sizeof(query) - strlen(query) - 1);
461 if (flag & PMINFO_APPINFO_GET_LABEL) {
462 strncat(query, query_label, sizeof(query) - strlen(query) - 1);
463 bind_params = g_list_append(bind_params, strdup(locale));
465 if (flag & PMINFO_APPINFO_GET_ICON) {
466 strncat(query, query_icon, sizeof(query) - strlen(query) - 1);
467 bind_params = g_list_append(bind_params, strdup(locale));
470 snprintf(buf, MAX_QUERY_LEN - 1, "%d", uid);
471 bind_params = g_list_append(bind_params, strdup(buf));
473 is_check_storage = __check_app_storage_status(filter);
475 ret = _get_filtered_query(filter, locale, uid, &constraint, &bind_params);
476 if (ret != PMINFO_R_OK) {
477 LOGE("Failed to get WHERE clause");
480 strncat(query, query_from_clause, sizeof(query) - strlen(query) - 1);
482 strncat(query, query_uid_info_clause, sizeof(query) - strlen(query) - 1);
485 strncat(query, constraint, sizeof(query) - strlen(query) - 1);
487 ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
488 if (ret != SQLITE_OK) {
489 LOGE("prepare failed: %s", sqlite3_errmsg(db));
490 ret = PMINFO_R_ERROR;
494 if (g_list_length(bind_params) != 0) {
495 ret = __bind_params(stmt, bind_params);
496 if (ret != SQLITE_OK) {
497 LOGE("Failed to bind parameters");
502 while (sqlite3_step(stmt) == SQLITE_ROW) {
503 info = calloc(1, sizeof(application_x));
505 LOGE("out of memory");
506 ret = PMINFO_R_ERROR;
510 _save_column_str(stmt, idx++, &info->appid);
511 _save_column_str(stmt, idx++, &info->installed_storage);
512 _save_column_str(stmt, idx++ , &info->external_path);
514 if (flag & PMINFO_APPINFO_GET_BASICINFO) {
515 _save_column_str(stmt, idx++, &info->component);
516 _save_column_str(stmt, idx++, &info->exec);
517 _save_column_str(stmt, idx++, &info->nodisplay);
518 _save_column_str(stmt, idx++, &info->type);
519 _save_column_str(stmt, idx++, &info->onboot);
520 _save_column_str(stmt, idx++, &info->multiple);
521 _save_column_str(stmt, idx++, &info->autorestart);
522 _save_column_str(stmt, idx++, &info->taskmanage);
523 _save_column_str(stmt, idx++, &info->hwacceleration);
524 _save_column_str(stmt, idx++, &info->screenreader);
525 _save_column_str(stmt, idx++, &info->mainapp);
526 _save_column_str(stmt, idx++, &info->recentimage);
527 _save_column_str(stmt, idx++, &info->launchcondition);
528 _save_column_str(stmt, idx++, &info->indicatordisplay);
529 _save_column_str(stmt, idx++, &info->portraitimg);
530 _save_column_str(stmt, idx++, &info->landscapeimg);
531 _save_column_str(stmt, idx++, &info->guestmode_visibility);
532 _save_column_str(stmt, idx++, &info->permission_type);
533 _save_column_str(stmt, idx++, &info->preload);
534 _save_column_str(stmt, idx++, &info->submode);
535 _save_column_str(stmt, idx++, &info->submode_mainid);
536 _save_column_str(stmt, idx++, &info->launch_mode);
537 _save_column_str(stmt, idx++, &info->ui_gadget);
538 _save_column_str(stmt, idx++, &info->support_disable);
539 _save_column_str(stmt, idx++, &info->process_pool);
540 _save_column_str(stmt, idx++, &bg_category_str);
541 _save_column_str(stmt, idx++, &info->package_type);
542 _save_column_str(stmt, idx++, &info->root_path);
543 _save_column_str(stmt, idx++, &info->api_version);
544 _save_column_str(stmt, idx++, &info->effective_appid);
545 _save_column_str(stmt, idx++, &info->is_disabled);
546 _save_column_str(stmt, idx++, &info->splash_screen_display);
547 _save_column_str(stmt, idx++, &info->tep_name);
548 _save_column_str(stmt, idx++, &info->zip_mount_file);
549 _save_column_str(stmt, idx++, &info->component_type);
550 _save_column_str(stmt, idx++, &info->package);
551 _save_column_str(stmt, idx++, &info->package_system);
552 _save_column_str(stmt, idx++, &info->removable);
553 _save_column_str(stmt, idx++, &info->package_installed_time);
554 _save_column_str(stmt, idx++, &info->support_mode);
555 _save_column_str(stmt, idx++, &info->support_ambient);
556 _save_column_str(stmt, idx++, &info->setup_appid);
557 info->background_category = __get_background_category(
559 free(bg_category_str);
560 bg_category_str = NULL;
563 info->for_all_users =
564 strdup((db_uid != global_user_uid) ? "false" : "true");
566 if (db_uid != global_user_uid) {
570 _save_column_str(stmt, idx++, &tmp_record);
571 if (tmp_record != NULL) {
572 if (strcasecmp(info->is_disabled, "false") == 0 &&
573 strcasecmp(tmp_record, "false") == 0) {
574 free(info->is_disabled);
575 info->is_disabled = tmp_record;
581 _save_column_str(stmt, idx++, &tmp_record);
582 if (tmp_record != NULL) {
583 if (strcasecmp(info->splash_screen_display, "false") == 0 &&
584 strcasecmp(tmp_record, "false") == 0) {
585 free(info->splash_screen_display);
586 info->splash_screen_display = tmp_record;
593 if (flag & PMINFO_APPINFO_GET_LABEL) {
595 _save_column_str(stmt, idx++, &tmp_record);
596 if (_add_label_info_into_list(locale, tmp_record, &info->label)) {
597 ret = PMINFO_R_ERROR;
602 if (flag & PMINFO_APPINFO_GET_ICON) {
604 _save_column_str(stmt, idx++, &tmp_record);
605 if (_add_icon_info_into_list(locale, tmp_record, &info->icon)) {
606 ret = PMINFO_R_ERROR;
611 if (flag & PMINFO_APPINFO_GET_CATEGORY) {
612 if (_appinfo_get_category(db, info->appid,
614 ret = PMINFO_R_ERROR;
619 if (flag & PMINFO_APPINFO_GET_APP_CONTROL) {
620 if (_appinfo_get_app_control(db, info->appid,
621 &info->appcontrol)) {
622 ret = PMINFO_R_ERROR;
627 if (flag & PMINFO_APPINFO_GET_METADATA) {
628 if (_appinfo_get_metadata(db, info->appid,
630 ret = PMINFO_R_ERROR;
635 if (flag & PMINFO_APPINFO_GET_SPLASH_SCREEN) {
636 if (_appinfo_get_splashscreens(db, info->appid,
637 &info->splashscreens)) {
638 ret = PMINFO_R_ERROR;
643 if (is_check_storage &&
644 __appinfo_check_installed_storage(info) != PMINFO_R_OK) {
645 ret = PMINFO_R_ERROR;
646 pkgmgrinfo_basic_free_application(info);
651 g_hash_table_insert(applications, (gpointer)info->appid,
658 sqlite3_finalize(stmt);
663 if (ret != PMINFO_R_OK && info != NULL)
664 pkgmgrinfo_basic_free_application(info);
666 g_list_free_full(bind_params, free);
671 API int appinfo_internal_filter_get_list(
673 pkgmgrinfo_appinfo_filter_h filter,
674 uid_t uid, GHashTable **appinfo_list) {
679 if (db == NULL || filter == NULL || appinfo_list == NULL) {
681 return PMINFO_R_EINVAL;
684 locale = _get_system_locale();
686 return PMINFO_R_ERROR;
688 list = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
689 __free_applications);
692 return PMINFO_R_ERROR;
695 ret = _appinfo_get_applications(db, uid, uid, locale, filter,
696 PMINFO_APPINFO_GET_ALL, list);
697 if (!g_hash_table_size(list) && uid != GLOBAL_USER)
698 ret = _appinfo_get_applications(db, GLOBAL_USER, uid, locale, filter,
699 PMINFO_APPINFO_GET_ALL, list);
701 if (ret != PMINFO_R_OK) {
702 g_hash_table_destroy(list);
707 if (!g_hash_table_size(list)) {
708 // TODO: add logs that failed to find appid
709 g_hash_table_destroy(list);
711 return PMINFO_R_ENOENT;
714 *appinfo_list = list;
715 // TODO: locale should be delivered