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 void __free_applications(gpointer data)
22 pkgmgrinfo_basic_free_application((application_x *)data);
25 static void __parse_appcontrol(GList **appcontrol, char *appcontrol_str,
26 char *visibility, char *id)
33 if (appcontrol_str == NULL)
36 dup = strdup(appcontrol_str);
38 _LOGE("out of memory");
43 ac = calloc(1, sizeof(appcontrol_x));
45 _LOGE("out of memory");
48 token = strtok_r(dup, "|", &ptr);
49 if (token && strcmp(token, "NULL"))
50 ac->operation = strdup(token);
51 token = strtok_r(NULL, "|", &ptr);
52 if (token && strcmp(token, "NULL"))
53 ac->uri = strdup(token);
54 token = strtok_r(NULL, "|", &ptr);
55 if (token && strcmp(token, "NULL"))
56 ac->mime = strdup(token);
57 ac->visibility = strdup(visibility);
59 *appcontrol = g_list_prepend(*appcontrol, ac);
60 } while ((token = strtok_r(NULL, ";", &ptr)));
65 static int _appinfo_get_splashscreens(sqlite3 *db, const char *appid,
66 GList **splashscreens)
68 static const char query_raw[] =
69 "SELECT src, type, orientation, indicatordisplay, operation, color_depth "
70 "FROM package_app_splash_screen WHERE app_id=%Q";
77 query = sqlite3_mprintf(query_raw, appid);
79 LOGE("out of memory");
80 return PMINFO_R_ERROR;
83 ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
85 if (ret != SQLITE_OK) {
86 LOGE("prepare failed: %s", sqlite3_errmsg(db));
87 return PMINFO_R_ERROR;
90 while (sqlite3_step(stmt) == SQLITE_ROW) {
91 info = calloc(1, sizeof(splashscreen_x));
93 LOGE("out of memory");
94 sqlite3_finalize(stmt);
95 return PMINFO_R_ERROR;
98 _save_column_str(stmt, idx++, &info->src);
99 _save_column_str(stmt, idx++, &info->type);
100 _save_column_str(stmt, idx++, &info->orientation);
101 _save_column_str(stmt, idx++, &info->indicatordisplay);
102 _save_column_str(stmt, idx++, &info->operation);
103 _save_column_str(stmt, idx++, &info->color_depth);
104 *splashscreens = g_list_prepend(*splashscreens, info);
107 sqlite3_finalize(stmt);
112 static int _appinfo_get_metadata(sqlite3 *db, const char *appid,
115 static const char query_raw[] =
116 "SELECT md_key, md_value "
117 "FROM package_app_app_metadata WHERE app_id=%Q";
124 query = sqlite3_mprintf(query_raw, appid);
126 LOGE("out of memory");
127 return PMINFO_R_ERROR;
130 ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
132 if (ret != SQLITE_OK) {
133 LOGE("prepare failed: %s", sqlite3_errmsg(db));
134 return PMINFO_R_ERROR;
137 while (sqlite3_step(stmt) == SQLITE_ROW) {
138 info = calloc(1, sizeof(metadata_x));
140 LOGE("out of memory");
141 sqlite3_finalize(stmt);
142 return PMINFO_R_ERROR;
145 _save_column_str(stmt, idx++, &info->key);
146 _save_column_str(stmt, idx++, &info->value);
147 *metadata = g_list_prepend(*metadata, info);
150 sqlite3_finalize(stmt);
156 static int _appinfo_get_app_control(sqlite3 *db, const char *appid,
159 static const char query_raw[] =
160 "SELECT app_control, visibility, app_control_id "
161 "FROM package_app_app_control WHERE app_id=%Q";
170 query = sqlite3_mprintf(query_raw, appid);
172 LOGE("out of memory");
173 return PMINFO_R_ERROR;
176 ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
178 if (ret != SQLITE_OK) {
179 LOGE("prepare failed: %s", sqlite3_errmsg(db));
180 return PMINFO_R_ERROR;
183 while (sqlite3_step(stmt) == SQLITE_ROW) {
188 _save_column_str(stmt, idx++, &str);
189 _save_column_str(stmt, idx++, &visibility);
190 _save_column_str(stmt, idx++, &id);
192 __parse_appcontrol(appcontrol, str, visibility, id);
198 sqlite3_finalize(stmt);
203 static int _appinfo_get_category(sqlite3 *db, const char *appid,
206 static const char query_raw[] =
207 "SELECT category FROM package_app_app_category WHERE app_id=%Q";
213 query = sqlite3_mprintf(query_raw, appid);
215 LOGE("out of memory");
216 return PMINFO_R_ERROR;
219 ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
221 if (ret != SQLITE_OK) {
222 LOGE("prepare failed: %s", sqlite3_errmsg(db));
223 return PMINFO_R_ERROR;
226 while (sqlite3_step(stmt) == SQLITE_ROW) {
228 _save_column_str(stmt, 0, &val);
230 *category = g_list_prepend(*category, (gpointer)val);
233 sqlite3_finalize(stmt);
238 static GList *__get_background_category(const char *value)
240 GList *category_list = NULL;
241 int convert_value = 0;
242 if (!value || strlen(value) == 0)
245 convert_value = atoi(value);
246 if (convert_value < 0)
249 if (convert_value & APP_BG_CATEGORY_USER_DISABLE_TRUE_VAL)
250 category_list = g_list_prepend(category_list, strdup(APP_BG_CATEGORY_USER_DISABLE_TRUE_STR));
252 category_list = g_list_prepend(category_list, strdup(APP_BG_CATEGORY_USER_DISABLE_FALSE_STR));
254 if (convert_value & APP_BG_CATEGORY_MEDIA_VAL)
255 category_list = g_list_prepend(category_list, strdup(APP_BG_CATEGORY_MEDIA_STR));
257 if (convert_value & APP_BG_CATEGORY_DOWNLOAD_VAL)
258 category_list = g_list_prepend(category_list, strdup(APP_BG_CATEGORY_DOWNLOAD_STR));
260 if (convert_value & APP_BG_CATEGORY_BGNETWORK_VAL)
261 category_list = g_list_prepend(category_list, strdup(APP_BG_CATEGORY_BGNETWORK_STR));
263 if (convert_value & APP_BG_CATEGORY_LOCATION_VAL)
264 category_list = g_list_prepend(category_list, strdup(APP_BG_CATEGORY_LOCATION_STR));
266 if (convert_value & APP_BG_CATEGORY_SENSOR_VAL)
267 category_list = g_list_prepend(category_list, strdup(APP_BG_CATEGORY_SENSOR_STR));
269 if (convert_value & APP_BG_CATEGORY_IOTCOMM_VAL)
270 category_list = g_list_prepend(category_list, strdup(APP_BG_CATEGORY_IOTCOMM_STR));
272 if (convert_value & APP_BG_CATEGORY_SYSTEM_VAL)
273 category_list = g_list_prepend(category_list, strdup(APP_BG_CATEGORY_SYSTEM));
275 return category_list;
279 static int __bind_params(sqlite3_stmt *stmt, GList *params)
281 GList *tmp_list = NULL;
285 if (stmt == NULL || params == NULL)
286 return PMINFO_R_EINVAL;
290 ret = sqlite3_bind_text(stmt, ++idx, (char *)tmp_list->data, -1, SQLITE_STATIC);
291 if (ret != SQLITE_OK)
292 return PMINFO_R_ERROR;
293 tmp_list = tmp_list->next;
299 static const char join_localized_info[] =
300 " LEFT OUTER JOIN package_app_localized_info"
301 " ON ai.app_id=package_app_localized_info.app_id"
302 " AND package_app_localized_info.app_locale=?";
303 static const char join_category[] =
304 " LEFT OUTER JOIN package_app_app_category"
305 " ON ai.app_id=package_app_app_category.app_id";
306 static const char join_app_control[] =
307 " LEFT OUTER JOIN package_app_app_control"
308 " ON ai.app_id=package_app_app_control.app_id";
309 static const char join_metadata[] =
310 " LEFT OUTER JOIN package_app_app_metadata"
311 " ON ai.app_id=package_app_app_metadata.app_id ";
312 static const char join_privilege[] =
313 " LEFT OUTER JOIN package_privilege_info"
314 " ON ai.package=package_privilege_info.package ";
316 static int _get_filtered_query(pkgmgrinfo_filter_x *filter,
317 const char *locale, uid_t uid, char **query, GList **bind_params)
321 char *condition = NULL;
322 char buf[MAX_QUERY_LEN] = { '\0' };
323 char tmp_query[MAX_QUERY_LEN] = { '\0' };
328 strncat(buf, " WHERE 1=1", sizeof(buf) - strlen(buf) - 1);
330 for (list = filter->list; list; list = list->next) {
331 joined |= __get_filter_condition(list->data, uid, &condition, bind_params);
332 if (condition == NULL)
335 strncat(buf, " AND ", sizeof(buf) - strlen(buf) - 1);
337 strncat(buf, condition, sizeof(buf) - strlen(buf) - 1);
342 if (filter->list_metadata)
343 strncat(buf, " AND (", sizeof(buf) - strlen(buf) - 1);
344 for (list = filter->list_metadata; list; list = list->next) {
345 joined |= __get_metadata_filter_condition(list->data,
346 &condition, bind_params);
347 if (condition == NULL)
349 strncat(buf, condition, sizeof(buf) - strlen(buf) - 1);
353 strncat(buf, " OR ", sizeof(buf) - strlen(buf) - 1);
355 if (filter->list_metadata)
356 strncat(buf, "1=0)", sizeof(buf) - strlen(buf) - 1);
358 if (joined & E_PMINFO_APPINFO_JOIN_LOCALIZED_INFO) {
359 strncat(tmp_query, join_localized_info, sizeof(tmp_query) - strlen(tmp_query) - 1);
360 *bind_params = g_list_append(*bind_params, strdup(locale));
362 if (joined & E_PMINFO_APPINFO_JOIN_CATEGORY)
363 strncat(tmp_query, join_category, sizeof(tmp_query) - strlen(tmp_query) - 1);
364 if (joined & E_PMINFO_APPINFO_JOIN_APP_CONTROL)
365 strncat(tmp_query, join_app_control, sizeof(tmp_query) - strlen(tmp_query) - 1);
366 if (joined & E_PMINFO_APPINFO_JOIN_METADATA)
367 strncat(tmp_query, join_metadata, sizeof(tmp_query) - strlen(tmp_query) - 1);
368 if (joined & E_PMINFO_APPINFO_JOIN_PRIVILEGE)
369 strncat(tmp_query, join_privilege, sizeof(tmp_query) - strlen(tmp_query) - 1);
371 size = strlen(tmp_query) + strlen(buf) + 1;
372 *query = (char *)calloc(1, size);
374 return PMINFO_R_ERROR;
375 snprintf(*query, size, "%s%s", tmp_query, buf);
380 static bool __check_app_storage_status(pkgmgrinfo_filter_x *tmp_filter)
382 GSList *tmp_list = NULL;
383 pkgmgrinfo_node_x *tmp_node = NULL;
386 if (tmp_filter == NULL)
389 property = _pminfo_appinfo_convert_to_prop_bool(PMINFO_APPINFO_PROP_APP_CHECK_STORAGE);
390 for (tmp_list = tmp_filter->list; tmp_list != NULL;
391 tmp_list = g_slist_next(tmp_list)) {
392 tmp_node = (pkgmgrinfo_node_x *)tmp_list->data;
393 if (property == tmp_node->prop) {
394 if (strcmp(tmp_node->value, "true") == 0)
404 static int _appinfo_get_applications(sqlite3 *db, uid_t db_uid, uid_t uid,
405 const char *locale, pkgmgrinfo_filter_x *filter, int flag,
406 GHashTable *applications)
408 static const char query_raw[] =
409 "SELECT DISTINCT ai.app_id, ai.app_installed_storage, "
410 "ai.app_external_path";
411 static const char query_basic[] =
412 ", ai.app_component, ai.app_exec, "
413 "ai.app_nodisplay, ai.app_type, ai.app_onboot, "
414 "ai.app_multiple, ai.app_autorestart, ai.app_taskmanage, "
415 "ai.app_hwacceleration, ai.app_screenreader, "
416 "ai.app_mainapp, ai.app_recentimage, ai.app_launchcondition, "
417 "ai.app_indicatordisplay, ai.app_portraitimg, "
418 "ai.app_landscapeimg, ai.app_guestmodevisibility, "
419 "ai.app_permissiontype, ai.app_preload, ai.app_submode, "
420 "ai.app_submode_mainid, ai.app_launch_mode, ai.app_ui_gadget, "
421 "ai.app_support_disable, ai.app_process_pool, "
422 "ai.app_background_category, ai.app_package_type, "
423 "ai.app_root_path, ai.app_api_version, ai.app_effective_appid, "
424 "ai.app_disable, ai.app_splash_screen_display, ai.app_tep_name, "
425 "ai.app_zip_mount_file, ai.component_type, ai.package, "
426 "ai.app_package_system, ai.app_removable, "
427 "ai.app_package_installed_time, ai.app_support_mode, "
428 "ai.app_support_ambient, ai.app_setup_appid";
429 static const char query_uid_info[] =
430 ", ui.is_disabled, ui.is_splash_screen_enabled";
431 static const char query_label[] =
433 "(SELECT app_label FROM package_app_localized_info WHERE ai.app_id=app_id AND app_locale=?), "
434 "(SELECT app_label FROM package_app_localized_info WHERE ai.app_id=app_id AND app_locale='No Locale'))";
435 static const char query_icon[] =
437 "(SELECT app_icon FROM package_app_localized_info WHERE ai.app_id=app_id AND app_locale=?), "
438 "(SELECT app_icon FROM package_app_localized_info WHERE ai.app_id=app_id AND app_locale='No Locale'))";
439 static const char query_from_clause[] = " FROM package_app_info as ai";
440 static const char query_uid_info_clause[] =
441 " LEFT OUTER JOIN package_app_info_for_uid AS ui "
442 "ON (ai.app_id=ui.app_id AND ui.uid=?)";
443 int ret = PMINFO_R_ERROR;
445 char *bg_category_str = NULL;
446 char *constraint = NULL;
447 char *tmp_record = NULL;
448 char query[MAX_QUERY_LEN] = { '\0' };
449 char buf[BUFSIZE] = { '\0' };
450 application_x *info = NULL;
451 GList *bind_params = NULL;
452 sqlite3_stmt *stmt = NULL;
453 bool is_check_storage = true;
454 const uid_t global_user_uid = GLOBAL_USER;
456 snprintf(query, MAX_QUERY_LEN - 1, "%s", query_raw);
458 if (flag & PMINFO_APPINFO_GET_BASICINFO) {
459 strncat(query, query_basic, sizeof(query) - strlen(query) - 1);
460 strncat(query, query_uid_info, sizeof(query) - strlen(query) - 1);
462 if (flag & PMINFO_APPINFO_GET_LABEL) {
463 strncat(query, query_label, sizeof(query) - strlen(query) - 1);
464 bind_params = g_list_append(bind_params, strdup(locale));
466 if (flag & PMINFO_APPINFO_GET_ICON) {
467 strncat(query, query_icon, sizeof(query) - strlen(query) - 1);
468 bind_params = g_list_append(bind_params, strdup(locale));
471 snprintf(buf, MAX_QUERY_LEN - 1, "%d", uid);
472 bind_params = g_list_append(bind_params, strdup(buf));
474 is_check_storage = __check_app_storage_status(filter);
476 ret = _get_filtered_query(filter, locale, uid, &constraint, &bind_params);
477 if (ret != PMINFO_R_OK) {
478 LOGE("Failed to get WHERE clause");
481 strncat(query, query_from_clause, sizeof(query) - strlen(query) - 1);
483 strncat(query, query_uid_info_clause, sizeof(query) - strlen(query) - 1);
486 strncat(query, constraint, sizeof(query) - strlen(query) - 1);
488 ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
489 if (ret != SQLITE_OK) {
490 LOGE("prepare failed: %s", sqlite3_errmsg(db));
491 ret = PMINFO_R_ERROR;
495 if (g_list_length(bind_params) != 0) {
496 ret = __bind_params(stmt, bind_params);
497 if (ret != SQLITE_OK) {
498 LOGE("Failed to bind parameters");
503 while (sqlite3_step(stmt) == SQLITE_ROW) {
504 info = calloc(1, sizeof(application_x));
506 LOGE("out of memory");
507 ret = PMINFO_R_ERROR;
511 _save_column_str(stmt, idx++, &info->appid);
512 _save_column_str(stmt, idx++, &info->installed_storage);
513 _save_column_str(stmt, idx++ , &info->external_path);
515 if (flag & PMINFO_APPINFO_GET_BASICINFO) {
516 _save_column_str(stmt, idx++, &info->component);
517 _save_column_str(stmt, idx++, &info->exec);
518 _save_column_str(stmt, idx++, &info->nodisplay);
519 _save_column_str(stmt, idx++, &info->type);
520 _save_column_str(stmt, idx++, &info->onboot);
521 _save_column_str(stmt, idx++, &info->multiple);
522 _save_column_str(stmt, idx++, &info->autorestart);
523 _save_column_str(stmt, idx++, &info->taskmanage);
524 _save_column_str(stmt, idx++, &info->hwacceleration);
525 _save_column_str(stmt, idx++, &info->screenreader);
526 _save_column_str(stmt, idx++, &info->mainapp);
527 _save_column_str(stmt, idx++, &info->recentimage);
528 _save_column_str(stmt, idx++, &info->launchcondition);
529 _save_column_str(stmt, idx++, &info->indicatordisplay);
530 _save_column_str(stmt, idx++, &info->portraitimg);
531 _save_column_str(stmt, idx++, &info->landscapeimg);
532 _save_column_str(stmt, idx++, &info->guestmode_visibility);
533 _save_column_str(stmt, idx++, &info->permission_type);
534 _save_column_str(stmt, idx++, &info->preload);
535 _save_column_str(stmt, idx++, &info->submode);
536 _save_column_str(stmt, idx++, &info->submode_mainid);
537 _save_column_str(stmt, idx++, &info->launch_mode);
538 _save_column_str(stmt, idx++, &info->ui_gadget);
539 _save_column_str(stmt, idx++, &info->support_disable);
540 _save_column_str(stmt, idx++, &info->process_pool);
541 _save_column_str(stmt, idx++, &bg_category_str);
542 _save_column_str(stmt, idx++, &info->package_type);
543 _save_column_str(stmt, idx++, &info->root_path);
544 _save_column_str(stmt, idx++, &info->api_version);
545 _save_column_str(stmt, idx++, &info->effective_appid);
546 _save_column_str(stmt, idx++, &info->is_disabled);
547 _save_column_str(stmt, idx++, &info->splash_screen_display);
548 _save_column_str(stmt, idx++, &info->tep_name);
549 _save_column_str(stmt, idx++, &info->zip_mount_file);
550 _save_column_str(stmt, idx++, &info->component_type);
551 _save_column_str(stmt, idx++, &info->package);
552 _save_column_str(stmt, idx++, &info->package_system);
553 _save_column_str(stmt, idx++, &info->removable);
554 _save_column_str(stmt, idx++, &info->package_installed_time);
555 _save_column_str(stmt, idx++, &info->support_mode);
556 _save_column_str(stmt, idx++, &info->support_ambient);
557 _save_column_str(stmt, idx++, &info->setup_appid);
558 info->background_category = __get_background_category(
560 free(bg_category_str);
561 bg_category_str = NULL;
564 info->for_all_users =
565 strdup((db_uid != global_user_uid) ? "false" : "true");
567 if (db_uid != global_user_uid) {
571 _save_column_str(stmt, idx++, &tmp_record);
572 if (tmp_record != NULL) {
573 if (strcasecmp(info->is_disabled, "false") == 0 &&
574 strcasecmp(tmp_record, "false") == 0) {
575 free(info->is_disabled);
576 info->is_disabled = tmp_record;
582 _save_column_str(stmt, idx++, &tmp_record);
583 if (tmp_record != NULL) {
584 if (strcasecmp(info->splash_screen_display, "false") == 0 &&
585 strcasecmp(tmp_record, "false") == 0) {
586 free(info->splash_screen_display);
587 info->splash_screen_display = tmp_record;
594 if (flag & PMINFO_APPINFO_GET_LABEL) {
596 _save_column_str(stmt, idx++, &tmp_record);
597 if (_add_label_info_into_list(locale, tmp_record, &info->label)) {
598 ret = PMINFO_R_ERROR;
603 if (flag & PMINFO_APPINFO_GET_ICON) {
605 _save_column_str(stmt, idx++, &tmp_record);
606 if (_add_icon_info_into_list(locale, tmp_record, &info->icon)) {
607 ret = PMINFO_R_ERROR;
612 if (flag & PMINFO_APPINFO_GET_CATEGORY) {
613 if (_appinfo_get_category(db, info->appid,
615 ret = PMINFO_R_ERROR;
620 if (flag & PMINFO_APPINFO_GET_APP_CONTROL) {
621 if (_appinfo_get_app_control(db, info->appid,
622 &info->appcontrol)) {
623 ret = PMINFO_R_ERROR;
628 if (flag & PMINFO_APPINFO_GET_METADATA) {
629 if (_appinfo_get_metadata(db, info->appid,
631 ret = PMINFO_R_ERROR;
636 if (flag & PMINFO_APPINFO_GET_SPLASH_SCREEN) {
637 if (_appinfo_get_splashscreens(db, info->appid,
638 &info->splashscreens)) {
639 ret = PMINFO_R_ERROR;
644 if (is_check_storage &&
645 __appinfo_check_installed_storage(info) != PMINFO_R_OK) {
646 ret = PMINFO_R_ERROR;
647 pkgmgrinfo_basic_free_application(info);
652 g_hash_table_insert(applications, (gpointer)info->appid,
659 sqlite3_finalize(stmt);
664 if (ret != PMINFO_R_OK && info != NULL)
665 pkgmgrinfo_basic_free_application(info);
667 g_list_free_full(bind_params, free);
672 API int appinfo_internal_filter_get_list(
674 pkgmgrinfo_appinfo_filter_h filter,
675 uid_t uid, const char *locale, GHashTable **appinfo_list) {
679 if (db == NULL || filter == NULL || appinfo_list == NULL) {
681 return PMINFO_R_EINVAL;
684 list = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
685 __free_applications);
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_destroy(list);
700 if (!g_hash_table_size(list)) {
701 // TODO: add logs that failed to find appid
702 g_hash_table_destroy(list);
703 return PMINFO_R_ENOENT;
706 *appinfo_list = list;