summaryrefslogtreecommitdiff
path: root/src/components/application_manager/include/application_manager/state_controller_impl.h
blob: e0200f55af1cb7a18ded55bd91892ad47c96dd6a (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
/*
 * 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.
 */

#ifndef SRC_COMPONENTS_APPLICATION_MANAGER_INCLUDE_APPLICATION_MANAGER_STATE_CONTROLLER_IMPL_H_
#define SRC_COMPONENTS_APPLICATION_MANAGER_INCLUDE_APPLICATION_MANAGER_STATE_CONTROLLER_IMPL_H_

#include <list>
#include <map>
#include <unordered_set>
#include "application_manager/application.h"
#include "application_manager/application_manager.h"
#include "application_manager/hmi_state.h"
#include "application_manager/message_helper.h"
#include "application_manager/state_controller.h"
#include "event_engine/event_observer.h"
#include "interfaces/MOBILE_API.h"
#include "utils/helpers.h"
#include "utils/lock.h"

namespace application_manager {

class StateControllerImpl : public event_engine::EventObserver,
                            public StateController {
 public:
  explicit StateControllerImpl(ApplicationManager& app_mngr);

  void SetRegularState(ApplicationSharedPtr app,
                       const WindowID window_id,
                       HmiStatePtr state,
                       const bool SendActivateApp) OVERRIDE;

  void SetRegularState(
      ApplicationSharedPtr app,
      const WindowID window_id,
      const mobile_apis::HMILevel::eType hmi_level,
      const mobile_apis::AudioStreamingState::eType audio_state,
      const mobile_apis::VideoStreamingState::eType video_state,
      const bool SendActivateApp) OVERRIDE;

  void SetRegularState(ApplicationSharedPtr app,
                       const WindowID window_id,
                       const mobile_apis::HMILevel::eType hmi_level,
                       const bool SendActivateApp) OVERRIDE;

  void SetRegularState(
      ApplicationSharedPtr app,
      const WindowID window_id,
      const mobile_apis::HMILevel::eType hmi_level,
      const mobile_apis::AudioStreamingState::eType audio_state,
      const mobile_apis::VideoStreamingState::eType video_state,
      const mobile_apis::SystemContext::eType system_context,
      const bool SendActivateApp) OVERRIDE;

  void SetRegularState(ApplicationSharedPtr app,
                       const WindowID window_id,
                       const mobile_apis::HMILevel::eType hmi_level) OVERRIDE;

  void SetRegularState(
      ApplicationSharedPtr app,
      const WindowID window_id,
      const mobile_apis::AudioStreamingState::eType audio_state,
      const mobile_apis::VideoStreamingState::eType video_state) OVERRIDE;

  void SetRegularState(
      ApplicationSharedPtr app,
      const WindowID window_id,
      const mobile_apis::SystemContext::eType system_context) OVERRIDE;

  void SetRegularState(ApplicationSharedPtr app,
                       const WindowID window_id,
                       HmiStatePtr state) OVERRIDE;

  void OnApplicationRegistered(
      ApplicationSharedPtr app,
      const mobile_apis::HMILevel::eType default_level) OVERRIDE;

  // EventObserver interface
  void HandleOnEvent(const event_engine::Event& event) OVERRIDE;
  void HandleOnEvent(const event_engine::MobileEvent& event) OVERRIDE;

  void OnAppWindowAdded(
      ApplicationSharedPtr app,
      const WindowID window_id,
      const mobile_apis::WindowType::eType window_type,
      const mobile_apis::HMILevel::eType default_level) OVERRIDE;

  void OnVideoStreamingStarted(ApplicationConstSharedPtr app) OVERRIDE;

  void OnVideoStreamingStopped(ApplicationConstSharedPtr app) OVERRIDE;

  void OnStateChanged(ApplicationSharedPtr app,
                      const WindowID window_id,
                      HmiStatePtr old_state,
                      HmiStatePtr new_state) OVERRIDE;

  bool IsStateActive(HmiState::StateID state_id) const OVERRIDE;

  void ActivateDefaultWindow(ApplicationSharedPtr app) OVERRIDE;
  void ExitDefaultWindow(ApplicationSharedPtr app) OVERRIDE;
  void DeactivateApp(ApplicationSharedPtr app,
                     const WindowID window_id) OVERRIDE;

  void ResumePostponedWindows(const uint32_t app_id) OVERRIDE;

  void DropPostponedWindows(const uint32_t app_id) OVERRIDE;

  PostponedActivationController& GetPostponedActivationController() OVERRIDE;

 private:
  void RequestHMIStateChange(ApplicationConstSharedPtr app,
                             HmiStatePtr resolved_state,
                             hmi_apis::Common_HMILevel::eType level,
                             bool send_policy_priority);

  /**
   * @brief The HmiLevelConflictResolver struct
   * Move other application to HmiStates if applied moved to FULL or LIMITED
   */
  struct HmiLevelConflictResolver {
    const ApplicationSharedPtr applied_;
    const WindowID window_id_;
    const HmiStatePtr state_;
    StateControllerImpl* state_ctrl_;
    HmiLevelConflictResolver(const ApplicationSharedPtr app,
                             const WindowID window_id,
                             const HmiStatePtr state,
                             StateControllerImpl* state_ctrl)
        : applied_(app)
        , window_id_(window_id)
        , state_(state)
        , state_ctrl_(state_ctrl) {}
    void operator()(ApplicationSharedPtr to_resolve);
  };

  template <typename UnaryFunction>
  void ForEachApplication(UnaryFunction func) const {
    ApplicationSet applications;
    {
      DataAccessor<ApplicationSet> accessor = app_mngr_.applications();
      applications = accessor.GetData();
    }

    for (const auto& app : applications) {
      func(app);
    }
  }

  /**
   * @brief ResolveHmiState Checks if requested hmi state is
   * allowed by current states context and correct it if it possible
   *
   * @param app application to apply state
   *
   * @param state state to be checked
   *
   * @return Resolved hmi state or empty pointer in case requested
   * hmi state is not allowed
   */
  HmiStatePtr ResolveHmiState(ApplicationSharedPtr app,
                              HmiStatePtr state) const;

  /**
   * @brief IsResumptionAllowed checks, if app is allowed to be resumed in
   * current state
   * @param app Application
   * @param state State to be checked
   * @return true, if app is allowed to be resumed, otherwise - false
   */
  bool IsResumptionAllowed(ApplicationSharedPtr app, HmiStatePtr state) const;

  /**
   * @brief GetAvailableHmiLevel Returns closest to requested
   * available hmi level for application
   *
   * @param app application to apply state
   *
   * @param hmi_level requested hmi level
   *
   * @return Resolved hmi state or empty pointer in case requested
   * hmi state is not allowed
   */
  mobile_apis::HMILevel::eType GetAvailableHmiLevel(
      ApplicationSharedPtr app, mobile_apis::HMILevel::eType hmi_level) const;

  /**
   * @brief IsStateAvailable Checks if hmi state is available
   * to apply for specified application
   *
   * @param app application to apply state
   *
   * @param state state to be checked
   *
   * @return true if state is available, false otherwise
   */
  bool IsStateAvailable(ApplicationSharedPtr app, HmiStatePtr state) const;

  /**
   * @brief IsStateAvailableForResumption Checks if hmi state is available
   * to apply for specified application during resumption
   *
   * @param app application to apply state
   *
   * @param state state to be checked
   *
   * @return true if state is available, false otherwise
   */
  bool IsStateAvailableForResumption(ApplicationSharedPtr app,
                                     HmiStatePtr state) const;

  /**
   * @brief ApplyPostponedStateForApp tries to apply postponed state
   * to application if it's allowed by current active states
   */
  void ApplyPostponedStateForApp(ApplicationSharedPtr app);

  /**
   * @brief IsTempStateActive Checks if specified temp state
   * is currently active
   *
   * @return true if state is active, false otherwise
   */
  bool IsTempStateActive(HmiState::StateID ID) const;

  /**
   *  Function to add new temporary HmiState for application
   */
  template <HmiState::StateID ID>
  void HMIStateStarted(ApplicationSharedPtr app) {
    DCHECK_OR_RETURN_VOID(app);
    const WindowIds window_ids = app->GetWindowIds();

    for (auto window_id : window_ids) {
      HmiStatePtr old_hmi_state = app->CurrentHmiState(window_id);
      HmiStatePtr new_hmi_state = CreateHmiState(app, ID);
      DCHECK_OR_RETURN_VOID(new_hmi_state);
      DCHECK_OR_RETURN_VOID(new_hmi_state->state_id() !=
                            HmiState::STATE_ID_REGULAR);
      new_hmi_state->set_parent(old_hmi_state);
      new_hmi_state->set_window_type(old_hmi_state->window_type());
      app->AddHMIState(window_id, new_hmi_state);
      OnStateChanged(app, window_id, old_hmi_state, new_hmi_state);
    }
  }

  /**
   * @brief TempStateStarted add HMI State ID in StateControllerImpl collection
   * @param ID state identifier
   */
  void TempStateStarted(HmiState::StateID ID);

  /**
   * @brief TempStateStopped remove HMI State ID from StateControllerImpl
   * collection
   * @param ID state identifier
   */
  void TempStateStopped(HmiState::StateID ID);

  /**
   * Function to remove temporary HmiState for application
   */
  template <HmiState::StateID ID>
  void HMIStateStopped(ApplicationSharedPtr app) {
    DCHECK_OR_RETURN_VOID(app);
    const WindowIds window_ids = app->GetWindowIds();

    for (auto window_id : window_ids) {
      HmiStatePtr cur = app->CurrentHmiState(window_id);
      HmiStatePtr old_hmi_state =
          CreateHmiState(app, HmiState::StateID::STATE_ID_REGULAR);
      DCHECK_OR_RETURN_VOID(old_hmi_state);
      old_hmi_state->set_hmi_level(cur->hmi_level());
      old_hmi_state->set_window_type(cur->window_type());
      old_hmi_state->set_audio_streaming_state(cur->audio_streaming_state());
      old_hmi_state->set_video_streaming_state(cur->video_streaming_state());
      old_hmi_state->set_system_context(cur->system_context());
      app->RemoveHMIState(window_id, ID);
      HmiStatePtr new_hmi_state = app->CurrentHmiState(window_id);
      OnStateChanged(app, window_id, old_hmi_state, new_hmi_state);
    }
  }

  /**
   * @brief ApplyRegularState setup regular hmi state, that will appear if no
   * specific events are active, without sending ActivateApp
   * @param app appication to setup default State
   * @param window_id id of applicaion's window to apply HMI state
   * @param state state of new defailt state
   */
  void ApplyRegularState(ApplicationSharedPtr app,
                         const WindowID window_id,
                         HmiStatePtr state);

  /**
   * @brief UpdateAppWindowsStreamingState updates all application windows
   * audio/video streaming state according to a new HMI state of the main window
   * @param app pointer to affected application
   * @param state pointer to state with the new streaming state of the main
   * window
   */
  void UpdateAppWindowsStreamingState(ApplicationSharedPtr app,
                                      HmiStatePtr state);

  /**
   * @brief SetupRegularHmiState set regular HMI State without
   * resolving conflicts and ActivateApp request
   * @param app application
   * @param window_id id of applicaion's window to apply HMI state
   * @param state hmi_state to setup
   */
  void SetupRegularHmiState(ApplicationSharedPtr app,
                            const WindowID window_id,
                            HmiStatePtr state);

  /**
   * @brief SetupRegularHmiState set regular HMI State without
   * resolving conflicts and ActivateApp request
   * @param app application
   * @param window_id id of applicaion's window to apply HMI state
   * @param hmi_level of new regular state
   * @param audio_state of new regular state
   * @param video_state of new regular state
   */
  void SetupRegularHmiState(
      ApplicationSharedPtr app,
      const WindowID window_id,
      const mobile_apis::HMILevel::eType hmi_level,
      const mobile_apis::AudioStreamingState::eType audio_state,
      const mobile_apis::VideoStreamingState::eType video_state);

  /**
   * @brief OnHMIResponse callback for activate app or close application
   * response
   * @param message Smart Object
   */
  void OnHMIResponse(const smart_objects::SmartObject& message);

  /**
   * @brief OnAppDeactivated callback for OnAppDeactivated notification
   * @param message Smart Object
   */
  void OnAppDeactivated(const smart_objects::SmartObject& message);

  /**
   * @brief OnAppActivated callback for OnAppActivated notification
   * @param message Smart Object
   */
  void OnAppActivated(const smart_objects::SmartObject& message);

  /**
   * @brief Apply temporary state
   */
  template <HmiState::StateID ID>
  void ApplyTempState() {
    ForEachApplication(std::bind1st(
        std::mem_fun(&StateControllerImpl::HMIStateStarted<ID>), this));
    TempStateStarted(ID);
  }

  /**
   * @brief Cancel temporary state
   */
  template <HmiState::StateID ID>
  void CancelTempState() {
    ForEachApplication(std::bind1st(
        std::mem_fun(&StateControllerImpl::HMIStateStopped<ID>), this));
    TempStateStopped(ID);
  }

  /**
   * @brief CreateHmiState creates Hmi state according to state_id
   * @param app_id application ID
   * @param state_id state id
   * @return
   */
  HmiStatePtr CreateHmiState(std::shared_ptr<Application> app,
                             HmiState::StateID state_id) const;

  /**
   * @brief Determines AudioStreamingState value
   * for application with HMI level specified.
   * @param app an application to calculate for
   * @param hmi_level HMI level
   * @return AudioStreamingState value
   */
  mobile_apis::AudioStreamingState::eType CalcAudioState(
      ApplicationSharedPtr app,
      const mobile_apis::HMILevel::eType hmi_level) const;

  /**
   * @brief Determines VideoStreamingState value
   * for application with HMI level specified.
   * @param app an application to calculate for
   * @param hmi_level HMI level
   * @return VideoStreamingState value
   */
  mobile_apis::VideoStreamingState::eType CalcVideoState(
      ApplicationSharedPtr app,
      const mobile_apis::HMILevel::eType hmi_level) const;

  typedef std::list<HmiState::StateID> StateIDList;
  StateIDList active_states_;
  mutable sync_primitives::Lock active_states_lock_;
  std::map<uint32_t, HmiStatePtr> waiting_for_response_;

  typedef std::pair<WindowID, HmiStatePtr> WindowStatePair;
  typedef std::list<WindowStatePair> WindowStatePairs;
  std::map<uint32_t, WindowStatePairs> postponed_app_widgets_;

  std::unordered_set<uint32_t> apps_with_pending_hmistatus_notification_;
  mutable sync_primitives::Lock apps_with_pending_hmistatus_notification_lock_;
  ApplicationManager& app_mngr_;
  PostponedActivationController postponed_activation_controller_;
};
}  // namespace application_manager

#endif  // SRC_COMPONENTS_APPLICATION_MANAGER_INCLUDE_APPLICATION_MANAGER_STATE_CONTROLLER_IMPL_H_