1 /*
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "entities/flashlight_entity.h"
17
18 #include <cinttypes>
19
20 #include "battery_stats_service.h"
21 #include "stats_log.h"
22
23 namespace OHOS {
24 namespace PowerMgr {
25 namespace {
26 auto g_statsService = DelayedStatsSpSingleton<BatteryStatsService>::GetInstance();
27 }
28
FlashlightEntity()29 FlashlightEntity::FlashlightEntity()
30 {
31 consumptionType_ = BatteryStatsInfo::CONSUMPTION_TYPE_FLASHLIGHT;
32 }
33
GetActiveTimeMs(int32_t uid,StatsUtils::StatsType statsType,int16_t level)34 int64_t FlashlightEntity::GetActiveTimeMs(int32_t uid, StatsUtils::StatsType statsType, int16_t level)
35 {
36 int64_t activeTimeMs = StatsUtils::DEFAULT_VALUE;
37 switch (statsType) {
38 case StatsUtils::STATS_TYPE_FLASHLIGHT_ON: {
39 auto iter = flashlightTimerMap_.find(uid);
40 if (iter != flashlightTimerMap_.end()) {
41 activeTimeMs = iter->second->GetRunningTimeMs();
42 STATS_HILOGD(COMP_SVC, "Get flashlight on time: %{public}" PRId64 "ms for uid: %{public}d",
43 activeTimeMs, uid);
44 break;
45 }
46 STATS_HILOGD(COMP_SVC, "Didn't find related timer for uid: %{public}d, return 0", uid);
47 break;
48 }
49 default:
50 break;
51 }
52 return activeTimeMs;
53 }
54
Calculate(int32_t uid)55 void FlashlightEntity::Calculate(int32_t uid)
56 {
57 auto flashlightOnAverageMa =
58 g_statsService->GetBatteryStatsParser()->GetAveragePowerMa(StatsUtils::CURRENT_FLASHLIGHT_ON);
59 auto flashlightOnTimeMs = GetActiveTimeMs(uid, StatsUtils::STATS_TYPE_FLASHLIGHT_ON);
60 auto flashlightOnPowerMah = flashlightOnAverageMa * flashlightOnTimeMs / StatsUtils::MS_IN_HOUR;
61 auto iter = flashlightPowerMap_.find(uid);
62 if (iter != flashlightPowerMap_.end()) {
63 STATS_HILOGD(COMP_SVC, "Update flashlight on power consumption: %{public}lfmAh for uid: %{public}d",
64 flashlightOnAverageMa, uid);
65 iter->second = flashlightOnPowerMah;
66 } else {
67 STATS_HILOGD(COMP_SVC, "Create flashlight on power consumption: %{public}lfmAh for uid: %{public}d",
68 flashlightOnAverageMa, uid);
69 flashlightPowerMap_.insert(std::pair<int32_t, double>(uid, flashlightOnPowerMah));
70 }
71 }
72
GetEntityPowerMah(int32_t uidOrUserId)73 double FlashlightEntity::GetEntityPowerMah(int32_t uidOrUserId)
74 {
75 double power = StatsUtils::DEFAULT_VALUE;
76 auto iter = flashlightPowerMap_.find(uidOrUserId);
77 if (iter != flashlightPowerMap_.end()) {
78 power = iter->second;
79 STATS_HILOGD(COMP_SVC, "Get app flashlight power consumption: %{public}lfmAh for uid: %{public}d",
80 power, uidOrUserId);
81 } else {
82 STATS_HILOGD(COMP_SVC,
83 "No app flashlight power consumption related to uid: %{public}d was found, return 0", uidOrUserId);
84 }
85 return power;
86 }
87
GetStatsPowerMah(StatsUtils::StatsType statsType,int32_t uid)88 double FlashlightEntity::GetStatsPowerMah(StatsUtils::StatsType statsType, int32_t uid)
89 {
90 double power = StatsUtils::DEFAULT_VALUE;
91 if (statsType == StatsUtils::STATS_TYPE_FLASHLIGHT_ON) {
92 auto flashlightOnIter = flashlightPowerMap_.find(uid);
93 if (flashlightOnIter != flashlightPowerMap_.end()) {
94 power = flashlightOnIter->second;
95 STATS_HILOGD(COMP_SVC,
96 "Get flashlight on power consumption: %{public}lfmAh for uid: %{public}d", power, uid);
97 } else {
98 STATS_HILOGD(COMP_SVC,
99 "No flashlight on power consumption related to uid: %{public}d was found, return 0", uid);
100 }
101 }
102 return power;
103 }
104
GetOrCreateTimer(int32_t uid,StatsUtils::StatsType statsType,int16_t level)105 std::shared_ptr<StatsHelper::ActiveTimer> FlashlightEntity::GetOrCreateTimer(int32_t uid,
106 StatsUtils::StatsType statsType, int16_t level)
107 {
108 std::shared_ptr<StatsHelper::ActiveTimer> timer = nullptr;
109 switch (statsType) {
110 case StatsUtils::STATS_TYPE_FLASHLIGHT_ON: {
111 auto flashlightOnIter = flashlightTimerMap_.find(uid);
112 if (flashlightOnIter != flashlightTimerMap_.end()) {
113 STATS_HILOGD(COMP_SVC, "Get flashlight on timer for uid: %{public}d", uid);
114 timer = flashlightOnIter->second;
115 break;
116 }
117 STATS_HILOGD(COMP_SVC, "Create flashlight on timer for uid: %{public}d", uid);
118 std::shared_ptr<StatsHelper::ActiveTimer> flashTimer = std::make_shared<StatsHelper::ActiveTimer>();
119 flashlightTimerMap_.insert(
120 std::pair<int32_t, std::shared_ptr<StatsHelper::ActiveTimer>>(uid, flashTimer));
121 timer = flashTimer;
122 break;
123 }
124 default:
125 STATS_HILOGW(COMP_SVC, "Create active timer failed");
126 break;
127 }
128 return timer;
129 }
130
Reset()131 void FlashlightEntity::Reset()
132 {
133 // Reset app Flashlight on total power consumption
134 for (auto& iter : flashlightPowerMap_) {
135 iter.second = StatsUtils::DEFAULT_VALUE;
136 }
137
138 // Reset Flashlight on timer
139 for (auto& iter : flashlightTimerMap_) {
140 if (iter.second) {
141 iter.second->Reset();
142 }
143 }
144 }
145 } // namespace PowerMgr
146 } // namespace OHOS