1 /*
2  * Copyright (c) 2021 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 BATTERY_STATS_CLIENT_H
17 #define BATTERY_STATS_CLIENT_H
18 
19 #include <memory>
20 #include <mutex>
21 #include <singleton.h>
22 #include <string>
23 
24 #include "iremote_object.h"
25 
26 #include "battery_stats_errors.h"
27 #include "battery_stats_info.h"
28 #include "ibattery_stats.h"
29 #include "stats_utils.h"
30 
31 namespace OHOS {
32 namespace PowerMgr {
33 class BatteryStatsClient final : public DelayedRefSingleton<BatteryStatsClient> {
34     DECLARE_DELAYED_REF_SINGLETON(BatteryStatsClient);
35 
36 public:
37     DISALLOW_COPY_AND_MOVE(BatteryStatsClient);
38     BatteryStatsInfoList GetBatteryStats();
39     void SetOnBattery(bool isOnBattery);
40     double GetAppStatsMah(const int32_t& uid);
41     double GetAppStatsPercent(const int32_t& uid);
42     double GetPartStatsMah(const BatteryStatsInfo::ConsumptionType& type);
43     double GetPartStatsPercent(const BatteryStatsInfo::ConsumptionType& type);
44     uint64_t GetTotalTimeSecond(const StatsUtils::StatsType& statsType, const int32_t& uid = StatsUtils::INVALID_VALUE);
45     uint64_t GetTotalDataBytes(const StatsUtils::StatsType& statsType, const int32_t& uid = StatsUtils::INVALID_VALUE);
46     void Reset();
47     std::string Dump(const std::vector<std::string>& args);
48     StatsError GetLastError();
49 
50 #ifndef STATS_SERVICE_DEATH_UT
51 private:
52 #endif
53     class BatteryStatsDeathRecipient : public IRemoteObject::DeathRecipient {
54     public:
55         BatteryStatsDeathRecipient() = default;
56         ~BatteryStatsDeathRecipient() = default;
57         void OnRemoteDied(const wptr<IRemoteObject>& remote);
58     private:
59         DISALLOW_COPY_AND_MOVE(BatteryStatsDeathRecipient);
60     };
61 
62     ErrCode Connect();
63     StatsError lastError_ {StatsError::ERR_OK};
64     sptr<IBatteryStats> proxy_ {nullptr};
65     sptr<IRemoteObject::DeathRecipient> deathRecipient_ {nullptr};
66     void ResetProxy(const wptr<IRemoteObject>& remote);
67     std::mutex mutex_;
68 };
69 } // namespace PowerMgr
70 } // namespace OHOS
71 #endif // BATTERY_STATS_CLIENT_H