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.
24 #include "rua_internal.h"
26 #include "db-schema.h"
28 int rua_add_history_for_uid(char *pkg_name, char *app_path, char *arg, uid_t uid)
31 char time_str[32] = {0,};
34 if (pkg_name == NULL || app_path == NULL) {
35 LOGE("invalid param");
39 r = _rua_util_check_uid(uid);
45 LOGE("bundle_create fail out of memory.");
48 snprintf(time_str, sizeof(time_str), "%d", (int)time(NULL));
49 bundle_add_str(b, AUL_K_RUA_PKGNAME, pkg_name);
50 bundle_add_str(b, AUL_K_RUA_APPPATH, app_path);
51 bundle_add_str(b, AUL_K_RUA_ARG, arg);
52 bundle_add_str(b, AUL_K_RUA_TIME, time_str);
54 r = aul_add_rua_history_for_uid(b, uid);
55 LOGI("rua_add_history_for_uid result : %d ", r);
60 int rua_delete_history_with_pkgname(char *pkg_name)
62 return rua_delete_history_with_pkgname_for_uid(pkg_name, getuid());
65 int rua_delete_history_with_pkgname_for_uid(char *pkg_name, uid_t uid)
70 r = _rua_util_check_uid(uid);
76 LOGE("bundle_create fail out of memory.");
79 bundle_add_str(b, AUL_K_RUA_PKGNAME, pkg_name);
80 r = aul_delete_rua_history_for_uid(b, uid);
81 LOGI("rua_delete_history_with_pkgname result : %d ", r);
86 int rua_delete_history_with_apppath(char *app_path)
88 return rua_delete_history_with_apppath_for_uid(app_path, getuid());
91 int rua_delete_history_with_apppath_for_uid(char *app_path, uid_t uid)
96 r = _rua_util_check_uid(uid);
102 LOGE("bundle_create fail out of memory.");
105 bundle_add_str(b, AUL_K_RUA_APPPATH, app_path);
106 r = aul_delete_rua_history_for_uid(b, uid);
107 LOGI("rua_delete_history_with_apppath result : %d ", r);
112 int rua_clear_history(void)
114 return rua_clear_history_for_uid(getuid());
117 int rua_clear_history_for_uid(uid_t uid)
121 r = _rua_util_check_uid(uid);
125 r = aul_delete_rua_history_for_uid(NULL, uid);
126 LOGI("rua_clear_history result : %d ", r);
130 int rua_history_load_db(char ***table, int *nrows, int *ncols)
132 return rua_history_load_db_for_uid(table, nrows, ncols, getuid());
135 int rua_history_load_db_for_uid(char ***table, int *nrows, int *ncols, uid_t uid)
138 char query[QUERY_MAXLEN];
140 char **db_result = NULL;
150 r = _rua_util_check_uid(uid);
154 r = _rua_util_open_db(&db, SQLITE_OPEN_READONLY, uid, RUA_DB_NAME);
158 snprintf(query, QUERY_MAXLEN,
159 "select pkg_name, app_path, arg, launch_time from %s order by launch_time desc;", RUA_HISTORY);
161 r = sqlite3_get_table(db, query, &db_result, nrows, ncols, &db_err);
166 sqlite3_free_table(db_result);
173 int rua_history_unload_db(char ***table)
176 sqlite3_free_table(*table);
183 int rua_history_get_rec(struct rua_rec *rec, char **table, int nrows, int ncols,
186 char **db_result = NULL;
196 db_result = table + ((row + 1) * ncols);
198 tmp = db_result[RUA_COL_PKGNAME];
202 LOGI("get rec pkg_name %s", rec->pkg_name);
203 tmp = db_result[RUA_COL_APPPATH];
207 tmp = db_result[RUA_COL_ARG];
211 tmp = db_result[RUA_COL_LAUNCHTIME];
213 rec->launch_time = atoi(tmp);
218 int rua_is_latest_app(const char *pkg_name)
220 return rua_is_latest_app_for_uid(pkg_name, getuid());
223 int rua_is_latest_app_for_uid(const char *pkg_name, uid_t uid)
227 const unsigned char *ct;
229 char *query = "select pkg_name from rua_history order by launch_time desc limit 1;";
234 r = _rua_util_check_uid(uid);
238 r = _rua_util_open_db(&db, SQLITE_OPEN_READONLY, uid, RUA_DB_NAME);
242 r = sqlite3_prepare(db, query, sizeof(query), &stmt, NULL);
243 if (r != SQLITE_OK) {
248 r = sqlite3_step(stmt);
249 if (r == SQLITE_ROW) {
250 ct = sqlite3_column_text(stmt, 0);
251 if (ct == NULL || ct[0] == '\0') {
256 if (strncmp(pkg_name, (const char *)ct, strlen(pkg_name)) == 0) {
264 sqlite3_finalize(stmt);