From 9c1c0c4bba7b0270f0ca993b437addb0f0c0a2a1 Mon Sep 17 00:00:00 2001 From: Du Jiang Date: Wed, 23 Mar 2022 17:15:13 +0800 Subject: [PATCH 1/2] Add settings helper to monitor brightness Signed-off-by: zhaoruiyuan Change-Id: I95aa0fa2e864ad1ca7a173a0367adf0ca4f786cd --- service/BUILD.gn | 9 + .../include/display_power_mgr_service.h | 5 + .../include/display_power_settings_observer.h | 31 ++++ .../display_power_settings_rdb_helper.h | 75 ++++++++ .../native/src/display_power_mgr_service.cpp | 54 ++++++ .../src/display_power_settings_observer.cpp | 39 ++++ .../src/display_power_settings_rdb_helper.cpp | 169 ++++++++++++++++++ 7 files changed, 382 insertions(+) create mode 100644 service/native/include/display_power_settings_observer.h create mode 100644 service/native/include/display_power_settings_rdb_helper.h create mode 100644 service/native/src/display_power_settings_observer.cpp create mode 100644 service/native/src/display_power_settings_rdb_helper.cpp diff --git a/service/BUILD.gn b/service/BUILD.gn index da617b5..ce363f1 100644 --- a/service/BUILD.gn +++ b/service/BUILD.gn @@ -19,6 +19,7 @@ config("displaymgr_private_config") { "//base/hiviewdfx/hicollie/interfaces/native/innerkits/include/xcollie", "//base/powermgr/power_manager/interfaces/innerkits/native/include", "//base/sensors/sensor/interfaces/native/include", + "//foundation/aafwk/standard/interfaces/innerkits/dataobs_manager:dataobs_manager", "//foundation/multimedia/image_standard/interfaces/innerkits/include", ] } @@ -33,6 +34,8 @@ config("displaymgr_public_config") { ohos_shared_library("displaymgrservice") { sources = [ "native/src/display_power_mgr_service.cpp", + "native/src/display_power_settings_observer.cpp", + "native/src/display_power_settings_rdb_helper.cpp", "native/src/display_system_ability.cpp", "native/src/gradual_animator.cpp", "native/src/screen_action.cpp", @@ -51,6 +54,8 @@ ohos_shared_library("displaymgrservice") { deps = [ "${displaymgr_native_innerkits_path}:displaymgr", "//drivers/peripheral/display/hal:hdi_display_device", + "//foundation/aafwk/standard/frameworks/kits/ability/native:abilitykit_native", + "//foundation/aafwk/standard/interfaces/innerkits/dataobs_manager:dataobs_manager", "//foundation/graphic/standard:libsurface", "//foundation/graphic/standard:libwmservice", "//foundation/graphic/standard/rosen/modules/render_service_client:librender_service_client", @@ -59,12 +64,16 @@ ohos_shared_library("displaymgrservice") { ] external_deps = [ + "ability_base:want", + "ability_base:zuri", "bundle_framework:appexecfwk_base", "eventhandler:libeventhandler", "hicollie_native:libhicollie", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", "multimedia_image_standard:image_native", + "native_appdatamgr:native_dataability", + "native_appdatamgr:native_rdb", "safwk:system_ability_fwk", "samgr_standard:samgr_proxy", "sensor:sensor_interface_native", diff --git a/service/native/include/display_power_mgr_service.h b/service/native/include/display_power_mgr_service.h index 87d09ab..799f31f 100644 --- a/service/native/include/display_power_mgr_service.h +++ b/service/native/include/display_power_mgr_service.h @@ -23,6 +23,7 @@ #include "delayed_sp_singleton.h" #include "display_common.h" #include "display_power_mgr_stub.h" +#include "display_power_settings_observer.h" #include "screen_controller.h" #include "sensor_agent.h" @@ -72,6 +73,9 @@ private: int32_t GetBrightnessFromLightScalar(float scalar); void ActivateAmbientSensor(); void DeactivateAmbientSensor(); + void InitBrightness(); + void RegisterDatabaseObserver(); + void UnRegisterDatabaseObserver(); std::shared_ptr action_; @@ -82,6 +86,7 @@ private: SensorUser user_; sptr callback_; sptr cbDeathRecipient_; + sptr settingsObserver_; time_t lastLuxTime_ {0}; float lastLux_ {0}; diff --git a/service/native/include/display_power_settings_observer.h b/service/native/include/display_power_settings_observer.h new file mode 100644 index 0000000..8c4a2d6 --- /dev/null +++ b/service/native/include/display_power_settings_observer.h @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2022 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 DISPLAY_POWER_SETTINGS_OBSERVER_H +#define DISPLAY_POWER_SETTINGS_OBSERVER_H + +#include "data_ability_observer_stub.h" + +namespace OHOS { +namespace DisplayPowerMgr { +class DisplayPowerSettingsObserver : public AAFwk::DataAbilityObserverStub { +public: + DisplayPowerSettingsObserver(); + ~DisplayPowerSettingsObserver(); + void OnChange() override; +}; +} // namespace DisplayPowerMgr +} // namespace OHOS +#endif // DISPLAY_POWER_SETTINGS_OBSERVER_H \ No newline at end of file diff --git a/service/native/include/display_power_settings_rdb_helper.h b/service/native/include/display_power_settings_rdb_helper.h new file mode 100644 index 0000000..24776d3 --- /dev/null +++ b/service/native/include/display_power_settings_rdb_helper.h @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2022 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 DISPLAY_POWER_SETTINGS_RDB_HELPER_H +#define DISPLAY_POWER_SETTINGS_RDB_HELPER_H + +#include +#include +#include + +#include "abs_shared_result_set.h" +#include "data_ability_helper.h" +#include "data_ability_predicates.h" +#include "iservice_registry.h" +#include "result_set.h" +#include "system_ability_definition.h" +#include "uri.h" +#include "values_bucket.h" + +namespace OHOS { +namespace DisplayPowerMgr { +class DisplayPowerSettingsRdbHelper : public DelayedSingleton { + DECLARE_DELAYED_SINGLETON(DisplayPowerSettingsRdbHelper); +public: + enum class SettingsItem { + BRIGHTNESS = 1, + SETTING_MAX, + }; + inline static const std::string BRIGHTNESS_SETTING_URI = + "dataability:///com.ohos.settingsdata.DataAbility/settings.screen.brightness"; + inline static const std::string BRIGHTNESS_COLUMN = "VALUE"; + /** + * Registers an observer to DataObsMgr specified by the given Uri. + * + * @param item, Indicates the settings item. + * @param dataObserver, Indicates the IDataAbilityObserver object. + */ + void RegisterSettingsObserver(SettingsItem item, const sptr &dataObserver); + + /** + * Deregisters an observer used for DataObsMgr specified by the given Uri. + * + * @param item, Indicates the settings item. + * @param dataObserver, Indicates the IDataAbilityObserver object. + */ + void UnRegisterSettingsObserver(SettingsItem item, const sptr &dataObserver); + + /** + * Notifies the registered observers of a change to the data resource specified by Uri. + * + * @param item, Indicates the settings item. + */ + void NotifyChange(SettingsItem item); + int GetValue(SettingsItem item); + void PutValue(SettingsItem item, int value); + std::shared_ptr CreateDataAbilityHelper(); + +private: + std::shared_ptr settingHelper_; +}; +} // namespace DisplayPowerMgr +} // namespace OHOS +#endif // DISPLAY_POWER_SETTINGS_RDB_HELPER_H diff --git a/service/native/src/display_power_mgr_service.cpp b/service/native/src/display_power_mgr_service.cpp index 8d0b781..d716b43 100644 --- a/service/native/src/display_power_mgr_service.cpp +++ b/service/native/src/display_power_mgr_service.cpp @@ -20,6 +20,8 @@ #include #include "display_log.h" +#include "display_power_settings_observer.h" +#include "display_power_settings_rdb_helper.h" namespace OHOS { namespace DisplayPowerMgr { @@ -35,11 +37,14 @@ DisplayPowerMgrService::DisplayPowerMgrService() } callback_ = nullptr; cbDeathRecipient_ = nullptr; + settingsObserver_ = nullptr; InitSensors(); + RegisterDatabaseObserver(); } DisplayPowerMgrService::~DisplayPowerMgrService() { + UnRegisterDatabaseObserver(); } bool DisplayPowerMgrService::SetDisplayState(uint32_t id, DisplayState state, uint32_t reason) @@ -49,6 +54,18 @@ bool DisplayPowerMgrService::SetDisplayState(uint32_t id, DisplayState state, ui if (iterater == controllerMap_.end()) { return false; } + + // test settings + std::shared_ptr settingHelper = DisplayPowerSettingsRdbHelper::GetInstance(); + if (settingHelper != nullptr) { + int value = settingHelper->GetValue(DisplayPowerSettingsRdbHelper::SettingsItem::BRIGHTNESS); + DISPLAY_HILOGI(COMP_SVC, "GetValue brightness from settings: %{public}d", value); + } else { + DISPLAY_HILOGI(COMP_SVC, "settingHelper is nullptr"); + } + + // test settings + if (id == GetMainDisplayId()) { DISPLAY_HILOGI(COMP_SVC, "change ambient sensor status"); if (state == DisplayState::DISPLAY_ON) { @@ -394,5 +411,42 @@ void DisplayPowerMgrService::CallbackDeathRecipient::OnRemoteDied(const wptrcallback_ = nullptr; } + +void DisplayPowerMgrService::InitBrightness() +{ + std::shared_ptr settingHelper = DisplayPowerSettingsRdbHelper::GetInstance(); + if (settingHelper == nullptr) { + DISPLAY_HILOGI(COMP_SVC, "InitBrightness no settingHelper"); + return; + } + int value = settingHelper->GetValue(DisplayPowerSettingsRdbHelper::SettingsItem::BRIGHTNESS); + DISPLAY_HILOGI(COMP_SVC, "current brightness is %{public}d", value); +} + +void DisplayPowerMgrService::RegisterDatabaseObserver() +{ + if (settingsObserver_ == nullptr) { + settingsObserver_ = new DisplayPowerSettingsObserver(); + } + std::shared_ptr settingHelper = DisplayPowerSettingsRdbHelper::GetInstance(); + if (settingHelper == nullptr) { + DISPLAY_HILOGI(COMP_SVC, "RegisterDatabaseObserver no settingHelper"); + return; + } + settingHelper->RegisterSettingsObserver( + DisplayPowerSettingsRdbHelper::SettingsItem::BRIGHTNESS, settingsObserver_); +} + +void DisplayPowerMgrService::UnRegisterDatabaseObserver() +{ + std::shared_ptr settingHelper = DisplayPowerSettingsRdbHelper::GetInstance(); + if (settingHelper == nullptr) { + DISPLAY_HILOGI(COMP_SVC, "UnRegisterDatabaseObserver no settingHelper"); + return; + } + settingHelper->UnRegisterSettingsObserver( + DisplayPowerSettingsRdbHelper::SettingsItem::BRIGHTNESS, settingsObserver_); + settingsObserver_ = nullptr; +} } // namespace DisplayPowerMgr } // namespace OHOS diff --git a/service/native/src/display_power_settings_observer.cpp b/service/native/src/display_power_settings_observer.cpp new file mode 100644 index 0000000..00549df --- /dev/null +++ b/service/native/src/display_power_settings_observer.cpp @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2022 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. + */ + +#include "display_power_settings_observer.h" + +#include "display_common.h" +#include "display_log.h" +#include "display_power_settings_rdb_helper.h" + +namespace OHOS { +namespace DisplayPowerMgr { +DisplayPowerSettingsObserver::DisplayPowerSettingsObserver() +{} + +DisplayPowerSettingsObserver::~DisplayPowerSettingsObserver() = default; + +void DisplayPowerSettingsObserver::OnChange() +{ + std::shared_ptr settingHelper = DisplayPowerSettingsRdbHelper::GetInstance(); + if (settingHelper == nullptr) { + return; + } + int value = settingHelper->GetValue(DisplayPowerSettingsRdbHelper::SettingsItem::BRIGHTNESS); + DISPLAY_HILOGI(COMP_SVC, "brightness turn to %{public}d", value); +} +} // namespace DisplayPowerMgr +} // namespace OHOS \ No newline at end of file diff --git a/service/native/src/display_power_settings_rdb_helper.cpp b/service/native/src/display_power_settings_rdb_helper.cpp new file mode 100644 index 0000000..fe15782 --- /dev/null +++ b/service/native/src/display_power_settings_rdb_helper.cpp @@ -0,0 +1,169 @@ +/* + * Copyright (C) 2022 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. + */ + +#include "display_power_settings_rdb_helper.h" + +#include +#include +#include "display_common.h" +#include "display_log.h" + +namespace OHOS { +namespace DisplayPowerMgr { +DisplayPowerSettingsRdbHelper::DisplayPowerSettingsRdbHelper() +{ + settingHelper_ = CreateDataAbilityHelper(); +} + +DisplayPowerSettingsRdbHelper::~DisplayPowerSettingsRdbHelper() {} + +std::shared_ptr DisplayPowerSettingsRdbHelper::CreateDataAbilityHelper() +{ + sptr saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (saManager == nullptr) { + DISPLAY_HILOGE(COMP_SVC, "DisplayPowerRdbHelper GetSystemAbilityManager failed."); + return nullptr; + } + sptr remoteObj = saManager->GetSystemAbility(TELEPHONY_CELLULAR_DATA_SYS_ABILITY_ID); + if (remoteObj == nullptr) { + DISPLAY_HILOGE(COMP_SVC, "DisplayPowerRdbHelper GetSystemAbility Service Failed."); + return nullptr; + } + return AppExecFwk::DataAbilityHelper::Creator(remoteObj); +} + +void DisplayPowerSettingsRdbHelper::UnRegisterSettingsObserver( + SettingsItem item, const sptr &dataObserver) +{ + if (settingHelper_ == nullptr) { + DISPLAY_HILOGE(COMP_SVC, "UnRegister settings observer failed by nullptr"); + return; + } + Uri uri = Uri(""); + switch (item) { + case SettingsItem::BRIGHTNESS: + uri = Uri(BRIGHTNESS_SETTING_URI); + break; + default: + break; + } + settingHelper_->UnregisterObserver(uri, dataObserver); + settingHelper_->Release(); +} + +void DisplayPowerSettingsRdbHelper::RegisterSettingsObserver( + SettingsItem item, const sptr&dataObserver) +{ + if (settingHelper_ == nullptr) { + DISPLAY_HILOGE(COMP_SVC, "Register settings observer by nullptr"); + return; + } + Uri uri = Uri(""); + switch (item) { + case SettingsItem::BRIGHTNESS: + uri = Uri(BRIGHTNESS_SETTING_URI); + break; + default: + break; + } + settingHelper_->RegisterObserver(uri, dataObserver); + settingHelper_->Release(); +} + +void DisplayPowerSettingsRdbHelper::NotifyChange(SettingsItem item) +{ + if (settingHelper_ == nullptr) { + DISPLAY_HILOGE(COMP_SVC, "notify settings changed fail by nullptr"); + return; + } + Uri uri = Uri(""); + switch (item) { + case SettingsItem::BRIGHTNESS: + uri = Uri(BRIGHTNESS_SETTING_URI); + break; + default: + break; + } + settingHelper_->NotifyChange(uri); + settingHelper_->Release(); +} + +int DisplayPowerSettingsRdbHelper::GetValue(SettingsItem item) +{ + int resultValue = 0; + if (settingHelper_ == nullptr) { + DISPLAY_HILOGE(COMP_SVC, "helper_ is null"); + return -1; + } + std::string column; + Uri uri = Uri(""); + switch (item) { + case SettingsItem::BRIGHTNESS: + column = BRIGHTNESS_COLUMN; + uri = Uri(BRIGHTNESS_SETTING_URI); + break; + default: + break; + } + NativeRdb::DataAbilityPredicates predicates; + std::vector columns; + columns.emplace_back(column); + std::shared_ptr result = settingHelper_->Query(uri, columns, predicates); + if (result == nullptr) { + DISPLAY_HILOGE(COMP_SVC, "setting DB: query error"); + return -1; + } + settingHelper_->Release(); + result->GoToFirstRow(); + int columnIndex; + result->GetColumnIndex(column, columnIndex); + result->GetInt(columnIndex, resultValue); + result->Close(); + DISPLAY_HILOGI(COMP_SVC, "Query end resultValue is %{public}d", resultValue); + return resultValue; +} + +void DisplayPowerSettingsRdbHelper::PutValue(SettingsItem item, int value) +{ + if (settingHelper_ == nullptr) { + DISPLAY_HILOGE(COMP_SVC, "helper_ is null"); + return; + } + std::string column; + Uri uri = Uri(""); + switch (item) { + case SettingsItem::BRIGHTNESS: + column = BRIGHTNESS_COLUMN; + uri = Uri(BRIGHTNESS_SETTING_URI); + break; + default: + break; + } + int existValue = GetValue(item); + NativeRdb::ValuesBucket bucket; + bucket.PutInt(column, value); + int32_t result; + if (existValue < 0) { + result = settingHelper_->Insert(uri, bucket); + } else { + NativeRdb::DataAbilityPredicates predicates; + result = settingHelper_->Update(uri, bucket, predicates); + } + DISPLAY_HILOGI(COMP_SVC, "put value return %{public}d", result); + settingHelper_->NotifyChange(uri); + settingHelper_->Release(); +} +} // namespace DisplayPowerMgr +} // namespace OHOS \ No newline at end of file -- Gitee From 0488827c6e2492ff277b788fbbf8000956a4b995 Mon Sep 17 00:00:00 2001 From: Du Jiang Date: Wed, 13 Apr 2022 10:18:30 +0800 Subject: [PATCH 2/2] [Test]Change service id to DISPLAY_MANAGER_SERVICE_ID Change-Id: Ib378896cd0ad1950742bd9066c74eb67e2365273 Signed-off-by: Du Jiang --- service/native/src/display_power_mgr_service.cpp | 3 ++- service/native/src/display_power_settings_rdb_helper.cpp | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/service/native/src/display_power_mgr_service.cpp b/service/native/src/display_power_mgr_service.cpp index d716b43..6c7fcf3 100644 --- a/service/native/src/display_power_mgr_service.cpp +++ b/service/native/src/display_power_mgr_service.cpp @@ -39,7 +39,6 @@ DisplayPowerMgrService::DisplayPowerMgrService() cbDeathRecipient_ = nullptr; settingsObserver_ = nullptr; InitSensors(); - RegisterDatabaseObserver(); } DisplayPowerMgrService::~DisplayPowerMgrService() @@ -56,6 +55,8 @@ bool DisplayPowerMgrService::SetDisplayState(uint32_t id, DisplayState state, ui } // test settings + RegisterDatabaseObserver(); + std::shared_ptr settingHelper = DisplayPowerSettingsRdbHelper::GetInstance(); if (settingHelper != nullptr) { int value = settingHelper->GetValue(DisplayPowerSettingsRdbHelper::SettingsItem::BRIGHTNESS); diff --git a/service/native/src/display_power_settings_rdb_helper.cpp b/service/native/src/display_power_settings_rdb_helper.cpp index fe15782..2fad06f 100644 --- a/service/native/src/display_power_settings_rdb_helper.cpp +++ b/service/native/src/display_power_settings_rdb_helper.cpp @@ -36,7 +36,7 @@ std::shared_ptr DisplayPowerSettingsRdbHelper::Cr DISPLAY_HILOGE(COMP_SVC, "DisplayPowerRdbHelper GetSystemAbilityManager failed."); return nullptr; } - sptr remoteObj = saManager->GetSystemAbility(TELEPHONY_CELLULAR_DATA_SYS_ABILITY_ID); + sptr remoteObj = saManager->GetSystemAbility(DISPLAY_MANAGER_SERVICE_ID); if (remoteObj == nullptr) { DISPLAY_HILOGE(COMP_SVC, "DisplayPowerRdbHelper GetSystemAbility Service Failed."); return nullptr; -- Gitee