POI: AGL LifeCycle Management
[apps/agl-service-windowmanager.git] / src / window_manager.cpp
index cd7d2e5..bbfd08c 100644 (file)
@@ -187,6 +187,11 @@ result<int> WindowManager::api_request_surface(char const *appid, char const *dr
         auto id = int(this->id_alloc.generate_id(role));
         this->tmp_surface2app[id] = {str_id, lid};
 
+        // POI: AGL LifeCycle Management
+        this->amgr.states[str_id] = lcm::CREATED;
+        this->amgr.emit_activity_status_changed(str_id.c_str(),
+                                                this->amgr.states_s[lcm::CREATED]);
+
         return Ok<int>(id);
     }
 
@@ -246,6 +251,11 @@ char const *WindowManager::api_request_surface(char const *appid, char const *dr
     auto client = g_app_list.lookUpClient(str_id);
     client->addSurface(sid);
 
+    // POI: AGL LifeCycle Management
+    this->amgr.states[str_id] = lcm::CREATED;
+    this->amgr.emit_activity_status_changed(str_id.c_str(),
+                                            this->amgr.states_s[lcm::CREATED]);
+
     return nullptr;
 }
 
@@ -285,7 +295,7 @@ void WindowManager::api_activate_window(char const *appid, char const *drawing_n
     }
 
      // Do allocate tasks
-    
+
     ret = this->checkPolicy(req_num);
 
     if (ret != WMError::SUCCESS)
@@ -294,6 +304,13 @@ void WindowManager::api_activate_window(char const *appid, char const *drawing_n
         HMI_SEQ_ERROR(req_num, errorDescription(ret));
         g_app_list.removeRequest(req_num);
         this->processNextRequest();
+    } else {
+      // POI: AGL LifeCycle Management
+      if (this->amgr.states[id] == lcm::CREATED) {
+        this->amgr.states[id] = lcm::STARTED;
+        this->amgr.emit_activity_status_changed(id.c_str(),
+                                                this->amgr.states_s[lcm::STARTED]);
+      }
     }
 }
 
@@ -334,6 +351,14 @@ void WindowManager::api_deactivate_window(char const *appid, char const *drawing
         HMI_SEQ_ERROR(req_num, errorDescription(ret));
         g_app_list.removeRequest(req_num);
         this->processNextRequest();
+    } else {
+      // POI: AGL LifeCycle Management
+      int state = this->amgr.states[id];
+      if (state == lcm::STARTED || state == lcm::BACKGROUND) {
+        this->amgr.states[id] = lcm::STOPPED;
+        this->amgr.emit_activity_status_changed(id.c_str(),
+                                                this->amgr.states_s[lcm::STOPPED]);
+      }
     }
 }
 
@@ -492,6 +517,12 @@ void WindowManager::removeClient(const string &appid)
     auto client = g_app_list.lookUpClient(appid);
     this->lc->appTerminated(client);
     g_app_list.removeClient(appid);
+    // POI: AGL LifeCycle Management
+    if (this->amgr.states[appid] != lcm::DESTROYED) {
+      this->amgr.states[appid] = lcm::DESTROYED;
+      this->amgr.emit_activity_status_changed(appid.c_str(),
+                                              this->amgr.states_s[lcm::DESTROYED]);
+    }
 }
 
 void WindowManager::exceptionProcessForTransition()
@@ -853,9 +884,22 @@ void WindowManager::emitScreenUpdated(unsigned req_num)
 
     for(const auto& action: actions)
     {
+        std::string appid = action.client->appID();
+
         if(action.visible != TaskVisible::INVISIBLE)
         {
-            json_object_array_add(jarray, json_object_new_string(action.client->appID().c_str()));
+            json_object_array_add(jarray, json_object_new_string(appid.c_str()));
+            // POI: AGL LifeCycle Management
+            if (this->amgr.states[appid] != lcm::FOREGROUND) {
+              this->amgr.states[appid] = lcm::FOREGROUND;
+              this->amgr.emit_activity_status_changed(appid.c_str(), this->amgr.states_s[lcm::FOREGROUND]);
+            }
+        } else {
+            // POI: AGL LifeCycle Management
+            if (this->amgr.states[appid] != lcm::BACKGROUND) {
+                this->amgr.states[appid] = lcm::BACKGROUND;
+                this->amgr.emit_activity_status_changed(appid.c_str(), this->amgr.states_s[lcm::BACKGROUND]);
+            }
         }
     }
     json_object_object_add(j, kKeyIds, jarray);
