• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef STATS_HELPER_H
17 #define STATS_HELPER_H
18 
19 #include <cinttypes>
20 
21 #include "stats_log.h"
22 #include "stats_utils.h"
23 
24 namespace OHOS {
25 namespace PowerMgr {
26 class StatsHelper {
27 public:
28     class ActiveTimer {
29     public:
30         ActiveTimer() = default;
31         ~ActiveTimer() = default;
StartRunning()32         bool StartRunning()
33         {
34             if (isRunning_) {
35                 STATS_HILOGD(COMP_SVC, "Active timer was already started");
36                 return false;
37             }
38             startTimeMs_ = GetOnBatteryBootTimeMs();
39             isRunning_ = true;
40             STATS_HILOGD(COMP_SVC, "Active timer is started");
41             return true;
42         }
43 
StopRunning()44         bool StopRunning()
45         {
46             if (!isRunning_) {
47                 STATS_HILOGD(COMP_SVC, "No related active timer is running");
48                 return false;
49             }
50             auto stopTimeMs = GetOnBatteryBootTimeMs();
51             totalTimeMs_ += stopTimeMs - startTimeMs_;
52             isRunning_ = false;
53             STATS_HILOGD(COMP_SVC, "Active timer is stopped");
54             return true;
55         }
56 
GetRunningTimeMs()57         int64_t GetRunningTimeMs()
58         {
59             if (isRunning_) {
60                 auto tmpStopTimeMs = GetOnBatteryBootTimeMs();
61                 totalTimeMs_ += tmpStopTimeMs - startTimeMs_;
62                 startTimeMs_ = tmpStopTimeMs;
63             }
64             return totalTimeMs_;
65         }
66 
AddRunningTimeMs(int64_t avtiveTime)67         void AddRunningTimeMs(int64_t avtiveTime)
68         {
69             if (avtiveTime > StatsUtils::DEFAULT_VALUE) {
70                 totalTimeMs_ += avtiveTime;
71                 STATS_HILOGD(COMP_SVC, "Add on active Time: %{public}" PRId64 "", avtiveTime);
72             } else {
73                 STATS_HILOGW(COMP_SVC, "Invalid active time, ignore");
74             }
75         }
76 
Reset()77         void Reset()
78         {
79             isRunning_ = false;
80             startTimeMs_ = GetOnBatteryBootTimeMs();
81             totalTimeMs_ = StatsUtils::DEFAULT_VALUE;
82         }
83     private:
84         bool isRunning_ = false;
85         int64_t startTimeMs_ = StatsUtils::DEFAULT_VALUE;
86         int64_t totalTimeMs_ = StatsUtils::DEFAULT_VALUE;
87     };
88 
89     class Counter {
90     public:
91         Counter() = default;
92         ~Counter() = default;
AddCount(int64_t count)93         void AddCount(int64_t count)
94         {
95             if (count > StatsUtils::DEFAULT_VALUE) {
96                 if (IsOnBattery()) {
97                     totalCount_ += count;
98                 }
99                 STATS_HILOGD(COMP_SVC, "Add data bytes: %{public}" PRId64 ", total data bytes is: %{public}" PRId64 "",
100                     count, totalCount_);
101             } else {
102                 STATS_HILOGW(COMP_SVC, "Invalid data counts");
103             }
104         }
105 
GetCount()106         int64_t GetCount()
107         {
108             return totalCount_;
109         }
110 
Reset()111         void Reset()
112         {
113             totalCount_ = StatsUtils::DEFAULT_VALUE;
114         }
115     private:
116         int64_t totalCount_ = StatsUtils::DEFAULT_VALUE;
117     };
118     static void SetOnBattery(bool onBattery);
119     static void SetScreenOff(bool screenOff);
120     static int64_t GetOnBatteryBootTimeMs();
121     static int64_t GetOnBatteryUpTimeMs();
122     static bool IsOnBattery();
123     static bool IsOnBatteryScreenOff();
124     static int64_t GetBootTimeMs();
125     static int64_t GetUpTimeMs();
126 private:
127     static int64_t latestUnplugBootTimeMs_;
128     static int64_t latestUnplugUpTimeMs_;
129     static int64_t onBatteryBootTimeMs_;
130     static int64_t onBatteryUpTimeMs_;
131     static bool onBattery_;
132     static bool screenOff_;
133 };
134 } // namespace PowerMgr
135 } // namespace OHOS
136 #endif // STATS_HELPER_H