Implement app-control & notification actions 41/140941/6
authorMu-Woong Lee <muwoong.lee@samsung.com>
Thu, 27 Jul 2017 07:33:19 +0000 (16:33 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Thu, 27 Jul 2017 11:09:46 +0000 (20:09 +0900)
Change-Id: I182952d9b2f857480cdadc53e09196aff34d8c93
Signed-off-by: Mu-Woong Lee <muwoong.lee@samsung.com>
12 files changed:
include/job_scheduler_internal.h
packaging/context-job-scheduler.spec
src/client-dummy/job_scheduler.cpp
src/client/job_scheduler.cpp
src/server/ActionDBusCaller.cpp [deleted file]
src/server/ActionDBusCaller.h [deleted file]
src/server/ActionState.cpp
src/server/CMakeLists.txt
src/server/IPC.cpp [new file with mode: 0644]
src/server/IPC.h [new file with mode: 0644]
src/server/JobSchedulerService.cpp
src/shared/JobAction.cpp

index ba0426ed167a026b174250992a04133fdcafd199..fc764b5a3d8b0deb4058474be44fcdecff2fa396 100644 (file)
@@ -277,11 +277,23 @@ int ctx_sched_job_set_one_time(ctx_sched_job_h job, bool one_time);
 /**
  * @brief      Sets the app-control that will be called when the job's execution criteria is satisfied.
  * @param[in]  job                     Job handle
- * @param[in]  app_control     Serialized app-control
+ * @param[in]  app_control     App-control (bundle-exported)
  * @return     @c 0 on success, otherwise a negative error value
  */
 int ctx_sched_job_set_app_control(ctx_sched_job_h job, bundle* app_control);
 
+/**
+ * @brief      Sets the notification that will be called when the job's execution criteria is satisfied.
+ * @param[in]  job                     Job handle
+ * @param[in]  title           Title text
+ * @param[in]  content         Content body text
+ * @param[in]  icon_path       Icon file path
+ * @param[in]  app_control     App-control (bundle-exported)
+ * @return     @c 0 on success, otherwise a negative error value
+ */
+int ctx_sched_job_set_notification(ctx_sched_job_h job,
+               const char* title, const char* content, const char* icon_path, bundle* app_control);
+
 /**
  * @brief      Sets the DBus method that will be called when the job's execution criteria is satisfied.
  * @param[in]  job                     Job handle
index beb8f70356d928e464384778a13fb06b943016ba..e0d0b6acf5c5c35ac9378274ff80afe58ae9cc4d 100644 (file)
@@ -18,6 +18,8 @@ BuildRequires: pkgconfig(capi-base-common)
 BuildRequires: pkgconfig(capi-system-info)
 BuildRequires: pkgconfig(capi-system-device)
 BuildRequires: pkgconfig(capi-system-runtime-info)
+BuildRequires: pkgconfig(aul)
+BuildRequires: pkgconfig(notification)
 
 BuildRequires: pkgconfig(context-common-server)
 BuildRequires: pkgconfig(context-common-client)
index 3c9fc7484c2e27203159b90f9173585876443e82..0b434517cf31026e7dd284cbb1a189229d1303b0 100644 (file)
@@ -144,6 +144,12 @@ EXPORT_API int ctx_sched_job_set_app_control(ctx_sched_job_h job, bundle* app_co
        return E_SUPPORT;
 }
 
+EXPORT_API int ctx_sched_job_set_notification(ctx_sched_job_h job,
+               const char* title, const char* content, const char* icon_path, bundle* app_control)
+{
+       return E_SUPPORT;
+}
+
 EXPORT_API int ctx_sched_job_set_dbus(ctx_sched_job_h job,
                const char* bus_name, const char* object_path, const char* interface,
                const char* method, GVariant* parameters)
index 8e6bfffc3433452c0617d57d92bf8228140d8cdb..21afc464bee595297708503e4d786756dc2cf2db 100644 (file)
@@ -384,6 +384,20 @@ EXPORT_API int ctx_sched_job_set_app_control(ctx_sched_job_h job, bundle* app_co
        return E_NONE;
 }
 
+EXPORT_API int ctx_sched_job_set_notification(ctx_sched_job_h job,
+               const char* title, const char* content, const char* icon_path, bundle* app_control)
+{
+       IF_FAIL_RETURN(job && title && content, E_PARAM);
+
+       JobAction* action = new(std::nothrow) JobNotification(
+                       title, content, icon_path ? icon_path : EMPTY_STR, app_control);
+       ASSERT_ALLOC(action);
+
+       job->jobInfo->setAction(action);
+
+       return E_NONE;
+}
+
 EXPORT_API int ctx_sched_job_set_dbus(ctx_sched_job_h job,
                const char* bus_name, const char* object_path, const char* interface,
                const char* method, GVariant* parameters)
diff --git a/src/server/ActionDBusCaller.cpp b/src/server/ActionDBusCaller.cpp
deleted file mode 100644 (file)
index 8ad0ce7..0000000
+++ /dev/null
@@ -1,67 +0,0 @@
-/*
- * Copyright (c) 2017 Samsung Electronics Co., Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "ActionDBusCaller.h"
-
-using namespace ctx;
-
-GDBusConnection* ActionDBusCaller::__connection = NULL;
-
-static void __async_ready_cb(GObject* obj, GAsyncResult* res, gpointer data)
-{
-       GError *gerr = NULL;
-       GDBusConnection* conn = static_cast<GDBusConnection*>(data);
-       GVariant* param = g_dbus_connection_call_finish(conn, res, &gerr);
-
-       if (param)
-               g_variant_unref(param);
-
-       HANDLE_GERROR(gerr);
-}
-
-ActionDBusCaller::ActionDBusCaller()
-{
-}
-
-ActionDBusCaller::~ActionDBusCaller()
-{
-}
-
-bool ActionDBusCaller::call(const std::string& busName, const std::string& objectPath,
-               const std::string& interface, const std::string& methodName, GVariant* param)
-{
-       IF_FAIL_RETURN(__connection, false);
-
-       g_dbus_connection_call(__connection, busName.c_str(),
-                       objectPath.c_str(), interface.c_str(), methodName.c_str(), param,
-                       NULL, G_DBUS_CALL_FLAGS_NONE, CTX_DBUS_TIMEOUT, NULL, __async_ready_cb, __connection);
-
-       return true;
-}
-
-bool ActionDBusCaller::call(const std::string& busName, const std::string& objectPath,
-               const std::string& interface, const std::string& methodName, GVariant* param, uid_t uid)
-{
-       IF_FAIL_RETURN(__connection, false);
-
-       //TODO
-       return false;
-}
-
-void ActionDBusCaller::setConnection(GDBusConnection* conn)
-{
-       __connection = conn;
-}
diff --git a/src/server/ActionDBusCaller.h b/src/server/ActionDBusCaller.h
deleted file mode 100644 (file)
index 2d2d29d..0000000
+++ /dev/null
@@ -1,46 +0,0 @@
-/*
- * Copyright (c) 2017 Samsung Electronics Co., Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef __CONTEXT_JOB_SCHEDULER_ACTION_DBUS_CALLER_H__
-#define __CONTEXT_JOB_SCHEDULER_ACTION_DBUS_CALLER_H__
-
-#include <string>
-#include <ContextTypes.h>
-
-namespace ctx {
-
-       class ActionDBusCaller {
-       public:
-               ActionDBusCaller();
-               ~ActionDBusCaller();
-
-               bool call(const std::string& busName, const std::string& objectPath,
-                               const std::string& interface, const std::string& methodName,
-                               GVariant* param);
-
-               bool call(const std::string& busName, const std::string& objectPath,
-                               const std::string& interface, const std::string& methodName,
-                               GVariant* param, uid_t uid);
-
-               static void setConnection(GDBusConnection* conn);
-
-       private:
-               static GDBusConnection* __connection;
-       };
-
-}
-
-#endif /* __CONTEXT_JOB_SCHEDULER_ACTION_DBUS_CALLER_H__ */
index 880dfbdfe90377f592e173f41ca4fb115855bd3f..d32ff616fa5adc51cab08afbeb48299e60a9c765 100644 (file)
  * limitations under the License.
  */
 
+#include <aul_svc.h>
+#include <notification.h>
+#include <notification_internal.h>
+#include <vconf.h>
 #include <Conf.h>
 #include <JobSchedulerTypesPrivate.h>
 #include <JobAction.h>
-#include "ActionDBusCaller.h"
+#include "IPC.h"
 #include "ActionState.h"
 #include "ClosingState.h"
 
@@ -84,32 +88,97 @@ void ActionState::jobFinished()
 
 void ActionState::__callDBusMethod()
 {
-       ActionDBusCaller caller;
        JobDBusCall* action = static_cast<JobDBusCall*>(getJobInfo()->getAction());
        bool success = false;
 
        if (getUid() == SYSTEM_UID) {
                _D("Call %s.%s", action->getInterface().c_str(), action->getMethodName().c_str());
-               success = caller.call(action->getBusName(), action->getObjectPath(),
+               success = IPC::dbusCall(action->getBusName(), action->getObjectPath(),
                                action->getInterface(), action->getMethodName(), action->getParameters());
        } else {
                _D("Call %s.%s of %u", action->getInterface().c_str(), action->getMethodName().c_str(), static_cast<unsigned>(getUid()));
-               success = caller.call(action->getBusName(), action->getObjectPath(),
-                               action->getInterface(), action->getMethodName(), action->getParameters(),
-                               getUid());
+               _W("Not supported yet");
+
+               //TODO: Delegate to the agent daemon
        }
 
        IF_FAIL_VOID_TAG(success, _E, "DBus call failed");
 }
 
+static void __aul_svc_result_cb(bundle* b, int requestCode, aul_svc_result_val result, void* data)
+{
+       _D("App-control sent: %d", result);
+}
+
 void ActionState::__sendAppControl()
 {
-       //TODO
+       IF_FAIL_VOID_TAG(getUid() != SYSTEM_UID, _W, "Not a user session");
+
+       _D("Send app-control to %u", static_cast<unsigned>(getUid()));
+
+       bundle* bn = static_cast<JobAppControl*>(getJobInfo()->getAction())->getAppControl();
+       IF_FAIL_VOID_TAG(bn, _E, "App-control not found");
+
+       int pid = aul_svc_run_service_for_uid(bn, 0, __aul_svc_result_cb, NULL, getUid());
+       IF_FAIL_VOID_TAG(pid >= 0, _E, "Sending app-control failed");
+}
+
+static notification_h __create_notification(const std::string& title,
+               const std::string& content, const std::string& iconPath, bundle* appCtrlBundle)
+{
+       notification_h notification = notification_create(NOTIFICATION_TYPE_NOTI);
+
+       if (!title.empty()) {
+               notification_set_text(notification,
+                               NOTIFICATION_TEXT_TYPE_TITLE, title.c_str(), NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
+       }
+
+       if (!content.empty()) {
+               notification_set_text(notification,
+                               NOTIFICATION_TEXT_TYPE_CONTENT, content.c_str(), NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
+       }
+
+       if (!iconPath.empty()) {
+               notification_set_image(notification, NOTIFICATION_IMAGE_TYPE_ICON, iconPath.c_str());
+       }
+
+       if (appCtrlBundle) {
+               notification_set_execute_option(notification,
+                               NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, NULL, NULL, appCtrlBundle);
+       } else {
+               notification_set_property(notification, NOTIFICATION_PROP_DISABLE_APP_LAUNCH);
+       }
+
+       int sound = 0;
+       vconf_get_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, &sound);
+       if (sound) {
+               notification_set_sound(notification, NOTIFICATION_SOUND_TYPE_DEFAULT, NULL);
+       }
+
+       int vibration = 0;
+       vconf_get_bool(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, &vibration);
+       if (vibration) {
+               notification_set_vibration(notification,
+                               NOTIFICATION_VIBRATION_TYPE_DEFAULT, NULL);
+       }
+
+       return notification;
 }
 
 void ActionState::__postNotification()
 {
-       //TODO
+       IF_FAIL_VOID_TAG(getUid() != SYSTEM_UID, _W, "Not a user session");
+
+       JobNotification* action = static_cast<JobNotification*>(getJobInfo()->getAction());
+
+       notification_h notification = __create_notification(action->getTitle(),
+                       action->getContent(), action->getIconPath(), action->getAppControl());
+       IF_FAIL_VOID_TAG(notification, _E, "Notification construction failed");
+
+       int error = notification_post_for_uid(notification, getUid());
+       notification_free(notification);
+
+       IF_FAIL_VOID_TAG(error == NOTIFICATION_ERROR_NONE, _E, "Posting notification failed");
 }
 
 void ActionState::__stop()
index 3fd32d222c9072b2f975c0c9d84969e3ecb361e8..ef001a1b2230137cb505692206f3e6913b17fb48 100644 (file)
@@ -2,6 +2,7 @@ SET(target "${PROJECT_NAME}-server-genuine")
 
 SET(DEPS "${DEPS} jsoncpp sqlite3 vconf context-common-server")
 SET(DEPS "${DEPS} capi-system-info capi-system-device capi-system-runtime-info")
+SET(DEPS "${DEPS} aul notification")
 
 FILE(GLOB_RECURSE SRCS *.cpp ../shared/*.cpp)
 MESSAGE("Sources: ${SRCS}")
diff --git a/src/server/IPC.cpp b/src/server/IPC.cpp
new file mode 100644 (file)
index 0000000..d578212
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "IPC.h"
+
+using namespace ctx;
+
+GDBusConnection* IPC::__dbusConnection = NULL;
+
+static void __async_ready_cb(GObject* obj, GAsyncResult* res, gpointer data)
+{
+       GError *gerr = NULL;
+       GDBusConnection* conn = static_cast<GDBusConnection*>(data);
+       GVariant* param = g_dbus_connection_call_finish(conn, res, &gerr);
+
+       if (param)
+               g_variant_unref(param);
+
+       HANDLE_GERROR(gerr);
+}
+
+IPC::IPC()
+{
+}
+
+bool IPC::dbusCall(const std::string& busName, const std::string& objectPath,
+               const std::string& interface, const std::string& methodName, GVariant* param)
+{
+       IF_FAIL_RETURN(__dbusConnection, false);
+
+       g_dbus_connection_call(__dbusConnection, busName.c_str(),
+                       objectPath.c_str(), interface.c_str(), methodName.c_str(), param,
+                       NULL, G_DBUS_CALL_FLAGS_NONE, CTX_DBUS_TIMEOUT, NULL, __async_ready_cb, __dbusConnection);
+
+       return true;
+}
+
+void IPC::setDBus(GDBusConnection* conn)
+{
+       __dbusConnection = conn;
+}
diff --git a/src/server/IPC.h b/src/server/IPC.h
new file mode 100644 (file)
index 0000000..f339b32
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __CONTEXT_JOB_SCHEDULER_IPC_H__
+#define __CONTEXT_JOB_SCHEDULER_IPC_H__
+
+#include <string>
+#include <ContextTypes.h>
+
+namespace ctx {
+
+       class IPC {
+       public:
+               static bool dbusCall(const std::string& busName, const std::string& objectPath,
+                               const std::string& interface, const std::string& methodName,
+                               GVariant* param);
+
+               static void setDBus(GDBusConnection* conn);
+
+               //TODO: Socket
+
+       private:
+               IPC();
+
+               static GDBusConnection* __dbusConnection;
+       };
+
+}
+
+#endif /* __CONTEXT_JOB_SCHEDULER_IPC_H__ */
index d720d1692dbb1709a428da5ed5e2b98655fa508d..ddcd183470515a6c704dccb34e4fb9ee3e1e865d 100644 (file)
@@ -22,7 +22,7 @@
 #include "MethodCallHandler.h"
 #include "ContextManager.h"
 #include "SchedTimer.h"
-#include "ActionDBusCaller.h"
+#include "IPC.h"
 
 #define SYSTEM_UID     0
 
@@ -63,7 +63,7 @@ bool JobSchedulerService::prepare()
 {
        ContextManager::init();
        SchedTimer::init();
-       ActionDBusCaller::setConnection(__serviceRunner->getConnection());
+       IPC::setDBus(__serviceRunner->getConnection());
        __jobManagers.emplace(SYSTEM_UID, new JobManager(SYSTEM_UID));
        return true;
 }
index e6c228ca7b279408d7146a28115062970bfad7ac..902a553601c7148d3e501868ed2e68dd5236fb3c 100644 (file)
@@ -36,6 +36,9 @@ using namespace ctx;
 
 static std::string __bundleToString(bundle* bndl)
 {
+       if (!bndl)
+               return EMPTY_STR;
+
        bundle_raw* encoded = NULL;
        int length = 0;
 
@@ -51,6 +54,9 @@ static std::string __bundleToString(bundle* bndl)
 
 static bundle* __stringToBundle(const std::string& str)
 {
+       if (str.empty())
+               return NULL;
+
        return bundle_decode(reinterpret_cast<const unsigned char*>(str.c_str()), strlen(str.c_str()));
 }