@@ -928,3 +972,164 @@ void WindowManager::processNextRequest()
 }
 
 } // namespace wm
+
+namespace lcm
+{
+
+ActivityManager::ActivityManager (void)
+    : states_s { "NOTEXISTS",
+                 "CREATED",
+                 "DESTROYED",
+                 "STARTED",
+                 "STOPPED",
+                 "FOREGROUND",
+                 "BACKGROUND" }
+{
+  ;
+}
+
+void ActivityManager::api_register_activity_observer (afb_req_t req, void* &lcm_ctx)
+{
+  //std::string observer = afb_req_get_application_id(req);
+  observer* obs = static_cast<observer *>(lcm_ctx);
+
+  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));
+
+  if (obs == nullptr) {
+    // 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);
+    obs = new observer({
+        event,
+        std::bitset<NUM_STATUS>(ACTIVITY_FILTER_ALL_SET)
+      });
+
+    if (afb_req_subscribe(req, event) != 0) {
+      afb_req_fail(req, "failed", "cannot subscribe event");
+      return;
+    }
+    HMI_DEBUG("observer(%p, target=\"%s\") is created.[NEW]", obs, target.c_str());
+
+    lcm_ctx = static_cast<observer *>(obs);
+  }
+
+  if (this->map_observers.count(target)) {
+    // already registered
+    HMI_DEBUG("observer(%p, target=\"%s\") is already exist", obs, target.c_str());
+    std::vector<observer*> obs_v = this->map_observers[target];
+    auto result = std::find(obs_v.begin(), obs_v.end(), obs);
+    if (result != obs_v.end()) {
+      HMI_DEBUG("observer(%p, target=\"%s\") is already registered", obs, target.c_str());
+      return;
+    }
+  }
+  HMI_DEBUG("observer(%p, target=\"%s\") is registered", obs, target.c_str());
+  (this->map_observers[target]).push_back(obs);
+}
+
+void ActivityManager::remove_observer (observer *obs, std::vector<observer*>& obs_v)
+{
+  auto obs_itr = obs_v.begin();
+  while (obs_itr != obs_v.end()) {
+    if (*obs_itr == obs) {
+      obs_itr = obs_v.erase(obs_itr);
+    } else {
+      obs_itr++;
+    }
+  }
+}
+
+void ActivityManager::api_unregister_activity_observer (afb_req_t req, void* &lcm_ctx)
+{
+  observer *obs = static_cast<observer *>(lcm_ctx);
+
+  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(%p->\"%s\") is unregistered.", obs, target.c_str());
+
+  if (this->map_observers.count(target) != 0) {
+    remove_observer(obs, this->map_observers[target]);
+  }
+}
+
+void ActivityManager::lcm_clear_context (void* &lcm_ctx)
+{
+  observer *obs = static_cast<observer *>(lcm_ctx);
+
+  for (auto itr = this->map_observers.begin(); itr != this->map_observers.end(); itr++) {
+    remove_observer(obs, itr->second);
+  }
+
+  HMI_DEBUG("observer(%p) is removed.", obs);
+
+  delete obs;
+}
+
+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)
+  HMI_DEBUG("emit_activity_status_changed(%s, %s)", appid, state);
+
+  std::string id = appid;
+
+  if (this->map_observers.count(id)) {
+    std::vector<observer*>& obs_v = this->map_observers[id];
+
+    for (auto itr = obs_v.begin(); itr != obs_v.end(); ++itr) {
+      auto obs = *itr;
+      afb_event_t event = obs->event;
+      if (afb_event_is_valid(event)) {
+        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));
+
+        HMI_DEBUG("send Event_statusChanged(%s, %s)", appid, state);
+
+        afb_event_push(event, json_object_get(data));
+        json_object_put(data);
+      } else {
+        HMI_ERROR("afb_event is not valid");
+      }
+    }
+  }
+}
+
+} // namespace lcm