1595d5899Sopenharmony_ci/* 2595d5899Sopenharmony_ci * Copyright (c) 2023-2023 Huawei Device Co., Ltd. 3595d5899Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4595d5899Sopenharmony_ci * you may not use this file except in compliance with the License. 5595d5899Sopenharmony_ci * You may obtain a copy of the License at 6595d5899Sopenharmony_ci * 7595d5899Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8595d5899Sopenharmony_ci * 9595d5899Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10595d5899Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11595d5899Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12595d5899Sopenharmony_ci * See the License for the specific language governing permissions and 13595d5899Sopenharmony_ci * limitations under the License. 14595d5899Sopenharmony_ci */ 15595d5899Sopenharmony_ci 16595d5899Sopenharmony_ci#include "brightness_setting_helper.h" 17595d5899Sopenharmony_ci 18595d5899Sopenharmony_ci#include "display_log.h" 19595d5899Sopenharmony_ci#include "setting_provider.h" 20595d5899Sopenharmony_ci#include "system_ability_definition.h" 21595d5899Sopenharmony_ci 22595d5899Sopenharmony_cinamespace OHOS { 23595d5899Sopenharmony_cinamespace DisplayPowerMgr { 24595d5899Sopenharmony_cinamespace { 25595d5899Sopenharmony_ciconstexpr int32_t AUTO_BRIGHTNESS_DISABLE = 0; 26595d5899Sopenharmony_ciconstexpr int32_t AUTO_BRIGHTNESS_ENABLE = 1; 27595d5899Sopenharmony_ci} 28595d5899Sopenharmony_ci 29595d5899Sopenharmony_ciusing namespace OHOS::PowerMgr; 30595d5899Sopenharmony_cisptr<SettingObserver> BrightnessSettingHelper::mAutoBrightnessObserver; 31595d5899Sopenharmony_cisptr<SettingObserver> BrightnessSettingHelper::mBrightnessObserver; 32595d5899Sopenharmony_ci 33595d5899Sopenharmony_civoid BrightnessSettingHelper::RegisterSettingBrightnessObserver(SettingObserver::UpdateFunc func) 34595d5899Sopenharmony_ci{ 35595d5899Sopenharmony_ci if (mBrightnessObserver) { 36595d5899Sopenharmony_ci DISPLAY_HILOGD(FEAT_BRIGHTNESS, "setting brightness observer is already registered"); 37595d5899Sopenharmony_ci return; 38595d5899Sopenharmony_ci } 39595d5899Sopenharmony_ci SettingProvider& provider = SettingProvider::GetInstance(DISPLAY_MANAGER_SERVICE_ID); 40595d5899Sopenharmony_ci mBrightnessObserver = provider.CreateObserver(SETTING_BRIGHTNESS_KEY, func); 41595d5899Sopenharmony_ci ErrCode ret = provider.RegisterObserver(mBrightnessObserver); 42595d5899Sopenharmony_ci if (ret != ERR_OK) { 43595d5899Sopenharmony_ci DISPLAY_HILOGW(FEAT_BRIGHTNESS, "register setting brightness observer failed, ret=%{public}d", ret); 44595d5899Sopenharmony_ci mBrightnessObserver = nullptr; 45595d5899Sopenharmony_ci } 46595d5899Sopenharmony_ci} 47595d5899Sopenharmony_ci 48595d5899Sopenharmony_civoid BrightnessSettingHelper::UnregisterSettingBrightnessObserver() 49595d5899Sopenharmony_ci{ 50595d5899Sopenharmony_ci if (mBrightnessObserver == nullptr) { 51595d5899Sopenharmony_ci DISPLAY_HILOGD(FEAT_BRIGHTNESS, "mBrightnessObserver is nullptr, no need to unregister"); 52595d5899Sopenharmony_ci return; 53595d5899Sopenharmony_ci } 54595d5899Sopenharmony_ci SettingProvider& provider = SettingProvider::GetInstance(DISPLAY_MANAGER_SERVICE_ID); 55595d5899Sopenharmony_ci ErrCode ret = provider.UnregisterObserver(mBrightnessObserver); 56595d5899Sopenharmony_ci if (ret != ERR_OK) { 57595d5899Sopenharmony_ci DISPLAY_HILOGW(FEAT_BRIGHTNESS, "unregister setting brightness observer failed, ret=%{public}d", ret); 58595d5899Sopenharmony_ci } 59595d5899Sopenharmony_ci mBrightnessObserver = nullptr; 60595d5899Sopenharmony_ci} 61595d5899Sopenharmony_ci 62595d5899Sopenharmony_civoid BrightnessSettingHelper::SetSettingBrightness(uint32_t value) 63595d5899Sopenharmony_ci{ 64595d5899Sopenharmony_ci uint32_t settingBrightness; 65595d5899Sopenharmony_ci if (GetSettingBrightness(settingBrightness) != ERR_OK) { 66595d5899Sopenharmony_ci return; 67595d5899Sopenharmony_ci } 68595d5899Sopenharmony_ci if (value == static_cast<uint32_t>(settingBrightness)) { 69595d5899Sopenharmony_ci DISPLAY_HILOGI(FEAT_BRIGHTNESS, "no need to set setting brightness"); 70595d5899Sopenharmony_ci return; 71595d5899Sopenharmony_ci } 72595d5899Sopenharmony_ci SettingProvider& provider = SettingProvider::GetInstance(DISPLAY_MANAGER_SERVICE_ID); 73595d5899Sopenharmony_ci auto ret = provider.PutIntValue(SETTING_BRIGHTNESS_KEY, static_cast<int32_t>(value)); 74595d5899Sopenharmony_ci if (ret != ERR_OK) { 75595d5899Sopenharmony_ci DISPLAY_HILOGW(FEAT_BRIGHTNESS, "set setting brightness failed, ret=%{public}d", ret); 76595d5899Sopenharmony_ci return; 77595d5899Sopenharmony_ci } 78595d5899Sopenharmony_ci DISPLAY_HILOGI(FEAT_BRIGHTNESS, "set setting brightness=%{public}u", value); 79595d5899Sopenharmony_ci} 80595d5899Sopenharmony_ci 81595d5899Sopenharmony_ciErrCode BrightnessSettingHelper::GetSettingBrightness(uint32_t& brightness, const std::string& key) 82595d5899Sopenharmony_ci{ 83595d5899Sopenharmony_ci SettingProvider& provider = SettingProvider::GetInstance(DISPLAY_MANAGER_SERVICE_ID); 84595d5899Sopenharmony_ci int32_t value; 85595d5899Sopenharmony_ci ErrCode ret = provider.GetIntValue(key, value); 86595d5899Sopenharmony_ci if (ret != ERR_OK) { 87595d5899Sopenharmony_ci DISPLAY_HILOGW(FEAT_BRIGHTNESS, "get setting brightness failed, ret=%{public}d", ret); 88595d5899Sopenharmony_ci return ret; 89595d5899Sopenharmony_ci } 90595d5899Sopenharmony_ci brightness = static_cast<uint32_t>(value); 91595d5899Sopenharmony_ci return ERR_OK; 92595d5899Sopenharmony_ci} 93595d5899Sopenharmony_ci 94595d5899Sopenharmony_civoid BrightnessSettingHelper::RegisterSettingAutoBrightnessObserver(SettingObserver::UpdateFunc func) 95595d5899Sopenharmony_ci{ 96595d5899Sopenharmony_ci if (mAutoBrightnessObserver) { 97595d5899Sopenharmony_ci DISPLAY_HILOGD(FEAT_BRIGHTNESS, "setting auto brightness observer is already registered"); 98595d5899Sopenharmony_ci return; 99595d5899Sopenharmony_ci } 100595d5899Sopenharmony_ci SettingProvider& provider = SettingProvider::GetInstance(DISPLAY_MANAGER_SERVICE_ID); 101595d5899Sopenharmony_ci mAutoBrightnessObserver = provider.CreateObserver(SETTING_AUTO_ADJUST_BRIGHTNESS_KEY, func); 102595d5899Sopenharmony_ci ErrCode ret = provider.RegisterObserver(mAutoBrightnessObserver); 103595d5899Sopenharmony_ci if (ret != ERR_OK) { 104595d5899Sopenharmony_ci DISPLAY_HILOGW(FEAT_BRIGHTNESS, "register setting auto brightness observer failed, ret=%{public}d", ret); 105595d5899Sopenharmony_ci mAutoBrightnessObserver = nullptr; 106595d5899Sopenharmony_ci } 107595d5899Sopenharmony_ci} 108595d5899Sopenharmony_ci 109595d5899Sopenharmony_civoid BrightnessSettingHelper::UnregisterSettingAutoBrightnessObserver() 110595d5899Sopenharmony_ci{ 111595d5899Sopenharmony_ci if (mAutoBrightnessObserver == nullptr) { 112595d5899Sopenharmony_ci DISPLAY_HILOGD(FEAT_BRIGHTNESS, "mAutoBrightnessObserver is nullptr, no need to unregister"); 113595d5899Sopenharmony_ci return; 114595d5899Sopenharmony_ci } 115595d5899Sopenharmony_ci SettingProvider& provider = SettingProvider::GetInstance(DISPLAY_MANAGER_SERVICE_ID); 116595d5899Sopenharmony_ci ErrCode ret = provider.UnregisterObserver(mAutoBrightnessObserver); 117595d5899Sopenharmony_ci if (ret != ERR_OK) { 118595d5899Sopenharmony_ci DISPLAY_HILOGW(FEAT_BRIGHTNESS, "unregister setting auto brightness observer failed, ret=%{public}d", ret); 119595d5899Sopenharmony_ci } 120595d5899Sopenharmony_ci mAutoBrightnessObserver = nullptr; 121595d5899Sopenharmony_ci} 122595d5899Sopenharmony_ci 123595d5899Sopenharmony_civoid BrightnessSettingHelper::SetSettingAutoBrightness(bool enable) 124595d5899Sopenharmony_ci{ 125595d5899Sopenharmony_ci DISPLAY_HILOGI(FEAT_BRIGHTNESS, "SetSettingAutoBrightness mode, enable=%{public}d", enable); 126595d5899Sopenharmony_ci SettingProvider& provider = SettingProvider::GetInstance(DISPLAY_MANAGER_SERVICE_ID); 127595d5899Sopenharmony_ci int32_t value = enable ? AUTO_BRIGHTNESS_ENABLE : AUTO_BRIGHTNESS_DISABLE; 128595d5899Sopenharmony_ci ErrCode ret = provider.PutIntValue(SETTING_AUTO_ADJUST_BRIGHTNESS_KEY, value); 129595d5899Sopenharmony_ci if (ret != ERR_OK) { 130595d5899Sopenharmony_ci DISPLAY_HILOGW( 131595d5899Sopenharmony_ci FEAT_BRIGHTNESS, "set setting auto brightness failed, enable=%{public}d, ret=%{public}d", enable, ret); 132595d5899Sopenharmony_ci } 133595d5899Sopenharmony_ci} 134595d5899Sopenharmony_ci 135595d5899Sopenharmony_cibool BrightnessSettingHelper::GetSettingAutoBrightness(const std::string& key) 136595d5899Sopenharmony_ci{ 137595d5899Sopenharmony_ci SettingProvider& provider = SettingProvider::GetInstance(DISPLAY_MANAGER_SERVICE_ID); 138595d5899Sopenharmony_ci int32_t value; 139595d5899Sopenharmony_ci ErrCode ret = provider.GetIntValue(key, value); 140595d5899Sopenharmony_ci if (ret != ERR_OK) { 141595d5899Sopenharmony_ci DISPLAY_HILOGW( 142595d5899Sopenharmony_ci FEAT_BRIGHTNESS, "get setting auto brightness failed key=%{public}s, ret=%{public}d", key.c_str(), ret); 143595d5899Sopenharmony_ci } 144595d5899Sopenharmony_ci return (value == AUTO_BRIGHTNESS_ENABLE); 145595d5899Sopenharmony_ci} 146595d5899Sopenharmony_ci} // namespace DisplayPowerMgr 147595d5899Sopenharmony_ci} // namespace OHOS