1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "modal_system_dialog/modal_system_dialog_ui_extension.h"
17
18 #include "ability_manager_client.h"
19 #include "hilog_tag_wrapper.h"
20 #include "in_process_call_wrapper.h"
21 #include "scene_board_judgement.h"
22
23 namespace OHOS {
24 namespace AbilityRuntime {
25 namespace {
26 constexpr int32_t INVALID_USERID = -1;
27 constexpr int32_t MESSAGE_PARCEL_KEY_SIZE = 1;
28 constexpr const char* SYSTEM_SCENEBOARD_BUNDLE_NAME = "com.ohos.sceneboard";
29 constexpr const char* SYSTEM_SCENEBOARD_ABILITY_NAME = "com.ohos.sceneboard.systemdialog";
30 }
31
CreateModalUIExtension(const std::string & commandStr)32 bool ModalSystemDialogUIExtension::CreateModalUIExtension(const std::string &commandStr)
33 {
34 TAG_LOGD(AAFwkTag::ABILITYMGR, "called");
35 sptr<DialogConnection> connectionCallback(new (std::nothrow) DialogConnection(commandStr));
36 if (connectionCallback == nullptr) {
37 TAG_LOGE(AAFwkTag::ABILITYMGR, "null callback");
38 return false;
39 }
40 auto abilityManagerClient = AAFwk::AbilityManagerClient::GetInstance();
41 if (abilityManagerClient == nullptr) {
42 TAG_LOGE(AAFwkTag::ABILITYMGR, "null abilityManagerClient");
43 return false;
44 }
45 AAFwk::Want systemUIWant;
46 systemUIWant.SetElementName(SYSTEM_SCENEBOARD_BUNDLE_NAME, SYSTEM_SCENEBOARD_ABILITY_NAME);
47
48 auto result =
49 IN_PROCESS_CALL(abilityManagerClient->ConnectAbility(systemUIWant, connectionCallback, INVALID_USERID));
50 if (result != ERR_OK) {
51 TAG_LOGE(AAFwkTag::ABILITYMGR, "connect ability failed, result: %{public}d", result);
52 return false;
53 }
54 return true;
55 }
56
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remote,int resultCode)57 void ModalSystemDialogUIExtension::DialogConnection::OnAbilityConnectDone(
58 const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remote, int resultCode)
59 {
60 TAG_LOGD(AAFwkTag::ABILITYMGR, "called");
61 if (remote == nullptr) {
62 TAG_LOGE(AAFwkTag::ABILITYMGR, "null remote");
63 return;
64 }
65
66 MessageParcel data;
67 MessageParcel reply;
68 MessageOption option;
69 if (!data.WriteInt32(MESSAGE_PARCEL_KEY_SIZE)) {
70 TAG_LOGE(AAFwkTag::ABILITYMGR, "write MESSAGE_PARCEL_KEY_SIZE fail");
71 return;
72 }
73 if (!data.WriteString16(u"parameters")) {
74 TAG_LOGE(AAFwkTag::ABILITYMGR, "write parameters fail");
75 return;
76 }
77 if (!data.WriteString16(Str8ToStr16(commandStr_))) {
78 TAG_LOGE(AAFwkTag::ABILITYMGR, "write commandStr_ fail");
79 return;
80 }
81 auto ret = remote->SendRequest(AAFwk::IAbilityConnection::ON_CONNECT_SYSTEM_COMMON_DIALOG, data, reply, option);
82 if (ret != ERR_OK) {
83 TAG_LOGE(AAFwkTag::ABILITYMGR, "show dialog failed");
84 } else {
85 TAG_LOGI(AAFwkTag::ABILITYMGR, "show system dialog successed");
86 }
87 }
88
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int resultCode)89 void ModalSystemDialogUIExtension::DialogConnection::OnAbilityDisconnectDone(
90 const AppExecFwk::ElementName &element, int resultCode)
91 {
92 TAG_LOGD(AAFwkTag::ABILITYMGR, "called");
93 }
94 } // namespace AbilityRuntime
95 } // namespace OHOS