2 * Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
23 /* For multi-user support */
24 #include <tzplatform_config.h>
26 #include "rua_internal.h"
28 #include "db-schema.h"
30 int rua_add_history_for_uid(char *pkg_name, char *app_path, char *arg, uid_t uid)
33 char time_str[32] = {0,};
36 if (pkg_name == NULL || app_path == NULL) {
37 LOGE("invalid param");
41 r = _rua_util_check_uid(uid);
47 LOGE("bundle_create fail out of memory.");
50 snprintf(time_str, sizeof(time_str), "%d", (int)time(NULL));
51 bundle_add_str(b, AUL_K_RUA_PKGNAME, pkg_name);
52 bundle_add_str(b, AUL_K_RUA_APPPATH, app_path);
53 bundle_add_str(b, AUL_K_RUA_ARG, arg);
54 bundle_add_str(b, AUL_K_RUA_TIME, time_str);
56 r = aul_add_rua_history_for_uid(b, uid);
57 LOGI("rua_add_history_for_uid result : %d ", r);
62 int rua_delete_history_with_pkgname(char *pkg_name)
64 return rua_delete_history_with_pkgname_for_uid(pkg_name, getuid());
67 int rua_delete_history_with_pkgname_for_uid(char *pkg_name, uid_t uid)
72 r = _rua_util_check_uid(uid);
78 LOGE("bundle_create fail out of memory.");
81 bundle_add_str(b, AUL_K_RUA_PKGNAME, pkg_name);
82 r = aul_delete_rua_history_for_uid(b, uid);
83 LOGI("rua_delete_history_with_pkgname result : %d ", r);
88 int rua_delete_history_with_apppath(char *app_path)
90 return rua_delete_history_with_apppath_for_uid(app_path, getuid());
93 int rua_delete_history_with_apppath_for_uid(char *app_path, uid_t uid)
98 r = _rua_util_check_uid(uid);
104 LOGE("bundle_create fail out of memory.");
107 bundle_add_str(b, AUL_K_RUA_APPPATH, app_path);
108 r = aul_delete_rua_history_for_uid(b, uid);
109 LOGI("rua_delete_history_with_apppath result : %d ", r);
114 int rua_clear_history(void)
116 return rua_clear_history_for_uid(getuid());
119 int rua_clear_history_for_uid(uid_t uid)
123 r = _rua_util_check_uid(uid);
127 r = aul_delete_rua_history_for_uid(NULL, uid);
128 LOGI("rua_clear_history result : %d ", r);
132 int rua_history_load_db(char ***table, int *nrows, int *ncols)
134 return rua_history_load_db_for_uid(table, nrows, ncols, getuid());
137 int rua_history_load_db_for_uid(char ***table, int *nrows, int *ncols, uid_t uid)
140 char query[QUERY_MAXLEN];
142 char **db_result = NULL;
152 r = _rua_util_check_uid(uid);
156 r = _rua_util_open_db(&db, SQLITE_OPEN_READONLY, uid, RUA_DB_NAME);
160 snprintf(query, QUERY_MAXLEN,
161 "select pkg_name, app_path, arg, launch_time from %s order by launch_time desc;", RUA_HISTORY);
163 r = sqlite3_get_table(db, query, &db_result, nrows, ncols, &db_err);
168 sqlite3_free_table(db_result);
175 int rua_history_unload_db(char ***table)
178 sqlite3_free_table(*table);
185 int rua_history_get_rec(struct rua_rec *rec, char **table, int nrows, int ncols,
188 char **db_result = NULL;
198 db_result = table + ((row + 1) * ncols);
200 tmp = db_result[RUA_COL_PKGNAME];
204 LOGI("get rec pkg_name %s", rec->pkg_name);
205 tmp = db_result[RUA_COL_APPPATH];
209 tmp = db_result[RUA_COL_ARG];
213 tmp = db_result[RUA_COL_LAUNCHTIME];
215 rec->launch_time = atoi(tmp);
220 int rua_is_latest_app(const char *pkg_name)
222 return rua_is_latest_app_for_uid(pkg_name, getuid());
225 int rua_is_latest_app_for_uid(const char *pkg_name, uid_t uid)
229 const unsigned char *ct;
231 char *query = "select pkg_name from rua_history order by launch_time desc limit 1;";
236 r = _rua_util_check_uid(uid);
240 r = _rua_util_open_db(&db, SQLITE_OPEN_READONLY, uid, RUA_DB_NAME);
244 r = sqlite3_prepare(db, query, sizeof(query), &stmt, NULL);
245 if (r != SQLITE_OK) {
250 r = sqlite3_step(stmt);
251 if (r == SQLITE_ROW) {
252 ct = sqlite3_column_text(stmt, 0);
253 if (ct == NULL || ct[0] == '\0') {
258 if (strncmp(pkg_name, (const char *)ct, strlen(pkg_name)) == 0) {
266 sqlite3_finalize(stmt);