add lastmode function
authorwang_zhiqiang <wang_zhiqiang@dl.cn.nexty-ele.com>
Mon, 22 Apr 2019 09:38:59 +0000 (17:38 +0800)
committerwang_zhiqiang <wang_zhiqiang@dl.cn.nexty-ele.com>
Tue, 23 Apr 2019 11:23:14 +0000 (19:23 +0800)
Change-Id: I4b7ca96d1333d170c211b420d79ea9a4d3e1272a

src/hs-apprecover.cpp
src/hs-apprecover.h
src/hs-clientmanager.cpp
src/hs-clientmanager.h
src/hs-config.cpp
src/hs-config.h
src/hs-helper.cpp

index ded070e..cedf751 100644 (file)
@@ -36,7 +36,7 @@ HS_AppRecover* HS_AppRecover::me = nullptr;
  */
 int on_screen_update_event(afb_api_t api, const char *event, struct json_object *object)
 {
-
+    HS_AppRecover::instance()->screenUpdated(object);
     return 0;
 }
 
@@ -97,6 +97,9 @@ void HS_AppRecover::startRecovery(afb_api_t api, recover_map &map)
             recover_info.recover_type = key;
             recover_info.visibility = m.visibility;
             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);
@@ -112,11 +115,15 @@ void HS_AppRecover::startRecovery(afb_api_t api, recover_map &map)
  *  - appid : application id liked "dashboard"
  *
  * #### Return
- * false : not all application recovered
- * true : all applications recovered
+ * None
+ * 
  */
-bool HS_AppRecover::registerRecoveredApp(std::string &appid)
+void HS_AppRecover::registerRecoveredApp(const std::string &appid)
 {
+    if(m_recovering_set.empty()) {
+        return;
+    }
+
     auto it = m_recovering_set.find(appid);
     if(it != m_recovering_set.end()) {
         m_recovering_set.erase(appid);
@@ -126,6 +133,100 @@ bool HS_AppRecover::registerRecoveredApp(std::string &appid)
             HS_ClientManager::instance()->pushEvent("showWindow", nullptr, appid);
         }
     }
+}
+
+/**
+ * screenUpdated event handler
+ *
+ * #### Parameters
+ *  - obj : screenUpdate object
+ *
+ * #### Return
+ * None
+ * 
+ */
+void HS_AppRecover::screenUpdated(struct json_object *obj)
+{
+    std::set<std::string> s_mode;
+    struct json_object *ids_obj;
+    if(json_object_object_get_ex(obj, key_ids.c_str(), &ids_obj)) {
+        if(json_object_get_type(ids_obj) == json_type_array) {
+            int array_len = json_object_array_length(ids_obj);
+            for (int i = 0; i < array_len; ++i) {
+                struct json_object *j_id = json_object_array_get_idx(ids_obj, i);
+                std::string appid = json_object_get_string(j_id);
+                if(!isHomescreenApp(appid)) {
+                    s_mode.insert(appid);
+                }
+            }
+            if(!s_mode.empty()) {
+                updateLastmode(s_mode);
+            }
+        }
+        else {
+            AFB_WARNING("screenUpdated list isn't array.");
+        }
+    }
+}
+
+/**
+ * update lastmode file
+ *
+ * #### Parameters
+ *  - set : new lastmode set
+ *
+ * #### Return
+ * None
+ * 
+ */
+void HS_AppRecover::updateLastmode(std::set<std::string> &set)
+{
+    if(set.size() == m_lastmode_list.size()) {
+        bool is_same = true;
+        for(auto &m : set) {
+            auto it = m_lastmode_list.find(m);
+            if(it == m_lastmode_list.end()) {
+                is_same = false;
+                break;
+            }
+        }
+        if(is_same) {   // lastmode aren't changed
+            return;
+        }
+    }
+
+    m_lastmode_list.swap(set);
+    struct json_object *arr_obj = json_object_new_array();
+    for(auto &it : m_lastmode_list) {
+        struct json_object *j_obj = json_object_new_object();
+        json_object_object_add(j_obj, HS_Config::key_appid.c_str(), json_object_new_string(it.c_str()));
+        json_object_object_add(j_obj, HS_Config::key_visibility.c_str(), json_object_new_string("visible"));
+        json_object_array_add(arr_obj, j_obj);
+    }
+
+    auto path = HS_Config::root_dir + "/etc/" + HS_Config::lastmode_json;
+    if(writeJsonFile(path.c_str(), arr_obj) < 0) {
+        AFB_ERROR("write lastmode error.");
+    }
+}
 
-    return m_recovering_set.empty() ? true : false;
+/**
+ * judge whether app is Homescreen app
+ *
+ * #### Parameters
+ *  - appid : application id
+ *
+ * #### Return
+ * true : homescreen app
+ * false : not homescreen app
+ *
+ */
+bool HS_AppRecover::isHomescreenApp(const std::string &appid) const
+{
+    auto it = m_recover_apps_list.find(appid);
+    if(it != m_recover_apps_list.end()
+    && it->second.recover_type == HS_Config::keys_recover_type[0]) {
+            return true;
+    }
+    return false;
 }
