1 /*
2  * Copyright (c) 2021-2023 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 }
27 
FlashlightEntity()28 FlashlightEntity::FlashlightEntity()
29 {
30     consumptionType_ = BatteryStatsInfo::CONSUMPTION_TYPE_FLASHLIGHT;
31 }
32 
GetActiveTimeMs(int32_t uid,StatsUtils::StatsType statsType,int16_t level)33 int64_t FlashlightEntity::GetActiveTimeMs(int32_t uid, StatsUtils::StatsType statsType, int16_t level)
34 {
35     int64_t activeTimeMs = StatsUtils::DEFAULT_VALUE;
36     if (statsType != StatsUtils::STATS_TYPE_FLASHLIGHT_ON) {
37         return activeTimeMs;
38     }
39 
40     auto iter = flashlightTimerMap_.find(uid);
41     if (iter != flashlightTimerMap_.end()) {
42         activeTimeMs = iter->second->GetRunningTimeMs();
43         STATS_HILOGD(COMP_SVC, "Get flashlight on time: %{public}" PRId64 "ms for uid: %{public}d", activeTimeMs, uid);
44         return activeTimeMs;
45     }
46     STATS_HILOGD(COMP_SVC, "Didn't find related timer for uid: %{public}d, return 0", uid);
47     return activeTimeMs;
48 }
49 
Calculate(int32_t uid)50 void FlashlightEntity::Calculate(int32_t uid)
51 {
52     auto bss = BatteryStatsService::GetInstance();
53     auto flashlightOnAverageMa =
54         bss->GetBatteryStatsParser()->GetAveragePowerMa(StatsUtils::CURRENT_FLASHLIGHT_ON);
55     auto flashlightOnTimeMs = GetActiveTimeMs(uid, StatsUtils::STATS_TYPE_FLASHLIGHT_ON);
56     auto flashlightOnPowerMah = flashlightOnAverageMa * flashlightOnTimeMs / StatsUtils::MS_IN_HOUR;
57     auto iter = flashlightPowerMap_.find(uid);
58     if (iter != flashlightPowerMap_.end()) {
59         STATS_HILOGD(COMP_SVC, "Update flashlight on power consumption: %{public}lfmAh for uid: %{public}d",
60             flashlightOnAverageMa, uid);
61         iter->second = flashlightOnPowerMah;
62     } else {
63         STATS_HILOGD(COMP_SVC, "Create flashlight on power consumption: %{public}lfmAh for uid: %{public}d",
64             flashlightOnAverageMa, uid);
65         flashlightPowerMap_.insert(std::pair<int32_t, double>(uid, flashlightOnPowerMah));
66     }
67 }
68 
GetEntityPowerMah(int32_t uidOrUserId)69 double FlashlightEntity::GetEntityPowerMah(int32_t uidOrUserId)
70 {
71     double power = StatsUtils::DEFAULT_VALUE;
72     auto iter = flashlightPowerMap_.find(uidOrUserId);
73     if (iter != flashlightPowerMap_.end()) {
74         power = iter->second;
75         STATS_HILOGD(COMP_SVC, "Get app flashlight power consumption: %{public}lfmAh for uid: %{public}d",
76             power, uidOrUserId);
77     } else {
78         STATS_HILOGD(COMP_SVC,
79             "No app flashlight power consumption related to uid: %{public}d was found, return 0", uidOrUserId);
80     }
81     return power;
82 }
83 
GetStatsPowerMah(StatsUtils::StatsType statsType,int32_t uid)84 double FlashlightEntity::GetStatsPowerMah(StatsUtils::StatsType statsType, int32_t uid)
85 {
86     double power = StatsUtils::DEFAULT_VALUE;
87     if (statsType == StatsUtils::STATS_TYPE_FLASHLIGHT_ON) {
88         auto flashlightOnIter = flashlightPowerMap_.find(uid);
89         if (flashlightOnIter != flashlightPowerMap_.end()) {
90             power = flashlightOnIter->second;
91             STATS_HILOGD(COMP_SVC,
92                 "Get flashlight on power consumption: %{public}lfmAh for uid: %{public}d", power, uid);
93         } else {
94             STATS_HILOGD(COMP_SVC,
95                 "No flashlight on power consumption related to uid: %{public}d was found, return 0", uid);
96         }
97     }
98     return power;
99 }
100 
GetOrCreateTimer(int32_t uid,StatsUtils::StatsType statsType,int16_t level)101 std::shared_ptr<StatsHelper::ActiveTimer> FlashlightEntity::GetOrCreateTimer(int32_t uid,
102     StatsUtils::StatsType statsType, int16_t level)
103 {
104     if (statsType != StatsUtils::STATS_TYPE_FLASHLIGHT_ON) {
105         return nullptr;
106     }
107 
108     auto flashlightOnIter = flashlightTimerMap_.find(uid);
109     if (flashlightOnIter != flashlightTimerMap_.end()) {
110         STATS_HILOGD(COMP_SVC, "Get flashlight on timer for uid: %{public}d", uid);
111         return flashlightOnIter->second;
112     }
113     STATS_HILOGD(COMP_SVC, "Create flashlight on timer for uid: %{public}d", uid);
114     std::shared_ptr<StatsHelper::ActiveTimer> flashTimer = std::make_shared<StatsHelper::ActiveTimer>();
115     flashlightTimerMap_.insert(std::pair<int32_t, std::shared_ptr<StatsHelper::ActiveTimer>>(uid, flashTimer));
116     return flashTimer;
117 }
118 
Reset()119 void FlashlightEntity::Reset()
120 {
121     // Reset app Flashlight on total power consumption
122     for (auto& iter : flashlightPowerMap_) {
123         iter.second = StatsUtils::DEFAULT_VALUE;
124     }
125 
126     // Reset Flashlight on timer
127     for (auto& iter : flashlightTimerMap_) {
128         if (iter.second) {
129             iter.second->Reset();
130         }
131     }
132 }
133 } // namespace PowerMgr
134 } // namespace OHOS