+
+namespace lcm
+{
+
+ActivityManager::ActivityManager (void)
+ : states_s { "NOTEXISTS",
+ "CREATED",
+ "DESTROYED",
+ "STARTED",
+ "STOPPED",
+ "FOREGROUND",
+ "BACKGROUND" }
+{
+ ;
+}
+
+void ActivityManager::api_register_activity_observer (afb_req_t req)
+{
+ std::string observer = afb_req_get_application_id(req);
+
+ json_object *j_data = afb_req_json(req);
+ json_object *j_target;
+ if (!json_object_object_get_ex(j_data, LCM_TARGET, &j_target)) {
+ afb_req_fail(req, "failed", "Need char const* argument target");
+ return;
+ }
+ std::string target(json_object_get_string(j_target));
+
+ for (auto itr = this->map_observers.begin(); itr != this->map_observers.end(); ++itr) {
+ observers mp = itr->second;
+ if (mp.count(observer)) {
+ // Already exist
+ HMI_DEBUG("observer(%s) is already exist", observer.c_str());
+ if (this->map_observers[target].count(observer)) {
+ // Aready registered
+ HMI_DEBUG("observer(%s->%s) is already registered", observer.c_str(), target.c_str());
+ } else {
+ HMI_DEBUG("observer(%s->%s) is registered", observer.c_str(), target.c_str());
+
+ (this->map_observers[target])[observer] = mp[observer];
+ }
+ return;
+ }
+ }
+
+ // New observer
+ afb_event_t event = afb_daemon_make_event(LCM_EVENT_STATUS_CHANGED);
+ //afb_event_t event = afb_daemon_make_event(LCM_EVENT_STATUS_CHANGED);
+ (this->map_observers[target])[observer] = {
+ event,
+ std::bitset<NUM_STATUS>(ACTIVITY_FILTER_ALL_SET)
+ };
+
+ if (afb_req_subscribe(req, event) != 0) {
+ HMI_ERROR("cannot subscribe event");
+ }
+}
+
+void ActivityManager::api_unregister_activity_observer (afb_req_t req)
+{
+ std::string observer = afb_req_get_application_id(req);
+
+ json_object *j_data = afb_req_json(req);
+ json_object *j_target;
+
+ if (!json_object_object_get_ex(j_data, LCM_TARGET, &j_target)) {
+ afb_req_fail(req, "failed", "Need char const* argument target");
+ return;
+ }
+ std::string target(json_object_get_string(j_target));
+
+ HMI_DEBUG("observer(%s->%s) is unregistered.", observer.c_str(), target.c_str());
+}
+
+wm::result<json_object *> ActivityManager::api_get_activity_status (const char *appid)
+{
+ std::string id = appid;
+ int st = NOTEXISTS;
+
+ if (this->states.count(id)) {
+ st = this->states[id];
+
+ if (st < 0 || st >= NUM_STATUS) {
+ HMI_ERROR("Illegal lifecycle state (%d) of [%s]", st, appid);
+ return wm::Err<json_object *>("Illegal lifecycle state");
+ }
+ }
+
+ const char * state = this->states_s[st];
+
+ json_object *object = json_object_new_object();
+ json_object_object_add(object, LCM_API, json_object_new_string(LCM_API_GET_ACTIVITY_STATUS));
+ json_object_object_add(object, LCM_TARGET, json_object_new_string(appid));
+ json_object_object_add(object, LCM_STATE, json_object_new_string(state));
+
+ return wm::Ok<json_object *>(object);
+}
+
+void ActivityManager::emit_activity_status_changed (const char* appid, const char* state)
+{
+ // POI: AGL LifeCycle Management
+ // E.g. statusChanged(CREATED->STARTED)
+ std::string id = appid;
+
+ if (this->map_observers.count(id)) {
+ observers mp = this->map_observers[id];
+ for (auto itr = mp.begin(); itr != mp.end(); ++itr) {
+ afb_event_t event = itr->second.event;
+ if (afb_event_is_valid(event)) {
+ HMI_DEBUG("emit_activity_status_changed(%s, %s)", appid, state);
+ json_object *data = json_object_new_object();
+ json_object_object_add(data, LCM_STATE, json_object_new_string(state));
+ json_object_object_add(data, LCM_TARGET, json_object_new_string(appid));
+
+ afb_event_push(event, json_object_get(data));
+ json_object_put(data);
+ } else {
+ HMI_ERROR("afb_event is not valid");
+ }
+ }
+ }
+}
+
+} // namespace lcm