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 "dm_dialog_manager.h"
17
18 #include <pthread.h>
19 #include <thread>
20
21 #include "ability_manager_client.h"
22 #include "auth_message_processor.h"
23 #include "dm_anonymous.h"
24 #include "dm_log.h"
25 #include "json_object.h"
26 #include "parameter.h"
27 #include "dm_single_instance.h"
28 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
29 #include "ffrt.h"
30 #endif
31
32 namespace OHOS {
33 namespace DistributedHardware {
34 static constexpr int32_t INVALID_USERID = -1;
35 static constexpr int32_t MESSAGE_PARCEL_KEY_SIZE = 3;
36 static constexpr int32_t WINDOW_LEVEL_UPPER = 2;
37 static constexpr int32_t WINDOW_LEVEL_DEFAULT = 1;
38 constexpr const char* CONNECT_PIN_DIALOG = "pinDialog";
39 constexpr const char* DM_UI_BUNDLE_NAME = "com.ohos.devicemanagerui";
40 constexpr const char* CONFIRM_ABILITY_NAME = "com.ohos.devicemanagerui.ConfirmUIExtAbility";
41 constexpr const char* PIN_ABILITY_NAME = "com.ohos.devicemanagerui.PincodeUIExtAbility";
42 constexpr const char* INPUT_ABILITY_NAME = "com.ohos.devicemanagerui.InputUIExtAbility";
43
44 std::string DmDialogManager::bundleName_ = "";
45 std::string DmDialogManager::abilityName_ = "";
46 std::string DmDialogManager::deviceName_ = "";
47 std::string DmDialogManager::appOperationStr_ = "";
48 std::string DmDialogManager::customDescriptionStr_ = "";
49 std::string DmDialogManager::targetDeviceName_ = "";
50 std::string DmDialogManager::pinCode_ = "";
51 std::string DmDialogManager::hostPkgLabel_ = "";
52 int32_t DmDialogManager::deviceType_ = -1;
53 DmDialogManager DmDialogManager::dialogMgr_;
54 sptr<OHOS::AAFwk::IAbilityConnection> DmDialogManager::dialogConnectionCallback_(
55 new (std::nothrow) DialogAbilityConnection());
56
57 std::atomic<bool> DmDialogManager::isConnectSystemUI_(false);
58 sptr<IRemoteObject> g_remoteObject = nullptr;
59
DmDialogManager()60 DmDialogManager::DmDialogManager()
61 {
62 LOGI("DmDialogManager constructor");
63 }
64
~DmDialogManager()65 DmDialogManager::~DmDialogManager()
66 {
67 LOGI("DmDialogManager destructor");
68 }
69
GetInstance()70 DmDialogManager &DmDialogManager::GetInstance()
71 {
72 return dialogMgr_;
73 }
74
ShowConfirmDialog(const std::string param)75 void DmDialogManager::ShowConfirmDialog(const std::string param)
76 {
77 std::string deviceName = "";
78 std::string appOperationStr = "";
79 std::string customDescriptionStr = "";
80 std::string hostPkgLabel = "";
81 int32_t deviceType = -1;
82 JsonObject jsonObject(param);
83 if (!jsonObject.IsDiscarded()) {
84 if (IsString(jsonObject, TAG_REQUESTER)) {
85 deviceName = jsonObject[TAG_REQUESTER].Get<std::string>();
86 }
87 if (IsString(jsonObject, TAG_APP_OPERATION)) {
88 appOperationStr = jsonObject[TAG_APP_OPERATION].Get<std::string>();
89 }
90 if (IsString(jsonObject, TAG_CUSTOM_DESCRIPTION)) {
91 customDescriptionStr = jsonObject[TAG_CUSTOM_DESCRIPTION].Get<std::string>();
92 }
93 if (IsInt32(jsonObject, TAG_LOCAL_DEVICE_TYPE)) {
94 deviceType = jsonObject[TAG_LOCAL_DEVICE_TYPE].Get<std::int32_t>();
95 }
96 if (IsString(jsonObject, TAG_HOST_PKGLABEL)) {
97 hostPkgLabel = jsonObject[TAG_HOST_PKGLABEL].Get<std::string>();
98 }
99 }
100
101 bundleName_ = DM_UI_BUNDLE_NAME;
102 abilityName_ = CONFIRM_ABILITY_NAME;
103 deviceName_ = deviceName;
104 appOperationStr_ = appOperationStr;
105 customDescriptionStr_ = customDescriptionStr;
106 deviceType_ = deviceType;
107 hostPkgLabel_ = hostPkgLabel;
108 ConnectExtension();
109 }
110
ShowPinDialog(const std::string param)111 void DmDialogManager::ShowPinDialog(const std::string param)
112 {
113 bundleName_ = DM_UI_BUNDLE_NAME;
114 abilityName_ = PIN_ABILITY_NAME;
115 pinCode_ = param;
116 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
117 ffrt::submit([]() { ConnectExtension(); });
118 #else
119 std::thread pinDilog([]() { ConnectExtension(); });
120 int32_t ret = pthread_setname_np(pinDilog.native_handle(), CONNECT_PIN_DIALOG.c_str());
121 if (ret != DM_OK) {
122 LOGE("pinDilog setname failed.");
123 }
124 pinDilog.detach();
125 #endif
126 }
127
ShowInputDialog(const std::string param)128 void DmDialogManager::ShowInputDialog(const std::string param)
129 {
130 targetDeviceName_ = param;
131 bundleName_ = DM_UI_BUNDLE_NAME;
132 abilityName_ = INPUT_ABILITY_NAME;
133 ConnectExtension();
134 }
135
ConnectExtension()136 void DmDialogManager::ConnectExtension()
137 {
138 LOGI("DmDialogManager::ConnectExtension start.");
139 if (isConnectSystemUI_.load() && dialogConnectionCallback_ != nullptr && g_remoteObject != nullptr) {
140 AppExecFwk::ElementName element;
141 dialogConnectionCallback_->OnAbilityConnectDone(element, g_remoteObject, INVALID_USERID);
142 LOGI("DmDialogManager::ConnectExtension dialog has been show.");
143 return;
144 }
145 AAFwk::Want want;
146 std::string bundleName = "com.ohos.sceneboard";
147 std::string abilityName = "com.ohos.sceneboard.systemdialog";
148 want.SetElementName(bundleName, abilityName);
149 auto abilityManager = AAFwk::AbilityManagerClient::GetInstance();
150 if (abilityManager == nullptr) {
151 LOGE("AbilityManagerClient is nullptr");
152 return;
153 }
154
155 LOGI("DmDialogManager::ConnectExtension abilityManager ConnectAbility begin.");
156 auto ret = abilityManager->ConnectAbility(want, dialogConnectionCallback_, INVALID_USERID);
157 if (ret != ERR_OK) {
158 LOGE("ConnectExtensionAbility sceneboard failed.");
159 bundleName = "com.ohos.systemui";
160 abilityName = "com.ohos.systemui.dialog";
161 want.SetElementName(bundleName, abilityName);
162 ret = abilityManager->ConnectAbility(want, dialogConnectionCallback_, INVALID_USERID);
163 if (ret != ERR_OK) {
164 LOGE("ConnectExtensionAbility systemui failed.");
165 }
166 }
167 }
168
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int resultCode)169 void DmDialogManager::DialogAbilityConnection::OnAbilityConnectDone(
170 const AppExecFwk::ElementName& element, const sptr<IRemoteObject>& remoteObject, int resultCode)
171 {
172 LOGI("OnAbilityConnectDone");
173 std::unique_lock<std::mutex> lock(mutex_);
174 if (remoteObject == nullptr) {
175 LOGE("remoteObject is nullptr");
176 return;
177 }
178 if (g_remoteObject == nullptr) {
179 g_remoteObject = remoteObject;
180 }
181 MessageParcel data;
182 MessageParcel reply;
183 MessageOption option;
184 data.WriteInt32(MESSAGE_PARCEL_KEY_SIZE);
185 data.WriteString16(u"bundleName");
186 data.WriteString16(Str8ToStr16(DmDialogManager::GetBundleName()));
187 data.WriteString16(u"abilityName");
188 data.WriteString16(Str8ToStr16(DmDialogManager::GetAbilityName()));
189 data.WriteString16(u"parameters");
190 JsonObject param;
191 param["ability.want.params.uiExtensionType"] = "sysDialog/common";
192 param["sysDialogZOrder"] = WINDOW_LEVEL_DEFAULT;
193 if (DmDialogManager::GetAbilityName() == INPUT_ABILITY_NAME) {
194 param["sysDialogZOrder"] = WINDOW_LEVEL_UPPER;
195 }
196 param["pinCode"] = DmDialogManager::GetPinCode();
197 param["deviceName"] = DmDialogManager::GetDeviceName();
198 param["appOperationStr"] = DmDialogManager::GetAppOperationStr();
199 param["customDescriptionStr"] = DmDialogManager::GetCustomDescriptionStr();
200 param["deviceType"] = DmDialogManager::GetDeviceType();
201 param[TAG_TARGET_DEVICE_NAME] = DmDialogManager::GetTargetDeviceName();
202 param[TAG_HOST_PKGLABEL] = DmDialogManager::GetHostPkgLabel();
203 param["disableUpGesture"] = 1;
204 std::string paramStr = SafetyDump(param);
205 data.WriteString16(Str8ToStr16(paramStr));
206 LOGI("show dm dialog is begin");
207 const uint32_t cmdCode = 1;
208 int32_t ret = remoteObject->SendRequest(cmdCode, data, reply, option);
209 if (ret != ERR_OK) {
210 LOGE("show dm dialog is failed: %{public}d", ret);
211 return;
212 }
213 isConnectSystemUI_.store(true);
214 LOGI("show dm dialog is success");
215 }
216
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int resultCode)217 void DmDialogManager::DialogAbilityConnection::OnAbilityDisconnectDone(
218 const AppExecFwk::ElementName& element, int resultCode)
219 {
220 LOGI("OnAbilityDisconnectDone");
221 std::lock_guard<std::mutex> lock(mutex_);
222 g_remoteObject = nullptr;
223 isConnectSystemUI_.store(false);
224 }
225 }
226 }