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 <atomic>
19
20 #include "ability_manager_client.h"
21 #include "message_parcel.h"
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 std::atomic_bool g_isDialogShow = false;
36 sptr<IRemoteObject> g_remoteObject = nullptr;
37 }
38
39 std::string AuthorizationDialog::bundleName_ = "com.ohos.powerdialog";
40 std::string AuthorizationDialog::abilityName_ = "PowerUiExtensionAbility";
41 std::string AuthorizationDialog::uiExtensionType_ = "sysDialog/power";
42
AuthorizationDialog()43 AuthorizationDialog::AuthorizationDialog() : dialogConnectionCallback_(new DialogAbilityConnection()) {}
44
~AuthorizationDialog()45 AuthorizationDialog::~AuthorizationDialog()
46 {
47 dialogConnectionCallback_ = nullptr;
48 }
49
ConnectSystemUi()50 bool AuthorizationDialog::ConnectSystemUi()
51 {
52 CHKPR(dialogConnectionCallback_, false);
53 if (g_isDialogShow) {
54 AppExecFwk::ElementName element;
55 dialogConnectionCallback_->OnAbilityConnectDone(element, g_remoteObject, INVALID_USERID);
56 MMI_HILOGW("Power dialog has been show");
57 return true;
58 }
59 auto abilityMgr = AAFwk::AbilityManagerClient::GetInstance();
60 CHKPF(abilityMgr);
61
62 AAFwk::Want want;
63 want.SetElementName("com.ohos.systemui", "com.ohos.systemui.dialog");
64 ErrCode result = abilityMgr->ConnectAbility(want, dialogConnectionCallback_, INVALID_USERID);
65 if (result != ERR_OK) {
66 MMI_HILOGW("ConnectAbility systemui dialog failed, result:%{public}d", result);
67 return false;
68 }
69 MMI_HILOGI("ConnectAbility systemui dialog success");
70 return true;
71 }
72
OnAbilityConnectDone( const AppExecFwk::ElementName& element, const sptr<IRemoteObject>& remoteObject, int resultCode)73 void AuthorizationDialog::DialogAbilityConnection::OnAbilityConnectDone(
74 const AppExecFwk::ElementName& element, const sptr<IRemoteObject>& remoteObject, int resultCode)
75 {
76 CALL_DEBUG_ENTER;
77 std::lock_guard lock(mutex_);
78 CHKPV(remoteObject);
79 if (g_remoteObject == nullptr) {
80 g_remoteObject = remoteObject;
81 }
82 ffrt::submit([remoteObject] {
83 MessageParcel data;
84 MessageParcel reply;
85 MessageOption option;
86 data.WriteInt32(MESSAGE_PARCEL_KEY_SIZE);
87 data.WriteString16(u"bundleName");
88 data.WriteString16(Str8ToStr16(AuthorizationDialog::GetBundleName()));
89 data.WriteString16(u"abilityName");
90 data.WriteString16(Str8ToStr16(AuthorizationDialog::GetAbilityName()));
91 data.WriteString16(u"parameters");
92 std::string midStr = "\"";
93 std::string paramStr = "{\"ability.want.params.uiExtensionType\":"+ midStr +
94 AuthorizationDialog::GetUiExtensionType() + midStr + ",\"sysDialogZOrder\":2,\"isInputDlg\":true}";
95 data.WriteString16(Str8ToStr16(paramStr));
96 MMI_HILOGI("Show power dialog is begin");
97 const uint32_t cmdCode = 1;
98 int32_t ret = remoteObject->SendRequest(cmdCode, data, reply, option);
99 if (ret != ERR_OK) {
100 MMI_HILOGW("Show power dialog is failed:%{public}d", ret);
101 return;
102 }
103 g_isDialogShow = true;
104 MMI_HILOGI("Show power dialog is success");
105 });
106 }
107
OnAbilityDisconnectDone( const AppExecFwk::ElementName& element, int resultCode)108 void AuthorizationDialog::DialogAbilityConnection::OnAbilityDisconnectDone(
109 const AppExecFwk::ElementName& element, int resultCode)
110 {
111 CALL_DEBUG_ENTER;
112 std::lock_guard lock(mutex_);
113 g_isDialogShow = false;
114 g_remoteObject = nullptr;
115 }
116 } // namespace MMI
117 } // namespace OHOS