1 /*
2 * Copyright (c) 2023 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 "authorization_dialog.h"
17
18 #include "ability_manager_client.h"
19 #ifdef OHOS_BUILD_ENABLE_DFX_RADAR
20 #include "dfx_hisysevent.h"
21 #endif // OHOS_BUILD_ENABLE_DFX_RADAR
22
23 #include "mmi_log.h"
24
25 #undef MMI_LOG_DOMAIN
26 #define MMI_LOG_DOMAIN MMI_LOG_SERVER
27 #undef MMI_LOG_TAG
28 #define MMI_LOG_TAG "AuthorizationDialog"
29
30 namespace OHOS {
31 namespace MMI {
32 namespace {
33 constexpr int32_t INVALID_USERID { -1 };
34 constexpr int32_t MESSAGE_PARCEL_KEY_SIZE { 3 };
35 }
36
37 std::string AuthorizationDialog::bundleName_ = "com.ohos.powerdialog";
38 std::string AuthorizationDialog::abilityName_ = "PowerUiExtensionAbility";
39 std::string AuthorizationDialog::uiExtensionType_ = "sysDialog/power";
40
AuthorizationDialog()41 AuthorizationDialog::AuthorizationDialog() : dialogConnectionCallback_(new DialogAbilityConnection()) {}
42
~AuthorizationDialog()43 AuthorizationDialog::~AuthorizationDialog()
44 {
45 CALL_DEBUG_ENTER;
46 CloseDialog();
47 dialogConnectionCallback_ = nullptr;
48 }
49
ConnectSystemUi()50 bool AuthorizationDialog::ConnectSystemUi()
51 {
52 CALL_DEBUG_ENTER;
53 CHKPR(dialogConnectionCallback_, false);
54 if (dialogConnectionCallback_->DialogIsOpen()) {
55 MMI_HILOGW("Power dialog has been show");
56 return true;
57 }
58
59 if (dialogConnectionCallback_->IsConnected()) {
60 MMI_HILOGW("Dialog reopens");
61 dialogConnectionCallback_->OpenDialog();
62 return true;
63 }
64 MMI_HILOGI("ConnectAbility systemui beigin");
65 auto abilityMgr = AAFwk::AbilityManagerClient::GetInstance();
66 CHKPF(abilityMgr);
67
68 AAFwk::Want want;
69 want.SetElementName("com.ohos.sceneboard", "com.ohos.sceneboard.systemdialog");
70 auto begin = std::chrono::high_resolution_clock::now();
71 ErrCode result = abilityMgr->ConnectAbility(want, dialogConnectionCallback_, INVALID_USERID);
72 auto durationMS = std::chrono::duration_cast<std::chrono::milliseconds>(
73 std::chrono::high_resolution_clock::now() - begin).count();
74 #ifdef OHOS_BUILD_ENABLE_DFX_RADAR
75 DfxHisysevent::ReportApiCallTimes(ApiDurationStatistics::Api::ABILITY_MGR_CONNECT_ABILITY, durationMS);
76 #endif // OHOS_BUILD_ENABLE_DFX_RADAR
77 if (result != ERR_OK) {
78 MMI_HILOGW("ConnectAbility systemui dialog failed, result:%{public}d", result);
79 return false;
80 }
81 MMI_HILOGI("ConnectAbility systemui dialog success");
82 return true;
83 }
84
CloseDialog()85 void AuthorizationDialog::CloseDialog()
86 {
87 CALL_DEBUG_ENTER;
88 dialogConnectionCallback_->CloseDialog();
89 }
90
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int resultCode)91 void AuthorizationDialog::DialogAbilityConnection::OnAbilityConnectDone(
92 const AppExecFwk::ElementName& element, const sptr<IRemoteObject>& remoteObject, int resultCode)
93 {
94 CALL_DEBUG_ENTER;
95 std::lock_guard lock(mutex_);
96 CHKPV(remoteObject);
97 if (remoteObject_ == nullptr) {
98 remoteObject_ = remoteObject;
99 }
100 ffrt::submit([&] {
101 this->OpenDialog();
102 });
103 }
104
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int resultCode)105 void AuthorizationDialog::DialogAbilityConnection::OnAbilityDisconnectDone(
106 const AppExecFwk::ElementName& element, int resultCode)
107 {
108 CALL_DEBUG_ENTER;
109 std::lock_guard lock(mutex_);
110 remoteObject_ = nullptr;
111 // disconnted window must be shutdown
112 isDialogShow_ = false;
113 }
114
CloseDialog()115 void AuthorizationDialog::DialogAbilityConnection::CloseDialog()
116 {
117 CALL_DEBUG_ENTER;
118 std::lock_guard lock(mutex_);
119 CHKPV(remoteObject_);
120 if (!isDialogShow_) {
121 MMI_HILOGI("Has closed!");
122 return;
123 }
124 MessageParcel data;
125 MessageParcel reply;
126 MessageOption option;
127 const uint32_t cmdCode = 3;
128 int32_t ret = remoteObject_->SendRequest(cmdCode, data, reply, option);
129 int32_t replyCode = -1;
130 bool success = false;
131 if (ret == ERR_OK) {
132 success = reply.ReadInt32(replyCode);
133 }
134 isDialogShow_ = false;
135 MMI_HILOGI("CloseDialog: ret=%d, %d, %d", ret, success, replyCode);
136 }
137
OpenDialog()138 void AuthorizationDialog::DialogAbilityConnection::OpenDialog()
139 {
140 CALL_DEBUG_ENTER;
141 MessageParcel data;
142 MessageParcel reply;
143 MessageOption option;
144 data.WriteInt32(MESSAGE_PARCEL_KEY_SIZE);
145 data.WriteString16(u"bundleName");
146 data.WriteString16(Str8ToStr16(AuthorizationDialog::GetBundleName()));
147 data.WriteString16(u"abilityName");
148 data.WriteString16(Str8ToStr16(AuthorizationDialog::GetAbilityName()));
149 data.WriteString16(u"parameters");
150 std::string midStr = "\"";
151 std::string paramStr = "{\"ability.want.params.uiExtensionType\":"+ midStr +
152 AuthorizationDialog::GetUiExtensionType() + midStr + ",\"sysDialogZOrder\":2,\"isInputDlg\":true}";
153 data.WriteString16(Str8ToStr16(paramStr));
154 MMI_HILOGI("Show power dialog is begin");
155 const uint32_t cmdCode = 1;
156 std::lock_guard lock(mutex_);
157 CHKPV(remoteObject_);
158 int32_t ret = remoteObject_->SendRequest(cmdCode, data, reply, option);
159 if (ret != ERR_OK) {
160 MMI_HILOGW("Show power dialog is failed:%{public}d", ret);
161 return;
162 }
163 isDialogShow_ = true;
164 MMI_HILOGI("Show power dialog is success");
165 }
166
DialogIsOpen()167 bool AuthorizationDialog::DialogAbilityConnection::DialogIsOpen()
168 {
169 CALL_DEBUG_ENTER;
170 std::lock_guard lock(mutex_);
171 return isDialogShow_;
172 }
173
IsConnected()174 bool AuthorizationDialog::DialogAbilityConnection::IsConnected()
175 {
176 std::lock_guard lock(mutex_);
177 if (remoteObject_ != nullptr) {
178 return true;
179 }
180 return false;
181 }
182 } // namespace MMI
183 } // namespace OHOS