\ No newline at end of file
index abaf20b..3a0f8d5 100644 (file)
@@ -38,12 +38,18 @@ public:
     static HS_AppRecover* instance(void);
     int init(afb_api_t api);
     void startRecovery(afb_api_t api, recover_map &map);
-    bool registerRecoveredApp(std::string &appid);
+    void registerRecoveredApp(const std::string &appid);
+    void screenUpdated(struct json_object *obj);
 
 private:
+    void updateLastmode(std::set<std::string> &set);
+    bool isHomescreenApp(const std::string &appid) const;
+
+    const std::string key_ids = "ids";
     static HS_AppRecover* me;
     std::map<std::string, struct app_recover_info> m_recover_apps_list;
     std::set<std::string> m_recovering_set;
+    std::set<std::string> m_lastmode_list;
 };
 
 #endif // HOMESCREEN_APPRECOVER_H
\ No newline at end of file
index 83d6bb3..1128b01 100644 (file)
@@ -17,6 +17,7 @@
 #include <cstring>
 #include <algorithm>
 #include "hs-clientmanager.h"
+#include "hs-apprecover.h"
 
 static const char _homescreen[] = "homescreen";
 
@@ -187,7 +188,7 @@ int HS_ClientManager::handleRequest(afb_req_t request, const char *verb, const c
                 appid2ctxt[appid] = createClientCtxt(request, appid);
                 HS_Client* client = addClient(request, appid);
                 ret = client->handleRequest(request, "subscribe");
-                registerApplication(appid);
+                HS_AppRecover::instance()->registerRecoveredApp(std::string(appid));
             }
             else {
                 AFB_NOTICE("not exist session");
@@ -232,19 +233,4 @@ int HS_ClientManager::pushEvent(const char *event, struct json_object *param, st
     }
 
     return 0;
-}
-
-/**
- * register recovered application
- *
- * #### Parameters
- *  - appid : application id
- *
- * #### Return
- * None
- *
- */
-void HS_ClientManager::registerApplication(std::string appid)
-{
-
 }
\ No newline at end of file
index cbddaba..efc36de 100644 (file)
@@ -53,7 +53,6 @@ private:
     HS_ClientCtxt* createClientCtxt(afb_req_t req, std::string appid);
     HS_Client* addClient(afb_req_t req, std::string appid);
     void removeClient(std::string appid);
-    void registerApplication(std::string appid);
 
 private:
     static HS_ClientManager* me;
index b8f9f5b..c0d6942 100644 (file)
@@ -22,6 +22,10 @@ const std::array<std::string, 3> HS_Config::keys_recover_type = {   // based on
     "default-lastmode",
     "normal-apps"
 };
+const std::string HS_Config::lastmode_json = "lastmode.json";
+const std::string HS_Config::key_appid = "appid";
+const std::string HS_Config::key_visibility = "visibility";
+std::string HS_Config::root_dir = "";
 
 /**
  * read configuration file to memory
@@ -35,14 +39,14 @@ const std::array<std::string, 3> HS_Config::keys_recover_type = {   // based on
  */
 int HS_Config::readConfig(void)
 {
-    auto rootdir = std::string(getenv("AFM_APP_INSTALL_DIR"));
-    auto path = rootdir + "/etc/" + hs_conf_json;
+    root_dir = std::string(getenv("AFM_APP_INSTALL_DIR"));
+    auto path = root_dir + "/etc/" + hs_conf_json;
     if(readJsonFile(path.c_str(), &m_hs_conf) < 0) {
         AFB_ERROR("read %s failed.", hs_conf_json.c_str());
         return -1;
     }
 
-    path = rootdir + "/etc/" + lastmode_json;
+    path = root_dir + "/etc/" + lastmode_json;
     if(readJsonFile(path.c_str(), &m_lastmode) < 0) {
         AFB_ERROR("read %s failed.", lastmode_json.c_str());
         m_lastmode = nullptr;
index d422248..fda49a7 100644 (file)
@@ -48,19 +48,20 @@ public:
     const struct handshake_info* getHandshakeInfo(void) const {return &m_handshake_info;}
     recover_map& getRecoverMap(void) {return m_recover_map;}
     static const std::array<std::string, RECOVER_MAX> keys_recover_type;   // based on hs-conf.json
+    static const std::string lastmode_json;
+    static const std::string key_appid;
+    static const std::string key_visibility;
+    static std::string root_dir;
 
 private:
     int parseConfig(void);
     std::vector<struct recover_app_info> getRecoverAppInfo(struct json_object *obj);
 
     const std::string hs_conf_json = "hs-conf.json";
-    const std::string lastmode_json = "lastmode.json";
     const std::string key_handshake = "handshake";
     const std::string key_times = "times";
     const std::string key_sleep = "sleep";
     const std::string key_recover = "recover";
-    const std::string key_appid = "appid";
-    const std::string key_visibility = "visibility";
 
     struct json_object *m_hs_conf;
     struct json_object *m_lastmode;
index 33ab721..30b53b2 100644 (file)
@@ -330,8 +330,8 @@ int readJsonFile(const char* file, struct json_object **obj)
  * - obj : json_object
  *
  * #### Return
- * 0 : read success
- * -1 : read fail
+ * 0 : write success
+ * -1 : write fail
  *
  */
 int writeJsonFile(const char* file,  struct json_object *obj)