1 /*
2 * Copyright (c) 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 #define LOG_TAG "Charging"
16 #include "charging.h"
17 #include "battery_info.h"
18 #include "battery_srv_client.h"
19 #include "log_print.h"
20
21 namespace OHOS {
22 namespace DistributedData {
23 __attribute__((used)) Charging Charging::instance_;
Charging()24 Charging::Charging() noexcept
25 {
26 BackupRuleManager::GetInstance().RegisterPlugin(
27 "Charging", [this]() -> auto { return this; });
28 }
29
CanBackup()30 bool Charging::CanBackup()
31 {
32 auto &batterySrvClient = PowerMgr::BatterySrvClient::GetInstance();
33 auto chargingStatus = batterySrvClient.GetChargingStatus();
34 if (chargingStatus != PowerMgr::BatteryChargeState::CHARGE_STATE_ENABLE) {
35 if (chargingStatus != PowerMgr::BatteryChargeState::CHARGE_STATE_FULL) {
36 ZLOGD("the device is not in charge state, chargingStatus=%{public}d.", chargingStatus);
37 return false;
38 }
39 auto batteryPluggedType = batterySrvClient.GetPluggedType();
40 if (batteryPluggedType != PowerMgr::BatteryPluggedType::PLUGGED_TYPE_AC &&
41 batteryPluggedType != PowerMgr::BatteryPluggedType::PLUGGED_TYPE_USB &&
42 batteryPluggedType != PowerMgr::BatteryPluggedType::PLUGGED_TYPE_WIRELESS) {
43 ZLOGD("the device is not in charge full statue, the batteryPluggedType is %{public}d.", batteryPluggedType);
44 return false;
45 }
46 }
47 return true;
48 }
49 } // namespace DistributedData
50 } // namespace OHOS