15ccb8f90Sopenharmony_ci/* 25ccb8f90Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 35ccb8f90Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 45ccb8f90Sopenharmony_ci * you may not use this file except in compliance with the License. 55ccb8f90Sopenharmony_ci * You may obtain a copy of the License at 65ccb8f90Sopenharmony_ci * 75ccb8f90Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 85ccb8f90Sopenharmony_ci * 95ccb8f90Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 105ccb8f90Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 115ccb8f90Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 125ccb8f90Sopenharmony_ci * See the License for the specific language governing permissions and 135ccb8f90Sopenharmony_ci * limitations under the License. 145ccb8f90Sopenharmony_ci */ 155ccb8f90Sopenharmony_ci 165ccb8f90Sopenharmony_ci#include <dlfcn.h> 175ccb8f90Sopenharmony_ci#include <string> 185ccb8f90Sopenharmony_ci#include "power_log.h" 195ccb8f90Sopenharmony_ci#include "power_utils.h" 205ccb8f90Sopenharmony_ci 215ccb8f90Sopenharmony_cinamespace OHOS { 225ccb8f90Sopenharmony_cinamespace PowerMgr { 235ccb8f90Sopenharmony_ciconst std::string PowerUtils::GetReasonTypeString(StateChangeReason type) 245ccb8f90Sopenharmony_ci{ 255ccb8f90Sopenharmony_ci switch (type) { 265ccb8f90Sopenharmony_ci case StateChangeReason::STATE_CHANGE_REASON_INIT: 275ccb8f90Sopenharmony_ci return std::string("INIT"); 285ccb8f90Sopenharmony_ci case StateChangeReason::STATE_CHANGE_REASON_TIMEOUT: 295ccb8f90Sopenharmony_ci return std::string("TIMEOUT"); 305ccb8f90Sopenharmony_ci case StateChangeReason::STATE_CHANGE_REASON_RUNNING_LOCK: 315ccb8f90Sopenharmony_ci return std::string("RUNNING_LOCK"); 325ccb8f90Sopenharmony_ci case StateChangeReason::STATE_CHANGE_REASON_BATTERY: 335ccb8f90Sopenharmony_ci return std::string("BATTERY"); 345ccb8f90Sopenharmony_ci case StateChangeReason::STATE_CHANGE_REASON_THERMAL: 355ccb8f90Sopenharmony_ci return std::string("THERMAL"); 365ccb8f90Sopenharmony_ci case StateChangeReason::STATE_CHANGE_REASON_WORK: 375ccb8f90Sopenharmony_ci return std::string("WORK"); 385ccb8f90Sopenharmony_ci case StateChangeReason::STATE_CHANGE_REASON_SYSTEM: 395ccb8f90Sopenharmony_ci return std::string("SYSTEM"); 405ccb8f90Sopenharmony_ci case StateChangeReason::STATE_CHANGE_REASON_APPLICATION: 415ccb8f90Sopenharmony_ci return std::string("APPLICATION"); 425ccb8f90Sopenharmony_ci case StateChangeReason::STATE_CHANGE_REASON_SETTINGS: 435ccb8f90Sopenharmony_ci return std::string("SETTINGS"); 445ccb8f90Sopenharmony_ci case StateChangeReason::STATE_CHANGE_REASON_HARD_KEY: 455ccb8f90Sopenharmony_ci return std::string("HARD_KEY"); 465ccb8f90Sopenharmony_ci case StateChangeReason::STATE_CHANGE_REASON_TOUCH: 475ccb8f90Sopenharmony_ci return std::string("TOUCH"); 485ccb8f90Sopenharmony_ci case StateChangeReason::STATE_CHANGE_REASON_CABLE: 495ccb8f90Sopenharmony_ci return std::string("CABLE"); 505ccb8f90Sopenharmony_ci case StateChangeReason::STATE_CHANGE_REASON_SENSOR: 515ccb8f90Sopenharmony_ci return std::string("SENSOR"); 525ccb8f90Sopenharmony_ci case StateChangeReason::STATE_CHANGE_REASON_LID: 535ccb8f90Sopenharmony_ci return std::string("LID"); 545ccb8f90Sopenharmony_ci case StateChangeReason::STATE_CHANGE_REASON_CAMERA: 555ccb8f90Sopenharmony_ci return std::string("CAMERA"); 565ccb8f90Sopenharmony_ci case StateChangeReason::STATE_CHANGE_REASON_ACCESSIBILITY: 575ccb8f90Sopenharmony_ci return std::string("ACCESS"); 585ccb8f90Sopenharmony_ci case StateChangeReason::STATE_CHANGE_REASON_RESET: 595ccb8f90Sopenharmony_ci return std::string("RESET"); 605ccb8f90Sopenharmony_ci case StateChangeReason::STATE_CHANGE_REASON_POWER_KEY: 615ccb8f90Sopenharmony_ci return std::string("POWER_KEY"); 625ccb8f90Sopenharmony_ci case StateChangeReason::STATE_CHANGE_REASON_KEYBOARD: 635ccb8f90Sopenharmony_ci return std::string("KEYBOARD"); 645ccb8f90Sopenharmony_ci case StateChangeReason::STATE_CHANGE_REASON_MOUSE: 655ccb8f90Sopenharmony_ci return std::string("MOUSE"); 665ccb8f90Sopenharmony_ci case StateChangeReason::STATE_CHANGE_REASON_DOUBLE_CLICK: 675ccb8f90Sopenharmony_ci return std::string("DOUBLE_CLICK"); 685ccb8f90Sopenharmony_ci case StateChangeReason::STATE_CHANGE_REASON_PEN: 695ccb8f90Sopenharmony_ci return std::string("PEN"); 705ccb8f90Sopenharmony_ci case StateChangeReason::STATE_CHANGE_REASON_SWITCH: 715ccb8f90Sopenharmony_ci return std::string("SWITCH"); 725ccb8f90Sopenharmony_ci case StateChangeReason::STATE_CHANGE_REASON_PRE_BRIGHT: 735ccb8f90Sopenharmony_ci return std::string("PRE_BRIGHT"); 745ccb8f90Sopenharmony_ci case StateChangeReason::STATE_CHANGE_REASON_PRE_BRIGHT_AUTH_SUCCESS: 755ccb8f90Sopenharmony_ci return std::string("PRE_BRIGHT_ATUH_SUCCESS"); 765ccb8f90Sopenharmony_ci case StateChangeReason::STATE_CHANGE_REASON_PRE_BRIGHT_AUTH_FAIL_SCREEN_ON: 775ccb8f90Sopenharmony_ci return std::string("PRE_BRIGHT_ATUH_FAIL_SCREEN_ON"); 785ccb8f90Sopenharmony_ci case StateChangeReason::STATE_CHANGE_REASON_PRE_BRIGHT_AUTH_FAIL_SCREEN_OFF: 795ccb8f90Sopenharmony_ci return std::string("PRE_BRIGHT_ATUH_FAIL_SCREEN_OFF"); 805ccb8f90Sopenharmony_ci case StateChangeReason::STATE_CHANGE_REASON_AOD_SLIDING: 815ccb8f90Sopenharmony_ci return std::string("AOD_SLIDING"); 825ccb8f90Sopenharmony_ci case StateChangeReason::STATE_CHANGE_REASON_REMOTE: 835ccb8f90Sopenharmony_ci return std::string("REMOTE"); 845ccb8f90Sopenharmony_ci case StateChangeReason::STATE_CHANGE_REASON_REFRESH: 855ccb8f90Sopenharmony_ci return std::string("REFRESH"); 865ccb8f90Sopenharmony_ci case StateChangeReason::STATE_CHANGE_REASON_COORDINATION: 875ccb8f90Sopenharmony_ci return std::string("COORDINATION_OVERRIDE"); 885ccb8f90Sopenharmony_ci case StateChangeReason::STATE_CHANGE_REASON_PROXIMITY: 895ccb8f90Sopenharmony_ci return std::string("PROXIMITY"); 905ccb8f90Sopenharmony_ci case StateChangeReason::STATE_CHANGE_REASON_INCOMING_CALL: 915ccb8f90Sopenharmony_ci return std::string("INCOMING_CALL"); 925ccb8f90Sopenharmony_ci case StateChangeReason::STATE_CHANGE_REASON_SHELL: 935ccb8f90Sopenharmony_ci return std::string("SHELL"); 945ccb8f90Sopenharmony_ci case StateChangeReason::STATE_CHANGE_REASON_TIMEOUT_NO_SCREEN_LOCK: 955ccb8f90Sopenharmony_ci return std::string("TIMEOUT_NO_SCREEN_LOCK"); 965ccb8f90Sopenharmony_ci case StateChangeReason::STATE_CHANGE_REASON_PICKUP: 975ccb8f90Sopenharmony_ci return std::string("PICKUP"); 985ccb8f90Sopenharmony_ci case StateChangeReason::STATE_CHANGE_REASON_EXIT_SYSTEM_STR: 995ccb8f90Sopenharmony_ci return std::string("EXIT_SYSTEM_STR"); 1005ccb8f90Sopenharmony_ci case StateChangeReason::STATE_CHANGE_REASON_UNKNOWN: 1015ccb8f90Sopenharmony_ci return std::string("UNKNOWN"); 1025ccb8f90Sopenharmony_ci default: 1035ccb8f90Sopenharmony_ci break; 1045ccb8f90Sopenharmony_ci } 1055ccb8f90Sopenharmony_ci return std::string("UNKNOWN"); 1065ccb8f90Sopenharmony_ci} 1075ccb8f90Sopenharmony_ci 1085ccb8f90Sopenharmony_ciconst std::string PowerUtils::GetPowerStateString(PowerState state) 1095ccb8f90Sopenharmony_ci{ 1105ccb8f90Sopenharmony_ci switch (state) { 1115ccb8f90Sopenharmony_ci case PowerState::AWAKE: 1125ccb8f90Sopenharmony_ci return std::string("AWAKE"); 1135ccb8f90Sopenharmony_ci case PowerState::FREEZE: 1145ccb8f90Sopenharmony_ci return std::string("FREEZE"); 1155ccb8f90Sopenharmony_ci case PowerState::INACTIVE: 1165ccb8f90Sopenharmony_ci return std::string("INACTIVE"); 1175ccb8f90Sopenharmony_ci case PowerState::STAND_BY: 1185ccb8f90Sopenharmony_ci return std::string("STAND_BY"); 1195ccb8f90Sopenharmony_ci case PowerState::DOZE: 1205ccb8f90Sopenharmony_ci return std::string("DOZE"); 1215ccb8f90Sopenharmony_ci case PowerState::SLEEP: 1225ccb8f90Sopenharmony_ci return std::string("SLEEP"); 1235ccb8f90Sopenharmony_ci case PowerState::HIBERNATE: 1245ccb8f90Sopenharmony_ci return std::string("HIBERNATE"); 1255ccb8f90Sopenharmony_ci case PowerState::SHUTDOWN: 1265ccb8f90Sopenharmony_ci return std::string("SHUTDOWN"); 1275ccb8f90Sopenharmony_ci case PowerState::DIM: 1285ccb8f90Sopenharmony_ci return std::string("DIM"); 1295ccb8f90Sopenharmony_ci case PowerState::UNKNOWN: 1305ccb8f90Sopenharmony_ci return std::string("UNKNOWN"); 1315ccb8f90Sopenharmony_ci default: 1325ccb8f90Sopenharmony_ci break; 1335ccb8f90Sopenharmony_ci } 1345ccb8f90Sopenharmony_ci return std::string("UNKNOWN"); 1355ccb8f90Sopenharmony_ci} 1365ccb8f90Sopenharmony_ci 1375ccb8f90Sopenharmony_ciconst std::string PowerUtils::GetDisplayStateString(DisplayState state) 1385ccb8f90Sopenharmony_ci{ 1395ccb8f90Sopenharmony_ci switch (state) { 1405ccb8f90Sopenharmony_ci case DisplayState::DISPLAY_OFF: 1415ccb8f90Sopenharmony_ci return std::string("DISPLAY_OFF"); 1425ccb8f90Sopenharmony_ci case DisplayState::DISPLAY_DIM: 1435ccb8f90Sopenharmony_ci return std::string("DISPLAY_DIM"); 1445ccb8f90Sopenharmony_ci case DisplayState::DISPLAY_ON: 1455ccb8f90Sopenharmony_ci return std::string("DISPLAY_ON"); 1465ccb8f90Sopenharmony_ci case DisplayState::DISPLAY_SUSPEND: 1475ccb8f90Sopenharmony_ci return std::string("DISPLAY_SUSPEND"); 1485ccb8f90Sopenharmony_ci case DisplayState::DISPLAY_UNKNOWN: 1495ccb8f90Sopenharmony_ci return std::string("DISPLAY_UNKNOWN"); 1505ccb8f90Sopenharmony_ci default: 1515ccb8f90Sopenharmony_ci break; 1525ccb8f90Sopenharmony_ci } 1535ccb8f90Sopenharmony_ci return std::string("DISPLAY_UNKNOWN"); 1545ccb8f90Sopenharmony_ci} 1555ccb8f90Sopenharmony_ci 1565ccb8f90Sopenharmony_ciconst std::string PowerUtils::GetRunningLockTypeString(RunningLockType type) 1575ccb8f90Sopenharmony_ci{ 1585ccb8f90Sopenharmony_ci switch (type) { 1595ccb8f90Sopenharmony_ci case RunningLockType::RUNNINGLOCK_SCREEN: 1605ccb8f90Sopenharmony_ci return "SCREEN"; 1615ccb8f90Sopenharmony_ci case RunningLockType::RUNNINGLOCK_BACKGROUND: 1625ccb8f90Sopenharmony_ci return "BACKGROUND"; 1635ccb8f90Sopenharmony_ci case RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL: 1645ccb8f90Sopenharmony_ci return "PROXIMITY_SCREEN_CONTROL"; 1655ccb8f90Sopenharmony_ci case RunningLockType::RUNNINGLOCK_COORDINATION: 1665ccb8f90Sopenharmony_ci return "RUNNINGLOCK_COORDINATION"; 1675ccb8f90Sopenharmony_ci case RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE: 1685ccb8f90Sopenharmony_ci return "BACKGROUND_PHONE"; 1695ccb8f90Sopenharmony_ci case RunningLockType::RUNNINGLOCK_BACKGROUND_NOTIFICATION: 1705ccb8f90Sopenharmony_ci return "BACKGROUND_NOTIFICATION"; 1715ccb8f90Sopenharmony_ci case RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO: 1725ccb8f90Sopenharmony_ci return "BACKGROUND_AUDIO"; 1735ccb8f90Sopenharmony_ci case RunningLockType::RUNNINGLOCK_BACKGROUND_SPORT: 1745ccb8f90Sopenharmony_ci return "BACKGROUND_SPORT"; 1755ccb8f90Sopenharmony_ci case RunningLockType::RUNNINGLOCK_BACKGROUND_NAVIGATION: 1765ccb8f90Sopenharmony_ci return "BACKGROUND_NAVIGATION"; 1775ccb8f90Sopenharmony_ci case RunningLockType::RUNNINGLOCK_BACKGROUND_TASK: 1785ccb8f90Sopenharmony_ci return "BACKGROUND_TASK"; 1795ccb8f90Sopenharmony_ci case RunningLockType::RUNNINGLOCK_BUTT: 1805ccb8f90Sopenharmony_ci return "BUTT"; 1815ccb8f90Sopenharmony_ci default: 1825ccb8f90Sopenharmony_ci break; 1835ccb8f90Sopenharmony_ci } 1845ccb8f90Sopenharmony_ci return "UNKNOWN"; 1855ccb8f90Sopenharmony_ci} 1865ccb8f90Sopenharmony_ci 1875ccb8f90Sopenharmony_ciconst std::string PowerUtils::JsonToSimpleStr(const std::string& json) 1885ccb8f90Sopenharmony_ci{ 1895ccb8f90Sopenharmony_ci std::string str; 1905ccb8f90Sopenharmony_ci for (auto ch : json) { 1915ccb8f90Sopenharmony_ci if (ch != ' ' && ch != '\n') { 1925ccb8f90Sopenharmony_ci str += ch; 1935ccb8f90Sopenharmony_ci } 1945ccb8f90Sopenharmony_ci } 1955ccb8f90Sopenharmony_ci return str; 1965ccb8f90Sopenharmony_ci} 1975ccb8f90Sopenharmony_ci 1985ccb8f90Sopenharmony_cibool PowerUtils::IsForegroundApplication(const std::string& appName) 1995ccb8f90Sopenharmony_ci{ 2005ccb8f90Sopenharmony_ci void* handler = dlopen("libpower_ability.z.so", RTLD_NOW); 2015ccb8f90Sopenharmony_ci if (handler == nullptr) { 2025ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_UTIL, "dlopen libpower_ability.z.so failed, reason : %{public}s", dlerror()); 2035ccb8f90Sopenharmony_ci return false; 2045ccb8f90Sopenharmony_ci } 2055ccb8f90Sopenharmony_ci 2065ccb8f90Sopenharmony_ci auto powerIsForegroundApplicationFunc = 2075ccb8f90Sopenharmony_ci reinterpret_cast<bool (*)(const std::string&)>(dlsym(handler, "PowerIsForegroundApplication")); 2085ccb8f90Sopenharmony_ci if (powerIsForegroundApplicationFunc == nullptr) { 2095ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_UTIL, "find PowerIsForegroundApplication function failed, reason : %{public}s", dlerror()); 2105ccb8f90Sopenharmony_ci#ifndef FUZZ_TEST 2115ccb8f90Sopenharmony_ci dlclose(handler); 2125ccb8f90Sopenharmony_ci#endif 2135ccb8f90Sopenharmony_ci handler = nullptr; 2145ccb8f90Sopenharmony_ci return false; 2155ccb8f90Sopenharmony_ci } 2165ccb8f90Sopenharmony_ci bool isForeground = powerIsForegroundApplicationFunc(appName); 2175ccb8f90Sopenharmony_ci#ifndef FUZZ_TEST 2185ccb8f90Sopenharmony_ci dlclose(handler); 2195ccb8f90Sopenharmony_ci#endif 2205ccb8f90Sopenharmony_ci handler = nullptr; 2215ccb8f90Sopenharmony_ci return isForeground; 2225ccb8f90Sopenharmony_ci} 2235ccb8f90Sopenharmony_ci} // namespace PowerMgr 2245ccb8f90Sopenharmony_ci} // namespace OHOS 225