feat: prevent accidental contact during exercise

Signed-off-by: yanxuejun <yanxuejun@h-partners.com>
This commit is contained in:
yanxuejun 2024-09-04 02:22:00 +00:00
parent 10c75c2f58
commit 763ae9f00f
9 changed files with 197 additions and 8 deletions

View File

@ -31,6 +31,7 @@
"power_manager_feature_enable_s4",
"power_manager_feature_doubleclick_or_pickup",
"power_manager_feature_force_sleep_broadcast",
"power_manager_feature_movement",
"power_manager_feature_screen_on_timeout_check",
"power_manager_feature_dialog_path",
"power_manager_feature_charging_type_setting"

View File

@ -23,6 +23,7 @@
"uid" : "powermgr",
"apl" : "system_core",
"permission" : [
"ohos.permission.ACTIVITY_MOTION",
"ohos.permission.INPUT_MONITORING",
"ohos.permission.PERMISSION_USED_STATS",
"ohos.permission.DISTRIBUTED_SOFTBUS_CENTER",

View File

@ -26,6 +26,7 @@ declare_args() {
power_manager_feature_screen_on_timeout_check = false
power_manager_feature_doubleclick_or_pickup = true
power_manager_feature_force_sleep_broadcast = false
power_manager_feature_movement = true
power_manager_feature_charging_type_setting = false
}

View File

@ -125,6 +125,10 @@ ohos_shared_library("powermgrservice") {
defines = []
if (power_manager_feature_movement) {
defines += [ "MSDP_MOVEMENT_ENABLE" ]
}
if (use_libfuzzer) {
defines += [ "FUZZ_TEST" ]
}

View File

@ -308,6 +308,11 @@ private:
int32_t timeOutMS = -1);
static void RegisterBootCompletedCallback();
static bool IsDeveloperMode();
#ifdef MSDP_MOVEMENT_ENABLE
void RegisterMovementCallback();
void UnRegisterMovementCallback();
void ResetMovementState();
#endif
inline PowerModeModule& GetPowerModeModule()
{

View File

@ -100,6 +100,9 @@ public:
bool IsCollaborationScreenOn();
void Reset();
int64_t GetSleepTime();
#ifdef MSDP_MOVEMENT_ENABLE
bool IsMovementStateOn();
#endif
PowerState GetState()
{

View File

@ -51,6 +51,9 @@
#ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING
#include "battery_srv_client.h"
#endif
#ifdef MSDP_MOVEMENT_ENABLE
#include <dlfcn.h>
#endif
using namespace OHOS::AppExecFwk;
using namespace OHOS::AAFwk;
@ -93,6 +96,9 @@ void PowerMgrService::OnStart()
AddSystemAbilityListener(DEVICE_STANDBY_SERVICE_SYSTEM_ABILITY_ID);
AddSystemAbilityListener(DISPLAY_MANAGER_SERVICE_ID);
AddSystemAbilityListener(DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID);
#ifdef MSDP_MOVEMENT_ENABLE
AddSystemAbilityListener(MSDP_MOVEMENT_SERVICE_ID);
#endif
SystemSuspendController::GetInstance().RegisterHdiStatusListener();
if (!Publish(DelayedSpSingleton<PowerMgrService>::GetInstance())) {
POWER_HILOGE(COMP_SVC, "Register to system ability manager failed");
@ -577,6 +583,9 @@ void PowerMgrService::OnStop()
RemoveSystemAbilityListener(SUSPEND_MANAGER_SYSTEM_ABILITY_ID);
RemoveSystemAbilityListener(DEVICE_STANDBY_SERVICE_SYSTEM_ABILITY_ID);
RemoveSystemAbilityListener(DISPLAY_MANAGER_SERVICE_ID);
#ifdef MSDP_MOVEMENT_ENABLE
RemoveSystemAbilityListener(MSDP_MOVEMENT_SERVICE_ID);
#endif
#ifdef POWER_WAKEUPDOUBLE_OR_PICKUP_ENABLE
SettingHelper::UnregisterSettingWakeupDoubleObserver();
SettingHelper::UnregisterSettingWakeupPickupObserver();
@ -586,6 +595,9 @@ void PowerMgrService::OnStop()
if (!OHOS::EventFwk::CommonEventManager::UnSubscribeCommonEvent(subscriberPtr_)) {
POWER_HILOGE(COMP_SVC, "Power Onstop unregister to commonevent manager failed!");
}
#ifdef MSDP_MOVEMENT_ENABLE
UnRegisterMovementCallback();
#endif
}
void PowerMgrService::Reset()
@ -610,6 +622,16 @@ void PowerMgrService::OnRemoveSystemAbility(int32_t systemAbilityId, const std::
std::lock_guard lock(lockMutex_);
runningLockMgr_->ResetRunningLocks();
}
#ifdef MSDP_MOVEMENT_ENABLE
if (systemAbilityId == MSDP_MOVEMENT_SERVICE_ID) {
auto power = DelayedSpSingleton<PowerMgrService>::GetInstance();
if (power == nullptr) {
POWER_HILOGI(COMP_SVC, "get PowerMgrService fail");
return;
}
power->ResetMovementState();
}
#endif
}
void PowerMgrService::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
@ -624,8 +646,101 @@ void PowerMgrService::OnAddSystemAbility(int32_t systemAbilityId, const std::str
if (systemAbilityId == DISPLAY_MANAGER_SERVICE_ID) {
RegisterBootCompletedCallback();
}
#ifdef MSDP_MOVEMENT_ENABLE
if (systemAbilityId == MSDP_MOVEMENT_SERVICE_ID) {
auto power = DelayedSpSingleton<PowerMgrService>::GetInstance();
if (power == nullptr) {
POWER_HILOGI(COMP_SVC, "get PowerMgrService fail");
return;
}
power->UnRegisterMovementCallback();
power->RegisterMovementCallback();
}
#endif
}
#ifdef MSDP_MOVEMENT_ENABLE
static const char* MOVEMENT_SUBSCRIBER_CONFIG = "RegisterMovementCallback";
static const char* MOVEMENT_UNSUBSCRIBER_CONFIG = "UnRegisterMovementCallback";
static const char* RESET_MOVEMENT_STATE_CONFIG = "ResetMovementState";
static const char* POWER_MANAGER_EXT_PATH = "/system/lib64/libpower_manager_ext.z.so";
typedef void(*FuncMovementSubscriber)();
typedef void(*FuncMovementUnsubscriber)();
typedef void(*FuncResetMovementState)();
void PowerMgrService::RegisterMovementCallback()
{
POWER_HILOGI(COMP_SVC, "Start to RegisterMovementCallback");
void *subscriberHandler = dlopen(POWER_MANAGER_EXT_PATH, RTLD_LAZY | RTLD_NODELETE);
if (subscriberHandler == nullptr) {
POWER_HILOGE(COMP_SVC, "Dlopen RegisterMovementCallback failed, reason : %{public}s", dlerror());
return;
}
FuncMovementSubscriber MovementSubscriberFlag =
reinterpret_cast<FuncMovementSubscriber>(dlsym(subscriberHandler, MOVEMENT_SUBSCRIBER_CONFIG));
if (MovementSubscriberFlag == nullptr) {
POWER_HILOGE(COMP_SVC, "RegisterMovementCallback is null, reason : %{public}s", dlerror());
dlclose(subscriberHandler);
subscriberHandler = nullptr;
return;
}
MovementSubscriberFlag();
POWER_HILOGI(COMP_SVC, "RegisterMovementCallback Success");
dlclose(subscriberHandler);
subscriberHandler = nullptr;
return;
}
void PowerMgrService::UnRegisterMovementCallback()
{
POWER_HILOGI(COMP_SVC, "Start to UnRegisterMovementCallback");
void *unSubscriberHandler = dlopen(POWER_MANAGER_EXT_PATH, RTLD_LAZY | RTLD_NODELETE);
if (unSubscriberHandler == nullptr) {
POWER_HILOGE(COMP_SVC, "Dlopen UnRegisterMovementCallback failed, reason : %{public}s", dlerror());
return;
}
FuncMovementUnsubscriber MovementUnsubscriberFlag =
reinterpret_cast<FuncMovementUnsubscriber>(dlsym(unSubscriberHandler, MOVEMENT_UNSUBSCRIBER_CONFIG));
if (MovementUnsubscriberFlag == nullptr) {
POWER_HILOGE(COMP_SVC, "UnRegisterMovementCallback is null, reason : %{public}s", dlerror());
dlclose(unSubscriberHandler);
unSubscriberHandler = nullptr;
return;
}
MovementUnsubscriberFlag();
POWER_HILOGI(COMP_SVC, "UnRegisterMovementCallback Success");
dlclose(unSubscriberHandler);
unSubscriberHandler = nullptr;
return;
}
void PowerMgrService::ResetMovementState()
{
POWER_HILOGI(COMP_SVC, "Start to ResetMovementState");
void *resetMovementStateHandler = dlopen(POWER_MANAGER_EXT_PATH, RTLD_LAZY | RTLD_NODELETE);
if (resetMovementStateHandler == nullptr) {
POWER_HILOGE(COMP_SVC, "Dlopen ResetMovementState failed, reason : %{public}s", dlerror());
return;
}
FuncResetMovementState ResetMovementStateFlag =
reinterpret_cast<FuncResetMovementState>(dlsym(resetMovementStateHandler, RESET_MOVEMENT_STATE_CONFIG));
if (ResetMovementStateFlag == nullptr) {
POWER_HILOGE(COMP_SVC, "ResetMovementState is null, reason : %{public}s", dlerror());
dlclose(resetMovementStateHandler);
resetMovementStateHandler = nullptr;
return;
}
ResetMovementStateFlag();
POWER_HILOGI(COMP_SVC, "ResetMovementState Success");
dlclose(resetMovementStateHandler);
resetMovementStateHandler = nullptr;
return;
}
#endif
int32_t PowerMgrService::Dump(int32_t fd, const std::vector<std::u16string>& args)
{
if (!isBootCompleted_) {

View File

@ -32,6 +32,9 @@
#include "os_account_manager.h"
#include "parameters.h"
#endif
#ifdef MSDP_MOVEMENT_ENABLE
#include <dlfcn.h>
#endif
namespace OHOS {
namespace PowerMgr {
@ -146,22 +149,59 @@ void PowerStateMachine::InitTransitMap()
});
}
#ifdef MSDP_MOVEMENT_ENABLE
static const char* MOVEMENT_STATE_CONFIG = "GetMovementState";
static const char* POWER_MANAGER_EXT_PATH = "/system/lib64/libpower_manager_ext.z.so";
typedef bool(*FuncMovementState)();
bool PowerStateMachine::IsMovementStateOn()
{
POWER_HILOGD(FEATURE_POWER_STATE, "Start to GetMovementState");
void *stateHandler = dlopen(POWER_MANAGER_EXT_PATH, RTLD_LAZY | RTLD_NODELETE);
if (stateHandler == nullptr) {
POWER_HILOGE(FEATURE_POWER_STATE, "Dlopen GetMovementState failed, reason : %{public}s", dlerror());
return false;
}
FuncMovementState MovementStateFlag = reinterpret_cast<FuncMovementState>(dlsym(stateHandler,
MOVEMENT_STATE_CONFIG));
if (MovementStateFlag == nullptr) {
POWER_HILOGE(FEATURE_POWER_STATE, "GetMovementState is null, reason : %{public}s", dlerror());
dlclose(stateHandler);
stateHandler = nullptr;
return false;
}
bool ret = MovementStateFlag();
dlclose(stateHandler);
stateHandler = nullptr;
return ret;
}
#endif
bool PowerStateMachine::CanTransitTo(PowerState to, StateChangeReason reason)
{
bool isForbidden = forbidMap_.count(currentState_) && forbidMap_[currentState_].count(to);
if (isForbidden) {
return false;
}
#ifdef HAS_SENSORS_SENSOR_PART
// prevent the double click and pickup to light up the screen when calling
// prevent the double click and pickup to light up the screen when calling or sporting
if ((reason == StateChangeReason::STATE_CHANGE_REASON_DOUBLE_CLICK ||
reason == StateChangeReason::STATE_CHANGE_REASON_PICKUP) &&
IsProximityClose() && to == PowerState::AWAKE) {
POWER_HILOGI(
FEATURE_POWER_STATE, "Double-click or pickup isn't allowed to wakeup device when proximity is close.");
return false;
}
reason == StateChangeReason::STATE_CHANGE_REASON_PICKUP) && to == PowerState::AWAKE) {
#ifdef HAS_SENSORS_SENSOR_PART
if (IsProximityClose()) {
POWER_HILOGI(FEATURE_POWER_STATE,
"Double-click or pickup isn't allowed to wakeup device when proximity is close.");
return false;
}
#endif
#ifdef MSDP_MOVEMENT_ENABLE
if (IsMovementStateOn()) {
POWER_HILOGI(FEATURE_POWER_STATE,
"Double-click or pickup isn't allowed to wakeup device when movement state is on.");
return false;
}
#endif
}
bool isAllowed = (!allowMapByReason_.count(reason) ||
(allowMapByReason_[reason].count(currentState_) && allowMapByReason_[reason][currentState_].count(to)));
return isAllowed;

View File

@ -282,4 +282,23 @@ HWTEST_F(PowerMgrServiceNativeTest, PowerMgrServiceNative011, TestSize.Level0)
POWER_HILOGI(LABEL_TEST, "PowerMgrServiceNative011 end.");
}
#ifdef MSDP_MOVEMENT_ENABLE
/**
* @tc.name: PowerMgrServiceNative012
* @tc.desc: test RegisterMovementCallback
* @tc.type: FUNC
*/
HWTEST_F(PowerMgrServiceNativeTest, PowerMgrServiceNative012, TestSize.Level0)
{
POWER_HILOGI(LABEL_TEST, "PowerMgrServiceNative012 begin.");
auto stateMachine = std::make_shared<PowerStateMachine>(g_pmsTest);
g_pmsTest->RegisterMovementCallback();
g_pmsTest->UnRegisterMovementCallback();
g_pmsTest->ResetMovementState();
bool ret = stateMachine->IsMovementStateOn();
EXPECT_TRUE(ret == false);
POWER_HILOGI(LABEL_TEST, "PowerMgrServiceNative012 end.");
}
#endif
} // namespace