improvement sandbox/wangzhiqiang/run_mode
authorwang_zhiqiang <wang_zhiqiang@dl.cn.nexty-ele.com>
Wed, 16 Jan 2019 02:29:02 +0000 (10:29 +0800)
committerwang_zhiqiang <wang_zhiqiang@dl.cn.nexty-ele.com>
Wed, 16 Jan 2019 02:29:02 +0000 (10:29 +0800)
Change-Id: I03dd59e12f50d4092ef9f9cb3995c8927c16650d

src/hs-periphery.cpp
src/hs-periphery.h

index 06e95ea..783b01a 100644 (file)
@@ -19,8 +19,6 @@
 #include "hmi-debug.h"
 #include "hs-clientmanager.h"
 
-static const char _restriction_on[] = "RestrictionOn";
-static const char _restriction_off[] = "RestrictionOff";
 
 /* -------------------------------------HS_PeripheryManager------------------------------------------ */
 
@@ -44,7 +42,7 @@ int HS_PeripheryManager::init(afb_api_t api)
         HMI_ERROR("homescreen-service","restriction init failed.");
     }
     else {
-        periphery_list[std::string("restriction")] = restriction;
+        periphery_list[restriction->getAppid()] = restriction;
     }
     return ret;
 }
@@ -118,26 +116,10 @@ int HS_Restriction::init(afb_api_t api)
  */
 void HS_Restriction::onEvent(afb_api_t api, const char *event, struct json_object *object)
 {
-    if(!isConcernedEvent(event))
-        return;
-
-    std::string ev = event;
-    std::size_t pos = ev.find("/");
-    if(pos != std::string::npos) {
-        ev = ev.substr(pos + 1);
-    }
-    else {
-        HMI_ERROR("homescreen-service","received event is error.");
-        return;
-    }
-
-    if(ev == _restriction_on) {
-        restrictionOn(api, object);
-    }
-    else if(ev == _restriction_off) {
-        restrictionOff(api, object);
-    }
-    else {
+    auto ip = concerned_event_list.find(std::string(event));
+    if(ip != concerned_event_list.end()) {
+        HMI_NOTICE("homescreen-service","[%s] event received.", event);
+        (this->*(ip->second))(api, object);
     }
 }
 
index d2da39e..3ed98eb 100644 (file)
 #include <unordered_map>
 #include "hs-helper.h"
 
-class HS_Periphery {
-public:
+struct HS_Periphery {
     virtual int init(afb_api_t api) = 0;
     virtual void onEvent(afb_api_t api, const char *event, struct json_object *object) = 0;
+    std::string getAppid() {return my_appid;}
+
+    std::string my_appid;
 };
 
 class HS_Restriction : public HS_Periphery {
 public:
-    HS_Restriction() = default;
+    HS_Restriction() {my_appid = "restriction";}
     ~HS_Restriction() = default;
+    HS_Restriction(HS_Restriction const &) = delete;
+    HS_Restriction &operator=(HS_Restriction const &) = delete;
 
     int init(afb_api_t api);
     void onEvent(afb_api_t api, const char *event, struct json_object *object);
 
 private:
-    const std::unordered_set<const char*> concerned_event_list {
-        "windowmanager/RestrictionOn",
-        "windowmanager/RestrictionOff"
+    typedef void (HS_Restriction::*func_handler)(afb_api_t, struct json_object*);
+    const std::unordered_map<std::string, func_handler> concerned_event_list {
+        {"windowmanager/RestrictionOn",        &HS_Restriction::restrictionOn},
+        {"windowmanager/RestrictionOff",       &HS_Restriction::restrictionOff}
     };
-    inline bool isConcernedEvent(const char* event) const {
-        return (concerned_event_list.find(event) != concerned_event_list.end()) ? true : false;
-    }
 
     void restrictionOn(afb_api_t api, struct json_object *object);
     void restrictionOff(afb_api_t api, struct json_object *object);
-
-    std::string m_appid = "restriction";
 };
 
 class HS_PeripheryManager {
@@ -59,20 +59,12 @@ public:
     void onEvent(afb_api_t api, const char *event, struct json_object *object);
 
     inline bool isPeripheryApp(const char* name) const {
-        return (periphery_app_list.find(name) != periphery_app_list.end()) ? true : false;
+        return (periphery_list.find(name) != periphery_list.end()) ? true : false;
     }
 
 private:
-    const std::unordered_set<const char*> periphery_app_list {
-        "launcher",
-        "homescreen",
-        "onscreenapp",
-        "restriction"
-    };
-
     static HS_PeripheryManager* me;
     std::unordered_map<std::string, HS_Periphery*> periphery_list;
-    // HS_Restriction m_restriction;
 };
 
 #endif // HOMESCREEN_PERIPHERY_H
\ No newline at end of file