From 29b13bb98e2ab9a0d99bed24a6637d5b3e0aa2ef Mon Sep 17 00:00:00 2001 From: ShiJie Date: Thu, 20 Jul 2023 10:18:14 +0800 Subject: [PATCH 1/2] feat: dialog changed to UIExtensionAbility Signed-off-by: ShiJie Change-Id: Iabdd7b0121a0c625cca2bf92a8de976bf989f0bc --- .../ServiceExtAbility/PowerDialogAbility.ts | 81 -------- .../PowerUiExtensionAbility.ts} | 16 +- .../entry/src/main/ets/pages/powerDialog.ets | 29 ++- power_dialog/entry/src/main/module.json | 20 +- services/BUILD.gn | 1 + services/native/include/power_mgr_service.h | 12 +- services/native/src/power_mgr_dumper.cpp | 4 +- services/native/src/power_mgr_service.cpp | 67 +------ .../native/src/shutdown/shutdown_dialog.cpp | 187 ++++++++++++++++++ .../native/src/shutdown/shutdown_dialog.h | 104 +++++----- 10 files changed, 297 insertions(+), 224 deletions(-) delete mode 100644 power_dialog/entry/src/main/ets/ServiceExtAbility/PowerDialogAbility.ts rename power_dialog/entry/src/main/ets/{Application/MyAbilityStage.ts => UiExtensionAbility/PowerUiExtensionAbility.ts} (55%) create mode 100644 services/native/src/shutdown/shutdown_dialog.cpp rename power_dialog/entry/src/main/ets/MainAbility/MainAbility.ts => services/native/src/shutdown/shutdown_dialog.h (33%) diff --git a/power_dialog/entry/src/main/ets/ServiceExtAbility/PowerDialogAbility.ts b/power_dialog/entry/src/main/ets/ServiceExtAbility/PowerDialogAbility.ts deleted file mode 100644 index c5d0f795..00000000 --- a/power_dialog/entry/src/main/ets/ServiceExtAbility/PowerDialogAbility.ts +++ /dev/null @@ -1,81 +0,0 @@ -/* - * 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 ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility'; -import window from '@ohos.window'; -import display from '@ohos.display'; - -const BG_COLOR = '#00000000'; -let powerWindowFirst = undefined; - -export default class PowerServiceExtAbility extends ServiceExtensionAbility { - /** - * Lifecycle function, called back when a service extension is started for initialization. - */ - onCreate(want) { - console.log('PowerServiceExtAbility onCreate' + want.abilityName); - globalThis.extensionContext = this.context; - globalThis.g_powerWindowFirst = powerWindowFirst; - } - - /** - * Lifecycle function, called back when a service extension is started or recall. - */ - onRequest(want, startId) { - globalThis.abilityWant = want; - console.log('PowerServiceExtAbility onRequest. start id is ' + startId); - console.log('want: ' + JSON.stringify(want)); - display.getDefaultDisplay().then(dis => { - let navigationBarRect = { - left: 0, - top: 0, - width: dis.width, - height: dis.height - }; - this.createWindow('Power Dialog' + startId, window.WindowType.TYPE_FLOAT, navigationBarRect); - }); - } - - /** - * Lifecycle function, called back before a service extension is destroyed. - */ - onDestroy() { - console.log('PowerServiceExtAbility onDestroy.'); - } - - private async createWindow(name: string, windowType: number, rect) { - try { - if (globalThis.g_powerWindowFirst !== undefined) { - console.log('destroy first power window'); - globalThis.g_powerWindowFirst.destroy(); - globalThis.g_powerWindowFirst = undefined; - } - const powerWin = await window.create(globalThis.extensionContext, name, windowType); - if (globalThis.g_powerWindowFirst === undefined) { - powerWindowFirst = powerWin; - globalThis.g_powerWindowFirst = powerWindowFirst; - } - globalThis.powerWindow = powerWin; - await powerWin.moveTo(rect.left, rect.top); - await powerWin.resetSize(rect.width, rect.height); - await powerWin.loadContent('pages/powerDialog'); - await powerWin.setBackgroundColor(BG_COLOR); - await powerWin.show(); - console.log('Power window create success'); - } catch { - console.log('Power window create failed'); - } - } -} diff --git a/power_dialog/entry/src/main/ets/Application/MyAbilityStage.ts b/power_dialog/entry/src/main/ets/UiExtensionAbility/PowerUiExtensionAbility.ts similarity index 55% rename from power_dialog/entry/src/main/ets/Application/MyAbilityStage.ts rename to power_dialog/entry/src/main/ets/UiExtensionAbility/PowerUiExtensionAbility.ts index 96b662f0..445f28a7 100644 --- a/power_dialog/entry/src/main/ets/Application/MyAbilityStage.ts +++ b/power_dialog/entry/src/main/ets/UiExtensionAbility/PowerUiExtensionAbility.ts @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2023 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 @@ -13,10 +13,14 @@ * limitations under the License. */ -import AbilityStage from '@ohos.app.ability.AbilityStage'; +import UIExtensionAbility from '@ohos.app.ability.UIExtensionAbility'; -export default class MyAbilityStage extends AbilityStage { - onCreate() { - console.log('MyAbilityStage onCreate'); +export default class PowerUiExtensionAbility extends UIExtensionAbility { + onSessionCreate(want, session): void { + let storage: LocalStorage = new LocalStorage({ + 'session': session + }); + session.loadContent('pages/powerDialog', storage); + session.setWindowBackgroundColor('#00000000'); } -} \ No newline at end of file +} diff --git a/power_dialog/entry/src/main/ets/pages/powerDialog.ets b/power_dialog/entry/src/main/ets/pages/powerDialog.ets index db157aaa..8700f97a 100644 --- a/power_dialog/entry/src/main/ets/pages/powerDialog.ets +++ b/power_dialog/entry/src/main/ets/pages/powerDialog.ets @@ -13,8 +13,8 @@ * limitations under the License. */ +import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession'; import power from '@ohos.power'; -import hilog from '@ohos.hilog'; import Constants from '../common/constant'; @Extend(Button) function customizeButton() { @@ -108,19 +108,32 @@ struct PowerDialog { } onCancel() { - globalThis.powerWindow.destroy(); - globalThis.g_powerWindowFirst = undefined; - globalThis.extensionContext.terminateSelf(); + try { + console.log('power dialog terminateSelf'); + let storage = LocalStorage.GetShared() + let session = storage.get('session'); + session.terminateSelf(); + } catch(err) { + console.log('power dialog cancel failed: ' + JSON.stringify(err)); + } } onShutdown() { - this.timeoutId = setTimeout(this.onCancel, this.timeoutMs); - power.shutdown("power_dialog"); + try { + this.timeoutId = setTimeout(this.onCancel, this.timeoutMs); + power.shutdown("power_dialog"); + } catch(err) { + console.log('power dialog shutdown failed: ' + JSON.stringify(err)); + } } onReboot() { - this.timeoutId = setTimeout(this.onCancel, this.timeoutMs); - power.reboot("power_dialog"); + try { + this.timeoutId = setTimeout(this.onCancel, this.timeoutMs); + power.reboot("power_dialog"); + } catch(err) { + console.log('power dialog reboot failed: ' + JSON.stringify(err)); + } } existApp() { diff --git a/power_dialog/entry/src/main/module.json b/power_dialog/entry/src/main/module.json index e2282453..c30d040a 100644 --- a/power_dialog/entry/src/main/module.json +++ b/power_dialog/entry/src/main/module.json @@ -2,8 +2,7 @@ "module": { "name": "entry", "type": "feature", - "srcEntrance": "./ets/Application/MyAbilityStage.ts", - "description": "$string:entry_desc", + "srcEntry": "", "mainElement": "MainAbility", "deviceTypes": [ "default", @@ -15,28 +14,27 @@ "abilities": [ { "name": "MainAbility", - "srcEntrance": "./ets/MainAbility/MainAbility.ts", - "description": "$string:MainAbility_desc", "icon": "$media:icon", "label": "$string:MainAbility_label", "startWindowIcon": "$media:icon", "startWindowBackground": "$color:white", "visible": false, - "launchType": "standard" + "launchType": "standard", + "srcEntry": "" } ], "extensionAbilities": [ { - "name": "PowerServiceExtAbility", - "srcEntrance": "./ets/ServiceExtAbility/PowerDialogAbility.ts", + "name": "PowerUiExtensionAbility", + "srcEntry": "./ets/UiExtensionAbility/PowerUiExtensionAbility.ts", "icon": "$media:icon", "label": "$string:PowerServiceExtAbility_label", "visible": false, - "type": "service" + "type": "sysDialog/common" }, { "name": "ThermalServiceExtAbility_high", - "srcEntrance": "./ets/ServiceExtAbility/ThermalHighDialogAbility.ts", + "srcEntry": "./ets/ServiceExtAbility/ThermalHighDialogAbility.ts", "icon": "$media:icon", "label": "$string:ThermalServiceExtAbility_high_label", "visible": false, @@ -44,7 +42,7 @@ }, { "name": "ThermalServiceExtAbility_low", - "srcEntrance": "./ets/ServiceExtAbility/ThermalLowDialogAbility.ts", + "srcEntry": "./ets/ServiceExtAbility/ThermalLowDialogAbility.ts", "icon": "$media:icon", "label": "$string:ThermalServiceExtAbility_low_label", "visible": false, @@ -57,7 +55,7 @@ "name": "ohos.permission.REBOOT", "usedScene": { "abilities": [ - "PowerDialogAbility" + "PowerUiExtensionAbility" ], "when": "always" } diff --git a/services/BUILD.gn b/services/BUILD.gn index 57cc422a..e8827c1e 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -48,6 +48,7 @@ ohos_shared_library("powermgrservice") { "native/src/setting/setting_helper.cpp", "native/src/shutdown/shutdown_callback_holer.cpp", "native/src/shutdown/shutdown_controller.cpp", + "native/src/shutdown/shutdown_dialog.cpp", "native/src/suspend/suspend_controller.cpp", "native/src/suspend/suspend_source_parser.cpp", "native/src/suspend/suspend_sources.cpp", diff --git a/services/native/include/power_mgr_service.h b/services/native/include/power_mgr_service.h index dbc67133..a2295b74 100644 --- a/services/native/include/power_mgr_service.h +++ b/services/native/include/power_mgr_service.h @@ -28,6 +28,7 @@ #include "power_state_machine.h" #include "running_lock_mgr.h" #include "shutdown_controller.h" +#include "shutdown_dialog.h" #include "sp_singleton.h" #include "suspend_controller.h" #include "wakeup_controller.h" @@ -85,16 +86,13 @@ public: void RegisterShutdownCallback(const sptr& callback, ShutdownPriority priority) override; void UnRegisterShutdownCallback(const sptr& callback) override; - void HandleShutdownRequest(); void HandleKeyEvent(int32_t keyCode); void HandlePointEvent(int32_t type); - void KeyMonitorInit(); void KeyMonitorCancel(); void SwitchSubscriberInit(); void SwitchSubscriberCancel(); void HallSensorSubscriberInit(); void HallSensorSubscriberCancel(); - bool ShowPowerDialog(); bool CheckDialogAndShuttingDown(); void SuspendControllerInit(); void WakeupControllerInit(); @@ -122,6 +120,10 @@ public: { return wakeupController_; } + ShutdownDialog& GetShutdownDialog() + { + return shutdownDialog_; + } bool IsServiceReady() const { return ready_; @@ -176,7 +178,6 @@ private: static const int32_t TIMEOUT = 10000; // 10seconds }; - static constexpr int32_t LONG_PRESS_DELAY_MS = 3000; static constexpr int32_t POWER_KEY_PRESS_DELAY_MS = 10000; static constexpr int32_t INIT_KEY_MONITOR_DELAY_MS = 1000; static constexpr int32_t HALL_REPORT_INTERVAL = 0; @@ -209,9 +210,8 @@ private: std::shared_ptr powerMgrNotify_; std::shared_ptr shutdownController_; PowerModeModule powerModeModule_; + ShutdownDialog shutdownDialog_; uint32_t mockCount_ {0}; - bool isDialogShown_ {false}; - int32_t powerkeyLongPressId_ {0}; int32_t switchId_ {0}; int32_t doubleClickId_ {0}; int32_t monitorId_ {0}; diff --git a/services/native/src/power_mgr_dumper.cpp b/services/native/src/power_mgr_dumper.cpp index 7b40b793..4189b4cc 100644 --- a/services/native/src/power_mgr_dumper.cpp +++ b/services/native/src/power_mgr_dumper.cpp @@ -42,11 +42,11 @@ bool PowerMgrDumper::Dump(const std::vector& args, std::string& res return true; } if (args[0] == ARGS_DIALOG) { - pms->HandleShutdownRequest(); + pms->GetShutdownDialog().ConnectSystemUi(); return true; } if (args[0] == ARGS_REG_KEY) { - pms->KeyMonitorInit(); + pms->GetShutdownDialog().KeyMonitorInit(); return true; } for (auto it = args.begin(); it != args.end(); it++) { diff --git a/services/native/src/power_mgr_service.cpp b/services/native/src/power_mgr_service.cpp index ee21b0c9..07c4a0ac 100755 --- a/services/native/src/power_mgr_service.cpp +++ b/services/native/src/power_mgr_service.cpp @@ -28,6 +28,7 @@ #include #include "ability_manager_client.h" +#include "ability_connect_callback_stub.h" #include "permission.h" #include "power_common.h" #include "power_mgr_dumper.h" @@ -122,7 +123,7 @@ void PowerMgrService::RegisterBootCompletedCallback() auto powerStateMachine = power->GetPowerStateMachine(); powerStateMachine->RegisterDisplayOffTimeObserver(); powerStateMachine->InitState(); - power->KeyMonitorInit(); + power->GetShutdownDialog().KeyMonitorInit(); power->HallSensorSubscriberInit(); power->SwitchSubscriberInit(); power->SuspendControllerInit(); @@ -149,28 +150,6 @@ bool PowerMgrService::PowerStateMachineInit() return true; } -void PowerMgrService::KeyMonitorInit() -{ - POWER_HILOGD(FEATURE_INPUT, "Initialize the long press powerkey"); - std::shared_ptr keyOption = std::make_shared(); - std::set preKeys; - - keyOption->SetPreKeys(preKeys); - keyOption->SetFinalKey(OHOS::MMI::KeyEvent::KEYCODE_POWER); - keyOption->SetFinalKeyDown(true); - keyOption->SetFinalKeyDownDuration(LONG_PRESS_DELAY_MS); - powerkeyLongPressId_ = InputManager::GetInstance()->SubscribeKeyEvent( - keyOption, [this](std::shared_ptr keyEvent) { - POWER_HILOGI(FEATURE_INPUT, "Receive long press powerkey"); - FFRTUtils::SubmitTask([this] { - HandleShutdownRequest(); - }); - }); - if (powerkeyLongPressId_ != ERROR_UNSUPPORT) { - POWER_HILOGI(FEATURE_INPUT, "SubscribeKeyEvent long press powerkey success: %{public}d", powerkeyLongPressId_); - } -} - void PowerMgrService::KeyMonitorCancel() { POWER_HILOGI(FEATURE_INPUT, "Unsubscribe key information"); @@ -179,9 +158,7 @@ void PowerMgrService::KeyMonitorCancel() POWER_HILOGI(FEATURE_INPUT, "InputManager is null"); return; } - if (powerkeyLongPressId_ >= 0) { - inputManager->UnsubscribeKeyEvent(powerkeyLongPressId_); - } + shutdownDialog_.KeyMonitorCancel(); if (doubleClickId_ >= 0) { inputManager->UnsubscribeKeyEvent(doubleClickId_); } @@ -267,42 +244,14 @@ void PowerMgrService::HallSensorSubscriberCancel() } } -bool PowerMgrService::ShowPowerDialog() -{ - POWER_HILOGD(COMP_SVC, "PowerMgrService::ShowPowerDialog start."); - auto client = AbilityManagerClient::GetInstance(); - if (client == nullptr) { - return false; - } - AAFwk::Want want; - want.SetElementName("com.ohos.powerdialog", "PowerServiceExtAbility"); - int32_t result = client->StartAbility(want); - if (result != 0) { - POWER_HILOGE(COMP_SVC, "ShowPowerDialog failed, result = %{public}d", result); - return false; - } - isDialogShown_ = true; - POWER_HILOGD(COMP_SVC, "ShowPowerDialog success."); - return true; -} - -void PowerMgrService::HandleShutdownRequest() -{ - POWER_HILOGD(FEATURE_SHUTDOWN, "HandleShutdown"); - bool showSuccess = ShowPowerDialog(); - if (!showSuccess) { - return; - } - RefreshActivity(static_cast(time(nullptr)), UserActivityType::USER_ACTIVITY_TYPE_BUTTON, false); -} - bool PowerMgrService::CheckDialogAndShuttingDown() { - bool isShuttingDown = this->shutdownController_->IsShuttingDown(); - if (isDialogShown_ || isShuttingDown) { + bool isShuttingDown = shutdownController_->IsShuttingDown(); + bool isLongPress = shutdownDialog_.IsLongPress(); + if (isLongPress || isShuttingDown) { POWER_HILOGW( - FEATURE_INPUT, "isDialogShown: %{public}d, isShuttingDown: %{public}d", isDialogShown_, isShuttingDown); - isDialogShown_ = false; + FEATURE_INPUT, "isLongPress: %{public}d, isShuttingDown: %{public}d", isLongPress, isShuttingDown); + shutdownDialog_.ResetLongPressFlag(); return true; } return false; diff --git a/services/native/src/shutdown/shutdown_dialog.cpp b/services/native/src/shutdown/shutdown_dialog.cpp new file mode 100644 index 00000000..b247178d --- /dev/null +++ b/services/native/src/shutdown/shutdown_dialog.cpp @@ -0,0 +1,187 @@ +/* + * Copyright (c) 2023 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 "shutdown_dialog.h" + +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "ffrt_utils.h" +#include "power_log.h" +#include "power_mgr_service.h" + +using namespace OHOS::MMI; +using namespace OHOS::AAFwk; + +namespace OHOS { +namespace PowerMgr { +namespace { +static constexpr int32_t LONG_PRESS_DELAY_MS = 3000; +static constexpr int32_t INVALID_USERID = -1; +static constexpr int32_t MESSAGE_PARCEL_KEY_SIZE = 3; +static constexpr int32_t INIT_LONG_PRESS_RETRY = 3; +static constexpr uint32_t RETRY_TIME = 1000; +std::atomic_bool g_isDialogShow = false; +std::atomic_bool g_longPressShow = false; +int32_t g_retryCount = 1; +FFRTQueue g_queue("shutdown_dialog"); +sptr g_remoteObject = nullptr; +} // namespace +ShutdownDialog::ShutdownDialog() : dialogConnectionCallback_(new DialogAbilityConnection()) {} + +ShutdownDialog::~ShutdownDialog() +{ + dialogConnectionCallback_ = nullptr; +} + +void ShutdownDialog::KeyMonitorInit() +{ + POWER_HILOGD(FEATURE_SHUTDOWN, "Initialize the long press powerkey"); + std::shared_ptr keyOption = std::make_shared(); + std::set preKeys; + + keyOption->SetPreKeys(preKeys); + keyOption->SetFinalKey(KeyEvent::KEYCODE_POWER); + keyOption->SetFinalKeyDown(true); + keyOption->SetFinalKeyDownDuration(LONG_PRESS_DELAY_MS); + longPressId_ = + InputManager::GetInstance()->SubscribeKeyEvent(keyOption, [this](std::shared_ptr keyEvent) { + POWER_HILOGI(FEATURE_SHUTDOWN, "Receive long press powerkey"); + ConnectSystemUi(); + }); + if (longPressId_ < ERR_OK) { + if (g_retryCount <= INIT_LONG_PRESS_RETRY) { + POWER_HILOGI(FEATURE_SHUTDOWN, "SubscribeKey long press failed errcode = %{public}d, Try again in 1 second", + longPressId_); + FFRTTask task = [this] { + KeyMonitorInit(); + }; + FFRTUtils::SubmitDelayTask(task, RETRY_TIME, g_queue); + g_retryCount++; + } + return; + } + POWER_HILOGI(FEATURE_SHUTDOWN, "SubscribeKey long press success"); +} + +void ShutdownDialog::KeyMonitorCancel() +{ + InputManager* inputManager = InputManager::GetInstance(); + if (inputManager == nullptr) { + POWER_HILOGW(FEATURE_SHUTDOWN, "InputManager is null"); + return; + } + if (longPressId_ >= ERR_OK) { + inputManager->UnsubscribeKeyEvent(longPressId_); + } + longPressId_ = 0; +} + +bool ShutdownDialog::ConnectSystemUi() +{ + if (g_isDialogShow) { + AppExecFwk::ElementName element; + dialogConnectionCallback_->OnAbilityConnectDone(element, g_remoteObject, INVALID_USERID); + POWER_HILOGW(FEATURE_SHUTDOWN, "power dialog has been show"); + return true; + } + auto ams = AbilityManagerClient::GetInstance(); + if (ams == nullptr) { + POWER_HILOGW(FEATURE_SHUTDOWN, "AbilityManagerClient is nullptr"); + return false; + } + + Want want; + want.SetElementName("com.ohos.systemui", "com.ohos.systemui.dialog"); + ErrCode result = ams->ConnectAbility(want, dialogConnectionCallback_, INVALID_USERID); + if (result != ERR_OK) { + POWER_HILOGW(FEATURE_SHUTDOWN, "ConnectAbility systemui dialog failed, result = %{public}d", result); + return false; + } + POWER_HILOGI(FEATURE_SHUTDOWN, "ConnectAbility systemui dialog success."); + return true; +} + +bool ShutdownDialog::IsLongPress() const +{ + return g_longPressShow; +} + +void ShutdownDialog::ResetLongPressFlag() +{ + g_longPressShow = false; +} + +void ShutdownDialog::DialogAbilityConnection::OnAbilityConnectDone( + const AppExecFwk::ElementName& element, const sptr& remoteObject, int resultCode) +{ + POWER_HILOGD(FEATURE_SHUTDOWN, "OnAbilityConnectDone"); + std::lock_guard lock(mutex_); + if (remoteObject == nullptr) { + POWER_HILOGW(FEATURE_SHUTDOWN, "remoteObject is nullptr"); + return; + } + if (g_remoteObject == nullptr) { + g_remoteObject = remoteObject; + } + FFRTUtils::SubmitTask([remoteObject] { + MessageParcel data; + MessageParcel reply; + MessageOption option; + data.WriteInt32(MESSAGE_PARCEL_KEY_SIZE); + data.WriteString16(u"bundleName"); + data.WriteString16(u"com.ohos.powerdialog"); + data.WriteString16(u"abilityName"); + data.WriteString16(u"PowerUiExtensionAbility"); + data.WriteString16(u"parameters"); + // sysDialogZOrder = 2 displayed on the lock screen + data.WriteString16(u"{\"ability.want.params.uiExtensionType\":\"sysDialog/common\",\"sysDialogZOrder\":2}"); + POWER_HILOGD(FEATURE_SHUTDOWN, "show power dialog is begin"); + const uint32_t cmdCode = 1; + int32_t ret = remoteObject->SendRequest(cmdCode, data, reply, option); + if (ret != ERR_OK) { + POWER_HILOGW(FEATURE_SHUTDOWN, "show power dialog is failed: %{public}d", ret); + return; + } + g_isDialogShow = true; + g_longPressShow = true; + POWER_HILOGI(FEATURE_SHUTDOWN, "show power dialog is success"); + auto pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + return; + } + pms->RefreshActivity( + static_cast(time(nullptr)), UserActivityType::USER_ACTIVITY_TYPE_ATTENTION, false); + }); +} + +void ShutdownDialog::DialogAbilityConnection::OnAbilityDisconnectDone( + const AppExecFwk::ElementName& element, int resultCode) +{ + POWER_HILOGD(FEATURE_SHUTDOWN, "OnAbilityDisconnectDone"); + std::lock_guard lock(mutex_); + g_isDialogShow = false; + g_longPressShow = false; + g_remoteObject = nullptr; +} +} // namespace PowerMgr +} // namespace OHOS diff --git a/power_dialog/entry/src/main/ets/MainAbility/MainAbility.ts b/services/native/src/shutdown/shutdown_dialog.h similarity index 33% rename from power_dialog/entry/src/main/ets/MainAbility/MainAbility.ts rename to services/native/src/shutdown/shutdown_dialog.h index 7f269ebc..437c1443 100644 --- a/power_dialog/entry/src/main/ets/MainAbility/MainAbility.ts +++ b/services/native/src/shutdown/shutdown_dialog.h @@ -1,51 +1,53 @@ -/* - * 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 Ability from '@ohos.app.ability.UIAbility'; - -let TAG = 'Dialog_MainAbility:'; - -export default class MainAbility extends Ability { - onCreate(want, launchParam) { - console.log(TAG + 'MainAbility onCreate, ability name is ' + want.abilityName + '.'); - globalThis.context = this.context; - } - - onWindowStageCreate(windowStage) { - // Main window is created, set main page for this ability - windowStage.setUIContent(this.context, 'pages/powerdialog', null); - console.log(TAG + 'MainAbility onWindowStageCreate.'); - } - - onForeground() { - // Ability has brought to foreground - console.log(TAG + 'MainAbility onForeground.'); - } - - onBackground() { - // Ability has back to background - console.log(TAG + 'MainAbility onBackground.'); - this.context.terminateSelf(); - } - - onDestroy() { - console.log(TAG + 'MainAbility onDestroy.'); - } - - onWindowStageDestroy() { - // Main window is destroyed, release UI related resources - console.log(TAG + 'MainAbility onWindowStageDestroy.'); - } -}; +/* + * Copyright (c) 2023 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_POWER_MANAGER_SHUTDOWN_DIALOG_H +#define POWERMGR_POWER_MANAGER_SHUTDOWN_DIALOG_H + +#include + +#include "ability_connect_callback_interface.h" +#include "ability_connect_callback_stub.h" + +namespace OHOS { +namespace PowerMgr { +class ShutdownDialog { +public: + ShutdownDialog(); + ~ShutdownDialog(); + void KeyMonitorInit(); + void KeyMonitorCancel(); + bool ConnectSystemUi(); + bool IsLongPress() const; + void ResetLongPressFlag(); + +private: + class DialogAbilityConnection : public OHOS::AAFwk::AbilityConnectionStub { + public: + void OnAbilityConnectDone( + const AppExecFwk::ElementName& element, const sptr& remoteObject, int resultCode) override; + void OnAbilityDisconnectDone(const AppExecFwk::ElementName& element, int resultCode) override; + + private: + std::mutex mutex_; + }; + + int32_t longPressId_ {0}; + sptr dialogConnectionCallback_ {nullptr}; +}; +} // namespace PowerMgr +} // namespace OHOS + +#endif // POWERMGR_POWER_MANAGER_SHUTDOWN_DIALOG_H -- Gitee From 5d206cfceb257df35da1fb5083af384de825cf1b Mon Sep 17 00:00:00 2001 From: ShiJie Date: Thu, 20 Jul 2023 10:18:59 +0800 Subject: [PATCH 2/2] test: Modify the dialog use case Signed-off-by: ShiJie Change-Id: I482083231b58e0279f81732480d2b02a88aa55d5 --- services/native/src/power_mgr_service.cpp | 4 +- test/apitest/inner_api/shutdown/BUILD.gn | 1 + test/fuzztest/power_fuzzer/BUILD.gn | 1 + test/systemtest/BUILD.gn | 1 + .../include/power_mgr_powerdialog_test.h | 9 +--- .../src/power_mgr_powerdialog_test.cpp | 43 +++++++------------ test/unittest/BUILD.gn | 1 + .../src/power_mgr_service_native_test.cpp | 4 -- test/unittest/src/servicetest/BUILD.gn | 1 + 9 files changed, 24 insertions(+), 41 deletions(-) diff --git a/services/native/src/power_mgr_service.cpp b/services/native/src/power_mgr_service.cpp index 07c4a0ac..bdc29097 100755 --- a/services/native/src/power_mgr_service.cpp +++ b/services/native/src/power_mgr_service.cpp @@ -27,13 +27,13 @@ #include #include -#include "ability_manager_client.h" #include "ability_connect_callback_stub.h" +#include "ability_manager_client.h" +#include "ffrt_utils.h" #include "permission.h" #include "power_common.h" #include "power_mgr_dumper.h" #include "sysparam.h" -#include "ffrt_utils.h" #include "system_suspend_controller.h" #include "xcollie/watchdog.h" diff --git a/test/apitest/inner_api/shutdown/BUILD.gn b/test/apitest/inner_api/shutdown/BUILD.gn index 424b51a6..366d9035 100644 --- a/test/apitest/inner_api/shutdown/BUILD.gn +++ b/test/apitest/inner_api/shutdown/BUILD.gn @@ -36,6 +36,7 @@ config("module_private_config") { deps_ex = [ "ability_base:base", "ability_base:want", + "ability_runtime:ability_manager", "common_event_service:cesfwk_innerkits", "ipc:ipc_core", "hilog:libhilog", diff --git a/test/fuzztest/power_fuzzer/BUILD.gn b/test/fuzztest/power_fuzzer/BUILD.gn index 9f969958..709c534f 100644 --- a/test/fuzztest/power_fuzzer/BUILD.gn +++ b/test/fuzztest/power_fuzzer/BUILD.gn @@ -47,6 +47,7 @@ ohos_fuzztest("PowerFuzzTest") { ] external_deps = [ + "ability_runtime:ability_manager", "c_utils:utils", "hilog:libhilog", "input:libmmi-client", diff --git a/test/systemtest/BUILD.gn b/test/systemtest/BUILD.gn index d53d3609..875f5268 100644 --- a/test/systemtest/BUILD.gn +++ b/test/systemtest/BUILD.gn @@ -33,6 +33,7 @@ config("module_private_config") { deps_ex = [ "ability_base:base", "ability_base:want", + "ability_runtime:ability_manager", "bundle_framework:appexecfwk_base", "bundle_framework:appexecfwk_core", "common_event_service:cesfwk_innerkits", diff --git a/test/systemtest/include/power_mgr_powerdialog_test.h b/test/systemtest/include/power_mgr_powerdialog_test.h index 6597b2ca..0ed3b4d2 100644 --- a/test/systemtest/include/power_mgr_powerdialog_test.h +++ b/test/systemtest/include/power_mgr_powerdialog_test.h @@ -17,18 +17,11 @@ #define POWER_MGR_POWERDIALOG_TEST_H #include -#include "power_mgr_service.h" namespace OHOS { namespace PowerMgr { -class PowerMgrPowerDialog : public testing::Test { -public: - static void SetUpTestCase(); - static void TearDownTestCase(); - void SetUp(); - void TearDown(); -}; +class PowerMgrPowerDialog : public testing::Test {}; } // namespace PowerMgr } // namespace OHOS #endif \ No newline at end of file diff --git a/test/systemtest/src/power_mgr_powerdialog_test.cpp b/test/systemtest/src/power_mgr_powerdialog_test.cpp index c3aabf25..b868eda3 100644 --- a/test/systemtest/src/power_mgr_powerdialog_test.cpp +++ b/test/systemtest/src/power_mgr_powerdialog_test.cpp @@ -15,41 +15,30 @@ #include "power_mgr_powerdialog_test.h" +#include + +#define private public +#define protected public +#include "shutdown_dialog.h" +#undef private +#undef protected + using namespace testing::ext; using namespace OHOS::PowerMgr; -using namespace OHOS::EventFwk; -using namespace OHOS; using namespace std; -void PowerMgrPowerDialog::SetUpTestCase() -{ - auto pms = DelayedSpSingleton::GetInstance(); - pms->OnStart(); - SystemAbility::MakeAndRegisterAbility(pms.GetRefPtr()); -} - -void PowerMgrPowerDialog::TearDownTestCase() -{ -} - -void PowerMgrPowerDialog::SetUp() -{ -} - -void PowerMgrPowerDialog::TearDown() -{ -} - namespace { /** - * @tc.name: PowerMgr_PowerDialog_001 - * @tc.desc: test pull up PowerDialog in ShowPowerDialog + * @tc.name: LongPressKeyMonitorInitTest + * @tc.desc: test KeyMonitorInit and KeyMonitorCancel * @tc.type: FUNC - * @tc.require: issueI5I9BF */ -HWTEST_F(PowerMgrPowerDialog, PowerMgr_PowerDialog_001, TestSize.Level2) +HWTEST_F(PowerMgrPowerDialog, LongPressKeyMonitorInitTest, TestSize.Level2) { - auto powerMgrService = DelayedSpSingleton::GetInstance(); - EXPECT_TRUE(powerMgrService->ShowPowerDialog()); + ShutdownDialog shutdownDialog; + shutdownDialog.KeyMonitorInit(); + EXPECT_TRUE(shutdownDialog.longPressId_ >= OHOS::ERR_OK); + shutdownDialog.KeyMonitorCancel(); + EXPECT_TRUE(shutdownDialog.longPressId_ == OHOS::ERR_OK); } } diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 8283cc27..3359b43e 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -51,6 +51,7 @@ config("module_mock_private_config") { deps_ex = [ "ability_base:base", "ability_base:want", + "ability_runtime:ability_manager", "config_policy:configpolicy_util", "common_event_service:cesfwk_innerkits", "ipc:ipc_core", diff --git a/test/unittest/src/power_mgr_service_native_test.cpp b/test/unittest/src/power_mgr_service_native_test.cpp index 77c6dda7..f4baf676 100644 --- a/test/unittest/src/power_mgr_service_native_test.cpp +++ b/test/unittest/src/power_mgr_service_native_test.cpp @@ -82,13 +82,11 @@ namespace { */ HWTEST_F(PowerMgrServiceNativeTest, PowerMgrServiceNative001, TestSize.Level0) { - g_pmsTest->KeyMonitorInit(); g_pmsTest->KeyMonitorCancel(); g_pmsTest->HallSensorSubscriberInit(); g_pmsTest->HallSensorSubscriberCancel(); - g_pmsTest->HandleShutdownRequest(); int32_t keyCode = OHOS::MMI::KeyEvent::KEYCODE_F1; g_pmsTest->HandleKeyEvent(keyCode); int32_t type = OHOS::MMI::PointerEvent::SOURCE_TYPE_MOUSE; @@ -97,11 +95,9 @@ HWTEST_F(PowerMgrServiceNativeTest, PowerMgrServiceNative001, TestSize.Level0) EXPECT_TRUE(g_pmsTest->OverrideScreenOffTime(TIMEOUTMS)); EXPECT_TRUE(g_pmsTest->RestoreScreenOffTime()); - g_pmsTest->powerkeyLongPressId_ = UNCANCELID; g_pmsTest->doubleClickId_ = UNCANCELID; g_pmsTest->monitorId_ = UNCANCELID; g_pmsTest->KeyMonitorCancel(); - g_pmsTest->KeyMonitorInit(); POWER_HILOGI(LABEL_TEST, "PowerMgrServiceNative001 end."); } diff --git a/test/unittest/src/servicetest/BUILD.gn b/test/unittest/src/servicetest/BUILD.gn index b7fdc6c4..b48da571 100644 --- a/test/unittest/src/servicetest/BUILD.gn +++ b/test/unittest/src/servicetest/BUILD.gn @@ -46,6 +46,7 @@ config("module_mock_private_config") { deps_ex = [ "ability_base:base", "ability_base:want", + "ability_runtime:ability_manager", "common_event_service:cesfwk_innerkits", "ipc:ipc_core", "hilog:libhilog", -- Gitee