summaryrefslogtreecommitdiff
path: root/src/components/application_manager/src/resumption/resumption_data_json.cc
blob: a2b99b4ac12281b8545a1a8386ada8b76db7c88b (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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/*
 * Copyright (c) 2017, 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/resumption/resumption_data_json.h"
#include "application_manager/application_manager.h"
#include "application_manager/application_manager_settings.h"
#include "application_manager/message_helper.h"
#include "application_manager/smart_object_keys.h"
#include "formatters/CFormatterJsonBase.h"
#include "json/json.h"
#include "smart_objects/smart_object.h"

namespace resumption {

namespace formatters = ns_smart_device_link::ns_json_handler::formatters;

SDL_CREATE_LOG_VARIABLE("Resumption")

ResumptionDataJson::ResumptionDataJson(
    resumption::LastStateWrapperPtr last_state_wrapper,
    const application_manager::ApplicationManager& application_manager)
    : ResumptionData(application_manager)
    , last_state_wrapper_(last_state_wrapper) {}

void ResumptionDataJson::SaveApplication(
    app_mngr::ApplicationSharedPtr application) {
  using namespace app_mngr;
  SDL_LOG_AUTO_TRACE();
  DCHECK_OR_RETURN_VOID(application);

  const std::string& policy_app_id = application->policy_app_id();
  SDL_LOG_DEBUG("app_id : " << application->app_id()
                            << " policy_app_id : " << policy_app_id);
  const std::string hash = application->curHash();
  const uint32_t grammar_id = application->get_grammar_id();
  const int64_t time_stamp = (int64_t)time(NULL);
  const std::string device_mac = application->mac_address();
  const mobile_apis::HMILevel::eType hmi_level =
      application->hmi_level(mobile_apis::PredefinedWindows::DEFAULT_WINDOW);
  const bool is_subscribed_for_way_points =
      application_manager_.IsAppSubscribedForWayPoints(*application);

  Json::Value tmp;
  resumption::LastStateAccessor accessor = last_state_wrapper_->get_accessor();
  Json::Value dictionary = accessor.GetData().dictionary();
  Json::Value& json_app =
      GetFromSavedOrAppend(policy_app_id, device_mac, dictionary);

  json_app[strings::device_id] = device_mac;
  json_app[strings::app_id] = policy_app_id;
  json_app[strings::grammar_id] = grammar_id;
  json_app[strings::connection_key] = application->app_id();
  json_app[strings::hmi_app_id] = application->hmi_app_id();
  json_app[strings::is_media_application] = application->IsAudioApplication();
  json_app[strings::hmi_level] = static_cast<int32_t>(hmi_level);
  json_app[strings::ign_off_count] = 0;
  json_app[strings::hash_id] = hash;
  formatters::CFormatterJsonBase::objToJsonValue(
      GetApplicationCommands(application), tmp);
  json_app[strings::application_commands] = tmp;
  formatters::CFormatterJsonBase::objToJsonValue(
      GetApplicationSubMenus(application), tmp);
  json_app[strings::application_submenus] = tmp;
  formatters::CFormatterJsonBase::objToJsonValue(
      GetApplicationInteractionChoiseSets(application), tmp);
  json_app[strings::application_choice_sets] = tmp;
  formatters::CFormatterJsonBase::objToJsonValue(
      GetApplicationGlobalProperties(application), tmp);
  json_app[strings::application_global_properties] = tmp;
  formatters::CFormatterJsonBase::objToJsonValue(
      GetApplicationSubscriptions(application), tmp);
  json_app[strings::application_subscriptions] = tmp;
  formatters::CFormatterJsonBase::objToJsonValue(
      GetApplicationFiles(application), tmp);
  json_app[strings::application_files] = tmp;
  formatters::CFormatterJsonBase::objToJsonValue(
      GetApplicationWidgetsInfo(application), tmp);
  json_app[strings::windows_info] = tmp;
  json_app[strings::time_stamp] = time_stamp;
  json_app[strings::subscribed_for_way_points] = is_subscribed_for_way_points;
  formatters::CFormatterJsonBase::objToJsonValue(
      application->get_user_location(), tmp);
  json_app[strings::user_location] = tmp;

  accessor.GetMutableData().set_dictionary(dictionary);
  SDL_LOG_DEBUG("SaveApplication : " << json_app.toStyledString());
}

bool ResumptionDataJson::IsHMIApplicationIdExist(uint32_t hmi_app_id) const {
  using namespace app_mngr;
  SDL_LOG_AUTO_TRACE();

  resumption::LastStateAccessor accessor = last_state_wrapper_->get_accessor();
  Json::Value dictionary = accessor.GetData().dictionary();
  const Json::Value& saved_apps = GetSavedApplications(dictionary);

  for (const auto& saved_app : saved_apps) {
    if (saved_app.isMember(strings::hmi_app_id)) {
      if (saved_app[strings::hmi_app_id].asUInt() == hmi_app_id) {
        return true;
      }
    }
  }
  return false;
}

uint32_t ResumptionDataJson::GetHMIApplicationID(
    const std::string& policy_app_id, const std::string& device_id) const {
  using namespace app_mngr;
  SDL_LOG_AUTO_TRACE();

  uint32_t hmi_app_id = 0;

  const int idx = GetObjectIndex(policy_app_id, device_id);
  if (-1 == idx) {
    SDL_LOG_WARN("Application not saved");
    return hmi_app_id;
  }
  resumption::LastStateAccessor accessor = last_state_wrapper_->get_accessor();
  Json::Value dictionary = accessor.GetData().dictionary();
  const Json::Value& json_app = GetSavedApplications(dictionary)[idx];
  if (json_app.isMember(strings::app_id) &&
      json_app.isMember(strings::device_id)) {
    hmi_app_id = json_app[strings::hmi_app_id].asUInt();
  }
  SDL_LOG_DEBUG("hmi_app_id :" << hmi_app_id);
  return hmi_app_id;
}

void ResumptionDataJson::IncrementIgnOffCount() {
  using namespace app_mngr;
  SDL_LOG_AUTO_TRACE();

  Json::Value to_save = Json::arrayValue;
  Json::Value dictionary;
  resumption::LastStateAccessor accessor = last_state_wrapper_->get_accessor();
  dictionary = accessor.GetData().dictionary();
  Json::Value& saved_apps = GetSavedApplications(dictionary);
  for (auto& json_app : saved_apps) {
    if (json_app.isMember(strings::ign_off_count)) {
      Json::Value& ign_off_count = json_app[strings::ign_off_count];
      const uint32_t counter_value = ign_off_count.asUInt();
      ign_off_count = counter_value + 1;
    } else {
      SDL_LOG_WARN("Unknown key among saved applications");
      Json::Value& ign_off_count = json_app[strings::ign_off_count];
      ign_off_count = 1;
    }
    to_save.append(json_app);
  }
  SetSavedApplication(to_save, dictionary);
  SetLastIgnOffTime(time(nullptr), dictionary);
  accessor.GetMutableData().set_dictionary(dictionary);
  SDL_LOG_DEBUG(GetResumptionData(dictionary).toStyledString());
}

void ResumptionDataJson::DecrementIgnOffCount() {
  using namespace app_mngr;
  SDL_LOG_AUTO_TRACE();

  resumption::LastStateAccessor accessor = last_state_wrapper_->get_accessor();
  Json::Value dictionary = accessor.GetData().dictionary();
  Json::Value& saved_apps = GetSavedApplications(dictionary);
  for (auto& saved_app : saved_apps) {
    if (saved_app.isMember(strings::ign_off_count)) {
      const uint32_t ign_off_count = saved_app[strings::ign_off_count].asUInt();
      if (0 == ign_off_count) {
        SDL_LOG_WARN("Application has not been suspended");
      } else {
        saved_app[strings::ign_off_count] = ign_off_count - 1;
      }
    } else {
      SDL_LOG_WARN("Unknown key among saved applications");
      saved_app[strings::ign_off_count] = 0;
    }
  }
  accessor.GetMutableData().set_dictionary(dictionary);
}

bool ResumptionDataJson::GetHashId(const std::string& policy_app_id,
                                   const std::string& device_id,
                                   std::string& hash_id) const {
  using namespace app_mngr;
  SDL_LOG_AUTO_TRACE();

  const int idx = GetObjectIndex(policy_app_id, device_id);
  if (-1 == idx) {
    SDL_LOG_WARN("Application not saved");
    return false;
  }

  resumption::LastStateAccessor accessor = last_state_wrapper_->get_accessor();
  Json::Value dictionary = accessor.GetData().dictionary();
  const Json::Value& json_app = GetSavedApplications(dictionary)[idx];
  SDL_LOG_DEBUG("Saved_application_data: " << json_app.toStyledString());
  if (json_app.isMember(strings::hash_id) &&
      json_app.isMember(strings::time_stamp)) {
    hash_id = json_app[strings::hash_id].asString();
    return true;
  }
  SDL_LOG_WARN("There are some unknown keys in the dictionary.");
  return false;
}

bool ResumptionDataJson::GetSavedApplication(
    const std::string& policy_app_id,
    const std::string& device_id,
    smart_objects::SmartObject& saved_app) const {
  using namespace app_mngr;
  SDL_LOG_AUTO_TRACE();

  const int idx = GetObjectIndex(policy_app_id, device_id);
  if (-1 == idx) {
    return false;
  }
  resumption::LastStateAccessor accessor = last_state_wrapper_->get_accessor();
  Json::Value dictionary = accessor.GetData().dictionary();
  const Json::Value& json_saved_app = GetSavedApplications(dictionary)[idx];
  formatters::CFormatterJsonBase::jsonValueToObj(json_saved_app, saved_app);

  return true;
}

bool ResumptionDataJson::RemoveApplicationFromSaved(
    const std::string& policy_app_id, const std::string& device_id) {
  using namespace app_mngr;
  SDL_LOG_AUTO_TRACE();

  bool result = false;
  std::vector<Json::Value> temp;
  resumption::LastStateAccessor accessor = last_state_wrapper_->get_accessor();
  Json::Value dictionary = accessor.GetData().dictionary();
  Json::Value& saved_apps = GetSavedApplications(dictionary);
  for (auto& app : saved_apps) {
    if (app.isMember(strings::app_id) && app.isMember(strings::device_id)) {
      const std::string& saved_policy_app_id = app[strings::app_id].asString();
      const std::string& saved_device_id = app[strings::device_id].asString();
      if (saved_policy_app_id == policy_app_id &&
          saved_device_id == device_id) {
        result = true;
      } else {
        temp.push_back(app);
      }
    }
  }

  if (false == result) {
    SDL_LOG_TRACE("EXIT result: " << (result ? "true" : "false"));
    accessor.GetMutableData().set_dictionary(dictionary);
    return result;
  }

  GetSavedApplications(dictionary).clear();
  for (auto& app : temp) {
    GetSavedApplications(dictionary).append(app);
  }
  SDL_LOG_TRACE("EXIT result: " << (result ? "true" : "false"));
  accessor.GetMutableData().set_dictionary(dictionary);
  return result;
}

int64_t ResumptionDataJson::GetIgnOffTime() const {
  using namespace app_mngr;
  SDL_LOG_AUTO_TRACE();

  resumption::LastStateAccessor accessor = last_state_wrapper_->get_accessor();
  Json::Value dictionary = accessor.GetData().dictionary();
  Json::Value& resumption = GetResumptionData(dictionary);
  if (!resumption.isMember(strings::last_ign_off_time)) {
    resumption[strings::last_ign_off_time] = 0;
    accessor.GetMutableData().set_dictionary(dictionary);
    SDL_LOG_WARN("last_save_time section is missed");
  }
  return resumption[strings::last_ign_off_time].asInt64();
}

uint32_t ResumptionDataJson::GetGlobalIgnOnCounter() const {
  using namespace app_mngr;
  SDL_LOG_AUTO_TRACE();

  resumption::LastStateAccessor accessor = last_state_wrapper_->get_accessor();
  Json::Value dictionary = accessor.GetData().dictionary();
  Json::Value& resumption = GetResumptionData(dictionary);
  if (resumption.isMember(strings::global_ign_on_counter)) {
    const uint32_t global_ign_on_counter =
        resumption[strings::global_ign_on_counter].asUInt();
    SDL_LOG_DEBUG("Global Ign On Counter = " << global_ign_on_counter);
    return global_ign_on_counter;
  }
  return 1;
}

void ResumptionDataJson::IncrementGlobalIgnOnCounter() {
  using namespace app_mngr;
  SDL_LOG_AUTO_TRACE();

  resumption::LastStateAccessor accessor = last_state_wrapper_->get_accessor();
  Json::Value dictionary = accessor.GetData().dictionary();
  Json::Value& resumption = GetResumptionData(dictionary);
  if (resumption.isMember(strings::global_ign_on_counter)) {
    const uint32_t global_ign_on_counter =
        resumption[strings::global_ign_on_counter].asUInt();
    SDL_LOG_DEBUG(
        "Global IGN ON counter in resumption data: " << global_ign_on_counter);
    resumption[strings::global_ign_on_counter] = global_ign_on_counter + 1;
    SDL_LOG_DEBUG("Global IGN ON counter new value: "
                  << resumption[strings::global_ign_on_counter].asUInt());
  } else {
    resumption[strings::global_ign_on_counter] = 1;
  }
  accessor.GetMutableData().set_dictionary(dictionary);
  accessor.GetMutableData().SaveToFileSystem();
}

void ResumptionDataJson::ResetGlobalIgnOnCount() {
  using namespace app_mngr;
  SDL_LOG_AUTO_TRACE();

  resumption::LastStateAccessor accessor = last_state_wrapper_->get_accessor();
  Json::Value dictionary = accessor.GetData().dictionary();
  Json::Value& resumption = GetResumptionData(dictionary);

  resumption[strings::global_ign_on_counter] = 0;
  accessor.GetMutableData().set_dictionary(dictionary);
  SDL_LOG_DEBUG("Global IGN ON counter resetting");
}

ssize_t ResumptionDataJson::IsApplicationSaved(
    const std::string& policy_app_id, const std::string& device_id) const {
  SDL_LOG_AUTO_TRACE();

  return GetObjectIndex(policy_app_id, device_id);
}

Json::Value& ResumptionDataJson::GetFromSavedOrAppend(
    const std::string& policy_app_id,
    const std::string& device_id,
    Json::Value& dictionary) const {
  using namespace app_mngr;
  SDL_LOG_AUTO_TRACE();

  Json::Value& saved_apps = GetSavedApplications(dictionary);
  for (auto& saved_app : saved_apps) {
    if (device_id == saved_app[strings::device_id].asString() &&
        policy_app_id == saved_app[strings::app_id].asString()) {
      return saved_app;
    }
  }

  return GetSavedApplications(dictionary).append(Json::Value());
}

void ResumptionDataJson::GetDataForLoadResumeData(
    smart_objects::SmartObject& saved_data) const {
  using namespace app_mngr;
  SDL_LOG_AUTO_TRACE();

  smart_objects::SmartObject so_array_data(smart_objects::SmartType_Array);
  int i = 0;
  resumption::LastStateAccessor accessor = last_state_wrapper_->get_accessor();
  Json::Value dictionary = accessor.GetData().dictionary();
  Json::Value& saved_apps = GetSavedApplications(dictionary);

  for (auto& saved_app : saved_apps) {
    if ((saved_app.isMember(strings::hmi_level)) &&
        (saved_app.isMember(strings::ign_off_count)) &&
        (saved_app.isMember(strings::time_stamp)) &&
        (saved_app.isMember(strings::app_id)) &&
        (saved_app.isMember(strings::device_id))) {
      smart_objects::SmartObject so(smart_objects::SmartType_Map);
      so[strings::hmi_level] = saved_app[strings::hmi_level].asInt();
      so[strings::ign_off_count] = saved_app[strings::ign_off_count].asInt();
      so[strings::time_stamp] = saved_app[strings::time_stamp].asInt64();
      so[strings::app_id] = saved_app[strings::app_id].asString();
      so[strings::device_id] = saved_app[strings::device_id].asString();
      so_array_data[i++] = so;
    }
  }
  saved_data = so_array_data;
}

ResumptionDataJson::~ResumptionDataJson() {
  // TODO Probably Save Data on disk
}

void ResumptionDataJson::UpdateHmiLevel(
    const std::string& policy_app_id,
    const std::string& device_id,
    mobile_apis::HMILevel::eType hmi_level) {
  SDL_LOG_AUTO_TRACE();
  using namespace app_mngr;

  int idx = GetObjectIndex(policy_app_id, device_id);
  if (-1 == idx) {
    SDL_LOG_WARN("Application isn't saved with mobile_app_id = "
                 << policy_app_id << " device_id = " << device_id);
    return;
  }
  resumption::LastStateAccessor accessor = last_state_wrapper_->get_accessor();
  Json::Value dictionary = accessor.GetData().dictionary();
  GetSavedApplications(dictionary)[idx][strings::hmi_level] = hmi_level;
  accessor.GetMutableData().set_dictionary(dictionary);
}

Json::Value& ResumptionDataJson::GetSavedApplications(
    Json::Value& dictionary) const {
  using namespace app_mngr;
  SDL_LOG_AUTO_TRACE();

  Json::Value& resumption = GetResumptionData(dictionary);
  if (!resumption.isMember(strings::resume_app_list)) {
    resumption[strings::resume_app_list] = Json::Value(Json::arrayValue);
    SDL_LOG_WARN("app_list section is missed");
  }
  Json::Value& resume_app_list = resumption[strings::resume_app_list];
  if (!resume_app_list.isArray()) {
    SDL_LOG_ERROR("resume_app_list type INVALID rewrite");
    resume_app_list = Json::Value(Json::arrayValue);
  }
  return resume_app_list;
}

Json::Value& ResumptionDataJson::GetResumptionData(
    Json::Value& dictionary) const {
  using namespace app_mngr;
  SDL_LOG_AUTO_TRACE();

  if (!dictionary.isMember(strings::resumption)) {
    dictionary[strings::resumption] = Json::Value(Json::objectValue);
    SDL_LOG_WARN("resumption section is missed");
  }
  Json::Value& resumption = dictionary[strings::resumption];
  if (!resumption.isObject()) {
    SDL_LOG_ERROR("resumption type INVALID rewrite");
    resumption = Json::Value(Json::objectValue);
  }
  return resumption;
}

ssize_t ResumptionDataJson::GetObjectIndex(const std::string& policy_app_id,
                                           const std::string& device_id) const {
  using namespace app_mngr;
  SDL_LOG_AUTO_TRACE();

  resumption::LastStateAccessor accessor = last_state_wrapper_->get_accessor();
  Json::Value dictionary = accessor.GetData().dictionary();
  const Json::Value& apps = GetSavedApplications(dictionary);
  const Json::ArrayIndex size = apps.size();
  Json::ArrayIndex idx = 0;
  for (; idx != size; ++idx) {
    if (apps[idx].isMember(strings::app_id) &&
        apps[idx].isMember(strings::device_id)) {
      const std::string& saved_app_id = apps[idx][strings::app_id].asString();
      const std::string& saved_device_id =
          apps[idx][strings::device_id].asString();
      if (device_id == saved_device_id && policy_app_id == saved_app_id) {
        SDL_LOG_DEBUG("Found " << idx);
        return idx;
      }
    }
  }
  return -1;
}

bool ResumptionDataJson::IsResumptionDataValid(uint32_t index) const {
  using namespace app_mngr;
  SDL_LOG_AUTO_TRACE();

  resumption::LastStateAccessor accessor = last_state_wrapper_->get_accessor();
  Json::Value dictionary = accessor.GetData().dictionary();
  const Json::Value& json_app = GetSavedApplications(dictionary)[index];
  if (!json_app.isMember(strings::app_id) ||
      !json_app.isMember(strings::ign_off_count) ||
      !json_app.isMember(strings::hmi_level) ||
      !json_app.isMember(strings::hmi_app_id) ||
      !json_app.isMember(strings::time_stamp) ||
      !json_app.isMember(strings::device_id)) {
    SDL_LOG_ERROR("Wrong resumption data");
    return false;
  }

  if (json_app.isMember(strings::hmi_app_id) &&
      0 >= json_app[strings::hmi_app_id].asUInt()) {
    SDL_LOG_ERROR("Wrong resumption hmi app ID");
    return false;
  }

  return true;
}

void ResumptionDataJson::SetSavedApplication(Json::Value& apps_json,
                                             Json::Value& dictionary) {
  SDL_LOG_AUTO_TRACE();

  Json::Value& app_list = GetSavedApplications(dictionary);
  app_list = apps_json;
}

void ResumptionDataJson::SetLastIgnOffTime(time_t ign_off_time,
                                           Json::Value& dictionary) {
  using namespace app_mngr;
  SDL_LOG_AUTO_TRACE();

  SDL_LOG_WARN("ign_off_time = " << ign_off_time);
  Json::Value& resumption = GetResumptionData(dictionary);
  resumption[strings::last_ign_off_time] = static_cast<int64_t>(ign_off_time);
}

bool ResumptionDataJson::Init() {
  SDL_LOG_AUTO_TRACE();
  return true;
}

bool ResumptionDataJson::DropAppDataResumption(const std::string& device_id,
                                               const std::string& app_id) {
  SDL_LOG_AUTO_TRACE();
  using namespace app_mngr;

  resumption::LastStateAccessor accessor = last_state_wrapper_->get_accessor();
  Json::Value dictionary = accessor.GetData().dictionary();
  Json::Value& application =
      GetFromSavedOrAppend(app_id, device_id, dictionary);
  if (application.isNull()) {
    SDL_LOG_DEBUG("Application " << app_id << " with device_id " << device_id
                                 << " hasn't been found in resumption data.");
    return false;
  }
  application[strings::application_commands].clear();
  application[strings::application_submenus].clear();
  application[strings::application_choice_sets].clear();
  application[strings::application_global_properties].clear();
  application[strings::application_subscriptions].clear();
  application[strings::application_files].clear();
  application[strings::user_location].clear();
  application.removeMember(strings::grammar_id);
  accessor.GetMutableData().set_dictionary(dictionary);
  SDL_LOG_DEBUG("Resumption data for application "
                << app_id << " with device_id " << device_id
                << " has been dropped.");
  return true;
}

void ResumptionDataJson::Persist() {
  last_state_wrapper_->get_accessor().GetMutableData().SaveToFileSystem();
}

}  // namespace resumption