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, sizeof(query),
159 "SELECT pkg_name, app_path, arg, launch_time, instance_id, "
160 "instance_name, icon, uri FROM %s ORDER BY launch_time DESC;",
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);
217 tmp = db_result[RUA_COL_INSTANCE_ID];
218 if (tmp && tmp[0] != '\0')
219 rec->instance_id = tmp;
221 rec->instance_id = NULL;
223 tmp = db_result[RUA_COL_INSTANCE_NAME];
224 if (tmp && tmp[0] != '\0')
225 rec->instance_name = tmp;
227 rec->instance_name = NULL;
229 tmp = db_result[RUA_COL_ICON];
230 if (tmp && tmp[0] != '\0')
235 tmp = db_result[RUA_COL_URI];
236 if (tmp && tmp[0] != '\0')
244 int rua_is_latest_app(const char *pkg_name)
246 return rua_is_latest_app_for_uid(pkg_name, getuid());
249 int rua_is_latest_app_for_uid(const char *pkg_name, uid_t uid)
253 const unsigned char *ct;
255 char *query = "select pkg_name from rua_history order by launch_time desc limit 1;";
260 r = _rua_util_check_uid(uid);
264 r = _rua_util_open_db(&db, SQLITE_OPEN_READONLY, uid, RUA_DB_NAME);
268 r = sqlite3_prepare(db, query, sizeof(query), &stmt, NULL);
269 if (r != SQLITE_OK) {
274 r = sqlite3_step(stmt);
275 if (r == SQLITE_ROW) {
276 ct = sqlite3_column_text(stmt, 0);
277 if (ct == NULL || ct[0] == '\0') {
282 if (strncmp(pkg_name, (const char *)ct, strlen(pkg_name)) == 0) {
290 sqlite3_finalize(stmt);
307 int rua_delete_history_with_instance_id(const char *app_id,
308 const char *instance_id)
313 if (app_id == NULL) {
314 LOGE("Invalid parameter");
320 LOGE("Out of memory");
324 bundle_add_str(b, AUL_K_RUA_PKGNAME, app_id);
326 bundle_add_str(b, AUL_K_RUA_INSTANCE_ID, instance_id);
328 ret = aul_delete_rua_history_for_uid(b, getuid());
331 LOGE("Failed to delete rua history - result(%d)", ret);