add event
[src/libhomescreen.git] / src / libhomescreen.cpp
index 121def7..8b0ffd8 100644 (file)
@@ -1,5 +1,6 @@
 /*
  * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
+ * Copyright (c) 2018,2019 TOYOTA MOTOR CORPORATION
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -47,7 +48,10 @@ const std::vector<std::string> LibHomeScreen::api_list {
        std::string("hideWindow"),
        std::string("replyShowWindow"),
        std::string("showNotification"),
-       std::string("showInformation")
+       std::string("showInformation"),
+       std::string("getRunnables"),
+       std::string("registerShortcut"),
+       std::string("updateShortcut")
 };
 
 const std::vector<std::string> LibHomeScreen::event_list {
@@ -59,6 +63,13 @@ const std::vector<std::string> LibHomeScreen::event_list {
        std::string("replyShowWindow"),
        std::string("showNotification"),
        std::string("showInformation"),
+       std::string("application-list-changed"),
+       std::string("registerShortcut"),
+       std::string("updateShortcut"),
+       std::string("setDestination"),
+       std::string("cancelDestination"),
+       std::string("startNavigation"),
+       std::string("stopNavigation"),
        std::string("none")
 };
 
@@ -303,35 +314,43 @@ int LibHomeScreen::onScreenReply(const char* reply_message)
  */
 void LibHomeScreen::set_event_handler(enum EventType et, handler_func f)
 {
-       if (et >= 1 && et <= 7) {
-               switch (et) {
-                       case Event_ShowWindow:
-                               this->subscribe(LibHomeScreen::event_list[0]);
-                               break;
-                       case Event_OnScreenMessage:
-                               this->subscribe(LibHomeScreen::event_list[1]);
-                               break;
-                       case Event_OnScreenReply:
-                               this->subscribe(LibHomeScreen::event_list[2]);
-                               break;
-                       case Event_HideWindow:
-                               this->subscribe(LibHomeScreen::event_list[3]);
-                               break;
-                       case Event_ReplyShowWindow:
-                               this->subscribe(LibHomeScreen::event_list[4]);
-                               break;
-                       case Event_ShowNotification:
-                               this->subscribe(LibHomeScreen::event_list[5]);
-                               break;
-                       case Event_ShowInformation:
-                               this->subscribe(LibHomeScreen::event_list[6]);
-                               break;
-               }
-
+       if (et > Event_Min && et < Event_Max) {
                this->handlers[et] = std::move(f);
        }
 }
 
+/**
+ * This function subscribe HomeScreen event
+ *
+ * #### Parameters
+ * None
+ *
+ * #### Return
+ * - Nothing
+ *
+ * #### Note
+ * To call HomeScreen's subscribe APIs.
+ *
+ */
+void LibHomeScreen::publishSubscription(void)
+{
+       struct json_object* j_obj = json_object_new_array();
+       for(auto &it : handlers) {
+        json_object_array_add(j_obj, json_object_new_string(LibHomeScreen::event_list[it.first - 1].c_str()));
+       }
+
+       if(!sp_websock) {
+               return;
+       }
+       struct json_object* push_obj = json_object_new_object();
+       json_object_object_add(push_obj, "event", j_obj);
+
+       int ret = afb_wsj1_call_j(sp_websock, API, "subscribe", push_obj, _on_reply_static, this);
+       if (ret < 0) {
+               HMI_ERROR("libhomescreen","Failed to call subscribe.");
+       }
+}
+
 /**
  * This function calls the API of HomeScreen via WebSocket
  *
@@ -597,6 +616,78 @@ int LibHomeScreen::showInformation(json_object* json)
        return this->call("showInformation", json);
 }
 
+/**
+ * get runnables list
+ *
+ * Call HomeScreen Service's getRunnables verb to get runnalbes list.
+ *
+ * #### Parameters
+ * - Nothing
+ *
+ * #### Return
+ * - Returns 0 on success or -1 in case of error.
+ *
+ */
+int LibHomeScreen::getRunnables(void)
+{
+       return this->call("getRunnables", nullptr);
+}
+
+/**
+ * register shortcut to homescreen
+ *
+ * Call HomeScreen Service's registerShortcut verb to regitster shortcut.
+ *
+ * #### Parameters
+ * - json [in] : This argument should be specified to the json parameters.
+ *
+ * #### Return
+ * - Returns 0 on success or -1 in case of error.
+ *
+ */
+int LibHomeScreen::registerShortcut(const char* application_id, json_object* json)
+{
+    if(!sp_websock)
+    {
+               return -1;
+    }
+
+       struct json_object* j_obj = json_object_new_object();
+       struct json_object* val = json_object_new_string(application_id);
+       json_object_object_add(j_obj, ApplicationId, val);
+       json_object_object_add(j_obj, "parameter", json);
+
+    return this->call("registerShortcut", j_obj);
+}
+
+
+/**
+ * update shortcut to launcher
+ *
+ * Call HomeScreen Service's updateShortcut verb to update shortcut.
+ *
+ * #### Parameters
+ * - json [in] : This argument should be specified to the json parameters.
+ *
+ * #### Return
+ * - Returns 0 on success or -1 in case of error.
+ *
+ */
+int LibHomeScreen::updateShortcut(const char* application_id, json_object* json)
+{
+    if(!sp_websock)
+    {
+               return -1;
+    }
+
+       struct json_object* j_obj = json_object_new_object();
+       struct json_object* val = json_object_new_string(application_id);
+       json_object_object_add(j_obj, ApplicationId, val);
+       json_object_object_add(j_obj, "parameter", json);
+
+    return this->call("updateShortcut", j_obj);
+}
+
 
 /************* Callback Function *************/
 
@@ -650,46 +741,11 @@ void LibHomeScreen::on_event(void *closure, const char *event, struct afb_wsj1_m
                return;
        }
 
-       if (strcasecmp(event_type, LibHomeScreen::event_list[0].c_str()) == 0) {
-               auto i = this->handlers.find(Event_ShowWindow);
-               if ( i != this->handlers.end() ) {
-                       i->second(json_data);
-               }
-       }
-       else if (strcasecmp(event_type, LibHomeScreen::event_list[1].c_str()) == 0) {
-               auto i = this->handlers.find(Event_OnScreenMessage);
-               if ( i != this->handlers.end() ) {
-                       i->second(json_data);
-               }
-       }
-       else if (strcasecmp(event_type, LibHomeScreen::event_list[2].c_str()) == 0) {
-               auto i = this->handlers.find(Event_OnScreenReply);
-               if ( i != this->handlers.end() ) {
-                       i->second(json_data);
-               }
-       }
-       else if (strcasecmp(event_type, LibHomeScreen::event_list[3].c_str()) == 0) {
-               auto i = this->handlers.find(Event_HideWindow);
-               if ( i != this->handlers.end() ) {
-                       i->second(json_data);
-               }
-       }
-       else if (strcasecmp(event_type, LibHomeScreen::event_list[4].c_str()) == 0) {
-               auto i = this->handlers.find(Event_ReplyShowWindow);
-               if ( i != this->handlers.end() ) {
-                       i->second(json_data);
-               }
-       }
-       else if (strcasecmp(event_type, LibHomeScreen::event_list[5].c_str()) == 0) {
-               auto i = this->handlers.find(Event_ShowNotification);
-               if ( i != this->handlers.end() ) {
-                       i->second(json_data);
-               }
-       }
-       else if (strcasecmp(event_type, LibHomeScreen::event_list[6].c_str()) == 0) {
-               auto i = this->handlers.find(Event_ShowInformation);
-               if ( i != this->handlers.end() ) {
-                       i->second(json_data);
+       int e_type = getEventType(event_type);
+       if(e_type < Event_Max) {
+               auto it = this->handlers.find(EventType(e_type));
+               if ( it != this->handlers.end() ) {
+                       it->second(json_data);
                }
        }
 }
@@ -708,6 +764,20 @@ void LibHomeScreen::on_reply(void *closure, struct afb_wsj1_msg *msg)
        }
 }
 
+/*
+* convert event name to event type
+*/
+int LibHomeScreen::getEventType(const char *event)
+{
+       int i = 0;
+       for(; i < LibHomeScreen::event_list.size(); ++i) {
+               if (strcasecmp(event, LibHomeScreen::event_list[i].c_str()) == 0) {
+                       break;
+               }
+       }
+       return (i + 1) < Event_Max ? (i + 1) : Event_Max;
+}
+
 static bool has_verb(const string& verb)
 {
        HMI_DEBUG("libhomescreen","verb is %s", verb.c_str());