add status api from tizen_2.4 08/46608/7
authorJiwoong Im <jiwoong.im@samsung.com>
Mon, 24 Aug 2015 05:36:49 +0000 (14:36 +0900)
committerJiwoong Im <jiwoong.im@samsung.com>
Tue, 25 Aug 2015 07:22:44 +0000 (16:22 +0900)
- int aul_app_get_status_bypid(int pid);
- int aul_add_status_local_cb(int (*func) (int, void *), void *data);
- int aul_remove_status_local_cb(int (*func) (int, void *), void *data);
- int aul_invoke_status_local_cb(int status);

Change-Id: If680ab9228fdebd28c5a5cbd2487a6b37b702e34
Signed-off-by: Jiwoong Im <jiwoong.im@samsung.com>
am_daemon/amd_request.c
am_daemon/amd_status.c
include/app_sock.h
include/aul.h
src/status.c
test/aul_test.c

index 64982a7..9125000 100644 (file)
@@ -535,10 +535,21 @@ static gboolean __request_handler(gpointer data)
                        break;
                case APP_STATUS_UPDATE:
                        status = (int *)pkt->data;
-                       ret = _status_update_app_info_list(cr.pid, *status, cr.uid);
+                       if (*status == STATUS_NORESTART) {
+                               appid = _status_app_get_appid_bypid(cr.pid);
+                               ai = appinfo_find(cr.uid, appid);
+                               appinfo_set_value((struct appinfo *)ai, AIT_STATUS, "norestart");
+                       } else {
+                               ret = _status_update_app_info_list(cr.pid, *status, cr.uid);
+                       }
                        //__send_result_to_client(clifd, ret);
                        close(clifd);
                        break;
