1/* 2 * Copyright (c) 2023-2023 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#include "brightness_setting_helper.h" 17 18#include "display_log.h" 19#include "setting_provider.h" 20#include "system_ability_definition.h" 21 22namespace OHOS { 23namespace DisplayPowerMgr { 24namespace { 25constexpr int32_t AUTO_BRIGHTNESS_DISABLE = 0; 26constexpr int32_t AUTO_BRIGHTNESS_ENABLE = 1; 27} 28 29using namespace OHOS::PowerMgr; 30sptr<SettingObserver> BrightnessSettingHelper::mAutoBrightnessObserver; 31sptr<SettingObserver> BrightnessSettingHelper::mBrightnessObserver; 32 33void BrightnessSettingHelper::RegisterSettingBrightnessObserver(SettingObserver::UpdateFunc func) 34{ 35 if (mBrightnessObserver) { 36 DISPLAY_HILOGD(FEAT_BRIGHTNESS, "setting brightness observer is already registered"); 37 return; 38 } 39 SettingProvider& provider = SettingProvider::GetInstance(DISPLAY_MANAGER_SERVICE_ID); 40 mBrightnessObserver = provider.CreateObserver(SETTING_BRIGHTNESS_KEY, func); 41 ErrCode ret = provider.RegisterObserver(mBrightnessObserver); 42 if (ret != ERR_OK) { 43 DISPLAY_HILOGW(FEAT_BRIGHTNESS, "register setting brightness observer failed, ret=%{public}d", ret); 44 mBrightnessObserver = nullptr; 45 } 46} 47 48void BrightnessSettingHelper::UnregisterSettingBrightnessObserver() 49{ 50 if (mBrightnessObserver == nullptr) { 51 DISPLAY_HILOGD(FEAT_BRIGHTNESS, "mBrightnessObserver is nullptr, no need to unregister"); 52 return; 53 } 54 SettingProvider& provider = SettingProvider::GetInstance(DISPLAY_MANAGER_SERVICE_ID); 55 ErrCode ret = provider.UnregisterObserver(mBrightnessObserver); 56 if (ret != ERR_OK) { 57 DISPLAY_HILOGW(FEAT_BRIGHTNESS, "unregister setting brightness observer failed, ret=%{public}d", ret); 58 } 59 mBrightnessObserver = nullptr; 60} 61 62void BrightnessSettingHelper::SetSettingBrightness(uint32_t value) 63{ 64 uint32_t settingBrightness; 65 if (GetSettingBrightness(settingBrightness) != ERR_OK) { 66 return; 67 } 68 if (value == static_cast<uint32_t>(settingBrightness)) { 69 DISPLAY_HILOGI(FEAT_BRIGHTNESS, "no need to set setting brightness"); 70 return; 71 } 72 SettingProvider& provider = SettingProvider::GetInstance(DISPLAY_MANAGER_SERVICE_ID); 73 auto ret = provider.PutIntValue(SETTING_BRIGHTNESS_KEY, static_cast<int32_t>(value)); 74 if (ret != ERR_OK) { 75 DISPLAY_HILOGW(FEAT_BRIGHTNESS, "set setting brightness failed, ret=%{public}d", ret); 76 return; 77 } 78 DISPLAY_HILOGI(FEAT_BRIGHTNESS, "set setting brightness=%{public}u", value); 79} 80 81ErrCode BrightnessSettingHelper::GetSettingBrightness(uint32_t& brightness, const std::string& key) 82{ 83 SettingProvider& provider = SettingProvider::GetInstance(DISPLAY_MANAGER_SERVICE_ID); 84 int32_t value; 85 ErrCode ret = provider.GetIntValue(key, value); 86 if (ret != ERR_OK) { 87 DISPLAY_HILOGW(FEAT_BRIGHTNESS, "get setting brightness failed, ret=%{public}d", ret); 88 return ret; 89 } 90 brightness = static_cast<uint32_t>(value); 91 return ERR_OK; 92} 93 94void BrightnessSettingHelper::RegisterSettingAutoBrightnessObserver(SettingObserver::UpdateFunc func) 95{ 96 if (mAutoBrightnessObserver) { 97 DISPLAY_HILOGD(FEAT_BRIGHTNESS, "setting auto brightness observer is already registered"); 98 return; 99 } 100 SettingProvider& provider = SettingProvider::GetInstance(DISPLAY_MANAGER_SERVICE_ID); 101 mAutoBrightnessObserver = provider.CreateObserver(SETTING_AUTO_ADJUST_BRIGHTNESS_KEY, func); 102 ErrCode ret = provider.RegisterObserver(mAutoBrightnessObserver); 103 if (ret != ERR_OK) { 104 DISPLAY_HILOGW(FEAT_BRIGHTNESS, "register setting auto brightness observer failed, ret=%{public}d", ret); 105 mAutoBrightnessObserver = nullptr; 106 } 107} 108 109void BrightnessSettingHelper::UnregisterSettingAutoBrightnessObserver() 110{ 111 if (mAutoBrightnessObserver == nullptr) { 112 DISPLAY_HILOGD(FEAT_BRIGHTNESS, "mAutoBrightnessObserver is nullptr, no need to unregister"); 113 return; 114 } 115 SettingProvider& provider = SettingProvider::GetInstance(DISPLAY_MANAGER_SERVICE_ID); 116 ErrCode ret = provider.UnregisterObserver(mAutoBrightnessObserver); 117 if (ret != ERR_OK) { 118 DISPLAY_HILOGW(FEAT_BRIGHTNESS, "unregister setting auto brightness observer failed, ret=%{public}d", ret); 119 } 120 mAutoBrightnessObserver = nullptr; 121} 122 123void BrightnessSettingHelper::SetSettingAutoBrightness(bool enable) 124{ 125 DISPLAY_HILOGI(FEAT_BRIGHTNESS, "SetSettingAutoBrightness mode, enable=%{public}d", enable); 126 SettingProvider& provider = SettingProvider::GetInstance(DISPLAY_MANAGER_SERVICE_ID); 127 int32_t value = enable ? AUTO_BRIGHTNESS_ENABLE : AUTO_BRIGHTNESS_DISABLE; 128 ErrCode ret = provider.PutIntValue(SETTING_AUTO_ADJUST_BRIGHTNESS_KEY, value); 129 if (ret != ERR_OK) { 130 DISPLAY_HILOGW( 131 FEAT_BRIGHTNESS, "set setting auto brightness failed, enable=%{public}d, ret=%{public}d", enable, ret); 132 } 133} 134 135bool BrightnessSettingHelper::GetSettingAutoBrightness(const std::string& key) 136{ 137 SettingProvider& provider = SettingProvider::GetInstance(DISPLAY_MANAGER_SERVICE_ID); 138 int32_t value; 139 ErrCode ret = provider.GetIntValue(key, value); 140 if (ret != ERR_OK) { 141 DISPLAY_HILOGW( 142 FEAT_BRIGHTNESS, "get setting auto brightness failed key=%{public}s, ret=%{public}d", key.c_str(), ret); 143 } 144 return (value == AUTO_BRIGHTNESS_ENABLE); 145} 146} // namespace DisplayPowerMgr 147} // namespace OHOS