APPLINK-6400:
[profile/ivi/smartdevicelink.git] / src / components / application_manager / src / usage_statistics.cc
1 /**
2  * Copyright (c) 2014, Ford Motor Company
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * Redistributions of source code must retain the above copyright notice, this
9  * list of conditions and the following disclaimer.
10  *
11  * Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following
13  * disclaimer in the documentation and/or other materials provided with the
14  * distribution.
15  *
16  * Neither the name of the Ford Motor Company nor the names of its contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include "application_manager/usage_statistics.h"
34
35 #include "smart_objects/smart_object.h"
36 #include "smart_objects/enum_schema_item.h"
37 #include "usage_statistics/statistics_manager.h"
38 #include "utils/macro.h"
39
40 using namespace mobile_apis;
41 using namespace NsSmartDeviceLink::NsSmartObjects;
42 using namespace usage_statistics;
43
44 namespace application_manager {
45
46 namespace {
47
48 std::string LanguageIdToString(Language::eType lang_id) {
49   typedef std::map<Language::eType, std::string> EnumMap;
50   const EnumMap& enum_map =
51       TEnumSchemaItem<Language::eType>::getEnumElementsStringRepresentation();
52   EnumMap::const_iterator found = enum_map.find(lang_id);
53   if (found != enum_map.end()) {
54     return found->second;
55   } else {
56     return "unknown";
57   }
58 }
59
60 }  // namespace
61
62 UsageStatistics::UsageStatistics(
63     const std::string& app_id,
64     usage_statistics::StatisticsManager* statistics_manager)
65     : time_in_hmi_state_(statistics_manager, app_id),
66       app_registration_language_gui_(statistics_manager, app_id, LANGUAGE_GUI),
67       app_registration_language_vui_(statistics_manager, app_id, LANGUAGE_VUI),
68       count_of_rejected_rpc_calls_(statistics_manager, app_id,
69                                    REJECTED_RPC_CALLS),
70       count_of_rpcs_sent_in_hmi_none_(statistics_manager, app_id,
71                                       RPCS_IN_HMI_NONE),
72       count_of_user_selections_(statistics_manager, app_id, USER_SELECTIONS),
73       count_of_run_attempts_while_revoked_(statistics_manager, app_id,
74                                            RUN_ATTEMPTS_WHILE_REVOKED),
75       count_of_removals_for_bad_behavior_(statistics_manager, app_id,
76                                           REMOVALS_MISBEHAVED) {
77   time_in_hmi_state_.Start(SECONDS_HMI_NONE);
78 }
79
80 void UsageStatistics::RecordHmiStateChanged(HMILevel::eType new_hmi_level) {
81   using namespace mobile_apis::HMILevel;
82   AppStopwatchId next_stopwatch = SECONDS_HMI_NONE;
83   switch (new_hmi_level) {
84     case HMI_FULL:
85       next_stopwatch = SECONDS_HMI_FULL;
86       break;
87     case HMI_LIMITED:
88       next_stopwatch = SECONDS_HMI_LIMITED;
89       break;
90     case HMI_BACKGROUND:
91       next_stopwatch = SECONDS_HMI_BACKGROUND;
92       break;
93     case HMI_NONE:
94       next_stopwatch = SECONDS_HMI_NONE;
95       break;
96     default:
97       NOTREACHED()
98       ;
99   }
100   time_in_hmi_state_.Switch(next_stopwatch);
101 }
102
103 void UsageStatistics::RecordAppRegistrationGuiLanguage(
104     Language::eType gui_language) {
105   app_registration_language_gui_.Update(LanguageIdToString(gui_language));
106 }
107
108 void UsageStatistics::RecordAppRegistrationVuiLanguage(
109     Language::eType vui_language) {
110   app_registration_language_gui_.Update(LanguageIdToString(vui_language));
111 }
112
113 void UsageStatistics::RecordRpcSentInHMINone() {
114   ++count_of_rpcs_sent_in_hmi_none_;
115 }
116
117 void UsageStatistics::RecordPolicyRejectedRpcCall() {
118   ++count_of_rejected_rpc_calls_;
119 }
120
121 void UsageStatistics::RecordAppUserSelection() {
122   ++count_of_user_selections_;
123 }
124
125 void UsageStatistics::RecordRunAttemptsWhileRevoked() {
126   ++count_of_run_attempts_while_revoked_;
127 }
128
129 void UsageStatistics::RecordRemovalsForBadBehavior() {
130   ++count_of_removals_for_bad_behavior_;
131 }
132
133 }  // namespace application_manager