• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     CHKPF(dialogConnectionCallback_);
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     CHKPV(dialogConnectionCallback_);
89     dialogConnectionCallback_->CloseDialog();
90 }
91 
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int resultCode)92 void AuthorizationDialog::DialogAbilityConnection::OnAbilityConnectDone(
93     const AppExecFwk::ElementName& element, const sptr<IRemoteObject>& remoteObject, int resultCode)
94 {
95     CALL_DEBUG_ENTER;
96     std::lock_guard lock(mutex_);
97     CHKPV(remoteObject);
98     if (remoteObject_ == nullptr) {
99         remoteObject_ = remoteObject;
100     }
101     ffrt::submit([&] {
102         this->OpenDialog();
103     });
104 }
105 
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int resultCode)106 void AuthorizationDialog::DialogAbilityConnection::OnAbilityDisconnectDone(
107     const AppExecFwk::ElementName& element, int resultCode)
108 {
109     CALL_DEBUG_ENTER;
110     std::lock_guard lock(mutex_);
111     remoteObject_ = nullptr;
112     // disconnted window must be shutdown
113     isDialogShow_ = false;
114 }
115 
CloseDialog()116 void AuthorizationDialog::DialogAbilityConnection::CloseDialog()
117 {
118     CALL_DEBUG_ENTER;
119     std::lock_guard lock(mutex_);
120     CHKPV(remoteObject_);
121     if (!isDialogShow_) {
122         MMI_HILOGI("Has closed!");
123         return;
124     }
125     MessageParcel data;
126     MessageParcel reply;
127     MessageOption option;
128     const uint32_t cmdCode = 3;
129     int32_t ret = remoteObject_->SendRequest(cmdCode, data, reply, option);
130     int32_t replyCode = -1;
131     bool success = false;
132     if (ret == ERR_OK) {
133         success = reply.ReadInt32(replyCode);
134     }
135     isDialogShow_ = false;
136     MMI_HILOGI("CloseDialog: ret=%d, %d, %d", ret, success, replyCode);
137 }
138 
OpenDialog()139 void AuthorizationDialog::DialogAbilityConnection::OpenDialog()
140 {
141     CALL_DEBUG_ENTER;
142     MessageParcel data;
143     MessageParcel reply;
144     MessageOption option;
145     data.WriteInt32(MESSAGE_PARCEL_KEY_SIZE);
146     data.WriteString16(u"bundleName");
147     data.WriteString16(Str8ToStr16(AuthorizationDialog::GetBundleName()));
148     data.WriteString16(u"abilityName");
149     data.WriteString16(Str8ToStr16(AuthorizationDialog::GetAbilityName()));
150     data.WriteString16(u"parameters");
151     std::string midStr = "\"";
152     std::string paramStr = "{\"ability.want.params.uiExtensionType\":"+ midStr +
153         AuthorizationDialog::GetUiExtensionType() + midStr + ",\"sysDialogZOrder\":2,\"isInputDlg\":true}";
154     data.WriteString16(Str8ToStr16(paramStr));
155     MMI_HILOGI("Show power dialog is begin");
156     const uint32_t cmdCode = 1;
157     std::lock_guard lock(mutex_);
158     CHKPV(remoteObject_);
159     int32_t ret = remoteObject_->SendRequest(cmdCode, data, reply, option);
160     if (ret != ERR_OK) {
161         MMI_HILOGW("Show power dialog is failed:%{public}d", ret);
162         return;
163     }
164     isDialogShow_ = true;
165     MMI_HILOGI("Show power dialog is success");
166 }
167 
DialogIsOpen()168 bool AuthorizationDialog::DialogAbilityConnection::DialogIsOpen()
169 {
170     CALL_DEBUG_ENTER;
171     std::lock_guard lock(mutex_);
172     return isDialogShow_;
173 }
174 
IsConnected()175 bool AuthorizationDialog::DialogAbilityConnection::IsConnected()
176 {
177     std::lock_guard lock(mutex_);
178     if (remoteObject_ != nullptr) {
179         return true;
180     }
181     return false;
182 }
183 } // namespace MMI
184 } // namespace OHOS