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.
28 #include "db-schema.h"
30 #include "rua_private.h"
32 API int rua_add_history_for_uid(char *pkg_name, char *app_path, char *arg, uid_t uid)
35 char time_str[32] = {0,};
38 if (pkg_name == NULL || app_path == NULL) {
39 LOGE("invalid param");
43 r = _rua_util_check_uid(uid);
49 LOGE("bundle_create fail out of memory.");
52 snprintf(time_str, sizeof(time_str), "%d", (int)time(NULL));
53 bundle_add_str(b, AUL_K_RUA_PKGNAME, pkg_name);
54 bundle_add_str(b, AUL_K_RUA_APPPATH, app_path);
55 bundle_add_str(b, AUL_K_RUA_ARG, arg);
56 bundle_add_str(b, AUL_K_RUA_TIME, time_str);
58 r = aul_add_rua_history_for_uid(b, uid);
59 LOGI("rua_add_history_for_uid result : %d ", r);
64 API int rua_delete_history_with_pkgname_for_uid(char *pkg_name, uid_t uid)
69 r = _rua_util_check_uid(uid);
75 LOGE("bundle_create fail out of memory.");
78 bundle_add_str(b, AUL_K_RUA_PKGNAME, pkg_name);
79 r = aul_delete_rua_history_for_uid(b, uid);
80 LOGI("rua_delete_history_with_pkgname result : %d ", r);
85 API int rua_delete_history_with_pkgname(char *pkg_name)
87 return rua_delete_history_with_pkgname_for_uid(pkg_name, getuid());
90 API int rua_delete_history_with_apppath_for_uid(char *app_path, uid_t uid)
95 r = _rua_util_check_uid(uid);
101 LOGE("bundle_create fail out of memory.");
104 bundle_add_str(b, AUL_K_RUA_APPPATH, app_path);
105 r = aul_delete_rua_history_for_uid(b, uid);
106 LOGI("rua_delete_history_with_apppath result : %d ", r);
111 API int rua_delete_history_with_apppath(char *app_path)
113 return rua_delete_history_with_apppath_for_uid(app_path, getuid());
116 API int rua_clear_history_for_uid(uid_t uid)
120 r = _rua_util_check_uid(uid);
124 r = aul_delete_rua_history_for_uid(NULL, uid);
125 LOGI("rua_clear_history result : %d ", r);
129 API int rua_clear_history(void)
131 return rua_clear_history_for_uid(getuid());
134 API int rua_history_load_db_for_uid(char ***table, int *nrows, int *ncols, uid_t uid)
137 char query[QUERY_MAXLEN];
139 char **db_result = NULL;
149 r = _rua_util_check_uid(uid);
153 r = _rua_util_open_db(&db, SQLITE_OPEN_READONLY, uid, RUA_DB_NAME);
157 snprintf(query, sizeof(query),
158 "SELECT pkg_name, app_path, arg, launch_time, instance_id, "
159 "instance_name, icon, uri FROM %s ORDER BY launch_time DESC;",
162 r = sqlite3_get_table(db, query, &db_result, nrows, ncols, &db_err);
167 sqlite3_free_table(db_result);
174 API int rua_history_load_db(char ***table, int *nrows, int *ncols)
176 return rua_history_load_db_for_uid(table, nrows, ncols, getuid());
179 API int rua_register_update_cb_for_uid(rua_history_update_cb callback, void *user_data, int *callback_id, uid_t uid)
183 if (callback == NULL)
186 r = _rua_util_check_uid(uid);
190 r = rua_dbus_signal_subscribe(callback, user_data, callback_id);
195 API int rua_register_update_cb(rua_history_update_cb callback, void *user_data, int *callback_id)
197 return rua_register_update_cb_for_uid(callback, user_data, callback_id, getuid());
200 API int rua_unregister_update_cb_for_uid(int callback_id, uid_t uid)
204 r = _rua_util_check_uid(uid);
208 r = rua_dbus_signal_unsubscribe(callback_id);
213 API int rua_unregister_update_cb(int callback_id)
215 return rua_unregister_update_cb_for_uid(callback_id, getuid());
218 API int rua_history_get_rec(struct rua_rec *rec, char **table, int nrows, int ncols,
221 char **db_result = NULL;
231 db_result = table + ((row + 1) * ncols);
233 tmp = db_result[RUA_COL_PKGNAME];
237 LOGI("get rec pkg_name %s", rec->pkg_name);
238 tmp = db_result[RUA_COL_APPPATH];
242 tmp = db_result[RUA_COL_ARG];
246 tmp = db_result[RUA_COL_LAUNCHTIME];
248 rec->launch_time = atoi(tmp);
250 tmp = db_result[RUA_COL_INSTANCE_ID];
251 if (tmp && tmp[0] != '\0')
252 rec->instance_id = tmp;
254 rec->instance_id = NULL;
256 tmp = db_result[RUA_COL_INSTANCE_NAME];
257 if (tmp && tmp[0] != '\0')
258 rec->instance_name = tmp;
260 rec->instance_name = NULL;
262 tmp = db_result[RUA_COL_ICON];
263 if (tmp && tmp[0] != '\0')
268 tmp = db_result[RUA_COL_URI];
269 if (tmp && tmp[0] != '\0')
277 API int rua_history_unload_db(char ***table)
280 sqlite3_free_table(*table);
287 API int rua_is_latest_app_for_uid(const char *pkg_name, uid_t uid)
291 const unsigned char *ct;
293 char *query = "select pkg_name from rua_history order by launch_time desc limit 1;";
298 r = _rua_util_check_uid(uid);
302 r = _rua_util_open_db(&db, SQLITE_OPEN_READONLY, uid, RUA_DB_NAME);
306 r = sqlite3_prepare(db, query, sizeof(query), &stmt, NULL);
307 if (r != SQLITE_OK) {
312 r = sqlite3_step(stmt);
313 if (r == SQLITE_ROW) {
314 ct = sqlite3_column_text(stmt, 0);
315 if (ct == NULL || ct[0] == '\0') {
320 if (strncmp(pkg_name, (const char *)ct, strlen(pkg_name)) == 0) {
328 sqlite3_finalize(stmt);
335 API int rua_is_latest_app(const char *pkg_name)
337 return rua_is_latest_app_for_uid(pkg_name, getuid());
340 API int rua_init(void)
345 API int rua_fini(void)
350 API int rua_delete_history_with_instance_id(const char *app_id,
351 const char *instance_id)
356 if (app_id == NULL) {
357 LOGE("Invalid parameter");
363 LOGE("Out of memory");
367 bundle_add_str(b, AUL_K_RUA_PKGNAME, app_id);
369 bundle_add_str(b, AUL_K_RUA_INSTANCE_ID, instance_id);
371 ret = aul_delete_rua_history_for_uid(b, getuid());
374 LOGE("Failed to delete rua history - result(%d)", ret);