15ccb8f90Sopenharmony_ci/*
25ccb8f90Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
35ccb8f90Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
45ccb8f90Sopenharmony_ci * you may not use this file except in compliance with the License.
55ccb8f90Sopenharmony_ci * You may obtain a copy of the License at
65ccb8f90Sopenharmony_ci *
75ccb8f90Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
85ccb8f90Sopenharmony_ci *
95ccb8f90Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
105ccb8f90Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
115ccb8f90Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
125ccb8f90Sopenharmony_ci * See the License for the specific language governing permissions and
135ccb8f90Sopenharmony_ci * limitations under the License.
145ccb8f90Sopenharmony_ci */
155ccb8f90Sopenharmony_ci
165ccb8f90Sopenharmony_ci#include "setting_helper.h"
175ccb8f90Sopenharmony_ci
185ccb8f90Sopenharmony_ci#include "power_log.h"
195ccb8f90Sopenharmony_ci#include "power_utils.h"
205ccb8f90Sopenharmony_ci#include <cinttypes>
215ccb8f90Sopenharmony_ci#include <system_ability_definition.h>
225ccb8f90Sopenharmony_ci
235ccb8f90Sopenharmony_cinamespace OHOS {
245ccb8f90Sopenharmony_cinamespace PowerMgr {
255ccb8f90Sopenharmony_cinamespace {
265ccb8f90Sopenharmony_ciconstexpr int32_t WAKEUP_SOURCE_OPEN = 1;
275ccb8f90Sopenharmony_ciconstexpr int32_t WAKEUP_SOURCE_CLOSE = 0;
285ccb8f90Sopenharmony_ci}
295ccb8f90Sopenharmony_cisptr<SettingObserver> SettingHelper::doubleClickObserver_ = nullptr;
305ccb8f90Sopenharmony_cisptr<SettingObserver> SettingHelper::pickUpObserver_ = nullptr;
315ccb8f90Sopenharmony_cisptr<SettingObserver> SettingHelper::powerModeObserver_ = nullptr;
325ccb8f90Sopenharmony_cisptr<SettingObserver> SettingHelper::lidObserver_ = nullptr;
335ccb8f90Sopenharmony_ci
345ccb8f90Sopenharmony_civoid SettingHelper::UpdateCurrentUserId()
355ccb8f90Sopenharmony_ci{
365ccb8f90Sopenharmony_ci    SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID);
375ccb8f90Sopenharmony_ci    settingProvider.UpdateCurrentUserId();
385ccb8f90Sopenharmony_ci}
395ccb8f90Sopenharmony_ci
405ccb8f90Sopenharmony_cibool SettingHelper::IsSettingKeyValid(const std::string& key)
415ccb8f90Sopenharmony_ci{
425ccb8f90Sopenharmony_ci    return SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID).IsValidKey(key);
435ccb8f90Sopenharmony_ci}
445ccb8f90Sopenharmony_ci
455ccb8f90Sopenharmony_ciint32_t SettingHelper::GetSettingIntValue(const std::string& key, int32_t defaultVal)
465ccb8f90Sopenharmony_ci{
475ccb8f90Sopenharmony_ci    SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID);
485ccb8f90Sopenharmony_ci    int32_t value = defaultVal;
495ccb8f90Sopenharmony_ci    ErrCode ret = settingProvider.GetIntValue(key, value);
505ccb8f90Sopenharmony_ci    if (ret != ERR_OK) {
515ccb8f90Sopenharmony_ci        POWER_HILOGW(COMP_UTILS, "get setting key=%{public}s failed, ret=%{public}d", key.c_str(), ret);
525ccb8f90Sopenharmony_ci    }
535ccb8f90Sopenharmony_ci    return value;
545ccb8f90Sopenharmony_ci}
555ccb8f90Sopenharmony_ci
565ccb8f90Sopenharmony_civoid SettingHelper::SetSettingIntValue(const std::string& key, int32_t value)
575ccb8f90Sopenharmony_ci{
585ccb8f90Sopenharmony_ci    SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID);
595ccb8f90Sopenharmony_ci    ErrCode ret = settingProvider.PutIntValue(key, value);
605ccb8f90Sopenharmony_ci    if (ret != ERR_OK) {
615ccb8f90Sopenharmony_ci        POWER_HILOGW(COMP_UTILS,
625ccb8f90Sopenharmony_ci            "set setting key=%{public}s value=%{public}d failed, ret=%{public}d",
635ccb8f90Sopenharmony_ci            key.c_str(), value, ret);
645ccb8f90Sopenharmony_ci    }
655ccb8f90Sopenharmony_ci}
665ccb8f90Sopenharmony_ci
675ccb8f90Sopenharmony_ciint64_t SettingHelper::GetSettingLongValue(const std::string& key, int64_t defaultVal)
685ccb8f90Sopenharmony_ci{
695ccb8f90Sopenharmony_ci    SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID);
705ccb8f90Sopenharmony_ci    int64_t value = defaultVal;
715ccb8f90Sopenharmony_ci    ErrCode ret = settingProvider.GetLongValue(key, value);
725ccb8f90Sopenharmony_ci    if (ret != ERR_OK) {
735ccb8f90Sopenharmony_ci        POWER_HILOGW(COMP_UTILS, "get setting key=%{public}s failed, ret=%{public}d", key.c_str(), ret);
745ccb8f90Sopenharmony_ci    }
755ccb8f90Sopenharmony_ci    return value;
765ccb8f90Sopenharmony_ci}
775ccb8f90Sopenharmony_ci
785ccb8f90Sopenharmony_civoid SettingHelper::SetSettingLongValue(const std::string& key, int64_t value)
795ccb8f90Sopenharmony_ci{
805ccb8f90Sopenharmony_ci    SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID);
815ccb8f90Sopenharmony_ci    ErrCode ret = settingProvider.PutLongValue(key, value);
825ccb8f90Sopenharmony_ci    if (ret != ERR_OK) {
835ccb8f90Sopenharmony_ci        POWER_HILOGW(COMP_UTILS,
845ccb8f90Sopenharmony_ci            "set setting key=%{public}s value=%{public}" PRId64 " failed, ret=%{public}d",
855ccb8f90Sopenharmony_ci            key.c_str(), value, ret);
865ccb8f90Sopenharmony_ci    }
875ccb8f90Sopenharmony_ci}
885ccb8f90Sopenharmony_ci
895ccb8f90Sopenharmony_ciconst std::string SettingHelper::GetSettingStringValue(const std::string& key)
905ccb8f90Sopenharmony_ci{
915ccb8f90Sopenharmony_ci    SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID);
925ccb8f90Sopenharmony_ci    std::string value;
935ccb8f90Sopenharmony_ci    ErrCode ret = settingProvider.GetStringValue(key, value);
945ccb8f90Sopenharmony_ci    if (ret != ERR_OK) {
955ccb8f90Sopenharmony_ci        POWER_HILOGW(COMP_UTILS, "get setting key=%{public}s failed, ret=%{public}d", key.c_str(), ret);
965ccb8f90Sopenharmony_ci    }
975ccb8f90Sopenharmony_ci    return value;
985ccb8f90Sopenharmony_ci}
995ccb8f90Sopenharmony_ci
1005ccb8f90Sopenharmony_civoid SettingHelper::SetSettingJsonStringValue(const std::string& key, const std::string& jsonConfig)
1015ccb8f90Sopenharmony_ci{
1025ccb8f90Sopenharmony_ci    SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID);
1035ccb8f90Sopenharmony_ci    ErrCode ret = settingProvider.PutStringValue(key, jsonConfig);
1045ccb8f90Sopenharmony_ci    if (ret != ERR_OK) {
1055ccb8f90Sopenharmony_ci        POWER_HILOGW(COMP_UTILS,
1065ccb8f90Sopenharmony_ci            "set setting key=%{public}s failed, jsonConfig=%{public}s, ret=%{public}d",
1075ccb8f90Sopenharmony_ci            key.c_str(), PowerUtils::JsonToSimpleStr(jsonConfig).c_str(), ret);
1085ccb8f90Sopenharmony_ci    }
1095ccb8f90Sopenharmony_ci}
1105ccb8f90Sopenharmony_ci
1115ccb8f90Sopenharmony_cisptr<SettingObserver> SettingHelper::RegisterSettingKeyObserver(
1125ccb8f90Sopenharmony_ci    const std::string& key, SettingObserver::UpdateFunc& func)
1135ccb8f90Sopenharmony_ci{
1145ccb8f90Sopenharmony_ci    SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID);
1155ccb8f90Sopenharmony_ci    auto settingObserver = settingProvider.CreateObserver(key, func);
1165ccb8f90Sopenharmony_ci    ErrCode ret = settingProvider.RegisterObserver(settingObserver);
1175ccb8f90Sopenharmony_ci    if (ret != ERR_OK) {
1185ccb8f90Sopenharmony_ci        POWER_HILOGW(COMP_UTILS, "register setting key=%{public}s observer failed, ret=%{public}d",
1195ccb8f90Sopenharmony_ci            key.c_str(), ret);
1205ccb8f90Sopenharmony_ci        return nullptr;
1215ccb8f90Sopenharmony_ci    }
1225ccb8f90Sopenharmony_ci    return settingObserver;
1235ccb8f90Sopenharmony_ci}
1245ccb8f90Sopenharmony_ci
1255ccb8f90Sopenharmony_civoid SettingHelper::UnregisterSettingObserver(sptr<SettingObserver>& observer)
1265ccb8f90Sopenharmony_ci{
1275ccb8f90Sopenharmony_ci    if (observer == nullptr) {
1285ccb8f90Sopenharmony_ci        return;
1295ccb8f90Sopenharmony_ci    }
1305ccb8f90Sopenharmony_ci    SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID).UnregisterObserver(observer);
1315ccb8f90Sopenharmony_ci}
1325ccb8f90Sopenharmony_ci
1335ccb8f90Sopenharmony_cibool SettingHelper::IsWakeupPickupSettingValid()
1345ccb8f90Sopenharmony_ci{
1355ccb8f90Sopenharmony_ci    return IsSettingKeyValid(SETTING_POWER_WAKEUP_PICKUP_KEY);
1365ccb8f90Sopenharmony_ci}
1375ccb8f90Sopenharmony_ci
1385ccb8f90Sopenharmony_cibool SettingHelper::GetSettingWakeupPickup(const std::string& key)
1395ccb8f90Sopenharmony_ci{
1405ccb8f90Sopenharmony_ci    SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID);
1415ccb8f90Sopenharmony_ci    int32_t value = 0;
1425ccb8f90Sopenharmony_ci    ErrCode ret = settingProvider.GetIntValue(key, value);
1435ccb8f90Sopenharmony_ci    if (ret != ERR_OK) {
1445ccb8f90Sopenharmony_ci        POWER_HILOGE(COMP_UTILS, "get setting power wakeup pickup key failed, ret=%{public}d", ret);
1455ccb8f90Sopenharmony_ci    }
1465ccb8f90Sopenharmony_ci    return (value == WAKEUP_SOURCE_OPEN);
1475ccb8f90Sopenharmony_ci}
1485ccb8f90Sopenharmony_ci
1495ccb8f90Sopenharmony_civoid SettingHelper::SetSettingWakeupPickup(bool enable)
1505ccb8f90Sopenharmony_ci{
1515ccb8f90Sopenharmony_ci    POWER_HILOGI(COMP_UTILS, "SetSettingWakeupPickup switch, enable=%{public}d", enable);
1525ccb8f90Sopenharmony_ci    int32_t value = enable ? WAKEUP_SOURCE_OPEN : WAKEUP_SOURCE_CLOSE;
1535ccb8f90Sopenharmony_ci    SetSettingIntValue(SETTING_POWER_WAKEUP_PICKUP_KEY, value);
1545ccb8f90Sopenharmony_ci}
1555ccb8f90Sopenharmony_ci
1565ccb8f90Sopenharmony_civoid SettingHelper::RegisterSettingWakeupPickupObserver(SettingObserver::UpdateFunc& func)
1575ccb8f90Sopenharmony_ci{
1585ccb8f90Sopenharmony_ci    if (pickUpObserver_) {
1595ccb8f90Sopenharmony_ci        POWER_HILOGI(COMP_UTILS, "setting wakeup pickup observer is already registered");
1605ccb8f90Sopenharmony_ci        return;
1615ccb8f90Sopenharmony_ci    }
1625ccb8f90Sopenharmony_ci    pickUpObserver_ = RegisterSettingKeyObserver(SETTING_POWER_WAKEUP_PICKUP_KEY, func);
1635ccb8f90Sopenharmony_ci}
1645ccb8f90Sopenharmony_ci
1655ccb8f90Sopenharmony_civoid SettingHelper::UnregisterSettingWakeupPickupObserver()
1665ccb8f90Sopenharmony_ci{
1675ccb8f90Sopenharmony_ci    if (!pickUpObserver_) {
1685ccb8f90Sopenharmony_ci        POWER_HILOGI(COMP_UTILS, "pickUpObserver_ is nullptr, no need to unregister");
1695ccb8f90Sopenharmony_ci        return;
1705ccb8f90Sopenharmony_ci    }
1715ccb8f90Sopenharmony_ci    UnregisterSettingObserver(pickUpObserver_);
1725ccb8f90Sopenharmony_ci    pickUpObserver_ = nullptr;
1735ccb8f90Sopenharmony_ci}
1745ccb8f90Sopenharmony_ci
1755ccb8f90Sopenharmony_ci#ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING
1765ccb8f90Sopenharmony_cibool SettingHelper::IsDisplayOffTimeSettingValid()
1775ccb8f90Sopenharmony_ci{
1785ccb8f90Sopenharmony_ci    return IsSettingKeyValid(SETTING_DISPLAY_AC_OFF_TIME_KEY) &&
1795ccb8f90Sopenharmony_ci        IsSettingKeyValid(SETTING_DISPLAY_DC_OFF_TIME_KEY);
1805ccb8f90Sopenharmony_ci}
1815ccb8f90Sopenharmony_ci
1825ccb8f90Sopenharmony_cibool SettingHelper::IsSettingDisplayAcScreenOffTimeValid()
1835ccb8f90Sopenharmony_ci{
1845ccb8f90Sopenharmony_ci    return IsSettingKeyValid(SETTING_DISPLAY_AC_OFF_TIME_KEY);
1855ccb8f90Sopenharmony_ci}
1865ccb8f90Sopenharmony_ci
1875ccb8f90Sopenharmony_ciint64_t SettingHelper::GetSettingDisplayAcScreenOffTime(int64_t defaultVal)
1885ccb8f90Sopenharmony_ci{
1895ccb8f90Sopenharmony_ci    return GetSettingLongValue(SETTING_DISPLAY_AC_OFF_TIME_KEY, defaultVal);
1905ccb8f90Sopenharmony_ci}
1915ccb8f90Sopenharmony_ci
1925ccb8f90Sopenharmony_civoid SettingHelper::SetSettingDisplayAcScreenOffTime(int64_t time)
1935ccb8f90Sopenharmony_ci{
1945ccb8f90Sopenharmony_ci    SetSettingLongValue(SETTING_DISPLAY_AC_OFF_TIME_KEY, time);
1955ccb8f90Sopenharmony_ci}
1965ccb8f90Sopenharmony_ci
1975ccb8f90Sopenharmony_cisptr<SettingObserver> SettingHelper::RegisterSettingDisplayAcScreenOffTimeObserver(SettingObserver::UpdateFunc& func)
1985ccb8f90Sopenharmony_ci{
1995ccb8f90Sopenharmony_ci    return RegisterSettingKeyObserver(SETTING_DISPLAY_AC_OFF_TIME_KEY, func);
2005ccb8f90Sopenharmony_ci}
2015ccb8f90Sopenharmony_ci
2025ccb8f90Sopenharmony_cibool SettingHelper::IsSettingDisplayDcScreenOffTimeValid()
2035ccb8f90Sopenharmony_ci{
2045ccb8f90Sopenharmony_ci    return IsSettingKeyValid(SETTING_DISPLAY_DC_OFF_TIME_KEY);
2055ccb8f90Sopenharmony_ci}
2065ccb8f90Sopenharmony_ci
2075ccb8f90Sopenharmony_ciint64_t SettingHelper::GetSettingDisplayDcScreenOffTime(int64_t defaultVal)
2085ccb8f90Sopenharmony_ci{
2095ccb8f90Sopenharmony_ci    return GetSettingLongValue(SETTING_DISPLAY_DC_OFF_TIME_KEY, defaultVal);
2105ccb8f90Sopenharmony_ci}
2115ccb8f90Sopenharmony_ci
2125ccb8f90Sopenharmony_civoid SettingHelper::SetSettingDisplayDcScreenOffTime(int64_t time)
2135ccb8f90Sopenharmony_ci{
2145ccb8f90Sopenharmony_ci    SetSettingLongValue(SETTING_DISPLAY_DC_OFF_TIME_KEY, time);
2155ccb8f90Sopenharmony_ci}
2165ccb8f90Sopenharmony_ci
2175ccb8f90Sopenharmony_cisptr<SettingObserver> SettingHelper::RegisterSettingDisplayDcScreenOffTimeObserver(SettingObserver::UpdateFunc& func)
2185ccb8f90Sopenharmony_ci{
2195ccb8f90Sopenharmony_ci    return RegisterSettingKeyObserver(SETTING_DISPLAY_DC_OFF_TIME_KEY, func);
2205ccb8f90Sopenharmony_ci}
2215ccb8f90Sopenharmony_ci#else
2225ccb8f90Sopenharmony_cibool SettingHelper::IsDisplayOffTimeSettingValid()
2235ccb8f90Sopenharmony_ci{
2245ccb8f90Sopenharmony_ci    return IsSettingKeyValid(SETTING_DISPLAY_OFF_TIME_KEY);
2255ccb8f90Sopenharmony_ci}
2265ccb8f90Sopenharmony_ci
2275ccb8f90Sopenharmony_ciint64_t SettingHelper::GetSettingDisplayOffTime(int64_t defaultVal)
2285ccb8f90Sopenharmony_ci{
2295ccb8f90Sopenharmony_ci    return GetSettingLongValue(SETTING_DISPLAY_OFF_TIME_KEY, defaultVal);
2305ccb8f90Sopenharmony_ci}
2315ccb8f90Sopenharmony_ci
2325ccb8f90Sopenharmony_civoid SettingHelper::SetSettingDisplayOffTime(int64_t time)
2335ccb8f90Sopenharmony_ci{
2345ccb8f90Sopenharmony_ci    SetSettingLongValue(SETTING_DISPLAY_OFF_TIME_KEY, time);
2355ccb8f90Sopenharmony_ci}
2365ccb8f90Sopenharmony_ci
2375ccb8f90Sopenharmony_cisptr<SettingObserver> SettingHelper::RegisterSettingDisplayOffTimeObserver(SettingObserver::UpdateFunc& func)
2385ccb8f90Sopenharmony_ci{
2395ccb8f90Sopenharmony_ci    return RegisterSettingKeyObserver(SETTING_DISPLAY_OFF_TIME_KEY, func);
2405ccb8f90Sopenharmony_ci}
2415ccb8f90Sopenharmony_ci#endif
2425ccb8f90Sopenharmony_ci
2435ccb8f90Sopenharmony_cibool SettingHelper::IsAutoAdjustBrightnessSettingValid()
2445ccb8f90Sopenharmony_ci{
2455ccb8f90Sopenharmony_ci    return IsSettingKeyValid(SETTING_AUTO_ADJUST_BRIGHTNESS_KEY);
2465ccb8f90Sopenharmony_ci}
2475ccb8f90Sopenharmony_ci
2485ccb8f90Sopenharmony_ciint32_t SettingHelper::GetSettingAutoAdjustBrightness(int32_t defaultVal)
2495ccb8f90Sopenharmony_ci{
2505ccb8f90Sopenharmony_ci    return GetSettingIntValue(SETTING_AUTO_ADJUST_BRIGHTNESS_KEY, defaultVal);
2515ccb8f90Sopenharmony_ci}
2525ccb8f90Sopenharmony_ci
2535ccb8f90Sopenharmony_civoid SettingHelper::SetSettingAutoAdjustBrightness(SwitchStatus status)
2545ccb8f90Sopenharmony_ci{
2555ccb8f90Sopenharmony_ci    SetSettingIntValue(SETTING_AUTO_ADJUST_BRIGHTNESS_KEY, static_cast<int32_t>(status));
2565ccb8f90Sopenharmony_ci}
2575ccb8f90Sopenharmony_ci
2585ccb8f90Sopenharmony_cisptr<SettingObserver> SettingHelper::RegisterSettingAutoAdjustBrightnessObserver(SettingObserver::UpdateFunc& func)
2595ccb8f90Sopenharmony_ci{
2605ccb8f90Sopenharmony_ci    return RegisterSettingKeyObserver(SETTING_AUTO_ADJUST_BRIGHTNESS_KEY, func);
2615ccb8f90Sopenharmony_ci}
2625ccb8f90Sopenharmony_ci
2635ccb8f90Sopenharmony_cibool SettingHelper::IsBrightnessSettingValid()
2645ccb8f90Sopenharmony_ci{
2655ccb8f90Sopenharmony_ci    return IsSettingKeyValid(SETTING_BRIGHTNESS_KEY);
2665ccb8f90Sopenharmony_ci}
2675ccb8f90Sopenharmony_ci
2685ccb8f90Sopenharmony_civoid SettingHelper::SetSettingBrightness(int32_t brightness)
2695ccb8f90Sopenharmony_ci{
2705ccb8f90Sopenharmony_ci    SetSettingIntValue(SETTING_BRIGHTNESS_KEY, brightness);
2715ccb8f90Sopenharmony_ci}
2725ccb8f90Sopenharmony_ci
2735ccb8f90Sopenharmony_cibool SettingHelper::IsVibrationSettingValid()
2745ccb8f90Sopenharmony_ci{
2755ccb8f90Sopenharmony_ci    return IsSettingKeyValid(SETTING_VIBRATION_KEY);
2765ccb8f90Sopenharmony_ci}
2775ccb8f90Sopenharmony_ci
2785ccb8f90Sopenharmony_ciint32_t SettingHelper::GetSettingVibration(int32_t defaultVal)
2795ccb8f90Sopenharmony_ci{
2805ccb8f90Sopenharmony_ci    return GetSettingIntValue(SETTING_VIBRATION_KEY, defaultVal);
2815ccb8f90Sopenharmony_ci}
2825ccb8f90Sopenharmony_ci
2835ccb8f90Sopenharmony_civoid SettingHelper::SetSettingVibration(SwitchStatus status)
2845ccb8f90Sopenharmony_ci{
2855ccb8f90Sopenharmony_ci    SetSettingIntValue(SETTING_VIBRATION_KEY, static_cast<int32_t>(status));
2865ccb8f90Sopenharmony_ci}
2875ccb8f90Sopenharmony_ci
2885ccb8f90Sopenharmony_cisptr<SettingObserver> SettingHelper::RegisterSettingVibrationObserver(SettingObserver::UpdateFunc& func)
2895ccb8f90Sopenharmony_ci{
2905ccb8f90Sopenharmony_ci    return RegisterSettingKeyObserver(SETTING_VIBRATION_KEY, func);
2915ccb8f90Sopenharmony_ci}
2925ccb8f90Sopenharmony_ci
2935ccb8f90Sopenharmony_cibool SettingHelper::IsWindowRotationSettingValid()
2945ccb8f90Sopenharmony_ci{
2955ccb8f90Sopenharmony_ci    return IsSettingKeyValid(SETTING_WINDOW_ROTATION_KEY);
2965ccb8f90Sopenharmony_ci}
2975ccb8f90Sopenharmony_ci
2985ccb8f90Sopenharmony_ciint32_t SettingHelper::GetSettingWindowRotation(int32_t defaultVal)
2995ccb8f90Sopenharmony_ci{
3005ccb8f90Sopenharmony_ci    return GetSettingIntValue(SETTING_WINDOW_ROTATION_KEY, defaultVal);
3015ccb8f90Sopenharmony_ci}
3025ccb8f90Sopenharmony_ci
3035ccb8f90Sopenharmony_civoid SettingHelper::SetSettingWindowRotation(SwitchStatus status)
3045ccb8f90Sopenharmony_ci{
3055ccb8f90Sopenharmony_ci    SetSettingIntValue(SETTING_WINDOW_ROTATION_KEY, static_cast<int32_t>(status));
3065ccb8f90Sopenharmony_ci}
3075ccb8f90Sopenharmony_ci
3085ccb8f90Sopenharmony_cisptr<SettingObserver> SettingHelper::RegisterSettingWindowRotationObserver(SettingObserver::UpdateFunc& func)
3095ccb8f90Sopenharmony_ci{
3105ccb8f90Sopenharmony_ci    return RegisterSettingKeyObserver(SETTING_WINDOW_ROTATION_KEY, func);
3115ccb8f90Sopenharmony_ci}
3125ccb8f90Sopenharmony_ci
3135ccb8f90Sopenharmony_cibool SettingHelper::IsIntellVoiceSettingValid()
3145ccb8f90Sopenharmony_ci{
3155ccb8f90Sopenharmony_ci    return IsSettingKeyValid(SETTING_INTELL_VOICE_KEY);
3165ccb8f90Sopenharmony_ci}
3175ccb8f90Sopenharmony_ci
3185ccb8f90Sopenharmony_ciint32_t SettingHelper::GetSettingIntellVoice(int32_t defaultVal)
3195ccb8f90Sopenharmony_ci{
3205ccb8f90Sopenharmony_ci    return GetSettingIntValue(SETTING_INTELL_VOICE_KEY, defaultVal);
3215ccb8f90Sopenharmony_ci}
3225ccb8f90Sopenharmony_ci
3235ccb8f90Sopenharmony_civoid SettingHelper::SetSettingIntellVoice(SwitchStatus status)
3245ccb8f90Sopenharmony_ci{
3255ccb8f90Sopenharmony_ci    SetSettingIntValue(SETTING_INTELL_VOICE_KEY, static_cast<int32_t>(status));
3265ccb8f90Sopenharmony_ci}
3275ccb8f90Sopenharmony_ci
3285ccb8f90Sopenharmony_cisptr<SettingObserver> SettingHelper::RegisterSettingIntellVoiceObserver(SettingObserver::UpdateFunc& func)
3295ccb8f90Sopenharmony_ci{
3305ccb8f90Sopenharmony_ci    return RegisterSettingKeyObserver(SETTING_INTELL_VOICE_KEY, func);
3315ccb8f90Sopenharmony_ci}
3325ccb8f90Sopenharmony_ci
3335ccb8f90Sopenharmony_ci#ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING
3345ccb8f90Sopenharmony_cisptr<SettingObserver> SettingHelper::RegisterSettingAcSuspendSourcesObserver(SettingObserver::UpdateFunc& func)
3355ccb8f90Sopenharmony_ci{
3365ccb8f90Sopenharmony_ci    return RegisterSettingKeyObserver(SETTING_POWER_AC_SUSPEND_SOURCES_KEY, func);
3375ccb8f90Sopenharmony_ci}
3385ccb8f90Sopenharmony_ci
3395ccb8f90Sopenharmony_cibool SettingHelper::IsSuspendSourcesSettingValid()
3405ccb8f90Sopenharmony_ci{
3415ccb8f90Sopenharmony_ci    return IsSettingKeyValid(SETTING_POWER_AC_SUSPEND_SOURCES_KEY) &&
3425ccb8f90Sopenharmony_ci        IsSettingKeyValid(SETTING_POWER_DC_SUSPEND_SOURCES_KEY);
3435ccb8f90Sopenharmony_ci}
3445ccb8f90Sopenharmony_ci
3455ccb8f90Sopenharmony_cibool SettingHelper::IsSettingAcSuspendSourcesValid()
3465ccb8f90Sopenharmony_ci{
3475ccb8f90Sopenharmony_ci    return IsSettingKeyValid(SETTING_POWER_AC_SUSPEND_SOURCES_KEY);
3485ccb8f90Sopenharmony_ci}
3495ccb8f90Sopenharmony_ci
3505ccb8f90Sopenharmony_ciconst std::string SettingHelper::GetSettingAcSuspendSources()
3515ccb8f90Sopenharmony_ci{
3525ccb8f90Sopenharmony_ci    return GetSettingStringValue(SETTING_POWER_AC_SUSPEND_SOURCES_KEY);
3535ccb8f90Sopenharmony_ci}
3545ccb8f90Sopenharmony_ci
3555ccb8f90Sopenharmony_civoid SettingHelper::SetSettingAcSuspendSources(const std::string& jsonConfig)
3565ccb8f90Sopenharmony_ci{
3575ccb8f90Sopenharmony_ci    SetSettingJsonStringValue(SETTING_POWER_AC_SUSPEND_SOURCES_KEY, jsonConfig);
3585ccb8f90Sopenharmony_ci}
3595ccb8f90Sopenharmony_ci
3605ccb8f90Sopenharmony_cisptr<SettingObserver> SettingHelper::RegisterSettingDcSuspendSourcesObserver(SettingObserver::UpdateFunc& func)
3615ccb8f90Sopenharmony_ci{
3625ccb8f90Sopenharmony_ci    return RegisterSettingKeyObserver(SETTING_POWER_DC_SUSPEND_SOURCES_KEY, func);
3635ccb8f90Sopenharmony_ci}
3645ccb8f90Sopenharmony_ci
3655ccb8f90Sopenharmony_cibool SettingHelper::IsSettingDcSuspendSourcesValid()
3665ccb8f90Sopenharmony_ci{
3675ccb8f90Sopenharmony_ci    return IsSettingKeyValid(SETTING_POWER_DC_SUSPEND_SOURCES_KEY);
3685ccb8f90Sopenharmony_ci}
3695ccb8f90Sopenharmony_ci
3705ccb8f90Sopenharmony_ciconst std::string SettingHelper::GetSettingDcSuspendSources()
3715ccb8f90Sopenharmony_ci{
3725ccb8f90Sopenharmony_ci    return GetSettingStringValue(SETTING_POWER_DC_SUSPEND_SOURCES_KEY);
3735ccb8f90Sopenharmony_ci}
3745ccb8f90Sopenharmony_ci
3755ccb8f90Sopenharmony_civoid SettingHelper::SetSettingDcSuspendSources(const std::string& jsonConfig)
3765ccb8f90Sopenharmony_ci{
3775ccb8f90Sopenharmony_ci    SetSettingJsonStringValue(SETTING_POWER_DC_SUSPEND_SOURCES_KEY, jsonConfig);
3785ccb8f90Sopenharmony_ci}
3795ccb8f90Sopenharmony_ci#else
3805ccb8f90Sopenharmony_cisptr<SettingObserver> SettingHelper::RegisterSettingSuspendSourcesObserver(SettingObserver::UpdateFunc& func)
3815ccb8f90Sopenharmony_ci{
3825ccb8f90Sopenharmony_ci    return RegisterSettingKeyObserver(SETTING_POWER_SUSPEND_SOURCES_KEY, func);
3835ccb8f90Sopenharmony_ci}
3845ccb8f90Sopenharmony_ci
3855ccb8f90Sopenharmony_cibool SettingHelper::IsSuspendSourcesSettingValid()
3865ccb8f90Sopenharmony_ci{
3875ccb8f90Sopenharmony_ci    return IsSettingKeyValid(SETTING_POWER_SUSPEND_SOURCES_KEY);
3885ccb8f90Sopenharmony_ci}
3895ccb8f90Sopenharmony_ci
3905ccb8f90Sopenharmony_ciconst std::string SettingHelper::GetSettingSuspendSources()
3915ccb8f90Sopenharmony_ci{
3925ccb8f90Sopenharmony_ci    return GetSettingStringValue(SETTING_POWER_SUSPEND_SOURCES_KEY);
3935ccb8f90Sopenharmony_ci}
3945ccb8f90Sopenharmony_ci
3955ccb8f90Sopenharmony_civoid SettingHelper::SetSettingSuspendSources(const std::string& jsonConfig)
3965ccb8f90Sopenharmony_ci{
3975ccb8f90Sopenharmony_ci    SetSettingJsonStringValue(SETTING_POWER_SUSPEND_SOURCES_KEY, jsonConfig);
3985ccb8f90Sopenharmony_ci}
3995ccb8f90Sopenharmony_ci#endif
4005ccb8f90Sopenharmony_ci
4015ccb8f90Sopenharmony_cisptr<SettingObserver> SettingHelper::RegisterSettingWakeupSourcesObserver(SettingObserver::UpdateFunc& func)
4025ccb8f90Sopenharmony_ci{
4035ccb8f90Sopenharmony_ci    return RegisterSettingKeyObserver(SETTING_POWER_WAKEUP_SOURCES_KEY, func);
4045ccb8f90Sopenharmony_ci}
4055ccb8f90Sopenharmony_ci
4065ccb8f90Sopenharmony_cibool SettingHelper::IsWakeupSourcesSettingValid()
4075ccb8f90Sopenharmony_ci{
4085ccb8f90Sopenharmony_ci    return IsSettingKeyValid(SETTING_POWER_WAKEUP_SOURCES_KEY);
4095ccb8f90Sopenharmony_ci}
4105ccb8f90Sopenharmony_ci
4115ccb8f90Sopenharmony_ciconst std::string SettingHelper::GetSettingWakeupSources()
4125ccb8f90Sopenharmony_ci{
4135ccb8f90Sopenharmony_ci    return GetSettingStringValue(SETTING_POWER_WAKEUP_SOURCES_KEY);
4145ccb8f90Sopenharmony_ci}
4155ccb8f90Sopenharmony_ci
4165ccb8f90Sopenharmony_civoid SettingHelper::SetSettingWakeupSources(const std::string& jsonConfig)
4175ccb8f90Sopenharmony_ci{
4185ccb8f90Sopenharmony_ci    SetSettingJsonStringValue(SETTING_POWER_WAKEUP_SOURCES_KEY, jsonConfig);
4195ccb8f90Sopenharmony_ci}
4205ccb8f90Sopenharmony_ci
4215ccb8f90Sopenharmony_cibool SettingHelper::IsWakeupDoubleSettingValid()
4225ccb8f90Sopenharmony_ci{
4235ccb8f90Sopenharmony_ci    return IsSettingKeyValid(SETTING_POWER_WAKEUP_DOUBLE_KEY);
4245ccb8f90Sopenharmony_ci}
4255ccb8f90Sopenharmony_ci
4265ccb8f90Sopenharmony_cibool SettingHelper::GetSettingWakeupDouble(const std::string& key)
4275ccb8f90Sopenharmony_ci{
4285ccb8f90Sopenharmony_ci    SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID);
4295ccb8f90Sopenharmony_ci    int32_t value;
4305ccb8f90Sopenharmony_ci    ErrCode ret = settingProvider.GetIntValue(key, value);
4315ccb8f90Sopenharmony_ci    if (ret != ERR_OK) {
4325ccb8f90Sopenharmony_ci        POWER_HILOGE(COMP_UTILS, "get setting power Wakeup double key failed, ret=%{public}d", ret);
4335ccb8f90Sopenharmony_ci    }
4345ccb8f90Sopenharmony_ci    return (value == WAKEUP_SOURCE_OPEN);
4355ccb8f90Sopenharmony_ci}
4365ccb8f90Sopenharmony_ci
4375ccb8f90Sopenharmony_civoid SettingHelper::SetSettingWakeupDouble(bool enable)
4385ccb8f90Sopenharmony_ci{
4395ccb8f90Sopenharmony_ci    POWER_HILOGI(COMP_UTILS, "SetSettingWakeupDouble switch, enable=%{public}d", enable);
4405ccb8f90Sopenharmony_ci    int32_t value = enable ? WAKEUP_SOURCE_OPEN : WAKEUP_SOURCE_CLOSE;
4415ccb8f90Sopenharmony_ci    SetSettingIntValue(SETTING_POWER_WAKEUP_DOUBLE_KEY, value);
4425ccb8f90Sopenharmony_ci}
4435ccb8f90Sopenharmony_ci
4445ccb8f90Sopenharmony_civoid SettingHelper::RegisterSettingWakeupDoubleObserver(SettingObserver::UpdateFunc& func)
4455ccb8f90Sopenharmony_ci{
4465ccb8f90Sopenharmony_ci    if (doubleClickObserver_) {
4475ccb8f90Sopenharmony_ci        POWER_HILOGI(COMP_UTILS, "setting wakeup double click observer is already registered");
4485ccb8f90Sopenharmony_ci        return;
4495ccb8f90Sopenharmony_ci    }
4505ccb8f90Sopenharmony_ci    doubleClickObserver_ = RegisterSettingKeyObserver(SETTING_POWER_WAKEUP_DOUBLE_KEY, func);
4515ccb8f90Sopenharmony_ci}
4525ccb8f90Sopenharmony_ci
4535ccb8f90Sopenharmony_civoid SettingHelper::UnregisterSettingWakeupDoubleObserver()
4545ccb8f90Sopenharmony_ci{
4555ccb8f90Sopenharmony_ci    if (!doubleClickObserver_) {
4565ccb8f90Sopenharmony_ci        POWER_HILOGI(COMP_UTILS, "doubleClickObserver_ is nullptr, no need to unregister");
4575ccb8f90Sopenharmony_ci        return;
4585ccb8f90Sopenharmony_ci    }
4595ccb8f90Sopenharmony_ci    UnregisterSettingObserver(doubleClickObserver_);
4605ccb8f90Sopenharmony_ci    doubleClickObserver_ = nullptr;
4615ccb8f90Sopenharmony_ci}
4625ccb8f90Sopenharmony_ci
4635ccb8f90Sopenharmony_civoid SettingHelper::SaveCurrentMode(int32_t mode)
4645ccb8f90Sopenharmony_ci{
4655ccb8f90Sopenharmony_ci    SetSettingIntValue(SETTING_POWER_MODE_KEY, mode);
4665ccb8f90Sopenharmony_ci}
4675ccb8f90Sopenharmony_ci
4685ccb8f90Sopenharmony_ciint32_t SettingHelper::ReadCurrentMode(int32_t defaultMode)
4695ccb8f90Sopenharmony_ci{
4705ccb8f90Sopenharmony_ci    return GetSettingIntValue(SETTING_POWER_MODE_KEY, defaultMode);
4715ccb8f90Sopenharmony_ci}
4725ccb8f90Sopenharmony_ci
4735ccb8f90Sopenharmony_civoid SettingHelper::RegisterSettingPowerModeObserver(SettingObserver::UpdateFunc& func)
4745ccb8f90Sopenharmony_ci{
4755ccb8f90Sopenharmony_ci    if (powerModeObserver_) {
4765ccb8f90Sopenharmony_ci        POWER_HILOGI(COMP_UTILS, "setting power mode observer is already registered");
4775ccb8f90Sopenharmony_ci        return;
4785ccb8f90Sopenharmony_ci    }
4795ccb8f90Sopenharmony_ci    powerModeObserver_ = RegisterSettingKeyObserver(SETTING_POWER_MODE_KEY, func);
4805ccb8f90Sopenharmony_ci}
4815ccb8f90Sopenharmony_ci
4825ccb8f90Sopenharmony_civoid SettingHelper::UnRegisterSettingPowerModeObserver()
4835ccb8f90Sopenharmony_ci{
4845ccb8f90Sopenharmony_ci    if (!powerModeObserver_) {
4855ccb8f90Sopenharmony_ci        POWER_HILOGI(COMP_UTILS, "powerModeObserver_ is nullptr, no need to unregister");
4865ccb8f90Sopenharmony_ci        return;
4875ccb8f90Sopenharmony_ci    }
4885ccb8f90Sopenharmony_ci    auto ret = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID).UnregisterObserver(powerModeObserver_);
4895ccb8f90Sopenharmony_ci    if (ret != ERR_OK) {
4905ccb8f90Sopenharmony_ci        POWER_HILOGE(COMP_UTILS, "unregister setting power mode observer failed, ret=%{public}d", ret);
4915ccb8f90Sopenharmony_ci    }
4925ccb8f90Sopenharmony_ci    powerModeObserver_ = nullptr;
4935ccb8f90Sopenharmony_ci}
4945ccb8f90Sopenharmony_ci
4955ccb8f90Sopenharmony_ciconst std::string SettingHelper::ReadPowerModeRecoverMap()
4965ccb8f90Sopenharmony_ci{
4975ccb8f90Sopenharmony_ci    return GetSettingStringValue(SETTING_POWER_MODE_BACKUP_KEY);
4985ccb8f90Sopenharmony_ci}
4995ccb8f90Sopenharmony_ci
5005ccb8f90Sopenharmony_civoid SettingHelper::SavePowerModeRecoverMap(const std::string& jsonConfig)
5015ccb8f90Sopenharmony_ci{
5025ccb8f90Sopenharmony_ci    SetSettingJsonStringValue(SETTING_POWER_MODE_BACKUP_KEY, jsonConfig);
5035ccb8f90Sopenharmony_ci}
5045ccb8f90Sopenharmony_ci
5055ccb8f90Sopenharmony_cibool SettingHelper::IsWakeupLidSettingValid()
5065ccb8f90Sopenharmony_ci{
5075ccb8f90Sopenharmony_ci    return IsSettingKeyValid(SETTING_POWER_WAKEUP_LID_KEY);
5085ccb8f90Sopenharmony_ci}
5095ccb8f90Sopenharmony_ci
5105ccb8f90Sopenharmony_civoid SettingHelper::RegisterSettingWakeupLidObserver(SettingObserver::UpdateFunc& func)
5115ccb8f90Sopenharmony_ci{
5125ccb8f90Sopenharmony_ci    if (!IsWakeupLidSettingValid()) {
5135ccb8f90Sopenharmony_ci        POWER_HILOGE(COMP_UTILS, "settings.power.wakeup_lid is valid.");
5145ccb8f90Sopenharmony_ci        return;
5155ccb8f90Sopenharmony_ci    }
5165ccb8f90Sopenharmony_ci    lidObserver_ = RegisterSettingKeyObserver(SETTING_POWER_WAKEUP_LID_KEY, func);
5175ccb8f90Sopenharmony_ci}
5185ccb8f90Sopenharmony_ci
5195ccb8f90Sopenharmony_civoid SettingHelper::UnRegisterSettingWakeupLidObserver()
5205ccb8f90Sopenharmony_ci{
5215ccb8f90Sopenharmony_ci    if (!lidObserver_) {
5225ccb8f90Sopenharmony_ci        POWER_HILOGI(COMP_UTILS, "lidObserver_ is nullptr, no need to unregister");
5235ccb8f90Sopenharmony_ci        return;
5245ccb8f90Sopenharmony_ci    }
5255ccb8f90Sopenharmony_ci    if (!IsWakeupLidSettingValid()) {
5265ccb8f90Sopenharmony_ci        POWER_HILOGE(COMP_UTILS, "settings.power.wakeup_lid is valid.");
5275ccb8f90Sopenharmony_ci        return;
5285ccb8f90Sopenharmony_ci    }
5295ccb8f90Sopenharmony_ci    auto ret = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID).UnregisterObserver(lidObserver_);
5305ccb8f90Sopenharmony_ci    if (ret != ERR_OK) {
5315ccb8f90Sopenharmony_ci        POWER_HILOGE(COMP_UTILS, "unregister setting wakeup lid observer failed, ret=%{public}d", ret);
5325ccb8f90Sopenharmony_ci    }
5335ccb8f90Sopenharmony_ci    lidObserver_ = nullptr;
5345ccb8f90Sopenharmony_ci}
5355ccb8f90Sopenharmony_ci
5365ccb8f90Sopenharmony_cibool SettingHelper::GetSettingWakeupLid(const std::string& key)
5375ccb8f90Sopenharmony_ci{
5385ccb8f90Sopenharmony_ci    if (!IsWakeupLidSettingValid()) {
5395ccb8f90Sopenharmony_ci        POWER_HILOGE(COMP_UTILS, "settings.power.wakeup_lid is valid.");
5405ccb8f90Sopenharmony_ci        return false;
5415ccb8f90Sopenharmony_ci    }
5425ccb8f90Sopenharmony_ci    SettingProvider& settingProvider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID);
5435ccb8f90Sopenharmony_ci    int32_t value = 0;
5445ccb8f90Sopenharmony_ci    ErrCode ret = settingProvider.GetIntValue(SETTING_POWER_WAKEUP_LID_KEY, value);
5455ccb8f90Sopenharmony_ci    if (ret != ERR_OK) {
5465ccb8f90Sopenharmony_ci        POWER_HILOGE(COMP_UTILS, "get setting power wakeup lid key failed, ret=%{public}d", ret);
5475ccb8f90Sopenharmony_ci    }
5485ccb8f90Sopenharmony_ci    return (value == WAKEUP_SOURCE_OPEN);
5495ccb8f90Sopenharmony_ci}
5505ccb8f90Sopenharmony_ci
5515ccb8f90Sopenharmony_civoid SettingHelper::SetSettingWakeupLid(bool enable)
5525ccb8f90Sopenharmony_ci{
5535ccb8f90Sopenharmony_ci    POWER_HILOGI(COMP_UTILS, "SetSettingWakeupLid, enable=%{public}d", enable);
5545ccb8f90Sopenharmony_ci    int32_t value = enable ? WAKEUP_SOURCE_OPEN : WAKEUP_SOURCE_CLOSE;
5555ccb8f90Sopenharmony_ci    SetSettingIntValue(SETTING_POWER_WAKEUP_LID_KEY, value);
5565ccb8f90Sopenharmony_ci}
5575ccb8f90Sopenharmony_ci} // namespace PowerMgr
5585ccb8f90Sopenharmony_ci} // namespace OHOS
559