summaryrefslogtreecommitdiff
path: root/src/components/application_manager/src/hmi_state.cc
blob: 6dc3264ac0b1c5ffa57d09565a49a81872fa5cfd (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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453

/*
 * Copyright (c) 2015, 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/hmi_state.h"

#include <boost/assign.hpp>
#include <boost/bimap.hpp>
#include <ostream>

#include "application_manager/application_manager.h"
#include "utils/helpers.h"

namespace application_manager {

SDL_CREATE_LOG_VARIABLE("HmiState")

HmiState::HmiState(std::shared_ptr<Application> app,
                   const ApplicationManager& app_mngr,
                   StateID state_id)
    : hmi_app_id_(app->hmi_app_id())
    , state_id_(state_id)
    , app_mngr_(app_mngr)
    , parent_(HmiStatePtr())
    , window_type_(mobile_apis::WindowType::INVALID_ENUM)
    , hmi_level_(mobile_apis::HMILevel::INVALID_ENUM)
    , audio_streaming_state_(mobile_apis::AudioStreamingState::INVALID_ENUM)
    , video_streaming_state_(mobile_apis::VideoStreamingState::INVALID_ENUM)
    , system_context_(mobile_apis::SystemContext::INVALID_ENUM) {
  SDL_LOG_DEBUG(*this);
}

HmiState::HmiState(std::shared_ptr<Application> app,
                   const ApplicationManager& app_mngr)
    : hmi_app_id_(app->hmi_app_id())
    , state_id_(STATE_ID_REGULAR)
    , app_mngr_(app_mngr)
    , parent_(HmiStatePtr())
    , window_type_(mobile_apis::WindowType::INVALID_ENUM)
    , hmi_level_(mobile_apis::HMILevel::INVALID_ENUM)
    , audio_streaming_state_(mobile_apis::AudioStreamingState::INVALID_ENUM)
    , video_streaming_state_(mobile_apis::VideoStreamingState::INVALID_ENUM)
    , system_context_(mobile_apis::SystemContext::INVALID_ENUM) {
  SDL_LOG_DEBUG(*this);
}

void HmiState::set_parent(HmiStatePtr parent) {
  parent_ = parent;
}

bool HmiState::is_navi_app() const {
  const ApplicationSharedPtr app =
      app_mngr_.application_by_hmi_app(hmi_app_id_);
  return app ? app->is_navi() : false;
}

bool HmiState::is_projection_app() const {
  const ApplicationSharedPtr app =
      app_mngr_.application_by_hmi_app(hmi_app_id_);
  return app ? app->mobile_projection_enabled() : false;
}

bool HmiState::is_media_app() const {
  const ApplicationSharedPtr app =
      app_mngr_.application_by_hmi_app(hmi_app_id_);
  return app ? app->is_media_application() : false;
}

bool HmiState::is_voice_communication_app() const {
  const ApplicationSharedPtr app =
      app_mngr_.application_by_hmi_app(hmi_app_id_);
  return app ? app->is_voice_communication_supported() : false;
}

bool HmiState::is_mobile_projection_app() const {
  const ApplicationSharedPtr app =
      app_mngr_.application_by_hmi_app(hmi_app_id_);
  return app ? app->mobile_projection_enabled() : false;
}

mobile_apis::HMILevel::eType HmiState::parent_hmi_level() const {
  using namespace mobile_apis;
  return parent() ? parent()->hmi_level() : HMILevel::HMI_NONE;
}

mobile_apis::HMILevel::eType HmiState::parent_max_hmi_level() const {
  using namespace mobile_apis;
  return parent() ? parent()->max_hmi_level() : HMILevel::HMI_FULL;
}

mobile_apis::AudioStreamingState::eType HmiState::parent_audio_state() const {
  using namespace mobile_apis;
  return parent() ? parent()->audio_streaming_state()
                  : AudioStreamingState::NOT_AUDIBLE;
}

mobile_apis::AudioStreamingState::eType HmiState::parent_max_audio_state()
    const {
  using namespace mobile_apis;
  return parent() ? parent()->max_audio_streaming_state()
                  : AudioStreamingState::AUDIBLE;
}

mobile_apis::VideoStreamingState::eType HmiState::parent_video_state() const {
  using namespace mobile_apis;
  return parent() ? parent()->video_streaming_state()
                  : VideoStreamingState::NOT_STREAMABLE;
}

mobile_apis::VideoStreamingState::eType HmiState::parent_max_video_state()
    const {
  using namespace mobile_apis;
  return parent() ? parent()->max_video_streaming_state()
                  : VideoStreamingState::STREAMABLE;
}

mobile_apis::WindowType::eType HmiState::window_type() const {
  return window_type_;
}

void HmiState::set_window_type(
    const mobile_apis::WindowType::eType window_type) {
  window_type_ = window_type;
}

mobile_apis::AudioStreamingState::eType VRHmiState::audio_streaming_state()
    const {
  using namespace mobile_apis;
  return AudioStreamingState::NOT_AUDIBLE;
}

mobile_apis::AudioStreamingState::eType VRHmiState::max_audio_streaming_state()
    const {
  using namespace mobile_apis;
  return AudioStreamingState::NOT_AUDIBLE;
}

VRHmiState::VRHmiState(std::shared_ptr<Application> app,
                       const ApplicationManager& app_mngr)
    : HmiState(app, app_mngr, STATE_ID_VR_SESSION) {}

TTSHmiState::TTSHmiState(std::shared_ptr<Application> app,
                         const ApplicationManager& app_mngr)
    : HmiState(app, app_mngr, STATE_ID_TTS_SESSION) {}

mobile_apis::AudioStreamingState::eType TTSHmiState::audio_streaming_state()
    const {
  using namespace helpers;
  using namespace mobile_apis;

  if (Compare<HMILevel::eType, EQ, ONE>(
          hmi_level(), HMILevel::HMI_FULL, HMILevel::HMI_LIMITED)) {
    return std::max(parent_audio_state(), max_audio_streaming_state());
  }
  return AudioStreamingState::NOT_AUDIBLE;
}

mobile_apis::AudioStreamingState::eType TTSHmiState::max_audio_streaming_state()
    const {
  using namespace mobile_apis;

  if (app_mngr_.is_attenuated_supported()) {
    return std::max(parent_max_audio_state(), AudioStreamingState::ATTENUATED);
  }
  return AudioStreamingState::NOT_AUDIBLE;
}

VideoStreamingHmiState::VideoStreamingHmiState(
    std::shared_ptr<Application> app, const ApplicationManager& app_mngr)
    : HmiState(app, app_mngr, STATE_ID_VIDEO_STREAMING) {}

mobile_apis::VideoStreamingState::eType
VideoStreamingHmiState::video_streaming_state() const {
  return std::max(parent_video_state(), max_video_streaming_state());
}

mobile_apis::VideoStreamingState::eType
VideoStreamingHmiState::max_video_streaming_state() const {
  using namespace mobile_apis;
  const ApplicationSharedPtr app =
      app_mngr_.application_by_hmi_app(hmi_app_id_);
  if (app && app->IsVideoApplication()) {
    return std::max(parent_max_video_state(), VideoStreamingState::STREAMABLE);
  }

  return VideoStreamingState::NOT_STREAMABLE;
}

NaviStreamingHmiState::NaviStreamingHmiState(std::shared_ptr<Application> app,
                                             const ApplicationManager& app_mngr)
    : VideoStreamingHmiState(app, app_mngr) {
  set_state_id(STATE_ID_NAVI_STREAMING);
}

mobile_apis::AudioStreamingState::eType
NaviStreamingHmiState::audio_streaming_state() const {
  return std::max(parent_audio_state(), max_audio_streaming_state());
}

mobile_apis::AudioStreamingState::eType
NaviStreamingHmiState::max_audio_streaming_state() const {
  using namespace mobile_apis;
  auto expected = AudioStreamingState::AUDIBLE;
  if (!is_navi_app()) {
    expected = app_mngr_.is_attenuated_supported()
                   ? AudioStreamingState::ATTENUATED
                   : AudioStreamingState::NOT_AUDIBLE;
  }

  return std::max(expected, parent_max_audio_state());
}

PhoneCallHmiState::PhoneCallHmiState(std::shared_ptr<Application> app,
                                     const ApplicationManager& app_mngr)
    : HmiState(app, app_mngr, STATE_ID_PHONE_CALL) {}

SafetyModeHmiState::SafetyModeHmiState(std::shared_ptr<Application> app,
                                       const ApplicationManager& app_mngr)
    : HmiState(app, app_mngr, STATE_ID_SAFETY_MODE) {}

DeactivateHMI::DeactivateHMI(std::shared_ptr<Application> app,
                             const ApplicationManager& app_mngr)
    : HmiState(app, app_mngr, STATE_ID_DEACTIVATE_HMI) {}

mobile_apis::HMILevel::eType DeactivateHMI::hmi_level() const {
  using namespace mobile_apis;
  if (HMILevel::INVALID_ENUM == parent_hmi_level()) {
    return parent_hmi_level();
  }
  return std::max(parent_hmi_level(), max_hmi_level());
}

mobile_apis::HMILevel::eType DeactivateHMI::max_hmi_level() const {
  using namespace helpers;
  using namespace mobile_apis;

  if (WindowType::WIDGET == window_type()) {
    return std::max(HMILevel::HMI_FULL, parent_max_hmi_level());
  }

  return std::max(HMILevel::HMI_BACKGROUND, parent_max_hmi_level());
}

AudioSource::AudioSource(std::shared_ptr<Application> app,
                         const ApplicationManager& app_mngr)
    : HmiState(app, app_mngr, STATE_ID_AUDIO_SOURCE)
    , keep_context_(app->keep_context()) {
  app->set_keep_context(false);
}

mobile_apis::HMILevel::eType AudioSource::hmi_level() const {
  using namespace mobile_apis;

  if (HMILevel::INVALID_ENUM == parent_hmi_level()) {
    return parent_hmi_level();
  }

  if (WindowType::WIDGET == window_type() || keep_context_) {
    return std::max(parent_hmi_level(), max_hmi_level());
  }

  auto expected = HMILevel::HMI_BACKGROUND;
  if (is_navi_app() || is_projection_app() || is_voice_communication_app()) {
    expected = HMILevel::HMI_LIMITED;
  }

  return std::max(std::max(expected, max_hmi_level()), parent_hmi_level());
}

mobile_apis::AudioStreamingState::eType AudioSource::audio_streaming_state()
    const {
  return is_media_app() && !is_navi_app()
             ? mobile_apis::AudioStreamingState::NOT_AUDIBLE
             : parent_audio_state();
}

mobile_apis::VideoStreamingState::eType AudioSource::video_streaming_state()
    const {
  return parent_video_state();
}

mobile_apis::HMILevel::eType AudioSource::max_hmi_level() const {
  using namespace mobile_apis;

  if (WindowType::WIDGET == window_type()) {
    return std::max(HMILevel::HMI_FULL, parent_max_hmi_level());
  }

  auto expected = HMILevel::HMI_FULL;
  if (!keep_context_ && is_media_app() && !is_navi_app() &&
      !is_projection_app()) {
    expected = HMILevel::HMI_BACKGROUND;
  }

  return std::max(expected, parent_max_hmi_level());
}

mobile_apis::AudioStreamingState::eType AudioSource::max_audio_streaming_state()
    const {
  return is_media_app() && !is_navi_app()
             ? mobile_apis::AudioStreamingState::NOT_AUDIBLE
             : parent_max_audio_state();
}

mobile_apis::VideoStreamingState::eType AudioSource::max_video_streaming_state()
    const {
  return parent() ? parent()->max_video_streaming_state()
                  : mobile_apis::VideoStreamingState::STREAMABLE;
}

EmbeddedNavi::EmbeddedNavi(std::shared_ptr<Application> app,
                           const ApplicationManager& app_mngr)
    : HmiState(app, app_mngr, STATE_ID_EMBEDDED_NAVI) {}

mobile_apis::AudioStreamingState::eType EmbeddedNavi::audio_streaming_state()
    const {
  return is_navi_app() ? mobile_apis::AudioStreamingState::NOT_AUDIBLE
                       : parent_audio_state();
}

mobile_apis::VideoStreamingState::eType EmbeddedNavi::video_streaming_state()
    const {
  return mobile_apis::VideoStreamingState::NOT_STREAMABLE;
}

mobile_apis::HMILevel::eType EmbeddedNavi::hmi_level() const {
  using namespace mobile_apis;
  using namespace helpers;

  if (HMILevel::INVALID_ENUM == parent_hmi_level()) {
    return parent_hmi_level();
  }

  if (WindowType::WIDGET == window_type()) {
    return std::max(parent_hmi_level(), max_hmi_level());
  }

  auto expected = HMILevel::HMI_BACKGROUND;
  if ((is_media_app() || is_voice_communication_app()) && !is_navi_app()) {
    expected = HMILevel::HMI_LIMITED;
  }

  return std::max(std::max(expected, max_hmi_level()), parent_hmi_level());
}

mobile_apis::AudioStreamingState::eType
EmbeddedNavi::max_audio_streaming_state() const {
  return is_navi_app() ? mobile_apis::AudioStreamingState::NOT_AUDIBLE
                       : parent_max_audio_state();
}

mobile_apis::VideoStreamingState::eType
EmbeddedNavi::max_video_streaming_state() const {
  return is_navi_app() ? mobile_apis::VideoStreamingState::NOT_STREAMABLE
                       : parent_max_video_state();
}

mobile_apis::HMILevel::eType EmbeddedNavi::max_hmi_level() const {
  using namespace mobile_apis;
  using namespace helpers;

  if (WindowType::WIDGET == window_type()) {
    return std::max(HMILevel::HMI_FULL, parent_max_hmi_level());
  }

  auto expected = HMILevel::HMI_FULL;
  if (is_navi_app()) {
    expected = HMILevel::HMI_BACKGROUND;
  }

  return std::max(expected, parent_max_hmi_level());
}

namespace {
typedef boost::bimap<HmiState::StateID, std::string> StateID2StrMap;
const StateID2StrMap kStateID2StrMap =
    boost::assign::list_of<StateID2StrMap::relation>(
        HmiState::StateID::STATE_ID_CURRENT, "CURRENT")(
        HmiState::StateID::STATE_ID_REGULAR, "REGULAR")(
        HmiState::StateID::STATE_ID_POSTPONED, "POSTPONED")(
        HmiState::StateID::STATE_ID_PHONE_CALL, "PHONE_CALL")(
        HmiState::StateID::STATE_ID_SAFETY_MODE, "SAFETY_MODE")(
        HmiState::StateID::STATE_ID_VR_SESSION, "VR_SESSION")(
        HmiState::StateID::STATE_ID_TTS_SESSION, "TTS_SESSION")(
        HmiState::StateID::STATE_ID_VIDEO_STREAMING, "VIDEO_STREAMING")(
        HmiState::StateID::STATE_ID_NAVI_STREAMING, "NAVI_STREAMING")(
        HmiState::StateID::STATE_ID_DEACTIVATE_HMI, "DEACTIVATE_HMI")(
        HmiState::StateID::STATE_ID_AUDIO_SOURCE, "AUDIO_SOURCE")(
        HmiState::StateID::STATE_ID_EMBEDDED_NAVI, "EMBEDDED_NAVI");
}  // anonymous namespace

// cppcheck-suppress unusedFunction //Used in the next
// overload of operator<<
std::ostream& operator<<(std::ostream& os, const HmiState::StateID src) {
  try {
    os << kStateID2StrMap.left.at(src);
  } catch (const std::exception&) {
    // specified element have NOT been found
    os << "UNRECOGNIZED(" << static_cast<int>(src) << ")";
  }

  return os;
}

std::ostream& operator<<(std::ostream& os, const HmiState& src) {
  const ApplicationSharedPtr app =
      src.app_mngr_.application_by_hmi_app(src.hmi_app_id_);
  os << "HMIState(app id:";
  if (app) {
    os << app->app_id();
  } else {
    os << "(none)";
  }
  os << ", hmi app id:" << src.hmi_app_id_
     << ", window_type:" << src.window_type_ << ", state:" << src.state_id()
     << ", hmi_level:" << src.hmi_level()
     << ", audio:" << src.audio_streaming_state()
     << ", video:" << src.video_streaming_state()
     << ", context:" << src.system_context() << ')';

  return os;
}

}  // namespace application_manager