summaryrefslogtreecommitdiff
path: root/src/components/application_manager/src/hmi_language_handler.cc
blob: 2c6b156d61397e2ac2382b6a7dce827468e07b45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
 * Copyright (c) 2016, Ford Motor Company
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following
 * disclaimer in the documentation and/or other materials provided with the
 * distribution.
 *
 * Neither the name of the Ford Motor Company nor the names of its contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
*/

#include "application_manager/application_manager_impl.h"
#include "application_manager/hmi_language_handler.h"
#include "application_manager/message_helper.h"
#include "application_manager/hmi_capabilities.h"
#include "utils/helpers.h"
#include "resumption/last_state.h"

static const std::string LanguagesKey = "Languages";
static const std::string UIKey = "UI";
static const std::string VRKey = "VR";
static const std::string TTSKey = "TTS";

namespace application_manager {

CREATE_LOGGERPTR_GLOBAL(logger_, "ApplicationManager")

HMILanguageHandler::HMILanguageHandler()
  : is_ui_language_received_(false),
    is_vr_language_received_(false),
    is_tts_language_received_(false) {
}

void HMILanguageHandler::set_ui_language(
      hmi_apis::Common_Language::eType language) {
  LOG4CXX_AUTO_TRACE(logger_);
  resumption::LastState::instance()->dictionary[LanguagesKey][UIKey] = language;
}

void HMILanguageHandler::set_vr_language(
      hmi_apis::Common_Language::eType language) {
  LOG4CXX_AUTO_TRACE(logger_);
  resumption::LastState::instance()->dictionary[LanguagesKey][VRKey] = language;
}

void HMILanguageHandler::set_tts_language(
      hmi_apis::Common_Language::eType language) {
  LOG4CXX_AUTO_TRACE(logger_);
  resumption::LastState::instance()->dictionary[LanguagesKey][TTSKey] = language;
}

hmi_apis::Common_Language::eType HMILanguageHandler::get_ui_language() const {
  LOG4CXX_AUTO_TRACE(logger_);
  using namespace resumption;
  using namespace hmi_apis;
  if (LastState::instance()->dictionary.isMember(LanguagesKey)) {
    if (LastState::instance()->dictionary[LanguagesKey].isMember(UIKey)) {
      Common_Language::eType ui_language =
          static_cast<Common_Language::eType>(
          LastState::instance()->dictionary[LanguagesKey][UIKey].asUInt());

      return ui_language;
    }
  }
  return Common_Language::INVALID_ENUM;
}

hmi_apis::Common_Language::eType HMILanguageHandler::get_vr_language() const {
  LOG4CXX_AUTO_TRACE(logger_);
  using namespace resumption;
  using namespace hmi_apis;
  if (LastState::instance()->dictionary.isMember(LanguagesKey)) {
    if (LastState::instance()->dictionary[LanguagesKey].isMember(VRKey)) {
      Common_Language::eType vr_language =
          static_cast<Common_Language::eType>(
          LastState::instance()->dictionary[LanguagesKey][VRKey].asUInt());

      return vr_language;
    }
  }
  return Common_Language::INVALID_ENUM;
}

hmi_apis::Common_Language::eType HMILanguageHandler::get_tts_language() const {
  LOG4CXX_AUTO_TRACE(logger_);
  using namespace resumption;
  using namespace hmi_apis;
  if (LastState::instance()->dictionary.isMember(LanguagesKey)) {
    if (LastState::instance()->dictionary[LanguagesKey].isMember(TTSKey)) {
      // Web HMI returns -1 which causes assert for debug
      Common_Language::eType tts_language = //Common_Language::EN_US;
          static_cast<Common_Language::eType>(
          LastState::instance()->dictionary[LanguagesKey][TTSKey].asUInt());

      return tts_language;
    }
  }
  return Common_Language::INVALID_ENUM;
}

void HMILanguageHandler::on_event(const event_engine::Event& event) {
  LOG4CXX_AUTO_TRACE(logger_);
  switch (event.id()) {
  case hmi_apis::FunctionID::UI_GetLanguage:
    LOG4CXX_DEBUG(logger_, "Got UI language response.");
    is_ui_language_received_ = true;
    break;
  case hmi_apis::FunctionID::VR_GetLanguage:
    LOG4CXX_DEBUG(logger_, "Got VR language response.");
    is_vr_language_received_ = true;
    break;
  case hmi_apis::FunctionID::TTS_GetLanguage:
    LOG4CXX_DEBUG(logger_, "Got TTS language response.");
    is_tts_language_received_ = true;
    break;
  default:
    return;
  }

  if (is_ui_language_received_ && is_vr_language_received_ &&
      is_tts_language_received_) {
    VerifyRegisteredApps();
  }
}

void HMILanguageHandler::set_handle_response_for(
    const event_engine::smart_objects::SmartObject& request) {
  LOG4CXX_AUTO_TRACE(logger_);
  using namespace helpers;
  if (!request.keyExists(strings::params)) {
    LOG4CXX_ERROR(logger_, "Object does not have " << strings::params
                  << " key.");
    return;
  }

  if (!request[strings::params].keyExists(strings::function_id)) {
    LOG4CXX_ERROR(logger_, "Object does not have " << strings::function_id
                  << " key.");
    return;
  }

  if (!request[strings::params].keyExists(strings::correlation_id)) {
    LOG4CXX_ERROR(logger_, "Object does not have " << strings::correlation_id
                  << " key.");
    return;
  }

  hmi_apis::FunctionID::eType function_id =
      static_cast<hmi_apis::FunctionID::eType>(
        request[strings::params][strings::function_id].asInt());

  if (!Compare<hmi_apis::FunctionID::eType, EQ, ONE>(
        function_id,
        hmi_apis::FunctionID::UI_GetLanguage,
        hmi_apis::FunctionID::VR_GetLanguage,
        hmi_apis::FunctionID::TTS_GetLanguage)) {
    LOG4CXX_ERROR(logger_,
                  "Only *GetLanguage request are allowed to be subscribed.");
    return;
  }

  uint32_t correlation_id =
      request[strings::params][strings::correlation_id].asUInt();

  subscribe_on_event(function_id, correlation_id);

  LOG4CXX_DEBUG(logger_, "Subscribed for function_id " << function_id <<
                " and correlation_id " << correlation_id);
}

void HMILanguageHandler::VerifyRegisteredApps() const {
  LOG4CXX_AUTO_TRACE(logger_);
  using namespace helpers;
  HMICapabilities& hmi_capabilities =
        ApplicationManagerImpl::instance()->hmi_capabilities();

  hmi_apis::Common_Language::eType ui_language =
          hmi_capabilities.active_ui_language();
  hmi_apis::Common_Language::eType vr_language =
          hmi_capabilities.active_vr_language();
  hmi_apis::Common_Language::eType tts_language =
          hmi_capabilities.active_tts_language();
  ApplicationManagerImpl::ApplicationListAccessor accessor;
  ApplicationSetIt it = accessor.begin();
  for (; accessor.end() != it;) {
    ApplicationSharedPtr app = *it++;
    if (app->ui_language() !=
        MessageHelper::CommonToMobileLanguage(ui_language) ||
        !Compare<mobile_apis::Language::eType, EQ, ALL>(
          app->language(),
          MessageHelper::CommonToMobileLanguage(vr_language),
          MessageHelper::CommonToMobileLanguage(tts_language))) {
      LOG4CXX_INFO(logger_, "Application with app_id " << app->app_id()
                   << " will be unregistered because of "
                      "HMI language(s) mismatch.");
      MessageHelper::SendOnLanguageChangeToMobile(app->app_id());
      MessageHelper::SendOnAppInterfaceUnregisteredNotificationToMobile(
                  app->app_id(),
                  mobile_api::AppInterfaceUnregisteredReason::LANGUAGE_CHANGE);
      ApplicationManagerImpl::instance()->UnregisterApplication(
                  app->app_id(), mobile_apis::Result::SUCCESS, false);
    }
  }
}

} // namespace application_manager