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_info.h" 27 #include "ibattery_stats.h" 28 #include "stats_utils.h" 29 30 namespace OHOS { 31 namespace PowerMgr { 32 class BatteryStatsClient final : public DelayedRefSingleton<BatteryStatsClient> { 33 DECLARE_DELAYED_REF_SINGLETON(BatteryStatsClient) 34 35 public: 36 DISALLOW_COPY_AND_MOVE(BatteryStatsClient); 37 BatteryStatsInfoList GetBatteryStats(); 38 void SetOnBattery(bool isOnBattery); 39 double GetAppStatsMah(const int32_t& uid); 40 double GetAppStatsPercent(const int32_t& uid); 41 double GetPartStatsMah(const BatteryStatsInfo::ConsumptionType& type); 42 double GetPartStatsPercent(const BatteryStatsInfo::ConsumptionType& type); 43 uint64_t GetTotalTimeSecond(const StatsUtils::StatsType& statsType, const int32_t& uid = StatsUtils::INVALID_VALUE); 44 uint64_t GetTotalDataBytes(const StatsUtils::StatsType& statsType, const int32_t& uid = StatsUtils::INVALID_VALUE); 45 void Reset(); 46 std::string Dump(const std::vector<std::string>& args); 47 private: 48 class BatteryStatsDeathRecipient : public IRemoteObject::DeathRecipient { 49 public: 50 BatteryStatsDeathRecipient() = default; 51 ~BatteryStatsDeathRecipient() = default; 52 void OnRemoteDied(const wptr<IRemoteObject>& remote); 53 private: 54 DISALLOW_COPY_AND_MOVE(BatteryStatsDeathRecipient); 55 }; 56 57 ErrCode Connect(); 58 sptr<IBatteryStats> proxy_ {nullptr}; 59 sptr<IRemoteObject::DeathRecipient> deathRecipient_ {nullptr}; 60 void ResetProxy(const wptr<IRemoteObject>& remote); 61 std::mutex mutex_; 62 }; 63 } // namespace PowerMgr 64 } // namespace OHOS 65 #endif // BATTERY_STATS_CLIENT_H