diff --git a/frameworks/native/display_power_mgr_client.cpp b/frameworks/native/display_power_mgr_client.cpp index 94e59c8bd1466f17d6d2eb767969be3808b2c749..cd18d42f5249aed16de02dd083336d400cd76f2e 100644 --- a/frameworks/native/display_power_mgr_client.cpp +++ b/frameworks/native/display_power_mgr_client.cpp @@ -90,6 +90,24 @@ DisplayState DisplayPowerMgrClient::GetDisplayState(uint32_t id) return proxy->GetDisplayState(id); } +std::vector DisplayPowerMgrClient::GetDisplayIds() +{ + auto proxy = GetProxy(); + if (proxy == nullptr) { + return std::vector(); + } + return proxy->GetDisplayIds(); +} + +int32_t DisplayPowerMgrClient::GetMainDisplayId() +{ + auto proxy = GetProxy(); + if (proxy == nullptr) { + return -1; + } + return proxy->GetMainDisplayId(); +} + bool DisplayPowerMgrClient::SetBrightness(uint32_t value, uint32_t id) { auto proxy = GetProxy(); @@ -108,6 +126,15 @@ bool DisplayPowerMgrClient::AdjustBrightness(uint32_t value, uint32_t duration, return proxy->AdjustBrightness(id, value, duration); } +bool DisplayPowerMgrClient::AutoAdjustBrightness(bool enable) +{ + auto proxy = GetProxy(); + if (proxy == nullptr) { + return false; + } + return proxy->AutoAdjustBrightness(enable); +} + bool DisplayPowerMgrClient::SetStateConfig(DisplayState state, uint32_t value, uint32_t id) { auto proxy = GetProxy(); @@ -116,5 +143,14 @@ bool DisplayPowerMgrClient::SetStateConfig(DisplayState state, uint32_t value, u } return proxy->SetStateConfig(id, state, value); } + +bool DisplayPowerMgrClient::RegisterCallback(sptr callback) +{ + auto proxy = GetProxy(); + if (proxy == nullptr) { + return false; + } + return proxy->RegisterCallback(callback); +} } // namespace DisplayPowerMgr } // namespace OHOS diff --git a/interfaces/innerkits/native/include/display_power_mgr_client.h b/interfaces/innerkits/native/include/display_power_mgr_client.h index 3478eb3e870fbbd7f048767914b56557acdad7d3..9e434809a8f6fc868889d736c3321f45a5d3e40f 100644 --- a/interfaces/innerkits/native/include/display_power_mgr_client.h +++ b/interfaces/innerkits/native/include/display_power_mgr_client.h @@ -18,8 +18,10 @@ #include #include +#include #include "display_info.h" +#include "idisplay_power_callback.h" #include "idisplay_power_mgr.h" namespace OHOS { @@ -30,9 +32,13 @@ class DisplayPowerMgrClient : public DelayedRefSingleton public: bool SetDisplayState(DisplayState state, uint32_t id = 0); DisplayState GetDisplayState(uint32_t id = 0); + std::vector GetDisplayIds(); + int32_t GetMainDisplayId(); bool SetBrightness(uint32_t value, uint32_t id = 0); bool AdjustBrightness(uint32_t value, uint32_t duration, uint32_t id = 0); bool SetStateConfig(DisplayState state, uint32_t value, uint32_t id = 0); + bool AutoAdjustBrightness(bool enable); + bool RegisterCallback(sptr callback); private: class DisplayDeathRecipient : public IRemoteObject::DeathRecipient { diff --git a/interfaces/innerkits/native/include/idisplay_power_callback.h b/interfaces/innerkits/native/include/idisplay_power_callback.h new file mode 100644 index 0000000000000000000000000000000000000000..ff1ac202815fb93f71ebce209fc105be9f2e8896 --- /dev/null +++ b/interfaces/innerkits/native/include/idisplay_power_callback.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef POWERMGR_IDISPLAY_POWER_CALLBACK_H +#define POWERMGR_IDISPLAY_POWER_CALLBACK_H + +#include +#include + +#include "display_info.h" + +namespace OHOS { +namespace DisplayPowerMgr { +class IDisplayPowerCallback : public IRemoteBroker { +public: + virtual void OnDisplayStateChanged(DisplayState state) = 0; + + DECLARE_INTERFACE_DESCRIPTOR(u"ohos.powermgr.IDisplayPowerCallback"); +}; +} // namespace DisplayPowerMgr +} // namespace OHOS +#endif // POWERMGR_IDISPLAY_POWER_CALLBACK_H \ No newline at end of file diff --git a/interfaces/innerkits/native/include/idisplay_power_mgr.h b/interfaces/innerkits/native/include/idisplay_power_mgr.h index cf2f5e9de9ad39943a121183b093e33073bc3cd9..1bae7cde7f22dc9bb9163b1588d960b5a2e6c4aa 100644 --- a/interfaces/innerkits/native/include/idisplay_power_mgr.h +++ b/interfaces/innerkits/native/include/idisplay_power_mgr.h @@ -17,8 +17,10 @@ #define DISPLAYMGR_IDISPLAY_MGR_H #include +#include #include "display_info.h" +#include "idisplay_power_callback.h" namespace OHOS { namespace DisplayPowerMgr { @@ -27,16 +29,24 @@ public: enum { SET_DISPLAY_STATE = 0, GET_DISPLAY_STATE, + GET_DISPLAY_IDS, + GET_MAIN_DISPLAY_ID, SET_BRIGHTNESS, ADJUST_BRIGHTNESS, + AUTO_ADJUST_BRIGHTNESS, SET_STATE_CONFIG, + REGISTER_CALLBACK, }; virtual bool SetDisplayState(uint32_t id, DisplayState state) = 0; virtual DisplayState GetDisplayState(uint32_t id) = 0; + virtual std::vector GetDisplayIds() = 0; + virtual uint32_t GetMainDisplayId() = 0; virtual bool SetBrightness(uint32_t id, int32_t value) = 0; virtual bool AdjustBrightness(uint32_t id, int32_t value, uint32_t duration) = 0; + virtual bool AutoAdjustBrightness(bool enable) = 0; virtual bool SetStateConfig(uint32_t id, DisplayState state, int32_t value) = 0; + virtual bool RegisterCallback(sptr callback) = 0; DECLARE_INTERFACE_DESCRIPTOR(u"ohos.displaypowermgr.IDisplayPowerMgr"); }; diff --git a/service/BUILD.gn b/service/BUILD.gn index abdaac4f293090796f3b38b146dcd7a13818156c..a08cb4438820ff9d059b82d59d9a32afb602d9fc 100644 --- a/service/BUILD.gn +++ b/service/BUILD.gn @@ -14,7 +14,10 @@ import("//base/powermgr/display_manager/displaymgr.gni") config("displaymgr_private_config") { - include_dirs = [ "//utils/system/safwk/native/include" ] + include_dirs = [ + "//utils/system/safwk/native/include", + "//base/sensors/sensor/interfaces/native/include", + ] } config("displaymgr_public_config") { @@ -55,6 +58,7 @@ ohos_shared_library("displaymgrservice") { "ipc:ipc_core", "safwk:system_ability_fwk", "samgr_standard:samgr_proxy", + "sensor:sensor_interface_native", ] part_name = "${displaymgr_native_part_name}" diff --git a/service/native/include/display_power_mgr_service.h b/service/native/include/display_power_mgr_service.h index 96a5fc5db9e69612b12c5c5fa95b5b3d2844e6d6..15483de17a4a01529253d4f55aeccb0092395b28 100644 --- a/service/native/include/display_power_mgr_service.h +++ b/service/native/include/display_power_mgr_service.h @@ -20,21 +20,26 @@ #include #include +#include "delayed_sp_singleton.h" +#include "display_common.h" #include "display_power_mgr_stub.h" #include "screen_controller.h" -#include "display_common.h" +#include "sensor_agent.h" namespace OHOS { namespace DisplayPowerMgr { class DisplayPowerMgrService : public DisplayPowerMgrStub { public: - DisplayPowerMgrService(); - ~DisplayPowerMgrService() = default; + virtual ~DisplayPowerMgrService(); virtual bool SetDisplayState(uint32_t id, DisplayState state) override; virtual DisplayState GetDisplayState(uint32_t id) override; + virtual std::vector GetDisplayIds() override; + virtual uint32_t GetMainDisplayId() override; virtual bool SetBrightness(uint32_t id, int32_t value) override; virtual bool AdjustBrightness(uint32_t id, int32_t value, uint32_t duration) override; + virtual bool AutoAdjustBrightness(bool enable) override; virtual bool SetStateConfig(uint32_t id, DisplayState state, int32_t value) override; + virtual bool RegisterCallback(sptr callback) override; virtual int32_t Dump(int32_t fd, const std::vector& args) override; class DisplaySystemAbility : public SystemAbility { @@ -64,7 +69,35 @@ public: REGISTER_SYSTEM_ABILITY_BY_ID(DisplaySystemAbility, DISPLAY_MANAGER_SERVICE_ID, true); private: + static const uint32_t AUTO_ADJUST_BRIGHTNESS_DURATION = 1000; + static const int32_t BRIGHTNESS_CHANGE_MIN = 5; + static const int32_t LUX_TO_NIT_SQRT_RADIO = 5; + static const time_t LUX_STABLE_TIME = 1100 ; + static constexpr float LUX_CHANGE_RATE_THRESHOLD = 10.0; + static constexpr float LUX_CHANGE_STABLE_MIN = 100.0; + static const int32_t NIT_MIN = 2; + static const int32_t NIT_MAX = 450; + static const int32_t BRIGHTNESS_MIN = 1; + static const int32_t BRIGHTNESS_MAX = 255; + static void AmbientLightCallback(SensorEvent *event); + + friend DelayedSpSingleton; + + DisplayPowerMgrService(); + void InitSensors(); + bool IsChangedLux(float scalar); + bool CalculateBrightness(float scalar, int32_t& brightness); + int32_t GetBrightnessFromLightScalar(float scalar); + std::map> controllerMap_; + bool supportLightSensor_ {false}; + bool autoBrightness_ {false}; + SensorUser user_; + sptr callback_; + + time_t lastLuxTime_ {0}; + float lastLux_ {0}; + bool luxChanged_ {false}; }; } // namespace DisplayPowerMgr } // namespace OHOS diff --git a/service/native/include/gradual_animator.h b/service/native/include/gradual_animator.h index f592facf209951fcf1cceaabec88192bde68f7ea..8f1d54c1f20849e8efc0af0e1bbb023a6c2636d8 100644 --- a/service/native/include/gradual_animator.h +++ b/service/native/include/gradual_animator.h @@ -25,9 +25,9 @@ namespace OHOS { namespace DisplayPowerMgr { class AnimateCallback { public: - virtual void onStart() = 0; - virtual void onChanged(int32_t currentValue) = 0; - virtual void onEnd() = 0; + virtual void OnStart() = 0; + virtual void OnChanged(int32_t currentValue) = 0; + virtual void OnEnd() = 0; }; class GradualAnimator : public std::enable_shared_from_this { @@ -51,7 +51,7 @@ private: }; void NextStep(); std::string name_; - std::shared_ptr callback_; + std::weak_ptr callback_; std::shared_ptr eventRunner_; std::shared_ptr handler_; bool animating_ = false; diff --git a/service/native/include/screen_controller.h b/service/native/include/screen_controller.h index 3ad2c07d2d27d848f26d49672dc7981571bef962..1d4653708780ac52152032db32f0aec43e72d98d 100644 --- a/service/native/include/screen_controller.h +++ b/service/native/include/screen_controller.h @@ -40,9 +40,10 @@ public: bool UpdateStateConfig(DisplayState state, uint32_t value); bool UpdateBrightness(uint32_t value, uint32_t duration = 0); bool IsScreenOn(); - virtual void onStart() override; - virtual void onChanged(int32_t currentValue) override; - virtual void onEnd() override; + uint32_t GetBrightness(); + virtual void OnStart() override; + virtual void OnChanged(int32_t currentValue) override; + virtual void OnEnd() override; private: static const uint32_t SCREEN_BRIGHTNESS_UPDATE_DURATION = 200; std::mutex mutex_; diff --git a/service/native/src/display_power_mgr_service.cpp b/service/native/src/display_power_mgr_service.cpp index ef3c967f05d8dbaabc13c19ecbc646765ab3e4c4..72df371c26881acb78554d2d9b0eb2761577ef3e 100644 --- a/service/native/src/display_power_mgr_service.cpp +++ b/service/native/src/display_power_mgr_service.cpp @@ -16,22 +16,33 @@ #include "display_power_mgr_service.h" #include +#include #include +#include "hilog_wrapper.h" + namespace OHOS { namespace DisplayPowerMgr { DisplayPowerMgrService::DisplayPowerMgrService() { + DISPLAY_HILOGI(MODULE_SERVICE, "DisplayPowerMgrService Create"); std::shared_ptr screenAction = std::make_shared(); std::vector devIds = screenAction->GetDisplayIds(); int count = devIds.size(); for (int i = 0; i < count; i++) { + DISPLAY_HILOGI(MODULE_SERVICE, "find display: %{public}d", devIds[i]); controllerMap_.emplace(devIds[i], std::make_shared(devIds[i], screenAction)); } + InitSensors(); +} + +DisplayPowerMgrService::~DisplayPowerMgrService() +{ } bool DisplayPowerMgrService::SetDisplayState(uint32_t id, DisplayState state) { + DISPLAY_HILOGI(MODULE_SERVICE, "SetDisplayState %{public}d, %{public}d", id, state); auto iterater = controllerMap_.find(id); if (iterater == controllerMap_.end()) { return false; @@ -41,6 +52,7 @@ bool DisplayPowerMgrService::SetDisplayState(uint32_t id, DisplayState state) DisplayState DisplayPowerMgrService::GetDisplayState(uint32_t id) { + DISPLAY_HILOGI(MODULE_SERVICE, "GetDisplayState %{public}d", id); auto iterater = controllerMap_.find(id); if (iterater == controllerMap_.end()) { return DisplayState::DISPLAY_UNKNOWN; @@ -48,8 +60,26 @@ DisplayState DisplayPowerMgrService::GetDisplayState(uint32_t id) return iterater->second->GetState(); } +std::vector DisplayPowerMgrService::GetDisplayIds() +{ + DISPLAY_HILOGI(MODULE_SERVICE, "GetDisplayIds"); + std::vector ids; + for (auto iter = controllerMap_.begin(); iter != controllerMap_.end(); iter++) { + ids.push_back(iter->first); + } + return ids; +} + +uint32_t DisplayPowerMgrService::GetMainDisplayId() +{ + DISPLAY_HILOGI(MODULE_SERVICE, "GetMainDisplayId"); + // To add modified when window manager can tell us which is the main display + return 0; +} + bool DisplayPowerMgrService::SetBrightness(uint32_t id, int32_t value) { + DISPLAY_HILOGI(MODULE_SERVICE, "SetDisplayState %{public}d, %{public}d", id, value); auto iterater = controllerMap_.find(id); if (iterater == controllerMap_.end()) { return false; @@ -59,6 +89,8 @@ bool DisplayPowerMgrService::SetBrightness(uint32_t id, int32_t value) bool DisplayPowerMgrService::AdjustBrightness(uint32_t id, int32_t value, uint32_t duration) { + DISPLAY_HILOGI(MODULE_SERVICE, "SetDisplayState %{public}d, %{public}d, %{public}d", + id, value, duration); auto iterater = controllerMap_.find(id); if (iterater == controllerMap_.end()) { return false; @@ -66,8 +98,41 @@ bool DisplayPowerMgrService::AdjustBrightness(uint32_t id, int32_t value, uint32 return iterater->second->UpdateBrightness(value, duration); } +bool DisplayPowerMgrService::AutoAdjustBrightness(bool enable) +{ + DISPLAY_HILOGI(MODULE_SERVICE, "AutoAdjustBrightness start"); + if (!supportLightSensor_) { + DISPLAY_HILOGI(MODULE_SERVICE, "AutoAdjustBrightness not support"); + return false; + } + if (enable) { + DISPLAY_HILOGI(MODULE_SERVICE, "AutoAdjustBrightness enable"); + if (autoBrightness_) { + DISPLAY_HILOGW(MODULE_SERVICE, "AutoAdjustBrightness is already enabled"); + return true; + } + strcpy_s(user_.name, sizeof(user_.name), "DisplayPowerMgrService"); + user_.userData = nullptr; + user_.callback = &AmbientLightCallback; + SubscribeSensor(SENSOR_TYPE_ID_AMBIENT_LIGHT, &user_); + ActivateSensor(SENSOR_TYPE_ID_AMBIENT_LIGHT, &user_); + SetMode(SENSOR_TYPE_ID_AMBIENT_LIGHT, &user_, SENSOR_ON_CHANGE); + } else { + DISPLAY_HILOGI(MODULE_SERVICE, "AutoAdjustBrightness disable"); + if (!autoBrightness_) { + DISPLAY_HILOGW(MODULE_SERVICE, "AutoAdjustBrightness is already disabled"); + return true; + } + DeactivateSensor(SENSOR_TYPE_ID_AMBIENT_LIGHT, &user_); + UnsubscribeSensor(SENSOR_TYPE_ID_PROXIMITY, &user_); + } + return true; +} + bool DisplayPowerMgrService::SetStateConfig(uint32_t id, DisplayState state, int32_t value) { + DISPLAY_HILOGI(MODULE_SERVICE, "SetStateConfig %{public}d, %{public}d, %{public}d", + id, state, value); auto iterater = controllerMap_.find(id); if (iterater == controllerMap_.end()) { return false; @@ -75,13 +140,181 @@ bool DisplayPowerMgrService::SetStateConfig(uint32_t id, DisplayState state, int return iterater->second->UpdateStateConfig(state, value); } +bool DisplayPowerMgrService::RegisterCallback(sptr callback) +{ + DISPLAY_HILOGI(MODULE_SERVICE, "RegisterCallback"); + callback_ = callback; + return true; +} + int32_t DisplayPowerMgrService::Dump(int32_t fd, const std::vector& args) { - std::string result("Empty dump info"); + std::string result("DISPLAY POWER MANAGER DUMP:\n"); + for (auto iter = controllerMap_.begin(); iter != controllerMap_.end(); iter++) { + result.append("Display Id="); + result.append(std::to_string(iter->first)); + result.append(" State="); + result.append(std::to_string(static_cast(iter->second->GetState()))); + result.append(" Brightness="); + result.append(std::to_string(iter->second->GetBrightness())); + result.append("\n"); + } + + result.append("Support Ambient Light: "); + if (supportLightSensor_) { + result.append("TRUE"); + } else { + result.append("FALSE"); + } + result.append("\n"); + + result.append("Auto Adjust Brightness: "); + if (autoBrightness_) { + result.append("ON"); + } else { + result.append("OFF"); + } + result.append("\n"); + if (!SaveStringToFd(fd, result)) { DISPLAY_HILOGE(MODULE_SERVICE, "Failed to save dump info to fd"); } return ERR_OK; } + +void DisplayPowerMgrService::InitSensors() +{ + DISPLAY_HILOGI(MODULE_SERVICE, "InitSensors start"); + SensorInfo* sensorInfo = nullptr; + int32_t count; + int ret = GetAllSensors(&sensorInfo, &count); + if (ret != 0 || sensorInfo == nullptr) { + DISPLAY_HILOGI(MODULE_SERVICE, "Can't get sensors"); + return; + } + supportLightSensor_ = false; + for (int i = 0; i < count; i++) { + if (sensorInfo[i].sensorId == SENSOR_TYPE_ID_AMBIENT_LIGHT) { + DISPLAY_HILOGI(MODULE_SERVICE, "AMBIENT_LIGHT Support"); + supportLightSensor_ = true; + break; + } + } + + if (!supportLightSensor_) { + DISPLAY_HILOGI(MODULE_SERVICE, "AMBIENT_LIGHT not support"); + } + free(sensorInfo); +} + +void DisplayPowerMgrService::AmbientLightCallback(SensorEvent *event) +{ + DISPLAY_HILOGI(MODULE_SERVICE, "AmbientSensorCallback"); + if (event->sensorTypeId != SENSOR_TYPE_ID_AMBIENT_LIGHT) { + DISPLAY_HILOGI(MODULE_SERVICE, "Sensor Callback is not AMBIENT_LIGHT"); + return; + } + auto pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + DISPLAY_HILOGI(MODULE_SERVICE, "Sensor Callback no service"); + return; + } + uint32_t mainDispId = pms->GetMainDisplayId(); + auto mainDisp = pms->controllerMap_.find(mainDispId); + if (mainDisp == pms->controllerMap_.end()) { + return; + } + AmbientLightData* data = (AmbientLightData*)event->data; + DISPLAY_HILOGI(MODULE_SERVICE, "AmbientLightCallback: %{public}f", data->intensity); + int32_t brightness = mainDisp->second->GetBrightness(); + if (pms->CalculateBrightness(data->intensity, brightness)) { + pms->AdjustBrightness(mainDispId, brightness, AUTO_ADJUST_BRIGHTNESS_DURATION); + } +} + +bool DisplayPowerMgrService::IsChangedLux(float scalar) +{ + DISPLAY_HILOGI(MODULE_SERVICE, "IsChangedLux: (%{public}d), %{public}f vs %{public}f", + luxChanged_, lastLux_, scalar); + + if (lastLuxTime_ <= 0) { + DISPLAY_HILOGI(MODULE_SERVICE, "IsChangedLux: receive lux at first time"); + lastLuxTime_ = time(0); + lastLux_ = scalar; + luxChanged_ = true; + return false; + } + + if (!luxChanged_) { + float luxChangeMin = (lastLux_ / LUX_CHANGE_RATE_THRESHOLD) + 1; + if (abs(scalar - lastLux_) < luxChangeMin) { + DISPLAY_HILOGI(MODULE_SERVICE, "IsChangedLux: Too little change"); + return false; + } else { + DISPLAY_HILOGI(MODULE_SERVICE, "IsChangedLux: First time to change, wait for stable"); + lastLuxTime_ = time(0); + lastLux_ = scalar; + luxChanged_ = true; + return false; + } + } else { + float luxChangeMin = (lastLux_ / LUX_CHANGE_RATE_THRESHOLD) + 1; + if (luxChangeMin < LUX_CHANGE_STABLE_MIN) { + luxChangeMin = LUX_CHANGE_STABLE_MIN; + } + if (abs(scalar - lastLux_) <= luxChangeMin) { + DISPLAY_HILOGI(MODULE_SERVICE, "IsChangedLux: stable lux"); + if (time(0) - lastLuxTime_ >= LUX_STABLE_TIME) { + DISPLAY_HILOGI(MODULE_SERVICE, "IsChangedLux: stable enought to change"); + lastLuxTime_ = time(0); + lastLux_ = scalar; + luxChanged_ = false; + return true; + } + } else { + DISPLAY_HILOGI(MODULE_SERVICE, "IsChangedLux: unstable lux, wait for stable"); + lastLuxTime_ = time(0); + lastLux_ = scalar; + luxChanged_ = true; + return false; + } + } + + return false; +} + +bool DisplayPowerMgrService::CalculateBrightness(float scalar, int32_t& brightness) +{ + if (!IsChangedLux(scalar)) { + DISPLAY_HILOGI(MODULE_SERVICE, "Lux is not change"); + return false; + } + int32_t change = GetBrightnessFromLightScalar(scalar); + if (abs(change - brightness) < BRIGHTNESS_CHANGE_MIN) { + DISPLAY_HILOGI(MODULE_SERVICE, "Small change, don't need to adjust brightness"); + return false; + } + brightness = change; + return true; +} + +int32_t DisplayPowerMgrService::GetBrightnessFromLightScalar(float scalar) +{ + DISPLAY_HILOGI(MODULE_SERVICE, "GetBrightnessFromLightScalar: %{public}f", scalar); + // use simple quadratic equation (lux = (nit / 5) ^ 2) to calculate nit + int32_t nit = static_cast(5 * sqrt(scalar)); + if (nit < NIT_MIN) { + nit = NIT_MIN; + } else if (nit > NIT_MAX) { + nit = NIT_MAX; + } + DISPLAY_HILOGI(MODULE_SERVICE, "nit: %{public}d", nit); + + int32_t brightness = BRIGHTNESS_MIN + + ((BRIGHTNESS_MAX - BRIGHTNESS_MIN) * (nit - NIT_MIN) / (NIT_MAX - NIT_MIN)); + DISPLAY_HILOGI(MODULE_SERVICE, "brightness: %{public}d", brightness); + + return brightness; +} } // namespace DisplayPowerMgr } // namespace OHOS diff --git a/service/native/src/display_system_ability.cpp b/service/native/src/display_system_ability.cpp index 496b357b26026a1fc73518535903f282cbbafe28..b689b2e79be4a085c1b665bce5c517a6b7514d04 100644 --- a/service/native/src/display_system_ability.cpp +++ b/service/native/src/display_system_ability.cpp @@ -23,11 +23,10 @@ REGISTER_SYSTEM_ABILITY_BY_ID(DisplaySystemAbility, DISPLAY_MANAGER_SERVICE_ID, } void DisplaySystemAbility::OnStart() { - DISPLAY_HILOGI(MODULE_SERVICE, "Start service"); - service_ = new DisplayPowerMgrService(); - if (!Publish(service_)) { - DISPLAY_HILOGE(MODULE_SERVICE, "Failed to publish service"); - } + DISPLAY_HILOGI(MODULE_SERVICE, "Start service"); + if (!Publish(DelayedSpSingleton::GetInstance())) { + DISPLAY_HILOGE(MODULE_SERVICE, "Failed to publish service"); + } } void DisplaySystemAbility::OnStop() diff --git a/service/native/src/gradual_animator.cpp b/service/native/src/gradual_animator.cpp index 84b88ce7efbd12bf792c28b900187e5285050644..32614d17cc0b1d02f622d42b0357c980456967c0 100644 --- a/service/native/src/gradual_animator.cpp +++ b/service/native/src/gradual_animator.cpp @@ -25,10 +25,6 @@ GradualAnimator::GradualAnimator(const std::string& name, DISPLAY_HILOGD(MODULE_SERVICE, "GradualAnimator construct start"); name_ = name; callback_ = callback; - eventRunner_ = AppExecFwk::EventRunner::Create(name_); - if (eventRunner_ == nullptr) { - DISPLAY_HILOGW(MODULE_SERVICE, "GradualAnimator failed due to create EventRunner"); - } from_ = 0; to_ = 0; current_ = 0; @@ -45,7 +41,7 @@ void GradualAnimator::StartAnimation(int32_t from, int32_t to, uint32_t duration DISPLAY_HILOGD(MODULE_SERVICE, "StartAnimation from=%{public}d, to=%{public}d, duration=%{public}d", from, to, duration); - if (callback_ == nullptr) { + if (callback_.lock() == nullptr) { DISPLAY_HILOGW(MODULE_SERVICE, "Callback is NULL"); return; } @@ -57,9 +53,14 @@ void GradualAnimator::StartAnimation(int32_t from, int32_t to, uint32_t duration if (steps_ < 1) { steps_ = 1; } - stride_ = (to_ - from_) / steps_; + stride_ = (to_ - from_) / static_cast(steps_); currentStep_ = 0; if (handler_ == nullptr) { + eventRunner_ = AppExecFwk::EventRunner::Create(name_); + if (eventRunner_ == nullptr) { + DISPLAY_HILOGW(MODULE_SERVICE, "GradualAnimator failed due to create EventRunner"); + return; + } handler_ = std::make_shared(eventRunner_, shared_from_this()); } animating_ = true; @@ -72,11 +73,12 @@ void GradualAnimator::StopAnimation() DISPLAY_HILOGD(MODULE_SERVICE, "GradualAnimator StopAnimation start"); animating_ = false; handler_->RemoveEvent(EVENT_STEP); - if (callback_ == nullptr) { + if (callback_.lock() == nullptr) { DISPLAY_HILOGW(MODULE_SERVICE, "Callback is NULL"); return; } - callback_->onEnd(); + std::shared_ptr callback = callback_.lock(); + callback->OnEnd(); DISPLAY_HILOGD(MODULE_SERVICE, "GradualAnimator StopAnimation end"); } @@ -91,24 +93,27 @@ void GradualAnimator::NextStep() DISPLAY_HILOGW(MODULE_SERVICE, "NextStep, not animating"); return; } - if (callback_ == nullptr) { + if (callback_.lock() == nullptr) { DISPLAY_HILOGW(MODULE_SERVICE, "Callback is NULL"); return; } + std::shared_ptr callback = callback_.lock(); currentStep_++; if (currentStep_ == 1) { - callback_->onStart(); + callback->OnStart(); } if (currentStep_ < steps_) { current_ = current_ + stride_; - callback_->onChanged(current_); + callback->OnChanged(current_); handler_->SendEvent(EVENT_STEP, 0, updateTime_); } else { current_ = to_; - callback_->onChanged(current_); - callback_->onEnd(); + callback->OnChanged(current_); + callback->OnEnd(); animating_ = false; } + DISPLAY_HILOGD(MODULE_SERVICE, "NextStep: Step=%{public}d, current=%{public}d, stride=%{public}d", + currentStep_, current_, stride_); } GradualAnimator::AnimatorHandler::AnimatorHandler( @@ -124,6 +129,10 @@ void GradualAnimator::AnimatorHandler::ProcessEvent(const AppExecFwk::InnerEvent DISPLAY_HILOGD(MODULE_SERVICE, "AnimatorHandler::%{public}s ,eventid = %d", __func__, event->GetInnerEventId()); std::shared_ptr animator = owner_.lock(); + if (animator == nullptr) { + DISPLAY_HILOGD(MODULE_SERVICE, "AnimatorHandler no object"); + return; + } switch (event->GetInnerEventId()) { case EVENT_STEP: { animator->NextStep(); diff --git a/service/native/src/screen_controller.cpp b/service/native/src/screen_controller.cpp index 93f34fe74cab65a0de8dc52a9e569cb3072a500c..39631b19165c783f6e8565092f51017f935ba39d 100644 --- a/service/native/src/screen_controller.cpp +++ b/service/native/src/screen_controller.cpp @@ -66,8 +66,8 @@ bool ScreenController::UpdateState(DisplayState state) bool ScreenController::UpdateBrightness(uint32_t value, uint32_t duraion) { std::lock_guard lock(mutex_); - DISPLAY_HILOGI(MODULE_SERVICE, "ScreenController UpdateState: %{public}d, %{public}d", - devId_, value); + DISPLAY_HILOGI(MODULE_SERVICE, "ScreenController UpdateBrightness: %{public}d, %{public}d, %{public}d", + devId_, value, duraion); if (animator_ == nullptr) { std::string name = "ScreenController_" + std::to_string(devId_); std::shared_ptr callback = shared_from_this(); @@ -78,7 +78,7 @@ bool ScreenController::UpdateBrightness(uint32_t value, uint32_t duraion) } if (duraion > 0) { DISPLAY_HILOGI(MODULE_SERVICE, "UpdateState gradually"); - animator_->StartAnimation(brightness_, value, SCREEN_BRIGHTNESS_UPDATE_DURATION); + animator_->StartAnimation(brightness_, value, duraion); return true; } bool ret = action_->SetBrightness(devId_, value); @@ -88,7 +88,7 @@ bool ScreenController::UpdateBrightness(uint32_t value, uint32_t duraion) } else { DISPLAY_HILOGI(MODULE_SERVICE, "Update brightness falied! %{public}d", value); } - + return ret; } @@ -113,23 +113,32 @@ bool ScreenController::IsScreenOn() return (state_ == DisplayState::DISPLAY_ON || state_ == DisplayState::DISPLAY_DIM); } -void ScreenController::onStart() +uint32_t ScreenController::GetBrightness() +{ + std::lock_guard lock(mutex_); + if (brightness_ == 0) { + brightness_ = action_->GetBrightness(devId_); + } + return brightness_; +} + +void ScreenController::OnStart() { DISPLAY_HILOGD(MODULE_SERVICE, "ScreenAnimatorCallback onStart"); } -void ScreenController::onChanged(int32_t currentValue) +void ScreenController::OnChanged(int32_t currentValue) { - brightness_ = currentValue; - bool ret = action_->SetBrightness(devId_, currentValue); + brightness_ = static_cast(currentValue); + bool ret = action_->SetBrightness(devId_, brightness_); if (ret) { - DISPLAY_HILOGD(MODULE_SERVICE, "Update brightness to %{public}d", currentValue); + DISPLAY_HILOGD(MODULE_SERVICE, "Update brightness to %{public}d", brightness_); } else { - DISPLAY_HILOGD(MODULE_SERVICE, "Update brightness falied! %{public}d", currentValue); + DISPLAY_HILOGD(MODULE_SERVICE, "Update brightness falied! %{public}d", brightness_); } } -void ScreenController::onEnd() +void ScreenController::OnEnd() { DISPLAY_HILOGD(MODULE_SERVICE, "ScreenAnimatorCallback OnEnd"); } diff --git a/service/zidl/include/display_power_mgr_proxy.h b/service/zidl/include/display_power_mgr_proxy.h index e27b0d08223d18b25ee67191e55c1336d5da3f06..93da3c83da858c93bdeeadb41b505f8d022ffede 100644 --- a/service/zidl/include/display_power_mgr_proxy.h +++ b/service/zidl/include/display_power_mgr_proxy.h @@ -30,9 +30,14 @@ public: virtual bool SetDisplayState(uint32_t id, DisplayState state) override; virtual DisplayState GetDisplayState(uint32_t id) override; + virtual std::vector GetDisplayIds() override; + virtual uint32_t GetMainDisplayId() override; + virtual bool SetBrightness(uint32_t id, int32_t value) override; virtual bool AdjustBrightness(uint32_t id, int32_t value, uint32_t duration) override; + virtual bool AutoAdjustBrightness(bool enable) override; virtual bool SetStateConfig(uint32_t id, DisplayState state, int32_t value) override; + virtual bool RegisterCallback(sptr callback) override; private: static inline BrokerDelegator delegator_; diff --git a/service/zidl/include/display_power_mgr_stub.h b/service/zidl/include/display_power_mgr_stub.h index 2a35eddfe75ad7e0d64fdbaf54eaae2ec7a8cc1a..726833930cb225425ec7724bde66f6807d8b61dd 100644 --- a/service/zidl/include/display_power_mgr_stub.h +++ b/service/zidl/include/display_power_mgr_stub.h @@ -29,9 +29,13 @@ public: private: int32_t SetDisplayStateStub(MessageParcel& data, MessageParcel& reply); int32_t GetDisplayStateStub(MessageParcel& data, MessageParcel& reply); + int32_t GetDisplayIdsStub(MessageParcel& data, MessageParcel& reply); + int32_t GetMainDisplayIdStub(MessageParcel& data, MessageParcel& reply); int32_t SetBrightnessStub(MessageParcel& data, MessageParcel& reply); int32_t AdjustBrightnessStub(MessageParcel& data, MessageParcel& reply); + int32_t AutoAdjustBrightnessStub(MessageParcel& data, MessageParcel& reply); int32_t SetStateConfigStub(MessageParcel& data, MessageParcel& reply); + int32_t RegisterCallbackStub(MessageParcel& data, MessageParcel& reply); }; } // namespace DisplayPowerMgr } // namespace OHOS diff --git a/service/zidl/src/display_power_mgr_proxy.cpp b/service/zidl/src/display_power_mgr_proxy.cpp index f0538f7824865602931ef8135c7fe319ce628abc..f9d2beeba09644611de5174772ac461641a29d50 100644 --- a/service/zidl/src/display_power_mgr_proxy.cpp +++ b/service/zidl/src/display_power_mgr_proxy.cpp @@ -85,6 +85,78 @@ DisplayState DisplayPowerMgrProxy::GetDisplayState(uint32_t id) return static_cast(result); } +std::vector DisplayPowerMgrProxy::GetDisplayIds() +{ + sptr remote = Remote(); + std::vector result; + + RETURN_IF_WITH_RET(remote == nullptr, result); + + uint32_t count = 0; + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(DisplayPowerMgrProxy::GetDescriptor())) { + DISPLAY_HILOGE(MODULE_INNERKIT, "DisplayPowerMgrClient::%{public}s write descriptor failed!", __func__); + return result; + } + + int ret = remote->SendRequest(static_cast(IDisplayPowerMgr::GET_DISPLAY_IDS), + data, reply, option); + if (ret != ERR_OK) { + DISPLAY_HILOGE(MODULE_INNERKIT, "DisplayPowerMgrProxy::%{public}s SendRequest is failed,%d", __func__, ret); + return result; + } + + if (!reply.ReadUint32(count)) { + DISPLAY_HILOGE(MODULE_INNERKIT, "Readback fail!"); + return result; + } + + for (uint32_t i = 0; i < count; i++) { + uint32_t value; + if (reply.ReadUint32(value)) { + result.push_back(value); + } else { + DISPLAY_HILOGE(MODULE_INNERKIT, "read value fail: %{public}d", i); + } + } + + return result; +} + +uint32_t DisplayPowerMgrProxy::GetMainDisplayId() +{ + sptr remote = Remote(); + uint32_t result = 0; + + RETURN_IF_WITH_RET(remote == nullptr, result); + + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(DisplayPowerMgrProxy::GetDescriptor())) { + DISPLAY_HILOGE(MODULE_INNERKIT, "DisplayPowerMgrClient::%{public}s write descriptor failed!", __func__); + return result; + } + + int ret = remote->SendRequest(static_cast(IDisplayPowerMgr::GET_MAIN_DISPLAY_ID), + data, reply, option); + if (ret != ERR_OK) { + DISPLAY_HILOGE(MODULE_INNERKIT, "DisplayPowerMgrProxy::%{public}s SendRequest is failed,%d", __func__, ret); + return result; + } + + if (!reply.ReadUint32(result)) { + DISPLAY_HILOGE(MODULE_INNERKIT, "Readback fail!"); + return result; + } + + return result; +} + bool DisplayPowerMgrProxy::SetBrightness(uint32_t id, int32_t value) { sptr remote = Remote(); @@ -152,6 +224,38 @@ bool DisplayPowerMgrProxy::AdjustBrightness(uint32_t id, int32_t value, uint32_t return result; } +bool DisplayPowerMgrProxy::AutoAdjustBrightness(bool enable) +{ + sptr remote = Remote(); + RETURN_IF_WITH_RET(remote == nullptr, false); + + bool result = false; + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(DisplayPowerMgrProxy::GetDescriptor())) { + DISPLAY_HILOGE(MODULE_INNERKIT, "DisplayPowerMgrProxy::%{public}s write descriptor failed!", __func__); + return result; + } + + WRITE_PARCEL_WITH_RET(data, Bool, enable, false); + + int ret = remote->SendRequest(static_cast(IDisplayPowerMgr::AUTO_ADJUST_BRIGHTNESS), + data, reply, option); + if (ret != ERR_OK) { + DISPLAY_HILOGE(MODULE_INNERKIT, "DisplayPowerMgrProxy::%{public}s SendRequest is failed: %d", __func__, ret); + return result; + } + + if (!reply.ReadBool(result)) { + DISPLAY_HILOGE(MODULE_INNERKIT, "Readback fail!"); + return result; + } + + return result; +} + bool DisplayPowerMgrProxy::SetStateConfig(uint32_t id, DisplayState state, int32_t value) { sptr remote = Remote(); @@ -186,5 +290,37 @@ bool DisplayPowerMgrProxy::SetStateConfig(uint32_t id, DisplayState state, int32 return result; } + +bool DisplayPowerMgrProxy::RegisterCallback(sptr callback) +{ + sptr remote = Remote(); + RETURN_IF_WITH_RET(remote == nullptr, false); + + bool result = false; + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(DisplayPowerMgrProxy::GetDescriptor())) { + DISPLAY_HILOGE(MODULE_INNERKIT, "DisplayPowerMgrProxy::%{public}s write descriptor failed!", __func__); + return result; + } + + WRITE_PARCEL_WITH_RET(data, RemoteObject, callback->AsObject(), false); + + int ret = remote->SendRequest(static_cast(IDisplayPowerMgr::REGISTER_CALLBACK), + data, reply, option); + if (ret != ERR_OK) { + DISPLAY_HILOGE(MODULE_INNERKIT, "DisplayPowerMgrProxy::%{public}s SendRequest is failed: %d", __func__, ret); + return result; + } + + if (!reply.ReadBool(result)) { + DISPLAY_HILOGE(MODULE_INNERKIT, "Readback fail!"); + return result; + } + + return result; +} } // namespace DisplayPowerMgr } // namespace OHOS diff --git a/service/zidl/src/display_power_mgr_stub.cpp b/service/zidl/src/display_power_mgr_stub.cpp index 8fe9b53919ac52d809066bbc73fc3af2f48c5760..b709150f143438d8627a753162b0632480ab13e7 100644 --- a/service/zidl/src/display_power_mgr_stub.cpp +++ b/service/zidl/src/display_power_mgr_stub.cpp @@ -38,10 +38,16 @@ int32_t DisplayPowerMgrStub::OnRemoteRequest(uint32_t code, MessageParcel &data, return SetDisplayStateStub(data, reply); case static_cast(IDisplayPowerMgr::GET_DISPLAY_STATE): return GetDisplayStateStub(data, reply); + case static_cast(IDisplayPowerMgr::GET_DISPLAY_IDS): + return GetDisplayIdsStub(data, reply); + case static_cast(IDisplayPowerMgr::GET_MAIN_DISPLAY_ID): + return GetMainDisplayIdStub(data, reply); case static_cast(IDisplayPowerMgr::SET_BRIGHTNESS): return SetBrightnessStub(data, reply); case static_cast(IDisplayPowerMgr::ADJUST_BRIGHTNESS): return AdjustBrightnessStub(data, reply); + case static_cast(IDisplayPowerMgr::AUTO_ADJUST_BRIGHTNESS): + return AutoAdjustBrightnessStub(data, reply); case static_cast(IDisplayPowerMgr::SET_STATE_CONFIG): return SetStateConfigStub(data, reply); default: @@ -59,7 +65,7 @@ int32_t DisplayPowerMgrStub::SetDisplayStateStub(MessageParcel& data, MessagePar bool ret = SetDisplayState(id, static_cast(state)); if (!reply.WriteBool(ret)) { - DISPLAY_HILOGE(MODULE_SERVICE, "Failed to write SetScreenState return value"); + DISPLAY_HILOGE(MODULE_SERVICE, "Failed to write SetDisplayStateStub return value"); return E_WRITE_PARCEL_ERROR; } return ERR_OK; @@ -73,12 +79,36 @@ int32_t DisplayPowerMgrStub::GetDisplayStateStub(MessageParcel& data, MessagePar DisplayState ret = GetDisplayState(id); if (!reply.WriteUint32(static_cast(ret))) { - DISPLAY_HILOGE(MODULE_SERVICE, "Failed to write SetScreenState return value"); + DISPLAY_HILOGE(MODULE_SERVICE, "Failed to write GetDisplayStateStub return value"); return E_WRITE_PARCEL_ERROR; } return ERR_OK; } +int32_t DisplayPowerMgrStub::GetDisplayIdsStub(MessageParcel& data, MessageParcel& reply) +{ + std::vector result = GetDisplayIds(); + if (!reply.WriteUint32(static_cast(result.size()))) { + DISPLAY_HILOGE(MODULE_SERVICE, "Failed to write GetDisplayIdsStub return value"); + return E_WRITE_PARCEL_ERROR; + } + for (uint32_t i = 0; i < result.size(); i++) { + if (!reply.WriteUint32(static_cast(result[i]))) { + DISPLAY_HILOGE(MODULE_SERVICE, "Failed to write GetDisplayIdsStub"); + } + } + return ERR_OK; +} + +int32_t DisplayPowerMgrStub::GetMainDisplayIdStub(MessageParcel& data, MessageParcel& reply) +{ + uint32_t result = GetMainDisplayId(); + if (!reply.WriteUint32(result)) { + DISPLAY_HILOGE(MODULE_SERVICE, "Failed to write GetMainDisplayIdStub return value"); + return E_WRITE_PARCEL_ERROR; + } + return ERR_OK; +} int32_t DisplayPowerMgrStub::SetBrightnessStub(MessageParcel& data, MessageParcel& reply) { @@ -108,7 +138,21 @@ int32_t DisplayPowerMgrStub::AdjustBrightnessStub(MessageParcel& data, MessagePa bool ret = AdjustBrightness(id, value, duration); if (!reply.WriteBool(ret)) { - DISPLAY_HILOGE(MODULE_SERVICE, "Failed to write SetBrightness return value"); + DISPLAY_HILOGE(MODULE_SERVICE, "Failed to write AdjustBrightnessStub return value"); + return E_WRITE_PARCEL_ERROR; + } + return ERR_OK; +} + +int32_t DisplayPowerMgrStub::AutoAdjustBrightnessStub(MessageParcel& data, MessageParcel& reply) +{ + bool enable = 0; + + READ_PARCEL_WITH_RET(data, Bool, enable, E_READ_PARCEL_ERROR); + + bool ret = AutoAdjustBrightness(enable); + if (!reply.WriteBool(ret)) { + DISPLAY_HILOGE(MODULE_SERVICE, "Failed to write AutoAdjustBrightnessStub return value"); return E_WRITE_PARCEL_ERROR; } return ERR_OK; @@ -126,10 +170,20 @@ int32_t DisplayPowerMgrStub::SetStateConfigStub(MessageParcel& data, MessageParc bool ret = SetStateConfig(id, static_cast(state), value); if (!reply.WriteBool(ret)) { - DISPLAY_HILOGE(MODULE_SERVICE, "Failed to write SetBrightness return value"); + DISPLAY_HILOGE(MODULE_SERVICE, "Failed to write SetStateConfigStub return value"); return E_WRITE_PARCEL_ERROR; } return ERR_OK; } + +int32_t DisplayPowerMgrStub::RegisterCallbackStub(MessageParcel& data, MessageParcel& reply) +{ + sptr obj = data.ReadRemoteObject(); + RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR); + sptr callback = iface_cast(obj); + RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR); + RegisterCallback(callback); + return ERR_OK; +} } // namespace DisplayPowerMgr } // namespace OHOS diff --git a/test/native/unittest/BUILD.gn b/test/native/unittest/BUILD.gn index fd2098edb624d4eaae8937b56bf156c67c23ff5e..a5f28fe38b73eeef71efeef78b382df907ac8a0f 100644 --- a/test/native/unittest/BUILD.gn +++ b/test/native/unittest/BUILD.gn @@ -22,6 +22,7 @@ config("module_private_config") { include_dirs = [ "include", + "//base/sensors/sensor/interfaces/native/include", "//utils/system/safwk/native/include", "//foundation/appexecfwk/standard/interfaces/innerkits/libeventhandler/include", ] @@ -51,5 +52,6 @@ ohos_unittest("unittest_display_mgr_service") { "ipc:ipc_core", "safwk:system_ability_fwk", "samgr_standard:samgr_proxy", + "sensor:sensor_interface_native", ] } diff --git a/test/native/unittest/src/display_power_mgr_service_test.cpp b/test/native/unittest/src/display_power_mgr_service_test.cpp index ff3cc9e203e93c40b30babf0bb1007dfff953084..20461d0597ade098ae8bda3877dbe67c3c61b80c 100644 --- a/test/native/unittest/src/display_power_mgr_service_test.cpp +++ b/test/native/unittest/src/display_power_mgr_service_test.cpp @@ -17,6 +17,7 @@ #include #include +#include #include "display_power_mgr_client.h" #include "display_power_mgr_service.h" @@ -200,4 +201,64 @@ HWTEST_F(DisplayPowerMgrServiceTest, DisplayPowerMgrService012, TestSize.Level0) DisplayState state = DisplayPowerMgrClient::GetInstance().GetDisplayState(); EXPECT_TRUE(state == DisplayState::DISPLAY_UNKNOWN); } -} \ No newline at end of file + +/** + * @tc.name: DisplayPowerMgrService013 + * @tc.desc: Test GetDisplayIds + * @tc.type: FUNC + */ +HWTEST_F(DisplayPowerMgrServiceTest, DisplayPowerMgrService013, TestSize.Level0) +{ + DISPLAY_HILOGI(MODULE_SERVICE, "DisplayPowerMgrService013: fun is start"); + std::vector ret = DisplayPowerMgrClient::GetInstance().GetDisplayIds(); + sleep(5); + EXPECT_TRUE(ret.size() != 0); +} + +/** + * @tc.name: DisplayPowerMgrService014 + * @tc.desc: Test GetDisplayIds + * @tc.type: FUNC + */ +HWTEST_F(DisplayPowerMgrServiceTest, DisplayPowerMgrService014, TestSize.Level0) +{ + DISPLAY_HILOGI(MODULE_SERVICE, "DisplayPowerMgrService014: fun is start"); + int32_t ret = DisplayPowerMgrClient::GetInstance().GetMainDisplayId(); + sleep(5); + EXPECT_TRUE(ret == 0); +} + +/** + * @tc.name: DisplayPowerMgrService015 + * @tc.desc: Test GetDisplayIds + * @tc.type: FUNC + */ +HWTEST_F(DisplayPowerMgrServiceTest, DisplayPowerMgrService015, TestSize.Level0) +{ + DISPLAY_HILOGI(MODULE_SERVICE, "DisplayPowerMgrService015: fun is start"); + bool ret = DisplayPowerMgrClient::GetInstance().AutoAdjustBrightness(true); + sleep(5); + if (ret) { + DISPLAY_HILOGI(MODULE_SERVICE, "AutoAdjustBrightness: is supported"); + ret = DisplayPowerMgrClient::GetInstance().AutoAdjustBrightness(false); + EXPECT_TRUE(ret); + } else { + DISPLAY_HILOGI(MODULE_SERVICE, "AutoAdjustBrightness: is not supported"); + EXPECT_FALSE(ret); + } +} + +/** + * @tc.name: DisplayPowerMgrService016 + * @tc.desc: Test GetDisplayIds + * @tc.type: FUNC + */ +HWTEST_F(DisplayPowerMgrServiceTest, DisplayPowerMgrService016, TestSize.Level0) +{ + DISPLAY_HILOGI(MODULE_SERVICE, "DisplayPowerMgrService016: fun is start"); + bool ret = DisplayPowerMgrClient::GetInstance().AdjustBrightness(0, 3000); + sleep(5); + EXPECT_TRUE(ret); + DISPLAY_HILOGI(MODULE_SERVICE, "DisplayPowerMgrService016: fun is end"); +} +} diff --git a/utils/native/include/delayed_sp_singleton.h b/utils/native/include/delayed_sp_singleton.h new file mode 100644 index 0000000000000000000000000000000000000000..3bd17a2f62f5a2d11348418e7a7d421ae53a30a6 --- /dev/null +++ b/utils/native/include/delayed_sp_singleton.h @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef POWERMGR_SP_SINGLETON_H +#define POWERMGR_SP_SINGLETON_H + +#include +#include +#include + +#include "nocopyable.h" + +namespace OHOS { +namespace DisplayPowerMgr { +template +class DelayedSpSingleton : public NoCopyable { +public: + static sptr GetInstance(); + static void DestroyInstance(); + +private: + static sptr instance_; + static std::mutex mutex_; +}; + +template +sptr DelayedSpSingleton::instance_ = nullptr; + +template +std::mutex DelayedSpSingleton::mutex_; + +template +sptr DelayedSpSingleton::GetInstance() +{ + if (!instance_) { + std::lock_guard lock(mutex_); + if (instance_ == nullptr) { + instance_ = new T(); + } + } + + return instance_; +} + +template +void DelayedSpSingleton::DestroyInstance() +{ + std::lock_guard lock(mutex_); + if (instance_) { + instance_.clear(); + instance_ = nullptr; + } +} +} // namespace PowerMgr +} // namespace OHOS +#endif