+               case APP_GET_STATUS:
+                       memcpy(&pid, pkt->data, sizeof(int));
+                       ret = _status_get_app_info_status(pid, 0);
+                       __send_result_to_client(clifd, ret);
+                       break;
                case APP_RELEASED:
                        appid = malloc(MAX_PACKAGE_STR_SIZE);
                        if (appid == NULL) {
index 10a131e..e4c9a96 100644 (file)
@@ -21,6 +21,7 @@
 
 #include <stdlib.h>
 #include <stdio.h>
+#include <stdbool.h>
 #include <glib.h>
 #include <aul.h>
 #include <string.h>
@@ -159,7 +160,8 @@ int _status_get_app_info_status(int pid, uid_t uid)
        for (iter = app_status_info_list; iter != NULL; iter = g_slist_next(iter))
        {
                info_t = (app_status_info_t *)iter->data;
-               if(pid == info_t->pid && uid == info_t->uid) {
+               if (pid == info_t->pid &&
+                               (uid == 0) ? true : (uid == info_t->uid)) {
                        return info_t->status;
                }
        }
index 6f272ff..620cb90 100644 (file)
@@ -64,6 +64,7 @@ enum app_cmd {
        APP_GROUP_GET_GROUP_PIDS,
        APP_GROUP_RESUME,
        APP_GROUP_GET_LEADER_PID,
+       APP_GET_STATUS,
 
        /* for special purpose */
        AMD_RELOAD_APPINFO,
index deccf7c..e2fd249 100644 (file)
@@ -94,7 +94,9 @@ enum app_status {
        STATUS_VISIBLE,
        STATUS_BG,
        STATUS_DYING,
-       STATUS_HOME
+       STATUS_HOME,
+       STATUS_NORESTART,
+       STATUS_SERVICE
 };
 
 /** @} */
@@ -1656,6 +1658,152 @@ int aul_add_caller_cb(int pid,  void (*caller_cb) (int, void *), void *data);
 int aul_remove_caller_cb(int pid);
 int aul_invoke_caller_cb(int pid);
 
+/**
+ * @par Description:
+ *     This API gets status of specified application process id.
+ * @par Purpose:
+ *     This API's purpose is to get the application's status.
+ *
+ * @param[in]  pid     pid of application
+ * @return     0 or greater if success, nagative value if fail
+ * @retval     STATUS_LAUNCHING
+ * @retval     STATUS_CREATED
+ * @retval     STATUS_FOCUS
+ * @retval     STATUS_VISIBLE
+ * @retval     STATUS_BG
+ * @retval     STATUS_DYING
+ * @retval     STATUS_HOME
+ * @retval     STATUS_NORESTART
+ * @see
+ *     aul_status_update
+ * @pre
+ *     None
+ * @post
+ *     None
+ * @code
+ * #include <aul.h>
+ *
+ * int iterfunc(const aul_app_info *info, void *data)
+ * {
+ *     int status;
+ *     status = aul_app_get_status_bypid(info->pid);
+ *     if (status == STATUS_FOCUS) {
+ *             printf("%s has focus", info->app_id);
+ *             (int *)data = info->pid;
+ *             return -1;
+ *     }
+ *     return 0;
+ * }
+ *
+ * int find_focus_app_pid()
+ * {
+ *     int pid = 0;
+ *     aul_app_get_running_app_info(iterfunc, &pid);
+ *     return pid;
+ * }
+ * @endcode
+ * @remark
+ *     None
+ */
+int aul_app_get_status_bypid(int pid);
+
+/**
+ * @par Description
+ *     This API sets callback function that on application status changed.
+ * @par Purpose:
+ *     This API's purpose is to listen the application's status changed within
+ *     the caller process. In general, a library that required to release resource on
+ *     application's status may use this API.
+ *
+ * @param[in]  func    callback function
+ * @param[in]  data    user data
+ * @return     0 if success, negative value if fail
+ * @retval     AUL_R_OK        - success
+ * @retval     AUL_R_ERROR     - general error
+ * @see
+ *     aul_remove_status_local_cb
+ * @pre
+ *     None
+ * @post
+ *     None
+ * @code
+ * #include <aul.h>
+ *
+ * int status_changed(int status, void *data)
+ * {
+ *     if (status == STATUS_FOCUS)
+ *             printf("%d has focus\n", getpid());
+ *
+ *     if (status == STATUS_VISIBLE)
+ *             printf("%d resume\n", getpid());
+ *
+ *     if (status == STATUS_BG0
+ *             printf("%d pause\n", getpid());
+ * }
+ *
+ * void listen_app_status()
+ * {
+ *     aul_add_status_local_cb(status_changed, NULL);
+ * }
+ * @endcode
+ * @remark
+ *     None
+ *
+ */
+int aul_add_status_local_cb(int (*func) (int, void *), void *data);
+
+/**
+ * @par Description
+ *     This API unsets callback function that on application status changed.
+ * @par Purpose:
+ *     This API's purpose is to remove callback that added by
+ *     aul_add_status_local_cb.
+ *
+ * @param[in]  func    callback function
+ * @param[in]  data    user data
+ * @return     0 if success, negative value if fail
+ * @retval     AUL_R_OK        - success
+ * @retval     AUL_R_ERROR     - general error
+ *
+ * @pre
+ *     None
+ * @post
+ *     None
+ * @see
+ *     aul_add_status_local_cb
+ * @code
+ * #include <aul.h>
+ *
+ * int status_changed(int status, void *data)
+ * {
+ *     if (status == STATUS_FOCUS)
+ *             printf("%d has focus\n", getpid());
+ *
+ *     if (status == STATUS_VISIBLE)
+ *             printf("%d resume\n", getpid());
+ *
+ *     if (status == STATUS_BG0
+ *             printf("%d pause\n", getpid());
+ * }
+ *
+ * void listen_app_status()
+ * {
+ *     aul_add_status_local_cb(status_changed, NULL);
+ * }
+ *
+ * void ignore_app_status()
+ * {
+ *     aul_remove_status_local_cb(status_changed, NULL);
+ * }
+ *
+ * @endcode
+ * @remark
+ *     None
+ *
+ */
+int aul_remove_status_local_cb(int (*func) (int, void *), void *data);
+int aul_invoke_status_local_cb(int status);
+
 typedef int (*data_control_provider_handler_fn) (bundle *b, int request_id, void *data);
 int aul_set_data_control_provider_cb(data_control_provider_handler_fn handler);
 int aul_unset_data_control_provider_cb(void);
index 83678b6..f337d38 100644 (file)
 #include "aul_api.h"
 #include "launch.h"
 
+typedef struct _app_status_cb_info_t {
+       int (*handler) (int status, void *data);
+       void *data;
+       struct _app_status_cb_info_t *next;
+} app_status_cb_info_t;
+
+app_status_cb_info_t *app_status_cb = NULL;
+
+static int app_status = STATUS_LAUNCHING;
+
 SLPAPI int aul_status_update(int status)
 {
        int ret;
+       app_status_cb_info_t *cb = app_status_cb;
+
+       app_status = status;
 
        ret = __app_send_raw_with_noreply(AUL_UTIL_PID, APP_STATUS_UPDATE, (unsigned char *)&status, sizeof(status));
 
+       if (!ret) {
+               while (cb) {
+                       if (cb->handler) {
+                               if (cb->handler(app_status, cb->data) < 0)
+                                       aul_remove_status_local_cb(cb->handler, cb->data);
+                       }
+
+                       cb = cb->next;
+               }
+       }
+
+       return ret;
+}
+
+SLPAPI  int aul_app_get_status_bypid(int pid)
+{
+       int ret;
+
+       if (pid == getpid()) {
+               return app_status;
+       }
+
+       ret = __app_send_raw(AUL_UTIL_PID, APP_GET_STATUS, (unsigned char *)&pid, sizeof(pid));
+
        return ret;
 }
 
+SLPAPI int aul_add_status_local_cb(int (*func)(int status, void *data), void *data)
+{
+       app_status_cb_info_t *cb = app_status_cb;
+
+       if (func == NULL)
+               return -1;
+
+       // check known callback
+       while (cb) {
+               if (cb && cb->handler == func && cb->data == data) {
+                       // already in list
+                       return 0;
+               }
+               cb = cb->next;
+       }
+
+       cb = (app_status_cb_info_t *)malloc(sizeof(app_status_cb_info_t));
+       if (cb == NULL)
+               return -1;
+
+       cb->handler = func;
+       cb->data = data;
+
+       cb->next = app_status_cb;
+       app_status_cb = cb;
+
+       return 0;
+}
+
+SLPAPI int aul_remove_status_local_cb(int (*func)(int status, void *data), void *data)
+{
+       app_status_cb_info_t *cb = app_status_cb;
+       app_status_cb_info_t *tmp = NULL;
+
+       if (app_status_cb
+                && app_status_cb->handler == func
+                && app_status_cb->data == data) {
+               cb = app_status_cb->next;
+               free(app_status_cb);
+               app_status_cb = cb;
+               return 0;
+       }
+
+       while (cb) {
+               if (cb->next
+                        && cb->next->handler == func
+                        && cb->next->data == data) {
+                       tmp = cb->next->next;
+                       free(cb->next);
+                       cb->next = tmp;
+                       return 0;
+               }
+
+               cb = cb->next;
+       }
+
+       return -1;
+}
+
+SLPAPI int aul_invoke_status_local_cb(int status)
+{
+       app_status_cb_info_t *cb = app_status_cb;
+
+       while (cb) {
+               if (cb->handler) {
+                       if (cb->handler(status, cb->data) < 0)
+                               aul_remove_status_local_cb(cb->handler, cb->data);
+               }
+
+               cb = cb->next;
+       }
+
+       return 0;
+}
+
 SLPAPI int aul_running_list_update(char *appid, char *app_path, char *pid)
 {
        int ret;
index 38d39d3..f43eb01 100644 (file)
@@ -399,6 +399,32 @@ static int get_pkg_func()
        return 0;
 }
 
+static char *status_text[] = {
+       "STATUS_LAUNCHING",
+       "STATUS_CREATED",
+       "STATUS_FOCUS",
+       "STATUS_VISIBLE",
+       "STATUS_BG",
+       "STATUS_DYING",
+       "STATUS_HOME",
+       "STATUS_NORESTART",
+       "STATUS_SERVICE",
+};
+
+static int get_status_pid()
+{
+       int ret;
+       ret = aul_app_get_status_bypid(apn_pid);
+
+       printf("pid: %d status: %d ", apn_pid, ret);
+       if (ret >= STATUS_LAUNCHING && ret <= STATUS_NORESTART)
+               printf("(%s)", status_text[ret]);
+
+       printf("\n");
+
+       return 0;
+}
+
 static int update_running_list()
 {
        aul_running_list_update(gargv[2], gargv[3], gargv[4]);
@@ -559,6 +585,8 @@ static test_func_t test_func[] = {
                "[usage] update_list <appid> <app_path> <pid>"},
        {"reload", reload_appinfo, "reload appinfo table",
                "[usage] reload"},
+       {"get_status_pid", get_status_pid, "aul_app_get_status_bypid test",
+               "[usage] get_status_pid <pid>"},
 /*
        {"setpkg", set_pkg_func, "set package",
                "[usage] setpkg <pkgname> <apppath>"},