1d590543dSopenharmony_ci/*
2d590543dSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3d590543dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4d590543dSopenharmony_ci * you may not use this file except in compliance with the License.
5d590543dSopenharmony_ci * You may obtain a copy of the License at
6d590543dSopenharmony_ci *
7d590543dSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8d590543dSopenharmony_ci *
9d590543dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10d590543dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11d590543dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12d590543dSopenharmony_ci * See the License for the specific language governing permissions and
13d590543dSopenharmony_ci * limitations under the License.
14d590543dSopenharmony_ci */
15d590543dSopenharmony_ci
16d590543dSopenharmony_ci#include "entities/screen_entity.h"
17d590543dSopenharmony_ci
18d590543dSopenharmony_ci#include <cinttypes>
19d590543dSopenharmony_ci
20d590543dSopenharmony_ci#include "battery_stats_service.h"
21d590543dSopenharmony_ci#include "stats_log.h"
22d590543dSopenharmony_ci
23d590543dSopenharmony_cinamespace OHOS {
24d590543dSopenharmony_cinamespace PowerMgr {
25d590543dSopenharmony_cinamespace {
26d590543dSopenharmony_ci}
27d590543dSopenharmony_ciScreenEntity::ScreenEntity()
28d590543dSopenharmony_ci{
29d590543dSopenharmony_ci    consumptionType_ = BatteryStatsInfo::CONSUMPTION_TYPE_SCREEN;
30d590543dSopenharmony_ci}
31d590543dSopenharmony_ci
32d590543dSopenharmony_ciint64_t ScreenEntity::GetActiveTimeMs(StatsUtils::StatsType statsType, int16_t level)
33d590543dSopenharmony_ci{
34d590543dSopenharmony_ci    int64_t activeTimeMs = StatsUtils::DEFAULT_VALUE;
35d590543dSopenharmony_ci    switch (statsType) {
36d590543dSopenharmony_ci        case StatsUtils::STATS_TYPE_SCREEN_ON: {
37d590543dSopenharmony_ci            if (screenOnTimer_ != nullptr) {
38d590543dSopenharmony_ci                activeTimeMs = screenOnTimer_->GetRunningTimeMs();
39d590543dSopenharmony_ci                STATS_HILOGD(COMP_SVC, "Get screen on time: %{public}" PRId64 "ms", activeTimeMs);
40d590543dSopenharmony_ci                break;
41d590543dSopenharmony_ci            }
42d590543dSopenharmony_ci            STATS_HILOGD(COMP_SVC, "Didn't find related timer, return 0");
43d590543dSopenharmony_ci            break;
44d590543dSopenharmony_ci        }
45d590543dSopenharmony_ci        case StatsUtils::STATS_TYPE_SCREEN_BRIGHTNESS: {
46d590543dSopenharmony_ci            if (level != StatsUtils::INVALID_VALUE) {
47d590543dSopenharmony_ci                auto iter = screenBrightnessTimerMap_.find(level);
48d590543dSopenharmony_ci                if (iter != screenBrightnessTimerMap_.end() && iter->second != nullptr) {
49d590543dSopenharmony_ci                    activeTimeMs = iter->second->GetRunningTimeMs();
50d590543dSopenharmony_ci                    STATS_HILOGD(COMP_SVC,
51d590543dSopenharmony_ci                        "Get screen brightness time: %{public}" PRId64 "ms of brightness level: %{public}d",
52d590543dSopenharmony_ci                        activeTimeMs, level);
53d590543dSopenharmony_ci                    break;
54d590543dSopenharmony_ci                }
55d590543dSopenharmony_ci                STATS_HILOGD(COMP_SVC, "No screen brightness timer found, return 0");
56d590543dSopenharmony_ci                break;
57d590543dSopenharmony_ci            }
58d590543dSopenharmony_ci            activeTimeMs = GetBrightnessTotalTimeMs();
59d590543dSopenharmony_ci            STATS_HILOGD(COMP_SVC, "Get screen brightness total time: %{public}" PRId64 "ms", activeTimeMs);
60d590543dSopenharmony_ci            break;
61d590543dSopenharmony_ci        }
62d590543dSopenharmony_ci        default:
63d590543dSopenharmony_ci            break;
64d590543dSopenharmony_ci    }
65d590543dSopenharmony_ci    return activeTimeMs;
66d590543dSopenharmony_ci}
67d590543dSopenharmony_ci
68d590543dSopenharmony_ciint64_t ScreenEntity::GetBrightnessTotalTimeMs()
69d590543dSopenharmony_ci{
70d590543dSopenharmony_ci    int64_t totalTimeMs = StatsUtils::DEFAULT_VALUE;
71d590543dSopenharmony_ci    for (auto timerIter : screenBrightnessTimerMap_) {
72d590543dSopenharmony_ci        totalTimeMs += timerIter.second->GetRunningTimeMs();
73d590543dSopenharmony_ci    }
74d590543dSopenharmony_ci    return totalTimeMs;
75d590543dSopenharmony_ci}
76d590543dSopenharmony_ci
77d590543dSopenharmony_civoid ScreenEntity::Calculate(int32_t uid)
78d590543dSopenharmony_ci{
79d590543dSopenharmony_ci    auto bss = BatteryStatsService::GetInstance();
80d590543dSopenharmony_ci    auto screenOnAverageMa =
81d590543dSopenharmony_ci        bss->GetBatteryStatsParser()->GetAveragePowerMa(StatsUtils::CURRENT_SCREEN_ON);
82d590543dSopenharmony_ci    auto screenOnTimeMs = GetActiveTimeMs(StatsUtils::STATS_TYPE_SCREEN_ON);
83d590543dSopenharmony_ci    double screenOnPowerMah = screenOnAverageMa * screenOnTimeMs;
84d590543dSopenharmony_ci
85d590543dSopenharmony_ci    auto brightnessAverageMa =
86d590543dSopenharmony_ci        bss->GetBatteryStatsParser()->GetAveragePowerMa(StatsUtils::CURRENT_SCREEN_BRIGHTNESS);
87d590543dSopenharmony_ci    double brightnessPowerMah = StatsUtils::DEFAULT_VALUE;
88d590543dSopenharmony_ci    for (auto& iter : screenBrightnessTimerMap_) {
89d590543dSopenharmony_ci        if (iter.second != nullptr) {
90d590543dSopenharmony_ci            auto averageMa = brightnessAverageMa * iter.first;
91d590543dSopenharmony_ci            auto timeMs = GetActiveTimeMs(StatsUtils::STATS_TYPE_SCREEN_BRIGHTNESS, iter.first);
92d590543dSopenharmony_ci            brightnessPowerMah += averageMa * timeMs;
93d590543dSopenharmony_ci        }
94d590543dSopenharmony_ci    }
95d590543dSopenharmony_ci
96d590543dSopenharmony_ci    screenPowerMah_ = (screenOnPowerMah + brightnessPowerMah) / StatsUtils::MS_IN_HOUR;
97d590543dSopenharmony_ci    totalPowerMah_ += screenPowerMah_;
98d590543dSopenharmony_ci    std::shared_ptr<BatteryStatsInfo> statsInfo = std::make_shared<BatteryStatsInfo>();
99d590543dSopenharmony_ci    statsInfo->SetConsumptioType(BatteryStatsInfo::CONSUMPTION_TYPE_SCREEN);
100d590543dSopenharmony_ci    statsInfo->SetPower(screenPowerMah_);
101d590543dSopenharmony_ci    statsInfoList_.push_back(statsInfo);
102d590543dSopenharmony_ci    STATS_HILOGD(COMP_SVC, "Calculate screen active power consumption: %{public}lfmAh", screenPowerMah_);
103d590543dSopenharmony_ci}
104d590543dSopenharmony_ci
105d590543dSopenharmony_cidouble ScreenEntity::GetEntityPowerMah(int32_t uidOrUserId)
106d590543dSopenharmony_ci{
107d590543dSopenharmony_ci    return screenPowerMah_;
108d590543dSopenharmony_ci}
109d590543dSopenharmony_ci
110d590543dSopenharmony_cidouble ScreenEntity::GetStatsPowerMah(StatsUtils::StatsType statsType, int32_t uid)
111d590543dSopenharmony_ci{
112d590543dSopenharmony_ci    return screenPowerMah_;
113d590543dSopenharmony_ci}
114d590543dSopenharmony_ci
115d590543dSopenharmony_cistd::shared_ptr<StatsHelper::ActiveTimer> ScreenEntity::GetOrCreateTimer(StatsUtils::StatsType statsType, int16_t level)
116d590543dSopenharmony_ci{
117d590543dSopenharmony_ci    std::shared_ptr<StatsHelper::ActiveTimer> timer = nullptr;
118d590543dSopenharmony_ci    switch (statsType) {
119d590543dSopenharmony_ci        case StatsUtils::STATS_TYPE_SCREEN_ON: {
120d590543dSopenharmony_ci            if (screenOnTimer_ != nullptr) {
121d590543dSopenharmony_ci                STATS_HILOGD(COMP_SVC, "Get screen on timer");
122d590543dSopenharmony_ci                timer = screenOnTimer_;
123d590543dSopenharmony_ci                break;
124d590543dSopenharmony_ci            }
125d590543dSopenharmony_ci            STATS_HILOGD(COMP_SVC, "Create screen on timer");
126d590543dSopenharmony_ci            screenOnTimer_ = std::make_shared<StatsHelper::ActiveTimer>();
127d590543dSopenharmony_ci            timer = screenOnTimer_;
128d590543dSopenharmony_ci            break;
129d590543dSopenharmony_ci        }
130d590543dSopenharmony_ci        case StatsUtils::STATS_TYPE_SCREEN_BRIGHTNESS: {
131d590543dSopenharmony_ci            if (level <= StatsUtils::INVALID_VALUE || level > StatsUtils::SCREEN_BRIGHTNESS_BIN) {
132d590543dSopenharmony_ci                STATS_HILOGD(COMP_SVC, "Illegal brightness");
133d590543dSopenharmony_ci                break;
134d590543dSopenharmony_ci            }
135d590543dSopenharmony_ci            auto iter = screenBrightnessTimerMap_.find(level);
136d590543dSopenharmony_ci            if (iter != screenBrightnessTimerMap_.end() && iter->second != nullptr) {
137d590543dSopenharmony_ci                STATS_HILOGD(COMP_SVC, "Get screen brightness timer of brightness level: %{public}d", level);
138d590543dSopenharmony_ci                timer = iter->second;
139d590543dSopenharmony_ci                break;
140d590543dSopenharmony_ci            }
141d590543dSopenharmony_ci            STATS_HILOGD(COMP_SVC, "Create screen brightness timer of brightness level: %{public}d", level);
142d590543dSopenharmony_ci            std::shared_ptr<StatsHelper::ActiveTimer> brightnessTimer = std::make_shared<StatsHelper::ActiveTimer>();
143d590543dSopenharmony_ci            screenBrightnessTimerMap_.insert(
144d590543dSopenharmony_ci                std::pair<int32_t, std::shared_ptr<StatsHelper::ActiveTimer>>(level, brightnessTimer));
145d590543dSopenharmony_ci            timer = brightnessTimer;
146d590543dSopenharmony_ci            break;
147d590543dSopenharmony_ci        }
148d590543dSopenharmony_ci        default:
149d590543dSopenharmony_ci            STATS_HILOGW(COMP_SVC, "Create active timer failed");
150d590543dSopenharmony_ci            break;
151d590543dSopenharmony_ci    }
152d590543dSopenharmony_ci    return timer;
153d590543dSopenharmony_ci}
154d590543dSopenharmony_ci
155d590543dSopenharmony_civoid ScreenEntity::Reset()
156d590543dSopenharmony_ci{
157d590543dSopenharmony_ci    // Reset app Screen total power consumption
158d590543dSopenharmony_ci    screenPowerMah_ = StatsUtils::DEFAULT_VALUE;
159d590543dSopenharmony_ci
160d590543dSopenharmony_ci    // Reset Screen on timer
161d590543dSopenharmony_ci    if (screenOnTimer_ != nullptr) {
162d590543dSopenharmony_ci        screenOnTimer_->Reset();
163d590543dSopenharmony_ci    }
164d590543dSopenharmony_ci
165d590543dSopenharmony_ci    // Reset Screen brightness timer
166d590543dSopenharmony_ci    for (auto& iter : screenBrightnessTimerMap_) {
167d590543dSopenharmony_ci        if (iter.second != nullptr) {
168d590543dSopenharmony_ci            iter.second->Reset();
169d590543dSopenharmony_ci        }
170d590543dSopenharmony_ci    }
171d590543dSopenharmony_ci}
172d590543dSopenharmony_ci
173d590543dSopenharmony_civoid ScreenEntity::DumpInfo(std::string& result, int32_t uid)
174d590543dSopenharmony_ci{
175d590543dSopenharmony_ci    int64_t onTime = GetActiveTimeMs(StatsUtils::STATS_TYPE_SCREEN_ON);
176d590543dSopenharmony_ci    result.append("Screen dump:\n")
177d590543dSopenharmony_ci        .append("Screen on time: ")
178d590543dSopenharmony_ci        .append(ToString(onTime))
179d590543dSopenharmony_ci        .append("ms")
180d590543dSopenharmony_ci        .append("\n");
181d590543dSopenharmony_ci}
182d590543dSopenharmony_ci} // namespace PowerMgr
183d590543dSopenharmony_ci} // namespace OHOS