improvement
[apps/agl-service-homescreen.git] / src / hs-apprecover.cpp
index cedf751..bd34eda 100644 (file)
@@ -71,9 +71,9 @@ HS_AppRecover* HS_AppRecover::instance(void)
  */
 int HS_AppRecover::init(afb_api_t api)
 {
+    setEventHook("windowmanager/screenUpdated", on_screen_update_event);
     HS_WmProxy wm_proxy;
     wm_proxy.subscribe(api, HS_WmProxy::Event_ScreenUpdated);
-    setEventHook("windowmanager/screenUpdated", on_screen_update_event);
     return 0;
 }
 
@@ -90,20 +90,34 @@ int HS_AppRecover::init(afb_api_t api)
  */
 void HS_AppRecover::startRecovery(afb_api_t api, recover_map &map)
 {
-    HS_AfmMainProxy afm_proxy;
     for(auto &key : HS_Config::keys_recover_type) {
         for(auto &m : map[key]){
-            struct app_recover_info recover_info;
-            recover_info.recover_type = key;
-            recover_info.visibility = m.visibility;
+            struct app_recover_info recover_info = {
+                .recover_type = key,
+                .visibility = m.visibility,
+                .after = m.after
+            };
             m_recover_apps_list[m.appid] = std::move(recover_info);
-            if(key == HS_Config::keys_recover_type[1]) {
-                m_lastmode_list.insert(m.appid);
-            }
 
             // recover application
-            m_recovering_set.insert(m.appid);
-            afm_proxy.start(api,  HS_AppInfo::instance()->getAppProperty(m.appid, _keyId));
+            auto it = m_recovering_set.find(m.appid);
+            if(it == m_recovering_set.end()) {
+                m_recovering_set.insert(m.appid);
+                std::string &after = m_recover_apps_list[m.appid].after;
+                if(!after.empty()) {
+                    auto w = m_wait_recover_set.find(m_recover_apps_list[m.appid].after);
+                    if(w != m_wait_recover_set.end()) {
+                        m_wait_recover_set[after].insert(m.appid);
+                    }
+                    else {
+                        std::set<std::string> new_set;
+                        new_set.insert(m.appid);
+                        m_wait_recover_set[after] = std::move(new_set);
+                    }
+                    continue;   // don't immediately start application, wait until after applicaiton started.
+                }
+                startApplication(api, m.appid);
+            }
         }
     }
 }
@@ -115,15 +129,18 @@ void HS_AppRecover::startRecovery(afb_api_t api, recover_map &map)
  *  - appid : application id liked "dashboard"
  *
  * #### Return
- * None
+ * false : not recover app
+ * true : recover app
  * 
  */
-void HS_AppRecover::registerRecoveredApp(const std::string &appid)
+bool HS_AppRecover::registerRecoveredApp(afb_api_t api, const std::string &appid)
 {
+    bool ret = false;
     if(m_recovering_set.empty()) {
-        return;
+        return ret;
     }
 
+    AFB_INFO("recover appid=[%s].", appid.c_str());
     auto it = m_recovering_set.find(appid);
     if(it != m_recovering_set.end()) {
         m_recovering_set.erase(appid);
@@ -132,7 +149,18 @@ void HS_AppRecover::registerRecoveredApp(const std::string &appid)
         && ip->second.visibility) {
             HS_ClientManager::instance()->pushEvent("showWindow", nullptr, appid);
         }
+        ret = true;
+    }
+
+    // check wait recover application
+    auto w = m_wait_recover_set.find(appid);
+    if(w != m_wait_recover_set.end()) {
+        for(auto &ref : m_wait_recover_set[appid]) {
+            startApplication(api, ref);
+        }
+        m_wait_recover_set.erase(appid);
     }
+    return ret;
 }
 
 /**
@@ -147,6 +175,11 @@ void HS_AppRecover::registerRecoveredApp(const std::string &appid)
  */
 void HS_AppRecover::screenUpdated(struct json_object *obj)
 {
+    if(m_lastmode_list.empty()) {
+        AFB_NOTICE("init lastmode is null, so don't record lastmode.");
+        return;
+    }
+
     std::set<std::string> s_mode;
     struct json_object *ids_obj;
     if(json_object_object_get_ex(obj, key_ids.c_str(), &ids_obj)) {
@@ -169,6 +202,23 @@ void HS_AppRecover::screenUpdated(struct json_object *obj)
     }
 }
 
+/**
+ * start application
+ *
+ * #### Parameters
+ *  - api : the api
+ *  - appid : application id liked "dashboard"
+ *
+ * #### Return
+ * None
+ *
+ */
+void HS_AppRecover::startApplication(afb_api_t api, const std::string &appid)
+{
+    HS_AfmMainProxy afm_proxy;
+    afm_proxy.start(api,  HS_AppInfo::instance()->getAppProperty(appid, _keyId));
+}
+
 /**
  * update lastmode file
  *