diff --git a/frameworks/dialog/dialog_ui/js/BUILD.gn b/frameworks/dialog/dialog_ui/js/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..7ef2602722ab4d8527787eb941e49bc527048ee1 --- /dev/null +++ b/frameworks/dialog/dialog_ui/js/BUILD.gn @@ -0,0 +1,27 @@ +# 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. + +import("//base/powermgr/power_manager/powermgr.gni") +import("//foundation/ace/ace_engine/build/ace_gen_sa_dialog_js.gni") + +gen_sa_dialog_js("dialog_js_files_etc") { + project_path = "//base/powermgr/power_manager/frameworks/dialog/dialog_ui/js" + i18n_files = [ + "i18n/en-US.json", + "i18n/zh-CN.json", + ] + common_files = [] + dialog_name = "power_dialog" + part_name = "${powermgr_native_part_name}" + subsystem_name = "powermgr" +} diff --git a/frameworks/dialog/dialog_ui/js/common/.gitkeep b/frameworks/dialog/dialog_ui/js/common/.gitkeep new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/frameworks/dialog/dialog_ui/js/i18n/en-US.json b/frameworks/dialog/dialog_ui/js/i18n/en-US.json new file mode 100644 index 0000000000000000000000000000000000000000..b734dae15f37486018feb8e5daa48d73a39895a4 --- /dev/null +++ b/frameworks/dialog/dialog_ui/js/i18n/en-US.json @@ -0,0 +1,8 @@ +{ + "message":{ + "title": "Do you want to shut down ?", + "shutdownButton": "Power Off", + "rebootButton": "Restart", + "cancelButton": "Cancel" + } +} diff --git a/frameworks/dialog/dialog_ui/js/i18n/zh-CN.json b/frameworks/dialog/dialog_ui/js/i18n/zh-CN.json new file mode 100644 index 0000000000000000000000000000000000000000..61eb14b407db2a445ac8d5583f84f3e4383c89aa --- /dev/null +++ b/frameworks/dialog/dialog_ui/js/i18n/zh-CN.json @@ -0,0 +1,8 @@ +{ + "message": { + "title": "请确认是否关机", + "shutdownButton": "关机", + "rebootButton": "重启", + "cancelButton": "取消" + } +} diff --git a/frameworks/dialog/dialog_ui/js/pages/index/index.css b/frameworks/dialog/dialog_ui/js/pages/index/index.css new file mode 100644 index 0000000000000000000000000000000000000000..1972e2ad66bb9211674d63cb0db468bfb56aaa73 --- /dev/null +++ b/frameworks/dialog/dialog_ui/js/pages/index/index.css @@ -0,0 +1,25 @@ +.container { + display: flex; + flex-direction: row; + justify-content: center; + align-items: center; + left: 0px; + top: 0px; + width: 100%; + height: 100%; +} + +.title { + font-size: 60px; + text-align: center; + width: 100%; + height: 40%; + margin: 10px; +} + +.btn { + width: 50%; + height: 100px; + font-size: 40px; + margin: 10px; +} diff --git a/frameworks/dialog/dialog_ui/js/pages/index/index.hml b/frameworks/dialog/dialog_ui/js/pages/index/index.hml new file mode 100644 index 0000000000000000000000000000000000000000..1998976a21d5889179ac25f95d3a115d4edc0f88 --- /dev/null +++ b/frameworks/dialog/dialog_ui/js/pages/index/index.hml @@ -0,0 +1,11 @@ +
+ + + +
diff --git a/frameworks/dialog/dialog_ui/js/pages/index/index.js b/frameworks/dialog/dialog_ui/js/pages/index/index.js new file mode 100644 index 0000000000000000000000000000000000000000..9fe1b17dca65b76c1bf7a322b1f25738db80dcda --- /dev/null +++ b/frameworks/dialog/dialog_ui/js/pages/index/index.js @@ -0,0 +1,25 @@ +import router from '@ohos.router' + +export default { + data: { + message: router.getParams().message, + shutdownButton: router.getParams().shutdownButton, + rebootButton: router.getParams().rebootButton, + cancelButton: router.getParams().cancelButton, + }, + onInit() { + console.info('getParams: ' + router.getParams()); + }, + onShutdown() { + console.info('click shutdown'); + callNativeHandler("EVENT_SHUTDOWN", "shutdown"); + }, + onReboot() { + console.info('click reboot'); + callNativeHandler("EVENT_REBOOT", "reboot"); + }, + onCancel() { + console.info('click cancel'); + callNativeHandler("EVENT_CANCEL", "cancel"); + } +} diff --git a/frameworks/native/power_mgr_client.cpp b/frameworks/native/power_mgr_client.cpp index c205db8e69bf7178d31fe8af464e9d9f408f3eb6..a0901413a49f41df6cf7ac63a0691f3935be0823 100644 --- a/frameworks/native/power_mgr_client.cpp +++ b/frameworks/native/power_mgr_client.cpp @@ -257,12 +257,12 @@ uint32_t PowerMgrClient::GetDeviceMode() return proxy_->GetDeviceMode(); } -int PowerMgrClient::Dump(int32_t fd, const std::vector& args) +std::string PowerMgrClient::Dump(const std::vector& args) { - RETURN_IF_WITH_RET(Connect() != ERR_OK, ERR_NO_INIT); + std::string error = "can't connect service"; + RETURN_IF_WITH_RET(Connect() != ERR_OK, error); POWER_HILOGE(MODULE_INNERKIT, "%{public}s called.", __func__); - auto serviceRemote = proxy_->AsObject(); - return serviceRemote->Dump(fd, args); + return proxy_->ShellDump(args, args.size()); } } // namespace PowerMgr } // namespace OHOS diff --git a/interfaces/innerkits/native/include/ipower_mgr.h b/interfaces/innerkits/native/include/ipower_mgr.h index fe22d9380ff9c4d42bf7b51dfe960551c7a7f60f..45bb5d8a7265b75765d37e4c04313296b9386c2c 100644 --- a/interfaces/innerkits/native/include/ipower_mgr.h +++ b/interfaces/innerkits/native/include/ipower_mgr.h @@ -17,6 +17,7 @@ #define POWERMGR_IPOWER_MANAGER_H #include +#include #include #include @@ -56,7 +57,8 @@ public: UNREG_POWER_MODE_CALLBACK, SET_DISPLAY_SUSPEND, SETMODE_DEVICE, - GETMODE_DEVICE + GETMODE_DEVICE, + SHELL_DUMP }; virtual void CreateRunningLock(const sptr& token, const RunningLockInfo& runningLockInfo) = 0; @@ -93,6 +95,7 @@ public: virtual void SetDisplaySuspend(bool enable) = 0; virtual void SetDeviceMode(const uint32_t& mode) = 0; virtual uint32_t GetDeviceMode() = 0; + virtual std::string ShellDump(const std::vector& args, uint32_t argc) = 0; DECLARE_INTERFACE_DESCRIPTOR(u"ohos.powermgr.IPowerMgr"); }; diff --git a/interfaces/innerkits/native/include/power_mgr_client.h b/interfaces/innerkits/native/include/power_mgr_client.h index 6a4c0f46f1cbc84c9b4d529232ffe67a3ab8c06b..489cad753f7c81bca5da9f8f3bf441881b52a07e 100644 --- a/interfaces/innerkits/native/include/power_mgr_client.h +++ b/interfaces/innerkits/native/include/power_mgr_client.h @@ -127,7 +127,7 @@ public: void UnRegisterShutdownCallback(const sptr& callback); void RegisterPowerModeCallback(const sptr& callback); void UnRegisterPowerModeCallback(const sptr& callback); - int Dump(int32_t fd, const std::vector& args); + std::string Dump(const std::vector& args); private: class PowerMgrDeathRecipient : public IRemoteObject::DeathRecipient { diff --git a/services/BUILD.gn b/services/BUILD.gn index 5c6bfdb2af0ca2a227415753e2327e40d6b63458..fab8fc551424d41e27ec7eb5657d569a8606612e 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -15,9 +15,12 @@ import("//base/powermgr/power_manager/powermgr.gni") config("powermgr_private_config") { include_dirs = [ - "//utils/system/safwk/native/include", + "//base/global/resmgr_standard/interfaces/innerkits/include/", "//base/sensors/sensor/interfaces/native/include", "//foundation/multimodalinput/input/interfaces/native/innerkits/proxy/include", + "//foundation/windowmanager/interfaces/innerkits/wm/", + "//third_party/flutter/skia/", + "//utils/system/safwk/native/include", ] } @@ -57,10 +60,12 @@ ohos_shared_library("powermgrservice") { public_configs = [ ":powermgr_public_config" ] deps = [ + "${powermgr_framework_path}/dialog/dialog_ui/js:dialog_js_files_etc", "${powermgr_native_innerkits_path}:powermgr_client", "${powermgr_utils_path}:powermgr_utils", "native/src/actions:powermgr_actions", "//drivers/peripheral/display/hal:hdi_display_device", + "//foundation/ace/ace_engine/interfaces/innerkits/ui_service_manager:ui_service_mgr", "//utils/native/base:utils", ] diff --git a/services/native/include/power_mgr_service.h b/services/native/include/power_mgr_service.h index 84d38cf1494b16314d574d3da28efc7e5fff96a0..fcf3bbe183e30b286984755573a354f121c3b15c 100644 --- a/services/native/include/power_mgr_service.h +++ b/services/native/include/power_mgr_service.h @@ -77,7 +77,9 @@ public: virtual void SetDisplaySuspend(bool enable) override; virtual void SetDeviceMode(const uint32_t& mode) override; virtual uint32_t GetDeviceMode() override; + virtual std::string ShellDump(const std::vector& args, uint32_t argc) override; + void HandleShowdownRequest(); void HandleKeyEvent(int32_t keyCode); void HandlePointEvent(); std::shared_ptr GetHandler() const diff --git a/services/native/include/powerms_event_handler.h b/services/native/include/powerms_event_handler.h index b131288722b478672d5af3e6f76ec09457f67b43..ab5f8dd2d73916f9111915300efe308658f77c1c 100644 --- a/services/native/include/powerms_event_handler.h +++ b/services/native/include/powerms_event_handler.h @@ -32,6 +32,7 @@ public: CHECK_USER_ACTIVITY_SLEEP_TIMEOUT_MSG = 4, SYSTEM_WAKE_UP_MSG = 5, POWER_KEY_TIMEOUT_MSG = 6, + SHUTDOWN_REQUEST_MSG = 7, }; PowermsEventHandler(const std::shared_ptr& runner, diff --git a/services/native/src/power_mgr_dumper.cpp b/services/native/src/power_mgr_dumper.cpp index a842c5975c5450a4a4c3be6259f6956442d997d3..2cdea1548e721053e5747f233708c5486e8d9d47 100644 --- a/services/native/src/power_mgr_dumper.cpp +++ b/services/native/src/power_mgr_dumper.cpp @@ -26,7 +26,8 @@ const std::string ARGS_ALL = "-a"; const std::string ARGS_HELP = "-h"; const std::string ARGS_RUNNINGLOCK = "-r"; const std::string ARGS_STATE = "-s"; -const std::string ARGS_HDF = "-d"; +const std::string ARGS_HDF = "-f"; +const std::string ARGS_DIALOG = "-d"; } bool PowerMgrDumper::Dump(const std::vector& args, std::string& result) @@ -41,6 +42,10 @@ bool PowerMgrDumper::Dump(const std::vector& args, std::string& res if (pms == nullptr) { return true; } + if (args[0] == ARGS_DIALOG) { + pms->HandleShowdownRequest(); + return true; + } for (auto it = args.begin(); it != args.end(); it++) { if (*it == ARGS_RUNNINGLOCK) { auto runningLockMgr = pms->GetRunningLockMgr(); @@ -84,7 +89,8 @@ void PowerMgrDumper::ShowUsage(std::string& result) .append(" -h: show this help.\n") .append(" -r: show the information of runninglock.\n") .append(" -s: show the information of power state machine.\n") - .append(" -d: show the information of power hdf.\n"); + .append(" -f: show the information of power hdf.\n") + .append(" -d: show power off dialog.\n"); } } // namespace PowerMgr } // namespace OHOS diff --git a/services/native/src/power_mgr_service.cpp b/services/native/src/power_mgr_service.cpp index 9cd937d99beed8d19eb2ea63e8f38f80447c6dff..66327197065fe6f2612bf8f94676e21ebc080b04 100644 --- a/services/native/src/power_mgr_service.cpp +++ b/services/native/src/power_mgr_service.cpp @@ -29,6 +29,8 @@ #include "permission.h" #include "power_common.h" #include "power_mgr_dumper.h" +#include "ui_service_mgr_client.h" +#include "wm_common.h" namespace OHOS { namespace PowerMgr { @@ -163,7 +165,7 @@ void PowerMgrService::KeyMonitorInit() int32_t id = InputManager::GetInstance()->SubscribeKeyEvent(keyOption, [this](std::shared_ptr keyEvent) { POWER_HILOGI(MODULE_SERVICE, "Receive long press powerkey"); - this->ShutDownDevice(REASON_POWER_KEY); + handler_->RemoveEvent(PowermsEventHandler::SHUTDOWN_REQUEST_MSG); }); keyOption.reset(); @@ -199,6 +201,40 @@ void PowerMgrService::KeyMonitorInit() InputManager::GetInstance()->AddMonitor(std::static_pointer_cast(callback)); } +void PowerMgrService::HandleShowdownRequest() +{ + POWER_HILOGI(MODULE_SERVICE, "HandleShutdown"); + // show dialog + const std::string params = "{\"title\":\"Show down!\"," \ + "\"message\":\"Do you want to shut down?\", \"shutdownButton\":\"Shutdown\", " \ + "\"rebootButton\":\"Reboot\", \"cancelButton\":\"Cancel\"}"; + const int POSTION_X = 200; + const int POSTION_Y = 200; + const int WIDTH = 500; + const int HEIGHT = 500; + Ace::UIServiceMgrClient::GetInstance()->ShowDialog( + "power_dialog", + params, + OHOS::Rosen::WindowType::WINDOW_TYPE_SYSTEM_ALARM_WINDOW, + POSTION_X, + POSTION_Y, + WIDTH, + HEIGHT, + [this](int32_t id, const std::string& event, const std::string& params) { + POWER_HILOGI(MODULE_SERVICE, "Dialog callback: %{public}s, %{public}s", + event.c_str(), params.c_str()); + if (event == "EVENT_SHUTDOWN") { + this->ShutDownDevice(REASON_POWER_KEY); + } else if (event == "EVENT_REBOOT") { + this->RebootDevice(REASON_POWER_KEY); + } else if (event == "EVENT_CANCEL") { + Ace::UIServiceMgrClient::GetInstance()->CancelDialog(id); + } + }); + + return; +} + void PowerMgrService::HandlePowerKeyUp() { POWER_HILOGI(MODULE_SERVICE, "Receive release powerkey"); @@ -693,5 +729,17 @@ uint32_t PowerMgrService::GetDeviceMode() POWER_HILOGI(MODULE_SERVICE, "PID: %{public}d Call %{public}s !", pid, __func__); return powerModeModule_.GetModeItem(); } + +std::string PowerMgrService::ShellDump(const std::vector& args, uint32_t argc) +{ + std::lock_guard lock(mutex_); + pid_t pid = IPCSkeleton::GetCallingPid(); + POWER_HILOGI(MODULE_SERVICE, "PID: %{public}d Call %{public}s !", pid, __func__); + + std::string result; + bool ret = PowerMgrDumper::Dump(args, result); + POWER_HILOGI(MODULE_SERVICE, "PowerMgrDumper :%{public}d", ret); + return result; +} } // namespace PowerMgr } // namespace OHOS diff --git a/services/native/src/powerms_event_handler.cpp b/services/native/src/powerms_event_handler.cpp index ffaecc64f0ef9a6f4834f09b5b06e9f5a84a1eda..0509d23eda51f52a968a3db720ab93ef9f7f0b24 100644 --- a/services/native/src/powerms_event_handler.cpp +++ b/services/native/src/powerms_event_handler.cpp @@ -59,6 +59,10 @@ void PowermsEventHandler::ProcessEvent([[maybe_unused]] const AppExecFwk::InnerE pmsptr->HandlePowerKeyTimeout(); break; } + case SHUTDOWN_REQUEST_MSG: { + pmsptr->HandleShowdownRequest(); + break; + } default: POWER_HILOGD(MODULE_SERVICE, "PowermsEventHandler::no event id matched."); } diff --git a/services/zidl/include/power_mgr_proxy.h b/services/zidl/include/power_mgr_proxy.h index d2c0a958c27a02d32ace0b03878a5d75273ad75c..2aadd7fb82d7ed06ed450baa718131acf01dfdf1 100644 --- a/services/zidl/include/power_mgr_proxy.h +++ b/services/zidl/include/power_mgr_proxy.h @@ -59,6 +59,7 @@ public: virtual void SetDisplaySuspend(bool enable) override; virtual void SetDeviceMode(const uint32_t& mode) override; virtual uint32_t GetDeviceMode() override; + virtual std::string ShellDump(const std::vector& args, uint32_t argc) override; private: static inline BrokerDelegator delegator_; }; diff --git a/services/zidl/include/power_mgr_stub.h b/services/zidl/include/power_mgr_stub.h index 1ff2a3aa075dbcdf7758690c111c35386c9cb52c..c8260d61c2ed95cad2daf3a41830fc5ee90c4ee6 100644 --- a/services/zidl/include/power_mgr_stub.h +++ b/services/zidl/include/power_mgr_stub.h @@ -56,6 +56,7 @@ private: int32_t SetDisplaySuspendStub(MessageParcel& data); int32_t SetDeviceModeStub(MessageParcel& data); int32_t GetDeviceModeStub(MessageParcel& reply); + int32_t ShellDumpStub(MessageParcel& data, MessageParcel& reply); }; } // namespace PowerMgr } // namespace OHOS diff --git a/services/zidl/src/power_mgr_proxy.cpp b/services/zidl/src/power_mgr_proxy.cpp index 65372a2fca883a28ea1361d0454f9ea2d51f1ee7..9f49284d9bcfb096c989e190fe0fa0e8a4662883 100644 --- a/services/zidl/src/power_mgr_proxy.cpp +++ b/services/zidl/src/power_mgr_proxy.cpp @@ -641,5 +641,36 @@ uint32_t PowerMgrProxy::GetDeviceMode() POWER_HILOGD(MODULE_SERVICE, "PowerMgrStub::GetDeviceMode, cmd = %{public}u.", used); return used; } + +std::string PowerMgrProxy::ShellDump(const std::vector& args, uint32_t argc) +{ + sptr remote = Remote(); + std::string result = "remote error"; + RETURN_IF_WITH_RET(remote == nullptr, result); + + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) { + POWER_HILOGE(MODULE_INNERKIT, "PowerMgrProxy::%{public}s write descriptor failed!", __func__); + return 0; + } + + data.WriteUint32(argc); + for (uint32_t i = 0; i < argc; i++) { + data.WriteString(args[i]); + } + int ret = remote->SendRequest(static_cast(IPowerMgr::SHELL_DUMP), + data, reply, option); + if (ret != ERR_OK) { + POWER_HILOGE(MODULE_INNERKIT, + "PowerMgrProxy::%{public}s SendRequest is failed, error code: %{public}d", __func__, ret); + return result; + } + result = reply.ReadString(); + + return result; +} } // namespace PowerMgr } // namespace OHOS diff --git a/services/zidl/src/power_mgr_stub.cpp b/services/zidl/src/power_mgr_stub.cpp index 1edc9d6518fbdb6944ed6a94d74cc94458c7c338..1bd895cff6cbec81c09e25b940c24fb170577a39 100644 --- a/services/zidl/src/power_mgr_stub.cpp +++ b/services/zidl/src/power_mgr_stub.cpp @@ -114,6 +114,9 @@ int PowerMgrStub::OnRemoteRequest(uint32_t code, MessageParcel &data, case static_cast(IPowerMgr::GETMODE_DEVICE): { return GetDeviceModeStub(reply); } + case static_cast(IPowerMgr::SHELL_DUMP): { + return ShellDumpStub(data, reply); + } default: { return IPCObjectStub::OnRemoteRequest(code, data, reply, option); } @@ -378,5 +381,32 @@ int32_t PowerMgrStub::GetDeviceModeStub(MessageParcel& reply) } return ERR_OK; } + +int32_t PowerMgrStub::ShellDumpStub(MessageParcel& data, MessageParcel& reply) +{ + uint32_t argc; + std::vector args; + + if (!data.ReadUint32(argc)) { + POWER_HILOGE(MODULE_INNERKIT, "Readback fail!"); + return E_READ_PARCEL_ERROR; + } + + for (uint32_t i = 0; i < argc; i++) { + std::string arg = data.ReadString(); + if (!arg.empty()) { + args.push_back(arg); + } else { + POWER_HILOGE(MODULE_INNERKIT, "read value fail: %{public}d", i); + } + } + + std::string ret = ShellDump(args, argc); + if (!reply.WriteString(ret)) { + POWER_HILOGE(MODULE_SERVICE, "PowerMgrStub:: Dump Writeback Fail!"); + return E_WRITE_PARCEL_ERROR; + } + return ERR_OK; +} } // namespace PowerMgr } // namespace OHOS diff --git a/utils/shell/src/power_shell_command.cpp b/utils/shell/src/power_shell_command.cpp index bdcbfba39a30eebdbc371d44357998b971e76f6c..c560b2c5f94569be35dc8926729e52d56cf7b303 100644 --- a/utils/shell/src/power_shell_command.cpp +++ b/utils/shell/src/power_shell_command.cpp @@ -118,33 +118,18 @@ extern "C" void PrintDumpFileError(std::string& receiver, const char* path) receiver.append(path); receiver.append(") failed: "); receiver.append(std::to_string(errno)); + receiver.append("\n"); } ErrCode PowerShellCommand::RunAsDumpCommand() { resultReceiver_.clear(); - int fd = open(TEMP_DUMP_LOG_PATH, - O_RDWR | O_APPEND | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO); - if (fd < 0) { - PrintDumpFileError(resultReceiver_, TEMP_DUMP_LOG_PATH); - return ERR_OK; - } - std::vector args; - for (int i = 2; i < argc_; i++) { - std::string str(argv_[i]); - args.push_back(Str8ToStr16(str)); - } + PowerMgrClient &client = PowerMgrClient::GetInstance(); - int ret = client.Dump(fd, args); - resultReceiver_.append("Power Dump result: "); - resultReceiver_.append(std::to_string(ret)); - resultReceiver_.append("\n"); - char buff[DUMP_BUFF_SIZE]; - while (read(fd, buff, sizeof(buff) - 1) > 0) { - resultReceiver_.append(buff); - } - close(fd); - remove(TEMP_DUMP_LOG_PATH); + std::string ret = client.Dump(argList_); + resultReceiver_.append("Power Dump result: \n"); + resultReceiver_.append(ret); + return ERR_OK; } }